use of java.awt.font.GlyphVector in project libgdx by libgdx.
the class BMFontUtil method getGlyph.
/** @return May be null. */
private Glyph getGlyph(char c) {
char[] chars = { c };
GlyphVector vector = unicodeFont.getFont().layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
Rectangle bounds = vector.getGlyphPixelBounds(0, GlyphPage.renderContext, 0, 0);
return unicodeFont.getGlyph(vector.getGlyphCode(0), c, bounds, vector, 0);
}
use of java.awt.font.GlyphVector in project libgdx by libgdx.
the class UnicodeFont method initializeFont.
private void initializeFont(Font baseFont, int size, boolean bold, boolean italic) {
Map attributes = baseFont.getAttributes();
attributes.put(TextAttribute.SIZE, new Float(size));
attributes.put(TextAttribute.WEIGHT, bold ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR);
attributes.put(TextAttribute.POSTURE, italic ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR);
try {
attributes.put(TextAttribute.class.getDeclaredField("KERNING").get(null), TextAttribute.class.getDeclaredField("KERNING_ON").get(null));
} catch (Throwable ignored) {
}
font = baseFont.deriveFont(attributes);
metrics = GlyphPage.scratchGraphics.getFontMetrics(font);
ascent = metrics.getAscent();
descent = metrics.getDescent();
leading = metrics.getLeading();
// Determine width of space glyph (getGlyphPixelBounds gives a width of zero).
char[] chars = " ".toCharArray();
GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
spaceWidth = vector.getGlyphLogicalBounds(0).getBounds().width;
}
use of java.awt.font.GlyphVector in project libgdx by libgdx.
the class UnicodeFont method getWidth.
public int getWidth(String text) {
if (text == null)
throw new IllegalArgumentException("text cannot be null.");
if (text.length() == 0)
return 0;
char[] chars = text.toCharArray();
GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
int width = 0;
int extraX = 0;
boolean startNewLine = false;
for (int glyphIndex = 0, n = vector.getNumGlyphs(); glyphIndex < n; glyphIndex++) {
int charIndex = vector.getGlyphCharIndex(glyphIndex);
int codePoint = text.codePointAt(charIndex);
Rectangle bounds = getGlyphBounds(vector, glyphIndex, codePoint);
if (startNewLine && codePoint != '\n')
extraX = -bounds.x;
if (glyphIndex > 0)
extraX += paddingLeft + paddingRight + paddingAdvanceX;
width = Math.max(width, bounds.x + extraX + bounds.width);
if (codePoint == '\n')
startNewLine = true;
}
return width;
}
use of java.awt.font.GlyphVector in project libgdx by libgdx.
the class UnicodeFont method drawUnicode.
private void drawUnicode(String text, int startIndex, int endIndex) {
char[] chars = text.substring(0, endIndex).toCharArray();
GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
int maxWidth = 0, totalHeight = 0, lines = 0;
int extraX = 0, extraY = ascent;
boolean startNewLine = false;
Texture lastBind = null;
int offsetX = 0;
for (int glyphIndex = 0, n = vector.getNumGlyphs(); glyphIndex < n; glyphIndex++) {
int charIndex = vector.getGlyphCharIndex(glyphIndex);
if (charIndex < startIndex)
continue;
if (charIndex > endIndex)
break;
int codePoint = text.codePointAt(charIndex);
Rectangle bounds = getGlyphBounds(vector, glyphIndex, codePoint);
bounds.x += offsetX;
Glyph glyph = getGlyph(vector.getGlyphCode(glyphIndex), codePoint, bounds, vector, glyphIndex);
if (startNewLine && codePoint != '\n') {
extraX = -bounds.x;
startNewLine = false;
}
if (glyph.getTexture() == null && missingGlyph != null && glyph.isMissing())
glyph = missingGlyph;
if (glyph.getTexture() != null) {
// Draw glyph, only binding a new glyph page texture when necessary.
Texture texture = glyph.getTexture();
if (lastBind != null && lastBind != texture) {
GL11.glEnd();
lastBind = null;
}
if (lastBind == null) {
texture.bind();
GL11.glBegin(GL11.GL_QUADS);
lastBind = texture;
}
int glyphX = bounds.x + extraX;
int glyphY = bounds.y + extraY;
GL11.glTexCoord2f(glyph.getU(), glyph.getV());
GL11.glVertex3f(glyphX, glyphY, 0);
GL11.glTexCoord2f(glyph.getU(), glyph.getV2());
GL11.glVertex3f(glyphX, glyphY + glyph.getHeight(), 0);
GL11.glTexCoord2f(glyph.getU2(), glyph.getV2());
GL11.glVertex3f(glyphX + glyph.getWidth(), glyphY + glyph.getHeight(), 0);
GL11.glTexCoord2f(glyph.getU2(), glyph.getV());
GL11.glVertex3f(glyphX + glyph.getWidth(), glyphY, 0);
}
if (glyphIndex > 0)
extraX += paddingRight + paddingLeft + paddingAdvanceX;
maxWidth = Math.max(maxWidth, bounds.x + extraX + bounds.width);
totalHeight = Math.max(totalHeight, ascent + bounds.y + bounds.height);
if (codePoint == '\n') {
// Mac gives -1 for bounds.x of '\n', so use the bounds.x of the next glyph.
startNewLine = true;
extraY += getLineHeight();
lines++;
totalHeight = 0;
} else if (renderType == RenderType.Native)
offsetX += bounds.width;
}
if (lastBind != null)
GL11.glEnd();
}
use of java.awt.font.GlyphVector in project platform_frameworks_base by android.
the class BidiRenderer method render.
/**
* Renders the text to the right of the bounds with the given font.
* @param font The font to render the text with.
*/
private void render(int start, int limit, Font font, int flag, float[] advances, int advancesIndex, boolean draw) {
FontRenderContext frc;
if (mGraphics != null) {
frc = mGraphics.getFontRenderContext();
} else {
frc = Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
// Metrics obtained this way don't have anti-aliasing set. So,
// we create a new FontRenderContext with anti-aliasing set.
frc = new FontRenderContext(font.getTransform(), mPaint.isAntiAliased(), frc.usesFractionalMetrics());
}
GlyphVector gv = font.layoutGlyphVector(frc, mText, start, limit, flag);
int ng = gv.getNumGlyphs();
int[] ci = gv.getGlyphCharIndices(0, ng, null);
if (advances != null) {
for (int i = 0; i < ng; i++) {
int adv_idx = advancesIndex + ci[i];
advances[adv_idx] += gv.getGlyphMetrics(i).getAdvanceX();
}
}
if (draw && mGraphics != null) {
mGraphics.drawGlyphVector(gv, mBounds.right, mBaseline);
}
// Update the bounds.
Rectangle2D awtBounds = gv.getLogicalBounds();
RectF bounds = awtRectToAndroidRect(awtBounds, mBounds.right, mBaseline);
// coordinates from the bounds as an offset.
if (Math.abs(mBounds.right - mBounds.left) == 0) {
mBounds = bounds;
} else {
mBounds.union(bounds);
}
}
Aggregations