2547
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
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 |
This is a demo example:
|
|
13 |
|
|
14 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
|
|
15 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
16 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
17 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
|
|
18 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
19 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
20 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
21 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
22 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
23 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
24 |
SUCH DAMAGE.
|
|
25 |
"
|
|
26 |
|
|
27 |
|
3659
|
28 |
"{ Package: 'stx:libview' }"
|
|
29 |
|
2546
|
30 |
FontDescription subclass:#CompoundFont
|
3660
|
31 |
instanceVariableNames:'baseFont characterToFontMapping maxAscent maxDescent maxHeight
|
|
32 |
device'
|
|
33 |
classVariableNames:''
|
|
34 |
poolDictionaries:''
|
|
35 |
category:'Graphics-Support'
|
2546
|
36 |
!
|
|
37 |
|
|
38 |
!CompoundFont class methodsFor:'documentation'!
|
|
39 |
|
2547
|
40 |
copyright
|
|
41 |
"
|
|
42 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
43 |
All Rights Reserved
|
|
44 |
|
|
45 |
This software is furnished under a license and may be used
|
|
46 |
only in accordance with the terms of that license and with the
|
|
47 |
inclusion of the above copyright notice. This software may not
|
|
48 |
be provided or otherwise made available to, or used by, any
|
|
49 |
other person. No title to or ownership of the software is
|
|
50 |
hereby transferred.
|
|
51 |
|
|
52 |
This is a demo example:
|
|
53 |
|
|
54 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
|
|
55 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
56 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
57 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
|
|
58 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
59 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
60 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
61 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
62 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
63 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
64 |
SUCH DAMAGE.
|
|
65 |
"
|
|
66 |
|
|
67 |
!
|
|
68 |
|
2546
|
69 |
documentation
|
|
70 |
"
|
|
71 |
a CompountFont is a font which consists of character glyphs from multiple
|
|
72 |
other (base-)fonts; for each character code, an individual font may be given.
|
|
73 |
|
|
74 |
This has been mostly added to allow for non-EURO fonts to be used with ST/X,
|
|
75 |
by defining a mixedFont, which has an EURO-glyph at the desired character
|
|
76 |
position.
|
|
77 |
Do not hardCode usage of MixedFonts into your application, since they
|
|
78 |
might disappear in the future (when Unicode support has been fully
|
|
79 |
implemented in ST/X, and Unicode fonts are generally available under X).
|
|
80 |
I.e. to use these fonts, add appropriate setup to the styleSheet,
|
|
81 |
or private.rc and use those fonts transparently.
|
|
82 |
|
|
83 |
[Instance variables:]
|
|
84 |
|
|
85 |
baseFont <Font> fallback (default-) font
|
|
86 |
characterToFontMapping <Dictionary> maps characters to a fonts
|
|
87 |
|
|
88 |
[class variables:]
|
|
89 |
|
|
90 |
[see also:]
|
|
91 |
Font BitmapFont
|
|
92 |
DeviceDrawable GraphicsContext
|
|
93 |
|
|
94 |
[author:]
|
|
95 |
Claus Gittinger
|
|
96 |
"
|
|
97 |
|
|
98 |
|
|
99 |
!
|
|
100 |
|
|
101 |
examples
|
|
102 |
"
|
|
103 |
a mixed font; all vowels are displayed in times;
|
|
104 |
the rest in helvetica.
|
|
105 |
[exBegin]
|
|
106 |
|font top list|
|
|
107 |
|
3659
|
108 |
font := CompoundFont basedOn:(Font family:'courier' size:18).
|
2546
|
109 |
#($a $e $i $o $u) do:[:char |
|
3659
|
110 |
font glyphAt:char putFont:(Font family:'times' size:18).
|
2546
|
111 |
].
|
|
112 |
top := ScrollableView forView:(list := EditTextView new).
|
|
113 |
list font:font.
|
|
114 |
list list:#('a' 'z' 'aaa' 'zzz' 'azaz' 'zaza' 'aa' 'az' 'za' 'hello' 'abcdef' 'xyz').
|
|
115 |
top extent:200@200.
|
|
116 |
top open.
|
|
117 |
[exEnd]
|
|
118 |
|
|
119 |
|
|
120 |
a mixed font; the dollar character is replaced by the european
|
|
121 |
EURO symbol; the rest is helvetica
|
|
122 |
(this is a hack - we really need a resizable font for this):
|
|
123 |
|
|
124 |
[exBegin]
|
|
125 |
|font baseFont euroGlyph glyphs euroFont top list|
|
|
126 |
|
|
127 |
baseFont := Font family:'helvetica' size:12.
|
|
128 |
baseFont := baseFont onDevice:Display.
|
|
129 |
glyphs := Array new:256.
|
|
130 |
euroGlyph := Form
|
3659
|
131 |
width:12
|
2546
|
132 |
height:16
|
|
133 |
fromArray:#(
|
|
134 |
2r00000000 2r00000000
|
|
135 |
2r00000000 2r00000000
|
|
136 |
2r00000000 2r00000000
|
|
137 |
2r00000111 2r11000000
|
|
138 |
2r00001000 2r00100000
|
|
139 |
2r00010000 2r00000000
|
|
140 |
2r01111111 2r10000000
|
|
141 |
2r00010000 2r00000000
|
|
142 |
2r01111111 2r10000000
|
|
143 |
2r00010000 2r00000000
|
|
144 |
2r00001000 2r00100000
|
|
145 |
2r00000111 2r11000000
|
|
146 |
2r00000000 2r00000000
|
|
147 |
2r00000000 2r00000000
|
3659
|
148 |
2r00000000 2r00000000
|
|
149 |
2r00000000 2r00000000
|
2546
|
150 |
).
|
|
151 |
glyphs at:($$ asciiValue+1) put:euroGlyph.
|
|
152 |
|
|
153 |
euroFont := BitmapFont new glyphs:glyphs.
|
|
154 |
euroFont setAscent:(baseFont ascent).
|
|
155 |
euroFont setDescent:(baseFont descent).
|
|
156 |
|
|
157 |
font := CompoundFont basedOn:baseFont.
|
|
158 |
font glyphAt:$$ putFont:euroFont.
|
|
159 |
|
|
160 |
top := ScrollableView forView:(list := EditTextView new).
|
|
161 |
list font:font.
|
|
162 |
list list:#('100 $' '193 DM').
|
|
163 |
top extent:200@200.
|
|
164 |
top open.
|
|
165 |
[exEnd]
|
|
166 |
"
|
|
167 |
! !
|
|
168 |
|
|
169 |
!CompoundFont class methodsFor:'instance creation'!
|
|
170 |
|
|
171 |
basedOn:aRealFont
|
|
172 |
^ self new baseFont:aRealFont
|
|
173 |
! !
|
|
174 |
|
|
175 |
!CompoundFont methodsFor:'accessing'!
|
|
176 |
|
|
177 |
baseFont
|
|
178 |
"return the value of the instance variable 'baseFont' (automatically generated)"
|
|
179 |
|
3659
|
180 |
^ baseFont
|
|
181 |
!
|
2546
|
182 |
|
|
183 |
baseFont:something
|
|
184 |
"set the value of the instance variable 'baseFont' (automatically generated)"
|
|
185 |
|
|
186 |
baseFont := something.
|
|
187 |
maxAscent := maxDescent := nil.
|
|
188 |
!
|
|
189 |
|
|
190 |
glyphAt:char putFont:aFont
|
|
191 |
characterToFontMapping isNil ifTrue:[
|
|
192 |
characterToFontMapping := Dictionary new.
|
|
193 |
].
|
|
194 |
|
|
195 |
characterToFontMapping at:char put:aFont.
|
|
196 |
maxAscent := maxDescent := nil.
|
|
197 |
|
3660
|
198 |
!
|
|
199 |
|
|
200 |
graphicsDevice
|
|
201 |
"return the device I am on"
|
|
202 |
|
|
203 |
^ device
|
2546
|
204 |
! !
|
|
205 |
|
|
206 |
!CompoundFont methodsFor:'displaying'!
|
|
207 |
|
|
208 |
displayOpaqueString:aString from:index1 to:index2 x:x0 y:y0 in:aGC
|
|
209 |
"this is only called for fonts which have a nil fontId,
|
|
210 |
and therefore use the replacementFont. Should never be called
|
|
211 |
for non-replacement fonts."
|
|
212 |
|
|
213 |
|x y|
|
|
214 |
|
|
215 |
x := x0.
|
|
216 |
y := y0.
|
|
217 |
|
|
218 |
self
|
|
219 |
substringPartsOf:aString from:index1 to:index2
|
|
220 |
do:[:s :i1 :i2 :font |
|
|
221 |
font displayOpaqueString:s from:i1 to:i2 x:x y:y in:aGC.
|
|
222 |
x := x + (font widthOf:aString from:i1 to:i2).
|
|
223 |
].
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
!
|
|
228 |
|
|
229 |
displayString:aString from:index1 to:index2 x:x0 y:y0 in:aGC
|
|
230 |
"this is only called for fonts which have a nil fontId,
|
|
231 |
and therefore use the replacementFont. Should never be called
|
|
232 |
for non-replacement fonts."
|
|
233 |
|
|
234 |
|x y|
|
|
235 |
|
|
236 |
x := x0.
|
|
237 |
y := y0.
|
|
238 |
|
|
239 |
self
|
|
240 |
substringPartsOf:aString from:index1 to:index2
|
|
241 |
do:[:s :i1 :i2 :font |
|
|
242 |
font displayString:s from:i1 to:i2 x:x y:y in:aGC.
|
|
243 |
x := x + (font widthOf:aString from:i1 to:i2).
|
|
244 |
].
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
! !
|
|
249 |
|
|
250 |
!CompoundFont methodsFor:'private'!
|
|
251 |
|
|
252 |
computeMaxBounds
|
|
253 |
maxAscent := baseFont ascent.
|
|
254 |
maxDescent := baseFont descent.
|
3660
|
255 |
maxHeight := baseFont height.
|
2546
|
256 |
characterToFontMapping notNil ifTrue:[
|
|
257 |
characterToFontMapping keysAndValuesDo:[:char :aFont |
|
|
258 |
maxAscent := maxAscent max:aFont ascent.
|
|
259 |
maxDescent := maxDescent max:aFont descent.
|
3660
|
260 |
maxHeight := maxHeight max:aFont height.
|
2546
|
261 |
]
|
|
262 |
].
|
|
263 |
!
|
|
264 |
|
|
265 |
substringPartsOf:aString from:index1 to:index2 do:aBlock
|
|
266 |
"helper - evaluate aBlock for parts of a string, which use the same font.
|
|
267 |
aBlock is invoked for consecutive substrings, passing the string,
|
|
268 |
the startIndex, endIndex and the font as arguments."
|
|
269 |
|
|
270 |
|i1 i2 fn char currentFont|
|
|
271 |
|
|
272 |
index2 < index1 ifTrue:[^ self].
|
|
273 |
|
|
274 |
i1 := index1.
|
|
275 |
currentFont := characterToFontMapping at:(aString at:i1) ifAbsent:baseFont.
|
|
276 |
i2 := i1 + 1.
|
|
277 |
|
|
278 |
[i2 <= index2] whileTrue:[
|
|
279 |
char := aString at:i2.
|
|
280 |
fn := characterToFontMapping at:char ifAbsent:baseFont.
|
|
281 |
fn ~~ currentFont ifTrue:[
|
|
282 |
aBlock value:aString value:i1 value:(i2-1) value:currentFont.
|
|
283 |
currentFont := fn.
|
|
284 |
i1 := i2.
|
|
285 |
].
|
|
286 |
i2 := i2 + 1.
|
|
287 |
].
|
|
288 |
|
|
289 |
i1 < i2 ifTrue:[
|
|
290 |
aBlock value:aString value:i1 value:(i2-1) value:currentFont.
|
|
291 |
].
|
|
292 |
|
|
293 |
"
|
|
294 |
|f|
|
|
295 |
|
|
296 |
f := self new.
|
|
297 |
f baseFont:#baseFont.
|
|
298 |
f fontAt:$$ put:#font2.
|
|
299 |
f substringPartsOf:'ae$a' from:1 to:8
|
|
300 |
do:[:s :i1 :i2 :f |
|
|
301 |
Transcript
|
|
302 |
show:i1; space;
|
|
303 |
show:i2; space;
|
|
304 |
showCR:f
|
|
305 |
]
|
|
306 |
"
|
|
307 |
! !
|
|
308 |
|
|
309 |
!CompoundFont methodsFor:'queries'!
|
|
310 |
|
|
311 |
ascent
|
|
312 |
maxAscent isNil ifTrue:[
|
|
313 |
self computeMaxBounds
|
|
314 |
].
|
|
315 |
^ maxAscent
|
|
316 |
!
|
|
317 |
|
|
318 |
descent
|
|
319 |
maxDescent isNil ifTrue:[
|
|
320 |
self computeMaxBounds
|
|
321 |
].
|
|
322 |
^ maxDescent
|
|
323 |
!
|
|
324 |
|
3660
|
325 |
height
|
|
326 |
maxHeight isNil ifTrue:[
|
|
327 |
self computeMaxBounds
|
|
328 |
].
|
|
329 |
^ maxHeight.
|
|
330 |
!
|
|
331 |
|
2546
|
332 |
isFixedWidth
|
|
333 |
"return true, if this is a fixed pitch font (i.e. all characters
|
|
334 |
are of the same width)"
|
|
335 |
|
|
336 |
baseFont isFixedWidth ifFalse:[^ false].
|
|
337 |
characterToFontMapping keysAndValuesDo:[:char :font |
|
|
338 |
font isFixedWidth ifFalse:[^ false].
|
|
339 |
].
|
|
340 |
^ true
|
|
341 |
|
|
342 |
!
|
|
343 |
|
|
344 |
maxAscent
|
|
345 |
^ self ascent
|
|
346 |
!
|
|
347 |
|
|
348 |
maxDescent
|
|
349 |
^ self descent
|
|
350 |
!
|
|
351 |
|
3659
|
352 |
onDevice:aDevice
|
2546
|
353 |
"return a device representation of the receiver.
|
|
354 |
Since I am device independent, return the receiver."
|
|
355 |
|
3660
|
356 |
|newFonts lastFont lastDeviceFont newFont|
|
|
357 |
|
|
358 |
aDevice == device ifTrue:[ ^ self ].
|
2546
|
359 |
|
|
360 |
baseFont := baseFont onDevice:aDevice.
|
|
361 |
newFonts := Dictionary new.
|
|
362 |
characterToFontMapping keysAndValuesDo:[:char :font |
|
3660
|
363 |
font == lastFont ifTrue:[
|
|
364 |
newFont := lastDeviceFont
|
|
365 |
] ifFalse:[
|
|
366 |
newFont := font onDevice:aDevice.
|
|
367 |
lastFont := font.
|
|
368 |
lastDeviceFont := newFont.
|
|
369 |
].
|
|
370 |
newFonts at:char put:newFont
|
2546
|
371 |
].
|
3660
|
372 |
characterToFontMapping := newFonts.
|
|
373 |
device := aDevice.
|
2546
|
374 |
!
|
|
375 |
|
|
376 |
widthOf:aString from:index1 to:index2
|
|
377 |
|w|
|
|
378 |
|
|
379 |
w := 0.
|
|
380 |
self
|
|
381 |
substringPartsOf:aString from:index1 to:index2
|
|
382 |
do:[:s :i1 :i2 :f |
|
|
383 |
w := w + (f widthOf:s from:i1 to:i2)
|
|
384 |
].
|
|
385 |
^ w
|
|
386 |
|
|
387 |
! !
|
|
388 |
|
|
389 |
!CompoundFont class methodsFor:'documentation'!
|
|
390 |
|
|
391 |
version
|
3660
|
392 |
^ '$Header: /cvs/stx/stx/libview/CompoundFont.st,v 1.4 2002-07-23 19:33:48 mb Exp $'
|
2546
|
393 |
! !
|