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