use of java.text.AttributedString 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.text.AttributedString in project hid-serial by rayshobby.
the class StyledString method readObject.
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
// Recreate transient elements
spacer = getParagraghSpacer(wrapWidth);
styledText = new AttributedString(plainText);
styledText = insertParagraphMarkers(plainText, styledText);
linesInfo = new LinkedList<TextLayoutInfo>();
applyAttributes();
}
use of java.text.AttributedString in project hid-serial by rayshobby.
the class StyledString method convertToSingleLineText.
/**
* Converts this StyledString from multi-line to single-line by replacing all EOL
* characters with the space character
* for paragraphs
* @param ptext
* @param as
* @return the converted string
*/
StyledString convertToSingleLineText() {
// Make sure we have something to work with.
if (styledText == null || plainText == null) {
plainText = "";
styledText = new AttributedString(plainText);
} else {
// Scan through plain text and for each EOL replace the paragraph spacer from
// the attributed string (styledText).
int fromIndex = plainText.indexOf('\n', 0);
if (fromIndex >= 0) {
while (fromIndex >= 0) {
try {
// if text == "\n" then an exception is thrown
styledText.addAttribute(TextAttribute.CHAR_REPLACEMENT, ' ', fromIndex, fromIndex + 1);
fromIndex = plainText.indexOf('\n', fromIndex + 1);
} catch (Exception excp) {
break;
}
}
// Finally replace all EOL in the plainText
plainText = plainText.replace('\n', ' ');
}
}
wrapWidth = Integer.MAX_VALUE;
return this;
}
use of java.text.AttributedString in project robovm by robovm.
the class OldAttributedCharacterIteratorTest method test_getRunStartLAttribute.
public void test_getRunStartLAttribute() {
assertEquals(0, it.getRunStart(AttributedCharacterIterator.Attribute.LANGUAGE));
AttributedString as = new AttributedString("test text");
as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "GERMAN", 2, 5);
as.addAttribute(AttributedCharacterIterator.Attribute.READING, "READ", 2, 7);
assertEquals(0, as.getIterator().getRunStart(AttributedCharacterIterator.Attribute.LANGUAGE));
assertEquals(0, as.getIterator().getRunStart(AttributedCharacterIterator.Attribute.READING));
}
use of java.text.AttributedString in project robovm by robovm.
the class OldAttributedStringTest method test_ConstructorLAttributedCharacterIterator_3.
public void test_ConstructorLAttributedCharacterIterator_3() {
String testString = "Test string";
AttributedString attrString = new AttributedString(testString);
AttributedCharacterIterator iter = attrString.getIterator();
AttributedString attrString2;
attrString2 = new AttributedString(iter, 2, 7, new AttributedCharacterIterator.Attribute[] {});
assertEqualString("String must match!", "st st", attrString2);
attrString2 = new AttributedString(iter, 2, 7, null);
assertEqualString("String must match!", "st st", attrString2);
}
Aggregations