use of java.text.AttributedString in project chipKIT32-MAX by chipKIT32.
the class CompositionTextManager method getCommittedText.
public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex) {
int length = endIndex - beginIndex;
String textAreaString = textArea.getText(beginIndex, length);
return new AttributedString(textAreaString).getIterator();
}
use of java.text.AttributedString 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.text.AttributedString in project binnavi by google.
the class ZyLineContent method regenerateLine.
private void regenerateLine(final String text, final Font font, final List<CStyleRunData> textColorStyleRun) {
m_text = Preconditions.checkNotNull(text, "Error: text argument can not be null");
Preconditions.checkNotNull(textColorStyleRun, "Error: textColorStyleRun argument can not be null");
m_atext = new AttributedString(text);
if (!isEmpty()) {
if (font != null) {
m_atext.addAttribute(TextAttribute.FONT, font);
}
// After the line is created we can process the accumulated style information.
for (final CStyleRunData data : textColorStyleRun) {
final int position = data.getStart();
final int realLength = calculateRealLength(position, data.getLength());
validatePartialLineArguments(position, realLength);
m_atext.addAttribute(TextAttribute.FOREGROUND, data.getColor(), position, position + realLength);
if (data.getLineObject() != null) {
m_lineObjects.add(data.getLineObject());
}
if (data.getObject() != null) {
setObject(position, realLength, data.getObject());
}
}
m_textLayout = new TextLayout(m_atext.getIterator(), m_fontContext);
}
if (font != null) {
updateCharBounds(font);
}
}
use of java.text.AttributedString in project robovm by robovm.
the class NativeDecimalFormat method formatToCharacterIterator.
public AttributedCharacterIterator formatToCharacterIterator(Object object) {
if (object == null) {
throw new NullPointerException("object == null");
}
if (!(object instanceof Number)) {
throw new IllegalArgumentException("object not a Number: " + object.getClass());
}
Number number = (Number) object;
FieldPositionIterator fpIter = new FieldPositionIterator();
String text;
if (number instanceof BigInteger || number instanceof BigDecimal) {
text = new String(formatDigitList(this.address, number.toString(), fpIter));
} else if (number instanceof Double || number instanceof Float) {
double dv = number.doubleValue();
text = new String(formatDouble(this.address, dv, fpIter));
} else {
long lv = number.longValue();
text = new String(formatLong(this.address, lv, fpIter));
}
AttributedString as = new AttributedString(text);
while (fpIter.next()) {
Format.Field field = fpIter.field();
as.addAttribute(field, field, fpIter.start(), fpIter.limit());
}
// return the CharacterIterator from AttributedString
return as.getIterator();
}
use of java.text.AttributedString 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);
}
Aggregations