Hi All,

We (HSLF developers) are searching for good object model to work with the 
character and paragraph styles.
It would be good to discuss a possible architecture and know your opinions,
before we settle. Working with text is a key feature of a PowerPoint API and 
this discussion won't harm. :))

A possible solution to handle character styles is to use 
java.text.AttributedString, a standard Java class
which is meant for working with format runs in Java2D. 

Use cases may look as follows:

(a) Create a text box and set character format:

org.apache.poi.hslf.model.TextRun txrun = ...; //HSLF object which represents a 
run of text in a powerpoint document

String text = "Hello, World";
txrun.settext(text)

AttributedString str = txrun.getCharacterStyle();

str.addAttribute(TextAttribute.FAMILY, "Arial"); //set the font family to the 
entire string
str.addAttribute(TextAttribute.SIZE, new Float(32), 0, 5); //set the font size 
to a a subrange
str.addAttribute(TextAttribute.FOREGROUND, Color.red, 3, 6);//set the font 
color to a a subrange


(b) For a text run read format runs or get attributes at the specified position:

org.apache.poi.hslf.model.TextRun txrun = ...;
String text = txrun.getText();
AttributedString str = txrun.getCharacterStyle();

        //iterate over format runs

        AttributedCharacterIterator it = str.getIterator();

        int index = 0;
        while (index < it.getEndIndex()) {
            it.setIndex(index);
            
            inr runLimit = it.getRunLimit();            
            Map attrs = it.getAttributes();
            String fragment = text.substring(index , runLimit);
            System.out.println("[" + fragment + "]: "+ attrs);

            index = runLimit;
        }

  //get format attributes at the specified position:

        it.setIndex(8);
        Map attr = it.getAttributes();
        System.out.println("attributes at position 8: " + attr);


 Advantages of this approach:

 - there are lots of docs on Java2D and people can use their previous experience
 or read recommended books to learn more how to use java.text.AttributedString.

 - This part of HSLF becomes compatible with Java graphics.
Users can use the AttributedString object to paint the string using 
java.awt.Graphics:

Graphics2D.drawString(AttributedCharacterIterator iterator, float x, float y) 
should output something similar to what you see in PowerPoint.

 - We don't need any special objects to handle stylings. (Currently we
 have RichTextRun object with getters and setters for font name, font
 size, bold/italic/underline, etc). All is done via the standard methods
 of java.text.AttributedString.

Drawbacks:
 - My main concern is that the exposing of AttributedString may not be 
user-friendly. 
Also I'm not sure if this approach will fit for working with paragraph styles.

I think that the API to work with paragraph styles should be symmetrical, i.e.
set paragraph attributes via AttributedString:

 AttributedString str = txrun.getParagraphStyle();
 //do paragraph styling


 So, I would like to hear you comments. Also, If anyone knows a good
 alternative to the suggested architecture - please share your
 knowledge.

Regards,
Yegor Kozlov


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/

Reply via email to