Search in sources :

Example 1 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project poi by apache.

the class TestBugs method bug57796.

@Test
public void bug57796() throws IOException {
    HSLFSlideShow ppt = open("WithLinks.ppt");
    HSLFSlide slide = ppt.getSlides().get(0);
    HSLFTextShape shape = (HSLFTextShape) slide.getShapes().get(1);
    List<HSLFHyperlink> hlList = HSLFHyperlink.find(shape);
    HSLFHyperlink hlShape = hlList.get(0);
    HSLFTextRun r = shape.getTextParagraphs().get(1).getTextRuns().get(0);
    HSLFHyperlink hlRun = r.getHyperlink();
    assertEquals(hlRun.getId(), hlShape.getId());
    assertEquals(hlRun.getAddress(), hlShape.getAddress());
    assertEquals(hlRun.getLabel(), hlShape.getLabel());
    assertEquals(hlRun.getTypeEnum(), hlShape.getTypeEnum());
    assertEquals(hlRun.getStartIndex(), hlShape.getStartIndex());
    assertEquals(hlRun.getEndIndex(), hlShape.getEndIndex());
    OutputStream nullOutput = new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        }
    };
    final boolean[] found = { false };
    DummyGraphics2d dgfx = new DummyGraphics2d(new PrintStream(nullOutput)) {

        @Override
        public void drawString(AttributedCharacterIterator iterator, float x, float y) {
            // For the test file, common sl draws textruns one by one and not mixed
            // so we evaluate the whole iterator
            Map<Attribute, Object> attributes = null;
            StringBuffer sb = new StringBuffer();
            for (char c = iterator.first(); c != CharacterIterator.DONE; c = iterator.next()) {
                sb.append(c);
                attributes = iterator.getAttributes();
            }
            if ("Jakarta HSSF".equals(sb.toString())) {
                // this is a test for a manually modified ppt, for real hyperlink label
                // one would need to access the screen tip record
                String href = (String) attributes.get(DrawTextParagraph.HYPERLINK_HREF);
                String label = (String) attributes.get(DrawTextParagraph.HYPERLINK_LABEL);
                assertEquals("http://jakarta.apache.org/poi/hssf/", href);
                assertEquals("Open Jakarta POI HSSF module test  ", label);
                found[0] = true;
            }
        }
    };
    ppt.getSlides().get(1).draw(dgfx);
    assertTrue(found[0]);
    ppt.close();
}
Also used : PrintStream(java.io.PrintStream) Attribute(java.text.AttributedCharacterIterator.Attribute) OutputStream(java.io.OutputStream) AttributedCharacterIterator(java.text.AttributedCharacterIterator) DummyGraphics2d(org.apache.poi.hssf.usermodel.DummyGraphics2d) Test(org.junit.Test)

Example 2 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project robovm by robovm.

the class Support_Format method findFields.

/**
     * finds attributes with regards to char index in this
     * AttributedCharacterIterator, and puts them in a vector
     *
     * @param iterator
     * @return a vector, each entry in this vector are of type FieldContainer ,
     *         which stores start and end indexes and an attribute this range
     *         has
     */
protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
    Vector<FieldContainer> result = new Vector<FieldContainer>();
    while (iterator.getIndex() != iterator.getEndIndex()) {
        int start = iterator.getRunStart();
        int end = iterator.getRunLimit();
        Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
        while (it.hasNext()) {
            AttributedCharacterIterator.Attribute attribute = it.next();
            Object value = iterator.getAttribute(attribute);
            result.add(new FieldContainer(start, end, attribute, value));
        // System.out.println(start + " " + end + ": " + attribute + ",
        // " + value );
        // System.out.println("v.add(new FieldContainer(" + start +"," +
        // end +"," + attribute+ "," + value+ "));");
        }
        iterator.setIndex(end);
    }
    return result;
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) Attribute(java.text.AttributedCharacterIterator.Attribute) Vector(java.util.Vector) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 3 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project es6draft by anba.

the class DateTimeFormatConstructor method CreateDateTimeParts.

/**
     * CreateDateTimeParts(dateTimeFormat, x)
     * 
     * @param dateTimeFormat
     *            the date format object
     * @param date
     *            the date object
     * @return the formatted date-time object
     */
private static List<Map.Entry<String, String>> CreateDateTimeParts(DateTimeFormatObject dateTimeFormat, Date date) {
    ArrayList<Map.Entry<String, String>> parts = new ArrayList<>();
    DateFormat dateFormat = dateTimeFormat.getDateFormat();
    AttributedCharacterIterator iterator = dateFormat.formatToCharacterIterator(date);
    StringBuilder sb = new StringBuilder();
    for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
        sb.append(ch);
        if (iterator.getIndex() + 1 == iterator.getRunLimit()) {
            Iterator<Attribute> keyIterator = iterator.getAttributes().keySet().iterator();
            String key;
            if (keyIterator.hasNext()) {
                key = fieldToString((DateFormat.Field) keyIterator.next());
            } else {
                key = "separator";
            }
            String value = sb.toString();
            sb.setLength(0);
            parts.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
        }
    }
    return parts;
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) ArrayList(java.util.ArrayList) AttributedCharacterIterator(java.text.AttributedCharacterIterator) AbstractMap(java.util.AbstractMap) DateField(com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.DateField) DateFormat(com.ibm.icu.text.DateFormat) SimpleDateFormat(com.ibm.icu.text.SimpleDateFormat)

Example 4 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project jdk8u_jdk by JetBrains.

the class StyledParagraph method addInputMethodAttrs.

/**
     * Return a Map with entries from oldStyles, as well as input
     * method entries, if any.
     */
static Map<? extends Attribute, ?> addInputMethodAttrs(Map<? extends Attribute, ?> oldStyles) {
    Object value = oldStyles.get(TextAttribute.INPUT_METHOD_HIGHLIGHT);
    try {
        if (value != null) {
            if (value instanceof Annotation) {
                value = ((Annotation) value).getValue();
            }
            InputMethodHighlight hl;
            hl = (InputMethodHighlight) value;
            Map<? extends Attribute, ?> imStyles = null;
            try {
                imStyles = hl.getStyle();
            } catch (NoSuchMethodError e) {
            }
            if (imStyles == null) {
                Toolkit tk = Toolkit.getDefaultToolkit();
                imStyles = tk.mapInputMethodHighlight(hl);
            }
            if (imStyles != null) {
                HashMap<Attribute, Object> newStyles = new HashMap<>(5, (float) 0.9);
                newStyles.putAll(oldStyles);
                newStyles.putAll(imStyles);
                return newStyles;
            }
        }
    } catch (ClassCastException e) {
    }
    return oldStyles;
}
Also used : HashMap(java.util.HashMap) Attribute(java.text.AttributedCharacterIterator.Attribute) Toolkit(java.awt.Toolkit) InputMethodHighlight(java.awt.im.InputMethodHighlight) Annotation(java.text.Annotation)

Example 5 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project Purus-Pasta by puruscor.

the class RichText method main.

public static void main(String[] args) throws Exception {
    String cmd = args[0].intern();
    if (cmd == "render") {
        Map<Attribute, Object> a = new HashMap<Attribute, Object>(std.defattrs);
        PosixArgs opt = PosixArgs.getopt(args, 1, "aw:f:s:");
        boolean aa = false;
        int width = 0;
        for (char c : opt.parsed()) {
            if (c == 'a') {
                aa = true;
            } else if (c == 'f') {
                a.put(TextAttribute.FAMILY, opt.arg);
            } else if (c == 'w') {
                width = Integer.parseInt(opt.arg);
            } else if (c == 's') {
                a.put(TextAttribute.SIZE, Integer.parseInt(opt.arg));
            }
        }
        Foundry fnd = new Foundry(a);
        fnd.aa = aa;
        RichText t = fnd.render(opt.rest[0], width);
        java.io.OutputStream out = new java.io.FileOutputStream(opt.rest[1]);
        javax.imageio.ImageIO.write(t.img, "PNG", out);
        out.close();
    } else if (cmd == "pagina") {
        PosixArgs opt = PosixArgs.getopt(args, 1, "aw:");
        boolean aa = false;
        int width = 0;
        for (char c : opt.parsed()) {
            if (c == 'a') {
                aa = true;
            } else if (c == 'w') {
                width = Integer.parseInt(opt.arg);
            }
        }
        Foundry fnd = new Foundry();
        fnd.aa = aa;
        Resource res = Resource.local().loadwait(opt.rest[0]);
        Resource.Pagina p = res.layer(Resource.pagina);
        if (p == null)
            throw (new Exception("No pagina in " + res + ", loaded from " + res.source));
        RichText t = fnd.render(p.text, width);
        java.io.OutputStream out = new java.io.FileOutputStream(opt.rest[1]);
        javax.imageio.ImageIO.write(t.img, "PNG", out);
        out.close();
    }
}
Also used : TextAttribute(java.awt.font.TextAttribute) Attribute(java.text.AttributedCharacterIterator.Attribute) HashMap(java.util.HashMap) AttributedString(java.text.AttributedString) IOException(java.io.IOException)

Aggregations

Attribute (java.text.AttributedCharacterIterator.Attribute)11 AttributedCharacterIterator (java.text.AttributedCharacterIterator)7 AbstractMap (java.util.AbstractMap)3 ArrayList (java.util.ArrayList)3 DateField (com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.DateField)2 DateFormat (com.ibm.icu.text.DateFormat)2 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Vector (java.util.Vector)2 NumberFormat (com.ibm.icu.text.NumberFormat)1 Toolkit (java.awt.Toolkit)1 TextAttribute (java.awt.font.TextAttribute)1 InputMethodHighlight (java.awt.im.InputMethodHighlight)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1