use of java.awt.font.FontRenderContext in project poi by apache.
the class DrawTextParagraph method tab2space.
/**
* Replace a tab with the effective number of white spaces.
*/
private String tab2space(TextRun tr) {
AttributedString string = new AttributedString(" ");
String fontFamily = tr.getFontFamily();
if (fontFamily == null) {
fontFamily = "Lucida Sans";
}
string.addAttribute(TextAttribute.FAMILY, fontFamily);
Double fs = tr.getFontSize();
if (fs == null) {
fs = 12d;
}
string.addAttribute(TextAttribute.SIZE, fs.floatValue());
TextLayout l = new TextLayout(string.getIterator(), new FontRenderContext(null, true, true));
double wspace = l.getAdvance();
Double tabSz = paragraph.getDefaultTabSize();
if (tabSz == null) {
tabSz = wspace * 4;
}
int numSpaces = (int) Math.ceil(tabSz / wspace);
StringBuilder buf = new StringBuilder();
for (int i = 0; i < numSpaces; i++) {
buf.append(' ');
}
return buf.toString();
}
use of java.awt.font.FontRenderContext in project jgnash by ccavanaugh.
the class AWTFontUtilities method getFontRenderContext.
private static synchronized FontRenderContext getFontRenderContext() {
FontRenderContext context = contextReference.get();
if (context == null) {
context = new FontRenderContext(null, true, true);
contextReference = new SoftReference<>(context);
}
return context;
}
use of java.awt.font.FontRenderContext in project jgnash by ccavanaugh.
the class AWTFontUtilities method getStringWidth.
/**
* Calculates the width of the specified {@code String}.
*
* @param text the text to be weighted.
* @param font the font to be weighted
* @return the width of the given string in 1/72" dpi.
*/
private static int getStringWidth(final String text, final Font font) {
final FontRenderContext frc = getFontRenderContext();
// text is padded by one space
final Rectangle2D bounds = font.getStringBounds(text + " ", frc);
return (int) ceil(bounds.getWidth()) + 5;
}
use of java.awt.font.FontRenderContext in project chipKIT32-MAX by chipKIT32.
the class CompositionTextManager method getTextLayout.
private TextLayout getTextLayout(AttributedCharacterIterator text, int committed_count) {
AttributedString composed = new AttributedString(text, committed_count, text.getEndIndex());
Font font = textArea.getPainter().getFont();
FontRenderContext context = ((Graphics2D) (textArea.getPainter().getGraphics())).getFontRenderContext();
composed.addAttribute(TextAttribute.FONT, font);
TextLayout layout = new TextLayout(composed.getIterator(), context);
return layout;
}
use of java.awt.font.FontRenderContext in project android_frameworks_base by crdroidandroid.
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