BitmapFont.st
changeset 1088 136b1b7996a0
child 1154 07bc33341696
equal deleted inserted replaced
1087:1e5a93e03d7f 1088:136b1b7996a0
       
     1 "
       
     2  COPYRIGHT (c) 1994 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  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 FontDescription subclass:#BitmapFont
       
    28 	instanceVariableNames:'characterBitmaps ascent descent width'
       
    29 	classVariableNames:''
       
    30 	poolDictionaries:''
       
    31 	category:'Graphics-Support'
       
    32 !
       
    33 
       
    34 !BitmapFont class methodsFor:'documentation'!
       
    35 
       
    36 copyright
       
    37 "
       
    38  COPYRIGHT (c) 1994 by Claus Gittinger
       
    39 	      All Rights Reserved
       
    40 
       
    41  This software is furnished under a license and may be used
       
    42  only in accordance with the terms of that license and with the
       
    43  inclusion of the above copyright notice.   This software may not
       
    44  be provided or otherwise made available to, or used by, any
       
    45  other person.  No title to or ownership of the software is
       
    46  hereby transferred.
       
    47 
       
    48  This is a demo example:
       
    49 
       
    50  THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
       
    51  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    52  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    53  ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
       
    54  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    55  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
       
    56  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    57  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
       
    58  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
       
    59  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
       
    60  SUCH DAMAGE.
       
    61 "
       
    62 !
       
    63 
       
    64 documentation
       
    65 "
       
    66     This class demonstrates, that it is possible to define your own
       
    67     renderers for fonts - you could even write a class which reads 
       
    68     a truetype font from a ttf file and display those 
       
    69     (maybe someone finds the time to do this 
       
    70      and provides the code to the public domain ?)
       
    71 
       
    72     Here is a simple & sample implementation of private bitmap fonts;
       
    73     Glyphs for each character are stored in the instance variable
       
    74     'characterBitmaps'.
       
    75     Some sample glyphs can be created with the class' sampleGlyphs method.
       
    76     The required protocol is found in drawing and accessing.
       
    77 "
       
    78 !
       
    79 
       
    80 examples
       
    81 "
       
    82   a label showing characters in this new bitmap font:
       
    83 
       
    84     |font l|
       
    85 
       
    86     font := (BitmapFont new glyphs:(BitmapFont sampleGlyhps)).
       
    87     font setAscent:13; setDescent:3.
       
    88 
       
    89     l := Label new.
       
    90     l font:font.
       
    91     l label:'aazzazaz'.
       
    92     l open.
       
    93 
       
    94 
       
    95   a label showing characters in a new smily font:
       
    96 
       
    97     |font l|
       
    98 
       
    99     font := (BitmapFont new glyphs:(BitmapFont smilyGlyhps)).
       
   100     font setAscent:16; setDescent:0.
       
   101 
       
   102     l := Label new.
       
   103     l font:font.
       
   104     l label:'aabbaaa'.
       
   105     l open.
       
   106 
       
   107 
       
   108   demonstrate, that this font can be used in textViews just as any other font:
       
   109   (well, missing character glyphs are blanked)
       
   110 
       
   111     |font top list|
       
   112 
       
   113     font := (BitmapFont new glyphs:(BitmapFont sampleGlyhps)).
       
   114     font setAscent:13; setDescent:3.
       
   115 
       
   116     top := ScrollableView forView:(list := SelectionInListView new).
       
   117     list font:font.
       
   118     list list:#('a' 'z' 'aaa' 'zzz' 'azaz' 'zaza' 'aa' 'az' 'za' 'hello' 'abcdef' 'xyz').
       
   119     top extent:200@200.
       
   120     top open.
       
   121 
       
   122   demonstrate, that this font can be used in textViews just as any other font:
       
   123   (well, missing character glyphs are blanked)
       
   124 
       
   125     |font top list|
       
   126 
       
   127     font := (BitmapFont new glyphs:(BitmapFont sampleGlyhps)).
       
   128     font setAscent:13; setDescent:3.
       
   129 
       
   130     top := ScrollableView forView:(list := EditTextView new).
       
   131     list font:font.
       
   132     list list:#('a' 'z' 'aaa' 'zzz' 'azaz' 'zaza' 'aa' 'az' 'za' 'hello' 'abcdef' 'xyz').
       
   133     top extent:200@200.
       
   134     top open.
       
   135 "
       
   136 ! !
       
   137 
       
   138 !BitmapFont class methodsFor:'instance creation'!
       
   139 
       
   140 new
       
   141     |newFont|
       
   142 
       
   143     newFont := super new 
       
   144 		  family:'private' 
       
   145 		  face:nil
       
   146 		  style:nil
       
   147 		  size:nil
       
   148 		  encoding:nil. 
       
   149 
       
   150     ^ newFont
       
   151 
       
   152     "
       
   153      BitmapFont new glyphs:(self sampleGlyhps).
       
   154     "
       
   155 ! !
       
   156 
       
   157 !BitmapFont class methodsFor:'private'!
       
   158 
       
   159 sampleGlyhps
       
   160     "return the bitmap array for a sample font
       
   161      (only contains glyphs for $a and $z)"
       
   162 
       
   163     |characters|
       
   164 
       
   165     characters := Array new:256.
       
   166     characters 
       
   167 	at:(Character space asciiValue + 1) 
       
   168 	put:(Form 
       
   169 		width:16 
       
   170 		height:16 
       
   171 		fromArray:#[2r00000000 2r00000000
       
   172 			    2r00000000 2r00000000
       
   173 			    2r00000000 2r00000000
       
   174 			    2r00000000 2r00000000
       
   175 			    2r00000000 2r00000000
       
   176 			    2r00000000 2r00000000
       
   177 			    2r00000000 2r00000000
       
   178 			    2r00000000 2r00000000
       
   179 			    2r00000000 2r00000000
       
   180 			    2r00000000 2r00000000
       
   181 			    2r00000000 2r00000000
       
   182 			    2r00000000 2r00000000
       
   183 			    2r00000000 2r00000000
       
   184 			    2r00000000 2r00000000
       
   185 			    2r00000000 2r00000000
       
   186 			    2r00000000 2r00000000]).
       
   187 
       
   188     characters 
       
   189 	at:($a asciiValue + 1) 
       
   190 	put:(Form 
       
   191 		width:16 
       
   192 		height:16 
       
   193 		fromArray:#[2r00000000 2r00000000
       
   194 			    2r00000000 2r00000000
       
   195 			    2r00000000 2r00000000
       
   196 			    2r00011111 2r11111000
       
   197 			    2r00011111 2r11111000
       
   198 			    2r00011000 2r00011000
       
   199 			    2r00011000 2r00011000
       
   200 			    2r00011000 2r00011000
       
   201 			    2r00011000 2r00011000
       
   202 			    2r00011000 2r00011000
       
   203 			    2r00011000 2r00011000
       
   204 			    2r00011111 2r11111110
       
   205 			    2r00011111 2r11111110
       
   206 			    2r00000000 2r00000000
       
   207 			    2r00000000 2r00000000
       
   208 			    2r00000000 2r00000000]).
       
   209 
       
   210     characters 
       
   211 	at:($z asciiValue + 1) 
       
   212 	put:(Form 
       
   213 		width:16 
       
   214 		height:16 
       
   215 		fromArray:#[2r00000000 2r00000000
       
   216 			    2r00000000 2r00000000
       
   217 			    2r00000000 2r00000000
       
   218 			    2r00011111 2r11111000
       
   219 			    2r00011111 2r11111000
       
   220 			    2r00000000 2r00110000
       
   221 			    2r00000000 2r01100000
       
   222 			    2r00011111 2r11111000
       
   223 			    2r00011111 2r11111000
       
   224 			    2r00000110 2r00000000
       
   225 			    2r00001100 2r00000000
       
   226 			    2r00011111 2r11111000
       
   227 			    2r00011111 2r11111000
       
   228 			    2r00000000 2r00000000
       
   229 			    2r00000000 2r00000000
       
   230 			    2r00000000 2r00000000]).
       
   231 
       
   232     ^ characters
       
   233 !
       
   234 
       
   235 smilyGlyhps
       
   236     "return the bitmap array for a smily font
       
   237      (only contains glyphs for $a and $b)"
       
   238 
       
   239     |characters|
       
   240 
       
   241     characters := Array new:256.
       
   242     characters 
       
   243 	at:(Character space asciiValue + 1) 
       
   244 	put:(Form 
       
   245 		width:16 
       
   246 		height:16 
       
   247 		fromArray:#[2r00000000 2r00000000
       
   248 			    2r00000000 2r00000000
       
   249 			    2r00000000 2r00000000
       
   250 			    2r00000000 2r00000000
       
   251 			    2r00000000 2r00000000
       
   252 			    2r00000000 2r00000000
       
   253 			    2r00000000 2r00000000
       
   254 			    2r00000000 2r00000000
       
   255 			    2r00000000 2r00000000
       
   256 			    2r00000000 2r00000000
       
   257 			    2r00000000 2r00000000
       
   258 			    2r00000000 2r00000000
       
   259 			    2r00000000 2r00000000
       
   260 			    2r00000000 2r00000000
       
   261 			    2r00000000 2r00000000
       
   262 			    2r00000000 2r00000000]).
       
   263 
       
   264     characters 
       
   265 	at:($a asciiValue + 1) 
       
   266 	put:(Form 
       
   267 		width:16 
       
   268 		height:16 
       
   269 		fromArray:#[2r00000001 2r10000000
       
   270 			    2r00001110 2r01110000
       
   271 			    2r00011000 2r00011000
       
   272 			    2r00100000 2r00000100
       
   273 			    2r01100110 2r01100110
       
   274 			    2r01000110 2r01100010
       
   275 			    2r01000000 2r00000010
       
   276 			    2r10000001 2r00000001
       
   277 			    2r10000001 2r00000001
       
   278 			    2r01001000 2r00010010
       
   279 			    2r01001100 2r00110010
       
   280 			    2r01100111 2r11100110
       
   281 			    2r00100001 2r10000100
       
   282 			    2r00011000 2r00011000
       
   283 			    2r00001110 2r01110000
       
   284 			    2r00000001 2r10000000]).
       
   285 
       
   286     characters 
       
   287 	at:($b asciiValue + 1) 
       
   288 	put:(Form 
       
   289 		width:16 
       
   290 		height:16 
       
   291 		fromArray:#[2r00000001 2r10000000
       
   292 			    2r00001110 2r01110000
       
   293 			    2r00011000 2r00011000
       
   294 			    2r00100000 2r00000100
       
   295 			    2r01100110 2r01100110
       
   296 			    2r01000110 2r01100010
       
   297 			    2r01000000 2r00000010
       
   298 			    2r10000001 2r00000001
       
   299 			    2r10000001 2r00000001
       
   300 			    2r01000000 2r00000010
       
   301 			    2r01000001 2r10000010
       
   302 			    2r01100010 2r01000110
       
   303 			    2r00100010 2r01000100
       
   304 			    2r00011000 2r00011000
       
   305 			    2r00001110 2r01110000
       
   306 			    2r00000001 2r10000000]).
       
   307 
       
   308     characters 
       
   309 	at:($c asciiValue + 1) 
       
   310 	put:(Form 
       
   311 		width:16 
       
   312 		height:16 
       
   313 		fromArray:#[2r00000001 2r10000000
       
   314 			    2r00001110 2r01110000
       
   315 			    2r00011000 2r00011000
       
   316 			    2r00100000 2r00000100
       
   317 			    2r01100110 2r01100110
       
   318 			    2r01000110 2r01100010
       
   319 			    2r01000000 2r00000010
       
   320 			    2r10000001 2r00000001
       
   321 			    2r10000001 2r00000001
       
   322 			    2r01000000 2r00000010
       
   323 			    2r01000001 2r10000010
       
   324 			    2r01100011 2r11000110
       
   325 			    2r00100011 2r11000100
       
   326 			    2r00011001 2r10011000
       
   327 			    2r00001110 2r01110000
       
   328 			    2r00000001 2r10000000]).
       
   329 
       
   330     ^ characters
       
   331 ! !
       
   332 
       
   333 !BitmapFont methodsFor:'accessing'!
       
   334 
       
   335 glyphs:aGlyphArray
       
   336     characterBitmaps := aGlyphArray.
       
   337     width isNil ifTrue:[
       
   338 	width := aGlyphArray 
       
   339 			inject:0 
       
   340 			into:[:max :glyph | glyph isNil ifTrue:[
       
   341 						max
       
   342 					    ] ifFalse:[
       
   343 						max max:glyph width
       
   344 					    ]
       
   345 			     ]
       
   346     ].
       
   347 !
       
   348 
       
   349 setAscent:aNumber
       
   350     ascent := aNumber.
       
   351 !
       
   352 
       
   353 setDescent:aNumber
       
   354     descent := aNumber.
       
   355 ! !
       
   356 
       
   357 !BitmapFont methodsFor:'drawing'!
       
   358 
       
   359 displayOpaqueString:aString from:index1 to:index2 x:x0 y:y in:aGC
       
   360     |x|
       
   361 
       
   362     x := x0.
       
   363     index1 to:index2 do:[:index |
       
   364 	self drawCharacter:(aString at:index) asciiValue in:aGC x:x y:y opaque:true.
       
   365 	x := x + (self widthOfCharacter:(aString at:index) asciiValue)
       
   366     ]
       
   367 !
       
   368 
       
   369 displayOpaqueString:aString x:x0 y:y in:aGC
       
   370     |x|
       
   371 
       
   372     x := x0.
       
   373     aString do:[:character |
       
   374 	self drawCharacter:character asciiValue in:aGC x:x y:y opaque:true.
       
   375 	x := x + (self widthOfCharacter:character asciiValue)
       
   376     ]
       
   377 !
       
   378 
       
   379 displayString:aString from:index1 to:index2 x:x0 y:y in:aGC
       
   380     |x|
       
   381 
       
   382     x := x0.
       
   383     index1 to:index2 do:[:index |
       
   384 	self drawCharacter:(aString at:index) asciiValue in:aGC x:x y:y opaque:false.
       
   385 	x := x + (self widthOfCharacter:(aString at:index) asciiValue)
       
   386     ]
       
   387 !
       
   388 
       
   389 displayString:aString x:x0 y:y in:aGC
       
   390     |x|
       
   391 
       
   392     x := x0.
       
   393     aString do:[:character |
       
   394 	self drawCharacter:character asciiValue in:aGC x:x y:y opaque:false.
       
   395 	x := x + (self widthOfCharacter:character asciiValue)
       
   396     ]
       
   397 !
       
   398 
       
   399 drawCharacter:ascii in:aGC x:x y:y
       
   400     |glyph|
       
   401 
       
   402     (ascii between:0 and:255) ifFalse:[^ self].
       
   403     glyph := characterBitmaps at:(ascii + 1).
       
   404     glyph isNil ifTrue:[^ self].
       
   405     aGC displayForm:glyph x:x y:y-ascent
       
   406 !
       
   407 
       
   408 drawCharacter:ascii in:aGC x:x y:y opaque:opaque
       
   409     |glyph|
       
   410 
       
   411     (ascii between:0 and:255) ifFalse:[^ self].
       
   412     glyph := characterBitmaps at:(ascii + 1).
       
   413     glyph isNil ifTrue:[^ self].
       
   414     aGC displayForm:glyph x:x y:y-ascent
       
   415 ! !
       
   416 
       
   417 !BitmapFont methodsFor:'queries'!
       
   418 
       
   419 ascent
       
   420     "return the ascent - the number of pixels above the baseLine"
       
   421 
       
   422     ^ ascent
       
   423 !
       
   424 
       
   425 ascentOn:aDevice
       
   426     "return the ascent - the number of pixels above the baseLine"
       
   427 
       
   428     ^ ascent
       
   429 !
       
   430 
       
   431 descent
       
   432     "return the descent - the number of pixels below the baseLine"
       
   433 
       
   434     ^ descent
       
   435 !
       
   436 
       
   437 descentOn:aDevice
       
   438     "return the descent - the number of pixels below the baseLine"
       
   439 
       
   440     ^ descent
       
   441 !
       
   442 
       
   443 fontId
       
   444     "return the fonts device ID - here, there is none"
       
   445 
       
   446     ^ nil
       
   447 !
       
   448 
       
   449 height
       
   450     "return the height - the height in pixels of the highest character"
       
   451 
       
   452     ^ descent + ascent.
       
   453 !
       
   454 
       
   455 heightOf:aString
       
   456     "return the height - the height in pixels of the highest character"
       
   457 
       
   458     ^ descent + ascent.
       
   459 !
       
   460 
       
   461 heightOfCharacter:ascii
       
   462     "return the height of a specific character"
       
   463 
       
   464     |glyph|
       
   465 
       
   466     (ascii between:0 and:255) ifFalse:[^ 0].
       
   467     glyph := characterBitmaps at:(ascii + 1).
       
   468     glyph isNil ifTrue:[
       
   469 	glyph := characterBitmaps at:(Character space asciiValue + 1).
       
   470     ].
       
   471     glyph isNil ifTrue:[^ 0].
       
   472     ^ glyph height
       
   473 !
       
   474 
       
   475 heightOn:aDevice
       
   476     "return the height - the height in pixels of the highest character"
       
   477 
       
   478     ^ descent + ascent.
       
   479 !
       
   480 
       
   481 isFixedWidth
       
   482     "return true if all of the fonts characters are equal in
       
   483      width."
       
   484 
       
   485     ^ true
       
   486 !
       
   487 
       
   488 maxAscent
       
   489     ^ ascent.
       
   490 !
       
   491 
       
   492 maxHeight
       
   493     ^ descent + ascent.
       
   494 !
       
   495 
       
   496 on:aDevice
       
   497     "return a device representation of the receiver"
       
   498 
       
   499     ^ self
       
   500 !
       
   501 
       
   502 width 
       
   503     "return the width - the average width in pixels"
       
   504 
       
   505     ^ width
       
   506 !
       
   507 
       
   508 widthOf:aString
       
   509     "return the width of a string"
       
   510 
       
   511     |sumW|
       
   512 
       
   513     sumW := 0.
       
   514     aString do:[:character |
       
   515 	sumW := sumW + (self widthOfCharacter:character asciiValue) 
       
   516     ].
       
   517     ^ sumW
       
   518 !
       
   519 
       
   520 widthOf:aString from:start to:stop
       
   521     "return the width of a substring"
       
   522 
       
   523     |sumW|
       
   524 
       
   525     (stop < start) ifTrue:[^ 0].
       
   526     sumW := 0.
       
   527     start to:stop do:[:index |
       
   528 	sumW := sumW + (self widthOfCharacter:(aString at:index) asciiValue) 
       
   529     ].
       
   530     ^ sumW
       
   531 !
       
   532 
       
   533 widthOfCharacter:ascii
       
   534     "return the width of a specific character"
       
   535 
       
   536     |glyph|
       
   537 
       
   538     (ascii between:0 and:255) ifFalse:[^ 0].
       
   539     glyph := characterBitmaps at:(ascii + 1).
       
   540     glyph isNil ifTrue:[
       
   541 	glyph := characterBitmaps at:(Character space asciiValue + 1).
       
   542     ].
       
   543     glyph isNil ifTrue:[^ 0].
       
   544     ^ glyph width
       
   545 !
       
   546 
       
   547 widthOn:aDevice
       
   548     "return the width - the average width in pixels"
       
   549 
       
   550     ^ width
       
   551 ! !
       
   552 
       
   553 !BitmapFont class methodsFor:'documentation'!
       
   554 
       
   555 version
       
   556     ^ '$Header: /cvs/stx/stx/libview/BitmapFont.st,v 1.1 1996-10-22 21:47:28 cg Exp $'
       
   557 ! !