ftglyph.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /***************************************************************************/
  2. /* */
  3. /* ftglyph.h */
  4. /* */
  5. /* FreeType convenience functions to handle glyphs (specification). */
  6. /* */
  7. /* Copyright 1996-2003, 2006, 2008, 2009, 2011, 2013 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. /*************************************************************************/
  290. /* */
  291. /* <Enum> */
  292. /* ft_glyph_bbox_xxx */
  293. /* */
  294. /* <Description> */
  295. /* These constants are deprecated. Use the corresponding */
  296. /* @FT_Glyph_BBox_Mode values instead. */
  297. /* */
  298. /* <Values> */
  299. /* ft_glyph_bbox_unscaled :: See @FT_GLYPH_BBOX_UNSCALED. */
  300. /* ft_glyph_bbox_subpixels :: See @FT_GLYPH_BBOX_SUBPIXELS. */
  301. /* ft_glyph_bbox_gridfit :: See @FT_GLYPH_BBOX_GRIDFIT. */
  302. /* ft_glyph_bbox_truncate :: See @FT_GLYPH_BBOX_TRUNCATE. */
  303. /* ft_glyph_bbox_pixels :: See @FT_GLYPH_BBOX_PIXELS. */
  304. /* */
  305. #define ft_glyph_bbox_unscaled FT_GLYPH_BBOX_UNSCALED
  306. #define ft_glyph_bbox_subpixels FT_GLYPH_BBOX_SUBPIXELS
  307. #define ft_glyph_bbox_gridfit FT_GLYPH_BBOX_GRIDFIT
  308. #define ft_glyph_bbox_truncate FT_GLYPH_BBOX_TRUNCATE
  309. #define ft_glyph_bbox_pixels FT_GLYPH_BBOX_PIXELS
  310. /*************************************************************************/
  311. /* */
  312. /* <Function> */
  313. /* FT_Glyph_Get_CBox */
  314. /* */
  315. /* <Description> */
  316. /* Return a glyph's `control box'. The control box encloses all the */
  317. /* outline's points, including Bézier control points. Though it */
  318. /* coincides with the exact bounding box for most glyphs, it can be */
  319. /* slightly larger in some situations (like when rotating an outline */
  320. /* that contains Bézier outside arcs). */
  321. /* */
  322. /* Computing the control box is very fast, while getting the bounding */
  323. /* box can take much more time as it needs to walk over all segments */
  324. /* and arcs in the outline. To get the latter, you can use the */
  325. /* `ftbbox' component, which is dedicated to this single task. */
  326. /* */
  327. /* <Input> */
  328. /* glyph :: A handle to the source glyph object. */
  329. /* */
  330. /* mode :: The mode that indicates how to interpret the returned */
  331. /* bounding box values. */
  332. /* */
  333. /* <Output> */
  334. /* acbox :: The glyph coordinate bounding box. Coordinates are */
  335. /* expressed in 1/64th of pixels if it is grid-fitted. */
  336. /* */
  337. /* <Note> */
  338. /* Coordinates are relative to the glyph origin, using the y~upwards */
  339. /* convention. */
  340. /* */
  341. /* If the glyph has been loaded with @FT_LOAD_NO_SCALE, `bbox_mode' */
  342. /* must be set to @FT_GLYPH_BBOX_UNSCALED to get unscaled font */
  343. /* units in 26.6 pixel format. The value @FT_GLYPH_BBOX_SUBPIXELS */
  344. /* is another name for this constant. */
  345. /* */
  346. /* If the font is tricky and the glyph has been loaded with */
  347. /* @FT_LOAD_NO_SCALE, the resulting CBox is meaningless. To get */
  348. /* reasonable values for the CBox it is necessary to load the glyph */
  349. /* at a large ppem value (so that the hinting instructions can */
  350. /* properly shift and scale the subglyphs), then extracting the CBox, */
  351. /* which can be eventually converted back to font units. */
  352. /* */
  353. /* Note that the maximum coordinates are exclusive, which means that */
  354. /* one can compute the width and height of the glyph image (be it in */
  355. /* integer or 26.6 pixels) as: */
  356. /* */
  357. /* { */
  358. /* width = bbox.xMax - bbox.xMin; */
  359. /* height = bbox.yMax - bbox.yMin; */
  360. /* } */
  361. /* */
  362. /* Note also that for 26.6 coordinates, if `bbox_mode' is set to */
  363. /* @FT_GLYPH_BBOX_GRIDFIT, the coordinates will also be grid-fitted, */
  364. /* which corresponds to: */
  365. /* */
  366. /* { */
  367. /* bbox.xMin = FLOOR(bbox.xMin); */
  368. /* bbox.yMin = FLOOR(bbox.yMin); */
  369. /* bbox.xMax = CEILING(bbox.xMax); */
  370. /* bbox.yMax = CEILING(bbox.yMax); */
  371. /* } */
  372. /* */
  373. /* To get the bbox in pixel coordinates, set `bbox_mode' to */
  374. /* @FT_GLYPH_BBOX_TRUNCATE. */
  375. /* */
  376. /* To get the bbox in grid-fitted pixel coordinates, set `bbox_mode' */
  377. /* to @FT_GLYPH_BBOX_PIXELS. */
  378. /* */
  379. FT_EXPORT( void )
  380. FT_Glyph_Get_CBox( FT_Glyph glyph,
  381. FT_UInt bbox_mode,
  382. FT_BBox *acbox );
  383. /*************************************************************************/
  384. /* */
  385. /* <Function> */
  386. /* FT_Glyph_To_Bitmap */
  387. /* */
  388. /* <Description> */
  389. /* Convert a given glyph object to a bitmap glyph object. */
  390. /* */
  391. /* <InOut> */
  392. /* the_glyph :: A pointer to a handle to the target glyph. */
  393. /* */
  394. /* <Input> */
  395. /* render_mode :: An enumeration that describes how the data is */
  396. /* rendered. */
  397. /* */
  398. /* origin :: A pointer to a vector used to translate the glyph */
  399. /* image before rendering. Can be~0 (if no */
  400. /* translation). The origin is expressed in */
  401. /* 26.6 pixels. */
  402. /* */
  403. /* destroy :: A boolean that indicates that the original glyph */
  404. /* image should be destroyed by this function. It is */
  405. /* never destroyed in case of error. */
  406. /* */
  407. /* <Return> */
  408. /* FreeType error code. 0~means success. */
  409. /* */
  410. /* <Note> */
  411. /* This function does nothing if the glyph format isn't scalable. */
  412. /* */
  413. /* The glyph image is translated with the `origin' vector before */
  414. /* rendering. */
  415. /* */
  416. /* The first parameter is a pointer to an @FT_Glyph handle, that will */
  417. /* be _replaced_ by this function (with newly allocated data). */
  418. /* Typically, you would use (omitting error handling): */
  419. /* */
  420. /* */
  421. /* { */
  422. /* FT_Glyph glyph; */
  423. /* FT_BitmapGlyph glyph_bitmap; */
  424. /* */
  425. /* */
  426. /* // load glyph */
  427. /* error = FT_Load_Char( face, glyph_index, FT_LOAD_DEFAUT ); */
  428. /* */
  429. /* // extract glyph image */
  430. /* error = FT_Get_Glyph( face->glyph, &glyph ); */
  431. /* */
  432. /* // convert to a bitmap (default render mode + destroying old) */
  433. /* if ( glyph->format != FT_GLYPH_FORMAT_BITMAP ) */
  434. /* { */
  435. /* error = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, */
  436. /* 0, 1 ); */
  437. /* if ( error ) // `glyph' unchanged */
  438. /* ... */
  439. /* } */
  440. /* */
  441. /* // access bitmap content by typecasting */
  442. /* glyph_bitmap = (FT_BitmapGlyph)glyph; */
  443. /* */
  444. /* // do funny stuff with it, like blitting/drawing */
  445. /* ... */
  446. /* */
  447. /* // discard glyph image (bitmap or not) */
  448. /* FT_Done_Glyph( glyph ); */
  449. /* } */
  450. /* */
  451. /* */
  452. /* Here another example, again without error handling: */
  453. /* */
  454. /* */
  455. /* { */
  456. /* FT_Glyph glyphs[MAX_GLYPHS] */
  457. /* */
  458. /* */
  459. /* ... */
  460. /* */
  461. /* for ( idx = 0; i < MAX_GLYPHS; i++ ) */
  462. /* error = FT_Load_Glyph( face, idx, FT_LOAD_DEFAULT ) || */
  463. /* FT_Get_Glyph ( face->glyph, &glyph[idx] ); */
  464. /* */
  465. /* ... */
  466. /* */
  467. /* for ( idx = 0; i < MAX_GLYPHS; i++ ) */
  468. /* { */
  469. /* FT_Glyph bitmap = glyphs[idx]; */
  470. /* */
  471. /* */
  472. /* ... */
  473. /* */
  474. /* // after this call, `bitmap' no longer points into */
  475. /* // the `glyphs' array (and the old value isn't destroyed) */
  476. /* FT_Glyph_To_Bitmap( &bitmap, FT_RENDER_MODE_MONO, 0, 0 ); */
  477. /* */
  478. /* ... */
  479. /* */
  480. /* FT_Done_Glyph( bitmap ); */
  481. /* } */
  482. /* */
  483. /* ... */
  484. /* */
  485. /* for ( idx = 0; i < MAX_GLYPHS; i++ ) */
  486. /* FT_Done_Glyph( glyphs[idx] ); */
  487. /* } */
  488. /* */
  489. FT_EXPORT( FT_Error )
  490. FT_Glyph_To_Bitmap( FT_Glyph* the_glyph,
  491. FT_Render_Mode render_mode,
  492. FT_Vector* origin,
  493. FT_Bool destroy );
  494. /*************************************************************************/
  495. /* */
  496. /* <Function> */
  497. /* FT_Done_Glyph */
  498. /* */
  499. /* <Description> */
  500. /* Destroy a given glyph. */
  501. /* */
  502. /* <Input> */
  503. /* glyph :: A handle to the target glyph object. */
  504. /* */
  505. FT_EXPORT( void )
  506. FT_Done_Glyph( FT_Glyph glyph );
  507. /* */
  508. /* other helpful functions */
  509. /*************************************************************************/
  510. /* */
  511. /* <Section> */
  512. /* computations */
  513. /* */
  514. /*************************************************************************/
  515. /*************************************************************************/
  516. /* */
  517. /* <Function> */
  518. /* FT_Matrix_Multiply */
  519. /* */
  520. /* <Description> */
  521. /* Perform the matrix operation `b = a*b'. */
  522. /* */
  523. /* <Input> */
  524. /* a :: A pointer to matrix `a'. */
  525. /* */
  526. /* <InOut> */
  527. /* b :: A pointer to matrix `b'. */
  528. /* */
  529. /* <Note> */
  530. /* The result is undefined if either `a' or `b' is zero. */
  531. /* */
  532. FT_EXPORT( void )
  533. FT_Matrix_Multiply( const FT_Matrix* a,
  534. FT_Matrix* b );
  535. /*************************************************************************/
  536. /* */
  537. /* <Function> */
  538. /* FT_Matrix_Invert */
  539. /* */
  540. /* <Description> */
  541. /* Invert a 2x2 matrix. Return an error if it can't be inverted. */
  542. /* */
  543. /* <InOut> */
  544. /* matrix :: A pointer to the target matrix. Remains untouched in */
  545. /* case of error. */
  546. /* */
  547. /* <Return> */
  548. /* FreeType error code. 0~means success. */
  549. /* */
  550. FT_EXPORT( FT_Error )
  551. FT_Matrix_Invert( FT_Matrix* matrix );
  552. /* */
  553. FT_END_HEADER
  554. #endif /* __FTGLYPH_H__ */
  555. /* END */
  556. /* Local Variables: */
  557. /* coding: utf-8 */
  558. /* End: */