HersheyFont.st
changeset 3501 f5a4a6ceaffc
parent 3500 256dfb6ac4df
child 3746 57a7dc5fde0c
equal deleted inserted replaced
3500:256dfb6ac4df 3501:f5a4a6ceaffc
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 1989 by Claus Gittinger
     4  COPYRIGHT (c) 1989 by Claus Gittinger
     3               All Rights Reserved
     5               All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
   126 !
   128 !
   127 
   129 
   128 documentation
   130 documentation
   129 "
   131 "
   130     HersheyFont provides (limited) support to draw characters
   132     HersheyFont provides (limited) support to draw characters
   131     from the hershey font data file 'hersh.oc'
   133     from the hershey font set.
   132     (which is available via ftp and must be aquired from elsewhere due to
   134 
   133      copyright restrictions - google for 'hersh.oc').
       
   134         
       
   135     This is a demo class - not maintained or meant for productive use.
   135     This is a demo class - not maintained or meant for productive use.
   136     Notice: this is a very old demo - it should be rewritten to honor
   136     Notice: this is a very old demo - it should be rewritten to honor
   137     the FontDescription protocol (see BitmapFont as a better example).
   137     the FontDescription protocol (see BitmapFont as a better example).
   138 
   138 
       
   139     Caveat: not completely correct; the ascent and descent data is wrong, which
       
   140     gives textView a little trouble when selecting. (see examples)
       
   141     We leave it as an exercise to the interested user to fix those.
       
   142 
       
   143     You need the data file 'hersh.oc' and the glyph data files
       
   144         astrol.hmp              astrology
       
   145         scripts.hmp             cursive
       
   146         cyrilc.hmp              cyrillic
       
   147         romans.hmp              futura.l
       
   148         romand.hmp              futura.m
       
   149         gotheng.hmp             gothic.eng
       
   150         gothger.hmp             gothic.ger
       
   151         gothita.hmp             gothic.ita
       
   152         greeks.hmp              greek
       
   153         marker.hmp              markers
       
   154         lowmat.hmp              math.low
       
   155         uppmat.hmp              math.upp
       
   156         meteo.hmp               meteorology
       
   157         music.hmp               music
       
   158         scriptc.hmp             script
       
   159         symbol.hmp              symbolic
       
   160         greekc.hmp              times.g
       
   161         italicc.hmp             times.i
       
   162         italict.hmp             times.ib
       
   163         romanc.hmp              times.r
       
   164         romant.hmp              times.rb
       
   165         japan.hmp               japanese
       
   166 
       
   167     (these are available via http/ftp and must be aquired from elsewhere due to
       
   168      copyright restrictions - google for 'hersh.oc' or search for hershey in github).
       
   169         
       
   170     You should install those files in some font folder 
       
   171     (see hersheyFontPath for a list of places where they are expected)
       
   172     
   139     [see also:]
   173     [see also:]
   140         BitmapFont FontDescription Font
   174         BitmapFont FontDescription Font
   141 
   175 
   142 
   176 
   143     Support for hershey fonts is based on a freeware cd-rom distribution 
   177     Support for hershey fonts is based on a freeware cd-rom distribution 
   199 "
   233 "
   200 !
   234 !
   201 
   235 
   202 examples
   236 examples
   203 "
   237 "
       
   238                                                                 [exBegin]
       
   239      |aView|
       
   240 
       
   241      aView := TextView new extent:400@400.
       
   242      aView openAndWait.
       
   243      aView font:(HersheyFont family:'hershey-gothic-german' face:'bold' style:'roman' size:12).
       
   244      aView contents:'
       
   245 
       
   246 Hello world
       
   247 
       
   248 Is this nice?'.
       
   249                                                                 [exEnd]
       
   250 
       
   251                                                                 [exBegin]
   204      |aView|
   252      |aView|
   205 
   253 
   206      aView := View new extent:500@200.
   254      aView := View new extent:500@200.
   207      aView openAndWait.
   255      aView openAndWait.
   208      aView backingStore:true.
   256      aView backingStore:true.
   315      HersheyFont showFont:(HersheyFont family:'hershey-gothic-english' 
   363      HersheyFont showFont:(HersheyFont family:'hershey-gothic-english' 
   316                                          face:'bold' 
   364                                          face:'bold' 
   317                                         style:'roman' 
   365                                         style:'roman' 
   318                                          size:24) in:aView
   366                                          size:24) in:aView
   319      ].
   367      ].
       
   368                                                                 [exEnd]
   320 "
   369 "
   321 ! !
   370 ! !
   322 
   371 
   323 !HersheyFont class methodsFor:'instance creation'!
   372 !HersheyFont class methodsFor:'instance creation'!
   324 
   373 
   366         ]
   415         ]
   367     ].
   416     ].
   368     ^ nil
   417     ^ nil
   369 
   418 
   370     "
   419     "
   371      HersheyFont family:'hershey-gothic-german' face:'medium' style:'roman' size:12
   420      HersheyFont family:'hershey-gothic-german' face:'bold' style:'roman' size:12
   372     "
   421     "
   373 
   422 
   374     "Modified: 16.1.1997 / 18:19:47 / cg"
   423     "Modified: 16.1.1997 / 18:19:47 / cg"
   375 ! !
   424 ! !
   376 
   425 
   523 
   572 
   524 hersheyFontPath
   573 hersheyFontPath
   525     "the directory, where the glyph data is found;
   574     "the directory, where the glyph data is found;
   526      you may have to modify this a bit ..."
   575      you may have to modify this a bit ..."
   527 
   576 
   528     ^ '/usr/local/fonts/hershey/data/hersh.oc'
   577     #(
   529 
   578         '/usr/local/fonts/hershey'
       
   579         '/usr/fonts/hershey'
       
   580         '~/fonts/hershey'
       
   581         '~/Library/fonts/hershey'
       
   582         '~/Fonts/hershey'
       
   583         '/Library/fonts/hershey'
       
   584         '~/Desktop/fonts/hershey'
       
   585         '~/Desktop/hershey'
       
   586         '~/Downloads/fonts/hershey'
       
   587     ) do:[:each |
       
   588         |fn|
       
   589         
       
   590         fn := each asFilename.
       
   591         (fn / 'data') exists ifTrue:[^ fn pathName].
       
   592     ].
       
   593     
       
   594     ^ '/usr/local/fonts/hershey'
       
   595 
       
   596     "
       
   597      self hersheyFontPath
       
   598     "
       
   599     
   530     "Modified: 16.1.1997 / 18:24:35 / cg"
   600     "Modified: 16.1.1997 / 18:24:35 / cg"
   531 !
   601 !
   532 
   602 
   533 name:aFileName family:family face:face style:style size:sz
   603 name:aFileName family:family face:face style:style size:sz
   534     "return a font with glyph-data from aFileName"
   604     "return a font with glyph-data from aFileName"
   543 !
   613 !
   544 
   614 
   545 readDataFile
   615 readDataFile
   546     |inStream b5 b3 chars moves glyphNo nPairs char1 char2 index|
   616     |inStream b5 b3 chars moves glyphNo nPairs char1 char2 index|
   547 
   617 
   548     inStream := FileStream readonlyFileNamed:(self hersheyFontPath).
   618     inStream := FileStream readonlyFileNamed:(self hersheyFontPath asFilename / 'data/hersh.oc').
   549     inStream isNil ifTrue:[
   619     inStream isNil ifTrue:[
   550         self error:'no glyph data file ''' , self hersheyFontPath , ''' found.'.
   620         self error:'no glyph data file ''' , self hersheyFontPath , ''' found.'.
   551         ^ self
   621         ^ self
   552     ].
   622     ].
   553 
   623 
   554     GlyphData isNil ifTrue:[
   624     GlyphData isNil ifTrue:[
   555         Transcript showCR:'reading hershey glyphs ...'.
   625         Transcript showCR:'reading hershey glyphs ...'.
   556 
   626 
   557         GlyphData := OrderedCollection new:4000; grow:4000.
   627         GlyphData := (OrderedCollection new:4000) grow:4000.
   558         b5 := String new:5.
   628         b5 := String new:5.
   559         b3 := String new:3.
   629         b3 := String new:3.
   560         [inStream atEnd] whileFalse:[
   630         [inStream atEnd] whileFalse:[
   561             chars := inStream nextBytes:5 into:b5.
   631             chars := inStream nextBytes:5 into:b5.
   562             glyphNo := Number readFromString:b5.
   632             glyphNo := Number readFromString:b5.
   589     "Modified: 16.1.1997 / 18:24:15 / cg"
   659     "Modified: 16.1.1997 / 18:24:15 / cg"
   590 ! !
   660 ! !
   591 
   661 
   592 !HersheyFont methodsFor:'drawing'!
   662 !HersheyFont methodsFor:'drawing'!
   593 
   663 
   594 displayString:aString from:index1 to:index2 x:x0 y:y in:aGC
   664 displayString:aString from:index1 to:index2 x:x0 y:y in:aGC opaque:opaque
   595     |x|
   665     |x|
   596 
   666 
   597     x := x0.
   667     x := x0.
   598     index1 to:index2 do:[:index |
   668     index1 to:index2 do:[:index |
   599 	self drawCharacter:(aString at:index) asciiValue in:aGC x:x y:y.
   669         self drawCharacter:(aString at:index) asciiValue in:aGC x:x y:y.
   600 	x := x + (self widthOfCharacter:(aString at:index) asciiValue)
   670         x := x + (self widthOfCharacter:(aString at:index) asciiValue)
   601     ]
   671     ]
   602 !
   672 !
   603 
   673 
   604 displayString:aString x:x0 y:y in:aGC
   674 displayString:aString x:x0 y:y in:aGC
   605     |x|
   675     |x|
   613 
   683 
   614 drawCharacter:ascii in:aGC x:x y:y
   684 drawCharacter:ascii in:aGC x:x y:y
   615     |glyphNo|
   685     |glyphNo|
   616 
   686 
   617     (ascii between:32 and:127) ifFalse:[^ self].
   687     (ascii between:32 and:127) ifFalse:[^ self].
       
   688     
   618     glyphNo := glyphs at:(ascii - 32 + 1).
   689     glyphNo := glyphs at:(ascii - 32 + 1).
   619     glyphNo isNil ifTrue:[^ self].
   690     glyphNo isNil ifTrue:[^ self].
   620     self class drawGlyph:glyphNo in:aGC x:x y:y scale:scale
   691     self class drawGlyph:glyphNo in:aGC x:x y:y scale:scale
   621 ! !
   692 ! !
   622 
   693 
   628     style := st.
   699     style := st.
   629     size := sz.
   700     size := sz.
   630     scale := sz / 24 
   701     scale := sz / 24 
   631 !
   702 !
   632 
   703 
       
   704 onDevice:aDevice
       
   705     ^ self copy setDevice:aDevice
       
   706 !
       
   707 
   633 readGlyphsFrom:aFileName
   708 readGlyphsFrom:aFileName
   634     "read glyph definitions from aFileName"
   709     "read glyph definitions from aFileName"
   635 
   710 
   636     |inStream ascii index1 index2|
   711     |inStream ascii index1 index2|
   637 
   712 
   638     self class readDataFile.
   713     self class readDataFile.
   639     glyphs := Array new:(256 - 32).
   714     glyphs := Array new:(256 - 32).
   640     inStream := FileStream readonlyFileNamed:('/LocalLibrary/Fonts/hershey/fonts/',aFileName,'.hmp').
   715     inStream := FileStream readonlyFileNamed:(self class hersheyFontPath asFilename / 'fonts' / (aFileName , '.hmp')).
   641     ascii := 32.
   716     ascii := 32.
   642     [inStream atEnd] whileFalse:[
   717     [inStream atEnd] whileFalse:[
   643 	index1 := Number readFrom:inStream.
   718         index1 := Number readFrom:inStream.
   644 	index2 := Number readFrom:inStream.
   719         index2 := Number readFrom:inStream.
   645 	index2 == 0 ifTrue:[
   720         index2 == 0 ifTrue:[
   646 	    index2 := index1
   721             index2 := index1
   647 	].
   722         ].
   648 	index1 to:index2 do:[:pos |
   723         index1 to:index2 do:[:pos |
   649 	    glyphs at:(ascii - 32 + 1) put:pos.
   724             glyphs at:(ascii - 32 + 1) put:pos.
   650 	    ascii := ascii + 1
   725             ascii := ascii + 1
   651 	].
   726         ].
   652 	inStream skipSeparators
   727         inStream skipSeparators
   653     ].
   728     ].
   654     inStream close.
   729     inStream close.
   655     ^ self
   730     ^ self
   656 !
   731 !
   657 
   732 
   658 scale:aScale
   733 scale:aScale
   659     scale := aScale
   734     scale := aScale
       
   735 !
       
   736 
       
   737 setDevice:aDevice
       
   738     super setDevice:aDevice.
       
   739     scale isNil ifTrue:[
       
   740         scale := size / 24 
       
   741     ].    
       
   742     ascent := maxAscent := self ascentOn:aDevice.
       
   743     descent := maxDescent := self descentOn:aDevice.
       
   744     width := minWidth := maxWidth := self widthOn:aDevice.
       
   745     isFixedWidth := false.
       
   746     minCode := 32.
       
   747     maxCode := 127.
       
   748     encoding := 'ascii'.
   660 ! !
   749 ! !
   661 
   750 
   662 !HersheyFont methodsFor:'queries'!
   751 !HersheyFont methodsFor:'queries'!
   663 
   752 
   664 ascentOn:aDevice
   753 ascentOn:aDevice
   680 
   769 
   681 heightOn:aDevice
   770 heightOn:aDevice
   682     ^ scale * (24 + 12)
   771     ^ scale * (24 + 12)
   683 !
   772 !
   684 
   773 
   685 onDevice:aDevice
   774 isAlienFont
   686     ^ self
   775     ^ true
   687 !
   776 !
   688 
   777 
   689 widthOf:aString
   778 widthOf:aString
   690     |sumW|
   779     |sumW|
   691 
   780 
   711 
   800 
   712 widthOfCharacter:ascii
   801 widthOfCharacter:ascii
   713     |glyphNo|
   802     |glyphNo|
   714 
   803 
   715     (ascii between:32 and:127) ifFalse:[^ 0].
   804     (ascii between:32 and:127) ifFalse:[^ 0].
       
   805     
   716     glyphNo := glyphs at:(ascii - 32 + 1).
   806     glyphNo := glyphs at:(ascii - 32 + 1).
   717     glyphNo isNil ifTrue:[^ 0].
   807     glyphNo isNil ifTrue:[^ 0].
   718     ^ self class widthOfGlyph:glyphNo scale:scale
   808     ^ self class widthOfGlyph:glyphNo scale:scale
       
   809 !
       
   810 
       
   811 widthOn:aDevice
       
   812     ^ self widthOfCharacter:($m codePoint)
   719 ! !
   813 ! !
   720 
   814 
   721 !HersheyFont class methodsFor:'documentation'!
   815 !HersheyFont class methodsFor:'documentation'!
   722 
   816 
   723 version
   817 version
   724     ^ '$Header$'
   818     ^ '$Header$'
       
   819 !
       
   820 
       
   821 version_CVS
       
   822     ^ '$Header$'
   725 ! !
   823 ! !
   726 
   824