gxfcopy.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  1. /* Copyright (C) 2002 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxfcopy.c,v 1.55 2004/12/15 23:21:32 igor Exp $ */
  14. /* Font copying for high-level output */
  15. #include "memory_.h"
  16. #include "gx.h"
  17. #include "gscencs.h"
  18. #include "gserrors.h"
  19. #include "gsline.h" /* for BuildChar */
  20. #include "gspaint.h" /* for BuildChar */
  21. #include "gspath.h" /* for gs_moveto in BuildChar */
  22. #include "gsstruct.h"
  23. #include "gsutil.h"
  24. #include "stream.h"
  25. #include "gxfont.h"
  26. #include "gxfont1.h"
  27. #include "gxfont42.h"
  28. #include "gxfcid.h"
  29. #include "gxfcopy.h"
  30. #include "gxfcache.h" /* for gs_font_dir_s */
  31. #include "gxistate.h" /* for Type 1 glyph_outline */
  32. #include "gxtext.h" /* for BuildChar */
  33. #include "gxtype1.h" /* for Type 1 glyph_outline */
  34. #include "gzstate.h" /* for path for BuildChar */
  35. #include "gdevpsf.h"
  36. #define GLYPHS_SIZE_IS_PRIME 1 /* Old code = 0, new code = 1. */
  37. /* ================ Types and structures ================ */
  38. typedef struct gs_copied_glyph_s gs_copied_glyph_t;
  39. typedef struct gs_copied_font_data_s gs_copied_font_data_t;
  40. typedef struct gs_copied_font_procs_s {
  41. int (*finish_copy_font)(gs_font *font, gs_font *copied);
  42. int (*copy_glyph)(gs_font *font, gs_glyph glyph, gs_font *copied,
  43. int options);
  44. int (*add_encoding)(gs_font *copied, gs_char chr, gs_glyph glyph);
  45. int (*named_glyph_slot)(gs_copied_font_data_t *cfdata, gs_glyph glyph,
  46. gs_copied_glyph_t **pslot);
  47. /* Font procedures */
  48. font_proc_encode_char((*encode_char));
  49. font_proc_glyph_info((*glyph_info));
  50. font_proc_glyph_outline((*glyph_outline));
  51. } gs_copied_font_procs_t;
  52. /*
  53. * We need to store the Subrs data for Type 1/2 and CIDFontType 0 fonts,
  54. * and the GlobalSubrs data for all but Type 1.
  55. */
  56. typedef struct gs_subr_info_s {
  57. byte *data; /* Subr data */
  58. int count;
  59. uint *starts; /* [count+1] Subr[i] = data[starts[i]..starts[i+1]] */
  60. } gs_subr_info_t;
  61. /*
  62. * The glyphs for copied fonts are stored explicitly in a table indexed by
  63. * glyph number.
  64. * For Type 1 fonts, the glyph numbers are parallel to the hashed name table.
  65. * For TrueType fonts, the glyph number is the TrueType GID.
  66. * For CIDFontType 0 fonts, the glyph number is the CID.
  67. * For CIDFontType 2 fonts, the glyph number is the TrueType GID;
  68. * a separate CIDMap array maps CIDs to GIDs.
  69. */
  70. struct gs_copied_glyph_s {
  71. gs_const_string gdata; /* vector data */
  72. #define HAS_DATA 1 /* entry is in use */
  73. /* HAS_SBW* are only used for TT-based fonts */
  74. #define HAS_SBW0 2 /* has hmtx */
  75. #define HAS_SBW1 4 /* has vmtx */
  76. byte used; /* non-zero iff this entry is in use */
  77. /* (if not, gdata.{data,size} = 0) */
  78. };
  79. /*
  80. * We use a special GC descriptor to avoid large GC overhead.
  81. */
  82. gs_private_st_composite(st_gs_copied_glyph_element, gs_copied_glyph_t,
  83. "gs_copied_glyph_t[]", copied_glyph_element_enum_ptrs,
  84. copied_glyph_element_reloc_ptrs);
  85. private ENUM_PTRS_WITH(copied_glyph_element_enum_ptrs, gs_copied_glyph_t *pcg)
  86. if (index < size / (uint)sizeof(gs_copied_glyph_t))
  87. return ENUM_CONST_STRING(&pcg[index].gdata);
  88. return 0;
  89. ENUM_PTRS_END
  90. private RELOC_PTRS_WITH(copied_glyph_element_reloc_ptrs, gs_copied_glyph_t *pcg)
  91. {
  92. uint count = size / (uint)sizeof(gs_copied_glyph_t);
  93. gs_copied_glyph_t *p = pcg;
  94. for (; count > 0; --count, ++p)
  95. if (p->gdata.size > 0)
  96. RELOC_CONST_STRING_VAR(p->gdata);
  97. }
  98. RELOC_PTRS_END
  99. /*
  100. * Type 1 and TrueType fonts also have a 'names' table, parallel to the
  101. * 'glyphs' table.
  102. * For Type 1 fonts, this is a hash table; glyph numbers are assigned
  103. * arbitrarily, according to the hashed placement of the names.
  104. * For TrueType fonts, this is indexed by GID.
  105. * The strings in this table are either those returned by the font's
  106. * glyph_name procedure, which we assume are garbage-collected, or those
  107. * associated with the known encodings, which we assume are immutable.
  108. */
  109. typedef struct gs_copied_glyph_name_s {
  110. gs_glyph glyph; /* key (for comparison and glyph_name only) */
  111. gs_const_string str; /* glyph name */
  112. } gs_copied_glyph_name_t;
  113. /*
  114. * We use the same special GC descriptor as above for 'names'.
  115. */
  116. gs_private_st_composite(st_gs_copied_glyph_name_element,
  117. gs_copied_glyph_name_t,
  118. "gs_copied_glyph_name_t[]",
  119. copied_glyph_name_enum_ptrs,
  120. copied_glyph_name_reloc_ptrs);
  121. private ENUM_PTRS_WITH(copied_glyph_name_enum_ptrs, gs_copied_glyph_name_t *pcgn)
  122. if (index < size / (uint)sizeof(gs_copied_glyph_name_t)) {
  123. const gs_copied_glyph_name_t *const p = &pcgn[index];
  124. return (p->str.size == 0 ||
  125. gs_is_c_glyph_name(p->str.data, p->str.size) ?
  126. ENUM_CONST_STRING2(0, 0) :
  127. ENUM_CONST_STRING(&p->str));
  128. }
  129. return 0;
  130. /* We should mark glyph name here, but we have no access to
  131. the gs_font_dir instance. Will mark in gs_copied_font_data_enum_ptrs.
  132. */
  133. ENUM_PTRS_END
  134. private RELOC_PTRS_WITH(copied_glyph_name_reloc_ptrs, gs_copied_glyph_name_t *pcgn)
  135. {
  136. uint count = size / (uint)sizeof(gs_copied_glyph_name_t);
  137. gs_copied_glyph_name_t *p = pcgn;
  138. for (; count > 0; --count, ++p)
  139. if (p->str.size > 0 && !gs_is_c_glyph_name(p->str.data, p->str.size))
  140. RELOC_CONST_STRING_VAR(p->str);
  141. }
  142. RELOC_PTRS_END
  143. /*
  144. * To accommodate glyphs with multiple names, there is an additional
  145. * 'extra_names' table. Since this is rare, this table uses linear search.
  146. */
  147. typedef struct gs_copied_glyph_extra_name_s gs_copied_glyph_extra_name_t;
  148. struct gs_copied_glyph_extra_name_s {
  149. gs_copied_glyph_name_t name;
  150. uint gid; /* index in glyphs table */
  151. gs_copied_glyph_extra_name_t *next;
  152. };
  153. BASIC_PTRS(gs_copied_glyph_extra_name_ptrs) {
  154. GC_STRING_ELT(gs_copied_glyph_extra_name_t, name.str),
  155. GC_OBJ_ELT(gs_copied_glyph_extra_name_t, next)
  156. };
  157. gs_private_st_basic(st_gs_copied_glyph_extra_name,
  158. gs_copied_glyph_extra_name_t,
  159. "gs_copied_glyph_extra_name_t",
  160. gs_copied_glyph_extra_name_ptrs,
  161. gs_copied_glyph_extra_name_data);
  162. /*
  163. * The client_data of copied fonts points to an instance of
  164. * gs_copied_font_data_t.
  165. */
  166. struct gs_copied_font_data_s {
  167. gs_font_info_t info; /* from the original font, must be first */
  168. const gs_copied_font_procs_t *procs;
  169. gs_copied_glyph_t *glyphs; /* [glyphs_size] */
  170. uint glyphs_size; /* (a power of 2 or a prime number for Type 1/2) */
  171. uint num_glyphs; /* The number of glyphs copied. */
  172. gs_glyph notdef; /* CID 0 or .notdef glyph */
  173. /*
  174. * We don't use a union for the rest of the data, because some of the
  175. * cases overlap and it's just not worth the trouble.
  176. */
  177. gs_copied_glyph_name_t *names; /* (Type 1/2, TrueType) [glyphs_size] */
  178. gs_copied_glyph_extra_name_t *extra_names; /* (TrueType) */
  179. byte *data; /* (TrueType and CID fonts) copied data */
  180. uint data_size; /* (TrueType and CID fonts) */
  181. gs_glyph *Encoding; /* (Type 1/2 and Type 42) [256] */
  182. ushort *CIDMap; /* (CIDFontType 2) [CIDCount] */
  183. gs_subr_info_t subrs; /* (Type 1/2 and CIDFontType 0) */
  184. gs_subr_info_t global_subrs; /* (Type 2 and CIDFontType 0) */
  185. gs_font_cid0 *parent; /* (Type 1 subfont) => parent CIDFontType 0 */
  186. gs_font_dir *dir;
  187. };
  188. extern_st(st_gs_font_info);
  189. private
  190. ENUM_PTRS_WITH(gs_copied_font_data_enum_ptrs, gs_copied_font_data_t *cfdata)
  191. if (index == 12) {
  192. gs_copied_glyph_name_t *names = cfdata->names;
  193. gs_copied_glyph_extra_name_t *en = cfdata->extra_names;
  194. int i;
  195. if (names != NULL)
  196. for (i = 0; i < cfdata->glyphs_size; ++i)
  197. if (names[i].glyph < gs_c_min_std_encoding_glyph)
  198. cfdata->dir->ccache.mark_glyph(mem, names[i].glyph, NULL);
  199. for (; en != NULL; en = en->next)
  200. if (en->name.glyph < gs_c_min_std_encoding_glyph)
  201. cfdata->dir->ccache.mark_glyph(mem, en->name.glyph, NULL);
  202. }
  203. return ENUM_USING(st_gs_font_info, &cfdata->info, sizeof(gs_font_info_t), index - 12);
  204. ENUM_PTR3(0, gs_copied_font_data_t, glyphs, names, extra_names);
  205. ENUM_PTR3(3, gs_copied_font_data_t, data, Encoding, CIDMap);
  206. ENUM_PTR3(6, gs_copied_font_data_t, subrs.data, subrs.starts, global_subrs.data);
  207. ENUM_PTR3(9, gs_copied_font_data_t, global_subrs.starts, parent, dir);
  208. ENUM_PTRS_END
  209. private RELOC_PTRS_WITH(gs_copied_font_data_reloc_ptrs, gs_copied_font_data_t *cfdata)
  210. {
  211. RELOC_PTR3(gs_copied_font_data_t, glyphs, names, extra_names);
  212. RELOC_PTR3(gs_copied_font_data_t, data, Encoding, CIDMap);
  213. RELOC_PTR3(gs_copied_font_data_t, subrs.data, subrs.starts, global_subrs.data);
  214. RELOC_PTR3(gs_copied_font_data_t, global_subrs.starts, parent, dir);
  215. RELOC_USING(st_gs_font_info, &cfdata->info, sizeof(gs_font_info_t));
  216. }
  217. RELOC_PTRS_END
  218. gs_private_st_composite(st_gs_copied_font_data, gs_copied_font_data_t, "gs_copied_font_data_t",\
  219. gs_copied_font_data_enum_ptrs, gs_copied_font_data_reloc_ptrs);
  220. inline private gs_copied_font_data_t *
  221. cf_data(const gs_font *font)
  222. {
  223. return (gs_copied_font_data_t *)font->client_data;
  224. }
  225. /* ================ Procedures ================ */
  226. /* ---------------- Private utilities ---------------- */
  227. /* Copy a string. Return 0 or gs_error_VMerror. */
  228. private int
  229. copy_string(gs_memory_t *mem, gs_const_string *pstr, client_name_t cname)
  230. {
  231. const byte *data = pstr->data;
  232. uint size = pstr->size;
  233. byte *str;
  234. if (data == 0)
  235. return 0; /* empty string */
  236. str = gs_alloc_string(mem, size, cname);
  237. pstr->data = str;
  238. if (str == 0)
  239. return_error(gs_error_VMerror);
  240. memcpy(str, data, size);
  241. return 0;
  242. }
  243. /* Free a copied string. */
  244. private void
  245. uncopy_string(gs_memory_t *mem, gs_const_string *pstr, client_name_t cname)
  246. {
  247. if (pstr->data)
  248. gs_free_const_string(mem, pstr->data, pstr->size, cname);
  249. }
  250. /*
  251. * Allocate an Encoding for a Type 1 or Type 42 font.
  252. */
  253. private int
  254. copied_Encoding_alloc(gs_font *copied)
  255. {
  256. gs_copied_font_data_t *const cfdata = cf_data(copied);
  257. gs_glyph *Encoding = (gs_glyph *)
  258. gs_alloc_byte_array(copied->memory, 256, sizeof(*cfdata->Encoding),
  259. "copy_font_type1(Encoding)");
  260. int i;
  261. if (Encoding == 0)
  262. return_error(gs_error_VMerror);
  263. for (i = 0; i < 256; ++i)
  264. Encoding[i] = GS_NO_GLYPH;
  265. cfdata->Encoding = Encoding;
  266. return 0;
  267. }
  268. /*
  269. * Allocate and set up data copied from a TrueType or CID font.
  270. * stell(*s) + extra is the length of the data.
  271. */
  272. private int
  273. copied_data_alloc(gs_font *copied, stream *s, uint extra, int code)
  274. {
  275. gs_copied_font_data_t *const cfdata = cf_data(copied);
  276. uint len = stell(s);
  277. byte *fdata;
  278. if (code < 0)
  279. return code;
  280. fdata = gs_alloc_bytes(copied->memory, len + extra, "copied_data_alloc");
  281. if (fdata == 0)
  282. return_error(gs_error_VMerror);
  283. swrite_string(s, fdata, len);
  284. cfdata->data = fdata;
  285. cfdata->data_size = len + extra;
  286. return 0;
  287. }
  288. /*
  289. * Copy Subrs or GSubrs from a font.
  290. */
  291. private int
  292. copy_subrs(gs_font_type1 *pfont, bool global, gs_subr_info_t *psi,
  293. gs_memory_t *mem)
  294. {
  295. int i, code;
  296. uint size;
  297. gs_glyph_data_t gdata;
  298. byte *data;
  299. uint *starts;
  300. gdata.memory = pfont->memory;
  301. /* Scan the font to determine the size of the subrs. */
  302. for (i = 0, size = 0;
  303. (code = pfont->data.procs.subr_data(pfont, i, global, &gdata)) !=
  304. gs_error_rangecheck;
  305. ++i) {
  306. if (code >= 0) {
  307. size += gdata.bits.size;
  308. gs_glyph_data_free(&gdata, "copy_subrs");
  309. }
  310. }
  311. if (size == 0)
  312. data = 0, starts = 0, i = 0;
  313. else {
  314. /* Allocate the copy. */
  315. data = gs_alloc_bytes(mem, size, "copy_subrs(data)");
  316. starts = (uint *)gs_alloc_byte_array(mem, i + 1, sizeof(*starts),
  317. "copy_subrs(starts)");
  318. if (data == 0 || starts == 0) {
  319. gs_free_object(mem, starts, "copy_subrs(starts)");
  320. gs_free_object(mem, data, "copy_subrs(data)");
  321. return_error(gs_error_VMerror);
  322. }
  323. /* Copy the data. */
  324. for (i = 0, size = 0;
  325. (code = pfont->data.procs.subr_data(pfont, i, global, &gdata)) !=
  326. gs_error_rangecheck;
  327. ++i) {
  328. starts[i] = size;
  329. if (code >= 0) {
  330. memcpy(data + size, gdata.bits.data, gdata.bits.size);
  331. size += gdata.bits.size;
  332. gs_glyph_data_free(&gdata, "copy_subrs");
  333. }
  334. }
  335. starts[i] = size;
  336. }
  337. psi->data = data;
  338. psi->starts = starts;
  339. psi->count = i;
  340. return 0;
  341. }
  342. /*
  343. * Return a pointer to the definition of a copied glyph, accessed either by
  344. * name or by glyph number. If the glyph is out of range, return
  345. * gs_error_rangecheck; if the glyph is in range but undefined, store a
  346. * pointer to the slot where it would be added, which will have gdata.data
  347. * == 0, and return gs_error_undefined; if the glyph is defined, store the
  348. * pointer and return 0.
  349. */
  350. private int
  351. copied_glyph_slot(gs_copied_font_data_t *cfdata, gs_glyph glyph,
  352. gs_copied_glyph_t **pslot)
  353. {
  354. uint gsize = cfdata->glyphs_size;
  355. *pslot = 0;
  356. if (glyph >= GS_MIN_GLYPH_INDEX) {
  357. /* CIDFontType 2 uses glyph indices for slots. */
  358. if (glyph - GS_MIN_GLYPH_INDEX >= gsize)
  359. return_error(gs_error_rangecheck);
  360. *pslot = &cfdata->glyphs[glyph - GS_MIN_GLYPH_INDEX];
  361. } else if (glyph >= GS_MIN_CID_GLYPH) {
  362. /* CIDFontType 0 uses CIDS for slots. */
  363. if (glyph - GS_MIN_CID_GLYPH >= gsize)
  364. return_error(gs_error_rangecheck);
  365. *pslot = &cfdata->glyphs[glyph - GS_MIN_CID_GLYPH];
  366. } else if (cfdata->names == 0)
  367. return_error(gs_error_rangecheck);
  368. else {
  369. int code = cfdata->procs->named_glyph_slot(cfdata, glyph, pslot);
  370. if (code < 0)
  371. return code;
  372. }
  373. if (!(*pslot)->used)
  374. return_error(gs_error_undefined);
  375. return 0;
  376. }
  377. private int
  378. named_glyph_slot_none(gs_copied_font_data_t *cfdata, gs_glyph glyph,
  379. gs_copied_glyph_t **pslot)
  380. {
  381. return_error(gs_error_rangecheck);
  382. }
  383. private int
  384. named_glyph_slot_hashed(gs_copied_font_data_t *cfdata, gs_glyph glyph,
  385. gs_copied_glyph_t **pslot)
  386. {
  387. uint gsize = cfdata->glyphs_size;
  388. gs_copied_glyph_name_t *names = cfdata->names;
  389. uint hash = (uint)glyph % gsize;
  390. /*
  391. * gsize is either a prime number or a power of 2.
  392. * If it is prime, any positive reprobe below gsize guarantees that we
  393. * will touch every slot.
  394. * If it is a power of 2, any odd reprobe guarantees that we
  395. * will touch every slot.
  396. */
  397. uint hash2 = ((uint)glyph / gsize * 2 + 1) % gsize;
  398. uint tries = gsize;
  399. while (names[hash].str.data != 0 && names[hash].glyph != glyph) {
  400. hash = (hash + hash2) % gsize;
  401. if (!tries)
  402. return gs_error_undefined;
  403. tries--;
  404. }
  405. *pslot = &cfdata->glyphs[hash];
  406. return 0;
  407. }
  408. private int
  409. named_glyph_slot_linear(gs_copied_font_data_t *cfdata, gs_glyph glyph,
  410. gs_copied_glyph_t **pslot)
  411. {
  412. {
  413. gs_copied_glyph_name_t *names = cfdata->names;
  414. int i;
  415. for (i = 0; i < cfdata->glyphs_size; ++i)
  416. if (names[i].glyph == glyph) {
  417. *pslot = &cfdata->glyphs[i];
  418. return 0;
  419. }
  420. }
  421. /* This might be a glyph with multiple names. Search extra_names. */
  422. {
  423. gs_copied_glyph_extra_name_t *extra_name = cfdata->extra_names;
  424. for (; extra_name != 0; extra_name = extra_name->next)
  425. if (extra_name->name.glyph == glyph) {
  426. *pslot = &cfdata->glyphs[extra_name->gid];
  427. return 0;
  428. }
  429. }
  430. return_error(gs_error_rangecheck);
  431. }
  432. /*
  433. * Add glyph data to the glyph table. This handles copying the vector
  434. * data, detecting attempted redefinitions, and freeing temporary glyph
  435. * data. The glyph must be an integer, an index in the glyph table.
  436. * Return 1 if the glyph was already defined, 0 if newly added (or an
  437. * error per options).
  438. */
  439. private int
  440. copy_glyph_data(gs_font *font, gs_glyph glyph, gs_font *copied, int options,
  441. gs_glyph_data_t *pgdata, const byte *prefix, int prefix_bytes)
  442. {
  443. gs_copied_font_data_t *const cfdata = cf_data(copied);
  444. uint size = pgdata->bits.size;
  445. gs_copied_glyph_t *pcg = 0;
  446. int code = copied_glyph_slot(cfdata, glyph, &pcg);
  447. switch (code) {
  448. case 0: /* already defined */
  449. if ((options & COPY_GLYPH_NO_OLD) ||
  450. pcg->gdata.size != prefix_bytes + size ||
  451. memcmp(pcg->gdata.data, prefix, prefix_bytes) ||
  452. memcmp(pcg->gdata.data + prefix_bytes,
  453. pgdata->bits.data, size)
  454. )
  455. code = gs_note_error(gs_error_invalidaccess);
  456. else
  457. code = 1;
  458. break;
  459. case gs_error_undefined:
  460. if (options & COPY_GLYPH_NO_NEW)
  461. code = gs_note_error(gs_error_undefined);
  462. else if (pcg == NULL)
  463. code = gs_note_error(gs_error_undefined);
  464. else {
  465. uint str_size = prefix_bytes + size;
  466. byte *str = gs_alloc_string(copied->memory, str_size,
  467. "copy_glyph_data(data)");
  468. if (str == 0)
  469. code = gs_note_error(gs_error_VMerror);
  470. else {
  471. if (prefix_bytes)
  472. memcpy(str, prefix, prefix_bytes);
  473. memcpy(str + prefix_bytes, pgdata->bits.data, size);
  474. pcg->gdata.data = str;
  475. pcg->gdata.size = str_size;
  476. pcg->used = HAS_DATA;
  477. code = 0;
  478. cfdata->num_glyphs++;
  479. }
  480. }
  481. default:
  482. break;
  483. }
  484. gs_glyph_data_free(pgdata, "copy_glyph_data");
  485. return code;
  486. }
  487. /*
  488. * Copy a glyph name into the names table.
  489. */
  490. private int
  491. copy_glyph_name(gs_font *font, gs_glyph glyph, gs_font *copied,
  492. gs_glyph copied_glyph)
  493. {
  494. gs_glyph known_glyph;
  495. gs_copied_font_data_t *const cfdata = cf_data(copied);
  496. gs_copied_glyph_t *pcg;
  497. int code = copied_glyph_slot(cfdata, copied_glyph, &pcg);
  498. gs_copied_glyph_name_t *pcgn;
  499. gs_const_string str;
  500. if (code < 0 ||
  501. (code = font->procs.glyph_name(font, glyph, &str)) < 0
  502. )
  503. return code;
  504. /* Try to share a permanently allocated known glyph name. */
  505. if ((known_glyph = gs_c_name_glyph(str.data, str.size)) != GS_NO_GLYPH)
  506. gs_c_glyph_name(known_glyph, &str);
  507. else if ((code = copy_string(copied->memory, &str, "copy_glyph_name")) < 0)
  508. return code;
  509. pcgn = cfdata->names + (pcg - cfdata->glyphs);
  510. if (pcgn->glyph != GS_NO_GLYPH &&
  511. (pcgn->str.size != str.size ||
  512. memcmp(pcgn->str.data, str.data, str.size))
  513. ) {
  514. /* This is a glyph with multiple names. Add an extra_name entry. */
  515. gs_copied_glyph_extra_name_t *extra_name =
  516. gs_alloc_struct(copied->memory, gs_copied_glyph_extra_name_t,
  517. &st_gs_copied_glyph_extra_name,
  518. "copy_glyph_name(extra_name)");
  519. if (extra_name == 0)
  520. return_error(gs_error_VMerror);
  521. extra_name->next = cfdata->extra_names;
  522. extra_name->gid = pcg - cfdata->glyphs;
  523. cfdata->extra_names = extra_name;
  524. pcgn = &extra_name->name;
  525. }
  526. pcgn->glyph = glyph;
  527. pcgn->str = str;
  528. return 0;
  529. }
  530. /*
  531. * Find the .notdef glyph in a font.
  532. */
  533. private gs_glyph
  534. find_notdef(gs_font_base *font)
  535. {
  536. int index = 0;
  537. gs_glyph glyph;
  538. while (font->procs.enumerate_glyph((gs_font *)font, &index,
  539. GLYPH_SPACE_NAME, &glyph),
  540. index != 0)
  541. if (gs_font_glyph_is_notdef(font, glyph))
  542. return glyph;
  543. return GS_NO_GLYPH; /* best we can do */
  544. }
  545. /*
  546. * Add an Encoding entry to a character-indexed font (Type 1/2/42).
  547. */
  548. private int
  549. copied_char_add_encoding(gs_font *copied, gs_char chr, gs_glyph glyph)
  550. {
  551. gs_copied_font_data_t *const cfdata = cf_data(copied);
  552. gs_glyph *Encoding = cfdata->Encoding;
  553. gs_copied_glyph_t *pslot;
  554. int code;
  555. if (Encoding == 0)
  556. return_error(gs_error_invalidaccess);
  557. if (chr >= 256 || glyph >= GS_MIN_CID_GLYPH)
  558. return_error(gs_error_rangecheck);
  559. code = copied_glyph_slot(cfdata, glyph, &pslot);
  560. if (code < 0)
  561. return code;
  562. if (Encoding[chr] != glyph && Encoding[chr] != GS_NO_GLYPH)
  563. return_error(gs_error_invalidaccess);
  564. Encoding[chr] = glyph;
  565. return 0;
  566. }
  567. /* Don't allow adding an Encoding entry. */
  568. private int
  569. copied_no_add_encoding(gs_font *copied, gs_char chr, gs_glyph glyph)
  570. {
  571. return_error(gs_error_invalidaccess);
  572. }
  573. /* ---------------- Font procedures ---------------- */
  574. private int
  575. copied_font_info(gs_font *font, const gs_point *pscale, int members,
  576. gs_font_info_t *info)
  577. {
  578. if (pscale != 0)
  579. return_error(gs_error_rangecheck);
  580. *info = cf_data(font)->info;
  581. return 0;
  582. }
  583. private gs_glyph
  584. copied_encode_char(gs_font *copied, gs_char chr, gs_glyph_space_t glyph_space)
  585. {
  586. gs_copied_font_data_t *const cfdata = cf_data(copied);
  587. const gs_glyph *Encoding = cfdata->Encoding;
  588. if (chr >= 256 || Encoding == 0)
  589. return GS_NO_GLYPH;
  590. return Encoding[chr];
  591. }
  592. private int
  593. copied_enumerate_glyph(gs_font *font, int *pindex,
  594. gs_glyph_space_t glyph_space, gs_glyph *pglyph)
  595. {
  596. gs_copied_font_data_t *const cfdata = cf_data(font);
  597. for (; *pindex < cfdata->glyphs_size; ++*pindex)
  598. if (cfdata->glyphs[*pindex].used) {
  599. *pglyph =
  600. (glyph_space == GLYPH_SPACE_NAME && cfdata->names != 0 ?
  601. cfdata->names[*pindex].glyph :
  602. /* CIDFontType 0 uses CIDS as slot indices; CIDFontType 2 uses GIDs. */
  603. *pindex + (glyph_space == GLYPH_SPACE_NAME
  604. ? GS_MIN_CID_GLYPH : GS_MIN_GLYPH_INDEX));
  605. ++(*pindex);
  606. return 0;
  607. }
  608. *pindex = 0;
  609. return 0;
  610. }
  611. private int
  612. copied_glyph_name(gs_font *font, gs_glyph glyph, gs_const_string *pstr)
  613. {
  614. gs_copied_font_data_t *const cfdata = cf_data(font);
  615. gs_copied_glyph_t *pcg;
  616. if (glyph >= GS_MIN_CID_GLYPH)
  617. return_error(gs_error_rangecheck);
  618. if (copied_glyph_slot(cfdata, glyph, &pcg) < 0)
  619. return_error(gs_error_undefined);
  620. *pstr = cfdata->names[pcg - cfdata->glyphs].str;
  621. return 0;
  622. }
  623. private int
  624. copied_build_char(gs_text_enum_t *pte, gs_state *pgs, gs_font *font,
  625. gs_char chr, gs_glyph glyph)
  626. {
  627. int wmode = font->WMode;
  628. int code;
  629. gs_glyph_info_t info;
  630. double wxy[6];
  631. double sbw_stub[4]; /* Currently glyph_outline retrieves sbw only with type 1,2,9 fonts. */
  632. if (glyph == GS_NO_GLYPH) {
  633. glyph = font->procs.encode_char(font, chr, GLYPH_SPACE_INDEX);
  634. if (glyph == GS_NO_GLYPH) {
  635. glyph = cf_data(font)->notdef;
  636. }
  637. }
  638. /*
  639. * Type 1/2 outlines don't require a current point, but TrueType
  640. * outlines do. We might want to fix this someday....
  641. */
  642. if ((code = gs_moveto(pgs, 0.0, 0.0)) < 0 ||
  643. (code = font->procs.glyph_info(font, glyph, NULL,
  644. (GLYPH_INFO_WIDTH << wmode) |
  645. GLYPH_INFO_BBOX |
  646. GLYPH_INFO_OUTLINE_WIDTHS,
  647. &info)) < 0
  648. )
  649. return code;
  650. wxy[0] = info.width[wmode].x;
  651. wxy[1] = info.width[wmode].y;
  652. wxy[2] = info.bbox.p.x;
  653. wxy[3] = info.bbox.p.y;
  654. wxy[4] = info.bbox.q.x;
  655. wxy[5] = info.bbox.q.y;
  656. if ((code = gs_text_setcachedevice(pte, wxy)) < 0 ||
  657. (code = font->procs.glyph_outline(font, wmode, glyph, &ctm_only(pgs),
  658. pgs->path, sbw_stub)) < 0
  659. )
  660. return code;
  661. if (font->PaintType != 0) {
  662. gs_setlinewidth(pgs, font->StrokeWidth);
  663. return gs_stroke(pgs);
  664. } else {
  665. return gs_fill(pgs);
  666. }
  667. }
  668. private inline bool
  669. compare_arrays(const float *v0, int l0, const float *v1, int l1)
  670. {
  671. if (l0 != l1)
  672. return false;
  673. if (memcmp(v0, v1, l0 * sizeof(v0[0])))
  674. return false;
  675. return true;
  676. }
  677. #define compare_tables(a, b) compare_arrays(a.values, a.count, b.values, b.count)
  678. private int
  679. compare_glyphs(const gs_font *cfont, const gs_font *ofont, gs_glyph *glyphs,
  680. int num_glyphs, int glyphs_step, int level)
  681. {
  682. /*
  683. * Checking widths because we can synthesize fonts from random fonts
  684. * having same FontName and FontType.
  685. * We must request width explicitely because Type 42 stores widths
  686. * separately from outline data. We could skip it for Type 1, which doesn't.
  687. * We don't care of Metrics, Metrics2 because copied font never has them.
  688. */
  689. int i, WMode = ofont->WMode;
  690. int members = (GLYPH_INFO_WIDTH0 << WMode) | GLYPH_INFO_OUTLINE_WIDTHS | GLYPH_INFO_NUM_PIECES;
  691. gs_matrix mat;
  692. gs_copied_font_data_t *const cfdata = cf_data(cfont);
  693. int num_new_glyphs = 0;
  694. gs_make_identity(&mat);
  695. for (i = 0; i < num_glyphs; i++) {
  696. gs_glyph glyph = *(gs_glyph *)((byte *)glyphs + i * glyphs_step);
  697. gs_glyph pieces0[40], *pieces = pieces0;
  698. gs_glyph_info_t info0, info1;
  699. int code0 = ofont->procs.glyph_info((gs_font *)ofont, glyph, &mat, members, &info0);
  700. int code1 = cfont->procs.glyph_info((gs_font *)cfont, glyph, &mat, members, &info1);
  701. int code2, code;
  702. if (code0 == gs_error_undefined)
  703. continue;
  704. if (code1 == gs_error_undefined) {
  705. num_new_glyphs++;
  706. if (num_new_glyphs > cfdata->glyphs_size - cfdata->num_glyphs)
  707. return 0;
  708. continue;
  709. }
  710. if (code0 < 0)
  711. return code0;
  712. if (code1 < 0)
  713. return code1;
  714. if (info0.num_pieces != info1.num_pieces)
  715. return 0;
  716. if (info0.width[WMode].x != info1.width[WMode].x ||
  717. info0.width[WMode].y != info1.width[WMode].y)
  718. return 0;
  719. if (WMode && (info0.v.x != info1.v.x || info0.v.y != info1.v.y))
  720. return 0;
  721. if (info0.num_pieces > 0) {
  722. if(level > 5)
  723. return_error(gs_error_rangecheck); /* abnormal glyph recursion */
  724. if (info0.num_pieces > countof(pieces0) / 2) {
  725. pieces = (gs_glyph *)gs_alloc_bytes(cfont->memory,
  726. sizeof(glyphs) * info0.num_pieces * 2, "compare_glyphs");
  727. if (pieces == 0)
  728. return_error(gs_error_VMerror);
  729. }
  730. info0.pieces = pieces;
  731. info1.pieces = pieces + info0.num_pieces;
  732. code0 = ofont->procs.glyph_info((gs_font *)ofont, glyph, &mat,
  733. GLYPH_INFO_PIECES, &info0);
  734. code1 = cfont->procs.glyph_info((gs_font *)cfont, glyph, &mat,
  735. GLYPH_INFO_PIECES, &info1);
  736. if (code0 >= 0 && code1 >= 0) {
  737. code2 = memcmp(info0.pieces, info1.pieces, info0.num_pieces * sizeof(*pieces));
  738. if (!code2)
  739. code = compare_glyphs(cfont, ofont, pieces, info0.num_pieces, glyphs_step, level + 1);
  740. else
  741. code = 0; /* Quiet compiler. */
  742. } else
  743. code2 = code = 0;
  744. if (pieces != pieces0)
  745. gs_free_object(cfont->memory, pieces, "compare_glyphs");
  746. if (code0 == gs_error_undefined)
  747. continue;
  748. if (code1 == gs_error_undefined) {
  749. num_new_glyphs++;
  750. if (num_new_glyphs > cfdata->glyphs_size - cfdata->num_glyphs)
  751. return 0;
  752. continue;
  753. }
  754. if (code0 < 0)
  755. return code0;
  756. if (code1 < 0)
  757. return code1;
  758. if (code2 || code == 0) {
  759. return 0;
  760. }
  761. } else {
  762. gs_glyph_data_t gdata0, gdata1;
  763. switch(cfont->FontType) {
  764. case ft_encrypted:
  765. case ft_encrypted2: {
  766. gs_font_type1 *font0 = (gs_font_type1 *)cfont;
  767. gs_font_type1 *font1 = (gs_font_type1 *)ofont;
  768. gdata0.memory = font0->memory;
  769. gdata1.memory = font1->memory;
  770. code0 = font0->data.procs.glyph_data(font0, glyph, &gdata0);
  771. code1 = font1->data.procs.glyph_data(font1, glyph, &gdata1);
  772. break;
  773. }
  774. case ft_TrueType:
  775. case ft_CID_TrueType: {
  776. gs_font_type42 *font0 = (gs_font_type42 *)cfont;
  777. gs_font_type42 *font1 = (gs_font_type42 *)ofont;
  778. uint glyph_index0 = font0->data.get_glyph_index(font0, glyph);
  779. uint glyph_index1 = font1->data.get_glyph_index(font1, glyph);
  780. gdata0.memory = font0->memory;
  781. gdata1.memory = font1->memory;
  782. code0 = font0->data.get_outline(font0, glyph_index0, &gdata0);
  783. code1 = font1->data.get_outline(font1, glyph_index1, &gdata1);
  784. break;
  785. }
  786. case ft_CID_encrypted: {
  787. gs_font_cid0 *font0 = (gs_font_cid0 *)cfont;
  788. gs_font_cid0 *font1 = (gs_font_cid0 *)ofont;
  789. int fidx0, fidx1;
  790. gdata0.memory = font0->memory;
  791. gdata1.memory = font1->memory;
  792. code0 = font0->cidata.glyph_data((gs_font_base *)font0, glyph, &gdata0, &fidx0);
  793. code1 = font1->cidata.glyph_data((gs_font_base *)font1, glyph, &gdata1, &fidx1);
  794. break;
  795. }
  796. default:
  797. return_error(gs_error_unregistered); /* unimplemented */
  798. }
  799. if (code0 < 0) {
  800. if (code1 >= 0)
  801. gs_glyph_data_free(&gdata1, "compare_glyphs");
  802. return code0;
  803. }
  804. if (code1 < 0) {
  805. if (code0 >= 0)
  806. gs_glyph_data_free(&gdata0, "compare_glyphs");
  807. return code1;
  808. }
  809. if (gdata0.bits.size != gdata1.bits.size)
  810. return 0;
  811. if (memcmp(gdata0.bits.data, gdata0.bits.data, gdata0.bits.size))
  812. return 0;
  813. gs_glyph_data_free(&gdata0, "compare_glyphs");
  814. gs_glyph_data_free(&gdata1, "compare_glyphs");
  815. }
  816. }
  817. return 1;
  818. }
  819. /* ---------------- Individual FontTypes ---------------- */
  820. /* ------ Type 1 ------ */
  821. private int
  822. copied_type1_glyph_data(gs_font_type1 * pfont, gs_glyph glyph,
  823. gs_glyph_data_t *pgd)
  824. {
  825. gs_copied_font_data_t *const cfdata = cf_data((gs_font *)pfont);
  826. gs_copied_glyph_t *pslot;
  827. int code = copied_glyph_slot(cfdata, glyph, &pslot);
  828. if (code < 0)
  829. return code;
  830. gs_glyph_data_from_string(pgd, pslot->gdata.data, pslot->gdata.size,
  831. NULL);
  832. return 0;
  833. }
  834. private int
  835. copied_type1_subr_data(gs_font_type1 * pfont, int subr_num, bool global,
  836. gs_glyph_data_t *pgd)
  837. {
  838. gs_copied_font_data_t *const cfdata = cf_data((gs_font *)pfont);
  839. const gs_subr_info_t *psi =
  840. (global ? &cfdata->global_subrs : &cfdata->subrs);
  841. if (subr_num < 0 || subr_num >= psi->count)
  842. return_error(gs_error_rangecheck);
  843. gs_glyph_data_from_string(pgd, psi->data + psi->starts[subr_num],
  844. psi->starts[subr_num + 1] -
  845. psi->starts[subr_num],
  846. NULL);
  847. return 0;
  848. }
  849. private int
  850. copied_type1_seac_data(gs_font_type1 * pfont, int ccode,
  851. gs_glyph * pglyph, gs_const_string *gstr, gs_glyph_data_t *pgd)
  852. {
  853. /*
  854. * This can only be invoked if the components have already been
  855. * copied to their proper positions, so it is simple.
  856. */
  857. gs_glyph glyph = gs_c_known_encode((gs_char)ccode, ENCODING_INDEX_STANDARD);
  858. gs_glyph glyph1;
  859. int code;
  860. if (glyph == GS_NO_GLYPH)
  861. return_error(gs_error_rangecheck);
  862. code = gs_c_glyph_name(glyph, gstr);
  863. if (code < 0)
  864. return code;
  865. code = pfont->dir->global_glyph_code(pfont->memory, gstr, &glyph1);
  866. if (code < 0)
  867. return code;
  868. if (pglyph)
  869. *pglyph = glyph1;
  870. if (pgd)
  871. return copied_type1_glyph_data(pfont, glyph1, pgd);
  872. else
  873. return 0;
  874. }
  875. private int
  876. copied_type1_push_values(void *callback_data, const fixed *values, int count)
  877. {
  878. return_error(gs_error_unregistered);
  879. }
  880. private int
  881. copied_type1_pop_value(void *callback_data, fixed *value)
  882. {
  883. return_error(gs_error_unregistered);
  884. }
  885. private int
  886. copy_font_type1(gs_font *font, gs_font *copied)
  887. {
  888. gs_font_type1 *font1 = (gs_font_type1 *)font;
  889. gs_font_type1 *copied1 = (gs_font_type1 *)copied;
  890. gs_copied_font_data_t *const cfdata = cf_data(copied);
  891. int code;
  892. cfdata->notdef = find_notdef((gs_font_base *)font);
  893. code = copied_Encoding_alloc(copied);
  894. if (code < 0)
  895. return code;
  896. if ((code = copy_subrs(font1, false, &cfdata->subrs, copied->memory)) < 0 ||
  897. (code = copy_subrs(font1, true, &cfdata->global_subrs, copied->memory)) < 0
  898. ) {
  899. gs_free_object(copied->memory, cfdata->Encoding,
  900. "copy_font_type1(Encoding)");
  901. return code;
  902. }
  903. /*
  904. * We don't need real push/pop procedures, because we can't do anything
  905. * useful with fonts that have non-standard OtherSubrs anyway.
  906. */
  907. copied1->data.procs.glyph_data = copied_type1_glyph_data;
  908. copied1->data.procs.subr_data = copied_type1_subr_data;
  909. copied1->data.procs.seac_data = copied_type1_seac_data;
  910. copied1->data.procs.push_values = copied_type1_push_values;
  911. copied1->data.procs.pop_value = copied_type1_pop_value;
  912. copied1->data.proc_data = 0;
  913. return 0;
  914. }
  915. private int
  916. copy_glyph_type1(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
  917. {
  918. gs_glyph_data_t gdata;
  919. gs_font_type1 *font1 = (gs_font_type1 *)font;
  920. int code;
  921. int rcode;
  922. gdata.memory = font->memory;
  923. code = font1->data.procs.glyph_data(font1, glyph, &gdata);
  924. if (code < 0)
  925. return code;
  926. code = copy_glyph_data(font, glyph, copied, options, &gdata, NULL, 0);
  927. if (code < 0)
  928. return code;
  929. rcode = code;
  930. if (code == 0)
  931. code = copy_glyph_name(font, glyph, copied, glyph);
  932. return (code < 0 ? code : rcode);
  933. }
  934. private int
  935. copied_type1_glyph_outline(gs_font *font, int WMode, gs_glyph glyph,
  936. const gs_matrix *pmat, gx_path *ppath, double sbw[4])
  937. { /*
  938. * 'WMode' may be inherited from an upper font.
  939. * We ignore in because Type 1,2 charstrings do not depend on it.
  940. */
  941. /*
  942. * This code duplicates much of zcharstring_outline in zchar1.c.
  943. * This is unfortunate, but we don't see a simple way to refactor the
  944. * code to avoid it.
  945. */
  946. gs_glyph_data_t gdata;
  947. gs_font_type1 *const pfont1 = (gs_font_type1 *)font;
  948. int code;
  949. const gs_glyph_data_t *pgd = &gdata;
  950. gs_type1_state cis;
  951. gs_imager_state gis;
  952. gdata.memory = pfont1->memory;
  953. code = pfont1->data.procs.glyph_data(pfont1, glyph, &gdata);
  954. if (code < 0)
  955. return code;
  956. if (pgd->bits.size <= max(pfont1->data.lenIV, 0))
  957. return_error(gs_error_invalidfont);
  958. /* Initialize just enough of the imager state. */
  959. if (pmat)
  960. gs_matrix_fixed_from_matrix(&gis.ctm, pmat);
  961. else {
  962. gs_matrix imat;
  963. gs_make_identity(&imat);
  964. gs_matrix_fixed_from_matrix(&gis.ctm, &imat);
  965. }
  966. gis.flatness = 0;
  967. code = gs_type1_interp_init(&cis, &gis, ppath, NULL, NULL, true, 0,
  968. pfont1);
  969. if (code < 0)
  970. return code;
  971. cis.no_grid_fitting = true;
  972. /* Continue interpreting. */
  973. for (;;) {
  974. int value;
  975. code = pfont1->data.interpret(&cis, pgd, &value);
  976. switch (code) {
  977. case 0: /* all done */
  978. /* falls through */
  979. default: /* code < 0, error */
  980. return code;
  981. case type1_result_callothersubr: /* unknown OtherSubr */
  982. return_error(gs_error_rangecheck); /* can't handle it */
  983. case type1_result_sbw: /* [h]sbw, just continue */
  984. pgd = 0;
  985. type1_cis_get_metrics(&cis, sbw);
  986. }
  987. }
  988. }
  989. private const gs_copied_font_procs_t copied_procs_type1 = {
  990. copy_font_type1, copy_glyph_type1, copied_char_add_encoding,
  991. named_glyph_slot_hashed,
  992. copied_encode_char, gs_type1_glyph_info, copied_type1_glyph_outline
  993. };
  994. private bool
  995. same_type1_subrs(const gs_font_type1 *cfont, const gs_font_type1 *ofont,
  996. bool global)
  997. {
  998. gs_glyph_data_t gdata0, gdata1;
  999. int i, code = 0;
  1000. bool exit = false;
  1001. gdata0.memory = cfont->memory;
  1002. gdata1.memory = ofont->memory;
  1003. /* Scan the font to determine the size of the subrs. */
  1004. for (i = 0; !exit; i++) {
  1005. int code0 = cfont->data.procs.subr_data((gs_font_type1 *)cfont,
  1006. i, global, &gdata0);
  1007. int code1 = ofont->data.procs.subr_data((gs_font_type1 *)ofont,
  1008. i, global, &gdata1);
  1009. bool missing0, missing1;
  1010. if (code0 == gs_error_rangecheck && code1 == gs_error_rangecheck)
  1011. return 1; /* Both arrays exceeded. */
  1012. /* Some fonts use null for skiping elements in subrs array.
  1013. This gives typecheck.
  1014. */
  1015. missing0 = (code0 == gs_error_rangecheck || code0 == gs_error_typecheck);
  1016. missing1 = (code1 == gs_error_rangecheck || code1 == gs_error_typecheck);
  1017. if (missing0 && missing1)
  1018. continue;
  1019. if (missing0 && !missing1)
  1020. return 0; /* The copy has insufficient subrs. */
  1021. if (missing1)
  1022. continue;
  1023. if (code0 < 0)
  1024. code = code0, exit = true;
  1025. else if (code1 < 0)
  1026. code = code1, exit = true;
  1027. else if (gdata0.bits.size != gdata1.bits.size)
  1028. exit = true;
  1029. else if (memcmp(gdata0.bits.data, gdata1.bits.data, gdata0.bits.size))
  1030. exit = true;
  1031. if (code0 > 0)
  1032. gs_glyph_data_free(&gdata0, "same_type1_subrs");
  1033. if (code1 > 0)
  1034. gs_glyph_data_free(&gdata1, "same_type1_subrs");
  1035. }
  1036. return code;
  1037. }
  1038. private bool
  1039. same_type1_hinting(const gs_font_type1 *cfont, const gs_font_type1 *ofont)
  1040. {
  1041. const gs_type1_data *d0 = &cfont->data, *d1 = &ofont->data;
  1042. if (d0->lenIV != d1->lenIV)
  1043. return false;
  1044. /*
  1045. if (d0->defaultWidthX != d1->defaultWidthX)
  1046. return false;
  1047. if (d0->nominalWidthX != d1->nominalWidthX)
  1048. return false;
  1049. */
  1050. if (d0->BlueFuzz != d1->BlueFuzz)
  1051. return false;
  1052. if (d0->BlueScale != d1->BlueScale)
  1053. return false;
  1054. if (d0->BlueShift != d1->BlueShift)
  1055. return false;
  1056. if (d0->ExpansionFactor != d1->ExpansionFactor)
  1057. return false;
  1058. if (d0->ForceBold != d1->ForceBold)
  1059. return false;
  1060. if (!compare_tables(d0->FamilyBlues, d1->FamilyBlues))
  1061. return false;
  1062. if (!compare_tables(d0->FamilyOtherBlues, d1->FamilyOtherBlues))
  1063. return false;
  1064. if (d0->LanguageGroup != d1->LanguageGroup)
  1065. return false;
  1066. if (!compare_tables(d0->OtherBlues, d1->OtherBlues))
  1067. return false;
  1068. if (d0->RndStemUp != d1->RndStemUp)
  1069. return false;
  1070. if (!compare_tables(d0->StdHW, d1->StdHW))
  1071. return false;
  1072. if (!compare_tables(d0->StemSnapH, d1->StemSnapH))
  1073. return false;
  1074. if (!compare_tables(d0->StemSnapV, d1->StemSnapV))
  1075. return false;
  1076. if (!compare_tables(d0->WeightVector, d1->WeightVector))
  1077. return false;
  1078. if (!same_type1_subrs(cfont, ofont, false))
  1079. return false;
  1080. if (!same_type1_subrs(cfont, ofont, true))
  1081. return false;
  1082. /*
  1083. * We ignore differences in OtherSubrs because pdfwrite
  1084. * must build without PS interpreter and therefore copied font
  1085. * have no storage for them.
  1086. */
  1087. return true;
  1088. }
  1089. /* ------ Type 42 ------ */
  1090. private int
  1091. copied_type42_string_proc(gs_font_type42 *font, ulong offset, uint len,
  1092. const byte **pstr)
  1093. {
  1094. gs_copied_font_data_t *const cfdata = font->data.proc_data;
  1095. if (offset + len > cfdata->data_size)
  1096. return_error(gs_error_rangecheck);
  1097. *pstr = cfdata->data + offset;
  1098. return 0;
  1099. }
  1100. private int
  1101. copied_type42_get_outline(gs_font_type42 *font, uint glyph_index,
  1102. gs_glyph_data_t *pgd)
  1103. {
  1104. gs_copied_font_data_t *const cfdata = font->data.proc_data;
  1105. gs_copied_glyph_t *pcg;
  1106. if (glyph_index >= cfdata->glyphs_size)
  1107. return_error(gs_error_rangecheck);
  1108. pcg = &cfdata->glyphs[glyph_index];
  1109. if (!pcg->used)
  1110. gs_glyph_data_from_null(pgd);
  1111. else
  1112. gs_glyph_data_from_string(pgd, pcg->gdata.data, pcg->gdata.size, NULL);
  1113. return 0;
  1114. }
  1115. private int
  1116. copied_type42_get_metrics(gs_font_type42 * pfont, uint glyph_index,
  1117. int wmode, float sbw[4])
  1118. {
  1119. /* Check whether we have metrics for this (glyph,wmode) pair. */
  1120. gs_copied_font_data_t *const cfdata = pfont->data.proc_data;
  1121. gs_copied_glyph_t *pcg;
  1122. if (glyph_index >= cfdata->glyphs_size)
  1123. return_error(gs_error_rangecheck);
  1124. pcg = &cfdata->glyphs[glyph_index];
  1125. if (!(pcg->used & (HAS_SBW0 << wmode)))
  1126. return_error(gs_error_undefined);
  1127. return gs_type42_default_get_metrics(pfont, glyph_index, wmode, sbw);
  1128. }
  1129. private uint
  1130. copied_type42_get_glyph_index(gs_font_type42 *font, gs_glyph glyph)
  1131. {
  1132. gs_copied_font_data_t *const cfdata = cf_data((gs_font *)font);
  1133. gs_copied_glyph_t *pcg;
  1134. int code = copied_glyph_slot(cfdata, glyph, &pcg);
  1135. if (code < 0)
  1136. return GS_NO_GLYPH;
  1137. return pcg - cfdata->glyphs;
  1138. }
  1139. private int
  1140. copy_font_type42(gs_font *font, gs_font *copied)
  1141. {
  1142. gs_font_type42 *const font42 = (gs_font_type42 *)font;
  1143. gs_font_type42 *const copied42 = (gs_font_type42 *)copied;
  1144. gs_copied_font_data_t *const cfdata = cf_data(copied);
  1145. /*
  1146. * We "write" the font, aside from the glyphs, into an in-memory
  1147. * structure, and access it from there.
  1148. * We allocate room at the end of the copied data for fake hmtx/vmtx.
  1149. */
  1150. uint extra = font42->data.trueNumGlyphs * 8;
  1151. stream fs;
  1152. int code;
  1153. cfdata->notdef = find_notdef((gs_font_base *)font);
  1154. code = copied_Encoding_alloc(copied);
  1155. if (code < 0)
  1156. return code;
  1157. s_init(&fs, font->memory);
  1158. swrite_position_only(&fs);
  1159. code = (font->FontType == ft_TrueType ? psf_write_truetype_stripped(&fs, font42)
  1160. : psf_write_cid2_stripped(&fs, (gs_font_cid2 *)font42));
  1161. code = copied_data_alloc(copied, &fs, extra, code);
  1162. if (code < 0)
  1163. goto fail;
  1164. if (font->FontType == ft_TrueType)
  1165. psf_write_truetype_stripped(&fs, font42);
  1166. else
  1167. psf_write_cid2_stripped(&fs, (gs_font_cid2 *)font42);
  1168. copied42->data.string_proc = copied_type42_string_proc;
  1169. copied42->data.proc_data = cfdata;
  1170. code = gs_type42_font_init(copied42);
  1171. if (code < 0)
  1172. goto fail2;
  1173. /* gs_type42_font_init overwrites enumerate_glyph. */
  1174. copied42->procs.enumerate_glyph = copied_enumerate_glyph;
  1175. copied42->data.get_glyph_index = copied_type42_get_glyph_index;
  1176. copied42->data.get_outline = copied_type42_get_outline;
  1177. copied42->data.get_metrics = copied_type42_get_metrics;
  1178. copied42->data.metrics[0].numMetrics =
  1179. copied42->data.metrics[1].numMetrics =
  1180. extra / 8;
  1181. copied42->data.metrics[0].offset = cfdata->data_size - extra;
  1182. copied42->data.metrics[1].offset = cfdata->data_size - extra / 2;
  1183. copied42->data.metrics[0].length =
  1184. copied42->data.metrics[1].length =
  1185. extra / 2;
  1186. memset(cfdata->data + cfdata->data_size - extra, 0, extra);
  1187. copied42->data.numGlyphs = font42->data.numGlyphs;
  1188. copied42->data.trueNumGlyphs = font42->data.trueNumGlyphs;
  1189. return 0;
  1190. fail2:
  1191. gs_free_object(copied->memory, cfdata->data,
  1192. "copy_font_type42(data)");
  1193. fail:
  1194. gs_free_object(copied->memory, cfdata->Encoding,
  1195. "copy_font_type42(Encoding)");
  1196. return code;
  1197. }
  1198. private int
  1199. copy_glyph_type42(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
  1200. {
  1201. gs_glyph_data_t gdata;
  1202. gs_font_type42 *font42 = (gs_font_type42 *)font;
  1203. gs_font_cid2 *fontCID2 = (gs_font_cid2 *)font;
  1204. gs_font_type42 *const copied42 = (gs_font_type42 *)copied;
  1205. uint gid = (options & COPY_GLYPH_BY_INDEX ? glyph - GS_MIN_GLYPH_INDEX :
  1206. font->FontType == ft_CID_TrueType
  1207. ? fontCID2->cidata.CIDMap_proc(fontCID2, glyph)
  1208. : font42->data.get_glyph_index(font42, glyph));
  1209. int code;
  1210. int rcode;
  1211. gs_copied_font_data_t *const cfdata = cf_data(copied);
  1212. gs_copied_glyph_t *pcg;
  1213. float sbw[4];
  1214. double factor = font42->data.unitsPerEm;
  1215. int i;
  1216. gdata.memory = font42->memory;
  1217. code = font42->data.get_outline(font42, gid, &gdata);
  1218. if (code < 0)
  1219. return code;
  1220. code = copy_glyph_data(font, gid + GS_MIN_GLYPH_INDEX, copied, options,
  1221. &gdata, NULL, 0);
  1222. if (code < 0)
  1223. return code;
  1224. rcode = code;
  1225. if (glyph < GS_MIN_CID_GLYPH)
  1226. code = copy_glyph_name(font, glyph, copied,
  1227. gid + GS_MIN_GLYPH_INDEX);
  1228. DISCARD(copied_glyph_slot(cfdata, gid + GS_MIN_GLYPH_INDEX, &pcg)); /* can't fail */
  1229. for (i = 0; i < 2; ++i) {
  1230. if (font42->data.get_metrics(font42, gid, i, sbw) >= 0) {
  1231. int sb = (int)(sbw[i] * factor + 0.5);
  1232. uint width = (uint)(sbw[2 + i] * factor + 0.5);
  1233. byte *pmetrics =
  1234. cfdata->data + copied42->data.metrics[i].offset + gid * 4;
  1235. pmetrics[0] = (byte)(width >> 8);
  1236. pmetrics[1] = (byte)width;
  1237. pmetrics[2] = (byte)(sb >> 8);
  1238. pmetrics[3] = (byte)sb;
  1239. pcg->used |= HAS_SBW0 << i;
  1240. }
  1241. factor = -factor; /* values are negated for WMode = 1 */
  1242. }
  1243. return (code < 0 ? code : rcode);
  1244. }
  1245. private gs_glyph
  1246. copied_type42_encode_char(gs_font *copied, gs_char chr,
  1247. gs_glyph_space_t glyph_space)
  1248. {
  1249. gs_copied_font_data_t *const cfdata = cf_data(copied);
  1250. const gs_glyph *Encoding = cfdata->Encoding;
  1251. gs_glyph glyph;
  1252. if (chr >= 256 || Encoding == 0)
  1253. return GS_NO_GLYPH;
  1254. glyph = Encoding[chr];
  1255. if (glyph_space == GLYPH_SPACE_INDEX) {
  1256. /* Search linearly for the glyph by name. */
  1257. gs_copied_glyph_t *pcg;
  1258. int code = named_glyph_slot_linear(cfdata, glyph, &pcg);
  1259. if (code < 0 || !pcg->used)
  1260. return GS_NO_GLYPH;
  1261. return GS_MIN_GLYPH_INDEX + (pcg - cfdata->glyphs);
  1262. }
  1263. return glyph;
  1264. }
  1265. private const gs_copied_font_procs_t copied_procs_type42 = {
  1266. copy_font_type42, copy_glyph_type42, copied_char_add_encoding,
  1267. named_glyph_slot_linear,
  1268. copied_type42_encode_char, gs_type42_glyph_info, gs_type42_glyph_outline
  1269. };
  1270. private inline int
  1271. access_type42_data(gs_font_type42 *pfont, ulong base, ulong length,
  1272. const byte **vptr)
  1273. {
  1274. /* See ACCESS macro in gstype42.c */
  1275. return pfont->data.string_proc(pfont, base, length, vptr);
  1276. }
  1277. private inline uint
  1278. U16(const byte *p)
  1279. {
  1280. return ((uint)p[0] << 8) + p[1];
  1281. }
  1282. private int
  1283. same_type42_hinting(gs_font_type42 *font0, gs_font_type42 *font1)
  1284. {
  1285. gs_type42_data *d0 = &font0->data, *d1 = &font1->data;
  1286. gs_font_type42 *font[2];
  1287. uint pos[2][3];
  1288. uint len[2][3] = {{0,0,0}, {0,0,0}};
  1289. int i, j, code;
  1290. if (d0->unitsPerEm != d1->unitsPerEm)
  1291. return 0;
  1292. font[0] = font0;
  1293. font[1] = font1;
  1294. memset(pos, 0, sizeof(pos));
  1295. for (j = 0; j < 2; j++) {
  1296. const byte *OffsetTable;
  1297. uint numTables;
  1298. code = access_type42_data(font[j], 0, 12, &OffsetTable);
  1299. if (code < 0)
  1300. return code;
  1301. numTables = U16(OffsetTable + 4);
  1302. for (i = 0; i < numTables; ++i) {
  1303. const byte *tab;
  1304. ulong start;
  1305. uint length;
  1306. code = access_type42_data(font[j], 12 + i * 16, 16, &tab);
  1307. if (code < 0)
  1308. return code;
  1309. start = get_u32_msb(tab + 8);
  1310. length = get_u32_msb(tab + 12);
  1311. if (!memcmp("prep", tab, 4))
  1312. pos[j][0] = start, len[j][0] = length;
  1313. else if (!memcmp("cvt ", tab, 4))
  1314. pos[j][1] = start, len[j][1] = length;
  1315. else if (!memcmp("fpgm", tab, 4))
  1316. pos[j][2] = start, len[j][2] = length;
  1317. }
  1318. }
  1319. for (i = 0; i < 3; i++) {
  1320. if (len[0][i] != len[1][i])
  1321. return 0;
  1322. }
  1323. for (i = 0; i < 3; i++) {
  1324. if (len[0][i] != 0) {
  1325. const byte *data0, *data1;
  1326. code = access_type42_data(font0, pos[0][i], len[0][i], &data0);
  1327. if (code < 0)
  1328. return code;
  1329. code = access_type42_data(font1, pos[1][i], len[1][i], &data1);
  1330. if (code < 0)
  1331. return code;
  1332. if (memcmp(data0, data1, len[1][i]))
  1333. return 0;
  1334. }
  1335. }
  1336. return 1;
  1337. }
  1338. #undef ACCESS
  1339. /* ------ CIDFont shared ------ */
  1340. private int
  1341. copy_font_cid_common(gs_font *font, gs_font *copied, gs_font_cid_data *pcdata)
  1342. {
  1343. return (copy_string(copied->memory, &pcdata->CIDSystemInfo.Registry,
  1344. "Registry") |
  1345. copy_string(copied->memory, &pcdata->CIDSystemInfo.Ordering,
  1346. "Ordering"));
  1347. }
  1348. /* ------ CIDFontType 0 ------ */
  1349. private int
  1350. copied_cid0_glyph_data(gs_font_base *font, gs_glyph glyph,
  1351. gs_glyph_data_t *pgd, int *pfidx)
  1352. {
  1353. gs_font_cid0 *fcid0 = (gs_font_cid0 *)font;
  1354. gs_copied_font_data_t *const cfdata = cf_data((gs_font *)font);
  1355. gs_copied_glyph_t *pcg;
  1356. int code = copied_glyph_slot(cfdata, glyph, &pcg);
  1357. int fdbytes = fcid0->cidata.FDBytes;
  1358. int i;
  1359. if (pfidx)
  1360. *pfidx = 0;
  1361. if (code < 0) {
  1362. if (pgd)
  1363. gs_glyph_data_from_null(pgd);
  1364. return_error(gs_error_undefined);
  1365. }
  1366. if (pfidx)
  1367. for (i = 0; i < fdbytes; ++i)
  1368. *pfidx = (*pfidx << 8) + pcg->gdata.data[i];
  1369. if (pgd)
  1370. gs_glyph_data_from_string(pgd, pcg->gdata.data + fdbytes,
  1371. pcg->gdata.size - fdbytes, NULL);
  1372. return 0;
  1373. }
  1374. private int
  1375. copied_sub_type1_glyph_data(gs_font_type1 * pfont, gs_glyph glyph,
  1376. gs_glyph_data_t *pgd)
  1377. {
  1378. return
  1379. copied_cid0_glyph_data((gs_font_base *)cf_data((gs_font *)pfont)->parent,
  1380. glyph, pgd, NULL);
  1381. }
  1382. private int
  1383. cid0_subfont(gs_font *copied, gs_glyph glyph, gs_font_type1 **pfont1)
  1384. {
  1385. int fidx;
  1386. int code = copied_cid0_glyph_data((gs_font_base *)copied, glyph, NULL,
  1387. &fidx);
  1388. if (code >= 0) {
  1389. gs_font_cid0 *font0 = (gs_font_cid0 *)copied;
  1390. if (fidx >= font0->cidata.FDArray_size)
  1391. return_error(gs_error_unregistered); /* Must not happen. */
  1392. *pfont1 = font0->cidata.FDArray[fidx];
  1393. }
  1394. return code;
  1395. }
  1396. private int
  1397. copied_cid0_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  1398. int members, gs_glyph_info_t *info)
  1399. {
  1400. gs_font_type1 *subfont1;
  1401. int code = cid0_subfont(font, glyph, &subfont1);
  1402. if (code < 0)
  1403. return code;
  1404. if (members & GLYPH_INFO_WIDTH1) {
  1405. /* Hack : There is no way to pass WMode from font to glyph_info,
  1406. * and usually CID font has no metrics for WMode 1.
  1407. * Therefore we use FontBBox as default size.
  1408. * Warning : this incompletely implements the request :
  1409. * other requested members are not retrieved.
  1410. */
  1411. gs_font_info_t finfo;
  1412. int code = subfont1->procs.font_info(font, NULL, FONT_INFO_BBOX, &finfo);
  1413. if (code < 0)
  1414. return code;
  1415. info->width[1].x = 0;
  1416. info->width[1].y = -finfo.BBox.q.x; /* Sic! */
  1417. info->v.x = finfo.BBox.q.x / 2;
  1418. info->v.y = finfo.BBox.q.y;
  1419. info->members = GLYPH_INFO_WIDTH1;
  1420. return 0;
  1421. }
  1422. return subfont1->procs.glyph_info((gs_font *)subfont1, glyph, pmat,
  1423. members, info);
  1424. }
  1425. private int
  1426. copied_cid0_glyph_outline(gs_font *font, int WMode, gs_glyph glyph,
  1427. const gs_matrix *pmat, gx_path *ppath, double sbw[4])
  1428. {
  1429. gs_font_type1 *subfont1;
  1430. int code = cid0_subfont(font, glyph, &subfont1);
  1431. if (code < 0)
  1432. return code;
  1433. return subfont1->procs.glyph_outline((gs_font *)subfont1, WMode, glyph, pmat,
  1434. ppath, sbw);
  1435. }
  1436. private int
  1437. copy_font_cid0(gs_font *font, gs_font *copied)
  1438. {
  1439. gs_font_cid0 *copied0 = (gs_font_cid0 *)copied;
  1440. gs_copied_font_data_t *const cfdata = cf_data(copied);
  1441. gs_font_type1 **FDArray =
  1442. gs_alloc_struct_array(copied->memory, copied0->cidata.FDArray_size,
  1443. gs_font_type1 *,
  1444. &st_gs_font_type1_ptr_element, "FDArray");
  1445. int i = 0, code;
  1446. if (FDArray == 0)
  1447. return_error(gs_error_VMerror);
  1448. code = copy_font_cid_common(font, copied, &copied0->cidata.common);
  1449. if (code < 0)
  1450. goto fail;
  1451. for (; i < copied0->cidata.FDArray_size; ++i) {
  1452. gs_font *subfont = (gs_font *)copied0->cidata.FDArray[i];
  1453. gs_font_type1 *subfont1 = (gs_font_type1 *)subfont;
  1454. gs_font *subcopy;
  1455. gs_font_type1 *subcopy1;
  1456. gs_copied_font_data_t *subdata;
  1457. if (i == 0) {
  1458. /* copy_subrs requires a Type 1 font, even for GSubrs. */
  1459. code = copy_subrs(subfont1, true, &cfdata->global_subrs,
  1460. copied->memory);
  1461. if (code < 0)
  1462. goto fail;
  1463. }
  1464. code = gs_copy_font(subfont, &subfont->FontMatrix, copied->memory, &subcopy);
  1465. if (code < 0)
  1466. goto fail;
  1467. subcopy1 = (gs_font_type1 *)subcopy;
  1468. subcopy1->data.parent = NULL;
  1469. subdata = cf_data(subcopy);
  1470. subdata->parent = copied0;
  1471. gs_free_object(copied->memory, subdata->Encoding,
  1472. "copy_font_cid0(Encoding)");
  1473. subdata->Encoding = 0;
  1474. /*
  1475. * Share the glyph data and global_subrs with the parent. This
  1476. * allows copied_type1_glyph_data in the subfont to do the right
  1477. * thing.
  1478. */
  1479. gs_free_object(copied->memory, subdata->names,
  1480. "copy_font_cid0(subfont names)");
  1481. gs_free_object(copied->memory, subdata->glyphs,
  1482. "copy_font_cid0(subfont glyphs)");
  1483. subcopy1->data.procs.glyph_data = copied_sub_type1_glyph_data;
  1484. subdata->glyphs = cfdata->glyphs;
  1485. subdata->glyphs_size = cfdata->glyphs_size;
  1486. subdata->names = 0;
  1487. subdata->global_subrs = cfdata->global_subrs;
  1488. FDArray[i] = subcopy1;
  1489. }
  1490. cfdata->notdef = GS_MIN_CID_GLYPH;
  1491. copied0->cidata.FDArray = FDArray;
  1492. copied0->cidata.FDBytes =
  1493. (copied0->cidata.FDArray_size <= 1 ? 0 :
  1494. copied0->cidata.FDArray_size <= 256 ? 1 : 2);
  1495. copied0->cidata.glyph_data = copied_cid0_glyph_data;
  1496. return 0;
  1497. fail:
  1498. while (--i >= 0)
  1499. gs_free_object(copied->memory, FDArray[i], "copy_font_cid0(subfont)");
  1500. gs_free_object(copied->memory, FDArray, "FDArray");
  1501. return code;
  1502. }
  1503. private int
  1504. copy_glyph_cid0(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
  1505. {
  1506. gs_glyph_data_t gdata;
  1507. gs_font_cid0 *fcid0 = (gs_font_cid0 *)font;
  1508. gs_font_cid0 *copied0 = (gs_font_cid0 *)copied;
  1509. int fdbytes = copied0->cidata.FDBytes;
  1510. int fidx;
  1511. int code;
  1512. byte prefix[MAX_FDBytes];
  1513. int i;
  1514. gdata.memory = font->memory;
  1515. code = fcid0->cidata.glyph_data((gs_font_base *)font, glyph,
  1516. &gdata, &fidx);
  1517. if (code < 0)
  1518. return code;
  1519. for (i = fdbytes - 1; i >= 0; --i, fidx >>= 8)
  1520. prefix[i] = (byte)fidx;
  1521. if (fidx != 0)
  1522. return_error(gs_error_rangecheck);
  1523. return copy_glyph_data(font, glyph, copied, options, &gdata, prefix, fdbytes);
  1524. }
  1525. private const gs_copied_font_procs_t copied_procs_cid0 = {
  1526. copy_font_cid0, copy_glyph_cid0, copied_no_add_encoding,
  1527. named_glyph_slot_none,
  1528. gs_no_encode_char, copied_cid0_glyph_info, copied_cid0_glyph_outline
  1529. };
  1530. private int
  1531. same_cid0_hinting(const gs_font_cid0 *cfont, const gs_font_cid0 *ofont)
  1532. {
  1533. int i;
  1534. if (cfont->cidata.FDArray_size != ofont->cidata.FDArray_size)
  1535. return 0;
  1536. for (i = 0; i < cfont->cidata.FDArray_size; i++) {
  1537. gs_font_type1 *subfont0 = cfont->cidata.FDArray[i];
  1538. gs_font_type1 *subfont1 = ofont->cidata.FDArray[i];
  1539. if (!same_type1_hinting(subfont0, subfont1))
  1540. return 0;
  1541. }
  1542. return 1;
  1543. }
  1544. /* ------ CIDFontType 2 ------ */
  1545. private int
  1546. copied_cid2_CIDMap_proc(gs_font_cid2 *fcid2, gs_glyph glyph)
  1547. {
  1548. uint cid = glyph - GS_MIN_CID_GLYPH;
  1549. gs_copied_font_data_t *const cfdata = cf_data((gs_font *)fcid2);
  1550. const ushort *CIDMap = cfdata->CIDMap;
  1551. if (glyph < GS_MIN_CID_GLYPH || cid >= fcid2->cidata.common.CIDCount)
  1552. return_error(gs_error_rangecheck);
  1553. if (CIDMap[cid] == 0xffff)
  1554. return -1;
  1555. return CIDMap[cid];
  1556. }
  1557. private uint
  1558. copied_cid2_get_glyph_index(gs_font_type42 *font, gs_glyph glyph)
  1559. {
  1560. int glyph_index = copied_cid2_CIDMap_proc((gs_font_cid2 *)font, glyph);
  1561. if (glyph_index < 0)
  1562. return GS_NO_GLYPH;
  1563. return glyph_index;
  1564. }
  1565. private int
  1566. copy_font_cid2(gs_font *font, gs_font *copied)
  1567. {
  1568. gs_font_cid2 *copied2 = (gs_font_cid2 *)copied;
  1569. gs_copied_font_data_t *const cfdata = cf_data(copied);
  1570. int code;
  1571. int CIDCount = copied2->cidata.common.CIDCount;
  1572. ushort *CIDMap = (ushort *)
  1573. gs_alloc_byte_array(copied->memory, CIDCount, sizeof(ushort),
  1574. "copy_font_cid2(CIDMap");
  1575. if (CIDMap == 0)
  1576. return_error(gs_error_VMerror);
  1577. code = copy_font_cid_common(font, copied, &copied2->cidata.common);
  1578. if (code < 0 ||
  1579. (code = copy_font_type42(font, copied)) < 0
  1580. ) {
  1581. gs_free_object(copied->memory, CIDMap, "copy_font_cid2(CIDMap");
  1582. return code;
  1583. }
  1584. cfdata->notdef = GS_MIN_CID_GLYPH;
  1585. memset(CIDMap, 0xff, CIDCount * sizeof(*CIDMap));
  1586. cfdata->CIDMap = CIDMap;
  1587. copied2->cidata.MetricsCount = 0;
  1588. copied2->cidata.CIDMap_proc = copied_cid2_CIDMap_proc;
  1589. {
  1590. gs_font_type42 *const copied42 = (gs_font_type42 *)copied;
  1591. copied42->data.get_glyph_index = copied_cid2_get_glyph_index;
  1592. }
  1593. return 0;
  1594. }
  1595. private int expand_CIDMap(gs_font_cid2 *copied2, uint CIDCount)
  1596. {
  1597. ushort *CIDMap;
  1598. gs_copied_font_data_t *const cfdata = cf_data((gs_font *)copied2);
  1599. if (CIDCount <= copied2->cidata.common.CIDCount)
  1600. return 0;
  1601. CIDMap = (ushort *)
  1602. gs_alloc_byte_array(copied2->memory, CIDCount, sizeof(ushort),
  1603. "copy_font_cid2(CIDMap");
  1604. if (CIDMap == 0)
  1605. return_error(gs_error_VMerror);
  1606. memcpy(CIDMap, cfdata->CIDMap, copied2->cidata.common.CIDCount * sizeof(*CIDMap));
  1607. memset(CIDMap + copied2->cidata.common.CIDCount, 0xFF,
  1608. (CIDCount - copied2->cidata.common.CIDCount) * sizeof(*CIDMap));
  1609. cfdata->CIDMap = CIDMap;
  1610. copied2->cidata.common.CIDCount = CIDCount;
  1611. return 0;
  1612. }
  1613. private int
  1614. copy_glyph_cid2(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
  1615. {
  1616. gs_font_cid2 *fcid2 = (gs_font_cid2 *)font;
  1617. gs_copied_font_data_t *const cfdata = cf_data(copied);
  1618. gs_font_cid2 *copied2 = (gs_font_cid2 *)copied;
  1619. int gid;
  1620. int code;
  1621. if (!(options & COPY_GLYPH_BY_INDEX)) {
  1622. uint cid = glyph - GS_MIN_CID_GLYPH;
  1623. int CIDCount;
  1624. code = expand_CIDMap(copied2, cid + 1);
  1625. if (code < 0)
  1626. return code;
  1627. CIDCount = copied2->cidata.common.CIDCount;
  1628. gid = fcid2->cidata.CIDMap_proc(fcid2, glyph);
  1629. if (gid < 0 || gid >= cfdata->glyphs_size)
  1630. return_error(gs_error_rangecheck);
  1631. if (cid > CIDCount)
  1632. return_error(gs_error_invalidaccess);
  1633. if (cfdata->CIDMap[cid] != 0xffff && cfdata->CIDMap[cid] != gid)
  1634. return_error(gs_error_invalidaccess);
  1635. code = copy_glyph_type42(font, glyph, copied, options);
  1636. if (code < 0)
  1637. return code;
  1638. cfdata->CIDMap[cid] = gid;
  1639. } else {
  1640. gid = glyph - GS_MIN_GLYPH_INDEX;
  1641. if (gid < 0 || gid >= cfdata->glyphs_size)
  1642. return_error(gs_error_rangecheck);
  1643. code = copy_glyph_type42(font, glyph, copied, options);
  1644. if (code < 0)
  1645. return code;
  1646. }
  1647. return code;
  1648. }
  1649. private const gs_copied_font_procs_t copied_procs_cid2 = {
  1650. copy_font_cid2, copy_glyph_cid2, copied_no_add_encoding,
  1651. named_glyph_slot_none,
  1652. gs_no_encode_char, gs_type42_glyph_info, gs_type42_glyph_outline
  1653. };
  1654. private int
  1655. same_cid2_hinting(const gs_font_cid2 *cfont, const gs_font_cid2 *ofont)
  1656. {
  1657. return same_type42_hinting((gs_font_type42 *)cfont, (gs_font_type42 *)ofont);
  1658. }
  1659. /* ---------------- Public ---------------- */
  1660. /*
  1661. * Procedure vector for copied fonts.
  1662. */
  1663. private font_proc_font_info(copied_font_info);
  1664. private font_proc_enumerate_glyph(copied_enumerate_glyph);
  1665. private const gs_font_procs copied_font_procs = {
  1666. 0, /* define_font, not supported */
  1667. 0, /* make_font, not supported */
  1668. copied_font_info,
  1669. gs_default_same_font,
  1670. 0, /* encode_char, varies by FontType */
  1671. 0, /* decode_char, not supported */
  1672. copied_enumerate_glyph,
  1673. 0, /* glyph_info, varies by FontType */
  1674. 0, /* glyph_outline, varies by FontType */
  1675. copied_glyph_name,
  1676. gs_default_init_fstack,
  1677. gs_default_next_char_glyph,
  1678. copied_build_char
  1679. };
  1680. #if GLYPHS_SIZE_IS_PRIME
  1681. private const int some_primes[] = {
  1682. /* Arbitrary choosen prime numbers, being reasonable for a Type 1|2 font size.
  1683. We start with 257 to fit 256 glyphs and .notdef .
  1684. Smaller numbers aren't useful, because we don't know whether a font
  1685. will add more glyphs incrementally when we allocate its stable copy.
  1686. */
  1687. 257, 359, 521, 769, 1031, 2053,
  1688. 3079, 4099, 5101, 6101, 7109, 8209, 10007, 12007, 14009,
  1689. 16411, 20107, 26501, 32771, 48857, 65537};
  1690. #endif
  1691. /*
  1692. * Copy a font, aside from its glyphs.
  1693. */
  1694. int
  1695. gs_copy_font(gs_font *font, const gs_matrix *orig_matrix, gs_memory_t *mem, gs_font **pfont_new)
  1696. {
  1697. gs_memory_type_ptr_t fstype = gs_object_type(font->memory, font);
  1698. uint fssize = gs_struct_type_size(fstype);
  1699. gs_font *copied = 0;
  1700. gs_copied_font_data_t *cfdata = 0;
  1701. gs_font_info_t info;
  1702. gs_copied_glyph_t *glyphs = 0;
  1703. uint glyphs_size;
  1704. gs_copied_glyph_name_t *names = 0;
  1705. bool have_names = false;
  1706. const gs_copied_font_procs_t *procs;
  1707. int code;
  1708. /*
  1709. * Check for a supported FontType, and compute the size of its
  1710. * copied glyph table.
  1711. */
  1712. switch (font->FontType) {
  1713. case ft_TrueType:
  1714. procs = &copied_procs_type42;
  1715. glyphs_size = ((gs_font_type42 *)font)->data.trueNumGlyphs;
  1716. have_names = true;
  1717. break;
  1718. case ft_encrypted:
  1719. case ft_encrypted2:
  1720. procs = &copied_procs_type1;
  1721. /* Count the glyphs. */
  1722. glyphs_size = 0;
  1723. {
  1724. int index = 0;
  1725. gs_glyph glyph;
  1726. while (font->procs.enumerate_glyph(font, &index, GLYPH_SPACE_NAME,
  1727. &glyph), index != 0)
  1728. ++glyphs_size;
  1729. }
  1730. #if GLYPHS_SIZE_IS_PRIME
  1731. /*
  1732. * Make glyphs_size a prime number to ensure termination of the loop in
  1733. * named_glyphs_slot_hashed, q.v.
  1734. * Also reserve additional slots for the case of font merging and
  1735. * for possible font increments.
  1736. */
  1737. glyphs_size = glyphs_size * 3 / 2;
  1738. if (glyphs_size < 257)
  1739. glyphs_size = 257;
  1740. { int i;
  1741. for (i = 0; i < count_of(some_primes); i++)
  1742. if (glyphs_size <= some_primes[i])
  1743. break;
  1744. if (i >= count_of(some_primes))
  1745. return_error(gs_error_rangecheck);
  1746. glyphs_size = some_primes[i];
  1747. }
  1748. #else
  1749. /*
  1750. * Make names_size a power of 2 to ensure termination of the loop in
  1751. * named_glyphs_slot_hashed, q.v.
  1752. */
  1753. glyphs_size = glyphs_size * 3 / 2;
  1754. while (glyphs_size & (glyphs_size - 1))
  1755. glyphs_size = (glyphs_size | (glyphs_size - 1)) + 1;
  1756. if (glyphs_size < 256) /* probably incremental font */
  1757. glyphs_size = 256;
  1758. #endif
  1759. have_names = true;
  1760. break;
  1761. case ft_CID_encrypted:
  1762. procs = &copied_procs_cid0;
  1763. glyphs_size = ((gs_font_cid0 *)font)->cidata.common.CIDCount;
  1764. break;
  1765. case ft_CID_TrueType:
  1766. procs = &copied_procs_cid2;
  1767. /* Glyphs are indexed by GID, not by CID. */
  1768. glyphs_size = ((gs_font_cid2 *)font)->data.trueNumGlyphs;
  1769. break;
  1770. default:
  1771. return_error(gs_error_rangecheck);
  1772. }
  1773. /* Get the font_info for copying. */
  1774. memset(&info, 0, sizeof(info));
  1775. info.Flags_requested = ~0;
  1776. code = font->procs.font_info(font, NULL, ~0, &info);
  1777. if (code < 0)
  1778. return code;
  1779. /* Allocate the generic copied information. */
  1780. glyphs = gs_alloc_struct_array(mem, glyphs_size, gs_copied_glyph_t,
  1781. &st_gs_copied_glyph_element,
  1782. "gs_copy_font(glyphs)");
  1783. if (have_names != 0)
  1784. names = gs_alloc_struct_array(mem, glyphs_size, gs_copied_glyph_name_t,
  1785. &st_gs_copied_glyph_name_element,
  1786. "gs_copy_font(names)");
  1787. copied = gs_alloc_struct(mem, gs_font, fstype,
  1788. "gs_copy_font(copied font)");
  1789. cfdata = gs_alloc_struct(mem, gs_copied_font_data_t,
  1790. &st_gs_copied_font_data,
  1791. "gs_copy_font(wrapper data)");
  1792. if (cfdata)
  1793. memset(cfdata, 0, sizeof(*cfdata));
  1794. if (glyphs == 0 || (names == 0 && have_names) || copied == 0 ||
  1795. cfdata == 0
  1796. ) {
  1797. code = gs_note_error(gs_error_VMerror);
  1798. goto fail;
  1799. }
  1800. cfdata->info = info;
  1801. cfdata->dir = font->dir;
  1802. if ((code = (copy_string(mem, &cfdata->info.Copyright,
  1803. "gs_copy_font(Copyright)") |
  1804. copy_string(mem, &cfdata->info.Notice,
  1805. "gs_copy_font(Notice)") |
  1806. copy_string(mem, &cfdata->info.FamilyName,
  1807. "gs_copy_font(FamilyName)") |
  1808. copy_string(mem, &cfdata->info.FullName,
  1809. "gs_copy_font(FullName)"))) < 0
  1810. )
  1811. goto fail;
  1812. /* Initialize the copied font. */
  1813. memcpy(copied, font, fssize);
  1814. copied->next = copied->prev = 0;
  1815. copied->memory = mem;
  1816. copied->is_resource = false;
  1817. gs_notify_init(&copied->notify_list, mem);
  1818. copied->base = copied;
  1819. copied->FontMatrix = *orig_matrix;
  1820. copied->client_data = cfdata;
  1821. copied->procs = copied_font_procs;
  1822. copied->procs.encode_char = procs->encode_char;
  1823. copied->procs.glyph_info = procs->glyph_info;
  1824. copied->procs.glyph_outline = procs->glyph_outline;
  1825. {
  1826. gs_font_base *bfont = (gs_font_base *)copied;
  1827. bfont->FAPI = 0;
  1828. bfont->FAPI_font_data = 0;
  1829. bfont->encoding_index = ENCODING_INDEX_UNKNOWN;
  1830. code = uid_copy(&bfont->UID, mem, "gs_copy_font(UID)");
  1831. if (code < 0)
  1832. goto fail;
  1833. }
  1834. cfdata->procs = procs;
  1835. memset(glyphs, 0, glyphs_size * sizeof(*glyphs));
  1836. cfdata->glyphs = glyphs;
  1837. cfdata->glyphs_size = glyphs_size;
  1838. cfdata->num_glyphs = 0;
  1839. if (names)
  1840. memset(names, 0, glyphs_size * sizeof(*names));
  1841. cfdata->names = names;
  1842. if (names != 0) {
  1843. uint i;
  1844. for (i = 0; i < glyphs_size; ++i)
  1845. names[i].glyph = GS_NO_GLYPH;
  1846. }
  1847. /* Do FontType-specific initialization. */
  1848. code = procs->finish_copy_font(font, copied);
  1849. if (code < 0)
  1850. goto fail;
  1851. *pfont_new = copied;
  1852. if (cfdata->notdef != GS_NO_GLYPH)
  1853. code = gs_copy_glyph(font, cfdata->notdef, copied);
  1854. return code;
  1855. fail:
  1856. /* Free storage and exit. */
  1857. if (cfdata) {
  1858. uncopy_string(mem, &cfdata->info.FullName,
  1859. "gs_copy_font(FullName)");
  1860. uncopy_string(mem, &cfdata->info.FamilyName,
  1861. "gs_copy_font(FamilyName)");
  1862. uncopy_string(mem, &cfdata->info.Notice,
  1863. "gs_copy_font(Notice)");
  1864. uncopy_string(mem, &cfdata->info.Copyright,
  1865. "gs_copy_font(Copyright)");
  1866. gs_free_object(mem, cfdata, "gs_copy_font(wrapper data)");
  1867. }
  1868. gs_free_object(mem, copied, "gs_copy_font(copied font)");
  1869. gs_free_object(mem, names, "gs_copy_font(names)");
  1870. gs_free_object(mem, glyphs, "gs_copy_font(glyphs)");
  1871. return code;
  1872. }
  1873. /*
  1874. * Copy a glyph, including any sub-glyphs.
  1875. */
  1876. int
  1877. gs_copy_glyph(gs_font *font, gs_glyph glyph, gs_font *copied)
  1878. {
  1879. return gs_copy_glyph_options(font, glyph, copied, 0);
  1880. }
  1881. int
  1882. gs_copy_glyph_options(gs_font *font, gs_glyph glyph, gs_font *copied,
  1883. int options)
  1884. {
  1885. int code;
  1886. #define MAX_GLYPH_PIECES 64 /* arbitrary, but 32 is too small - bug 687698. */
  1887. gs_glyph glyphs[MAX_GLYPH_PIECES];
  1888. uint count = 1, i;
  1889. if (copied->procs.font_info != copied_font_info)
  1890. return_error(gs_error_rangecheck);
  1891. code = cf_data(copied)->procs->copy_glyph(font, glyph, copied, options);
  1892. if (code != 0)
  1893. return code;
  1894. /* Copy any sub-glyphs. */
  1895. glyphs[0] = glyph;
  1896. code = psf_add_subset_pieces(glyphs, &count, MAX_GLYPH_PIECES, MAX_GLYPH_PIECES,
  1897. font);
  1898. if (code < 0)
  1899. return code;
  1900. if (count > MAX_GLYPH_PIECES)
  1901. return_error(gs_error_limitcheck);
  1902. for (i = 1; i < count; ++i) {
  1903. code = gs_copy_glyph_options(font, glyphs[i], copied,
  1904. (options & ~COPY_GLYPH_NO_OLD) | COPY_GLYPH_BY_INDEX);
  1905. if (code < 0)
  1906. return code;
  1907. }
  1908. /*
  1909. * Because 'seac' accesses the Encoding of the font as well as the
  1910. * glyphs, we have to copy the Encoding entries as well.
  1911. */
  1912. if (count == 1)
  1913. return 0;
  1914. switch (font->FontType) {
  1915. case ft_encrypted:
  1916. case ft_encrypted2:
  1917. break;
  1918. default:
  1919. return 0;
  1920. }
  1921. #if 0 /* No need to add subglyphs to the Encoding because they always are
  1922. taken from StandardEncoding (See the Type 1 spec about 'seac').
  1923. Attempt to add them to the encoding can cause a conflict,
  1924. if the encoding specifies different glyphs for these char codes
  1925. (See the bug #687172). */
  1926. {
  1927. gs_copied_glyph_t *pcg;
  1928. gs_glyph_data_t gdata;
  1929. gs_char chars[2];
  1930. gdata.memory = font->memory;
  1931. /* Since we just copied the glyph, copied_glyph_slot can't fail. */
  1932. DISCARD(copied_glyph_slot(cf_data(copied), glyph, &pcg));
  1933. gs_glyph_data_from_string(&gdata, pcg->gdata.data, pcg->gdata.size,
  1934. NULL);
  1935. code = gs_type1_piece_codes((gs_font_type1 *)font, &gdata, chars);
  1936. if (code <= 0 || /* 0 is not possible here */
  1937. (code = gs_copied_font_add_encoding(copied, chars[0], glyphs[1])) < 0 ||
  1938. (code = gs_copied_font_add_encoding(copied, chars[1], glyphs[2])) < 0
  1939. )
  1940. return code;
  1941. }
  1942. #endif
  1943. return 0;
  1944. #undef MAX_GLYPH_PIECES
  1945. }
  1946. /*
  1947. * Add an Encoding entry to a copied font. The glyph need not already have
  1948. * been copied.
  1949. */
  1950. int
  1951. gs_copied_font_add_encoding(gs_font *copied, gs_char chr, gs_glyph glyph)
  1952. {
  1953. gs_copied_font_data_t *const cfdata = cf_data(copied);
  1954. if (copied->procs.font_info != copied_font_info)
  1955. return_error(gs_error_rangecheck);
  1956. return cfdata->procs->add_encoding(copied, chr, glyph);
  1957. }
  1958. /*
  1959. * Copy all the glyphs and, if relevant, Encoding entries from a font. This
  1960. * is equivalent to copying the glyphs and Encoding entries individually,
  1961. * and returns errors under the same conditions.
  1962. */
  1963. int
  1964. gs_copy_font_complete(gs_font *font, gs_font *copied)
  1965. {
  1966. int index, code = 0;
  1967. gs_glyph_space_t space = GLYPH_SPACE_NAME;
  1968. gs_glyph glyph;
  1969. /*
  1970. * For Type 1 fonts and CIDFonts, enumerating the glyphs using
  1971. * GLYPH_SPACE_NAME will cover all the glyphs. (The "names" of glyphs
  1972. * in CIDFonts are CIDs, but that is not a problem.) For Type 42 fonts,
  1973. * however, we have to copy by name once, so that we also copy the
  1974. * name-to-GID mapping (the CharStrings dictionary in PostScript), and
  1975. * then copy again by GID, to cover glyphs that don't have names.
  1976. */
  1977. for (;;) {
  1978. for (index = 0;
  1979. code >= 0 &&
  1980. (font->procs.enumerate_glyph(font, &index, space, &glyph),
  1981. index != 0);
  1982. )
  1983. code = gs_copy_glyph(font, glyph, copied);
  1984. /* For Type 42 fonts, if we copied by name, now copy again by index. */
  1985. if (space == GLYPH_SPACE_NAME && font->FontType == ft_TrueType)
  1986. space = GLYPH_SPACE_INDEX;
  1987. else
  1988. break;
  1989. }
  1990. if (cf_data(copied)->Encoding != 0)
  1991. for (index = 0; code >= 0 && index < 256; ++index) {
  1992. glyph = font->procs.encode_char(font, (gs_char)index,
  1993. GLYPH_SPACE_NAME);
  1994. if (glyph != GS_NO_GLYPH)
  1995. code = gs_copied_font_add_encoding(copied, (gs_char)index,
  1996. glyph);
  1997. }
  1998. if (copied->FontType != ft_composite) {
  1999. gs_font_base *bfont = (gs_font_base *)font;
  2000. gs_font_base *bcopied = (gs_font_base *)copied;
  2001. bcopied->encoding_index = bfont->encoding_index;
  2002. bcopied->nearest_encoding_index = bfont->nearest_encoding_index;
  2003. }
  2004. return code;
  2005. }
  2006. /*
  2007. * Check whether specified glyphs can be copied from another font.
  2008. * It means that (1) fonts have same hinting parameters and
  2009. * (2) font subsets for the specified glyph set don't include different
  2010. * outlines or metrics. Possible returned values :
  2011. * 0 (incompatible), 1 (compatible), < 0 (error)
  2012. */
  2013. int
  2014. gs_copied_can_copy_glyphs(const gs_font *cfont, const gs_font *ofont,
  2015. gs_glyph *glyphs, int num_glyphs, int glyphs_step,
  2016. bool check_hinting)
  2017. {
  2018. int code = 0;
  2019. if (cfont == ofont)
  2020. return 1;
  2021. if (cfont->FontType != ofont->FontType)
  2022. return 0;
  2023. if (cfont->WMode != ofont->WMode)
  2024. return 0;
  2025. if (cfont->font_name.size == 0 || ofont->font_name.size == 0) {
  2026. if (cfont->key_name.size != ofont->key_name.size ||
  2027. memcmp(cfont->key_name.chars, ofont->key_name.chars,
  2028. cfont->font_name.size))
  2029. return 0; /* Don't allow to merge random fonts. */
  2030. } else {
  2031. if (cfont->font_name.size != ofont->font_name.size ||
  2032. memcmp(cfont->font_name.chars, ofont->font_name.chars,
  2033. cfont->font_name.size))
  2034. return 0; /* Don't allow to merge random fonts. */
  2035. }
  2036. if (check_hinting) {
  2037. switch(cfont->FontType) {
  2038. case ft_encrypted:
  2039. case ft_encrypted2:
  2040. if (!same_type1_hinting((const gs_font_type1 *)cfont,
  2041. (const gs_font_type1 *)ofont))
  2042. return 0;
  2043. code = 1;
  2044. break;
  2045. case ft_TrueType:
  2046. code = same_type42_hinting((gs_font_type42 *)cfont,
  2047. (gs_font_type42 *)ofont);
  2048. break;
  2049. case ft_CID_encrypted:
  2050. if (!gs_is_CIDSystemInfo_compatible(
  2051. gs_font_cid_system_info(cfont),
  2052. gs_font_cid_system_info(ofont)))
  2053. return 0;
  2054. code = same_cid0_hinting((const gs_font_cid0 *)cfont,
  2055. (const gs_font_cid0 *)ofont);
  2056. break;
  2057. case ft_CID_TrueType:
  2058. if (!gs_is_CIDSystemInfo_compatible(
  2059. gs_font_cid_system_info(cfont),
  2060. gs_font_cid_system_info(ofont)))
  2061. return 0;
  2062. code = same_cid2_hinting((const gs_font_cid2 *)cfont,
  2063. (const gs_font_cid2 *)ofont);
  2064. break;
  2065. default:
  2066. return_error(gs_error_unregistered); /* Must not happen. */
  2067. }
  2068. if (code <= 0) /* an error or false */
  2069. return code;
  2070. }
  2071. return compare_glyphs(cfont, ofont, glyphs, num_glyphs, glyphs_step, 0);
  2072. }
  2073. /* Extension glyphs may be added to a font to resolve
  2074. glyph name conflicts while conwerting a PDF Widths into Metrics.
  2075. This function drops them before writing out an embedded font. */
  2076. int
  2077. copied_drop_extension_glyphs(gs_font *copied)
  2078. {
  2079. /* Note : This function drops 'used' flags for some glyphs
  2080. and truncates glyph names. Can't use the font
  2081. for outlining|rasterization|width after applying it.
  2082. */
  2083. gs_copied_font_data_t *const cfdata = cf_data(copied);
  2084. uint gsize = cfdata->glyphs_size, i;
  2085. const int sl = strlen(gx_extendeg_glyph_name_separator);
  2086. for (i = 0; i < gsize; i++) {
  2087. gs_copied_glyph_t *pslot = &cfdata->glyphs[i];
  2088. gs_copied_glyph_name_t *name;
  2089. int l, j, k, i0;
  2090. if (!pslot->used)
  2091. continue;
  2092. name = &cfdata->names[i];
  2093. l = name->str.size - sl, j;
  2094. for (j = 0; j < l; j ++)
  2095. if (!memcmp(gx_extendeg_glyph_name_separator, name->str.data + j, sl))
  2096. break;
  2097. if (j >= l)
  2098. continue;
  2099. /* Found an extension name.
  2100. Find the corresponding non-extended one. */
  2101. i0 = i;
  2102. for (k = 0; k < gsize; k++)
  2103. if (cfdata->glyphs[k].used &&
  2104. cfdata->names[k].str.size == j &&
  2105. !memcmp(cfdata->names[k].str.data, name->str.data, j) &&
  2106. !bytes_compare(pslot->gdata.data, pslot->gdata.size,
  2107. cfdata->glyphs[k].gdata.data, cfdata->glyphs[k].gdata.size)) {
  2108. i0 = k;
  2109. break;
  2110. }
  2111. /* Truncate the extended glyph name. */
  2112. cfdata->names[i0].str.size = j;
  2113. /* Drop others with same prefix. */
  2114. for (k = 0; k < gsize; k++)
  2115. if (k != i0 && cfdata->glyphs[k].used &&
  2116. cfdata->names[k].str.size >= j + sl &&
  2117. !memcmp(cfdata->names[k].str.data, name->str.data, j) &&
  2118. !memcmp(gx_extendeg_glyph_name_separator, name + j, sl) &&
  2119. !bytes_compare(pslot->gdata.data, pslot->gdata.size,
  2120. cfdata->glyphs[k].gdata.data, cfdata->glyphs[k].gdata.size))
  2121. cfdata->glyphs[k].used = false;
  2122. }
  2123. return 0;
  2124. }