/** * <p> * Creates and initializes the text layout used to compute the size hint. * </p> * * @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(" "); int tabWidth = fTextLayout.getBounds().width; fTextLayout.setTabs(new int[] {tabWidth}); fTextLayout.setText(""); }
/** * 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$ }
public static String shortenText(final GC gc, String text, final int width, final String ellipses) { if (gc.textExtent(text, 0).x <= width) { return text; } final int ellipseWidth = gc.textExtent(ellipses, 0).x; final int length = text.length(); final TextLayout layout = new TextLayout(gc.getDevice()); layout.setText(text); int end = layout.getPreviousOffset(length, SWT.MOVEMENT_CLUSTER); while (end > 0) { text = text.substring(0, end); final int l = gc.textExtent(text, 0).x; if (l + ellipseWidth <= width) { break; } end = layout.getPreviousOffset(end, SWT.MOVEMENT_CLUSTER); } layout.dispose(); return end == 0 ? text.substring(0, 1) : text + ellipses; }
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) { if (innerTagFactory == null) { innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder); } innerTagFactory.reset(); TextLayout layout = getCellTextLayout(cell); int counts = layout.getLineCount(); int contentHeight = 0; for (int i = 0; i < counts; i++) { contentHeight += layout.getLineBounds(i).height; } layout.dispose(); contentHeight += Math.max(counts - 1, 0) * SEGMENT_LINE_SPACING; contentHeight += 4;// 加上编辑模式下,StyledTextCellEditor的边框 contentHeight += topPadding; contentHeight += bottomPadding; return contentHeight; }
public TextLayout getTextLayout(GC gc, GridItem item, int columnIndex, boolean innerTagStyled, boolean drawInnerTag) { TextLayout layout = new TextLayout(gc.getDevice()); layout.setFont(font); layout.setTabs(new int[]{tabWidth}); innerTagFactory.reset(); String displayStr = ""; try{ displayStr = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag(item.getText(columnIndex))); }catch (NullPointerException e) { return null; } layout.setText(displayStr); int width = getBounds().width - leftMargin - rightMargin; layout.setWidth(width < 1 ? 1 : width); layout.setSpacing(Constants.SEGMENT_LINE_SPACING); layout.setAlignment(SWT.LEFT); layout.setOrientation(item.getParent().getOrientation()); if (displayStr.length() != 0 && innerTagStyled) { attachInnertTagStyle(gc, layout, drawInnerTag); } return layout; }
protected void drawInnerTag(GC gc, TextLayout layout) { String displayStr = layout.getText(); List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans(); Rectangle bounds = getBounds(); for (InnerTagBean innerTagBean : innerTagBeans) { String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean)); int start = displayStr.indexOf(placeHolder); if (start == -1) { continue; } if (gc != null) { Point p = layout.getLocation(start, false); int x = bounds.x + p.x + leftMargin; x += SEGMENT_LINE_SPACING; Point tagSize = tagRender.calculateTagSize(innerTagBean); int lineIdx = layout.getLineIndex(start); Rectangle r = layout.getLineBounds(lineIdx); int y = bounds.y + p.y + topMargin + r.height / 2 - tagSize.y /2; tagRender.draw(gc, innerTagBean, x, y - layout.getAscent()); } } }
public static void configure(TextLayout textLayout) { String text = textLayout.getText(); Document doc = new Document(text); ITokenScanner scanner = getRecipeScanner(doc); scanner.setRange(doc, 0, doc.getLength()); IToken token; while ((token = scanner.nextToken()) != Token.EOF) { int offset = scanner.getTokenOffset(); int length = scanner.getTokenLength(); Object data = token.getData(); if (data != null && data instanceof TextStyle) { TextStyle textStyle = (TextStyle) data; textLayout.setStyle(textStyle, offset, offset + length - 1); } } }
public static void configure(TextLayout textLayout) { String text = textLayout.getText(); Document doc = new Document(text); ITokenScanner scanner = getRecipeScanner(doc); scanner.setRange(doc, 0, doc.getLength()); IToken token; while ((token = scanner.nextToken()) != Token.EOF) { int offset = scanner.getTokenOffset(); int length = scanner.getTokenLength(); Object data = token.getData(); if (data != null && data instanceof TextStyle) { TextStyle textStyle = (TextStyle) data; textLayout.setStyle(textStyle, offset, offset + length - 1); } } scanner = null; doc = null; }
/** * @see Graphics#drawTextLayout(TextLayout, int, int, int, int, Color, * Color) */ public void drawTextLayout(TextLayout layout, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) { TextLayout scaled = zoomTextLayout(layout); if (scaled == null) { return; } try { graphics.drawTextLayout(scaled, (int) Math.floor(x * zoom + fractionalX), (int) Math.floor(y * zoom + fractionalY), selectionStart, selectionEnd, selectionBackground, selectionForeground); } finally { scaled.dispose(); } }
Point getPointInBox(TextFragmentBox box, int offset, int index, boolean trailing) { offset -= box.offset; offset = Math.min(box.length, offset); Point result = new Point(0, box.getTextTop()); if (bidiInfo == null) { if (trailing && offset < box.length) offset++; String substring = getText().substring(box.offset, box.offset + offset); result.x = getTextUtilities().getTextExtents(substring, getFont()).width; } else { TextLayout layout = FlowUtilities.getTextLayout(); layout.setFont(getFont()); String fragString = getBidiSubstring(box, index); layout.setText(fragString); offset += getBidiPrefixLength(box, index); result.x = layout.getLocation(offset, trailing).x; if (isMirrored()) result.x = box.width - result.x; } result.x += box.getX(); return result; }
public TextLayout getTextLayout(GC gc, GridItem item, int columnIndex, boolean innerTagStyled, boolean drawInnerTag) { TextLayout layout = new TextLayout(gc.getDevice()); layout.setFont(JFaceResources.getFont(net.heartsome.cat.ts.ui.Constants.MATCH_VIEWER_TEXT_FONT)); innerTagFactory.reset(); String displayStr = ""; try{ displayStr = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag(item.getText(columnIndex))); }catch (NullPointerException e) { return null; } layout.setText(displayStr); int width = getBounds().width - leftMargin - rightMargin; layout.setWidth(width < 1 ? 1 : width); layout.setSpacing(Constants.SEGMENT_LINE_SPACING); layout.setAlignment(SWT.LEFT); if (displayStr.length() != 0 && innerTagStyled) { attachInnertTagStyle(gc, layout, drawInnerTag); } return layout; }
protected void drawBlankString( Graphics g, String s ) { TextLayout tl = new TextLayout( Display.getCurrent( ) ); // bidi_hcg: Apply text direction tl.setOrientation( this.rtl ? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT ); tl.setText( s ); Rectangle rc = tl.getBounds( ); int left = ( getClientArea( ).width - rc.width ) / 2; int top = ( getClientArea( ).height - rc.height ) / 2; g.drawText( s, getClientArea( ).x + left, getClientArea( ).y + top ); tl.dispose(); }
/** * Convenience method * * @param inWidget * {@link StyledText} * @return {@link PaintObjectListener} */ public static PaintObjectListener getPaintObjectListener( final StyledText inWidget) { return new PaintObjectListener() { @Override public void paintObject(final PaintObjectEvent inEvent) { final Display lDisplay = inEvent.display; final StyleRange lStyle = inEvent.style; final int lPosition = inEvent.x + lStyle.metrics.width - BULLET_WIDTH + 2; Font lFont = lStyle.font; if (lFont == null) lFont = inWidget.getFont(); final TextLayout lLayout = new TextLayout(lDisplay); lLayout.setAscent(inEvent.ascent); lLayout.setDescent(inEvent.descent); lLayout.setFont(lFont); lLayout.setText(String.format("%s.", inEvent.bulletIndex + 1)); //$NON-NLS-1$ lLayout.draw(inEvent.gc, lPosition, inEvent.y); lLayout.dispose(); } }; }
private TextLayout getTextLayout(Plugin plugin, boolean selected) { final TextLayout textLayout = new TextLayout(display); int length1 = plugin.getName().length(); String text = plugin.getName() + "\n \n" + plugin.getDescription(); textLayout.setText(text); textLayout.setStyle(selected ? styleTitleSelected : styleTitle, 0, length1); textLayout.setStyle(styleGap, length1 + 1, length1 + 2); textLayout.setStyle(selected ? styleRowSelected : styleRow, length1 + 3, text.length()); return textLayout; }
protected TextLayout getTextLayout(int lineIndex, int orientation, int width, int lineSpacing, Object obj, Method proceed, Object[] args) throws Exception { args[0] = lineIndex; args[1] = orientation; args[2] = width; args[3] = lineSpacing; return (TextLayout) proceed.invoke(obj, args); }
boolean isSameLineSpacing(int lineIndex, int newLineSpacing) { if (layouts == null) { return false; } int layoutIndex = lineIndex - topIndex; if (0 <= layoutIndex && layoutIndex < layouts.length) { TextLayout layout = layouts[layoutIndex]; return layout != null && !layout.isDisposed() && layout.getSpacing() == newLineSpacing; } return false; }
protected void updateImage() { Rectangle bounds = getBounds(); if (bounds.width <= 0 || bounds.height <= 0) return; TextLayout layout = new TextLayout(Display.getDefault()); //layout.setFont(getFont()); layout.setText(getText()); for (StyleRange styleRange : ranges) { //styleRange.background = ColorConstants.white; layout.setStyle(styleRange, styleRange.start, styleRange.start + styleRange.length); } if (image != null && !image.isDisposed()) { image.dispose(); } image = new Image(Display.getDefault(), bounds.width, bounds.height); GC gc = new GC(image); gc.setBackground(ColorConstants.red); layout.draw(gc, 0, 0); image.getImageData().transparentPixel = image.getImageData().palette .getPixel(ColorConstants.white.getRGB()); layout.dispose(); gc.dispose(); }
public CellRenderer(final Grid<T> grid) { super(grid); errorImage = getImage("cell_error.gif"); contentLocation = new Point(0, 0); imageExtent = new Point(0, 0); innerBounds = new Rectangle(0, 0, 0, 0); textLayout = new TextLayout(grid.getDisplay()); anchorCollector = new StyleCollector(); grid.addDisposeListener(new DisposeListener() { @Override public void widgetDisposed(DisposeEvent e) { textLayout.dispose(); } }); }
int calculateOffset(final TextLayout layout, final int offset) { final int nextOffset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); if (nextOffset != offset) { return layout.getPreviousOffset(nextOffset, SWT.MOVEMENT_CLUSTER); } return offset; }
/** * Shorten the given text <code>t</code> so that its length doesn't exceed * the given width. The default implementation replaces characters in the * center of the original string with an ellipsis ("..."). * Override if you need a different strategy. * * @param gc the gc to use for text measurement * @param t the text to shorten * @param width the width to shorten the text to, in pixels * @return the shortened text * Note, this code was copied from * org.eclipse.swt.custom.CLabel.shortenText() */ protected String shortenText(GC gc, String t, int width) { if (t == null) return null; int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x; if (width<=w) return t; int l = t.length(); int max = l/2; int min = 0; int mid = (max+min)/2 - 1; if (mid <= 0) return t; TextLayout layout = new TextLayout (getDisplay()); layout.setText(t); mid = validateOffset(layout, mid); while (min < mid && mid < max) { String s1 = t.substring(0, mid); String s2 = t.substring(validateOffset(layout, l-mid), l); int l1 = gc.textExtent(s1, DRAW_FLAGS).x; int l2 = gc.textExtent(s2, DRAW_FLAGS).x; if (l1+w+l2 > width) { max = mid; mid = validateOffset(layout, (max+min)/2); } else if (l1+w+l2 < width) { min = mid; mid = validateOffset(layout, (max+min)/2); } else { min = max; } } String result = mid == 0 ? t : t.substring(0, mid) + ELLIPSIS + t.substring(validateOffset(layout, l-mid), l); layout.dispose(); return result; }
private TextLayout getCellTextLayout(LayerCell cell) { int orientation = editor.getTable().getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT); TextLayout layout = new TextLayout(editor.getTable().getDisplay()); layout.setOrientation(orientation); layout.setSpacing(Constants.SEGMENT_LINE_SPACING); layout.setFont(font); layout.setAscent(ascent); layout.setDescent(descent); // 和 StyledTextEditor 同步 layout.setTabs(new int[] { tabWidth }); Rectangle rectangle = cell.getBounds(); int width = rectangle.width - leftPadding - rightPadding; width -= 1; if (wrapText && width > 0) { layout.setWidth(width); } String displayText = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag((String) cell.getDataValue())); if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) { displayText = displayText.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n"); displayText = displayText.replaceAll("\\t", Constants.TAB_CHARACTER + "\u200B"); displayText = displayText.replaceAll(" ", Constants.SPACE_CHARACTER + "\u200B"); } layout.setText(displayText); List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans(); for (InnerTagBean innerTagBean : innerTagBeans) { String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean)); int start = displayText.indexOf(placeHolder); if (start == -1) { continue; } TextStyle style = new TextStyle(); Point rect = tagRender.calculateTagSize(innerTagBean); style.metrics = new GlyphMetrics(rect.y, 0, rect.x + SEGMENT_LINE_SPACING * 2); layout.setStyle(style, start, start + placeHolder.length() - 1); } return layout; }
private void appendNonprintingStyle(TextLayout layout) { TextStyle style = new TextStyle(font, GUIHelper.getColor(new RGB(100, 100, 100)), null); String s = layout.getText(); Matcher matcher = Constants.NONPRINTING_PATTERN.matcher(s); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); // style.metrics = new GlyphMetrics(10, 0, 1); layout.setStyle(style, start, end - 1); } }
/** * 设置字体 * @param font * ; */ public void setFont(Font font) { TextLayout layout = new TextLayout(Display.getDefault()); layout.setFont(font); StringBuffer tabBuffer = new StringBuffer(tabSize); for (int i = 0; i < tabSize; i++) { tabBuffer.append(' '); } layout.setText(tabBuffer.toString()); tabWidth = layout.getBounds().width; layout.dispose(); this.font = font; }
/** * @see Graphics#drawTextLayout(TextLayout, int, int, int, int, Color, * Color) */ public void drawTextLayout(TextLayout layout, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) { // $TODO probably just call checkPaint since Font and BG color don't // apply checkText(); layout.draw(gc, x + translateX, y + translateY, selectionStart, selectionEnd, selectionForeground, selectionBackground); }
/** * @param frag * @param string * @param font * @since 3.1 */ private static void initBidi(TextFragmentBox frag, String string, Font font) { if (frag.requiresBidi()) { TextLayout textLayout = getTextLayout(); textLayout.setFont(font); // $TODO need to insert overrides in front of string. textLayout.setText(string); } }
/** * @see TextLayout#getBounds() */ protected Rectangle getTextLayoutBounds(String s, Font f, int start, int end) { TextLayout textLayout = getTextLayout(); textLayout.setFont(f); textLayout.setText(s); return textLayout.getBounds(start, end); }
/** * Calculates the width taken up by the given text before a line-break is * encountered. * * @param text * the text in which the break is to be found * @param width * the width before the next line-break (if one's found; the * width of all the given text, otherwise) will be added on to * the first int in the given array * @return <code>true</code> if a line-break was found * @since 3.1 */ boolean addLeadingWordWidth(String text, int[] width) { if (text.length() == 0) return false; // if (Character.isWhitespace(text.charAt(0))) // return true; text = 'a' + text + 'a'; // TODO: GEFGWT commented out // FlowUtilities.LINE_BREAK.setText(text); // int index = FlowUtilities.LINE_BREAK.next() - 1; int index = 0; // GEFGWT commented out if (index == 0) return true; // while (Character.isWhitespace(text.charAt(index))) // index--; // boolean result = index < text.length() - 1; // index should point to the end of the actual text (not including the // 'a' that was // appended), if there were no breaks if (index == text.length() - 1) index--; text = text.substring(1, index + 1); if (bidiInfo == null) width[0] += getTextUtilities().getTextExtents(text, getFont()).width; else { TextLayout textLayout = FlowUtilities.getTextLayout(); textLayout.setFont(getFont()); textLayout.setText(text); width[0] += textLayout.getBounds().width; } return true; }
private int findOffset(Point p, int[] trailing, TextFragmentBox box, int boxIndex) { if (box == null) return -1; TextLayout layout = FlowUtilities.getTextLayout(); layout.setFont(getFont()); layout.setText(getBidiSubstring(box, boxIndex)); int x = p.x - box.getX(); if (isMirrored()) x = box.getWidth() - x; int layoutOffset = layout .getOffset(x, p.y - box.getTextTop(), trailing); return box.offset + layoutOffset - getBidiPrefixLength(box, boxIndex); }
protected void paintText(Graphics g, String draw, int x, int y, int bidiLevel) { if (bidiLevel == -1) { g.drawText(draw, x, y); } else { TextLayout tl = FlowUtilities.getTextLayout(); if (isMirrored()) tl.setOrientation(SWT.RIGHT_TO_LEFT); tl.setFont(g.getFont()); tl.setText(draw); g.drawTextLayout(tl, x, y); } }
/** * @param layout * @param cell * @param applyColors * @return the text width delta (0 if the text layout contains no other font) */ private int updateTextLayout(TextLayout layout, ViewerCell cell, boolean applyColors) { layout.setStyle(null, 0, Integer.MAX_VALUE); // clear old styles layout.setText(cell.getText()); layout.setFont(cell.getFont()); // set also if null to clear previous usages int originalTextWidth = layout.getBounds().width; // text width without any styles boolean containsOtherFont= false; StyleRange[] styleRanges = cell.getStyleRanges(); if (styleRanges != null) { // user didn't fill styled ranges for (int i = 0; i < styleRanges.length; i++) { StyleRange curr = prepareStyleRange(styleRanges[i], applyColors); layout.setStyle(curr, curr.start, curr.start + curr.length - 1); if (curr.font != null) { containsOtherFont= true; } } } int textWidthDelta = 0; if (containsOtherFont) { textWidthDelta = layout.getBounds().width - originalTextWidth; } return textWidthDelta; }