Debian Codegraph @

pdf/pdf_fapi.c

raw
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 /* Interface to FAPI for the PDF interpreter */
17
18 /* Include this first so that we don't get a macro redefnition of 'offsetof' */
19 #include "pdf_int.h"
20
21 #include "memory_.h"
22 #include "gsmemory.h"
23 #include "gserrors.h"
24 #include "gxdevice.h"
25 #include "gxfont.h"
26 #include "gxfont0.h"
27 #include "gxfcid.h"
28
29 #include "gzstate.h"
30 #include "gxchar.h" /* for st_gs_show_enum */
31 #include "gdebug.h"
32 #include "gxfapi.h"
33 #include "gscoord.h"
34 #include "gspath.h"
35 #include "pdf_dict.h"
36 #include "pdf_array.h"
37 #include "pdf_font.h"
38 #include "pdf_fontTT.h"
39 #include "gscencs.h"
40 #include "gsagl.h"
41 #include "gxfont1.h" /* for gs_font_type1_s */
42 #include "gscrypt1.h" /* for crypt_c1 */
43
44 #ifdef UFST_BRIDGE
45 static const char *UFSTFONTDIR = ""; /* A bogus linux location */
46 #ifndef __WIN32__
47 static const char *UFSTFCOS = "%rom%fontdata/mtfonts/pclps2/mt3/pclp2_xj.fco:%rom%fontdata/mtfonts/pcl45/mt3/wd____xh.fco";
48 #else
49 static const char *UFSTFCOS = "%rom%fontdata/mtfonts/pclps2/mt3/pclp2_xj.fco;%rom%fontdata/mtfonts/pcl45/mt3/wd____xh.fco";
50 #endif
51 static const char *UFSTPLUGINS = "%rom%fontdata/mtfonts/pcl45/mt3/plug__xi.fco";
52
53 static const char *UFSTDIRPARM = "UFST_SSdir=";
54
55 static const char *UFSTPLUGINPARM = "UFST_PlugIn=";
56
57 extern const char gp_file_name_list_separator;
58 #endif /* UFST_BRIDGE */
59
60 extern single_glyph_list_t SingleGlyphList[];
61
62
63 static int pdfi_get_server_param(gs_fapi_server * I, const char *subtype, char **server_param, int *server_param_size);
64
65
66 /* forward declarations for the pdfi_ff_stub definition */
67 static int
68 pdfi_fapi_get_word(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, unsigned short *ret);
69
70 static int
71 pdfi_fapi_get_long(gs_fapi_font * ff, gs_fapi_font_feature var_id, int index, unsigned long *ret);
72
73 static int
74 pdfi_fapi_get_float(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, float *ret);
75
76 static int
77 pdfi_fapi_get_name(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, char *buffer, int len);
78
79 static int
80 pdfi_fapi_get_proc(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, char *buffer);
81
82 static int
83 pdfi_fapi_get_gsubr(gs_fapi_font *ff, int index, byte *buf, int buf_length);
84
85 static int
86 pdfi_fapi_get_subr(gs_fapi_font *ff, int index, byte *buf, int buf_length);
87
88 static int
89 pdfi_fapi_get_raw_subr(gs_fapi_font *ff, int index, byte *buf, int buf_length);
90
91 static int
92 pdfi_fapi_serialize_tt_font(gs_fapi_font * ff, void *buf, int buf_size);
93
94 static int
95 pdfi_fapi_retrieve_tt_font(gs_fapi_font * ff, void **buf, int *buf_size);
96
97 static int
98 pdfi_fapi_get_charstring(gs_fapi_font *ff, int index, byte *buf, ushort buf_length);
99
100 static int
101 pdfi_fapi_get_charstring_name(gs_fapi_font *ff, int index, byte *buf, ushort buf_length);
102
103
104 static int
105 pdfi_fapi_get_glyphname_or_cid(gs_text_enum_t *penum, gs_font_base * pbfont, gs_string * charstring,
106 gs_string * name, gs_glyph ccode, gs_string * enc_char_name,
107 char *font_file_path, gs_fapi_char_ref * cr, bool bCID);
108
109 static int
110 pdfi_fapi_get_glyph(gs_fapi_font * ff, gs_glyph char_code, byte * buf, int buf_length);
111
112
113 static int
114 pdfi_get_glyphdirectory_data(gs_fapi_font * ff, int char_code,
115 const byte ** ptr);
116
117 static int
118 pdfi_fapi_set_cache(gs_text_enum_t * penum, const gs_font_base * pbfont,
119 const gs_string * char_name, gs_glyph cid,
120 const double pwidth[2], const gs_rect * pbbox,
121 const double Metrics2_sbw_default[4], bool * imagenow);
122
123 static int
124 pdfi_fapi_get_metrics(gs_fapi_font * ff, gs_string * char_name, gs_glyph cid, double *m, bool vertical);
125
126 static const gs_fapi_font pdfi_ff_stub = {
127 0, /* server_font_data */
128 0, /* need_decrypt */
129 NULL, /* const gs_memory_t */
130 0, /* font_file_path */
131 0, /* full_font_buf */
132 0, /* full_font_buf_len */
133 0, /* subfont */
134 false, /* is_type1 */
135 false, /* is_cid */
136 false, /* is_outline_font */
137 false, /* is_mtx_skipped */
138 false, /* is_vertical */
139 false, /* metrics_only */
140 {{3, 1}, {1, 0}, {3, 0}, {3, 10}, {-1, -1}}, /* ttf_cmap_req */
141 {-1, -1}, /* ttf_cmap_selected */
142 0, /* client_ctx_p */
143 0, /* client_font_data */
144 0, /* client_font_data2 */
145 0, /* char_data */
146 0, /* char_data_len */
147 0, /* embolden */
148 pdfi_fapi_get_word, /* get_word */
149 pdfi_fapi_get_long, /* get_long */
150 pdfi_fapi_get_float, /* get_float */
151 pdfi_fapi_get_name, /* get_name */
152 pdfi_fapi_get_proc, /* get_proc */
153 pdfi_fapi_get_gsubr, /* get_gsubr */
154 pdfi_fapi_get_subr, /* get_subr */
155 pdfi_fapi_get_raw_subr, /* get_raw_subr */
156 pdfi_fapi_get_glyph, /* get_glyph */
157 pdfi_fapi_serialize_tt_font, /* serialize_tt_font */
158 pdfi_fapi_retrieve_tt_font, /* retrieve_tt_font */
159 pdfi_fapi_get_charstring, /* get_charstring */
160 pdfi_fapi_get_charstring_name, /* get_charstring_name */
161 pdfi_get_glyphdirectory_data, /* get_GlyphDirectory_data_ptr */
162 pdfi_fapi_get_glyphname_or_cid, /* get_glyphname_or_cid */
163 pdfi_fapi_get_metrics, /* fapi_get_metrics */
164 pdfi_fapi_set_cache /* fapi_set_cache */
165 };
166
167 static inline ushort
168 float_to_ushort(float v)
169 {
170 return ((ushort) (v * 16)); /* fixme : the scale may depend on renderer */
171 }
172
173 static int
174 pdfi_fapi_get_word(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, unsigned short *ret)
175 {
176 int code = 0;
177 gs_font_type1 *pfont = (gs_font_type1 *) ff->client_font_data;
178
179 switch ((int)var_id) {
180 case gs_fapi_font_feature_Weight:
181 *ret = 0; /* wrong */
182 break;
183 case gs_fapi_font_feature_ItalicAngle:
184 *ret = 0; /* wrong */
185 break;
186 case gs_fapi_font_feature_IsFixedPitch:
187 *ret = 0; /* wrong */
188 break;
189 case gs_fapi_font_feature_UnderLinePosition:
190 *ret = 0; /* wrong */
191 break;
192 case gs_fapi_font_feature_UnderlineThickness:
193 *ret = 0; /* wrong */
194 break;
195 case gs_fapi_font_feature_FontType:
196 *ret = (pfont->FontType == 2 ? 2 : 1);
197 break;
198 case gs_fapi_font_feature_FontBBox:
199 switch (index) {
200 case 0:
201 *ret = ((ushort) pfont->FontBBox.p.x);
202 break;
203 case 1:
204 *ret = ((ushort) pfont->FontBBox.p.y);
205 break;
206 case 2:
207 *ret = ((ushort) pfont->FontBBox.q.x);
208 break;
209 case 3:
210 *ret = ((ushort) pfont->FontBBox.q.y);
211 break;
212 default:
213 code = gs_note_error(gs_error_rangecheck);
214 }
215 break;
216 case gs_fapi_font_feature_BlueValues_count:
217 *ret = pfont->data.BlueValues.count;
218 break;
219 case gs_fapi_font_feature_BlueValues:
220 *ret = (float_to_ushort(pfont->data.BlueValues.values[index]));
221 break;
222 case gs_fapi_font_feature_OtherBlues_count:
223 *ret = pfont->data.OtherBlues.count;
224 break;
225 case gs_fapi_font_feature_OtherBlues:
226 *ret = (float_to_ushort(pfont->data.OtherBlues.values[index]));
227 break;
228 case gs_fapi_font_feature_FamilyBlues_count:
229 *ret = pfont->data.FamilyBlues.count;
230 break;
231 case gs_fapi_font_feature_FamilyBlues:
232 *ret = (float_to_ushort(pfont->data.FamilyBlues.values[index]));
233 break;
234 case gs_fapi_font_feature_FamilyOtherBlues_count:
235 *ret = pfont->data.FamilyOtherBlues.count;
236 break;
237 case gs_fapi_font_feature_FamilyOtherBlues:
238 *ret = (float_to_ushort(pfont->data.FamilyOtherBlues.values[index]));
239 break;
240 case gs_fapi_font_feature_BlueShift:
241 *ret = float_to_ushort(pfont->data.BlueShift);
242 break;
243 case gs_fapi_font_feature_BlueFuzz:
244 *ret = float_to_ushort(pfont->data.BlueShift);
245 break;
246 case gs_fapi_font_feature_StdHW:
247 *ret = (pfont->data.StdHW.count == 0 ? 0 : float_to_ushort(pfont->data.StdHW.values[0])); /* UFST bug ? */
248 break;
249 case gs_fapi_font_feature_StdVW:
250 *ret = (pfont->data.StdVW.count == 0 ? 0 : float_to_ushort(pfont->data.StdVW.values[0])); /* UFST bug ? */
251 break;
252 case gs_fapi_font_feature_StemSnapH_count:
253 *ret = pfont->data.StemSnapH.count;
254 break;
255 case gs_fapi_font_feature_StemSnapH:
256 *ret = float_to_ushort(pfont->data.StemSnapH.values[index]);
257 break;
258 case gs_fapi_font_feature_StemSnapV_count:
259 *ret = pfont->data.StemSnapV.count;
260 break;
261 case gs_fapi_font_feature_StemSnapV:
262 *ret = float_to_ushort(pfont->data.StemSnapV.values[index]);
263 break;
264 case gs_fapi_font_feature_ForceBold:
265 *ret = pfont->data.ForceBold;
266 break;
267 case gs_fapi_font_feature_LanguageGroup:
268 *ret = pfont->data.LanguageGroup;
269 break;
270 case gs_fapi_font_feature_lenIV:
271 *ret = ff->need_decrypt ? 0 : pfont->data.lenIV;
272 break;
273 case gs_fapi_font_feature_GlobalSubrs_count:
274 {
275 if (pfont->FontType == ft_encrypted2) {
276 pdf_font_cff *pdffont2 = (pdf_font_cff *)pfont->client_data;
277 *ret = pdffont2->GlobalSubrs == NULL ? 0 : pdfi_array_size(pdffont2->GlobalSubrs);
278 }
279 else {
280 *ret = 0;
281 code = gs_note_error(gs_error_invalidaccess);
282 }
283 break;
284 }
285 case gs_fapi_font_feature_Subrs_count:
286 {
287 if (pfont->FontType == ft_encrypted) {
288 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
289 *ret = pdffont1->Subrs == NULL ? 0 : pdfi_array_size(pdffont1->Subrs);
290 }
291 else if (pfont->FontType == ft_encrypted2) {
292 pdf_font_cff *pdffont2 = (pdf_font_cff *)pfont->client_data;
293 *ret = pdffont2->Subrs == NULL ? 0 : pdfi_array_size(pdffont2->Subrs);
294 }
295 else {
296 *ret = 0;
297 code = gs_note_error(gs_error_invalidaccess);
298 }
299 break;
300 }
301 case gs_fapi_font_feature_CharStrings_count:
302 {
303 if (pfont->FontType == ft_encrypted) {
304 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
305 *ret = pdffont1->CharStrings->entries;
306 }
307 break;
308 }
309 case gs_fapi_font_feature_BlendBlueValues_length:
310 case gs_fapi_font_feature_BlendOtherBlues_length:
311 case gs_fapi_font_feature_BlendOtherBlues_count:
312 case gs_fapi_font_feature_BlendBlueScale_count:
313 case gs_fapi_font_feature_BlendBlueShift_count:
314 case gs_fapi_font_feature_BlendBlueShift:
315 case gs_fapi_font_feature_BlendBlueFuzz_count:
316 case gs_fapi_font_feature_BlendBlueFuzz:
317 case gs_fapi_font_feature_BlendForceBold_count:
318 case gs_fapi_font_feature_BlendForceBold:
319 case gs_fapi_font_feature_BlendStdHW_length:
320 case gs_fapi_font_feature_BlendStdHW_count:
321 case gs_fapi_font_feature_BlendStdHW:
322 case gs_fapi_font_feature_BlendStdVW_length:
323 case gs_fapi_font_feature_BlendStdVW_count:
324 case gs_fapi_font_feature_BlendStdVW:
325 case gs_fapi_font_feature_BlendStemSnapH_length:
326 case gs_fapi_font_feature_BlendStemSnapH_count:
327 case gs_fapi_font_feature_BlendStemSnapH:
328 case gs_fapi_font_feature_BlendStemSnapV_length:
329 case gs_fapi_font_feature_BlendStemSnapV_count:
330 case gs_fapi_font_feature_BlendStemSnapV:
331 {
332 code = 0;
333 *ret = 0;
334 }
335 break;
336 case gs_fapi_font_feature_DollarBlend:
337 {
338 if (pfont->data.WeightVector.count > 0) { /* If count > 0, it's MM font, and we "have" a $Blend */
339 *ret = 1;
340 }
341 else {
342 *ret = 0;
343 }
344 }
345 break;
346 case gs_fapi_font_feature_DollarBlend_length:
347 {
348 /* Use the built-in boiler plate */
349 *ret = 0;
350 }
351 break;
352 case gs_fapi_font_feature_BlendAxisTypes_count:
353 {
354 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
355 if (pdffont1->blendaxistypes != NULL) {
356 *ret = (unsigned short)pdffont1->blendaxistypes->size;
357 }
358 else {
359 *ret = 0;
360 }
361 }
362 break;
363 case gs_fapi_font_feature_WeightVector_count:
364 {
365 *ret = (unsigned short)pfont->data.WeightVector.count;
366 }
367 break;
368 case gs_fapi_font_feature_BlendDesignPositionsArrays_count:
369 {
370 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
371 if (pdffont1->blenddesignpositions != NULL) {
372 *ret = (unsigned short)pdffont1->blenddesignpositions->size;
373 }
374 else {
375 *ret = 0;
376 }
377 }
378 break;
379 case gs_fapi_font_feature_BlendDesignMapArrays_count:
380 {
381 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
382 if (pdffont1->blenddesignmap != NULL) {
383 *ret = (unsigned short)pdffont1->blenddesignmap->size;
384 }
385 else {
386 *ret = 0;
387 }
388 }
389 break;
390 case gs_fapi_font_feature_BlendDesignMapSubArrays_count:
391 {
392 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
393 if (pdffont1->blenddesignmap != NULL) {
394 pdf_array *suba;
395 code = pdfi_array_get(pdffont1->ctx, pdffont1->blenddesignmap, index, (pdf_obj **)&suba);
396 if (code < 0) {
397 *ret = 0;
398 break;
399 }
400 *ret = (unsigned short)suba->size;
401 pdfi_countdown(suba);
402 }
403 else {
404 *ret = 0;
405 }
406 }
407 break;
408 case gs_fapi_font_feature_BlendFontBBox_length:
409 {
410 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
411 if (pdffont1->blendfontbbox != NULL) {
412 *ret = (unsigned short)pdffont1->blendfontbbox->size;
413 }
414 else {
415 *ret = 0;
416 }
417 }
418 break;
419 case gs_fapi_font_feature_BlendFontBBox:
420 {
421 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
422 if (pdffont1->blendfontbbox != NULL) {
423 pdf_array *suba;
424 double d;
425 int ind, aind;
426 ind = index % 4;
427 aind = (index - ind) / 4;
428 code = pdfi_array_get(pdffont1->ctx, pdffont1->blendfontbbox, aind, (pdf_obj **)&suba);
429 if (code < 0) {
430 *ret = 0;
431 break;
432 }
433 code = pdfi_array_get_number(pdffont1->ctx, suba, ind, &d);
434 pdfi_countdown(suba);
435 if (code < 0) {
436 *ret = 0;
437 break;
438 }
439 *ret = (unsigned short)d;
440 }
441 else {
442 *ret = 0;
443 }
444 }
445 break;
446 default:
447 code = gs_error_undefined;
448 *ret = -1;
449 }
450 return code;
451 }
452
453 static int
454 pdfi_fapi_get_long(gs_fapi_font * ff, gs_fapi_font_feature var_id, int index, unsigned long *ret)
455 {
456 gs_font_type1 *pfont = (gs_font_type1 *) ff->client_font_data;
457 int code = 0;
458
459 switch ((int)var_id) {
460 case gs_fapi_font_feature_UniqueID:
461 *ret = pfont->UID.id;
462 break;
463 case gs_fapi_font_feature_BlueScale:
464 *ret = (ulong) (pfont->data.BlueScale * 65536);
465 break;
466 case gs_fapi_font_feature_Subrs_total_size:
467 {
468 if (pfont->FontType == ft_encrypted) {
469 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
470 int i;
471 *ret = 0;
472 for (i = 0; i < (pdffont1->Subrs == NULL ? 0 : pdfi_array_size(pdffont1->Subrs)); i++) {
473 pdf_string *subr_str = NULL;
474 code = pdfi_array_get_type(pdffont1->ctx, pdffont1->Subrs, i, PDF_STRING, (pdf_obj **)&subr_str);
475 if (code >= 0) {
476 *ret += subr_str->length;
477 }
478 pdfi_countdown(subr_str);
479 }
480 }
481 }
482 break;
483 default:
484 code = gs_error_undefined;
485 break;
486 }
487 return code;
488 }
489
490 static int
491 pdfi_fapi_get_float(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, float *ret)
492 {
493 gs_font_base *pbfont = (gs_font_base *) ff->client_font_data2;
494 int code = 0;
495 gs_fapi_server *I = pbfont->FAPI;
496
497 switch ((int)var_id) {
498 case gs_fapi_font_feature_FontMatrix:
499 {
500 double FontMatrix_div;
501 gs_matrix m, *mptr;
502
503 if (I && I->get_fontmatrix) {
504 FontMatrix_div = 1;
505 mptr = &m;
506 I->get_fontmatrix(I, mptr);
507 }
508 else {
509 FontMatrix_div = ((ff->is_cid && (!FAPI_ISCIDFONT(pbfont))) ? 1000 : 1);
510 mptr = &(pbfont->base->FontMatrix);
511 }
512 switch (index) {
513 case 0:
514 default:
515 *ret = (mptr->xx / FontMatrix_div);
516 break;
517 case 1:
518 *ret = (mptr->xy / FontMatrix_div);
519 break;
520 case 2:
521 *ret = (mptr->yx / FontMatrix_div);
522 break;
523 case 3:
524 *ret = (mptr->yy / FontMatrix_div);
525 break;
526 case 4:
527 *ret = (mptr->tx / FontMatrix_div);
528 break;
529 case 5:
530 *ret = (mptr->ty / FontMatrix_div);
531 break;
532 }
533 break;
534 }
535 case gs_fapi_font_feature_WeightVector:
536 {
537 gs_font_type1 *pfont1 = (gs_font_type1 *) pbfont;
538 if (index < pfont1->data.WeightVector.count) {
539 *ret = pfont1->data.WeightVector.values[index];
540 }
541 else {
542 *ret = 0;
543 }
544 }
545 break;
546 case gs_fapi_font_feature_BlendDesignPositionsArrayValue:
547 {
548 int array_index, subind;
549 pdf_array *suba;
550 double d;
551 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pbfont->client_data;
552 *ret = 0;
553
554 code = pdfi_array_get(pdffont1->ctx, pdffont1->blenddesignpositions, 0, (pdf_obj **)&suba);
555 if (code < 0)
556 break;
557
558 /* The FAPI code assumes gs style storage - i.e. it allocates the maximum number of entries
559 permissable by the spec, and fills in only those required.
560 pdfi doesn't, so we unpick that here
561 */
562 index = (index % 8) + (suba->size * index / 8);
563
564 array_index = index / suba->size;
565 subind = index % suba->size;
566 pdfi_countdown(suba);
567 code = pdfi_array_get(pdffont1->ctx, pdffont1->blenddesignpositions, array_index, (pdf_obj**)&suba);
568 if (code < 0) {
569 code = 0;
570 break;
571 }
572
573 code = pdfi_array_get_number(pdffont1->ctx, suba, subind, &d);
574 pdfi_countdown(suba);
575 if (code < 0) {
576 code = 0;
577 break;
578 }
579 *ret = (float)d;
580 }
581 break;
582 case gs_fapi_font_feature_BlendDesignMapArrayValue:
583 {
584 int i, j, k;
585 pdf_array *suba, *subsuba;
586 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pbfont->client_data;
587 *ret = (float)0;
588 code = 0;
589
590 for (i = 0; i < pdffont1->blenddesignmap->size && code >= 0; i++) {
591 code = pdfi_array_get(pdffont1->ctx, pdffont1->blenddesignmap, i, (pdf_obj **)&suba);
592 if (code < 0)
593 continue;
594 for (j = 0; j < suba->size && code >= 0; j++) {
595 code = pdfi_array_get(pdffont1->ctx, suba, i, (pdf_obj **)&subsuba);
596 if (code < 0)
597 continue;
598 for (k = 0; k < subsuba->size && code >= 0; k++) {
599 /* The FAPI code assumes gs style storage - i.e. it allocates the maximum number of entries
600 permissable by the spec, and fills in only those required.
601 pdfi doesn't, hence the multiplications by 64.
602 */
603 if ((i * 64) + (j * 64) + k == index) {
604 double d;
605 code = pdfi_array_get_number(pdffont1->ctx, suba, i, &d);
606 if (code < 0)
607 continue;
608 *ret = (float)d;
609 pdfi_countdown(subsuba);
610 pdfi_countdown(suba);
611 goto gotit;
612 }
613 }
614 pdfi_countdown(subsuba);
615 }
616 pdfi_countdown(suba);
617 }
618 code = 0;
619 }
620 gotit:
621 break;
622
623 default:
624 code = gs_error_undefined;
625 }
626
627 return code;
628 }
629
630 /* Only required for multiple masters, I believe. Buffer is guaranteed to exist */
631 static int
632 pdfi_fapi_get_name(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, char *buffer, int len)
633 {
634 gs_font_base *pbfont = (gs_font_base *) ff->client_font_data2;
635 int code = 0;
636
637 switch ((int)var_id) {
638 case gs_fapi_font_feature_BlendAxisTypes:
639 {
640 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pbfont->client_data;
641 pdf_name *n;
642 code = pdfi_array_get(pdffont1->ctx, pdffont1->blendaxistypes, index, (pdf_obj **)&n);
643 if (code < 0)
644 break;
645 if (n->length <= len - 1) {
646 memcpy(buffer, n->data, n->length);
647 buffer[n->length] = '\0';
648 }
649 else
650 code = gs_error_limitcheck;
651 pdfi_countdown(n);
652 }
653 break;
654 default:
655 code = gs_error_undefined;
656 }
657 return code;
658 }
659
660 /* Only required for multiple masters, I believe */
661 static int
662 pdfi_fapi_get_proc(gs_fapi_font *ff, gs_fapi_font_feature var_id, int index, char *buffer)
663 {
664 int code = 0;
665
666 (void)index;
667 (void)buffer;
668
669 switch ((int)var_id) {
670 case gs_fapi_font_feature_DollarBlend:
671 break;
672 default:
673 code = gs_error_undefined;
674 }
675 return code;
676 }
677
678 static inline void
679 decode_bytes(byte *p, const byte *s, int l, int lenIV)
680 {
681 ushort state = 4330;
682
683 for (; l; s++, l--) {
684 uchar c = (*s ^ (state >> 8));
685
686 state = (*s + state) * crypt_c1 + crypt_c2;
687 if (lenIV > 0)
688 lenIV--;
689 else {
690 *p = c;
691 p++;
692 }
693 }
694 }
695
696 static int
697 pdfi_fapi_get_gsubr(gs_fapi_font *ff, int index, byte *buf, int buf_length)
698 {
699 gs_font_type1 *pfont = (gs_font_type1 *) ff->client_font_data;
700 int code = 0;
701 if (pfont->FontType == ft_encrypted2) {
702 pdf_font_cff *pdffont2 = (pdf_font_cff *)pfont->client_data;
703 if (index > (pdffont2->GlobalSubrs == NULL ? 0 : pdfi_array_size(pdffont2->GlobalSubrs))) {
704 code = gs_error_rangecheck;
705 }
706 else {
707 int leniv = (pfont->data.lenIV > 0 ? pfont->data.lenIV : 0);
708 pdf_string *subrstring = NULL;
709
710 code = pdfi_array_get(pdffont2->ctx, pdffont2->GlobalSubrs, index, (pdf_obj **)&subrstring);
711 if (code >= 0) {
712 if (pdfi_type_of(subrstring) == PDF_STRING) {
713 code = subrstring->length - leniv;
714 if (buf && buf_length >= code) {
715 if (ff->need_decrypt && pfont->data.lenIV >= 0) {
716 decode_bytes(buf, subrstring->data, code + leniv, pfont->data.lenIV);
717 }
718 else {
719 memcpy(buf, subrstring->data, code);
720 }
721 }
722 }
723 else {
724 code = gs_note_error(gs_error_invalidfont);
725 }
726 pdfi_countdown(subrstring);
727 }
728 }
729 }
730 else {
731 code = gs_note_error(gs_error_invalidfont);
732 }
733 return code;
734 }
735
736 static int
737 pdfi_fapi_get_subr(gs_fapi_font *ff, int index, byte *buf, int buf_length)
738 {
739 gs_font_type1 *pfont = (gs_font_type1 *) ff->client_font_data;
740 int code = 0;
741
742 if (pfont->FontType == ft_encrypted) {
743 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
744 if (index > (pdffont1->Subrs == NULL ? 0 : pdfi_array_size(pdffont1->Subrs))) {
745 code = gs_note_error(gs_error_rangecheck);
746 }
747 else {
748 int leniv = (pfont->data.lenIV > 0 ? pfont->data.lenIV : 0);
749 pdf_string *subr_str = NULL;
750
751 code = pdfi_array_get_type(pdffont1->ctx, pdffont1->Subrs, index, PDF_STRING, (pdf_obj **)&subr_str);
752 if (code >= 0) {
753 code = subr_str->length - leniv;
754 if (buf && buf_length >= code) {
755 if (ff->need_decrypt && pfont->data.lenIV >= 0) {
756 decode_bytes(buf, subr_str->data, code + leniv, pfont->data.lenIV);
757 }
758 else {
759 memcpy(buf, subr_str->data, code);
760 }
761 }
762 }
763 else {
764 /* Ignore invalid or missing subrs */
765 code = 0;
766 }
767 pdfi_countdown(subr_str);
768 }
769 }
770 else if (pfont->FontType == ft_encrypted2) {
771 pdf_font_cff *pdffont2 = (pdf_font_cff *)pfont->client_data;
772 if (index > (pdffont2->Subrs == NULL ? 0 : pdfi_array_size(pdffont2->Subrs))) {
773 code = gs_error_rangecheck;
774 }
775 else {
776 int leniv = (pfont->data.lenIV > 0 ? pfont->data.lenIV : 0);
777 pdf_string *subrstring;
778
779 if (pdffont2->Subrs == NULL)
780 code = gs_note_error(gs_error_invalidfont);
781 else
782 code = pdfi_array_get(pdffont2->ctx, pdffont2->Subrs, index, (pdf_obj **)&subrstring);
783 if (code >= 0) {
784 if (pdfi_type_of(subrstring) == PDF_STRING) {
785 if (subrstring->length > 0) {
786 code = subrstring->length - leniv;
787 if (buf && buf_length >= code) {
788 if (ff->need_decrypt && pfont->data.lenIV >= 0) {
789 decode_bytes(buf, subrstring->data, code + leniv, pfont->data.lenIV);
790 }
791 else {
792 memcpy(buf, subrstring->data, code);
793 }
794 }
795 }
796 }
797 else {
798 code = gs_note_error(gs_error_invalidfont);
799 }
800 pdfi_countdown(subrstring);
801 }
802 }
803 }
804 else {
805 code = gs_note_error(gs_error_invalidfont);
806 }
807 return code;
808 }
809
810 static int
811 pdfi_fapi_get_raw_subr(gs_fapi_font *ff, int index, byte *buf, int buf_length)
812 {
813 gs_font_type1 *pfont = (gs_font_type1 *) ff->client_font_data;
814 int code = 0;
815
816 if (pfont->FontType == ft_encrypted) {
817 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pfont->client_data;
818 if (index > (pdffont1->Subrs == NULL ? 0 : pdfi_array_size(pdffont1->Subrs))) {
819 code = gs_error_rangecheck;
820 }
821 else {
822 pdf_string *subr_str = NULL;
823 code = pdfi_array_get_type(pdffont1->ctx, pdffont1->Subrs, index, PDF_STRING, (pdf_obj **)&subr_str);
824 if (code >= 0) {
825 code = subr_str->length;
826 if (buf && buf_length >= code) {
827 memcpy(buf, subr_str->data, code);
828 }
829 }
830 else {
831 /* Ignore missing or invalid subrs */
832 code = 0;
833 }
834 pdfi_countdown(subr_str);
835 }
836 }
837 return code;
838 }
839
840 static int
841 pdfi_fapi_get_charstring(gs_fapi_font *ff, int index, byte *buf, ushort buf_length)
842 {
843 return 0;
844 }
845
846 static int
847 pdfi_fapi_get_charstring_name(gs_fapi_font *ff, int index, byte *buf, ushort buf_length)
848 {
849 return 0;
850 }
851
852 static int
853 pdfi_fapi_get_glyphname_or_cid(gs_text_enum_t *penum, gs_font_base * pbfont, gs_string * charstring,
854 gs_string * name, gs_glyph ccode, gs_string * enc_char_name,
855 char *font_file_path, gs_fapi_char_ref * cr, bool bCID)
856 {
857 gs_fapi_server *I = pbfont->FAPI;
858 int code = 0;
859 pdf_context *ctx = (pdf_context *) ((pdf_font *)pbfont->client_data)->ctx;
860
861 if (pbfont->FontType == ft_CID_TrueType) {
862 pdf_cidfont_type2 *pttfont = (pdf_cidfont_type2 *)pbfont->client_data;
863 gs_glyph gid;
864
865 if (ccode >= GS_MIN_CID_GLYPH)
866 ccode = ccode - GS_MIN_CID_GLYPH;
867
868 if (pttfont->substitute == false) {
869 gid = ccode;
870 if (pttfont->cidtogidmap != NULL && pttfont->cidtogidmap->length > (ccode << 1) + 1) {
871 gid = pttfont->cidtogidmap->data[ccode << 1] << 8 | pttfont->cidtogidmap->data[(ccode << 1) + 1];
872 }
873 cr->client_char_code = ccode;
874 cr->char_codes[0] = gid;
875 cr->is_glyph_index = true;
876 }
877 else { /* If the composite font has a decoding, then this is a subsituted CIDFont with a "known" ordering */
878 unsigned int gc = 0, cc = (unsigned int)ccode;
879 byte uc[4];
880 int l, i;
881
882 if (penum->text.operation & TEXT_FROM_SINGLE_CHAR) {
883 cc = penum->text.data.d_char;
884 } else if (penum->text.operation & TEXT_FROM_SINGLE_GLYPH) {
885 cc = penum->text.data.d_glyph - GS_MIN_CID_GLYPH;
886 }
887 else {
888 byte *c = (byte *)&penum->text.data.bytes[penum->index - penum->bytes_decoded];
889 cc = 0;
890 for (i = 0; i < penum->bytes_decoded ; i++) {
891 cc |= c[i] << ((penum->bytes_decoded - 1) - i) * 8;
892 }
893 }
894
895 l = penum->orig_font->procs.decode_glyph((gs_font *)penum->orig_font, ccode, (gs_char)cc, (ushort *)uc, 4);
896 if (l > 0 && l <= sizeof(uc)) {
897 cc = 0;
898 for (i = 0; i < l; i++) {
899 cc |= uc[l - 1 - i] << (i * 8);
900 }
901 }
902 else
903 cc = ccode;
904 /* All known cmap tables map 32 to the space glyph, so if it looks like
905 we're going to use a notdef, then substitute the glyph for the code point 32
906 */
907 if (l != 0) {
908 code = pdfi_fapi_check_cmap_for_GID((gs_font *)pbfont, cc, &gc);
909 if (code < 0 || gc == 0)
910 (void)pdfi_fapi_check_cmap_for_GID((gs_font *)pbfont, 32, &gc);
911 }
912 else {
913 if (ccode == 0) {
914 (void)pdfi_fapi_check_cmap_for_GID((gs_font *)pbfont, 32, &gc);
915 }
916 else {
917 gc = ccode;
918 }
919 }
920
921 cr->client_char_code = ccode;
922 cr->char_codes[0] = gc;
923 cr->is_glyph_index = true;
924 }
925 return 0;
926 }
927 /* For cff based CIDFonts (and thus "Type 1" based CIDFonts, since the code
928 * is common to both) and cff fonts we only claim to the FAPI server that
929 * we have one (or two?) glyphs, because it makes the "stub" font simpler.
930 * But Freetype bounds checks the character code (or gid) against the number
931 * of glyphs in the font *before* asking us for the glyph data. So we need
932 * to extract the charstring here, store it in the fapi font, and unpack it
933 * in pdfi_fapi_get_glyph(), so we can then claim we're always rendering glyph
934 * index zero, and thus pass the bounds check.
935 */
936 else if (penum->current_font->FontType == ft_CID_encrypted) {
937 gs_font_cid0 *pfont9 = (gs_font_cid0 *)penum->current_font;
938 gs_glyph_data_t gd;
939 int f_ind;
940
941 code = (*pfont9->cidata.glyph_data)((gs_font_base *)pfont9, ccode, &gd, &f_ind);
942 if (code < 0) {
943 code = (*pfont9->cidata.glyph_data)((gs_font_base *)pfont9, 0, &gd, &f_ind);
944 }
945 if (code < 0)
946 return_error(gs_error_invalidfont);
947
948 I->ff.char_data = (void *)gd.bits.data;
949 I->ff.char_data_len = gd.bits.size;
950
951 cr->client_char_code = 0;
952 cr->char_codes[0] = 0;
953 cr->is_glyph_index = true;
954 I->ff.client_font_data2 = penum->fstack.items[penum->fstack.depth].font;
955
956 return 0;
957 }
958 else if (pbfont->FontType == ft_encrypted2) {
959 pdf_font_cff *cfffont = (pdf_font_cff *)pbfont->client_data;
960 pdf_name *glyphname = NULL;
961 pdf_string *charstr = NULL;
962 gs_const_string gname;
963
964 code = (*ctx->get_glyph_name)((gs_font *)pbfont, ccode, &gname);
965 if (code >= 0) {
966 code = pdfi_name_alloc(ctx, (byte *) gname.data, gname.size, (pdf_obj **) &glyphname);
967 if (code < 0)
968 return code;
969 pdfi_countup(glyphname);
970 }
971
972 if (code < 0) {
973 pdfi_countdown(glyphname);
974 return code;
975 }
976 code = pdfi_dict_get_by_key(cfffont->ctx, cfffont->CharStrings, glyphname, (pdf_obj **)&charstr);
977 if (code < 0) {
978 code = pdfi_map_glyph_name_via_agl(cfffont->CharStrings, glyphname, &charstr);
979 if (code < 0)
980 code = pdfi_dict_get(cfffont->ctx, cfffont->CharStrings, ".notdef", (pdf_obj **)&charstr);
981 }
982 pdfi_countdown(glyphname);
983 if (code < 0)
984 return code;
985
986 I->ff.char_data = charstr->data;
987 I->ff.char_data_len = charstr->length;
988
989 cr->client_char_code = 0;
990 cr->char_codes[0] = 0;
991 cr->is_glyph_index = true;
992
993 pdfi_countdown(charstr);
994 return code;
995 }
996 else if (pbfont->FontType == ft_TrueType) {
997 /* I'm not clear if the heavy lifting should be here or in pdfi_tt_encode_char() */
998 pdf_font_truetype *ttfont = (pdf_font_truetype *)pbfont->client_data;
999 pdf_name *GlyphName = NULL;
1000 gs_const_string gname;
1001 int i;
1002 uint cc = 0;
1003 if ((ttfont->descflags & 4) != 0) {
1004 if (ttfont->cmap == pdfi_truetype_cmap_30) {
1005
1006 ccode = cr->client_char_code;
1007 code = pdfi_fapi_check_cmap_for_GID((gs_font *)pbfont, (uint)ccode, &cc);
1008 if (code < 0 || cc == 0)
1009 cr->char_codes[0] = ccode | 0xf0 << 8;
1010 else
1011 cr->char_codes[0] = ccode;
1012 cr->is_glyph_index = false;
1013 }
1014 else {
1015 cr->char_codes[0] = cr->client_char_code;
1016 cr->is_glyph_index = false;
1017 }
1018 }
1019 else {
1020
1021 code = (*ctx->get_glyph_name)((gs_font *)pbfont, ccode, &gname);
1022 if (code >= 0) {
1023 code = pdfi_name_alloc(ctx, (byte *) gname.data, gname.size, (pdf_obj **) &GlyphName);
1024 if (code >= 0)
1025 pdfi_countup(GlyphName);
1026 else
1027 return_error(gs_error_VMerror);
1028 }
1029
1030 cr->char_codes[0] = cr->client_char_code;
1031 cr->is_glyph_index = false;
1032 if (code < 0)
1033 return 0;
1034
1035 if (ttfont->cmap == pdfi_truetype_cmap_10) {
1036 gs_glyph g;
1037
1038 g = gs_c_name_glyph((const byte *)GlyphName->data, GlyphName->length);
1039 if (g != GS_NO_GLYPH) {
1040 g = (gs_glyph)gs_c_decode(g, ENCODING_INDEX_MACROMAN);
1041 }
1042 else {
1043 g = GS_NO_CHAR;
1044 }
1045
1046 if (g != GS_NO_CHAR) {
1047 code = pdfi_fapi_check_cmap_for_GID((gs_font *)pbfont, (uint)g, &cc);
1048 }
1049
1050 if (code < 0 || cc == 0) {
1051 gs_font_type42 *pfonttt = (gs_font_type42 *)pbfont;
1052 gs_string gname;
1053 gname.data = GlyphName->data;
1054 gname.size = GlyphName->length;
1055
1056 code = pdfi_find_post_entry(pfonttt, (gs_const_string *)&gname, &cc);
1057 if (code >= 0) {
1058 cr->char_codes[0] = cc;
1059 cr->is_glyph_index = true;
1060 }
1061 }
1062 else {
1063 cr->char_codes[0] = g;
1064 cr->is_glyph_index = false;
1065 }
1066 }
1067 else if (ttfont->cmap == pdfi_truetype_cmap_31) {
1068 unsigned int cc;
1069 single_glyph_list_t *sgl = (single_glyph_list_t *)&(SingleGlyphList);
1070 /* Not to spec, but... if we get a "uni..." formatted name, use
1071 the hex value from that.
1072 */
1073 if (GlyphName->length == 7 && !strncmp((char *)GlyphName->data, "uni", 3)) {
1074 char gnbuf[64];
1075 int l = (GlyphName->length - 3) > 63 ? 63 : GlyphName->length - 3;
1076
1077 memcpy(gnbuf, GlyphName->data + 3, l);
1078 gnbuf[l] = '\0';
1079 l = sscanf(gnbuf, "%x", &cc);
1080 if (l > 0)
1081 cr->char_codes[0] = cc;
1082 else
1083 cr->char_codes[0] = 0;
1084 }
1085 else {
1086 /* Slow linear search, we could binary chop it */
1087 for (i = 0; sgl[i].Glyph != 0x00; i++) {
1088 if (sgl[i].Glyph[0] == GlyphName->data[0]
1089 && strlen(sgl[i].Glyph) == GlyphName->length
1090 && !strncmp((char *)sgl[i].Glyph, (char *)GlyphName->data, GlyphName->length))
1091 break;
1092 }
1093 if (sgl[i].Glyph != NULL) {
1094 code = pdfi_fapi_check_cmap_for_GID((gs_font *)pbfont, (uint)sgl[i].Unicode, &cc);
1095 if (code < 0 || cc == 0)
1096 cc = 0;
1097 else
1098 cc = sgl[i].Unicode;
1099 }
1100 else
1101 cc = 0;
1102
1103 if (cc == 0) {
1104 gs_font_type42 *pfonttt = (gs_font_type42 *)pbfont;
1105 gs_string gname;
1106 gname.data = GlyphName->data;
1107 gname.size = GlyphName->length;
1108
1109 code = pdfi_find_post_entry(pfonttt, (gs_const_string *)&gname, &cc);
1110 if (code >= 0) {
1111 cr->char_codes[0] = cc;
1112 cr->is_glyph_index = true;
1113 }
1114 }
1115 else {
1116 cr->char_codes[0] = cc;
1117 cr->is_glyph_index = false;
1118 }
1119 }
1120 }
1121 pdfi_countdown(GlyphName);
1122 return 0;
1123 }
1124 }
1125 else if (pbfont->FontType == ft_encrypted || pbfont->FontType == ft_MicroType) {
1126 gs_const_string gname;
1127 code = (*ctx->get_glyph_name)((gs_font *)pbfont, ccode, &gname);
1128 I->ff.char_data = cr->char_name = enc_char_name->data = (byte *)gname.data;
1129 I->ff.char_data_len = cr->char_name_length = enc_char_name->size = gname.size;
1130 cr->is_glyph_index = false;
1131 #ifdef UFST_BRIDGE
1132 if (pbfont->FontType == ft_MicroType) {
1133 pdf_font_microtype *mtf = (pdf_font_microtype *)pbfont->client_data;
1134 int64_t charcode;
1135 /* Slightly naff, but we have to explicitly check for .notdef */
1136 if (cr->char_name_length == 7 && memcmp(cr->char_name, ".notdef", cr->char_name_length) == 0) {
1137 code = 0;
1138 charcode = 0;
1139 }
1140 else {
1141 code = pdfi_lookup_fco_char_code(mtf->DecodingID, strlen(mtf->DecodingID), (const char *)cr->char_name, cr->char_name_length, &charcode);
1142 }
1143 if (code == 0) {
1144 cr->char_codes[0] = (int)charcode;
1145 cr->is_glyph_index = false;
1146 }
1147 }
1148 #endif /* UFST_BRIDGE */
1149 }
1150 return code;
1151 }
1152
1153 static int
1154 pdfi_fapi_get_glyph(gs_fapi_font * ff, gs_glyph char_code, byte * buf, int buf_length)
1155 {
1156 gs_font_base *pbfont = (gs_font_base *) ff->client_font_data2;
1157 gs_fapi_server *I = pbfont->FAPI;
1158 int code = 0;
1159 pdf_name *encn;
1160 int cstrlen = 0;
1161
1162 if (ff->is_type1) {
1163
1164 if (pbfont->FontType == ft_encrypted) {
1165 gs_font_type1 *pfont1 = (gs_font_type1 *) pbfont;
1166 pdf_name *glyphname = NULL;
1167 pdf_string *charstring = NULL;
1168 pdf_font_type1 *pdffont1 = (pdf_font_type1 *)pbfont->client_data;
1169 int leniv = pfont1->data.lenIV > 0 ? pfont1->data.lenIV : 0;
1170
1171 if (I->ff.char_data != NULL) {
1172 code = pdfi_name_alloc(pdffont1->ctx, (byte *)I->ff.char_data, I->ff.char_data_len, (pdf_obj **)&glyphname);
1173 if (code < 0)
1174 return code;
1175 pdfi_countup(glyphname);
1176 code = pdfi_dict_get_by_key(pdffont1->ctx, pdffont1->CharStrings, glyphname, (pdf_obj **)&charstring);
1177 if (code < 0) {
1178 code = pdfi_map_glyph_name_via_agl(pdffont1->CharStrings, glyphname, &charstring);
1179 if (code < 0) {
1180 code = pdfi_dict_get(pdffont1->ctx, pdffont1->CharStrings, ".notdef", (pdf_obj **)&charstring);
1181 if (code < 0) {
1182 pdfi_countdown(glyphname);
1183 code = gs_note_error(gs_error_invalidfont);
1184 goto done;
1185 }
1186 }
1187 }
1188 pdfi_countdown(glyphname);
1189 cstrlen = charstring->length - leniv;
1190 if (buf != NULL && cstrlen <= buf_length) {
1191 if (ff->need_decrypt && pfont1->data.lenIV >= 0)
1192 decode_bytes(buf, charstring->data, cstrlen + leniv, leniv);
1193 else
1194 memcpy(buf, charstring->data, charstring->length);
1195 }
1196 pdfi_countdown(charstring);
1197 /* Trigger the seac case below - we can do this safely
1198 because I->ff.char_data points to a string managed
1199 by the Encoding array in the pdf_font object
1200 */
1201 if (buf != NULL)
1202 I->ff.char_data = NULL;
1203 }
1204 else { /* SEAC */
1205 gs_const_string encstr;
1206 gs_glyph enc_ind = gs_c_known_encode(char_code, ENCODING_INDEX_STANDARD);
1207
1208 if (enc_ind == GS_NO_GLYPH) {
1209 code = gs_error_invalidfont;
1210 }
1211 else {
1212 gs_c_glyph_name(enc_ind, &encstr);
1213 code = pdfi_name_alloc(pdffont1->ctx, (byte *)encstr.data, encstr.size, (pdf_obj **)&encn);
1214 if (code < 0)
1215 goto done;
1216
1217 pdfi_countup(encn);
1218 code = pdfi_dict_get_by_key(pdffont1->ctx, pdffont1->CharStrings, encn, (pdf_obj **)&charstring);
1219 pdfi_countdown(encn);
1220 if (code < 0)
1221 goto done;
1222 cstrlen = charstring->length - leniv;
1223 if (buf != NULL && code <= buf_length) {
1224 if (ff->need_decrypt && leniv >= 0)
1225 decode_bytes(buf, charstring->data, cstrlen + leniv, leniv);
1226 else
1227 memcpy(buf, charstring->data, charstring->length);
1228 }
1229 pdfi_countdown(charstring);
1230 }
1231 }
1232 }
1233 else if (pbfont->FontType == ft_CID_encrypted || pbfont->FontType == ft_encrypted2) {
1234 gs_font_type1 *pfont = (gs_font_type1 *) (gs_font_base *) ff->client_font_data;
1235 int leniv = pfont->data.lenIV > 0 ? pfont->data.lenIV : 0;
1236
1237 if (I->ff.char_data_len > 0 && I->ff.char_data != NULL) {
1238 cstrlen = I->ff.char_data_len - leniv;
1239
1240 if (buf && buf_length >= cstrlen) {
1241 if (ff->need_decrypt && pfont->data.lenIV >= 0)
1242 decode_bytes(buf, I->ff.char_data, cstrlen + leniv, leniv);
1243 else
1244 memcpy(buf, I->ff.char_data, cstrlen);
1245
1246 /* Trigger the seac case below - we can do this safely
1247 because I->ff.char_data points to a string managed
1248 by the charstrings dict in the pdf_font object
1249 */
1250 I->ff.char_data = NULL;
1251 }
1252 }
1253 else {
1254 pdf_font_cff *pdffont = (pdf_font_cff *)pfont->client_data;
1255 pdf_name *encn;
1256 pdf_string *charstring;
1257
1258 if (pbfont->FontType == ft_CID_encrypted) {
1259 /* we're dealing with a font that's an entry in a CIDFont FDArray
1260 so don't try to use an Encoding
1261 */
1262 char indstring[33];
1263 int l;
1264 l = gs_snprintf(indstring, 32, "%u", (unsigned int)char_code);
1265 code = pdfi_name_alloc(pdffont->ctx, (byte *)indstring, l, (pdf_obj **)&encn);
1266 }
1267 else {
1268 gs_const_string encstr;
1269 gs_glyph enc_ind = gs_c_known_encode(char_code, ENCODING_INDEX_STANDARD);
1270
1271 /* Nonsense values for char_code should probably trigger an error (as above)
1272 but other consumers seem tolerant, so....
1273 */
1274 if (enc_ind == GS_NO_GLYPH)
1275 enc_ind = gs_c_known_encode(0, ENCODING_INDEX_STANDARD);
1276
1277 code = gs_c_glyph_name(enc_ind, &encstr);
1278
1279 if (code < 0)
1280 code = pdfi_name_alloc(pdffont->ctx, (byte *)".notdef", 7, (pdf_obj **)&encn);
1281 else
1282 code = pdfi_name_alloc(pdffont->ctx, (byte *)encstr.data, encstr.size, (pdf_obj **)&encn);
1283 }
1284 if (code < 0)
1285 goto done;
1286
1287 pdfi_countup(encn);
1288 code = pdfi_dict_get_by_key(pdffont->ctx, pdffont->CharStrings, encn, (pdf_obj **)&charstring);
1289 pdfi_countdown(encn);
1290 if (code < 0)
1291 goto done;
1292 cstrlen = charstring->length - leniv;
1293 if (buf != NULL && cstrlen <= buf_length) {
1294 if (ff->need_decrypt && pfont->data.lenIV >= 0)
1295 decode_bytes(buf, charstring->data, cstrlen + leniv, leniv);
1296 else
1297 memcpy(buf, charstring->data, charstring->length);
1298 }
1299 pdfi_countdown(charstring);
1300 }
1301
1302 }
1303 }
1304 else {
1305 gs_font_type42 *pfont42 = (gs_font_type42 *)pbfont;
1306 gs_glyph_data_t pgd;
1307
1308 code = pfont42->data.get_outline(pfont42, char_code, &pgd);
1309 cstrlen = pgd.bits.size;
1310 if (code >= 0) {
1311 if (buf && buf_length >= cstrlen) {
1312 memcpy(buf, pgd.bits.data, cstrlen);
1313 }
1314 }
1315 }
1316 done:
1317 if (code < 0)
1318 return code;
1319
1320 return cstrlen;
1321 }
1322
1323 static int
1324 pdfi_fapi_serialize_tt_font(gs_fapi_font * ff, void *buf, int buf_size)
1325 {
1326 return 0;
1327 }
1328
1329 static int
1330 pdfi_fapi_retrieve_tt_font(gs_fapi_font * ff, void **buf, int *buf_size)
1331 {
1332 gs_font_type42 *pfonttt = (gs_font_type42 *) ff->client_font_data;
1333 pdf_font_truetype *pdfttf = (pdf_font_truetype *)pfonttt->client_data;
1334
1335 *buf = pdfttf->sfnt->data;
1336 *buf_size = pdfttf->sfnt->length;
1337
1338 return 0;
1339 }
1340
1341
1342 static int
1343 pdfi_get_glyphdirectory_data(gs_fapi_font * ff, int char_code,
1344 const byte ** ptr)
1345 {
1346 return (0);
1347 }
1348
1349 static int
1350 pdfi_fapi_get_metrics(gs_fapi_font * ff, gs_string * char_name, gs_glyph cid, double *m, bool vertical)
1351 {
1352 return 0;
1353 }
1354
1355 static int
1356 pdfi_fapi_set_cache(gs_text_enum_t * penum, const gs_font_base * pbfont,
1357 const gs_string * char_name, gs_glyph cid,
1358 const double pwidth[2], const gs_rect * pbbox,
1359 const double Metrics2_sbw_default[4], bool * imagenow)
1360 {
1361 int code = 0;
1362 int code2;
1363 gs_gstate *pgs = penum->pgs;
1364 float w2[10];
1365 double widths[6] = {0};
1366 gs_point pt;
1367 gs_font_base *pbfont1 = (gs_font_base *)pbfont;
1368 gs_matrix imat;
1369 gs_matrix mat1;
1370
1371 mat1 = pbfont1->FontMatrix;
1372
1373 if (penum->orig_font->FontType == ft_composite) {
1374
1375 if (cid >= GS_MIN_CID_GLYPH) {
1376 cid = cid - GS_MIN_CID_GLYPH;
1377 }
1378
1379 if (pbfont->FontType == ft_encrypted || pbfont->FontType == ft_encrypted2) {
1380 gs_fapi_server *I = (gs_fapi_server *)pbfont1->FAPI;
1381 pbfont1 = (gs_font_base *)I->ff.client_font_data2;
1382 /* The following cannot fail - if the matrix multiplication didn't work
1383 we'd have errored out at a higher level
1384 */
1385 (void)gs_matrix_multiply(&pbfont->FontMatrix, &pbfont1->FontMatrix, &mat1);
1386 }
1387
1388 code = pdfi_get_cidfont_glyph_metrics((gs_font *)pbfont1, cid, widths, true);
1389 if (code < 0) {
1390 /* Insert warning here! */
1391 code = 0; /* Using the defaults */
1392 }
1393 }
1394 else {
1395 code = -1;
1396 }
1397
1398 /* Since we have to tranverse a few things by the inverse font matrix,
1399 invert the matrix once upfront.
1400 */
1401 code2 = gs_matrix_invert(&mat1, &imat);
1402 if (code2 < 0)
1403 return code2; /* By this stage, this is basically impossible */
1404
1405 if (code < 0) {
1406 w2[0] = (float)pwidth[0];
1407 w2[1] = (float)pwidth[1];
1408 }
1409 else {
1410 /* gs_distance_transform() cannot return an error */
1411 (void)gs_distance_transform(widths[GLYPH_W0_WIDTH_INDEX], widths[GLYPH_W0_HEIGHT_INDEX], &imat, &pt);
1412
1413 w2[0] = pt.x / 1000.0;
1414 w2[1] = pt.y / 1000.0;
1415 }
1416 w2[2] = pbbox->p.x;
1417 w2[3] = pbbox->p.y;
1418 w2[4] = pbbox->q.x;
1419 w2[5] = pbbox->q.y;
1420
1421 (void)gs_distance_transform(widths[GLYPH_W1_WIDTH_INDEX], widths[GLYPH_W1_HEIGHT_INDEX], &imat, &pt);
1422 w2[6] = pt.x / 1000.0;
1423 w2[7] = pt.y / 1000.0;
1424 (void)gs_distance_transform(widths[GLYPH_W1_V_X_INDEX], widths[GLYPH_W1_V_Y_INDEX], &imat, &pt);
1425 w2[8] = pt.x / 1000.0;
1426 w2[9] = pt.y / 1000.0;
1427
1428 if ((code = gs_setcachedevice2((gs_show_enum *) penum, pgs, w2)) < 0) {
1429 return (code);
1430 }
1431
1432 *imagenow = true;
1433 return (code);
1434 }
1435
1436
1437 static int
1438 pdfi_fapi_build_char(gs_show_enum * penum, gs_gstate * pgs, gs_font * pfont,
1439 gs_char chr, gs_glyph glyph)
1440 {
1441 int code = 0;
1442 gs_font_base *pbfont1;
1443 gs_fapi_server *I;
1444
1445 /* gs_fapi_do_char() expects the "natural" glyph, not the offset value */
1446 if (glyph >= GS_MIN_CID_GLYPH)
1447 glyph -= GS_MIN_CID_GLYPH;
1448
1449 pbfont1 = (gs_font_base *)pfont;
1450
1451 if (penum->fstack.depth >= 0) {
1452 gs_font_cid0 *cidpfont = (gs_font_cid0 *)penum->fstack.items[penum->fstack.depth].font;
1453 if (cidpfont->FontType == ft_CID_encrypted) {
1454 pbfont1 = (gs_font_base *)cidpfont->cidata.FDArray[penum->fstack.items[penum->fstack.depth].index];
1455 I = (gs_fapi_server *)pbfont1->FAPI;
1456 I->ff.client_font_data2 = cidpfont;
1457 }
1458 }
1459 /* If between the font's creation and now another interpreter has driven FAPI (i.e. in a Postscript Begin/EndPage
1460 context, the FAPI server data may end up set appropriately for the other interpreter, if that's happened, put
1461 ours back before trying to interpret the glyph.
1462 */
1463 if (((gs_fapi_server *)pbfont1->FAPI)->ff.get_glyphname_or_cid != pdfi_fapi_get_glyphname_or_cid) {
1464 if (pbfont1->FontType == ft_MicroType) {
1465 pdf_font_microtype *pdffont = (pdf_font_microtype *)pbfont1->client_data;
1466 char pathnm[gp_file_name_sizeof];
1467 memcpy(pathnm, pdffont->filename->data, pdffont->filename->length);
1468 pathnm[pdffont->filename->length] = '\0';
1469
1470 code = pdfi_fapi_passfont((pdf_font *)pdffont, pdffont->fco_index, (char *)"UFST", pathnm, NULL, 0);
1471 }
1472 else {
1473 code = pdfi_fapi_passfont((pdf_font *)pbfont1->client_data, 0, NULL, NULL, NULL, 0);
1474 }
1475 }
1476
1477 if (code >= 0)
1478 code = gs_fapi_do_char((gs_font *)pbfont1, pgs, (gs_text_enum_t *) penum, NULL, false, NULL, NULL, chr, glyph, 0);
1479
1480 return (code);
1481 }
1482
1483 #if 0
1484 static int
1485 pdfi_fapi_set_cache_metrics(gs_text_enum_t * penum, const gs_font_base * pbfont,
1486 const gs_string * char_name, int cid,
1487 const double pwidth[2], const gs_rect * pbbox,
1488 const double Metrics2_sbw_default[4],
1489 bool * imagenow)
1490 {
1491 return (gs_error_unknownerror);
1492 }
1493
1494 static gs_glyph
1495 pdfi_fapi_encode_char(gs_font * pfont, gs_char pchr, gs_glyph_space_t not_used)
1496 {
1497 return (gs_glyph) pchr;
1498 }
1499 #endif
1500
1501 int
1502 pdfi_fapi_passfont(pdf_font *font, int subfont, char *fapi_request,
1503 char *file_name, byte * font_data, int font_data_len)
1504 {
1505 char *fapi_id = NULL;
1506 int code = 0;
1507 gs_string fdata;
1508 gs_string *fdatap = &fdata;
1509 gs_font_base *pbfont = (gs_font_base *)font->pfont;
1510 gs_fapi_font local_pdf_ff_stub = pdfi_ff_stub;
1511 gs_fapi_ttf_cmap_request symbolic_req[GS_FAPI_NUM_TTF_CMAP_REQ] = {{3, 0}, {1, 0}, {3, 1}, {3, 10}, {-1, -1}};
1512 gs_fapi_ttf_cmap_request nonsymbolic_req[GS_FAPI_NUM_TTF_CMAP_REQ] = {{3, 1}, {1, 0}, {3, 0}, {-1, -1}, {-1, -1}};
1513 int plat, enc;
1514 const char *xlatmapu = (char *)"Microtype\000\000*\000FCO_Unicode";
1515 const char *xlatmaps = (char *)"Microtype\000\000*\000FCO_Symbol";
1516 const char *xlatmapd = (char *)"Microtype\000\000*\000FCO_Dingbats";
1517 char *xlatmap = NULL;
1518 char *decodingID = NULL;
1519
1520 if (!gs_fapi_available(pbfont->memory, fapi_request)) {
1521 return (code);
1522 }
1523
1524 if (font->pdfi_font_type == e_pdf_font_microtype) {
1525 switch (((pdf_font_microtype *)font)->fco_index) {
1526 case 27:
1527 case 76:
1528 xlatmap = (char *)xlatmaps;
1529 break;
1530 case 82:
1531 xlatmap = (char *)xlatmapd;
1532 break;
1533 default:
1534 xlatmap = (char *)xlatmapu;
1535 break;
1536 }
1537 }
1538 if (font->pdfi_font_type == e_pdf_font_truetype) {
1539 fdatap = NULL;
1540 }
1541 else if (font->pdfi_font_type == e_pdf_cidfont_type2) {
1542 fdatap->data = ((pdf_cidfont_type2 *)font)->sfnt->data;
1543 fdatap->size = ((pdf_cidfont_type2 *)font)->sfnt->length;
1544 }
1545 else {
1546 fdatap->data = font_data;
1547 fdatap->size = font_data_len;
1548 }
1549
1550 if (font->pdfi_font_type == e_pdf_font_truetype) {
1551 pdf_font_truetype *ttfont = (pdf_font_truetype *)font;
1552 *local_pdf_ff_stub.ttf_cmap_req = (ttfont->descflags & 4) ? *symbolic_req : *nonsymbolic_req;
1553 }
1554 else {
1555 /* doesn't really matter for non-ttf */
1556 *local_pdf_ff_stub.ttf_cmap_req = *nonsymbolic_req;
1557 }
1558
1559 gs_fapi_set_servers_client_data(pbfont->memory,
1560 (const gs_fapi_font *)&local_pdf_ff_stub,
1561 (gs_font *)pbfont);
1562
1563 code =
1564 gs_fapi_passfont((gs_font *)pbfont, subfont, (char *)file_name, fdatap,
1565 (char *)fapi_request, xlatmap, (char **)&fapi_id,
1566 &decodingID, pdfi_get_server_param);
1567
1568 if (code < 0 || fapi_id == NULL) {
1569 return code;
1570 }
1571
1572 if (font->pdfi_font_type == e_pdf_font_truetype) {
1573 pdf_font_truetype *ttfont = (pdf_font_truetype *)font;
1574 plat = pbfont->FAPI->ff.ttf_cmap_selected.platform_id;
1575 enc = pbfont->FAPI->ff.ttf_cmap_selected.encoding_id;
1576 ttfont->cmap = pdfi_truetype_cmap_none;
1577
1578 if (plat == 1 && enc == 0) {
1579 ttfont->cmap = pdfi_truetype_cmap_10;
1580 }
1581 else if (plat == 3 && enc == 0) {
1582 ttfont->cmap = pdfi_truetype_cmap_30;
1583 }
1584 else if (plat == 3 && enc == 1) {
1585 ttfont->cmap = pdfi_truetype_cmap_31;
1586 }
1587 else if (plat == 3 && enc == 10) { /* Currently shouldn't arise */
1588 ttfont->cmap = pdfi_truetype_cmap_310;
1589 }
1590 /* Officially only 3,1 in PDF, but 0,x is Unicode, too */
1591 else if (plat == 0) {
1592 ttfont->cmap = pdfi_truetype_cmap_31;
1593 code = pdfi_set_warning_stop(OBJ_CTX(font), gs_note_error(gs_error_invalidfont), NULL, W_PDF_INVALID_TTF_CMAP, "pdfi_fapi_passfont", NULL);
1594 }
1595 else {
1596 code = pdfi_set_warning_stop(OBJ_CTX(font), gs_note_error(gs_error_invalidfont), NULL, W_PDF_INVALID_TTF_CMAP, "pdfi_fapi_passfont", NULL);
1597 }
1598 }
1599 if (font->pdfi_font_type == e_pdf_font_microtype) {
1600 ((pdf_font_microtype *)font)->DecodingID = decodingID;
1601 }
1602 pbfont->procs.build_char = pdfi_fapi_build_char;
1603
1604 return (code);
1605 }
1606
1607 int
1608 pdfi_fapi_check_cmap_for_GID(gs_font *pfont, uint cid, uint *gid)
1609 {
1610 if (pfont->FontType == ft_TrueType
1611 || pfont->FontType == ft_CID_TrueType) {
1612 gs_font_base *pbfont = (gs_font_base *)pfont;
1613 gs_fapi_server *I = pbfont->FAPI;
1614
1615 if (I) {
1616 uint c = cid;
1617 I->ff.server_font_data = pbfont->FAPI_font_data;
1618 I->check_cmap_for_GID(I, &c);
1619 *gid = c;
1620 return 0;
1621 }
1622 }
1623 return_error(gs_error_invalidfont);
1624 }
1625
1626 #ifdef UFST_BRIDGE
1627 bool
1628 pdfi_fapi_ufst_available(gs_memory_t * mem)
1629 {
1630 gs_fapi_server *serv = NULL;
1631 int code = gs_fapi_find_server(mem, (char *)"UFST", &serv, pdfi_get_server_param);
1632
1633 if (code == 0 && serv != NULL) {
1634 return (true);
1635 } else {
1636 return (false);
1637 }
1638 }
1639 /* FIXME: environment variables.... */
1640 const char *
1641 pdfi_fapi_ufst_get_fco_list(gs_memory_t * mem)
1642 {
1643 return (UFSTFCOS);
1644 }
1645
1646 const char *
1647 pdfi_fapi_ufst_get_font_dir(gs_memory_t * mem)
1648 {
1649 return (UFSTFONTDIR);
1650 }
1651 static inline int
1652 pdfi_fapi_get_mtype_font_info(gs_font * pfont, gs_fapi_font_info item,
1653 void *data, int *size)
1654 {
1655 return (gs_fapi_get_font_info(pfont, item, 0, data, size));
1656 }
1657
1658 int
1659 pdfi_fapi_get_mtype_font_name(gs_font * pfont, byte * data, int *size)
1660 {
1661 return (pdfi_fapi_get_mtype_font_info
1662 (pfont, gs_fapi_font_info_name, data, size));
1663 }
1664
1665 int
1666 pdfi_fapi_get_mtype_font_number(gs_font * pfont, int *font_number)
1667 {
1668 int size = (int)sizeof(*font_number);
1669
1670 return (pdfi_fapi_get_mtype_font_info
1671 (pfont, gs_fapi_font_info_uid, font_number, &size));
1672 }
1673
1674 #if 0
1675 int
1676 pdfi_fapi_get_mtype_font_spaceBand(gs_font * pfont, uint * spaceBand)
1677 {
1678 int size = (int)sizeof(*spaceBand);
1679
1680 return (pdfi_fapi_get_mtype_font_info
1681 (pfont, gs_fapi_font_info_pitch, spaceBand, &size));
1682 }
1683
1684 int
1685 pdfi_fapi_get_mtype_font_scaleFactor(gs_font * pfont, uint * scaleFactor)
1686 {
1687 int size = (int)sizeof(*scaleFactor);
1688
1689 return (pdfi_fapi_get_mtype_font_info
1690 (pfont, gs_fapi_font_info_design_units, scaleFactor, &size));
1691 }
1692 #endif
1693
1694
1695 static int
1696 pdfi_get_server_param(gs_fapi_server * I, const char *subtype,
1697 char **server_param, int *server_param_size)
1698 {
1699 int length = 0;
1700 char SEPARATOR_STRING[2];
1701
1702 SEPARATOR_STRING[0] = (char)gp_file_name_list_separator;
1703 SEPARATOR_STRING[1] = '\0';
1704
1705 length += strlen(UFSTDIRPARM);
1706 length += strlen(UFSTFONTDIR);
1707 length += strlen(SEPARATOR_STRING);
1708 length += strlen(UFSTPLUGINPARM);
1709 length += strlen(UFSTPLUGINS);
1710 length++;
1711
1712 if ((*server_param) != NULL && (*server_param_size) >= length) {
1713 strcpy((char *)*server_param, (char *)UFSTDIRPARM);
1714 strcat((char *)*server_param, (char *)UFSTFONTDIR);
1715 strcat((char *)*server_param, (char *)SEPARATOR_STRING);
1716 strcat((char *)*server_param, (char *)UFSTPLUGINPARM);
1717 strcat((char *)*server_param, (char *)UFSTPLUGINS);
1718 } else {
1719 *server_param = NULL;
1720 *server_param_size = length;
1721 }
1722 return 1;
1723 }
1724 #else /* UFST_BRIDGE */
1725 static int
1726 pdfi_get_server_param(gs_fapi_server * I, const char *subtype,
1727 char **server_param, int *server_param_size)
1728 {
1729 *server_param = NULL;
1730 *server_param_size = 0;
1731 return 1;
1732 }
1733 #endif /* UFST_BRIDGE */