TextLayout getTextLayout(int lineIndex, int orientation, int width, int lineSpacing) { TextLayout layout = null; if (styledText != null) { int topIndex = styledText.topIndex > 0 ? styledText.topIndex - 1 : 0; if (layouts == null || topIndex != this.topIndex) { TextLayout[] newLayouts = new TextLayout[CACHE_SIZE]; if (layouts != null) { for (int i = 0; i < layouts.length; i++) { if (layouts[i] != null) { int layoutIndex = (i + this.topIndex) - topIndex; if (0 <= layoutIndex && layoutIndex < newLayouts.length) { newLayouts[layoutIndex] = layouts[i]; } else { layouts[i].dispose(); } } } } if (bullets != null && bulletsIndices != null && topIndex != this.topIndex) { int delta = topIndex - this.topIndex; if (delta > 0) { if (delta < bullets.length) { System.arraycopy(bullets, delta, bullets, 0, bullets.length - delta); System.arraycopy(bulletsIndices, delta, bulletsIndices, 0, bulletsIndices.length - delta); } int startIndex = Math.max(0, bullets.length - delta); for (int i = startIndex; i < bullets.length; i++) bullets[i] = null; } else { if (-delta < bullets.length) { System.arraycopy(bullets, 0, bullets, -delta, bullets.length + delta); System.arraycopy(bulletsIndices, 0, bulletsIndices, -delta, bulletsIndices.length + delta); } int endIndex = Math.min(bullets.length, -delta); for (int i = 0; i < endIndex; i++) bullets[i] = null; } } this.topIndex = topIndex; layouts = newLayouts; } if (layouts != null) { int layoutIndex = lineIndex - topIndex; if (0 <= layoutIndex && layoutIndex < layouts.length) { layout = layouts[layoutIndex]; if (layout != null) { if (lineWidth[lineIndex] != -1) return layout; } else { layout = layouts[layoutIndex] = new TextLayout(device); } } } } if (layout == null) layout = new TextLayout(device); String line = content.getLine(lineIndex); int lineOffset = content.getOffsetAtLine(lineIndex); int[] segments = null; int indent = 0; int alignment = SWT.LEFT; boolean justify = false; Bullet bullet = null; int[] ranges = null; StyleRange[] styles = null; int rangeStart = 0, styleCount = 0; StyledTextEvent event = null; if (styledText != null) { event = styledText.getLineStyleData(lineOffset, line); segments = styledText.getBidiSegments(lineOffset, line); indent = styledText.indent; alignment = styledText.alignment; justify = styledText.justify; } if (event != null) { indent = event.indent; alignment = event.alignment; justify = event.justify; bullet = event.bullet; ranges = event.ranges; styles = event.styles; if (styles != null) { styleCount = styles.length; if (styledText.isFixedLineHeight()) { for (int i = 0; i < styleCount; i++) { if (styles[i].isVariableHeight()) { styledText.verticalScrollOffset = -1; styledText.setVariableLineHeight(); styledText.redraw(); break; } } } } if (bullets == null || bulletsIndices == null) { bullets = new Bullet[CACHE_SIZE]; bulletsIndices = new int[CACHE_SIZE]; } int index = lineIndex - topIndex; if (0 <= index && index < CACHE_SIZE) { bullets[index] = bullet; bulletsIndices[index] = event.bulletIndex; } } else { if (lines != null) { LineInfo info = lines[lineIndex]; if (info != null) { if ((info.flags & INDENT) != 0) indent = info.indent; if ((info.flags & ALIGNMENT) != 0) alignment = info.alignment; if ((info.flags & JUSTIFY) != 0) justify = info.justify; if ((info.flags & SEGMENTS) != 0) segments = info.segments; } } if (bulletsIndices != null) { bullets = null; bulletsIndices = null; } if (bullets != null) { for (int i = 0; i < bullets.length; i++) { if (bullets[i].indexOf(lineIndex) != -1) { bullet = bullets[i]; break; } } } ranges = this.ranges; styles = this.styles; styleCount = this.styleCount; if (ranges != null) { rangeStart = getRangeIndex(lineOffset, -1, styleCount << 1); } else { rangeStart = getRangeIndex(lineOffset, -1, styleCount); } } if (bullet != null) { StyleRange style = bullet.style; GlyphMetrics metrics = style.metrics; indent += metrics.width; } layout.setFont(regularFont); layout.setAscent(ascent); layout.setDescent(descent); layout.setText(line); layout.setOrientation(orientation); layout.setSegments(segments); layout.setWidth(width); layout.setSpacing(lineSpacing); layout.setTabs(new int[]{tabWidth}); layout.setIndent(indent); layout.setAlignment(alignment); layout.setJustify(justify);  int lastOffset = 0; int length = line.length(); if (styles != null) { if (ranges != null) { int rangeCount = styleCount << 1; for (int i = rangeStart; i < rangeCount; i += 2) { int start, end; if (lineOffset > ranges[i]) { start = 0; end = Math.min (length, ranges[i + 1] - lineOffset + ranges[i]); } else { start = ranges[i] - lineOffset; end = Math.min(length, start + ranges[i + 1]); } if (start >= length) break; if (lastOffset < start) { layout.setStyle(null, lastOffset, start - 1); } layout.setStyle(getStyleRange(styles[i >> 1]), start, end); lastOffset = Math.max(lastOffset, end); } } else { for (int i = rangeStart; i < styleCount; i++) { int start, end; if (lineOffset > styles[i].start) { start = 0; end = Math.min (length, styles[i].length - lineOffset + styles[i].start); } else { start = styles[i].start - lineOffset; end = Math.min(length, start + styles[i].length); } if (start >= length) break; if (lastOffset < start) { layout.setStyle(null, lastOffset, start - 1); } layout.setStyle(getStyleRange(styles[i]), start, end); lastOffset = Math.max(lastOffset, end); } } } if (lastOffset < length) layout.setStyle(null, lastOffset, length); if (styledText != null && styledText.isFixedLineHeight()) { int index = -1; int lineCount = layout.getLineCount(); int height = getLineHeight(); for (int i = 0; i < lineCount; i++) { int lineHeight = layout.getLineBounds(i).height; if (lineHeight > height) { height = lineHeight; index = i; } } if (index != -1) { FontMetrics metrics = layout.getLineMetrics(index); ascent = metrics.getAscent() + metrics.getLeading(); descent = metrics.getDescent(); if (layouts != null) { for (int i = 0; i < layouts.length; i++) { if (layouts[i] != null && layouts[i] != layout) { layouts[i].setAscent(ascent); layouts[i].setDescent(descent); } } } if (styledText.verticalScrollOffset != 0) { int topIndex = styledText.topIndex; int topIndexY = styledText.topIndexY; int lineHeight = getLineHeight(); if (topIndexY >= 0) { styledText.verticalScrollOffset = (topIndex - 1) * lineHeight + lineHeight - topIndexY; } else { styledText.verticalScrollOffset = topIndex * lineHeight - topIndexY; } } styledText.calculateScrollBars(); if (styledText.isBidiCaret()) styledText.createCaretBitmaps(); styledText.caretDirection = SWT.NULL; styledText.setCaretLocation(); styledText.redraw(); } } return layout;
TextLayout getTextLayout(int lineIndex) { return getTextLayout(lineIndex, styledText.getOrientation(), styledText.getWrapWidth(), styledText.lineSpacing);
TextLayout getTextLayout(int lineIndex, int orientation, int width, int lineSpacing) {	TextLayout layout = null;	if (styledText != null) {		int topIndex = styledText.topIndex > 0 ? styledText.topIndex - 1 : 0;			if (layouts == null || topIndex != this.topIndex) {			TextLayout[] newLayouts = new TextLayout[CACHE_SIZE];			if (layouts != null) {				for (int i = 0; i < layouts.length; i++) {					if (layouts[i] != null) {						int layoutIndex = (i + this.topIndex) - topIndex;						if (0 <= layoutIndex && layoutIndex < newLayouts.length) {							newLayouts[layoutIndex] = layouts[i];						} else {							layouts[i].dispose();						}					}				}			}			if (bullets != null && bulletsIndices != null && topIndex != this.topIndex) {				int delta = topIndex - this.topIndex;				if (delta > 0) {					if (delta < bullets.length) {						System.arraycopy(bullets, delta, bullets, 0, bullets.length - delta);						System.arraycopy(bulletsIndices, delta, bulletsIndices, 0, bulletsIndices.length - delta);					}					int startIndex = Math.max(0, bullets.length - delta);					for (int i = startIndex; i < bullets.length; i++) bullets[i] = null;				} else {					if (-delta < bullets.length) {						System.arraycopy(bullets, 0, bullets, -delta, bullets.length + delta);						System.arraycopy(bulletsIndices, 0, bulletsIndices, -delta, bulletsIndices.length + delta);					}					int endIndex = Math.min(bullets.length, -delta);					for (int i = 0; i < endIndex; i++) bullets[i] = null;				}			}			this.topIndex = topIndex;			layouts = newLayouts;		}		if (layouts != null) {			int layoutIndex = lineIndex - topIndex;			if (0 <= layoutIndex && layoutIndex < layouts.length) {				layout = layouts[layoutIndex];				if (layout != null) {					if (lineWidth[lineIndex] != -1) return layout;				} else {					layout = layouts[layoutIndex] = new TextLayout(device);				}			}		}	}	if (layout == null) layout = new TextLayout(device);	String line = content.getLine(lineIndex);	int lineOffset = content.getOffsetAtLine(lineIndex);	int[] segments = null;	int indent = 0;	int alignment = SWT.LEFT;	boolean justify = false;	Bullet bullet = null;	int[] ranges = null;	StyleRange[] styles = null;	int rangeStart = 0, styleCount = 0;	StyledTextEvent event = null;	if (styledText != null) {		event = styledText.getLineStyleData(lineOffset, line);		segments = styledText.getBidiSegments(lineOffset, line);		indent = styledText.indent;		alignment = styledText.alignment;		justify = styledText.justify;	}	if (event != null) {		indent = event.indent;		alignment = event.alignment;		justify = event.justify;		bullet = event.bullet;		ranges = event.ranges;		styles = event.styles;		if (styles != null) {			styleCount = styles.length;			if (styledText.isFixedLineHeight()) {				for (int i = 0; i < styleCount; i++) {					if (styles[i].isVariableHeight()) {						styledText.verticalScrollOffset = -1;						styledText.setVariableLineHeight();						styledText.redraw();						break;					}				}			}		}		if (bullets == null || bulletsIndices == null) {			bullets = new Bullet[CACHE_SIZE];			bulletsIndices = new int[CACHE_SIZE];		}		int index = lineIndex - topIndex;		if (0 <= index && index < CACHE_SIZE) {			bullets[index] = bullet;			bulletsIndices[index] = event.bulletIndex;		}	} else {		if (lines != null) {			LineInfo info = lines[lineIndex];			if (info != null) {				if ((info.flags & INDENT) != 0) indent = info.indent;				if ((info.flags & ALIGNMENT) != 0) alignment = info.alignment;				if ((info.flags & JUSTIFY) != 0) justify = info.justify;				if ((info.flags & SEGMENTS) != 0) segments = info.segments;			}		}		if (bulletsIndices != null) {			bullets = null;			bulletsIndices = null;		}		if (bullets != null) {			for (int i = 0; i < bullets.length; i++) {				if (bullets[i].indexOf(lineIndex) != -1) {					bullet = bullets[i];					break;				}			}		}		ranges = this.ranges;		styles = this.styles;		styleCount = this.styleCount;		if (ranges != null) {			rangeStart = getRangeIndex(lineOffset, -1, styleCount << 1);		} else {			rangeStart = getRangeIndex(lineOffset, -1, styleCount);		}	}	if (bullet != null) {		StyleRange style = bullet.style;		GlyphMetrics metrics = style.metrics;		indent += metrics.width;	}	layout.setFont(regularFont);	layout.setAscent(ascent);	layout.setDescent(descent);	layout.setText(line);	layout.setOrientation(orientation);	layout.setSegments(segments);	layout.setWidth(width);	layout.setSpacing(lineSpacing);	layout.setTabs(new int[]{tabWidth});	layout.setIndent(indent);	layout.setAlignment(alignment);	layout.setJustify(justify);		int lastOffset = 0;	int length = line.length();	if (styles != null) {		if (ranges != null) {			int rangeCount = styleCount << 1;			for (int i = rangeStart; i < rangeCount; i += 2) {				int start, end;				if (lineOffset > ranges[i]) {					start = 0;					end = Math.min (length, ranges[i + 1] - lineOffset + ranges[i]);				} else {					start = ranges[i] - lineOffset;					end = Math.min(length, start + ranges[i + 1]);				}				if (start >= length) break;				if (lastOffset < start) {					layout.setStyle(null, lastOffset, start - 1);				}				layout.setStyle(getStyleRange(styles[i >> 1]), start, end);				lastOffset = Math.max(lastOffset, end);			}		} else {			for (int i = rangeStart; i < styleCount; i++) {				int start, end;				if (lineOffset > styles[i].start) {					start = 0;					end = Math.min (length, styles[i].length - lineOffset + styles[i].start);				} else {					start = styles[i].start - lineOffset;					end = Math.min(length, start + styles[i].length);				}				if (start >= length) break;				if (lastOffset < start) {					layout.setStyle(null, lastOffset, start - 1);				}				layout.setStyle(getStyleRange(styles[i]), start, end);				lastOffset = Math.max(lastOffset, end);			}		}	}	if (lastOffset < length) layout.setStyle(null, lastOffset, length);	if (styledText != null && styledText.isFixedLineHeight()) {		int index = -1;		int lineCount = layout.getLineCount();		int height = getLineHeight();		for (int i = 0; i < lineCount; i++) {			int lineHeight = layout.getLineBounds(i).height;			if (lineHeight > height) {				height = lineHeight;				index = i;			}		}		if (index != -1) {			FontMetrics metrics = layout.getLineMetrics(index);			ascent = metrics.getAscent() + metrics.getLeading();			descent = metrics.getDescent();			if (layouts != null) {				for (int i = 0; i < layouts.length; i++) {					if (layouts[i] != null && layouts[i] != layout) {						layouts[i].setAscent(ascent);						layouts[i].setDescent(descent);					}				}			}			if (styledText.verticalScrollOffset != 0) {				int topIndex = styledText.topIndex;				int topIndexY = styledText.topIndexY;				int lineHeight = getLineHeight();				if (topIndexY >= 0) {					styledText.verticalScrollOffset = (topIndex - 1) * lineHeight + lineHeight - topIndexY;				} else {					styledText.verticalScrollOffset = topIndex * lineHeight - topIndexY;				}			}			styledText.calculateScrollBars();			if (styledText.isBidiCaret()) styledText.createCaretBitmaps();			styledText.caretDirection = SWT.NULL;			styledText.setCaretLocation();			styledText.redraw();		}	}	return layout;}