ftrend1.c 7.7 KB

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