Text Style Code in TextMorph

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Text Style Code in TextMorph

Patrick R.

Hi everyone,


I have just started investigating a bug with the PluggableTextMorphPlus where a TextStyle affecting a complete Text remained valid after setting a String instead. During debugging I have encountered the following code in TextMorph>>#newContents:


(text notNil and: [ (textSize := text size) > 0] and: [ (text runLengthFor: 1) = textSize ]) 
    ifTrue: [ | attribs |
attribs := text attributesAt: 1 forStyle: textStyle.
Text string: stringOrText copy attributes: attribs.]
    ifFalse: [ Text fromString: stringOrText copy ]

What it does is that if the previous style applied to all of the Text, then the new Text also gets this style applied. Why is that? That seems pretty weird to me to only handle this one special case. Does anyone know the rational behind that?

Thanks and bests

Patrick



Reply | Threaded
Open this post in threaded view
|

Re: Text Style Code in TextMorph

marcel.taeumel
Patrick R. wrote
Hi everyone,


I have just started investigating a bug with the PluggableTextMorphPlus where a TextStyle affecting a complete Text remained valid after setting a String instead. During debugging I have encountered the following code in TextMorph>>#newContents:


(text notNil and: [ (textSize := text size) > 0] and: [ (text runLengthFor: 1) = textSize ])
    ifTrue: [ | attribs |
attribs := text attributesAt: 1 forStyle: textStyle.
Text string: stringOrText copy attributes: attribs.]
    ifFalse: [ Text fromString: stringOrText copy ]

What it does is that if the previous style applied to all of the Text, then the new Text also gets this style applied. Why is that? That seems pretty weird to me to only handle this one special case. Does anyone know the rational behind that?

Thanks and bests

Patrick
Hi Patrick,

this seems to be an unexpected side-effect of #newContents:. Please remove it.

Note that the TextStyle is only needed for TextFontChange text attributes. What it actually does here is that it copies the current text attributes from the first character and applies it to the new contents. Do not confuse "text style" with "text attribute". :-)

IMO: If some application wants to set the text programmatically and keep the current formatting, that application should be responsible for retrieving the current formatting and applying it to the new contents. We have Text >> #attributesAt: and Text >> #addAttribute: for that.

Best,
Marcel