static int font_with_same_UID_and_another_metrics(const gs_font *pfont0, const gs_font *pfont1) { const gs_font_base *pbfont0 = (const gs_font_base *)pfont0; const gs_font_base *pbfont1 = (const gs_font_base *)pfont1; if (uid_equal(&pbfont0->UID, &pbfont1->UID)) { const ref *pfdict0 = &pfont_data(gs_font_parent(pbfont0))->dict; const ref *pfdict1 = &pfont_data(gs_font_parent(pbfont1))->dict; ref *pmdict0, *pmdict1; if (pbfont0->WMode || dict_find_string(pfdict0, "Metrics", &pmdict0) <= 0) pmdict0 = NULL; if (pbfont1->WMode || dict_find_string(pfdict1, "Metrics", &pmdict1) <= 0) pmdict1 = NULL; if (!pmdict0 != !pmdict1) return 1; if (pmdict0 != NULL && !obj_eq(pfont0->memory, pmdict0, pmdict1)) return 1; if (!pbfont0->WMode || dict_find_string(pfdict0, "Metrics2", &pmdict0) <= 0) pmdict0 = NULL; if (!pbfont0->WMode || dict_find_string(pfdict1, "Metrics2", &pmdict1) <= 0) pmdict1 = NULL; if (!pmdict0 != !pmdict1) return 1; if (pmdict0 != NULL && !obj_eq(pfont0->memory, pmdict0, pmdict1)) return 1; } return 0; }
/* Make a transformed font (common code for makefont/scalefont). */ static int make_font(i_ctx_t *i_ctx_p, const gs_matrix * pmat) { os_ptr op = osp; os_ptr fp = op - 1; gs_font *oldfont, *newfont; int code; ref *pencoding = 0; code = font_param(fp, &oldfont); if (code < 0) return code; { uint space = ialloc_space(idmemory); ialloc_set_space(idmemory, r_space(fp)); if (dict_find_string(fp, "Encoding", &pencoding) > 0 && !r_is_array(pencoding) ) code = gs_note_error(e_invalidfont); else { /* * Temporarily substitute the new dictionary * for the old one, in case the Encoding changed. */ ref olddict; olddict = *pfont_dict(oldfont); *pfont_dict(oldfont) = *fp; code = gs_makefont(ifont_dir, oldfont, pmat, &newfont); *pfont_dict(oldfont) = olddict; } ialloc_set_space(idmemory, space); } if (code < 0) return code; /* * We have to allow for the possibility that the font's Encoding * is different from that of the base font. Note that the * font_data of the new font was simply copied from the old one. */ if (pencoding != 0 && !obj_eq(imemory, pencoding, &pfont_data(newfont)->Encoding) ) { if (newfont->FontType == ft_composite) return_error(e_rangecheck); /* We should really do validity checking here.... */ ref_assign(&pfont_data(newfont)->Encoding, pencoding); lookup_gs_simple_font_encoding((gs_font_base *) newfont); } *fp = *pfont_dict(newfont); pop(1); return 0; }
/* * Get CDevProc. */ bool zchar_get_CDevProc(const gs_font_base * pbfont, ref **ppcdevproc) { const ref *pfdict = &pfont_data(gs_font_parent(pbfont))->dict; return dict_find_string(pfdict, "CDevProc", ppcdevproc) > 0; }
/* Compare the encoding of a simple font with the registered encodings. */ void lookup_gs_simple_font_encoding(gs_font_base * pfont) { const ref *pfe = &pfont_data(pfont)->Encoding; int index = -1; pfont->encoding_index = index; if (r_type(pfe) == t_array && r_size(pfe) <= 256) { /* Look for an encoding that's "close". */ uint esize = r_size(pfe); int near_index = -1; uint best = esize / 3; /* must match at least this many */ gs_const_string fstrs[256]; int i; /* Get the string names of the glyphs in the font's Encoding. */ for (i = 0; i < esize; ++i) { ref fchar; if (array_get(pfont->memory, pfe, (long)i, &fchar) < 0 || !r_has_type(&fchar, t_name) ) fstrs[i].data = 0, fstrs[i].size = 0; else { ref nsref; name_string_ref(pfont->memory, &fchar, &nsref); fstrs[i].data = nsref.value.const_bytes; fstrs[i].size = r_size(&nsref); } } /* Compare them against the known encodings. */ for (index = 0; index < NUM_KNOWN_REAL_ENCODINGS; ++index) { uint match = esize; for (i = esize; --i >= 0;) { gs_const_string rstr; gs_c_glyph_name(gs_c_known_encode((gs_char)i, index), &rstr); if (rstr.size == fstrs[i].size && !memcmp(rstr.data, fstrs[i].data, rstr.size) ) continue; if (--match <= best) break; } if (match > best) { best = match; near_index = index; /* If we have a perfect match, stop now. */ if (best == esize) break; } } index = near_index; if (best == esize) pfont->encoding_index = index; } pfont->nearest_encoding_index = index; }
int build_gs_FDArray_font(i_ctx_t *i_ctx_p, ref *op, gs_font_base **ppfont, font_type ftype, gs_memory_type_ptr_t pstype, const build_proc_refs * pbuild) { gs_font_base *pfont; font_data *pdata; int code = build_gs_outline_font(i_ctx_p, op, &pfont, ftype, pstype, pbuild, bf_options_none, build_FDArray_sub_font); static const double bbox[4] = { 0, 0, 0, 0 }; gs_uid uid; if (code < 0) return code; pdata = pfont_data(pfont); /* Fill in members normally set by build_gs_primitive_font. */ make_null(&pdata->CharStrings); /* Fill in members normally set by build_gs_simple_font. */ uid_set_invalid(&uid); init_gs_simple_font(pfont, bbox, &uid); pfont->encoding_index = ENCODING_INDEX_UNKNOWN; pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN; /* Fill in members normally set by build_gs_font. */ pfont->key_name = pfont->font_name; *ppfont = pfont; return 0; }
int zchar_charstring_data(gs_font *font, const ref *pgref, gs_glyph_data_t *pgd) { ref *pcstr; if (dict_find(&pfont_data(font)->CharStrings, pgref, &pcstr) <= 0) return_error(e_undefined); if (!r_has_type(pcstr, t_string)) { /* * The ADOBEPS4 Windows driver replaces the .notdef entry of * otherwise normal Type 1 fonts with the procedure * {pop 0 0 setcharwidth} * To prevent this from making the font unembeddable in PDF files * (with our present font-writing code), we recognize this as a * special case and return a Type 1 CharString consisting of * 0 0 hsbw endchar */ if (font->FontType == ft_encrypted && charstring_is_notdef_proc(font->memory, pcstr) ) return charstring_make_notdef(pgd, font); else return_error(e_typecheck); } gs_glyph_data_from_string(pgd, pcstr->value.const_bytes, r_size(pcstr), NULL); return 0; }
/* The caller guarantees that *op is a dictionary. */ int build_gs_primitive_font(i_ctx_t *i_ctx_p, os_ptr op, gs_font_base ** ppfont, font_type ftype, gs_memory_type_ptr_t pstype, const build_proc_refs * pbuild, build_font_options_t options) { ref *pcharstrings = 0; ref CharStrings; gs_font_base *pfont; font_data *pdata; int code; if (dict_find_string(op, "CharStrings", &pcharstrings) <= 0) { if (!(options & bf_CharStrings_optional)) return_error(e_invalidfont); } else { ref *ignore; if (!r_has_type(pcharstrings, t_dictionary)) return_error(e_invalidfont); if ((options & bf_notdef_required) != 0 && dict_find_string(pcharstrings, ".notdef", &ignore) <= 0 ) return_error(e_invalidfont); /* * Since build_gs_simple_font may resize the dictionary and cause * pointers to become invalid, save CharStrings. */ CharStrings = *pcharstrings; } code = build_gs_outline_font(i_ctx_p, op, ppfont, ftype, pstype, pbuild, options, build_gs_simple_font); if (code != 0) return code; pfont = *ppfont; pdata = pfont_data(pfont); if (pcharstrings) ref_assign(&pdata->CharStrings, &CharStrings); else make_null(&pdata->CharStrings); /* Check that the UniqueIDs match. This is part of the */ /* Adobe protection scheme, but we may as well emulate it. */ if (uid_is_valid(&pfont->UID) && !dict_check_uid_param(op, &pfont->UID) ) uid_set_invalid(&pfont->UID); if (uid_is_valid(&pfont->UID)) { const gs_font *pfont0 = (const gs_font *)pfont; code = gs_font_find_similar(ifont_dir, &pfont0, font_with_same_UID_and_another_metrics); if (code < 0) return code; /* Must not happen. */ if (code) uid_set_invalid(&pfont->UID); } return 0; }
/* Get Unicode UTF-16 code for a glyph. */ gs_char gs_font_map_glyph_to_unicode(gs_font *font, gs_glyph glyph, int ch) { font_data *pdata = pfont_data(font); const ref *UnicodeDecoding; if (r_type(&pdata->GlyphNames2Unicode) == t_dictionary) { gs_char c = gs_font_map_glyph_by_dict(font->memory, &pdata->GlyphNames2Unicode, glyph); if (c != GS_NO_CHAR) return c; if (ch != -1) { /* -1 indicates a CIDFont */ /* Its possible that we have a GlyphNames2Unicode dictionary * which contains integers and Unicode values, rather than names * and Unicode values. This happens if the input was PDF, the font * has a ToUnicode Cmap, but no Encoding. In this case we need to * use the character code as an index into the dictionary. Try that * now before we fall back to the UnicodeDecoding. */ ref *v, n; make_int(&n, ch); if (dict_find(&pdata->GlyphNames2Unicode, &n, &v) > 0) { if (r_has_type(v, t_string)) { int i, l = r_size(v); gs_char c = 0; for (i = 0; i < l; i++) c = (c << 8) | v->value.const_bytes[i]; return c; } if (r_type(v) == t_integer) return v->value.intval; } } /* * Fall through, because test.ps for SF bug #529103 requres * to examine both tables. Due to that the Unicode Decoding resource * can't be a default value for FontInfo.GlyphNames2Unicode . */ } if (glyph <= GS_MIN_CID_GLYPH) { UnicodeDecoding = zfont_get_to_unicode_map(font->dir); if (UnicodeDecoding != NULL && r_type(UnicodeDecoding) == t_dictionary) return gs_font_map_glyph_by_dict(font->memory, UnicodeDecoding, glyph); } return GS_NO_CHAR; /* No map. */ }
/* pcref points to a t_string ref. */ static int type1_continue_dispatch(i_ctx_t *i_ctx_p, gs_type1exec_state *pcxs, const ref * pcref, ref *pos, int num_args) { int value; int code; gs_glyph_data_t cs_data; gs_glyph_data_t *pcsd; cs_data.memory = imemory; if (pcref == 0) { pcsd = 0; } else { gs_glyph_data_from_string(&cs_data, pcref->value.const_bytes, r_size(pcref), NULL); pcsd = &cs_data; } /* * Since OtherSubrs may push or pop values on the PostScript operand * stack, remove the arguments of .type1execchar before calling the * Type 1 interpreter, and put them back afterwards unless we're * about to execute an OtherSubr procedure. Also, we must set up * the callback data for pushing OtherSubrs arguments. */ pcxs->i_ctx_p = i_ctx_p; pcxs->num_args = num_args; memcpy(pcxs->save_args, osp - (num_args - 1), num_args * sizeof(ref)); osp -= num_args; gs_type1_set_callback_data(&pcxs->cis, pcxs); code = pcxs->cis.pfont->data.interpret(&pcxs->cis, pcsd, &value); switch (code) { case type1_result_callothersubr: { /* * The Type 1 interpreter handles all known OtherSubrs, * so this must be an unknown one. */ const font_data *pfdata = pfont_data(gs_currentfont(igs)); code = array_get(imemory, &pfdata->u.type1.OtherSubrs, (long)value, pos); if (code >= 0) return type1_result_callothersubr; } } /* Put back the arguments removed above. */ memcpy(osp + 1, pcxs->save_args, num_args * sizeof(ref)); osp += num_args; return code; }
/* - .wrapfont - */ static int zwrapfont(i_ctx_t *i_ctx_p) { gs_font *font = gs_currentfont(igs); gs_font_type0 *font0; int wmode = 0; int code; switch (font->FontType) { case ft_TrueType: code = gs_font_type0_from_type42(&font0, (gs_font_type42 *)font, wmode, true, font->memory); if (code < 0) return code; /* * Patch up BuildChar and CIDMap. This isn't necessary for * TrueType fonts in general, only for Type 42 fonts whose * BuildChar is implemented in PostScript code. */ { font_data *pdata = pfont_data(font); const char *bgstr = "%Type11BuildGlyph"; ref temp; make_int(&temp, 0); ref_assign(&pdata->u.type42.CIDMap, &temp); code = name_ref((const byte *)bgstr, strlen(bgstr), &temp, 1); if (code < 0) return code; r_set_attrs(&temp, a_executable); ref_assign(&pdata->BuildGlyph, &temp); } break; case ft_CID_encrypted: case ft_CID_user_defined: case ft_CID_TrueType: code = gs_font_type0_from_cidfont(&font0, font, wmode, NULL, font->memory); break; default: return_error(e_rangecheck); } if (code < 0) return code; gs_setfont(igs, (gs_font *)font0); return 0; }
void get_GlyphNames2Unicode(i_ctx_t *i_ctx_p, gs_font *pfont, ref *pdref) { ref *pfontinfo = NULL, *g2u = NULL; font_data *pdata; if (dict_find_string(pdref, "FontInfo", &pfontinfo) <= 0 || !r_has_type(pfontinfo, t_dictionary) || dict_find_string(pfontinfo, "GlyphNames2Unicode", &g2u) <= 0 || !r_has_type(pfontinfo, t_dictionary)) return; /* * Since build_gs_font may resize the dictionary and cause * pointers to become invalid, save Glyph2Unicode */ pdata = pfont_data(pfont); ref_assign_new(&pdata->GlyphNames2Unicode, g2u); }
/* * Get the metrics for a character from the Metrics dictionary of a base * font. If present, store the l.s.b. in psbw[0,1] and the width in * psbw[2,3]. */ int /*metrics_present*/ zchar_get_metrics(const gs_font_base *pbfont, const ref *pcnref, float psbw[4]) { const ref *pfdict = &pfont_data(pbfont)->dict; ref *pmdict; if ( dict_find_string(pfdict, "Metrics", &pmdict) > 0 ) { ref *pmvalue; check_type_only(*pmdict, t_dictionary); check_dict_read(*pmdict); if ( dict_find(pmdict, pcnref, &pmvalue) > 0 ) { if ( num_params(pmvalue, 1, psbw + 2) >= 0 ) { /* <wx> only */ psbw[3] = 0; return metricsWidthOnly; } else { int code; check_read_type_only(*pmvalue, t_array); switch ( r_size(pmvalue) ) { case 2: /* [<sbx> <wx>] */ code = num_params(pmvalue->value.refs + 1, 2, psbw); psbw[2] = psbw[1]; psbw[1] = psbw[3] = 0; break; case 4: /* [<sbx> <sby> <wx> <wy>] */ code = num_params(pmvalue->value.refs + 3, 4, psbw); break; default: return_error(e_rangecheck); } if ( code < 0 ) return code; return metricsSideBearingAndWidth; } } } return metricsNone; }
/* Encode a character. */ gs_glyph zfont_encode_char(gs_font *pfont, gs_char chr, gs_glyph_space_t gspace) { font_data *pdata = pfont_data(pfont); const ref *pencoding = &pdata->Encoding; ulong index = chr; /* work around VAX widening bug */ ref cname; int code = array_get(pfont->memory, pencoding, (long)index, &cname); if (code < 0 || !r_has_type(&cname, t_name)) return gs_no_glyph; if (pfont->FontType == ft_user_defined && r_type(&pdata->BuildGlyph) == t_null) { ref nsref, tname; name_string_ref(pfont->memory, &cname, &nsref); if (r_size(&nsref) == 7 && !memcmp(nsref.value.const_bytes, ".notdef", r_size(&nsref))) { /* A special support for high level devices. They need a glyph name but the font doesn't provide one due to an instandard BuildChar. Such fonts don't conform to PLRM section 5.3.7, but we've got real examples that we want to handle (Bug 686982). Construct a name here. Low level devices don't pass here, because regular PS interpretation doesn't need such names. */ char buf[20]; int code; if (gspace == GLYPH_SPACE_NOGEN) return gs_no_glyph; sprintf(buf, "j%ld", chr); /* 'j' is arbutrary. */ code = name_ref(pfont->memory, (const byte *)buf, strlen(buf), &tname, 1); if (code < 0) { /* Can't propagate the error due to interface limitation, return with .notdef */ } else cname = tname; } } return (gs_glyph)name_index(pfont->memory, &cname); }
/* Get the vertical metrics for a character from Metrics2, if present. */ int zchar_get_metrics2(const gs_font_base * pbfont, const ref * pcnref, double pwv[4]) { const ref *pfdict = &pfont_data(gs_font_parent(pbfont))->dict; ref *pmdict; if (dict_find_string(pfdict, "Metrics2", &pmdict) > 0) { ref *pmvalue; check_type_only(*pmdict, t_dictionary); check_dict_read(*pmdict); if (dict_find(pmdict, pcnref, &pmvalue) > 0) { check_read_type_only(*pmvalue, t_array); if (r_size(pmvalue) == 4) { int code = num_params(pmvalue->value.refs + 3, 4, pwv); return (code < 0 ? code : metricsSideBearingAndWidth); } } } return metricsNone; }
int zfont_info(gs_font *font, const gs_point *pscale, int members, gs_font_info_t *info) { int code = gs_default_font_info(font, pscale, members & ~(FONT_INFO_COPYRIGHT | FONT_INFO_NOTICE | FONT_INFO_FAMILY_NAME | FONT_INFO_FULL_NAME), info); const ref *pfdict; ref *pfontinfo, *pvalue; if (code < 0) return code; pfdict = &pfont_data(font)->dict; if (dict_find_string(pfdict, "FontInfo", &pfontinfo) <= 0 || !r_has_type(pfontinfo, t_dictionary)) return 0; if ((members & FONT_INFO_COPYRIGHT) && zfont_info_has(pfontinfo, "Copyright", &info->Copyright)) info->members |= FONT_INFO_COPYRIGHT; if ((members & FONT_INFO_NOTICE) && zfont_info_has(pfontinfo, "Notice", &info->Notice)) info->members |= FONT_INFO_NOTICE; if ((members & FONT_INFO_FAMILY_NAME) && zfont_info_has(pfontinfo, "FamilyName", &info->FamilyName)) info->members |= FONT_INFO_FAMILY_NAME; if ((members & FONT_INFO_FULL_NAME) && zfont_info_has(pfontinfo, "FullName", &info->FullName)) info->members |= FONT_INFO_FULL_NAME; if ((members & FONT_INFO_EMBEDDING_RIGHTS) && (dict_find_string(pfontinfo, "FSType", &pvalue) > 0)) { info->EmbeddingRights = pvalue->value.intval; info->members |= FONT_INFO_EMBEDDING_RIGHTS; } return code; }
/* * Note that op_show_continue_dispatch sets osp = op explicitly iff the * dispatch succeeds. This is so that the show operators don't pop anything * from the o-stack if they don't succeed. Note also that if it returns an * error, it has freed the enumerator. */ int op_show_continue_dispatch(i_ctx_t *i_ctx_p, int npop, int code) { os_ptr op = osp - npop; gs_text_enum_t *penum = senum; switch (code) { case 0: { /* all done */ os_ptr save_osp = osp; osp = op; code = (*real_opproc(&seproc)) (i_ctx_p); op_show_free(i_ctx_p, code); if (code < 0) { osp = save_osp; return code; } return o_pop_estack; } case TEXT_PROCESS_INTERVENE: { ref *pslot = &sslot; /* only used for kshow */ push(2); make_int(op - 1, gs_text_current_char(penum)); /* previous char */ make_int(op, gs_text_next_char(penum)); push_op_estack(op_show_continue); /* continue after kerning */ *++esp = *pslot; /* kerning procedure */ return o_push_estack; } case TEXT_PROCESS_RENDER: { gs_font *pfont = gs_currentfont(igs); font_data *pfdata = pfont_data(pfont); gs_char chr = gs_text_current_char(penum); gs_glyph glyph = gs_text_current_glyph(penum); push(2); op[-1] = pfdata->dict; /* push the font */ /* * For Type 1 and Type 4 fonts, prefer BuildChar to BuildGlyph * if there is no glyph, or if there is both a character and a * glyph and the glyph is the one that corresponds to the * character in the Encoding, so that PostScript procedures * appearing in the CharStrings dictionary will receive the * character code rather than the character name; for Type 3 * fonts, prefer BuildGlyph to BuildChar. For other font types * (such as CID fonts), only BuildGlyph will be present. */ if (pfont->FontType == ft_user_defined) { /* Type 3 font, prefer BuildGlyph. */ if (level2_enabled && !r_has_type(&pfdata->BuildGlyph, t_null) && glyph != gs_no_glyph ) { glyph_ref(imemory, glyph, op); esp[2] = pfdata->BuildGlyph; } else if (r_has_type(&pfdata->BuildChar, t_null)) goto err; else if (chr == gs_no_char) { /* glyphshow, reverse map the character */ /* through the Encoding */ ref gref; const ref *pencoding = &pfdata->Encoding; glyph_ref(imemory, glyph, &gref); if (!map_glyph_to_char(imemory, &gref, pencoding, (ref *) op) ) { /* Not found, try .notdef */ name_enter_string(imemory, ".notdef", &gref); if (!map_glyph_to_char(imemory, &gref, pencoding, (ref *) op) ) goto err; } esp[2] = pfdata->BuildChar; } else { make_int(op, chr & 0xff); esp[2] = pfdata->BuildChar; } } else { /* * For a Type 1 or Type 4 font, prefer BuildChar or * BuildGlyph as described above: we know that both * BuildChar and BuildGlyph are present. For other font * types, only BuildGlyph is available. */ ref eref, gref; if (chr != gs_no_char && !r_has_type(&pfdata->BuildChar, t_null) && (glyph == gs_no_glyph || (!r_has_type(&pfdata->Encoding, t_null) && array_get(imemory, &pfdata->Encoding, (long)(chr & 0xff), &eref) >= 0 && (glyph_ref(imemory, glyph, &gref), obj_eq(imemory, &gref, &eref)))) ) { make_int(op, chr & 0xff); esp[2] = pfdata->BuildChar; } else { /* We might not have a glyph: substitute 0. **HACK** */ if (glyph == gs_no_glyph) make_int(op, 0); else glyph_ref(imemory, glyph, op); esp[2] = pfdata->BuildGlyph; } } /* Save the stack depths in case we bail out. */ sodepth.value.intval = ref_stack_count(&o_stack) - 2; sddepth.value.intval = ref_stack_count(&d_stack); push_op_estack(op_show_continue); ++esp; /* skip BuildChar or BuildGlyph proc */ return o_push_estack; } case TEXT_PROCESS_CDEVPROC: { gs_font *pfont = penum->current_font; ref cnref; op_proc_t cont = op_show_continue, exec_cont = 0; gs_glyph glyph = penum->returned.current_glyph; int code; pop(npop); op = osp; glyph_ref(imemory, glyph, &cnref); if (pfont->FontType == ft_CID_TrueType) { gs_font_type42 *pfont42 = (gs_font_type42 *)pfont; uint glyph_index = pfont42->data.get_glyph_index(pfont42, glyph); code = zchar42_set_cache(i_ctx_p, (gs_font_base *)pfont42, &cnref, glyph_index, cont, &exec_cont); } else if (pfont->FontType == ft_CID_encrypted) code = z1_set_cache(i_ctx_p, (gs_font_base *)pfont, &cnref, glyph, cont, &exec_cont); else return_error(e_unregistered); /* Unimplemented. */ if (exec_cont != 0) return_error(e_unregistered); /* Must not happen. */ return code; } default: /* error */ err: if (code >= 0) code = gs_note_error(e_invalidfont); return op_show_free(i_ctx_p, code); } }
int zdefault_make_font(gs_font_dir * pdir, const gs_font * oldfont, const gs_matrix * pmat, gs_font ** ppfont) { gs_font *newfont = *ppfont; gs_memory_t *mem = newfont->memory; /* HACK: we know this font was allocated by the interpreter. */ gs_ref_memory_t *imem = (gs_ref_memory_t *)mem; ref *fp = pfont_dict(oldfont); font_data *pdata; ref newdict, newmat, scalemat; uint dlen = dict_maxlength(fp); uint mlen = dict_length(fp) + 3; /* FontID, OrigFont, ScaleMatrix */ int code; if (dlen < mlen) dlen = mlen; if ((pdata = gs_alloc_struct(mem, font_data, &st_font_data, "make_font(font_data)")) == 0 ) return_error(e_VMerror); /* * This dictionary is newly created: it's safe to pass NULL as the * dstack pointer to dict_copy and dict_put_string. */ if ((code = dict_alloc(imem, dlen, &newdict)) < 0 || (code = dict_copy(fp, &newdict, NULL)) < 0 || (code = gs_alloc_ref_array(imem, &newmat, a_all, 12, "make_font(matrices)")) < 0 ) return code; refset_null_new(newmat.value.refs, 12, imemory_new_mask(imem)); ref_assign(&scalemat, &newmat); r_set_size(&scalemat, 6); scalemat.value.refs += 6; /* * Create the scaling matrix. We could do this several different * ways: by "dividing" the new FontMatrix by the base FontMatrix, by * multiplying the current scaling matrix by a ScaleMatrix kept in * the gs_font, or by multiplying the current scaling matrix by the * ScaleMatrix from the font dictionary. We opt for the last of * these. */ { gs_matrix scale, prev_scale; ref *ppsm; if (!(dict_find_string(fp, "ScaleMatrix", &ppsm) > 0 && read_matrix(mem, ppsm, &prev_scale) >= 0 && gs_matrix_multiply(pmat, &prev_scale, &scale) >= 0) ) scale = *pmat; write_matrix_new(&scalemat, &scale, imem); } r_clear_attrs(&scalemat, a_write); r_set_size(&newmat, 6); write_matrix_new(&newmat, &newfont->FontMatrix, imem); r_clear_attrs(&newmat, a_write); if ((code = dict_put_string(&newdict, "FontMatrix", &newmat, NULL)) < 0 || (code = dict_put_string(&newdict, "OrigFont", pfont_dict(oldfont->base), NULL)) < 0 || (code = dict_put_string(&newdict, "ScaleMatrix", &scalemat, NULL)) < 0 || (code = add_FID(NULL, &newdict, newfont, imem)) < 0 ) return code; newfont->client_data = pdata; *pdata = *pfont_data(oldfont); pdata->dict = newdict; r_clear_attrs(dict_access_ref(&newdict), a_write); return 0; }
/* * Consult Metrics2 and CDevProc, and call setcachedevice[2]. Return * o_push_estack if we had to call a CDevProc, or if we are skipping the * rendering process (only getting the metrics). */ int zchar_set_cache(os_ptr op, const gs_font_base *pbfont, const ref *pcnref, const float psb[2], const float pwidth[2], const gs_rect *pbbox, int (*cont_fill)(P1(os_ptr)), int (*cont_stroke)(P1(os_ptr))) { const ref *pfdict = &pfont_data(pbfont)->dict; ref *pmdict; ref *pcdevproc; int have_cdevproc; ref rpop; bool metrics2 = false; int (*cont)(P1(os_ptr)); float w2[10]; gs_show_enum *penum = op_show_find(); w2[0] = pwidth[0], w2[1] = pwidth[1]; /* Adjust the bounding box for stroking if needed. */ w2[2] = pbbox->p.x, w2[3] = pbbox->p.y; w2[4] = pbbox->q.x, w2[5] = pbbox->q.y; if ( pbfont->PaintType == 0 ) cont = cont_fill; else { double expand = max(1.415, gs_currentmiterlimit(igs)) * gs_currentlinewidth(igs) / 2; w2[2] -= expand, w2[3] -= expand; w2[4] += expand, w2[5] += expand; cont = cont_stroke; } /* Check for Metrics2. */ if ( dict_find_string(pfdict, "Metrics2", &pmdict) > 0 ) { ref *pmvalue; check_type_only(*pmdict, t_dictionary); check_dict_read(*pmdict); if ( dict_find(pmdict, pcnref, &pmvalue) > 0 ) { check_read_type_only(*pmvalue, t_array); if ( r_size(pmvalue) == 4 ) { int code = num_params(pmvalue->value.refs + 3, 4, w2 + 6); if ( code < 0 ) return code; metrics2 = true; } } } /* Check for CDevProc or "short-circuiting". */ have_cdevproc = dict_find_string(pfdict, "CDevProc", &pcdevproc) > 0; if ( have_cdevproc || gs_show_width_only(penum) ) { int i; int (*zsetc)(P1(os_ptr)); int nparams; if ( have_cdevproc ) { check_proc_only(*pcdevproc); zsetc = zsetcachedevice2; if ( !metrics2 ) { w2[6] = w2[0], w2[7] = w2[1]; w2[8] = w2[9] = 0; } nparams = 10; } else { make_oper(&rpop, 0, zpop); pcdevproc = &rpop; if ( metrics2 ) zsetc = zsetcachedevice2, nparams = 10; else zsetc = zsetcachedevice, nparams = 6; } check_estack(3); /* Push the l.s.b. for .type1addpath if necessary. */ if ( psb != 0 ) { push(nparams + 3); make_real(op - (nparams + 2), psb[0]); make_real(op - (nparams + 1), psb[1]); } else { push(nparams + 1); } for ( i = 0; i < nparams; ++i ) make_real(op - nparams + i, w2[i]); ref_assign(op, pcnref); push_op_estack(cont); push_op_estack(zsetc); ++esp; ref_assign(esp, pcdevproc); return o_push_estack; } { int code = (metrics2 ? gs_setcachedevice2(penum, igs, w2) : gs_setcachedevice(penum, igs, w2)); if ( code < 0 ) return code; } /* No metrics modification, do the stroke or fill now. */ /* Push the l.s.b. for .type1addpath if necessary. */ if ( psb != 0 ) { push(2); make_real(op - 1, psb[0]); make_real(op, psb[1]); } return cont(op); }
/* Get bytes from GlyphData or DataSource. */ static int cid0_read_bytes(gs_font_cid0 *pfont, ulong base, uint count, byte *buf, gs_glyph_data_t *pgd) { const font_data *pfdata = pfont_data(pfont); byte *data = buf; gs_font *gdfont = 0; /* pfont if newly allocated, 0 if not */ int code = 0; /* Check for overflow. */ if (base != (long)base || base > base + count) return_error(e_rangecheck); if (r_has_type(&pfdata->u.cid0.DataSource, t_null)) { /* Get the bytes from GlyphData (a string or array of strings). */ const ref *pgdata = &pfdata->u.cid0.GlyphData; if (r_has_type(pgdata, t_string)) { /* single string */ uint size = r_size(pgdata); if (base >= size || count > size - base) return_error(e_rangecheck); data = pgdata->value.bytes + base; } else { /* array of strings */ /* * The algorithm is similar to the one in * string_array_access_proc in zfont42.c, but it also has to * deal with the case where the requested string crosses array * elements. */ ulong skip = base; uint copied = 0; uint index = 0; ref rstr; uint size; for (;; skip -= size, ++index) { int code = array_get(pfont->memory, pgdata, index, &rstr); if (code < 0) return code; if (!r_has_type(&rstr, t_string)) return_error(e_typecheck); size = r_size(&rstr); if (skip < size) break; } size -= skip; if (count <= size) { data = rstr.value.bytes + skip; } else { /* multiple strings needed */ if (data == 0) { /* no buffer provided */ data = gs_alloc_string(pfont->memory, count, "cid0_read_bytes"); if (data == 0) return_error(e_VMerror); gdfont = (gs_font *)pfont; /* newly allocated */ } memcpy(data, rstr.value.bytes + skip, size); copied = size; while (copied < count) { int code = array_get(pfont->memory, pgdata, ++index, &rstr); if (code < 0) goto err; if (!r_has_type(&rstr, t_string)) { code = gs_note_error(e_typecheck); goto err; } size = r_size(&rstr); if (size > count - copied) size = count - copied; memcpy(data + copied, rstr.value.bytes, size); copied += size; } } } } else { /* Get the bytes from DataSource (a stream). */ stream *s; uint nread; i_ctx_t *i_ctx_p = get_minst_from_memory(pfont->memory)->i_ctx_p; check_read_known_file(i_ctx_p, s, &pfdata->u.cid0.DataSource, return_error); if (sseek(s, base) < 0) return_error(e_ioerror); if (data == 0) { /* no buffer provided */ data = gs_alloc_string(pfont->memory, count, "cid0_read_bytes"); if (data == 0) return_error(e_VMerror); gdfont = (gs_font *)pfont; /* newly allocated */ } if (sgets(s, data, count, &nread) < 0 || nread != count) { code = gs_note_error(e_ioerror); goto err; } } gs_glyph_data_from_string(pgd, data, count, gdfont); return code; err: if (data != buf) gs_free_string(pfont->memory, data, count, "cid0_read_bytes"); return code; }
/* <string|name> <font_dict> .buildfont9 <string|name> <font> */ static int zbuildfont9(i_ctx_t *i_ctx_p) { os_ptr op = osp; build_proc_refs build; int code = build_proc_name_refs(imemory, &build, NULL, "%Type9BuildGlyph"); gs_font_cid_data common; ref GlyphDirectory, GlyphData, DataSource; ref *prfda, cfnstr; ref *pCIDFontName, CIDFontName; gs_font_type1 **FDArray; uint FDArray_size; int FDBytes; uint CIDMapOffset; gs_font_base *pfont; gs_font_cid0 *pfcid; uint i; /* * If the CIDFont's data have been loaded into VM, GlyphData will be * a string or an array of strings; if they are loaded incrementally * from a file, GlyphData will be an integer, and DataSource will be * a (reusable) stream. */ if (code < 0 || (code = cid_font_data_param(op, &common, &GlyphDirectory)) < 0 || (code = dict_find_string(op, "FDArray", &prfda)) < 0 || (code = dict_find_string(op, "CIDFontName", &pCIDFontName)) <= 0 || (code = dict_int_param(op, "FDBytes", 0, MAX_FDBytes, -1, &FDBytes)) < 0 ) return code; /* * Since build_gs_simple_font may resize the dictionary and cause * pointers to become invalid, save CIDFontName */ CIDFontName = *pCIDFontName; if (r_has_type(&GlyphDirectory, t_null)) { /* Standard CIDFont, require GlyphData and CIDMapOffset. */ ref *pGlyphData; if ((code = dict_find_string(op, "GlyphData", &pGlyphData)) < 0 || (code = dict_uint_param(op, "CIDMapOffset", 0, max_uint - 1, max_uint, &CIDMapOffset)) < 0) return code; GlyphData = *pGlyphData; if (r_has_type(&GlyphData, t_integer)) { ref *pds; stream *ignore_s; if ((code = dict_find_string(op, "DataSource", &pds)) < 0) return code; check_read_file(i_ctx_p, ignore_s, pds); DataSource = *pds; } else { if (!r_has_type(&GlyphData, t_string) && !r_is_array(&GlyphData)) return_error(e_typecheck); make_null(&DataSource); } } else { make_null(&GlyphData); make_null(&DataSource); CIDMapOffset = 0; } if (!r_is_array(prfda)) return_error(e_invalidfont); FDArray_size = r_size(prfda); if (FDArray_size == 0) return_error(e_invalidfont); FDArray = ialloc_struct_array(FDArray_size, gs_font_type1 *, &st_gs_font_type1_ptr_element, "buildfont9(FDarray)"); if (FDArray == 0) return_error(e_VMerror); memset(FDArray, 0, sizeof(gs_font_type1 *) * FDArray_size); for (i = 0; i < FDArray_size; ++i) { ref rfd; array_get(imemory, prfda, (long)i, &rfd); code = fd_array_element(i_ctx_p, &FDArray[i], &rfd); if (code < 0) goto fail; } code = build_gs_outline_font(i_ctx_p, op, &pfont, ft_CID_encrypted, &st_gs_font_cid0, &build, bf_Encoding_optional | bf_UniqueID_ignored, build_gs_simple_font); if (code < 0) goto fail; if (code == 1) { /* The font already has a FID, don't need to build it again. Release FDArray and return normally. fixme: FDArray fonts are thrown for garbager. We're not safe to build them after build_gs_simple_font(..., &pfont, ...), because a failure in building them would throw an underbuilt font with unclear consequences. */ ifree_object(FDArray, "buildfont9(FDarray)"); return 0; } pfont->procs.enumerate_glyph = gs_font_cid0_enumerate_glyph; pfont->procs.glyph_outline = z9_glyph_outline; pfont->procs.glyph_info = z9_glyph_info; pfcid = (gs_font_cid0 *)pfont; pfcid->cidata.common = common; pfcid->cidata.CIDMapOffset = CIDMapOffset; pfcid->cidata.FDArray = FDArray; pfcid->cidata.FDArray_size = FDArray_size; pfcid->cidata.FDBytes = FDBytes; pfcid->cidata.glyph_data = z9_glyph_data; pfcid->cidata.proc_data = 0; /* for GC */ if (pfcid->font_name.size == 0) { get_font_name(imemory, &cfnstr, &CIDFontName); copy_font_name(&pfcid->font_name, &cfnstr); } ref_assign(&pfont_data(pfont)->u.cid0.GlyphDirectory, &GlyphDirectory); ref_assign(&pfont_data(pfont)->u.cid0.GlyphData, &GlyphData); ref_assign(&pfont_data(pfont)->u.cid0.DataSource, &DataSource); code = define_gs_font(i_ctx_p, (gs_font *)pfont); if (code >= 0) code = gs_notify_register(&pfont->notify_list, notify_remove_font_type9, pfont); if (code >= 0) { for (i = 0; i < FDArray_size; ++i) { FDArray[i]->dir = pfont->dir; FDArray[i]->data.parent = pfont; } return code; } fail: ifree_object(FDArray, "buildfont9(FDarray)"); return code; }
/* Note that pgd may be NULL. */ static int z9_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_glyph_data_t *pgd, int *pfidx) { gs_font_cid0 *pfont = (gs_font_cid0 *)pbfont; const font_data *pfdata = pfont_data(pfont); long glyph_index = (long)(glyph - gs_min_cid_glyph); gs_glyph_data_t gdata; ulong fidx; int code; gdata.memory = pfont->memory; if (!r_has_type(&pfdata->u.cid0.GlyphDirectory, t_null)) { code = font_gdir_get_outline(pfont->memory, &pfdata->u.cid0.GlyphDirectory, glyph_index, &gdata); if (code < 0) return code; /* Get the definition from GlyphDirectory. */ if (!gdata.bits.data) return_error(e_rangecheck); code = get_index(&gdata, pfont->cidata.FDBytes, &fidx); if (code < 0) return code; if (fidx >= pfont->cidata.FDArray_size) return_error(e_rangecheck); if (pgd) *pgd = gdata; *pfidx = (int)fidx; return code; } /* Get the definition from the binary data (GlyphData or DataSource). */ if (glyph_index < 0 || glyph_index >= pfont->cidata.common.CIDCount) { *pfidx = 0; if (pgd) gs_glyph_data_from_null(pgd); return_error(e_rangecheck); } { byte fd_gd[(MAX_FDBytes + MAX_GDBytes) * 2]; uint num_bytes = pfont->cidata.FDBytes + pfont->cidata.common.GDBytes; ulong base = pfont->cidata.CIDMapOffset + glyph_index * num_bytes; ulong gidx, fidx_next, gidx_next; int rcode = cid0_read_bytes(pfont, base, (ulong)(num_bytes * 2), fd_gd, &gdata); gs_glyph_data_t orig_data; if (rcode < 0) return rcode; orig_data = gdata; if ((code = get_index(&gdata, pfont->cidata.FDBytes, &fidx)) < 0 || (code = get_index(&gdata, pfont->cidata.common.GDBytes, &gidx)) < 0 || (code = get_index(&gdata, pfont->cidata.FDBytes, &fidx_next)) < 0 || (code = get_index(&gdata, pfont->cidata.common.GDBytes, &gidx_next)) < 0 ) DO_NOTHING; gs_glyph_data_free(&orig_data, "z9_glyph_data"); if (code < 0) return code; /* * Some CID fonts (from Adobe!) have invalid font indexes for * missing glyphs. Handle this now. */ if (gidx_next <= gidx) { /* missing glyph */ *pfidx = 0; if (pgd) gs_glyph_data_from_null(pgd); return_error(e_undefined); } if (fidx >= pfont->cidata.FDArray_size) return_error(e_rangecheck); *pfidx = (int)fidx; if (pgd == 0) return 0; return cid0_read_bytes(pfont, gidx, gidx_next - gidx, NULL, pgd); } }
/* * Consult Metrics2 and CDevProc, and call setcachedevice[2]. Return * o_push_estack if we had to call a CDevProc, or if we are skipping the * rendering process (only getting the metrics). * Returns exec_cont - a function, which must be called by caller after this function. */ int zchar_set_cache(i_ctx_t *i_ctx_p, const gs_font_base * pbfont, const ref * pcnref, const double psb[2], const double pwidth[2], const gs_rect * pbbox, op_proc_t cont, op_proc_t *exec_cont, const double Metrics2_sbw_default[4]) { os_ptr op = osp; ref *pcdevproc, *valueref; int have_cdevproc; ref rpop; ref cid, *cidptr; bool metrics2; bool metrics2_use_default = false; double w2[10]; gs_text_enum_t *penum = op_show_find(i_ctx_p); w2[0] = pwidth[0], w2[1] = pwidth[1]; /* Adjust the bounding box for stroking if needed. */ w2[2] = pbbox->p.x, w2[3] = pbbox->p.y; w2[4] = pbbox->q.x, w2[5] = pbbox->q.y; if (pbfont->PaintType != 0) { double expand = max(1.415, gs_currentmiterlimit(igs)) * gs_currentlinewidth(igs) / 2; w2[2] -= expand, w2[3] -= expand; w2[4] += expand, w2[5] += expand; } /* Check for Metrics2. */ { int code = zchar_get_metrics2(pbfont, pcnref, w2 + 6); if (code < 0) return code; metrics2 = code > 0; } /* * For FontType 9 and 11, if Metrics2 is missing, the caller provides * default Metrics2 values derived from the FontBBox. */ if (!metrics2 && Metrics2_sbw_default != NULL) { w2[6] = Metrics2_sbw_default[2]; w2[7] = Metrics2_sbw_default[3]; w2[8] = Metrics2_sbw_default[0]; w2[9] = Metrics2_sbw_default[1]; metrics2 = true; metrics2_use_default = true; } /* Check for CDevProc or "short-circuiting". */ have_cdevproc = zchar_get_CDevProc(pbfont, &pcdevproc); /* Obscure test. The CDevProc is supposed to be called with the original CID but what we get passed * here is the TT GID. So the CDevProc won't do the right thing. We need to extract the CID from the * enumerator, and use that instead. */ cidptr = (ref *)pcnref; if (pbfont->FontType == ft_CID_TrueType && dict_find_string(&pfont_data(gs_font_parent(pbfont))->dict, "File", &valueref) > 0) { if (pbfont->key_name.size != pbfont->font_name.size || strncmp((const char *)pbfont->key_name.chars, (const char *)pbfont->font_name.chars, pbfont->key_name.size)) { if (penum->returned.current_glyph >= GS_MIN_CID_GLYPH) { make_int(&cid, penum->returned.current_glyph - GS_MIN_CID_GLYPH); } else { make_int(&cid, penum->returned.current_glyph); } cidptr = &cid; } } if (have_cdevproc || zchar_show_width_only(penum)) { int i; op_proc_t zsetc; int nparams; if (have_cdevproc) { check_proc_only(*pcdevproc); zsetc = zsetcachedevice2; /* If we have cdevproc and the font type is CID type 0, we'll throw away Metrics2_sbw_default that is calculated from FontBBox. */ if (!metrics2 || (penum->current_font->FontType == ft_CID_encrypted && metrics2_use_default)) { w2[6] = w2[0], w2[7] = w2[1]; w2[8] = w2[9] = 0; } nparams = 10; } else { make_oper(&rpop, 0, zpop); pcdevproc = &rpop; if (metrics2) zsetc = zsetcachedevice2, nparams = 10; else zsetc = zsetcachedevice, nparams = 6; } check_estack(3); /* Push the l.s.b. for .type1addpath if necessary. */ if (psb != 0) { push(nparams + 3); make_real(op - (nparams + 2), psb[0]); make_real(op - (nparams + 1), psb[1]); } else { push(nparams + 1); } for (i = 0; i < nparams; ++i) make_real(op - nparams + i, w2[i]); ref_assign(op, cidptr); push_op_estack(cont); push_op_estack(zsetc); ++esp; ref_assign(esp, pcdevproc); return o_push_estack; } { int code = (metrics2 ? gs_text_setcachedevice2(penum, w2) : gs_text_setcachedevice(penum, w2)); if (code < 0) return code; } /* No metrics modification, do the stroke or fill now. */ /* Push the l.s.b. for .type1addpath if necessary. */ if (psb != 0) { push(2); make_real(op - 1, psb[0]); make_real(op, psb[1]); } *exec_cont = cont; return 0; }