comparison org.eclipse.draw2d/src/org/eclipse/draw2d/text/ParagraphTextLayout.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.text.ParagraphTextLayout;
14
15 import java.lang.all;
16
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.draw2d.text.TextLayout;
19 import org.eclipse.draw2d.text.TextFlow;
20 import org.eclipse.draw2d.text.FlowUtilities;
21 import org.eclipse.draw2d.text.FlowContext;
22 import org.eclipse.draw2d.text.TextFragmentBox;
23 import org.eclipse.draw2d.text.FlowBorder;
24
25 /**
26 * The layout for {@link TextFlow}.
27 * @author hudsonr
28 * @since 2.1
29 */
30 public class ParagraphTextLayout
31 : TextLayout
32 {
33
34 /**
35 * Wrapping will ONLY occur at valid line breaks
36 */
37 public static const int WORD_WRAP_HARD = 0;
38
39 /**
40 * Wrapping will always occur at the end of the available space, breaking in the middle of
41 * a word.
42 */
43 public static const int WORD_WRAP_SOFT = 1;
44
45 /**
46 * Wrapping will always occur at the end of available space, truncating a word if it
47 * doesn't fit. Note that truncation is not supported across multiple figures and
48 * with BiDi. Undesired effects may result if that is the case.
49 */
50 public static const int WORD_WRAP_TRUNCATE = 2;
51
52 private int wrappingStyle = WORD_WRAP_HARD;
53
54 /**
55 * Constructs a new ParagraphTextLayout on the specified TextFlow.
56 * @param flow the TextFlow
57 */
58 public this(TextFlow flow) {
59 super(flow);
60 }
61
62 /**
63 * Constructs the layout with the specified TextFlow and wrapping style. The wrapping
64 * style must be one of:
65 * <UL>
66 * <LI>{@link #WORD_WRAP_HARD}</LI>
67 * <LI>{@link #WORD_WRAP_SOFT}</LI>
68 * <LI>{@link #WORD_WRAP_TRUNCATE}</LI>
69 * </UL>
70 * @param flow the textflow
71 * @param style the style of wrapping
72 */
73 public this(TextFlow flow, int style) {
74 this(flow);
75 wrappingStyle = style;
76 }
77
78 /**
79 * Given the Bidi levels of the given text, this method breaks the given text up by
80 * its level runs.
81 * @param text the String that needs to be broken up into its level runs
82 * @param levelInfo the Bidi levels
83 * @return the requested segment
84 */
85 private String[] getSegments(String text, int levelInfo[]) {
86 if (levelInfo.length is 1)
87 return [text];
88
89 String result[] = new String[levelInfo.length / 2 + 1];
90
91 int i;
92 int endOffset;
93 int beginOffset = 0;
94
95 for (i = 0; i < result.length - 1; i++) {
96 endOffset = levelInfo[i * 2 + 1];
97 result[i] = text.substring(beginOffset, endOffset);
98 beginOffset = endOffset;
99 }
100 endOffset = text.length;
101 result[i] = text.substring(beginOffset, endOffset);
102 return result;
103 }
104
105 class SegmentLookahead : /+FlowUtilities.+/LookAhead {
106 private int seg = -1;
107 private String segs[];
108 private int[] width;
109 private final int trailingBorderSize;
110 this(String segs[], int trailingBorderSize) {
111 this.segs = segs;
112 this.trailingBorderSize = trailingBorderSize;
113 }
114 public int getWidth() {
115 if (width is null) {
116 width = new int[1];
117 int startingIndex = seg + 1;
118 TextFlow textFlow = cast(TextFlow)getFlowFigure();
119
120 if (startingIndex is segs.length) {
121 width[0] += trailingBorderSize;
122 getContext().getWidthLookahead(textFlow, width);
123 } else {
124 String rest = segs[startingIndex];
125 for (int k = startingIndex + 1; k < segs.length; k++)
126 rest ~= segs[k];
127 if (!textFlow.addLeadingWordWidth(rest, width)) {
128 width[0] += trailingBorderSize;
129 getContext().getWidthLookahead(textFlow, width);
130 }
131 }
132 }
133 return width[0];
134 }
135 public void setIndex(int value) {
136 this.seg = value;
137 width = null;
138 }
139 }
140
141 /**
142 * @see org.eclipse.draw2d.text.FlowFigureLayout#layout()
143 */
144 protected void layout() {
145 TextFlow textFlow = cast(TextFlow)getFlowFigure();
146 int offset = 0;
147
148 FlowContext context = getContext();
149 List fragments = textFlow.getFragments();
150 Font font = textFlow.getFont();
151 int fragIndex = 0;
152 int advance = 0;
153
154 TextFragmentBox fragment;
155 int levelInfo[] = (textFlow.getBidiInfo() is null)
156 ? [-1]
157 : textFlow.getBidiInfo().levelInfo;
158
159 String segment;
160 String[] segments = getSegments(textFlow.getText(), levelInfo);
161 FlowBorder border = null;
162 if ( null !is cast(FlowBorder)textFlow.getBorder() )
163 border = cast(FlowBorder)textFlow.getBorder();
164
165 SegmentLookahead lookahead = new SegmentLookahead(segments, border is null ? 0 : border.getRightMargin());
166 int seg;
167
168 if (border !is null) {
169 fragment = getFragment(fragIndex++, fragments);
170 fragment.setBidiLevel(levelInfo[0]);
171 fragment.setTruncated(false);
172 fragment.offset = fragment.length = -1;
173 fragment.setWidth(border.getLeftMargin() + border.getInsets(textFlow).left);
174 if (context.getRemainingLineWidth()
175 < fragment.getWidth() + lookahead.getWidth())
176 context.endLine();
177 context.addToCurrentLine(fragment);
178 }
179
180 FlowUtilities flowUtilities = textFlow.getFlowUtilities_package();
181 for (seg = 0; seg < segments.length; seg++) {
182 segment = segments[seg];
183 lookahead.setIndex(seg);
184
185 do {
186 fragment = getFragment(fragIndex++, fragments);
187
188 fragment.offset = offset;
189 fragment.setBidiLevel(levelInfo[seg * 2]);
190
191 advance = flowUtilities.wrapFragmentInContext_package(fragment, segment,
192 context, lookahead, font, wrappingStyle);
193 segment = segment.substring(advance);
194 offset += advance;
195 if ((segment.length > 0
196 || fragment.length < advance)
197 || fragment.isTruncated())
198 context.endLine();
199 } while (segment.length > 0
200 || (!fragment.isTruncated() && fragment.length < advance));
201 }
202
203 if (border !is null) {
204 fragment = getFragment(fragIndex++, fragments);
205 fragment.setBidiLevel(levelInfo[0]);
206 fragment.setTruncated(false);
207 fragment.offset = fragment.length = -1;
208 fragment.setWidth(border.getRightMargin() + border.getInsets(textFlow).right);
209 context.addToCurrentLine(fragment);
210 }
211
212 //Remove the remaining unused fragments.
213 while (fragIndex < fragments.size())
214 fragments.remove(fragments.size() - 1);
215 }
216
217 }