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