private void setPreferredWidth() { Element root = codeArea.getDocument().getDefaultRootElement(); int lines = root.getElementCount(); int digits = Math.max(String.valueOf(lines).length(), 3); if (lastDigits != digits) { lastDigits = digits; FontMetrics fontMetrics = getFontMetrics(getFont()); int width = fontMetrics.charWidth('0') * digits; Insets insets = getInsets(); int preferredWidth = insets.left + insets.right + width; Dimension d = getPreferredSize(); if (d != null) { d.setSize(preferredWidth, NUM_HEIGHT); setPreferredSize(d); setSize(d); } } }
protected synchronized void damage(Rectangle r) { if (r != null) { JTextComponent component = getComponent(); x = r.x; y = r.y; Font font = component.getFont(); width = component.getFontMetrics(font).charWidth('w'); height = r.height; repaint(); } } }
public void paint(Graphics g) { if (isVisible()) { try { JTextComponent component = getComponent(); Rectangle r = component.getUI().modelToView(component, getDot()); Color c = g.getColor(); g.setColor(component.getBackground()); g.setXORMode(component.getCaretColor()); r.setBounds(r.x, r.y, g.getFontMetrics().charWidth('w'), g.getFontMetrics().getHeight()); g.fillRect(r.x, r.y, r.width, r.height); g.setPaintMode(); g.setColor(c); } catch (BadLocationException e) { e.printStackTrace(); } } }
/** * Calculate the width needed to display the maximum line number */ private void setPreferredWidth() { Element root = component.getDocument().getDefaultRootElement(); int lines = root.getElementCount(); int digits = Math.max(String.valueOf(lines).length(), minimumDisplayDigits); // Update sizes when number of digits in the line number changes if (lastDigits != digits) { lastDigits = digits; FontMetrics fontMetrics = getFontMetrics(getFont()); int width = fontMetrics.charWidth('0') * digits; Insets insets = getInsets(); int preferredWidth = insets.left + insets.right + width; Dimension d = getPreferredSize(); d.setSize(preferredWidth, HEIGHT); setPreferredSize(d); setSize(d); } }
protected TextString convertToCharacters(String text, Graphics2D g, FontFactory fontFactory, ColorFactory colorFactory) { TextString characters = new TextString(); FontRenderContext frc = g.getFontRenderContext(); double lastx = 0; for (int i = 0; i < text.length(); i++) { Font font = fontFactory.getFont(i); char c = text.charAt(i); FontMetrics fm = g.getFontMetrics(font); Rectangle2D bounds = font.getStringBounds(String.valueOf(c), frc); TextCharacter tc = new TextCharacter(); tc.setCharacter(c); tc.setFont(font); tc.setWidth(fm.charWidth(c)); tc.setHeight(fm.getAscent() + fm.getDescent()); tc.setAscent(fm.getAscent()); tc.setDescent(fm.getDescent()); tc.setX(lastx); tc.setY(0); tc.setFont(font); tc.setColor(colorFactory.getColor(i)); lastx += bounds.getWidth(); characters.addCharacter(tc); } return characters; }
@Override public float nextTabStop(float x, int tabOffset) { if (tabSizeInSpaces == 0) { return x; } int tabSizeInPixels = tabSizeInSpaces * fm.charWidth(' '); int ntabs = (((int) x) - xOffset) / tabSizeInPixels; return xOffset + ((ntabs + 1f) * tabSizeInPixels); }
private void paintInEdit(Graphics g, byte content, int x, int y) { g.setFont(HexView.BOLD_FONT); g.setColor(Color.white); char[] chars = getChars(content); if (selectedChar == 0) { g.setColor(HexView.SELECTED_COLOR); g.drawChars(chars, 0, 1, x, y); g.setColor(Color.black); g.drawChars(chars, 1, 1, x + g.getFontMetrics().charWidth(chars[0]), y); } else { g.setColor(Color.black); g.drawChars(chars, 0, 1, x, y); g.setColor(HexView.SELECTED_COLOR); g.drawChars(chars, 1, 1,x + g.getFontMetrics().charWidth(chars[0]), y); } setDefault(g); }
/** * This is here so subclasses such as <code>RSyntaxTextArea</code> that * have multiple fonts can define exactly what it means, for example, for * the margin line to be "80 characters" over. */ protected void updateMarginLineX() { Font font = getFont(); if (font == null) { marginLineX = 0; return; } marginLineX = getFontMetrics(font).charWidth('m') * marginSizeInChars; }
/** * Paint a selected line * @param g Graphics instance. * @param x int. x axis value. * @param y int. y axis value. */ private void paintInSelected(Graphics g, int x, int y) { g.setFont(HexView.BOLD_FONT); char[] content = model.getLineChars(selectedLine); g.drawChars(content, 0, selectedIndexInLine - 0, x, y); g.setColor(HexView.SELECTED_COLOR); x += g.getFontMetrics().charsWidth(content, 0, selectedIndexInLine-0); g.drawChars(content, selectedIndexInLine, 1, x, y); g.setColor(Color.black); x += g.getFontMetrics().charWidth(content[selectedIndexInLine]); g.drawChars(content, selectedIndexInLine+1, (content.length-1)-selectedIndexInLine, x, y); g.setFont(HexView.FONT); }
/** * Determines the minimum span for this view along an * axis. This is implemented to provide the superclass * behavior after first making sure that the current font * metrics are cached (for the nested lines which use * the metrics to determine the height of the potentially * wrapped lines). * * @param axis may be either View.X_AXIS or View.Y_AXIS * @return the span the view would like to be rendered into. * Typically the view is told to render into the span * that is returned, although there is no guarantee. * The parent may choose to resize or break the view. * @see View#getMinimumSpan */ @Override public float getMinimumSpan(int axis) { updateMetrics(); float span = super.getPreferredSpan(axis); if (axis==View.X_AXIS) { // EOL marker span += metrics.charWidth('\u00b6'); // metrics set in updateMetrics } return span; }
/** * Determines the maximum span for this view along an * axis. This is implemented to provide the superclass * behavior after first making sure that the current font * metrics are cached (for the nested lines which use * the metrics to determine the height of the potentially * wrapped lines). * * @param axis may be either View.X_AXIS or View.Y_AXIS * @return the span the view would like to be rendered into. * Typically the view is told to render into the span * that is returned, although there is no guarantee. * The parent may choose to resize or break the view. * @see View#getMaximumSpan */ @Override public float getMaximumSpan(int axis) { updateMetrics(); float span = super.getPreferredSpan(axis); if (axis==View.X_AXIS) { // EOL marker span += metrics.charWidth('\u00b6'); // metrics set in updateMetrics } return span; }
int w = width * ta.getFontMetrics(ta.getFont()).charWidth('m'); int h = (int) (lines * ta.getFontMetrics(ta.getFont()).getHeight()); ta.setPreferredSize(new Dimension(w, h));
final void updateMetrics() { Component host = getContainer(); Font f = host.getFont(); metrics = host.getFontMetrics(f); // Metrics for the default font. tabSize = getTabSize() * metrics.charWidth('m'); }
this.width = g.getFontMetrics().charWidth(dotChar); if (this.isVisible()) { g.fillRect(r.x, r.y, this.width, r.height);
@Override public int getOffsetBeforeX(RSyntaxTextArea textArea, TabExpander e, float startX, float endBeforeX) { FontMetrics fm = textArea.getFontMetricsForTokenType(getType()); int i = textOffset; int stop = i + textCount; float x = startX; while (i<stop) { if (text[i]=='\t') { x = e.nextTabStop(x, 0); } else { x += fm.charWidth(text[i]); } if (x>endBeforeX) { // If not even the first character fits into the space, go // ahead and say the first char does fit so we don't go into // an infinite loop. int intoToken = Math.max(i-textOffset, 1); return getOffset() + intoToken; } i++; } // If we got here, the whole token fit in (endBeforeX-startX) pixels. return getOffset() + textCount - 1; }
/** * Changes the width of the cells in the JList so you can see every digit * of each. */ void updateCellWidths() { int oldCellWidth = cellWidth; cellWidth = getRhsBorderWidth(); // Adjust the amount of space the line numbers take up, if necessary. if (textArea!=null) { Font font = getFont(); if (font!=null) { FontMetrics fontMetrics = getFontMetrics(font); int count = 0; int lineCount = textArea.getLineCount() + getLineNumberingStartIndex() - 1; do { lineCount = lineCount/10; count++; } while (lineCount >= 10); cellWidth += fontMetrics.charWidth('9')*(count+1) + 3; } } if (cellWidth!=oldCellWidth) { // Always true revalidate(); } }
float span = longLineWidth + getRhsCorrection(); // fudge factor if (host.getEOLMarkersVisible()) { span += metrics.charWidth('\u00B6');
/** * Iterate over the lines represented by the child elements * of the element this view represents, looking for the line * that is the longest. The <em>longLine</em> variable is updated to * represent the longest line contained. The <em>font</em> variable * is updated to indicate the font used to calculate the * longest line. */ void calculateLongestLine() { Component c = getContainer(); font = c.getFont(); metrics = c.getFontMetrics(font); tabSize = getTabSize() * metrics.charWidth(' '); Element lines = getElement(); int n = lines.getElementCount(); for (int i=0; i<n; i++) { Element line = lines.getElement(i); float w = getLineWidth(i); if (w > longLineWidth) { longLineWidth = w; longLine = line; } } }
if (axis==View.X_AXIS) { // Add EOL marker span = super.getPreferredSpan(axis); span += metrics.charWidth('\u00b6'); // metrics set in updateMetrics