Java Code Examples for java.awt.Font#layoutGlyphVector()

The following examples show how to use java.awt.Font#layoutGlyphVector() . These examples are extracted from open source projects. 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 Project: swcv   File: FontTests.java    License: MIT License 6 votes vote down vote up
@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g.setColor(Color.black);

    float x = 100;
    float y = 100;
    Font f = FontUtils.getFont();
    g.setFont(f);

    FontRenderContext frc = g2.getFontRenderContext();

    GlyphVector gv = f.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    Rectangle2D bb = gv.getPixelBounds(frc, x, y);
    
    
    double scale = 2.91;
    scale = 1.3461;
    scale = 2;
    bb.setRect(bb.getX() - 250.5, bb.getY() + 150.566, bb.getWidth()*scale, bb.getHeight()*scale);

    drawTextInBox(g2, text, bb);
}
 
Example 2
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 3
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 4
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 5
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 6
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 7
Source Project: swcv   File: WordCloudRenderer.java    License: MIT License 5 votes vote down vote up
private void drawTextInBox(Graphics2D g2, String text, Color color, SWCRectangle positionOnScreen)
{
    Font font = FontUtils.getFont();
    FontRenderContext frc = g2.getFontRenderContext();

    //bounding box of the word
    GlyphVector gv2 = font.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    Rectangle2D bb = gv2.getPixelBounds(frc, 0, 0);

    //find correct font size
    double scaleX = positionOnScreen.getWidth() / bb.getWidth();
    double scaleY = positionOnScreen.getHeight() / bb.getHeight();

    //get a new position for the text
    double x = positionOnScreen.getX() - bb.getX() * scaleX;
    double y = positionOnScreen.getY() - bb.getY() * scaleY;

    //preparing font
    AffineTransform at = new AffineTransform(scaleX, 0, 0, scaleY, 0, 0);
    Font deriveFont = font.deriveFont(at);
    g2.setFont(deriveFont);
    g2.setColor(color);

    //draw the label
    //GlyphVector gv = deriveFont.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    //g2.drawGlyphVector(gv, (float)x, (float)y);
    g2.drawString(text, (float)x, (float)y);
}
 
Example 8
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 9
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 10
public static void main(String[] args) {
    Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12);
    FontRenderContext frc = new FontRenderContext(null, false, false);
    GlyphVector gv = font.layoutGlyphVector(frc, "abc".toCharArray(), 1, 3,
                                            Font.LAYOUT_LEFT_TO_RIGHT);
    int idx0 = gv.getGlyphCharIndex(0);
    if (idx0 != 0) {
       throw new RuntimeException("Expected 0, got " + idx0);
    }
}
 
Example 11
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 12
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example 13
Source Project: ph-commons   File: FontKerningFuncTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testKerning () throws IOException
{
  if (EOperatingSystem.getCurrentOS ().isWindowsBased ())
  {
    // required to get graphics up and running...
    GraphicsEnvironment.getLocalGraphicsEnvironment ();
    final int nFontSize = 25;

    final ICommonsMap <TextAttribute, Object> aTextAttributes = new CommonsHashMap <> ();
    aTextAttributes.put (TextAttribute.FAMILY, "Arial");
    aTextAttributes.put (TextAttribute.SIZE, Float.valueOf (nFontSize));
    final Font aFont = Font.getFont (aTextAttributes);

    final char [] aChars = "T,".toCharArray ();
    final GlyphVector aGlyphVector = aFont.layoutGlyphVector (new FontRenderContext (new AffineTransform (),
                                                                                     false,
                                                                                     true),
                                                              aChars,
                                                              0,
                                                              aChars.length,
                                                              Font.LAYOUT_LEFT_TO_RIGHT);
    final int tCode = aGlyphVector.getGlyphCode (0);
    final int commaCode = aGlyphVector.getGlyphCode (1);
    final Kerning aKerning = new Kerning (new FileInputStream (System.getenv ("windir") + "/fonts/ARIAL.TTF"));
    LOGGER.info (Float.toString (aKerning.getValue (tCode, commaCode, nFontSize)));
  }
  else
    LOGGER.warn ("Works only on Windows!");
}
 
Example 14
Source Project: swcv   File: FontTests.java    License: MIT License 5 votes vote down vote up
private void drawTextInBox(Graphics2D g2, String text, Rectangle2D positionOnScreen)
{
    Font font = FontUtils.getFont();
    FontRenderContext frc = g2.getFontRenderContext();

    //bounding box of the word
    GlyphVector gv2 = font.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    Rectangle bb = gv2.getPixelBounds(frc, 0.0f, 0.0f);

    //find correct font size
    float scaleX = (float)(positionOnScreen.getWidth() / bb.getWidth());
    float scaleY = (float)(positionOnScreen.getHeight() / bb.getHeight());

    //get a new position for the text
    float x = (float)(positionOnScreen.getX() - bb.getX() * scaleX);
    float y = (float)(positionOnScreen.getY() - bb.getY() * scaleY);

    //preparing font
    AffineTransform at = new AffineTransform(scaleX, 0, 0, scaleY, 0, 0);
    Font deriveFont = font.deriveFont(at);
    g2.setFont(deriveFont);
    g2.setColor(Color.black);

    //draw the label
    GlyphVector gv = deriveFont.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    g2.drawGlyphVector(gv, x, y);
    g2.draw(positionOnScreen);
}
 
Example 15
private static List<KerningPair> getFontKerningPairsOneChar(List<Character> availableChars, Font font, char firstChar) {
    List<KerningPair> ret = new ArrayList<>();

    char[] chars = new char[availableChars.size() * 2];

    for (int i = 0; i < availableChars.size(); i++) {
        chars[i * 2] = firstChar;
        chars[i * 2 + 1] = availableChars.get(i);
    }

    Map<AttributedCharacterIterator.Attribute, Object> withKerningAttrs = new HashMap<>();

    withKerningAttrs.put(TextAttribute.FONT, font);
    withKerningAttrs.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font withKerningFont = Font.getFont(withKerningAttrs);
    GlyphVector withKerningVector = withKerningFont.layoutGlyphVector(getFontRenderContext(withKerningFont), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
    int[] withKerningX = new int[availableChars.size()];
    for (int i = 0; i < availableChars.size(); i++) {
        withKerningX[i] = withKerningVector.getGlyphLogicalBounds(i * 2 + 1).getBounds().x;
    }

    Map<AttributedCharacterIterator.Attribute, Object> noKerningAttrs = new HashMap<>();
    noKerningAttrs.put(TextAttribute.FONT, font);
    noKerningAttrs.put(TextAttribute.KERNING, 0);
    Font noKerningFont = Font.getFont(noKerningAttrs);
    GlyphVector noKerningVector = noKerningFont.layoutGlyphVector(getFontRenderContext(noKerningFont), chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
    for (int i = 0; i < availableChars.size(); i++) {
        int noKerningX = noKerningVector.getGlyphLogicalBounds(i * 2 + 1).getBounds().x;
        int kerning = withKerningX[i] - noKerningX;
        if (kerning > 0) {
            ret.add(new KerningPair(firstChar, availableChars.get(i), kerning));
        }
    }
    return ret;
}
 
Example 16
Source Project: gdx-skineditor   File: BMFontUtil.java    License: Apache License 2.0 4 votes vote down vote up
private int getGlyphCode (Font font, int codePoint) {
	char[] chars = Character.toChars(codePoint);
	GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
	return vector.getGlyphCode(0);
}
 
Example 17
private int getGlyphCode (Font font, int codePoint) {
	char[] chars = Character.toChars(codePoint);
	GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
	return vector.getGlyphCode(0);
}
 
Example 18
private static void runTest() {
    im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = im.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, W, H);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    char[] chs = "Sample Text.".toCharArray();
    int len = chs.length;

    int x = 50, y = 100;

    FontRenderContext frc = g2d.getFontRenderContext();
    Font plain = new Font("Serif", Font.PLAIN, 48);
    GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
    g2d.setFont(plain);
    g2d.drawChars(chs, 0, len, x, y); y +=50;

    g2d.drawGlyphVector(pgv, x, y); y += 50;
    Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
    Rectangle2D plainGVBounds = pgv.getLogicalBounds();
    Font bold = new Font("Serif", Font.BOLD, 48);
    GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
    Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
    Rectangle2D boldGVBounds = bgv.getLogicalBounds();
    g2d.setFont(bold);
    g2d.drawChars(chs, 0, len, x, y); y +=50;
    g2d.drawGlyphVector(bgv, x, y);
    System.out.println("Plain String Bounds = " + plainStrBounds);
    System.out.println("Bold String Bounds = " + boldStrBounds);
    System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
    System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
    if (!plainStrBounds.equals(boldStrBounds) &&
         plainGVBounds.equals(boldGVBounds))
    {
        System.out.println("Test failed: Plain GV bounds same as Bold");
        if (!interactive) {
            throw new RuntimeException("Plain GV bounds same as Bold");
        }
    }

}
 
Example 19
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();
}
 
Example 20
private static void runTest() {
    im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = im.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, W, H);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    char[] chs = "Sample Text.".toCharArray();
    int len = chs.length;

    int x = 50, y = 100;

    FontRenderContext frc = g2d.getFontRenderContext();
    Font plain = new Font("Serif", Font.PLAIN, 48);
    GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
    g2d.setFont(plain);
    g2d.drawChars(chs, 0, len, x, y); y +=50;

    g2d.drawGlyphVector(pgv, x, y); y += 50;
    Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
    Rectangle2D plainGVBounds = pgv.getLogicalBounds();
    Font bold = new Font("Serif", Font.BOLD, 48);
    GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
    Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
    Rectangle2D boldGVBounds = bgv.getLogicalBounds();
    g2d.setFont(bold);
    g2d.drawChars(chs, 0, len, x, y); y +=50;
    g2d.drawGlyphVector(bgv, x, y);
    System.out.println("Plain String Bounds = " + plainStrBounds);
    System.out.println("Bold String Bounds = " + boldStrBounds);
    System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
    System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
    if (!plainStrBounds.equals(boldStrBounds) &&
         plainGVBounds.equals(boldGVBounds))
    {
        System.out.println("Test failed: Plain GV bounds same as Bold");
        if (!interactive) {
            throw new RuntimeException("Plain GV bounds same as Bold");
        }
    }

}