~ubuntu-branches/ubuntu/natty/luatex/natty

« back to all changes in this revision

Viewing changes to source/texk/web2c/luatexdir/font/writefont.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2009-12-25 09:47:05 UTC
  • mfrom: (1.1.9 upstream) (4.2.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091225094705-y33rpflo8t4u9nag
Tags: 0.50.0-1
* new upstream release
* disable fix-hurd-ftbfs patch, included upstream
* disable upstram-fixes, included upstream
* disable ubuntu_libpoppler-0.11, not needed anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* writefont.c
2
 
   
 
2
 
3
3
   Copyright 1996-2006 Han The Thanh <thanh@pdftex.org>
4
4
   Copyright 2006-2009 Taco Hoekwater <taco@luatex.org>
5
5
 
18
18
   You should have received a copy of the GNU General Public License along
19
19
   with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
20
20
 
 
21
static const char _svn_version[] =
 
22
    "$Id: writefont.c 3285 2009-12-23 19:37:18Z hhenkel $ "
 
23
    "$URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.50.0/source/texk/web2c/luatexdir/font/writefont.c $";
 
24
 
21
25
#include "ptexlib.h"
22
26
#include "luatexfont.h"
23
27
 
24
 
static const char _svn_version[] =
25
 
    "$Id: writefont.c 2331 2009-04-18 16:39:50Z hhenkel $ "
26
 
    "$URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.40.6/source/texk/web2c/luatexdir/font/writefont.c $";
27
 
 
28
 
void write_cid_fontdictionary(fo_entry * fo, internalfontnumber f);
29
 
void create_cid_fontdictionary(fm_entry * fm, integer font_objnum,
 
28
void write_cid_fontdictionary(PDF pdf, fo_entry * fo, internalfontnumber f);
 
29
void create_cid_fontdictionary(PDF pdf,
 
30
                               fm_entry * fm, int font_objnum,
30
31
                               internalfontnumber f);
31
32
 
32
33
const key_entry font_key[FONT_KEYS_NUM] = {
57
58
 
58
59
static int comp_fd_entry(const void *pa, const void *pb, void *p)
59
60
{
60
 
    int i;
61
61
    const fd_entry *p1 = (const fd_entry *) pa, *p2 = (const fd_entry *) pb;
62
62
    (void) p;
63
63
    assert(p1->fm != NULL && is_fontfile(p1->fm) &&
64
64
           p2->fm != NULL && is_fontfile(p2->fm));
65
 
    if ((i = strcmp(p1->fm->ff_name, p2->fm->ff_name)) != 0)
66
 
        return i;
67
 
    cmp_return(p1->fm->slant, p2->fm->slant);
68
 
    cmp_return(p1->fm->extend, p2->fm->extend);
69
 
    return 0;
 
65
    return strcmp(p1->fm->ff_name, p2->fm->ff_name);
70
66
}
71
67
 
72
68
/**********************************************************************/
126
122
static void preset_fontmetrics(fd_entry * fd, internalfontnumber f)
127
123
{
128
124
    int i;
129
 
    fd->font_dim[ITALIC_ANGLE_CODE].val =
130
 
        divide_scaled(-atan(get_slant(f) / 65536.0) * (180 / M_PI),
131
 
                      pdf_font_size(f), 3);
 
125
    fd->font_dim[ITALIC_ANGLE_CODE].val = 0;
132
126
    fd->font_dim[ASCENT_CODE].val =
133
127
        divide_scaled(char_height(f, 'h'), pdf_font_size(f), 3);
134
128
    fd->font_dim[CAPHEIGHT_CODE].val =
183
177
    }
184
178
}
185
179
 
186
 
static void write_fontmetrics(fd_entry * fd)
 
180
static void write_fontmetrics(PDF pdf, fd_entry * fd)
187
181
{
188
182
    int i;
189
183
    fix_fontmetrics(fd);
190
 
    pdf_printf("/%s [%i %i %i %i]\n", font_key[FONTBBOX1_CODE].pdfname,
 
184
    pdf_printf(pdf, "/%s [%i %i %i %i]\n", font_key[FONTBBOX1_CODE].pdfname,
191
185
               (int) fd->font_dim[FONTBBOX1_CODE].val,
192
186
               (int) fd->font_dim[FONTBBOX2_CODE].val,
193
187
               (int) fd->font_dim[FONTBBOX3_CODE].val,
194
188
               (int) fd->font_dim[FONTBBOX4_CODE].val);
195
189
    for (i = 0; i < GEN_KEY_NUM; i++)
196
190
        if (fd->font_dim[i].set)
197
 
            pdf_printf("/%s %i\n", font_key[i].pdfname, fd->font_dim[i].val);
 
191
            pdf_printf(pdf, "/%s %i\n", font_key[i].pdfname,
 
192
                       fd->font_dim[i].val);
198
193
}
199
194
 
200
195
/**********************************************************************/
201
196
 
202
 
static void preset_fontname(fo_entry * fo)
 
197
static void preset_fontname(fo_entry * fo, internalfontnumber f)
203
198
{
204
199
    if (fo->fm->ps_name != NULL)
205
200
        fo->fd->fontname = xstrdup(fo->fm->ps_name);    /* just fallback */
 
201
    else if (font_fullname(f) != NULL)
 
202
        fo->fd->fontname = xstrdup(font_fullname(f));
206
203
    else
207
204
        fo->fd->fontname = xstrdup(fo->fm->tfm_name);
208
205
}
209
206
 
210
 
static void write_fontname(fd_entry * fd, char *key)
 
207
static void write_fontname(PDF pdf, fd_entry * fd, char *key)
211
208
{
212
209
    assert(fd->fontname != NULL);
213
 
    pdf_puts("/");
 
210
    pdf_puts(pdf, "/");
214
211
    if (key != NULL)
215
 
        pdf_printf("%s /", key);
 
212
        pdf_printf(pdf, "%s /", key);
216
213
    if (fd->subset_tag != NULL)
217
 
        pdf_printf("%s+", fd->subset_tag);
218
 
    pdf_printf("%s\n", fd->fontname);
 
214
        pdf_printf(pdf, "%s+", fd->subset_tag);
 
215
    pdf_printf(pdf, "%s\n", fd->fontname);
219
216
}
220
217
 
221
 
static void write_fontname_object(fd_entry * fd)
 
218
static void write_fontname_object(PDF pdf, fd_entry * fd)
222
219
{
223
220
    assert(fd->fn_objnum != 0);
224
 
    pdf_begin_obj(fd->fn_objnum, 1);
225
 
    write_fontname(fd, NULL);
226
 
    pdf_end_obj();
 
221
    pdf_begin_obj(pdf, fd->fn_objnum, 1);
 
222
    write_fontname(pdf, fd, NULL);
 
223
    pdf_end_obj(pdf);
227
224
}
228
225
 
229
226
/**********************************************************************/
230
227
 
231
 
fd_entry *lookup_fd_entry(char *s, integer slant, integer extend)
 
228
fd_entry *lookup_fd_entry(char *s)
232
229
{
233
230
    fd_entry fd;
234
231
    fm_entry fm;
235
232
    assert(s != NULL);
236
233
    fm.ff_name = s;
237
 
    fm.slant = slant;
238
 
    fm.extend = extend;
239
234
    fd.fm = &fm;
240
235
    if (fd_tree == NULL) {
241
236
        fd_tree = avl_create(comp_fd_entry, NULL, &avl_xallocator);
249
244
    assert(fo != NULL);
250
245
    assert(fo->fm != NULL);
251
246
    assert(is_fontfile(fo->fm));
252
 
    return lookup_fd_entry(fo->fm->ff_name, fo->fm->slant, fo->fm->extend);
 
247
    return lookup_fd_entry(fo->fm->ff_name);
253
248
}
254
249
 
255
250
void register_fd_entry(fd_entry * fd)
260
255
        assert(fd_tree != NULL);
261
256
    }
262
257
    assert(fd != NULL && fd->fm != NULL && is_fontfile(fd->fm));
263
 
    assert(lookup_fd_entry(fd->fm->ff_name, fd->fm->slant, fd->fm->extend) == NULL);    /* font descriptor not yet registered */
 
258
    assert(lookup_fd_entry(fd->fm->ff_name) == NULL);   /* font descriptor not yet registered */
264
259
    aa = avl_probe(fd_tree, fd);
265
260
    assert(aa != NULL);
266
261
}
271
266
    assert(fo->fm != NULL);
272
267
    assert(fo->fd == NULL);
273
268
    fo->fd = new_fd_entry();
274
 
    preset_fontname(fo);
 
269
    preset_fontname(fo, f);
275
270
    preset_fontmetrics(fo->fd, f);
276
271
    fo->fd->fe = fo->fe;        /* encoding needed by TrueType writing */
277
272
    fo->fd->fm = fo->fm;        /* map entry needed by TrueType writing */
279
274
    assert(fo->fd->gl_tree != NULL);
280
275
}
281
276
 
282
 
integer get_fd_objnum(fd_entry * fd)
283
 
{
284
 
    assert(fd->fd_objnum != 0);
285
 
    return fd->fd_objnum;
286
 
}
287
 
 
288
 
integer get_fn_objnum(fd_entry * fd)
289
 
{
290
 
    if (fd->fn_objnum == 0)
291
 
        fd->fn_objnum = pdf_new_objnum();
292
 
    return fd->fn_objnum;
293
 
}
294
 
 
295
 
void embed_whole_font(fd_entry * fd)
296
 
{
297
 
    fd->all_glyphs = true;
298
 
}
299
 
 
300
277
/**********************************************************************/
301
278
/*
302
279
 * For all used characters of TeX font f, get corresponding glyph names
387
364
        return 1;
388
365
}
389
366
 
390
 
static void write_charwidth_array(fo_entry * fo, internalfontnumber f)
 
367
static void write_charwidth_array(PDF pdf, fo_entry * fo, internalfontnumber f)
391
368
{
392
369
    int i, j, *ip, *fip;
393
370
    struct avl_traverser t;
394
371
    assert(fo->tx_tree != NULL);
395
372
    assert(fo->cw_objnum == 0);
396
 
    fo->cw_objnum = pdf_new_objnum();
397
 
    pdf_begin_obj(fo->cw_objnum, 1);
 
373
    fo->cw_objnum = pdf_new_objnum(pdf);
 
374
    pdf_begin_obj(pdf, fo->cw_objnum, 1);
398
375
    avl_t_init(&t, fo->tx_tree);
399
376
    fip = (int *) avl_t_first(&t, fo->tx_tree);
400
377
    assert(fip != NULL);
401
 
    pdf_puts("[");
 
378
    pdf_puts(pdf, "[");
402
379
    for (ip = fip, j = *ip; ip != NULL; ip = (int *) avl_t_next(&t)) {
403
380
        if (ip != fip)
404
 
            pdf_puts(" ");
 
381
            pdf_puts(pdf, " ");
405
382
        i = *ip;
406
383
        while (j < i - 1) {
407
 
            pdf_puts("0 ");
 
384
            pdf_puts(pdf, "0 ");
408
385
            j++;
409
386
        }
410
387
        j = i;
411
 
        pdf_print_charwidth(f, i);
 
388
        pdf_print_charwidth(pdf, f, i);
412
389
    }
413
 
    pdf_puts("]\n");
414
 
    pdf_end_obj();
 
390
    pdf_puts(pdf, "]\n");
 
391
    pdf_end_obj(pdf);
415
392
}
416
393
 
417
394
/**********************************************************************/
451
428
 
452
429
/**********************************************************************/
453
430
 
454
 
static void write_fontfile(fd_entry * fd)
 
431
static void write_fontfile(PDF pdf, fd_entry * fd)
455
432
{
456
433
    assert(is_included(fd->fm));
457
434
    if (is_cidkeyed(fd->fm)) {
458
435
        if (is_opentype(fd->fm))
459
 
            writetype0(fd);
 
436
            writetype0(pdf, fd);
460
437
        else if (is_truetype(fd->fm))
461
 
            writetype2(fd);
 
438
            writetype2(pdf, fd);
462
439
        else if (is_type1(fd->fm))
463
 
            writetype1w(fd);
 
440
            writetype1w(pdf, fd);
464
441
        else
465
442
            assert(0);
466
443
    } else {
467
444
        if (is_type1(fd->fm))
468
 
            writet1(fd);
 
445
            writet1(pdf, fd);
469
446
        else if (is_truetype(fd->fm))
470
 
            writettf(fd);
 
447
            writettf(pdf, fd);
471
448
        else if (is_opentype(fd->fm))
472
 
            writeotf(fd);
 
449
            writeotf(pdf, fd);
473
450
        else
474
451
            assert(0);
475
452
    }
476
453
    if (!fd->ff_found)
477
454
        return;
478
455
    assert(fd->ff_objnum == 0);
479
 
    fd->ff_objnum = pdf_new_objnum();
480
 
    pdf_begin_dict(fd->ff_objnum, 0);   /* font file stream */
 
456
    fd->ff_objnum = pdf_new_objnum(pdf);
 
457
    pdf_begin_dict(pdf, fd->ff_objnum, 0);      /* font file stream */
481
458
    if (is_cidkeyed(fd->fm)) {
482
459
        /* No subtype is used for TrueType-based OpenType fonts */
483
460
        if (is_opentype(fd->fm) || is_type1(fd->fm))
484
 
            pdf_puts("/Subtype /CIDFontType0C\n");
 
461
            pdf_puts(pdf, "/Subtype /CIDFontType0C\n");
485
462
        /* else
486
 
           pdf_puts("/Subtype /OpenType\n"); */
 
463
           pdf_puts(pdf,"/Subtype /OpenType\n"); */
487
464
    } else {
488
465
        if (is_type1(fd->fm))
489
 
            pdf_printf("/Length1 %i\n/Length2 %i\n/Length3 %i\n",
 
466
            pdf_printf(pdf, "/Length1 %i\n/Length2 %i\n/Length3 %i\n",
490
467
                       (int) t1_length1, (int) t1_length2, (int) t1_length3);
491
468
        else if (is_truetype(fd->fm))
492
 
            pdf_printf("/Length1 %i\n", (int) ttf_length);
 
469
            pdf_printf(pdf, "/Length1 %i\n", (int) ttf_length);
493
470
        else if (is_opentype(fd->fm))
494
 
            pdf_puts("/Subtype /Type1C\n");
 
471
            pdf_puts(pdf, "/Subtype /Type1C\n");
495
472
        else
496
473
            assert(0);
497
474
    }
498
 
    pdf_begin_stream();
499
 
    fb_flush();
500
 
    pdf_end_stream();
 
475
    pdf_begin_stream(pdf);
 
476
    fb_flush(pdf);
 
477
    pdf_end_stream(pdf);
501
478
}
502
479
 
503
480
/**********************************************************************/
504
481
 
505
 
static void write_fontdescriptor(fd_entry * fd)
 
482
static void write_fontdescriptor(PDF pdf, fd_entry * fd)
506
483
{
507
484
    static const int std_flags[] = {
508
 
        /* indices for << start with 0, but bits start with 1, so the numbers 
 
485
        /* indices for << start with 0, but bits start with 1, so the numbers
509
486
         * for << are 1 lower than the bits in table 5.20 */
510
487
        /* *INDENT-OFF* */
511
488
        1 + 2 + (1 << 5),                       /* Courier */
529
506
    int fd_flags;
530
507
    assert(fd != NULL && fd->fm != NULL);
531
508
 
532
 
    if (is_fontfile(fd->fm))
533
 
        write_fontfile(fd);     /* this will set fd->ff_found if font file is found */
 
509
    if (is_fontfile(fd->fm) && is_included(fd->fm))
 
510
        write_fontfile(pdf, fd);        /* this will set fd->ff_found if font file is found */
534
511
    if (fd->fn_objnum != 0)
535
 
        write_fontname_object(fd);
 
512
        write_fontname_object(pdf, fd);
536
513
    if (fd->fd_objnum == 0)
537
 
        fd->fd_objnum = pdf_new_objnum();
538
 
    pdf_begin_dict(fd->fd_objnum, 1);
539
 
    pdf_puts("/Type /FontDescriptor\n");
540
 
    write_fontname(fd, "FontName");
 
514
        fd->fd_objnum = pdf_new_objnum(pdf);
 
515
    pdf_begin_dict(pdf, fd->fd_objnum, 1);
 
516
    pdf_puts(pdf, "/Type /FontDescriptor\n");
 
517
    write_fontname(pdf, fd, "FontName");
541
518
    if (fd->fm->fd_flags != FD_FLAGS_NOT_SET_IN_MAPLINE)
542
519
        fd_flags = (int) fd->fm->fd_flags;
543
520
    else if (fd->ff_found)
552
529
             fd->fm->ps_name != NULL ? fd->fm->ps_name : "No name given",
553
530
             fd->fm->tfm_name, fd_flags);
554
531
    }
555
 
    pdf_printf("/Flags %i\n", fd_flags);
556
 
    write_fontmetrics(fd);
 
532
    pdf_printf(pdf, "/Flags %i\n", fd_flags);
 
533
    write_fontmetrics(pdf, fd);
557
534
    if (is_cidkeyed(fd->fm)) {
558
535
        if (is_type1(fd->fm))
559
 
            pdf_printf("/FontFile3 %i 0 R\n", (int) fd->ff_objnum);
 
536
            pdf_printf(pdf, "/FontFile3 %i 0 R\n", (int) fd->ff_objnum);
560
537
        else if (is_truetype(fd->fm))
561
 
            pdf_printf("/FontFile2 %i 0 R\n", (int) fd->ff_objnum);
 
538
            pdf_printf(pdf, "/FontFile2 %i 0 R\n", (int) fd->ff_objnum);
562
539
        else if (is_opentype(fd->fm))
563
 
            pdf_printf("/FontFile3 %i 0 R\n", (int) fd->ff_objnum);
 
540
            pdf_printf(pdf, "/FontFile3 %i 0 R\n", (int) fd->ff_objnum);
564
541
        else
565
542
            assert(0);
566
543
    } else {
569
546
                /* /CharSet is optional; names may appear in any order */
570
547
                assert(fd->gl_tree != NULL);
571
548
                avl_t_init(&t, fd->gl_tree);
572
 
                pdf_puts("/CharSet (");
 
549
                pdf_puts(pdf, "/CharSet (");
573
550
                for (glyph = (char *) avl_t_first(&t, fd->gl_tree);
574
551
                     glyph != NULL; glyph = (char *) avl_t_next(&t))
575
 
                    pdf_printf("/%s", glyph);
576
 
                pdf_puts(")\n");
 
552
                    pdf_printf(pdf, "/%s", glyph);
 
553
                pdf_puts(pdf, ")\n");
577
554
            }
578
555
            if (is_type1(fd->fm))
579
 
                pdf_printf("/FontFile %i 0 R\n", (int) fd->ff_objnum);
 
556
                pdf_printf(pdf, "/FontFile %i 0 R\n", (int) fd->ff_objnum);
580
557
            else if (is_truetype(fd->fm))
581
 
                pdf_printf("/FontFile2 %i 0 R\n", (int) fd->ff_objnum);
 
558
                pdf_printf(pdf, "/FontFile2 %i 0 R\n", (int) fd->ff_objnum);
582
559
            else if (is_opentype(fd->fm))
583
 
                pdf_printf("/FontFile3 %i 0 R\n", (int) fd->ff_objnum);
 
560
                pdf_printf(pdf, "/FontFile3 %i 0 R\n", (int) fd->ff_objnum);
584
561
            else
585
562
                assert(0);
586
563
        }
587
564
    }
588
 
    /* TODO: Optional keys for CID fonts. 
 
565
    /* TODO: Optional keys for CID fonts.
589
566
 
590
567
       The most interesting ones are
591
568
       /Style << /Panose <12-byte string>>>
594
571
       the latter can be used in subsets, to give the included CIDs
595
572
       as a bitmap on the whole list.
596
573
     */
597
 
    pdf_end_dict();
 
574
    pdf_end_dict(pdf);
598
575
}
599
576
 
600
 
void write_fontdescriptors()
 
577
void write_fontdescriptors(PDF pdf)
601
578
{
602
579
    fd_entry *fd;
603
580
    struct avl_traverser t;
606
583
    avl_t_init(&t, fd_tree);
607
584
    for (fd = (fd_entry *) avl_t_first(&t, fd_tree); fd != NULL;
608
585
         fd = (fd_entry *) avl_t_next(&t))
609
 
        write_fontdescriptor(fd);
 
586
        write_fontdescriptor(pdf, fd);
610
587
}
611
588
 
612
589
/**********************************************************************/
613
590
 
614
 
void write_fontdictionary(fo_entry * fo)
 
591
void write_fontdictionary(PDF pdf, fo_entry * fo)
615
592
{
616
593
    assert(fo != NULL);
617
594
    assert(fo->fm != NULL);
618
595
    assert(fo->fo_objnum != 0); /* reserved as pdf_font_num(f) in pdftex.web */
619
596
 
620
597
    /* write ToUnicode entry if needed */
621
 
    if (fixed_gen_tounicode > 0 && fo->fd != NULL) {
 
598
    if (pdf->gen_tounicode > 0 && fo->fd != NULL) {
622
599
        if (fo->fe != NULL) {
623
600
            fo->tounicode_objnum =
624
 
                write_tounicode(fo->fe->glyph_names, fo->fe->name);
 
601
                write_tounicode(pdf, fo->fe->glyph_names, fo->fe->name);
625
602
        } else if (is_type1(fo->fm)) {
626
603
            assert(fo->fd->builtin_glyph_names != NULL);
627
604
            fo->tounicode_objnum =
628
 
                write_tounicode(fo->fd->builtin_glyph_names, fo->fm->tfm_name);
 
605
                write_tounicode(pdf, fo->fd->builtin_glyph_names,
 
606
                                fo->fm->tfm_name);
629
607
        }
630
608
    }
631
 
 
632
 
    pdf_begin_dict(fo->fo_objnum, 1);
633
 
    pdf_puts("/Type /Font\n");
634
 
    pdf_puts("/Subtype /");
 
609
    pdf_begin_dict(pdf, fo->fo_objnum, 1);
 
610
    pdf_puts(pdf, "/Type /Font\n");
 
611
    pdf_puts(pdf, "/Subtype /");
635
612
    if (is_type1(fo->fm))
636
 
        pdf_printf("%s\n", "Type1");
 
613
        pdf_printf(pdf, "%s\n", "Type1");
637
614
    else if (is_truetype(fo->fm))
638
 
        pdf_printf("%s\n", "TrueType");
 
615
        pdf_printf(pdf, "%s\n", "TrueType");
639
616
    else if (is_opentype(fo->fm))
640
 
        pdf_printf("%s\n", "Type1");
 
617
        pdf_printf(pdf, "%s\n", "Type1");
641
618
    else
642
619
        assert(0);
643
620
    assert(fo->fd != NULL && fo->fd->fd_objnum != 0);
644
 
    write_fontname(fo->fd, "BaseFont");
645
 
    pdf_printf("/FontDescriptor %i 0 R\n", (int) fo->fd->fd_objnum);
 
621
    write_fontname(pdf, fo->fd, "BaseFont");
 
622
    pdf_printf(pdf, "/FontDescriptor %i 0 R\n", (int) fo->fd->fd_objnum);
646
623
    assert(fo->cw_objnum != 0);
647
 
    pdf_printf("/FirstChar %i\n/LastChar %i\n/Widths %i 0 R\n",
 
624
    pdf_printf(pdf, "/FirstChar %i\n/LastChar %i\n/Widths %i 0 R\n",
648
625
               (int) fo->first_char, (int) fo->last_char, (int) fo->cw_objnum);
649
626
    if ((is_type1(fo->fm) || is_opentype(fo->fm)) && fo->fe != NULL
650
627
        && fo->fe->fe_objnum != 0)
651
 
        pdf_printf("/Encoding %i 0 R\n", (int) fo->fe->fe_objnum);
 
628
        pdf_printf(pdf, "/Encoding %i 0 R\n", (int) fo->fe->fe_objnum);
652
629
    if (fo->tounicode_objnum != 0)
653
 
        pdf_printf("/ToUnicode %i 0 R\n", (int) fo->tounicode_objnum);
654
 
    if (pdf_font_attr(fo->tex_font) != get_nullstr()) {
655
 
        pdf_print(pdf_font_attr(fo->tex_font));
656
 
        pdf_puts("\n");
 
630
        pdf_printf(pdf, "/ToUnicode %i 0 R\n", (int) fo->tounicode_objnum);
 
631
    if (pdf_font_attr(fo->tex_font) != get_nullstr() &&
 
632
        pdf_font_attr(fo->tex_font) != 0) {
 
633
        pdf_print(pdf, pdf_font_attr(fo->tex_font));
 
634
        pdf_puts(pdf, "\n");
657
635
    }
658
 
    pdf_end_dict();
 
636
    pdf_end_dict(pdf);
659
637
}
660
638
 
661
 
void write_fontdictionaries()
 
639
void write_fontdictionaries(PDF pdf)
662
640
{
663
641
    fo_entry *fo;
664
642
    struct avl_traverser t;
667
645
    avl_t_init(&t, fo_tree);
668
646
    for (fo = (fo_entry *) avl_t_first(&t, fo_tree); fo != NULL;
669
647
         fo = (fo_entry *) avl_t_next(&t))
670
 
        write_fontdictionary(fo);
 
648
        write_fontdictionary(pdf, fo);
671
649
}
672
650
 
673
651
/**********************************************************************/
676
654
 * @<Output fonts definition@>= in pdftex.web.
677
655
 */
678
656
 
679
 
void write_fontstuff()
 
657
void write_fontstuff(PDF pdf)
680
658
{
681
 
    write_fontdescriptors();
682
 
    write_fontencodings();      /* see writeenc.c */
683
 
    write_fontdictionaries();
 
659
    write_fontdescriptors(pdf);
 
660
    write_fontencodings(pdf);   /* see writeenc.c */
 
661
    write_fontdictionaries(pdf);
684
662
}
685
663
 
686
664
/**********************************************************************/
687
665
 
688
 
void create_fontdictionary(fm_entry * fm, integer font_objnum,
 
666
void create_fontdictionary(PDF pdf, fm_entry * fm, int font_objnum,
689
667
                           internalfontnumber f)
690
668
{
691
669
    fo_entry *fo = new_fo_entry();
700
678
        fo->fe = get_fe_entry(fo->fm->encname); /* returns NULL if .enc file couldn't be opened */
701
679
        if (fo->fe != NULL && (is_type1(fo->fm) || is_opentype(fo->fm))) {
702
680
            if (fo->fe->fe_objnum == 0)
703
 
                fo->fe->fe_objnum = pdf_new_objnum();   /* then it will be written out */
 
681
                fo->fe->fe_objnum = pdf_new_objnum(pdf);        /* then it will be written out */
704
682
            /* mark encoding pairs used by TeX to optimize encoding vector */
705
683
            fo->fe->tx_tree = mark_chars(fo, fo->fe->tx_tree, f);
706
684
        }
707
685
    }
708
686
    fo->tx_tree = mark_chars(fo, fo->tx_tree, f);       /* for write_charwidth_array() */
709
 
    write_charwidth_array(fo, f);
 
687
    write_charwidth_array(pdf, fo, f);
710
688
    if (!is_builtin(fo->fm)) {
711
689
        if (is_type1(fo->fm)) {
712
690
            if ((fo->fd = lookup_fontdescriptor(fo)) == NULL) {
728
706
            /* mark non-reencoded characters as chars on TeX level */
729
707
            fo->fd->tx_tree = mark_chars(fo, fo->fd->tx_tree, f);
730
708
        if (!is_type1(fo->fm))
731
 
            write_fontdescriptor(fo->fd);
 
709
            write_fontdescriptor(pdf, fo->fd);
732
710
    } else {
733
711
        /* builtin fonts still need the /Widths array and /FontDescriptor
734
712
         * (to avoid error 'font FOO contains bad /BBox')
735
713
         */
736
714
        create_fontdescriptor(fo, f);
737
 
        write_fontdescriptor(fo->fd);
 
715
        write_fontdescriptor(pdf, fo->fd);
738
716
        if (!is_std_t1font(fo->fm))
739
717
            pdftex_warn("font `%s' is not a standard font; "
740
718
                        "I suppose it is available to your PDF viewer then",
743
721
    if (is_type1(fo->fm))
744
722
        register_fo_entry(fo);
745
723
    else
746
 
        write_fontdictionary(fo);
 
724
        write_fontdictionary(pdf, fo);
747
725
}
748
726
 
749
727
/**********************************************************************/
764
742
    return 0;
765
743
}
766
744
 
767
 
void do_pdf_font(integer font_objnum, internalfontnumber f)
 
745
/* this function is in luafontloader/fontforge/fontforge/macbinary.c */
 
746
 
 
747
extern char *FindResourceTtfFont(char *filename, char *fontname);
 
748
 
 
749
void do_pdf_font(PDF pdf, int font_objnum, internalfontnumber f)
768
750
{
 
751
    int del_file = 0;
769
752
    fm_entry *fm;
770
753
    /* This is not 100% true: CID is actually needed whenever (and
771
754
     * only) there are more than 256 separate glyphs used. But for
781
764
        fm = new_fm_entry();
782
765
        fm->tfm_name = font_name(f);    /* or whatever, not a real tfm */
783
766
        fm->ff_name = font_filename(f); /* the actual file */
 
767
        if (font_psname(f) != NULL)
 
768
            fm->ps_name = font_psname(f);       /* the true name */
 
769
        else
 
770
            fm->ps_name = font_fullname(f);     /* the true name */
 
771
        if (fm->ff_name
 
772
            && strlen(fm->ff_name) >= 6
 
773
            && strstr(fm->ff_name,
 
774
                      ".dfont") == (fm->ff_name + strlen(fm->ff_name) - 6)) {
 
775
            /* In case of a .dfont, we will extract the correct ttf here,
 
776
               and adjust fm->ff_name to point to the temporary file.
 
777
               This file will be deleted later. Todo: keep a nicer name
 
778
               somewhere for the terminal message.
 
779
             */
 
780
            char *s = FindResourceTtfFont(fm->ff_name, fm->ps_name);
 
781
            if (s != NULL) {
 
782
                fm->ff_name = s;
 
783
                del_file = 1;
 
784
            } else {
 
785
                pdftex_fail
 
786
                    ("writefont.c: The file (%s) does not contain font `%s'",
 
787
                     fm->ff_name, fm->ps_name);
 
788
            }
 
789
        }
784
790
        fm->encname = font_encodingname(f);     /* for the CIDSystemInfo */
785
 
        fm->ps_name = font_fullname(f); /* the true name */
786
791
        fm->slant = font_slant(f);      /* slant factor */
 
792
        set_slantset(fm);
787
793
        fm->extend = font_extend(f);    /* extension factor */
 
794
        set_extendset(fm);
788
795
        fm->fd_flags = 4;       /* can perhaps be done better */
789
 
        fm->in_use = true;
 
796
        set_inuse(fm);
790
797
 
791
798
        switch (font_format(f)) {
792
799
        case opentype_format:
815
822
            }
816
823
        }
817
824
        set_cidkeyed(fm);
818
 
        create_cid_fontdictionary(fm, font_objnum, f);
 
825
        create_cid_fontdictionary(pdf, fm, font_objnum, f);
 
826
 
 
827
        if (del_file)
 
828
            unlink(fm->ff_name);
819
829
 
820
830
    } else {
821
831
        fm = hasfmentry(f) ? (fm_entry *) font_map(f) : NULL;
822
832
        if (fm == NULL || (fm->ps_name == NULL && fm->ff_name == NULL))
823
 
            writet3(font_objnum, f);
 
833
            writet3(pdf, font_objnum, f);
824
834
        else
825
 
            create_fontdictionary(fm, font_objnum, f);
 
835
            create_fontdictionary(pdf, fm, font_objnum, f);
826
836
    }
827
837
}
828
838
 
829
839
/**********************************************************************/
830
840
 
831
841
 
832
 
/* 
 
842
/*
833
843
   The glyph width is included in |glw_entry|, because that width
834
844
   depends on the value it has in the font where it is actually
835
845
   typeset from, not the font that is the 'owner' of the fd entry.
855
865
    assert(fo->fm != NULL);
856
866
    assert(fo->fd == NULL);
857
867
    fo->fd = new_fd_entry();
858
 
    preset_fontname(fo);
 
868
    preset_fontname(fo, f);
859
869
    preset_fontmetrics(fo->fd, f);
860
870
    fo->fd->fe = fo->fe;        /* encoding needed by TrueType writing */
861
871
    fo->fd->fm = fo->fm;        /* map entry needed by TrueType writing */
864
874
}
865
875
 
866
876
/*
867
 
   The values |font_bc()| and |font_ec()| are potentially large 
 
877
   The values |font_bc()| and |font_ec()| are potentially large
868
878
   character ids, but the strings that are written out use CID
869
879
   indexes, and those are limited to 16-bit values.
870
880
*/
878
888
        if (k == f || -f == pdf_font_num(k)) {
879
889
            l = pdf_font_size(k);
880
890
            for (i = font_bc(k); i <= font_ec(k); i++) {
881
 
                if (char_exists(k, i) && char_used(k, i)) {
 
891
                if (quick_char_exists(k, i) && char_used(k, i)) {
882
892
                    j = xtalloc(1, glw_entry);
883
893
                    j->id = char_index(k, i);
884
894
                    j->wd = divide_scaled_n(char_width(k, i), l, 10000.0);
894
904
    }
895
905
}
896
906
 
897
 
/* 
 
907
/*
898
908
   It is possible to compress the widths array even better, by using the
899
 
   alternate 'range' syntax and possibly even using /DW to set 
 
909
   alternate 'range' syntax and possibly even using /DW to set
900
910
   a default value.
901
 
  
 
911
 
902
912
   There is a some optimization here already: glyphs that are
903
 
   not used do not appear in the widths array at all. 
 
913
   not used do not appear in the widths array at all.
904
914
 
905
915
   We have to make sure that we do not output an (incorrect!)
906
916
   width for a character that exists in the font, but is not used
908
918
*/
909
919
 
910
920
 
911
 
static void write_cid_charwidth_array(fo_entry * fo)
 
921
static void write_cid_charwidth_array(PDF pdf, fo_entry * fo)
912
922
{
913
923
    int i, j;
914
924
    glw_entry *glyph;
915
925
    struct avl_traverser t;
916
926
 
917
927
    assert(fo->cw_objnum == 0);
918
 
    fo->cw_objnum = pdf_new_objnum();
919
 
    pdf_begin_obj(fo->cw_objnum, 1);
 
928
    fo->cw_objnum = pdf_new_objnum(pdf);
 
929
    pdf_begin_obj(pdf, fo->cw_objnum, 1);
920
930
    avl_t_init(&t, fo->fd->gl_tree);
921
931
    glyph = (glw_entry *) avl_t_first(&t, fo->fd->gl_tree);
922
932
    assert(glyph != NULL);
923
933
    i = glyph->id;
924
 
    pdf_printf("[ %i [", i);
 
934
    pdf_printf(pdf, "[ %i [", i);
925
935
    for (; glyph != NULL; glyph = (glw_entry *) avl_t_next(&t)) {
926
936
        j = glyph->wd;
927
937
        if (glyph->id > (unsigned) (i + 1)) {
928
 
            pdf_printf("] %i [", glyph->id);
 
938
            pdf_printf(pdf, "] %i [", glyph->id);
929
939
            j = glyph->wd;
930
940
        }
931
941
        if (glyph->id == (unsigned) (i + 1))
932
 
            pdf_puts(" ");
 
942
            pdf_puts(pdf, " ");
933
943
 
934
944
        if (j < 0) {
935
 
            pdf_puts("-");
 
945
            pdf_puts(pdf, "-");
936
946
            j = -j;
937
947
        }
938
948
 
939
949
        /* pdf_print_charwidth(f, i); */
940
950
 
941
 
        pdf_printf("%i", (j / 10));
 
951
        pdf_printf(pdf, "%i", (j / 10));
942
952
        if ((j % 10) != 0)
943
 
            pdf_printf(".%i", (j % 10));
 
953
            pdf_printf(pdf, ".%i", (j % 10));
944
954
 
945
955
        i = glyph->id;
946
956
    }
947
 
    pdf_puts("]]\n");
948
 
    pdf_end_obj();
 
957
    pdf_puts(pdf, "]]\n");
 
958
    pdf_end_obj(pdf);
949
959
}
950
960
 
951
961
 
952
 
void create_cid_fontdictionary(fm_entry * fm, integer font_objnum,
 
962
void create_cid_fontdictionary(PDF pdf,
 
963
                               fm_entry * fm, int font_objnum,
953
964
                               internalfontnumber f)
954
965
{
955
966
    fo_entry *fo = new_fo_entry();
961
972
    create_cid_fontdescriptor(fo, f);
962
973
    mark_cid_subset_glyphs(fo, f);
963
974
    if (is_subsetted(fo->fm)) {
964
 
        /* 
 
975
        /*
965
976
           this is a bit sneaky. |make_subset_tag()| actually expects the glyph tree
966
977
           to contain strings instead of |glw_entry| items. However, all calculations
967
978
           are done using explicit typecasts, so it works out ok.
968
979
         */
969
980
        make_subset_tag(fo->fd);
970
981
    }
971
 
    write_cid_charwidth_array(fo);
972
 
    write_fontdescriptor(fo->fd);
 
982
    write_cid_charwidth_array(pdf, fo);
 
983
    write_fontdescriptor(pdf, fo->fd);
973
984
 
974
 
    write_cid_fontdictionary(fo, f);
 
985
    write_cid_fontdictionary(pdf, fo, f);
975
986
    xfree(fo);
976
987
}
977
988
 
978
 
void write_cid_fontdictionary(fo_entry * fo, internalfontnumber f)
 
989
void write_cid_fontdictionary(PDF pdf, fo_entry * fo, internalfontnumber f)
979
990
{
980
991
    int i;
981
992
 
982
 
    fo->tounicode_objnum = write_cid_tounicode(fo, f);
 
993
    fo->tounicode_objnum = write_cid_tounicode(pdf, fo, f);
983
994
 
984
 
    pdf_begin_dict(fo->fo_objnum, 1);
985
 
    pdf_puts("/Type /Font\n");
986
 
    pdf_puts("/Subtype /Type0\n");
987
 
    pdf_puts("/Encoding /Identity-H\n");
988
 
    write_fontname(fo->fd, "BaseFont");
989
 
    i = pdf_new_objnum();
990
 
    pdf_printf("/DescendantFonts [%i 0 R]\n", i);
 
995
    pdf_begin_dict(pdf, fo->fo_objnum, 1);
 
996
    pdf_puts(pdf, "/Type /Font\n");
 
997
    pdf_puts(pdf, "/Subtype /Type0\n");
 
998
    pdf_puts(pdf, "/Encoding /Identity-H\n");
 
999
    write_fontname(pdf, fo->fd, "BaseFont");
 
1000
    i = pdf_new_objnum(pdf);
 
1001
    pdf_printf(pdf, "/DescendantFonts [%i 0 R]\n", i);
991
1002
    /* todo: the ToUnicode CMap */
992
1003
    if (fo->tounicode_objnum != 0)
993
 
        pdf_printf("/ToUnicode %i 0 R\n", (int) fo->tounicode_objnum);
994
 
 
995
 
    pdf_end_dict();
996
 
 
997
 
    pdf_begin_dict(i, 1);
998
 
    pdf_puts("/Type /Font\n");
 
1004
        pdf_printf(pdf, "/ToUnicode %i 0 R\n", (int) fo->tounicode_objnum);
 
1005
 
 
1006
    pdf_end_dict(pdf);
 
1007
 
 
1008
    pdf_begin_dict(pdf, i, 1);
 
1009
    pdf_puts(pdf, "/Type /Font\n");
999
1010
    if (is_opentype(fo->fm) || is_type1(fo->fm)) {
1000
 
        pdf_puts("/Subtype /CIDFontType0\n");
 
1011
        pdf_puts(pdf, "/Subtype /CIDFontType0\n");
1001
1012
    } else {
1002
 
        pdf_puts("/Subtype /CIDFontType2\n");
1003
 
        pdf_printf("/CIDToGIDMap /Identity\n");
 
1013
        pdf_puts(pdf, "/Subtype /CIDFontType2\n");
 
1014
        pdf_printf(pdf, "/CIDToGIDMap /Identity\n");
1004
1015
    }
1005
 
    write_fontname(fo->fd, "BaseFont");
1006
 
    pdf_printf("/FontDescriptor %i 0 R\n", (int) fo->fd->fd_objnum);
1007
 
    pdf_printf("/W %i 0 R\n", (int) fo->cw_objnum);
1008
 
    pdf_printf("/CIDSystemInfo <<\n");
1009
 
    pdf_printf("/Registry (%s)\n",
 
1016
    write_fontname(pdf, fo->fd, "BaseFont");
 
1017
    pdf_printf(pdf, "/FontDescriptor %i 0 R\n", (int) fo->fd->fd_objnum);
 
1018
    pdf_printf(pdf, "/W %i 0 R\n", (int) fo->cw_objnum);
 
1019
    pdf_printf(pdf, "/CIDSystemInfo <<\n");
 
1020
    pdf_printf(pdf, "/Registry (%s)\n",
1010
1021
               (font_cidregistry(f) ? font_cidregistry(f) : "Adobe"));
1011
 
    pdf_printf("/Ordering (%s)\n",
 
1022
    pdf_printf(pdf, "/Ordering (%s)\n",
1012
1023
               (font_cidordering(f) ? font_cidordering(f) : "Identity"));
1013
 
    pdf_printf("/Supplement %u\n", (unsigned int) font_cidsupplement(f));
1014
 
    pdf_printf(">>\n");
 
1024
    pdf_printf(pdf, "/Supplement %u\n", (unsigned int) font_cidsupplement(f));
 
1025
    pdf_printf(pdf, ">>\n");
1015
1026
 
1016
1027
    /* I doubt there is anything useful that could be written here */
1017
 
    /*      
 
1028
    /*
1018
1029
       if (pdf_font_attr(fo->tex_font) != get_nullstr()) {
1019
1030
       pdf_print(pdf_font_attr(fo->tex_font));
1020
 
       pdf_puts("\n");
 
1031
       pdf_puts(pdf,"\n");
1021
1032
       }
1022
1033
     */
1023
 
    pdf_end_dict();
 
1034
    pdf_end_dict(pdf);
1024
1035
 
1025
1036
}