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

The following are Jave code examples for showing how to use getDescent() 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.
+ Save this method
Example 1
Project: openjdk-jdk10   File: UnderlineTest.java   View Source Code 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   View Source Code 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   View Source Code 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   View Source Code 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   View Source Code 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   View Source Code 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   View Source Code 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: intellij-ce-playground   File: TextPainter.java   View Source Code Vote up 6 votes
private double drawHeaderOrFooterLine(Graphics2D g, double x, double y, double w, String headerText,
                                      String alignment) {
  FontRenderContext fontRenderContext = g.getFontRenderContext();
  LineMetrics lineMetrics = getHeaderFooterLineMetrics(g);
  float lineHeight = lineMetrics.getHeight();
  if (myPerformActualDrawing) {
    headerText = convertHeaderText(headerText);
    g.setFont(myHeaderFont);
    g.setColor(Color.black);
    float descent = lineMetrics.getDescent();
    double width = myHeaderFont.getStringBounds(headerText, fontRenderContext).getWidth() + getCharWidth(g);
    float yPos = (float) (lineHeight - descent + y);
    if (PrintSettings.LEFT.equals(alignment)) {
      drawStringToGraphics(g, headerText, x, yPos);
    } else if (PrintSettings.CENTER.equals(alignment)) {
      drawStringToGraphics(g, headerText, (float) (x + (w - width) / 2), yPos);
    } else if (PrintSettings.RIGHT.equals(alignment)) {
      drawStringToGraphics(g, headerText, (float) (x + w - width), yPos);
    }
  }
  return lineHeight;
}
 
Example 9
Project: ccu-historian   File: TextFragment.java   View Source Code 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;
    final FontMetrics fm = g2.getFontMetrics(this.font);
    final 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 10
Project: jdk8u_jdk   File: StandardGlyphVector.java   View Source Code 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   View Source Code 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: intellij-ce-playground   File: ASGallery.java   View Source Code Vote up 6 votes
private void paintLabel(Graphics g, int cell, Rectangle cellBounds, @Nullable String label, int thumbnailHeight) {
  if (!StringUtil.isEmpty(label)) {
    final Color fg;
    if (hasFocus() && cell == mySelectedIndex && (getImage(cell) != null || UIUtil.isUnderDarcula())) {
      fg = UIUtil.getTreeSelectionForeground();
    }
    else {
      fg = UIUtil.getTreeForeground();
    }
    GraphicsUtil.setupAntialiasing(g);
    g.setColor(fg);
    FontMetrics fontMetrics = g.getFontMetrics();
    LineMetrics metrics = fontMetrics.getLineMetrics(label, g);
    int width = fontMetrics.stringWidth(label);

    int textBoxTop = myCellMargin.top + thumbnailHeight;
    int cellBottom = cellBounds.height - myCellMargin.bottom;

    int textY = cellBounds.y + (cellBottom + textBoxTop + (int)(metrics.getHeight() - metrics.getDescent())) / 2 ;
    int textX = (cellBounds.width - myCellMargin.left - myCellMargin.right - width) / 2 + cellBounds.x + myCellMargin.left;
    g.drawString(label, textX, textY);
  }
}
 
Example 13
Project: jfreechart   File: TextFragment.java   View Source Code 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 14
Project: pumpernickel   File: JLink.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g) {
  super.paint(g);
	
	if(drawLine) {
		LineMetrics m = getFont().getLineMetrics(getText(),frc);
		Insets i = getInsets();
		int descent = (int)m.getDescent()-4;
		if(isEnabled()) {
			g.setColor(getForeground());
		} else {
			g.setColor(SystemColor.textInactiveText);
		}
		g.drawLine(i.left,getHeight()-i.bottom-descent,getWidth()-i.right-1,getHeight()-i.bottom-descent);
	}
}
 
Example 15
Project: Push2Display   File: GVTLineMetrics.java   View Source Code 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 16
Project: jaer   File: CollapsablePanel.java   View Source Code 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 17
Project: parabuild-ci   File: MeterLegend.java   View Source Code Vote up 5 votes
/**
 * Creates a legend item
 *
 * @param graphics  the graphics device.
 * @param item  the legend item.
 * @param x  the x coordinate.
 * @param y  the y coordinate.
 *
 * @return the legend item.
 */
private DrawableLegendItem createLegendItem(Graphics graphics,
                                            LegendItem item, double x, double y) {

    int innerGap = 2;
    FontMetrics fm = graphics.getFontMetrics();
    LineMetrics lm = fm.getLineMetrics(item.getLabel(), graphics);
    float textHeight = lm.getHeight();

    DrawableLegendItem drawable = new DrawableLegendItem(item);

    float xloc = (float) (x + innerGap + 1.15f * textHeight);
    float yloc = (float) (y + innerGap + (textHeight - lm.getLeading() - lm.getDescent()));

    drawable.setLabelPosition(new Point2D.Float(xloc, yloc));

    float boxDim = textHeight * 0.70f;
    xloc = (float) (x + innerGap + 0.15f * textHeight);
    yloc = (float) (y + innerGap + 0.15f * textHeight);

    drawable.setMarker(new Rectangle2D.Float(xloc, yloc, boxDim, boxDim));

    float width = (float) (drawable.getLabelPosition().getX() - x
                           + fm.stringWidth(item.getLabel()) + 0.5 * textHeight);

    float height = 2 * innerGap + textHeight;
    drawable.setBounds(x, y, width, height);
    return drawable;

}
 
Example 18
Project: erp   File: ImprimeLayout.java   View Source Code Vote up 5 votes
private void adicRodape(Graphics g, int iPag) {
	Graphics2D g2d = ( Graphics2D ) g.create();
	g2d.setPaint(Color.black);
	g2d.setFont(mRodFont);
	LineMetrics metrics = mRodFont.getLineMetrics(mDataStr, g2d.getFontRenderContext());
	int iTamTexto = getFontMetrics(mRodFont).stringWidth(mDataStr);
	int iPosYTexto = ( int ) ( iMY - metrics.getDescent() - metrics.getLeading() );
	int iPosRodY = ( int ) ( iMY - metrics.getHeight() );
	g2d.setStroke(new BasicStroke(2));
	g2d.drawLine(0, iPosRodY, iMX, iPosRodY);
	g2d.drawString(( iPag + 1 ) + " de " + iNumPags, 0, iPosYTexto);
	g2d.drawString(mDataStr, iMX - iTamTexto, iPosYTexto);
	g2d.dispose();
}
 
Example 19
Project: pumpernickel   File: TextBlock.java   View Source Code 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 20
Project: intellij-ce-playground   File: TextPainter.java   View Source Code Vote up 5 votes
private float getDescent(Graphics g) {
  if (myDescent >= 0) {
    return myDescent;
  }
  FontRenderContext fontRenderContext = ((Graphics2D) g).getFontRenderContext();
  LineMetrics lineMetrics = myPlainFont.getLineMetrics(DEFAULT_MEASURE_HEIGHT_TEXT, fontRenderContext);
  myDescent = lineMetrics.getDescent();
  return myDescent;
}
 
Example 21
Project: DeutschSim   File: GateTable.java   View Source Code 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   View Source Code 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   View Source Code 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   View Source Code 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: jfreechart   File: TextUtils.java   View Source Code 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}).
 *
 * @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 = TextUtils.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 26
Project: balloonist   File: VerticalLineBreakMeasurer.java   View Source Code 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;
}
 
Example 27
Project: aya-lang   File: TextUtils.java   View Source Code 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.
 *
 * @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, 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: aya-lang   File: TextUtils.java   View Source Code 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;
}