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