| 1 |
/* Copyright (C) 2019-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 type 3 font handling */ |
| 17 |
|
| 18 |
#include "pdf_int.h" |
| 19 |
#include "pdf_stack.h" |
| 20 |
#include "pdf_array.h" |
| 21 |
#include "pdf_dict.h" |
| 22 |
#include "pdf_font_types.h" |
| 23 |
#include "pdf_gstate.h" |
| 24 |
#include "pdf_font.h" |
| 25 |
#include "pdf_font3.h" |
| 26 |
#include "pdf_deref.h" |
| 27 |
#include "gscencs.h" |
| 28 |
#include "gscedata.h" /* For the encoding arrays */ |
| 29 |
#include "gsccode.h" /* For the Encoding indices */ |
| 30 |
#include "gsuid.h" /* For no_UniqueID */ |
| 31 |
#include "gsutil.h" /* For gs_next_ids() */ |
| 32 |
|
| 33 |
static int |
| 34 |
pdfi_type3_build_char(gs_show_enum * penum, gs_gstate * pgs, gs_font * pfont, |
| 35 |
gs_char chr, gs_glyph glyph) |
| 36 |
{ |
| 37 |
int code = 0; |
| 38 |
pdf_font_type3 *font; |
| 39 |
pdf_name *GlyphName = NULL; |
| 40 |
pdf_stream *CharProc = NULL; |
| 41 |
int SavedTextBlockDepth = 0; |
| 42 |
char Notdef[8] = {".notdef"}; |
| 43 |
|
| 44 |
font = (pdf_font_type3 *)pfont->client_data; |
| 45 |
|
| 46 |
SavedTextBlockDepth = OBJ_CTX(font)->text.BlockDepth; |
| 47 |
code = pdfi_array_get(OBJ_CTX(font), font->Encoding, (uint64_t)chr, (pdf_obj **)&GlyphName); |
| 48 |
if (code < 0) |
| 49 |
return code; |
| 50 |
|
| 51 |
code = pdfi_dict_get_by_key(OBJ_CTX(font), font->CharProcs, GlyphName, (pdf_obj **)&CharProc); |
| 52 |
if (code == gs_error_undefined) { |
| 53 |
byte *Key = NULL; |
| 54 |
/* Can't find the named glyph, try to find a /.notdef as a substitute */ |
| 55 |
Key = gs_alloc_bytes(OBJ_MEMORY(font), 8, "working buffer for BuildChar"); |
| 56 |
if (Key == NULL) |
| 57 |
goto build_char_error; |
| 58 |
memset(Key, 0x00, 8); |
| 59 |
memcpy(Key, Notdef, 8); |
| 60 |
code = pdfi_dict_get(OBJ_CTX(font), font->CharProcs, (const char *)Key, (pdf_obj **)&CharProc); |
| 61 |
gs_free_object(OBJ_MEMORY(font), Key, "working buffer for BuildChar"); |
| 62 |
if (code == gs_error_undefined) { |
| 63 |
code = 0; |
| 64 |
goto build_char_error; |
| 65 |
} |
| 66 |
} |
| 67 |
if (code < 0) |
| 68 |
goto build_char_error; |
| 69 |
if (pdfi_type_of(CharProc) != PDF_STREAM) { |
| 70 |
code = gs_note_error(gs_error_typecheck); |
| 71 |
goto build_char_error; |
| 72 |
} |
| 73 |
|
| 74 |
OBJ_CTX(font)->text.BlockDepth = 0; |
| 75 |
OBJ_CTX(font)->text.inside_CharProc = true; |
| 76 |
OBJ_CTX(font)->text.CharProc_d_type = pdf_type3_d_none; |
| 77 |
|
| 78 |
/* We used to have code here to emulate some Acrobat behaviour, but it interferes with getting |
| 79 |
* the result of /tests/pdf/safedocs/ContentStreamNoCycleType3insideType3.pdf because for that |
| 80 |
* file the nested type 3 font CharProc must inherit the stroke colour from the outer type 3 |
| 81 |
* font CharProc graphics state at the time the glyph is drawn. So the code was removed. The |
| 82 |
* Acrobat behaviour is not mentioned in the spec and Acrobat is completely incapable of |
| 83 |
* getting Patterns right, per the spec, when they are 'inside' multiple levels of content |
| 84 |
* streams. |
| 85 |
* The old comment began : |
| 86 |
* |
| 87 |
* * It turns out that if a type 3 font uses a stroke to draw, and does not |
| 88 |
* * acrually set the stroke colour, then we must use the fill colour instead. |
| 89 |
* * In effect we start a type 3 BuildChar with stroke colour = fill colour. |
| 90 |
*/ |
| 91 |
code = pdfi_gsave(OBJ_CTX(font)); |
| 92 |
if (code >= 0) { |
| 93 |
code = pdfi_run_context(OBJ_CTX(font), CharProc, font->PDF_font, true, "CharProc"); |
| 94 |
(void)pdfi_grestore(OBJ_CTX(font)); |
| 95 |
} |
| 96 |
|
| 97 |
OBJ_CTX(font)->text.inside_CharProc = false; |
| 98 |
OBJ_CTX(font)->text.CharProc_d_type = pdf_type3_d_none; |
| 99 |
OBJ_CTX(font)->text.BlockDepth = SavedTextBlockDepth; |
| 100 |
|
| 101 |
build_char_error: |
| 102 |
pdfi_countdown(GlyphName); |
| 103 |
pdfi_countdown(CharProc); |
| 104 |
return code; |
| 105 |
} |
| 106 |
|
| 107 |
static int alloc_type3_font(pdf_context *ctx, pdf_font_type3 **font) |
| 108 |
{ |
| 109 |
pdf_font_type3 *t3font = NULL; |
| 110 |
|
| 111 |
t3font = (pdf_font_type3 *)gs_alloc_bytes(ctx->memory, sizeof(pdf_font_type3), "pdfi_alloc_type3_font"); |
| 112 |
if (t3font == NULL) |
| 113 |
return_error(gs_error_VMerror); |
| 114 |
|
| 115 |
memset(t3font, 0x00, sizeof(pdf_font_type3)); |
| 116 |
(t3font)->ctx = ctx; |
| 117 |
(t3font)->type = PDF_FONT; |
| 118 |
|
| 119 |
#if REFCNT_DEBUG |
| 120 |
(t3font)->UID = ctx->UID++; |
| 121 |
outprintf(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", t3font->type, t3font->UID); |
| 122 |
#endif |
| 123 |
|
| 124 |
|
| 125 |
pdfi_countup(t3font); |
| 126 |
|
| 127 |
t3font->pfont = gs_alloc_struct(ctx->memory, gs_font_base, &st_gs_font_base, |
| 128 |
"pdfi (type 3 font)"); |
| 129 |
if (t3font->pfont == NULL) { |
| 130 |
pdfi_countdown(t3font); |
| 131 |
return_error(gs_error_VMerror); |
| 132 |
} |
| 133 |
|
| 134 |
memset(t3font->pfont, 0x00, sizeof(gs_font_base)); |
| 135 |
t3font->ctx = ctx; |
| 136 |
t3font->pdfi_font_type = e_pdf_font_type3; |
| 137 |
|
| 138 |
gs_make_identity(&t3font->pfont->orig_FontMatrix); |
| 139 |
gs_make_identity(&t3font->pfont->FontMatrix); |
| 140 |
t3font->pfont->next = t3font->pfont->prev = 0; |
| 141 |
t3font->pfont->memory = ctx->memory; |
| 142 |
t3font->pfont->dir = ctx->font_dir; |
| 143 |
t3font->pfont->is_resource = false; |
| 144 |
gs_notify_init(&t3font->pfont->notify_list, ctx->memory); |
| 145 |
t3font->pfont->base = (gs_font *) t3font->pfont; |
| 146 |
t3font->pfont->client_data = t3font; |
| 147 |
t3font->pfont->WMode = 0; |
| 148 |
t3font->pfont->PaintType = 0; |
| 149 |
t3font->pfont->StrokeWidth = 0; |
| 150 |
t3font->pfont->is_cached = 0; |
| 151 |
t3font->pfont->procs.init_fstack = gs_default_init_fstack; |
| 152 |
t3font->pfont->procs.next_char_glyph = gs_default_next_char_glyph; |
| 153 |
t3font->pfont->FAPI = NULL; |
| 154 |
t3font->pfont->FAPI_font_data = NULL; |
| 155 |
t3font->pfont->procs.glyph_name = ctx->get_glyph_name; |
| 156 |
t3font->pfont->procs.decode_glyph = pdfi_decode_glyph; |
| 157 |
t3font->pfont->procs.define_font = gs_no_define_font; |
| 158 |
t3font->pfont->procs.make_font = gs_no_make_font; |
| 159 |
|
| 160 |
t3font->default_font_info = gs_default_font_info; |
| 161 |
t3font->pfont->procs.font_info = pdfi_default_font_info; |
| 162 |
|
| 163 |
t3font->pfont->procs.glyph_info = gs_default_glyph_info; |
| 164 |
t3font->pfont->procs.glyph_outline = gs_no_glyph_outline; |
| 165 |
t3font->pfont->procs.encode_char = pdfi_encode_char; |
| 166 |
t3font->pfont->procs.build_char = pdfi_type3_build_char; |
| 167 |
t3font->pfont->procs.same_font = gs_default_same_font; |
| 168 |
t3font->pfont->procs.enumerate_glyph = gs_no_enumerate_glyph; |
| 169 |
|
| 170 |
t3font->pfont->FontType = ft_PDF_user_defined; |
| 171 |
/* outlines ? Bitmaps ? */ |
| 172 |
t3font->pfont->ExactSize = fbit_use_bitmaps; |
| 173 |
t3font->pfont->InBetweenSize = fbit_use_bitmaps; |
| 174 |
t3font->pfont->TransformedChar = fbit_transform_bitmaps; |
| 175 |
|
| 176 |
t3font->pfont->encoding_index = ENCODING_INDEX_UNKNOWN; |
| 177 |
t3font->pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN; |
| 178 |
|
| 179 |
t3font->pfont->client_data = (void *)t3font; |
| 180 |
t3font->pfont->id = gs_next_ids(ctx->memory, 1); |
| 181 |
uid_set_UniqueID(&t3font->pfont->UID, no_UniqueID); |
| 182 |
|
| 183 |
*font = (pdf_font_type3 *)t3font; |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
int pdfi_free_font_type3(pdf_obj *font) |
| 188 |
{ |
| 189 |
pdf_font_type3 *t3font = (pdf_font_type3 *)font; |
| 190 |
|
| 191 |
if (t3font->pfont) |
| 192 |
gs_free_object(OBJ_MEMORY(t3font), t3font->pfont, "Free type 3 font"); |
| 193 |
|
| 194 |
if (t3font->Widths) |
| 195 |
gs_free_object(OBJ_MEMORY(t3font), t3font->Widths, "Free type 3 font Widths array"); |
| 196 |
|
| 197 |
pdfi_countdown(t3font->PDF_font); |
| 198 |
pdfi_countdown(t3font->FontDescriptor); |
| 199 |
pdfi_countdown(t3font->CharProcs); |
| 200 |
pdfi_countdown(t3font->Encoding); |
| 201 |
pdfi_countdown(t3font->ToUnicode); |
| 202 |
pdfi_countdown(t3font->filename); /* Should never exist, but just in case */ |
| 203 |
pdfi_countdown(t3font->copyright); |
| 204 |
pdfi_countdown(t3font->notice); |
| 205 |
pdfi_countdown(t3font->fullname); |
| 206 |
pdfi_countdown(t3font->familyname); |
| 207 |
|
| 208 |
gs_free_object(OBJ_MEMORY(font), font, "Free type 3 font"); |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
int pdfi_read_type3_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, pdf_font **ppdffont) |
| 214 |
{ |
| 215 |
int code = 0; |
| 216 |
pdf_font_type3 *font = NULL; |
| 217 |
pdf_obj *obj = NULL; |
| 218 |
pdf_obj *tounicode = NULL; |
| 219 |
|
| 220 |
*ppdffont = NULL; |
| 221 |
code = alloc_type3_font(ctx, &font); |
| 222 |
if (code < 0) |
| 223 |
return code; |
| 224 |
|
| 225 |
font->object_num = font_dict->object_num; |
| 226 |
font->generation_num = font_dict->generation_num; |
| 227 |
font->indirect_num = font_dict->indirect_num; |
| 228 |
font->indirect_gen = font_dict->indirect_gen; |
| 229 |
|
| 230 |
code = pdfi_dict_get_type(ctx, font_dict, "FontBBox", PDF_ARRAY, &obj); |
| 231 |
if (code < 0) |
| 232 |
goto font3_error; |
| 233 |
code = pdfi_array_to_gs_rect(ctx, (pdf_array *)obj, &font->pfont->FontBBox); |
| 234 |
if (code < 0) |
| 235 |
goto font3_error; |
| 236 |
pdfi_countdown(obj); |
| 237 |
obj = NULL; |
| 238 |
|
| 239 |
code = pdfi_dict_get_type(ctx, font_dict, "FontMatrix", PDF_ARRAY, &obj); |
| 240 |
if (code < 0) |
| 241 |
goto font3_error; |
| 242 |
code = pdfi_array_to_gs_matrix(ctx, (pdf_array *)obj, &font->pfont->orig_FontMatrix); |
| 243 |
if (code < 0) |
| 244 |
goto font3_error; |
| 245 |
code = pdfi_array_to_gs_matrix(ctx, (pdf_array *)obj, &font->pfont->FontMatrix); |
| 246 |
if (code < 0) |
| 247 |
goto font3_error; |
| 248 |
pdfi_countdown(obj); |
| 249 |
obj = NULL; |
| 250 |
|
| 251 |
code = pdfi_dict_get(ctx, font_dict, "CharProcs", (pdf_obj **)&font->CharProcs); |
| 252 |
if (code < 0) |
| 253 |
goto font3_error; |
| 254 |
|
| 255 |
|
| 256 |
code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor); |
| 257 |
if (code < 0) |
| 258 |
goto font3_error; |
| 259 |
|
| 260 |
if (font->FontDescriptor != NULL) { |
| 261 |
pdf_obj *Name = NULL; |
| 262 |
|
| 263 |
code = pdfi_dict_get_type(ctx, (pdf_dict *) font->FontDescriptor, "FontName", PDF_NAME, (pdf_obj**)&Name); |
| 264 |
if (code < 0) |
| 265 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_FDESC_BAD_FONTNAME, "pdfi_load_font", ""); |
| 266 |
pdfi_countdown(Name); |
| 267 |
} |
| 268 |
|
| 269 |
pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font); |
| 270 |
/* ignore errors with widths... for now */ |
| 271 |
(void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)font, 1.0); |
| 272 |
|
| 273 |
|
| 274 |
code = pdfi_dict_get(ctx, font_dict, "Encoding", &obj); |
| 275 |
if (code < 0) |
| 276 |
goto font3_error; |
| 277 |
|
| 278 |
code = pdfi_create_Encoding(ctx, (pdf_font *)font, obj, NULL, (pdf_obj **)&font->Encoding); |
| 279 |
if (code < 0) |
| 280 |
goto font3_error; |
| 281 |
pdfi_countdown(obj); |
| 282 |
|
| 283 |
font->PDF_font = font_dict; |
| 284 |
pdfi_countup(font_dict); |
| 285 |
|
| 286 |
if (ctx->args.ignoretounicode != true) { |
| 287 |
code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode); |
| 288 |
if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) { |
| 289 |
pdf_cmap *tu = NULL; |
| 290 |
code = pdfi_read_cmap(ctx, tounicode, &tu); |
| 291 |
pdfi_countdown(tounicode); |
| 292 |
tounicode = (pdf_obj *)tu; |
| 293 |
} |
| 294 |
if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) { |
| 295 |
pdfi_countdown(tounicode); |
| 296 |
tounicode = NULL; |
| 297 |
code = 0; |
| 298 |
} |
| 299 |
} |
| 300 |
else { |
| 301 |
tounicode = NULL; |
| 302 |
} |
| 303 |
|
| 304 |
font->ToUnicode = tounicode; |
| 305 |
tounicode = NULL; |
| 306 |
|
| 307 |
code = replace_cache_entry(ctx, (pdf_obj *)font); |
| 308 |
if (code < 0) |
| 309 |
goto font3_error; |
| 310 |
|
| 311 |
pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font); |
| 312 |
code = gs_definefont(ctx->font_dir, (gs_font *)font->pfont); |
| 313 |
if (code < 0) |
| 314 |
goto font3_error; |
| 315 |
|
| 316 |
*ppdffont = (pdf_font *)font; |
| 317 |
|
| 318 |
return code; |
| 319 |
|
| 320 |
font3_error: |
| 321 |
pdfi_countdown(obj); |
| 322 |
pdfi_countdown(font); |
| 323 |
return code; |
| 324 |
} |