Java Code Examples for java.awt.font.LineMetrics.getAscent()

The following are Jave code examples for showing how to use getAscent() of the java.awt.font.LineMetrics class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: openjdk-jdk10   File: UnderlineTest.java   Source Code and License Vote up 7 votes
public void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, fpd.width, fpd.height);

    g.setColor(Color.RED);
    FontRenderContext frc = ((Graphics2D)g).getFontRenderContext();
    LineMetrics lm = f.getLineMetrics(fps, frc);
    int h = (int)(fpd.height - 20 - lm.getAscent());
    g.drawLine(20, h, fpd.width - 20, h);
    h = fpd.height - 20;
    g.drawLine(20, h, fpd.width - 20, h);
    h = (int)(fpd.height - 20 + lm.getDescent());
    g.drawLine(20, h, fpd.width - 20, h);

    g.setColor(Color.BLACK);
    g.setFont(f);
    g.drawString(fps, 50, fpd.height - 20);
}
 
Example 2
Project: OpenJSharp   File: StandardGlyphVector.java   Source Code and License Vote up 6 votes
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 3
Project: OpenJSharp   File: StandardGlyphVector.java   Source Code and License Vote up 6 votes
@Override
public Rectangle2D getLogicalBounds() {
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length() > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 4
Project: jdk8u-jdk   File: StandardGlyphVector.java   Source Code and License Vote up 6 votes
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 5
Project: openjdk-jdk10   File: StandardGlyphVector.java   Source Code and License Vote up 6 votes
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 6
Project: openjdk9   File: StandardGlyphVector.java   Source Code and License Vote up 6 votes
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 7
Project: openjdk9   File: UnderlineTest.java   Source Code and License Vote up 6 votes
public void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, fpd.width, fpd.height);

    g.setColor(Color.RED);
    FontRenderContext frc = ((Graphics2D)g).getFontRenderContext();
    LineMetrics lm = f.getLineMetrics(fps, frc);
    int h = (int)(fpd.height - 20 - lm.getAscent());
    g.drawLine(20, h, fpd.width - 20, h);
    h = fpd.height - 20;
    g.drawLine(20, h, fpd.width - 20, h);
    h = (int)(fpd.height - 20 + lm.getDescent());
    g.drawLine(20, h, fpd.width - 20, h);

    g.setColor(Color.BLACK);
    g.setFont(f);
    g.drawString(fps, 50, fpd.height - 20);
}
 
Example 8
Project: Push2Display   File: GVTLineMetrics.java   Source Code and License Vote up 6 votes
/**
 * Constructs a GVTLineMetrics object based on the specified line metrics
 * with a scale factor applied.
 *
 * @param lineMetrics The lineMetrics object that this metrics object will
 * be based upon.
 * @param scaleFactor The scale factor to apply to all metrics.
 */
public GVTLineMetrics(LineMetrics lineMetrics, float scaleFactor) {
    this.ascent = lineMetrics.getAscent() * scaleFactor;
    this.baselineIndex = lineMetrics.getBaselineIndex();
    this.baselineOffsets = lineMetrics.getBaselineOffsets();
    for (int i=0; i<baselineOffsets.length; i++) {
        this.baselineOffsets[i] *= scaleFactor;
    }
    this.descent = lineMetrics.getDescent() * scaleFactor;
    this.height = lineMetrics.getHeight() * scaleFactor;
    this.leading = lineMetrics.getLeading();
    this.numChars = lineMetrics.getNumChars();
    this.strikethroughOffset = 
        lineMetrics.getStrikethroughOffset() * scaleFactor;
    this.strikethroughThickness = 
        lineMetrics.getStrikethroughThickness() * scaleFactor;
    this.underlineOffset = lineMetrics.getUnderlineOffset() * scaleFactor;
    this.underlineThickness = 
        lineMetrics.getUnderlineThickness() * scaleFactor;
    this.overlineOffset = -this.ascent;
    this.overlineThickness = this.underlineThickness;
}
 
Example 9
Project: Push2Display   File: GVTLineMetrics.java   Source Code and License Vote up 6 votes
/**
 * Constructs a GVTLineMetrics object based on the specified line metrics.
 *
 * @param lineMetrics The lineMetrics object that this metrics object will
 * be based upon.
 */
public GVTLineMetrics(LineMetrics lineMetrics) {
    this.ascent = lineMetrics.getAscent();
    this.baselineIndex = lineMetrics.getBaselineIndex();
    this.baselineOffsets = lineMetrics.getBaselineOffsets();
    this.descent = lineMetrics.getDescent();
    this.height = lineMetrics.getHeight();
    this.leading = lineMetrics.getLeading();
    this.numChars = lineMetrics.getNumChars();
    this.strikethroughOffset = lineMetrics.getStrikethroughOffset();
    this.strikethroughThickness = lineMetrics.getStrikethroughThickness();
    this.underlineOffset = lineMetrics.getUnderlineOffset();
    this.underlineThickness = lineMetrics.getUnderlineThickness();
    this.overlineOffset = -this.ascent;
    this.overlineThickness = this.underlineThickness;
}
 
Example 10
Project: jdk8u_jdk   File: StandardGlyphVector.java   Source Code and License Vote up 6 votes
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 11
Project: lookaside_java-1.8.0-openjdk   File: StandardGlyphVector.java   Source Code and License Vote up 6 votes
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 12
Project: jfreechart   File: TextFragment.java   Source Code and License Vote up 6 votes
/**
 * Calculates the vertical offset between the baseline and the specified 
 * text anchor.
 * 
 * @param g2  the graphics device.
 * @param anchor  the anchor.
 * 
 * @return the offset.
 */
public float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) {
    float result = 0.0f;
    FontMetrics fm = g2.getFontMetrics(this.font);
    LineMetrics lm = fm.getLineMetrics("ABCxyz", g2);
    if (anchor.isTop()) {
        result = lm.getAscent();
    }
    else if (anchor.isHalfAscent()) {
        result = lm.getAscent() / 2.0f;
    }
    else if (anchor.isVerticalCenter()) {
        result = lm.getAscent() / 2.0f - lm.getDescent() / 2.0f;
    }
    else if (anchor.isBottom()) {
        result = -lm.getDescent() - lm.getLeading();
    }
    return result;                                             
}
 
Example 13
Project: JSSHTerminal   File: TextTerminal.java   Source Code and License Vote up 6 votes
private static int[] measureString(Font f,String s) {

    int[] ret = new int[3];
    if(dummyImg==null) {
      dummyImg = new BufferedImage(8,8,BufferedImage.TYPE_INT_RGB);
      Graphics2D g = (Graphics2D)dummyImg.getGraphics();
      //g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      //g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON);
      frc = g.getFontRenderContext();
      g.dispose();
    }
    LineMetrics lm = f.getLineMetrics(s,frc);
    Rectangle2D bounds = f.getStringBounds(s, frc);
    ret[0] = (int)(bounds.getWidth()+0.5);
    ret[1] = (int)(bounds.getHeight()+0.5);
    ret[2] = (int)(lm.getAscent()+0.5);
    return ret;

  }
 
Example 14
Project: jaer   File: CollapsablePanel.java   Source Code and License Vote up 5 votes
protected void paintComponent(Graphics g) {  
            super.paintComponent(g);  
            Graphics2D g2 = (Graphics2D) g;  
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                    RenderingHints.VALUE_ANTIALIAS_ON);  
            int h = getHeight();  
            int width = getWidth();
        	Polygon arrow = new Polygon();
//        	Polygon leftArrow = new Polygon();

//                g2.drawImage(closed, PAD, 0, h, h, this); 
            g2.setFont(font);  
            FontRenderContext frc = g2.getFontRenderContext();  
            LineMetrics lm = font.getLineMetrics(title, frc);  
            float height = lm.getAscent() + lm.getDescent();  
            float x = OFFSET;  
            float y = (h + height) / 2 - lm.getDescent();  
            g2.drawString(title, x, y);
            int th = ((int)height)-2;
            if (selected) {
            	arrow.addPoint(width - 3*th/2 + 1, h/2 - th/4);
            	arrow.addPoint(width - th/2 - 1, h/2 - th/4);
            	arrow.addPoint(width - th, h/2 + th/4 );
            	g2.fillPolygon(arrow);
            }
//                g2.drawImage(open, PAD, 0, h, h, this); 
            else { 
            	arrow.addPoint(width - 3*th/4, h/2 - (th-2)/2);
            	arrow.addPoint(width - 3*th/4, h/2 + (th-2)/2);
            	arrow.addPoint(width - 5*th/4, h /2);
            	g2.fillPolygon(arrow);
            }
        }
 
Example 15
Project: parabuild-ci   File: SpiderWebPlot.java   Source Code and License Vote up 5 votes
/**
 * Draws the label for one axis.
 * 
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value, 
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();
 
    String label = null;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }
 
    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent, 
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(), 
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 16
Project: ccu-historian   File: SpiderWebPlot.java   Source Code and License Vote up 5 votes
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 17
Project: gogui2   File: GameTreeNode.java   Source Code and License Vote up 5 votes
private void drawText(Graphics graphics)
{
    GameTreePanel.Label labelMode = m_gameTreePanel.getLabelMode();
    if (labelMode == GameTreePanel.Label.NONE)
        return;
    Move move = m_node.getMove();
    int size = m_gameTreePanel.getNodeSize();
    String text;
    if (labelMode == GameTreePanel.Label.MOVE)
    {
        if (move.getPoint() == null)
            return;
        text = move.getPoint().toString();
    }
    else
        text = Integer.toString(m_moveNumber);
    FontMetrics fontMetrics = graphics.getFontMetrics();
    LineMetrics lineMetrics = fontMetrics.getLineMetrics(text, graphics);
    int textWidth = fontMetrics.stringWidth(text);
    int ascent = (int)lineMetrics.getAscent();
    int xText = (size - textWidth) / 2;
    int yText = (ascent + size) / 2;
    if (move.getColor() == BLACK)
        graphics.setColor(Color.white);
    else
        graphics.setColor(Color.black);
    graphics.drawString(text, xText, yText);
}
 
Example 18
Project: jfreechart   File: SpiderWebPlot.java   Source Code and License Vote up 5 votes
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 19
Project: aya-lang   File: SpiderWebPlot.java   Source Code and License Vote up 5 votes
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 20
Project: pumpernickel   File: TextBlock.java   Source Code and License Vote up 5 votes
protected void paintText(Graphics2D g, GeneralPath bodyOutline) {
	Font font = getFont();
	String text = getText();
	g.setFont(font);
	FontRenderContext frc = g.getFontRenderContext();
	Rectangle2D r = font.getStringBounds(text, frc);
	LineMetrics lineMetrics = font.getLineMetrics(text, frc);
	
	Rectangle2D shapeBounds = ShapeBounds.getBounds(bodyOutline);
	float shapeCenterX = (float)shapeBounds.getCenterX();
	float shapeCenterY = (float)shapeBounds.getCenterY();
	float textX = (float)(shapeCenterX - r.getWidth()/2f);
	float textY = (float)(shapeCenterY + lineMetrics.getAscent()/2f - lineMetrics.getDescent()/3f);
	

	if(isTextShadowActive()) {
		Paint paint = this.getTextPaint();
		boolean isTextDark = true;
		if(paint instanceof Color) {
			Color color = (Color)paint;
			isTextDark = (color.getRed()+color.getGreen()+color.getBlue())/3 < 120;
		}
		g.setColor( isTextDark ? new Color(255, 255, 255, 40) : new Color(0,0,0,40));
		g.translate(0,1);
		g.drawString(text,  textX, textY);
		g.translate(0,1);
		g.drawString(text,  textX, textY);
		g.translate(0, -2);
	}

	g.setPaint(getTextPaint());
	g.drawString(text,  textX, textY);
}
 
Example 21
Project: DeutschSim   File: GateTable.java   Source Code and License Vote up 4 votes
@Override
public void paintComponent(Graphics g) {		
	super.paintComponent(g);
	
	Graphics2D g2d = (Graphics2D) g;
	g2d.setColor(Color.BLACK);
	g2d.setStroke(new BasicStroke(2));

	for (int y = gate_table_row_height / 2; y < get_canvas_height(); y += gate_table_row_height) {
		g2d.drawLine(0, y, get_canvas_width(), y);
	}
	
	for (int row = 0; row < table.get_row_count(); row++)
		for (int col = 0; col < table.get_col_count(); col++) {
			Gate gate = table.get_element(row, col);
			if (gate != null) {
				final int x = gate_table_col_width * col, y = gate_table_row_height * row,
						gate_height = gate_table_row_height * (gate.get_ports_number() - 1) + gate_table_cell_size;
				
				g2d.setStroke(new BasicStroke(1));
				
				g2d.setColor(Color.WHITE);
				g2d.fillRect(x + 1, y + 1, gate_table_cell_size - 1, gate_height - 1);
				
				g2d.setColor(Color.BLACK);
				g2d.drawRect(x, y, gate_table_cell_size, gate_height);
				
				g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				
				if (gate.get_id().equals(Tools.CONTROL_ID))
					dot_image.paintIcon(this, g2d, x, y);
				else {
					g2d.setFont(Tools.gate_font);
					
					FontRenderContext frc = g2d.getFontRenderContext();
					final int text_width = (int) Tools.gate_font.getStringBounds(gate.get_id(), frc).
							getWidth();
					
					LineMetrics lm = Tools.gate_font.getLineMetrics(gate.get_id(), frc);
					final int text_height = (int) (lm.getAscent() + lm.getDescent());
					
					final int text_x = x + (gate_table_cell_size - text_width) / 2,
							text_y = (int) (y + (gate_height + text_height) / 2 - lm.getDescent());
					
					// TODO add text cut-off
					g2d.drawString(gate.get_id(), text_x, text_y);
				}
			}
		}
	
	if (handler.get_last_mouse_point() != null) {
		g2d.setStroke(new BasicStroke(1));
		
		final int x = handler.get_last_mouse_point().x - handler.get_last_mouse_point().x % gate_table_col_width,
				y = handler.get_last_mouse_point().y - handler.get_last_mouse_point().y % gate_table_row_height;
		
		Color inner_transparent = new Color(255, 0, 0, 255 / 4);
		g2d.setColor(inner_transparent);
		g2d.fillRect(x, y, gate_table_cell_size, gate_table_cell_size);
		
		g2d.setColor(Color.RED);
		g2d.drawRect(x, y, gate_table_cell_size, gate_table_cell_size);
	}
}
 
Example 22
Project: ccu-historian   File: TextUtils.java   Source Code and License Vote up 4 votes
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not <code>null</code>, this
 *                    object will be updated by this method to match the
 *                    string bounds).
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isHorizontalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;
}
 
Example 23
Project: ccu-historian   File: TextUtilities.java   Source Code and License Vote up 4 votes
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not <code>null</code>, this
 *                    object will be updated by this method to match the
 *                    string bounds).
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 24
Project: ccu-historian   File: TextUtilities.java   Source Code and License Vote up 4 votes
/**
 * A utility method that calculates the rotation anchor offsets for a
 * string.  These offsets are relative to the text starting coordinate
 * (<code>BASELINE_LEFT</code>).
 *
 * @param g2  the graphics device.
 * @param text  the text.
 * @param anchor  the anchor point.
 *
 * @return The offsets.
 */
private static float[] deriveRotationAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor) {

    float[] result = new float[2];
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics metrics = g2.getFont().getLineMetrics(text, frc);
    FontMetrics fm = g2.getFontMetrics();
    Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
    float ascent = metrics.getAscent();
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isLeft()) {
        xAdj = 0.0f;
    }
    else if (anchor.isHorizontalCenter()) {
        xAdj = (float) bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = descent + leading - (float) bounds.getHeight();
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = descent + leading - (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isHalfAscent()) {
        yAdj = -halfAscent;
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = metrics.getDescent() + metrics.getLeading();
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 25
Project: gogui2   File: Field.java   Source Code and License Vote up 4 votes
private void drawLabel(int fieldX, int fieldY, Graphics boardGraphics,
                       Image boardImage, int boardWidth)
{
    setComposite(COMPOSITE_97);
    setFont(m_graphics, m_size);
    FontMetrics fontMetrics = m_graphics.getFontMetrics();
    LineMetrics lineMetrics =
        fontMetrics.getLineMetrics(m_label, m_graphics);
    int width = fontMetrics.stringWidth(m_label);
    int height = fontMetrics.getHeight();
    int ascent = (int)lineMetrics.getAscent();
    int x = Math.max((m_size - width) / 2, 0);
    int y = (ascent + m_size) / 2;
    if (m_ghostStone == null)
    {
        if (m_color == BLACK)
            m_graphics.setColor(Color.white);
        else
            m_graphics.setColor(Color.black);
    }
    else
    {
        if (m_ghostStone == BLACK)
            m_graphics.setColor(Color.white);
        else
            m_graphics.setColor(Color.black);
    }
    Rectangle oldClip = m_graphics.getClipBounds();
    width = Math.min(width, (int)(0.95 * m_size));
    m_graphics.setClip(x, y - ascent, width, height);
    if (m_color == EMPTY && m_ghostStone == null)
    {
        Rectangle oldBoardClip = boardGraphics.getClipBounds();
        boardGraphics.setClip(fieldX + x, fieldY + y - ascent,
                              width, height);
        boardGraphics.drawImage(boardImage, 0, 0, boardWidth, boardWidth,
                                null);
        if (m_fieldColor != null)
            drawFieldColor();
        boardGraphics.setClip(oldBoardClip);
        m_graphics.setColor(Color.black);
    }
    m_graphics.drawString(m_label, x, y);
    m_graphics.setClip(oldClip);
}
 
Example 26
Project: jfreechart   File: TextUtils.java   Source Code and License Vote up 4 votes
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not {@code null}).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not {@code null}, this
 *                    object will be updated by this method to match the
 *                    string bounds).
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = TextUtils.getTextBounds(text, g2, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 27
Project: jfreechart   File: TextUtils.java   Source Code and License Vote up 4 votes
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not {@code null}).
 * @param text  the text.
 * @param anchor  the anchor point.
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor) {

    float[] result = new float[2];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, g2, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 28
Project: balloonist   File: VerticalLineBreakMeasurer.java   Source Code and License Vote up 4 votes
public AbstractTextLayout nextLayout(float requestedWrappingHeight, int requestedEndIndex)
{
	// System.out.println("nextLayout requestedWrappingHeight="+requestedWrappingHeight+", requestedEndIndex="+requestedEndIndex);
	
	final char[] CHAR_HOLDER = new char[1];
	
	VerticalTextLayout vtl = new VerticalTextLayout();
	
	int accumulatedHeight = 0;
	int characters = 0;
	
	int initialIndex = attributedCharacterIterator.getIndex();
	
	float maxCharWidth = 16.0f; // TODO no
	boolean roomy = true;
	
	char theChar = 0;
	
	for (theChar = attributedCharacterIterator.current(); 
			theChar!=AttributedCharacterIterator.DONE && roomy && (requestedEndIndex==-1 || attributedCharacterIterator.getIndex()<requestedEndIndex);
			theChar=attributedCharacterIterator.next())
	{
		Font theFont = Font.getFont(attributedCharacterIterator.getAttributes()); // TODO only do this at end of run
		
		// System.out.println("theFont="+theFont+ "theChar="+theChar);
		
		CHAR_HOLDER[0]=theChar;
		LineMetrics lm = theFont.getLineMetrics(CHAR_HOLDER, 0, 1, fontRenderContext);
		
		if (accumulatedHeight + lm.getHeight() <requestedWrappingHeight)
		{
			if (lm.getAscent()>vtl.getAscent())
				vtl.setAscent(lm.getAscent());

			if (lm.getDescent()>vtl.getDescent())
				vtl.setDescent(lm.getDescent());

			if (lm.getLeading()>vtl.getLeading())
				vtl.setLeading(lm.getLeading());

			// TODO check width

			accumulatedHeight += lm.getHeight();

			characters++; // TODO redundant to ACI.getCharacterCount() ?
		}
		else
			roomy = false;
	}
	
	// System.out.println("accumulatedHeight="+accumulatedHeight);
	// System.out.println("characters="+characters);
	
	if (accumulatedHeight==0 || characters==0)
		return null;
	
	vtl.setAttributedCharacterIterator(attributedCharacterIterator);
	vtl.setBeginIndex(initialIndex);
	
	if (theChar==AttributedCharacterIterator.DONE)
	{
		vtl.setEndIndex(attributedCharacterIterator.getIndex());
	}
	else
		vtl.setEndIndex(attributedCharacterIterator.getIndex()+1);
	
	vtl.setCharacterCount(vtl.getEndIndex()-vtl.getBeginIndex()); // TODO is this right?
	
	vtl.setBounds(new Rectangle2D.Float(0.0f, 0.0f, maxCharWidth, accumulatedHeight)); // TODO ?
	
	return vtl;
}