-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
RubCharacterScanner.class.st
492 lines (427 loc) · 15 KB
/
RubCharacterScanner.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"
I scan characters
"
Class {
#name : #RubCharacterScanner,
#superclass : #Object,
#instVars : [
'destX',
'lastIndex',
'destY',
'stopConditions',
'text',
'textStyle',
'alignment',
'leftMargin',
'rightMargin',
'font',
'line',
'runStopIndex',
'spaceCount',
'spaceWidth',
'emphasisCode',
'kern',
'indentationLevel',
'baselineY',
'firstDestX',
'pendingKernX'
],
#classVars : [
'DefaultStopConditions',
'NilCondition',
'PaddedSpaceCondition',
'SpaceCondition'
],
#pools : [
'TextConstants'
],
#category : #'Rubric-TextScanning'
}
{ #category : #'class initialization' }
RubCharacterScanner class >> initialize [
"
RubCharacterScanner initialize
"
| a |
a := RubTextStopConditions new.
a at: 1 + 1 put: #embeddedObject.
a at: Character tab asciiValue + 1 put: #tab.
a at: Character cr asciiValue + 1 put: #cr.
a at: Character lf asciiValue + 1 put: #cr.
NilCondition := a copy.
DefaultStopConditions := a copy.
PaddedSpaceCondition := a copy.
PaddedSpaceCondition at: Character space asciiValue + 1 put: #paddedSpace.
SpaceCondition := a copy.
SpaceCondition at: Character space asciiValue + 1 put: #space.
]
{ #category : #private }
RubCharacterScanner >> addEmphasis: code [
"Set the bold-ital-under-strike emphasis."
emphasisCode := emphasisCode bitOr: code
]
{ #category : #private }
RubCharacterScanner >> addKern: kernDelta [
"Set the current kern amount."
kern := kern + kernDelta
]
{ #category : #private }
RubCharacterScanner >> backgroundColor: ignored [
"Overridden in DisplayScanner"
]
{ #category : #scanning }
RubCharacterScanner >> basicScanCharactersFrom: startIndex to: stopIndex in: sourceString rightX: rightX stopConditions: stops kern: kernDelta [
"Primitive. This is the inner loop of text display--but see
scanCharactersFrom: to:rightX: which would get the string,
stopConditions and displaying from the instance. March through source
String from startIndex to stopIndex. If any character is flagged with a
non-nil entry in stops, then return the corresponding value. Determine
width of each character from xTable, indexed by map.
If dextX would exceed rightX, then return stops at: 258.
Advance destX by the width of the character. If stopIndex has been
reached, then return stops at: 257. Optional.
See Object documentation whatIsAPrimitive."
| ascii nextDestX char floatDestX widthAndKernedWidth nextChar atEndOfRun |
<primitive: 103>
lastIndex := startIndex.
floatDestX := destX.
widthAndKernedWidth := Array new: 2.
atEndOfRun := false.
[lastIndex <= stopIndex]
whileTrue: [
char := (sourceString at: lastIndex).
ascii := char asciiValue.
(ascii < stops size and: [(stops at: ascii + 1) notNil]) ifTrue: [^ stops at: ascii + 1].
(self isBreakableAt: lastIndex in: sourceString in: Latin1Environment)
ifTrue: [ self registerBreakableIndex ].
"Note: The following is querying the font about the width
since the primitive may have failed due to a non-trivial
mapping of characters to glyphs or a non-existing xTable."
nextChar := (lastIndex + 1 <= stopIndex)
ifTrue:[sourceString at: lastIndex + 1]
ifFalse:[
atEndOfRun := true.
"if there is a next char in sourceString, then get the kern
and store it in pendingKernX"
lastIndex + 1 <= sourceString size
ifTrue:[sourceString at: lastIndex + 1]
ifFalse:[ nil]].
font
widthAndKernedWidthOfLeft: char
right: nextChar
into: widthAndKernedWidth.
nextDestX := floatDestX + (widthAndKernedWidth at: 1).
"note crossing of right component side, unless we would end up on the first position
again (endless loop, if line is to narrow to hold this current character)"
nextDestX > rightX ifTrue: [firstDestX ~= destX ifTrue:[ ^stops crossedX]].
floatDestX := floatDestX + kernDelta + (widthAndKernedWidth at: 2).
atEndOfRun
ifTrue:[
pendingKernX := (widthAndKernedWidth at: 2) - (widthAndKernedWidth at: 1).
floatDestX := floatDestX - pendingKernX].
destX := floatDestX.
lastIndex := lastIndex + 1].
lastIndex := stopIndex.
^ stops endOfRun
]
{ #category : #'scanner methods' }
RubCharacterScanner >> combinableChar: char for: prevEntity [
]
{ #category : #scanning }
RubCharacterScanner >> embeddedObject [
| savedIndex |
savedIndex := lastIndex.
text
attributesAt: lastIndex
do: [ :attr |
attr anchoredMorph
ifNotNil: [
"Following may look strange but logic gets reversed.
If the morph fits on this line we're not done (return false for true)
and if the morph won't fit we're done (return true for false)"
(self placeEmbeddedObject: attr anchoredMorph)
ifFalse: [ ^ true ] ] ].
lastIndex := savedIndex + 1. "for multiple(!) embedded morphs"
^ false
]
{ #category : #scanning }
RubCharacterScanner >> handleIndentation [
self indentationLevel timesRepeat: [
self plainTab]
]
{ #category : #scanning }
RubCharacterScanner >> indentationLevel [
"return the number of tabs that are currently being placed at the beginning of each line"
^indentationLevel ifNil:[0]
]
{ #category : #scanning }
RubCharacterScanner >> indentationLevel: anInteger [
"set the number of tabs to put at the beginning of each line"
indentationLevel := anInteger
]
{ #category : #initialization }
RubCharacterScanner >> initialize [
super initialize.
destX := destY := leftMargin := 0.
]
{ #category : #'scanner methods' }
RubCharacterScanner >> isBreakableAt: index in: sourceString in: encodingClass [
^ encodingClass isBreakableAt: index in: sourceString.
]
{ #category : #'text constants' }
RubCharacterScanner >> justified [
^ 3
]
{ #category : #scanning }
RubCharacterScanner >> leadingTab [
"return true if only tabs lie to the left"
line first to: lastIndex do:
[:i | (text at: i) == Character tab ifFalse: [^ false]].
^ true
]
{ #category : #scanning }
RubCharacterScanner >> measureString: aString inFont: aFont from: startIndex to: stopIndex [
"WARNING: In order to use this method the receiver has to be set up using #initializeStringMeasurer"
destX := destY := lastIndex := 0.
baselineY := aFont ascent.
font := aFont. " added Dec 03, 2004 "
" map := aFont characterToGlyphMap."
self scanCharactersFrom: startIndex to: stopIndex in: aString rightX: 999999 stopConditions: stopConditions kern: 0.
^destX
]
{ #category : #scanning }
RubCharacterScanner >> nextTabXFrom: anX [
"Tab stops are distances from the left margin. Set the distance into the
argument, anX, normalized for the paragraph's left margin."
| normalizedX tabX mx |
mx := 9999.
normalizedX := anX - leftMargin.
tabX := self tabWidth.
[ tabX <= normalizedX and: [ tabX < mx ] ] whileTrue: [ tabX := tabX + self tabWidth ].
^ tabX < mx
ifTrue: [ leftMargin + tabX min: rightMargin ]
ifFalse: [ rightMargin ]
]
{ #category : #scanning }
RubCharacterScanner >> placeEmbeddedObject: anchoredMorph [
"Place the anchoredMorph or return false if it cannot be placed.
In any event, advance destX by its width."
| w |
"Workaround: The following should really use #textAnchorType"
anchoredMorph relativeTextAnchorPosition ifNotNil:[^true].
destX := destX + (w := anchoredMorph width).
(destX > rightMargin and: [(leftMargin + w) <= rightMargin])
ifTrue: ["Won't fit, but would on next line"
^ false].
lastIndex := lastIndex + 1.
^ true
]
{ #category : #scanning }
RubCharacterScanner >> plainTab [
"This is the basic method of adjusting destX for a tab."
destX := (alignment = self justified and: [self leadingTab not])
ifTrue: "embedded tabs in justified text are weird"
[destX + (self tabWidth - (line justifiedTabDeltaFor: spaceCount)) max: destX]
ifFalse:
[self nextTabXFrom: destX].
pendingKernX := 0.
firstDestX := destX.
]
{ #category : #'multilingual scanning' }
RubCharacterScanner >> registerBreakableIndex [
"Record left x and character index of the line-wrappable point.
The default implementation here does nothing."
^ false.
]
{ #category : #scanning }
RubCharacterScanner >> scanCharactersFrom: startIndex to: stopIndex in: sourceString rightX: rightX stopConditions: stops kern: kernDelta [
| startEncoding selector |
sourceString isByteString
ifTrue: [ ^ self
basicScanCharactersFrom: startIndex
to: stopIndex
in: sourceString
rightX: rightX
stopConditions: stops
kern: kernDelta ].
sourceString isWideString ifFalse: [ ^ stops endOfRun ].
startIndex > stopIndex
ifTrue: [
lastIndex := stopIndex.
^ stops endOfRun ].
startEncoding := (sourceString at: startIndex) leadingChar.
selector := (EncodedCharSet charsetAt: startEncoding) scanSelector.
^ self
perform: selector
withArguments: (Array
with: startIndex
with: stopIndex
with: sourceString
with: rightX
with: stops
with: kernDelta).
]
{ #category : #'scanner methods' }
RubCharacterScanner >> scanJapaneseCharactersFrom: startIndex to: stopIndex in: sourceString rightX: rightX stopConditions: stops kern: kernDelta [
| ascii encoding f nextDestX maxAscii startEncoding |
lastIndex := startIndex.
lastIndex > stopIndex ifTrue: [lastIndex := stopIndex. ^ stops endOfRun].
startEncoding := (sourceString at: startIndex) leadingChar.
font ifNil: [font := TextStyle defaultFont fontArray at: 1].
font isFontSet ifTrue: [
f := [font fontArray at: startEncoding + 1]
on: Exception do: [:ex | nil].
f ifNil: [ f := font fontArray at: 1].
maxAscii := f maxAscii.
"xTable := f xTable.
maxAscii := xTable size - 2."
spaceWidth := f widthOf: Character space.
] ifFalse: [
maxAscii := font maxAscii.
].
[lastIndex <= stopIndex] whileTrue: [
"self halt."
encoding := (sourceString at: lastIndex) leadingChar.
encoding ~= startEncoding ifTrue: [lastIndex := lastIndex - 1. ^ stops endOfRun].
ascii := (sourceString at: lastIndex) charCode.
ascii > maxAscii ifTrue: [ascii := maxAscii].
(encoding = 0 and: [ascii < stops size and: [(stops at: ascii + 1) ~~ nil]]) ifTrue: [^ stops at: ascii + 1].
(self isBreakableAt: lastIndex in: sourceString in: (EncodedCharSet charsetAt: encoding)) ifTrue: [
self registerBreakableIndex.
].
nextDestX := destX + (font widthOf: (sourceString at: lastIndex)).
nextDestX > rightX ifTrue: [firstDestX ~= destX ifTrue: [^ stops crossedX]].
destX := nextDestX + kernDelta.
lastIndex := lastIndex + 1.
].
lastIndex := stopIndex.
^ stops endOfRun
]
{ #category : #scanning }
RubCharacterScanner >> scanMultiCharactersFrom: startIndex to: stopIndex in: sourceString rightX: rightX stopConditions: stops kern: kernDelta [
| ascii encoding f nextDestX maxAscii startEncoding floatDestX widthAndKernedWidth nextChar atEndOfRun |
lastIndex := startIndex.
lastIndex > stopIndex ifTrue: [lastIndex := stopIndex. ^ stops endOfRun].
startEncoding := (sourceString at: startIndex) leadingChar.
font ifNil: [font := TextStyle defaultFont fontArray at: 1].
font isFontSet ifTrue: [
f := [font fontArray at: startEncoding + 1]
on: Exception do: [:ex | nil].
f ifNil: [ f := font fontArray at: 1].
maxAscii := f maxAscii.
spaceWidth := f widthOf: Character space.
] ifFalse: [
maxAscii := font maxAscii.
].
floatDestX := destX.
widthAndKernedWidth := Array new: 2.
atEndOfRun := false.
[lastIndex <= stopIndex] whileTrue: [
encoding := (sourceString at: lastIndex) leadingChar.
encoding ~= startEncoding ifTrue: [lastIndex := lastIndex - 1. ^ stops endOfRun].
ascii := (sourceString at: lastIndex) charCode.
ascii > maxAscii ifTrue: [ascii := maxAscii].
(encoding = 0 and: [ascii < stops size and: [(stops at: ascii + 1) ~~ nil]]) ifTrue: [^ stops at: ascii + 1].
(self isBreakableAt: lastIndex in: sourceString in: Latin1Environment) ifTrue: [
self registerBreakableIndex.
].
nextChar := (lastIndex + 1 <= stopIndex)
ifTrue:[sourceString at: lastIndex + 1]
ifFalse:[
atEndOfRun := true.
"if there is a next char in sourceString, then get the kern
and store it in pendingKernX"
lastIndex + 1 <= sourceString size
ifTrue:[sourceString at: lastIndex + 1]
ifFalse:[ nil]].
font
widthAndKernedWidthOfLeft: (sourceString at: lastIndex)
right: nextChar
into: widthAndKernedWidth.
nextDestX := floatDestX + (widthAndKernedWidth at: 1).
nextDestX > rightX ifTrue: [destX ~= firstDestX ifTrue: [^stops crossedX]].
floatDestX := floatDestX + kernDelta + (widthAndKernedWidth at: 2).
atEndOfRun
ifTrue:[
pendingKernX := (widthAndKernedWidth at: 2) - (widthAndKernedWidth at: 1).
floatDestX := floatDestX - pendingKernX].
destX := floatDestX .
lastIndex := lastIndex + 1.
].
lastIndex := stopIndex.
^ stops endOfRun
]
{ #category : #private }
RubCharacterScanner >> setActualFont: aFont [
"Set the basal font to an isolated font reference."
font := aFont
]
{ #category : #private }
RubCharacterScanner >> setAlignment: style [
alignment := style.
]
{ #category : #private }
RubCharacterScanner >> setConditionArray: aSymbol [
aSymbol == #paddedSpace ifTrue: [^stopConditions := PaddedSpaceCondition "copy"].
"aSymbol == #space ifTrue: [^stopConditions := SpaceCondition copy]."
aSymbol == nil ifTrue: [^stopConditions := NilCondition "copy"].
self error: 'undefined stopcondition for space character'.
]
{ #category : #private }
RubCharacterScanner >> setFont [
| priorFont |
"Set the font and other emphasis."
priorFont := font.
text == nil
ifFalse: [
emphasisCode := 0.
kern := 0.
indentationLevel := 0.
alignment := textStyle alignment.
font := nil.
(text attributesAt: lastIndex forStyle: textStyle) do: [ :att | att emphasizeScanner: self ] ].
font == nil
ifTrue: [ self setFont: textStyle defaultFontIndex ].
font := font emphasized: emphasisCode.
priorFont
ifNotNil: [
font = priorFont
ifTrue: [
"font is the same, perhaps the color has changed?
We still want kerning between chars of the same
font, but of different color. So add any pending kern to destX"
destX := destX + (pendingKernX ifNil: [ 0 ]) ].
destX := destX + priorFont descentKern ].
pendingKernX := 0. "clear any pending kern so there is no danger of it being added twice"
destX := destX - font descentKern. "NOTE: next statement should be removed when clipping works"
leftMargin ifNotNil: [ destX := destX max: leftMargin ].
kern := kern - font baseKern. "Install various parameters from the font."
spaceWidth := font widthOf: Character space. " map := font characterToGlyphMap."
stopConditions := DefaultStopConditions
]
{ #category : #private }
RubCharacterScanner >> setFont: fontNumber [
"Set the font by number from the textStyle."
self setActualFont: (textStyle fontAt: fontNumber)
]
{ #category : #accessing }
RubCharacterScanner >> tabWidth [
^ textStyle rubTabWidth
]
{ #category : #private }
RubCharacterScanner >> text: t textStyle: ts [
text := t.
textStyle := ts
]
{ #category : #private }
RubCharacterScanner >> textColor: ignored [
"Overridden in DisplayScanner"
]
{ #category : #'multilingual scanning' }
RubCharacterScanner >> widthOf: char inFont: aFont [
(char isMemberOf: CombinedChar) ifTrue: [
^ aFont widthOf: char base.
] ifFalse: [
^ aFont widthOf: char.
].
]