| 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 |
#include "pdf_int.h" |
| 17 |
#include "pdf_types.h" |
| 18 |
#include "pdf_font_types.h" |
| 19 |
#include "pdf_dict.h" |
| 20 |
#include "pdf_font.h" |
| 21 |
#include "pdf_fontmt.h" |
| 22 |
|
| 23 |
#ifdef UFST_BRIDGE |
| 24 |
int |
| 25 |
pdfi_alloc_mt_font(pdf_context *ctx, pdf_string *fco_path, int32_t index, pdf_font_microtype **font) |
| 26 |
{ |
| 27 |
int code; |
| 28 |
pdf_font_microtype *pdffont = NULL; |
| 29 |
gs_font_base *pbfont = NULL; |
| 30 |
pdf_name *encname; |
| 31 |
static const byte *StandardEncodingString = (const byte *)"StandardEncoding"; |
| 32 |
static const byte *SymbolEncodingString = (const byte *)"SymbolEncoding"; |
| 33 |
static const byte *DingbatsEncodingString = (const byte *)"DingbatsEncoding"; |
| 34 |
|
| 35 |
pdffont = (pdf_font_microtype *)gs_alloc_bytes(ctx->memory, sizeof(pdf_font_microtype), "pdfi (mt pdf_font)"); |
| 36 |
if (pdffont == NULL) |
| 37 |
return_error(gs_error_VMerror); |
| 38 |
|
| 39 |
memset(pdffont, 0x00, sizeof(pdf_font)); |
| 40 |
pdffont->type = PDF_FONT; |
| 41 |
pdffont->ctx = ctx; |
| 42 |
pdffont->pdfi_font_type = e_pdf_font_microtype; |
| 43 |
pdffont->fco_index = index; |
| 44 |
pdffont->filename = fco_path; |
| 45 |
pdfi_countup(pdffont->filename); |
| 46 |
|
| 47 |
#if REFCNT_DEBUG |
| 48 |
pdffont->UID = ctx->UID++; |
| 49 |
outprintf("Allocated object of type %c with UID %"PRIi64"\n", pdffont->type, pdffont->UID); |
| 50 |
#endif |
| 51 |
|
| 52 |
pdfi_countup(pdffont); |
| 53 |
|
| 54 |
pbfont = (gs_font_base *)gs_alloc_struct(ctx->memory, gs_font_base, &st_gs_font_base, "pdfi (mt pbfont)"); |
| 55 |
if (pbfont == NULL) { |
| 56 |
pdfi_countdown(pdffont); |
| 57 |
return_error(gs_error_VMerror); |
| 58 |
} |
| 59 |
memset(pbfont, 0x00, sizeof(gs_font_base)); |
| 60 |
|
| 61 |
pdffont->pfont = pbfont; |
| 62 |
pbfont->client_data = pdffont; |
| 63 |
gs_make_identity(&pbfont->FontMatrix); |
| 64 |
pbfont->memory = ctx->memory; |
| 65 |
gs_notify_init(&pbfont->notify_list, ctx->memory); |
| 66 |
pbfont->FontType = ft_MicroType; |
| 67 |
pbfont->BitmapWidths = true; |
| 68 |
pbfont->ExactSize = fbit_use_outlines; |
| 69 |
pbfont->InBetweenSize = fbit_use_outlines; |
| 70 |
pbfont->TransformedChar = fbit_use_outlines; |
| 71 |
|
| 72 |
pbfont->FontBBox.p.x = -136.0; |
| 73 |
pbfont->FontBBox.p.y = -283.0; |
| 74 |
pbfont->FontBBox.q.x = 711.0; |
| 75 |
pbfont->FontBBox.q.y = 942.0; |
| 76 |
uid_set_invalid(&pbfont->UID); |
| 77 |
/* The buildchar proc will be filled in by FAPI - |
| 78 |
we won't worry about working without FAPI */ |
| 79 |
pbfont->procs.encode_char = pdfi_encode_char; |
| 80 |
pbfont->procs.glyph_name = pdfi_glyph_name; |
| 81 |
pbfont->procs.decode_glyph = pdfi_decode_glyph; |
| 82 |
pbfont->procs.define_font = gs_no_define_font; |
| 83 |
pbfont->procs.make_font = gs_no_make_font; |
| 84 |
pbfont->procs.init_fstack = gs_default_init_fstack; |
| 85 |
pbfont->procs.font_info = pdfi_default_font_info; |
| 86 |
pbfont->procs.next_char_glyph = gs_default_next_char_glyph; |
| 87 |
|
| 88 |
pbfont->procs.glyph_info = gs_default_glyph_info; |
| 89 |
pbfont->procs.glyph_outline = gs_no_glyph_outline; |
| 90 |
pbfont->procs.build_char = NULL; |
| 91 |
pbfont->procs.same_font = gs_default_same_font; |
| 92 |
pbfont->procs.enumerate_glyph = gs_no_enumerate_glyph; |
| 93 |
|
| 94 |
pbfont->encoding_index = ENCODING_INDEX_UNKNOWN; |
| 95 |
pbfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN; |
| 96 |
|
| 97 |
switch (index) { |
| 98 |
case 27: |
| 99 |
case 76: |
| 100 |
code = pdfi_name_alloc(ctx, (byte *)SymbolEncodingString, strlen((char *)SymbolEncodingString), (pdf_obj **)&encname); |
| 101 |
break; |
| 102 |
case 82: |
| 103 |
code = pdfi_name_alloc(ctx, (byte *)DingbatsEncodingString, strlen((char *)DingbatsEncodingString), (pdf_obj **)&encname); |
| 104 |
break; |
| 105 |
default: |
| 106 |
code = pdfi_name_alloc(ctx, (byte *)StandardEncodingString, strlen((char *)StandardEncodingString), (pdf_obj **)&encname); |
| 107 |
break; |
| 108 |
} |
| 109 |
if (code < 0) { |
| 110 |
pdfi_countdown(pdffont); |
| 111 |
return code; |
| 112 |
} |
| 113 |
pdfi_countup(encname); |
| 114 |
|
| 115 |
code = pdfi_create_Encoding(ctx, (pdf_font *)pdffont, (pdf_obj *)encname, NULL, (pdf_obj **)&pdffont->Encoding); |
| 116 |
pdfi_countdown(encname); |
| 117 |
if (code < 0) { |
| 118 |
pdfi_countdown(pdffont); |
| 119 |
return code; |
| 120 |
} |
| 121 |
*font = pdffont; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
int pdfi_free_font_microtype(pdf_obj *font) |
| 126 |
{ |
| 127 |
pdf_font_microtype *mtfont = (pdf_font_microtype *)font; |
| 128 |
|
| 129 |
if (mtfont->pfont) |
| 130 |
gs_free_object(OBJ_MEMORY(mtfont), mtfont->pfont, "Free mt gs_font"); |
| 131 |
|
| 132 |
if (mtfont->Widths) |
| 133 |
gs_free_object(OBJ_MEMORY(mtfont), mtfont->Widths, "Free mt font Widths array"); |
| 134 |
|
| 135 |
pdfi_countdown(mtfont->FontDescriptor); |
| 136 |
pdfi_countdown(mtfont->Encoding); |
| 137 |
pdfi_countdown(mtfont->BaseFont); |
| 138 |
pdfi_countdown(mtfont->PDF_font); |
| 139 |
pdfi_countdown(mtfont->ToUnicode); |
| 140 |
pdfi_countdown(mtfont->filename); |
| 141 |
|
| 142 |
gs_free_object(OBJ_MEMORY(mtfont), mtfont, "Free mt font"); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
int |
| 148 |
pdfi_copy_microtype_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont) |
| 149 |
{ |
| 150 |
pdf_obj *tmp; |
| 151 |
bool force_symbolic = false; |
| 152 |
char pathnm[gp_file_name_sizeof]; |
| 153 |
gs_font_base *pbfont; |
| 154 |
pdf_font_microtype *dmtfont, *mtfont = (pdf_font_microtype *)spdffont; |
| 155 |
int code = pdfi_alloc_mt_font(ctx, mtfont->filename, mtfont->fco_index, &dmtfont); |
| 156 |
if (code < 0) |
| 157 |
return code; |
| 158 |
pbfont = dmtfont->pfont; |
| 159 |
pdfi_countdown(dmtfont->Encoding); |
| 160 |
memcpy(dmtfont, mtfont, sizeof(pdf_font_microtype)); |
| 161 |
pdfi_countup(dmtfont->Encoding); |
| 162 |
dmtfont->refcnt = 1; |
| 163 |
dmtfont->pfont = pbfont; |
| 164 |
memcpy(dmtfont->pfont, mtfont->pfont, sizeof(gs_font_base)); |
| 165 |
dmtfont->pfont->client_data = dmtfont; |
| 166 |
pbfont->FAPI = NULL; |
| 167 |
pbfont->FAPI_font_data = NULL; |
| 168 |
memcpy(pathnm, dmtfont->filename->data, dmtfont->filename->length); |
| 169 |
pathnm[dmtfont->filename->length] = '\0'; |
| 170 |
|
| 171 |
dmtfont->PDF_font = font_dict; |
| 172 |
dmtfont->object_num = font_dict->object_num; |
| 173 |
dmtfont->generation_num = font_dict->generation_num; |
| 174 |
pdfi_countup(dmtfont->PDF_font); |
| 175 |
/* We want basefont and descriptor, but we can live without them */ |
| 176 |
dmtfont->BaseFont = NULL; |
| 177 |
(void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &dmtfont->BaseFont); |
| 178 |
dmtfont->FontDescriptor = NULL; |
| 179 |
(void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&dmtfont->FontDescriptor); |
| 180 |
pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)dmtfont); |
| 181 |
(void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)dmtfont, (double)0.001); |
| 182 |
|
| 183 |
if (pdfi_font_known_symbolic(dmtfont->BaseFont)) { |
| 184 |
force_symbolic = true; |
| 185 |
dmtfont->descflags |= 4; |
| 186 |
} |
| 187 |
|
| 188 |
tmp = NULL; |
| 189 |
code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp); |
| 190 |
if (code == 1) { |
| 191 |
pdfi_countdown(dmtfont->Encoding); |
| 192 |
dmtfont->Encoding = NULL; |
| 193 |
if (pdfi_type_of(tmp) == PDF_NAME && force_symbolic == true) { |
| 194 |
dmtfont->Encoding = spdffont->Encoding; |
| 195 |
pdfi_countup(dmtfont->Encoding); |
| 196 |
} |
| 197 |
else if (pdfi_type_of(tmp) == PDF_DICT && (dmtfont->descflags & 4) != 0) { |
| 198 |
code = pdfi_create_Encoding(ctx, (pdf_font *)dmtfont, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &dmtfont->Encoding); |
| 199 |
if (code >= 0) |
| 200 |
code = 1; |
| 201 |
} |
| 202 |
else { |
| 203 |
code = pdfi_create_Encoding(ctx, (pdf_font *)dmtfont, tmp, NULL, (pdf_obj **) & dmtfont->Encoding); |
| 204 |
if (code >= 0) |
| 205 |
code = 1; |
| 206 |
} |
| 207 |
pdfi_countdown(tmp); |
| 208 |
tmp = NULL; |
| 209 |
} |
| 210 |
else { |
| 211 |
pdfi_countdown(tmp); |
| 212 |
tmp = NULL; |
| 213 |
code = 0; |
| 214 |
} |
| 215 |
|
| 216 |
gs_notify_init(&dmtfont->pfont->notify_list, ctx->memory); |
| 217 |
code = gs_definefont(ctx->font_dir, (gs_font *) dmtfont->pfont); |
| 218 |
if (code < 0) { |
| 219 |
pdfi_countdown(dmtfont); |
| 220 |
return code; |
| 221 |
} |
| 222 |
code = pdfi_fapi_passfont((pdf_font *)dmtfont, dmtfont->fco_index, (char *)"UFST", pathnm, NULL, 0); |
| 223 |
if (code < 0) { |
| 224 |
pdfi_countdown(dmtfont); |
| 225 |
dmtfont = NULL; |
| 226 |
} |
| 227 |
*tpdffont = (pdf_font *)dmtfont; |
| 228 |
return code; |
| 229 |
} |
| 230 |
|
| 231 |
#endif /* UFST_BRIDGE */ |