25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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.swt.custom.StyledTextRenderer;
|
|
14
|
|
15
|
|
16
|
|
17 import org.eclipse.swt.SWT;
|
|
18 import org.eclipse.swt.graphics.Color;
|
|
19 import org.eclipse.swt.graphics.Device;
|
|
20 import org.eclipse.swt.graphics.Font;
|
|
21 import org.eclipse.swt.graphics.FontData;
|
|
22 import org.eclipse.swt.graphics.FontMetrics;
|
|
23 import org.eclipse.swt.graphics.GC;
|
|
24 import org.eclipse.swt.graphics.GlyphMetrics;
|
|
25 import org.eclipse.swt.graphics.Point;
|
|
26 import org.eclipse.swt.graphics.Rectangle;
|
|
27 import org.eclipse.swt.graphics.TextLayout;
|
|
28 import org.eclipse.swt.graphics.TextStyle;
|
|
29 import org.eclipse.swt.widgets.Display;
|
|
30 import org.eclipse.swt.widgets.IME;
|
|
31 import org.eclipse.swt.widgets.ScrollBar;
|
|
32 import org.eclipse.swt.custom.StyledText;
|
|
33 import org.eclipse.swt.custom.Bullet;
|
|
34 import org.eclipse.swt.custom.StyleRange;
|
|
35 import org.eclipse.swt.custom.StyledText;
|
|
36 import org.eclipse.swt.custom.StyledTextContent;
|
|
37 import org.eclipse.swt.custom.TextChangingEvent;
|
|
38 import org.eclipse.swt.custom.ST;
|
|
39 import org.eclipse.swt.custom.StyledTextEvent;
|
|
40
|
|
41 import java.lang.all;
|
|
42
|
48
|
43 version(Tango){
|
25
|
44 static import tango.text.Util;
|
|
45 static import tango.text.convert.Utf;
|
|
46 import tango.util.Convert;
|
48
|
47 } else { // Phobos
|
|
48 }
|
25
|
49
|
|
50 /**
|
|
51 * A StyledTextRenderer renders the content of a StyledText widget.
|
|
52 * This class can be used to render to the display or to a printer.
|
|
53 */
|
|
54 class StyledTextRenderer {
|
|
55 Device device;
|
|
56 StyledText styledText;
|
|
57 StyledTextContent content;
|
|
58
|
|
59 /* Font info */
|
|
60 Font regularFont, boldFont, italicFont, boldItalicFont;
|
|
61 int tabWidth;
|
|
62 int ascent, descent;
|
|
63 int averageCharWidth;
|
|
64
|
|
65 /* Line data */
|
|
66 int topIndex = -1;
|
|
67 TextLayout[] layouts;
|
|
68 int lineCount;
|
|
69 int[] lineWidth;
|
|
70 int[] lineHeight;
|
|
71 LineInfo[] lines;
|
|
72 int maxWidth;
|
|
73 int maxWidthLineIndex;
|
|
74 bool idleRunning;
|
|
75
|
|
76 /* Bullet */
|
|
77 Bullet[] bullets;
|
|
78 int[] bulletsIndices;
|
|
79 int[] redrawLines;
|
|
80
|
|
81 /* Style data */
|
|
82 int[] ranges;
|
|
83 int styleCount;
|
|
84 StyleRange[] styles;
|
|
85 StyleRange[] stylesSet;
|
|
86 int stylesSetCount = 0;
|
|
87 final static int BULLET_MARGIN = 8;
|
|
88
|
|
89 final static bool COMPACT_STYLES = true;
|
|
90 final static bool MERGE_STYLES = true;
|
|
91
|
|
92 final static int GROW = 32;
|
|
93 final static int IDLE_TIME = 50;
|
|
94 final static int CACHE_SIZE = 128;
|
|
95
|
|
96 final static int BACKGROUND = 1 << 0;
|
|
97 final static int ALIGNMENT = 1 << 1;
|
|
98 final static int INDENT = 1 << 2;
|
|
99 final static int JUSTIFY = 1 << 3;
|
|
100 final static int SEGMENTS = 1 << 5;
|
|
101
|
|
102 static class LineInfo {
|
|
103 int flags;
|
|
104 Color background;
|
|
105 int alignment;
|
|
106 int indent;
|
|
107 bool justify;
|
|
108 int[] segments;
|
|
109
|
|
110 public this() {
|
|
111 }
|
|
112 public this(LineInfo info) {
|
|
113 if (info !is null) {
|
|
114 flags = info.flags;
|
|
115 background = info.background;
|
|
116 alignment = info.alignment;
|
|
117 indent = info.indent;
|
|
118 justify = info.justify;
|
|
119 segments = info.segments;
|
|
120 }
|
|
121 }
|
|
122 }
|
|
123
|
|
124 this(Device device, StyledText styledText) {
|
|
125 this.device = device;
|
|
126 this.styledText = styledText;
|
|
127 }
|
|
128 int addMerge(int[] mergeRanges, StyleRange[] mergeStyles, int mergeCount, int modifyStart, int modifyEnd) {
|
|
129 int rangeCount = styleCount << 1;
|
|
130 StyleRange endStyle = null;
|
|
131 int endStart = 0, endLength = 0;
|
|
132 if (modifyEnd < rangeCount) {
|
|
133 endStyle = styles[modifyEnd >> 1];
|
|
134 endStart = ranges[modifyEnd];
|
|
135 endLength = ranges[modifyEnd + 1];
|
|
136 }
|
|
137 int grow = mergeCount - (modifyEnd - modifyStart);
|
|
138 if (rangeCount + grow >= ranges.length) {
|
|
139 int[] tmpRanges = new int[ranges.length + grow + (GROW << 1)];
|
|
140 System.arraycopy(ranges, 0, tmpRanges, 0, modifyStart);
|
|
141 StyleRange[] tmpStyles = new StyleRange[styles.length + (grow >> 1) + GROW];
|
|
142 System.arraycopy(styles, 0, tmpStyles, 0, modifyStart >> 1);
|
|
143 if (rangeCount > modifyEnd) {
|
|
144 System.arraycopy(ranges, modifyEnd, tmpRanges, modifyStart + mergeCount, rangeCount - modifyEnd);
|
|
145 System.arraycopy(styles, modifyEnd >> 1, tmpStyles, (modifyStart + mergeCount) >> 1, styleCount - (modifyEnd >> 1));
|
|
146 }
|
|
147 ranges = tmpRanges;
|
|
148 styles = tmpStyles;
|
|
149 } else {
|
|
150 if (rangeCount > modifyEnd) {
|
|
151 System.arraycopy(ranges, modifyEnd, ranges, modifyStart + mergeCount, rangeCount - modifyEnd);
|
|
152 System.arraycopy(styles, modifyEnd >> 1, styles, (modifyStart + mergeCount) >> 1, styleCount - (modifyEnd >> 1));
|
|
153 }
|
|
154 }
|
|
155 if (MERGE_STYLES) {
|
|
156 int j = modifyStart;
|
|
157 for (int i = 0; i < mergeCount; i += 2) {
|
|
158 if (j > 0 && ranges[j - 2] + ranges[j - 1] is mergeRanges[i] && mergeStyles[i >> 1].similarTo(styles[(j - 2) >> 1])) {
|
|
159 ranges[j - 1] += mergeRanges[i + 1];
|
|
160 } else {
|
|
161 styles[j >> 1] = mergeStyles[i >> 1];
|
|
162 ranges[j++] = mergeRanges[i];
|
|
163 ranges[j++] = mergeRanges[i + 1];
|
|
164 }
|
|
165 }
|
|
166 if (endStyle !is null && ranges[j - 2] + ranges[j - 1] is endStart && endStyle.similarTo(styles[(j - 2) >> 1])) {
|
|
167 ranges[j - 1] += endLength;
|
|
168 modifyEnd += 2;
|
|
169 mergeCount += 2;
|
|
170 }
|
|
171 if (rangeCount > modifyEnd) {
|
|
172 System.arraycopy(ranges, modifyStart + mergeCount, ranges, j, rangeCount - modifyEnd);
|
|
173 System.arraycopy(styles, (modifyStart + mergeCount) >> 1, styles, j >> 1, styleCount - (modifyEnd >> 1));
|
|
174 }
|
|
175 grow = (j - modifyStart) - (modifyEnd - modifyStart);
|
|
176 } else {
|
|
177 System.arraycopy(mergeRanges, 0, ranges, modifyStart, mergeCount);
|
|
178 System.arraycopy(mergeStyles, 0, styles, modifyStart >> 1, mergeCount >> 1);
|
|
179 }
|
|
180 styleCount += grow >> 1;
|
|
181 return grow;
|
|
182 }
|
|
183 int addMerge(StyleRange[] mergeStyles, int mergeCount, int modifyStart, int modifyEnd) {
|
|
184 int grow = mergeCount - (modifyEnd - modifyStart);
|
|
185 StyleRange endStyle = null;
|
|
186 if (modifyEnd < styleCount) endStyle = styles[modifyEnd];
|
|
187 if (styleCount + grow >= styles.length) {
|
|
188 StyleRange[] tmpStyles = new StyleRange[styles.length + grow + GROW];
|
|
189 System.arraycopy(styles, 0, tmpStyles, 0, modifyStart);
|
|
190 if (styleCount > modifyEnd) {
|
|
191 System.arraycopy(styles, modifyEnd, tmpStyles, modifyStart + mergeCount, styleCount - modifyEnd);
|
|
192 }
|
|
193 styles = tmpStyles;
|
|
194 } else {
|
|
195 if (styleCount > modifyEnd) {
|
|
196 System.arraycopy(styles, modifyEnd, styles, modifyStart + mergeCount, styleCount - modifyEnd);
|
|
197 }
|
|
198 }
|
|
199 if (MERGE_STYLES) {
|
|
200 int j = modifyStart;
|
|
201 for (int i = 0; i < mergeCount; i++) {
|
|
202 StyleRange newStyle = mergeStyles[i], style;
|
|
203 if (j > 0 && (style = styles[j - 1]).start + style.length is newStyle.start && newStyle.similarTo(style)) {
|
|
204 style.length += newStyle.length;
|
|
205 } else {
|
|
206 styles[j++] = newStyle;
|
|
207 }
|
|
208 }
|
|
209 StyleRange style = styles[j - 1];
|
|
210 if (endStyle !is null && style.start + style.length is endStyle.start && endStyle.similarTo(style)) {
|
|
211 style.length += endStyle.length;
|
|
212 modifyEnd++;
|
|
213 mergeCount++;
|
|
214 }
|
|
215 if (styleCount > modifyEnd) {
|
|
216 System.arraycopy(styles, modifyStart + mergeCount, styles, j, styleCount - modifyEnd);
|
|
217 }
|
|
218 grow = (j - modifyStart) - (modifyEnd - modifyStart);
|
|
219 } else {
|
|
220 System.arraycopy(mergeStyles, 0, styles, modifyStart, mergeCount);
|
|
221 }
|
|
222 styleCount += grow;
|
|
223 return grow;
|
|
224 }
|
|
225 void calculate(int startLine, int lineCount) {
|
|
226 int endLine = startLine + lineCount;
|
|
227 if (startLine < 0 || endLine > lineWidth.length) {
|
|
228 return;
|
|
229 }
|
|
230 int hTrim = styledText.leftMargin + styledText.rightMargin + styledText.getCaretWidth();
|
|
231 for (int i = startLine; i < endLine; i++) {
|
|
232 if (lineWidth[i] is -1 || lineHeight[i] is -1) {
|
|
233 TextLayout layout = getTextLayout(i);
|
|
234 Rectangle rect = layout.getBounds();
|
|
235 lineWidth[i] = rect.width + hTrim;
|
|
236 lineHeight[i] = rect.height;
|
|
237 disposeTextLayout(layout);
|
|
238 }
|
|
239 if (lineWidth[i] > maxWidth) {
|
|
240 maxWidth = lineWidth[i];
|
|
241 maxWidthLineIndex = i;
|
|
242 }
|
|
243 }
|
|
244 }
|
|
245 void calculateClientArea () {
|
|
246 int index = styledText.getTopIndex();
|
|
247 int lineCount = content.getLineCount();
|
|
248 int height = styledText.getClientArea().height;
|
|
249 int y = 0;
|
|
250 while (height > y && lineCount > index) {
|
|
251 calculate(index, 1);
|
|
252 y += lineHeight[index++];
|
|
253 }
|
|
254 }
|
|
255 void calculateIdle () {
|
|
256 if (idleRunning) return;
|
|
257 Runnable runnable = new class() Runnable {
|
|
258 public void run() {
|
|
259 if (styledText is null) return;
|
|
260 int i;
|
|
261 long start = System.currentTimeMillis();
|
|
262 for (i = 0; i < lineCount; i++) {
|
|
263 if (lineHeight[i] is -1 || lineWidth[i] is -1) {
|
|
264 calculate(i, 1);
|
|
265 if (System.currentTimeMillis() - start > IDLE_TIME) break;
|
|
266 }
|
|
267 }
|
|
268 if (i < lineCount) {
|
|
269 Display display = styledText.getDisplay();
|
|
270 display.asyncExec(this);
|
|
271 } else {
|
|
272 idleRunning = false;
|
|
273 styledText.setScrollBars(true);
|
|
274 ScrollBar bar = styledText.getVerticalBar();
|
|
275 if (bar !is null) {
|
|
276 bar.setSelection(styledText.getVerticalScrollOffset());
|
|
277 }
|
|
278 }
|
|
279 }
|
|
280 };
|
|
281 Display display = styledText.getDisplay();
|
|
282 display.asyncExec(runnable);
|
|
283 idleRunning = true;
|
|
284 }
|
|
285 void clearLineBackground(int startLine, int count) {
|
|
286 if (lines is null) return;
|
|
287 for (int i = startLine; i < startLine + count; i++) {
|
|
288 LineInfo info = lines[i];
|
|
289 if (info !is null) {
|
|
290 info.flags &= ~BACKGROUND;
|
|
291 info.background = null;
|
|
292 if (info.flags is 0) lines[i] = null;
|
|
293 }
|
|
294 }
|
|
295 }
|
|
296 void clearLineStyle(int startLine, int count) {
|
|
297 if (lines is null) return;
|
|
298 for (int i = startLine; i < startLine + count; i++) {
|
|
299 LineInfo info = lines[i];
|
|
300 if (info !is null) {
|
|
301 info.flags &= ~(ALIGNMENT | INDENT | JUSTIFY);
|
|
302 if (info.flags is 0) lines[i] = null;
|
|
303 }
|
|
304 }
|
|
305 }
|
|
306 void copyInto(StyledTextRenderer renderer) {
|
|
307 if (ranges !is null) {
|
|
308 int[] newRanges = renderer.ranges = new int[styleCount << 1];
|
|
309 System.arraycopy(ranges, 0, newRanges, 0, newRanges.length);
|
|
310 }
|
|
311 if (styles !is null) {
|
|
312 StyleRange[] newStyles = renderer.styles = new StyleRange[styleCount];
|
|
313 for (int i = 0; i < newStyles.length; i++) {
|
|
314 newStyles[i] = cast(StyleRange)styles[i].clone();
|
|
315 }
|
|
316 renderer.styleCount = styleCount;
|
|
317 }
|
|
318 if (lines !is null) {
|
|
319 LineInfo[] newLines = renderer.lines = new LineInfo[lineCount];
|
|
320 for (int i = 0; i < newLines.length; i++) {
|
|
321 newLines[i] = new LineInfo(lines[i]);
|
|
322 }
|
|
323 renderer.lineCount = lineCount;
|
|
324 }
|
|
325 }
|
|
326 void dispose() {
|
|
327 if (boldFont !is null) boldFont.dispose();
|
|
328 if (italicFont !is null) italicFont.dispose();
|
|
329 if (boldItalicFont !is null) boldItalicFont.dispose();
|
|
330 boldFont = italicFont = boldItalicFont = null;
|
|
331 reset();
|
|
332 content = null;
|
|
333 device = null;
|
|
334 styledText = null;
|
|
335 }
|
|
336 void disposeTextLayout (TextLayout layout) {
|
|
337 if (layouts !is null) {
|
|
338 for (int i = 0; i < layouts.length; i++) {
|
|
339 if (layouts[i] is layout) return;
|
|
340 }
|
|
341 }
|
|
342 layout.dispose();
|
|
343 }
|
|
344 void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) {
|
|
345 StyleRange style = bullet.style;
|
|
346 GlyphMetrics metrics = style.metrics;
|
|
347 Color color = style.foreground;
|
|
348 if (color !is null) gc.setForeground(color);
|
|
349 if ((bullet.type & ST.BULLET_DOT) !is 0 && StyledText.IS_MOTIF) {
|
|
350 int size = Math.max(4, (lineAscent + lineDescent) / 4);
|
|
351 if ((size & 1) is 0) size++;
|
|
352 if (color is null) {
|
|
353 Display display = styledText.getDisplay();
|
|
354 color = display.getSystemColor(SWT.COLOR_BLACK);
|
|
355 }
|
|
356 gc.setBackground(color);
|
|
357 int x = paintX + Math.max(0, metrics.width - size - BULLET_MARGIN);
|
|
358 gc.fillArc(x, paintY + size, size + 1, size + 1, 0, 360);
|
|
359 return;
|
|
360 }
|
|
361 Font font = style.font;
|
|
362 if (font !is null) gc.setFont(font);
|
|
363 String string = "";
|
|
364 int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER);
|
|
365 switch (type) {
|
|
366 case ST.BULLET_DOT: string = "\u2022"; break;
|
|
367 case ST.BULLET_NUMBER: string = to!(String)(index); break;
|
|
368 case ST.BULLET_LETTER_LOWER: string = [cast(char) (index % 26 + 97)]; break;
|
|
369 case ST.BULLET_LETTER_UPPER: string = [cast(char) (index % 26 + 65)]; break;
|
|
370 default:
|
|
371 }
|
|
372 if ((bullet.type & ST.BULLET_TEXT) !is 0) string ~= bullet.text;
|
|
373 Display display = styledText.getDisplay();
|
|
374 TextLayout layout = new TextLayout(display);
|
|
375 layout.setText(string);
|
|
376 layout.setAscent(lineAscent);
|
|
377 layout.setDescent(lineDescent);
|
|
378 style = cast(StyleRange)style.clone();
|
|
379 style.metrics = null;
|
|
380 if (style.font is null) style.font = getFont(style.fontStyle);
|
|
381 layout.setStyle(style, 0, string.length);
|
|
382 int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - BULLET_MARGIN);
|
|
383 layout.draw(gc, x, paintY);
|
|
384 layout.dispose();
|
|
385 }
|
|
386 int drawLine(int lineIndex, int paintX, int paintY, GC gc, Color widgetBackground, Color widgetForeground) {
|
|
387 TextLayout layout = getTextLayout(lineIndex);
|
|
388 String line = content.getLine(lineIndex);
|
|
389 int lineOffset = content.getOffsetAtLine(lineIndex);
|
|
390 int lineLength = line.length;
|
|
391 Point selection = styledText.getSelection();
|
|
392 int selectionStart = selection.x - lineOffset;
|
|
393 int selectionEnd = selection.y - lineOffset;
|
|
394 Rectangle client = styledText.getClientArea();
|
|
395 Color lineBackground = getLineBackground(lineIndex, null);
|
|
396 StyledTextEvent event = styledText.getLineBackgroundData(lineOffset, line);
|
|
397 if (event !is null && event.lineBackground !is null) lineBackground = event.lineBackground;
|
|
398
|
|
399 int height = layout.getBounds().height;
|
|
400 if (lineBackground !is null) {
|
|
401 gc.setBackground(lineBackground);
|
|
402 gc.fillRectangle(client.x, paintY, client.width, height);
|
|
403 } else {
|
|
404 gc.setBackground(widgetBackground);
|
|
405 styledText.drawBackground(gc, client.x, paintY, client.width, height);
|
|
406 }
|
|
407 gc.setForeground(widgetForeground);
|
|
408 if (selectionStart is selectionEnd || (selectionEnd <= 0 && selectionStart > lineLength - 1)) {
|
|
409 layout.draw(gc, paintX, paintY);
|
|
410 } else {
|
|
411 int start = Math.max(0, selectionStart);
|
|
412 int end = Math.min(lineLength, selectionEnd);
|
|
413 Color selectionFg = styledText.getSelectionForeground();
|
|
414 Color selectionBg = styledText.getSelectionBackground();
|
|
415 int flags;
|
|
416 if ((styledText.getStyle() & SWT.FULL_SELECTION) !is 0) {
|
|
417 flags = SWT.FULL_SELECTION;
|
|
418 } else {
|
|
419 flags = SWT.DELIMITER_SELECTION;
|
|
420 }
|
|
421 if (selectionStart <= lineLength && lineLength < selectionEnd ) {
|
|
422 flags |= SWT.LAST_LINE_SELECTION;
|
|
423 }
|
|
424 layout.draw(gc, paintX, paintY, start, end - 1, selectionFg, selectionBg, flags);
|
|
425 }
|
|
426
|
|
427 // draw objects
|
|
428 Bullet bullet = null;
|
|
429 int bulletIndex = -1;
|
|
430 if (bullets !is null) {
|
|
431 if (bulletsIndices !is null) {
|
|
432 int index = lineIndex - topIndex;
|
|
433 if (0 <= index && index < CACHE_SIZE) {
|
|
434 bullet = bullets[index];
|
|
435 bulletIndex = bulletsIndices[index];
|
|
436 }
|
|
437 } else {
|
|
438 for (int i = 0; i < bullets.length; i++) {
|
|
439 bullet = bullets[i];
|
|
440 bulletIndex = bullet.indexOf(lineIndex);
|
|
441 if (bulletIndex !is -1) break;
|
|
442 }
|
|
443 }
|
|
444 }
|
|
445 if (bulletIndex !is -1 && bullet !is null) {
|
|
446 FontMetrics metrics = layout.getLineMetrics(0);
|
|
447 int lineAscent = metrics.getAscent() + metrics.getLeading();
|
|
448 if (bullet.type is ST.BULLET_CUSTOM) {
|
|
449 bullet.style.start = lineOffset;
|
|
450 styledText.paintObject(gc, paintX, paintY, lineAscent, metrics.getDescent(), bullet.style, bullet, bulletIndex);
|
|
451 } else {
|
|
452 drawBullet(bullet, gc, paintX, paintY, bulletIndex, lineAscent, metrics.getDescent());
|
|
453 }
|
|
454 }
|
|
455 TextStyle[] styles = layout.getStyles();
|
|
456 int[] ranges = null;
|
|
457 for (int i = 0; i < styles.length; i++) {
|
|
458 if (styles[i].metrics !is null) {
|
|
459 if (ranges is null) ranges = layout.getRanges();
|
|
460 int start = ranges[i << 1];
|
|
461 int length = ranges[(i << 1) + 1] - start;
|
|
462 Point point = layout.getLocation(start, false);
|
|
463 FontMetrics metrics = layout.getLineMetrics(layout.getLineIndex(start));
|
|
464 StyleRange style = cast(StyleRange)(cast(StyleRange)styles[i]).clone();
|
|
465 style.start = start + lineOffset;
|
|
466 style.length = length;
|
|
467 int lineAscent = metrics.getAscent() + metrics.getLeading();
|
|
468 styledText.paintObject(gc, point.x + paintX, point.y + paintY, lineAscent, metrics.getDescent(), style, null, 0);
|
|
469 }
|
|
470 }
|
|
471 disposeTextLayout(layout);
|
|
472 return height;
|
|
473 }
|
|
474 int getBaseline() {
|
|
475 return ascent;
|
|
476 }
|
|
477 Font getFont(int style) {
|
|
478 switch (style) {
|
|
479 case SWT.BOLD:
|
|
480 if (boldFont !is null) return boldFont;
|
|
481 return boldFont = new Font(device, getFontData(style));
|
|
482 case SWT.ITALIC:
|
|
483 if (italicFont !is null) return italicFont;
|
|
484 return italicFont = new Font(device, getFontData(style));
|
|
485 case SWT.BOLD | SWT.ITALIC:
|
|
486 if (boldItalicFont !is null) return boldItalicFont;
|
|
487 return boldItalicFont = new Font(device, getFontData(style));
|
|
488 default:
|
|
489 return regularFont;
|
|
490 }
|
|
491 }
|
|
492 FontData[] getFontData(int style) {
|
|
493 FontData[] fontDatas = regularFont.getFontData();
|
|
494 for (int i = 0; i < fontDatas.length; i++) {
|
|
495 fontDatas[i].setStyle(style);
|
|
496 }
|
|
497 return fontDatas;
|
|
498 }
|
|
499 int getHeight () {
|
|
500 int defaultLineHeight = getLineHeight();
|
|
501 if (styledText.isFixedLineHeight()) {
|
|
502 return lineCount * defaultLineHeight;
|
|
503 }
|
|
504 int totalHeight = 0;
|
|
505 int width = styledText.getWrapWidth();
|
|
506 for (int i = 0; i < lineCount; i++) {
|
|
507 int height = lineHeight[i];
|
|
508 if (height is -1) {
|
|
509 if (width > 0) {
|
|
510 int length = content.getLine(i).length;
|
|
511 height = ((length * averageCharWidth / width) + 1) * defaultLineHeight;
|
|
512 } else {
|
|
513 height = defaultLineHeight;
|
|
514 }
|
|
515 }
|
|
516 totalHeight += height;
|
|
517 }
|
|
518 return totalHeight + styledText.topMargin + styledText.bottomMargin;
|
|
519 }
|
|
520 int getLineAlignment(int index, int defaultAlignment) {
|
|
521 if (lines is null) return defaultAlignment;
|
|
522 LineInfo info = lines[index];
|
|
523 if (info !is null && (info.flags & ALIGNMENT) !is 0) {
|
|
524 return info.alignment;
|
|
525 }
|
|
526 return defaultAlignment;
|
|
527 }
|
|
528 Color getLineBackground(int index, Color defaultBackground) {
|
|
529 if (lines is null) return defaultBackground;
|
|
530 LineInfo info = lines[index];
|
|
531 if (info !is null && (info.flags & BACKGROUND) !is 0) {
|
|
532 return info.background;
|
|
533 }
|
|
534 return defaultBackground;
|
|
535 }
|
|
536 Bullet getLineBullet (int index, Bullet defaultBullet) {
|
|
537 if (bullets is null) return defaultBullet;
|
|
538 if (bulletsIndices !is null) return defaultBullet;
|
|
539 for (int i = 0; i < bullets.length; i++) {
|
|
540 Bullet bullet = bullets[i];
|
|
541 if (bullet.indexOf(index) !is -1) return bullet;
|
|
542 }
|
|
543 return defaultBullet;
|
|
544 }
|
|
545 int getLineHeight() {
|
|
546 return ascent + descent;
|
|
547 }
|
|
548 int getLineHeight(int lineIndex) {
|
|
549 if (lineHeight[lineIndex] is -1) {
|
|
550 calculate(lineIndex, 1);
|
|
551 }
|
|
552 return lineHeight[lineIndex];
|
|
553 }
|
|
554 int getLineIndent(int index, int defaultIndent) {
|
|
555 if (lines is null) return defaultIndent;
|
|
556 LineInfo info = lines[index];
|
|
557 if (info !is null && (info.flags & INDENT) !is 0) {
|
|
558 return info.indent;
|
|
559 }
|
|
560 return defaultIndent;
|
|
561 }
|
|
562 bool getLineJustify(int index, bool defaultJustify) {
|
|
563 if (lines is null) return defaultJustify;
|
|
564 LineInfo info = lines[index];
|
|
565 if (info !is null && (info.flags & JUSTIFY) !is 0) {
|
|
566 return info.justify;
|
|
567 }
|
|
568 return defaultJustify;
|
|
569 }
|
|
570 int[] getLineSegments(int index, int[] defaultSegments) {
|
|
571 if (lines is null) return defaultSegments;
|
|
572 LineInfo info = lines[index];
|
|
573 if (info !is null && (info.flags & SEGMENTS) !is 0) {
|
|
574 return info.segments;
|
|
575 }
|
|
576 return defaultSegments;
|
|
577 }
|
|
578 int getRangeIndex(int offset, int low, int high) {
|
|
579 if (styleCount is 0) return 0;
|
|
580 if (ranges !is null) {
|
|
581 while (high - low > 2) {
|
|
582 int index = ((high + low) / 2) / 2 * 2;
|
|
583 int end = ranges[index] + ranges[index + 1];
|
|
584 if (end > offset) {
|
|
585 high = index;
|
|
586 } else {
|
|
587 low = index;
|
|
588 }
|
|
589 }
|
|
590 } else {
|
|
591 while (high - low > 1) {
|
|
592 int index = ((high + low) / 2);
|
|
593 int end = styles[index].start + styles[index].length;
|
|
594 if (end > offset) {
|
|
595 high = index;
|
|
596 } else {
|
|
597 low = index;
|
|
598 }
|
|
599 }
|
|
600 }
|
|
601 return high;
|
|
602 }
|
|
603 int[] getRanges(int start, int length) {
|
|
604 int[] newRanges;
|
|
605 int end = start + length - 1;
|
|
606 if (ranges !is null) {
|
|
607 int rangeCount = styleCount << 1;
|
|
608 int rangeStart = getRangeIndex(start, -1, rangeCount);
|
|
609 if (rangeStart >= rangeCount) return null;
|
|
610 if (ranges[rangeStart] > end) return null;
|
|
611 int rangeEnd = Math.min(rangeCount - 2, getRangeIndex(end, rangeStart - 1, rangeCount) + 1);
|
|
612 newRanges = new int[rangeEnd - rangeStart + 2];
|
|
613 System.arraycopy(ranges, rangeStart, newRanges, 0, newRanges.length);
|
|
614 } else {
|
|
615 int rangeStart = getRangeIndex(start, -1, styleCount);
|
|
616 if (rangeStart >= styleCount) return null;
|
|
617 if (styles[rangeStart].start > end) return null;
|
|
618 int rangeEnd = Math.min(styleCount - 1, getRangeIndex(end, rangeStart - 1, styleCount));
|
|
619 newRanges = new int[(rangeEnd - rangeStart + 1) << 1];
|
|
620 for (int i = rangeStart, j = 0; i <= rangeEnd; i++, j += 2) {
|
|
621 StyleRange style = styles[i];
|
|
622 newRanges[j] = style.start;
|
|
623 newRanges[j + 1] = style.length;
|
|
624 }
|
|
625 }
|
|
626 if (start > newRanges[0]) {
|
|
627 newRanges[1] = newRanges[0] + newRanges[1] - start;
|
|
628 newRanges[0] = start;
|
|
629 }
|
|
630 if (end < newRanges[newRanges.length - 2] + newRanges[newRanges.length - 1] - 1) {
|
|
631 newRanges[newRanges.length - 1] = end - newRanges[newRanges.length - 2] + 1;
|
|
632 }
|
|
633 return newRanges;
|
|
634 }
|
|
635 StyleRange[] getStyleRanges(int start, int length, bool includeRanges) {
|
|
636 StyleRange[] newStyles;
|
|
637 int end = start + length - 1;
|
|
638 if (ranges !is null) {
|
|
639 int rangeCount = styleCount << 1;
|
|
640 int rangeStart = getRangeIndex(start, -1, rangeCount);
|
|
641 if (rangeStart >= rangeCount) return null;
|
|
642 if (ranges[rangeStart] > end) return null;
|
|
643 int rangeEnd = Math.min(rangeCount - 2, getRangeIndex(end, rangeStart - 1, rangeCount) + 1);
|
|
644 newStyles = new StyleRange[((rangeEnd - rangeStart) >> 1) + 1];
|
|
645 if (includeRanges) {
|
|
646 for (int i = rangeStart, j = 0; i <= rangeEnd; i += 2, j++) {
|
|
647 newStyles[j] = cast(StyleRange)styles[i >> 1].clone();
|
|
648 newStyles[j].start = ranges[i];
|
|
649 newStyles[j].length = ranges[i + 1];
|
|
650 }
|
|
651 } else {
|
|
652 System.arraycopy(styles, rangeStart >> 1, newStyles, 0, newStyles.length);
|
|
653 }
|
|
654 } else {
|
|
655 int rangeStart = getRangeIndex(start, -1, styleCount);
|
|
656 if (rangeStart >= styleCount) return null;
|
|
657 if (styles[rangeStart].start > end) return null;
|
|
658 int rangeEnd = Math.min(styleCount - 1, getRangeIndex(end, rangeStart - 1, styleCount));
|
|
659 newStyles = new StyleRange[rangeEnd - rangeStart + 1];
|
|
660 System.arraycopy(styles, rangeStart, newStyles, 0, newStyles.length);
|
|
661 }
|
|
662 StyleRange style = newStyles[0];
|
|
663 if (start > style.start) {
|
|
664 if (!includeRanges || ranges is null) newStyles[0] = style = cast(StyleRange)style.clone();
|
|
665 style.length = style.start + style.length - start;
|
|
666 style.start = start;
|
|
667 }
|
|
668 style = newStyles[newStyles.length - 1];
|
|
669 if (end < style.start + style.length - 1) {
|
|
670 if (end < style.start) {
|
|
671 StyleRange[] tmp = new StyleRange[newStyles.length - 1];
|
|
672 System.arraycopy(newStyles, 0, tmp, 0, newStyles.length - 1);
|
|
673 newStyles = tmp;
|
|
674 } else {
|
|
675 if (!includeRanges || ranges is null) newStyles[newStyles.length - 1] = style = cast(StyleRange)style.clone();
|
|
676 style.length = end - style.start + 1;
|
|
677 }
|
|
678 }
|
|
679 return newStyles;
|
|
680 }
|
|
681 StyleRange getStyleRange(StyleRange style) {
|
|
682 if (style.start is 0 && style.length is 0 && style.fontStyle is SWT.NORMAL) return style;
|
|
683 StyleRange clone = cast(StyleRange)style.clone();
|
|
684 clone.start = clone.length = 0;
|
|
685 clone.fontStyle = SWT.NORMAL;
|
|
686 if (clone.font is null) clone.font = getFont(style.fontStyle);
|
|
687 return clone;
|
|
688 }
|
|
689 TextLayout getTextLayout(int lineIndex) {
|
|
690 return getTextLayout(lineIndex, styledText.getOrientation(), styledText.getWrapWidth(), styledText.lineSpacing);
|
|
691 }
|
|
692 TextLayout getTextLayout(int lineIndex, int orientation, int width, int lineSpacing) {
|
|
693 TextLayout layout = null;
|
|
694 if (styledText !is null) {
|
|
695 int topIndex = styledText.topIndex > 0 ? styledText.topIndex - 1 : 0;
|
|
696 if (layouts is null || topIndex !is this.topIndex) {
|
|
697 TextLayout[] newLayouts = new TextLayout[CACHE_SIZE];
|
|
698 if (layouts !is null) {
|
|
699 for (int i = 0; i < layouts.length; i++) {
|
|
700 if (layouts[i] !is null) {
|
|
701 int layoutIndex = (i + this.topIndex) - topIndex;
|
|
702 if (0 <= layoutIndex && layoutIndex < newLayouts.length) {
|
|
703 newLayouts[layoutIndex] = layouts[i];
|
|
704 } else {
|
|
705 layouts[i].dispose();
|
|
706 }
|
|
707 }
|
|
708 }
|
|
709 }
|
|
710 if (bullets !is null && bulletsIndices !is null && topIndex !is this.topIndex) {
|
|
711 int delta = topIndex - this.topIndex;
|
|
712 if (delta > 0) {
|
|
713 if (delta < bullets.length) {
|
|
714 System.arraycopy(bullets, delta, bullets, 0, bullets.length - delta);
|
|
715 System.arraycopy(bulletsIndices, delta, bulletsIndices, 0, bulletsIndices.length - delta);
|
|
716 }
|
|
717 int startIndex = Math.max(0, bullets.length - delta);
|
|
718 for (int i = startIndex; i < bullets.length; i++) bullets[i] = null;
|
|
719 } else {
|
|
720 if (-delta < bullets.length) {
|
|
721 System.arraycopy(bullets, 0, bullets, -delta, bullets.length + delta);
|
|
722 System.arraycopy(bulletsIndices, 0, bulletsIndices, -delta, bulletsIndices.length + delta);
|
|
723 }
|
|
724 int endIndex = Math.min(bullets.length, -delta);
|
|
725 for (int i = 0; i < endIndex; i++) bullets[i] = null;
|
|
726 }
|
|
727 }
|
|
728 this.topIndex = topIndex;
|
|
729 layouts = newLayouts;
|
|
730 }
|
|
731 if (layouts !is null) {
|
|
732 int layoutIndex = lineIndex - topIndex;
|
|
733 if (0 <= layoutIndex && layoutIndex < layouts.length) {
|
|
734 layout = layouts[layoutIndex];
|
|
735 if (layout !is null) {
|
|
736 if (lineWidth[lineIndex] !is -1) return layout;
|
|
737 } else {
|
|
738 layout = layouts[layoutIndex] = new TextLayout(device);
|
|
739 }
|
|
740 }
|
|
741 }
|
|
742 }
|
|
743 if (layout is null) layout = new TextLayout(device);
|
|
744 String line = content.getLine(lineIndex);
|
|
745 int lineOffset = content.getOffsetAtLine(lineIndex);
|
|
746 int[] segments = null;
|
|
747 int indent = 0;
|
|
748 int alignment = SWT.LEFT;
|
|
749 bool justify = false;
|
|
750 Bullet bullet = null;
|
|
751 int[] ranges = null;
|
|
752 StyleRange[] styles = null;
|
|
753 int rangeStart = 0, styleCount = 0;
|
|
754 StyledTextEvent event = null;
|
|
755 if (styledText !is null) {
|
|
756 event = styledText.getLineStyleData(lineOffset, line);
|
|
757 segments = styledText.getBidiSegments(lineOffset, line);
|
|
758 indent = styledText.indent;
|
|
759 alignment = styledText.alignment;
|
|
760 justify = styledText.justify;
|
|
761 }
|
|
762 if (event !is null) {
|
|
763 indent = event.indent;
|
|
764 alignment = event.alignment;
|
|
765 justify = event.justify;
|
|
766 bullet = event.bullet;
|
|
767 ranges = event.ranges;
|
|
768 styles = event.styles;
|
|
769 if (styles !is null) {
|
|
770 styleCount = styles.length;
|
|
771 if (styledText.isFixedLineHeight()) {
|
|
772 for (int i = 0; i < styleCount; i++) {
|
|
773 if (styles[i].isVariableHeight()) {
|
|
774 styledText.verticalScrollOffset = -1;
|
|
775 styledText.setVariableLineHeight();
|
|
776 styledText.redraw();
|
|
777 break;
|
|
778 }
|
|
779 }
|
|
780 }
|
|
781 }
|
|
782 if (bullets is null || bulletsIndices is null) {
|
|
783 bullets = new Bullet[CACHE_SIZE];
|
|
784 bulletsIndices = new int[CACHE_SIZE];
|
|
785 }
|
|
786 int index = lineIndex - topIndex;
|
|
787 if (0 <= index && index < CACHE_SIZE) {
|
|
788 bullets[index] = bullet;
|
|
789 bulletsIndices[index] = event.bulletIndex;
|
|
790 }
|
|
791 } else {
|
|
792 if (lines !is null) {
|
|
793 LineInfo info = lines[lineIndex];
|
|
794 if (info !is null) {
|
|
795 if ((info.flags & INDENT) !is 0) indent = info.indent;
|
|
796 if ((info.flags & ALIGNMENT) !is 0) alignment = info.alignment;
|
|
797 if ((info.flags & JUSTIFY) !is 0) justify = info.justify;
|
|
798 if ((info.flags & SEGMENTS) !is 0) segments = info.segments;
|
|
799 }
|
|
800 }
|
|
801 if (bulletsIndices !is null) {
|
|
802 bullets = null;
|
|
803 bulletsIndices = null;
|
|
804 }
|
|
805 if (bullets !is null) {
|
|
806 for (int i = 0; i < bullets.length; i++) {
|
|
807 if (bullets[i].indexOf(lineIndex) !is -1) {
|
|
808 bullet = bullets[i];
|
|
809 break;
|
|
810 }
|
|
811 }
|
|
812 }
|
|
813 ranges = this.ranges;
|
|
814 styles = this.styles;
|
|
815 styleCount = this.styleCount;
|
|
816 if (ranges !is null) {
|
|
817 rangeStart = getRangeIndex(lineOffset, -1, styleCount << 1);
|
|
818 } else {
|
|
819 rangeStart = getRangeIndex(lineOffset, -1, styleCount);
|
|
820 }
|
|
821 }
|
|
822 if (bullet !is null) {
|
|
823 StyleRange style = bullet.style;
|
|
824 GlyphMetrics metrics = style.metrics;
|
|
825 indent += metrics.width;
|
|
826 }
|
|
827 layout.setFont(regularFont);
|
|
828 layout.setAscent(ascent);
|
|
829 layout.setDescent(descent);
|
|
830 layout.setText(line);
|
|
831 layout.setOrientation(orientation);
|
|
832 layout.setSegments(segments);
|
|
833 layout.setWidth(width);
|
|
834 layout.setSpacing(lineSpacing);
|
|
835 layout.setTabs([tabWidth]);
|
|
836 layout.setIndent(indent);
|
|
837 layout.setAlignment(alignment);
|
|
838 layout.setJustify(justify);
|
|
839
|
|
840 int lastOffset = 0;
|
|
841 int length = line.length;
|
|
842 if (styles !is null) {
|
|
843 if (ranges !is null) {
|
|
844 int rangeCount = styleCount << 1;
|
|
845 for (int i = rangeStart; i < rangeCount; i += 2) {
|
|
846 int start, end;
|
|
847 if (lineOffset > ranges[i]) {
|
|
848 start = 0;
|
|
849 end = Math.min (length, ranges[i + 1] - lineOffset + ranges[i]);
|
|
850 } else {
|
|
851 start = ranges[i] - lineOffset;
|
|
852 end = Math.min(length, start + ranges[i + 1]);
|
|
853 }
|
|
854 if (start >= length) break;
|
|
855 if (lastOffset < start) {
|
|
856 layout.setStyle(null, lastOffset, start - 1);
|
|
857 }
|
|
858 layout.setStyle(getStyleRange(styles[i >> 1]), start, end);
|
|
859 lastOffset = Math.max(lastOffset, end);
|
|
860 }
|
|
861 } else {
|
|
862 for (int i = rangeStart; i < styleCount; i++) {
|
|
863 int start, end;
|
|
864 if (lineOffset > styles[i].start) {
|
|
865 start = 0;
|
|
866 end = Math.min (length, styles[i].length - lineOffset + styles[i].start);
|
|
867 } else {
|
|
868 start = styles[i].start - lineOffset;
|
|
869 end = Math.min(length, start + styles[i].length);
|
|
870 }
|
|
871 if (start >= length) break;
|
|
872 if (lastOffset < start) {
|
|
873 layout.setStyle(null, lastOffset, start - 1);
|
|
874 }
|
|
875 layout.setStyle(getStyleRange(styles[i]), start, end);
|
|
876 lastOffset = Math.max(lastOffset, end);
|
|
877 }
|
|
878 }
|
|
879 }
|
|
880 if (lastOffset < length) layout.setStyle(null, lastOffset, length);
|
|
881 if (styledText !is null && styledText.ime !is null) {
|
|
882 IME ime = styledText.ime;
|
|
883 int compositionOffset = ime.getCompositionOffset();
|
|
884 if (compositionOffset !is -1) {
|
|
885 int commitCount = ime.getCommitCount();
|
|
886 int compositionLength = ime.getText().length;
|
|
887 if (compositionLength !is commitCount) {
|
|
888 int compositionLine = content.getLineAtOffset(compositionOffset);
|
|
889 if (compositionLine is lineIndex) {
|
|
890 int[] imeRanges = ime.getRanges();
|
|
891 TextStyle[] imeStyles = ime.getStyles();
|
|
892 if (imeRanges.length > 0) {
|
|
893 for (int i = 0; i < imeStyles.length; i++) {
|
|
894 int start = imeRanges[i*2] - lineOffset;
|
|
895 int end = imeRanges[i*2+1] - lineOffset;
|
|
896 TextStyle imeStyle = imeStyles[i], userStyle;
|
|
897 for (int j = start; j <= end; j++) {
|
|
898 userStyle = layout.getStyle(j);
|
|
899 if (userStyle is null && j > 0) userStyle = layout.getStyle(j - 1);
|
|
900 if (userStyle is null && j + 1 < length) userStyle = layout.getStyle(j + 1);
|
|
901 if (userStyle is null) {
|
|
902 layout.setStyle(imeStyle, j, j);
|
|
903 } else {
|
|
904 TextStyle newStyle = new TextStyle(imeStyle);
|
|
905 if (newStyle.font is null) newStyle.font = userStyle.font;
|
|
906 if (newStyle.foreground is null) newStyle.foreground = userStyle.foreground;
|
|
907 if (newStyle.background is null) newStyle.background = userStyle.background;
|
|
908 layout.setStyle(newStyle, j, j);
|
|
909 }
|
|
910 }
|
|
911 }
|
|
912 } else {
|
|
913 int start = compositionOffset - lineOffset;
|
|
914 int end = start + compositionLength - 1;
|
|
915 TextStyle userStyle = layout.getStyle(start);
|
|
916 if (userStyle is null) {
|
|
917 if (start > 0) userStyle = layout.getStyle(start - 1);
|
|
918 if (userStyle is null && end + 1 < length) userStyle = layout.getStyle(end + 1);
|
|
919 if (userStyle !is null) {
|
|
920 TextStyle newStyle = new TextStyle();
|
|
921 newStyle.font = userStyle.font;
|
|
922 newStyle.foreground = userStyle.foreground;
|
|
923 newStyle.background = userStyle.background;
|
|
924 layout.setStyle(newStyle, start, end);
|
|
925 }
|
|
926 }
|
|
927 }
|
|
928 }
|
|
929 }
|
|
930 }
|
|
931 }
|
|
932
|
|
933 if (styledText !is null && styledText.isFixedLineHeight()) {
|
|
934 int index = -1;
|
|
935 int lineCount = layout.getLineCount();
|
|
936 int height = getLineHeight();
|
|
937 for (int i = 0; i < lineCount; i++) {
|
|
938 int lineHeight = layout.getLineBounds(i).height;
|
|
939 if (lineHeight > height) {
|
|
940 height = lineHeight;
|
|
941 index = i;
|
|
942 }
|
|
943 }
|
|
944 if (index !is -1) {
|
|
945 FontMetrics metrics = layout.getLineMetrics(index);
|
|
946 ascent = metrics.getAscent() + metrics.getLeading();
|
|
947 descent = metrics.getDescent();
|
|
948 if (layouts !is null) {
|
|
949 for (int i = 0; i < layouts.length; i++) {
|
|
950 if (layouts[i] !is null && layouts[i] !is layout) {
|
|
951 layouts[i].setAscent(ascent);
|
|
952 layouts[i].setDescent(descent);
|
|
953 }
|
|
954 }
|
|
955 }
|
|
956 if (styledText.verticalScrollOffset !is 0) {
|
|
957 int topIndex = styledText.topIndex;
|
|
958 int topIndexY = styledText.topIndexY;
|
|
959 int lineHeight = getLineHeight();
|
|
960 if (topIndexY >= 0) {
|
|
961 styledText.verticalScrollOffset = (topIndex - 1) * lineHeight + lineHeight - topIndexY;
|
|
962 } else {
|
|
963 styledText.verticalScrollOffset = topIndex * lineHeight - topIndexY;
|
|
964 }
|
|
965 }
|
|
966 styledText.calculateScrollBars();
|
|
967 if (styledText.isBidiCaret()) styledText.createCaretBitmaps();
|
|
968 styledText.caretDirection = SWT.NULL;
|
|
969 styledText.setCaretLocation();
|
|
970 styledText.redraw();
|
|
971 }
|
|
972 }
|
|
973 return layout;
|
|
974 }
|
|
975 int getWidth() {
|
|
976 return maxWidth;
|
|
977 }
|
|
978 void reset() {
|
|
979 if (layouts !is null) {
|
|
980 for (int i = 0; i < layouts.length; i++) {
|
|
981 TextLayout layout = layouts[i];
|
|
982 if (layout !is null) layout.dispose();
|
|
983 }
|
|
984 layouts = null;
|
|
985 }
|
|
986 topIndex = -1;
|
|
987 stylesSetCount = styleCount = lineCount = 0;
|
|
988 ranges = null;
|
|
989 styles = null;
|
|
990 stylesSet = null;
|
|
991 lines = null;
|
|
992 lineWidth = null;
|
|
993 lineHeight = null;
|
|
994 bullets = null;
|
|
995 bulletsIndices = null;
|
|
996 redrawLines = null;
|
|
997 }
|
|
998 void reset(int startLine, int lineCount) {
|
|
999 int endLine = startLine + lineCount;
|
|
1000 if (startLine < 0 || endLine > lineWidth.length) return;
|
|
1001 for (int i = startLine; i < endLine; i++) {
|
|
1002 lineWidth[i] = -1;
|
|
1003 lineHeight[i] = -1;
|
|
1004 }
|
|
1005 if (startLine <= maxWidthLineIndex && maxWidthLineIndex < endLine) {
|
|
1006 maxWidth = 0;
|
|
1007 maxWidthLineIndex = -1;
|
|
1008 if (lineCount !is this.lineCount) {
|
|
1009 for (int i = 0; i < this.lineCount; i++) {
|
|
1010 if (lineWidth[i] > maxWidth) {
|
|
1011 maxWidth = lineWidth[i];
|
|
1012 maxWidthLineIndex = i;
|
|
1013 }
|
|
1014 }
|
|
1015 }
|
|
1016 }
|
|
1017 }
|
|
1018 void setContent(StyledTextContent content) {
|
|
1019 reset();
|
|
1020 this.content = content;
|
|
1021 lineCount = content.getLineCount();
|
|
1022 lineWidth = new int[lineCount];
|
|
1023 lineHeight = new int[lineCount];
|
|
1024 reset(0, lineCount);
|
|
1025 }
|
|
1026 void setFont(Font font, int tabs) {
|
|
1027 TextLayout layout = new TextLayout(device);
|
|
1028 layout.setFont(regularFont);
|
|
1029 if (font !is null) {
|
|
1030 if (boldFont !is null) boldFont.dispose();
|
|
1031 if (italicFont !is null) italicFont.dispose();
|
|
1032 if (boldItalicFont !is null) boldItalicFont.dispose();
|
|
1033 boldFont = italicFont = boldItalicFont = null;
|
|
1034 regularFont = font;
|
|
1035 layout.setText(" ");
|
|
1036 layout.setFont(font);
|
|
1037 layout.setStyle(new TextStyle(getFont(SWT.NORMAL), null, null), 0, 0);
|
|
1038 layout.setStyle(new TextStyle(getFont(SWT.BOLD), null, null), 1, 1);
|
|
1039 layout.setStyle(new TextStyle(getFont(SWT.ITALIC), null, null), 2, 2);
|
|
1040 layout.setStyle(new TextStyle(getFont(SWT.BOLD | SWT.ITALIC), null, null), 3, 3);
|
|
1041 FontMetrics metrics = layout.getLineMetrics(0);
|
|
1042 ascent = metrics.getAscent() + metrics.getLeading();
|
|
1043 descent = metrics.getDescent();
|
|
1044 boldFont.dispose();
|
|
1045 italicFont.dispose();
|
|
1046 boldItalicFont.dispose();
|
|
1047 boldFont = italicFont = boldItalicFont = null;
|
|
1048 }
|
|
1049 layout.dispose();
|
|
1050 layout = new TextLayout(device);
|
|
1051 layout.setFont(regularFont);
|
|
1052 StringBuffer tabBuffer = new StringBuffer(tabs);
|
|
1053 for (int i = 0; i < tabs; i++) {
|
|
1054 tabBuffer.append(' ');
|
|
1055 }
|
|
1056 layout.setText(tabBuffer.toString());
|
|
1057 tabWidth = layout.getBounds().width;
|
|
1058 layout.dispose();
|
|
1059 if (styledText !is null) {
|
|
1060 GC gc = new GC(styledText);
|
|
1061 averageCharWidth = gc.getFontMetrics().getAverageCharWidth();
|
|
1062 gc.dispose();
|
|
1063 }
|
|
1064 }
|
|
1065 void setLineAlignment(int startLine, int count, int alignment) {
|
|
1066 if (lines is null) lines = new LineInfo[lineCount];
|
|
1067 for (int i = startLine; i < startLine + count; i++) {
|
|
1068 if (lines[i] is null) {
|
|
1069 lines[i] = new LineInfo();
|
|
1070 }
|
|
1071 lines[i].flags |= ALIGNMENT;
|
|
1072 lines[i].alignment = alignment;
|
|
1073 }
|
|
1074 }
|
|
1075 void setLineBackground(int startLine, int count, Color background) {
|
|
1076 if (lines is null) lines = new LineInfo[lineCount];
|
|
1077 for (int i = startLine; i < startLine + count; i++) {
|
|
1078 if (lines[i] is null) {
|
|
1079 lines[i] = new LineInfo();
|
|
1080 }
|
|
1081 lines[i].flags |= BACKGROUND;
|
|
1082 lines[i].background = background;
|
|
1083 }
|
|
1084 }
|
|
1085 void setLineBullet(int startLine, int count, Bullet bullet) {
|
|
1086 if (bulletsIndices !is null) {
|
|
1087 bulletsIndices = null;
|
|
1088 bullets = null;
|
|
1089 }
|
|
1090 if (bullets is null) {
|
|
1091 if (bullet is null) return;
|
|
1092 bullets = new Bullet[1];
|
|
1093 bullets[0] = bullet;
|
|
1094 }
|
|
1095 int index = 0;
|
|
1096 while (index < bullets.length) {
|
|
1097 if (bullet is bullets[index]) break;
|
|
1098 index++;
|
|
1099 }
|
|
1100 if (bullet !is null) {
|
|
1101 if (index is bullets.length) {
|
|
1102 Bullet[] newBulletsList = new Bullet[bullets.length + 1];
|
|
1103 System.arraycopy(bullets, 0, newBulletsList, 0, bullets.length);
|
|
1104 newBulletsList[index] = bullet;
|
|
1105 bullets = newBulletsList;
|
|
1106 }
|
|
1107 bullet.addIndices(startLine, count);
|
|
1108 } else {
|
|
1109 updateBullets(startLine, count, 0, false);
|
|
1110 styledText.redrawLinesBullet(redrawLines);
|
|
1111 redrawLines = null;
|
|
1112 }
|
|
1113 }
|
|
1114 void setLineIndent(int startLine, int count, int indent) {
|
|
1115 if (lines is null) lines = new LineInfo[lineCount];
|
|
1116 for (int i = startLine; i < startLine + count; i++) {
|
|
1117 if (lines[i] is null) {
|
|
1118 lines[i] = new LineInfo();
|
|
1119 }
|
|
1120 lines[i].flags |= INDENT;
|
|
1121 lines[i].indent = indent;
|
|
1122 }
|
|
1123 }
|
|
1124 void setLineJustify(int startLine, int count, bool justify) {
|
|
1125 if (lines is null) lines = new LineInfo[lineCount];
|
|
1126 for (int i = startLine; i < startLine + count; i++) {
|
|
1127 if (lines[i] is null) {
|
|
1128 lines[i] = new LineInfo();
|
|
1129 }
|
|
1130 lines[i].flags |= JUSTIFY;
|
|
1131 lines[i].justify = justify;
|
|
1132 }
|
|
1133 }
|
|
1134 void setLineSegments(int startLine, int count, int[] segments) {
|
|
1135 if (lines is null) lines = new LineInfo[lineCount];
|
|
1136 for (int i = startLine; i < startLine + count; i++) {
|
|
1137 if (lines[i] is null) {
|
|
1138 lines[i] = new LineInfo();
|
|
1139 }
|
|
1140 lines[i].flags |= SEGMENTS;
|
|
1141 lines[i].segments = segments;
|
|
1142 }
|
|
1143 }
|
|
1144 void setStyleRanges (int[] newRanges, StyleRange[] newStyles) {
|
|
1145 if (newStyles is null) {
|
|
1146 stylesSetCount = styleCount = 0;
|
|
1147 ranges = null;
|
|
1148 styles = null;
|
|
1149 stylesSet = null;
|
|
1150 return;
|
|
1151 }
|
|
1152 if (newRanges is null && COMPACT_STYLES) {
|
|
1153 newRanges = new int[newStyles.length << 1];
|
|
1154 StyleRange[] tmpStyles = new StyleRange[newStyles.length];
|
|
1155 if (stylesSet is null) stylesSet = new StyleRange[4];
|
|
1156 for (int i = 0, j = 0; i < newStyles.length; i++) {
|
|
1157 StyleRange newStyle = newStyles[i];
|
|
1158 newRanges[j++] = newStyle.start;
|
|
1159 newRanges[j++] = newStyle.length;
|
|
1160 int index = 0;
|
|
1161 while (index < stylesSetCount) {
|
|
1162 if (stylesSet[index].similarTo(newStyle)) break;
|
|
1163 index++;
|
|
1164 }
|
|
1165 if (index is stylesSetCount) {
|
|
1166 if (stylesSetCount is stylesSet.length) {
|
|
1167 StyleRange[] tmpStylesSet = new StyleRange[stylesSetCount + 4];
|
|
1168 System.arraycopy(stylesSet, 0, tmpStylesSet, 0, stylesSetCount);
|
|
1169 stylesSet = tmpStylesSet;
|
|
1170 }
|
|
1171 stylesSet[stylesSetCount++] = newStyle;
|
|
1172 }
|
|
1173 tmpStyles[i] = stylesSet[index];
|
|
1174 }
|
|
1175 newStyles = tmpStyles;
|
|
1176 }
|
|
1177
|
|
1178 if (styleCount is 0) {
|
|
1179 if (newRanges !is null) {
|
|
1180 ranges = new int[newRanges.length];
|
|
1181 System.arraycopy(newRanges, 0, ranges, 0, ranges.length);
|
|
1182 }
|
|
1183 styles = new StyleRange[newStyles.length];
|
|
1184 System.arraycopy(newStyles, 0, styles, 0, styles.length);
|
|
1185 styleCount = newStyles.length;
|
|
1186 return;
|
|
1187 }
|
|
1188 if (newRanges !is null && ranges is null) {
|
|
1189 ranges = new int[styles.length << 1];
|
|
1190 for (int i = 0, j = 0; i < styleCount; i++) {
|
|
1191 ranges[j++] = styles[i].start;
|
|
1192 ranges[j++] = styles[i].length;
|
|
1193 }
|
|
1194 }
|
|
1195 if (newRanges is null && ranges !is null) {
|
|
1196 newRanges = new int[newStyles.length << 1];
|
|
1197 for (int i = 0, j = 0; i < newStyles.length; i++) {
|
|
1198 newRanges[j++] = newStyles[i].start;
|
|
1199 newRanges[j++] = newStyles[i].length;
|
|
1200 }
|
|
1201 }
|
|
1202 if (ranges !is null) {
|
|
1203 int rangeCount = styleCount << 1;
|
|
1204 int start = newRanges[0];
|
|
1205 int modifyStart = getRangeIndex(start, -1, rangeCount), modifyEnd;
|
|
1206 bool insert = modifyStart is rangeCount;
|
|
1207 if (!insert) {
|
|
1208 int end = newRanges[newRanges.length - 2] + newRanges[newRanges.length - 1];
|
|
1209 modifyEnd = getRangeIndex(end, modifyStart - 1, rangeCount);
|
|
1210 insert = modifyStart is modifyEnd && ranges[modifyStart] >= end;
|
|
1211 }
|
|
1212 if (insert) {
|
|
1213 addMerge(newRanges, newStyles, newRanges.length, modifyStart, modifyStart);
|
|
1214 return;
|
|
1215 }
|
|
1216 modifyEnd = modifyStart;
|
|
1217 int[] mergeRanges = new int[6];
|
|
1218 StyleRange[] mergeStyles = new StyleRange[3];
|
|
1219 for (int i = 0; i < newRanges.length; i += 2) {
|
|
1220 int newStart = newRanges[i];
|
|
1221 int newEnd = newStart + newRanges[i + 1];
|
|
1222 if (newStart is newEnd) continue;
|
|
1223 int modifyLast = 0, mergeCount = 0;
|
|
1224 while (modifyEnd < rangeCount) {
|
|
1225 if (newStart >= ranges[modifyStart] + ranges[modifyStart + 1]) modifyStart += 2;
|
|
1226 if (ranges[modifyEnd] + ranges[modifyEnd + 1] > newEnd) break;
|
|
1227 modifyEnd += 2;
|
|
1228 }
|
|
1229 if (ranges[modifyStart] < newStart && newStart < ranges[modifyStart] + ranges[modifyStart + 1]) {
|
|
1230 mergeStyles[mergeCount >> 1] = styles[modifyStart >> 1];
|
|
1231 mergeRanges[mergeCount] = ranges[modifyStart];
|
|
1232 mergeRanges[mergeCount + 1] = newStart - ranges[modifyStart];
|
|
1233 mergeCount += 2;
|
|
1234 }
|
|
1235 mergeStyles[mergeCount >> 1] = newStyles[i >> 1];
|
|
1236 mergeRanges[mergeCount] = newStart;
|
|
1237 mergeRanges[mergeCount + 1] = newRanges[i + 1];
|
|
1238 mergeCount += 2;
|
|
1239 if (modifyEnd < rangeCount && ranges[modifyEnd] < newEnd && newEnd < ranges[modifyEnd] + ranges[modifyEnd + 1]) {
|
|
1240 mergeStyles[mergeCount >> 1] = styles[modifyEnd >> 1];
|
|
1241 mergeRanges[mergeCount] = newEnd;
|
|
1242 mergeRanges[mergeCount + 1] = ranges[modifyEnd] + ranges[modifyEnd + 1] - newEnd;
|
|
1243 mergeCount += 2;
|
|
1244 modifyLast = 2;
|
|
1245 }
|
|
1246 int grow = addMerge(mergeRanges, mergeStyles, mergeCount, modifyStart, modifyEnd + modifyLast);
|
|
1247 rangeCount += grow;
|
|
1248 modifyStart = modifyEnd += grow;
|
|
1249 }
|
|
1250 } else {
|
|
1251 int start = newStyles[0].start;
|
|
1252 int modifyStart = getRangeIndex(start, -1, styleCount), modifyEnd;
|
|
1253 bool insert = modifyStart is styleCount;
|
|
1254 if (!insert) {
|
|
1255 int end = newStyles[newStyles.length - 1].start + newStyles[newStyles.length - 1].length;
|
|
1256 modifyEnd = getRangeIndex(end, modifyStart - 1, styleCount);
|
|
1257 insert = modifyStart is modifyEnd && styles[modifyStart].start >= end;
|
|
1258 }
|
|
1259 if (insert) {
|
|
1260 addMerge(newStyles, newStyles.length, modifyStart, modifyStart);
|
|
1261 return;
|
|
1262 }
|
|
1263 modifyEnd = modifyStart;
|
|
1264 StyleRange[] mergeStyles = new StyleRange[3];
|
|
1265 for (int i = 0; i < newStyles.length; i++) {
|
|
1266 StyleRange newStyle = newStyles[i], style;
|
|
1267 int newStart = newStyle.start;
|
|
1268 int newEnd = newStart + newStyle.length;
|
|
1269 if (newStart is newEnd) continue;
|
|
1270 int modifyLast = 0, mergeCount = 0;
|
|
1271 while (modifyEnd < styleCount) {
|
|
1272 if (newStart >= styles[modifyStart].start + styles[modifyStart].length) modifyStart++;
|
|
1273 if (styles[modifyEnd].start + styles[modifyEnd].length > newEnd) break;
|
|
1274 modifyEnd++;
|
|
1275 }
|
|
1276 style = styles[modifyStart];
|
|
1277 if (style.start < newStart && newStart < style.start + style.length) {
|
|
1278 style = mergeStyles[mergeCount++] = cast(StyleRange)style.clone();
|
|
1279 style.length = newStart - style.start;
|
|
1280 }
|
|
1281 mergeStyles[mergeCount++] = newStyle;
|
|
1282 if (modifyEnd < styleCount) {
|
|
1283 style = styles[modifyEnd];
|
|
1284 if (style.start < newEnd && newEnd < style.start + style.length) {
|
|
1285 style = mergeStyles[mergeCount++] = cast(StyleRange)style.clone();
|
|
1286 style.length += style.start - newEnd;
|
|
1287 style.start = newEnd;
|
|
1288 modifyLast = 1;
|
|
1289 }
|
|
1290 }
|
|
1291 int grow = addMerge(mergeStyles, mergeCount, modifyStart, modifyEnd + modifyLast);
|
|
1292 modifyStart = modifyEnd += grow;
|
|
1293 }
|
|
1294 }
|
|
1295 }
|
|
1296 void textChanging(TextChangingEvent event) {
|
|
1297 int start = event.start;
|
|
1298 int newCharCount = event.newCharCount, replaceCharCount = event.replaceCharCount;
|
|
1299 int newLineCount = event.newLineCount, replaceLineCount = event.replaceLineCount;
|
|
1300
|
|
1301 updateRanges(start, replaceCharCount, newCharCount);
|
|
1302
|
|
1303 int startLine = content.getLineAtOffset(start);
|
|
1304 if (replaceCharCount is content.getCharCount()) lines = null;
|
|
1305 if (replaceLineCount is lineCount) {
|
|
1306 lineCount = newLineCount;
|
|
1307 lineWidth = new int[lineCount];
|
|
1308 lineHeight = new int[lineCount];
|
|
1309 reset(0, lineCount);
|
|
1310 } else {
|
|
1311 int delta = newLineCount - replaceLineCount;
|
|
1312 if (lineCount + delta > lineWidth.length) {
|
|
1313 int[] newWidths = new int[lineCount + delta + GROW];
|
|
1314 System.arraycopy(lineWidth, 0, newWidths, 0, lineCount);
|
|
1315 lineWidth = newWidths;
|
|
1316 int[] newHeights = new int[lineCount + delta + GROW];
|
|
1317 System.arraycopy(lineHeight, 0, newHeights, 0, lineCount);
|
|
1318 lineHeight = newHeights;
|
|
1319 }
|
|
1320 if (lines !is null) {
|
|
1321 if (lineCount + delta > lines.length) {
|
|
1322 LineInfo[] newLines = new LineInfo[lineCount + delta + GROW];
|
|
1323 System.arraycopy(lines, 0, newLines, 0, lineCount);
|
|
1324 lines = newLines;
|
|
1325 }
|
|
1326 }
|
|
1327 int startIndex = startLine + replaceLineCount + 1;
|
|
1328 int endIndex = startLine + newLineCount + 1;
|
|
1329 System.arraycopy(lineWidth, startIndex, lineWidth, endIndex, lineCount - startIndex);
|
|
1330 System.arraycopy(lineHeight, startIndex, lineHeight, endIndex, lineCount - startIndex);
|
|
1331 for (int i = startLine; i < endIndex; i++) {
|
|
1332 lineWidth[i] = lineHeight[i] = -1;
|
|
1333 }
|
|
1334 for (int i = lineCount + delta; i < lineCount; i++) {
|
|
1335 lineWidth[i] = lineHeight[i] = -1;
|
|
1336 }
|
|
1337 if (layouts !is null) {
|
|
1338 int layoutStartLine = startLine - topIndex;
|
|
1339 int layoutEndLine = layoutStartLine + replaceLineCount + 1;
|
|
1340 for (int i = layoutStartLine; i < layoutEndLine; i++) {
|
|
1341 if (0 <= i && i < layouts.length) {
|
|
1342 if (layouts[i] !is null) layouts[i].dispose();
|
|
1343 layouts[i] = null;
|
|
1344 if (bullets !is null && bulletsIndices !is null) bullets[i] = null;
|
|
1345 }
|
|
1346 }
|
|
1347 if (delta > 0) {
|
|
1348 for (int i = layouts.length - 1; i >= layoutEndLine; i--) {
|
|
1349 if (0 <= i && i < layouts.length) {
|
|
1350 endIndex = i + delta;
|
|
1351 if (0 <= endIndex && endIndex < layouts.length) {
|
|
1352 layouts[endIndex] = layouts[i];
|
|
1353 layouts[i] = null;
|
|
1354 if (bullets !is null && bulletsIndices !is null) {
|
|
1355 bullets[endIndex] = bullets[i];
|
|
1356 bulletsIndices[endIndex] = bulletsIndices[i];
|
|
1357 bullets[i] = null;
|
|
1358 }
|
|
1359 } else {
|
|
1360 if (layouts[i] !is null) layouts[i].dispose();
|
|
1361 layouts[i] = null;
|
|
1362 if (bullets !is null && bulletsIndices !is null) bullets[i] = null;
|
|
1363 }
|
|
1364 }
|
|
1365 }
|
|
1366 } else if (delta < 0) {
|
|
1367 for (int i = layoutEndLine; i < layouts.length; i++) {
|
|
1368 if (0 <= i && i < layouts.length) {
|
|
1369 endIndex = i + delta;
|
|
1370 if (0 <= endIndex && endIndex < layouts.length) {
|
|
1371 layouts[endIndex] = layouts[i];
|
|
1372 layouts[i] = null;
|
|
1373 if (bullets !is null && bulletsIndices !is null) {
|
|
1374 bullets[endIndex] = bullets[i];
|
|
1375 bulletsIndices[endIndex] = bulletsIndices[i];
|
|
1376 bullets[i] = null;
|
|
1377 }
|
|
1378 } else {
|
|
1379 if (layouts[i] !is null) layouts[i].dispose();
|
|
1380 layouts[i] = null;
|
|
1381 if (bullets !is null && bulletsIndices !is null) bullets[i] = null;
|
|
1382 }
|
|
1383 }
|
|
1384 }
|
|
1385 }
|
|
1386 }
|
|
1387 if (replaceLineCount !is 0 || newLineCount !is 0) {
|
|
1388 int startLineOffset = content.getOffsetAtLine(startLine);
|
|
1389 if (startLineOffset !is start) startLine++;
|
|
1390 updateBullets(startLine, replaceLineCount, newLineCount, true);
|
|
1391 if (lines !is null) {
|
|
1392 startIndex = startLine + replaceLineCount;
|
|
1393 endIndex = startLine + newLineCount;
|
|
1394 System.arraycopy(lines, startIndex, lines, endIndex, lineCount - startIndex);
|
|
1395 for (int i = startLine; i < endIndex; i++) {
|
|
1396 lines[i] = null;
|
|
1397 }
|
|
1398 for (int i = lineCount + delta; i < lineCount; i++) {
|
|
1399 lines[i] = null;
|
|
1400 }
|
|
1401 }
|
|
1402 }
|
|
1403 lineCount += delta;
|
|
1404 if (maxWidthLineIndex !is -1 && startLine <= maxWidthLineIndex && maxWidthLineIndex <= startLine + replaceLineCount) {
|
|
1405 maxWidth = 0;
|
|
1406 maxWidthLineIndex = -1;
|
|
1407 for (int i = 0; i < lineCount; i++) {
|
|
1408 if (lineWidth[i] > maxWidth) {
|
|
1409 maxWidth = lineWidth[i];
|
|
1410 maxWidthLineIndex = i;
|
|
1411 }
|
|
1412 }
|
|
1413 }
|
|
1414 }
|
|
1415 }
|
|
1416 void updateBullets(int startLine, int replaceLineCount, int newLineCount, bool update) {
|
|
1417 if (bullets is null) return;
|
|
1418 if (bulletsIndices !is null) return;
|
|
1419 for (int i = 0; i < bullets.length; i++) {
|
|
1420 Bullet bullet = bullets[i];
|
|
1421 int[] lines = bullet.removeIndices(startLine, replaceLineCount, newLineCount, update);
|
|
1422 if (lines !is null) {
|
|
1423 if (redrawLines is null) {
|
|
1424 redrawLines = lines;
|
|
1425 } else {
|
|
1426 int[] newRedrawBullets = new int[redrawLines.length + lines.length];
|
|
1427 System.arraycopy(redrawLines, 0, newRedrawBullets, 0, redrawLines.length);
|
|
1428 System.arraycopy(lines, 0, newRedrawBullets, redrawLines.length, lines.length);
|
|
1429 redrawLines = newRedrawBullets;
|
|
1430 }
|
|
1431 }
|
|
1432 }
|
|
1433 int removed = 0;
|
|
1434 for (int i = 0; i < bullets.length; i++) {
|
|
1435 if (bullets[i].size() is 0) removed++;
|
|
1436 }
|
|
1437 if (removed > 0) {
|
|
1438 if (removed is bullets.length) {
|
|
1439 bullets = null;
|
|
1440 } else {
|
|
1441 Bullet[] newBulletsList = new Bullet[bullets.length - removed];
|
|
1442 for (int i = 0, j = 0; i < bullets.length; i++) {
|
|
1443 Bullet bullet = bullets[i];
|
|
1444 if (bullet.size() > 0) newBulletsList[j++] = bullet;
|
|
1445 }
|
|
1446 bullets = newBulletsList;
|
|
1447 }
|
|
1448 }
|
|
1449 }
|
|
1450 void updateRanges(int start, int replaceCharCount, int newCharCount) {
|
|
1451 if (styleCount is 0 || (replaceCharCount is 0 && newCharCount is 0)) return;
|
|
1452 if (ranges !is null) {
|
|
1453 int rangeCount = styleCount << 1;
|
|
1454 int modifyStart = getRangeIndex(start, -1, rangeCount);
|
|
1455 if (modifyStart is rangeCount) return;
|
|
1456 int end = start + replaceCharCount;
|
|
1457 int modifyEnd = getRangeIndex(end, modifyStart - 1, rangeCount);
|
|
1458 int offset = newCharCount - replaceCharCount;
|
|
1459 if (modifyStart is modifyEnd && ranges[modifyStart] < start && end < ranges[modifyEnd] + ranges[modifyEnd + 1]) {
|
|
1460 if (newCharCount is 0) {
|
|
1461 ranges[modifyStart + 1] -= replaceCharCount;
|
|
1462 modifyEnd += 2;
|
|
1463 } else {
|
|
1464 if (rangeCount + 2 > ranges.length) {
|
|
1465 int[] newRanges = new int[ranges.length + (GROW << 1)];
|
|
1466 System.arraycopy(ranges, 0, newRanges, 0, rangeCount);
|
|
1467 ranges = newRanges;
|
|
1468 StyleRange[] newStyles = new StyleRange[styles.length + GROW];
|
|
1469 System.arraycopy(styles, 0, newStyles, 0, styleCount);
|
|
1470 styles = newStyles;
|
|
1471 }
|
|
1472 System.arraycopy(ranges, modifyStart + 2, ranges, modifyStart + 4, rangeCount - (modifyStart + 2));
|
|
1473 System.arraycopy(styles, (modifyStart + 2) >> 1, styles, (modifyStart + 4) >> 1, styleCount - ((modifyStart + 2) >> 1));
|
|
1474 ranges[modifyStart + 3] = ranges[modifyStart] + ranges[modifyStart + 1] - end;
|
|
1475 ranges[modifyStart + 2] = start + newCharCount;
|
|
1476 ranges[modifyStart + 1] = start - ranges[modifyStart];
|
|
1477 styles[(modifyStart >> 1) + 1] = styles[modifyStart >> 1];
|
|
1478 rangeCount += 2;
|
|
1479 styleCount++;
|
|
1480 modifyEnd += 4;
|
|
1481 }
|
|
1482 if (offset !is 0) {
|
|
1483 for (int i = modifyEnd; i < rangeCount; i += 2) {
|
|
1484 ranges[i] += offset;
|
|
1485 }
|
|
1486 }
|
|
1487 } else {
|
|
1488 if (ranges[modifyStart] < start && start < ranges[modifyStart] + ranges[modifyStart + 1]) {
|
|
1489 ranges[modifyStart + 1] = start - ranges[modifyStart];
|
|
1490 modifyStart += 2;
|
|
1491 }
|
|
1492 if (modifyEnd < rangeCount && ranges[modifyEnd] < end && end < ranges[modifyEnd] + ranges[modifyEnd + 1]) {
|
|
1493 ranges[modifyEnd + 1] = ranges[modifyEnd] + ranges[modifyEnd + 1] - end;
|
|
1494 ranges[modifyEnd] = end;
|
|
1495 }
|
|
1496 if (offset !is 0) {
|
|
1497 for (int i = modifyEnd; i < rangeCount; i += 2) {
|
|
1498 ranges[i] += offset;
|
|
1499 }
|
|
1500 }
|
|
1501 System.arraycopy(ranges, modifyEnd, ranges, modifyStart, rangeCount - modifyEnd);
|
|
1502 System.arraycopy(styles, modifyEnd >> 1, styles, modifyStart >> 1, styleCount - (modifyEnd >> 1));
|
|
1503 styleCount -= (modifyEnd - modifyStart) >> 1;
|
|
1504 }
|
|
1505 } else {
|
|
1506 int modifyStart = getRangeIndex(start, -1, styleCount);
|
|
1507 if (modifyStart is styleCount) return;
|
|
1508 int end = start + replaceCharCount;
|
|
1509 int modifyEnd = getRangeIndex(end, modifyStart - 1, styleCount);
|
|
1510 int offset = newCharCount - replaceCharCount;
|
|
1511 if (modifyStart is modifyEnd && styles[modifyStart].start < start && end < styles[modifyEnd].start + styles[modifyEnd].length) {
|
|
1512 if (newCharCount is 0) {
|
|
1513 styles[modifyStart].length -= replaceCharCount;
|
|
1514 modifyEnd++;
|
|
1515 } else {
|
|
1516 if (styleCount + 1 > styles.length) {
|
|
1517 StyleRange[] newStyles = new StyleRange[styles.length + GROW];
|
|
1518 System.arraycopy(styles, 0, newStyles, 0, styleCount);
|
|
1519 styles = newStyles;
|
|
1520 }
|
|
1521 System.arraycopy(styles, modifyStart + 1, styles, modifyStart + 2, styleCount - (modifyStart + 1));
|
|
1522 styles[modifyStart + 1] = cast(StyleRange)styles[modifyStart].clone();
|
|
1523 styles[modifyStart + 1].length = styles[modifyStart].start + styles[modifyStart].length - end;
|
|
1524 styles[modifyStart + 1].start = start + newCharCount;
|
|
1525 styles[modifyStart].length = start - styles[modifyStart].start;
|
|
1526 styleCount++;
|
|
1527 modifyEnd += 2;
|
|
1528 }
|
|
1529 if (offset !is 0) {
|
|
1530 for (int i = modifyEnd; i < styleCount; i++) {
|
|
1531 styles[i].start += offset;
|
|
1532 }
|
|
1533 }
|
|
1534 } else {
|
|
1535 if (styles[modifyStart].start < start && start < styles[modifyStart].start + styles[modifyStart].length) {
|
|
1536 styles[modifyStart].length = start - styles[modifyStart].start;
|
|
1537 modifyStart++;
|
|
1538 }
|
|
1539 if (modifyEnd < styleCount && styles[modifyEnd].start < end && end < styles[modifyEnd].start + styles[modifyEnd].length) {
|
|
1540 styles[modifyEnd].length = styles[modifyEnd].start + styles[modifyEnd].length - end;
|
|
1541 styles[modifyEnd].start = end;
|
|
1542 }
|
|
1543 if (offset !is 0) {
|
|
1544 for (int i = modifyEnd; i < styleCount; i++) {
|
|
1545 styles[i].start += offset;
|
|
1546 }
|
|
1547 }
|
|
1548 System.arraycopy(styles, modifyEnd, styles, modifyStart, styleCount - modifyEnd);
|
|
1549 styleCount -= modifyEnd - modifyStart;
|
|
1550 }
|
|
1551 }
|
|
1552 }
|
|
1553 }
|