public GlyphMetrics getGlyphMetrics(int ix) { if (ix < 0 || ix >= glyphs.length) { throw new IndexOutOfBoundsException("ix = " + ix); } Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D(); Point2D pt = getGlyphPosition(ix); vb.setRect(vb.getMinX() - pt.getX(), vb.getMinY() - pt.getY(), vb.getWidth(), vb.getHeight()); Point2D.Float adv = getGlyphStrike(ix).strike.getGlyphMetrics(glyphs[ix]); GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y, vb, GlyphMetrics.STANDARD); return gm; }
@Override public GlyphMetrics getGlyphMetrics(int ix) { if (ix < 0 || ix >= glyphs.length()) { throw new IndexOutOfBoundsException("ix = " + ix); } Rectangle2D vb = getGlyphVisualBounds(ix).getBounds2D(); Point2D pt = getGlyphPosition(ix); vb.setRect(vb.getMinX() - pt.getX(), vb.getMinY() - pt.getY(), vb.getWidth(), vb.getHeight()); Point2D.Float adv = strike.getGlyphMetrics( glyphs.charAt( ix ) ); GlyphMetrics gm = new GlyphMetrics(true, adv.x, adv.y, vb, (byte)0); return gm; }
@Override public MissingGlyph getGlyph(String unicode) { FontRenderContext frc = new FontRenderContext(null, true, true); GlyphVector vec = sysFont.createGlyphVector(frc, unicode); Glyph glyph = (Glyph)glyphCache.get(unicode); if (glyph == null) { glyph = new Glyph(); glyph.setPath(vec.getGlyphOutline(0)); GlyphMetrics gm = vec.getGlyphMetrics(0); glyph.setHorizAdvX((int)gm.getAdvanceX()); glyph.setVertAdvY((int)gm.getAdvanceY()); glyph.setVertOriginX(0); glyph.setVertOriginY(0); glyphCache.put(unicode, glyph); } return glyph; }
public Shape getGlyphLogicalBounds(int glyphIndex) { GlyphMetrics gm = getGlyphMetrics( glyphIndex ); if( gm == null ) return null; Rectangle2D r = gm.getBounds2D(); Point2D p = getGlyphPosition( glyphIndex ); double[] bounds = new double[] {p.getX() + r.getX() - gm.getLSB(), p.getY() + r.getY(), p.getX() + r.getX() - gm.getLSB() + gm.getAdvanceX(), p.getY() + r.getY() + r.getHeight()}; if (glyphTransforms[glyphIndex] != null) glyphTransforms[glyphIndex].transform(bounds, 0, bounds, 0, 2); return new Rectangle2D.Double(bounds[0], bounds[1], bounds[2] - bounds[0], bounds[3] - bounds[1]); }
private GlyphVector getWrappedGlyphVector(String str, float wrapping, Font font, FontRenderContext frc) { Point2D gmPos = new Point2D.Double(0.0d, 0.0d); GlyphVector gv = font.createGlyphVector(frc, str); float lineheight = (float) gv.getLogicalBounds().getHeight(); float xpos = 0.0f; float advance = 0.0f; int lineCount = 0; GlyphMetrics gm; for (int i = 0; i < gv.getNumGlyphs(); i++) { gm = gv.getGlyphMetrics(i); advance = gm.getAdvance(); if ((xpos < wrapping) && (wrapping <= (xpos + advance))) { lineCount++; xpos = 0.0f; } gmPos.setLocation(xpos, lineheight * lineCount); gv.setGlyphPosition(i, gmPos); xpos = xpos + advance; } return gv; }
Glyph (int codePoint, Rectangle bounds, GlyphVector vector, int index, UnicodeFont unicodeFont) { this.codePoint = codePoint; GlyphMetrics metrics = vector.getGlyphMetrics(index); int lsb = (int)metrics.getLSB(); if (lsb > 0) lsb = 0; int rsb = (int)metrics.getRSB(); if (rsb > 0) rsb = 0; int glyphWidth = bounds.width - lsb - rsb; int glyphHeight = bounds.height; if (glyphWidth > 0 && glyphHeight > 0) { int padTop = unicodeFont.getPaddingTop(); int padRight = unicodeFont.getPaddingRight(); int padBottom = unicodeFont.getPaddingBottom(); int padLeft = unicodeFont.getPaddingLeft(); int glyphSpacing = 1; // Needed to prevent filtering problems. width = (short)(glyphWidth + padLeft + padRight + glyphSpacing); height = (short)(glyphHeight + padTop + padBottom + glyphSpacing); yOffset = (short)(unicodeFont.getAscent() + bounds.y - padTop); } shape = vector.getGlyphOutline(index, -bounds.x + unicodeFont.getPaddingLeft(), -bounds.y + unicodeFont.getPaddingTop()); isMissing = !unicodeFont.getFont().canDisplay((char)codePoint); }
/** * Default Glyph constructor */ public LinuxGlyph(char c, int glyphIndex) { float metrics[] = new float[6]; int[] pxlMetrics = new int[6]; this.pFont = 0; this.fontSize = 0; Rectangle2D.Float rect = new Rectangle2D.Float(metrics[0], -metrics[1], metrics[4], metrics[5]); this.glMetrics = new GlyphMetrics((float)Math.ceil(metrics[2]), rect, (byte)0); this.glCode = glyphIndex; this.glChar = c; Rectangle rct = new Rectangle(pxlMetrics[0], -pxlMetrics[1], pxlMetrics[4], pxlMetrics[5]); this.glPointMetrics = new GlyphMetrics(pxlMetrics[2], rct, (byte)1); }
/** * Default Glyph constructor */ public WinGlyph(char c, int glyphIndex) { float metrics[] = new float[6]; int[] pxlMetrics = new int[6]; this.pFont = 0; this.fontSize = 0; Rectangle2D.Float rect = new Rectangle2D.Float(metrics[0], -metrics[1], metrics[4], metrics[5]); this.glMetrics = new GlyphMetrics((float)Math.ceil(metrics[2]), rect, (byte)0); this.glCode = glyphIndex; this.glChar = c; Rectangle rct = new Rectangle(pxlMetrics[0], -pxlMetrics[1], pxlMetrics[4], pxlMetrics[5]); this.glPointMetrics = new GlyphMetrics(pxlMetrics[2], rct, (byte)1); }
@Override public GlyphMetrics getGlyphMetrics(){ if (glMetrics == null) { float[] metrics = getGlyphMetrics(glyphPointer); this.glMetrics = new GlyphMetrics( true, Math.round(metrics[0]),//metrics[0], Math.round(metrics[1]),//metrics[1], //new Rectangle2D.Double(initOutline().getBounds2D().getMinX(), initOutline().getBounds2D().getMinY(), initOutline().getBounds2D().getMaxX() + 5, initOutline().getBounds2D().getMaxY()), initOutline().getBounds2D(),//new Rectangle2D.Float(metrics[2], -metrics[5]-1,metrics[4]- metrics[2] + 1, metrics[5] - metrics[3] + 1), GlyphMetrics.STANDARD); } return glMetrics; }
protected void writeGlyph(String characterName, Shape glyph, GlyphMetrics glyphMetrics) throws IOException { PDFStream glyphStream = pdf.openStream( createCharacterReference(characterName), new String[] { "Flate", "ASCII85" }); Rectangle2D bounds = glyphMetrics != null ? glyphMetrics.getBounds2D() : glyph.getBounds2D(); double advance = glyphMetrics != null ? glyphMetrics.getAdvance() : getUndefinedWidth(); glyphStream.glyph(advance, 0, bounds.getX(), bounds.getY(), bounds .getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight()); boolean windingRule = glyphStream.drawPath(glyph); if (windingRule) { glyphStream.fillEvenOdd(); } else { glyphStream.fill(); } pdf.close(glyphStream); }
/** * Create a new glyph * * @param codePoint The code point in which this glyph can be found * @param bounds The bounds that this glrph can fill * @param vector The vector this glyph is part of * @param index The index of this glyph within the vector * @param unicodeFont The font this glyph forms part of */ public Glyph(int codePoint, Rectangle bounds, GlyphVector vector, int index, UnicodeFont unicodeFont) { this.codePoint = codePoint; GlyphMetrics metrics = vector.getGlyphMetrics(index); int lsb = (int)metrics.getLSB(); if (lsb > 0) lsb = 0; int rsb = (int)metrics.getRSB(); if (rsb > 0) rsb = 0; int glyphWidth = bounds.width - lsb - rsb; int glyphHeight = bounds.height; if (glyphWidth > 0 && glyphHeight > 0) { int padTop = unicodeFont.getPaddingTop(); int padRight = unicodeFont.getPaddingRight(); int padBottom = unicodeFont.getPaddingBottom(); int padLeft = unicodeFont.getPaddingLeft(); int glyphSpacing = 1; // Needed to prevent filtering problems. width = (short)(glyphWidth + padLeft + padRight + glyphSpacing); height = (short)(glyphHeight + padTop + padBottom + glyphSpacing); yOffset = (short)(unicodeFont.getAscent() + bounds.y - padTop); } shape = vector.getGlyphOutline(index, -bounds.x + unicodeFont.getPaddingLeft(), -bounds.y + unicodeFont.getPaddingTop()); isMissing = !unicodeFont.getFont().canDisplay((char)codePoint); }
private int[] getGlyphMetrics (Font font, int codePoint) { // xOffset and xAdvance will be incorrect for unicode characters such as combining marks or non-spacing characters // (eg Pnujabi's "\u0A1C\u0A47") that require the context of surrounding glyphs to determine spacing, but thisis the // best we can do with the BMFont format. char[] chars = Character.toChars(codePoint); GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT); GlyphMetrics metrics = vector.getGlyphMetrics(0); int xOffset = vector.getGlyphPixelBounds(0, null, 0, 0).x - unicodeFont.getPaddingLeft(); int xAdvance = (int)(metrics.getAdvanceX() + unicodeFont.getPaddingAdvanceX() + unicodeFont.getPaddingLeft() + unicodeFont .getPaddingRight()); return new int[] {xOffset, xAdvance}; }
public void runTest(Object ctx, int numReps) { GVContext gvctx = (GVContext)ctx; GlyphVector gv = gvctx.gv; GlyphMetrics gm; do { for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) { gm = gv.getGlyphMetrics(i); } } while (--numReps >= 0); }