Author Topic: Set text to subscript and superscript via the Scribus API  (Read 807 times)

carlosgg

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
  • Scribus User
Set text to subscript and superscript via the Scribus API
« on: July 18, 2020, 12:17:31 am »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2473
  • Karma: 36
    • the graphics lab
Re: Set text to subscript and superscript via the Scribus API
« Reply #1 on: July 18, 2020, 11:15:50 am »
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

  • Newbie
  • *
  • Posts: 33
  • Karma: 0
  • Scribus User
Re: Set text to subscript and superscript via the Scribus API
« Reply #2 on: February 16, 2021, 01:52:02 am »
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:
Code: [Select]
# 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

 

palmate