summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/pdftexdir/writet3.c
blob: 5d5eb47729ff00db583fadd1fa09630d5c17b607 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* writet3.c: write a Type 3 (bitmap PK) font.
Copyright 1996-2019 Han The Thanh, <thanh@pdftex.org>

This file is part of pdfTeX.

pdfTeX is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

pdfTeX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ptexlib.h"
#include <kpathsea/tex-glyph.h>
#include <kpathsea/magstep.h>
#include <string.h>

#define T3_BUF_SIZE   1024

typedef char t3_line_entry;
define_array(t3_line);

FILE *t3_file;
static boolean t3_image_used;

static integer t3_char_procs[256];
static float t3_char_widths[256];
static int t3_glyph_num;
static float t3_font_scale;
static integer t3_b0, t3_b1, t3_b2, t3_b3;
static boolean is_pk_font;

#define t3_open()       \
    open_input(&t3_file, kpse_miscfonts_format, FOPEN_RBIN_MODE)
#define t3_close()      xfclose(t3_file, cur_file_name)
#define t3_getchar()    xgetc(t3_file)
#define t3_eof()        feof(t3_file)
#define t3_prefix(s)    str_prefix(t3_line_array, s)
#define t3_putchar(c)   pdfout(c)

#define t3_check_eof()                                     \
    if (t3_eof())                                          \
        pdftex_fail("unexpected end of file");

static void t3_getline(void)
{
    int c;
  restart:
    t3_line_ptr = t3_line_array;
    c = t3_getchar();
    while (!t3_eof()) {
        alloc_array(t3_line, 1, T3_BUF_SIZE);
        append_char_to_buf(c, t3_line_ptr, t3_line_array, t3_line_limit);
        if (c == 10)
            break;
        c = t3_getchar();
    }
    alloc_array(t3_line, 2, T3_BUF_SIZE);
    append_eol(t3_line_ptr, t3_line_array, T3_BUF_SIZE);
    if (t3_line_ptr - t3_line_array < 2 || *t3_line_array == '%') {
        if (!t3_eof())
            goto restart;
    }
}

static void t3_putline(void)
{
    char *p = t3_line_array;
    while (p < t3_line_ptr)
        t3_putchar(*p++);
}

static void update_bbox(integer llx, integer lly, integer urx, integer ury,
                        boolean is_first_glyph)
{
    if (is_first_glyph) {
        t3_b0 = llx;
        t3_b1 = lly;
        t3_b2 = urx;
        t3_b3 = ury;
    } else {
        if (llx < t3_b0)
            t3_b0 = llx;
        if (lly < t3_b1)
            t3_b1 = lly;
        if (urx > t3_b2)
            t3_b2 = urx;
        if (ury > t3_b3)
            t3_b3 = ury;
    }
}

static void t3_write_glyph(internalfontnumber f)
{
    static char t3_begin_glyph_str[] = "\\pdfglyph";
    static char t3_end_glyph_str[] = "\\endglyph";
    int glyph_index;
    int width, height, depth, llx, lly, urx, ury;
    char *p;
    t3_getline();
    if (t3_prefix(t3_begin_glyph_str)) {
        if (sscanf(t3_line_array + strlen(t3_begin_glyph_str) + 1,
                   "%i %i %i %i %i %i %i %i =", &glyph_index,
                   &width, &height, &depth, &llx, &lly, &urx, &ury) != 8) {
            remove_eol(p, t3_line_array);
            pdftex_fail("invalid glyph preamble: `%s'", t3_line_array);
        }
        if (glyph_index < fontbc[f] || glyph_index > fontec[f])
            return;
    } else
        return;
    if (!pdfcharmarked(f, glyph_index)) {
        while (!t3_prefix(t3_end_glyph_str)) {
            t3_check_eof();
            t3_getline();
        }
        return;
    }
    update_bbox(llx, lly, urx, ury, t3_glyph_num == 0);
    t3_glyph_num++;
    pdfnewdict(0, 0, 0);
    t3_char_procs[glyph_index] = objptr;
    if (width == 0)
        t3_char_widths[glyph_index] =
            (getcharwidth(f, glyph_index) / t3_font_scale) / pdffontsize[f];
    else
        t3_char_widths[glyph_index] = width;
    pdfbeginstream();
    t3_getline();
    pdf_printf("%i 0 %i %i %i %i d1\nq\n",
               (int) t3_char_widths[glyph_index], (int) llx,
               (int) lly, (int) urx, (int) ury);
    while (!t3_prefix(t3_end_glyph_str)) {
        t3_check_eof();
        if (t3_prefix("BI"))
            t3_image_used = true;
        t3_putline();
        t3_getline();
    }
    pdf_puts("Q\n");
    pdfendstream();
}

static integer get_pk_font_scale(internalfontnumber f)
{
    return
        dividescaled(pkscalefactor,
                     dividescaled(pdffontsize[f], onehundredbp,
                                  fixeddecimaldigits + 2), 0);
}

static integer pk_char_width(internalfontnumber f, scaled w)
{
    return
        dividescaled(dividescaled(w, pdffontsize[f], 7),
                     get_pk_font_scale(f), 0);
}

scaled getpkcharwidth(internalfontnumber f, scaled w)
{
    return (get_pk_font_scale(f) / 100000.0) *
        (pk_char_width(f, w) / 100.0) * pdffontsize[f];
}

static boolean writepk(internalfontnumber f)
{
    kpse_glyph_file_type font_ret;
    integer llx, lly, urx, ury;
    integer cw, rw, i, j;
    halfword *row;
    char *name;
    chardesc cd;
    boolean is_null_glyph, check_preamble;
    integer dpi;
    dpi =
        kpse_magstep_fix(round
                         (fixedpkresolution *
                          (((float) pdffontsize[f]) / fontdsize[f])),
                         fixedpkresolution, NULL);
    cur_file_name = makecstring(fontname[f]);
    name = kpse_find_pk(cur_file_name, (unsigned) dpi, &font_ret);
    if (name == NULL ||
        !FILESTRCASEEQ(cur_file_name, font_ret.name) ||
        !kpse_bitmap_tolerance((float) font_ret.dpi, (float) dpi)) {
        pdftex_fail("Font %s at %i not found", cur_file_name, (int) dpi);
    }
    t3_file = xfopen(name, FOPEN_RBIN_MODE);
    recorder_record_input(name);
    t3_image_used = true;
    is_pk_font = true;
    tex_printf(" <%s", (char *) name);
    cd.rastersize = 256;
    cd.raster = xtalloc(cd.rastersize, halfword);
    check_preamble = true;
    while (readchar(check_preamble, &cd) != 0) {
        check_preamble = false;
        if (!pdfcharmarked(f, cd.charcode))
            continue;
        t3_char_widths[cd.charcode] =
            pk_char_width(f, getcharwidth(f, cd.charcode));
        if (cd.cwidth < 1 || cd.cheight < 1) {
            cd.xescape = cd.cwidth = round(t3_char_widths[cd.charcode] / 100.0);
            cd.cheight = 1;
            cd.xoff = 0;
            cd.yoff = 0;
            is_null_glyph = true;
        } else
            is_null_glyph = false;
        llx = -cd.xoff;
        lly = cd.yoff - cd.cheight + 1;
        urx = cd.cwidth + llx + 1;
        ury = cd.cheight + lly;
        update_bbox(llx, lly, urx, ury, t3_glyph_num == 0);
        t3_glyph_num++;
        pdfnewdict(0, 0, 0);
        t3_char_procs[cd.charcode] = objptr;
        pdfbeginstream();
        pdfprintreal(t3_char_widths[cd.charcode], 2);
        pdf_printf(" 0 %i %i %i %i d1\n",
                   (int) llx, (int) lly, (int) urx, (int) ury);
        if (is_null_glyph)
            goto end_stream;
        pdf_printf("q\n%i 0 0 %i %i %i cm\nBI\n", (int) cd.cwidth,
                   (int) cd.cheight, (int) llx, (int) lly);
        pdf_printf("/W %i\n/H %i\n", (int) cd.cwidth, (int) cd.cheight);
        pdf_puts("/IM true\n/BPC 1\n/D [1 0]\nID ");
        cw = (cd.cwidth + 7) / 8;
        rw = (cd.cwidth + 15) / 16;
        row = cd.raster;
        for (i = 0; i < cd.cheight; i++) {
            for (j = 0; j < rw - 1; j++) {
                pdfout(*row / 256);
                pdfout(*row % 256);
                row++;
            }
            pdfout(*row / 256);
            if (2 * rw == cw)
                pdfout(*row % 256);
            row++;
        }
        pdf_puts("\nEI\nQ\n");
      end_stream:
        pdfendstream();
    }
    xfree(cd.raster);
    cur_file_name = NULL;
    return true;
}

/* We must remove duplicate glyph names since the duplicates render the
   output invalid. We give a warning, since this should only happen with
   ill-defined .enc files that should get fixed. */
static void remove_duplicate_glyph_names(char **g, const char *encname)
{
    struct avl_table *gl_tree;
    char *aa;
    int i;
    gl_tree = avl_create(comp_string_entry, NULL, &avl_xallocator);
    assert(gl_tree != NULL);
    for (i = 0; i < 256; i++) {
        if (g[i] == notdef)
            continue;
        aa = (char *) avl_find(gl_tree, g[i]);
        if (aa == NULL) {
            aa = (char *) avl_probe(gl_tree, g[i]);
            assert(aa != NULL);
        } else {
            pdftex_warn("%s: duplicate glyph name at position %d: %s",
                        encname, i, g[i]);
            xfree(g[i]);
            g[i] = (char *) notdef;
        }
    }
    avl_destroy(gl_tree, NULL);
}

void writet3(fm_entry * fm, int objnum, internalfontnumber f)
{
    static char t3_font_scale_str[] = "\\pdffontscale";
    int i;
    integer wptr, eptr, cptr;
    int first_char, last_char;
    integer pk_font_scale;
    boolean is_notdef;
    fe_entry *fe;
    char **glyph_names;
    integer tounicode_objnum;
    t3_glyph_num = 0;
    t3_image_used = false;
    for (i = 0; i < 256; i++) {
        t3_char_procs[i] = 0;
        t3_char_widths[i] = 0;
    }
    fe = fm && fm->encname ? get_fe_entry(fm->encname) : NULL;
    glyph_names = fe ? fe->glyph_names : NULL;
    if (glyph_names)
        remove_duplicate_glyph_names(glyph_names, fm->encname);
    packfilename(fontname[f], getnullstr(), maketexstring(".pgc"));
    cur_file_name = makecstring(makenamestring());
    is_pk_font = false;
    if (!t3_open()) {
        if (writepk(f))
            goto write_font_dict;
        else {
            cur_file_name = NULL;
            return;
        }
    }
    tex_printf("<%s", nameoffile + 1);
    t3_getline();
    if (!t3_prefix(t3_font_scale_str) ||
        sscanf(t3_line_array + strlen(t3_font_scale_str) + 1, "%g",
               &t3_font_scale) < 1 || t3_font_scale <= 0
        || t3_font_scale > 1000) {
        pdftex_fail("missing or invalid font scale");
    }
    while (!t3_eof())
        t3_write_glyph(f);
  write_font_dict:
    for (i = fontbc[f]; i <= fontec[f]; i++)
        if (pdfcharmarked(f, i))
            break;
    first_char = i;
    for (i = fontec[f]; i > first_char; i--)
        if (pdfcharmarked(f, i))
            break;
    last_char = i;
    /* write ToUnicode entry if we can */
    if (fixedgentounicode > 0 && !pdffontnobuiltintounicode[f] && fe != NULL)
        tounicode_objnum = write_tounicode(glyph_names, fm->tfm_name, fe->name);
    else
        tounicode_objnum = 0;
    pdfbegindict(objnum, 1);    /* Type 3 font dictionary */
    pdf_puts("/Type /Font\n/Subtype /Type3\n");
    pdf_printf("/Name /F%i\n", (int) f);
    if (pdffontattr[f] != getnullstr()) {
        pdfprint(pdffontattr[f]);
        pdf_puts("\n");
    }
    if (is_pk_font) {
        pk_font_scale = get_pk_font_scale(f);
        pdf_puts("/FontMatrix [");
        pdfprintreal(pk_font_scale, 5);
        pdf_puts(" 0 0 ");
        pdfprintreal(pk_font_scale, 5);
        pdf_puts(" 0 0]\n");
    } else
        pdf_printf("/FontMatrix [%g 0 0 %g 0 0]\n",
                   (double) t3_font_scale, (double) t3_font_scale);
    pdf_printf("/%s [ %i %i %i %i ]\n",
               font_key[FONTBBOX1_CODE].pdfname,
               (int) t3_b0, (int) t3_b1, (int) t3_b2, (int) t3_b3);
    pdf_printf("/Resources << /ProcSet [ /PDF %s] >>\n",
               t3_image_used ? "/ImageB " : "");
    pdf_printf("/FirstChar %i\n/LastChar %i\n", first_char, last_char);
    wptr = pdfnewobjnum();
    eptr = pdfnewobjnum();
    cptr = pdfnewobjnum();
    pdf_printf("/Widths %i 0 R\n/Encoding %i 0 R\n/CharProcs %i 0 R\n",
               (int) wptr, (int) eptr, (int) cptr);
    if (tounicode_objnum != 0)
        pdf_printf("/ToUnicode %i 0 R\n", (int) tounicode_objnum);
    pdfenddict();
    pdfbeginobj(wptr, 1);       /* chars width array */
    pdf_puts("[");
    if (is_pk_font)
        for (i = first_char; i <= last_char; i++) {
            pdfprintreal(t3_char_widths[i], 2);
            pdf_puts(" ");
    } else
        for (i = first_char; i <= last_char; i++)
            pdf_printf("%i ", (int) t3_char_widths[i]);
    pdf_puts("]\n");
    pdfendobj();
    pdfbegindict(eptr, 1);      /* encoding dictionary */
    pdf_printf("/Type /Encoding\n/Differences [%i", first_char);
    if (t3_char_procs[first_char] == 0) {
        pdf_printf("/%s", notdef);
        is_notdef = true;
    } else {
        if (glyph_names
            && glyph_names[first_char]
            && glyph_names[first_char] != notdef)
            pdf_printf("/%s", glyph_names[first_char]);
        else
            pdf_printf("/a%i", first_char);
        is_notdef = false;
    }
    for (i = first_char + 1; i <= last_char; i++) {
        if (t3_char_procs[i] == 0) {
            if (!is_notdef) {
                pdf_printf(" %i/%s", i, notdef);
                is_notdef = true;
            }
        } else {
            if (is_notdef) {
                pdf_printf(" %i", i);
                is_notdef = false;
            }
            if (glyph_names && glyph_names[i] && glyph_names[i] != notdef)
                pdf_printf("/%s", glyph_names[i]);
            else
                pdf_printf("/a%i", i);
        }
    }
    pdf_puts("]\n");
    pdfenddict();
    pdfbegindict(cptr, 1);      /* CharProcs dictionary */
    for (i = first_char; i <= last_char; i++)
        if (t3_char_procs[i] != 0) {
            if (glyph_names && glyph_names[i] && glyph_names[i] != notdef)
                pdf_printf("/%s %i 0 R\n", glyph_names[i],
                           (int) t3_char_procs[i]);
            else
                pdf_printf("/a%i %i 0 R\n", (int) i,
                           (int) t3_char_procs[i]);
        }
    pdfenddict();
    t3_close();
    tex_printf(">");
    cur_file_name = NULL;
}