David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 1 | /***************************************************************************/ |
| 2 | /* */ |
| 3 | /* ftrend1.c */ |
| 4 | /* */ |
| 5 | /* The FreeType glyph rasterizer interface (body). */ |
| 6 | /* */ |
Werner Lemberg | 415235d | 2001-06-28 17:49:10 +0000 | [diff] [blame] | 7 | /* Copyright 1996-2001 by */ |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 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 | |
David Turner | 19ed8af | 2000-12-08 02:42:29 +0000 | [diff] [blame] | 19 | #include <ft2build.h> |
| 20 | #include FT_INTERNAL_OBJECTS_H |
| 21 | #include FT_OUTLINE_H |
David Turner | 8d3a401 | 2001-03-20 11:14:24 +0000 | [diff] [blame] | 22 | #include "ftrend1.h" |
| 23 | #include "ftraster.h" |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 24 | |
Werner Lemberg | 1f7f0e8 | 2001-06-06 17:30:41 +0000 | [diff] [blame] | 25 | #include "rasterrs.h" |
| 26 | |
Werner Lemberg | cc069be | 2000-12-08 16:17:16 +0000 | [diff] [blame] | 27 | |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 28 | /* initialize renderer -- init its raster */ |
Werner Lemberg | 52005c3 | 2001-06-27 23:25:46 +0000 | [diff] [blame] | 29 | static FT_Error |
| 30 | ft_raster1_init( FT_Renderer render ) |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 31 | { |
| 32 | FT_Library library = FT_MODULE_LIBRARY( render ); |
| 33 | |
| 34 | |
| 35 | render->clazz->raster_class->raster_reset( render->raster, |
| 36 | library->raster_pool, |
| 37 | library->raster_pool_size ); |
| 38 | |
Werner Lemberg | 1f7f0e8 | 2001-06-06 17:30:41 +0000 | [diff] [blame] | 39 | return Raster_Err_Ok; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
| 43 | /* set render-specific mode */ |
Werner Lemberg | 52005c3 | 2001-06-27 23:25:46 +0000 | [diff] [blame] | 44 | static FT_Error |
| 45 | ft_raster1_set_mode( FT_Renderer render, |
| 46 | FT_ULong mode_tag, |
| 47 | FT_Pointer data ) |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 48 | { |
| 49 | /* we simply pass it to the raster */ |
| 50 | return render->clazz->raster_class->raster_set_mode( render->raster, |
| 51 | mode_tag, |
| 52 | data ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* transform a given glyph image */ |
Werner Lemberg | 52005c3 | 2001-06-27 23:25:46 +0000 | [diff] [blame] | 57 | static FT_Error |
| 58 | ft_raster1_transform( FT_Renderer render, |
| 59 | FT_GlyphSlot slot, |
| 60 | FT_Matrix* matrix, |
| 61 | FT_Vector* delta ) |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 62 | { |
Werner Lemberg | 1f7f0e8 | 2001-06-06 17:30:41 +0000 | [diff] [blame] | 63 | FT_Error error = Raster_Err_Ok; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | if ( slot->format != render->glyph_format ) |
| 67 | { |
Werner Lemberg | 1f7f0e8 | 2001-06-06 17:30:41 +0000 | [diff] [blame] | 68 | error = Raster_Err_Invalid_Argument; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 69 | goto Exit; |
| 70 | } |
| 71 | |
| 72 | if ( matrix ) |
| 73 | FT_Outline_Transform( &slot->outline, matrix ); |
| 74 | |
| 75 | if ( delta ) |
| 76 | FT_Outline_Translate( &slot->outline, delta->x, delta->y ); |
| 77 | |
| 78 | Exit: |
| 79 | return error; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* return the glyph's control box */ |
Werner Lemberg | 52005c3 | 2001-06-27 23:25:46 +0000 | [diff] [blame] | 84 | static void |
| 85 | ft_raster1_get_cbox( FT_Renderer render, |
| 86 | FT_GlyphSlot slot, |
| 87 | FT_BBox* cbox ) |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 88 | { |
David Turner | e459d74 | 2002-03-22 13:52:37 +0000 | [diff] [blame^] | 89 | FT_MEM_SET( cbox, 0, sizeof ( *cbox ) ); |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 90 | |
| 91 | if ( slot->format == render->glyph_format ) |
| 92 | FT_Outline_Get_CBox( &slot->outline, cbox ); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* convert a slot's glyph image into a bitmap */ |
Werner Lemberg | 52005c3 | 2001-06-27 23:25:46 +0000 | [diff] [blame] | 97 | static FT_Error |
| 98 | ft_raster1_render( FT_Renderer render, |
| 99 | FT_GlyphSlot slot, |
| 100 | FT_UInt mode, |
| 101 | FT_Vector* origin ) |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 102 | { |
| 103 | FT_Error error; |
| 104 | FT_Outline* outline; |
| 105 | FT_BBox cbox; |
| 106 | FT_UInt width, height, pitch; |
| 107 | FT_Bitmap* bitmap; |
| 108 | FT_Memory memory; |
| 109 | |
| 110 | FT_Raster_Params params; |
| 111 | |
| 112 | |
| 113 | /* check glyph image format */ |
| 114 | if ( slot->format != render->glyph_format ) |
| 115 | { |
Werner Lemberg | 1f7f0e8 | 2001-06-06 17:30:41 +0000 | [diff] [blame] | 116 | error = Raster_Err_Invalid_Argument; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 117 | goto Exit; |
| 118 | } |
| 119 | |
| 120 | /* check rendering mode */ |
| 121 | if ( mode != ft_render_mode_mono ) |
| 122 | { |
| 123 | /* raster1 is only capable of producing monochrome bitmaps */ |
| 124 | if ( render->clazz == &ft_raster1_renderer_class ) |
Werner Lemberg | 1f7f0e8 | 2001-06-06 17:30:41 +0000 | [diff] [blame] | 125 | return Raster_Err_Cannot_Render_Glyph; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
| 129 | /* raster5 is only capable of producing 5-gray-levels bitmaps */ |
| 130 | if ( render->clazz == &ft_raster5_renderer_class ) |
Werner Lemberg | 1f7f0e8 | 2001-06-06 17:30:41 +0000 | [diff] [blame] | 131 | return Raster_Err_Cannot_Render_Glyph; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | outline = &slot->outline; |
| 135 | |
| 136 | /* translate the outline to the new origin if needed */ |
| 137 | if ( origin ) |
| 138 | FT_Outline_Translate( outline, origin->x, origin->y ); |
| 139 | |
| 140 | /* compute the control box, and grid fit it */ |
| 141 | FT_Outline_Get_CBox( outline, &cbox ); |
| 142 | |
| 143 | cbox.xMin &= -64; |
| 144 | cbox.yMin &= -64; |
| 145 | cbox.xMax = ( cbox.xMax + 63 ) & -64; |
| 146 | cbox.yMax = ( cbox.yMax + 63 ) & -64; |
| 147 | |
| 148 | width = ( cbox.xMax - cbox.xMin ) >> 6; |
| 149 | height = ( cbox.yMax - cbox.yMin ) >> 6; |
| 150 | bitmap = &slot->bitmap; |
| 151 | memory = render->root.memory; |
| 152 | |
| 153 | /* release old bitmap buffer */ |
David Turner | 4a23351 | 2002-02-24 02:39:08 +0000 | [diff] [blame] | 154 | if ( slot->flags & FT_GLYPH_OWN_BITMAP ) |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 155 | { |
David Turner | e459d74 | 2002-03-22 13:52:37 +0000 | [diff] [blame^] | 156 | FT_FREE( bitmap->buffer ); |
David Turner | 4a23351 | 2002-02-24 02:39:08 +0000 | [diff] [blame] | 157 | slot->flags &= ~FT_GLYPH_OWN_BITMAP; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* allocate new one, depends on pixel format */ |
| 161 | if ( !( mode & ft_render_mode_mono ) ) |
| 162 | { |
| 163 | /* we pad to 32 bits, only for backwards compatibility with FT 1.x */ |
| 164 | pitch = ( width + 3 ) & -4; |
| 165 | bitmap->pixel_mode = ft_pixel_mode_grays; |
| 166 | bitmap->num_grays = 256; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | pitch = ( width + 7 ) >> 3; |
| 171 | bitmap->pixel_mode = ft_pixel_mode_mono; |
| 172 | } |
| 173 | |
| 174 | bitmap->width = width; |
| 175 | bitmap->rows = height; |
| 176 | bitmap->pitch = pitch; |
| 177 | |
David Turner | e459d74 | 2002-03-22 13:52:37 +0000 | [diff] [blame^] | 178 | if ( FT_ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) ) |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 179 | goto Exit; |
| 180 | |
David Turner | 4a23351 | 2002-02-24 02:39:08 +0000 | [diff] [blame] | 181 | slot->flags |= FT_GLYPH_OWN_BITMAP; |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 182 | |
| 183 | /* translate outline to render it into the bitmap */ |
| 184 | FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin ); |
| 185 | |
| 186 | /* set up parameters */ |
| 187 | params.target = bitmap; |
| 188 | params.source = outline; |
| 189 | params.flags = 0; |
| 190 | |
| 191 | if ( bitmap->pixel_mode == ft_pixel_mode_grays ) |
| 192 | params.flags |= ft_raster_flag_aa; |
| 193 | |
| 194 | /* render outline into the bitmap */ |
| 195 | error = render->raster_render( render->raster, ¶ms ); |
David Turner | fc3a316 | 2001-12-19 14:47:28 +0000 | [diff] [blame] | 196 | |
| 197 | FT_Outline_Translate( outline, cbox.xMin, cbox.yMin ); |
| 198 | |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 199 | if ( error ) |
| 200 | goto Exit; |
| 201 | |
| 202 | slot->format = ft_glyph_format_bitmap; |
| 203 | slot->bitmap_left = cbox.xMin >> 6; |
| 204 | slot->bitmap_top = cbox.yMax >> 6; |
| 205 | |
| 206 | Exit: |
| 207 | return error; |
| 208 | } |
| 209 | |
| 210 | |
David Turner | 76a5f62 | 2000-11-04 01:55:49 +0000 | [diff] [blame] | 211 | FT_CALLBACK_TABLE_DEF |
| 212 | const FT_Renderer_Class ft_raster1_renderer_class = |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 213 | { |
| 214 | { |
| 215 | ft_module_renderer, |
| 216 | sizeof( FT_RendererRec ), |
| 217 | |
| 218 | "raster1", |
| 219 | 0x10000L, |
| 220 | 0x20000L, |
| 221 | |
| 222 | 0, /* module specific interface */ |
| 223 | |
| 224 | (FT_Module_Constructor)ft_raster1_init, |
| 225 | (FT_Module_Destructor) 0, |
| 226 | (FT_Module_Requester) 0 |
| 227 | }, |
| 228 | |
| 229 | ft_glyph_format_outline, |
| 230 | |
| 231 | (FTRenderer_render) ft_raster1_render, |
| 232 | (FTRenderer_transform)ft_raster1_transform, |
| 233 | (FTRenderer_getCBox) ft_raster1_get_cbox, |
| 234 | (FTRenderer_setMode) ft_raster1_set_mode, |
| 235 | |
| 236 | (FT_Raster_Funcs*) &ft_standard_raster |
| 237 | }; |
| 238 | |
| 239 | |
| 240 | /* This renderer is _NOT_ part of the default modules; you will need */ |
| 241 | /* to register it by hand in your application. It should only be */ |
| 242 | /* used for backwards-compatibility with FT 1.x anyway. */ |
| 243 | /* */ |
David Turner | 76a5f62 | 2000-11-04 01:55:49 +0000 | [diff] [blame] | 244 | FT_CALLBACK_TABLE_DEF |
| 245 | const FT_Renderer_Class ft_raster5_renderer_class = |
David Turner | a4e2894 | 2000-10-26 00:30:33 +0000 | [diff] [blame] | 246 | { |
| 247 | { |
| 248 | ft_module_renderer, |
| 249 | sizeof( FT_RendererRec ), |
| 250 | |
| 251 | "raster5", |
| 252 | 0x10000L, |
| 253 | 0x20000L, |
| 254 | |
| 255 | 0, /* module specific interface */ |
| 256 | |
| 257 | (FT_Module_Constructor)ft_raster1_init, |
| 258 | (FT_Module_Destructor) 0, |
| 259 | (FT_Module_Requester) 0 |
| 260 | }, |
| 261 | |
| 262 | ft_glyph_format_outline, |
| 263 | |
| 264 | (FTRenderer_render) ft_raster1_render, |
| 265 | (FTRenderer_transform)ft_raster1_transform, |
| 266 | (FTRenderer_getCBox) ft_raster1_get_cbox, |
| 267 | (FTRenderer_setMode) ft_raster1_set_mode, |
| 268 | |
| 269 | (FT_Raster_Funcs*) &ft_standard_raster |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | /* END */ |