use of java.awt.font.FontRenderContext in project OpenNotebook by jaltekruse.
the class TextObjectGUI method drawMathObject.
public void drawMathObject(TextObject object, Graphics g, Point pageOrigin, float zoomLevel) {
ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
if (!object.getText().equals("")) {
Font f = g.getFont();
String message = object.getText();
g.setFont(f.deriveFont(sap.getFontSize()));
g.setColor(Color.BLACK);
Graphics2D graphics2D = (Graphics2D) g;
GraphicsEnvironment.getLocalGraphicsEnvironment();
AttributedString messageAS = new AttributedString(message);
messageAS.addAttribute(TextAttribute.FONT, g.getFont());
AttributedCharacterIterator messageIterator = messageAS.getIterator();
FontRenderContext messageFRC = graphics2D.getFontRenderContext();
LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
Insets insets = new Insets(2, 2, 2, 2);
float wrappingWidth = sap.getWidth() - insets.left - insets.right;
float x = sap.getxOrigin() + insets.left;
float y = sap.getyOrigin() + insets.top;
try {
TextLayout textLayout;
while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
textLayout = messageLBM.nextLayout(wrappingWidth);
y += textLayout.getAscent();
if (object.getAlignment().equals(TextObject.LEFT)) {
textLayout.draw(graphics2D, x, y);
} else if (object.getAlignment().equals(TextObject.RIGHT)) {
textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()), y);
} else {
//centered
textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()) / 2, y);
}
y += textLayout.getDescent() + textLayout.getLeading();
x = sap.getxOrigin() + insets.left;
}
} catch (Exception e) {
// TODO - logging and error reporting to user
// System.out.println("error with text rendering");
}
object.setHeight((int) ((y - sap.getyOrigin()) / zoomLevel));
g.setFont(f);
} else {
// draw the black box around the text box if nothing is in it
g.setColor(Color.BLACK);
g.drawRect(sap.getxOrigin(), sap.getyOrigin(), (int) (object.getWidth() * zoomLevel), (int) (object.getHeight() * zoomLevel));
}
if (((BooleanAttribute) object.getAttributeWithName(TextObject.SHOW_BOX)).getValue()) {
g.setColor(Color.BLACK);
g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
}
use of java.awt.font.FontRenderContext in project processing by processing.
the class Toolkit method getMonoFontList.
// Gets the plain (not bold, not italic) version of each
private static List<Font> getMonoFontList() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();
List<Font> outgoing = new ArrayList<Font>();
// Using AffineTransform.getScaleInstance(100, 100) doesn't change sizes
FontRenderContext frc = new FontRenderContext(new AffineTransform(), Preferences.getBoolean("editor.antialias"), // use fractional metrics
true);
for (Font font : fonts) {
if (font.getStyle() == Font.PLAIN && font.canDisplay('i') && font.canDisplay('M') && font.canDisplay(' ') && font.canDisplay('.')) {
// The old method just returns 1 or 0, and using deriveFont(size)
// is overkill. It also causes deprecation warnings
// @SuppressWarnings("deprecation")
// FontMetrics fm = awtToolkit.getFontMetrics(font);
//FontMetrics fm = awtToolkit.getFontMetrics(font.deriveFont(24));
// System.out.println(fm.charWidth('i') + " " + fm.charWidth('M'));
// if (fm.charWidth('i') == fm.charWidth('M') &&
// fm.charWidth('M') == fm.charWidth(' ') &&
// fm.charWidth(' ') == fm.charWidth('.')) {
double w = font.getStringBounds(" ", frc).getWidth();
if (w == font.getStringBounds("i", frc).getWidth() && w == font.getStringBounds("M", frc).getWidth() && w == font.getStringBounds(".", frc).getWidth()) {
// //PApplet.printArray(font.getAvailableAttributes());
// Map<TextAttribute,?> attr = font.getAttributes();
// System.out.println(font.getFamily() + " > " + font.getName());
// System.out.println(font.getAttributes());
// System.out.println(" " + attr.get(TextAttribute.WEIGHT));
// System.out.println(" " + attr.get(TextAttribute.POSTURE));
outgoing.add(font);
// System.out.println(" good " + w);
}
}
}
return outgoing;
}
use of java.awt.font.FontRenderContext in project scriptographer by scriptographer.
the class GraphicContext method getFontRenderContext.
/**
* Get the rendering context of the <code>Font</code> within this
* <code>Graphics2D</code> context.
* The {@link FontRenderContext}
* encapsulates application hints such as anti-aliasing and
* fractional metrics, as well as target device specific information
* such as dots-per-inch. This information should be provided by the
* application when using objects that perform typographical
* formatting, such as <code>Font</code> and
* <code>TextLayout</code>. This information should also be provided
* by applications that perform their own layout and need accurate
* measurements of various characteristics of glyphs such as advance
* and line height when various rendering hints have been applied to
* the text rendering.
*
* @return a reference to an instance of FontRenderContext.
* @see java.awt.font.FontRenderContext
* @see java.awt.Font#createGlyphVector(FontRenderContext,char[])
* @see java.awt.font.TextLayout
* @since JDK1.2
*/
public FontRenderContext getFontRenderContext() {
//
// Find if antialiasing should be used.
//
Object antialiasingHint = hints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
boolean isAntialiased = true;
if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_ON && antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT) {
// hint.
if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) {
antialiasingHint = hints.get(RenderingHints.KEY_ANTIALIASING);
// Test general hint
if (antialiasingHint != RenderingHints.VALUE_ANTIALIAS_ON && antialiasingHint != RenderingHints.VALUE_ANTIALIAS_DEFAULT) {
// off explicitly, use it.
if (antialiasingHint == RenderingHints.VALUE_ANTIALIAS_OFF)
isAntialiased = false;
}
} else
isAntialiased = false;
}
//
// Find out whether fractional metrics should be used.
//
boolean useFractionalMetrics = true;
if (hints.get(RenderingHints.KEY_FRACTIONALMETRICS) == RenderingHints.VALUE_FRACTIONALMETRICS_OFF)
useFractionalMetrics = false;
FontRenderContext frc = new FontRenderContext(defaultTransform, isAntialiased, useFractionalMetrics);
return frc;
}
use of java.awt.font.FontRenderContext in project limelight by slagyr.
the class PropPanelTest method hasChangesWhenTextIsChanged.
@Test
public void hasChangesWhenTextIsChanged() throws Exception {
TextPanel.staticFontRenderingContext = new FontRenderContext(new AffineTransform(), true, true);
Layouts.on(panel, panel.getDefaultLayout());
panel.resetNeededLayout();
panel.setText("blah");
assertEquals(true, panel.needsLayout());
panel.resetNeededLayout();
panel.setText("blah");
assertEquals(false, panel.needsLayout());
panel.setText("new text");
assertEquals(true, panel.needsLayout());
}
use of java.awt.font.FontRenderContext in project playn by threerings.
the class JavaTextLayout method layoutText.
public static JavaTextLayout[] layoutText(JavaGraphics gfx, String text, TextFormat format, TextWrap wrap) {
// normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF)
text = normalizeEOL(text);
// we do some fiddling to work around the fact that TextLayout chokes on the empty string
String ltext = text.length() == 0 ? " " : text;
// set up an attributed character iterator so that we can measure the text
AttributedString astring = new AttributedString(ltext);
if (format.font != null) {
astring.addAttribute(TextAttribute.FONT, ((JavaFont) format.font).jfont);
}
List<JavaTextLayout> layouts = new ArrayList<JavaTextLayout>();
FontRenderContext frc = format.antialias ? gfx.aaFontContext : gfx.aFontContext;
LineBreakMeasurer measurer = new LineBreakMeasurer(astring.getIterator(), frc);
int lastPos = ltext.length(), curPos = 0;
char eol = '\n';
while (curPos < lastPos) {
int nextRet = ltext.indexOf(eol, measurer.getPosition() + 1);
if (nextRet == -1) {
nextRet = lastPos;
}
TextLayout layout = measurer.nextLayout(wrap.width, nextRet, false);
int endPos = measurer.getPosition();
while (curPos < endPos && ltext.charAt(curPos) == eol) // skip over EOLs
curPos += 1;
layouts.add(new JavaTextLayout(ltext.substring(curPos, endPos), format, layout));
curPos = endPos;
}
return layouts.toArray(new JavaTextLayout[layouts.size()]);
}
Aggregations