Posted by
Denis Kudriashov on
Jan 13, 2013; 7:54pm
URL: https://forum.world.st/TxTextMorph-based-on-new-text-model-tp4663219.html
Hello.
I upload new version of TxText with new TxTextMorph which can show TxTextModel (
http://www.smalltalkhub.com/#!/~sig/TxText ). See class side examples.
Implementation details:TxTextMorph has textLayout instance variable which contains full precomputed information to draw text on screen. TxTextLayout contains collection of TxTextLine. TxTextLine contains collection of span objects (from text model) and offset with extent required for drawing lines (with right align for example).
There are TxTextLayoutStrategy hierarchy responsible for building specific text layout objects:
- TxSimpleTextLayoutStrategy build layout with actual text lines which text model contains
- TxWrapTextStrategy build layout with lines which satisfied preferredExtent (given from text morph bounds)
When text morph accepts new text model It asks layoutStrategy to build new layout. Then align selector applyed to layout. So text can be placed left to right, centered or right to left.
Morph drawing method very simple:
TxTextMorph>>drawOn: aCanvas
textLayout drawTextOn: aCanvas at: self position
TxTextLayout>>drawTextOn: aCanvas at: aPoint
| topOffset linePosition |
topOffset := 0.
lines do: [:eachLine |
linePosition := aPoint x @ (aPoint y + topOffset).
eachLine drawOn: aCanvas at: linePosition.
topOffset := topOffset + eachLine height.
]
TxTextLine>>drawOn: aCanvas at: aPoint
spans do: [:each |
each drawOn: aCanvas at: aPoint + offset
]
At the end TxCharactersSpan draw contents on canvas:
TxCharactersSpan >>drawOn: aCanvas at: aPoint
aCanvas
drawString: characters
at: aPoint
font: (self getAttribute: TxFontAttribute)
color: (self getAttribute: TxColorAttribute)
And with Athens api text attributes can be not hardcoded:
TxCharactersSpan >>drawAthensOn: aCanvas at: aPoint
attributes applyOn: aCanvas.
aCanvas pathTransform translateX: aPoint x Y: aPoint y.
aCanvas drawString: characters
Questions:Now text alignment (like centering) is property of TxTextMorph. So it global property of full text layout.
But how such attribute can be applied to specific line?
TxModel is list of spans. And each span object contains characters with same text attributes like font or color. Text alignment can be such attribute too. Do you think it should be attribute of all spans from single line? Or it should be only attribute of TxLineSeparator span? What your idea?
Future:Next plan: cursor navigation, text selection and editing features. And of course tests, sorry I not start with tests now.
I wait your comments , ideas and questions. Maybe you don't like my approach. I want to know why.
I really want clean and object solution for text stuff in pharo.