/modules/freetype2/src/raster/ftrend1.c

http://github.com/zpao/v8monkey · C · 291 lines · 180 code · 65 blank · 46 comment · 27 complexity · 42d9ededcd107007d311046cb18ea375 MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ftrend1.c */
  4. /* */
  5. /* The FreeType glyph rasterizer interface (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2005, 2006 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. cbox.xMin = FT_PIX_FLOOR( cbox.xMin );
  135. cbox.yMin = FT_PIX_FLOOR( cbox.yMin );
  136. cbox.xMax = FT_PIX_CEIL( cbox.xMax );
  137. cbox.yMax = FT_PIX_CEIL( cbox.yMax );
  138. width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 );
  139. height = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 );
  140. bitmap = &slot->bitmap;
  141. memory = render->root.memory;
  142. /* release old bitmap buffer */
  143. if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
  144. {
  145. FT_FREE( bitmap->buffer );
  146. slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
  147. }
  148. /* allocate new one, depends on pixel format */
  149. if ( !( mode & FT_RENDER_MODE_MONO ) )
  150. {
  151. /* we pad to 32 bits, only for backwards compatibility with FT 1.x */
  152. pitch = FT_PAD_CEIL( width, 4 );
  153. bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
  154. bitmap->num_grays = 256;
  155. }
  156. else
  157. {
  158. pitch = ( ( width + 15 ) >> 4 ) << 1;
  159. bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
  160. }
  161. bitmap->width = width;
  162. bitmap->rows = height;
  163. bitmap->pitch = pitch;
  164. if ( FT_ALLOC_MULT( bitmap->buffer, pitch, height ) )
  165. goto Exit;
  166. slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
  167. /* translate outline to render it into the bitmap */
  168. FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
  169. /* set up parameters */
  170. params.target = bitmap;
  171. params.source = outline;
  172. params.flags = 0;
  173. if ( bitmap->pixel_mode == FT_PIXEL_MODE_GRAY )
  174. params.flags |= FT_RASTER_FLAG_AA;
  175. /* render outline into the bitmap */
  176. error = render->raster_render( render->raster, &params );
  177. FT_Outline_Translate( outline, cbox.xMin, cbox.yMin );
  178. if ( error )
  179. goto Exit;
  180. slot->format = FT_GLYPH_FORMAT_BITMAP;
  181. slot->bitmap_left = (FT_Int)( cbox.xMin >> 6 );
  182. slot->bitmap_top = (FT_Int)( cbox.yMax >> 6 );
  183. Exit:
  184. return error;
  185. }
  186. FT_DEFINE_RENDERER(ft_raster1_renderer_class,
  187. FT_MODULE_RENDERER,
  188. sizeof( FT_RendererRec ),
  189. "raster1",
  190. 0x10000L,
  191. 0x20000L,
  192. 0, /* module specific interface */
  193. (FT_Module_Constructor)ft_raster1_init,
  194. (FT_Module_Destructor) 0,
  195. (FT_Module_Requester) 0
  196. ,
  197. FT_GLYPH_FORMAT_OUTLINE,
  198. (FT_Renderer_RenderFunc) ft_raster1_render,
  199. (FT_Renderer_TransformFunc)ft_raster1_transform,
  200. (FT_Renderer_GetCBoxFunc) ft_raster1_get_cbox,
  201. (FT_Renderer_SetModeFunc) ft_raster1_set_mode,
  202. (FT_Raster_Funcs*) &FT_STANDARD_RASTER_GET
  203. )
  204. /* This renderer is _NOT_ part of the default modules; you will need */
  205. /* to register it by hand in your application. It should only be */
  206. /* used for backwards-compatibility with FT 1.x anyway. */
  207. /* */
  208. FT_DEFINE_RENDERER(ft_raster5_renderer_class,
  209. FT_MODULE_RENDERER,
  210. sizeof( FT_RendererRec ),
  211. "raster5",
  212. 0x10000L,
  213. 0x20000L,
  214. 0, /* module specific interface */
  215. (FT_Module_Constructor)ft_raster1_init,
  216. (FT_Module_Destructor) 0,
  217. (FT_Module_Requester) 0
  218. ,
  219. FT_GLYPH_FORMAT_OUTLINE,
  220. (FT_Renderer_RenderFunc) ft_raster1_render,
  221. (FT_Renderer_TransformFunc)ft_raster1_transform,
  222. (FT_Renderer_GetCBoxFunc) ft_raster1_get_cbox,
  223. (FT_Renderer_SetModeFunc) ft_raster1_set_mode,
  224. (FT_Raster_Funcs*) &FT_STANDARD_RASTER_GET
  225. )
  226. /* END */