use of java.text.AttributedCharacterIterator 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.AttributedCharacterIterator 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);
}
use of java.text.AttributedCharacterIterator in project robovm by robovm.
the class OldAttributedStringTest method test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII.
public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII() {
AttributedString as = new AttributedString("test");
as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2, 3);
AttributedCharacterIterator it = as.getIterator();
assertEquals("non-null value limit", 2, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
as = new AttributedString("test");
as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, 2, 3);
it = as.getIterator();
assertEquals("null value limit", 4, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
try {
as = new AttributedString("test");
as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, -1, 3);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
}
// regression for Harmony-1244
as = new AttributedString("123", new WeakHashMap());
try {
as.addAttribute(null, new TreeSet(), 0, 1);
fail("should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
try {
as.addAttribute(null, new TreeSet(), -1, 1);
fail("should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
}
use of java.text.AttributedCharacterIterator in project robovm by robovm.
the class OldAttributedStringTest method test_getIterator$Ljava_text_AttributedCharacterIterator$AttributeII.
/**
* java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[],
* int, int) Test of method
* java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[],
* int, int).
*/
public void test_getIterator$Ljava_text_AttributedCharacterIterator$AttributeII() {
String test = "Test string";
try {
Map<AttributedCharacterIterator.Attribute, String> hm = new HashMap<AttributedCharacterIterator.Attribute, String>();
AttributedCharacterIterator.Attribute[] aci = new AttributedCharacterIterator.Attribute[3];
aci[0] = new TestAttributedCharacterIteratorAttribute("att1");
aci[1] = new TestAttributedCharacterIteratorAttribute("att2");
aci[2] = new TestAttributedCharacterIteratorAttribute("att3");
hm.put(aci[0], "value1");
hm.put(aci[1], "value2");
AttributedString attrString = new AttributedString(test);
attrString.addAttributes(hm, 2, 4);
AttributedCharacterIterator it = attrString.getIterator(aci, 1, 5);
assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[0]) == null);
assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[1]) == null);
assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[2]) == null);
it.next();
assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[0]).equals("value1"));
assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[1]).equals("value2"));
assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[2]) == null);
try {
attrString.getIterator(aci, -1, 5);
fail("IllegalArgumentException is not thrown.");
} catch (IllegalArgumentException iae) {
//expected
}
try {
attrString.getIterator(aci, 6, 5);
fail("IllegalArgumentException is not thrown.");
} catch (IllegalArgumentException iae) {
//expected
}
try {
attrString.getIterator(aci, 3, 2);
fail("IllegalArgumentException is not thrown.");
} catch (IllegalArgumentException iae) {
//expected
}
} catch (Exception e) {
fail("Unexpected exceptiption " + e.toString());
}
}
use of java.text.AttributedCharacterIterator in project robovm by robovm.
the class OldAttributedStringTest method assertEqualString.
static void assertEqualString(String msg, String expected, AttributedString attrString) {
AttributedCharacterIterator it = attrString.getIterator();
StringBuffer buf = new StringBuffer();
buf.append(it.first());
char ch;
while ((ch = it.next()) != CharacterIterator.DONE) buf.append(ch);
assertEquals(msg, expected, buf.toString());
}
Aggregations