summaryrefslogtreecommitdiff
path: root/src/pdflib/pdflib/p_pfm.c
blob: b1989a4f87611cef3004e06b70546ee7a77fca10 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*---------------------------------------------------------------------------*
 |              PDFlib - A library for generating PDF on the fly             |
 +---------------------------------------------------------------------------+
 | Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
 +---------------------------------------------------------------------------+
 |                                                                           |
 |    This software is subject to the PDFlib license. It is NOT in the       |
 |    public domain. Extended versions and commercial licenses are           |
 |    available, please check http://www.pdflib.com.                         |
 |                                                                           |
 *---------------------------------------------------------------------------*/

/* $Id: p_pfm.c,v 1.2 2009/10/20 18:14:16 scuri Exp $
 *
 * PDFlib routines for fast reading of PFM font metrics files
 *
 */

#include "p_intern.h"
#include "p_font.h"

/* read data types from the PFM */
#define PFM_BYTE(offset)  pfm[offset]
#define PFM_WORD(offset)  PDC_GET_WORD(&pfm[offset])
#define PFM_SHORT(offset) PDC_GET_SHORT(&pfm[offset])
#define PFM_DWORD(offset) PDC_GET_DWORD3(&pfm[offset])

/* Offsets in the buffer containing the various PFM structures */
#define header_base		0
#define header_dfVersion	(PFM_WORD(header_base + 0))
#define header_dfSize		(PFM_DWORD(header_base + 2))
#define header_dfAscent		(PFM_WORD(header_base + 74))
#define header_dfItalic		(PFM_BYTE(header_base + 80))
#define header_dfWeight		(PFM_WORD(header_base + 83))
#define header_dfCharSet	(PFM_BYTE(header_base + 85))
#define header_dfPitchAndFamily	(PFM_BYTE(header_base + 90))
#define header_dfMaxWidth	(PFM_WORD(header_base + 93))
#define header_dfFirstChar	(PFM_BYTE(header_base + 95))
#define header_dfLastChar	(PFM_BYTE(header_base + 96))
#define header_dfDefaultChar	(PFM_BYTE(header_base + 97))

#define ext_base		117
#define ext_dfExtentTable	(PFM_DWORD(ext_base + 6))
#define ext_dfKernPairs		(PFM_DWORD(ext_base + 14))
#define ext_dfKernTrack		(PFM_DWORD(ext_base + 18))
#define ext_dfDriverInfo	(PFM_DWORD(ext_base + 22))

#define etm_base		147
#define etmCapHeight		(PFM_SHORT(etm_base + 14))
#define etmXHeight		(PFM_SHORT(etm_base + 16))
#define etmLowerCaseAscent	(PFM_SHORT(etm_base + 18))
#define etmLowerCaseDescent	(PFM_SHORT(etm_base + 20))
#define etmSlant		(PFM_SHORT(etm_base + 22))
#define etmUnderlineOffset	(PFM_SHORT(etm_base + 32))
#define etmUnderlineWidth	(PFM_SHORT(etm_base + 34))

#define dfDevice		199

/* Windows font descriptor flags */
#define PDF_FIXED_PITCH         0x01  /* Fixed width font; rarely used flag */

#define PDF_DONTCARE            0x00  /* Don't care or don't know. */
#define PDF_ROMAN               0x10  /* Variable stroke width, serifed */
#define PDF_SWISS               0x20  /* Variable stroke width, sans-serifed */
#define PDF_MODERN              0x30  /* fixed pitch */
#define PDF_SCRIPT              0x40  /* Cursive, etc. */
#define PDF_DECORATIVE          0x50  /* Old English, etc. */

/* Windows character set flags */
#define PFM_ANSI_CHARSET          0
#define PFM_SYMBOL_CHARSET        2
#define PFM_GREEK_CHARSET       161
#define PFM_TURKISH_CHARSET     162
#define PFM_VIETNAMESE_CHARSET  163
#define PFM_HEBREW_CHARSET      177
#define PFM_ARABIC_CHARSET      178
#define PFM_BALTIC_CHARSET      186
#define PFM_RUSSIAN_CHARSET     204
#define PFM_THAI_CHARSET        222
#define PFM_EASTEUROPE_CHARSET  238

static const pdc_keyconn pdf_charset_keylist[] =
{
    {"winansi", PFM_ANSI_CHARSET      },
    {"",        PFM_SYMBOL_CHARSET    },
    {"cp1253",  PFM_GREEK_CHARSET     },
    {"cp1254",  PFM_TURKISH_CHARSET   },
    {"cp1258",  PFM_VIETNAMESE_CHARSET},
    {"cp1255",  PFM_HEBREW_CHARSET    },
    {"cp1256",  PFM_ARABIC_CHARSET    },
    {"cp1257",  PFM_BALTIC_CHARSET    },
    {"cp1251",  PFM_RUSSIAN_CHARSET   },
    {"cp874",   PFM_THAI_CHARSET      },
    {"cp1250",  PFM_EASTEUROPE_CHARSET},
    {NULL, 0},
};

#define PDF_STRING_PostScript	\
	((const char*) "\120\157\163\164\123\143\162\151\160\164")

/*
 *  Kerning pairs
 */
typedef struct kern_
{
    pdc_byte   first;           /* First character */
    pdc_byte   second;          /* Second character */
    pdc_byte   kern[2];         /* Kern distance */
}
KERN;

pdc_bool
pdf_check_pfm_encoding(PDF *p, pdf_font *font, pdc_encoding enc)
{
    const char *encname =
        pdc_errprintf(p->pdc, "%.*s", PDC_ERR_MAXSTRLEN,
                      pdf_get_encoding_name(p, enc, font));
    const char *intencname = NULL;
    pdc_encoding intenc = pdc_invalidenc;
    pdc_bool issymbfont = pdc_undef;

    pdc_logg_cond(p->pdc, 2, trc_font,
        "\tFont internal charset (dfCharSet): %d\n", font->ft.enc);

    /* Font encoding */
    intencname = pdc_get_keyword(font->ft.enc, pdf_charset_keylist);
    if (intencname == NULL)
    {
        pdc_set_errmsg(p->pdc, PDF_E_T1_BADCHARSET,
            pdc_errprintf(p->pdc, "%d", font->ft.enc), 0, 0, 0);
        return pdc_false;
    }

    if (strlen(intencname))
    {
        int codepage = 0;

        pdc_logg_cond(p->pdc, 2, trc_font,
            "\tFont internal encoding \"%s\" found\n", intencname);

        intenc = pdc_find_encoding(p->pdc, intencname);
        if (intenc == pdc_invalidenc)
            intenc = pdc_insert_encoding(p->pdc, intencname, &codepage,
                                         pdc_true);

        font->ft.issymbfont = pdc_false;
    }
    else
    {
        pdc_logg_cond(p->pdc, 2, trc_font, "\tSymbol font\n");

        font->ft.issymbfont = pdc_true;
        intenc = pdc_builtin;

        /* auto */
        if (!strcmp(font->encapiname, "auto"))
        {
            issymbfont = pdc_true;
            enc = pdc_builtin;
        }
    }

    /* builtin */
    if (enc == pdc_builtin)
        issymbfont = pdc_true;

    /* unicode */
    if (enc == pdc_unicode)
    {
        font->unibyte = pdc_true;
        issymbfont = pdc_false;
        enc = intenc;
    }

    /* encoding is subset of 8-bit encoding */
    if (enc >= pdc_winansi && intenc >= pdc_winansi)
    {
        if (pdc_is_encoding_subset(p->pdc, pdc_get_encoding_vector(p->pdc, enc),
                                   pdc_get_encoding_vector(p->pdc, intenc)))
        {
            if (enc != pdc_winansi && intenc == pdc_winansi &&
                strcmp(encname, "iso8859-1"))
            {
                font->towinansi = intenc;
            }
            else
            {
                enc = intenc;
            }
            issymbfont = pdc_false;
        }
    }

    /* illegal encoding */
    if (issymbfont == pdc_undef || font->ft.issymbfont == pdc_undef)
    {
        pdc_set_errmsg(p->pdc, PDF_E_FONT_BADENC, 0, 0, 0, 0);
        return pdc_false;
    }

    font->ft.enc = enc;
    if (issymbfont && !font->ft.issymbfont)
    {
        pdc_warning(p->pdc, PDF_E_FONT_FORCEENC,
                    pdf_get_encoding_name(p, intenc, NULL),
                    0, 0, 0);
        font->ft.enc = intenc;
    }
    if (!issymbfont && font->ft.issymbfont)
    {
        pdc_warning(p->pdc, PDF_E_FONT_FORCEENC,
                    pdf_get_encoding_name(p, pdc_builtin, NULL),
                    0, 0, 0);
        font->ft.enc = pdc_builtin;
        font->towinansi = pdc_invalidenc;
    }

    if (font->towinansi != pdc_invalidenc)
        pdf_transform_fontwidths(p, font,
                        pdc_get_encoding_vector(p->pdc, font->ft.enc),
                        pdc_get_encoding_vector(p->pdc, font->towinansi));

    return pdc_true;
}


/*
 * Currently we do not populate the following fields correctly:
 * - serif flag
 */

static pdc_bool
pdf_parse_pfm(PDF *p, pdc_file *fp, pdf_font *font)
{
    static const char fn[] = "pdf_parse_pfm";
    fnt_font_metric *ftm = &font->ft.m;
    size_t length;
    pdc_byte *pfm;
    pdc_bool ismem;
    int i, dfFirstChar, dfLastChar, default_width;
    unsigned long dfExtentTable;

    /* read whole file and close it */
    pfm = (pdc_byte *) pdc_freadall(fp, &length, &ismem);
    pdc_fclose(fp);

    /* check whether this is really a valid PostScript PFM file */
    if (pfm == NULL ||
	(header_dfVersion != 0x100 && header_dfVersion != 0x200) ||
	dfDevice > length ||
	strncmp((const char *) pfm + dfDevice, PDF_STRING_PostScript, 10) ||
	ext_dfDriverInfo > length)
    {
	if (!ismem)
	    pdc_free(p->pdc, pfm);
        return pdc_false;
    }

    /* fetch relevant data from the PFM */
    ftm->type = fnt_Type1;

    font->ft.name = pdc_strdup(p->pdc, (const char *)pfm + ext_dfDriverInfo);
    ftm->name = pdc_strdup(p->pdc, font->ft.name);

    pdc_logg_cond(p->pdc, 1, trc_font,
        "\tPostScript font name: \"%s\"\n", ftm->name);

    switch (header_dfPitchAndFamily & 0xF0)
    {
	case PDF_ROMAN:
            ftm->flags |= FNT_SERIF;
	    break;
	case PDF_MODERN:
	    /* Has to be ignored, contrary to MS's specs */
	    break;
	case PDF_SCRIPT:
            ftm->flags |= FNT_SCRIPT;
	    break;
	case PDF_DECORATIVE:
	    /* the dfCharSet flag lies in this case... */
	    header_dfCharSet = PFM_SYMBOL_CHARSET;
	    break;
	case PDF_SWISS:
	case PDF_DONTCARE:
	default:
	    break;
    }

    /* temporarily */
    font->ft.enc = (pdc_encoding) header_dfCharSet;

    dfFirstChar = header_dfFirstChar;
    dfLastChar = header_dfLastChar;
    dfExtentTable = ext_dfExtentTable;

    /*
     * Some rare PFMs do not contain any ExtentTable if the fixed pitch flag
     * is set. Use the dfMaxWidth entry for all glyphs in this case.
     * If the user forced the font to be monospaced we use this value instead.
     */
    if ((!(header_dfPitchAndFamily & PDF_FIXED_PITCH) && dfExtentTable == 0) ||
        font->opt.monospace)
    {
        ftm->isFixedPitch = pdc_true;
        default_width = font->opt.monospace ? font->opt.monospace :
				(int) header_dfMaxWidth;
    }
    else
    {
	/* default values -- don't take the width of the default character */
        default_width = FNT_DEFAULT_WIDTH;
    }

    font->ft.numcodes = 256;
    ftm->numwidths = font->ft.numcodes;
    ftm->widths = (int *) pdc_calloc(p->pdc, ftm->numwidths * sizeof(int), fn);
    for (i = 0; i < font->ft.numcodes; i++)
        ftm->widths[i] = default_width;

    if (!ftm->isFixedPitch)
    {
	if (ext_dfExtentTable == 0 ||
	    ext_dfExtentTable + 2 * (header_dfLastChar-header_dfFirstChar) + 1 >
		length)
        {
	    if (!ismem)
		pdc_free(p->pdc, pfm);
	    return pdc_false;
	}

	for (i = dfFirstChar; i <= dfLastChar; i++)
            ftm->widths[i] =
                          (int) PFM_WORD(dfExtentTable + 2 * (i - dfFirstChar));
	/*
         * Check whether the font is actually opt.monospaced
	 * (the fixed pitch flag is not necessarily set)
	 */
        default_width = ftm->widths[dfFirstChar];

	for (i = dfFirstChar+1; i <= dfLastChar; i++)
            if (default_width != ftm->widths[i])
		break;

	if (i == dfLastChar + 1)
            ftm->isFixedPitch = pdc_true;
    }

    font->ft.weight = fnt_check_weight(header_dfWeight);
    ftm->defwidth = default_width;
    ftm->italicAngle = (header_dfItalic ? etmSlant/(10.0) : 0.0);
    ftm->capHeight = etmCapHeight;
    ftm->xHeight = etmXHeight;
    ftm->descender = -etmLowerCaseDescent;
    ftm->ascender = (int) header_dfAscent;

    ftm->underlinePosition = -etmUnderlineOffset;
    ftm->underlineThickness = etmUnderlineWidth;

    ftm->urx = header_dfMaxWidth;


    if (!ismem)
        pdc_free(p->pdc, pfm);

    return pdc_true;
}

pdc_bool
pdf_get_metrics_pfm(
    PDF *p,
    pdf_font *font,
    const char *fontname,
    pdc_encoding enc,
    const char *filename,
    pdc_bool requested)
{
    static const char fn[] = "pdf_get_metrics_pfm";
    char fullname[PDC_FILENAMELEN];
    pdc_file *pfmfile;

    (void) fontname;

    /* open PFM file */
    pfmfile = pdc_fsearch_fopen(p->pdc, filename, fullname, "PFM ",
                                PDC_FILE_BINARY);
    if (pfmfile == NULL)
        return pdc_check_fopen_errmsg(p->pdc, requested);

    pdc_logg_cond(p->pdc, 1, trc_font,
        "\tLoading PFM metric fontfile \"%s\":\n", fullname);

    /* Read PFM metrics */
    if (!pdf_parse_pfm(p, pfmfile, font))
    {
        pdc_set_errmsg(p->pdc, PDF_E_FONT_CORRUPT, "PFM", fullname, 0, 0);
        return pdc_false;
    }

    /* save full filename */
    font->metricfilename = pdc_strdup_ext(p->pdc, fullname, 0, fn);

    /* Check encoding */
    if (!pdf_check_pfm_encoding(p, font, enc))
        return pdc_false;

    if (!pdf_make_fontflag(p, font))
        return pdc_false;

    return pdc_true;
}