hb-ft.cc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * Copyright © 2009 Red Hat, Inc.
  3. * Copyright © 2009 Keith Stribley
  4. * Copyright © 2015 Google, Inc.
  5. *
  6. * This is part of HarfBuzz, a text shaping library.
  7. *
  8. * Permission is hereby granted, without written agreement and without
  9. * license or royalty fees, to use, copy, modify, and distribute this
  10. * software and its documentation for any purpose, provided that the
  11. * above copyright notice and the following two paragraphs appear in
  12. * all copies of this software.
  13. *
  14. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  15. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  16. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  17. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  18. * DAMAGE.
  19. *
  20. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  21. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  23. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  24. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  25. *
  26. * Red Hat Author(s): Behdad Esfahbod
  27. * Google Author(s): Behdad Esfahbod
  28. */
  29. #include "hb.hh"
  30. #ifdef HAVE_FREETYPE
  31. #include "hb-ft.h"
  32. #include "hb-draw.hh"
  33. #include "hb-font.hh"
  34. #include "hb-machinery.hh"
  35. #include "hb-cache.hh"
  36. #include "hb-ot-os2-table.hh"
  37. #include "hb-ot-shaper-arabic-pua.hh"
  38. #include FT_ADVANCES_H
  39. #include FT_MULTIPLE_MASTERS_H
  40. #include FT_OUTLINE_H
  41. #include FT_TRUETYPE_TABLES_H
  42. /**
  43. * SECTION:hb-ft
  44. * @title: hb-ft
  45. * @short_description: FreeType integration
  46. * @include: hb-ft.h
  47. *
  48. * Functions for using HarfBuzz with the FreeType library.
  49. *
  50. * HarfBuzz supports using FreeType to provide face and
  51. * font data.
  52. *
  53. * <note>Note that FreeType is not thread-safe, therefore these
  54. * functions are not thread-safe either.</note>
  55. **/
  56. /* TODO:
  57. *
  58. * In general, this file does a fine job of what it's supposed to do.
  59. * There are, however, things that need more work:
  60. *
  61. * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything
  62. * would work fine. However, we also abuse this API for performing in font-space,
  63. * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode
  64. * for that, such that no rounding etc happens. As such, we don't set ppem, and
  65. * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale
  66. * ourselves.
  67. *
  68. * - We don't handle / allow for emboldening / obliqueing.
  69. *
  70. * - In the future, we should add constructors to create fonts in font space?
  71. */
  72. using hb_ft_advance_cache_t = hb_cache_t<16, 24, 8, false>;
  73. struct hb_ft_font_t
  74. {
  75. int load_flags;
  76. bool symbol; /* Whether selected cmap is symbol cmap. */
  77. bool unref; /* Whether to destroy ft_face when done. */
  78. bool transform; /* Whether to apply FT_Face's transform. */
  79. mutable hb_mutex_t lock; /* Protects members below. */
  80. FT_Face ft_face;
  81. mutable unsigned cached_serial;
  82. mutable hb_ft_advance_cache_t advance_cache;
  83. };
  84. static hb_ft_font_t *
  85. _hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref)
  86. {
  87. hb_ft_font_t *ft_font = (hb_ft_font_t *) hb_calloc (1, sizeof (hb_ft_font_t));
  88. if (unlikely (!ft_font)) return nullptr;
  89. ft_font->lock.init ();
  90. ft_font->ft_face = ft_face;
  91. ft_font->symbol = symbol;
  92. ft_font->unref = unref;
  93. ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
  94. ft_font->cached_serial = (unsigned) -1;
  95. ft_font->advance_cache.init ();
  96. return ft_font;
  97. }
  98. static void
  99. _hb_ft_face_destroy (void *data)
  100. {
  101. FT_Done_Face ((FT_Face) data);
  102. }
  103. static void
  104. _hb_ft_font_destroy (void *data)
  105. {
  106. hb_ft_font_t *ft_font = (hb_ft_font_t *) data;
  107. ft_font->advance_cache.fini ();
  108. if (ft_font->unref)
  109. _hb_ft_face_destroy (ft_font->ft_face);
  110. ft_font->lock.fini ();
  111. hb_free (ft_font);
  112. }
  113. /* hb_font changed, update FT_Face. */
  114. static void _hb_ft_hb_font_changed (hb_font_t *font, FT_Face ft_face)
  115. {
  116. hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
  117. float x_mult = 1.f, y_mult = 1.f;
  118. if (font->x_scale < 0) x_mult = -x_mult;
  119. if (font->y_scale < 0) y_mult = -y_mult;
  120. if (FT_Set_Char_Size (ft_face,
  121. abs (font->x_scale), abs (font->y_scale),
  122. 0, 0
  123. #if 0
  124. font->x_ppem * 72 * 64 / font->x_scale,
  125. font->y_ppem * 72 * 64 / font->y_scale
  126. #endif
  127. ) && ft_face->num_fixed_sizes)
  128. {
  129. #ifdef HAVE_FT_GET_TRANSFORM
  130. /* Bitmap font, eg. bitmap color emoji. */
  131. /* TODO Pick largest size? */
  132. int x_scale = ft_face->available_sizes[0].x_ppem;
  133. int y_scale = ft_face->available_sizes[0].y_ppem;
  134. FT_Set_Char_Size (ft_face,
  135. x_scale, y_scale,
  136. 0, 0);
  137. /* This contains the sign that was previously in x_mult/y_mult. */
  138. x_mult = (float) font->x_scale / x_scale;
  139. y_mult = (float) font->y_scale / y_scale;
  140. #endif
  141. }
  142. else
  143. { /* Shrug */ }
  144. if (x_mult != 1.f || y_mult != 1.f)
  145. {
  146. FT_Matrix matrix = { (int) roundf (x_mult * (1<<16)), 0,
  147. 0, (int) roundf (y_mult * (1<<16))};
  148. FT_Set_Transform (ft_face, &matrix, nullptr);
  149. ft_font->transform = true;
  150. }
  151. #if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
  152. unsigned int num_coords;
  153. const float *coords = hb_font_get_var_coords_design (font, &num_coords);
  154. if (num_coords)
  155. {
  156. FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (num_coords, sizeof (FT_Fixed));
  157. if (ft_coords)
  158. {
  159. for (unsigned int i = 0; i < num_coords; i++)
  160. ft_coords[i] = coords[i] * 65536.f;
  161. FT_Set_Var_Design_Coordinates (ft_face, num_coords, ft_coords);
  162. hb_free (ft_coords);
  163. }
  164. }
  165. #endif
  166. }
  167. /* Check if hb_font changed, update FT_Face. */
  168. static inline bool
  169. _hb_ft_hb_font_check_changed (hb_font_t *font,
  170. const hb_ft_font_t *ft_font)
  171. {
  172. if (font->serial != ft_font->cached_serial)
  173. {
  174. _hb_ft_hb_font_changed (font, ft_font->ft_face);
  175. ft_font->advance_cache.clear ();
  176. ft_font->cached_serial = font->serial;
  177. return true;
  178. }
  179. return false;
  180. }
  181. /**
  182. * hb_ft_font_set_load_flags:
  183. * @font: #hb_font_t to work upon
  184. * @load_flags: The FreeType load flags to set
  185. *
  186. * Sets the FT_Load_Glyph load flags for the specified #hb_font_t.
  187. *
  188. * For more information, see
  189. * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
  190. *
  191. * Since: 1.0.5
  192. **/
  193. void
  194. hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
  195. {
  196. if (hb_object_is_immutable (font))
  197. return;
  198. if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
  199. return;
  200. hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
  201. ft_font->load_flags = load_flags;
  202. }
  203. /**
  204. * hb_ft_font_get_load_flags:
  205. * @font: #hb_font_t to work upon
  206. *
  207. * Fetches the FT_Load_Glyph load flags of the specified #hb_font_t.
  208. *
  209. * For more information, see
  210. * https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
  211. *
  212. * Return value: FT_Load_Glyph flags found
  213. *
  214. * Since: 1.0.5
  215. **/
  216. int
  217. hb_ft_font_get_load_flags (hb_font_t *font)
  218. {
  219. if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
  220. return 0;
  221. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
  222. return ft_font->load_flags;
  223. }
  224. /**
  225. * hb_ft_font_get_face: (skip)
  226. * @font: #hb_font_t to work upon
  227. *
  228. * Fetches the FT_Face associated with the specified #hb_font_t
  229. * font object.
  230. *
  231. * Return value: (nullable): the FT_Face found or `NULL`
  232. *
  233. * Since: 0.9.2
  234. **/
  235. FT_Face
  236. hb_ft_font_get_face (hb_font_t *font)
  237. {
  238. if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
  239. return nullptr;
  240. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
  241. return ft_font->ft_face;
  242. }
  243. /**
  244. * hb_ft_font_lock_face: (skip)
  245. * @font: #hb_font_t to work upon
  246. *
  247. * Gets the FT_Face associated with @font, This face will be kept around until
  248. * you call hb_ft_font_unlock_face().
  249. *
  250. * Return value: (nullable) (transfer none): the FT_Face associated with @font or `NULL`
  251. * Since: 2.6.5
  252. **/
  253. FT_Face
  254. hb_ft_font_lock_face (hb_font_t *font)
  255. {
  256. if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
  257. return nullptr;
  258. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
  259. ft_font->lock.lock ();
  260. return ft_font->ft_face;
  261. }
  262. /**
  263. * hb_ft_font_unlock_face: (skip)
  264. * @font: #hb_font_t to work upon
  265. *
  266. * Releases an FT_Face previously obtained with hb_ft_font_lock_face().
  267. *
  268. * Since: 2.6.5
  269. **/
  270. void
  271. hb_ft_font_unlock_face (hb_font_t *font)
  272. {
  273. if (unlikely (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy))
  274. return;
  275. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
  276. ft_font->lock.unlock ();
  277. }
  278. static hb_bool_t
  279. hb_ft_get_nominal_glyph (hb_font_t *font,
  280. void *font_data,
  281. hb_codepoint_t unicode,
  282. hb_codepoint_t *glyph,
  283. void *user_data HB_UNUSED)
  284. {
  285. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  286. hb_lock_t lock (ft_font->lock);
  287. unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
  288. if (unlikely (!g))
  289. {
  290. if (unlikely (ft_font->symbol))
  291. {
  292. switch ((unsigned) font->face->table.OS2->get_font_page ()) {
  293. case OT::OS2::font_page_t::FONT_PAGE_NONE:
  294. if (unicode <= 0x00FFu)
  295. /* For symbol-encoded OpenType fonts, we duplicate the
  296. * U+F000..F0FF range at U+0000..U+00FF. That's what
  297. * Windows seems to do, and that's hinted about at:
  298. * https://docs.microsoft.com/en-us/typography/opentype/spec/recom
  299. * under "Non-Standard (Symbol) Fonts". */
  300. g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode);
  301. break;
  302. #ifndef HB_NO_OT_SHAPER_ARABIC_FALLBACK
  303. case OT::OS2::font_page_t::FONT_PAGE_SIMP_ARABIC:
  304. g = FT_Get_Char_Index (ft_font->ft_face, _hb_arabic_pua_simp_map (unicode));
  305. break;
  306. case OT::OS2::font_page_t::FONT_PAGE_TRAD_ARABIC:
  307. g = FT_Get_Char_Index (ft_font->ft_face, _hb_arabic_pua_trad_map (unicode));
  308. break;
  309. #endif
  310. default:
  311. break;
  312. }
  313. if (!g)
  314. return false;
  315. }
  316. else
  317. return false;
  318. }
  319. *glyph = g;
  320. return true;
  321. }
  322. static unsigned int
  323. hb_ft_get_nominal_glyphs (hb_font_t *font HB_UNUSED,
  324. void *font_data,
  325. unsigned int count,
  326. const hb_codepoint_t *first_unicode,
  327. unsigned int unicode_stride,
  328. hb_codepoint_t *first_glyph,
  329. unsigned int glyph_stride,
  330. void *user_data HB_UNUSED)
  331. {
  332. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  333. hb_lock_t lock (ft_font->lock);
  334. unsigned int done;
  335. for (done = 0;
  336. done < count && (*first_glyph = FT_Get_Char_Index (ft_font->ft_face, *first_unicode));
  337. done++)
  338. {
  339. first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
  340. first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
  341. }
  342. /* We don't need to do ft_font->symbol dance here, since HB calls the singular
  343. * nominal_glyph() for what we don't handle here. */
  344. return done;
  345. }
  346. static hb_bool_t
  347. hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
  348. void *font_data,
  349. hb_codepoint_t unicode,
  350. hb_codepoint_t variation_selector,
  351. hb_codepoint_t *glyph,
  352. void *user_data HB_UNUSED)
  353. {
  354. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  355. hb_lock_t lock (ft_font->lock);
  356. unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
  357. if (unlikely (!g))
  358. return false;
  359. *glyph = g;
  360. return true;
  361. }
  362. static void
  363. hb_ft_get_glyph_h_advances (hb_font_t* font, void* font_data,
  364. unsigned count,
  365. const hb_codepoint_t *first_glyph,
  366. unsigned glyph_stride,
  367. hb_position_t *first_advance,
  368. unsigned advance_stride,
  369. void *user_data HB_UNUSED)
  370. {
  371. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  372. hb_lock_t lock (ft_font->lock);
  373. FT_Face ft_face = ft_font->ft_face;
  374. int load_flags = ft_font->load_flags;
  375. float x_mult;
  376. #ifdef HAVE_FT_GET_TRANSFORM
  377. if (ft_font->transform)
  378. {
  379. FT_Matrix matrix;
  380. FT_Get_Transform (ft_face, &matrix, nullptr);
  381. x_mult = sqrtf ((float)matrix.xx * matrix.xx + (float)matrix.xy * matrix.xy) / 65536.f;
  382. }
  383. else
  384. #endif
  385. {
  386. x_mult = font->x_scale < 0 ? -1 : +1;
  387. }
  388. for (unsigned int i = 0; i < count; i++)
  389. {
  390. FT_Fixed v = 0;
  391. hb_codepoint_t glyph = *first_glyph;
  392. unsigned int cv;
  393. if (ft_font->advance_cache.get (glyph, &cv))
  394. v = cv;
  395. else
  396. {
  397. FT_Get_Advance (ft_face, glyph, load_flags, &v);
  398. ft_font->advance_cache.set (glyph, v);
  399. }
  400. *first_advance = (int) (v * x_mult + (1<<9)) >> 10;
  401. first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
  402. first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
  403. }
  404. }
  405. #ifndef HB_NO_VERTICAL
  406. static hb_position_t
  407. hb_ft_get_glyph_v_advance (hb_font_t *font,
  408. void *font_data,
  409. hb_codepoint_t glyph,
  410. void *user_data HB_UNUSED)
  411. {
  412. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  413. hb_lock_t lock (ft_font->lock);
  414. FT_Fixed v;
  415. float y_mult;
  416. #ifdef HAVE_FT_GET_TRANSFORM
  417. if (ft_font->transform)
  418. {
  419. FT_Matrix matrix;
  420. FT_Get_Transform (ft_font->ft_face, &matrix, nullptr);
  421. y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
  422. }
  423. else
  424. #endif
  425. {
  426. y_mult = font->y_scale < 0 ? -1 : +1;
  427. }
  428. if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
  429. return 0;
  430. v = (int) (y_mult * v);
  431. /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
  432. * have a Y growing upward. Hence the extra negation. */
  433. return (-v + (1<<9)) >> 10;
  434. }
  435. #endif
  436. #ifndef HB_NO_VERTICAL
  437. static hb_bool_t
  438. hb_ft_get_glyph_v_origin (hb_font_t *font,
  439. void *font_data,
  440. hb_codepoint_t glyph,
  441. hb_position_t *x,
  442. hb_position_t *y,
  443. void *user_data HB_UNUSED)
  444. {
  445. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  446. hb_lock_t lock (ft_font->lock);
  447. FT_Face ft_face = ft_font->ft_face;
  448. float x_mult, y_mult;
  449. #ifdef HAVE_FT_GET_TRANSFORM
  450. if (ft_font->transform)
  451. {
  452. FT_Matrix matrix;
  453. FT_Get_Transform (ft_face, &matrix, nullptr);
  454. x_mult = sqrtf ((float)matrix.xx * matrix.xx + (float)matrix.xy * matrix.xy) / 65536.f;
  455. y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
  456. }
  457. else
  458. #endif
  459. {
  460. x_mult = font->x_scale < 0 ? -1 : +1;
  461. y_mult = font->y_scale < 0 ? -1 : +1;
  462. }
  463. if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
  464. return false;
  465. /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
  466. * have a Y growing upward. Hence the extra negation. */
  467. *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
  468. *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
  469. *x = (hb_position_t) (x_mult * *x);
  470. *y = (hb_position_t) (y_mult * *y);
  471. return true;
  472. }
  473. #endif
  474. #ifndef HB_NO_OT_SHAPE_FALLBACK
  475. static hb_position_t
  476. hb_ft_get_glyph_h_kerning (hb_font_t *font,
  477. void *font_data,
  478. hb_codepoint_t left_glyph,
  479. hb_codepoint_t right_glyph,
  480. void *user_data HB_UNUSED)
  481. {
  482. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  483. hb_lock_t lock (ft_font->lock);
  484. FT_Vector kerningv;
  485. FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
  486. if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
  487. return 0;
  488. return kerningv.x;
  489. }
  490. #endif
  491. static hb_bool_t
  492. hb_ft_get_glyph_extents (hb_font_t *font,
  493. void *font_data,
  494. hb_codepoint_t glyph,
  495. hb_glyph_extents_t *extents,
  496. void *user_data HB_UNUSED)
  497. {
  498. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  499. hb_lock_t lock (ft_font->lock);
  500. FT_Face ft_face = ft_font->ft_face;
  501. float x_mult, y_mult;
  502. #ifdef HAVE_FT_GET_TRANSFORM
  503. if (ft_font->transform)
  504. {
  505. FT_Matrix matrix;
  506. FT_Get_Transform (ft_face, &matrix, nullptr);
  507. x_mult = sqrtf ((float)matrix.xx * matrix.xx + (float)matrix.xy * matrix.xy) / 65536.f;
  508. y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
  509. }
  510. else
  511. #endif
  512. {
  513. x_mult = font->x_scale < 0 ? -1 : +1;
  514. y_mult = font->y_scale < 0 ? -1 : +1;
  515. }
  516. if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
  517. return false;
  518. extents->x_bearing = (hb_position_t) (x_mult * ft_face->glyph->metrics.horiBearingX);
  519. extents->y_bearing = (hb_position_t) (y_mult * ft_face->glyph->metrics.horiBearingY);
  520. extents->width = (hb_position_t) (x_mult * ft_face->glyph->metrics.width);
  521. extents->height = (hb_position_t) (y_mult * -ft_face->glyph->metrics.height);
  522. return true;
  523. }
  524. static hb_bool_t
  525. hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
  526. void *font_data,
  527. hb_codepoint_t glyph,
  528. unsigned int point_index,
  529. hb_position_t *x,
  530. hb_position_t *y,
  531. void *user_data HB_UNUSED)
  532. {
  533. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  534. hb_lock_t lock (ft_font->lock);
  535. FT_Face ft_face = ft_font->ft_face;
  536. if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
  537. return false;
  538. if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
  539. return false;
  540. if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
  541. return false;
  542. *x = ft_face->glyph->outline.points[point_index].x;
  543. *y = ft_face->glyph->outline.points[point_index].y;
  544. return true;
  545. }
  546. static hb_bool_t
  547. hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
  548. void *font_data,
  549. hb_codepoint_t glyph,
  550. char *name, unsigned int size,
  551. void *user_data HB_UNUSED)
  552. {
  553. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  554. hb_lock_t lock (ft_font->lock);
  555. FT_Face ft_face = ft_font->ft_face;
  556. hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
  557. if (ret && (size && !*name))
  558. ret = false;
  559. return ret;
  560. }
  561. static hb_bool_t
  562. hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
  563. void *font_data,
  564. const char *name, int len, /* -1 means nul-terminated */
  565. hb_codepoint_t *glyph,
  566. void *user_data HB_UNUSED)
  567. {
  568. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  569. hb_lock_t lock (ft_font->lock);
  570. FT_Face ft_face = ft_font->ft_face;
  571. if (len < 0)
  572. *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
  573. else {
  574. /* Make a nul-terminated version. */
  575. char buf[128];
  576. len = hb_min (len, (int) sizeof (buf) - 1);
  577. strncpy (buf, name, len);
  578. buf[len] = '\0';
  579. *glyph = FT_Get_Name_Index (ft_face, buf);
  580. }
  581. if (*glyph == 0)
  582. {
  583. /* Check whether the given name was actually the name of glyph 0. */
  584. char buf[128];
  585. if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
  586. len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
  587. return true;
  588. }
  589. return *glyph != 0;
  590. }
  591. static hb_bool_t
  592. hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
  593. void *font_data,
  594. hb_font_extents_t *metrics,
  595. void *user_data HB_UNUSED)
  596. {
  597. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  598. hb_lock_t lock (ft_font->lock);
  599. FT_Face ft_face = ft_font->ft_face;
  600. float y_mult;
  601. #ifdef HAVE_FT_GET_TRANSFORM
  602. if (ft_font->transform)
  603. {
  604. FT_Matrix matrix;
  605. FT_Get_Transform (ft_face, &matrix, nullptr);
  606. y_mult = sqrtf ((float)matrix.yx * matrix.yx + (float)matrix.yy * matrix.yy) / 65536.f;
  607. }
  608. else
  609. #endif
  610. {
  611. y_mult = font->y_scale < 0 ? -1 : +1;
  612. }
  613. if (ft_face->units_per_EM != 0)
  614. {
  615. metrics->ascender = FT_MulFix(ft_face->ascender, ft_face->size->metrics.y_scale);
  616. metrics->descender = FT_MulFix(ft_face->descender, ft_face->size->metrics.y_scale);
  617. metrics->line_gap = FT_MulFix( ft_face->height, ft_face->size->metrics.y_scale ) - (metrics->ascender - metrics->descender);
  618. }
  619. else
  620. {
  621. /* Bitmap-only font, eg. color bitmap font. */
  622. metrics->ascender = ft_face->size->metrics.ascender;
  623. metrics->descender = ft_face->size->metrics.descender;
  624. metrics->line_gap = ft_face->size->metrics.height - (metrics->ascender - metrics->descender);
  625. }
  626. metrics->ascender = (hb_position_t) (y_mult * metrics->ascender);
  627. metrics->descender = (hb_position_t) (y_mult * metrics->descender);
  628. metrics->line_gap = (hb_position_t) (y_mult * metrics->line_gap);
  629. return true;
  630. }
  631. #ifndef HB_NO_DRAW
  632. static int
  633. _hb_ft_move_to (const FT_Vector *to,
  634. void *arg)
  635. {
  636. hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
  637. drawing->move_to (to->x, to->y);
  638. return FT_Err_Ok;
  639. }
  640. static int
  641. _hb_ft_line_to (const FT_Vector *to,
  642. void *arg)
  643. {
  644. hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
  645. drawing->line_to (to->x, to->y);
  646. return FT_Err_Ok;
  647. }
  648. static int
  649. _hb_ft_conic_to (const FT_Vector *control,
  650. const FT_Vector *to,
  651. void *arg)
  652. {
  653. hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
  654. drawing->quadratic_to (control->x, control->y,
  655. to->x, to->y);
  656. return FT_Err_Ok;
  657. }
  658. static int
  659. _hb_ft_cubic_to (const FT_Vector *control1,
  660. const FT_Vector *control2,
  661. const FT_Vector *to,
  662. void *arg)
  663. {
  664. hb_draw_session_t *drawing = (hb_draw_session_t *) arg;
  665. drawing->cubic_to (control1->x, control1->y,
  666. control2->x, control2->y,
  667. to->x, to->y);
  668. return FT_Err_Ok;
  669. }
  670. static void
  671. hb_ft_get_glyph_shape (hb_font_t *font HB_UNUSED,
  672. void *font_data,
  673. hb_codepoint_t glyph,
  674. hb_draw_funcs_t *draw_funcs, void *draw_data,
  675. void *user_data HB_UNUSED)
  676. {
  677. const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
  678. hb_lock_t lock (ft_font->lock);
  679. FT_Face ft_face = ft_font->ft_face;
  680. if (unlikely (FT_Load_Glyph (ft_face, glyph,
  681. FT_LOAD_NO_BITMAP | ft_font->load_flags)))
  682. return;
  683. if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
  684. return;
  685. const FT_Outline_Funcs outline_funcs = {
  686. _hb_ft_move_to,
  687. _hb_ft_line_to,
  688. _hb_ft_conic_to,
  689. _hb_ft_cubic_to,
  690. 0, /* shift */
  691. 0, /* delta */
  692. };
  693. hb_draw_session_t draw_session (draw_funcs, draw_data, font->slant_xy);
  694. FT_Outline_Decompose (&ft_face->glyph->outline,
  695. &outline_funcs,
  696. &draw_session);
  697. }
  698. #endif
  699. static inline void free_static_ft_funcs ();
  700. static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t>
  701. {
  702. static hb_font_funcs_t *create ()
  703. {
  704. hb_font_funcs_t *funcs = hb_font_funcs_create ();
  705. hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, nullptr, nullptr);
  706. hb_font_funcs_set_nominal_glyphs_func (funcs, hb_ft_get_nominal_glyphs, nullptr, nullptr);
  707. hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, nullptr, nullptr);
  708. hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, nullptr, nullptr);
  709. hb_font_funcs_set_glyph_h_advances_func (funcs, hb_ft_get_glyph_h_advances, nullptr, nullptr);
  710. //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, nullptr, nullptr);
  711. #ifndef HB_NO_VERTICAL
  712. //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, nullptr, nullptr);
  713. hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, nullptr, nullptr);
  714. hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, nullptr, nullptr);
  715. #endif
  716. #ifndef HB_NO_OT_SHAPE_FALLBACK
  717. hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, nullptr, nullptr);
  718. #endif
  719. //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, nullptr, nullptr);
  720. hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, nullptr, nullptr);
  721. hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, nullptr, nullptr);
  722. hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, nullptr, nullptr);
  723. hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, nullptr, nullptr);
  724. #ifndef HB_NO_DRAW
  725. hb_font_funcs_set_glyph_shape_func (funcs, hb_ft_get_glyph_shape, nullptr, nullptr);
  726. #endif
  727. hb_font_funcs_make_immutable (funcs);
  728. hb_atexit (free_static_ft_funcs);
  729. return funcs;
  730. }
  731. } static_ft_funcs;
  732. static inline
  733. void free_static_ft_funcs ()
  734. {
  735. static_ft_funcs.free_instance ();
  736. }
  737. static hb_font_funcs_t *
  738. _hb_ft_get_font_funcs ()
  739. {
  740. return static_ft_funcs.get_unconst ();
  741. }
  742. static void
  743. _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
  744. {
  745. bool symbol = ft_face->charmap && ft_face->charmap->encoding == FT_ENCODING_MS_SYMBOL;
  746. hb_ft_font_t *ft_font = _hb_ft_font_create (ft_face, symbol, unref);
  747. if (unlikely (!ft_font)) return;
  748. hb_font_set_funcs (font,
  749. _hb_ft_get_font_funcs (),
  750. ft_font,
  751. _hb_ft_font_destroy);
  752. }
  753. static hb_blob_t *
  754. _hb_ft_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
  755. {
  756. FT_Face ft_face = (FT_Face) user_data;
  757. FT_Byte *buffer;
  758. FT_ULong length = 0;
  759. FT_Error error;
  760. /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
  761. error = FT_Load_Sfnt_Table (ft_face, tag, 0, nullptr, &length);
  762. if (error)
  763. return nullptr;
  764. buffer = (FT_Byte *) hb_malloc (length);
  765. if (!buffer)
  766. return nullptr;
  767. error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
  768. if (error)
  769. {
  770. hb_free (buffer);
  771. return nullptr;
  772. }
  773. return hb_blob_create ((const char *) buffer, length,
  774. HB_MEMORY_MODE_WRITABLE,
  775. buffer, hb_free);
  776. }
  777. /**
  778. * hb_ft_face_create:
  779. * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon
  780. * @destroy: (nullable): A callback to call when the face object is not needed anymore
  781. *
  782. * Creates an #hb_face_t face object from the specified FT_Face.
  783. *
  784. * This variant of the function does not provide any life-cycle management.
  785. *
  786. * Most client programs should use hb_ft_face_create_referenced()
  787. * (or, perhaps, hb_ft_face_create_cached()) instead.
  788. *
  789. * If you know you have valid reasons not to use hb_ft_face_create_referenced(),
  790. * then it is the client program's responsibility to destroy @ft_face
  791. * after the #hb_face_t face object has been destroyed.
  792. *
  793. * Return value: (transfer full): the new #hb_face_t face object
  794. *
  795. * Since: 0.9.2
  796. **/
  797. hb_face_t *
  798. hb_ft_face_create (FT_Face ft_face,
  799. hb_destroy_func_t destroy)
  800. {
  801. hb_face_t *face;
  802. if (!ft_face->stream->read) {
  803. hb_blob_t *blob;
  804. blob = hb_blob_create ((const char *) ft_face->stream->base,
  805. (unsigned int) ft_face->stream->size,
  806. HB_MEMORY_MODE_READONLY,
  807. ft_face, destroy);
  808. face = hb_face_create (blob, ft_face->face_index);
  809. hb_blob_destroy (blob);
  810. } else {
  811. face = hb_face_create_for_tables (_hb_ft_reference_table, ft_face, destroy);
  812. }
  813. hb_face_set_index (face, ft_face->face_index);
  814. hb_face_set_upem (face, ft_face->units_per_EM);
  815. return face;
  816. }
  817. /**
  818. * hb_ft_face_create_referenced:
  819. * @ft_face: FT_Face to work upon
  820. *
  821. * Creates an #hb_face_t face object from the specified FT_Face.
  822. *
  823. * This is the preferred variant of the hb_ft_face_create*
  824. * function family, because it calls FT_Reference_Face() on @ft_face,
  825. * ensuring that @ft_face remains alive as long as the resulting
  826. * #hb_face_t face object remains alive. Also calls FT_Done_Face()
  827. * when the #hb_face_t face object is destroyed.
  828. *
  829. * Use this version unless you know you have good reasons not to.
  830. *
  831. * Return value: (transfer full): the new #hb_face_t face object
  832. *
  833. * Since: 0.9.38
  834. **/
  835. hb_face_t *
  836. hb_ft_face_create_referenced (FT_Face ft_face)
  837. {
  838. FT_Reference_Face (ft_face);
  839. return hb_ft_face_create (ft_face, _hb_ft_face_destroy);
  840. }
  841. static void
  842. hb_ft_face_finalize (void *arg)
  843. {
  844. FT_Face ft_face = (FT_Face) arg;
  845. hb_face_destroy ((hb_face_t *) ft_face->generic.data);
  846. }
  847. /**
  848. * hb_ft_face_create_cached:
  849. * @ft_face: FT_Face to work upon
  850. *
  851. * Creates an #hb_face_t face object from the specified FT_Face.
  852. *
  853. * This variant of the function caches the newly created #hb_face_t
  854. * face object, using the @generic pointer of @ft_face. Subsequent function
  855. * calls that are passed the same @ft_face parameter will have the same
  856. * #hb_face_t returned to them, and that #hb_face_t will be correctly
  857. * reference counted.
  858. *
  859. * However, client programs are still responsible for destroying
  860. * @ft_face after the last #hb_face_t face object has been destroyed.
  861. *
  862. * Return value: (transfer full): the new #hb_face_t face object
  863. *
  864. * Since: 0.9.2
  865. **/
  866. hb_face_t *
  867. hb_ft_face_create_cached (FT_Face ft_face)
  868. {
  869. if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
  870. {
  871. if (ft_face->generic.finalizer)
  872. ft_face->generic.finalizer (ft_face);
  873. ft_face->generic.data = hb_ft_face_create (ft_face, nullptr);
  874. ft_face->generic.finalizer = hb_ft_face_finalize;
  875. }
  876. return hb_face_reference ((hb_face_t *) ft_face->generic.data);
  877. }
  878. /**
  879. * hb_ft_font_create:
  880. * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon
  881. * @destroy: (nullable): A callback to call when the font object is not needed anymore
  882. *
  883. * Creates an #hb_font_t font object from the specified FT_Face.
  884. *
  885. * <note>Note: You must set the face size on @ft_face before calling
  886. * hb_ft_font_create() on it. HarfBuzz assumes size is always set and will
  887. * access `size` member of FT_Face unconditionally.</note>
  888. *
  889. * This variant of the function does not provide any life-cycle management.
  890. *
  891. * Most client programs should use hb_ft_font_create_referenced()
  892. * instead.
  893. *
  894. * If you know you have valid reasons not to use hb_ft_font_create_referenced(),
  895. * then it is the client program's responsibility to destroy @ft_face
  896. * after the #hb_font_t font object has been destroyed.
  897. *
  898. * HarfBuzz will use the @destroy callback on the #hb_font_t font object
  899. * if it is supplied when you use this function. However, even if @destroy
  900. * is provided, it is the client program's responsibility to destroy @ft_face,
  901. * and it is the client program's responsibility to ensure that @ft_face is
  902. * destroyed only after the #hb_font_t font object has been destroyed.
  903. *
  904. * Return value: (transfer full): the new #hb_font_t font object
  905. *
  906. * Since: 0.9.2
  907. **/
  908. hb_font_t *
  909. hb_ft_font_create (FT_Face ft_face,
  910. hb_destroy_func_t destroy)
  911. {
  912. hb_font_t *font;
  913. hb_face_t *face;
  914. face = hb_ft_face_create (ft_face, destroy);
  915. font = hb_font_create (face);
  916. hb_face_destroy (face);
  917. _hb_ft_font_set_funcs (font, ft_face, false);
  918. hb_ft_font_changed (font);
  919. return font;
  920. }
  921. /**
  922. * hb_ft_font_changed:
  923. * @font: #hb_font_t to work upon
  924. *
  925. * Refreshes the state of @font when the underlying FT_Face has changed.
  926. * This function should be called after changing the size or
  927. * variation-axis settings on the FT_Face.
  928. *
  929. * Since: 1.0.5
  930. **/
  931. void
  932. hb_ft_font_changed (hb_font_t *font)
  933. {
  934. if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
  935. return;
  936. hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
  937. FT_Face ft_face = ft_font->ft_face;
  938. hb_font_set_scale (font,
  939. (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16),
  940. (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1u<<15)) >> 16));
  941. #if 0 /* hb-ft works in no-hinting model */
  942. hb_font_set_ppem (font,
  943. ft_face->size->metrics.x_ppem,
  944. ft_face->size->metrics.y_ppem);
  945. #endif
  946. #if defined(HAVE_FT_GET_VAR_BLEND_COORDINATES) && !defined(HB_NO_VAR)
  947. FT_MM_Var *mm_var = nullptr;
  948. if (!FT_Get_MM_Var (ft_face, &mm_var))
  949. {
  950. FT_Fixed *ft_coords = (FT_Fixed *) hb_calloc (mm_var->num_axis, sizeof (FT_Fixed));
  951. int *coords = (int *) hb_calloc (mm_var->num_axis, sizeof (int));
  952. if (coords && ft_coords)
  953. {
  954. if (!FT_Get_Var_Blend_Coordinates (ft_face, mm_var->num_axis, ft_coords))
  955. {
  956. bool nonzero = false;
  957. for (unsigned int i = 0; i < mm_var->num_axis; ++i)
  958. {
  959. coords[i] = ft_coords[i] >>= 2;
  960. nonzero = nonzero || coords[i];
  961. }
  962. if (nonzero)
  963. hb_font_set_var_coords_normalized (font, coords, mm_var->num_axis);
  964. else
  965. hb_font_set_var_coords_normalized (font, nullptr, 0);
  966. }
  967. }
  968. hb_free (coords);
  969. hb_free (ft_coords);
  970. #ifdef HAVE_FT_DONE_MM_VAR
  971. FT_Done_MM_Var (ft_face->glyph->library, mm_var);
  972. #else
  973. hb_free (mm_var);
  974. #endif
  975. }
  976. #endif
  977. ft_font->advance_cache.clear ();
  978. ft_font->cached_serial = font->serial;
  979. }
  980. /**
  981. * hb_ft_hb_font_changed:
  982. * @font: #hb_font_t to work upon
  983. *
  984. * Refreshes the state of the underlying FT_Face of @font when the hb_font_t
  985. * @font has changed.
  986. * This function should be called after changing the size or
  987. * variation-axis settings on the @font.
  988. * This call is fast if nothing has changed on @font.
  989. *
  990. * Return value: true if changed, false otherwise
  991. *
  992. * Since: 4.4.0
  993. **/
  994. hb_bool_t
  995. hb_ft_hb_font_changed (hb_font_t *font)
  996. {
  997. if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
  998. return false;
  999. hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
  1000. return _hb_ft_hb_font_check_changed (font, ft_font);
  1001. }
  1002. /**
  1003. * hb_ft_font_create_referenced:
  1004. * @ft_face: FT_Face to work upon
  1005. *
  1006. * Creates an #hb_font_t font object from the specified FT_Face.
  1007. *
  1008. * <note>Note: You must set the face size on @ft_face before calling
  1009. * hb_ft_font_create_referenced() on it. HarfBuzz assumes size is always set
  1010. * and will access `size` member of FT_Face unconditionally.</note>
  1011. *
  1012. * This is the preferred variant of the hb_ft_font_create*
  1013. * function family, because it calls FT_Reference_Face() on @ft_face,
  1014. * ensuring that @ft_face remains alive as long as the resulting
  1015. * #hb_font_t font object remains alive.
  1016. *
  1017. * Use this version unless you know you have good reasons not to.
  1018. *
  1019. * Return value: (transfer full): the new #hb_font_t font object
  1020. *
  1021. * Since: 0.9.38
  1022. **/
  1023. hb_font_t *
  1024. hb_ft_font_create_referenced (FT_Face ft_face)
  1025. {
  1026. FT_Reference_Face (ft_face);
  1027. return hb_ft_font_create (ft_face, _hb_ft_face_destroy);
  1028. }
  1029. static inline void free_static_ft_library ();
  1030. static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<FT_Library>,
  1031. hb_ft_library_lazy_loader_t>
  1032. {
  1033. static FT_Library create ()
  1034. {
  1035. FT_Library l;
  1036. if (FT_Init_FreeType (&l))
  1037. return nullptr;
  1038. hb_atexit (free_static_ft_library);
  1039. return l;
  1040. }
  1041. static void destroy (FT_Library l)
  1042. {
  1043. FT_Done_FreeType (l);
  1044. }
  1045. static FT_Library get_null ()
  1046. {
  1047. return nullptr;
  1048. }
  1049. } static_ft_library;
  1050. static inline
  1051. void free_static_ft_library ()
  1052. {
  1053. static_ft_library.free_instance ();
  1054. }
  1055. static FT_Library
  1056. get_ft_library ()
  1057. {
  1058. return static_ft_library.get_unconst ();
  1059. }
  1060. static void
  1061. _release_blob (void *arg)
  1062. {
  1063. FT_Face ft_face = (FT_Face) arg;
  1064. hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
  1065. }
  1066. /**
  1067. * hb_ft_font_set_funcs:
  1068. * @font: #hb_font_t to work upon
  1069. *
  1070. * Configures the font-functions structure of the specified
  1071. * #hb_font_t font object to use FreeType font functions.
  1072. *
  1073. * In particular, you can use this function to configure an
  1074. * existing #hb_face_t face object for use with FreeType font
  1075. * functions even if that #hb_face_t face object was initially
  1076. * created with hb_face_create(), and therefore was not
  1077. * initially configured to use FreeType font functions.
  1078. *
  1079. * An #hb_face_t face object created with hb_ft_face_create()
  1080. * is preconfigured for FreeType font functions and does not
  1081. * require this function to be used.
  1082. *
  1083. * <note>Note: Internally, this function creates an FT_Face.
  1084. * </note>
  1085. *
  1086. * Since: 1.0.5
  1087. **/
  1088. void
  1089. hb_ft_font_set_funcs (hb_font_t *font)
  1090. {
  1091. hb_blob_t *blob = hb_face_reference_blob (font->face);
  1092. unsigned int blob_length;
  1093. const char *blob_data = hb_blob_get_data (blob, &blob_length);
  1094. if (unlikely (!blob_length))
  1095. DEBUG_MSG (FT, font, "Font face has empty blob");
  1096. FT_Face ft_face = nullptr;
  1097. FT_Error err = FT_New_Memory_Face (get_ft_library (),
  1098. (const FT_Byte *) blob_data,
  1099. blob_length,
  1100. hb_face_get_index (font->face),
  1101. &ft_face);
  1102. if (unlikely (err)) {
  1103. hb_blob_destroy (blob);
  1104. DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
  1105. return;
  1106. }
  1107. if (FT_Select_Charmap (ft_face, FT_ENCODING_MS_SYMBOL))
  1108. FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
  1109. ft_face->generic.data = blob;
  1110. ft_face->generic.finalizer = _release_blob;
  1111. _hb_ft_font_set_funcs (font, ft_face, true);
  1112. hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
  1113. _hb_ft_hb_font_changed (font, ft_face);
  1114. }
  1115. #endif