ftgloadr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /***************************************************************************/
  2. /* */
  3. /* ftgloadr.c */
  4. /* */
  5. /* The FreeType glyph loader (body). */
  6. /* */
  7. /* Copyright 2002 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. #include <ft2build.h>
  18. #include FT_INTERNAL_GLYPH_LOADER_H
  19. #include FT_INTERNAL_MEMORY_H
  20. #undef FT_COMPONENT
  21. #define FT_COMPONENT trace_gloader
  22. /*************************************************************************/
  23. /*************************************************************************/
  24. /*************************************************************************/
  25. /***** *****/
  26. /***** *****/
  27. /***** G L Y P H L O A D E R *****/
  28. /***** *****/
  29. /***** *****/
  30. /*************************************************************************/
  31. /*************************************************************************/
  32. /*************************************************************************/
  33. /*************************************************************************/
  34. /* */
  35. /* The glyph loader is a simple object which is used to load a set of */
  36. /* glyphs easily. It is critical for the correct loading of composites. */
  37. /* */
  38. /* Ideally, one can see it as a stack of abstract `glyph' objects. */
  39. /* */
  40. /* loader.base Is really the bottom of the stack. It describes a */
  41. /* single glyph image made of the juxtaposition of */
  42. /* several glyphs (those `in the stack'). */
  43. /* */
  44. /* loader.current Describes the top of the stack, on which a new */
  45. /* glyph can be loaded. */
  46. /* */
  47. /* Rewind Clears the stack. */
  48. /* Prepare Set up `loader.current' for addition of a new glyph */
  49. /* image. */
  50. /* Add Add the `current' glyph image to the `base' one, */
  51. /* and prepare for another one. */
  52. /* */
  53. /* The glyph loader is now a base object. Each driver used to */
  54. /* re-implement it in one way or the other, which wasted code and */
  55. /* energy. */
  56. /* */
  57. /*************************************************************************/
  58. /* create a new glyph loader */
  59. FT_BASE_DEF( FT_Error )
  60. FT_GlyphLoader_New( FT_Memory memory,
  61. FT_GlyphLoader *aloader )
  62. {
  63. FT_GlyphLoader loader;
  64. FT_Error error;
  65. if ( !FT_NEW( loader ) )
  66. {
  67. loader->memory = memory;
  68. *aloader = loader;
  69. }
  70. return error;
  71. }
  72. /* rewind the glyph loader - reset counters to 0 */
  73. FT_BASE_DEF( void )
  74. FT_GlyphLoader_Rewind( FT_GlyphLoader loader )
  75. {
  76. FT_GlyphLoad base = &loader->base;
  77. FT_GlyphLoad current = &loader->current;
  78. base->outline.n_points = 0;
  79. base->outline.n_contours = 0;
  80. base->num_subglyphs = 0;
  81. *current = *base;
  82. }
  83. /* reset the glyph loader, frees all allocated tables */
  84. /* and starts from zero */
  85. FT_BASE_DEF( void )
  86. FT_GlyphLoader_Reset( FT_GlyphLoader loader )
  87. {
  88. FT_Memory memory = loader->memory;
  89. FT_FREE( loader->base.outline.points );
  90. FT_FREE( loader->base.outline.tags );
  91. FT_FREE( loader->base.outline.contours );
  92. FT_FREE( loader->base.extra_points );
  93. FT_FREE( loader->base.subglyphs );
  94. loader->max_points = 0;
  95. loader->max_contours = 0;
  96. loader->max_subglyphs = 0;
  97. FT_GlyphLoader_Rewind( loader );
  98. }
  99. /* delete a glyph loader */
  100. FT_BASE_DEF( void )
  101. FT_GlyphLoader_Done( FT_GlyphLoader loader )
  102. {
  103. if ( loader )
  104. {
  105. FT_Memory memory = loader->memory;
  106. FT_GlyphLoader_Reset( loader );
  107. FT_FREE( loader );
  108. }
  109. }
  110. /* re-adjust the `current' outline fields */
  111. static void
  112. FT_GlyphLoader_Adjust_Points( FT_GlyphLoader loader )
  113. {
  114. FT_Outline* base = &loader->base.outline;
  115. FT_Outline* current = &loader->current.outline;
  116. current->points = base->points + base->n_points;
  117. current->tags = base->tags + base->n_points;
  118. current->contours = base->contours + base->n_contours;
  119. /* handle extra points table - if any */
  120. if ( loader->use_extra )
  121. loader->current.extra_points =
  122. loader->base.extra_points + base->n_points;
  123. }
  124. FT_BASE_DEF( FT_Error )
  125. FT_GlyphLoader_CreateExtra( FT_GlyphLoader loader )
  126. {
  127. FT_Error error;
  128. FT_Memory memory = loader->memory;
  129. if ( !FT_NEW_ARRAY( loader->base.extra_points, loader->max_points ) )
  130. {
  131. loader->use_extra = 1;
  132. FT_GlyphLoader_Adjust_Points( loader );
  133. }
  134. return error;
  135. }
  136. /* re-adjust the `current' subglyphs field */
  137. static void
  138. FT_GlyphLoader_Adjust_Subglyphs( FT_GlyphLoader loader )
  139. {
  140. FT_GlyphLoad base = &loader->base;
  141. FT_GlyphLoad current = &loader->current;
  142. current->subglyphs = base->subglyphs + base->num_subglyphs;
  143. }
  144. /* Ensure that we can add `n_points' and `n_contours' to our glyph. this */
  145. /* function reallocates its outline tables if necessary. Note that it */
  146. /* DOESN'T change the number of points within the loader! */
  147. /* */
  148. FT_BASE_DEF( FT_Error )
  149. FT_GlyphLoader_CheckPoints( FT_GlyphLoader loader,
  150. FT_UInt n_points,
  151. FT_UInt n_contours )
  152. {
  153. FT_Memory memory = loader->memory;
  154. FT_Error error = FT_Err_Ok;
  155. FT_Outline* base = &loader->base.outline;
  156. FT_Outline* current = &loader->current.outline;
  157. FT_Bool adjust = 1;
  158. FT_UInt new_max, old_max;
  159. /* check points & tags */
  160. new_max = base->n_points + current->n_points + n_points;
  161. old_max = loader->max_points;
  162. if ( new_max > old_max )
  163. {
  164. new_max = ( new_max + 7 ) & -8;
  165. if ( FT_RENEW_ARRAY( base->points, old_max, new_max ) ||
  166. FT_RENEW_ARRAY( base->tags, old_max, new_max ) )
  167. goto Exit;
  168. if ( loader->use_extra &&
  169. FT_RENEW_ARRAY( loader->base.extra_points, old_max, new_max ) )
  170. goto Exit;
  171. adjust = 1;
  172. loader->max_points = new_max;
  173. }
  174. /* check contours */
  175. old_max = loader->max_contours;
  176. new_max = base->n_contours + current->n_contours +
  177. n_contours;
  178. if ( new_max > old_max )
  179. {
  180. new_max = ( new_max + 3 ) & -4;
  181. if ( FT_RENEW_ARRAY( base->contours, old_max, new_max ) )
  182. goto Exit;
  183. adjust = 1;
  184. loader->max_contours = new_max;
  185. }
  186. if ( adjust )
  187. FT_GlyphLoader_Adjust_Points( loader );
  188. Exit:
  189. return error;
  190. }
  191. /* Ensure that we can add `n_subglyphs' to our glyph. this function */
  192. /* reallocates its subglyphs table if necessary. Note that it DOES */
  193. /* NOT change the number of subglyphs within the loader! */
  194. /* */
  195. FT_BASE_DEF( FT_Error )
  196. FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader loader,
  197. FT_UInt n_subs )
  198. {
  199. FT_Memory memory = loader->memory;
  200. FT_Error error = FT_Err_Ok;
  201. FT_UInt new_max, old_max;
  202. FT_GlyphLoad base = &loader->base;
  203. FT_GlyphLoad current = &loader->current;
  204. new_max = base->num_subglyphs + current->num_subglyphs + n_subs;
  205. old_max = loader->max_subglyphs;
  206. if ( new_max > old_max )
  207. {
  208. new_max = ( new_max + 1 ) & -2;
  209. if ( FT_RENEW_ARRAY( base->subglyphs, old_max, new_max ) )
  210. goto Exit;
  211. loader->max_subglyphs = new_max;
  212. FT_GlyphLoader_Adjust_Subglyphs( loader );
  213. }
  214. Exit:
  215. return error;
  216. }
  217. /* prepare loader for the addition of a new glyph on top of the base one */
  218. FT_BASE_DEF( void )
  219. FT_GlyphLoader_Prepare( FT_GlyphLoader loader )
  220. {
  221. FT_GlyphLoad current = &loader->current;
  222. current->outline.n_points = 0;
  223. current->outline.n_contours = 0;
  224. current->num_subglyphs = 0;
  225. FT_GlyphLoader_Adjust_Points ( loader );
  226. FT_GlyphLoader_Adjust_Subglyphs( loader );
  227. }
  228. /* add current glyph to the base image - and prepare for another */
  229. FT_BASE_DEF( void )
  230. FT_GlyphLoader_Add( FT_GlyphLoader loader )
  231. {
  232. FT_GlyphLoad base = &loader->base;
  233. FT_GlyphLoad current = &loader->current;
  234. FT_UInt n_curr_contours = current->outline.n_contours;
  235. FT_UInt n_base_points = base->outline.n_points;
  236. FT_UInt n;
  237. base->outline.n_points =
  238. (short)( base->outline.n_points + current->outline.n_points );
  239. base->outline.n_contours =
  240. (short)( base->outline.n_contours + current->outline.n_contours );
  241. base->num_subglyphs += current->num_subglyphs;
  242. /* adjust contours count in newest outline */
  243. for ( n = 0; n < n_curr_contours; n++ )
  244. current->outline.contours[n] =
  245. (short)( current->outline.contours[n] + n_base_points );
  246. /* prepare for another new glyph image */
  247. FT_GlyphLoader_Prepare( loader );
  248. }
  249. FT_BASE_DEF( FT_Error )
  250. FT_GlyphLoader_CopyPoints( FT_GlyphLoader target,
  251. FT_GlyphLoader source )
  252. {
  253. FT_Error error;
  254. FT_UInt num_points = source->base.outline.n_points;
  255. FT_UInt num_contours = source->base.outline.n_contours;
  256. error = FT_GlyphLoader_CheckPoints( target, num_points, num_contours );
  257. if ( !error )
  258. {
  259. FT_Outline* out = &target->base.outline;
  260. FT_Outline* in = &source->base.outline;
  261. FT_MEM_COPY( out->points, in->points,
  262. num_points * sizeof ( FT_Vector ) );
  263. FT_MEM_COPY( out->tags, in->tags,
  264. num_points * sizeof ( char ) );
  265. FT_MEM_COPY( out->contours, in->contours,
  266. num_contours * sizeof ( short ) );
  267. /* do we need to copy the extra points? */
  268. if ( target->use_extra && source->use_extra )
  269. FT_MEM_COPY( target->base.extra_points, source->base.extra_points,
  270. num_points * sizeof ( FT_Vector ) );
  271. out->n_points = (short)num_points;
  272. out->n_contours = (short)num_contours;
  273. FT_GlyphLoader_Adjust_Points( target );
  274. }
  275. return error;
  276. }
  277. /* END */