5 Fonts

5.1 The Font Model

Text related objects can be handled with the font option {font,Font}. A Font is represented as a two or three tuple:

  • {Family,Size}
  • {Family,Style,Size}

Examples of fonts are: {times,12}, {symbol,bold,18}, {courier,[bold,italic],6}, {screen,12}.

The most important requirement with the font model is to ensure that there is always a "best possible" font present. For example, if an application tries to use the font {times,17} on a computer system which does not have this font available, the gs font model automatically substitutes {times,16}.

Note that GS requires that the following fonts are available if using an X-server display:

  • fixed
  • -*-courier-*
  • -*-times-*
  • -*-helvetica-*
  • -*-symbol-*
  • "-*-new century schoolbook-"
  • -*-screen-*

To find out which font is actually chosen by the gs, use the option {choose_font,Font}. For example, the following situation might occur:

1> <input>G=gs:start().</input>
{1,<0.20.0>}
2><input>gs:read(G,{choose_font,{times,38}}).</input>
{times,[],38}
3> <input>gs:read(G,{choose_font,{screen,italic,6}}).</input> 
{courier,italic,6}
4> 
    

When programming with fonts, it is often necessary to find the size of a string which uses a specific font. {font_wh,Font} returns the width and height of any string and any font. The following example illustrates its usage:

Figure 5.1: Font Examples

-module(ex15).
-copyright('Copyright (c) 1991-97 Ericsson Telecom AB').
-vsn('$Revision: /main/release/3 $ ').

-export([start/0,init/0]).

start() -> spawn(ex15, init, []).

init() ->
    I=gs:start(),
    Win=gs:create(window, I,
                  [{width, 400},{height, 250},
                   {title,"Font Demo"},{map, true}]),
    E = gs:create(canvas, can1,Win,
		  [{x,0},{y, 0},{width,400},{height,250}]),
    Fonts = [{times,19},{screen,16},{helvetica,bold,21},
	     {symbol,12},{times,[bold,italic],33},{courier,6}],
    show_fonts_in_boxes(Fonts,0),
    receive
	{gs,_Id,destroy,_Data,_Arg} -> bye
    end.

show_fonts_in_boxes([],_) -> done;
show_fonts_in_boxes([Font|Fonts],Y) ->
    Txt = io_lib:format("Hi! ~p",[Font]),
    {Width,Height} = gs:read(can1,{font_wh,{Font,Txt}}),
    Y2=Y+Height+2,
    gs:create(rectangle,can1,[{coords,[{0,Y},{Width,Y2}]}]),
    gs:create(text,can1,[{font,Font},{text,Txt},{coords,[{0,Y+1}]}]),
    show_fonts_in_boxes(Fonts,Y2+1).

© 2010–2017 Ericsson AB
Licensed under the Apache License, Version 2.0.