Class | Package: cg | ToC | DocOverview | CGDoc | RelNotes | FAQ | Index | PermutedIndex
| Allegro CL version 9.0 Unrevised from 8.2 to 9.0. 8.2 version

## fontmetrics

The class of fontmetrics objects. Some functions that return attributes of fonts may be passed the font object directly, but most of them must instead be passed a fontmetrics object that reflects the font as it is used on a particular graphical stream.

A fontmetrics is created by calling either make-fontmetrics or fontmetrics, and is returned by each of those functions as well as by nfontmetrics. Once a fontmetrics object is returned by a call to either fontmetrics or nfontmetrics, it holds the values of various attributes of the current font of the window. Various accessor functions such as font-leading and font-fixed-width-p can then be called on the fontmetrics object to obtain information about the font as used on that stream.

Most of the fontmetrics accessors return the size (in pixels) of various parts of a character cell, such as font-descent to return the part of each character cell that is reserved for the parts of characters that descend below the baseline. These values may be different on different streams, even for a font of a particular declared pixel size (see font-size and make-font-ex). This might even be true among different kinds of windows on a single screen at a particular resolution. Therefore, these accessor functions do not work on a font object by itself, and must instead by passed a fontmetrics object that reflects the current font of a particular stream on which it may be used.

### Example

In this example, the form will return the internal font leading of the specified font when it is used on the window "my-window".

    (let* ((font-37 (make-font-ex nil "Arial" 24)))
      (with-font (my-window font-37)
        (font-internal-leading (fontmetrics my-window))))

Here is an example function that collects all of the attributes of a font and returns them as a plist. It allows you to pass in the stream where the font might be used, in case any measurements may differ based on the stream, and otherwise uses the screen for measurements. The function illustrates how some font attribute functions must be passed a fontmetrics object, while others take the font itself.

    (defun font-attributes (font &key (stream (screen *system*)))

      ;; This is needed if the stream is the screen or an instance
      ;; of os-widget-window, since these streams do not have permanent
      ;; device-contexts as "regular" windows do.
      (with-device-context (hdc stream)

        ;; Assign the font to the stream, and THEN retrieve a
        ;; fontmetrics object for the stream, which will then reflect
        ;; that font as used on that stream.
        (with-font (stream font)
          (let* ((fm (fontmetrics stream))
                 (plist nil))

            ;; Collect every attribute that requires a fontmetrics
            ;; object from our fontmetrics object that reflects the font.
            (dolist (accessor
                     '#.(reverse
                         '(font-height
                           font-ascent font-descent font-leading
                           font-external-leading font-internal-leading
                           font-fixed-width-p font-average-char-width
                           font-max-char-width font-vector-p
                           font-truetype-p font-device-p font-direction)))
              (push (funcall accessor fm) plist)
              (push accessor plist))

            ;; Add to the list the other font attributes that can be
            ;; called directly on the font object itself.
            (dolist (accessor
                     '#.(reverse
                         '(font-family
                           font-face font-size font-style
                           font-style-word font-size-is-char-height)))
              (push (funcall accessor font) plist)
              (push accessor plist))

            ;; Return the list of all of the font's attributes.
            plist))))

    ;;; Try printing the attributes of an example font.
    (pprint (font-attributes (make-font-ex nil "Arial" 24 '(:bold))))

    ==> (font-family nil font-face "Arial" font-size 24 font-style (:bold)
                 font-style-word 1 font-size-is-char-height t
                 font-height 30 font-ascent 23 font-descent 6
                 font-leading 6 font-external-leading 1
                 font-internal-leading 5 font-fixed-width-p nil
                 font-average-char-width 11 font-max-char-width 64
                 font-vector-p t font-truetype-p t font-device-p nil
                 font-direction :left-right)

Copyright (c) 1998-2019, Franz Inc. Oakland, CA., USA. All rights reserved.
This page was not revised from the 8.2 page.
Created 2012.5.30.

|  | ToC | DocOverview | CGDoc | RelNotes | FAQ | Index | PermutedIndex
| Allegro CL version 9.0 Unrevised from 8.2 to 9.0. 8.2 version