Search code examples
textfontssizesmalltalksqueak

How to specify Text size in Squeak


I was able to modify in many ways the appeareance of Text in Squeak but still, I can't find a simple way to change explicitely the font size (as a number). Do you know how to do it?

This is my snippet:

text1 := Text string: 'Hello ' attribute: (TextColor color: Color red).

text2 := Text string: 'World!' attribute:TextEmphasis normal.
text2 addAttribute: (TextColor color: Color magenta).
text2 addAttribute:  (TextFontChange font2).
text2 addAttribute:  (TextEmphasis underlined).

text := text1, ' ', text2.

(text asMorph) openInWorld .

Solution

  • Looks like the size is part of the font object. Which makes the code snippets I came up with also not quite simple:

    "Set a true type font of a certain size"
    text
       addAttribute:
           (TextFontReference toFont:
              ((TTCFont family: 'BitstreamVeraSerif' size: 18)
                  ifNil: [self error: 'Font not found']))
       from: 1 to: 4.
    
    "Use the font of a text style with a certain size"
    text
       addAttribute:
          (TextFontReference toFont:
             ((TextStyle named: 'Bitmap DejaVu Sans') fontOfPointSize: 22))
       from: 1 to: 4.
    
    "Use the current font with a different size"
    text
       addAttribute:
          (TextFontReference toFont:
              ((TextStyle named: (text fontAt: 1 withStyle: TextStyle default) familyName)
                 fontOfPointSize: 18))
       from: 1 to: 4.
    

    I included the ifNil: block in the first case because the TextMorph bails out and stops drawing if you install a nil font somewhere in the Text. TTCFront only returns a font if the combination of the font family and the size is present in the Squeak image.

    Also note that not all sizes are available. fontOfPointSize: will look for the nearest size that is available and less than or equal to the requested size. Explore the following:

    TTCFont registry
    
    TextStyle knownTextStyles