Applying styles to parts of text included in a textbox

Previous topic - Next topic

Rummidge

Hi all,

Is it any possible at all? API seems to say it is.

I am still in trouble with my problem applying styles. What I have been trying to do is the following:

[Scribus 1.4.5, scripting with Python]

1) Extracting the current raw text from an already formatted textbox. This textbox in fact has two lines, each one with a different style. So, they are separated by a newline character.

2) Those styles have been previously defined via F3-Styles as paragraph styles. Let's call them 'styleName 1' and 'styleName 2'.

3) I want to programatically substitute each of those lines with new text, preserving the format. For this purpose, I do the following:

Let's also assume 'textBoxName' is a valid textbox id.

# select and clean previous text content
scribus.selectObject('textBoxName')
scribus.deleteText()


I don't care of what the textbox contained.

# tracking the text insertion position
cursorPosition = 0
firstLineText = 'First Line Text'
secondLineText = 'Second Line Text'


This is just an initialization. Here it comes the interesting part:

# insert firstline, newline, secondline
scribus.insertText(firstLineText , cursorPosition)
cursorPosition = cursorPosition + len(firstLineText)
scribus.insertText('\n',cursorPosition)
cursorPosition = cursorPosition + 1
scribus.insertText(secondLineText , cursorPosition)


The result after the insertion is perfect. Two nice lines, one after the other, with standard formatting. Problem comes now.

# select firstline only and apply style
scribus.selectText(1,len(firstLineText))
style = 'styleName 1'
scribus.setStyle(style)


The paragraph style is applied to both lines, first and second: the full textbox text is applied this style.
In fact, it is acting like a frame style, a concept that apparently doesn't exist in this 1.4.5 version.

If I complete the action, applying the second line style to the second line text, things don't change a lot, as the new style is applied to the full textbox again, overwriting the previous style definition.

The API doc says:

QuoteFUNCTION:
selectText

SYNTAX:
selectText(start, count, ["name"])

Selects "count" characters of text in the text frame "name" starting from the character "start".
Character counting starts at 0.
If "count" is zero, any text selection will be cleared. 
If "name" is not given the currently selected item is used.
May throw IndexError if the selection is outside the bounds of the text.

and

QuoteFUNCTION:
setStyle

SYNTAX:
setStyle("style" [, "name"])

Apply the named "style" to the object named "name".
If is no object name given, it's applied on the selected object.

I assumed from it that invoking setStyle without a 'name', it was going to apply the style just to the selected part of the text inside the textbox, not the full textbox. In fact, it is acting like a textbox style, not like a paragraph style.

So:

1) either the API doesn't work (should I file a bug?) or
2) it is not well documented, or
3) I am confused and missing the very best and only way for doing this thing well.  :D (this I'd prefer)

Has anyone experienced something similar? Any way for avoiding it?

michaelu

setStyle sets a paragraph style. Use setCharacterStyle for parts of the text. Maybe this was not yet available in 2015.

michaelu

get/setFontFeatures are back in 1.5.6.svn. I called getFontFeatures for each character of some text, and most often it returned an empty string, sometimes "-clig,-liga". This tells me not much, though...