Skip to content

Commit 7cb6b19

Browse files
committed
update text chapter with measure strategy
1 parent 4dfcecb commit 7cb6b19

File tree

2 files changed

+116
-95
lines changed

2 files changed

+116
-95
lines changed

Chapters/bloc/.text.md.swp

44 KB
Binary file not shown.

Chapters/bloc/text.md

Lines changed: 116 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
Bloc comes with a full API to deal with text. Not only you can deal with
66
raw text, but you can apply styles as well.
77

8-
Before being displayed, you define your text as an instance of `BlRopedText`,
9-
which you can then style with **attributes** such as:
8+
Text is reprensented by abstract class *BlText* which serve a model for the UI
9+
component. Before being displayed, you define your text as an instance of
10+
`BlRopedText`, which you can then style with **attributes** such as:
1011

1112
- font name
1213
- font size
1314
- font foreground and background color
1415
- font style (normal, italic, bold).
1516

17+
Text can after be rendered as `BlTextElement`, which will take care of
18+
displaying properly the text with all properties defined on it.
19+
20+
Once you have a `BlTextElement`, all properties of `BlElement` apply.
21+
You can add your element to any existing `BlElement`, and integrate it easily in
22+
your graphical interface; it'll follow the same layout rules.
1623
```smalltalk
1724
labelText := 'hello from bloc' asRopedText
1825
foreground: Color orange;
@@ -26,21 +33,17 @@ label text: labelText.
2633
label openInNewSpace
2734
```
2835

29-
Another way to define attributes is to pass them as
30-
a collection:
36+
Another way to define attributes is to pass them as a collection:
3137

3238
```smalltalk
3339
BlTextElement new
3440
position: 5 @ 5;
3541
text: ('Rainbow!' asRopedText attributes:
3642
{ (BlTextForegroundAttribute paint: Color black)})
37-
3843
```
3944

4045
Take a look at `BlText` method for a full list of available text attributes.
4146

42-
Fonts are managed directly by Alexandrie. To get the list of available fonts,
43-
take a look at the result of `AeFontManager globalInstance familyNames`
4447

4548
text is like a collection, and you can apply different attributes to different
4649
part of your text:
@@ -69,14 +72,96 @@ part of your text:
6972

7073
![Multiple attributes.](figures/multipleTextAttributes.png width=60)
7174

75+
### BlText vs. BlTextElement
76+
77+
You have 2 different levels to manage text in Bloc.
78+
- `BlText` and its subclass `BlRopedText` create a text model where you can specify its attributes and style.
79+
- `BlTextElement` and its subclasses will properly display the text inside a Bloc element.
80+
81+
A small example. You can notice that `BlText` background is different from `BlTextElement` background.
82+
83+
```smalltalk
84+
| labelText label |
85+
labelText := 'hello from bloc' asRopedText
86+
background: Color orange ;
87+
fontSize: 75;
88+
fontName: 'Source Code Pro';
89+
italic;
90+
underline;
91+
underlineColor: Color red;
92+
vertical.
93+
94+
(labelText from: 1 to: 5) foreground: Color blue.
95+
(labelText from: 7 to: 11) foreground: Color white.
96+
(labelText from: 12 to: 15) foreground: Color red.
97+
98+
label := (BlTextElement text: labelText) position: 50 @ 10; background: Color yellow.
99+
```
100+
101+
you can define the style of your text through BlTextAttributesStyler
102+
103+
````smalltalk
104+
text := 'Hi John' asRopedText.
105+
106+
styler := BlTextAttributesStyler on: (text from: 3 to: 7).
107+
styler
108+
bold;
109+
italic;
110+
fontSize: 30;
111+
fontName: 'Roboto';
112+
monospace;
113+
foreground: Color green.
114+
styler style.
115+
```
116+
117+
or using a fluent API
118+
119+
````smalltalk
120+
text := 'Hi John' asRopedText.
121+
(text from: 3 to: 7) stylerDo: [ :aStyler | aStyler bold italic foreground: Color red ].
122+
````
123+
124+
As you may have noticed, this gives you a very fine-grained control over the style of your text.
125+
You also need to re-specify attributes when your text changes.
126+
If you want all your text to use the same attribute, you can then use `BlAttributedTextElement`.
127+
You can then change your text, `BlAttributedTextElement` will reuse its attributes.
128+
129+
130+
```smalltalk
131+
text := 'Hi John' asRopedText.
132+
133+
element := BlAttributedTextElement new.
134+
attributes := element attributesBuilder
135+
foreground: Color green;
136+
monospace;
137+
bold;
138+
italic;
139+
fontSize: 30;
140+
fontName: 'Roboto';
141+
monospace.
142+
143+
label := (element text: text)
144+
position: 50 @ 10;
145+
background: Color yellow;
146+
margin: (BlInsets all: 2);
147+
padding: (BlInsets all: 2);
148+
outskirts: BlOutskirts centered;
149+
border: (BlBorder paint: Color red width: 2).
150+
151+
element text: 'hello world' asRopedText.
152+
label.
153+
```
154+
155+
72156

73157
### Text size
74158

159+
Fonts are managed directly by Alexandrie. To get the list of available fonts,
160+
take a look at the result of `AeFontManager globalInstance familyNames`
161+
75162
The size of your text will depend on the font you have selected. This font will
76163
constrain aspect of the size of letters, words, and text.
77164
Let's familiarize ourselves with those basic measures.
78-
Bloc will get the measures (in `BATextParagraphSpan >> measure`)
79-
to get the size of your text, and position it into your element.
80165

81166
Figure *@MulAttri@* describes the information listed below:
82167

@@ -117,14 +202,16 @@ Internally, your text will be split into a collection of *spans*.
117202
A span is a homogeneous styled piece of text where every character has the same set of
118203
attributes.
119204

120-
### Text bounds
205+
### Advanced used of BlTextElement - Text bounds
121206

122-
Text can after be rendered as `BlTextElement`, which will take care of
123-
displaying properly the text with all properties defined on it.
124207

125-
Once you have a `BlTextElement`, all properties of `BlElement` apply.
126-
You can add your element to any existing `BlElement`, and integrate it easily in
127-
your graphical interface; it'll follow the same layout rules.
208+
You can define very precicely how your text is place in your BlTextElement.
209+
During Bloc rendering phase, element return their measure to be layedout
210+
properly. This happen in the `onMeasure:` message. you can customize the rule
211+
used for text measurement to make it fit to your Bloc scene. By default,
212+
BlTextElement use the *tightMeasurement* strategy. You can change it with the
213+
right message. For example, to use the editor measurement, simply state:
214+
`yourElement editorMeasurement`
128215

129216
`BlTextElement` has 3 available measures by default that determine its bounds.
130217

@@ -134,7 +221,19 @@ your graphical interface; it'll follow the same layout rules.
134221

135222
![Text measures.](figures/textMeasure.png width=80)
136223

137-
By default, *BlTextElement* will follow the *tightMeasurement* measure.
224+
You can develop your own strategy and customize text measure at two levels:
225+
1. Text is first converted to `BlTextParagraph`, which will receive a subclass
226+
of `BlTextParagraphBaselineMeasurer` as a baseline measure strategy
227+
1. Paragraph measure is then used by a subclass of
228+
`BlTextElementMeasurementStrategy` to specify the *BlBounds* of
229+
BlTextElement.
230+
231+
you can then add a new method do *BlTextElement* with your new strategy, like
232+
```
233+
BlTextElement >> tightMeasurement
234+
self measurement: BlTextElementTightMeasurementStrategy uniqueInstance.
235+
self baselineMeasurer: BlBoundsBaselineMeasurer uniqueInstance
236+
```
138237

139238
### Examples
140239

@@ -174,85 +273,7 @@ whose result is shown in Figure *@rect@*.
174273

175274

176275

177-
### BlText vs. BlTextElement
178276

179-
You have 2 different levels to manage text in Bloc.
180-
- `BlText` and its subclass `BlRopedText` create a text model where you can specify its attributes and style.
181-
- `BlTextElement` and its subclasses will properly display the text inside a Bloc element.
182-
183-
A small example. You can notice that `BlText` background is different from `BlTextElement` background.
184-
185-
```smalltalk
186-
| labelText label |
187-
labelText := 'hello from bloc' asRopedText
188-
background: Color orange ;
189-
fontSize: 75;
190-
fontName: 'Source Code Pro';
191-
italic;
192-
underline;
193-
underlineColor: Color red;
194-
vertical.
195-
196-
(labelText from: 1 to: 5) foreground: Color blue.
197-
(labelText from: 7 to: 11) foreground: Color white.
198-
(labelText from: 12 to: 15) foreground: Color red.
199-
200-
label := (BlTextElement text: labelText) position: 50 @ 10; background: Color yellow.
201-
```
202-
203-
you can define the style of your text through BlTextAttributesStyler
204-
205-
````smalltalk
206-
text := 'Hi John' asRopedText.
207-
208-
styler := BlTextAttributesStyler on: (text from: 3 to: 7).
209-
styler
210-
bold;
211-
italic;
212-
fontSize: 30;
213-
fontName: 'Roboto';
214-
monospace;
215-
foreground: Color green.
216-
styler style.
217-
```
218-
219-
or using a fluent API
220-
221-
````smalltalk
222-
text := 'Hi John' asRopedText.
223-
(text from: 3 to: 7) stylerDo: [ :aStyler | aStyler bold italic foreground: Color red ].
224-
````
225-
226-
As you may have noticed, this gives you a very fine-grained control over the style of your text.
227-
You also need to re-specify attributes when your text changes.
228-
If you want all your text to use the same attribute, you can then use `BlAttributedTextElement`.
229-
You can then change your text, `BlAttributedTextElement` will reuse its attributes.
230-
231-
232-
```smalltalk
233-
text := 'Hi John' asRopedText.
234-
235-
element := BlAttributedTextElement new.
236-
attributes := element attributesBuilder
237-
foreground: Color green;
238-
monospace;
239-
bold;
240-
italic;
241-
fontSize: 30;
242-
fontName: 'Roboto';
243-
monospace.
244-
245-
label := (element text: text)
246-
position: 50 @ 10;
247-
background: Color yellow;
248-
margin: (BlInsets all: 2);
249-
padding: (BlInsets all: 2);
250-
outskirts: BlOutskirts centered;
251-
border: (BlBorder paint: Color red width: 2).
252-
253-
element text: 'hello world' asRopedText.
254-
label.
255-
```
256277

257278
### upload a font to bloc
258279

@@ -391,4 +412,4 @@ label transformDo: [ :t |
391412
label size: label transformedBounds extent.
392413
```
393414

394-
In this snippet we rotate the element containing the text (but not the textElement) using `TBlTransformable>>tranformDo:` but for that we need to force its layout and just like in the earlier example, the transformation didn't change the position nor the bounds hence the last line.
415+
In this snippet we rotate the element containing the text (but not the textElement) using `TBlTransformable>>tranformDo:` but for that we need to force its layout and just like in the earlier example, the transformation didn't change the position nor the bounds hence the last line.

0 commit comments

Comments
 (0)