font.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/ucdn.h"
  24. #include "draw-imp.h"
  25. #include "color-imp.h"
  26. #include "glyph-imp.h"
  27. #include "pixmap-imp.h"
  28. #include <ft2build.h>
  29. #include <assert.h>
  30. #include FT_FREETYPE_H
  31. #include FT_ADVANCES_H
  32. #include FT_MODULE_H
  33. #include FT_STROKER_H
  34. #include FT_SYSTEM_H
  35. #include FT_TRUETYPE_TABLES_H
  36. #include FT_TRUETYPE_TAGS_H
  37. #ifndef FT_SFNT_OS2
  38. #define FT_SFNT_OS2 ft_sfnt_os2
  39. #endif
  40. /* 20 degrees */
  41. #define SHEAR 0.36397f
  42. int ft_char_index(void *face, int cid)
  43. {
  44. int gid = FT_Get_Char_Index(face, cid);
  45. if (gid == 0)
  46. gid = FT_Get_Char_Index(face, 0xf000 + cid);
  47. /* some chinese fonts only ship the similarly looking 0x2026 */
  48. if (gid == 0 && cid == 0x22ef)
  49. gid = FT_Get_Char_Index(face, 0x2026);
  50. return gid;
  51. }
  52. int ft_name_index(void *face, const char *name)
  53. {
  54. int code = FT_Get_Name_Index(face, (char*)name);
  55. if (code == 0)
  56. {
  57. int unicode = fz_unicode_from_glyph_name(name);
  58. if (unicode)
  59. {
  60. const char **dupnames = fz_duplicate_glyph_names_from_unicode(unicode);
  61. while (*dupnames)
  62. {
  63. code = FT_Get_Name_Index(face, (char*)*dupnames);
  64. if (code)
  65. break;
  66. dupnames++;
  67. }
  68. if (code == 0)
  69. {
  70. char buf[12];
  71. sprintf(buf, "uni%04X", unicode);
  72. code = FT_Get_Name_Index(face, buf);
  73. }
  74. }
  75. }
  76. return code;
  77. }
  78. static void fz_drop_freetype(fz_context *ctx);
  79. static fz_font *
  80. fz_new_font(fz_context *ctx, const char *name, int use_glyph_bbox, int glyph_count)
  81. {
  82. fz_font *font;
  83. font = fz_malloc_struct(ctx, fz_font);
  84. font->refs = 1;
  85. if (name)
  86. fz_strlcpy(font->name, name, sizeof font->name);
  87. else
  88. fz_strlcpy(font->name, "(null)", sizeof font->name);
  89. font->ft_face = NULL;
  90. font->flags.ft_substitute = 0;
  91. font->flags.fake_bold = 0;
  92. font->flags.fake_italic = 0;
  93. font->flags.has_opentype = 0;
  94. font->flags.embed = 0;
  95. font->flags.never_embed = 0;
  96. font->t3matrix = fz_identity;
  97. font->t3resources = NULL;
  98. font->t3procs = NULL;
  99. font->t3lists = NULL;
  100. font->t3widths = NULL;
  101. font->t3flags = NULL;
  102. font->t3doc = NULL;
  103. font->t3run = NULL;
  104. font->bbox.x0 = 0;
  105. font->bbox.y0 = 0;
  106. font->bbox.x1 = 1;
  107. font->bbox.y1 = 1;
  108. font->glyph_count = glyph_count;
  109. font->bbox_table = NULL;
  110. font->use_glyph_bbox = use_glyph_bbox;
  111. font->width_count = 0;
  112. font->width_table = NULL;
  113. font->subfont = 0;
  114. return font;
  115. }
  116. fz_font *
  117. fz_keep_font(fz_context *ctx, fz_font *font)
  118. {
  119. return fz_keep_imp(ctx, font, &font->refs);
  120. }
  121. static void
  122. free_resources(fz_context *ctx, fz_font *font)
  123. {
  124. int i;
  125. if (font->t3resources)
  126. {
  127. font->t3freeres(ctx, font->t3doc, font->t3resources);
  128. font->t3resources = NULL;
  129. }
  130. if (font->t3procs)
  131. {
  132. for (i = 0; i < 256; i++)
  133. fz_drop_buffer(ctx, font->t3procs[i]);
  134. }
  135. fz_free(ctx, font->t3procs);
  136. font->t3procs = NULL;
  137. }
  138. /*
  139. Internal function to remove the
  140. references to a document held by a Type3 font. This is
  141. called during document destruction to ensure that Type3
  142. fonts clean up properly.
  143. Without this call being made, Type3 fonts can be left
  144. holding pdf_obj references for the sake of interpretation
  145. operations that will never come. These references
  146. cannot be freed after the document, hence this function
  147. forces them to be freed earlier in the process.
  148. font: The font to decouple.
  149. t3doc: The document to which the font may refer.
  150. */
  151. void fz_decouple_type3_font(fz_context *ctx, fz_font *font, void *t3doc)
  152. {
  153. if (!font || !t3doc || font->t3doc == NULL)
  154. return;
  155. if (font->t3doc != t3doc)
  156. fz_throw(ctx, FZ_ERROR_ARGUMENT, "can't decouple type3 font from a different doc");
  157. font->t3doc = NULL;
  158. free_resources(ctx, font);
  159. }
  160. void
  161. fz_drop_font(fz_context *ctx, fz_font *font)
  162. {
  163. int fterr;
  164. int i;
  165. if (!fz_drop_imp(ctx, font, &font->refs))
  166. return;
  167. free_resources(ctx, font);
  168. if (font->t3lists)
  169. for (i = 0; i < 256; i++)
  170. fz_drop_display_list(ctx, font->t3lists[i]);
  171. fz_free(ctx, font->t3procs);
  172. fz_free(ctx, font->t3lists);
  173. fz_free(ctx, font->t3widths);
  174. fz_free(ctx, font->t3flags);
  175. if (font->ft_face)
  176. {
  177. fz_ft_lock(ctx);
  178. fterr = FT_Done_Face((FT_Face)font->ft_face);
  179. fz_ft_unlock(ctx);
  180. if (fterr)
  181. fz_warn(ctx, "FT_Done_Face(%s): %s", font->name, ft_error_string(fterr));
  182. fz_drop_freetype(ctx);
  183. }
  184. for (i = 0; i < 256; ++i)
  185. fz_free(ctx, font->encoding_cache[i]);
  186. fz_drop_buffer(ctx, font->buffer);
  187. if (font->bbox_table)
  188. {
  189. int n = (font->glyph_count+255)/256;
  190. for (i = 0; i < n; i++)
  191. fz_free(ctx, font->bbox_table[i]);
  192. fz_free(ctx, font->bbox_table);
  193. }
  194. fz_free(ctx, font->width_table);
  195. if (font->advance_cache)
  196. {
  197. int n = (font->glyph_count+255)/256;
  198. for (i = 0; i < n; i++)
  199. fz_free(ctx, font->advance_cache[i]);
  200. fz_free(ctx, font->advance_cache);
  201. }
  202. if (font->shaper_data.destroy && font->shaper_data.shaper_handle)
  203. {
  204. font->shaper_data.destroy(ctx, font->shaper_data.shaper_handle);
  205. }
  206. fz_free(ctx, font);
  207. }
  208. void
  209. fz_set_font_bbox(fz_context *ctx, fz_font *font, float xmin, float ymin, float xmax, float ymax)
  210. {
  211. if (xmin >= xmax || ymin >= ymax)
  212. {
  213. /* Invalid bbox supplied. */
  214. if (font->t3procs)
  215. {
  216. /* For type3 fonts we use the union of all the glyphs' bboxes. */
  217. font->bbox = fz_empty_rect;
  218. }
  219. else
  220. {
  221. /* For other fonts it would be prohibitively slow to measure the true one, so make one up. */
  222. font->bbox = fz_unit_rect;
  223. }
  224. font->flags.invalid_bbox = 1;
  225. }
  226. else
  227. {
  228. font->bbox.x0 = xmin;
  229. font->bbox.y0 = ymin;
  230. font->bbox.x1 = xmax;
  231. font->bbox.y1 = ymax;
  232. }
  233. }
  234. float fz_font_ascender(fz_context *ctx, fz_font *font)
  235. {
  236. return font->ascender;
  237. }
  238. float fz_font_descender(fz_context *ctx, fz_font *font)
  239. {
  240. return font->descender;
  241. }
  242. /*
  243. * Freetype hooks
  244. */
  245. struct fz_font_context
  246. {
  247. int ctx_refs;
  248. FT_Library ftlib;
  249. struct FT_MemoryRec_ ftmemory;
  250. int ftlib_refs;
  251. fz_load_system_font_fn *load_font;
  252. fz_load_system_cjk_font_fn *load_cjk_font;
  253. fz_load_system_fallback_font_fn *load_fallback_font;
  254. /* Cached fallback fonts */
  255. fz_font *base14[14];
  256. fz_font *cjk[4];
  257. struct { fz_font *serif, *sans; } fallback[256];
  258. fz_font *symbol1, *symbol2, *math, *music, *boxes;
  259. fz_font *emoji;
  260. };
  261. #undef __FTERRORS_H__
  262. #define FT_ERRORDEF(e, v, s) { (e), (s) },
  263. #define FT_ERROR_START_LIST
  264. #define FT_ERROR_END_LIST { 0, NULL }
  265. struct ft_error
  266. {
  267. int err;
  268. char *str;
  269. };
  270. static void *ft_alloc(FT_Memory memory, long size)
  271. {
  272. fz_context *ctx = (fz_context *) memory->user;
  273. return Memento_label(fz_malloc_no_throw(ctx, size), "ft_alloc");
  274. }
  275. static void ft_free(FT_Memory memory, void *block)
  276. {
  277. fz_context *ctx = (fz_context *) memory->user;
  278. fz_free(ctx, block);
  279. }
  280. static void *ft_realloc(FT_Memory memory, long cur_size, long new_size, void *block)
  281. {
  282. fz_context *ctx = (fz_context *) memory->user;
  283. void *newblock = NULL;
  284. if (new_size == 0)
  285. {
  286. fz_free(ctx, block);
  287. return newblock;
  288. }
  289. if (block == NULL)
  290. return ft_alloc(memory, new_size);
  291. return fz_realloc_no_throw(ctx, block, new_size);
  292. }
  293. void
  294. fz_ft_lock(fz_context *ctx)
  295. {
  296. fz_lock(ctx, FZ_LOCK_FREETYPE);
  297. fz_lock(ctx, FZ_LOCK_ALLOC);
  298. assert(ctx->font->ftmemory.user == NULL);
  299. ctx->font->ftmemory.user = ctx;
  300. fz_unlock(ctx, FZ_LOCK_ALLOC);
  301. }
  302. void
  303. fz_ft_unlock(fz_context *ctx)
  304. {
  305. fz_lock(ctx, FZ_LOCK_ALLOC);
  306. ctx->font->ftmemory.user = NULL;
  307. fz_unlock(ctx, FZ_LOCK_ALLOC);
  308. fz_unlock(ctx, FZ_LOCK_FREETYPE);
  309. }
  310. int
  311. fz_ft_lock_held(fz_context *ctx)
  312. {
  313. /* If this thread has locked the freetype lock already, then
  314. * the stored context will be this one. */
  315. return (ctx->font->ftmemory.user == ctx);
  316. }
  317. void fz_new_font_context(fz_context *ctx)
  318. {
  319. ctx->font = fz_malloc_struct(ctx, fz_font_context);
  320. ctx->font->ctx_refs = 1;
  321. ctx->font->ftlib = NULL;
  322. ctx->font->ftlib_refs = 0;
  323. ctx->font->load_font = NULL;
  324. ctx->font->ftmemory.user = NULL;
  325. ctx->font->ftmemory.alloc = ft_alloc;
  326. ctx->font->ftmemory.free = ft_free;
  327. ctx->font->ftmemory.realloc = ft_realloc;
  328. }
  329. fz_font_context *
  330. fz_keep_font_context(fz_context *ctx)
  331. {
  332. if (!ctx)
  333. return NULL;
  334. return fz_keep_imp(ctx, ctx->font, &ctx->font->ctx_refs);
  335. }
  336. void fz_drop_font_context(fz_context *ctx)
  337. {
  338. if (!ctx)
  339. return;
  340. if (fz_drop_imp(ctx, ctx->font, &ctx->font->ctx_refs))
  341. {
  342. int i;
  343. for (i = 0; i < (int)nelem(ctx->font->base14); ++i)
  344. fz_drop_font(ctx, ctx->font->base14[i]);
  345. for (i = 0; i < (int)nelem(ctx->font->cjk); ++i)
  346. fz_drop_font(ctx, ctx->font->cjk[i]);
  347. for (i = 0; i < (int)nelem(ctx->font->fallback); ++i)
  348. {
  349. fz_drop_font(ctx, ctx->font->fallback[i].serif);
  350. fz_drop_font(ctx, ctx->font->fallback[i].sans);
  351. }
  352. fz_drop_font(ctx, ctx->font->symbol1);
  353. fz_drop_font(ctx, ctx->font->symbol2);
  354. fz_drop_font(ctx, ctx->font->math);
  355. fz_drop_font(ctx, ctx->font->music);
  356. fz_drop_font(ctx, ctx->font->emoji);
  357. fz_drop_font(ctx, ctx->font->boxes);
  358. fz_free(ctx, ctx->font);
  359. ctx->font = NULL;
  360. }
  361. }
  362. void fz_install_load_system_font_funcs(fz_context *ctx,
  363. fz_load_system_font_fn *f,
  364. fz_load_system_cjk_font_fn *f_cjk,
  365. fz_load_system_fallback_font_fn *f_back)
  366. {
  367. ctx->font->load_font = f;
  368. ctx->font->load_cjk_font = f_cjk;
  369. ctx->font->load_fallback_font = f_back;
  370. }
  371. /* fz_load_*_font returns NULL if no font could be loaded (also on error) */
  372. fz_font *fz_load_system_font(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics)
  373. {
  374. fz_font *font = NULL;
  375. if (ctx->font->load_font)
  376. {
  377. fz_try(ctx)
  378. font = ctx->font->load_font(ctx, name, bold, italic, needs_exact_metrics);
  379. fz_catch(ctx)
  380. {
  381. fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
  382. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  383. fz_report_error(ctx);
  384. font = NULL;
  385. }
  386. }
  387. return font;
  388. }
  389. fz_font *fz_load_system_cjk_font(fz_context *ctx, const char *name, int ros, int serif)
  390. {
  391. fz_font *font = NULL;
  392. if (ctx->font->load_cjk_font)
  393. {
  394. fz_try(ctx)
  395. font = ctx->font->load_cjk_font(ctx, name, ros, serif);
  396. fz_catch(ctx)
  397. {
  398. fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
  399. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  400. fz_report_error(ctx);
  401. font = NULL;
  402. }
  403. }
  404. return font;
  405. }
  406. fz_font *fz_load_system_fallback_font(fz_context *ctx, int script, int language, int serif, int bold, int italic)
  407. {
  408. fz_font *font = NULL;
  409. if (ctx->font->load_fallback_font)
  410. {
  411. fz_try(ctx)
  412. font = ctx->font->load_fallback_font(ctx, script, language, serif, bold, italic);
  413. fz_catch(ctx)
  414. {
  415. fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
  416. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  417. fz_report_error(ctx);
  418. font = NULL;
  419. }
  420. }
  421. return font;
  422. }
  423. fz_font *fz_load_fallback_font(fz_context *ctx, int script, int language, int serif, int bold, int italic)
  424. {
  425. fz_font **fontp;
  426. const unsigned char *data;
  427. int ordering = FZ_ADOBE_JAPAN;
  428. int index;
  429. int subfont;
  430. int size;
  431. if (script < 0 || script >= (int)nelem(ctx->font->fallback))
  432. return NULL;
  433. /* TODO: bold and italic */
  434. index = script;
  435. if (script == UCDN_SCRIPT_HAN)
  436. {
  437. switch (language)
  438. {
  439. case FZ_LANG_ja: index = UCDN_LAST_SCRIPT + 1; ordering = FZ_ADOBE_JAPAN; break;
  440. case FZ_LANG_ko: index = UCDN_LAST_SCRIPT + 2; ordering = FZ_ADOBE_KOREA; break;
  441. case FZ_LANG_zh_Hans: index = UCDN_LAST_SCRIPT + 3; ordering = FZ_ADOBE_GB; break;
  442. case FZ_LANG_zh_Hant: index = UCDN_LAST_SCRIPT + 4; ordering = FZ_ADOBE_CNS; break;
  443. }
  444. }
  445. if (script == UCDN_SCRIPT_ARABIC)
  446. {
  447. if (language == FZ_LANG_ur || language == FZ_LANG_urd)
  448. index = UCDN_LAST_SCRIPT + 5;
  449. }
  450. if (serif)
  451. fontp = &ctx->font->fallback[index].serif;
  452. else
  453. fontp = &ctx->font->fallback[index].sans;
  454. if (!*fontp)
  455. {
  456. *fontp = fz_load_system_fallback_font(ctx, script, language, serif, bold, italic);
  457. if (!*fontp)
  458. {
  459. data = fz_lookup_noto_font(ctx, script, language, &size, &subfont);
  460. if (data)
  461. {
  462. *fontp = fz_new_font_from_memory(ctx, NULL, data, size, subfont, 0);
  463. /* Noto fonts can be embedded. */
  464. fz_set_font_embedding(ctx, *fontp, 1);
  465. }
  466. }
  467. }
  468. switch (script)
  469. {
  470. case UCDN_SCRIPT_HANGUL: script = UCDN_SCRIPT_HAN; ordering = FZ_ADOBE_KOREA; break;
  471. case UCDN_SCRIPT_HIRAGANA: script = UCDN_SCRIPT_HAN; ordering = FZ_ADOBE_JAPAN; break;
  472. case UCDN_SCRIPT_KATAKANA: script = UCDN_SCRIPT_HAN; ordering = FZ_ADOBE_JAPAN; break;
  473. case UCDN_SCRIPT_BOPOMOFO: script = UCDN_SCRIPT_HAN; ordering = FZ_ADOBE_CNS; break;
  474. }
  475. if (*fontp && (script == UCDN_SCRIPT_HAN))
  476. {
  477. (*fontp)->flags.cjk = 1;
  478. (*fontp)->flags.cjk_lang = ordering;
  479. }
  480. return *fontp;
  481. }
  482. static fz_font *fz_load_fallback_math_font(fz_context *ctx)
  483. {
  484. const unsigned char *data;
  485. int size;
  486. if (!ctx->font->math)
  487. {
  488. data = fz_lookup_noto_math_font(ctx, &size);
  489. if (data)
  490. ctx->font->math = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  491. }
  492. return ctx->font->math;
  493. }
  494. static fz_font *fz_load_fallback_music_font(fz_context *ctx)
  495. {
  496. const unsigned char *data;
  497. int size;
  498. if (!ctx->font->music)
  499. {
  500. data = fz_lookup_noto_music_font(ctx, &size);
  501. if (data)
  502. ctx->font->music = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  503. }
  504. return ctx->font->music;
  505. }
  506. static fz_font *fz_load_fallback_symbol1_font(fz_context *ctx)
  507. {
  508. const unsigned char *data;
  509. int size;
  510. if (!ctx->font->symbol1)
  511. {
  512. data = fz_lookup_noto_symbol1_font(ctx, &size);
  513. if (data)
  514. ctx->font->symbol1 = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  515. }
  516. return ctx->font->symbol1;
  517. }
  518. static fz_font *fz_load_fallback_symbol2_font(fz_context *ctx)
  519. {
  520. const unsigned char *data;
  521. int size;
  522. if (!ctx->font->symbol2)
  523. {
  524. data = fz_lookup_noto_symbol2_font(ctx, &size);
  525. if (data)
  526. ctx->font->symbol2 = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  527. }
  528. return ctx->font->symbol2;
  529. }
  530. static fz_font *fz_load_fallback_emoji_font(fz_context *ctx)
  531. {
  532. const unsigned char *data;
  533. int size;
  534. if (!ctx->font->emoji)
  535. {
  536. data = fz_lookup_noto_emoji_font(ctx, &size);
  537. if (data)
  538. ctx->font->emoji = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  539. }
  540. return ctx->font->emoji;
  541. }
  542. static fz_font *fz_load_fallback_boxes_font(fz_context *ctx)
  543. {
  544. const unsigned char *data;
  545. int size;
  546. if (!ctx->font->boxes)
  547. {
  548. data = fz_lookup_noto_boxes_font(ctx, &size);
  549. if (data)
  550. ctx->font->boxes = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  551. }
  552. return ctx->font->boxes;
  553. }
  554. static const struct ft_error ft_errors[] =
  555. {
  556. #include FT_ERRORS_H
  557. };
  558. const char *ft_error_string(int err)
  559. {
  560. const struct ft_error *e;
  561. for (e = ft_errors; e->str; e++)
  562. if (e->err == err)
  563. return e->str;
  564. return "Unknown error";
  565. }
  566. static void
  567. fz_keep_freetype(fz_context *ctx)
  568. {
  569. int fterr;
  570. int maj, min, pat;
  571. fz_font_context *fct = ctx->font;
  572. fz_ft_lock(ctx);
  573. if (fct->ftlib)
  574. {
  575. fct->ftlib_refs++;
  576. fz_ft_unlock(ctx);
  577. return;
  578. }
  579. fterr = FT_New_Library(&fct->ftmemory, &fct->ftlib);
  580. if (fterr)
  581. {
  582. const char *mess = ft_error_string(fterr);
  583. fz_ft_unlock(ctx);
  584. fz_throw(ctx, FZ_ERROR_LIBRARY, "cannot init freetype: %s", mess);
  585. }
  586. FT_Add_Default_Modules(fct->ftlib);
  587. FT_Library_Version(fct->ftlib, &maj, &min, &pat);
  588. if (maj == 2 && min == 1 && pat < 7)
  589. {
  590. fterr = FT_Done_Library(fct->ftlib);
  591. if (fterr)
  592. fz_warn(ctx, "FT_Done_Library(): %s", ft_error_string(fterr));
  593. fz_ft_unlock(ctx);
  594. fz_throw(ctx, FZ_ERROR_LIBRARY, "freetype version too old: %d.%d.%d", maj, min, pat);
  595. }
  596. fct->ftlib_refs++;
  597. fz_ft_unlock(ctx);
  598. }
  599. static void
  600. fz_drop_freetype(fz_context *ctx)
  601. {
  602. int fterr;
  603. fz_font_context *fct = ctx->font;
  604. fz_ft_lock(ctx);
  605. if (--fct->ftlib_refs == 0)
  606. {
  607. fterr = FT_Done_Library(fct->ftlib);
  608. if (fterr)
  609. fz_warn(ctx, "FT_Done_Library(): %s", ft_error_string(fterr));
  610. fct->ftlib = NULL;
  611. }
  612. fz_ft_unlock(ctx);
  613. }
  614. fz_font *
  615. fz_new_font_from_buffer(fz_context *ctx, const char *name, fz_buffer *buffer, int index, int use_glyph_bbox)
  616. {
  617. FT_Face face;
  618. TT_OS2 *os2;
  619. fz_font *font;
  620. int fterr;
  621. FT_ULong tag, size, i, n;
  622. FT_UShort flags;
  623. char namebuf[sizeof(font->name)];
  624. fz_ascdesc_source ascdesc_src = FZ_ASCDESC_FROM_FONT;
  625. fz_keep_freetype(ctx);
  626. fz_ft_lock(ctx);
  627. fterr = FT_New_Memory_Face(ctx->font->ftlib, buffer->data, (FT_Long)buffer->len, index, &face);
  628. fz_ft_unlock(ctx);
  629. if (fterr)
  630. {
  631. fz_drop_freetype(ctx);
  632. fz_throw(ctx, FZ_ERROR_LIBRARY, "FT_New_Memory_Face(%s): %s", name, ft_error_string(fterr));
  633. }
  634. if (!name)
  635. {
  636. if (!face->family_name)
  637. {
  638. name = face->style_name;
  639. }
  640. else if (!face->style_name)
  641. {
  642. name = face->family_name;
  643. }
  644. else if (strstr(face->style_name, face->family_name) == face->style_name)
  645. {
  646. name = face->style_name;
  647. }
  648. else
  649. {
  650. fz_strlcpy(namebuf, face->family_name, sizeof(namebuf));
  651. fz_strlcat(namebuf, " ", sizeof(namebuf));
  652. fz_strlcat(namebuf, face->style_name, sizeof(namebuf));
  653. name = namebuf;
  654. }
  655. }
  656. fz_try(ctx)
  657. font = fz_new_font(ctx, name, use_glyph_bbox, face->num_glyphs);
  658. fz_catch(ctx)
  659. {
  660. fz_ft_lock(ctx);
  661. fterr = FT_Done_Face(face);
  662. fz_ft_unlock(ctx);
  663. if (fterr)
  664. fz_warn(ctx, "FT_Done_Face(%s): %s", name, ft_error_string(fterr));
  665. fz_drop_freetype(ctx);
  666. fz_rethrow(ctx);
  667. }
  668. font->ft_face = face;
  669. fz_set_font_bbox(ctx, font,
  670. (float) face->bbox.xMin / face->units_per_EM,
  671. (float) face->bbox.yMin / face->units_per_EM,
  672. (float) face->bbox.xMax / face->units_per_EM,
  673. (float) face->bbox.yMax / face->units_per_EM);
  674. if (face->ascender <= 0 || face->ascender > FZ_MAX_TRUSTWORTHY_ASCENT * face->units_per_EM)
  675. font->ascender = 0.8f, ascdesc_src = FZ_ASCDESC_DEFAULT;
  676. else
  677. font->ascender = (float)face->ascender / face->units_per_EM;
  678. if (face->descender < FZ_MAX_TRUSTWORTHY_DESCENT * face->units_per_EM || face->descender > -FZ_MAX_TRUSTWORTHY_DESCENT * face->units_per_EM)
  679. font->descender = -0.2f, ascdesc_src = FZ_ASCDESC_DEFAULT;
  680. else
  681. {
  682. font->descender = (float)face->descender / face->units_per_EM;
  683. if (font->descender > 0)
  684. font->descender = -font->descender;
  685. }
  686. font->ascdesc_src = ascdesc_src;
  687. font->subfont = index;
  688. font->flags.is_mono = !!(face->face_flags & FT_FACE_FLAG_FIXED_WIDTH);
  689. font->flags.is_serif = 1;
  690. font->flags.is_bold = !!(face->style_flags & FT_STYLE_FLAG_BOLD);
  691. font->flags.is_italic = !!(face->style_flags & FT_STYLE_FLAG_ITALIC);
  692. font->flags.embed = 1;
  693. font->flags.never_embed = 0;
  694. if (FT_IS_SFNT(face))
  695. {
  696. fz_ft_lock(ctx);
  697. os2 = FT_Get_Sfnt_Table(face, FT_SFNT_OS2);
  698. if (os2)
  699. font->flags.is_serif = !(os2->sFamilyClass & 2048); /* Class 8 is sans-serif */
  700. flags = FT_Get_FSType_Flags(face);
  701. if (flags & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
  702. FT_FSTYPE_BITMAP_EMBEDDING_ONLY))
  703. {
  704. font->flags.never_embed = 1;
  705. font->flags.embed = 0;
  706. }
  707. FT_Sfnt_Table_Info(face, 0, NULL, &n);
  708. for (i = 0; i < n; ++i)
  709. {
  710. FT_Sfnt_Table_Info(face, i, &tag, &size);
  711. if (tag == TTAG_GDEF || tag == TTAG_GPOS || tag == TTAG_GSUB)
  712. font->flags.has_opentype = 1;
  713. }
  714. fz_ft_unlock(ctx);
  715. }
  716. if (name)
  717. {
  718. if (!font->flags.is_bold)
  719. {
  720. if (strstr(name, "Semibold")) font->flags.is_bold = 1;
  721. if (strstr(name, "Bold")) font->flags.is_bold = 1;
  722. }
  723. if (!font->flags.is_italic)
  724. {
  725. if (strstr(name, "Italic")) font->flags.is_italic = 1;
  726. if (strstr(name, "Oblique")) font->flags.is_italic = 1;
  727. }
  728. }
  729. font->buffer = fz_keep_buffer(ctx, buffer);
  730. return font;
  731. }
  732. fz_font *
  733. fz_new_font_from_memory(fz_context *ctx, const char *name, const unsigned char *data, int len, int index, int use_glyph_bbox)
  734. {
  735. fz_buffer *buffer = fz_new_buffer_from_shared_data(ctx, data, len);
  736. fz_font *font = NULL;
  737. fz_try(ctx)
  738. font = fz_new_font_from_buffer(ctx, name, buffer, index, use_glyph_bbox);
  739. fz_always(ctx)
  740. fz_drop_buffer(ctx, buffer);
  741. fz_catch(ctx)
  742. fz_rethrow(ctx);
  743. return font;
  744. }
  745. fz_font *
  746. fz_new_font_from_file(fz_context *ctx, const char *name, const char *path, int index, int use_glyph_bbox)
  747. {
  748. fz_buffer *buffer = fz_read_file(ctx, path);
  749. fz_font *font = NULL;
  750. fz_try(ctx)
  751. font = fz_new_font_from_buffer(ctx, name, buffer, index, use_glyph_bbox);
  752. fz_always(ctx)
  753. fz_drop_buffer(ctx, buffer);
  754. fz_catch(ctx)
  755. fz_rethrow(ctx);
  756. return font;
  757. }
  758. void fz_set_font_embedding(fz_context *ctx, fz_font *font, int embed)
  759. {
  760. if (!font)
  761. return;
  762. if (embed)
  763. {
  764. if (font->flags.never_embed)
  765. fz_warn(ctx, "not allowed to embed font: %s", font->name);
  766. else
  767. font->flags.embed = 1;
  768. }
  769. else
  770. {
  771. font->flags.embed = 0;
  772. }
  773. }
  774. static int
  775. find_base14_index(const char *name)
  776. {
  777. if (!strcmp(name, "Courier")) return 0;
  778. if (!strcmp(name, "Courier-Oblique")) return 1;
  779. if (!strcmp(name, "Courier-Bold")) return 2;
  780. if (!strcmp(name, "Courier-BoldOblique")) return 3;
  781. if (!strcmp(name, "Helvetica")) return 4;
  782. if (!strcmp(name, "Helvetica-Oblique")) return 5;
  783. if (!strcmp(name, "Helvetica-Bold")) return 6;
  784. if (!strcmp(name, "Helvetica-BoldOblique")) return 7;
  785. if (!strcmp(name, "Times-Roman")) return 8;
  786. if (!strcmp(name, "Times-Italic")) return 9;
  787. if (!strcmp(name, "Times-Bold")) return 10;
  788. if (!strcmp(name, "Times-BoldItalic")) return 11;
  789. if (!strcmp(name, "Symbol")) return 12;
  790. if (!strcmp(name, "ZapfDingbats")) return 13;
  791. return -1;
  792. }
  793. fz_font *
  794. fz_new_base14_font(fz_context *ctx, const char *name)
  795. {
  796. const unsigned char *data;
  797. int size;
  798. int x = find_base14_index(name);
  799. if (x >= 0)
  800. {
  801. if (ctx->font->base14[x])
  802. return fz_keep_font(ctx, ctx->font->base14[x]);
  803. data = fz_lookup_base14_font(ctx, name, &size);
  804. if (data)
  805. {
  806. ctx->font->base14[x] = fz_new_font_from_memory(ctx, name, data, size, 0, 1);
  807. ctx->font->base14[x]->flags.is_serif = (name[0] == 'T'); /* Times-Roman */
  808. /* Ideally we should not embed base14 fonts by default, but we have to
  809. * allow it for now until we have written code in pdf-device to output
  810. * base14s in a 'special' manner. */
  811. fz_set_font_embedding(ctx, ctx->font->base14[x], 1);
  812. return fz_keep_font(ctx, ctx->font->base14[x]);
  813. }
  814. }
  815. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot find builtin font with name '%s'", name);
  816. }
  817. fz_font *
  818. fz_new_cjk_font(fz_context *ctx, int ordering)
  819. {
  820. const unsigned char *data;
  821. int size, index;
  822. fz_font *font;
  823. if (ordering >= 0 && ordering < (int)nelem(ctx->font->cjk))
  824. {
  825. if (ctx->font->cjk[ordering])
  826. return fz_keep_font(ctx, ctx->font->cjk[ordering]);
  827. data = fz_lookup_cjk_font(ctx, ordering, &size, &index);
  828. if (data)
  829. font = fz_new_font_from_memory(ctx, NULL, data, size, index, 0);
  830. else
  831. font = fz_load_system_cjk_font(ctx, "SourceHanSerif", ordering, 1);
  832. /* FIXME: Currently the builtin one at least will be set to embed. Is that right? */
  833. if (font)
  834. {
  835. font->flags.cjk = 1;
  836. font->flags.cjk_lang = ordering;
  837. ctx->font->cjk[ordering] = font;
  838. return fz_keep_font(ctx, ctx->font->cjk[ordering]);
  839. }
  840. }
  841. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot find builtin CJK font");
  842. }
  843. fz_font *
  844. fz_new_builtin_font(fz_context *ctx, const char *name, int is_bold, int is_italic)
  845. {
  846. const unsigned char *data;
  847. int size;
  848. fz_font *font;
  849. data = fz_lookup_builtin_font(ctx, name, is_bold, is_italic, &size);
  850. if (!data)
  851. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot find builtin font with name '%s'", name);
  852. font = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  853. /* Don't embed builtin fonts. */
  854. fz_set_font_embedding(ctx, font, 0);
  855. return font;
  856. }
  857. static fz_matrix *
  858. fz_adjust_ft_glyph_width(fz_context *ctx, fz_font *font, int gid, fz_matrix *trm)
  859. {
  860. /* Fudge the font matrix to stretch the glyph if we've substituted the font. */
  861. if (font->flags.ft_stretch && font->width_table /* && font->wmode == 0 */)
  862. {
  863. FT_Error fterr;
  864. FT_Fixed adv = 0;
  865. float subw;
  866. float realw;
  867. fz_ft_lock(ctx);
  868. fterr = FT_Get_Advance(font->ft_face, gid, FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_IGNORE_TRANSFORM, &adv);
  869. fz_ft_unlock(ctx);
  870. if (fterr && fterr != FT_Err_Invalid_Argument)
  871. fz_warn(ctx, "FT_Get_Advance(%s,%d): %s", font->name, gid, ft_error_string(fterr));
  872. realw = adv * 1000.0f / ((FT_Face)font->ft_face)->units_per_EM;
  873. if (gid < font->width_count)
  874. subw = font->width_table[gid];
  875. else
  876. subw = font->width_default;
  877. /* Sanity check scaling in case of broken metrics. */
  878. if (realw > 0 && subw > 0)
  879. *trm = fz_pre_scale(*trm, subw / realw, 1);
  880. }
  881. return trm;
  882. }
  883. static fz_glyph *
  884. glyph_from_ft_bitmap(fz_context *ctx, int left, int top, FT_Bitmap *bitmap)
  885. {
  886. (void)Memento_label(bitmap->buffer, "ft_bitmap");
  887. if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO)
  888. return fz_new_glyph_from_1bpp_data(ctx, left, top - bitmap->rows, bitmap->width, bitmap->rows, bitmap->buffer + (bitmap->rows-1)*bitmap->pitch, -bitmap->pitch);
  889. else
  890. return fz_new_glyph_from_8bpp_data(ctx, left, top - bitmap->rows, bitmap->width, bitmap->rows, bitmap->buffer + (bitmap->rows-1)*bitmap->pitch, -bitmap->pitch);
  891. }
  892. static fz_pixmap *
  893. pixmap_from_ft_bitmap(fz_context *ctx, int left, int top, FT_Bitmap *bitmap)
  894. {
  895. (void)Memento_label(bitmap->buffer, "ft_bitmap");
  896. if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO)
  897. return fz_new_pixmap_from_1bpp_data(ctx, left, top - bitmap->rows, bitmap->width, bitmap->rows, bitmap->buffer + (bitmap->rows-1)*bitmap->pitch, -bitmap->pitch);
  898. else
  899. return fz_new_pixmap_from_8bpp_data(ctx, left, top - bitmap->rows, bitmap->width, bitmap->rows, bitmap->buffer + (bitmap->rows-1)*bitmap->pitch, -bitmap->pitch);
  900. }
  901. /* Takes the freetype lock, and returns with it held */
  902. static FT_GlyphSlot
  903. do_ft_render_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, int aa)
  904. {
  905. FT_Face face = font->ft_face;
  906. FT_Matrix m;
  907. FT_Vector v;
  908. FT_Error fterr;
  909. float strength = fz_matrix_expansion(trm) * 0.02f;
  910. fz_adjust_ft_glyph_width(ctx, font, gid, &trm);
  911. if (font->flags.fake_italic)
  912. trm = fz_pre_shear(trm, SHEAR, 0);
  913. fz_ft_lock(ctx);
  914. if (aa == 0)
  915. {
  916. /* enable grid fitting for non-antialiased rendering */
  917. float scale = fz_matrix_expansion(trm);
  918. m.xx = trm.a * 65536 / scale;
  919. m.yx = trm.b * 65536 / scale;
  920. m.xy = trm.c * 65536 / scale;
  921. m.yy = trm.d * 65536 / scale;
  922. v.x = 0;
  923. v.y = 0;
  924. fterr = FT_Set_Char_Size(face, 64 * scale, 64 * scale, 72, 72);
  925. if (fterr)
  926. fz_warn(ctx, "FT_Set_Char_Size(%s,%d,72): %s", font->name, (int)(64*scale), ft_error_string(fterr));
  927. FT_Set_Transform(face, &m, &v);
  928. fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_MONO);
  929. if (fterr)
  930. {
  931. fz_warn(ctx, "FT_Load_Glyph(%s,%d,FT_LOAD_TARGET_MONO): %s", font->name, gid, ft_error_string(fterr));
  932. goto retry_unhinted;
  933. }
  934. }
  935. else
  936. {
  937. retry_unhinted:
  938. /*
  939. * Freetype mutilates complex glyphs if they are loaded with
  940. * FT_Set_Char_Size 1.0. It rounds the coordinates before applying
  941. * transformation. To get more precision in freetype, we shift part of
  942. * the scale in the matrix into FT_Set_Char_Size instead.
  943. */
  944. /* Check for overflow; FreeType matrices use 16.16 fixed-point numbers */
  945. if (trm.a < -512 || trm.a > 512) return NULL;
  946. if (trm.b < -512 || trm.b > 512) return NULL;
  947. if (trm.c < -512 || trm.c > 512) return NULL;
  948. if (trm.d < -512 || trm.d > 512) return NULL;
  949. m.xx = trm.a * 64; /* should be 65536 */
  950. m.yx = trm.b * 64;
  951. m.xy = trm.c * 64;
  952. m.yy = trm.d * 64;
  953. v.x = trm.e * 64;
  954. v.y = trm.f * 64;
  955. fterr = FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */
  956. if (fterr)
  957. fz_warn(ctx, "FT_Set_Char_Size(%s,65536,72): %s", font->name, ft_error_string(fterr));
  958. FT_Set_Transform(face, &m, &v);
  959. fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
  960. if (fterr)
  961. {
  962. fz_warn(ctx, "FT_Load_Glyph(%s,%d,FT_LOAD_NO_HINTING): %s", font->name, gid, ft_error_string(fterr));
  963. return NULL;
  964. }
  965. }
  966. if (font->flags.fake_bold)
  967. {
  968. FT_Outline_Embolden(&face->glyph->outline, strength * 64);
  969. FT_Outline_Translate(&face->glyph->outline, -strength * 32, -strength * 32);
  970. }
  971. fterr = FT_Render_Glyph(face->glyph, aa > 0 ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
  972. if (fterr)
  973. {
  974. if (aa > 0)
  975. fz_warn(ctx, "FT_Render_Glyph(%s,%d,FT_RENDER_MODE_NORMAL): %s", font->name, gid, ft_error_string(fterr));
  976. else
  977. fz_warn(ctx, "FT_Render_Glyph(%s,%d,FT_RENDER_MODE_MONO): %s", font->name, gid, ft_error_string(fterr));
  978. return NULL;
  979. }
  980. return face->glyph;
  981. }
  982. fz_pixmap *
  983. fz_render_ft_glyph_pixmap(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, int aa)
  984. {
  985. FT_GlyphSlot slot = do_ft_render_glyph(ctx, font, gid, trm, aa);
  986. fz_pixmap *pixmap = NULL;
  987. if (slot == NULL)
  988. {
  989. fz_ft_unlock(ctx);
  990. return NULL;
  991. }
  992. fz_try(ctx)
  993. {
  994. pixmap = pixmap_from_ft_bitmap(ctx, slot->bitmap_left, slot->bitmap_top, &slot->bitmap);
  995. }
  996. fz_always(ctx)
  997. {
  998. fz_ft_unlock(ctx);
  999. }
  1000. fz_catch(ctx)
  1001. {
  1002. fz_rethrow(ctx);
  1003. }
  1004. return pixmap;
  1005. }
  1006. /* The glyph cache lock is always taken when this is called. */
  1007. fz_glyph *
  1008. fz_render_ft_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, int aa)
  1009. {
  1010. FT_GlyphSlot slot = do_ft_render_glyph(ctx, font, gid, trm, aa);
  1011. fz_glyph *glyph = NULL;
  1012. if (slot == NULL)
  1013. {
  1014. fz_ft_unlock(ctx);
  1015. return NULL;
  1016. }
  1017. fz_try(ctx)
  1018. {
  1019. glyph = glyph_from_ft_bitmap(ctx, slot->bitmap_left, slot->bitmap_top, &slot->bitmap);
  1020. }
  1021. fz_always(ctx)
  1022. {
  1023. fz_ft_unlock(ctx);
  1024. }
  1025. fz_catch(ctx)
  1026. {
  1027. fz_rethrow(ctx);
  1028. }
  1029. return glyph;
  1030. }
  1031. /* Takes the freetype lock, and returns with it held */
  1032. static FT_Glyph
  1033. do_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, const fz_stroke_state *state, int aa)
  1034. {
  1035. FT_Face face = font->ft_face;
  1036. float expansion = fz_matrix_expansion(ctm);
  1037. int linewidth = state->linewidth * expansion * 64 / 2;
  1038. FT_Matrix m;
  1039. FT_Vector v;
  1040. FT_Error fterr;
  1041. FT_Stroker stroker;
  1042. FT_Glyph glyph;
  1043. FT_Stroker_LineJoin line_join;
  1044. FT_Stroker_LineCap line_cap;
  1045. fz_adjust_ft_glyph_width(ctx, font, gid, &trm);
  1046. if (font->flags.fake_italic)
  1047. trm = fz_pre_shear(trm, SHEAR, 0);
  1048. m.xx = trm.a * 64; /* should be 65536 */
  1049. m.yx = trm.b * 64;
  1050. m.xy = trm.c * 64;
  1051. m.yy = trm.d * 64;
  1052. v.x = trm.e * 64;
  1053. v.y = trm.f * 64;
  1054. fz_ft_lock(ctx);
  1055. fterr = FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */
  1056. if (fterr)
  1057. {
  1058. fz_warn(ctx, "FT_Set_Char_Size(%s,65536,72): %s", font->name, ft_error_string(fterr));
  1059. return NULL;
  1060. }
  1061. FT_Set_Transform(face, &m, &v);
  1062. fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
  1063. if (fterr)
  1064. {
  1065. fz_warn(ctx, "FT_Load_Glyph(%s,%d,FT_LOAD_NO_HINTING): %s", font->name, gid, ft_error_string(fterr));
  1066. return NULL;
  1067. }
  1068. fterr = FT_Stroker_New(ctx->font->ftlib, &stroker);
  1069. if (fterr)
  1070. {
  1071. fz_warn(ctx, "FT_Stroker_New(): %s", ft_error_string(fterr));
  1072. return NULL;
  1073. }
  1074. line_join =
  1075. state->linejoin == FZ_LINEJOIN_MITER ? FT_STROKER_LINEJOIN_MITER_FIXED :
  1076. state->linejoin == FZ_LINEJOIN_ROUND ? FT_STROKER_LINEJOIN_ROUND :
  1077. state->linejoin == FZ_LINEJOIN_BEVEL ? FT_STROKER_LINEJOIN_BEVEL :
  1078. FT_STROKER_LINEJOIN_MITER_VARIABLE;
  1079. line_cap =
  1080. state->start_cap == FZ_LINECAP_BUTT ? FT_STROKER_LINECAP_BUTT :
  1081. state->start_cap == FZ_LINECAP_ROUND ? FT_STROKER_LINECAP_ROUND :
  1082. state->start_cap == FZ_LINECAP_SQUARE ? FT_STROKER_LINECAP_SQUARE :
  1083. state->start_cap == FZ_LINECAP_TRIANGLE ? FT_STROKER_LINECAP_BUTT :
  1084. FT_STROKER_LINECAP_BUTT;
  1085. FT_Stroker_Set(stroker, linewidth, line_cap, line_join, state->miterlimit * 65536);
  1086. fterr = FT_Get_Glyph(face->glyph, &glyph);
  1087. if (fterr)
  1088. {
  1089. fz_warn(ctx, "FT_Get_Glyph(): %s", ft_error_string(fterr));
  1090. FT_Stroker_Done(stroker);
  1091. return NULL;
  1092. }
  1093. fterr = FT_Glyph_Stroke(&glyph, stroker, 1);
  1094. if (fterr)
  1095. {
  1096. fz_warn(ctx, "FT_Glyph_Stroke(): %s", ft_error_string(fterr));
  1097. FT_Done_Glyph(glyph);
  1098. FT_Stroker_Done(stroker);
  1099. return NULL;
  1100. }
  1101. FT_Stroker_Done(stroker);
  1102. fterr = FT_Glyph_To_Bitmap(&glyph, aa > 0 ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
  1103. if (fterr)
  1104. {
  1105. fz_warn(ctx, "FT_Glyph_To_Bitmap(): %s", ft_error_string(fterr));
  1106. FT_Done_Glyph(glyph);
  1107. return NULL;
  1108. }
  1109. return glyph;
  1110. }
  1111. fz_glyph *
  1112. fz_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, const fz_stroke_state *state, int aa)
  1113. {
  1114. FT_Glyph glyph = do_render_ft_stroked_glyph(ctx, font, gid, trm, ctm, state, aa);
  1115. FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
  1116. fz_glyph *result = NULL;
  1117. if (bitmap == NULL)
  1118. {
  1119. fz_ft_unlock(ctx);
  1120. return NULL;
  1121. }
  1122. fz_try(ctx)
  1123. {
  1124. result = glyph_from_ft_bitmap(ctx, bitmap->left, bitmap->top, &bitmap->bitmap);
  1125. }
  1126. fz_always(ctx)
  1127. {
  1128. FT_Done_Glyph(glyph);
  1129. fz_ft_unlock(ctx);
  1130. }
  1131. fz_catch(ctx)
  1132. {
  1133. fz_rethrow(ctx);
  1134. }
  1135. return result;
  1136. }
  1137. static fz_rect *
  1138. get_gid_bbox(fz_context *ctx, fz_font *font, int gid)
  1139. {
  1140. int i;
  1141. if (gid < 0 || gid >= font->glyph_count || !font->use_glyph_bbox)
  1142. return NULL;
  1143. if (font->bbox_table == NULL) {
  1144. i = (font->glyph_count + 255)/256;
  1145. font->bbox_table = Memento_label(fz_malloc_array(ctx, i, fz_rect *), "bbox_table(top)");
  1146. memset(font->bbox_table, 0, sizeof(fz_rect *) * i);
  1147. }
  1148. if (font->bbox_table[gid>>8] == NULL) {
  1149. font->bbox_table[gid>>8] = Memento_label(fz_malloc_array(ctx, 256, fz_rect), "bbox_table");
  1150. for (i = 0; i < 256; i++) {
  1151. font->bbox_table[gid>>8][i] = fz_empty_rect;
  1152. }
  1153. }
  1154. return &font->bbox_table[gid>>8][gid & 255];
  1155. }
  1156. static fz_rect *
  1157. fz_bound_ft_glyph(fz_context *ctx, fz_font *font, int gid)
  1158. {
  1159. FT_Face face = font->ft_face;
  1160. FT_Error fterr;
  1161. FT_BBox cbox;
  1162. FT_Matrix m;
  1163. FT_Vector v;
  1164. fz_rect *bounds = get_gid_bbox(ctx, font, gid);
  1165. // TODO: refactor loading into fz_load_ft_glyph
  1166. // TODO: cache results
  1167. const int scale = face->units_per_EM;
  1168. const float recip = 1.0f / scale;
  1169. const float strength = 0.02f;
  1170. fz_matrix trm = fz_identity;
  1171. fz_adjust_ft_glyph_width(ctx, font, gid, &trm);
  1172. if (font->flags.fake_italic)
  1173. trm = fz_pre_shear(trm, SHEAR, 0);
  1174. m.xx = trm.a * 65536;
  1175. m.yx = trm.b * 65536;
  1176. m.xy = trm.c * 65536;
  1177. m.yy = trm.d * 65536;
  1178. v.x = trm.e * 65536;
  1179. v.y = trm.f * 65536;
  1180. fz_ft_lock(ctx);
  1181. /* Set the char size to scale=face->units_per_EM to effectively give
  1182. * us unscaled results. This avoids quantisation. We then apply the
  1183. * scale ourselves below. */
  1184. fterr = FT_Set_Char_Size(face, scale, scale, 72, 72);
  1185. if (fterr)
  1186. fz_warn(ctx, "FT_Set_Char_Size(%s,%d,72): %s", font->name, scale, ft_error_string(fterr));
  1187. FT_Set_Transform(face, &m, &v);
  1188. fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
  1189. if (fterr)
  1190. {
  1191. fz_warn(ctx, "FT_Load_Glyph(%s,%d,FT_LOAD_NO_HINTING): %s", font->name, gid, ft_error_string(fterr));
  1192. fz_ft_unlock(ctx);
  1193. bounds->x0 = bounds->x1 = trm.e;
  1194. bounds->y0 = bounds->y1 = trm.f;
  1195. return bounds;
  1196. }
  1197. if (font->flags.fake_bold)
  1198. {
  1199. FT_Outline_Embolden(&face->glyph->outline, strength * scale);
  1200. FT_Outline_Translate(&face->glyph->outline, -strength * 0.5f * scale, -strength * 0.5f * scale);
  1201. }
  1202. FT_Outline_Get_CBox(&face->glyph->outline, &cbox);
  1203. fz_ft_unlock(ctx);
  1204. bounds->x0 = cbox.xMin * recip;
  1205. bounds->y0 = cbox.yMin * recip;
  1206. bounds->x1 = cbox.xMax * recip;
  1207. bounds->y1 = cbox.yMax * recip;
  1208. if (fz_is_empty_rect(*bounds))
  1209. {
  1210. bounds->x0 = bounds->x1 = trm.e;
  1211. bounds->y0 = bounds->y1 = trm.f;
  1212. }
  1213. return bounds;
  1214. }
  1215. /* Turn FT_Outline into a fz_path */
  1216. struct closure {
  1217. fz_context *ctx;
  1218. fz_path *path;
  1219. fz_matrix trm;
  1220. };
  1221. static int move_to(const FT_Vector *p, void *cc_)
  1222. {
  1223. struct closure *cc = (struct closure *)cc_;
  1224. fz_context *ctx = cc->ctx;
  1225. fz_path *path = cc->path;
  1226. fz_point pt;
  1227. pt = fz_transform_point_xy(p->x, p->y, cc->trm);
  1228. fz_moveto(ctx, path, pt.x, pt.y);
  1229. return 0;
  1230. }
  1231. static int line_to(const FT_Vector *p, void *cc_)
  1232. {
  1233. struct closure *cc = (struct closure *)cc_;
  1234. fz_context *ctx = cc->ctx;
  1235. fz_path *path = cc->path;
  1236. fz_point pt;
  1237. pt = fz_transform_point_xy(p->x, p->y, cc->trm);
  1238. fz_lineto(ctx, path, pt.x, pt.y);
  1239. return 0;
  1240. }
  1241. static int conic_to(const FT_Vector *c, const FT_Vector *p, void *cc_)
  1242. {
  1243. struct closure *cc = (struct closure *)cc_;
  1244. fz_context *ctx = cc->ctx;
  1245. fz_path *path = cc->path;
  1246. fz_point ct, pt;
  1247. ct = fz_transform_point_xy(c->x, c->y, cc->trm);
  1248. pt = fz_transform_point_xy(p->x, p->y, cc->trm);
  1249. fz_quadto(ctx, path, ct.x, ct.y, pt.x, pt.y);
  1250. return 0;
  1251. }
  1252. static int cubic_to(const FT_Vector *c1, const FT_Vector *c2, const FT_Vector *p, void *cc_)
  1253. {
  1254. struct closure *cc = (struct closure *)cc_;
  1255. fz_context *ctx = cc->ctx;
  1256. fz_path *path = cc->path;
  1257. fz_point c1t, c2t, pt;
  1258. c1t = fz_transform_point_xy(c1->x, c1->y, cc->trm);
  1259. c2t = fz_transform_point_xy(c2->x, c2->y, cc->trm);
  1260. pt = fz_transform_point_xy(p->x, p->y, cc->trm);
  1261. fz_curveto(ctx, path, c1t.x, c1t.y, c2t.x, c2t.y, pt.x, pt.y);
  1262. return 0;
  1263. }
  1264. static const FT_Outline_Funcs outline_funcs = {
  1265. move_to, line_to, conic_to, cubic_to, 0, 0
  1266. };
  1267. fz_path *
  1268. fz_outline_ft_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
  1269. {
  1270. struct closure cc;
  1271. FT_Face face = font->ft_face;
  1272. int fterr;
  1273. const int scale = 65536;
  1274. const float recip = 1.0f / scale;
  1275. const float strength = 0.02f;
  1276. fz_adjust_ft_glyph_width(ctx, font, gid, &trm);
  1277. if (font->flags.fake_italic)
  1278. trm = fz_pre_shear(trm, SHEAR, 0);
  1279. fz_ft_lock(ctx);
  1280. fterr = FT_Set_Char_Size(face, scale, scale, 72, 72);
  1281. if (fterr)
  1282. fz_warn(ctx, "FT_Set_Char_Size(%s,%d,72): %s", font->name, scale, ft_error_string(fterr));
  1283. fterr = FT_Load_Glyph(face, gid, FT_LOAD_IGNORE_TRANSFORM);
  1284. if (fterr)
  1285. {
  1286. fz_warn(ctx, "FT_Load_Glyph(%s,%d,FT_LOAD_IGNORE_TRANSFORM): %s", font->name, gid, ft_error_string(fterr));
  1287. fterr = FT_Load_Glyph(face, gid, FT_LOAD_IGNORE_TRANSFORM | FT_LOAD_NO_HINTING);
  1288. }
  1289. if (fterr)
  1290. {
  1291. fz_warn(ctx, "FT_Load_Glyph(%s,%d,FT_LOAD_IGNORE_TRANSFORM | FT_LOAD_NO_HINTING): %s", font->name, gid, ft_error_string(fterr));
  1292. fz_ft_unlock(ctx);
  1293. return NULL;
  1294. }
  1295. if (font->flags.fake_bold)
  1296. {
  1297. FT_Outline_Embolden(&face->glyph->outline, strength * scale);
  1298. FT_Outline_Translate(&face->glyph->outline, -strength * 0.5f * scale, -strength * 0.5f * scale);
  1299. }
  1300. cc.path = NULL;
  1301. fz_try(ctx)
  1302. {
  1303. cc.ctx = ctx;
  1304. cc.path = fz_new_path(ctx);
  1305. cc.trm = fz_concat(fz_scale(recip, recip), trm);
  1306. fz_moveto(ctx, cc.path, cc.trm.e, cc.trm.f);
  1307. FT_Outline_Decompose(&face->glyph->outline, &outline_funcs, &cc);
  1308. fz_closepath(ctx, cc.path);
  1309. }
  1310. fz_always(ctx)
  1311. {
  1312. fz_ft_unlock(ctx);
  1313. }
  1314. fz_catch(ctx)
  1315. {
  1316. fz_warn(ctx, "freetype cannot decompose outline");
  1317. fz_drop_path(ctx, cc.path);
  1318. return NULL;
  1319. }
  1320. return cc.path;
  1321. }
  1322. /*
  1323. Type 3 fonts...
  1324. */
  1325. fz_font *
  1326. fz_new_type3_font(fz_context *ctx, const char *name, fz_matrix matrix)
  1327. {
  1328. fz_font *font;
  1329. font = fz_new_font(ctx, name, 1, 256);
  1330. fz_try(ctx)
  1331. {
  1332. font->t3procs = fz_calloc(ctx, 256, sizeof(fz_buffer*));
  1333. font->t3lists = fz_calloc(ctx, 256, sizeof(fz_display_list*));
  1334. font->t3widths = fz_calloc(ctx, 256, sizeof(float));
  1335. font->t3flags = fz_calloc(ctx, 256, sizeof(unsigned short));
  1336. }
  1337. fz_catch(ctx)
  1338. {
  1339. fz_drop_font(ctx, font);
  1340. fz_rethrow(ctx);
  1341. }
  1342. font->t3matrix = matrix;
  1343. return font;
  1344. }
  1345. static void
  1346. fz_bound_t3_glyph(fz_context *ctx, fz_font *font, int gid)
  1347. {
  1348. fz_display_list *list;
  1349. fz_device *dev;
  1350. fz_rect *r = get_gid_bbox(ctx, font, gid);
  1351. list = font->t3lists[gid];
  1352. if (!list)
  1353. {
  1354. *r = fz_empty_rect;
  1355. return;
  1356. }
  1357. dev = fz_new_bbox_device(ctx, r);
  1358. fz_try(ctx)
  1359. {
  1360. fz_run_display_list(ctx, list, dev, font->t3matrix, fz_infinite_rect, NULL);
  1361. fz_close_device(ctx, dev);
  1362. }
  1363. fz_always(ctx)
  1364. {
  1365. fz_drop_device(ctx, dev);
  1366. }
  1367. fz_catch(ctx)
  1368. {
  1369. fz_rethrow(ctx);
  1370. }
  1371. /* Update font bbox with glyph's computed bbox if the font bbox is invalid */
  1372. if (font->flags.invalid_bbox)
  1373. font->bbox = fz_union_rect(font->bbox, *r);
  1374. }
  1375. void
  1376. fz_prepare_t3_glyph(fz_context *ctx, fz_font *font, int gid)
  1377. {
  1378. fz_device *dev;
  1379. fz_rect d1_rect;
  1380. /* We've not already loaded this one! */
  1381. assert(font->t3lists[gid] == NULL);
  1382. font->t3lists[gid] = fz_new_display_list(ctx, font->bbox);
  1383. dev = fz_new_list_device(ctx, font->t3lists[gid]);
  1384. dev->flags = FZ_DEVFLAG_FILLCOLOR_UNDEFINED |
  1385. FZ_DEVFLAG_STROKECOLOR_UNDEFINED |
  1386. FZ_DEVFLAG_STARTCAP_UNDEFINED |
  1387. FZ_DEVFLAG_DASHCAP_UNDEFINED |
  1388. FZ_DEVFLAG_ENDCAP_UNDEFINED |
  1389. FZ_DEVFLAG_LINEJOIN_UNDEFINED |
  1390. FZ_DEVFLAG_MITERLIMIT_UNDEFINED |
  1391. FZ_DEVFLAG_LINEWIDTH_UNDEFINED |
  1392. FZ_DEVFLAG_DASH_PATTERN_UNDEFINED;
  1393. fz_try(ctx)
  1394. {
  1395. font->t3run(ctx, font->t3doc, font->t3resources, font->t3procs[gid], dev, fz_identity, NULL, NULL, NULL, NULL);
  1396. fz_close_device(ctx, dev);
  1397. font->t3flags[gid] = dev->flags;
  1398. d1_rect = dev->d1_rect;
  1399. }
  1400. fz_always(ctx)
  1401. {
  1402. fz_drop_device(ctx, dev);
  1403. }
  1404. fz_catch(ctx)
  1405. fz_rethrow(ctx);
  1406. if (fz_display_list_is_empty(ctx, font->t3lists[gid]))
  1407. {
  1408. fz_rect *r = get_gid_bbox(ctx, font, gid);
  1409. /* If empty, no need for a huge bbox, especially as the logic
  1410. * in the 'else if' can make it huge. */
  1411. r->x0 = font->flags.invalid_bbox ? 0 : font->bbox.x0;
  1412. r->y0 = font->flags.invalid_bbox ? 0 : font->bbox.y0;
  1413. r->x1 = r->x0 + .00001f;
  1414. r->y1 = r->y0 + .00001f;
  1415. }
  1416. else if (font->t3flags[gid] & FZ_DEVFLAG_BBOX_DEFINED)
  1417. {
  1418. fz_rect *r = get_gid_bbox(ctx, font, gid);
  1419. *r = fz_transform_rect(d1_rect, font->t3matrix);
  1420. if (font->flags.invalid_bbox || !fz_contains_rect(font->bbox, d1_rect))
  1421. {
  1422. /* Either the font bbox is invalid, or the d1_rect returned is
  1423. * incompatible with it. Either way, don't trust the d1 rect
  1424. * and calculate it from the contents. */
  1425. fz_bound_t3_glyph(ctx, font, gid);
  1426. }
  1427. }
  1428. else
  1429. {
  1430. /* No bbox has been defined for this glyph, so compute it. */
  1431. fz_bound_t3_glyph(ctx, font, gid);
  1432. }
  1433. }
  1434. void
  1435. fz_run_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_device *dev)
  1436. {
  1437. fz_display_list *list;
  1438. fz_matrix ctm;
  1439. list = font->t3lists[gid];
  1440. if (!list)
  1441. return;
  1442. ctm = fz_concat(font->t3matrix, trm);
  1443. fz_run_display_list(ctx, list, dev, ctm, fz_infinite_rect, NULL);
  1444. }
  1445. fz_pixmap *
  1446. fz_render_t3_glyph_pixmap(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_colorspace *model, const fz_irect *scissor, int aa)
  1447. {
  1448. fz_display_list *list;
  1449. fz_rect bounds;
  1450. fz_irect bbox;
  1451. fz_device *dev = NULL;
  1452. fz_pixmap *glyph;
  1453. fz_pixmap *result = NULL;
  1454. if (gid < 0 || gid > 255)
  1455. return NULL;
  1456. list = font->t3lists[gid];
  1457. if (!list)
  1458. return NULL;
  1459. if (font->t3flags[gid] & FZ_DEVFLAG_MASK)
  1460. {
  1461. if (font->t3flags[gid] & FZ_DEVFLAG_COLOR)
  1462. fz_warn(ctx, "type3 glyph claims to be both masked and colored");
  1463. model = NULL;
  1464. }
  1465. else if (font->t3flags[gid] & FZ_DEVFLAG_COLOR)
  1466. {
  1467. if (!model)
  1468. fz_warn(ctx, "colored type3 glyph wanted in masked context");
  1469. }
  1470. else
  1471. {
  1472. fz_warn(ctx, "type3 glyph doesn't specify masked or colored");
  1473. model = NULL; /* Treat as masked */
  1474. }
  1475. bounds = fz_expand_rect(fz_bound_glyph(ctx, font, gid, trm), 1);
  1476. bbox = fz_irect_from_rect(bounds);
  1477. bbox = fz_intersect_irect(bbox, *scissor);
  1478. /* Glyphs must always have alpha */
  1479. glyph = fz_new_pixmap_with_bbox(ctx, model, bbox, NULL/* FIXME */, 1);
  1480. fz_var(dev);
  1481. fz_try(ctx)
  1482. {
  1483. fz_clear_pixmap(ctx, glyph);
  1484. dev = fz_new_draw_device_type3(ctx, fz_identity, glyph);
  1485. fz_run_t3_glyph(ctx, font, gid, trm, dev);
  1486. fz_close_device(ctx, dev);
  1487. }
  1488. fz_always(ctx)
  1489. {
  1490. fz_drop_device(ctx, dev);
  1491. }
  1492. fz_catch(ctx)
  1493. {
  1494. fz_drop_pixmap(ctx, glyph);
  1495. fz_rethrow(ctx);
  1496. }
  1497. if (!model)
  1498. {
  1499. fz_try(ctx)
  1500. {
  1501. result = fz_alpha_from_gray(ctx, glyph);
  1502. }
  1503. fz_always(ctx)
  1504. {
  1505. fz_drop_pixmap(ctx, glyph);
  1506. }
  1507. fz_catch(ctx)
  1508. {
  1509. fz_rethrow(ctx);
  1510. }
  1511. }
  1512. else
  1513. result = glyph;
  1514. return result;
  1515. }
  1516. fz_glyph *
  1517. fz_render_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_colorspace *model, const fz_irect *scissor, int aa)
  1518. {
  1519. fz_pixmap *pixmap = fz_render_t3_glyph_pixmap(ctx, font, gid, trm, model, scissor, aa);
  1520. return fz_new_glyph_from_pixmap(ctx, pixmap);
  1521. }
  1522. void
  1523. fz_render_t3_glyph_direct(fz_context *ctx, fz_device *dev, fz_font *font, int gid, fz_matrix trm, void *gstate, fz_default_colorspaces *def_cs, void *fill_gstate, void *stroke_gstate)
  1524. {
  1525. fz_matrix ctm;
  1526. if (gid < 0 || gid > 255)
  1527. return;
  1528. if (font->t3flags[gid] & FZ_DEVFLAG_MASK)
  1529. {
  1530. if (font->t3flags[gid] & FZ_DEVFLAG_COLOR)
  1531. fz_warn(ctx, "type3 glyph claims to be both masked and colored");
  1532. }
  1533. else if (!(font->t3flags[gid] & FZ_DEVFLAG_COLOR))
  1534. {
  1535. fz_warn(ctx, "type3 glyph doesn't specify masked or colored");
  1536. }
  1537. ctm = fz_concat(font->t3matrix, trm);
  1538. font->t3run(ctx, font->t3doc, font->t3resources, font->t3procs[gid], dev, ctm, gstate, def_cs, fill_gstate, stroke_gstate);
  1539. }
  1540. fz_rect
  1541. fz_bound_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
  1542. {
  1543. fz_rect rect;
  1544. fz_rect *r = get_gid_bbox(ctx, font, gid);
  1545. if (r)
  1546. {
  1547. /* If the bbox is infinite or empty, distrust it */
  1548. if (fz_is_infinite_rect(*r) || fz_is_empty_rect(*r))
  1549. {
  1550. /* Get the real size from the glyph */
  1551. if (font->ft_face)
  1552. fz_bound_ft_glyph(ctx, font, gid);
  1553. else if (font->t3lists)
  1554. fz_bound_t3_glyph(ctx, font, gid);
  1555. else
  1556. /* If we can't get a real size, fall back to the font
  1557. * bbox. */
  1558. *r = font->bbox;
  1559. /* If the real size came back as empty, then store it as
  1560. * a very small rectangle to avoid us calling this same
  1561. * check every time. */
  1562. if (fz_is_empty_rect(*r))
  1563. {
  1564. r->x0 = 0;
  1565. r->y0 = 0;
  1566. r->x1 = 0.0000001f;
  1567. r->y1 = 0.0000001f;
  1568. }
  1569. }
  1570. rect = *r;
  1571. }
  1572. else
  1573. {
  1574. /* fall back to font bbox */
  1575. rect = font->bbox;
  1576. }
  1577. return fz_transform_rect(rect, trm);
  1578. }
  1579. fz_path *
  1580. fz_outline_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix ctm)
  1581. {
  1582. if (!font->ft_face)
  1583. return NULL;
  1584. return fz_outline_ft_glyph(ctx, font, gid, ctm);
  1585. }
  1586. int fz_glyph_cacheable(fz_context *ctx, fz_font *font, int gid)
  1587. {
  1588. if (!font->t3procs || !font->t3flags || gid < 0 || gid >= font->glyph_count)
  1589. return 1;
  1590. return (font->t3flags[gid] & FZ_DEVFLAG_UNCACHEABLE) == 0;
  1591. }
  1592. static float
  1593. fz_advance_ft_glyph_aux(fz_context *ctx, fz_font *font, int gid, int wmode, int locked)
  1594. {
  1595. FT_Error fterr;
  1596. FT_Fixed adv = 0;
  1597. int mask;
  1598. /* PDF and substitute font widths. */
  1599. if (font->flags.ft_stretch)
  1600. {
  1601. if (font->width_table)
  1602. {
  1603. if (gid < font->width_count)
  1604. return font->width_table[gid] / 1000.0f;
  1605. return font->width_default / 1000.0f;
  1606. }
  1607. }
  1608. mask = FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_IGNORE_TRANSFORM;
  1609. if (wmode)
  1610. mask |= FT_LOAD_VERTICAL_LAYOUT;
  1611. if (!locked)
  1612. fz_ft_lock(ctx);
  1613. fterr = FT_Get_Advance(font->ft_face, gid, mask, &adv);
  1614. if (!locked)
  1615. fz_ft_unlock(ctx);
  1616. if (fterr && fterr != FT_Err_Invalid_Argument)
  1617. {
  1618. fz_warn(ctx, "FT_Get_Advance(%s,%d): %s", font->name, gid, ft_error_string(fterr));
  1619. if (font->width_table)
  1620. {
  1621. if (gid < font->width_count)
  1622. return font->width_table[gid] / 1000.0f;
  1623. return font->width_default / 1000.0f;
  1624. }
  1625. }
  1626. return (float) adv / ((FT_Face)font->ft_face)->units_per_EM;
  1627. }
  1628. static float
  1629. fz_advance_ft_glyph(fz_context *ctx, fz_font *font, int gid, int wmode)
  1630. {
  1631. return fz_advance_ft_glyph_aux(ctx, font, gid, wmode, 0);
  1632. }
  1633. static float
  1634. fz_advance_t3_glyph(fz_context *ctx, fz_font *font, int gid)
  1635. {
  1636. if (gid < 0 || gid > 255)
  1637. return 0;
  1638. return font->t3widths[gid];
  1639. }
  1640. void
  1641. fz_get_glyph_name(fz_context *ctx, fz_font *font, int glyph, char *buf, int size)
  1642. {
  1643. FT_Face face = font->ft_face;
  1644. if (face)
  1645. {
  1646. if (FT_HAS_GLYPH_NAMES(face))
  1647. {
  1648. int fterr;
  1649. fz_ft_lock(ctx);
  1650. fterr = FT_Get_Glyph_Name(face, glyph, buf, size);
  1651. fz_ft_unlock(ctx);
  1652. if (fterr)
  1653. fz_warn(ctx, "FT_Get_Glyph_Name(%s,%d): %s", font->name, glyph, ft_error_string(fterr));
  1654. }
  1655. else
  1656. fz_snprintf(buf, size, "%d", glyph);
  1657. }
  1658. else
  1659. {
  1660. fz_snprintf(buf, size, "%d", glyph);
  1661. }
  1662. }
  1663. float
  1664. fz_advance_glyph(fz_context *ctx, fz_font *font, int gid, int wmode)
  1665. {
  1666. if (font->ft_face)
  1667. {
  1668. if (wmode)
  1669. return fz_advance_ft_glyph(ctx, font, gid, 1);
  1670. if (gid >= 0 && gid < font->glyph_count)
  1671. {
  1672. float f;
  1673. int block = gid>>8;
  1674. fz_ft_lock(ctx);
  1675. if (!font->advance_cache)
  1676. {
  1677. int n = (font->glyph_count+255)/256;
  1678. fz_try(ctx)
  1679. font->advance_cache = Memento_label(fz_malloc_array(ctx, n, float *), "font_advance_cache");
  1680. fz_catch(ctx)
  1681. {
  1682. fz_ft_unlock(ctx);
  1683. fz_rethrow(ctx);
  1684. }
  1685. memset(font->advance_cache, 0, n * sizeof(float *));
  1686. }
  1687. if (!font->advance_cache[block])
  1688. {
  1689. int i, n;
  1690. fz_try(ctx)
  1691. font->advance_cache[block] = Memento_label(fz_malloc_array(ctx, 256, float), "font_advance_cache");
  1692. fz_catch(ctx)
  1693. {
  1694. fz_ft_unlock(ctx);
  1695. fz_rethrow(ctx);
  1696. }
  1697. n = (block<<8)+256;
  1698. if (n > font->glyph_count)
  1699. n = font->glyph_count;
  1700. n -= (block<<8);
  1701. for (i = 0; i < n; ++i)
  1702. font->advance_cache[block][i] = fz_advance_ft_glyph_aux(ctx, font, (block<<8)+i, 0, 1);
  1703. }
  1704. f = font->advance_cache[block][gid & 255];
  1705. fz_ft_unlock(ctx);
  1706. return f;
  1707. }
  1708. return fz_advance_ft_glyph(ctx, font, gid, 0);
  1709. }
  1710. if (font->t3procs)
  1711. return fz_advance_t3_glyph(ctx, font, gid);
  1712. return 0;
  1713. }
  1714. int
  1715. fz_encode_character(fz_context *ctx, fz_font *font, int ucs)
  1716. {
  1717. if (font->ft_face)
  1718. {
  1719. int idx;
  1720. if (ucs >= 0 && ucs < 0x10000)
  1721. {
  1722. int pg = ucs >> 8;
  1723. int ix = ucs & 0xFF;
  1724. if (!font->encoding_cache[pg])
  1725. {
  1726. int i;
  1727. font->encoding_cache[pg] = fz_malloc_array(ctx, 256, uint16_t);
  1728. fz_ft_lock(ctx);
  1729. for (i = 0; i < 256; ++i)
  1730. font->encoding_cache[pg][i] = FT_Get_Char_Index(font->ft_face, (pg << 8) + i);
  1731. fz_ft_unlock(ctx);
  1732. }
  1733. return font->encoding_cache[pg][ix];
  1734. }
  1735. fz_ft_lock(ctx);
  1736. idx = FT_Get_Char_Index(font->ft_face, ucs);
  1737. fz_ft_unlock(ctx);
  1738. return idx;
  1739. }
  1740. return ucs;
  1741. }
  1742. int
  1743. fz_encode_character_sc(fz_context *ctx, fz_font *font, int unicode)
  1744. {
  1745. if (font->ft_face)
  1746. {
  1747. int cat = ucdn_get_general_category(unicode);
  1748. if (cat == UCDN_GENERAL_CATEGORY_LL || cat == UCDN_GENERAL_CATEGORY_LT)
  1749. {
  1750. int glyph;
  1751. const char *name;
  1752. char buf[20];
  1753. name = fz_glyph_name_from_unicode_sc(unicode);
  1754. if (name)
  1755. {
  1756. fz_ft_lock(ctx);
  1757. glyph = FT_Get_Name_Index(font->ft_face, (char*)name);
  1758. fz_ft_unlock(ctx);
  1759. if (glyph > 0)
  1760. return glyph;
  1761. }
  1762. sprintf(buf, "uni%04X.sc", unicode);
  1763. fz_ft_lock(ctx);
  1764. glyph = FT_Get_Name_Index(font->ft_face, buf);
  1765. fz_ft_unlock(ctx);
  1766. if (glyph > 0)
  1767. return glyph;
  1768. }
  1769. }
  1770. return fz_encode_character(ctx, font, unicode);
  1771. }
  1772. int
  1773. fz_encode_character_by_glyph_name(fz_context *ctx, fz_font *font, const char *glyphname)
  1774. {
  1775. int glyph = 0;
  1776. if (font->ft_face)
  1777. {
  1778. fz_ft_lock(ctx);
  1779. glyph = ft_name_index(font->ft_face, glyphname);
  1780. if (glyph == 0)
  1781. glyph = ft_char_index(font->ft_face, fz_unicode_from_glyph_name(glyphname));
  1782. fz_ft_unlock(ctx);
  1783. }
  1784. // TODO: type3 fonts (not needed for now)
  1785. return glyph;
  1786. }
  1787. /* FIXME: This should take language too eventually, to allow for fonts where we can select different
  1788. * languages using opentype features. */
  1789. int
  1790. fz_encode_character_with_fallback(fz_context *ctx, fz_font *user_font, int unicode, int script, int language, fz_font **out_font)
  1791. {
  1792. fz_font *font;
  1793. int is_serif = user_font->flags.is_serif;
  1794. int is_italic = user_font->flags.is_italic | user_font->flags.fake_italic;
  1795. int is_bold = user_font->flags.is_bold | user_font->flags.fake_bold;
  1796. int gid;
  1797. gid = fz_encode_character(ctx, user_font, unicode);
  1798. if (gid > 0)
  1799. return *out_font = user_font, gid;
  1800. if (script == 0)
  1801. script = ucdn_get_script(unicode);
  1802. /* Fix for ideographic/halfwidth/fullwidth punctuation forms. */
  1803. if ((unicode >= 0x3000 && unicode <= 0x303F) || (unicode >= 0xFF00 && unicode <= 0xFFEF))
  1804. {
  1805. if (script != UCDN_SCRIPT_HANGUL &&
  1806. script != UCDN_SCRIPT_HIRAGANA &&
  1807. script != UCDN_SCRIPT_KATAKANA &&
  1808. script != UCDN_SCRIPT_BOPOMOFO)
  1809. script = UCDN_SCRIPT_HAN;
  1810. }
  1811. font = fz_load_fallback_font(ctx, script, language, is_serif, is_bold, is_italic);
  1812. if (font)
  1813. {
  1814. gid = fz_encode_character(ctx, font, unicode);
  1815. if (gid > 0)
  1816. return *out_font = font, gid;
  1817. }
  1818. #ifndef TOFU_CJK_LANG
  1819. if (script == UCDN_SCRIPT_HAN)
  1820. {
  1821. font = fz_load_fallback_font(ctx, script, FZ_LANG_zh_Hant, is_serif, is_bold, is_italic);
  1822. if (font)
  1823. {
  1824. gid = fz_encode_character(ctx, font, unicode);
  1825. if (gid > 0)
  1826. return *out_font = font, gid;
  1827. }
  1828. font = fz_load_fallback_font(ctx, script, FZ_LANG_ja, is_serif, is_bold, is_italic);
  1829. if (font)
  1830. {
  1831. gid = fz_encode_character(ctx, font, unicode);
  1832. if (gid > 0)
  1833. return *out_font = font, gid;
  1834. }
  1835. font = fz_load_fallback_font(ctx, script, FZ_LANG_ko, is_serif, is_bold, is_italic);
  1836. if (font)
  1837. {
  1838. gid = fz_encode_character(ctx, font, unicode);
  1839. if (gid > 0)
  1840. return *out_font = font, gid;
  1841. }
  1842. font = fz_load_fallback_font(ctx, script, FZ_LANG_zh_Hans, is_serif, is_bold, is_italic);
  1843. if (font)
  1844. {
  1845. gid = fz_encode_character(ctx, font, unicode);
  1846. if (gid > 0)
  1847. return *out_font = font, gid;
  1848. }
  1849. }
  1850. #endif
  1851. font = fz_load_fallback_math_font(ctx);
  1852. if (font)
  1853. {
  1854. gid = fz_encode_character(ctx, font, unicode);
  1855. if (gid > 0)
  1856. return *out_font = font, gid;
  1857. }
  1858. font = fz_load_fallback_music_font(ctx);
  1859. if (font)
  1860. {
  1861. gid = fz_encode_character(ctx, font, unicode);
  1862. if (gid > 0)
  1863. return *out_font = font, gid;
  1864. }
  1865. font = fz_load_fallback_symbol1_font(ctx);
  1866. if (font)
  1867. {
  1868. gid = fz_encode_character(ctx, font, unicode);
  1869. if (gid > 0)
  1870. return *out_font = font, gid;
  1871. }
  1872. font = fz_load_fallback_symbol2_font(ctx);
  1873. if (font)
  1874. {
  1875. gid = fz_encode_character(ctx, font, unicode);
  1876. if (gid > 0)
  1877. return *out_font = font, gid;
  1878. }
  1879. font = fz_load_fallback_emoji_font(ctx);
  1880. if (font)
  1881. {
  1882. gid = fz_encode_character(ctx, font, unicode);
  1883. if (gid > 0)
  1884. return *out_font = font, gid;
  1885. }
  1886. font = fz_load_fallback_boxes_font(ctx);
  1887. if (font)
  1888. {
  1889. gid = fz_encode_character(ctx, font, unicode);
  1890. if (gid > 0)
  1891. return *out_font = font, gid;
  1892. }
  1893. font = fz_new_base14_font(ctx, "Symbol");
  1894. if (font)
  1895. {
  1896. fz_drop_font(ctx, font); /* it's cached in the font context, return a borrowed pointer */
  1897. gid = fz_encode_character(ctx, font, unicode);
  1898. if (gid > 0)
  1899. return *out_font = font, gid;
  1900. }
  1901. return *out_font = user_font, 0;
  1902. }
  1903. int fz_font_is_bold(fz_context *ctx, fz_font *font)
  1904. {
  1905. return font ? font->flags.is_bold : 0;
  1906. }
  1907. int fz_font_is_italic(fz_context *ctx, fz_font *font)
  1908. {
  1909. return font ? font->flags.is_italic : 0;
  1910. }
  1911. int fz_font_is_serif(fz_context *ctx, fz_font *font)
  1912. {
  1913. return font ? font->flags.is_serif : 0;
  1914. }
  1915. int fz_font_is_monospaced(fz_context *ctx, fz_font *font)
  1916. {
  1917. return font ? font->flags.is_mono : 0;
  1918. }
  1919. const char *fz_font_name(fz_context *ctx, fz_font *font)
  1920. {
  1921. return font ? font->name : "";
  1922. }
  1923. fz_buffer **fz_font_t3_procs(fz_context *ctx, fz_font *font)
  1924. {
  1925. return font ? font->t3procs : NULL;
  1926. }
  1927. fz_rect fz_font_bbox(fz_context *ctx, fz_font *font)
  1928. {
  1929. return font->bbox;
  1930. }
  1931. void *fz_font_ft_face(fz_context *ctx, fz_font *font)
  1932. {
  1933. return font ? font->ft_face : NULL;
  1934. }
  1935. fz_font_flags_t *fz_font_flags(fz_font *font)
  1936. {
  1937. return font ? &font->flags : NULL;
  1938. }
  1939. fz_shaper_data_t *fz_font_shaper_data(fz_context *ctx, fz_font *font)
  1940. {
  1941. return font ? &font->shaper_data : NULL;
  1942. }
  1943. void fz_font_digest(fz_context *ctx, fz_font *font, unsigned char digest[16])
  1944. {
  1945. if (!font->buffer)
  1946. fz_throw(ctx, FZ_ERROR_ARGUMENT, "no font file for digest");
  1947. if (!font->has_digest)
  1948. {
  1949. fz_md5_buffer(ctx, font->buffer, font->digest);
  1950. font->has_digest = 1;
  1951. }
  1952. memcpy(digest, font->digest, 16);
  1953. }
  1954. #define CHR(a,b,c,d) ((a<<24) | (b<<16) | (c<<8) | d)
  1955. typedef struct
  1956. {
  1957. uint32_t offset;
  1958. uint32_t length;
  1959. } ttc_block_details_t;
  1960. /* The operation of the following is largely based on the operation of
  1961. * https://github.com/fontist/extract_ttc/blob/main/ext/stripttc/stripttc.c
  1962. * released under a BSD 3-clause license.
  1963. */
  1964. fz_buffer *
  1965. fz_extract_ttf_from_ttc(fz_context *ctx, fz_font *font)
  1966. {
  1967. fz_stream *stream;
  1968. uint32_t tmp;
  1969. int i, count;
  1970. fz_buffer *buf = NULL;
  1971. fz_output *out = NULL;
  1972. ttc_block_details_t *bd = NULL;
  1973. uint32_t start_pos;
  1974. uint32_t csumpos = 0;
  1975. if (!font || !font->buffer)
  1976. fz_throw(ctx, FZ_ERROR_ARGUMENT, "missing input");
  1977. stream = fz_open_buffer(ctx, font->buffer);
  1978. fz_var(buf);
  1979. fz_var(out);
  1980. fz_var(bd);
  1981. fz_try(ctx)
  1982. {
  1983. /* Signature */
  1984. if (fz_read_uint32(ctx, stream) != CHR('t','t','c','f'))
  1985. fz_throw(ctx, FZ_ERROR_FORMAT, "Not a ttc");
  1986. /* Version */
  1987. tmp = fz_read_uint32(ctx, stream);
  1988. if (tmp != 0x10000 && tmp != 0x20000)
  1989. fz_throw(ctx, FZ_ERROR_FORMAT, "Unsupported TTC version");
  1990. /* How many subfonts are there? */
  1991. tmp = fz_read_uint32(ctx, stream);
  1992. if ((uint32_t)font->subfont >= tmp || font->subfont < 0)
  1993. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad subfont in TTC");
  1994. /* Read through the index table until we get the one for our subfont. */
  1995. for (i = 0; i <= font->subfont; i++)
  1996. tmp = fz_read_uint32(ctx, stream);
  1997. fz_seek(ctx, stream, tmp, SEEK_SET);
  1998. buf = fz_new_buffer(ctx, 1);
  1999. out = fz_new_output_with_buffer(ctx, buf);
  2000. fz_write_uint32_be(ctx, out, fz_read_uint32(ctx, stream)); /* sfnt version */
  2001. fz_write_uint16_be(ctx, out, count = fz_read_uint16(ctx, stream)); /* table count */
  2002. fz_write_uint16_be(ctx, out, fz_read_uint16(ctx, stream)); /* bsearch header */
  2003. fz_write_uint16_be(ctx, out, fz_read_uint16(ctx, stream));
  2004. fz_write_uint16_be(ctx, out, fz_read_uint16(ctx, stream));
  2005. /* We are currently here... */
  2006. start_pos = 4+2+2+2+2;
  2007. /* And after we've written the header, we will be here. */
  2008. start_pos += count*4*4;
  2009. bd = fz_malloc_array(ctx, count, ttc_block_details_t);
  2010. for (i = 0; i < count; i++)
  2011. {
  2012. uint32_t tag;
  2013. fz_write_uint32_be(ctx, out, tag = fz_read_uint32(ctx, stream));
  2014. fz_write_uint32_be(ctx, out, fz_read_uint32(ctx, stream)); /* checksum */
  2015. bd[i].offset = fz_read_uint32(ctx, stream);
  2016. fz_write_uint32_be(ctx, out, start_pos);
  2017. if (tag == CHR('h','e','a','d'))
  2018. csumpos = start_pos + 8;
  2019. fz_write_uint32_be(ctx, out, bd[i].length = fz_read_uint32(ctx, stream));
  2020. start_pos += (bd[i].length + 3) & ~3;
  2021. }
  2022. for (i = 0; i < count; i++)
  2023. {
  2024. uint32_t j;
  2025. fz_seek(ctx, stream, bd[i].offset, SEEK_SET);
  2026. for (j = 0; j < bd[i].length; j++)
  2027. fz_write_byte(ctx, out, fz_read_byte(ctx, stream));
  2028. if (bd[i].length & 1)
  2029. {
  2030. fz_write_byte(ctx, out, 0);
  2031. bd[i].length++;
  2032. }
  2033. if (bd[i].length & 2)
  2034. fz_write_uint16_be(ctx, out, 0);
  2035. }
  2036. fz_close_output(ctx, out);
  2037. }
  2038. fz_always(ctx)
  2039. {
  2040. fz_free(ctx, bd);
  2041. fz_drop_output(ctx, out);
  2042. fz_drop_stream(ctx, stream);
  2043. }
  2044. fz_catch(ctx)
  2045. {
  2046. fz_drop_buffer(ctx, buf);
  2047. fz_rethrow(ctx);
  2048. }
  2049. /* Now fixup the checksum */
  2050. if (csumpos)
  2051. {
  2052. unsigned char *data;
  2053. uint32_t sum = 0;
  2054. uint32_t j;
  2055. size_t len = fz_buffer_storage(ctx, buf, &data);
  2056. /* First off, blat the old checksum */
  2057. memset(data+csumpos, 0, 4);
  2058. /* Calculate the new sum. */
  2059. for (j = 0; j < len; j += 4)
  2060. {
  2061. uint32_t v = (data[j]<<24) | (data[j+1]<<16) | (data[j+2]<<8) | (data[j+3]);
  2062. sum += v;
  2063. }
  2064. sum = 0xb1b0afba-sum;
  2065. /* Insert it. */
  2066. data[csumpos] = sum>>24;
  2067. data[csumpos+1] = sum>>16;
  2068. data[csumpos+2] = sum>>8;
  2069. data[csumpos+3] = sum;
  2070. }
  2071. return buf;
  2072. }
  2073. void fz_enumerate_font_cmap(fz_context *ctx, fz_font *font, fz_cmap_callback *cb, void *opaque)
  2074. {
  2075. unsigned long ucs;
  2076. unsigned int gid;
  2077. if (font == NULL || font->ft_face == NULL)
  2078. return;
  2079. fz_ft_lock(ctx);
  2080. for (ucs = FT_Get_First_Char(font->ft_face, &gid); gid > 0; ucs = FT_Get_Next_Char(font->ft_face, ucs, &gid))
  2081. {
  2082. fz_ft_unlock(ctx);
  2083. cb(ctx, opaque, ucs, gid);
  2084. fz_ft_lock(ctx);
  2085. }
  2086. fz_ft_unlock(ctx);
  2087. }
  2088. void fz_calculate_font_ascender_descender(fz_context *ctx, fz_font *font)
  2089. {
  2090. int i, n;
  2091. fz_rect bounds = fz_empty_rect;
  2092. fz_matrix trm = { 1, 0, 0, 1, 0, 0 };
  2093. if (font == NULL)
  2094. return;
  2095. if (font->ascdesc_src == FZ_ASCDESC_FROM_BOUNDS)
  2096. return;
  2097. n = font->glyph_count;
  2098. for (i = 0; i < n; i++)
  2099. {
  2100. bounds = fz_union_rect(bounds, fz_bound_glyph(ctx, font, i, trm));
  2101. }
  2102. if (bounds.y1 > font->ascender)
  2103. font->ascender = bounds.y1;
  2104. if (bounds.y0 < font->descender)
  2105. font->descender = bounds.y0;
  2106. font->ascdesc_src = FZ_ASCDESC_FROM_BOUNDS;
  2107. }