use of java.awt.font.FontRenderContext in project smile by haifengl.
the class Graphics method drawTextBaseRatio.
/**
* Draw a string with given reference point and rotation angle. (0.5, 0.5)
* is center, (0, 0) is lower left, (1, 0) is upper left, etc.
* The angle of rotation is in radians. The logical are proportional to
* the base coordinates.
*/
public void drawTextBaseRatio(String label, double horizontalReference, double verticalReference, double rotation, double[] coord) {
int[] sc = projection.screenProjectionBaseRatio(coord);
int x = sc[0];
int y = sc[1];
AffineTransform transform = g2d.getTransform();
// Corner offset adjustment : Text Offset is used Here
FontRenderContext frc = g2d.getFontRenderContext();
Font font = g2d.getFont();
double w = font.getStringBounds(label, frc).getWidth();
double h = font.getSize2D();
if (rotation != 0) {
g2d.rotate(rotation, x, y);
}
x -= (int) (w * horizontalReference);
y += (int) (h * verticalReference);
g2d.drawString(label, x, y);
g2d.setTransform(transform);
}
use of java.awt.font.FontRenderContext in project OpenNotebook by jaltekruse.
the class AnswerBoxGUI method drawMathObject.
public void drawMathObject(AnswerBoxObject object, Graphics g, Point pageOrigin, float zoomLevel) {
ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
// TODO - decide how extra whitespace should be handled, should it always be stored?
// students may use it to format a multi-line answer
// although useful whitespace will likely not coming at the very beginning or very end
// of an answer
List<? extends MathObjectAttribute> correctAnswers = object.getListWithName(AnswerBoxObject.CORRECT_ANSWERS).getValues();
if (!object.getStudentAnswer().trim().equals("") || !correctAnswers.isEmpty()) {
Font f = g.getFont();
g.setColor(new Color(150, 210, 255));
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
String message = object.getStudentAnswer();
for (MathObjectAttribute mAtt : correctAnswers) {
message += mAtt.getValue().toString() + ";";
}
message = message.substring(0, message.length() - 1);
if (message.isEmpty()) {
// cannot have an empty string in AttributedString
message = " ";
}
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;
while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
y += textLayout.getAscent();
textLayout.draw(graphics2D, x, y);
y += textLayout.getDescent() + textLayout.getLeading();
x = sap.getxOrigin() + insets.left;
}
g.setFont(f);
} else {
g.setColor(new Color(230, 230, 230));
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
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 CompositionTextManager method getTextLayout.
private TextLayout getTextLayout(AttributedCharacterIterator text, int committedCount) {
boolean antialias = Preferences.getBoolean("editor.smooth");
TextAreaPainter painter = textArea.getPainter();
// create attributed string with font info.
AttributedString composed = new AttributedString(text, committedCount, text.getEndIndex());
Font font = painter.getFontMetrics().getFont();
composed.addAttribute(TextAttribute.FONT, font);
// set hint of antialiasing to render target.
Graphics2D g2d = (Graphics2D) painter.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
FontRenderContext frc = g2d.getFontRenderContext();
Messages.log("debug: FontRenderContext is Antialiased = " + frc.getAntiAliasingHint());
return new TextLayout(composed.getIterator(), frc);
}
use of java.awt.font.FontRenderContext in project processing by processing.
the class InputMethodSupport method getTextLayout.
private TextLayout getTextLayout(AttributedCharacterIterator text, int committedCount) {
boolean antialias = Preferences.getBoolean("editor.smooth");
TextAreaPainter painter = textArea.getPainter();
// create attributed string with font info.
if (text.getEndIndex() - (text.getBeginIndex() + committedCount) > 0) {
composedTextString = new AttributedString(text, committedCount, text.getEndIndex(), CUSTOM_IM_ATTRIBUTES);
Font font = painter.getFontMetrics().getFont();
TextAreaDefaults defaults = textArea.getDefaults();
Color bgColor = defaults.lineHighlight ? defaults.lineHighlightColor : defaults.bgcolor;
composedTextString.addAttribute(TextAttribute.FONT, font);
composedTextString.addAttribute(TextAttribute.FOREGROUND, defaults.fgcolor);
composedTextString.addAttribute(TextAttribute.BACKGROUND, bgColor);
} else {
composedTextString = new AttributedString("");
return null;
}
// set hint of antialiasing to render target.
Graphics2D g2d = (Graphics2D) painter.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
FontRenderContext frc = g2d.getFontRenderContext();
if (Base.DEBUG) {
Messages.log("debug: FontRenderContext is Antialiased = " + frc.getAntiAliasingHint());
}
return new TextLayout(composedTextString.getIterator(), frc);
}
use of java.awt.font.FontRenderContext in project hid-serial by rayshobby.
the class StyledString method getLines.
/**
* Get the text layouts for display if the string has changed since last call
* to this method regenerate them.
*
* @param g2d Graphics2D display context
* @return a list of text layouts for rendering
*/
public LinkedList<TextLayoutInfo> getLines(Graphics2D g2d) {
if (font != g2d.getFont()) {
setFont(g2d.getFont());
invalidText = true;
}
if (invalidText) {
styledText = new AttributedString(plainText);
styledText = insertParagraphMarkers(plainText, styledText);
applyAttributes();
invalidText = false;
invalidLayout = true;
}
if (invalidLayout) {
linesInfo.clear();
if (plainText.length() > 0) {
textHeight = 0;
maxLineLength = 0;
maxLineHeight = 0;
nbrLines = 0;
AttributedCharacterIterator paragraph = styledText.getIterator(null, 0, plainText.length());
FontRenderContext frc = g2d.getFontRenderContext();
lineMeasurer = new LineBreakMeasurer(paragraph, frc);
float yposinpara = 0;
int charssofar = 0;
while (lineMeasurer.getPosition() < plainText.length()) {
TextLayout layout = lineMeasurer.nextLayout(wrapWidth);
float advance = layout.getVisibleAdvance();
if (justify) {
if (justify && advance > justifyRatio * wrapWidth) {
//System.out.println(layout.getVisibleAdvance() + " " + breakWidth + " "+ layout.get);
// If advance > breakWidth then we have a line break
float jw = (advance > wrapWidth) ? advance - wrapWidth : wrapWidth;
layout = layout.getJustifiedLayout(jw);
}
}
// Remember the longest and tallest value for a layout so far.
float lh = getHeight(layout);
if (lh > maxLineHeight)
maxLineHeight = lh;
textHeight += lh;
if (advance <= wrapWidth && advance > maxLineLength)
maxLineLength = advance;
// Store layout and line info
linesInfo.add(new TextLayoutInfo(nbrLines, layout, charssofar, layout.getCharacterCount(), yposinpara));
charssofar += layout.getCharacterCount();
yposinpara += lh;
nbrLines++;
}
}
invalidLayout = false;
}
return linesInfo;
}
Aggregations