1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
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;
20 import java.util.List;
22 import org.eclipse.swt.*;
23 import org.eclipse.swt.graphics.*;
24 import org.eclipse.swt.widgets.*;
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.
30 class StyledTextRenderer {
32 StyledText styledText;
33 StyledTextContent content;
35 /* Custom line spacing */
36 StyledTextLineSpacingProvider lineSpacingProvider;
37 boolean lineSpacingComputing;
40 Font regularFont, boldFont, italicFont, boldItalicFont;
44 int tabLength; //tab length in spaces
50 LineSizeInfo[] lineSizes;
53 int maxWidthLineIndex;
65 StyleRange[] stylesSet;
66 int stylesSetCount = 0;
67 boolean hasLinks, fixedPitch;
68 final static int BULLET_MARGIN = 8;
70 final static boolean COMPACT_STYLES = true;
71 final static boolean MERGE_STYLES = true;
73 final static int GROW = 32;
74 final static int IDLE_TIME = 50;
75 final static int CACHE_SIZE = 300;
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;
87 static class LineSizeInfo {
89 private static final int RESETED_SIZE = -1;
95 public LineSizeInfo() {
100 * Reset the line size.
103 height = RESETED_SIZE;
104 width = RESETED_SIZE;
108 * Returns true if the TextLayout get from the layout pool can be directly used
109 * or must be refreshed with styles.
111 * @return true if the TextLayout get from the layout pool can be directly used
112 * or must be refreshed with styles.
114 boolean canLayout() {
115 return !needsRecalculateWidth();
119 * Returns true if it needs to recalculate the line size and false
122 * @return true if it needs to recalculate the line size and false
125 boolean needsRecalculateSize() {
126 return needsRecalculateWidth() || needsRecalculateHeight();
130 * Returns true if it needs to recalculate the line width and false
133 * @return true if it needs to recalculate the line width and false
136 boolean needsRecalculateWidth() {
137 return width == RESETED_SIZE;
141 * Returns true if it needs to recalculate the line height and false
144 * @return true if it needs to recalculate the line height and false
147 boolean needsRecalculateHeight() {
148 return height == RESETED_SIZE;
152 static class LineInfo {
160 char[] segmentsChars;
166 public LineInfo(LineInfo info) {
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;
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));
186 StyledTextRenderer(Device device, StyledText styledText) {
187 this.device = device;
188 this.styledText = styledText;
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];
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));
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));
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];
223 styles[j >> 1] = mergeStyles[i >> 1];
224 ranges[j++] = mergeRanges[i];
225 ranges[j++] = mergeRanges[i + 1];
228 if (endStyle != null && ranges[j - 2] + ranges[j - 1] == endStart && endStyle.similarTo(styles[(j - 2) >> 1])) {
229 ranges[j - 1] += endLength;
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));
237 grow = (j - modifyStart) - (modifyEnd - modifyStart);
239 System.arraycopy(mergeRanges, 0, ranges, modifyStart, mergeCount);
240 System.arraycopy(mergeStyles, 0, styles, modifyStart >> 1, mergeCount >> 1);
242 styleCount += grow >> 1;
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);
257 if (styleCount > modifyEnd) {
258 System.arraycopy(styles, modifyEnd, styles, modifyStart + mergeCount, styleCount - modifyEnd);
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;
268 styles[j++] = newStyle;
271 StyleRange style = styles[j - 1];
272 if (endStyle != null && style.start + style.length == endStyle.start && endStyle.similarTo(style)) {
273 style.length += endStyle.length;
277 if (styleCount > modifyEnd) {
278 System.arraycopy(styles, modifyStart + mergeCount, styles, j, styleCount - modifyEnd);
280 grow = (j - modifyStart) - (modifyEnd - modifyStart);
282 System.arraycopy(mergeStyles, 0, styles, modifyStart, mergeCount);
287 void calculate(int startLine, int lineCount) {
288 int endLine = startLine + lineCount;
289 if (startLine < 0 || endLine > lineSizes.length) {
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);
302 if (line.width > maxWidth) {
303 maxWidth = line.width;
304 maxWidthLineIndex = i;
308 LineSizeInfo getLineSize(int i) {
309 if (lineSizes[i] == null) {
310 lineSizes[i] = new LineSizeInfo();
314 void calculateClientArea () {
315 int index = Math.max (0, styledText.getTopIndex());
316 int lineCount = content.getLineCount();
317 int height = styledText.getClientArea().height;
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.
324 while (height > y && lineCount > index && lineSizes.length > index) {
326 y += lineSizes[index++].height;
329 void calculateIdle () {
330 if (idleRunning) return;
331 Runnable runnable = new Runnable() {
334 if (styledText == null) return;
336 long start = System.currentTimeMillis();
337 for (i = 0; i < lineCount; i++) {
338 LineSizeInfo line = getLineSize(i);
339 if (line.needsRecalculateSize()) {
341 if (System.currentTimeMillis() - start > IDLE_TIME) break;
345 Display display = styledText.getDisplay();
346 display.asyncExec(this);
349 styledText.setScrollBars(true);
350 ScrollBar bar = styledText.getVerticalBar();
352 bar.setSelection(styledText.getVerticalScrollOffset());
357 Display display = styledText.getDisplay();
358 display.asyncExec(runnable);
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];
366 info.flags &= ~BACKGROUND;
367 info.background = null;
368 if (info.flags == 0) lines[i] = null;
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];
377 info.flags &= ~(ALIGNMENT | INDENT | VERTICAL_INDENT | WRAP_INDENT | JUSTIFY | TABSTOPS);
378 if (info.flags == 0) lines[i] = null;
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);
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();
392 renderer.styleCount = styleCount;
395 LineInfo[] newLines = renderer.lines = new LineInfo[lineCount];
396 for (int i = 0; i < newLines.length; i++) {
397 newLines[i] = new LineInfo(lines[i]);
399 renderer.lineCount = lineCount;
403 if (boldFont != null) boldFont.dispose();
404 if (italicFont != null) italicFont.dispose();
405 if (boldItalicFont != null) boldItalicFont.dispose();
406 boldFont = italicFont = boldItalicFont = null;
412 void disposeTextLayout (TextLayout layout) {
413 if (layouts != null) {
414 for (int i = 0; i < layouts.length; i++) {
415 if (layouts[i] == layout) return;
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);
428 int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER);
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;
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);
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;
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);
471 gc.setBackground(lineBackground);
472 gc.fillRectangle(client.x, paintY + verticalIndent, client.width, height - verticalIndent);
474 gc.setBackground(widgetBackground);
475 styledText.drawBackground(gc, client.x, paintY, client.width, height);
477 gc.setForeground(widgetForeground);
478 if (selectionStart == selectionEnd || (selectionEnd <= 0 && selectionStart > lineLength - 1)) {
479 layout.draw(gc, paintX, paintY);
481 int start = Math.max(0, selectionStart);
482 int end = Math.min(lineLength, selectionEnd);
483 Color selectionFg = styledText.getSelectionForeground();
484 Color selectionBg = styledText.getSelectionBackground();
486 if ((styledText.getStyle() & SWT.FULL_SELECTION) != 0) {
487 flags = SWT.FULL_SELECTION;
489 flags = SWT.DELIMITER_SELECTION;
491 if (selectionStart <= lineLength && lineLength < selectionEnd ) {
492 flags |= SWT.LAST_LINE_SELECTION;
494 layout.draw(gc, paintX, paintY, start, end - 1, selectionFg, selectionBg, flags);
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];
508 for (int i = 0; i < bullets.length; i++) {
510 bulletIndex = bullet.indexOf(lineIndex);
511 if (bulletIndex != -1) break;
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);
522 drawBullet(bullet, gc, paintX, paintY, bulletIndex, lineAscent, metrics.getDescent());
525 TextStyle[] styles = layout.getStyles();
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);
541 disposeTextLayout(layout);
547 Font getFont(int style) {
550 if (boldFont != null) return boldFont;
551 return boldFont = new Font(device, getFontData(style));
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));
562 FontData[] getFontData(int style) {
563 FontData[] fontDatas = regularFont.getFontData();
564 for (int i = 0; i < fontDatas.length; i++) {
565 fontDatas[i].setStyle(style);
570 int defaultLineHeight = getLineHeight();
571 if (styledText.isFixedLineHeight()) {
572 return lineCount * defaultLineHeight + styledText.topMargin + styledText.bottomMargin;
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()) {
581 int length = content.getLine(i).length();
582 height = ((length * averageCharWidth / width) + 1) * defaultLineHeight;
584 height = defaultLineHeight;
587 totalHeight += height;
589 return totalHeight + styledText.topMargin + styledText.bottomMargin;
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);
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) {
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) {
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) {
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;
637 return defaultAlignment;
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;
645 return defaultBackground;
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;
654 return defaultBullet;
656 int getLineHeight() {
657 return ascent + descent;
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);
667 line.height = getLineHeight() + getLineSpacing(lineIndex);
673 * Returns true if the given line can use the default line height and false
678 * @return true if the given line can use the default line height and false
681 private boolean isVariableHeight(int lineIndex) {
682 if (styledText.isWordWrap()) {
683 // In word wrap mode, the line height must be recomputed with TextLayout
686 StyleRange[] styles = getStylesForLine(lineIndex);
687 if (styles != null) {
688 for (StyleRange style : styles) {
689 if (style.isVariableHeight()) {
690 // style is variable height
698 * returns true if the given line index defines custom line spacing and false
703 * @return true if the given line index defines custom line spacing and false
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) {
718 * Returns styles range for the given line index and null otherwise.
722 * @return styles range for the given line index and null otherwise.
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);
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) {
735 return defaultIndent;
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;
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;
751 return defaultWrapIndent;
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) {
759 return defaultJustify;
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;
767 return defaultTabStops;
769 StyledTextLineSpacingProvider getLineSpacingProvider() {
770 return lineSpacingProvider;
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];
785 while (high - low > 1) {
786 int index = ((high + low) / 2);
787 int end = styles[index].start + styles[index].length;
797 int[] getRanges(int start, int length) {
798 if (length == 0) return null;
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);
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;
823 if (start > newRanges[0]) {
824 newRanges[1] = newRanges[0] + newRanges[1] - start;
825 newRanges[0] = start;
827 if (end < newRanges[newRanges.length - 2] + newRanges[newRanges.length - 1] - 1) {
828 newRanges[newRanges.length - 1] = end - newRanges[newRanges.length - 2] + 1;
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];
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];
851 System.arraycopy(styles, rangeStart >> 1, newStyles, 0, newStyles.length);
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);
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;
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;
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);
886 TextLayout getTextLayout(int lineIndex) {
887 if (lineSpacingProvider == null) {
888 return getTextLayout(lineIndex, styledText.getOrientation(), styledText.getWrapWidth(), styledText.lineSpacing);
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;
896 // Check if line spacing has not changed
897 if (isSameLineSpacing(lineIndex, newLineSpacing)) {
898 return getTextLayout(lineIndex, styledText.getOrientation(), styledText.getWrapWidth(), newLineSpacing);
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) {
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.
913 lineSpacingComputing = true;
914 styledText.resetCache(lineIndex, 1);
915 styledText.setCaretLocation();
918 lineSpacingComputing = false;
923 boolean isSameLineSpacing(int lineIndex, int newLineSpacing) {
924 if (layouts == null) {
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;
935 private static final class StyleEntry {
936 public final int start;
937 public final int end;
938 public final TextStyle style;
940 public StyleEntry(TextStyle style, int start, int end) {
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];
960 layouts[i].dispose();
965 if (bullets != null && bulletsIndices != null && topIndex != this.topIndex) {
966 int delta = topIndex - this.topIndex;
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);
972 int startIndex = Math.max(0, bullets.length - delta);
973 for (int i = startIndex; i < bullets.length; i++) bullets[i] = null;
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);
979 int endIndex = Math.min(bullets.length, -delta);
980 for (int i = 0; i < endIndex; i++) bullets[i] = null;
983 this.topIndex = topIndex;
984 layouts = newLayouts;
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()) {
996 layout = layouts[layoutIndex] = new TextLayout(device);
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;
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;
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;
1033 justify = styledText.justify;
1034 if (styledText.tabs != null) tabs = styledText.tabs;
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();
1059 if (bullets == null || bulletsIndices == null) {
1060 bullets = new Bullet[CACHE_SIZE];
1061 bulletsIndices = new int[CACHE_SIZE];
1063 int index = lineIndex - topIndex;
1064 if (0 <= index && index < CACHE_SIZE) {
1065 bullets[index] = bullet;
1066 bulletsIndices[index] = event.bulletIndex;
1069 if (lines != null) {
1070 LineInfo info = lines[lineIndex];
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;
1082 if (bulletsIndices != null) {
1084 bulletsIndices = null;
1086 if (bullets != null) {
1087 for (int i = 0; i < bullets.length; i++) {
1088 if (bullets[i].indexOf(lineIndex) != -1) {
1089 bullet = bullets[i];
1094 ranges = this.ranges;
1095 styles = this.styles;
1096 styleCount = this.styleCount;
1097 if (ranges != null) {
1098 rangeStart = getRangeIndex(lineOffset, -1, styleCount << 1);
1100 rangeStart = getRangeIndex(lineOffset, -1, styleCount);
1103 if (bullet != null) {
1104 StyleRange style = bullet.style;
1105 GlyphMetrics metrics = style.metrics;
1106 indent += metrics.width;
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<>();
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) {
1122 if (lineOffset > ranges[i]) {
1124 end = Math.min (length, ranges[i + 1] - lineOffset + ranges[i]);
1126 start = ranges[i] - lineOffset;
1127 end = Math.min(length, start + ranges[i + 1]);
1129 if (start >= length) break;
1130 if (lastOffset < start) {
1131 styleEntries.add(new StyleEntry(null, lastOffset, start - 1));
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")) {
1137 line.substring(0, start) +
1138 line.substring(start, endIndex).replace('\t', ' ') +
1139 (end < line.length() ? line.substring(end + 1, line.length()) : "");
1141 styleEntries.add(new StyleEntry(style, start, end));
1142 lastOffset = Math.max(lastOffset, end);
1145 for (int i = rangeStart; i < styleCount; i++) {
1147 if (lineOffset > styles[i].start) {
1149 end = Math.min (length, styles[i].length - lineOffset + styles[i].start);
1151 start = styles[i].start - lineOffset;
1152 end = Math.min(length, start + styles[i].length);
1154 if (start >= length) break;
1155 if (lastOffset < start) {
1156 styleEntries.add(new StyleEntry(null, lastOffset, start - 1));
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")) {
1162 line.substring(0, start) +
1163 line.substring(start, endIndex).replace('\t', ' ') +
1164 (end < line.length() ? line.substring(end + 1, line.length()) : "");
1166 styleEntries.add(new StyleEntry(style, start, end));
1167 lastOffset = Math.max(lastOffset, end);
1171 if (lastOffset < length) styleEntries.add(new StyleEntry(null, lastOffset, length));
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);
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);
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);
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);
1248 if (styledText != null && styledText.isFixedLineHeight()) {
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;
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);
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;
1280 newVerticalScrollOffset = topIndex * lineHeight - topIndexY;
1282 styledText.scrollVertical(newVerticalScrollOffset - styledText.verticalScrollOffset, true);
1284 if (styledText.isBidiCaret()) styledText.createCaretBitmaps();
1285 styledText.caretDirection = SWT.NULL;
1286 styledText.setCaretLocation();
1287 styledText.redraw();
1296 if (layouts != null) {
1297 for (int i = 0; i < layouts.length; i++) {
1298 TextLayout layout = layouts[i];
1299 if (layout != null) layout.dispose();
1304 stylesSetCount = styleCount = lineCount = 0;
1311 bulletsIndices = null;
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));
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) {
1330 getLineSize(line.intValue()).resetSize();
1333 if (lines.contains(Integer.valueOf(maxWidthLineIndex))) {
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;
1347 void setContent(StyledTextContent content) {
1349 this.content = content;
1350 lineCount = content.getLineCount();
1351 lineSizes = new LineSizeInfo[lineCount];
1353 maxWidthLineIndex = -1;
1354 reset(0, lineCount);
1356 void setFont(Font font, int tabs) {
1357 TextLayout layout = new TextLayout(device);
1358 layout.setFont(regularFont);
1361 if (boldFont != null) boldFont.dispose();
1362 if (italicFont != null) italicFont.dispose();
1363 if (boldItalicFont != null) boldItalicFont.dispose();
1364 boldFont = italicFont = boldItalicFont = null;
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();
1376 italicFont.dispose();
1377 boldItalicFont.dispose();
1378 boldFont = italicFont = boldItalicFont = null;
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(' ');
1387 layout.setText(tabBuffer.toString());
1388 tabWidth = layout.getBounds().width;
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$
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();
1403 lines[i].flags |= ALIGNMENT;
1404 lines[i].alignment = alignment;
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();
1413 lines[i].flags |= BACKGROUND;
1414 lines[i].background = background;
1417 void setLineBullet(int startLine, int count, Bullet bullet) {
1418 if (bulletsIndices != null) {
1419 bulletsIndices = null;
1422 if (bullets == null) {
1423 if (bullet == null) return;
1424 bullets = new Bullet[1];
1425 bullets[0] = bullet;
1428 while (index < bullets.length) {
1429 if (bullet == bullets[index]) break;
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;
1439 bullet.addIndices(startLine, count);
1441 updateBullets(startLine, count, 0, false);
1442 styledText.redrawLinesBullet(redrawLines);
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();
1452 lines[i].flags |= INDENT;
1453 lines[i].indent = indent;
1456 void setLineVerticalIndent(int lineIndex, int verticalLineIndent) {
1458 lines = new LineInfo[lineCount];
1459 if (lines[lineIndex] == null) {
1460 lines[lineIndex] = new LineInfo();
1462 lines[lineIndex].flags |= VERTICAL_INDENT;
1463 lines[lineIndex].verticalIndent = verticalLineIndent;
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();
1471 lines[i].flags |= WRAP_INDENT;
1472 lines[i].wrapIndent = wrapIndent;
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();
1481 lines[i].flags |= JUSTIFY;
1482 lines[i].justify = justify;
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();
1491 lines[i].flags |= SEGMENTS;
1492 lines[i].segments = segments;
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();
1501 lines[i].flags |= SEGMENT_CHARS;
1502 lines[i].segmentsChars = segmentChars;
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();
1511 lines[i].flags |= TABSTOPS;
1512 lines[i].tabStops = tabStops;
1515 void setLineSpacingProvider(StyledTextLineSpacingProvider lineSpacingProvider) {
1516 this.lineSpacingProvider = lineSpacingProvider;
1518 void setStyleRanges (int[] newRanges, StyleRange[] newStyles) {
1519 if (newStyles == null) {
1520 stylesSetCount = styleCount = 0;
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;
1536 while (index < stylesSetCount) {
1537 if (stylesSet[index].similarTo(newStyle)) break;
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;
1546 stylesSet[stylesSetCount++] = newStyle;
1548 tmpStyles[i] = stylesSet[index];
1550 newStyles = tmpStyles;
1553 if (styleCount == 0) {
1554 if (newRanges != null) {
1555 ranges = new int[newRanges.length];
1556 System.arraycopy(newRanges, 0, ranges, 0, ranges.length);
1558 styles = new StyleRange[newStyles.length];
1559 System.arraycopy(newStyles, 0, styles, 0, styles.length);
1560 styleCount = newStyles.length;
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;
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;
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;
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;
1588 addMerge(newRanges, newStyles, newRanges.length, modifyStart, modifyStart);
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;
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];
1610 mergeStyles[mergeCount >> 1] = newStyles[i >> 1];
1611 mergeRanges[mergeCount] = newStart;
1612 mergeRanges[mergeCount + 1] = newRanges[i + 1];
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;
1621 int grow = addMerge(mergeRanges, mergeStyles, mergeCount, modifyStart, modifyEnd + modifyLast);
1623 modifyStart = modifyEnd += grow;
1626 int start = newStyles[0].start;
1627 int modifyStart = getRangeIndex(start, -1, styleCount), modifyEnd;
1628 boolean insert = modifyStart == styleCount;
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;
1635 addMerge(newStyles, newStyles.length, modifyStart, modifyStart);
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;
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;
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;
1666 int grow = addMerge(mergeStyles, mergeCount, modifyStart, modifyEnd + modifyLast);
1667 modifyStart = modifyEnd += grow;
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;
1676 updateRanges(start, replaceCharCount, newCharCount);
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);
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);
1690 if(lineCount < startIndex) {
1691 SWT.error(SWT.ERROR_INVALID_RANGE, null, "bug 478020: lineCount < startIndex: " + lineCount + ":" + startIndex);
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;
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);
1706 System.arraycopy(lineSizes, startIndex, lineSizes, endIndex, lineCount - startIndex);
1707 for (int i = startLine; i < endIndex; i++) {
1708 lineSizes[i] = null;
1710 for (int i = lineCount + delta; i < lineCount; i++) {
1711 lineSizes[i] = null;
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();
1720 if (bullets != null && bulletsIndices != null) bullets[i] = null;
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];
1730 if (bullets != null && bulletsIndices != null) {
1731 bullets[endIndex] = bullets[i];
1732 bulletsIndices[endIndex] = bulletsIndices[i];
1736 if (layouts[i] != null) layouts[i].dispose();
1738 if (bullets != null && bulletsIndices != null) bullets[i] = null;
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];
1749 if (bullets != null && bulletsIndices != null) {
1750 bullets[endIndex] = bullets[i];
1751 bulletsIndices[endIndex] = bulletsIndices[i];
1755 if (layouts[i] != null) layouts[i].dispose();
1757 if (bullets != null && bulletsIndices != null) bullets[i] = null;
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++) {
1774 for (int i = lineCount + delta; i < lineCount; i++) {
1780 if (maxWidthLineIndex != -1 && startLine <= maxWidthLineIndex && maxWidthLineIndex <= startLine + replaceLineCount) {
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;
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;
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;
1811 for (int i = 0; i < bullets.length; i++) {
1812 if (bullets[i].size() == 0) removed++;
1815 if (removed == bullets.length) {
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;
1823 bullets = newBulletsList;
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;
1841 if (rangeCount + 2 > ranges.length) {
1842 int[] newRanges = new int[ranges.length + (GROW << 1)];
1843 System.arraycopy(ranges, 0, newRanges, 0, rangeCount);
1845 StyleRange[] newStyles = new StyleRange[styles.length + GROW];
1846 System.arraycopy(styles, 0, newStyles, 0, styleCount);
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];
1860 for (int i = modifyEnd; i < rangeCount; i += 2) {
1861 ranges[i] += offset;
1865 if (ranges[modifyStart] < start && start < ranges[modifyStart] + ranges[modifyStart + 1]) {
1866 ranges[modifyStart + 1] = start - ranges[modifyStart];
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;
1874 for (int i = modifyEnd; i < rangeCount; i += 2) {
1875 ranges[i] += offset;
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;
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;
1893 if (styleCount + 1 > styles.length) {
1894 StyleRange[] newStyles = new StyleRange[styles.length + GROW];
1895 System.arraycopy(styles, 0, newStyles, 0, styleCount);
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;
1907 for (int i = modifyEnd; i < styleCount; i++) {
1908 styles[i].start += offset;
1912 if (styles[modifyStart].start < start && start < styles[modifyStart].start + styles[modifyStart].length) {
1913 styles[modifyStart].length = start - styles[modifyStart].start;
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;
1921 for (int i = modifyEnd; i < styleCount; i++) {
1922 styles[i].start += offset;
1925 System.arraycopy(styles, modifyEnd, styles, modifyStart, styleCount - modifyEnd);
1926 styleCount -= modifyEnd - modifyStart;
1931 public boolean hasVerticalIndent() {
1932 return Arrays.stream(lines).filter(Objects::nonNull) //
1933 .mapToInt(line -> line.verticalIndent) //
1934 .anyMatch(n -> n != 0);