@Override public AABBox getStringBounds(CharSequence string, float pixelSize) { if (string == null) { return new AABBox(); } final Metrics tmpMetrics = getMetrics(); final float lineGap = tmpMetrics.getLineGap(pixelSize); final float ascent = tmpMetrics.getAscent(pixelSize); final float descent = tmpMetrics.getDescent(pixelSize); final float advanceY = lineGap - descent + ascent; float totalHeight = 0; float totalWidth = 0; float curLineWidth = 0; for (int i = 0; i < string.length(); i++) { char character = string.charAt(i); if (character == '\n') { totalWidth = Math.max(curLineWidth, totalWidth); curLineWidth = 0; totalHeight -= advanceY; continue; } Glyph glyph; try { glyph = getGlyph(character); curLineWidth += glyph.getAdvance(pixelSize, true); } catch (FontException e) { logger.error(e.getMessage()); } } if (curLineWidth > 0) { totalHeight -= advanceY; totalWidth = Math.max(curLineWidth, totalWidth); } return new AABBox(0, 0, 0, totalWidth, totalHeight, 0); }
@Override public FontMetrics getFontMetrics(Font font) { if (font != null) { PGFont prismFont = (PGFont) font.impl_getNativeFont(); Metrics metrics = PrismFontUtils.getFontMetrics(prismFont); // TODO: what's the difference between ascent and maxAscent? float maxAscent = -metrics.getAscent(); // metrics.getMaxAscent(); float ascent = -metrics.getAscent(); float xheight = metrics.getXHeight(); float descent = metrics.getDescent(); // TODO: what's the difference between descent and maxDescent? float maxDescent = metrics.getDescent(); // metrics.getMaxDescent(); float leading = metrics.getLineGap(); return FontMetrics.impl_createFontMetrics( maxAscent, ascent, xheight, descent, maxDescent, leading, font); } else { return null; // this should never happen } }