Ghostscript's external font and text interface

## Table of contents

> - Introduction
> - Types
> - Implementation procedures
>   - Font-level procedures
>     - `lookup_font`
>     - `char_xglyph`
>     - `char_metrics`
>     - `render_char`
>     - `release`

For other information, see the Ghostscript overview.

# Introduction

Ghostscript can use the character rasterizer provided by the underlying operating system and window system; specifically, Adobe Type Manager (ATM) or a TrueType rasterizer under MS Windows, or the facilities provided by X Windows. This ability augments, but does not replace, Ghostscript's own Type 1 rasterizer: Ghostscript may still use its own rasterizer for very large characters, characters that are clipped or transformed in unusual ways, and for output to devices other than the screen.

Ghostscript connects to these platform facilities through a driver-like interface called the xfont (external font) interface. Current xfont implementations are associated directly with device drivers; in a future release, Ghostscript may separate them, so that (for example) it will be possible to use the platform rasterizer when writing to a file.

Please note that from this point, this file is likely to be useful only to a small number of Ghostscript porters and implementors.

# Types

Type |  | Declared / defined in |  | Represents
`gs_char` |  | `gsccode.h` |  | A character code that appears in a string. Currently it is always a single byte, but composite fonts or Unicode may require it to be wider in the future.
`gs_glyph` |  | `gsccode.h` |  | A character name like "period" or "epsilon". From the xfont implementation's point of view, it is just a handle; when necessary, Ghostscript provides a `gs_proc_glyph_name_t` procedure to convert it to a string name.
`gs_proc_glyph_name_t` |  | `gsccode.h` |  | A procedure that maps a `gs_glyph` to its string name; see the `char_xglyph` procedure.
`gx_xglyph` |  | `gsxfont.h` |  | A character or glyph code that can be used with a specific platform font. Normally it will be a character code that the implementation of `render_char` will turn into a 1-character string and give to the platform's "display string" operation.
`gx_xfont_procs` |  | `gsxfont.h`, `gxxfont.h` |  | The xfont analogue of `gx_device_procs`, the type of the procedure record that defines an xfont implementation.
`gx_xfont` |  | `gsxfont.h`, `gxxfont.h` |  | The gxfont analogue of `gx_device`, the type of the basic structure for an xfont.
(`encoding_index`) |  |  |  | Not really a type, although it probably should be: an `int` used to indicate the `Encoding` used by a font. Defined values are 0 `StandardEncoding` 1 `ISOLatin1Encoding` 2 `SymbolEncoding` 3 `DingbatsEncoding` -1 Other encoding

# Implementation procedures

All the procedures that return `int` results return 0 on success, or an appropriate negative error code for error conditions; these error codes are defined in `gserrors.h`. The relevant ones are the same as for drivers, and as with drivers, if an implementation procedure returns an error, it should use the `return_error` macro -- defined in `gx.h`, which is automatically included by `gdevprn.h` but not by `gserrors.h` -- rather than a simple `return` statement, for instance

> `return_error(gs_error_VMerror);`

## Font-level procedures

### `lookup_font`

`gx_xfont *(*lookup_font)(P7(gx_device *dev, const byte *fname, uint len, int encoding_index, const gs_uid *puid, const gs_matrix *pmat, const gs_memory_procs *mprocs))`
  Look up a font name, `UniqueID`, and matrix, and return an xfont, or `NULL` if no suitable xfont exists. Use `mprocs` to allocate the xfont and any subsidiary data structures. The matrix is the `FontMatrix` concatenated with the CTM, so (roughly speaking) the font size in pixels is `pmat -> yy` × 1000 for a normal Type 1 font.

  Note that this is the only implementation procedure that does not take an xfont * as its first argument. In fact, callers of `lookup_font` must use the `get_xfont_device` driver procedure to get the correct device to pass as the first argument to `lookup_font`.

### `char_xglyph`

`gx_xglyph (*char_xglyph)(P5(gx_xfont *xf, gs_char chr, int encoding_index, gs_glyph glyph, gs_proc_glyph_name_t glyph_name))`
  Convert a character name to an xglyph code. In the case of `glyphshow`, `chr` may be `gs_no_char`; for an ordinary `show` operation, if the character code is invalid, `glyph` may be `gs_no_glyph`.

### `char_metrics`

`int (*char_metrics)(P5(gx_xfont *xf, gx_xglyph xg, int wmode, gs_int_point *pwidth, gs_int_rect *pbbox))`
  Get the metrics for a character. If the metrics are unavailable, return 1.

### `render_char`

`int (*render_char)(P7(gx_xfont *xf, gx_xglyph xg, gx_device *target, int x, int y, gx_color_index color, int required))`
  Render a character. (x,y) corresponds to the character origin. The target may be any Ghostscript device. A good implementation will check whether the target can handle this type of xfont directly (for instance by checking the target name), and if so, will render the character directly; otherwise, it will do what has to be done in the general case, namely, get a bitmap for the character and use the target's `copy_mono` operation. If `required` is false, the procedure should return an error if the rendering operation would be expensive, since in this case Ghostscript has already cached the bitmap and metrics from a previous call with `required`=true. If the operation cannot be done, return 1.

### `release`

`int (*release)(P2(gx_xfont *xf, const gs_memory_procs *mprocs))`
  Release any external resources associated with an xfont. If `mprocs` is not `NULL`, also free any storage allocated by `lookup_font` (including the xfont itself).

Copyright © 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.

This file is part of Aladdin Ghostscript. See the Aladdin Free Public License (the "License") for full details of the terms of using, copying, modifying, and redistributing Aladdin Ghostscript.

Ghostscript version 6.0, 3 February 2000