ftglyph.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /***************************************************************************/
  2. /* */
  3. /* ftglyph.h */
  4. /* */
  5. /* FreeType convenience functions to handle glyphs (specification). */
  6. /* */
  7. /* Copyright 1996-2003, 2006, 2008, 2009, 2011, 2013, 2014 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  10. /* This file is part of the FreeType project, and may only be used, */
  11. /* modified, and distributed under the terms of the FreeType project */
  12. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  13. /* this file you indicate that you have read the license and */
  14. /* understand and accept it fully. */
  15. /* */
  16. /***************************************************************************/
  17. /*************************************************************************/
  18. /* */
  19. /* This file contains the definition of several convenience functions */
  20. /* that can be used by client applications to easily retrieve glyph */
  21. /* bitmaps and outlines from a given face. */
  22. /* */
  23. /* These functions should be optional if you are writing a font server */
  24. /* or text layout engine on top of FreeType. However, they are pretty */
  25. /* handy for many other simple uses of the library. */
  26. /* */
  27. /*************************************************************************/
  28. #ifndef __FTGLYPH_H__
  29. #define __FTGLYPH_H__
  30. #include <ft2build.h>
  31. #include FT_FREETYPE_H
  32. #ifdef FREETYPE_H
  33. #error "freetype.h of FreeType 1 has been loaded!"
  34. #error "Please fix the directory search order for header files"
  35. #error "so that freetype.h of FreeType 2 is found first."
  36. #endif
  37. FT_BEGIN_HEADER
  38. /*************************************************************************/
  39. /* */
  40. /* <Section> */
  41. /* glyph_management */
  42. /* */
  43. /* <Title> */
  44. /* Glyph Management */
  45. /* */
  46. /* <Abstract> */
  47. /* Generic interface to manage individual glyph data. */
  48. /* */
  49. /* <Description> */
  50. /* This section contains definitions used to manage glyph data */
  51. /* through generic FT_Glyph objects. Each of them can contain a */
  52. /* bitmap, a vector outline, or even images in other formats. */
  53. /* */
  54. /*************************************************************************/
  55. /* forward declaration to a private type */
  56. typedef struct FT_Glyph_Class_ FT_Glyph_Class;
  57. /*************************************************************************/
  58. /* */
  59. /* <Type> */
  60. /* FT_Glyph */
  61. /* */
  62. /* <Description> */
  63. /* Handle to an object used to model generic glyph images. It is a */
  64. /* pointer to the @FT_GlyphRec structure and can contain a glyph */
  65. /* bitmap or pointer. */
  66. /* */
  67. /* <Note> */
  68. /* Glyph objects are not owned by the library. You must thus release */
  69. /* them manually (through @FT_Done_Glyph) _before_ calling */
  70. /* @FT_Done_FreeType. */
  71. /* */
  72. typedef struct FT_GlyphRec_* FT_Glyph;
  73. /*************************************************************************/
  74. /* */
  75. /* <Struct> */
  76. /* FT_GlyphRec */
  77. /* */
  78. /* <Description> */
  79. /* The root glyph structure contains a given glyph image plus its */
  80. /* advance width in 16.16 fixed-point format. */
  81. /* */
  82. /* <Fields> */
  83. /* library :: A handle to the FreeType library object. */
  84. /* */
  85. /* clazz :: A pointer to the glyph's class. Private. */
  86. /* */
  87. /* format :: The format of the glyph's image. */
  88. /* */
  89. /* advance :: A 16.16 vector that gives the glyph's advance width. */
  90. /* */
  91. typedef struct FT_GlyphRec_
  92. {
  93. FT_Library library;
  94. const FT_Glyph_Class* clazz;
  95. FT_Glyph_Format format;
  96. FT_Vector advance;
  97. } FT_GlyphRec;
  98. /*************************************************************************/
  99. /* */
  100. /* <Type> */
  101. /* FT_BitmapGlyph */
  102. /* */
  103. /* <Description> */
  104. /* A handle to an object used to model a bitmap glyph image. This is */
  105. /* a sub-class of @FT_Glyph, and a pointer to @FT_BitmapGlyphRec. */
  106. /* */
  107. typedef struct FT_BitmapGlyphRec_* FT_BitmapGlyph;
  108. /*************************************************************************/
  109. /* */
  110. /* <Struct> */
  111. /* FT_BitmapGlyphRec */
  112. /* */
  113. /* <Description> */
  114. /* A structure used for bitmap glyph images. This really is a */
  115. /* `sub-class' of @FT_GlyphRec. */
  116. /* */
  117. /* <Fields> */
  118. /* root :: The root @FT_Glyph fields. */
  119. /* */
  120. /* left :: The left-side bearing, i.e., the horizontal distance */
  121. /* from the current pen position to the left border of the */
  122. /* glyph bitmap. */
  123. /* */
  124. /* top :: The top-side bearing, i.e., the vertical distance from */
  125. /* the current pen position to the top border of the glyph */
  126. /* bitmap. This distance is positive for upwards~y! */
  127. /* */
  128. /* bitmap :: A descriptor for the bitmap. */
  129. /* */
  130. /* <Note> */
  131. /* You can typecast an @FT_Glyph to @FT_BitmapGlyph if you have */
  132. /* `glyph->format == FT_GLYPH_FORMAT_BITMAP'. This lets you access */
  133. /* the bitmap's contents easily. */
  134. /* */
  135. /* The corresponding pixel buffer is always owned by @FT_BitmapGlyph */
  136. /* and is thus created and destroyed with it. */
  137. /* */
  138. typedef struct FT_BitmapGlyphRec_
  139. {
  140. FT_GlyphRec root;
  141. FT_Int left;
  142. FT_Int top;
  143. FT_Bitmap bitmap;
  144. } FT_BitmapGlyphRec;
  145. /*************************************************************************/
  146. /* */
  147. /* <Type> */
  148. /* FT_OutlineGlyph */
  149. /* */
  150. /* <Description> */
  151. /* A handle to an object used to model an outline glyph image. This */
  152. /* is a sub-class of @FT_Glyph, and a pointer to @FT_OutlineGlyphRec. */
  153. /* */
  154. typedef struct FT_OutlineGlyphRec_* FT_OutlineGlyph;
  155. /*************************************************************************/
  156. /* */
  157. /* <Struct> */
  158. /* FT_OutlineGlyphRec */
  159. /* */
  160. /* <Description> */
  161. /* A structure used for outline (vectorial) glyph images. This */
  162. /* really is a `sub-class' of @FT_GlyphRec. */
  163. /* */
  164. /* <Fields> */
  165. /* root :: The root @FT_Glyph fields. */
  166. /* */
  167. /* outline :: A descriptor for the outline. */
  168. /* */
  169. /* <Note> */
  170. /* You can typecast an @FT_Glyph to @FT_OutlineGlyph if you have */
  171. /* `glyph->format == FT_GLYPH_FORMAT_OUTLINE'. This lets you access */
  172. /* the outline's content easily. */
  173. /* */
  174. /* As the outline is extracted from a glyph slot, its coordinates are */
  175. /* expressed normally in 26.6 pixels, unless the flag */
  176. /* @FT_LOAD_NO_SCALE was used in @FT_Load_Glyph() or @FT_Load_Char(). */
  177. /* */
  178. /* The outline's tables are always owned by the object and are */
  179. /* destroyed with it. */
  180. /* */
  181. typedef struct FT_OutlineGlyphRec_
  182. {
  183. FT_GlyphRec root;
  184. FT_Outline outline;
  185. } FT_OutlineGlyphRec;
  186. /*************************************************************************/
  187. /* */
  188. /* <Function> */
  189. /* FT_Get_Glyph */
  190. /* */
  191. /* <Description> */
  192. /* A function used to extract a glyph image from a slot. Note that */
  193. /* the created @FT_Glyph object must be released with @FT_Done_Glyph. */
  194. /* */
  195. /* <Input> */
  196. /* slot :: A handle to the source glyph slot. */
  197. /* */
  198. /* <Output> */
  199. /* aglyph :: A handle to the glyph object. */
  200. /* */
  201. /* <Return> */
  202. /* FreeType error code. 0~means success. */
  203. /* */
  204. FT_EXPORT( FT_Error )
  205. FT_Get_Glyph( FT_GlyphSlot slot,
  206. FT_Glyph *aglyph );
  207. /*************************************************************************/
  208. /* */
  209. /* <Function> */
  210. /* FT_Glyph_Copy */
  211. /* */
  212. /* <Description> */
  213. /* A function used to copy a glyph image. Note that the created */
  214. /* @FT_Glyph object must be released with @FT_Done_Glyph. */
  215. /* */
  216. /* <Input> */
  217. /* source :: A handle to the source glyph object. */
  218. /* */
  219. /* <Output> */
  220. /* target :: A handle to the target glyph object. 0~in case of */
  221. /* error. */
  222. /* */
  223. /* <Return> */
  224. /* FreeType error code. 0~means success. */
  225. /* */
  226. FT_EXPORT( FT_Error )
  227. FT_Glyph_Copy( FT_Glyph source,
  228. FT_Glyph *target );
  229. /*************************************************************************/
  230. /* */
  231. /* <Function> */
  232. /* FT_Glyph_Transform */
  233. /* */
  234. /* <Description> */
  235. /* Transform a glyph image if its format is scalable. */
  236. /* */
  237. /* <InOut> */
  238. /* glyph :: A handle to the target glyph object. */
  239. /* */
  240. /* <Input> */
  241. /* matrix :: A pointer to a 2x2 matrix to apply. */
  242. /* */
  243. /* delta :: A pointer to a 2d vector to apply. Coordinates are */
  244. /* expressed in 1/64th of a pixel. */
  245. /* */
  246. /* <Return> */
  247. /* FreeType error code (if not 0, the glyph format is not scalable). */
  248. /* */
  249. /* <Note> */
  250. /* The 2x2 transformation matrix is also applied to the glyph's */
  251. /* advance vector. */
  252. /* */
  253. FT_EXPORT( FT_Error )
  254. FT_Glyph_Transform( FT_Glyph glyph,
  255. FT_Matrix* matrix,
  256. FT_Vector* delta );
  257. /*************************************************************************/
  258. /* */
  259. /* <Enum> */
  260. /* FT_Glyph_BBox_Mode */
  261. /* */
  262. /* <Description> */
  263. /* The mode how the values of @FT_Glyph_Get_CBox are returned. */
  264. /* */
  265. /* <Values> */
  266. /* FT_GLYPH_BBOX_UNSCALED :: */
  267. /* Return unscaled font units. */
  268. /* */
  269. /* FT_GLYPH_BBOX_SUBPIXELS :: */
  270. /* Return unfitted 26.6 coordinates. */
  271. /* */
  272. /* FT_GLYPH_BBOX_GRIDFIT :: */
  273. /* Return grid-fitted 26.6 coordinates. */
  274. /* */
  275. /* FT_GLYPH_BBOX_TRUNCATE :: */
  276. /* Return coordinates in integer pixels. */
  277. /* */
  278. /* FT_GLYPH_BBOX_PIXELS :: */
  279. /* Return grid-fitted pixel coordinates. */
  280. /* */
  281. typedef enum FT_Glyph_BBox_Mode_
  282. {
  283. FT_GLYPH_BBOX_UNSCALED = 0,
  284. FT_GLYPH_BBOX_SUBPIXELS = 0,
  285. FT_GLYPH_BBOX_GRIDFIT = 1,
  286. FT_GLYPH_BBOX_TRUNCATE = 2,
  287. FT_GLYPH_BBOX_PIXELS = 3
  288. } FT_Glyph_BBox_Mode;
  289. /* these constants are deprecated; use the corresponding */
  290. /* `FT_Glyph_BBox_Mode' values instead */
  291. #define ft_glyph_bbox_unscaled FT_GLYPH_BBOX_UNSCALED
  292. #define ft_glyph_bbox_subpixels FT_GLYPH_BBOX_SUBPIXELS
  293. #define ft_glyph_bbox_gridfit FT_GLYPH_BBOX_GRIDFIT
  294. #define ft_glyph_bbox_truncate FT_GLYPH_BBOX_TRUNCATE
  295. #define ft_glyph_bbox_pixels FT_GLYPH_BBOX_PIXELS
  296. /*************************************************************************/
  297. /* */
  298. /* <Function> */
  299. /* FT_Glyph_Get_CBox */
  300. /* */
  301. /* <Description> */
  302. /* Return a glyph's `control box'. The control box encloses all the */
  303. /* outline's points, including Bézier control points. Though it */
  304. /* coincides with the exact bounding box for most glyphs, it can be */
  305. /* slightly larger in some situations (like when rotating an outline */
  306. /* that contains Bézier outside arcs). */
  307. /* */
  308. /* Computing the control box is very fast, while getting the bounding */
  309. /* box can take much more time as it needs to walk over all segments */
  310. /* and arcs in the outline. To get the latter, you can use the */
  311. /* `ftbbox' component, which is dedicated to this single task. */
  312. /* */
  313. /* <Input> */
  314. /* glyph :: A handle to the source glyph object. */
  315. /* */
  316. /* mode :: The mode that indicates how to interpret the returned */
  317. /* bounding box values. */
  318. /* */
  319. /* <Output> */
  320. /* acbox :: The glyph coordinate bounding box. Coordinates are */
  321. /* expressed in 1/64th of pixels if it is grid-fitted. */
  322. /* */
  323. /* <Note> */
  324. /* Coordinates are relative to the glyph origin, using the y~upwards */
  325. /* convention. */
  326. /* */
  327. /* If the glyph has been loaded with @FT_LOAD_NO_SCALE, `bbox_mode' */
  328. /* must be set to @FT_GLYPH_BBOX_UNSCALED to get unscaled font */
  329. /* units in 26.6 pixel format. The value @FT_GLYPH_BBOX_SUBPIXELS */
  330. /* is another name for this constant. */
  331. /* */
  332. /* If the font is tricky and the glyph has been loaded with */
  333. /* @FT_LOAD_NO_SCALE, the resulting CBox is meaningless. To get */
  334. /* reasonable values for the CBox it is necessary to load the glyph */
  335. /* at a large ppem value (so that the hinting instructions can */
  336. /* properly shift and scale the subglyphs), then extracting the CBox, */
  337. /* which can be eventually converted back to font units. */
  338. /* */
  339. /* Note that the maximum coordinates are exclusive, which means that */
  340. /* one can compute the width and height of the glyph image (be it in */
  341. /* integer or 26.6 pixels) as: */
  342. /* */
  343. /* { */
  344. /* width = bbox.xMax - bbox.xMin; */
  345. /* height = bbox.yMax - bbox.yMin; */
  346. /* } */
  347. /* */
  348. /* Note also that for 26.6 coordinates, if `bbox_mode' is set to */
  349. /* @FT_GLYPH_BBOX_GRIDFIT, the coordinates will also be grid-fitted, */
  350. /* which corresponds to: */
  351. /* */
  352. /* { */
  353. /* bbox.xMin = FLOOR(bbox.xMin); */
  354. /* bbox.yMin = FLOOR(bbox.yMin); */
  355. /* bbox.xMax = CEILING(bbox.xMax); */
  356. /* bbox.yMax = CEILING(bbox.yMax); */
  357. /* } */
  358. /* */
  359. /* To get the bbox in pixel coordinates, set `bbox_mode' to */
  360. /* @FT_GLYPH_BBOX_TRUNCATE. */
  361. /* */
  362. /* To get the bbox in grid-fitted pixel coordinates, set `bbox_mode' */
  363. /* to @FT_GLYPH_BBOX_PIXELS. */
  364. /* */
  365. FT_EXPORT( void )
  366. FT_Glyph_Get_CBox( FT_Glyph glyph,
  367. FT_UInt bbox_mode,
  368. FT_BBox *acbox );
  369. /*************************************************************************/
  370. /* */
  371. /* <Function> */
  372. /* FT_Glyph_To_Bitmap */
  373. /* */
  374. /* <Description> */
  375. /* Convert a given glyph object to a bitmap glyph object. */
  376. /* */
  377. /* <InOut> */
  378. /* the_glyph :: A pointer to a handle to the target glyph. */
  379. /* */
  380. /* <Input> */
  381. /* render_mode :: An enumeration that describes how the data is */
  382. /* rendered. */
  383. /* */
  384. /* origin :: A pointer to a vector used to translate the glyph */
  385. /* image before rendering. Can be~0 (if no */
  386. /* translation). The origin is expressed in */
  387. /* 26.6 pixels. */
  388. /* */
  389. /* destroy :: A boolean that indicates that the original glyph */
  390. /* image should be destroyed by this function. It is */
  391. /* never destroyed in case of error. */
  392. /* */
  393. /* <Return> */
  394. /* FreeType error code. 0~means success. */
  395. /* */
  396. /* <Note> */
  397. /* This function does nothing if the glyph format isn't scalable. */
  398. /* */
  399. /* The glyph image is translated with the `origin' vector before */
  400. /* rendering. */
  401. /* */
  402. /* The first parameter is a pointer to an @FT_Glyph handle, that will */
  403. /* be _replaced_ by this function (with newly allocated data). */
  404. /* Typically, you would use (omitting error handling): */
  405. /* */
  406. /* */
  407. /* { */
  408. /* FT_Glyph glyph; */
  409. /* FT_BitmapGlyph glyph_bitmap; */
  410. /* */
  411. /* */
  412. /* // load glyph */
  413. /* error = FT_Load_Char( face, glyph_index, FT_LOAD_DEFAUT ); */
  414. /* */
  415. /* // extract glyph image */
  416. /* error = FT_Get_Glyph( face->glyph, &glyph ); */
  417. /* */
  418. /* // convert to a bitmap (default render mode + destroying old) */
  419. /* if ( glyph->format != FT_GLYPH_FORMAT_BITMAP ) */
  420. /* { */
  421. /* error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, */
  422. /* 0, 1 ); */
  423. /* if ( error ) // `glyph' unchanged */
  424. /* ... */
  425. /* } */
  426. /* */
  427. /* // access bitmap content by typecasting */
  428. /* glyph_bitmap = (FT_BitmapGlyph)glyph; */
  429. /* */
  430. /* // do funny stuff with it, like blitting/drawing */
  431. /* ... */
  432. /* */
  433. /* // discard glyph image (bitmap or not) */
  434. /* FT_Done_Glyph( glyph ); */
  435. /* } */
  436. /* */
  437. /* */
  438. /* Here another example, again without error handling: */
  439. /* */
  440. /* */
  441. /* { */
  442. /* FT_Glyph glyphs[MAX_GLYPHS] */
  443. /* */
  444. /* */
  445. /* ... */
  446. /* */
  447. /* for ( idx = 0; i < MAX_GLYPHS; i++ ) */
  448. /* error = FT_Load_Glyph( face, idx, FT_LOAD_DEFAULT ) || */
  449. /* FT_Get_Glyph ( face->glyph, &glyph[idx] ); */
  450. /* */
  451. /* ... */
  452. /* */
  453. /* for ( idx = 0; i < MAX_GLYPHS; i++ ) */
  454. /* { */
  455. /* FT_Glyph bitmap = glyphs[idx]; */
  456. /* */
  457. /* */
  458. /* ... */
  459. /* */
  460. /* // after this call, `bitmap' no longer points into */
  461. /* // the `glyphs' array (and the old value isn't destroyed) */
  462. /* FT_Glyph_To_Bitmap( &bitmap, FT_RENDER_MODE_MONO, 0, 0 ); */
  463. /* */
  464. /* ... */
  465. /* */
  466. /* FT_Done_Glyph( bitmap ); */
  467. /* } */
  468. /* */
  469. /* ... */
  470. /* */
  471. /* for ( idx = 0; i < MAX_GLYPHS; i++ ) */
  472. /* FT_Done_Glyph( glyphs[idx] ); */
  473. /* } */
  474. /* */
  475. FT_EXPORT( FT_Error )
  476. FT_Glyph_To_Bitmap( FT_Glyph* the_glyph,
  477. FT_Render_Mode render_mode,
  478. FT_Vector* origin,
  479. FT_Bool destroy );
  480. /*************************************************************************/
  481. /* */
  482. /* <Function> */
  483. /* FT_Done_Glyph */
  484. /* */
  485. /* <Description> */
  486. /* Destroy a given glyph. */
  487. /* */
  488. /* <Input> */
  489. /* glyph :: A handle to the target glyph object. */
  490. /* */
  491. FT_EXPORT( void )
  492. FT_Done_Glyph( FT_Glyph glyph );
  493. /* */
  494. /* other helpful functions */
  495. /*************************************************************************/
  496. /* */
  497. /* <Section> */
  498. /* computations */
  499. /* */
  500. /*************************************************************************/
  501. /*************************************************************************/
  502. /* */
  503. /* <Function> */
  504. /* FT_Matrix_Multiply */
  505. /* */
  506. /* <Description> */
  507. /* Perform the matrix operation `b = a*b'. */
  508. /* */
  509. /* <Input> */
  510. /* a :: A pointer to matrix `a'. */
  511. /* */
  512. /* <InOut> */
  513. /* b :: A pointer to matrix `b'. */
  514. /* */
  515. /* <Note> */
  516. /* The result is undefined if either `a' or `b' is zero. */
  517. /* */
  518. FT_EXPORT( void )
  519. FT_Matrix_Multiply( const FT_Matrix* a,
  520. FT_Matrix* b );
  521. /*************************************************************************/
  522. /* */
  523. /* <Function> */
  524. /* FT_Matrix_Invert */
  525. /* */
  526. /* <Description> */
  527. /* Invert a 2x2 matrix. Return an error if it can't be inverted. */
  528. /* */
  529. /* <InOut> */
  530. /* matrix :: A pointer to the target matrix. Remains untouched in */
  531. /* case of error. */
  532. /* */
  533. /* <Return> */
  534. /* FreeType error code. 0~means success. */
  535. /* */
  536. FT_EXPORT( FT_Error )
  537. FT_Matrix_Invert( FT_Matrix* matrix );
  538. /* */
  539. FT_END_HEADER
  540. #endif /* __FTGLYPH_H__ */
  541. /* END */
  542. /* Local Variables: */
  543. /* coding: utf-8 */
  544. /* End: */