void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) { StyleRange style = bullet.style; GlyphMetrics metrics = style.metrics; Color color = style.foreground; if (color != null) gc.setForeground(color); Font font = style.font; if (font != null) gc.setFont(font); String string = ""; int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER); switch (type) { case ST.BULLET_DOT: string = "\u2022"; break; case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break; case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break; case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break; } if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text; Display display = styledText.getDisplay(); TextLayout layout = new TextLayout(display); layout.setText(string); layout.setAscent(lineAscent); layout.setDescent(lineDescent); style = (StyleRange)style.clone(); style.metrics = null; if (style.font == null) style.font = getFont(style.fontStyle); layout.setStyle(style, 0, string.length()); int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - BULLET_MARGIN); layout.draw(gc, x, paintY); layout.dispose(); } int drawLine(int lineIndex, int paintX, int paintY, GC gc, Color widgetBackground, Color widgetForeground) {
private int updateTextLayout(TableItem item, int index, boolean isSelected) { fSharedLayout.setFont(item.getFont(index)); // XXX: needed to clear the style info, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=226090 fSharedLayout.setText(""); //$NON-NLS-1$ fSharedLayout.setText(item.getText(index)); int originalTextWidth= fSharedLayout.getBounds().width; // text width without any styles StyleRange[] ranges= getStyledRanges(item, index); if (ranges != null) { for (StyleRange range : ranges) { StyleRange curr= range; if (isSelected) { curr= (StyleRange) curr.clone(); curr.foreground= null; curr.background= null; } fSharedLayout.setStyle(curr, curr.start, curr.start + curr.length - 1); } } return fSharedLayout.getBounds().width - originalTextWidth; }
private TableOwnerDrawSupport(Table table) { int orientation= table.getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT); fSharedLayout= new TextLayout(table.getDisplay()); fSharedLayout.setOrientation(orientation); }
if ( ( (TextLayout) object ).isDisposed() ) { return; ( (TextLayout) object ).draw( gc, 0, 0 ); return;
private void doDispose() { if (textLayout != null && !textLayout.isDisposed()) { textLayout.dispose(); } if (resourceManager != null) { // Disposing the resource manager will dispose the color resourceManager.dispose(); resourceManager = null; } }
newLayouts[layoutIndex] = layouts[i]; } else { layouts[i].dispose(); 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); layout.setFont(regularFont); layout.setAscent(ascent); layout.setDescent(descent); layout.setText(line); layout.setOrientation(orientation); layout.setSegments(segments); layout.setSegmentsChars(segmentChars); layout.setWidth(width); layout.setSpacing(lineSpacing); layout.setTabs(tabs); layout.setIndent(indent); layout.setWrapIndent(wrapIndent); layout.setAlignment(alignment); layout.setJustify(justify); layout.setStyle(null, lastOffset, start - 1);
String shortenText(GC gc, String text, int width, String ellipses) { if (gc.textExtent(text, FLAGS).x <= width) return text; int ellipseWidth = gc.textExtent(ellipses, FLAGS).x; int length = text.length(); TextLayout layout = new TextLayout(parent.getDisplay()); layout.setText(text); int end = layout.getPreviousOffset(length, SWT.MOVEMENT_CLUSTER); while (end > 0) { text = text.substring(0, end); int l = gc.textExtent(text, FLAGS).x; if (l + ellipseWidth <= width) { break; } end = layout.getPreviousOffset(end, SWT.MOVEMENT_CLUSTER); } layout.dispose(); return end == 0 ? text.substring(0, 1) : text + ellipses; }
/** * Creates and initializes the text layout used * to compute the size hint. * * @since 3.2 */ private void createTextLayout() { fTextLayout= new TextLayout(fBrowser.getDisplay()); // Initialize fonts String symbolicFontName= fSymbolicFontName == null ? JFaceResources.DIALOG_FONT : fSymbolicFontName; Font font= JFaceResources.getFont(symbolicFontName); fTextLayout.setFont(font); fTextLayout.setWidth(-1); font= JFaceResources.getFontRegistry().getBold(symbolicFontName); fBoldStyle= new TextStyle(font, null, null); // Compute and set tab width fTextLayout.setText(" "); //$NON-NLS-1$ int tabWidth= fTextLayout.getBounds().width; fTextLayout.setTabs(new int[] { tabWidth }); fTextLayout.setText(""); //$NON-NLS-1$ }
protected String endShortenText(GC gc, String t, int width, int w) { TextLayout layout = new TextLayout (getDisplay()); layout.setText(t); String result = t; int len = gc.textExtent(result, DRAW_FLAGS).x; while ( len + w > width && result.length () > 0 ) { result = result.substring ( 0, validateOffset ( layout, result.length () - 1 ) ); len = gc.textExtent(result, DRAW_FLAGS).x; } layout.dispose(); return result + ellipsis; } protected String centerShortenText(GC gc, String t, int width, int w) {
int lineCount = layout.getLineCount(); while (index < lineCount) { Rectangle bounds = layout.getLineBounds(index); if (y <= bounds.y) break; index++; topOffset = content.getOffsetAtLine(lineIndex + 1); } else { topOffset = content.getOffsetAtLine(lineIndex) + layout.getLineOffsets()[index];
Rectangle [] getRectanglesInPixels (int linkIndex) { int lineCount = layout.getLineCount (); Rectangle [] rects = new Rectangle [lineCount]; int [] lineOffsets = layout.getLineOffsets (); Point point = offsets [linkIndex]; int lineStart = 1; while (point.x > lineOffsets [lineStart]) lineStart++; int lineEnd = 1; while (point.y > lineOffsets [lineEnd]) lineEnd++; int index = 0; if (lineStart == lineEnd) { rects [index++] = DPIUtil.autoScaleUp(layout.getBounds (point.x, point.y)); } else { rects [index++] = DPIUtil.autoScaleUp(layout.getBounds (point.x, lineOffsets [lineStart]-1)); rects [index++] = DPIUtil.autoScaleUp(layout.getBounds (lineOffsets [lineEnd-1], point.y)); if (lineEnd - lineStart > 1) { for (int i = lineStart; i < lineEnd - 1; i++) { rects [index++] = DPIUtil.autoScaleUp(layout.getLineBounds (i)); } } } if (rects.length != index) { Rectangle [] tmp = new Rectangle [index]; System.arraycopy (rects, 0, tmp, 0, index); rects = tmp; } return rects; }
cleanUpListBlock.setText(fCleanUpNames); TextLayout textLayout= new TextLayout(messageComposite.getDisplay()); textLayout.setText(fCleanUpNames); int lineCount= textLayout.getLineCount(); if (lineCount < 5) gridData.heightHint= textLayout.getLineBounds(0).height * 6; textLayout.dispose();
tableComposite.setLayout(tableColumnLayout); table = new Table(tableComposite, SWT.SINGLE | SWT.FULL_SELECTION); textLayout = new TextLayout(table.getDisplay()); textLayout.setOrientation(defaultOrientation); Font boldFont = resourceManager.createFont(FontDescriptor.createFrom( table.getFont()).setStyle(SWT.BOLD)); textLayout.setFont(table.getFont()); textLayout.setText(QuickAccessMessages.QuickAccess_AvailableCategories); int maxProviderWidth = (textLayout.getBounds().width); textLayout.setFont(boldFont); for (QuickAccessProvider provider : providers) { textLayout.setText(provider.getName()); int width = (textLayout.getBounds().width); if (width > maxProviderWidth) { maxProviderWidth = width;
int getVisualLineIndex(TextLayout layout, int offsetInLine) { int lineIndex = layout.getLineIndex(offsetInLine); int[] offsets = layout.getLineOffsets(); Caret caret = getCaret(); if (caret != null && lineIndex != 0 && offsetInLine == offsets[lineIndex]) { int lineY = layout.getLineBounds(lineIndex).y; int caretY = caret.getLocation().y - getLinePixel(getCaretLine()); if (lineY > caretY) lineIndex--; caretAlignment = OFFSET_LEADING; } return lineIndex; } int getCaretDirection() {
int getOffsetAtPoint(int x, int y, int[] trailing, boolean inTextOnly) { if (inTextOnly && y + getVerticalScrollOffset() < 0 || x + horizontalScrollOffset < 0) { return -1; } int bottomIndex = getPartialBottomIndex(); int height = getLinePixel(bottomIndex + 1); if (inTextOnly && y > height) { return -1; } int lineIndex = getLineIndex(y); int lineOffset = content.getOffsetAtLine(lineIndex); TextLayout layout = renderer.getTextLayout(lineIndex); x += horizontalScrollOffset - leftMargin; y -= getLinePixel(lineIndex); int offset = layout.getOffset(x, y, trailing); Rectangle rect = layout.getLineBounds(layout.getLineIndex(offset)); renderer.disposeTextLayout(layout); if (inTextOnly && !(rect.x <= x && x <= rect.x + rect.width)) { return -1; } return offset + lineOffset; } /**
checkWidget (); if (string == null) error (SWT.ERROR_NULL_ARGUMENT); if (layoutText != null) layoutText.dispose (); layoutText = null; if (boldFont != null) boldFont.dispose (); if (string.length () != 0) { Display display = getDisplay (); layoutText = new TextLayout (display); layoutText.setText (string); Font font = display.getSystemFont (); FontData data = font.getFontData () [0]; boldFont = new Font (display, data.getName (), data.getHeight (), SWT.BOLD); TextStyle style = new TextStyle (boldFont, null, null); layoutText.setStyle (style, 0, string.length ());
void disposeTextLayout (TextLayout layout) { if (layouts != null) { for (int i = 0; i < layouts.length; i++) { if (layouts[i] == layout) return; } } layout.dispose(); } void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) {
/** * Returns the bounds of the receiver. The width returned is either the * width of the longest line or the width set using {@link TextLayout#setWidth(int)}. * To obtain the text bounds of a line use {@link TextLayout#getLineBounds(int)}. * * @return the bounds of the receiver * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> * * @see #setWidth(int) * @see #getLineBounds(int) */ public Rectangle getBounds() { checkLayout(); Rectangle bounds = DPIUtil.autoScaleDown(getDevice(), getBoundsInPixels()); int lineCount = OS.pango_layout_get_line_count(layout); int totalLineheight = 0; for (int i = 0; i < lineCount; i++) { totalLineheight += this.getLineBounds(i).height; } bounds.height = totalLineheight; return bounds; } Rectangle getBoundsInPixels() {
@Override void setFontDescription (long /*int*/ font) { super.setFontDescription (font); layout.setFont (Font.gtk_new (display, font)); }
/** * Draws the receiver's text using the specified GC at the specified * point. * * @param gc the GC to draw * @param x the x coordinate of the top left corner of the rectangular area where the text is to be drawn * @param y the y coordinate of the top left corner of the rectangular area where the text is to be drawn * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the gc is null</li> * </ul> */ public void draw(GC gc, int x, int y) { draw(gc, x, y, -1, -1, null, null); }