Java Code Examples for java.awt.font.GlyphVector#getGlyphMetrics()

The following examples show how to use java.awt.font.GlyphVector#getGlyphMetrics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
  Point2D gmPos = new Point2D.Float();
  GlyphVector gv = font.createGlyphVector(frc, str);
  float lineHeight = (float) gv.getLogicalBounds().getHeight();
  float pos = 0f;
  int lineCount = 0;
  GlyphMetrics gm;

  for (int i = 0; i < gv.getNumGlyphs(); i++) {
    // TEST: gv.setGlyphTransform(i, at);
    gm = gv.getGlyphMetrics(i);
    float advance = gm.getAdvance();
    if (pos < width && width <= pos + advance) {
      lineCount++;
      pos = 0f;
    }
    gmPos.setLocation(pos, lineHeight * lineCount);
    gv.setGlyphPosition(i, gmPos);
    pos += advance;
  }
  return gv;
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static GlyphVector makeJustifiedGlyphVector(GlyphVector gv, int width) {
  Rectangle2D r = gv.getVisualBounds();
  float jw = (float) width;
  float vw = (float) r.getWidth();
  if (jw > vw) {
    int num = gv.getNumGlyphs();
    float xx = (jw - vw) / (num - 1f);
    float pos = num == 1 ? (jw - vw) * .5f : 0f;
    Point2D gmPos = new Point2D.Float();
    for (int i = 0; i < num; i++) {
      GlyphMetrics gm = gv.getGlyphMetrics(i);
      gmPos.setLocation(pos, 0f);
      gv.setGlyphPosition(i, gmPos);
      pos += gm.getAdvance() + xx;
    }
  }
  return gv;
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
  Point2D gmPos = new Point2D.Float();
  GlyphVector gv = font.createGlyphVector(frc, str);
  float lineHeight = (float) gv.getLogicalBounds().getHeight();
  float pos = 0f;
  int lineCount = 0;
  GlyphMetrics gm;
  for (int i = 0; i < gv.getNumGlyphs(); i++) {
    gm = gv.getGlyphMetrics(i);
    float advance = gm.getAdvance();
    if (pos < width && width <= pos + advance) {
      lineCount++;
      pos = 0f;
    }
    gmPos.setLocation(pos, lineHeight * lineCount);
    gv.setGlyphPosition(i, gmPos);
    pos += advance;
  }
  return gv;
}
 
Example 4
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static GlyphVector getWrappedGlyphVector(String str, double width, Font font, FontRenderContext frc) {
  Point2D gmPos = new Point2D.Float();
  GlyphVector gv = font.createGlyphVector(frc, str);
  float lineHeight = (float) gv.getLogicalBounds().getHeight();
  float pos = 0f;
  int lineCount = 0;
  GlyphMetrics gm;
  for (int i = 0; i < gv.getNumGlyphs(); i++) {
    gm = gv.getGlyphMetrics(i);
    float advance = gm.getAdvance();
    if (pos < width && width <= pos + advance) {
      lineCount++;
      pos = 0f;
    }
    gmPos.setLocation(pos, lineHeight * lineCount);
    gv.setGlyphPosition(i, gmPos);
    pos += advance;
  }
  return gv;
}
 
Example 5
Source File: GlyphVectorSerializationWrapper.java    From pumpernickel with MIT License 6 votes vote down vote up
public GlyphElement(GlyphVector gv, int index) {
	glyphCode = gv.getGlyphCode(index);
	transform = gv.getGlyphTransform(index);
	outline = new ShapeSerializationWrapper(gv.getGlyphOutline(index));
	position = new Point2DSerializationWrapper(
			gv.getGlyphPosition(index));
	logicalBounds = new ShapeSerializationWrapper(
			gv.getGlyphLogicalBounds(index));

	GlyphJustificationInfo gji = gv.getGlyphJustificationInfo(index);
	justificationInfo = gji == null ? null
			: new GlyphJustificationInfoSerializationWrapper(gji);
	glyphMetrics = new GlyphMetricsSerializationWrapper(
			gv.getGlyphMetrics(index));
	visualBounds = new ShapeSerializationWrapper(
			gv.getGlyphVisualBounds(index));
}
 
Example 6
Source File: BMFontUtil.java    From gdx-skineditor with Apache License 2.0 5 votes vote down vote up
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, GlyphPage.renderContext, 0.5f, 0).x - unicodeFont.getPaddingLeft();
	int xAdvance = (int)(metrics.getAdvanceX() + unicodeFont.getPaddingAdvanceX() + unicodeFont.getPaddingLeft() + unicodeFont
		.getPaddingRight());
	return new int[] {xOffset, xAdvance};
}
 
Example 7
Source File: TextMeasureTests.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: TextMeasureTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: TextMeasureTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: TextMeasureTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: TextMeasureTests.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: TextMeasureTests.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: BMFontUtil.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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};
}
 
Example 14
Source File: TextMeasureTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: TextMeasureTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: Glyph.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: TextMeasureTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 18
Source File: TextMeasureTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 19
Source File: TextMeasureTests.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 20
Source File: FontTag.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
public static float getSystemFontAdvance(Font aFont, Character character, Character nextCharacter) {
    String chars = "" + character + (nextCharacter == null ? "" : nextCharacter);
    GlyphVector gv = aFont.layoutGlyphVector(new FontRenderContext(aFont.getTransform(), true, true), chars.toCharArray(), 0, chars.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    GlyphMetrics gm = gv.getGlyphMetrics(0);
    return gm.getAdvanceX();
}