304
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
|
|
14 |
CharacterArray subclass:#Text
|
|
15 |
instanceVariableNames:'string runs'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Collections-Text'
|
|
19 |
!
|
|
20 |
|
|
21 |
!Text class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
|
|
36 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
|
40 |
Texts add emphasis information to a string.
|
|
41 |
|
|
42 |
This class is not yet fully implemented - being constructed.
|
|
43 |
|
|
44 |
[author:]
|
|
45 |
Claus Gittinger
|
|
46 |
|
|
47 |
[see also:]
|
|
48 |
CharacterArray String RunArray
|
|
49 |
"
|
|
50 |
! !
|
|
51 |
|
|
52 |
!Text class methodsFor:'instance creation'!
|
|
53 |
|
|
54 |
fromString:aString
|
|
55 |
^ self new string:aString emphasis:nil
|
|
56 |
|
|
57 |
"
|
|
58 |
Text fromString:'hello'
|
|
59 |
"
|
|
60 |
|
|
61 |
"Modified: 11.5.1996 / 14:20:35 / cg"
|
|
62 |
!
|
|
63 |
|
307
|
64 |
new:size
|
|
65 |
"redefined for string-protocol compatibility"
|
|
66 |
|
|
67 |
^ self string:(String new:size)
|
|
68 |
|
|
69 |
"Created: 12.5.1996 / 11:57:27 / cg"
|
|
70 |
!
|
|
71 |
|
304
|
72 |
string:aString
|
|
73 |
^ self new string:aString emphasis:nil
|
|
74 |
|
|
75 |
"
|
|
76 |
Text string:'hello'
|
|
77 |
"
|
|
78 |
|
|
79 |
"Modified: 11.5.1996 / 14:20:48 / cg"
|
|
80 |
!
|
|
81 |
|
|
82 |
string:aString emphasis:attribute
|
|
83 |
^ self new string:aString emphasis:attribute
|
|
84 |
|
|
85 |
"
|
|
86 |
Text string:'hello' emphasis:#bold
|
|
87 |
"
|
|
88 |
|
|
89 |
"Modified: 11.5.1996 / 14:21:01 / cg"
|
|
90 |
! !
|
|
91 |
|
|
92 |
!Text methodsFor:'comparing'!
|
|
93 |
|
|
94 |
= aStringOrText
|
|
95 |
"compare the receiver and the argument, ignoring emphasis"
|
|
96 |
|
|
97 |
^ string = aStringOrText string
|
|
98 |
|
|
99 |
|
|
100 |
"
|
|
101 |
'hello' asText = 'hello'
|
|
102 |
'hello' asText = 'hello' asText
|
|
103 |
'hello' asText allBold = 'hello'
|
|
104 |
"
|
|
105 |
|
|
106 |
"Modified: 11.5.1996 / 14:33:00 / cg"
|
|
107 |
! !
|
|
108 |
|
|
109 |
!Text methodsFor:'converting'!
|
|
110 |
|
|
111 |
asText
|
|
112 |
^ self
|
|
113 |
|
|
114 |
"Created: 11.5.1996 / 14:08:36 / cg"
|
|
115 |
!
|
|
116 |
|
|
117 |
at:characterIndex
|
|
118 |
^ string at:characterIndex
|
|
119 |
|
|
120 |
"Created: 11.5.1996 / 14:25:41 / cg"
|
|
121 |
!
|
|
122 |
|
|
123 |
at:characterIndex put:aCharacter
|
|
124 |
^ string at:characterIndex put:aCharacter
|
|
125 |
|
|
126 |
"Created: 11.5.1996 / 14:25:49 / cg"
|
|
127 |
! !
|
|
128 |
|
|
129 |
!Text methodsFor:'copying'!
|
|
130 |
|
|
131 |
, aStringOrText
|
|
132 |
"for now, return a string ..."
|
|
133 |
|
|
134 |
^ self species new
|
|
135 |
string:(string , aStringOrText)
|
|
136 |
runs:(runs , aStringOrText runs)
|
|
137 |
|
|
138 |
"Created: 11.5.1996 / 13:59:54 / cg"
|
307
|
139 |
! !
|
|
140 |
|
|
141 |
!Text methodsFor:'displaying'!
|
|
142 |
|
|
143 |
displayOn:aGC x:x y:y
|
|
144 |
"display the receiver on a GC"
|
|
145 |
|
|
146 |
self displayOn:aGC x:x y:y opaque:false
|
|
147 |
|
|
148 |
"Modified: 12.5.1996 / 11:14:57 / cg"
|
|
149 |
!
|
|
150 |
|
|
151 |
displayOn:aGC x:x y:y from:start to:stop
|
|
152 |
"display the receiver on a GC"
|
|
153 |
|
|
154 |
"q&d hack"
|
|
155 |
|
|
156 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:false
|
|
157 |
|
|
158 |
"Modified: 12.5.1996 / 12:49:33 / cg"
|
304
|
159 |
!
|
|
160 |
|
307
|
161 |
displayOn:aGC x:x0 y:y opaque:opaque
|
|
162 |
"display the receiver on a GC"
|
|
163 |
|
|
164 |
|savedFont font color boldFont italicFont savedPaint
|
|
165 |
bold italic underline strikeout pos x l|
|
|
166 |
|
|
167 |
savedFont := aGC basicFont.
|
|
168 |
savedPaint := aGC paint.
|
|
169 |
|
|
170 |
pos := 1.
|
|
171 |
x := x0.
|
|
172 |
runs runsDo:[:len :emphasis |
|
|
173 |
color := nil.
|
|
174 |
bold := italic := underline := strikeout := false.
|
|
175 |
|
|
176 |
emphasis isSymbol ifTrue:[
|
|
177 |
emphasis == #bold ifTrue:[bold := true]
|
|
178 |
ifFalse:[emphasis == #italic ifTrue:[italic := true]
|
|
179 |
ifFalse:[emphasis == #underline ifTrue:[underline := true]
|
|
180 |
ifFalse:[emphasis == #strikeout ifTrue:[strikeout := true]
|
|
181 |
]]]
|
|
182 |
] ifFalse:[
|
|
183 |
(emphasis isMemberOf:Association) ifTrue:[
|
|
184 |
emphasis key == #color ifTrue:[
|
|
185 |
color := emphasis value
|
|
186 |
]
|
|
187 |
] ifFalse:[
|
|
188 |
emphasis notNil ifTrue:[
|
|
189 |
emphasis do:[:entry |
|
|
190 |
entry == #bold ifTrue:[bold := true]
|
|
191 |
ifFalse:[entry == #italic ifTrue:[italic := true]
|
|
192 |
ifFalse:[entry == #underline ifTrue:[underline := true]
|
|
193 |
ifFalse:[entry == #strikeout ifTrue:[strikeout := true]
|
|
194 |
ifFalse:[
|
|
195 |
(entry isMemberOf:Association) ifTrue:[
|
|
196 |
entry key == #color ifTrue:[
|
|
197 |
color := entry value
|
|
198 |
]
|
|
199 |
]]]]]
|
|
200 |
]
|
|
201 |
]
|
|
202 |
]
|
|
203 |
].
|
|
204 |
|
|
205 |
color isNil ifTrue:[
|
|
206 |
color := savedPaint.
|
|
207 |
] ifFalse:[
|
|
208 |
color := color on:aGC device.
|
|
209 |
].
|
304
|
210 |
|
307
|
211 |
bold ifTrue:[
|
|
212 |
boldFont isNil ifTrue:[
|
|
213 |
boldFont := savedFont asBold on:aGC device
|
|
214 |
].
|
|
215 |
font := boldFont.
|
|
216 |
] ifFalse:[
|
|
217 |
italic ifTrue:[
|
|
218 |
italicFont isNil ifTrue:[
|
|
219 |
italicFont := savedFont asItalic on:aGC device
|
|
220 |
].
|
|
221 |
font := italicFont.
|
|
222 |
] ifFalse:[
|
|
223 |
font := savedFont
|
|
224 |
]
|
|
225 |
].
|
|
226 |
aGC basicFont:font.
|
|
227 |
aGC paint:color.
|
|
228 |
|
|
229 |
opaque ifTrue:[
|
|
230 |
aGC displayOpaqueString:string from:pos to:(pos + len - 1) x:x y:y.
|
|
231 |
] ifFalse:[
|
|
232 |
aGC displayString:string from:pos to:(pos + len - 1) x:x y:y.
|
|
233 |
].
|
|
234 |
l := aGC font widthOf:string from:pos to:(pos + len - 1).
|
|
235 |
|
|
236 |
underline ifTrue:[
|
|
237 |
aGC displayLineFromX:x y:y toX:x+l-1 y:y
|
|
238 |
].
|
|
239 |
|
|
240 |
x := x + l.
|
|
241 |
pos := pos + len
|
304
|
242 |
].
|
307
|
243 |
|
|
244 |
aGC basicFont:savedFont.
|
|
245 |
aGC paint:savedPaint.
|
|
246 |
|
|
247 |
"Created: 12.5.1996 / 11:14:30 / cg"
|
|
248 |
"Modified: 12.5.1996 / 11:55:06 / cg"
|
|
249 |
!
|
|
250 |
|
|
251 |
displayOpaqueOn:aGC x:x y:y
|
|
252 |
"display the receiver on a GC"
|
|
253 |
|
|
254 |
self displayOn:aGC x:x y:y opaque:true
|
304
|
255 |
|
307
|
256 |
"Modified: 12.5.1996 / 11:14:52 / cg"
|
|
257 |
!
|
|
258 |
|
|
259 |
displayOpaqueOn:aGC x:x y:y from:start to:stop
|
|
260 |
"display the receiver on a GC"
|
|
261 |
|
|
262 |
"q&d hack"
|
|
263 |
|
|
264 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:true
|
|
265 |
|
|
266 |
"Created: 12.5.1996 / 12:29:37 / cg"
|
|
267 |
"Modified: 12.5.1996 / 12:49:19 / cg"
|
304
|
268 |
! !
|
|
269 |
|
|
270 |
!Text methodsFor:'emphasis'!
|
|
271 |
|
|
272 |
allBold
|
|
273 |
"make all characters bold"
|
|
274 |
|
|
275 |
runs := RunArray new:(string size) withAll:#bold
|
|
276 |
|
|
277 |
"
|
|
278 |
(Text string:'hello') allBold
|
|
279 |
"
|
|
280 |
|
|
281 |
"Modified: 11.5.1996 / 14:22:12 / cg"
|
|
282 |
!
|
|
283 |
|
|
284 |
emphasisAt:characterIndex
|
|
285 |
"return the emphasis at some index"
|
|
286 |
|
|
287 |
^ runs at:characterIndex
|
|
288 |
|
|
289 |
"
|
|
290 |
(Text string:'hello') allBold emphasisAt:2
|
|
291 |
"
|
|
292 |
|
|
293 |
"Modified: 11.5.1996 / 14:22:31 / cg"
|
|
294 |
!
|
|
295 |
|
307
|
296 |
emphasisAt:characterIndex put:emphasis
|
|
297 |
"change the emphasis at some index. return the receiver"
|
|
298 |
|
|
299 |
runs at:characterIndex put:emphasis
|
|
300 |
|
|
301 |
"
|
|
302 |
(Text string:'hello') allBold emphasisAt:2 put:#italic
|
|
303 |
"
|
|
304 |
|
|
305 |
"Modified: 12.5.1996 / 12:40:31 / cg"
|
|
306 |
!
|
|
307 |
|
304
|
308 |
emphasizeAllWith:emphasis
|
|
309 |
"change the emphasis of all characters"
|
|
310 |
|
|
311 |
runs := RunArray new:(string size) withAll:emphasis
|
|
312 |
|
|
313 |
"
|
|
314 |
(Text string:'hello') allBold emphasizeAllWith:#italic
|
|
315 |
"
|
|
316 |
|
|
317 |
"Modified: 11.5.1996 / 14:22:52 / cg"
|
|
318 |
!
|
|
319 |
|
|
320 |
emphasizeFrom:start to:stop with:emphasis
|
|
321 |
"change the emphasis of a range of characters"
|
|
322 |
|
|
323 |
|newRuns|
|
|
324 |
|
|
325 |
newRuns := RunArray new.
|
|
326 |
|
|
327 |
"/ for now - a q&d hack
|
|
328 |
1 to:start-1 do:[:i |
|
|
329 |
newRuns add:(runs at:i).
|
|
330 |
].
|
|
331 |
newRuns add:emphasis withOccurrences:(stop - start + 1).
|
|
332 |
stop+1 to:string size do:[:i |
|
|
333 |
newRuns add:(runs at:i)
|
|
334 |
].
|
|
335 |
runs := newRuns
|
|
336 |
|
|
337 |
"
|
|
338 |
(Text string:'hello world')
|
|
339 |
emphasizeFrom:1 to:5 with:#bold;
|
|
340 |
emphasizeFrom:7 to:11 with:#italic
|
|
341 |
"
|
|
342 |
|
|
343 |
"Modified: 11.5.1996 / 14:30:02 / cg"
|
|
344 |
! !
|
|
345 |
|
|
346 |
!Text methodsFor:'printing & storing'!
|
|
347 |
|
|
348 |
displayString
|
|
349 |
^ self storeString
|
|
350 |
|
|
351 |
"Created: 11.5.1996 / 14:24:48 / cg"
|
|
352 |
!
|
|
353 |
|
|
354 |
printOn:aStream
|
|
355 |
string printOn:aStream
|
|
356 |
|
|
357 |
"Created: 11.5.1996 / 14:27:25 / cg"
|
|
358 |
!
|
|
359 |
|
|
360 |
storeOn:aStream
|
|
361 |
aStream nextPutAll:'(Text string:'.
|
|
362 |
string storeOn:aStream.
|
307
|
363 |
aStream nextPutAll:' runs:'.
|
304
|
364 |
runs storeOn:aStream.
|
|
365 |
aStream nextPutAll:')'.
|
|
366 |
|
|
367 |
"Created: 11.5.1996 / 14:27:09 / cg"
|
307
|
368 |
"Modified: 12.5.1996 / 12:45:16 / cg"
|
304
|
369 |
! !
|
|
370 |
|
|
371 |
!Text methodsFor:'private accessing'!
|
|
372 |
|
|
373 |
string:aString emphasis:emphasis
|
|
374 |
string := aString.
|
|
375 |
runs := RunArray new:string size withAll:emphasis
|
|
376 |
|
|
377 |
"
|
|
378 |
|t|
|
|
379 |
|
|
380 |
t := Text new string:'hello' emphasis:#bold.
|
|
381 |
t emphasisAt:2.
|
|
382 |
"
|
|
383 |
|
|
384 |
"Modified: 11.5.1996 / 14:19:38 / cg"
|
|
385 |
! !
|
|
386 |
|
|
387 |
!Text methodsFor:'queries'!
|
|
388 |
|
307
|
389 |
bitsPerCharacter
|
|
390 |
^ string bitsPerCharacter
|
304
|
391 |
|
307
|
392 |
"Created: 12.5.1996 / 15:44:04 / cg"
|
304
|
393 |
!
|
|
394 |
|
|
395 |
hasChangeOfEmphasis
|
|
396 |
^ runs notNil
|
|
397 |
|
|
398 |
"Created: 11.5.1996 / 14:03:19 / cg"
|
|
399 |
!
|
|
400 |
|
307
|
401 |
heightOn:aGC
|
|
402 |
"return the number of device units, required on aGC's device"
|
|
403 |
|
|
404 |
^ aGC font heightOf:string
|
|
405 |
|
|
406 |
"Created: 12.5.1996 / 11:02:03 / cg"
|
|
407 |
!
|
|
408 |
|
304
|
409 |
indexOf:aCharacter startingAt:index
|
|
410 |
^ string indexOf:aCharacter startingAt:index
|
|
411 |
|
|
412 |
"Created: 11.5.1996 / 13:58:56 / cg"
|
|
413 |
!
|
|
414 |
|
307
|
415 |
isText
|
|
416 |
^ true
|
|
417 |
|
|
418 |
"Created: 12.5.1996 / 10:56:24 / cg"
|
|
419 |
!
|
|
420 |
|
304
|
421 |
occurrencesOf:aCharacter
|
|
422 |
^ string occurrencesOf:aCharacter
|
|
423 |
|
|
424 |
"Created: 11.5.1996 / 13:58:46 / cg"
|
|
425 |
!
|
|
426 |
|
|
427 |
size
|
|
428 |
^ string size
|
|
429 |
|
|
430 |
"Created: 11.5.1996 / 14:25:15 / cg"
|
|
431 |
!
|
|
432 |
|
|
433 |
string
|
|
434 |
^ string
|
|
435 |
|
|
436 |
"Created: 11.5.1996 / 13:58:38 / cg"
|
307
|
437 |
!
|
|
438 |
|
|
439 |
widthOn:aGC
|
|
440 |
"return the number of device units, required on aGC's device"
|
|
441 |
|
|
442 |
|savedFont savedPaint boldFont italicFont bold italic pos f l|
|
|
443 |
|
|
444 |
savedFont := aGC basicFont on:aGC device.
|
|
445 |
|
|
446 |
pos := 1.
|
|
447 |
l := 0.
|
|
448 |
runs runsDo:[:len :emphasis |
|
|
449 |
emphasis isSymbol ifTrue:[
|
|
450 |
bold := emphasis == #bold.
|
|
451 |
italic := emphasis == #italic.
|
|
452 |
] ifFalse:[
|
|
453 |
emphasis isNil ifTrue:[
|
|
454 |
bold := italic := false
|
|
455 |
] ifFalse:[
|
|
456 |
bold := emphasis includesIdentical:#bold.
|
|
457 |
italic := emphasis includesIdentical:#italic.
|
|
458 |
]
|
|
459 |
].
|
|
460 |
|
|
461 |
bold ifTrue:[
|
|
462 |
boldFont isNil ifTrue:[
|
|
463 |
boldFont := savedFont asBold on:aGC device
|
|
464 |
].
|
|
465 |
f := boldFont.
|
|
466 |
] ifFalse:[
|
|
467 |
italic ifTrue:[
|
|
468 |
italicFont isNil ifTrue:[
|
|
469 |
italicFont := savedFont asItalic on:aGC device
|
|
470 |
].
|
|
471 |
f := italicFont
|
|
472 |
] ifFalse:[
|
|
473 |
f := savedFont
|
|
474 |
]
|
|
475 |
].
|
|
476 |
l := l + (f widthOf:string from:pos to:(pos + len - 1)).
|
|
477 |
pos := pos + len
|
|
478 |
].
|
|
479 |
|
|
480 |
^ l
|
|
481 |
|
|
482 |
"Modified: 12.5.1996 / 11:21:09 / cg"
|
|
483 |
! !
|
|
484 |
|
|
485 |
!Text methodsFor:'replacing'!
|
|
486 |
|
|
487 |
replaceFrom:start to:stop with:aCollection startingAt:startIndex
|
|
488 |
|idx|
|
|
489 |
|
|
490 |
aCollection isString ifTrue:[
|
|
491 |
aCollection hasChangeOfEmphasis ifTrue:[
|
|
492 |
string replaceFrom:start to:stop with:aCollection startingAt:startIndex.
|
|
493 |
idx := startIndex.
|
|
494 |
start to:stop do:[:col |
|
|
495 |
self emphasisAt:col put:(aCollection emphasisAt:idx).
|
|
496 |
idx := idx + 1.
|
|
497 |
].
|
|
498 |
^ self.
|
|
499 |
]
|
|
500 |
].
|
|
501 |
|
|
502 |
string replaceFrom:start to:stop with:aCollection startingAt:startIndex.
|
|
503 |
self emphasizeFrom:start to:stop with:nil
|
|
504 |
|
|
505 |
"
|
|
506 |
((Text string:'hello') allBold emphasisAt:2 put:#italic)
|
|
507 |
copyFrom:1 to:3
|
|
508 |
"
|
|
509 |
|
|
510 |
"Modified: 12.5.1996 / 12:44:53 / cg"
|
304
|
511 |
! !
|
|
512 |
|
|
513 |
!Text class methodsFor:'documentation'!
|
|
514 |
|
|
515 |
version
|
307
|
516 |
^ '$Header: /cvs/stx/stx/libbasic2/Text.st,v 1.2 1996-05-12 14:01:23 cg Exp $'
|
304
|
517 |
! !
|