1 /***************************************************************************/
2 /* */
3 /* ftsmooth.c */
4 /* */
5 /* Anti-aliasing renderer interface (body). */
6 /* */
7 /* Copyright 2000-2001 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 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_INTERNAL_OBJECTS_H
21 #include FT_OUTLINE_H
22 #include "ftsmooth.h"
23 #include "ftgrays.h"
25 #include "ftsmerrs.h"
28 /* initialize renderer -- init its raster */
29 static FT_Error
30 ft_smooth_init( FT_Renderer render )
31 {
32 FT_Library library = FT_MODULE_LIBRARY( render );
35 render->clazz->raster_class->raster_reset( render->raster,
36 library->raster_pool,
37 library->raster_pool_size );
39 return 0;
40 }
43 /* sets render-specific mode */
44 static FT_Error
45 ft_smooth_set_mode( FT_Renderer render,
46 FT_ULong mode_tag,
47 FT_Pointer data )
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 }
55 /* transform a given glyph image */
56 static FT_Error
57 ft_smooth_transform( FT_Renderer render,
58 FT_GlyphSlot slot,
59 FT_Matrix* matrix,
60 FT_Vector* delta )
61 {
62 FT_Error error = Smooth_Err_Ok;
65 if ( slot->format != render->glyph_format )
66 {
67 error = Smooth_Err_Invalid_Argument;
68 goto Exit;
69 }
71 if ( matrix )
72 FT_Outline_Transform( &slot->outline, matrix );
74 if ( delta )
75 FT_Outline_Translate( &slot->outline, delta->x, delta->y );
77 Exit:
78 return error;
79 }
82 /* return the glyph's control box */
83 static void
84 ft_smooth_get_cbox( FT_Renderer render,
85 FT_GlyphSlot slot,
86 FT_BBox* cbox )
87 {
88 MEM_Set( cbox, 0, sizeof ( *cbox ) );
90 if ( slot->format == render->glyph_format )
91 FT_Outline_Get_CBox( &slot->outline, cbox );
92 }
95 /* convert a slot's glyph image into a bitmap */
96 static FT_Error
97 ft_smooth_render( FT_Renderer render,
98 FT_GlyphSlot slot,
99 FT_UInt mode,
100 FT_Vector* origin )
101 {
102 FT_Error error;
103 FT_Outline* outline;
104 FT_BBox cbox;
105 FT_UInt width, height, pitch;
106 FT_Bitmap* bitmap;
107 FT_Memory memory;
109 FT_Raster_Params params;
112 /* check glyph image format */
113 if ( slot->format != render->glyph_format )
114 {
115 error = Smooth_Err_Invalid_Argument;
116 goto Exit;
117 }
119 /* check mode */
120 if ( mode != ft_render_mode_normal )
121 return Smooth_Err_Cannot_Render_Glyph;
123 outline = &slot->outline;
125 /* translate the outline to the new origin if needed */
126 if ( origin )
127 FT_Outline_Translate( outline, origin->x, origin->y );
129 /* compute the control box, and grid fit it */
130 FT_Outline_Get_CBox( outline, &cbox );
132 cbox.xMin &= -64;
133 cbox.yMin &= -64;
134 cbox.xMax = ( cbox.xMax + 63 ) & -64;
135 cbox.yMax = ( cbox.yMax + 63 ) & -64;
137 width = ( cbox.xMax - cbox.xMin ) >> 6;
138 height = ( cbox.yMax - cbox.yMin ) >> 6;
139 bitmap = &slot->bitmap;
140 memory = render->root.memory;
142 /* release old bitmap buffer */
143 if ( slot->flags & ft_glyph_own_bitmap )
144 {
145 FREE( bitmap->buffer );
146 slot->flags &= ~ft_glyph_own_bitmap;
147 }
149 /* allocate new one, depends on pixel format */
150 pitch = width;
151 bitmap->pixel_mode = ft_pixel_mode_grays;
152 bitmap->num_grays = 256;
153 bitmap->width = width;
154 bitmap->rows = height;
155 bitmap->pitch = pitch;
157 if ( ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) )
158 goto Exit;
160 slot->flags |= ft_glyph_own_bitmap;
162 /* translate outline to render it into the bitmap */
163 FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
165 /* set up parameters */
166 params.target = bitmap;
167 params.source = outline;
168 params.flags = ft_raster_flag_aa;
170 /* render outline into the bitmap */
171 error = render->raster_render( render->raster, ¶ms );
172 if ( error )
173 goto Exit;
175 slot->format = ft_glyph_format_bitmap;
176 slot->bitmap_left = cbox.xMin >> 6;
177 slot->bitmap_top = cbox.yMax >> 6;
179 Exit:
180 return error;
181 }
184 FT_CALLBACK_TABLE_DEF
185 const FT_Renderer_Class ft_smooth_renderer_class =
186 {
187 {
188 ft_module_renderer,
189 sizeof( FT_RendererRec ),
191 "smooth",
192 0x10000L,
193 0x20000L,
195 0, /* module specific interface */
197 (FT_Module_Constructor)ft_smooth_init,
198 (FT_Module_Destructor) 0,
199 (FT_Module_Requester) 0
200 },
202 ft_glyph_format_outline,
204 (FTRenderer_render) ft_smooth_render,
205 (FTRenderer_transform)ft_smooth_transform,
206 (FTRenderer_getCBox) ft_smooth_get_cbox,
207 (FTRenderer_setMode) ft_smooth_set_mode,
209 (FT_Raster_Funcs*) &ft_grays_raster
210 };
213 /* END */