summaryrefslogtreecommitdiff
path: root/pcl/pl/plfont.h
blob: 5d4bff71ab7091587e3cb7b5bb8c7f885a02ca88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* Copyright (C) 2001-2021 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* plfont.h */
/* Interface to PCL font utilities */

#ifndef plfont_INCLUDED
#  define plfont_INCLUDED

#include "gsccode.h"
#include "plsymbol.h"
#include "pldict.h"
#include "stream.h"             /* required by strmio.h */
#include "strmio.h"
#include "gsfont.h"
#include "gslibctx.h"
#include "gsgcache.h"
#include "gxfapi.h"

/* ---------------- PCL-specified definitions ---------------- */

/*
 * Define a types for 16-bit (Unicode/MSL) characters.
 * Note that these are always unsigned.
 */
typedef bits16 char16;

/* Define the Font Type for PCL5 downloaded fonts. */
typedef enum
{
    plft_7bit_printable = 0,    /* bound, 32-127 printable */
    plft_8bit_printable = 1,    /* bound, 32-127 & 160-255 printable */
    plft_8bit = 2,              /* bound, 0-255 printable */
    plft_16bit = 3,             /* bound, 0-65534 printable */
    plft_MSL = 10,              /* unbound, MSL codes */
    plft_Unicode = 11           /* unbound, Unicode codes */
} pl_font_type_t;

#define pl_font_is_bound(plfont)\
  ((plfont)->font_type < 10)

/* Define the Font Scaling Technology for PCL downloaded fonts. */
typedef enum
{
    plfst_Intellifont = 0,
    plfst_TrueType = 1,
    plfst_MicroType = 63,
    plfst_bitmap = 254
} pl_font_scaling_technology_t;

#define pl_font_is_scalable(plfont)\
  ((plfont)->scaling_technology != plfst_bitmap)

/*
 * Define the matching characteristics for PCL5 fonts.  Note that there
 * are two different values representing pitch: one in centipoints,
 * one in characters/inch * 100.
 */
#define pl_fp_pitch_cp(pfp) ((pfp)->pitch.cp)
#define pl_fp_set_pitch_cp(pfp, cpv)\
  ((pfp)->pitch.cp = (cpv),\
   (pfp)->pitch.per_inch_x100 = ( (cpv) == 0 ? (cpv) : 720000.0 / (cpv)))
#define pl_fp_pitch_per_inch(pfp) ((pfp)->pitch.per_inch_x100 / 100.0)
#define pl_fp_pitch_per_inch_x100(pfp) ((pfp)->pitch.per_inch_x100)
#define pl_fp_set_pitch_per_inch(pfp, cpi)\
  ((pfp)->pitch.cp = 7200.0 / (cpi),\
   (pfp)->pitch.per_inch_x100 = (cpi) * 100.0)
typedef struct pl_font_pitch_s
{
    double cp;
    double per_inch_x100;
} pl_font_pitch_t;

#define fp_pitch_value_cp(cpv)\
  { (cpv), 720000 / (cpv) }
typedef struct pl_font_params_s
{
    uint symbol_set;
    bool proportional_spacing;
    pl_font_pitch_t pitch;
    uint height_4ths;           /* unused in scalable fonts */
    uint style;
    int stroke_weight;
    uint typeface_family;
    /**** HACK, unfortunately there is not a better way to do this at
          the current time. ****/
    int pjl_font_number;
} pl_font_params_t;

/* ---------------- Internal structures ---------------- */

/*
 * Define the size values for hash tables.
 * We require used <= limit < size.
 */
#define pl_table_sizes\
  uint used;			/* # of entries in use */\
  uint limit;			/* max # of entries in use */\
  uint size;			/* allocated size of table */\
  uint skip                     /* rehashing interval */

/*
 * Define the hash table for mapping glyphs to character data.
 * Empty entries have data == 0 and glyph == 0;
 * deleted entries have data == 0 and glyph != 0.
 */
typedef struct pl_font_glyph_s
{
    gs_glyph glyph;
    const byte *data;
    int data_len;
} pl_font_glyph_t;

typedef struct pl_glyph_table_s
{
    pl_font_glyph_t *table;
                    pl_table_sizes;
} pl_glyph_table_t;

/*
 * Define the hash table for mapping character codes to TrueType glyph indices.
 * Empty entries have chr == gs_no_char and glyph == 0;
 * deleted entries have chr == gs_no_char and glyph != 0.
 */
typedef struct pl_tt_char_glyph_s
{
    gs_char chr;
    gs_glyph glyph;
} pl_tt_char_glyph_t;

typedef struct pl_tt_char_glyph_table_s
{
    pl_tt_char_glyph_t *table;
                       pl_table_sizes;
} pl_tt_char_glyph_table_t;

#define PL_MAX_WIDTHS_CACHE_NITEMS 256
typedef struct pl_glyph_width_node_s pl_glyph_width_node_t;

struct pl_glyph_width_node_s
{
    uint char_code;
    uint font_id;
    gs_point width;
    pl_glyph_width_node_t *next;
};

/*
 * We 'wrap' gs_fonts in our own structure.
 *
 */
typedef struct pl_font_s pl_font_t;

struct pl_font_s
{
    gs_font *pfont;             /* Type 42 if TrueType, Type 3 if bitmap. */
    int storage;                /* where the font is stored */
    bool data_are_permanent;    /* glyph data stored in rom */
    char *font_file;            /* non null only if data is stored in a
                                   file only relevant to pcl resident
                                   fonts. NB this should be done
                                   dynamically */
    bool font_file_loaded;      /* contents of the font file have be read into memory */
    byte *header;               /* downloaded header, or built-in font data */
    ulong header_size;
    /* Information extracted from the font or supplied by the client. */
    pl_font_scaling_technology_t scaling_technology;
    bool is_xl_format;          /* this is required for the agfa ufst scaler */
    pl_font_type_t font_type;
    bool allow_vertical_substitutes;
    /* Implementation of pl_font_char_width, see below */
    int (*char_width) (const pl_font_t * plfont, const void *pgs,
                       gs_char char_code, gs_point * pwidth);
    int (*char_metrics) (const pl_font_t * plfont, const void *pgs,
                         gs_char char_code, float metrics[4]);
    bool large_sizes;           /* segment sizes are 32 bits if true, 16 if false */
    /* (for segmented fonts only) */
    struct
    {
        uint x, y;
    } resolution;               /* resolution (for bitmap fonts) */
    float bold_fraction;        /* for PXL algorithmic bolding */
    int orient;                 /* true if pcl bitmap font designed in landscape */
    pl_font_params_t params;
    byte character_complement[8];       /* character complement (for unbound fonts) */
    struct o_
    {
        long GC;                /* Galley Character (optional) */
        long GT;                /* Global TrueType data (required, for TT fonts) */
        long VT;                /* VerTical substitution (optional) */
    } offsets;                  /* segment offsets, -1 if segment missing */
    /* Glyph table for downloaded fonts. */
    pl_glyph_table_t glyphs;
    /* Character to glyph map for downloaded TrueType fonts. */
    pl_tt_char_glyph_table_t char_glyphs;

    float pts_per_inch;         /* either 72 or 72.307 (for Intellifont) */
    pl_glyph_width_node_t *widths_cache;
    int widths_cache_nitems;
};

#define private_st_pl_font()	/* in plfont.c */\
  gs_private_st_ptrs4(st_pl_font, pl_font_t, "pl_font_t",\
    pl_font_enum_ptrs, pl_font_reloc_ptrs, pfont, header, glyphs.table,\
      char_glyphs.table)

/* ---------------- Procedural interface ---------------- */

/* Allocate and minimally initialize a font. */
pl_font_t *pl_alloc_font(gs_memory_t * mem, client_name_t cname);

/* copy a font */
pl_font_t *pl_clone_font(const pl_font_t * src, gs_memory_t * mem,
                         client_name_t cname);

/* Allocate the glyph table.  num_glyphs is just an estimate -- the table */
/* expands automatically as needed. */
int pl_font_alloc_glyph_table(pl_font_t * plfont, uint num_glyphs,
                              gs_memory_t * mem, client_name_t cname);

/* Allocate the glyph-to-character map for a downloaded TrueType font. */
int pl_tt_alloc_char_glyphs(pl_font_t * plfont, uint num_chars,
                            gs_memory_t * mem, client_name_t cname);

/* Fill in generic gs_font boilerplate. */
int pl_fill_in_font(gs_font * pfont, pl_font_t * plfont, gs_font_dir * pdir,
                    gs_memory_t * mem, const char *font_name);

/* Fill in bitmap and intellifont gs_font boilerplate. */
void pl_fill_in_bitmap_font(gs_font_base * pfont, long unique_id);

void pl_fill_in_intelli_font(gs_font_base * pfont, long unique_id);

/* Initialize the callback procedures for a bitmap and intellifont
   fonts. */
void pl_bitmap_init_procs(gs_font_base * pfont);

void pl_intelli_init_procs(gs_font_base * pfont);

/* Fill in TrueType gs_font boilerplate. */
/* data = NULL for downloaded fonts, the TT data for complete fonts. */
int pl_fill_in_tt_font(gs_font_type42 * pfont, void *data, long unique_id);

/* Initialize the callback procedures for a TrueType font. */
void pl_tt_init_procs(gs_font_type42 * pfont);

/* Finish initializing a TrueType font. */
void pl_tt_finish_init(gs_font_type42 * pfont, bool downloaded);

/*
 * Set large_sizes, scaling_technology, character_complement, offsets
 * (for TrueType fonts), and resolution (for bitmap fonts) by scanning
 * the segments of a segmented downloaded font.
 * This is used for PCL5 Format 15 and 16 fonts and for PCL XL fonts.
 * fst_offset is the offset of the Font Scaling Technology and Variety bytes;
 * the segment data runs from start_offset up to end_offset.
 * large_sizes = false indicates 2-byte segment sizes, true indicates 4-byte.
 */
typedef struct pl_font_offset_errors_s
{
    int illegal_font_data;
    int illegal_font_segment;   /* 0 means ignore unknown segments */
    /* 0 in any of the remaining values means return illegal_font_data */
    int illegal_font_header_fields;
    int illegal_null_segment_size;
    int missing_required_segment;
    int illegal_GT_segment;
    int illegal_GC_segment;
    int illegal_VT_segment;
    int illegal_BR_segment;
} pl_font_offset_errors_t;

int pl_font_scan_segments(const gs_memory_t * mem,
                          pl_font_t * plfont, int fst_offset,
                          int start_offset, long end_offset,
                          bool large_sizes,
                          const pl_font_offset_errors_t * pfoe);

/* Load a built-in (TrueType) font from external storage. */
int pl_load_tt_font(stream * in, gs_font_dir * pdir, gs_memory_t * mem,
                    long unique_id, pl_font_t ** pplfont, char *font_name);

/* allocate, read in and free tt font files to and from memory */
int pl_alloc_tt_fontfile_buffer(stream * in, gs_memory_t * mem,
                                byte ** pptt_font_data, ulong * size);
int pl_free_tt_fontfile_buffer(gs_memory_t * mem, byte * ptt_font_data);

/* Add a glyph to a font.  Return -1 if the table is full. */
int pl_font_add_glyph(pl_font_t * plfont, gs_glyph glyph, const byte * cdata, const int cdata_len);

void
pl_font_glyph_width_cache_remove_nodes(pl_font_t *plfont);

/* Determine the escapement of a character in a font / symbol set. */
/* If the font is bound, the symbol set is ignored. */
/* If the character is undefined, set the escapement to (0,0) and return 1. */
/* If pwidth is NULL, don't store the escapement. */
int pl_font_char_width(const pl_font_t * plfont, const void *pgs,
                       gs_char char_code, gs_point * pwidth);

/* Determine the character metrics.  If vertical substitution is in
   effect metrics[1] = lsb, metrics[3] = width otherwise metrics[0] =
   lsb and metrics 2 = width.   The same rules for character width apply */
int pl_font_char_metrics(const pl_font_t * plfont, const void *pgs,
                         gs_char char_code, float metrics[4]);

/* Look up a glyph in a font.  Return a pointer to the glyph's slot */
/* (data != 0) or where it should be added (data == 0). */
pl_font_glyph_t *pl_font_lookup_glyph(const pl_font_t * plfont,
                                      gs_glyph glyph);

/* Look up a truetype character */
pl_tt_char_glyph_t *pl_tt_lookup_char(const pl_font_t * plfont,
                                      gs_glyph glyph);

/* Determine whether a font, with a given symbol set, includes a given */
/* character.  If the font is bound, the symbol set is ignored. */
#define pl_font_includes_char(plfont, maps, matrix, char_code)\
  (pl_font_char_width(plfont, maps, matrix, char_code, (gs_point *)0) == 0)

/* Remove a glyph from a font.  Return 1 if the glyph was present. */
int pl_font_remove_glyph(pl_font_t * plfont, gs_glyph glyph);

/* Free a font.  This is the freeing procedure in the font dictionary. */
void pl_free_font(gs_memory_t * mem, void *plf, client_name_t cname);

/* load resident font data to ram */
int pl_load_resident_font_data_from_file(gs_memory_t * mem,
                                         pl_font_t * plfont);

/* keep resident font data in its original file */
int pl_store_resident_font_data_in_file(char *font_file, gs_memory_t * mem,
                                        pl_font_t * plfont);

/* check if the font is zlib deflated.  This test is sufficient
   because we are only checking if the header is a TT header or a zlib
   compressed header, exclusively. */
#define pl_is_tt_zlibC(header)\
    ((header[0] & 0xf) == 8)
/* agfa requires prepending a dummy header to xl fonts */
int pl_prepend_xl_dummy_header(gs_memory_t * mem, byte ** ppheader);

/* agfa requires swapping downloaded intellifont headers */
int pl_swap_header(byte * header, uint gifct);

/* UFST callbacks if needed */
void plu_set_callbacks(void);

/* disable component metrics in a TrueType glyph - emulate an HP printer bug */
int pl_font_disable_composite_metrics(pl_font_t * plfont, gs_glyph glyph);

pl_font_t *pl_lookup_font_by_pjl_number(pl_dict_t * pfontdict,
                                        int pjl_font_number);

#endif /* plfont_INCLUDED */