/src/freetype/src/raster/ftrend1.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 305 lines · 192 code · 66 blank · 47 comment · 29 complexity · 0b9c7fa5feeabfb76325a4365c6ea370 MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ftrend1.c */
  4. /* */
  5. /* The FreeType glyph rasterizer interface (body). */
  6. /* */
  7. /* Copyright 1996-2003, 2005, 2006, 2011 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_OBJECTS_H
  19. #include FT_OUTLINE_H
  20. #include "ftrend1.h"
  21. #include "ftraster.h"
  22. #include "rastpic.h"
  23. #include "rasterrs.h"
  24. /* initialize renderer -- init its raster */
  25. static FT_Error
  26. ft_raster1_init( FT_Renderer render )
  27. {
  28. FT_Library library = FT_MODULE_LIBRARY( render );
  29. render->clazz->raster_class->raster_reset( render->raster,
  30. library->raster_pool,
  31. library->raster_pool_size );
  32. return Raster_Err_Ok;
  33. }
  34. /* set render-specific mode */
  35. static FT_Error
  36. ft_raster1_set_mode( FT_Renderer render,
  37. FT_ULong mode_tag,
  38. FT_Pointer data )
  39. {
  40. /* we simply pass it to the raster */
  41. return render->clazz->raster_class->raster_set_mode( render->raster,
  42. mode_tag,
  43. data );
  44. }
  45. /* transform a given glyph image */
  46. static FT_Error
  47. ft_raster1_transform( FT_Renderer render,
  48. FT_GlyphSlot slot,
  49. const FT_Matrix* matrix,
  50. const FT_Vector* delta )
  51. {
  52. FT_Error error = Raster_Err_Ok;
  53. if ( slot->format != render->glyph_format )
  54. {
  55. error = Raster_Err_Invalid_Argument;
  56. goto Exit;
  57. }
  58. if ( matrix )
  59. FT_Outline_Transform( &slot->outline, matrix );
  60. if ( delta )
  61. FT_Outline_Translate( &slot->outline, delta->x, delta->y );
  62. Exit:
  63. return error;
  64. }
  65. /* return the glyph's control box */
  66. static void
  67. ft_raster1_get_cbox( FT_Renderer render,
  68. FT_GlyphSlot slot,
  69. FT_BBox* cbox )
  70. {
  71. FT_MEM_ZERO( cbox, sizeof ( *cbox ) );
  72. if ( slot->format == render->glyph_format )
  73. FT_Outline_Get_CBox( &slot->outline, cbox );
  74. }
  75. /* convert a slot's glyph image into a bitmap */
  76. static FT_Error
  77. ft_raster1_render( FT_Renderer render,
  78. FT_GlyphSlot slot,
  79. FT_Render_Mode mode,
  80. const FT_Vector* origin )
  81. {
  82. FT_Error error;
  83. FT_Outline* outline;
  84. FT_BBox cbox;
  85. FT_UInt width, height, pitch;
  86. FT_Bitmap* bitmap;
  87. FT_Memory memory;
  88. FT_Raster_Params params;
  89. /* check glyph image format */
  90. if ( slot->format != render->glyph_format )
  91. {
  92. error = Raster_Err_Invalid_Argument;
  93. goto Exit;
  94. }
  95. /* check rendering mode */
  96. #ifndef FT_CONFIG_OPTION_PIC
  97. if ( mode != FT_RENDER_MODE_MONO )
  98. {
  99. /* raster1 is only capable of producing monochrome bitmaps */
  100. if ( render->clazz == &ft_raster1_renderer_class )
  101. return Raster_Err_Cannot_Render_Glyph;
  102. }
  103. else
  104. {
  105. /* raster5 is only capable of producing 5-gray-levels bitmaps */
  106. if ( render->clazz == &ft_raster5_renderer_class )
  107. return Raster_Err_Cannot_Render_Glyph;
  108. }
  109. #else /* FT_CONFIG_OPTION_PIC */
  110. /* When PIC is enabled, we cannot get to the class object */
  111. /* so instead we check the final character in the class name */
  112. /* ("raster5" or "raster1"). Yes this is a hack. */
  113. /* The "correct" thing to do is have different render function */
  114. /* for each of the classes. */
  115. if ( mode != FT_RENDER_MODE_MONO )
  116. {
  117. /* raster1 is only capable of producing monochrome bitmaps */
  118. if ( render->clazz->root.module_name[6] == '1' )
  119. return Raster_Err_Cannot_Render_Glyph;
  120. }
  121. else
  122. {
  123. /* raster5 is only capable of producing 5-gray-levels bitmaps */
  124. if ( render->clazz->root.module_name[6] == '5' )
  125. return Raster_Err_Cannot_Render_Glyph;
  126. }
  127. #endif /* FT_CONFIG_OPTION_PIC */
  128. outline = &slot->outline;
  129. /* translate the outline to the new origin if needed */
  130. if ( origin )
  131. FT_Outline_Translate( outline, origin->x, origin->y );
  132. /* compute the control box, and grid fit it */
  133. FT_Outline_Get_CBox( outline, &cbox );
  134. /* undocumented but confirmed: bbox values get rounded */
  135. #if 1
  136. cbox.xMin = FT_PIX_ROUND( cbox.xMin );
  137. cbox.yMin = FT_PIX_ROUND( cbox.yMin );
  138. cbox.xMax = FT_PIX_ROUND( cbox.xMax );
  139. cbox.yMax = FT_PIX_ROUND( cbox.yMax );
  140. #else
  141. cbox.xMin = FT_PIX_FLOOR( cbox.xMin );
  142. cbox.yMin = FT_PIX_FLOOR( cbox.yMin );
  143. cbox.xMax = FT_PIX_CEIL( cbox.xMax );
  144. cbox.yMax = FT_PIX_CEIL( cbox.yMax );
  145. #endif
  146. width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 );
  147. height = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 );
  148. if ( width > FT_USHORT_MAX || height > FT_USHORT_MAX )
  149. {
  150. error = Raster_Err_Invalid_Argument;
  151. goto Exit;
  152. }
  153. bitmap = &slot->bitmap;
  154. memory = render->root.memory;
  155. /* release old bitmap buffer */
  156. if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
  157. {
  158. FT_FREE( bitmap->buffer );
  159. slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
  160. }
  161. /* allocate new one, depends on pixel format */
  162. if ( !( mode & FT_RENDER_MODE_MONO ) )
  163. {
  164. /* we pad to 32 bits, only for backwards compatibility with FT 1.x */
  165. pitch = FT_PAD_CEIL( width, 4 );
  166. bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
  167. bitmap->num_grays = 256;
  168. }
  169. else
  170. {
  171. pitch = ( ( width + 15 ) >> 4 ) << 1;
  172. bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
  173. }
  174. bitmap->width = width;
  175. bitmap->rows = height;
  176. bitmap->pitch = pitch;
  177. if ( FT_ALLOC_MULT( bitmap->buffer, pitch, height ) )
  178. goto Exit;
  179. slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
  180. /* translate outline to render it into the bitmap */
  181. FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
  182. /* set up parameters */
  183. params.target = bitmap;
  184. params.source = outline;
  185. params.flags = 0;
  186. if ( bitmap->pixel_mode == FT_PIXEL_MODE_GRAY )
  187. params.flags |= FT_RASTER_FLAG_AA;
  188. /* render outline into the bitmap */
  189. error = render->raster_render( render->raster, &params );
  190. FT_Outline_Translate( outline, cbox.xMin, cbox.yMin );
  191. if ( error )
  192. goto Exit;
  193. slot->format = FT_GLYPH_FORMAT_BITMAP;
  194. slot->bitmap_left = (FT_Int)( cbox.xMin >> 6 );
  195. slot->bitmap_top = (FT_Int)( cbox.yMax >> 6 );
  196. Exit:
  197. return error;
  198. }
  199. FT_DEFINE_RENDERER( ft_raster1_renderer_class,
  200. FT_MODULE_RENDERER,
  201. sizeof ( FT_RendererRec ),
  202. "raster1",
  203. 0x10000L,
  204. 0x20000L,
  205. 0, /* module specific interface */
  206. (FT_Module_Constructor)ft_raster1_init,
  207. (FT_Module_Destructor) 0,
  208. (FT_Module_Requester) 0
  209. ,
  210. FT_GLYPH_FORMAT_OUTLINE,
  211. (FT_Renderer_RenderFunc) ft_raster1_render,
  212. (FT_Renderer_TransformFunc)ft_raster1_transform,
  213. (FT_Renderer_GetCBoxFunc) ft_raster1_get_cbox,
  214. (FT_Renderer_SetModeFunc) ft_raster1_set_mode,
  215. (FT_Raster_Funcs*) &FT_STANDARD_RASTER_GET
  216. )
  217. /* This renderer is _NOT_ part of the default modules; you will need */
  218. /* to register it by hand in your application. It should only be */
  219. /* used for backwards-compatibility with FT 1.x anyway. */
  220. /* */
  221. FT_DEFINE_RENDERER( ft_raster5_renderer_class,
  222. FT_MODULE_RENDERER,
  223. sizeof ( FT_RendererRec ),
  224. "raster5",
  225. 0x10000L,
  226. 0x20000L,
  227. 0, /* module specific interface */
  228. (FT_Module_Constructor)ft_raster1_init,
  229. (FT_Module_Destructor) 0,
  230. (FT_Module_Requester) 0
  231. ,
  232. FT_GLYPH_FORMAT_OUTLINE,
  233. (FT_Renderer_RenderFunc) ft_raster1_render,
  234. (FT_Renderer_TransformFunc)ft_raster1_transform,
  235. (FT_Renderer_GetCBoxFunc) ft_raster1_get_cbox,
  236. (FT_Renderer_SetModeFunc) ft_raster1_set_mode,
  237. (FT_Raster_Funcs*) &FT_STANDARD_RASTER_GET
  238. )
  239. /* END */