Hello Georg,

you should better call the following method at the TextSelection:
tsel.applyStyle(stls.getStyle(SUBSCRIPT, OdfStyleFamily.Text));

There seems to be still a problem with automatic/hard styles being added
instead of using the template ones, but I am running out of time, sorry ;)

I added my complete test code, that you might want to insert for instance
into
org.odftoolkit.simple.text.ParagraphStyleHandlerTest for debugging
(Note: I ususally disable the other tests by adding @Ignore above their
method signature)
The output ODT will be added to the folder:
<odftoolkit20151009>/simple/target/test-classes

//Java Code - Start

    private static final String SUPERSCRIPT = "superscript";
    private static final String SUBSCRIPT = "subscript";

    private OdfOfficeStyles addDocumentStyles(TextDocument tdoc) {
        OdfOfficeStyles stls = tdoc.getOrCreateDocumentStyles();
        OdfStyle ostyle = stls.newStyle(SUBSCRIPT, OdfStyleFamily.Text);
        OdfStylePropertiesBase styleProp
            =
ostyle.getOrCreatePropertiesElement(OdfStylePropertiesSet.TextProperties);


styleProp.setOdfAttributeValue(OdfTextProperties.TextPosition.getName(),
            "sub 58%");

        ostyle = stls.newStyle(SUPERSCRIPT, OdfStyleFamily.Text);
        styleProp
            =
ostyle.getOrCreatePropertiesElement(OdfStylePropertiesSet.TextProperties);


styleProp.setOdfAttributeValue(OdfTextProperties.TextPosition.getName(),
            "super 58%");

        return stls;
    }

    @Test
    public void testSuperScript() {
        try {
            TextDocument targetDoc = TextDocument.newTextDocument();
            Section s1 = targetDoc.appendSection("S1");
            Paragraph p1 = s1.addParagraph("This is subscripted. This is
superscripted. ");
            OdfOfficeStyles stls = addDocumentStyles(targetDoc);

            TextSelection tsel;
            tsel = TextSelection.newTextSelection(null, "sub",
                p1.getOdfElement(), 8);
            tsel.applyStyle(stls.getStyle(SUBSCRIPT, OdfStyleFamily.Text));

            tsel = TextSelection.newTextSelection(null, "super",
                p1.getOdfElement(), 29);
            tsel.applyStyle(stls.getStyle(SUPERSCRIPT,
OdfStyleFamily.Text));

targetDoc.save(ResourceUtilities.newTestOutputFile("testSuperSubscript.odt"));
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            Assert.fail(e.getMessage());
        }
    }

//Java Code - End

Have a nice week-end,
Svante

On Fri, Oct 9, 2015 at 11:44 AM, Georg Füchsle <[email protected]>
wrote:

> In order to format a single character or word in continuous text I tried
> the following:
>
> - I created a global style "superscript"
>
>
> Javacode:
>     private static final String SUPERSCRIPT = "superscript";
>     private static final String SUBSCRIPT = "subscript";
>
>     public static void addDocumentStyles(TextDocument tdoc)
>     {
>     OdfOfficeStyles stls = tdoc.getOrCreateDocumentStyles();
>     OdfStyle ostyle = stls.newStyle(SUBSCRIPT, OdfStyleFamily.Text);
>     OdfStylePropertiesBase styleProp =
> ostyle.getOrCreatePropertiesElement(OdfStylePropertiesSet.TextProperties);
>
> styleProp.setOdfAttributeValue(OdfTextProperties.TextPosition.getName(),
> "sub 58%");
>
>     ostyle = stls.newStyle(SUPERSCRIPT, OdfStyleFamily.Text);
>     styleProp =
> ostyle.getOrCreatePropertiesElement(OdfStylePropertiesSet.TextProperties);
>
> styleProp.setOdfAttributeValue(OdfTextProperties.TextPosition.getName(),
> "super 58%");
>     }
>
> - second I created a Paragraph selected some text and set the style to
> "superscript":
>
>  Javacode:
>         Section s1 = s1 = target.appendSection("S1");
>         Paragraph p1 = s1.addParagraph("This is subscripted. This is
> superscripted. ");
>
>         TextSelection tsel;
>         Span tselSpan1;
>
>         tsel = TextSelection.newTextSelection(null, "sub",
> p1.getOdfElement(), 8);
>         tselSpan1 = Span.getInstanceof(tsel.createSpanElement());
>
>
> tselSpan1.getStyleHandler().getStyleElementForWrite().setStyleNameAttribute(SUBSCRIPT);
>
>         tsel = TextSelection.newTextSelection(null, "super",
> p1.getOdfElement(), 29);
>         tselSpan1 = Span.getInstanceof(tsel.createSpanElement());
>
>
> tselSpan1.getStyleHandler().getStyleElementForWrite().setStyleNameAttribute(SUPERSCRIPT);
>
>
>
>
> - My generated od-Document does not display super- and subscripted
> characters:
>
> I unzipped the document and saw in styles.xml the global styles:
>
> in styles.xml the my global styles are existent:
>
>     </office:styles>
>
>         ......
>         <style:style style:family="text" style:name="subscript">
>             <style:text-properties style:text-position="sub 58%"/>
>         </style:style>
>         <style:style style:family="text" style:name="superscript">
>             <style:text-properties style:text-position="super 58%"/>
>         </style:style>
>     </office:styles>
>
>
> I examined the content.xml:
> In content.xml these my global styles are written empty once more:
>
>     <office:automatic-styles>
>
>         .......
>         <style:style style:family="text" style:name="subscript"/>
>         <style:style style:family="text" style:name="superscript"/>
>     </office:automatic-styles>
>
>
> And the generated textspans reference 'wrong'  text-style-names:
>
> <text:section text:display="true" text:name="S1" text:style-name="a2921cb">
>                 <text:p>This is <text:span
> text:style-name="af1df46">sub</text:span>scripted. This is <text:span
> text:style-name="a4007fe">super</text:span>scripted. </text:p>
> </text:section>
>
>
> When I remove the generated empty styles in content.xml and correct the
> references of the text:span- styles. the characters are displayed in sub-
> and superscript.
>
> What do I wrong in the Javacode?
>
> Thanks in advance
>
> Gio
>

Reply via email to