| 1 |
/* Copyright (C) 2020-2025 Artifex Software, Inc. |
| 2 |
All Rights Reserved. |
| 3 |
|
| 4 |
This software is provided AS-IS with no warranty, either express or |
| 5 |
implied. |
| 6 |
|
| 7 |
This software is distributed under license and may not be copied, |
| 8 |
modified or distributed except as expressly authorized under the terms |
| 9 |
of the license contained in the file LICENSE in this distribution. |
| 10 |
|
| 11 |
Refer to licensing information at http://www.artifex.com or contact |
| 12 |
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
| 13 |
CA 94129, USA, for further information. |
| 14 |
*/ |
| 15 |
|
| 16 |
/* code for CIDFontType2/Type 9 font handling */ |
| 17 |
/* CIDFonts with Truetype outlines */ |
| 18 |
|
| 19 |
#include "pdf_int.h" |
| 20 |
#include "pdf_font.h" |
| 21 |
#include "pdf_font0.h" |
| 22 |
#include "pdf_fontTT.h" |
| 23 |
#include "pdf_font_types.h" |
| 24 |
#include "pdf_stack.h" |
| 25 |
#include "pdf_file.h" |
| 26 |
#include "pdf_dict.h" |
| 27 |
#include "pdf_array.h" |
| 28 |
#include "pdf_deref.h" |
| 29 |
#include "gxfont42.h" |
| 30 |
#include "gxfcid.h" |
| 31 |
#include "gsutil.h" /* For gs_next_ids() */ |
| 32 |
|
| 33 |
static int pdfi_cidtype2_string_proc(gs_font_type42 * pfont, ulong offset, uint length, |
| 34 |
const byte ** pdata) |
| 35 |
{ |
| 36 |
pdf_cidfont_type2 *ttfont = (pdf_cidfont_type2 *)pfont->client_data; |
| 37 |
int code = 0; |
| 38 |
|
| 39 |
if (offset + length > ttfont->sfnt->length) { |
| 40 |
*pdata = NULL; |
| 41 |
code = gs_note_error(gs_error_invalidfont); |
| 42 |
} |
| 43 |
else { |
| 44 |
*pdata = ttfont->sfnt->data + offset; |
| 45 |
} |
| 46 |
return code; |
| 47 |
} |
| 48 |
|
| 49 |
static int pdfi_cidtype2_CIDMap_proc(gs_font_cid2 *pfont, gs_glyph glyph) |
| 50 |
{ |
| 51 |
pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)pfont->client_data; |
| 52 |
uint gid = glyph - GS_MIN_CID_GLYPH; |
| 53 |
int code = 0; |
| 54 |
|
| 55 |
if (pdffont11->substitute == true) { |
| 56 |
unsigned int ucc = 0; |
| 57 |
int code = pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, NULL, 0); |
| 58 |
if (code == 2) { |
| 59 |
uchar sccode[2] = {0}; |
| 60 |
(void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&sccode, 2); |
| 61 |
ucc = (sccode[0] << 8) + sccode[1]; |
| 62 |
} |
| 63 |
else if (code == 4) { |
| 64 |
uchar iccode[4] = {0}; |
| 65 |
(void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&iccode, 2); |
| 66 |
ucc = (iccode[0] << 24) + (iccode[1] << 16) + (iccode[2] << 8) + iccode[3]; |
| 67 |
|
| 68 |
} |
| 69 |
if (code == 2 || code == 4) { |
| 70 |
code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (unsigned int)ucc, &gid); |
| 71 |
if (code < 0 || gid == 0) |
| 72 |
gid = glyph - GS_MIN_CID_GLYPH; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if (code == 0 && pdffont11->cidtogidmap != NULL && pdffont11->cidtogidmap->length > (gid << 1) + 1) { |
| 77 |
gid = pdffont11->cidtogidmap->data[gid << 1] << 8 | pdffont11->cidtogidmap->data[(gid << 1) + 1]; |
| 78 |
} |
| 79 |
|
| 80 |
return (int)gid; |
| 81 |
} |
| 82 |
|
| 83 |
static uint pdfi_cidtype2_get_glyph_index(gs_font_type42 *pfont, gs_glyph glyph) |
| 84 |
{ |
| 85 |
pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)pfont->client_data; |
| 86 |
uint gid = glyph - GS_MIN_CID_GLYPH; |
| 87 |
int code = 0; |
| 88 |
|
| 89 |
if (glyph < GS_MIN_CID_GLYPH) { |
| 90 |
gid = 0; |
| 91 |
} |
| 92 |
else { |
| 93 |
if (glyph < GS_MIN_GLYPH_INDEX) { |
| 94 |
if (pdffont11->substitute == true) { |
| 95 |
unsigned int ucc = 0; |
| 96 |
code = pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, NULL, 0); |
| 97 |
if (code == 2) { |
| 98 |
uchar sccode[2] = {0}; |
| 99 |
(void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&sccode, 2); |
| 100 |
ucc = (sccode[0] << 8) + sccode[1]; |
| 101 |
} |
| 102 |
else if (code == 4) { |
| 103 |
uchar iccode[4] = {0}; |
| 104 |
(void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&iccode, 2); |
| 105 |
ucc = (iccode[0] << 24) + (iccode[1] << 16) + (iccode[2] << 8) + iccode[3]; |
| 106 |
|
| 107 |
} |
| 108 |
if (code == 2 || code == 4) { |
| 109 |
code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (unsigned int)ucc, &gid); |
| 110 |
if (code < 0 || gid == 0) |
| 111 |
gid = glyph - GS_MIN_CID_GLYPH; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (code == 0 && pdffont11->cidtogidmap != NULL && pdffont11->cidtogidmap->length > (gid << 1) + 1) { |
| 116 |
gid = pdffont11->cidtogidmap->data[gid << 1] << 8 | pdffont11->cidtogidmap->data[(gid << 1) + 1]; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return gid; |
| 122 |
} |
| 123 |
|
| 124 |
static int |
| 125 |
pdfi_cidtype2_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat, |
| 126 |
int members, gs_glyph_info_t *info) |
| 127 |
{ |
| 128 |
int code; |
| 129 |
pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)font->client_data; |
| 130 |
int submembers = members & ~(GLYPH_INFO_WIDTHS | GLYPH_INFO_VVECTOR0 | GLYPH_INFO_VVECTOR1 | GLYPH_INFO_CDEVPROC); |
| 131 |
|
| 132 |
/* Call with the values we should get below removed from the "members" request */ |
| 133 |
code = (*pdffont11->orig_glyph_info)(font, glyph, pmat, submembers, info); |
| 134 |
|
| 135 |
if (code >= 0 && (members & GLYPH_INFO_WIDTHS) != 0 |
| 136 |
&& glyph > GS_MIN_CID_GLYPH |
| 137 |
&& glyph < GS_MIN_GLYPH_INDEX) { |
| 138 |
double widths[6] = {0}; |
| 139 |
code = pdfi_get_cidfont_glyph_metrics(font, (glyph - GS_MIN_CID_GLYPH), widths, true); |
| 140 |
if (code < 0) { |
| 141 |
/* If we couldn't get values back from W/W2, give up, and fill everything in from glyph_info */ |
| 142 |
code = (*pdffont11->orig_glyph_info)(font, glyph, pmat, members, info); |
| 143 |
} |
| 144 |
else { |
| 145 |
if (pmat == NULL) { |
| 146 |
info->width[0].x = widths[GLYPH_W0_WIDTH_INDEX] / 1000.0; |
| 147 |
info->width[0].y = widths[GLYPH_W0_HEIGHT_INDEX] / 1000.0; |
| 148 |
} |
| 149 |
else { |
| 150 |
code = gs_point_transform(widths[GLYPH_W0_WIDTH_INDEX] / 1000.0, widths[GLYPH_W0_HEIGHT_INDEX] / 1000.0, pmat, &info->width[0]); |
| 151 |
if (code < 0) |
| 152 |
return code; |
| 153 |
} |
| 154 |
info->members |= GLYPH_INFO_WIDTH0; |
| 155 |
|
| 156 |
if ((members & GLYPH_INFO_WIDTH1) != 0 |
| 157 |
&& (widths[GLYPH_W1_WIDTH_INDEX] != 0 |
| 158 |
|| widths[GLYPH_W1_HEIGHT_INDEX] != 0)) { |
| 159 |
if (pmat == NULL) { |
| 160 |
info->width[1].x = widths[GLYPH_W1_WIDTH_INDEX] / 1000.0; |
| 161 |
info->width[1].y = widths[GLYPH_W1_HEIGHT_INDEX] / 1000.0; |
| 162 |
} |
| 163 |
else { |
| 164 |
code = gs_point_transform(widths[GLYPH_W1_WIDTH_INDEX] / 1000.0, widths[GLYPH_W1_HEIGHT_INDEX] / 1000.0, pmat, &info->width[1]); |
| 165 |
if (code < 0) |
| 166 |
return code; |
| 167 |
} |
| 168 |
info->members |= GLYPH_INFO_WIDTH1; |
| 169 |
} |
| 170 |
if ((members & GLYPH_INFO_VVECTOR1) != 0) { |
| 171 |
if (pmat == NULL) { |
| 172 |
info->v.x = widths[GLYPH_W1_V_X_INDEX] / 1000.0; |
| 173 |
info->v.y = widths[GLYPH_W1_V_Y_INDEX] / 1000.0; |
| 174 |
} |
| 175 |
else { |
| 176 |
code = gs_point_transform(widths[GLYPH_W1_V_X_INDEX] / 1000.0, widths[GLYPH_W1_V_Y_INDEX] / 1000.0, pmat, &info->v); |
| 177 |
if (code < 0) |
| 178 |
return code; |
| 179 |
} |
| 180 |
info->members |= GLYPH_INFO_VVECTOR1; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return code; |
| 186 |
} |
| 187 |
|
| 188 |
static int |
| 189 |
pdfi_cidtype2_enumerate_glyph(gs_font *font, int *pindex, |
| 190 |
gs_glyph_space_t glyph_space, gs_glyph *pglyph) |
| 191 |
{ |
| 192 |
int code = 0; |
| 193 |
gs_font_cid2 *cid2 = (gs_font_cid2 *)font; |
| 194 |
pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)font->client_data; |
| 195 |
*pglyph = 0; |
| 196 |
|
| 197 |
if (*pindex <= 0) |
| 198 |
*pindex = 0; |
| 199 |
|
| 200 |
if (pdffont11->cidtogidmap != NULL && pdffont11->cidtogidmap->length > 0) { |
| 201 |
do { |
| 202 |
*pglyph = pdffont11->cidtogidmap->data[(*pindex) << 1] << 8 | pdffont11->cidtogidmap->data[((*pindex) << 1) + 1]; |
| 203 |
(*pindex)++; |
| 204 |
if (*pglyph == 0 && *pindex == 1) /* notdef - special case */ |
| 205 |
break; |
| 206 |
} while (*pglyph == 0 && ((*pindex) << 1) < pdffont11->cidtogidmap->length); |
| 207 |
|
| 208 |
if (((*pindex) << 1) >= pdffont11->cidtogidmap->length) { |
| 209 |
*pindex = 0; |
| 210 |
} |
| 211 |
else { |
| 212 |
if (*pglyph != 0 || (*pglyph == 0 && *pindex == 1)) { |
| 213 |
if (glyph_space == GLYPH_SPACE_INDEX) { |
| 214 |
*pglyph += GS_MIN_GLYPH_INDEX; |
| 215 |
} |
| 216 |
else { |
| 217 |
*pglyph = (*pindex) + GS_MIN_CID_GLYPH; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
else { |
| 223 |
if (*pindex < cid2->cidata.common.CIDCount) { |
| 224 |
if (glyph_space == GLYPH_SPACE_INDEX) { |
| 225 |
*pglyph = *pindex + GS_MIN_GLYPH_INDEX; |
| 226 |
} |
| 227 |
else { |
| 228 |
*pglyph = (*pindex) + GS_MIN_CID_GLYPH; |
| 229 |
} |
| 230 |
} |
| 231 |
else { |
| 232 |
*pindex = 0; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return code; |
| 237 |
} |
| 238 |
|
| 239 |
static void pdfi_set_cidtype2_custom_procs(pdf_cidfont_type2 *pdfttfont) |
| 240 |
{ |
| 241 |
gs_font_type42 *pfont = (gs_font_type42 *)pdfttfont->pfont; |
| 242 |
pdfttfont->default_font_info = pfont->procs.font_info; |
| 243 |
pfont->procs.font_info = pdfi_default_font_info; |
| 244 |
} |
| 245 |
|
| 246 |
static int |
| 247 |
pdfi_alloc_cidtype2_font(pdf_context *ctx, pdf_cidfont_type2 **font, bool is_cid) |
| 248 |
{ |
| 249 |
pdf_cidfont_type2 *ttfont = NULL; |
| 250 |
gs_font_cid2 *pfont = NULL; |
| 251 |
|
| 252 |
ttfont = (pdf_cidfont_type2 *)gs_alloc_bytes(ctx->memory, sizeof(pdf_cidfont_type2), "pdfi (cidtype2 pdf_font)"); |
| 253 |
if (ttfont == NULL) |
| 254 |
return_error(gs_error_VMerror); |
| 255 |
|
| 256 |
memset(ttfont, 0x00, sizeof(pdf_cidfont_type2)); |
| 257 |
ttfont->type = PDF_FONT; |
| 258 |
ttfont->ctx = ctx; |
| 259 |
ttfont->pdfi_font_type = e_pdf_cidfont_type2; |
| 260 |
|
| 261 |
#if REFCNT_DEBUG |
| 262 |
ttfont->UID = ctx->UID++; |
| 263 |
outprintf(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", ttfont->type, ttfont->UID); |
| 264 |
#endif |
| 265 |
|
| 266 |
pdfi_countup(ttfont); |
| 267 |
|
| 268 |
pfont = (gs_font_cid2 *)gs_alloc_struct(ctx->memory, gs_font_cid2, &st_gs_font_cid2, |
| 269 |
"pdfi (cidtype2 pfont)"); |
| 270 |
if (pfont == NULL) { |
| 271 |
pdfi_countdown(ttfont); |
| 272 |
return_error(gs_error_VMerror); |
| 273 |
} |
| 274 |
memset(pfont, 0x00, sizeof(gs_font_cid2)); |
| 275 |
|
| 276 |
ttfont->pfont = (gs_font_base *)pfont; |
| 277 |
|
| 278 |
gs_make_identity(&pfont->orig_FontMatrix); |
| 279 |
gs_make_identity(&pfont->FontMatrix); |
| 280 |
pfont->next = pfont->prev = 0; |
| 281 |
pfont->memory = ctx->memory; |
| 282 |
pfont->dir = ctx->font_dir; |
| 283 |
pfont->is_resource = false; |
| 284 |
gs_notify_init(&pfont->notify_list, ctx->memory); |
| 285 |
pfont->base = (gs_font *) ttfont->pfont; |
| 286 |
pfont->client_data = ttfont; |
| 287 |
pfont->WMode = 0; |
| 288 |
pfont->PaintType = 0; |
| 289 |
pfont->StrokeWidth = 0; |
| 290 |
pfont->is_cached = 0; |
| 291 |
pfont->FAPI = NULL; |
| 292 |
pfont->FAPI_font_data = NULL; |
| 293 |
pfont->procs.init_fstack = gs_default_init_fstack; |
| 294 |
pfont->procs.next_char_glyph = gs_default_next_char_glyph; |
| 295 |
pfont->FontType = ft_CID_TrueType; |
| 296 |
pfont->ExactSize = fbit_use_outlines; |
| 297 |
pfont->InBetweenSize = fbit_use_outlines; |
| 298 |
pfont->TransformedChar = fbit_use_outlines; |
| 299 |
/* We may want to do something clever with an XUID here */ |
| 300 |
pfont->id = gs_next_ids(ctx->memory, 1); |
| 301 |
uid_set_UniqueID(&pfont->UID, pfont->id); |
| 302 |
/* The buildchar proc will be filled in by FAPI - |
| 303 |
we won't worry about working without FAPI */ |
| 304 |
pfont->procs.encode_char = pdfi_encode_char; |
| 305 |
pfont->data.string_proc = pdfi_cidtype2_string_proc; |
| 306 |
pfont->procs.glyph_name = ctx->get_glyph_name; |
| 307 |
pfont->procs.decode_glyph = pdfi_cidfont_decode_glyph; |
| 308 |
pfont->procs.define_font = gs_no_define_font; |
| 309 |
pfont->procs.make_font = gs_no_make_font; |
| 310 |
|
| 311 |
ttfont->default_font_info = gs_default_font_info; |
| 312 |
pfont->procs.font_info = pdfi_default_font_info; |
| 313 |
|
| 314 |
pfont->procs.glyph_info = gs_default_glyph_info; |
| 315 |
pfont->procs.glyph_outline = gs_no_glyph_outline; |
| 316 |
pfont->procs.build_char = NULL; |
| 317 |
pfont->procs.same_font = gs_default_same_font; |
| 318 |
pfont->procs.enumerate_glyph = gs_no_enumerate_glyph; |
| 319 |
|
| 320 |
pfont->encoding_index = ENCODING_INDEX_UNKNOWN; |
| 321 |
pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN; |
| 322 |
|
| 323 |
cid_system_info_set_null(&pfont->cidata.common.CIDSystemInfo); |
| 324 |
pfont->cidata.common.CIDCount = 0; /* set later */ |
| 325 |
pfont->cidata.common.GDBytes = 2; /* not used */ |
| 326 |
pfont->cidata.MetricsCount = 0; |
| 327 |
pfont->cidata.CIDMap_proc = pdfi_cidtype2_CIDMap_proc; |
| 328 |
|
| 329 |
pfont->client_data = (void *)ttfont; |
| 330 |
|
| 331 |
*font = ttfont; |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
int pdfi_read_cidtype2_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, byte *buf, int64_t buflen, int findex, pdf_font **ppfont) |
| 336 |
{ |
| 337 |
pdf_cidfont_type2 *font; |
| 338 |
int code = 0; |
| 339 |
pdf_obj *fontdesc = NULL; |
| 340 |
pdf_obj *obj = NULL; |
| 341 |
gs_font_cid2 *cid2; |
| 342 |
|
| 343 |
if (ppfont == NULL) |
| 344 |
return_error(gs_error_invalidaccess); |
| 345 |
|
| 346 |
*ppfont = NULL; |
| 347 |
|
| 348 |
code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc); |
| 349 |
if (code <= 0) { |
| 350 |
/* We own the buffer now, so we must free it on error */ |
| 351 |
gs_free_object(ctx->memory, buf, "pdfi_read_cidtype2_font"); |
| 352 |
return_error(gs_error_invalidfont); |
| 353 |
} |
| 354 |
|
| 355 |
if ((code = pdfi_alloc_cidtype2_font(ctx, &font, false)) < 0) { |
| 356 |
/* We own the buffer now, so we must free it on error */ |
| 357 |
gs_free_object(ctx->memory, buf, "pdfi_read_cidtype2_font"); |
| 358 |
pdfi_countdown(fontdesc); |
| 359 |
return code; |
| 360 |
} |
| 361 |
font->PDF_font = font_dict; |
| 362 |
pdfi_countup(font_dict); |
| 363 |
font->object_num = font_dict->object_num; |
| 364 |
font->generation_num = font_dict->generation_num; |
| 365 |
font->indirect_num = font_dict->indirect_num; |
| 366 |
font->indirect_gen = font_dict->indirect_gen; |
| 367 |
|
| 368 |
font->FontDescriptor = (pdf_dict *)fontdesc; |
| 369 |
fontdesc = NULL; |
| 370 |
|
| 371 |
code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&font->sfnt); |
| 372 |
if (code < 0) { |
| 373 |
goto error; |
| 374 |
} |
| 375 |
pdfi_countup(font->sfnt); |
| 376 |
code = pdfi_buffer_set_data((pdf_obj *)font->sfnt, buf, buflen); |
| 377 |
if (code < 0) { |
| 378 |
goto error; |
| 379 |
} |
| 380 |
buf = NULL; |
| 381 |
|
| 382 |
/* Strictly speaking BaseFont is required, but we can continue without one */ |
| 383 |
code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, (pdf_obj **)&obj); |
| 384 |
if (code > 0) { |
| 385 |
pdf_name *nobj = (pdf_name *)obj; |
| 386 |
int nlen = nobj->length > gs_font_name_max ? gs_font_name_max : nobj->length; |
| 387 |
|
| 388 |
memcpy(font->pfont->key_name.chars, nobj->data, nlen); |
| 389 |
font->pfont->key_name.chars[nlen] = 0; |
| 390 |
font->pfont->key_name.size = nlen; |
| 391 |
memcpy(font->pfont->font_name.chars, nobj->data, nlen); |
| 392 |
font->pfont->font_name.chars[nlen] = 0; |
| 393 |
font->pfont->font_name.size = nlen; |
| 394 |
pdfi_countdown(obj); |
| 395 |
obj = NULL; |
| 396 |
} |
| 397 |
|
| 398 |
code = pdfi_dict_knownget_number(ctx, font_dict, "DW", &font->DW); |
| 399 |
if (code <= 0) { |
| 400 |
font->DW = 1000; |
| 401 |
} |
| 402 |
|
| 403 |
code = pdfi_dict_knownget_type(ctx, font_dict, "DW2", PDF_ARRAY, (pdf_obj **)&obj); |
| 404 |
if (code > 0) { |
| 405 |
font->DW2 = (pdf_array *)obj; |
| 406 |
obj = NULL; |
| 407 |
} |
| 408 |
else { |
| 409 |
font->DW2 = NULL; |
| 410 |
} |
| 411 |
code = pdfi_dict_knownget_type(ctx, font_dict, "W", PDF_ARRAY, (pdf_obj **)&obj); |
| 412 |
if (code > 0) { |
| 413 |
font->W = (pdf_array *)obj; |
| 414 |
obj = NULL; |
| 415 |
} |
| 416 |
else { |
| 417 |
font->W = NULL; |
| 418 |
} |
| 419 |
code = pdfi_dict_knownget_type(ctx, font_dict, "W2", PDF_ARRAY, (pdf_obj **)&obj); |
| 420 |
if (code > 0) { |
| 421 |
font->W2 = (pdf_array *)obj; |
| 422 |
obj = NULL; |
| 423 |
} |
| 424 |
else { |
| 425 |
font->W2 = NULL; |
| 426 |
} |
| 427 |
|
| 428 |
code = pdfi_dict_knownget(ctx, font_dict, "CIDToGIDMap", (pdf_obj **)&obj); |
| 429 |
if (code > 0) { |
| 430 |
/* CIDToGIDMap can only be a stream or a name, and if it's a name |
| 431 |
it's only permitted to be "/Identity", so ignore it |
| 432 |
*/ |
| 433 |
if (pdfi_type_of(obj) == PDF_STREAM) { |
| 434 |
byte *d; |
| 435 |
int64_t sz = 0; |
| 436 |
|
| 437 |
code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&font->cidtogidmap); |
| 438 |
if (code < 0) { |
| 439 |
goto error; |
| 440 |
} |
| 441 |
pdfi_countup(font->cidtogidmap); |
| 442 |
code = pdfi_stream_to_buffer(ctx, (pdf_stream *)obj, &d, &sz); |
| 443 |
if (code < 0) { |
| 444 |
goto error; |
| 445 |
} |
| 446 |
code = pdfi_buffer_set_data((pdf_obj *)font->cidtogidmap, d, (int32_t)sz); |
| 447 |
if (code < 0) { |
| 448 |
goto error; |
| 449 |
} |
| 450 |
} |
| 451 |
pdfi_countdown(obj); |
| 452 |
obj = NULL; |
| 453 |
} |
| 454 |
|
| 455 |
cid2 = (gs_font_cid2 *)font->pfont; |
| 456 |
|
| 457 |
code = pdfi_dict_knownget_type(ctx, font_dict, "CIDSystemInfo", PDF_DICT, (pdf_obj **)&obj); |
| 458 |
if (code <= 0) { |
| 459 |
cid2->cidata.common.CIDSystemInfo.Registry.data = NULL; |
| 460 |
cid2->cidata.common.CIDSystemInfo.Registry.size = 0; |
| 461 |
cid2->cidata.common.CIDSystemInfo.Ordering.data = NULL; |
| 462 |
cid2->cidata.common.CIDSystemInfo.Ordering.size = 0; |
| 463 |
} |
| 464 |
else { |
| 465 |
pdf_num *suppl = NULL; |
| 466 |
|
| 467 |
code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Registry", PDF_STRING, (pdf_obj **)&font->registry); |
| 468 |
if (code <= 0) { |
| 469 |
cid2->cidata.common.CIDSystemInfo.Registry.data = NULL; |
| 470 |
cid2->cidata.common.CIDSystemInfo.Registry.size = 0; |
| 471 |
} |
| 472 |
else { |
| 473 |
cid2->cidata.common.CIDSystemInfo.Registry.data = font->registry->data; |
| 474 |
cid2->cidata.common.CIDSystemInfo.Registry.size = font->registry->length; |
| 475 |
} |
| 476 |
code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Ordering", PDF_STRING, (pdf_obj **)&font->ordering); |
| 477 |
if (code <= 0) { |
| 478 |
cid2->cidata.common.CIDSystemInfo.Ordering.data = NULL; |
| 479 |
cid2->cidata.common.CIDSystemInfo.Ordering.size = 0; |
| 480 |
} |
| 481 |
else { |
| 482 |
cid2->cidata.common.CIDSystemInfo.Ordering.data = font->ordering->data; |
| 483 |
cid2->cidata.common.CIDSystemInfo.Ordering.size = font->ordering->length; |
| 484 |
} |
| 485 |
code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Supplement", PDF_INT, (pdf_obj **)&suppl); |
| 486 |
if (code <= 0) { |
| 487 |
cid2->cidata.common.CIDSystemInfo.Supplement = font->supplement = 0; |
| 488 |
} |
| 489 |
else { |
| 490 |
cid2->cidata.common.CIDSystemInfo.Supplement = font->supplement = suppl->value.i; |
| 491 |
} |
| 492 |
pdfi_countdown(suppl); |
| 493 |
} |
| 494 |
pdfi_countdown(obj); |
| 495 |
obj = NULL; |
| 496 |
|
| 497 |
|
| 498 |
code = gs_type42_font_init((gs_font_type42 *)font->pfont, findex); |
| 499 |
if (code < 0) { |
| 500 |
goto error; |
| 501 |
} |
| 502 |
pdfi_set_cidtype2_custom_procs(font); |
| 503 |
|
| 504 |
if (uid_is_XUID(&font->pfont->UID)) |
| 505 |
uid_free(&font->pfont->UID, font->pfont->memory, "pdfi_read_type1_font"); |
| 506 |
uid_set_invalid(&font->pfont->UID); |
| 507 |
font->pfont->id = gs_next_ids(ctx->memory, 1); |
| 508 |
|
| 509 |
code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont); |
| 510 |
if (code < 0) |
| 511 |
goto error; |
| 512 |
|
| 513 |
|
| 514 |
font->orig_glyph_info = font->pfont->procs.glyph_info; |
| 515 |
font->pfont->procs.glyph_info = pdfi_cidtype2_glyph_info; |
| 516 |
font->pfont->procs.enumerate_glyph = pdfi_cidtype2_enumerate_glyph; |
| 517 |
|
| 518 |
if (font->cidtogidmap != NULL) { |
| 519 |
gs_font_cid2 *cid2 = (gs_font_cid2 *)font->pfont; |
| 520 |
if (cid2->data.numGlyphs > font->cidtogidmap->length >> 1) |
| 521 |
cid2->cidata.common.CIDCount = cid2->data.numGlyphs; |
| 522 |
else { |
| 523 |
cid2->cidata.common.CIDCount = font->cidtogidmap->length >> 1; |
| 524 |
} |
| 525 |
cid2->cidata.common.MaxCID = cid2->cidata.common.CIDCount; |
| 526 |
} |
| 527 |
else { |
| 528 |
gs_font_cid2 *cid2 = (gs_font_cid2 *)font->pfont; |
| 529 |
cid2->cidata.common.CIDCount = cid2->data.numGlyphs; |
| 530 |
cid2->cidata.common.MaxCID = cid2->cidata.common.CIDCount; |
| 531 |
} |
| 532 |
cid2->data.substitute_glyph_index_vertical = gs_type42_substitute_glyph_index_vertical; |
| 533 |
cid2->cidata.orig_procs.get_outline = cid2->data.get_outline; |
| 534 |
cid2->data.get_glyph_index = pdfi_cidtype2_get_glyph_index; |
| 535 |
|
| 536 |
pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font); |
| 537 |
code = gs_definefont(ctx->font_dir, (gs_font *)font->pfont); |
| 538 |
if (code < 0) { |
| 539 |
goto error; |
| 540 |
} |
| 541 |
|
| 542 |
code = pdfi_fapi_passfont((pdf_font *)font, findex, NULL, NULL, font->sfnt->data, font->sfnt->length); |
| 543 |
if (code < 0) { |
| 544 |
goto error; |
| 545 |
} |
| 546 |
|
| 547 |
/* object_num can be zero if the dictionary was defined inline */ |
| 548 |
if (font->object_num != 0) { |
| 549 |
(void)replace_cache_entry(ctx, (pdf_obj *)font); |
| 550 |
} |
| 551 |
|
| 552 |
*ppfont = (pdf_font *)font; |
| 553 |
return code; |
| 554 |
error: |
| 555 |
pdfi_countdown(obj); |
| 556 |
obj = NULL; |
| 557 |
if (pdfi_dict_get(ctx, font_dict, ".Path", &obj) >= 0) |
| 558 |
{ |
| 559 |
char fname[gp_file_name_sizeof + 1]; |
| 560 |
pdf_string *fobj = (pdf_string *)obj; |
| 561 |
|
| 562 |
memcpy(fname, fobj->data, fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length); |
| 563 |
fname[fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length] = '\0'; |
| 564 |
|
| 565 |
(void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_cidtype2_font", "Error reading CIDType2/TrueType font file %s\n", fname); |
| 566 |
} |
| 567 |
else { |
| 568 |
(void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_cidtype2_font", "Error reading embedded CIDType2/TrueType font object %u\n", font_dict->object_num); |
| 569 |
} |
| 570 |
|
| 571 |
pdfi_countdown(obj); |
| 572 |
pdfi_countdown(font); |
| 573 |
return code; |
| 574 |
} |
| 575 |
|
| 576 |
int pdfi_free_font_cidtype2(pdf_obj *font) |
| 577 |
{ |
| 578 |
pdf_cidfont_type2 *pdfcidf = (pdf_cidfont_type2 *)font; |
| 579 |
gs_font_cid2 *pfont = (gs_font_cid2 *)pdfcidf->pfont; |
| 580 |
gs_free_object(OBJ_MEMORY(pdfcidf), pfont, "pdfi_free_font_cidtype2(pfont)"); |
| 581 |
|
| 582 |
pdfi_countdown(pdfcidf->cidtogidmap); |
| 583 |
|
| 584 |
pdfi_countdown(pdfcidf->sfnt); |
| 585 |
pdfi_countdown(pdfcidf->PDF_font); |
| 586 |
pdfi_countdown(pdfcidf->BaseFont); |
| 587 |
pdfi_countdown(pdfcidf->FontDescriptor); |
| 588 |
pdfi_countdown(pdfcidf->W); |
| 589 |
pdfi_countdown(pdfcidf->DW2); |
| 590 |
pdfi_countdown(pdfcidf->W2); |
| 591 |
pdfi_countdown(pdfcidf->registry); |
| 592 |
pdfi_countdown(pdfcidf->ordering); |
| 593 |
pdfi_countdown(pdfcidf->filename); |
| 594 |
pdfi_countdown(pdfcidf->copyright); |
| 595 |
pdfi_countdown(pdfcidf->notice); |
| 596 |
pdfi_countdown(pdfcidf->fullname); |
| 597 |
pdfi_countdown(pdfcidf->familyname); |
| 598 |
|
| 599 |
gs_free_object(OBJ_MEMORY(pdfcidf), pdfcidf, "pdfi_free_font_cidtype2(pdfcidf)"); |
| 600 |
return 0; |
| 601 |
|
| 602 |
return 0; |
| 603 |
} |