GC gc = new GC( text ); FontMetrics fm = gc.getFontMetrics(); int charWidth = fm.getAverageCharWidth(); int fieldWidth = text.computeSize( charWidth * 20, SWT.DEFAULT ).x; gc.dispose();
/** * Compute width of square * * @param styledText * @return the width of square */ private static int getSquareWidth(FontMetrics fontMetrics) { // width = 2 spaces + size width of square int width = 2 * fontMetrics.getAverageCharWidth() + getSquareSize(fontMetrics); return width; }
/** * Computes and remembers the x-offset of the margin column for the * current widget font. */ private void computeWidgetX() { GC gc= new GC(fTextWidget); int pixels= gc.getFontMetrics().getAverageCharWidth(); gc.dispose(); fCachedWidgetX= pixels * fMarginWidth; }
private int getAvarageCharWith(Control control) { GC gc= null; try { gc= new GC(control); return gc.getFontMetrics().getAverageCharWidth(); } finally { if (gc != null) gc.dispose(); } } }
private int getAvarageCharWith(Control control) { GC gc = null; try { gc = new GC(control); return gc.getFontMetrics().getAverageCharWidth(); } finally { if (gc != null) { gc.dispose(); } } }
@Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { GC gc= new GC(fText); gc.setFont(fTextFont); int width= gc.getFontMetrics().getAverageCharWidth(); int height= fText.getLineHeight(); //https://bugs.eclipse.org/bugs/show_bug.cgi?id=377109 gc.dispose(); return new Point(widthInChars * width, heightInChars * height); } }
@Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { GC gc= new GC(fText); gc.setFont(fTextFont); int width= gc.getFontMetrics().getAverageCharWidth(); int height= fText.getLineHeight(); //https://bugs.eclipse.org/bugs/show_bug.cgi?id=377109 gc.dispose(); return new Point(widthInChars * width, heightInChars * height); } }
@Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { GC gc= new GC(fText); gc.setFont(fTextFont); int width= gc.getFontMetrics().getAverageCharWidth(); int height= fText.getLineHeight(); gc.dispose(); return new Point (widthInChars * width, heightInChars * height); } }
@Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { GC gc= new GC(fText); gc.setFont(fTextFont); int width= gc.getFontMetrics().getAverageCharWidth(); int height= fText.getLineHeight(); gc.dispose(); return new Point (widthInChars * width, heightInChars * height); } }
private int getCharWidth() { GC gc = new GC(getTextWidget()); int charWidth = gc.getFontMetrics().getAverageCharWidth(); gc.dispose(); return charWidth; }
/** * Determine the average width of font used by the control. * * @param control * @return int */ public static final int getFontWidth(Control control) { GC gc = new GC(control.getDisplay()); int width = gc.getFontMetrics().getAverageCharWidth(); gc.dispose(); return width; }
@Override public void initializePanel(Composite parent) { final FillLayout rl = new FillLayout(); parent.setLayout(rl); final Text t = new Text(parent, SWT.SINGLE | SWT.ICON_CANCEL | SWT.CANCEL); t.setText(startString); final GC gc = new GC(t); final FontMetrics fm = gc.getFontMetrics(); final int width = CHARS * fm.getAverageCharWidth(); final int height = fm.getHeight(); gc.dispose(); t.setSize(t.computeSize(width, height)); t.addListener(SWT.DefaultSelection, this); t.addListener(SWT.Modify, this); vehicles.addAll(roadModel.getObjectsOfType(Vehicle.class)); }
@Override public void handleEvent(Event event) { GC gc = new GC(consoleText); int charWidth = gc.getFontMetrics().getAverageCharWidth(); gc.dispose(); int areaWidth = consoleText.getBounds().width; consoleText.setTabs(areaWidth / 6 / charWidth); } });
protected int getTextWidth(final Control control, final int width) { final GC gc = new GC(control); try { gc.setFont(title.getFont()); return gc.getFontMetrics().getAverageCharWidth() * width; } finally { gc.dispose(); } }
@Override public void handleEvent(Event event) { Color old = event.gc.getForeground(); event.gc.setForeground(Colors.black); int char_width = event.gc.getFontMetrics().getAverageCharWidth(); String pb_text = pbt.convert(result.getSelection()); event.gc.drawText(pb_text, (result.getSize().x - pb_text.length() * char_width )/2, -1, true); event.gc.setForeground(old); } });
/** * @return */ private int getDefaultWidth() { ICSSStyle style = this.getCSSStyle(); if (style == null) { style = DefaultStyle.getInstance(); } ICSSFont font = style.getCSSFont(); int fontWidth = FigureUtilities.getFontMetrics(font.getSwtFont()) .getAverageCharWidth(); return _columns * fontWidth + ARRAWWIDTH + HORIZONTAL_PADDING; }
/** * Returns the average character width of the given control's font. * * @param control the control to calculate the average char width for * @return the average character width of the controls font * * @since 3.4 */ public static int getAverageCharWidth(Control control) { GC gc= new GC(control); gc.setFont(control.getFont()); int increment= gc.getFontMetrics().getAverageCharWidth(); gc.dispose(); return increment; }
@Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { if (fSymbolicFontName == null) return null; GC gc= new GC(fBrowser); Font font= fSymbolicFontName == null ? JFaceResources.getDialogFont() : JFaceResources.getFont(fSymbolicFontName); gc.setFont(font); int width= gc.getFontMetrics().getAverageCharWidth(); int height= gc.getFontMetrics().getHeight(); gc.dispose(); return new Point(widthInChars * width, heightInChars * height); }
/** Populates the height and width of the system font. */ private static void populateSystemFont() { // create a tiny image to bind our GC to (not that it can't be size 0) Image dummyImg = new Image(assertUI(), 1, 1); GC gc = new GC(dummyImg); FontMetrics metrics = gc.getFontMetrics(); systemFontHeight = metrics.getHeight(); systemFontWidth = metrics.getAverageCharWidth(); if (OS.getNative().isMac()) { // add 20% width on Mac systemFontWidth = (systemFontWidth * 12) / 10; } gc.dispose(); dummyImg.dispose(); }
/** * Adjust the width and height of a control according to amount * of characters that should have space enough to be displayed * within this control. * * @param control * @param widthInChar * @param heightInChar */ protected void adjustSize(Control control, int widthInChar, int heightInChar) { org.eclipse.swt.graphics.GC gc = new org.eclipse.swt.graphics.GC(control); FormData data = ((FormData) control.getLayoutData()); if (widthInChar > 0) { data.width = getScaler().convertXToCurrent(widthInChar * gc.getFontMetrics().getAverageCharWidth()); } if (heightInChar > 0) { data.height = getScaler().convertYToCurrent(heightInChar * gc.getFontMetrics().getHeight()); } gc.dispose(); }