1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gdevpsf2.c 8250 2007-09-25 13:31:24Z giles $ */
15
/* Write an embedded CFF font with either Type 1 or Type 2 CharStrings */
16
#include "math_.h" /* for fabs */
34
/* Define additional opcodes used in Dicts, but not in CharStrings. */
38
/* Define the count of standard strings. */
39
#define NUM_STD_STRINGS 391
41
/* Define whether or not to skip writing an empty Subrs Index. */
42
#define SKIP_EMPTY_SUBRS
44
/* Define the structure for the string table. */
45
typedef struct cff_string_item_s {
47
int index1; /* index + 1, 0 means empty */
49
typedef struct cff_string_table_s {
50
cff_string_item_t *items;
57
/* Define the state of the CFF writer. */
58
typedef struct cff_writer_s {
61
gs_font_base *pfont; /* type1 or cid0 */
62
glyph_data_proc_t glyph_data;
65
cff_string_table_t std_strings;
66
cff_string_table_t strings;
69
typedef struct cff_glyph_subset_s {
70
psf_outline_glyphs_t glyphs;
71
int num_encoded; /* glyphs.subset_data[1..num_e] are encoded */
72
int num_encoded_chars; /* Encoding has num_e_chars defined entries */
75
/* ---------------- Output utilities ---------------- */
77
/* ------ String tables ------ */
79
/* Initialize a string table. */
81
cff_string_table_init(cff_string_table_t *pcst, cff_string_item_t *items,
86
memset(items, 0, size * sizeof(*items));
90
while (reprobe != 1 && igcd(size, reprobe) != 1)
91
reprobe = (reprobe * 2 + 1) % size;
93
pcst->reprobe = reprobe;
96
/* Add a string to a string table. */
98
cff_string_add(cff_string_table_t *pcst, const byte *data, uint size)
102
if (pcst->count >= pcst->size)
103
return_error(gs_error_limitcheck);
104
index = pcst->count++;
105
pcst->items[index].key.data = data;
106
pcst->items[index].key.size = size;
111
/* Look up a string, optionally adding it. */
112
/* Return 1 if the string was added. */
114
cff_string_index(cff_string_table_t *pcst, const byte *data, uint size,
115
bool enter, int *pindex)
117
/****** FAILS IF TABLE FULL AND KEY MISSING ******/
118
int j = (size == 0 ? 0 : data[0] * 23 + data[size - 1] * 59 + size);
121
while ((index = pcst->items[j %= pcst->size].index1) != 0) {
123
if (!bytes_compare(pcst->items[index].key.data,
124
pcst->items[index].key.size, data, size)) {
129
if (++c >= pcst->size)
133
return_error(gs_error_undefined);
134
index = cff_string_add(pcst, data, size);
137
pcst->items[j].index1 = index + 1;
142
/* Get the SID for a string or a glyph. */
144
cff_string_sid(cff_writer_t *pcw, const byte *data, uint size)
147
int code = cff_string_index(&pcw->std_strings, data, size, false, &index);
150
code = cff_string_index(&pcw->strings, data, size, true, &index);
153
index += NUM_STD_STRINGS;
158
cff_glyph_sid(cff_writer_t *pcw, gs_glyph glyph)
162
pcw->pfont->procs.glyph_name((gs_font *)pcw->pfont, glyph, &str);
166
return cff_string_sid(pcw, str.data, str.size);
169
/* ------ Low level ------ */
172
put_card16(cff_writer_t *pcw, uint c16)
174
sputc(pcw->strm, (byte)(c16 >> 8));
175
sputc(pcw->strm, (byte)c16);
178
offset_size(uint offset)
183
offset >>= 8, ++size;
187
put_offset(cff_writer_t *pcw, int offset)
191
for (i = pcw->offset_size - 1; i >= 0; --i)
192
sputc(pcw->strm, (byte)(offset >> (i * 8)));
195
put_bytes(stream * s, const byte *ptr, uint count)
199
sputs(s, ptr, count, &used);
203
check_ioerror(stream * s)
207
return sputs(s, (byte *)&used, 0, &used);
210
/* ------ Data types ------ */
214
cff_put_op(cff_writer_t *pcw, int op)
216
if (op >= CE_OFFSET) {
217
sputc(pcw->strm, cx_escape);
218
sputc(pcw->strm, (byte)(op - CE_OFFSET));
220
sputc(pcw->strm, (byte)op);
223
cff_put_int(cff_writer_t *pcw, int i)
225
stream *s = pcw->strm;
227
if (i >= -107 && i <= 107)
228
sputc(s, (byte)(i + 139));
229
else if (i <= 1131 && i >= 0)
230
put_card16(pcw, (c_pos2_0 << 8) + i - 108);
231
else if (i >= -1131 && i < 0)
232
put_card16(pcw, (c_neg2_0 << 8) - i - 108);
233
else if (i >= -32768 && i <= 32767) {
234
sputc(s, c2_shortint);
235
put_card16(pcw, i & 0xffff);
237
sputc(s, CD_LONGINT);
238
put_card16(pcw, i >> 16);
239
put_card16(pcw, i & 0xffff);
243
cff_put_int_value(cff_writer_t *pcw, int i, int op)
249
cff_put_int_if_ne(cff_writer_t *pcw, int i, int i_default, int op)
252
cff_put_int_value(pcw, i, op);
255
cff_put_bool(cff_writer_t *pcw, bool b)
257
cff_put_int(pcw, (b ? 1 : 0));
260
cff_put_bool_value(cff_writer_t *pcw, bool b, int op)
262
cff_put_bool(pcw, b);
266
cff_put_real(cff_writer_t *pcw, floatp f)
269
cff_put_int(pcw, (int)f);
271
/* Use decimal representation. */
276
sprintf(str, "%g", f);
277
sputc(pcw->strm, CD_REAL);
278
for (p = str; ; ++p) {
296
case '0': case '1': case '2': case '3': case '4':
297
case '5': case '6': case '7': case '8': case '9':
300
default: /* can't happen */
301
digit = 0xd; /* invalid */
305
b = (digit << 4) + 0xf;
307
sputc(pcw->strm, (byte)((b & 0xf0) + digit));
316
cff_put_real_value(cff_writer_t *pcw, floatp f, int op)
318
cff_put_real(pcw, f);
322
cff_put_real_if_ne(cff_writer_t *pcw, floatp f, floatp f_default, int op)
324
if ((float)f != (float)f_default)
325
cff_put_real_value(pcw, f, op);
328
cff_put_real_deltarray(cff_writer_t *pcw, const float *pf, int count, int op)
335
for (i = 0; i < count; ++i) {
338
cff_put_real(pcw, f - prev);
344
cff_put_string(cff_writer_t *pcw, const byte *data, uint size)
346
int sid = cff_string_sid(pcw, data, size);
350
cff_put_int(pcw, sid);
354
cff_put_string_value(cff_writer_t *pcw, const byte *data, uint size, int op)
356
int code = cff_put_string(pcw, data, size);
363
cff_extra_lenIV(const cff_writer_t *pcw, const gs_font_type1 *pfont)
365
return (pcw->options & WRITE_TYPE2_NO_LENIV ?
366
max(pfont->data.lenIV, 0) : 0);
369
cff_convert_charstrings(const cff_writer_t *pcw, const gs_font_base *pfont)
371
return (pfont->FontType != ft_encrypted2 &&
372
(pcw->options & WRITE_TYPE2_CHARSTRINGS) != 0);
375
cff_put_CharString(cff_writer_t *pcw, const byte *data, uint size,
376
gs_font_type1 *pfont)
378
int lenIV = pfont->data.lenIV;
379
stream *s = pcw->strm;
381
if (cff_convert_charstrings(pcw, (gs_font_base *)pfont)) {
382
gs_glyph_data_t gdata;
385
gdata.memory = pfont->memory;
386
gs_glyph_data_from_string(&gdata, data, size, NULL);
387
code = psf_convert_type1_to_type2(s, &gdata, pfont);
390
} else if (lenIV < 0 || !(pcw->options & WRITE_TYPE2_NO_LENIV))
391
put_bytes(s, data, size);
392
else if (size >= lenIV) {
393
/* Remove encryption. */
394
crypt_state state = crypt_charstring_seed;
395
byte buf[50]; /* arbitrary */
398
for (left = lenIV; left > 0; left -= n) {
399
n = min(left, sizeof(buf));
400
gs_type1_decrypt(buf, data + lenIV - left, n, &state);
402
for (left = size - lenIV; left > 0; left -= n) {
403
n = min(left, sizeof(buf));
404
gs_type1_decrypt(buf, data + size - left, n, &state);
405
put_bytes(s, buf, n);
411
cff_Index_size(uint count, uint total)
413
return (count == 0 ? 2 :
414
3 + offset_size(total + 1) * (count + 1) + total);
417
cff_put_Index_header(cff_writer_t *pcw, uint count, uint total)
419
put_card16(pcw, count);
421
pcw->offset_size = offset_size(total + 1);
422
sputc(pcw->strm, (byte)pcw->offset_size);
427
cff_put_Index(cff_writer_t *pcw, const cff_string_table_t *pcst)
431
if (pcst->count == 0) {
435
cff_put_Index_header(pcw, pcst->count, pcst->total);
436
for (j = 0, offset = 1; j < pcst->count; ++j) {
437
offset += pcst->items[j].key.size;
438
put_offset(pcw, offset);
440
for (j = 0; j < pcst->count; ++j)
441
put_bytes(pcw->strm, pcst->items[j].key.data, pcst->items[j].key.size);
444
/* ---------------- Main code ---------------- */
446
/* ------ Header ------ */
448
/* Write the header, setting offset_size. */
450
cff_write_header(cff_writer_t *pcw, uint end_offset)
452
pcw->offset_size = (end_offset > 0x7fff ? 3 : 2);
453
put_bytes(pcw->strm, (const byte *)"\001\000\004", 3);
454
sputc(pcw->strm, (byte)pcw->offset_size);
458
/* ------ Top Dict ------ */
461
* There are 3 variants of this: Type 1 / Type 2 font, CIDFontType 0
462
* CIDFont, and FDArray entry for CIDFont.
474
TOP_charset = 15, /* (offset or predefined index) */
475
#define charset_ISOAdobe 0
476
#define charset_Expert 1
477
#define charset_ExpertSubset 2
478
#define charset_DEFAULT 0
479
TOP_Encoding = 16, /* (offset or predefined index) */
480
#define Encoding_Standard 0
481
#define Encoding_Expert 1
482
#define Encoding_DEFAULT 0
483
TOP_CharStrings = 17, /* (offset) */
484
TOP_Private = 18, /* (offset) */
486
TOP_isFixedPitch = 33,
487
#define isFixedPitch_DEFAULT false
488
TOP_ItalicAngle = 34,
489
#define ItalicAngle_DEFAULT 0
490
TOP_UnderlinePosition = 35,
491
#define UnderlinePosition_DEFAULT (-100)
492
TOP_UnderlineThickness = 36,
493
#define UnderlineThickness_DEFAULT 50
495
#define PaintType_DEFAULT 0
496
TOP_CharstringType = 38,
497
#define CharstringType_DEFAULT 2
498
TOP_FontMatrix = 39, /* default is [0.001 0 0 0.001 0 0] */
499
TOP_StrokeWidth = 40,
500
#define StrokeWidth_DEFAULT 0
502
TOP_CIDFontVersion = 63,
503
#define CIDFontVersion_DEFAULT 0
504
TOP_CIDFontRevision = 64,
505
#define CIDFontRevision_DEFAULT 0
506
TOP_CIDFontType = 65,
507
#define CIDFontType_DEFAULT 0
509
#define CIDCount_DEFAULT 8720
511
TOP_FDArray = 68, /* (offset) */
512
TOP_FDSelect = 69, /* (offset) */
513
TOP_FontName = 70 /* only used in FDArray "fonts" */
517
cff_get_Top_info_common(cff_writer_t *pcw, gs_font_base *pbfont,
518
bool full_info, gs_font_info_t *pinfo)
520
pinfo->Flags_requested = FONT_IS_FIXED_WIDTH;
521
/* Preset defaults */
523
pinfo->Flags = pinfo->Flags_returned = 0;
524
pinfo->ItalicAngle = ItalicAngle_DEFAULT;
525
pinfo->UnderlinePosition = UnderlinePosition_DEFAULT;
526
pinfo->UnderlineThickness = UnderlineThickness_DEFAULT;
527
return pbfont->procs.font_info
528
((gs_font *)pbfont, NULL,
530
FONT_INFO_FLAGS | FONT_INFO_ITALIC_ANGLE |
531
FONT_INFO_UNDERLINE_POSITION |
532
FONT_INFO_UNDERLINE_THICKNESS : 0) |
533
(FONT_INFO_COPYRIGHT | FONT_INFO_NOTICE |
534
FONT_INFO_FAMILY_NAME | FONT_INFO_FULL_NAME),
538
cff_write_Top_common(cff_writer_t *pcw, gs_font_base *pbfont,
539
bool write_FontMatrix, const gs_font_info_t *pinfo)
542
* The Adobe documentation doesn't make it at all clear that if the
543
* FontMatrix is missing (defaulted) in a CFF CIDFont, all of the
544
* FontMatrices of the subfonts in FDArray are multiplied by 1000.
545
* (This is documented for ordinary CIDFonts, but not for CFF CIDFonts.)
546
* Because of this, the FontMatrix for a CFF CIDFont must be written
547
* even if if is the default. write_FontMatrix controls this.
550
if (pinfo->members & FONT_INFO_NOTICE)
551
cff_put_string_value(pcw, pinfo->Notice.data, pinfo->Notice.size,
553
if (pinfo->members & FONT_INFO_FULL_NAME)
554
cff_put_string_value(pcw, pinfo->FullName.data, pinfo->FullName.size,
556
if (pinfo->members & FONT_INFO_FAMILY_NAME)
557
cff_put_string_value(pcw, pinfo->FamilyName.data,
558
pinfo->FamilyName.size, TOP_FamilyName);
559
if (pcw->FontBBox.p.x != 0 || pcw->FontBBox.p.y != 0 ||
560
pcw->FontBBox.q.x != 0 || pcw->FontBBox.q.y != 0
562
/* An omitted FontBBox is equivalent to an empty one. */
564
* Since Acrobat Reader 4 on Solaris doesn't like
565
* an omitted FontBBox, we copy it here from
566
* the font descriptor, because the base font
567
* is allowed to omit it's FontBBox.
569
cff_put_real(pcw, pcw->FontBBox.p.x);
570
cff_put_real(pcw, pcw->FontBBox.p.y);
571
cff_put_real(pcw, pcw->FontBBox.q.x);
572
cff_put_real(pcw, pcw->FontBBox.q.y);
573
cff_put_op(pcw, TOP_FontBBox);
575
if (uid_is_UniqueID(&pbfont->UID))
576
cff_put_int_value(pcw, pbfont->UID.id, TOP_UniqueID);
577
else if (uid_is_XUID(&pbfont->UID)) {
580
for (j = 0; j < uid_XUID_size(&pbfont->UID); ++j)
581
cff_put_int(pcw, uid_XUID_values(&pbfont->UID)[j]);
582
cff_put_op(pcw, TOP_XUID);
585
* Acrobat Reader 3 gives an error if a CFF font includes any of the
588
if (!(pcw->options & WRITE_TYPE2_AR3)) {
589
if (pinfo->members & FONT_INFO_COPYRIGHT)
590
cff_put_string_value(pcw, pinfo->Copyright.data,
591
pinfo->Copyright.size, TOP_Copyright);
592
if (pinfo->Flags & pinfo->Flags_returned & FONT_IS_FIXED_WIDTH)
593
cff_put_bool_value(pcw, true, TOP_isFixedPitch);
594
cff_put_real_if_ne(pcw, pinfo->ItalicAngle, ItalicAngle_DEFAULT,
596
cff_put_int_if_ne(pcw, pinfo->UnderlinePosition,
597
UnderlinePosition_DEFAULT, TOP_UnderlinePosition);
598
cff_put_int_if_ne(pcw, pinfo->UnderlineThickness,
599
UnderlineThickness_DEFAULT, TOP_UnderlineThickness);
600
cff_put_int_if_ne(pcw, pbfont->PaintType, PaintType_DEFAULT,
604
static const gs_matrix fm_default = {
605
constant_matrix_body(0.001, 0, 0, 0.001, 0, 0)
608
if (write_FontMatrix ||
609
pbfont->FontMatrix.xx != fm_default.xx ||
610
pbfont->FontMatrix.xy != 0 || pbfont->FontMatrix.yx != 0 ||
611
pbfont->FontMatrix.yy != fm_default.yy ||
612
pbfont->FontMatrix.tx != 0 || pbfont->FontMatrix.ty != 0
614
cff_put_real(pcw, pbfont->FontMatrix.xx);
615
cff_put_real(pcw, pbfont->FontMatrix.xy);
616
cff_put_real(pcw, pbfont->FontMatrix.yx);
617
cff_put_real(pcw, pbfont->FontMatrix.yy);
618
cff_put_real(pcw, pbfont->FontMatrix.tx);
619
cff_put_real(pcw, pbfont->FontMatrix.ty);
620
cff_put_op(pcw, TOP_FontMatrix);
623
cff_put_real_if_ne(pcw, pbfont->StrokeWidth, StrokeWidth_DEFAULT,
627
/* Type 1 or Type 2 font */
629
cff_write_Top_font(cff_writer_t *pcw, uint Encoding_offset,
630
uint charset_offset, uint CharStrings_offset,
631
uint Private_offset, uint Private_size)
633
gs_font_base *pbfont = (gs_font_base *)pcw->pfont;
636
cff_get_Top_info_common(pcw, pbfont, true, &info);
637
cff_write_Top_common(pcw, pbfont, false, &info);
638
cff_put_int(pcw, Private_size);
639
cff_put_int_value(pcw, Private_offset, TOP_Private);
640
cff_put_int_value(pcw, CharStrings_offset, TOP_CharStrings);
641
cff_put_int_if_ne(pcw, charset_offset, charset_DEFAULT, TOP_charset);
642
cff_put_int_if_ne(pcw, Encoding_offset, Encoding_DEFAULT, TOP_Encoding);
644
int type = (pcw->options & WRITE_TYPE2_CHARSTRINGS ? 2 :
645
pbfont->FontType == ft_encrypted2 ? 2 : 1);
647
cff_put_int_if_ne(pcw, type, CharstringType_DEFAULT,
652
/* CIDFontType 0 CIDFont */
654
cff_write_ROS(cff_writer_t *pcw, const gs_cid_system_info_t *pcidsi)
656
cff_put_string(pcw, pcidsi->Registry.data, pcidsi->Registry.size);
657
cff_put_string(pcw, pcidsi->Ordering.data, pcidsi->Ordering.size);
658
cff_put_int_value(pcw, pcidsi->Supplement, TOP_ROS);
661
cff_write_Top_cidfont(cff_writer_t *pcw, uint charset_offset,
662
uint CharStrings_offset, uint FDSelect_offset,
663
uint Font_offset, const gs_font_info_t *pinfo)
665
gs_font_base *pbfont = (gs_font_base *)pcw->pfont;
666
gs_font_cid0 *pfont = (gs_font_cid0 *)pbfont;
668
cff_write_ROS(pcw, &pfont->cidata.common.CIDSystemInfo);
669
cff_write_Top_common(pcw, pbfont, true, pinfo); /* full_info = true */
670
cff_put_int_if_ne(pcw, charset_offset, charset_DEFAULT, TOP_charset);
671
cff_put_int_value(pcw, CharStrings_offset, TOP_CharStrings);
673
* CIDFontVersion and CIDFontRevision aren't used consistently,
674
* so we don't currently write them. CIDFontType is always 0.
676
cff_put_int_if_ne(pcw, pfont->cidata.common.CIDCount, CIDCount_DEFAULT,
678
/* We don't use UIDBase. */
679
cff_put_int_value(pcw, Font_offset, TOP_FDArray);
680
cff_put_int_value(pcw, FDSelect_offset, TOP_FDSelect);
683
/* FDArray Index for CIDFont (offsets only) */
685
cff_write_FDArray_offsets(cff_writer_t *pcw, uint *FDArray_offsets,
690
cff_put_Index_header(pcw, num_fonts,
691
FDArray_offsets[num_fonts] - FDArray_offsets[0]);
692
for (j = 1; j <= num_fonts; ++j)
693
put_offset(pcw, FDArray_offsets[j] - FDArray_offsets[0] + 1);
696
/* FDArray entry for CIDFont */
698
cff_write_Top_fdarray(cff_writer_t *pcw, gs_font_base *pbfont,
699
uint Private_offset, uint Private_size)
701
const gs_font_name *pfname = &pbfont->font_name;
704
cff_get_Top_info_common(pcw, pbfont, false, &info);
705
cff_write_Top_common(pcw, pbfont, false, &info);
706
cff_put_int(pcw, Private_size);
707
cff_put_int_value(pcw, Private_offset, TOP_Private);
708
if (pfname->size == 0)
709
pfname = &pbfont->key_name;
711
cff_put_string(pcw, pfname->chars, pfname->size);
712
cff_put_op(pcw, TOP_FontName);
716
/* ------ Private Dict ------ */
718
/* Defaults are noted in comments. */
720
PRIVATE_BlueValues = 6, /* (deltarray) */
721
PRIVATE_OtherBlues = 7, /* (deltarray) */
722
PRIVATE_FamilyBlues = 8, /* (deltarray) */
723
PRIVATE_FamilyOtherBlues = 9, /* (deltarray) */
726
PRIVATE_Subrs = 19, /* (offset, relative to Private Dict) */
727
PRIVATE_defaultWidthX = 20,
728
#define defaultWidthX_DEFAULT fixed_0
729
PRIVATE_nominalWidthX = 21,
730
#define nominalWidthX_DEFAULT fixed_0
731
PRIVATE_BlueScale = 41,
732
#define BlueScale_DEFAULT 0.039625
733
PRIVATE_BlueShift = 42,
734
#define BlueShift_DEFAULT 7
735
PRIVATE_BlueFuzz = 43,
736
#define BlueFuzz_DEFAULT 1
737
PRIVATE_StemSnapH = 44, /* (deltarray) */
738
PRIVATE_StemSnapV = 45, /* (deltarray) */
739
PRIVATE_ForceBold = 46,
740
#define ForceBold_DEFAULT false
741
PRIVATE_ForceBoldThreshold = 47,
742
#define ForceBoldThreshold_DEFAULT 0
744
#define lenIV_DEFAULT (-1)
745
PRIVATE_LanguageGroup = 49,
746
#define LanguageGroup_DEFAULT 0
747
PRIVATE_ExpansionFactor = 50,
748
#define ExpansionFactor_DEFAULT 0.06
749
PRIVATE_initialRandomSeed = 51
750
#define initialRandomSeed_DEFAULT 0
753
const long default_defaultWidthX = defaultWidthX_DEFAULT; /* For gdevpsfx.c */
756
cff_write_Private(cff_writer_t *pcw, uint Subrs_offset,
757
const gs_font_type1 *pfont)
759
#define PUT_FLOAT_TABLE(member, op)\
760
cff_put_real_deltarray(pcw, pfont->data.member.values,\
761
pfont->data.member.count, op)
763
PUT_FLOAT_TABLE(BlueValues, PRIVATE_BlueValues);
764
PUT_FLOAT_TABLE(OtherBlues, PRIVATE_OtherBlues);
765
PUT_FLOAT_TABLE(FamilyBlues, PRIVATE_FamilyBlues);
766
PUT_FLOAT_TABLE(FamilyOtherBlues, PRIVATE_FamilyOtherBlues);
767
if (pfont->data.StdHW.count > 0)
768
cff_put_real_value(pcw, pfont->data.StdHW.values[0], PRIVATE_StdHW);
769
if (pfont->data.StdVW.count > 0)
770
cff_put_real_value(pcw, pfont->data.StdVW.values[0], PRIVATE_StdVW);
772
cff_put_int_value(pcw, Subrs_offset, PRIVATE_Subrs);
773
if (pfont->FontType != ft_encrypted) {
774
if (pfont->data.defaultWidthX != defaultWidthX_DEFAULT)
775
cff_put_real_value(pcw, fixed2float(pfont->data.defaultWidthX),
776
PRIVATE_defaultWidthX);
777
if (pfont->data.nominalWidthX != nominalWidthX_DEFAULT)
778
cff_put_real_value(pcw, fixed2float(pfont->data.nominalWidthX),
779
PRIVATE_nominalWidthX);
780
cff_put_int_if_ne(pcw, pfont->data.initialRandomSeed,
781
initialRandomSeed_DEFAULT,
782
PRIVATE_initialRandomSeed);
784
cff_put_real_if_ne(pcw, pfont->data.BlueScale, BlueScale_DEFAULT,
786
cff_put_real_if_ne(pcw, pfont->data.BlueShift, BlueShift_DEFAULT,
788
cff_put_int_if_ne(pcw, pfont->data.BlueFuzz, BlueFuzz_DEFAULT,
790
PUT_FLOAT_TABLE(StemSnapH, PRIVATE_StemSnapH);
791
PUT_FLOAT_TABLE(StemSnapV, PRIVATE_StemSnapV);
792
if (pfont->data.ForceBold != ForceBold_DEFAULT)
793
cff_put_bool_value(pcw, pfont->data.ForceBold,
795
/* (ForceBoldThreshold) */
796
if (!(pcw->options & WRITE_TYPE2_NO_LENIV))
797
cff_put_int_if_ne(pcw, pfont->data.lenIV, lenIV_DEFAULT,
799
cff_put_int_if_ne(pcw, pfont->data.LanguageGroup, LanguageGroup_DEFAULT,
800
PRIVATE_LanguageGroup);
801
cff_put_real_if_ne(pcw, pfont->data.ExpansionFactor,
802
ExpansionFactor_DEFAULT, PRIVATE_ExpansionFactor);
803
/* initialRandomSeed was handled above */
805
#undef PUT_FLOAT_TABLE
808
/* ------ CharStrings Index ------ */
810
/* These are separate procedures only for readability. */
812
cff_write_CharStrings_offsets(cff_writer_t *pcw, psf_glyph_enum_t *penum,
815
gs_font_base *pfont = pcw->pfont;
823
psf_enumerate_glyphs_reset(penum);
824
for (glyph = gs_no_glyph, count = 0, offset = 1;
825
(code = psf_enumerate_glyphs_next(penum, &glyph)) != 1;
827
gs_glyph_data_t gdata;
831
gdata.memory = pfont->memory;
833
(gcode = pcw->glyph_data(pfont, glyph, &gdata, &pfd)) >= 0
837
if (gdata.bits.size >= (extra_lenIV = cff_extra_lenIV(pcw, pfd))) {
838
if (cff_convert_charstrings(pcw, (gs_font_base *)pfd)) {
839
swrite_position_only(&poss);
840
code = psf_convert_type1_to_type2(&poss, &gdata, pfd);
843
offset += stell(&poss);
845
offset += gdata.bits.size - extra_lenIV;
847
gs_glyph_data_free(&gdata, "cff_write_CharStrings_offsets");
849
put_offset(pcw, offset);
855
cff_write_CharStrings(cff_writer_t *pcw, psf_glyph_enum_t *penum,
856
uint charstrings_count, uint charstrings_size)
858
gs_font_base *pfont = pcw->pfont;
863
cff_put_Index_header(pcw, charstrings_count, charstrings_size);
864
cff_write_CharStrings_offsets(pcw, penum, &ignore_count);
865
psf_enumerate_glyphs_reset(penum);
866
for (glyph = gs_no_glyph;
867
(code = psf_enumerate_glyphs_next(penum, &glyph)) != 1;
869
gs_glyph_data_t gdata;
872
gdata.memory = pfont->memory;
874
(code = pcw->glyph_data(pfont, glyph, &gdata, &pfd)) >= 0
876
cff_put_CharString(pcw, gdata.bits.data, gdata.bits.size, pfd);
877
gs_glyph_data_free(&gdata, "cff_write_CharStrings");
882
/* ------ [G]Subrs Index ------ */
885
* Currently, we always write all the Subrs, even for subsets.
886
* We will fix this someday.
890
cff_write_Subrs_offsets(cff_writer_t *pcw, uint *pcount, gs_font_type1 *pfont,
893
int extra_lenIV = cff_extra_lenIV(pcw, pfont);
896
gs_glyph_data_t gdata;
898
gdata.memory = pfont->memory;
899
for (j = 0, offset = 1;
900
(code = pfont->data.procs.subr_data(pfont, j, global, &gdata)) !=
903
if (code >= 0 && gdata.bits.size >= extra_lenIV)
904
offset += gdata.bits.size - extra_lenIV;
905
put_offset(pcw, offset);
907
gs_glyph_data_free(&gdata, "cff_write_Subrs_offsets");
914
cff_write_Subrs(cff_writer_t *pcw, uint subrs_count, uint subrs_size,
915
gs_font_type1 *pfont, bool global)
919
gs_glyph_data_t gdata;
922
gdata.memory = pfont->memory;
923
cff_put_Index_header(pcw, subrs_count, subrs_size);
924
cff_write_Subrs_offsets(pcw, &ignore_count, pfont, global);
926
(code = pfont->data.procs.subr_data(pfont, j, global, &gdata)) !=
930
cff_put_CharString(pcw, gdata.bits.data, gdata.bits.size, pfont);
931
gs_glyph_data_free(&gdata, "cff_write_Subrs");
936
/* ------ Encoding/charset ------ */
939
cff_Encoding_size(int num_encoded, int num_encoded_chars)
941
int n = min(num_encoded, 255);
944
(num_encoded_chars > n ?
945
1 + (num_encoded_chars - n) * 3 : 0);
949
cff_write_Encoding(cff_writer_t *pcw, cff_glyph_subset_t *pgsub)
951
stream *s = pcw->strm;
952
/* This procedure is only used for Type 1 / Type 2 fonts. */
953
gs_font_type1 *pfont = (gs_font_type1 *)pcw->pfont;
954
byte used[255], index[255], supplement[256];
955
int num_enc = min(pgsub->num_encoded, sizeof(index));
959
memset(used, 0, num_enc);
960
for (j = 0; j < 256; ++j) {
961
gs_glyph glyph = pfont->procs.encode_char((gs_font *)pfont,
966
if (glyph == gs_no_glyph || glyph == pgsub->glyphs.notdef)
968
i = psf_sorted_glyphs_index_of(pgsub->glyphs.subset_data + 1,
969
pgsub->num_encoded, glyph);
971
continue; /* encoded but not in subset */
972
if (i >= sizeof(used) || used[i])
973
supplement[nsupp++] = j;
975
index[i] = j, used[i] = 1;
977
sputc(s, (byte)(nsupp ? 0x80 : 0));
978
sputc(s, (byte)num_enc);
980
{ int num_enc_chars = pgsub->num_encoded_chars;
982
if (nsupp != num_enc_chars - num_enc)
983
lprintf3("nsupp = %d, num_enc_chars = %d, num_enc = %d\n",
984
nsupp, num_enc_chars, num_enc);
985
for (j = 0; j < num_enc; ++j)
987
lprintf2("glyph %d = 0x%lx not used\n", j,
988
pgsub->glyphs.subset_data[j + 1]);
991
put_bytes(s, index, num_enc);
993
/* Write supplementary entries for multiply-encoded glyphs. */
994
sputc(s, (byte)nsupp);
995
for (j = 0; j < nsupp; ++j) {
996
byte chr = supplement[j];
1001
pfont->procs.encode_char((gs_font *)pfont,
1003
GLYPH_SPACE_NAME)));
1010
cff_write_charset(cff_writer_t *pcw, cff_glyph_subset_t *pgsub)
1014
sputc(pcw->strm, 0);
1015
for (j = 1; j < pgsub->glyphs.subset_size; ++j)
1016
put_card16(pcw, cff_glyph_sid(pcw, pgsub->glyphs.subset_data[j]));
1020
cff_write_cidset(cff_writer_t *pcw, psf_glyph_enum_t *penum)
1025
sputc(pcw->strm, 0);
1026
psf_enumerate_glyphs_reset(penum);
1027
while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) {
1028
/* Skip glyph 0 (the .notdef glyph), which is always first. */
1029
if (glyph != gs_min_cid_glyph)
1030
put_card16(pcw, (uint)(glyph - gs_min_cid_glyph));
1032
return min(code, 0);
1035
/* ------ FDSelect ------ */
1037
/* Determine the size of FDSelect. */
1039
cff_FDSelect_size(cff_writer_t *pcw, psf_glyph_enum_t *penum, uint *pformat)
1041
gs_font_cid0 *const pfont = (gs_font_cid0 *)pcw->pfont;
1042
gs_font_base *const pbfont = (gs_font_base *)pfont;
1045
uint linear_size = 1, range_size = 5;
1048
/* Determine whether format 0 or 3 is more efficient. */
1049
psf_enumerate_glyphs_reset(penum);
1050
while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) {
1053
code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index);
1055
if (font_index != prev)
1056
range_size += 3, prev = font_index;
1060
if (range_size < linear_size) {
1069
/* Write FDSelect. size and format were returned by cff_FDSelect_size. */
1071
cff_write_FDSelect(cff_writer_t *pcw, psf_glyph_enum_t *penum, uint size,
1074
stream *s = pcw->strm;
1075
gs_font_cid0 *const pfont = (gs_font_cid0 *)pcw->pfont;
1076
gs_font_base *const pbfont = (gs_font_base *)pfont;
1082
spputc(s, (byte)format);
1083
psf_enumerate_glyphs_reset(penum);
1085
case 3: /* ranges */
1086
put_card16(pcw, (size - 5) / 3);
1087
while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) {
1090
code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index);
1092
if (font_index != prev) {
1093
put_card16(pcw, cid_count);
1094
sputc(s, (byte)font_index);
1100
put_card16(pcw, cid_count);
1102
case 0: /* linear table */
1103
while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) {
1106
code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index);
1108
sputc(s, (byte)font_index);
1111
default: /* not possible */
1112
return_error(gs_error_rangecheck);
1117
/* ------ Main procedure ------ */
1119
/* Write the CFF definition of a Type 1 or Type 2 font. */
1121
psf_write_type2_font(stream *s, gs_font_type1 *pfont, int options,
1122
gs_glyph *subset_glyphs, uint subset_size,
1123
const gs_const_string *alt_font_name,
1124
gs_int_rect *FontBBox)
1126
gs_font_base *const pbfont = (gs_font_base *)pfont;
1127
cff_writer_t writer;
1128
cff_glyph_subset_t subset;
1129
cff_string_item_t *std_string_items;
1130
cff_string_item_t *string_items;
1131
gs_const_string font_name;
1133
uint charstrings_count, charstrings_size;
1134
uint subrs_count, subrs_size;
1135
uint gsubrs_count, gsubrs_size, encoding_size, charset_size;
1136
uint number_of_glyphs = 0, number_of_strings;
1138
* Set the offsets and sizes to the largest reasonable values
1142
Top_size = 0x7fffff,
1148
Private_size = 0x7fffff,
1150
End_offset = 0x7fffff;
1152
psf_glyph_enum_t genum;
1158
/* Allocate the string tables. */
1159
psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont,
1160
NULL, 0, GLYPH_SPACE_NAME);
1161
while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1)
1163
subset.glyphs.subset_data = (gs_glyph *)gs_alloc_bytes(pfont->memory,
1164
number_of_glyphs * sizeof(glyph), "psf_write_type2_font");
1165
number_of_strings = number_of_glyphs + MAX_CFF_MISC_STRINGS;
1166
std_string_items = (cff_string_item_t *)gs_alloc_bytes(pfont->memory,
1167
(MAX_CFF_STD_STRINGS + number_of_strings) * sizeof(cff_string_item_t),
1168
"psf_write_type2_font");
1169
if (std_string_items == NULL || subset.glyphs.subset_data == NULL)
1170
return_error(gs_error_VMerror);
1171
string_items = std_string_items + MAX_CFF_STD_STRINGS;
1173
/* Get subset glyphs. */
1174
code = psf_get_type1_glyphs(&subset.glyphs, pfont, subset_glyphs,
1178
if (subset.glyphs.notdef == gs_no_glyph)
1179
return_error(gs_error_rangecheck); /* notdef is required */
1181
/* If we're writing Type 2 CharStrings, don't encrypt them. */
1182
if (options & WRITE_TYPE2_CHARSTRINGS) {
1183
options |= WRITE_TYPE2_NO_LENIV;
1184
if (pfont->FontType != ft_encrypted2)
1185
pfont->data.defaultWidthX = pfont->data.nominalWidthX = 0;
1187
writer.options = options;
1188
s_init(&poss, NULL);
1189
swrite_position_only(&poss);
1190
writer.strm = &poss;
1191
writer.pfont = pbfont;
1192
writer.glyph_data = psf_type1_glyph_data;
1193
writer.offset_size = 1; /* arbitrary */
1194
writer.start_pos = stell(s);
1195
writer.FontBBox = *FontBBox;
1197
/* Initialize the enumeration of the glyphs. */
1198
psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont,
1199
subset.glyphs.subset_glyphs,
1200
(subset.glyphs.subset_glyphs ?
1201
subset.glyphs.subset_size : 0),
1204
/* Shuffle the glyphs into the order .notdef, encoded, unencoded. */
1206
gs_glyph encoded[256];
1207
int num_enc, num_enc_chars;
1209
/* Get the list of encoded glyphs. */
1210
for (j = 0, num_enc_chars = 0; j < 256; ++j) {
1211
glyph = pfont->procs.encode_char((gs_font *)pfont, (gs_char)j,
1213
if (glyph != gs_no_glyph && glyph != subset.glyphs.notdef &&
1214
(subset.glyphs.subset_glyphs == 0 ||
1215
psf_sorted_glyphs_include(subset.glyphs.subset_data,
1216
subset.glyphs.subset_size, glyph)))
1217
encoded[num_enc_chars++] = glyph;
1219
subset.num_encoded_chars = num_enc_chars;
1220
subset.num_encoded = num_enc =
1221
psf_sort_glyphs(encoded, num_enc_chars);
1223
/* Get the complete list of glyphs if we don't have it already. */
1224
if (!subset.glyphs.subset_glyphs) {
1227
psf_enumerate_glyphs_reset(&genum);
1228
while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1)
1230
if (num_glyphs == number_of_glyphs)
1231
return_error(gs_error_limitcheck);
1232
subset.glyphs.subset_data[num_glyphs++] = glyph;
1234
subset.glyphs.subset_size =
1235
psf_sort_glyphs(subset.glyphs.subset_data, num_glyphs);
1236
subset.glyphs.subset_glyphs = subset.glyphs.subset_data;
1239
/* Move the unencoded glyphs to the top of the list. */
1241
* We could do this in time N rather than N log N with a two-finger
1242
* algorithm, but it doesn't seem worth the trouble right now.
1245
int from = subset.glyphs.subset_size;
1249
glyph = subset.glyphs.subset_data[--from];
1250
if (glyph != subset.glyphs.notdef &&
1251
!psf_sorted_glyphs_include(encoded, num_enc, glyph))
1252
subset.glyphs.subset_data[--to] = glyph;
1255
if (to != num_enc + 1)
1256
lprintf2("to = %d, num_enc + 1 = %d\n", to, num_enc + 1);
1260
/* Move .notdef and the encoded glyphs to the bottom of the list. */
1261
subset.glyphs.subset_data[0] = subset.glyphs.notdef;
1262
memcpy(subset.glyphs.subset_data + 1, encoded,
1263
sizeof(encoded[0]) * num_enc);
1266
/* Set the font name. */
1268
font_name = *alt_font_name;
1270
font_name.data = pfont->font_name.chars,
1271
font_name.size = pfont->font_name.size;
1273
/* Initialize the string tables. */
1274
cff_string_table_init(&writer.std_strings, std_string_items,
1275
MAX_CFF_STD_STRINGS);
1276
for (j = 0; (glyph = gs_c_known_encode((gs_char)j,
1277
ENCODING_INDEX_CFFSTRINGS)) != gs_no_glyph;
1279
gs_const_string str;
1282
gs_c_glyph_name(glyph, &str);
1283
cff_string_index(&writer.std_strings, str.data, str.size, true,
1286
cff_string_table_init(&writer.strings, string_items, number_of_strings);
1288
/* Enter miscellaneous strings in the string table. */
1289
cff_write_Top_font(&writer, 0, 0, 0, 0, 0);
1291
/* Enter the glyph names in the string table. */
1292
/* (Note that we have changed the glyph list.) */
1293
psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont,
1294
subset.glyphs.subset_data,
1295
subset.glyphs.subset_size,
1297
while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1)
1299
code = cff_glyph_sid(&writer, glyph);
1305
* The CFF specification says that the Encoding, charset, CharStrings,
1306
* Private, and Local Subr sections may be in any order. To minimize
1307
* the risk of incompatibility with Adobe software, we produce them in
1308
* the order just mentioned.
1312
* Compute the size of the GSubrs Index, if not omitted.
1314
if ((options & WRITE_TYPE2_NO_GSUBRS) != 0 ||
1315
cff_convert_charstrings(&writer, pbfont) /* we expand all Subrs */
1317
gsubrs_count = 0, gsubrs_size = 0;
1319
gsubrs_size = cff_write_Subrs_offsets(&writer, &gsubrs_count,
1323
* Compute the size of the Encoding. For simplicity, we currently
1324
* always store the Encoding explicitly. Note that because CFF stores
1325
* the Encoding in an "inverted" form, we need to count the number of
1326
* glyphs that occur at more than one place in the Encoding.
1328
encoding_size = cff_Encoding_size(subset.num_encoded,
1329
subset.num_encoded_chars);
1332
* Compute the size of the charset. For simplicity, we currently
1333
* always store the charset explicitly.
1335
charset_size = 1 + (subset.glyphs.subset_size - 1) * 2;
1337
/* Compute the size of the CharStrings Index. */
1338
code = cff_write_CharStrings_offsets(&writer, &genum, &charstrings_count);
1341
charstrings_size = (uint)code;
1343
/* Compute the size of the (local) Subrs Index. */
1344
#ifdef SKIP_EMPTY_SUBRS
1346
(cff_convert_charstrings(&writer, pbfont) ? 0 :
1347
cff_write_Subrs_offsets(&writer, &subrs_count, pfont, false));
1349
if (cff_convert_charstrings(&writer, pbfont))
1350
subrs_count = 0; /* we expand all Subrs */
1351
subrs_size = cff_write_Subrs_offsets(&writer, &subrs_count, pfont, false);
1355
* The offsets of the Private Dict and the CharStrings Index
1356
* depend on the size of the Top Dict; the offset of the Subrs also
1357
* depends on the size of the Private Dict. However, the size of the
1358
* Top Dict depends on the offsets of the CharStrings Index, the
1359
* charset, and the Encoding, and on the offset and size of the Private
1360
* Dict, because of the variable-length encoding of the offsets and
1361
* size; for the same reason, the size of the Private Dict depends on
1362
* the offset of the Subrs. Fortunately, the relationship between the
1363
* value of an offset or size and the size of its encoding is monotonic.
1364
* Therefore, we start by assuming the largest reasonable value for all
1365
* the sizes and iterate until everything converges.
1368
swrite_position_only(&poss);
1369
writer.strm = &poss;
1371
/* Compute the offsets. */
1372
GSubrs_offset = 4 + cff_Index_size(1, font_name.size) +
1373
cff_Index_size(1, Top_size) +
1374
cff_Index_size(writer.strings.count, writer.strings.total);
1375
Encoding_offset = GSubrs_offset +
1376
cff_Index_size(gsubrs_count, gsubrs_size);
1377
charset_offset = Encoding_offset + encoding_size;
1378
CharStrings_offset = charset_offset + charset_size;
1379
Private_offset = CharStrings_offset +
1380
cff_Index_size(charstrings_count, charstrings_size);
1381
Subrs_offset = Private_size; /* relative to Private Dict */
1384
if(check_ioerror(writer.strm))
1385
return_error(gs_error_ioerror);
1386
start_pos = stell(writer.strm);
1387
/* Write the header, setting offset_size. */
1388
cff_write_header(&writer, End_offset);
1390
/* Write the names Index. */
1391
cff_put_Index_header(&writer, 1, font_name.size);
1392
put_offset(&writer, font_name.size + 1);
1393
put_bytes(writer.strm, font_name.data, font_name.size);
1395
/* Write the Top Index. */
1396
cff_put_Index_header(&writer, 1, Top_size);
1397
put_offset(&writer, Top_size + 1);
1398
offset = stell(writer.strm) - start_pos;
1399
cff_write_Top_font(&writer, Encoding_offset, charset_offset,
1401
Private_offset, Private_size);
1402
Top_size = stell(writer.strm) - start_pos - offset;
1404
/* Write the strings Index. */
1405
cff_put_Index(&writer, &writer.strings);
1406
if(check_ioerror(writer.strm))
1407
return_error(gs_error_ioerror);
1409
/* Write the GSubrs Index, if any, checking the offset. */
1410
offset = stell(writer.strm) - start_pos;
1411
if_debug2('l', "[l]GSubrs = %u => %u\n", GSubrs_offset, offset);
1412
if (offset > GSubrs_offset)
1413
return_error(gs_error_rangecheck);
1414
GSubrs_offset = offset;
1415
if (gsubrs_count == 0 || cff_convert_charstrings(&writer, pbfont))
1416
cff_put_Index_header(&writer, 0, 0);
1418
cff_write_Subrs(&writer, gsubrs_count, gsubrs_size, pfont, true);
1420
/* Write the Encoding. */
1421
cff_write_Encoding(&writer, &subset);
1423
/* Write the charset. */
1424
cff_write_charset(&writer, &subset);
1426
/* Write the CharStrings Index, checking the offset. */
1427
offset = stell(writer.strm) - start_pos;
1428
if (offset > CharStrings_offset)
1429
return_error(gs_error_rangecheck);
1430
CharStrings_offset = offset;
1431
cff_write_CharStrings(&writer, &genum, charstrings_count,
1433
if(check_ioerror(writer.strm))
1434
return_error(gs_error_ioerror);
1436
/* Write the Private Dict, checking the offset. */
1437
offset = stell(writer.strm) - start_pos;
1438
if (offset > Private_offset)
1439
return_error(gs_error_rangecheck);
1440
Private_offset = offset;
1441
cff_write_Private(&writer, (subrs_size == 0 ? 0 : Subrs_offset), pfont);
1442
Private_size = stell(writer.strm) - start_pos - offset;
1444
/* Write the Subrs Index, checking the offset. */
1445
offset = stell(writer.strm) - (start_pos + Private_offset);
1446
if (offset > Subrs_offset)
1447
return_error(gs_error_rangecheck);
1448
Subrs_offset = offset;
1449
if (cff_convert_charstrings(&writer, pbfont))
1450
cff_put_Index_header(&writer, 0, 0);
1451
else if (subrs_size != 0)
1452
cff_write_Subrs(&writer, subrs_count, subrs_size, pfont, false);
1454
/* Check the final offset. */
1455
if(check_ioerror(writer.strm))
1456
return_error(gs_error_ioerror);
1457
offset = stell(writer.strm) - start_pos;
1458
if (offset > End_offset)
1459
return_error(gs_error_rangecheck);
1460
if (offset == End_offset) {
1461
/* The iteration has converged. Write the result. */
1462
if (writer.strm == &poss) {
1467
/* No convergence yet. */
1468
End_offset = offset;
1473
gs_free_object(pfont->memory, std_string_items, "psf_write_type2_font");
1474
gs_free_object(pfont->memory, subset.glyphs.subset_data, "psf_write_type2_font");
1478
/* Write the CFF definition of a CIDFontType 0 font (CIDFont). */
1480
cid0_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_glyph_data_t *pgd,
1481
gs_font_type1 **ppfont)
1483
gs_font_cid0 *const pfont = (gs_font_cid0 *)pbfont;
1485
int code = pfont->cidata.glyph_data(pbfont, glyph, pgd, &font_index);
1488
*ppfont = pfont->cidata.FDArray[font_index];
1493
offset_error(const char *msg)
1495
if_debug1('l', "[l]%s offset error\n", msg);
1496
return gs_error_rangecheck;
1499
# define offset_error(msg) gs_error_rangecheck
1502
psf_write_cid0_font(stream *s, gs_font_cid0 *pfont, int options,
1503
const byte *subset_cids, uint subset_size,
1504
const gs_const_string *alt_font_name)
1507
* CIDFontType 0 fonts differ from ordinary Type 1 / Type 2 fonts
1509
* The TOP Dict starts with a ROS operator.
1510
* The TOP Dict must include FDArray and FDSelect operators.
1511
* The TOP Dict may include CIDFontVersion, CIDFontRevision,
1512
* CIDFontType, CIDCount, and UIDBase operators.
1513
* The TOP Dict must not include an Encoding operator.
1514
* The charset is defined in terms of CIDs rather than SIDs.
1515
* FDArray references a Font Index in which each element is a Dict
1516
* defining a font without charset, Encoding, or CharStrings.
1517
* FDSelect references a structure mapping CIDs to font numbers.
1519
gs_font_base *const pbfont = (gs_font_base *)pfont;
1520
cff_writer_t writer;
1521
cff_string_item_t std_string_items[500]; /* 391 entries used */
1522
/****** HOW TO DETERMINE THE SIZE OF STRINGS? ******/
1523
cff_string_item_t string_items[500 /* character names */ +
1524
40 /* misc. values */];
1525
gs_const_string font_name;
1527
uint charstrings_count, charstrings_size;
1528
uint gsubrs_count, gsubrs_size;
1529
uint charset_size, fdselect_size, fdselect_format;
1530
uint subrs_count[256], subrs_size[256];
1532
* Set the offsets and sizes to the largest reasonable values
1536
Top_size = 0x7fffff,
1537
GSubrs_offset = 0x7fffff,
1538
charset_offset = 0x7fffff,
1539
FDSelect_offset = 0x7fffff,
1540
CharStrings_offset = 0x7fffff,
1541
Font_offset = 0x7fffff,
1542
FDArray_offsets[257],
1543
Private_offsets[257],
1545
End_offset = 0x7fffff;
1547
psf_glyph_enum_t genum;
1548
gs_font_info_t info;
1551
int num_fonts = pfont->cidata.FDArray_size;
1555
/* Initialize the enumeration of the glyphs. */
1556
psf_enumerate_cids_begin(&genum, (gs_font *)pfont, subset_cids,
1559
/* Check that the font can be written. */
1560
code = psf_check_outline_glyphs((gs_font_base *)pfont, &genum,
1564
/* The .notdef glyph (glyph 0) must be included. */
1565
if (subset_cids && subset_size > 0 && !(subset_cids[0] & 0x80))
1566
return_error(gs_error_rangecheck);
1568
writer.options = options;
1569
s_init(&poss, NULL);
1570
swrite_position_only(&poss);
1571
writer.strm = &poss;
1572
writer.pfont = pbfont;
1573
writer.glyph_data = cid0_glyph_data;
1574
writer.offset_size = 1; /* arbitrary */
1575
writer.start_pos = stell(s);
1576
writer.FontBBox.p.x = writer.FontBBox.p.y = 0;
1577
writer.FontBBox.q.x = writer.FontBBox.q.y = 0;
1579
/* Set the font name. */
1581
font_name = *alt_font_name;
1582
else if (pfont->font_name.size)
1583
font_name.data = pfont->font_name.chars,
1584
font_name.size = pfont->font_name.size;
1586
font_name.data = pfont->key_name.chars,
1587
font_name.size = pfont->key_name.size;
1589
/* Initialize the string tables. */
1590
cff_string_table_init(&writer.std_strings, std_string_items,
1591
countof(std_string_items));
1592
cff_string_table_init(&writer.strings, string_items,
1593
countof(string_items));
1595
/* Make all entries in the string table. */
1596
cff_write_ROS(&writer, &pfont->cidata.common.CIDSystemInfo);
1597
for (j = 0; j < num_fonts; ++j) {
1598
gs_font_type1 *pfd = pfont->cidata.FDArray[j];
1600
cff_write_Top_fdarray(&writer, (gs_font_base *)pfd, 0, 0);
1604
* The CFF specification says that sections after the initial Indexes
1605
* may be in any order. To minimize the risk of incompatibility with
1606
* Adobe software, we produce them in the order illustrated in the
1610
/* Initialize the offset arrays. */
1611
for (j = 0; j <= num_fonts; ++j)
1612
FDArray_offsets[j] = Private_offsets[j] = Subrs_offsets[j] =
1613
0x7effffff / num_fonts * j + 0x1000000;
1616
* Compute the size of the GSubrs Index, if not omitted.
1617
* Arbitrarily use FDArray[0] to access the GSubrs and to determine
1618
* the CharString type.
1620
if ((options & WRITE_TYPE2_NO_GSUBRS) != 0 ||
1621
cff_convert_charstrings(&writer,
1622
(const gs_font_base *)pfont->cidata.FDArray[0])
1623
/* we expand all Subrs */
1625
gsubrs_count = 0, gsubrs_size = 0;
1627
gsubrs_size = cff_write_Subrs_offsets(&writer, &gsubrs_count,
1628
pfont->cidata.FDArray[0], true);
1631
* Compute the size of the charset. For simplicity, we currently
1632
* always store the charset explicitly.
1634
swrite_position_only(&poss);
1635
cff_write_cidset(&writer, &genum);
1636
charset_size = stell(&poss);
1638
/* Compute the size of the FDSelect strucure. */
1639
fdselect_size = cff_FDSelect_size(&writer, &genum, &fdselect_format);
1641
/* Compute the size of the CharStrings Index. */
1643
cff_write_CharStrings_offsets(&writer, &genum, &charstrings_count);
1645
/* Compute the size of the (local) Subrs Indexes. */
1646
for (j = 0; j < num_fonts; ++j) {
1647
gs_font_type1 *pfd = pfont->cidata.FDArray[j];
1649
#ifdef SKIP_EMPTY_SUBRS
1651
(cff_convert_charstrings(&writer, (gs_font_base *)pfd) ? 0 :
1652
cff_write_Subrs_offsets(&writer, &subrs_count[j], pfd, false));
1654
if (cff_convert_charstrings(&writer, (gs_font_base *)pfd))
1655
subrs_count[j] = 0; /* we expand all Subrs */
1656
subrs_size[j] = cff_write_Subrs_offsets(&writer, &subrs_count[j], pfd, false);
1660
/* Get the font_info once, since it may be expensive. */
1661
cff_get_Top_info_common(&writer, (gs_font_base *)pfont, true, &info);
1664
* The offsets of the Private Dict and the CharStrings Index
1665
* depend on the size of the Top Dict; the offset of the Subrs also
1666
* depends on the size of the Private Dict. However, the size of the
1667
* Top Dict depends on the offsets of the CharStrings Index and the
1668
* charset, and on the offset and size of the Private Dict,
1669
* because of the variable-length encoding of the offsets and
1670
* size; for the same reason, the size of the Private Dict depends on
1671
* the offset of the Subrs. Fortunately, the relationship between the
1672
* value of an offset or size and the size of its encoding is monotonic.
1673
* Therefore, we start by assuming the largest reasonable value for all
1674
* the sizes and iterate until everything converges.
1677
swrite_position_only(&poss);
1678
writer.strm = &poss;
1680
/* Compute the offsets. */
1681
GSubrs_offset = 4 + cff_Index_size(1, font_name.size) +
1682
cff_Index_size(1, Top_size) +
1683
cff_Index_size(writer.strings.count, writer.strings.total);
1684
charset_offset = GSubrs_offset +
1685
cff_Index_size(gsubrs_count, gsubrs_size);
1686
FDSelect_offset = charset_offset + charset_size;
1687
CharStrings_offset = FDSelect_offset + fdselect_size;
1688
if_debug4('l', "[l]GSubrs at %u, charset at %u, FDSelect at %u, CharStrings at %u\n",
1689
GSubrs_offset, charset_offset, FDSelect_offset, CharStrings_offset);
1692
start_pos = stell(writer.strm);
1693
if_debug1('l', "[l]start_pos = %ld\n", start_pos);
1694
/* Write the header, setting offset_size. */
1695
cff_write_header(&writer, End_offset);
1697
/* Write the names Index. */
1698
cff_put_Index_header(&writer, 1, font_name.size);
1699
put_offset(&writer, font_name.size + 1);
1700
put_bytes(writer.strm, font_name.data, font_name.size);
1702
/* Write the Top Index. */
1703
cff_put_Index_header(&writer, 1, Top_size);
1704
put_offset(&writer, Top_size + 1);
1705
offset = stell(writer.strm) - start_pos;
1706
cff_write_Top_cidfont(&writer, charset_offset, CharStrings_offset,
1707
FDSelect_offset, Font_offset, &info);
1708
Top_size = stell(writer.strm) - start_pos - offset;
1709
if_debug1('l', "[l]Top_size = %u\n", Top_size);
1711
/* Write the strings Index. */
1712
cff_put_Index(&writer, &writer.strings);
1714
/* Write the GSubrs Index, if any, checking the offset. */
1715
offset = stell(writer.strm) - start_pos;
1716
if_debug2('l', "[l]GSubrs = %u => %u\n", GSubrs_offset, offset);
1717
if (offset > GSubrs_offset)
1718
return_error(gs_error_rangecheck);
1719
GSubrs_offset = offset;
1720
if (gsubrs_count == 0 ||
1721
cff_convert_charstrings(&writer,
1722
(const gs_font_base *)pfont->cidata.FDArray[0])
1724
cff_put_Index_header(&writer, 0, 0);
1726
cff_write_Subrs(&writer, gsubrs_count, gsubrs_size,
1727
pfont->cidata.FDArray[0], true);
1729
/* Write the charset. */
1730
if_debug1('l', "[l]charset = %lu\n", stell(writer.strm) - start_pos);
1731
cff_write_cidset(&writer, &genum);
1733
/* Write the FDSelect structure, checking the offset. */
1734
offset = stell(writer.strm) - start_pos;
1735
if_debug2('l', "[l]FDSelect = %u => %u\n", FDSelect_offset, offset);
1736
if (offset > FDSelect_offset)
1737
return_error(offset_error("FDselect"));
1738
FDSelect_offset = offset;
1739
cff_write_FDSelect(&writer, &genum, fdselect_size, fdselect_format);
1741
/* Write the CharStrings Index, checking the offset. */
1742
offset = stell(writer.strm) - start_pos;
1743
if_debug2('l', "[l]CharStrings = %u => %u\n", CharStrings_offset, offset);
1744
if (offset > CharStrings_offset)
1745
return_error(offset_error("CharStrings"));
1746
CharStrings_offset = offset;
1747
cff_write_CharStrings(&writer, &genum, charstrings_count,
1750
/* Write the Font Dict Index. */
1751
offset = stell(writer.strm) - start_pos;
1752
if_debug2('l', "[l]Font = %u => %u\n", Font_offset, offset);
1753
if (offset > Font_offset)
1754
return_error(offset_error("Font"));
1755
Font_offset = offset;
1756
cff_write_FDArray_offsets(&writer, FDArray_offsets, num_fonts);
1757
offset = stell(writer.strm) - start_pos;
1758
if_debug2('l', "[l]FDArray[0] = %u => %u\n", FDArray_offsets[0], offset);
1759
if (offset > FDArray_offsets[0])
1760
return_error(offset_error("FDArray[0]"));
1761
FDArray_offsets[0] = offset;
1762
for (j = 0; j < num_fonts; ++j) {
1763
gs_font_type1 *pfd = pfont->cidata.FDArray[j];
1765
/* If we're writing Type 2 CharStrings, don't encrypt them. */
1766
if (options & WRITE_TYPE2_CHARSTRINGS) {
1767
options |= WRITE_TYPE2_NO_LENIV;
1768
if (pfd->FontType != ft_encrypted2)
1769
pfd->data.defaultWidthX = pfd->data.nominalWidthX = 0;
1771
cff_write_Top_fdarray(&writer, (gs_font_base *)pfd, Private_offsets[j],
1772
Private_offsets[j + 1] - Private_offsets[j]);
1773
offset = stell(writer.strm) - start_pos;
1774
if_debug3('l', "[l]FDArray[%d] = %u => %u\n", j + 1,
1775
FDArray_offsets[j + 1], offset);
1776
if (offset > FDArray_offsets[j + 1])
1777
return_error(offset_error("FDArray"));
1778
FDArray_offsets[j + 1] = offset;
1781
/* Write the Private Dicts, checking the offset. */
1782
for (j = 0; ; ++j) {
1785
offset = stell(writer.strm) - start_pos;
1786
if_debug3('l', "[l]Private[%d] = %u => %u\n",
1787
j, Private_offsets[j], offset);
1788
if (offset > Private_offsets[j])
1789
return_error(offset_error("Private"));
1790
Private_offsets[j] = offset;
1793
pfd = pfont->cidata.FDArray[j];
1794
cff_write_Private(&writer,
1795
(subrs_size[j] == 0 ? 0 : Subrs_offsets[j]), pfd);
1798
/* Write the Subrs Indexes, checking the offsets. */
1799
for (j = 0; ; ++j) {
1802
offset = stell(writer.strm) - (start_pos + Private_offsets[j]);
1803
if_debug3('l', "[l]Subrs[%d] = %u => %u\n",
1804
j, Subrs_offsets[j], offset);
1805
if (offset > Subrs_offsets[j])
1806
return_error(offset_error("Subrs"));
1807
Subrs_offsets[j] = offset;
1810
pfd = pfont->cidata.FDArray[j];
1811
if (cff_convert_charstrings(&writer, (gs_font_base *)pfd))
1812
cff_put_Index_header(&writer, 0, 0);
1813
else if (subrs_size[j] != 0)
1814
cff_write_Subrs(&writer, subrs_count[j], subrs_size[j], pfd, false);
1817
/* Check the final offset. */
1818
offset = stell(writer.strm) - start_pos;
1819
if_debug2('l', "[l]End = %u => %u\n", End_offset, offset);
1820
if (offset > End_offset)
1821
return_error(offset_error("End"));
1822
if (offset == End_offset) {
1823
/* The iteration has converged. Write the result. */
1824
if (writer.strm == &poss) {
1829
/* No convergence yet. */
1830
End_offset = offset;