1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Anton Leherbauer (Wind River Systems) - Bug 439419
14 * Angelo Zerr <angelo.zerr@gmail.com> - Customize different line spacing of StyledText - Bug 522020
15 *******************************************************************************/
16 package org.eclipse.swt.custom;
17
18
19 import java.util.*;
20 import java.util.List;
21
22 import org.eclipse.swt.*;
23 import org.eclipse.swt.graphics.*;
24 import org.eclipse.swt.widgets.*;
25
26 /**
27 * A StyledTextRenderer renders the content of a StyledText widget.
28 * This class can be used to render to the display or to a printer.
29 */
30 class StyledTextRenderer {
31 Device device;
32 StyledText styledText;
33 StyledTextContent content;
34
35 /* Custom line spacing */
36 StyledTextLineSpacingProvider lineSpacingProvider;
37 boolean lineSpacingComputing;
38
39 /* Font info */
40 Font regularFont, boldFont, italicFont, boldItalicFont;
41 int tabWidth;
42 int ascent, descent;
43 int averageCharWidth;
44 int tabLength; //tab length in spaces
45
46 /* Line data */
47 int topIndex = -1;
48 TextLayout[] layouts;
49 int lineCount;
50 LineSizeInfo[] lineSizes;
51 LineInfo[] lines;
52 int maxWidth;
53 int maxWidthLineIndex;
54 float averageLineHeight;
55 int linesInAverageLineHeight;
56 boolean idleRunning;
57
58 /* Bullet */
59 Bullet[] bullets;
60 int[] bulletsIndices;
61 int[] redrawLines;
62
63 /* Style data */
64 int[] ranges;
65 int styleCount;
66 StyleRange[] styles;
67 StyleRange[] stylesSet;
68 int stylesSetCount = 0;
69 boolean hasLinks, fixedPitch;
70 final static int BULLET_MARGIN = 8;
71
72 final static boolean COMPACT_STYLES = true;
73 final static boolean MERGE_STYLES = true;
74
75 final static int GROW = 32;
76 final static int IDLE_TIME = 50;
77 final static int CACHE_SIZE = 300;
78
79 final static int BACKGROUND = 1 << 0;
80 final static int ALIGNMENT = 1 << 1;
81 final static int INDENT = 1 << 2;
82 final static int JUSTIFY = 1 << 3;
83 final static int SEGMENTS = 1 << 5;
84 final static int TABSTOPS = 1 << 6;
85 final static int WRAP_INDENT = 1 << 7;
86 final static int SEGMENT_CHARS = 1 << 8;
87 final static int VERTICAL_INDENT = 1 << 9;
88
89 static class LineSizeInfo {
90
91 private static final int RESETED_SIZE = -1;
92
93 /* Line size */
94 int height;
95 int width;
96
LineSizeInfo()97 public LineSizeInfo() {
98 resetSize();
99 }
100
101 /**
102 * Reset the line size.
103 */
resetSize()104 void resetSize() {
105 height = RESETED_SIZE;
106 width = RESETED_SIZE;
107 }
108
109 /**
110 * Returns true if the TextLayout get from the layout pool can be directly used
111 * or must be refreshed with styles.
112 *
113 * @return true if the TextLayout get from the layout pool can be directly used
114 * or must be refreshed with styles.
115 */
canLayout()116 boolean canLayout() {
117 return !needsRecalculateWidth();
118 }
119
120 /**
121 * Returns true if it needs to recalculate the line size and false
122 * otherwise.
123 *
124 * @return true if it needs to recalculate the line size and false
125 * otherwise.
126 */
needsRecalculateSize()127 boolean needsRecalculateSize() {
128 return needsRecalculateWidth() || needsRecalculateHeight();
129 }
130
131 /**
132 * Returns true if it needs to recalculate the line width and false
133 * otherwise.
134 *
135 * @return true if it needs to recalculate the line width and false
136 * otherwise.
137 */
needsRecalculateWidth()138 boolean needsRecalculateWidth() {
139 return width == RESETED_SIZE;
140 }
141
142 /**
143 * Returns true if it needs to recalculate the line height and false
144 * otherwise.
145 *
146 * @return true if it needs to recalculate the line height and false
147 * otherwise.
148 */
needsRecalculateHeight()149 boolean needsRecalculateHeight() {
150 return height == RESETED_SIZE;
151 }
152 }
153
154 static class LineInfo {
155 int flags;
156 Color background;
157 int alignment;
158 int indent;
159 int wrapIndent;
160 boolean justify;
161 int[] segments;
162 char[] segmentsChars;
163 int[] tabStops;
164 int verticalIndent;
165
LineInfo()166 public LineInfo() {
167 }
LineInfo(LineInfo info)168 public LineInfo(LineInfo info) {
169 if (info != null) {
170 flags = info.flags;
171 background = info.background;
172 alignment = info.alignment;
173 indent = info.indent;
174 wrapIndent = info.wrapIndent;
175 justify = info.justify;
176 segments = info.segments;
177 segmentsChars = info.segmentsChars;
178 tabStops = info.tabStops;
179 verticalIndent = info.verticalIndent;
180 }
181 }
182 }
cap(TextLayout layout, int offset)183 static int cap (TextLayout layout, int offset) {
184 if (layout == null) return offset;
185 return Math.min (layout.getText().length() -1, Math.max (0, offset));
186 }
187
StyledTextRenderer(Device device, StyledText styledText)188 StyledTextRenderer(Device device, StyledText styledText) {
189 this.device = device;
190 this.styledText = styledText;
191 }
addMerge(int[] mergeRanges, StyleRange[] mergeStyles, int mergeCount, int modifyStart, int modifyEnd)192 int addMerge(int[] mergeRanges, StyleRange[] mergeStyles, int mergeCount, int modifyStart, int modifyEnd) {
193 int rangeCount = styleCount << 1;
194 StyleRange endStyle = null;
195 int endStart = 0, endLength = 0;
196 if (modifyEnd < rangeCount) {
197 endStyle = styles[modifyEnd >> 1];
198 endStart = ranges[modifyEnd];
199 endLength = ranges[modifyEnd + 1];
200 }
201 int grow = mergeCount - (modifyEnd - modifyStart);
202 if (rangeCount + grow >= ranges.length) {
203 int[] tmpRanges = new int[ranges.length + grow + (GROW << 1)];
204 System.arraycopy(ranges, 0, tmpRanges, 0, modifyStart);
205 StyleRange[] tmpStyles = new StyleRange[styles.length + (grow >> 1) + GROW];
206 System.arraycopy(styles, 0, tmpStyles, 0, modifyStart >> 1);
207 if (rangeCount > modifyEnd) {
208 System.arraycopy(ranges, modifyEnd, tmpRanges, modifyStart + mergeCount, rangeCount - modifyEnd);
209 System.arraycopy(styles, modifyEnd >> 1, tmpStyles, (modifyStart + mergeCount) >> 1, styleCount - (modifyEnd >> 1));
210 }
211 ranges = tmpRanges;
212 styles = tmpStyles;
213 } else {
214 if (rangeCount > modifyEnd) {
215 System.arraycopy(ranges, modifyEnd, ranges, modifyStart + mergeCount, rangeCount - modifyEnd);
216 System.arraycopy(styles, modifyEnd >> 1, styles, (modifyStart + mergeCount) >> 1, styleCount - (modifyEnd >> 1));
217 }
218 }
219 if (MERGE_STYLES) {
220 int j = modifyStart;
221 for (int i = 0; i < mergeCount; i += 2) {
222 if (j > 0 && ranges[j - 2] + ranges[j - 1] == mergeRanges[i] && mergeStyles[i >> 1].similarTo(styles[(j - 2) >> 1])) {
223 ranges[j - 1] += mergeRanges[i + 1];
224 } else {
225 styles[j >> 1] = mergeStyles[i >> 1];
226 ranges[j++] = mergeRanges[i];
227 ranges[j++] = mergeRanges[i + 1];
228 }
229 }
230 if (endStyle != null && ranges[j - 2] + ranges[j - 1] == endStart && endStyle.similarTo(styles[(j - 2) >> 1])) {
231 ranges[j - 1] += endLength;
232 modifyEnd += 2;
233 mergeCount += 2;
234 }
235 if (rangeCount > modifyEnd) {
236 System.arraycopy(ranges, modifyStart + mergeCount, ranges, j, rangeCount - modifyEnd);
237 System.arraycopy(styles, (modifyStart + mergeCount) >> 1, styles, j >> 1, styleCount - (modifyEnd >> 1));
238 }
239 grow = (j - modifyStart) - (modifyEnd - modifyStart);
240 } else {
241 System.arraycopy(mergeRanges, 0, ranges, modifyStart, mergeCount);
242 System.arraycopy(mergeStyles, 0, styles, modifyStart >> 1, mergeCount >> 1);
243 }
244 styleCount += grow >> 1;
245 return grow;
246 }
addMerge(StyleRange[] mergeStyles, int mergeCount, int modifyStart, int modifyEnd)247 int addMerge(StyleRange[] mergeStyles, int mergeCount, int modifyStart, int modifyEnd) {
248 int grow = mergeCount - (modifyEnd - modifyStart);
249 StyleRange endStyle = null;
250 if (modifyEnd < styleCount) endStyle = styles[modifyEnd];
251 if (styleCount + grow >= styles.length) {
252 StyleRange[] tmpStyles = new StyleRange[styles.length + grow + GROW];
253 System.arraycopy(styles, 0, tmpStyles, 0, modifyStart);
254 if (styleCount > modifyEnd) {
255 System.arraycopy(styles, modifyEnd, tmpStyles, modifyStart + mergeCount, styleCount - modifyEnd);
256 }
257 styles = tmpStyles;
258 } else {
259 if (styleCount > modifyEnd) {
260 System.arraycopy(styles, modifyEnd, styles, modifyStart + mergeCount, styleCount - modifyEnd);
261 }
262 }
263 if (MERGE_STYLES) {
264 int j = modifyStart;
265 for (int i = 0; i < mergeCount; i++) {
266 StyleRange newStyle = mergeStyles[i], style;
267 if (j > 0 && (style = styles[j - 1]).start + style.length == newStyle.start && newStyle.similarTo(style)) {
268 style.length += newStyle.length;
269 } else {
270 styles[j++] = newStyle;
271 }
272 }
273 StyleRange style = styles[j - 1];
274 if (endStyle != null && style.start + style.length == endStyle.start && endStyle.similarTo(style)) {
275 style.length += endStyle.length;
276 modifyEnd++;
277 mergeCount++;
278 }
279 if (styleCount > modifyEnd) {
280 System.arraycopy(styles, modifyStart + mergeCount, styles, j, styleCount - modifyEnd);
281 }
282 grow = (j - modifyStart) - (modifyEnd - modifyStart);
283 } else {
284 System.arraycopy(mergeStyles, 0, styles, modifyStart, mergeCount);
285 }
286 styleCount += grow;
287 return grow;
288 }
calculate(int startLine, int lineCount)289 void calculate(int startLine, int lineCount) {
290 int endLine = startLine + lineCount;
291 if (startLine < 0 || endLine > lineSizes.length) {
292 return;
293 }
294 int hTrim = styledText.leftMargin + styledText.rightMargin + styledText.getCaretWidth();
295 for (int i = startLine; i < endLine; i++) {
296 LineSizeInfo line = getLineSize(i);
297 if (line.needsRecalculateSize()) {
298 TextLayout layout = getTextLayout(i);
299 Rectangle rect = layout.getBounds();
300 line.width = rect.width + hTrim;
301 line.height = rect.height;
302 averageLineHeight += (line.height - Math.round(averageLineHeight)) / ++linesInAverageLineHeight;
303 disposeTextLayout(layout);
304 }
305 if (line.width > maxWidth) {
306 maxWidth = line.width;
307 maxWidthLineIndex = i;
308 }
309 }
310 }
getLineSize(int i)311 LineSizeInfo getLineSize(int i) {
312 if (lineSizes[i] == null) {
313 lineSizes[i] = new LineSizeInfo();
314 }
315 return lineSizes[i];
316 }
calculateClientArea()317 void calculateClientArea () {
318 int index = Math.max (0, styledText.getTopIndex());
319 int lineCount = content.getLineCount();
320 int height = styledText.getClientArea().height;
321 int y = 0;
322 /*
323 * There exists a possibility of ArrayIndexOutOfBounds Exception in
324 * below code, exact scenario not known. To avoid this exception added
325 * check for 'index' value, refer Bug 471192.
326 */
327 while (height > y && lineCount > index && lineSizes.length > index) {
328 calculate(index, 1);
329 y += lineSizes[index++].height;
330 }
331 }
calculateIdle()332 void calculateIdle () {
333 if (idleRunning) return;
334 Runnable runnable = new Runnable() {
335 @Override
336 public void run() {
337 if (styledText == null) return;
338 int i;
339 long start = System.currentTimeMillis();
340 for (i = 0; i < lineCount; i++) {
341 LineSizeInfo line = getLineSize(i);
342 if (line.needsRecalculateSize()) {
343 calculate(i, 1);
344 if (System.currentTimeMillis() - start > IDLE_TIME) break;
345 }
346 }
347 if (i < lineCount) {
348 Display display = styledText.getDisplay();
349 display.asyncExec(this);
350 } else {
351 idleRunning = false;
352 styledText.setScrollBars(true);
353 ScrollBar bar = styledText.getVerticalBar();
354 if (bar != null) {
355 bar.setSelection(styledText.getVerticalScrollOffset());
356 }
357 }
358 }
359 };
360 Display display = styledText.getDisplay();
361 display.asyncExec(runnable);
362 idleRunning = true;
363 }
clearLineBackground(int startLine, int count)364 void clearLineBackground(int startLine, int count) {
365 if (lines == null) return;
366 for (int i = startLine; i < startLine + count; i++) {
367 LineInfo info = lines[i];
368 if (info != null) {
369 info.flags &= ~BACKGROUND;
370 info.background = null;
371 if (info.flags == 0) lines[i] = null;
372 }
373 }
374 }
clearLineStyle(int startLine, int count)375 void clearLineStyle(int startLine, int count) {
376 if (lines == null) return;
377 for (int i = startLine; i < startLine + count; i++) {
378 LineInfo info = lines[i];
379 if (info != null) {
380 info.flags &= ~(ALIGNMENT | INDENT | VERTICAL_INDENT | WRAP_INDENT | JUSTIFY | TABSTOPS);
381 if (info.flags == 0) lines[i] = null;
382 }
383 }
384 }
copyInto(StyledTextRenderer renderer)385 void copyInto(StyledTextRenderer renderer) {
386 if (ranges != null) {
387 int[] newRanges = renderer.ranges = new int[styleCount << 1];
388 System.arraycopy(ranges, 0, newRanges, 0, newRanges.length);
389 }
390 if (styles != null) {
391 StyleRange[] newStyles = renderer.styles = new StyleRange[styleCount];
392 for (int i = 0; i < newStyles.length; i++) {
393 newStyles[i] = (StyleRange)styles[i].clone();
394 }
395 renderer.styleCount = styleCount;
396 }
397 if (lines != null) {
398 LineInfo[] newLines = renderer.lines = new LineInfo[lineCount];
399 for (int i = 0; i < newLines.length; i++) {
400 newLines[i] = new LineInfo(lines[i]);
401 }
402 renderer.lineCount = lineCount;
403 }
404 }
dispose()405 void dispose() {
406 if (boldFont != null) boldFont.dispose();
407 if (italicFont != null) italicFont.dispose();
408 if (boldItalicFont != null) boldItalicFont.dispose();
409 boldFont = italicFont = boldItalicFont = null;
410 reset();
411 content = null;
412 device = null;
413 styledText = null;
414 }
disposeTextLayout(TextLayout layout)415 void disposeTextLayout (TextLayout layout) {
416 if (layouts != null) {
417 for (TextLayout l : layouts) {
418 if (l == layout) return;
419 }
420 }
421 layout.dispose();
422 }
drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent)423 void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) {
424 StyleRange style = bullet.style;
425 GlyphMetrics metrics = style.metrics;
426 Color color = style.foreground;
427 if (color != null) gc.setForeground(color);
428 Font font = style.font;
429 if (font != null) gc.setFont(font);
430 String string = "";
431 int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER);
432 switch (type) {
433 case ST.BULLET_DOT: string = "\u2022"; break;
434 case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break;
435 case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break;
436 case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break;
437 }
438 if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text;
439 Display display = styledText.getDisplay();
440 TextLayout layout = new TextLayout(display);
441 layout.setText(string);
442 layout.setAscent(lineAscent);
443 layout.setDescent(lineDescent);
444 style = (StyleRange)style.clone();
445 style.metrics = null;
446 if (style.font == null) style.font = getFont(style.fontStyle);
447 layout.setStyle(style, 0, string.length());
448 int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - BULLET_MARGIN);
449 layout.draw(gc, x, paintY);
450 layout.dispose();
451 }
drawLine(int lineIndex, int paintX, int paintY, GC gc, Color widgetBackground, Color widgetForeground)452 int drawLine(int lineIndex, int paintX, int paintY, GC gc, Color widgetBackground, Color widgetForeground) {
453 TextLayout layout = getTextLayout(lineIndex);
454 String line = content.getLine(lineIndex);
455 int lineOffset = content.getOffsetAtLine(lineIndex);
456 int lineLength = line.length();
457 Point selection = styledText.getSelection();
458 int selectionStart = selection.x - lineOffset;
459 int selectionEnd = selection.y - lineOffset;
460 if (styledText.getBlockSelection()) {
461 selectionStart = selectionEnd = 0;
462 }
463 Rectangle client = styledText.getClientArea();
464 Color lineBackground = getLineBackground(lineIndex, null);
465 StyledTextEvent event = styledText.getLineBackgroundData(lineOffset, line);
466 if (event != null && event.lineBackground != null) lineBackground = event.lineBackground;
467 int height = layout.getBounds().height;
468 int verticalIndent = layout.getVerticalIndent();
469 if (lineBackground != null) {
470 if (verticalIndent > 0) {
471 gc.setBackground(widgetBackground);
472 gc.fillRectangle(client.x, paintY, client.width, verticalIndent);
473 }
474 gc.setBackground(lineBackground);
475 gc.fillRectangle(client.x, paintY + verticalIndent, client.width, height - verticalIndent);
476 } else {
477 gc.setBackground(widgetBackground);
478 styledText.drawBackground(gc, client.x, paintY, client.width, height);
479 }
480 gc.setForeground(widgetForeground);
481 if (selectionStart == selectionEnd || (selectionEnd <= 0 && selectionStart > lineLength - 1)) {
482 layout.draw(gc, paintX, paintY);
483 } else {
484 int start = Math.max(0, selectionStart);
485 int end = Math.min(lineLength, selectionEnd);
486 Color selectionFg = styledText.getSelectionForeground();
487 Color selectionBg = styledText.getSelectionBackground();
488 int flags;
489 if ((styledText.getStyle() & SWT.FULL_SELECTION) != 0) {
490 flags = SWT.FULL_SELECTION;
491 } else {
492 flags = SWT.DELIMITER_SELECTION;
493 }
494 if (selectionStart <= lineLength && lineLength < selectionEnd ) {
495 flags |= SWT.LAST_LINE_SELECTION;
496 }
497 layout.draw(gc, paintX, paintY, start, end - 1, selectionFg, selectionBg, flags);
498 }
499
500 // draw objects
501 Bullet bullet = null;
502 int bulletIndex = -1;
503 if (bullets != null) {
504 if (bulletsIndices != null) {
505 int index = lineIndex - topIndex;
506 if (0 <= index && index < CACHE_SIZE) {
507 bullet = bullets[index];
508 bulletIndex = bulletsIndices[index];
509 }
510 } else {
511 for (Bullet b : bullets) {
512 bullet = b;
513 bulletIndex = bullet.indexOf(lineIndex);
514 if (bulletIndex != -1) break;
515 }
516 }
517 }
518 if (bulletIndex != -1 && bullet != null) {
519 FontMetrics metrics = layout.getLineMetrics(0);
520 int lineAscent = metrics.getAscent() + metrics.getLeading();
521 if (bullet.type == ST.BULLET_CUSTOM) {
522 bullet.style.start = lineOffset;
523 styledText.paintObject(gc, paintX, paintY, lineAscent, metrics.getDescent(), bullet.style, bullet, bulletIndex);
524 } else {
525 drawBullet(bullet, gc, paintX, paintY, bulletIndex, lineAscent, metrics.getDescent());
526 }
527 }
528 TextStyle[] styles = layout.getStyles();
529 int[] ranges = null;
530 for (int i = 0; i < styles.length; i++) {
531 if (styles[i].metrics != null) {
532 if (ranges == null) ranges = layout.getRanges();
533 int start = ranges[i << 1];
534 int length = ranges[(i << 1) + 1] - start + 1;
535 Point point = layout.getLocation(start, false);
536 FontMetrics metrics = layout.getLineMetrics(layout.getLineIndex(start));
537 StyleRange style = (StyleRange)((StyleRange)styles[i]).clone();
538 style.start = start + lineOffset;
539 style.length = length;
540 int lineAscent = metrics.getAscent() + metrics.getLeading();
541 styledText.paintObject(gc, point.x + paintX, point.y + paintY, lineAscent, metrics.getDescent(), style, null, 0);
542 }
543 }
544 disposeTextLayout(layout);
545 return height;
546 }
getBaseline()547 int getBaseline() {
548 return ascent;
549 }
getCachedLineHeight(int lineIndex)550 int getCachedLineHeight(int lineIndex) {
551 return getLineHeight(lineIndex, false);
552 }
getFont(int style)553 Font getFont(int style) {
554 switch (style) {
555 case SWT.BOLD:
556 if (boldFont != null) return boldFont;
557 return boldFont = new Font(device, getFontData(style));
558 case SWT.ITALIC:
559 if (italicFont != null) return italicFont;
560 return italicFont = new Font(device, getFontData(style));
561 case SWT.BOLD | SWT.ITALIC:
562 if (boldItalicFont != null) return boldItalicFont;
563 return boldItalicFont = new Font(device, getFontData(style));
564 default:
565 return regularFont;
566 }
567 }
getFontData(int style)568 FontData[] getFontData(int style) {
569 FontData[] fontDatas = regularFont.getFontData();
570 for (FontData fontData : fontDatas) {
571 fontData.setStyle(style);
572 }
573 return fontDatas;
574 }
getHeight()575 int getHeight () {
576 int defaultLineHeight = getLineHeight();
577 if (styledText.isFixedLineHeight()) {
578 return lineCount * defaultLineHeight + styledText.topMargin + styledText.bottomMargin;
579 }
580 int totalHeight = 0;
581 int width = styledText.getWrapWidth();
582 for (int i = 0; i < lineCount; i++) {
583 LineSizeInfo line = getLineSize(i);
584 int height = line.height;
585 if (line.needsRecalculateHeight()) {
586 if (width > 0) {
587 int length = content.getLine(i).length();
588 height = ((length * averageCharWidth / width) + 1) * defaultLineHeight;
589 } else {
590 height = defaultLineHeight;
591 }
592 }
593 totalHeight += height;
594 }
595 return totalHeight + styledText.topMargin + styledText.bottomMargin;
596 }
hasLink(int offset)597 boolean hasLink(int offset) {
598 if (offset == -1) return false;
599 int lineIndex = content.getLineAtOffset(offset);
600 int lineOffset = content.getOffsetAtLine(lineIndex);
601 String line = content.getLine(lineIndex);
602 StyledTextEvent event = styledText.getLineStyleData(lineOffset, line);
603 if (event != null) {
604 StyleRange[] styles = event.styles;
605 if (styles != null) {
606 int[] ranges = event.ranges;
607 if (ranges != null) {
608 for (int i = 0; i < ranges.length; i+=2) {
609 if (ranges[i] <= offset && offset < ranges[i] + ranges[i+1] && styles[i >> 1].underline && styles[i >> 1].underlineStyle == SWT.UNDERLINE_LINK) {
610 return true;
611 }
612 }
613 } else {
614 for (StyleRange style : styles) {
615 if (style.start <= offset && offset < style.start + style.length && style.underline && style.underlineStyle == SWT.UNDERLINE_LINK) {
616 return true;
617 }
618 }
619 }
620 }
621 } else {
622 if (ranges != null) {
623 int rangeCount = styleCount << 1;
624 int index = getRangeIndex(offset, -1, rangeCount);
625 if (index >= rangeCount) return false;
626 int rangeStart = ranges[index];
627 int rangeLength = ranges[index + 1];
628 StyleRange rangeStyle = styles[index >> 1];
629 if (rangeStart <= offset && offset < rangeStart + rangeLength && rangeStyle.underline && rangeStyle.underlineStyle == SWT.UNDERLINE_LINK) {
630 return true;
631 }
632 }
633 }
634 return false;
635 }
getLineAlignment(int index, int defaultAlignment)636 int getLineAlignment(int index, int defaultAlignment) {
637 if (lines == null) return defaultAlignment;
638 LineInfo info = lines[index];
639 if (info != null && (info.flags & ALIGNMENT) != 0) {
640 return info.alignment;
641 }
642 return defaultAlignment;
643 }
getLineBackground(int index, Color defaultBackground)644 Color getLineBackground(int index, Color defaultBackground) {
645 if (lines == null) return defaultBackground;
646 LineInfo info = lines[index];
647 if (info != null && (info.flags & BACKGROUND) != 0) {
648 return info.background;
649 }
650 return defaultBackground;
651 }
getLineBullet(int index, Bullet defaultBullet)652 Bullet getLineBullet (int index, Bullet defaultBullet) {
653 if (bullets == null) return defaultBullet;
654 if (bulletsIndices != null) return defaultBullet;
655 for (Bullet bullet : bullets) {
656 if (bullet.indexOf(index) != -1) return bullet;
657 }
658 return defaultBullet;
659 }
getLineHeight()660 int getLineHeight() {
661 return ascent + descent;
662 }
getLineHeight(int lineIndex)663 int getLineHeight(int lineIndex) {
664 return getLineHeight(lineIndex, true);
665 }
getLineHeight(int lineIndex, boolean exact)666 int getLineHeight(int lineIndex, boolean exact) {
667 LineSizeInfo line = getLineSize(lineIndex);
668 if (line.needsRecalculateHeight()) {
669 // here we are in "variable line height", the call of calculate which uses TextLayout is very slow
670 // so use the average line height of all calculated lines when many heights are needed e.g. for scrolling.
671 if (isVariableHeight(lineIndex)) {
672 if (exact) {
673 calculate(lineIndex, 1);
674 } else {
675 return Math.round(averageLineHeight);
676 }
677 } else {
678 line.height = getLineHeight() + getLineSpacing(lineIndex);
679 }
680 }
681 return line.height;
682 }
683 /**
684 * Returns true if the given line can use the default line height and false
685 * otherwise.
686 *
687 * @param lineIndex
688 * line index
689 * @return true if the given line can use the default line height and false
690 * otherwise.
691 */
isVariableHeight(int lineIndex)692 private boolean isVariableHeight(int lineIndex) {
693 if (styledText.isWordWrap()) {
694 // In word wrap mode, the line height must be recomputed with TextLayout
695 return true;
696 }
697 StyleRange[] styles = getStylesForLine(lineIndex);
698 if (styles != null) {
699 for (StyleRange style : styles) {
700 if (style.isVariableHeight()) {
701 // style is variable height
702 return true;
703 }
704 }
705 }
706 return false;
707 }
708 /**
709 * returns true if the given line index defines custom line spacing and false
710 * otherwise.
711 *
712 * @param lineIndex
713 * the line index.
714 * @return true if the given line index defines custom line spacing and false
715 * otherwise.
716 */
getLineSpacing(int lineIndex)717 private int getLineSpacing(int lineIndex) {
718 if (styledText.lineSpacing > 0) {
719 return styledText.lineSpacing;
720 } else if (lineSpacingProvider != null) {
721 Integer lineSpacing = lineSpacingProvider.getLineSpacing(lineIndex);
722 if (lineSpacing != null) {
723 return lineSpacing;
724 }
725 }
726 return 0;
727 }
728 /**
729 * Returns styles range for the given line index and null otherwise.
730 *
731 * @param lineIndex
732 * the line index.
733 * @return styles range for the given line index and null otherwise.
734 */
getStylesForLine(int lineIndex)735 private StyleRange[] getStylesForLine(int lineIndex) {
736 int start = styledText.getOffsetAtLine(lineIndex);
737 int length = styledText.getLine(lineIndex).length();
738 return getStyleRanges(start, length, false);
739 }
getLineIndent(int index, int defaultIndent)740 int getLineIndent(int index, int defaultIndent) {
741 if (lines == null) return defaultIndent;
742 LineInfo info = lines[index];
743 if (info != null && (info.flags & INDENT) != 0) {
744 return info.indent;
745 }
746 return defaultIndent;
747 }
getLineVerticalIndent(int index)748 int getLineVerticalIndent(int index) {
749 if (lines == null) return 0;
750 LineInfo info = lines[index];
751 if (info != null && (info.flags & VERTICAL_INDENT) != 0) {
752 return info.verticalIndent;
753 }
754 return 0;
755 }
getLineWrapIndent(int index, int defaultWrapIndent)756 int getLineWrapIndent(int index, int defaultWrapIndent) {
757 if (lines == null) return defaultWrapIndent;
758 LineInfo info = lines[index];
759 if (info != null && (info.flags & WRAP_INDENT) != 0) {
760 return info.wrapIndent;
761 }
762 return defaultWrapIndent;
763 }
getLineJustify(int index, boolean defaultJustify)764 boolean getLineJustify(int index, boolean defaultJustify) {
765 if (lines == null) return defaultJustify;
766 LineInfo info = lines[index];
767 if (info != null && (info.flags & JUSTIFY) != 0) {
768 return info.justify;
769 }
770 return defaultJustify;
771 }
getLineTabStops(int index, int[] defaultTabStops)772 int[] getLineTabStops(int index, int[] defaultTabStops) {
773 if (lines == null) return defaultTabStops;
774 LineInfo info = lines[index];
775 if (info != null && (info.flags & TABSTOPS) != 0) {
776 return info.tabStops;
777 }
778 return defaultTabStops;
779 }
getLineSpacingProvider()780 StyledTextLineSpacingProvider getLineSpacingProvider() {
781 return lineSpacingProvider;
782 }
getRangeIndex(int offset, int low, int high)783 int getRangeIndex(int offset, int low, int high) {
784 if (styleCount == 0) return 0;
785 if (ranges != null) {
786 while (high - low > 2) {
787 int index = ((high + low) / 2) / 2 * 2;
788 int end = ranges[index] + ranges[index + 1];
789 if (end > offset) {
790 high = index;
791 } else {
792 low = index;
793 }
794 }
795 } else {
796 while (high - low > 1) {
797 int index = ((high + low) / 2);
798 int end = styles[index].start + styles[index].length;
799 if (end > offset) {
800 high = index;
801 } else {
802 low = index;
803 }
804 }
805 }
806 return high;
807 }
getRanges(int start, int length)808 int[] getRanges(int start, int length) {
809 if (length == 0) return null;
810 int[] newRanges;
811 int end = start + length - 1;
812 if (ranges != null) {
813 int rangeCount = styleCount << 1;
814 int rangeStart = getRangeIndex(start, -1, rangeCount);
815 if (rangeStart >= rangeCount) return null;
816 if (ranges[rangeStart] > end) return null;
817 int rangeEnd = Math.min(rangeCount - 2, getRangeIndex(end, rangeStart - 1, rangeCount));
818 if (ranges[rangeEnd] > end) rangeEnd = Math.max(rangeStart, rangeEnd - 2);
819 newRanges = new int[rangeEnd - rangeStart + 2];
820 System.arraycopy(ranges, rangeStart, newRanges, 0, newRanges.length);
821 } else {
822 int rangeStart = getRangeIndex(start, -1, styleCount);
823 if (rangeStart >= styleCount) return null;
824 if (styles[rangeStart].start > end) return null;
825 int rangeEnd = Math.min(styleCount - 1, getRangeIndex(end, rangeStart - 1, styleCount));
826 if (styles[rangeEnd].start > end) rangeEnd = Math.max(rangeStart, rangeEnd - 1);
827 newRanges = new int[(rangeEnd - rangeStart + 1) << 1];
828 for (int i = rangeStart, j = 0; i <= rangeEnd; i++, j += 2) {
829 StyleRange style = styles[i];
830 newRanges[j] = style.start;
831 newRanges[j + 1] = style.length;
832 }
833 }
834 if (start > newRanges[0]) {
835 newRanges[1] = newRanges[0] + newRanges[1] - start;
836 newRanges[0] = start;
837 }
838 if (end < newRanges[newRanges.length - 2] + newRanges[newRanges.length - 1] - 1) {
839 newRanges[newRanges.length - 1] = end - newRanges[newRanges.length - 2] + 1;
840 }
841 return newRanges;
842 }
getStyleRanges(int start, int length, boolean includeRanges)843 StyleRange[] getStyleRanges(int start, int length, boolean includeRanges) {
844 if (length == 0) return null;
845 StyleRange[] newStyles;
846 int end = start + length - 1;
847 if (ranges != null) {
848 int rangeCount = styleCount << 1;
849 int rangeStart = getRangeIndex(start, -1, rangeCount);
850 if (rangeStart >= rangeCount) return null;
851 if (ranges[rangeStart] > end) return null;
852 int rangeEnd = Math.min(rangeCount - 2, getRangeIndex(end, rangeStart - 1, rangeCount));
853 if (ranges[rangeEnd] > end) rangeEnd = Math.max(rangeStart, rangeEnd - 2);
854 newStyles = new StyleRange[((rangeEnd - rangeStart) >> 1) + 1];
855 if (includeRanges) {
856 for (int i = rangeStart, j = 0; i <= rangeEnd; i += 2, j++) {
857 newStyles[j] = (StyleRange)styles[i >> 1].clone();
858 newStyles[j].start = ranges[i];
859 newStyles[j].length = ranges[i + 1];
860 }
861 } else {
862 System.arraycopy(styles, rangeStart >> 1, newStyles, 0, newStyles.length);
863 }
864 } else {
865 int rangeStart = getRangeIndex(start, -1, styleCount);
866 if (rangeStart >= styleCount) return null;
867 if (styles[rangeStart].start > end) return null;
868 int rangeEnd = Math.min(styleCount - 1, getRangeIndex(end, rangeStart - 1, styleCount));
869 if (styles[rangeEnd].start > end) rangeEnd = Math.max(rangeStart, rangeEnd - 1);
870 newStyles = new StyleRange[rangeEnd - rangeStart + 1];
871 System.arraycopy(styles, rangeStart, newStyles, 0, newStyles.length);
872 }
873 if (includeRanges || ranges == null) {
874 StyleRange style = newStyles[0];
875 if (start > style.start) {
876 newStyles[0] = style = (StyleRange)style.clone();
877 style.length = style.start + style.length - start;
878 style.start = start;
879 }
880 style = newStyles[newStyles.length - 1];
881 if (end < style.start + style.length - 1) {
882 newStyles[newStyles.length - 1] = style = (StyleRange)style.clone();
883 style.length = end - style.start + 1;
884 }
885 }
886 return newStyles;
887 }
getStyleRange(StyleRange style)888 StyleRange getStyleRange(StyleRange style) {
889 if (style.underline && style.underlineStyle == SWT.UNDERLINE_LINK) hasLinks = true;
890 if (style.start == 0 && style.length == 0 && style.fontStyle == SWT.NORMAL) return style;
891 StyleRange clone = (StyleRange)style.clone();
892 clone.start = clone.length = 0;
893 clone.fontStyle = SWT.NORMAL;
894 if (clone.font == null) clone.font = getFont(style.fontStyle);
895 return clone;
896 }
getTextLayout(int lineIndex)897 TextLayout getTextLayout(int lineIndex) {
898 if (lineSpacingProvider == null) {
899 return getTextLayout(lineIndex, styledText.getOrientation(), styledText.getWrapWidth(), styledText.lineSpacing);
900 }
901 // Compute line spacing for the given line index.
902 int newLineSpacing = styledText.lineSpacing;
903 Integer spacing = lineSpacingProvider.getLineSpacing(lineIndex);
904 if (spacing != null && spacing.intValue() >= 0) {
905 newLineSpacing = spacing;
906 }
907 // Check if line spacing has not changed
908 if (isSameLineSpacing(lineIndex, newLineSpacing)) {
909 return getTextLayout(lineIndex, styledText.getOrientation(), styledText.getWrapWidth(), newLineSpacing);
910 }
911 // Get text layout with original StyledText line spacing.
912 TextLayout layout = getTextLayout(lineIndex, styledText.getOrientation(), styledText.getWrapWidth(),
913 styledText.lineSpacing);
914 if (layout.getSpacing() != newLineSpacing) {
915 layout.setSpacing(newLineSpacing);
916 if (lineSpacingComputing) {
917 return layout;
918 }
919 try {
920 /* Call of resetCache, setCaretLocation, redraw call getTextLayout method
921 * To avoid having stack overflow, lineSpacingComputing flag is used to call
922 * resetCache, setCaretLocation, redraw methods only at the end of the compute of all lines spacing.
923 */
924 lineSpacingComputing = true;
925 styledText.resetCache(lineIndex, 1);
926 styledText.setCaretLocation();
927 styledText.redraw();
928 } finally {
929 lineSpacingComputing = false;
930 }
931 }
932 return layout;
933 }
isSameLineSpacing(int lineIndex, int newLineSpacing)934 boolean isSameLineSpacing(int lineIndex, int newLineSpacing) {
935 if (layouts == null) {
936 return false;
937 }
938 int layoutIndex = lineIndex - topIndex;
939 if (0 <= layoutIndex && layoutIndex < layouts.length) {
940 TextLayout layout = layouts[layoutIndex];
941 return layout != null && !layout.isDisposed() && layout.getSpacing() == newLineSpacing;
942 }
943 return false;
944 }
945
946 private static final class StyleEntry {
947 public final int start;
948 public final int end;
949 public final TextStyle style;
950
StyleEntry(TextStyle style, int start, int end)951 public StyleEntry(TextStyle style, int start, int end) {
952 this.style = style;
953 this.start = start;
954 this.end = end;
955 }
956 }
957
getTextLayout(int lineIndex, int orientation, int width, int lineSpacing)958 TextLayout getTextLayout(int lineIndex, int orientation, int width, int lineSpacing) {
959 TextLayout layout = null;
960 if (styledText != null) {
961 int topIndex = styledText.topIndex > 0 ? styledText.topIndex - 1 : 0;
962 if (layouts == null || topIndex != this.topIndex) {
963 TextLayout[] newLayouts = new TextLayout[CACHE_SIZE];
964 if (layouts != null) {
965 for (int i = 0; i < layouts.length; i++) {
966 if (layouts[i] != null) {
967 int layoutIndex = (i + this.topIndex) - topIndex;
968 if (0 <= layoutIndex && layoutIndex < newLayouts.length) {
969 newLayouts[layoutIndex] = layouts[i];
970 } else {
971 layouts[i].dispose();
972 }
973 }
974 }
975 }
976 if (bullets != null && bulletsIndices != null && topIndex != this.topIndex) {
977 int delta = topIndex - this.topIndex;
978 if (delta > 0) {
979 if (delta < bullets.length) {
980 System.arraycopy(bullets, delta, bullets, 0, bullets.length - delta);
981 System.arraycopy(bulletsIndices, delta, bulletsIndices, 0, bulletsIndices.length - delta);
982 }
983 int startIndex = Math.max(0, bullets.length - delta);
984 for (int i = startIndex; i < bullets.length; i++) bullets[i] = null;
985 } else {
986 if (-delta < bullets.length) {
987 System.arraycopy(bullets, 0, bullets, -delta, bullets.length + delta);
988 System.arraycopy(bulletsIndices, 0, bulletsIndices, -delta, bulletsIndices.length + delta);
989 }
990 int endIndex = Math.min(bullets.length, -delta);
991 for (int i = 0; i < endIndex; i++) bullets[i] = null;
992 }
993 }
994 this.topIndex = topIndex;
995 layouts = newLayouts;
996 }
997 if (layouts != null) {
998 int layoutIndex = lineIndex - topIndex;
999 if (0 <= layoutIndex && layoutIndex < layouts.length) {
1000 layout = layouts[layoutIndex];
1001 if (layout != null) {
1002 // Bug 520374: lineIndex can be >= linesSize.length
1003 if(lineIndex < lineSizes.length && getLineSize(lineIndex).canLayout()) {
1004 return layout;
1005 }
1006 } else {
1007 layout = layouts[layoutIndex] = new TextLayout(device);
1008 }
1009 }
1010 }
1011 }
1012 if (layout == null) layout = new TextLayout(device);
1013 String line = content.getLine(lineIndex);
1014 int lineOffset = content.getOffsetAtLine(lineIndex);
1015 int[] segments = null;
1016 char[] segmentChars = null;
1017 int indent = 0;
1018 int wrapIndent = 0;
1019 int verticalIndent = 0;
1020 int alignment = SWT.LEFT;
1021 int textDirection = orientation;
1022 boolean justify = false;
1023 int[] tabs = {tabWidth};
1024 Bullet bullet = null;
1025 int[] ranges = null;
1026 StyleRange[] styles = null;
1027 int rangeStart = 0, styleCount = 0;
1028 StyledTextEvent event = null;
1029 if (styledText != null) {
1030 event = styledText.getBidiSegments(lineOffset, line);
1031 if (event != null) {
1032 segments = event.segments;
1033 segmentChars = event.segmentsChars;
1034 }
1035 event = styledText.getLineStyleData(lineOffset, line);
1036 indent = styledText.indent;
1037 wrapIndent = styledText.wrapIndent;
1038 alignment = styledText.alignment;
1039 if (styledText.isAutoDirection()) {
1040 textDirection = SWT.AUTO_TEXT_DIRECTION;
1041 } else if ((styledText.getStyle() & SWT.FLIP_TEXT_DIRECTION) != 0) {
1042 textDirection = orientation == SWT.RIGHT_TO_LEFT ? SWT.LEFT_TO_RIGHT : SWT.RIGHT_TO_LEFT;
1043 }
1044 justify = styledText.justify;
1045 if (styledText.tabs != null) tabs = styledText.tabs;
1046 }
1047 if (event != null) {
1048 indent = event.indent;
1049 verticalIndent = event.verticalIndent;
1050 wrapIndent = event.wrapIndent;
1051 alignment = event.alignment;
1052 justify = event.justify;
1053 bullet = event.bullet;
1054 ranges = event.ranges;
1055 styles = event.styles;
1056 if (event.tabStops != null) tabs = event.tabStops;
1057 if (styles != null) {
1058 styleCount = styles.length;
1059 if (styledText.isFixedLineHeight()) {
1060 for (int i = 0; i < styleCount; i++) {
1061 if (styles[i].isVariableHeight()) {
1062 styledText.hasStyleWithVariableHeight = true;
1063 styledText.verticalScrollOffset = -1;
1064 styledText.redraw();
1065 break;
1066 }
1067 }
1068 }
1069 }
1070 if (bullets == null || bulletsIndices == null) {
1071 bullets = new Bullet[CACHE_SIZE];
1072 bulletsIndices = new int[CACHE_SIZE];
1073 }
1074 int index = lineIndex - topIndex;
1075 if (0 <= index && index < CACHE_SIZE) {
1076 bullets[index] = bullet;
1077 bulletsIndices[index] = event.bulletIndex;
1078 }
1079 } else {
1080 if (lines != null) {
1081 LineInfo info = lines[lineIndex];
1082 if (info != null) {
1083 if ((info.flags & INDENT) != 0) indent = info.indent;
1084 if ((info.flags & VERTICAL_INDENT) != 0) verticalIndent = info.verticalIndent;
1085 if ((info.flags & WRAP_INDENT) != 0) wrapIndent = info.wrapIndent;
1086 if ((info.flags & ALIGNMENT) != 0) alignment = info.alignment;
1087 if ((info.flags & JUSTIFY) != 0) justify = info.justify;
1088 if ((info.flags & SEGMENTS) != 0) segments = info.segments;
1089 if ((info.flags & SEGMENT_CHARS) != 0) segmentChars = info.segmentsChars;
1090 if ((info.flags & TABSTOPS) != 0) tabs = info.tabStops;
1091 }
1092 }
1093 if (bulletsIndices != null) {
1094 bullets = null;
1095 bulletsIndices = null;
1096 }
1097 if (bullets != null) {
1098 for (Bullet b : bullets) {
1099 if (b.indexOf(lineIndex) != -1) {
1100 bullet = b;
1101 break;
1102 }
1103 }
1104 }
1105 ranges = this.ranges;
1106 styles = this.styles;
1107 styleCount = this.styleCount;
1108 if (ranges != null) {
1109 rangeStart = getRangeIndex(lineOffset, -1, styleCount << 1);
1110 } else {
1111 rangeStart = getRangeIndex(lineOffset, -1, styleCount);
1112 }
1113 }
1114 if (bullet != null) {
1115 StyleRange style = bullet.style;
1116 GlyphMetrics metrics = style.metrics;
1117 indent += metrics.width;
1118 }
1119
1120 // prepare styles, as it may change the line content, do it before calling layout.setText()
1121 // This needs to happen early to handle the case of GlyphMetrics on \t.
1122 // The root cause is that TextLayout doesn't return the right value for the bounds when
1123 // GlyphMetrics are applied on \t. A better fix could be implemented directly in (all 3)
1124 // TextLayout classes.
1125 List<StyleEntry> styleEntries = new ArrayList<>();
1126 int lastOffset = 0;
1127 int length = line.length();
1128 if (styles != null) {
1129 if (ranges != null) {
1130 int rangeCount = styleCount << 1;
1131 for (int i = rangeStart; i < rangeCount; i += 2) {
1132 int start, end;
1133 if (lineOffset > ranges[i]) {
1134 start = 0;
1135 end = Math.min (length, ranges[i + 1] - lineOffset + ranges[i]);
1136 } else {
1137 start = ranges[i] - lineOffset;
1138 end = Math.min(length, start + ranges[i + 1]);
1139 }
1140 if (start >= length) break;
1141 if (lastOffset < start) {
1142 styleEntries.add(new StyleEntry(null, lastOffset, start - 1));
1143 }
1144 TextStyle style = getStyleRange(styles[i >> 1]);
1145 int endIndex = Math.max(start, Math.min(length, end + 1));
1146 if (style.metrics != null && line.substring(start, endIndex).contains("\t")) {
1147 line =
1148 line.substring(0, start) +
1149 line.substring(start, endIndex).replace('\t', ' ') +
1150 (end < line.length() ? line.substring(end + 1, line.length()) : "");
1151 }
1152 styleEntries.add(new StyleEntry(style, start, end));
1153 lastOffset = Math.max(lastOffset, end);
1154 }
1155 } else {
1156 for (int i = rangeStart; i < styleCount; i++) {
1157 int start, end;
1158 if (lineOffset > styles[i].start) {
1159 start = 0;
1160 end = Math.min (length, styles[i].length - lineOffset + styles[i].start);
1161 } else {
1162 start = styles[i].start - lineOffset;
1163 end = Math.min(length, start + styles[i].length);
1164 }
1165 if (start >= length) break;
1166 if (lastOffset < start) {
1167 styleEntries.add(new StyleEntry(null, lastOffset, start - 1));
1168 }
1169 TextStyle style = getStyleRange(styles[i]);
1170 int endIndex = Math.max(start, Math.min(length, end + 1));
1171 if (style.metrics != null && line.substring(start, endIndex).contains("\t")) {
1172 line =
1173 line.substring(0, start) +
1174 line.substring(start, endIndex).replace('\t', ' ') +
1175 (end < line.length() ? line.substring(end + 1, line.length()) : "");
1176 }
1177 styleEntries.add(new StyleEntry(style, start, end));
1178 lastOffset = Math.max(lastOffset, end);
1179 }
1180 }
1181 }
1182 if (lastOffset < length) styleEntries.add(new StyleEntry(null, lastOffset, length));
1183
1184 layout.setFont(regularFont);
1185 layout.setAscent(ascent);
1186 layout.setDescent(descent);
1187 layout.setText(line);
1188 layout.setOrientation(orientation);
1189 layout.setSegments(segments);
1190 layout.setSegmentsChars(segmentChars);
1191 layout.setWidth(width);
1192 layout.setSpacing(lineSpacing);
1193 layout.setTabs(tabs);
1194 layout.setDefaultTabWidth(tabLength);
1195 layout.setIndent(indent);
1196 layout.setVerticalIndent(verticalIndent);
1197 layout.setWrapIndent(wrapIndent);
1198 layout.setAlignment(alignment);
1199 layout.setJustify(justify);
1200 layout.setTextDirection(textDirection);
1201 // apply styles, must be done after layout.setText()
1202 for (StyleEntry styleEntry : styleEntries) {
1203 layout.setStyle(styleEntry.style, styleEntry.start, styleEntry.end);
1204 }
1205
1206 if (styledText != null && styledText.ime != null) {
1207 IME ime = styledText.ime;
1208 int compositionOffset = ime.getCompositionOffset();
1209 if (compositionOffset != -1) {
1210 int commitCount = ime.getCommitCount();
1211 int compositionLength = ime.getText().length();
1212 if (compositionLength != commitCount) {
1213 int compositionLine = content.getLineAtOffset(compositionOffset);
1214 if (compositionLine == lineIndex) {
1215 int[] imeRanges = ime.getRanges();
1216 TextStyle[] imeStyles = ime.getStyles();
1217 if (imeRanges.length > 0) {
1218 for (int i = 0; i < imeStyles.length; i++) {
1219 int start = imeRanges[i*2] - lineOffset;
1220 int end = imeRanges[i*2+1] - lineOffset;
1221 TextStyle imeStyle = imeStyles[i], userStyle;
1222 for (int j = start; j <= end; j++) {
1223 if (!(0 <= j && j < length)) break;
1224 userStyle = layout.getStyle(cap(layout, j));
1225 if (userStyle == null && j > 0) userStyle = layout.getStyle(cap(layout, j - 1));
1226 if (userStyle == null && j + 1 < length) userStyle = layout.getStyle(cap(layout, j + 1));
1227 if (userStyle == null) {
1228 layout.setStyle(imeStyle, j, j);
1229 } else {
1230 TextStyle newStyle = new TextStyle(imeStyle);
1231 if (newStyle.font == null) newStyle.font = userStyle.font;
1232 if (newStyle.foreground == null) newStyle.foreground = userStyle.foreground;
1233 if (newStyle.background == null) newStyle.background = userStyle.background;
1234 layout.setStyle(newStyle, j, j);
1235 }
1236 }
1237 }
1238 } else {
1239 int start = compositionOffset - lineOffset;
1240 int end = start + compositionLength - 1;
1241 TextStyle userStyle = layout.getStyle(cap(layout, start));
1242 if (userStyle == null) {
1243 if (start > 0) userStyle = layout.getStyle(cap(layout, start - 1));
1244 if (userStyle == null && end + 1 < length) userStyle = layout.getStyle(cap(layout, end + 1));
1245 if (userStyle != null) {
1246 TextStyle newStyle = new TextStyle();
1247 newStyle.font = userStyle.font;
1248 newStyle.foreground = userStyle.foreground;
1249 newStyle.background = userStyle.background;
1250 layout.setStyle(newStyle, start, end);
1251 }
1252 }
1253 }
1254 }
1255 }
1256 }
1257 }
1258
1259 if (styledText != null && styledText.isFixedLineHeight()) {
1260 int index = -1;
1261 int lineCount = layout.getLineCount();
1262 int height = getLineHeight();
1263 for (int i = 0; i < lineCount; i++) {
1264 int lineHeight = layout.getLineBounds(i).height;
1265 if (lineHeight > height) {
1266 height = lineHeight;
1267 index = i;
1268 }
1269 }
1270 if (index != -1) {
1271 FontMetrics metrics = layout.getLineMetrics(index);
1272 ascent = metrics.getAscent() + metrics.getLeading();
1273 descent = metrics.getDescent();
1274 if (layouts != null) {
1275 for (TextLayout l : layouts) {
1276 if (l != null && l != layout) {
1277 l.setAscent(ascent);
1278 l.setDescent(descent);
1279 }
1280 }
1281 }
1282 styledText.calculateScrollBars();
1283 if (styledText.verticalScrollOffset != 0) {
1284 int topIndex = styledText.topIndex;
1285 int topIndexY = styledText.topIndexY;
1286 int lineHeight = getLineHeight();
1287 int newVerticalScrollOffset;
1288 if (topIndexY >= 0) {
1289 newVerticalScrollOffset = (topIndex - 1) * lineHeight + lineHeight - topIndexY;
1290 } else {
1291 newVerticalScrollOffset = topIndex * lineHeight - topIndexY;
1292 }
1293 styledText.scrollVertical(newVerticalScrollOffset - styledText.verticalScrollOffset, true);
1294 }
1295 if (styledText.isBidiCaret()) styledText.createCaretBitmaps();
1296 styledText.caretDirection = SWT.NULL;
1297 styledText.setCaretLocation();
1298 styledText.redraw();
1299 }
1300 }
1301 return layout;
1302 }
getWidth()1303 int getWidth() {
1304 return maxWidth;
1305 }
reset()1306 void reset() {
1307 if (layouts != null) {
1308 for (TextLayout layout : layouts) {
1309 if (layout != null) layout.dispose();
1310 }
1311 layouts = null;
1312 }
1313 topIndex = -1;
1314 stylesSetCount = styleCount = lineCount = 0;
1315 ranges = null;
1316 styles = null;
1317 stylesSet = null;
1318 lines = null;
1319 lineSizes = null;
1320 bullets = null;
1321 bulletsIndices = null;
1322 redrawLines = null;
1323 hasLinks = false;
1324 }
reset(int startLine, int lineCount)1325 void reset(int startLine, int lineCount) {
1326 int endLine = startLine + lineCount;
1327 if (startLine < 0 || endLine > lineSizes.length) return;
1328 SortedSet<Integer> lines = new TreeSet<>();
1329 for (int i = startLine; i < endLine; i++) {
1330 lines.add(Integer.valueOf(i));
1331 }
1332 reset(lines);
1333 }
reset(Set<Integer> lines)1334 void reset(Set<Integer> lines) {
1335 if (lines == null || lines.isEmpty()) return;
1336 int resetLineCount = 0;
1337 for (Integer line : lines) {
1338 if (line >= 0 || line < lineCount) {
1339 resetLineCount++;
1340 getLineSize(line.intValue()).resetSize();
1341 }
1342 }
1343 if (linesInAverageLineHeight > resetLineCount) {
1344 linesInAverageLineHeight -= resetLineCount;
1345 } else {
1346 linesInAverageLineHeight = 0;
1347 averageLineHeight = 0.0f;
1348 }
1349 if (lines.contains(Integer.valueOf(maxWidthLineIndex))) {
1350 maxWidth = 0;
1351 maxWidthLineIndex = -1;
1352 if (resetLineCount != this.lineCount) {
1353 for (int i = 0; i < this.lineCount; i++) {
1354 LineSizeInfo lineSize = getLineSize(i);
1355 if (lineSize.width > maxWidth) {
1356 maxWidth = lineSize.width;
1357 maxWidthLineIndex = i;
1358 }
1359 }
1360 }
1361 }
1362 }
setContent(StyledTextContent content)1363 void setContent(StyledTextContent content) {
1364 reset();
1365 this.content = content;
1366 lineCount = content.getLineCount();
1367 lineSizes = new LineSizeInfo[lineCount];
1368 maxWidth = 0;
1369 maxWidthLineIndex = -1;
1370 reset(0, lineCount);
1371 }
setFont(Font font, int tabs)1372 void setFont(Font font, int tabs) {
1373 TextLayout layout = new TextLayout(device);
1374 layout.setFont(regularFont);
1375 tabLength = tabs;
1376 if (font != null) {
1377 if (boldFont != null) boldFont.dispose();
1378 if (italicFont != null) italicFont.dispose();
1379 if (boldItalicFont != null) boldItalicFont.dispose();
1380 boldFont = italicFont = boldItalicFont = null;
1381 regularFont = font;
1382 layout.setText(" ");
1383 layout.setFont(font);
1384 layout.setStyle(new TextStyle(getFont(SWT.NORMAL), null, null), 0, 0);
1385 layout.setStyle(new TextStyle(getFont(SWT.BOLD), null, null), 1, 1);
1386 layout.setStyle(new TextStyle(getFont(SWT.ITALIC), null, null), 2, 2);
1387 layout.setStyle(new TextStyle(getFont(SWT.BOLD | SWT.ITALIC), null, null), 3, 3);
1388 FontMetrics metrics = layout.getLineMetrics(0);
1389 ascent = metrics.getAscent() + metrics.getLeading();
1390 descent = metrics.getDescent();
1391 boldFont.dispose();
1392 italicFont.dispose();
1393 boldItalicFont.dispose();
1394 boldFont = italicFont = boldItalicFont = null;
1395 }
1396 layout.dispose();
1397 layout = new TextLayout(device);
1398 layout.setFont(regularFont);
1399 StringBuilder tabBuffer = new StringBuilder(tabs);
1400 for (int i = 0; i < tabs; i++) {
1401 tabBuffer.append(' ');
1402 }
1403 layout.setText(tabBuffer.toString());
1404 tabWidth = layout.getBounds().width;
1405 layout.dispose();
1406 if (styledText != null) {
1407 GC gc = new GC(styledText);
1408 averageCharWidth = (int) gc.getFontMetrics().getAverageCharacterWidth();
1409 fixedPitch = gc.stringExtent("l").x == gc.stringExtent("W").x; //$NON-NLS-1$ //$NON-NLS-2$
1410 gc.dispose();
1411 }
1412 }
setLineAlignment(int startLine, int count, int alignment)1413 void setLineAlignment(int startLine, int count, int alignment) {
1414 if (lines == null) lines = new LineInfo[lineCount];
1415 for (int i = startLine; i < startLine + count; i++) {
1416 if (lines[i] == null) {
1417 lines[i] = new LineInfo();
1418 }
1419 lines[i].flags |= ALIGNMENT;
1420 lines[i].alignment = alignment;
1421 }
1422 }
setLineBackground(int startLine, int count, Color background)1423 void setLineBackground(int startLine, int count, Color background) {
1424 if (lines == null) lines = new LineInfo[lineCount];
1425 for (int i = startLine; i < startLine + count; i++) {
1426 if (lines[i] == null) {
1427 lines[i] = new LineInfo();
1428 }
1429 lines[i].flags |= BACKGROUND;
1430 lines[i].background = background;
1431 }
1432 }
setLineBullet(int startLine, int count, Bullet bullet)1433 void setLineBullet(int startLine, int count, Bullet bullet) {
1434 if (bulletsIndices != null) {
1435 bulletsIndices = null;
1436 bullets = null;
1437 }
1438 if (bullets == null) {
1439 if (bullet == null) return;
1440 bullets = new Bullet[1];
1441 bullets[0] = bullet;
1442 }
1443 int index = 0;
1444 while (index < bullets.length) {
1445 if (bullet == bullets[index]) break;
1446 index++;
1447 }
1448 if (bullet != null) {
1449 if (index == bullets.length) {
1450 Bullet[] newBulletsList = new Bullet[bullets.length + 1];
1451 System.arraycopy(bullets, 0, newBulletsList, 0, bullets.length);
1452 newBulletsList[index] = bullet;
1453 bullets = newBulletsList;
1454 }
1455 bullet.addIndices(startLine, count);
1456 } else {
1457 updateBullets(startLine, count, 0, false);
1458 styledText.redrawLinesBullet(redrawLines);
1459 redrawLines = null;
1460 }
1461 }
setLineIndent(int startLine, int count, int indent)1462 void setLineIndent(int startLine, int count, int indent) {
1463 if (lines == null) lines = new LineInfo[lineCount];
1464 for (int i = startLine; i < startLine + count; i++) {
1465 if (lines[i] == null) {
1466 lines[i] = new LineInfo();
1467 }
1468 lines[i].flags |= INDENT;
1469 lines[i].indent = indent;
1470 }
1471 }
setLineVerticalIndent(int lineIndex, int verticalLineIndent)1472 void setLineVerticalIndent(int lineIndex, int verticalLineIndent) {
1473 if (lines == null)
1474 lines = new LineInfo[lineCount];
1475 if (lines[lineIndex] == null) {
1476 lines[lineIndex] = new LineInfo();
1477 }
1478 lines[lineIndex].flags |= VERTICAL_INDENT;
1479 lines[lineIndex].verticalIndent = verticalLineIndent;
1480 }
setLineWrapIndent(int startLine, int count, int wrapIndent)1481 void setLineWrapIndent(int startLine, int count, int wrapIndent) {
1482 if (lines == null) lines = new LineInfo[lineCount];
1483 for (int i = startLine; i < startLine + count; i++) {
1484 if (lines[i] == null) {
1485 lines[i] = new LineInfo();
1486 }
1487 lines[i].flags |= WRAP_INDENT;
1488 lines[i].wrapIndent = wrapIndent;
1489 }
1490 }
setLineJustify(int startLine, int count, boolean justify)1491 void setLineJustify(int startLine, int count, boolean justify) {
1492 if (lines == null) lines = new LineInfo[lineCount];
1493 for (int i = startLine; i < startLine + count; i++) {
1494 if (lines[i] == null) {
1495 lines[i] = new LineInfo();
1496 }
1497 lines[i].flags |= JUSTIFY;
1498 lines[i].justify = justify;
1499 }
1500 }
setLineSegments(int startLine, int count, int[] segments)1501 void setLineSegments(int startLine, int count, int[] segments) {
1502 if (lines == null) lines = new LineInfo[lineCount];
1503 for (int i = startLine; i < startLine + count; i++) {
1504 if (lines[i] == null) {
1505 lines[i] = new LineInfo();
1506 }
1507 lines[i].flags |= SEGMENTS;
1508 lines[i].segments = segments;
1509 }
1510 }
setLineSegmentChars(int startLine, int count, char[] segmentChars)1511 void setLineSegmentChars(int startLine, int count, char[] segmentChars) {
1512 if (lines == null) lines = new LineInfo[lineCount];
1513 for (int i = startLine; i < startLine + count; i++) {
1514 if (lines[i] == null) {
1515 lines[i] = new LineInfo();
1516 }
1517 lines[i].flags |= SEGMENT_CHARS;
1518 lines[i].segmentsChars = segmentChars;
1519 }
1520 }
setLineTabStops(int startLine, int count, int[] tabStops)1521 void setLineTabStops(int startLine, int count, int[] tabStops) {
1522 if (lines == null) lines = new LineInfo[lineCount];
1523 for (int i = startLine; i < startLine + count; i++) {
1524 if (lines[i] == null) {
1525 lines[i] = new LineInfo();
1526 }
1527 lines[i].flags |= TABSTOPS;
1528 lines[i].tabStops = tabStops;
1529 }
1530 }
setLineSpacingProvider(StyledTextLineSpacingProvider lineSpacingProvider)1531 void setLineSpacingProvider(StyledTextLineSpacingProvider lineSpacingProvider) {
1532 this.lineSpacingProvider = lineSpacingProvider;
1533 }
setStyleRanges(int[] newRanges, StyleRange[] newStyles)1534 void setStyleRanges (int[] newRanges, StyleRange[] newStyles) {
1535 if (newStyles == null) {
1536 stylesSetCount = styleCount = 0;
1537 ranges = null;
1538 styles = null;
1539 stylesSet = null;
1540 hasLinks = false;
1541 return;
1542 }
1543 if (newRanges == null && COMPACT_STYLES) {
1544 newRanges = new int[newStyles.length << 1];
1545 StyleRange[] tmpStyles = new StyleRange[newStyles.length];
1546 if (stylesSet == null) stylesSet = new StyleRange[4];
1547 for (int i = 0, j = 0; i < newStyles.length; i++) {
1548 StyleRange newStyle = newStyles[i];
1549 newRanges[j++] = newStyle.start;
1550 newRanges[j++] = newStyle.length;
1551 int index = 0;
1552 while (index < stylesSetCount) {
1553 if (stylesSet[index].similarTo(newStyle)) break;
1554 index++;
1555 }
1556 if (index == stylesSetCount) {
1557 if (stylesSetCount == stylesSet.length) {
1558 StyleRange[] tmpStylesSet = new StyleRange[stylesSetCount + 4];
1559 System.arraycopy(stylesSet, 0, tmpStylesSet, 0, stylesSetCount);
1560 stylesSet = tmpStylesSet;
1561 }
1562 stylesSet[stylesSetCount++] = newStyle;
1563 }
1564 tmpStyles[i] = stylesSet[index];
1565 }
1566 newStyles = tmpStyles;
1567 }
1568
1569 if (styleCount == 0) {
1570 if (newRanges != null) {
1571 ranges = new int[newRanges.length];
1572 System.arraycopy(newRanges, 0, ranges, 0, ranges.length);
1573 }
1574 styles = new StyleRange[newStyles.length];
1575 System.arraycopy(newStyles, 0, styles, 0, styles.length);
1576 styleCount = newStyles.length;
1577 return;
1578 }
1579 if (newRanges != null && ranges == null) {
1580 ranges = new int[styles.length << 1];
1581 for (int i = 0, j = 0; i < styleCount; i++) {
1582 ranges[j++] = styles[i].start;
1583 ranges[j++] = styles[i].length;
1584 }
1585 }
1586 if (newRanges == null && ranges != null) {
1587 newRanges = new int[newStyles.length << 1];
1588 for (int i = 0, j = 0; i < newStyles.length; i++) {
1589 newRanges[j++] = newStyles[i].start;
1590 newRanges[j++] = newStyles[i].length;
1591 }
1592 }
1593 if (ranges != null) {
1594 int rangeCount = styleCount << 1;
1595 int start = newRanges[0];
1596 int modifyStart = getRangeIndex(start, -1, rangeCount), modifyEnd;
1597 boolean insert = modifyStart == rangeCount;
1598 if (!insert) {
1599 int end = newRanges[newRanges.length - 2] + newRanges[newRanges.length - 1];
1600 modifyEnd = getRangeIndex(end, modifyStart - 1, rangeCount);
1601 insert = modifyStart == modifyEnd && ranges[modifyStart] >= end;
1602 }
1603 if (insert) {
1604 addMerge(newRanges, newStyles, newRanges.length, modifyStart, modifyStart);
1605 return;
1606 }
1607 modifyEnd = modifyStart;
1608 int[] mergeRanges = new int[6];
1609 StyleRange[] mergeStyles = new StyleRange[3];
1610 for (int i = 0; i < newRanges.length; i += 2) {
1611 int newStart = newRanges[i];
1612 int newEnd = newStart + newRanges[i + 1];
1613 if (newStart == newEnd) continue;
1614 int modifyLast = 0, mergeCount = 0;
1615 while (modifyEnd < rangeCount) {
1616 if (newStart >= ranges[modifyStart] + ranges[modifyStart + 1]) modifyStart += 2;
1617 if (ranges[modifyEnd] + ranges[modifyEnd + 1] > newEnd) break;
1618 modifyEnd += 2;
1619 }
1620 if (ranges[modifyStart] < newStart && newStart < ranges[modifyStart] + ranges[modifyStart + 1]) {
1621 mergeStyles[mergeCount >> 1] = styles[modifyStart >> 1];
1622 mergeRanges[mergeCount] = ranges[modifyStart];
1623 mergeRanges[mergeCount + 1] = newStart - ranges[modifyStart];
1624 mergeCount += 2;
1625 }
1626 mergeStyles[mergeCount >> 1] = newStyles[i >> 1];
1627 mergeRanges[mergeCount] = newStart;
1628 mergeRanges[mergeCount + 1] = newRanges[i + 1];
1629 mergeCount += 2;
1630 if (modifyEnd < rangeCount && ranges[modifyEnd] < newEnd && newEnd < ranges[modifyEnd] + ranges[modifyEnd + 1]) {
1631 mergeStyles[mergeCount >> 1] = styles[modifyEnd >> 1];
1632 mergeRanges[mergeCount] = newEnd;
1633 mergeRanges[mergeCount + 1] = ranges[modifyEnd] + ranges[modifyEnd + 1] - newEnd;
1634 mergeCount += 2;
1635 modifyLast = 2;
1636 }
1637 int grow = addMerge(mergeRanges, mergeStyles, mergeCount, modifyStart, modifyEnd + modifyLast);
1638 rangeCount += grow;
1639 modifyStart = modifyEnd += grow;
1640 }
1641 } else {
1642 int start = newStyles[0].start;
1643 int modifyStart = getRangeIndex(start, -1, styleCount), modifyEnd;
1644 boolean insert = modifyStart == styleCount;
1645 if (!insert) {
1646 int end = newStyles[newStyles.length - 1].start + newStyles[newStyles.length - 1].length;
1647 modifyEnd = getRangeIndex(end, modifyStart - 1, styleCount);
1648 insert = modifyStart == modifyEnd && styles[modifyStart].start >= end;
1649 }
1650 if (insert) {
1651 addMerge(newStyles, newStyles.length, modifyStart, modifyStart);
1652 return;
1653 }
1654 modifyEnd = modifyStart;
1655 StyleRange[] mergeStyles = new StyleRange[3];
1656 for (StyleRange newStyle : newStyles) {
1657 StyleRange style;
1658 int newStart = newStyle.start;
1659 int newEnd = newStart + newStyle.length;
1660 if (newStart == newEnd) continue;
1661 int modifyLast = 0, mergeCount = 0;
1662 while (modifyEnd < styleCount) {
1663 if (newStart >= styles[modifyStart].start + styles[modifyStart].length) modifyStart++;
1664 if (styles[modifyEnd].start + styles[modifyEnd].length > newEnd) break;
1665 modifyEnd++;
1666 }
1667 style = styles[modifyStart];
1668 if (style.start < newStart && newStart < style.start + style.length) {
1669 style = mergeStyles[mergeCount++] = (StyleRange)style.clone();
1670 style.length = newStart - style.start;
1671 }
1672 mergeStyles[mergeCount++] = newStyle;
1673 if (modifyEnd < styleCount) {
1674 style = styles[modifyEnd];
1675 if (style.start < newEnd && newEnd < style.start + style.length) {
1676 style = mergeStyles[mergeCount++] = (StyleRange)style.clone();
1677 style.length += style.start - newEnd;
1678 style.start = newEnd;
1679 modifyLast = 1;
1680 }
1681 }
1682 int grow = addMerge(mergeStyles, mergeCount, modifyStart, modifyEnd + modifyLast);
1683 modifyStart = modifyEnd += grow;
1684 }
1685 }
1686 }
textChanging(TextChangingEvent event)1687 void textChanging(TextChangingEvent event) {
1688 int start = event.start;
1689 int newCharCount = event.newCharCount, replaceCharCount = event.replaceCharCount;
1690 int newLineCount = event.newLineCount, replaceLineCount = event.replaceLineCount;
1691
1692 updateRanges(start, replaceCharCount, newCharCount);
1693
1694 int startLine = content.getLineAtOffset(start);
1695 if (replaceCharCount == content.getCharCount()) lines = null;
1696 if (replaceLineCount == lineCount) {
1697 lineCount = newLineCount;
1698 lineSizes = new LineSizeInfo[lineCount];
1699 reset(0, lineCount);
1700 } else {
1701 int startIndex = startLine + replaceLineCount + 1;
1702 int endIndex = startLine + newLineCount + 1;
1703 if(lineCount < startLine) {
1704 SWT.error(SWT.ERROR_INVALID_RANGE, null, "bug 478020: lineCount < startLine: " + lineCount + ":" + startLine);
1705 }
1706 if(lineCount < startIndex) {
1707 SWT.error(SWT.ERROR_INVALID_RANGE, null, "bug 478020: lineCount < startIndex: " + lineCount + ":" + startIndex);
1708 }
1709 int delta = newLineCount - replaceLineCount;
1710 if (lineCount + delta > lineSizes.length) {
1711 LineSizeInfo[] newLineSizes = new LineSizeInfo[lineCount + delta + GROW];
1712 System.arraycopy(lineSizes, 0, newLineSizes, 0, lineCount);
1713 lineSizes = newLineSizes;
1714 }
1715 if (lines != null) {
1716 if (lineCount + delta > lines.length) {
1717 LineInfo[] newLines = new LineInfo[lineCount + delta + GROW];
1718 System.arraycopy(lines, 0, newLines, 0, lineCount);
1719 lines = newLines;
1720 }
1721 }
1722 System.arraycopy(lineSizes, startIndex, lineSizes, endIndex, lineCount - startIndex);
1723 for (int i = startLine; i < endIndex; i++) {
1724 lineSizes[i] = null;
1725 }
1726 for (int i = lineCount + delta; i < lineCount; i++) {
1727 lineSizes[i] = null;
1728 }
1729 if (layouts != null) {
1730 int layoutStartLine = startLine - topIndex;
1731 int layoutEndLine = layoutStartLine + replaceLineCount + 1;
1732 for (int i = layoutStartLine; i < layoutEndLine; i++) {
1733 if (0 <= i && i < layouts.length) {
1734 if (layouts[i] != null) layouts[i].dispose();
1735 layouts[i] = null;
1736 if (bullets != null && bulletsIndices != null) bullets[i] = null;
1737 }
1738 }
1739 if (delta > 0) {
1740 for (int i = layouts.length - 1; i >= layoutEndLine; i--) {
1741 if (0 <= i && i < layouts.length) {
1742 endIndex = i + delta;
1743 if (0 <= endIndex && endIndex < layouts.length) {
1744 layouts[endIndex] = layouts[i];
1745 layouts[i] = null;
1746 if (bullets != null && bulletsIndices != null) {
1747 bullets[endIndex] = bullets[i];
1748 bulletsIndices[endIndex] = bulletsIndices[i];
1749 bullets[i] = null;
1750 }
1751 } else {
1752 if (layouts[i] != null) layouts[i].dispose();
1753 layouts[i] = null;
1754 if (bullets != null && bulletsIndices != null) bullets[i] = null;
1755 }
1756 }
1757 }
1758 } else if (delta < 0) {
1759 for (int i = layoutEndLine; i < layouts.length; i++) {
1760 if (0 <= i && i < layouts.length) {
1761 endIndex = i + delta;
1762 if (0 <= endIndex && endIndex < layouts.length) {
1763 layouts[endIndex] = layouts[i];
1764 layouts[i] = null;
1765 if (bullets != null && bulletsIndices != null) {
1766 bullets[endIndex] = bullets[i];
1767 bulletsIndices[endIndex] = bulletsIndices[i];
1768 bullets[i] = null;
1769 }
1770 } else {
1771 if (layouts[i] != null) layouts[i].dispose();
1772 layouts[i] = null;
1773 if (bullets != null && bulletsIndices != null) bullets[i] = null;
1774 }
1775 }
1776 }
1777 }
1778 }
1779 if (replaceLineCount != 0 || newLineCount != 0) {
1780 int startLineOffset = content.getOffsetAtLine(startLine);
1781 if (startLineOffset != start) startLine++;
1782 updateBullets(startLine, replaceLineCount, newLineCount, true);
1783 if (lines != null) {
1784 startIndex = startLine + replaceLineCount;
1785 endIndex = startLine + newLineCount;
1786 System.arraycopy(lines, startIndex, lines, endIndex, lineCount - startIndex);
1787 for (int i = startLine; i < endIndex; i++) {
1788 lines[i] = null;
1789 }
1790 for (int i = lineCount + delta; i < lineCount; i++) {
1791 lines[i] = null;
1792 }
1793 }
1794 }
1795 lineCount += delta;
1796 if (maxWidthLineIndex != -1 && startLine <= maxWidthLineIndex && maxWidthLineIndex <= startLine + replaceLineCount) {
1797 maxWidth = 0;
1798 maxWidthLineIndex = -1;
1799 for (int i = 0; i < lineCount; i++) {
1800 LineSizeInfo lineSize = getLineSize(i);
1801 if (lineSize.width > maxWidth) {
1802 maxWidth = lineSize.width;
1803 maxWidthLineIndex = i;
1804 }
1805 }
1806 }
1807 }
1808 }
updateBullets(int startLine, int replaceLineCount, int newLineCount, boolean update)1809 void updateBullets(int startLine, int replaceLineCount, int newLineCount, boolean update) {
1810 if (bullets == null) return;
1811 if (bulletsIndices != null) return;
1812 for (Bullet bullet : bullets) {
1813 int[] lines = bullet.removeIndices(startLine, replaceLineCount, newLineCount, update);
1814 if (lines != null) {
1815 if (redrawLines == null) {
1816 redrawLines = lines;
1817 } else {
1818 int[] newRedrawBullets = new int[redrawLines.length + lines.length];
1819 System.arraycopy(redrawLines, 0, newRedrawBullets, 0, redrawLines.length);
1820 System.arraycopy(lines, 0, newRedrawBullets, redrawLines.length, lines.length);
1821 redrawLines = newRedrawBullets;
1822 }
1823 }
1824 }
1825 int removed = 0;
1826 for (Bullet bullet : bullets) {
1827 if (bullet.size() == 0) removed++;
1828 }
1829 if (removed > 0) {
1830 if (removed == bullets.length) {
1831 bullets = null;
1832 } else {
1833 Bullet[] newBulletsList = new Bullet[bullets.length - removed];
1834 for (int i = 0, j = 0; i < bullets.length; i++) {
1835 Bullet bullet = bullets[i];
1836 if (bullet.size() > 0) newBulletsList[j++] = bullet;
1837 }
1838 bullets = newBulletsList;
1839 }
1840 }
1841 }
updateRanges(int start, int replaceCharCount, int newCharCount)1842 void updateRanges(int start, int replaceCharCount, int newCharCount) {
1843 if (styleCount == 0 || (replaceCharCount == 0 && newCharCount == 0)) return;
1844 if (ranges != null) {
1845 int rangeCount = styleCount << 1;
1846 int modifyStart = getRangeIndex(start, -1, rangeCount);
1847 if (modifyStart == rangeCount) return;
1848 int end = start + replaceCharCount;
1849 int modifyEnd = getRangeIndex(end, modifyStart - 1, rangeCount);
1850 int offset = newCharCount - replaceCharCount;
1851 if (modifyStart == modifyEnd && ranges[modifyStart] < start && end < ranges[modifyEnd] + ranges[modifyEnd + 1]) {
1852 if (newCharCount == 0) {
1853 ranges[modifyStart + 1] -= replaceCharCount;
1854 modifyEnd += 2;
1855 } else {
1856 if (rangeCount + 2 > ranges.length) {
1857 int[] newRanges = new int[ranges.length + (GROW << 1)];
1858 System.arraycopy(ranges, 0, newRanges, 0, rangeCount);
1859 ranges = newRanges;
1860 StyleRange[] newStyles = new StyleRange[styles.length + GROW];
1861 System.arraycopy(styles, 0, newStyles, 0, styleCount);
1862 styles = newStyles;
1863 }
1864 System.arraycopy(ranges, modifyStart + 2, ranges, modifyStart + 4, rangeCount - (modifyStart + 2));
1865 System.arraycopy(styles, (modifyStart + 2) >> 1, styles, (modifyStart + 4) >> 1, styleCount - ((modifyStart + 2) >> 1));
1866 ranges[modifyStart + 3] = ranges[modifyStart] + ranges[modifyStart + 1] - end;
1867 ranges[modifyStart + 2] = start + newCharCount;
1868 ranges[modifyStart + 1] = start - ranges[modifyStart];
1869 styles[(modifyStart >> 1) + 1] = styles[modifyStart >> 1];
1870 rangeCount += 2;
1871 styleCount++;
1872 modifyEnd += 4;
1873 }
1874 if (offset != 0) {
1875 for (int i = modifyEnd; i < rangeCount; i += 2) {
1876 ranges[i] += offset;
1877 }
1878 }
1879 } else {
1880 if (ranges[modifyStart] < start && start < ranges[modifyStart] + ranges[modifyStart + 1]) {
1881 ranges[modifyStart + 1] = start - ranges[modifyStart];
1882 modifyStart += 2;
1883 }
1884 if (modifyEnd < rangeCount && ranges[modifyEnd] < end && end < ranges[modifyEnd] + ranges[modifyEnd + 1]) {
1885 ranges[modifyEnd + 1] = ranges[modifyEnd] + ranges[modifyEnd + 1] - end;
1886 ranges[modifyEnd] = end;
1887 }
1888 if (offset != 0) {
1889 for (int i = modifyEnd; i < rangeCount; i += 2) {
1890 ranges[i] += offset;
1891 }
1892 }
1893 System.arraycopy(ranges, modifyEnd, ranges, modifyStart, rangeCount - modifyEnd);
1894 System.arraycopy(styles, modifyEnd >> 1, styles, modifyStart >> 1, styleCount - (modifyEnd >> 1));
1895 styleCount -= (modifyEnd - modifyStart) >> 1;
1896 }
1897 } else {
1898 int modifyStart = getRangeIndex(start, -1, styleCount);
1899 if (modifyStart == styleCount) return;
1900 int end = start + replaceCharCount;
1901 int modifyEnd = getRangeIndex(end, modifyStart - 1, styleCount);
1902 int offset = newCharCount - replaceCharCount;
1903 if (modifyStart == modifyEnd && styles[modifyStart].start < start && end < styles[modifyEnd].start + styles[modifyEnd].length) {
1904 if (newCharCount == 0) {
1905 styles[modifyStart].length -= replaceCharCount;
1906 modifyEnd++;
1907 } else {
1908 if (styleCount + 1 > styles.length) {
1909 StyleRange[] newStyles = new StyleRange[styles.length + GROW];
1910 System.arraycopy(styles, 0, newStyles, 0, styleCount);
1911 styles = newStyles;
1912 }
1913 System.arraycopy(styles, modifyStart + 1, styles, modifyStart + 2, styleCount - (modifyStart + 1));
1914 styles[modifyStart + 1] = (StyleRange)styles[modifyStart].clone();
1915 styles[modifyStart + 1].length = styles[modifyStart].start + styles[modifyStart].length - end;
1916 styles[modifyStart + 1].start = start + newCharCount;
1917 styles[modifyStart].length = start - styles[modifyStart].start;
1918 styleCount++;
1919 modifyEnd += 2;
1920 }
1921 if (offset != 0) {
1922 for (int i = modifyEnd; i < styleCount; i++) {
1923 styles[i].start += offset;
1924 }
1925 }
1926 } else {
1927 if (styles[modifyStart].start < start && start < styles[modifyStart].start + styles[modifyStart].length) {
1928 styles[modifyStart].length = start - styles[modifyStart].start;
1929 modifyStart++;
1930 }
1931 if (modifyEnd < styleCount && styles[modifyEnd].start < end && end < styles[modifyEnd].start + styles[modifyEnd].length) {
1932 styles[modifyEnd].length = styles[modifyEnd].start + styles[modifyEnd].length - end;
1933 styles[modifyEnd].start = end;
1934 }
1935 if (offset != 0) {
1936 for (int i = modifyEnd; i < styleCount; i++) {
1937 styles[i].start += offset;
1938 }
1939 }
1940 System.arraycopy(styles, modifyEnd, styles, modifyStart, styleCount - modifyEnd);
1941 styleCount -= modifyEnd - modifyStart;
1942 }
1943 }
1944 }
1945
hasVerticalIndent()1946 public boolean hasVerticalIndent() {
1947 return Arrays.stream(lines).filter(Objects::nonNull) //
1948 .mapToInt(line -> line.verticalIndent) //
1949 .anyMatch(n -> n != 0);
1950 }
1951
1952
1953 }
1954