Set text to subscript and superscript via the Scribus API

Previous topic - Next topic

carlosgg

Hello, how can I set characters in a text frame to subscript or superscript via the Scribus API? The only thing I can figure out is to create a text frame for the subscript/superscript and moving it a little up or down relative to the main text frame and doing it that way. I am using Scribus 1.4.8. Thank you.

a.l.e

in scribus 1.5 you should be able to do that with character styles.

scribus 1.4 is in maintenance mode so new features will (normally) be added.
if it's not there, you should think about upgrading to 1.5, even if it's the development branch.

RobSay

I had exactly the same question - and it is possible, if a little finicky - so adding this for anyone else looking in the future. You need Scribus 1.5.6 to use the setCharacterStyles method

Given a text frame with some content - you need to:
- get all the text
- find the position of the text you want to be in superscript
- select the text
- set the style to a character style that has superscript
- move to the next one ..

A bit of brute force in this method - but it worked for me:

# Text markers that can be used to identify where to apply superscript
# ORDINALS = ["0th", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th"]
# Character style already defined in my document
# additionalNotesSuperscriptCharStyle = "Additional Note Superscript"
# notesFrame is the selected Text Frame

    for superscript in ORDINALS :
        # find the ordinal text in the content of the frame - don't have to care about unicode any more
        superscriptPosition = getAllText(notesFrame).find(superscript)

        while (superscriptPosition > -1) :
            superscriptPosition += 1 # increment - so the number is not selected
            selectText(superscriptPosition, 2, notesFrame) # all of the ordinals are 2 characters, use calculated length if needed for others
            setCharacterStyle(additionalNotesSuperscriptCharStyle, notesFrame) # character style must exist
            selectText(0,0,notesFrame) # deselect the text just styled
            superscriptPosition = getAllText(notesFrame).find(superscript, superscriptPosition) # find the next ordinal from the current position


Lots of ways to improve that, you could probably use a regex. You could also optionally dynamically create the character style based on the selected text - this is needed if the frame contains multiple paragraph or character styles.

cheers

Rob