1 /***************************************************************************/
5 /* Anti-aliasing renderer interface (body). */
7 /* Copyright 2000-2001, 2002, 2003, 2004, 2005, 2006 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
20 #include FT_INTERNAL_OBJECTS_H
28 /* initialize renderer -- init its raster */
30 ft_smooth_init( FT_Renderer render
)
32 FT_Library library
= FT_MODULE_LIBRARY( render
);
35 render
->clazz
->raster_class
->raster_reset( render
->raster
,
37 library
->raster_pool_size
);
43 /* sets render-specific mode */
45 ft_smooth_set_mode( FT_Renderer render
,
49 /* we simply pass it to the raster */
50 return render
->clazz
->raster_class
->raster_set_mode( render
->raster
,
55 /* transform a given glyph image */
57 ft_smooth_transform( FT_Renderer render
,
59 const FT_Matrix
* matrix
,
60 const FT_Vector
* delta
)
62 FT_Error error
= Smooth_Err_Ok
;
65 if ( slot
->format
!= render
->glyph_format
)
67 error
= Smooth_Err_Invalid_Argument
;
72 FT_Outline_Transform( &slot
->outline
, matrix
);
75 FT_Outline_Translate( &slot
->outline
, delta
->x
, delta
->y
);
82 /* return the glyph's control box */
84 ft_smooth_get_cbox( FT_Renderer render
,
88 FT_MEM_ZERO( cbox
, sizeof ( *cbox
) );
90 if ( slot
->format
== render
->glyph_format
)
91 FT_Outline_Get_CBox( &slot
->outline
, cbox
);
95 /* convert a slot's glyph image into a bitmap */
97 ft_smooth_render_generic( FT_Renderer render
,
100 const FT_Vector
* origin
,
101 FT_Render_Mode required_mode
)
104 FT_Outline
* outline
= NULL
;
106 FT_UInt width
, height
, height_org
, width_org
, pitch
;
109 FT_Int hmul
= mode
== FT_RENDER_MODE_LCD
;
110 FT_Int vmul
= mode
== FT_RENDER_MODE_LCD_V
;
111 FT_Pos x_shift
, y_shift
, x_left
, y_top
;
113 FT_Raster_Params params
;
116 /* check glyph image format */
117 if ( slot
->format
!= render
->glyph_format
)
119 error
= Smooth_Err_Invalid_Argument
;
124 if ( mode
!= required_mode
)
125 return Smooth_Err_Cannot_Render_Glyph
;
127 outline
= &slot
->outline
;
129 /* translate the outline to the new origin if needed */
131 FT_Outline_Translate( outline
, origin
->x
, origin
->y
);
133 /* compute the control box, and grid fit it */
134 FT_Outline_Get_CBox( outline
, &cbox
);
136 cbox
.xMin
= FT_PIX_FLOOR( cbox
.xMin
);
137 cbox
.yMin
= FT_PIX_FLOOR( cbox
.yMin
);
138 cbox
.xMax
= FT_PIX_CEIL( cbox
.xMax
);
139 cbox
.yMax
= FT_PIX_CEIL( cbox
.yMax
);
141 width
= (FT_UInt
)( ( cbox
.xMax
- cbox
.xMin
) >> 6 );
142 height
= (FT_UInt
)( ( cbox
.yMax
- cbox
.yMin
) >> 6 );
143 bitmap
= &slot
->bitmap
;
144 memory
= render
->root
.memory
;
149 /* release old bitmap buffer */
150 if ( slot
->internal
->flags
& FT_GLYPH_OWN_BITMAP
)
152 FT_FREE( bitmap
->buffer
);
153 slot
->internal
->flags
&= ~FT_GLYPH_OWN_BITMAP
;
156 /* allocate new one, depends on pixel format */
161 pitch
= FT_PAD_CEIL( width
, 4 );
167 x_shift
= (FT_Int
) cbox
.xMin
;
168 y_shift
= (FT_Int
) cbox
.yMin
;
169 x_left
= (FT_Int
)( cbox
.xMin
>> 6 );
170 y_top
= (FT_Int
)( cbox
.yMax
>> 6 );
172 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
174 if ( slot
->library
->lcd_filter_func
)
176 FT_Int extra
= slot
->library
->lcd_extra
;
181 x_shift
-= 64 * ( extra
>> 1 );
183 pitch
= FT_PAD_CEIL( width
, 4 );
184 x_left
-= extra
>> 1;
189 y_shift
-= 64 * ( extra
>> 1 );
197 bitmap
->pixel_mode
= FT_PIXEL_MODE_GRAY
;
198 bitmap
->num_grays
= 256;
199 bitmap
->width
= width
;
200 bitmap
->rows
= height
;
201 bitmap
->pitch
= pitch
;
203 /* translate outline to render it into the bitmap */
204 FT_Outline_Translate( outline
, -x_shift
, -y_shift
);
206 if ( FT_ALLOC( bitmap
->buffer
, (FT_ULong
)pitch
* height
) )
209 slot
->internal
->flags
|= FT_GLYPH_OWN_BITMAP
;
211 /* set up parameters */
212 params
.target
= bitmap
;
213 params
.source
= outline
;
214 params
.flags
= FT_RASTER_FLAG_AA
;
216 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
218 /* implode outline if needed */
220 FT_Vector
* points
= outline
->points
;
221 FT_Vector
* points_end
= points
+ outline
->n_points
;
226 for ( vec
= points
; vec
< points_end
; vec
++ )
230 for ( vec
= points
; vec
< points_end
; vec
++ )
234 /* render outline into the bitmap */
235 error
= render
->raster_render( render
->raster
, ¶ms
);
237 /* deflate outline if needed */
239 FT_Vector
* points
= outline
->points
;
240 FT_Vector
* points_end
= points
+ outline
->n_points
;
245 for ( vec
= points
; vec
< points_end
; vec
++ )
249 for ( vec
= points
; vec
< points_end
; vec
++ )
253 if ( slot
->library
->lcd_filter_func
)
254 slot
->library
->lcd_filter_func( bitmap
, mode
, slot
->library
);
256 #else /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
258 /* render outline into bitmap */
259 error
= render
->raster_render( render
->raster
, ¶ms
);
261 /* expand it horizontally */
264 FT_Byte
* line
= bitmap
->buffer
;
268 for ( hh
= height_org
; hh
> 0; hh
--, line
+= pitch
)
271 FT_Byte
* end
= line
+ width
;
274 for ( xx
= width_org
; xx
> 0; xx
-- )
276 FT_UInt pixel
= line
[xx
-1];
279 end
[-3] = (FT_Byte
)pixel
;
280 end
[-2] = (FT_Byte
)pixel
;
281 end
[-1] = (FT_Byte
)pixel
;
287 /* expand it vertically */
290 FT_Byte
* read
= bitmap
->buffer
+ ( height
- height_org
) * pitch
;
291 FT_Byte
* write
= bitmap
->buffer
;
295 for ( hh
= height_org
; hh
> 0; hh
-- )
297 memcpy( write
, read
, pitch
);
300 memcpy( write
, read
, pitch
);
303 memcpy( write
, read
, pitch
);
309 #endif /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
311 FT_Outline_Translate( outline
, x_shift
, y_shift
);
316 slot
->format
= FT_GLYPH_FORMAT_BITMAP
;
317 slot
->bitmap_left
= x_left
;
318 slot
->bitmap_top
= y_top
;
321 if ( outline
&& origin
)
322 FT_Outline_Translate( outline
, -origin
->x
, -origin
->y
);
328 /* convert a slot's glyph image into a bitmap */
330 ft_smooth_render( FT_Renderer render
,
333 const FT_Vector
* origin
)
335 if ( mode
== FT_RENDER_MODE_LIGHT
)
336 mode
= FT_RENDER_MODE_NORMAL
;
338 return ft_smooth_render_generic( render
, slot
, mode
, origin
,
339 FT_RENDER_MODE_NORMAL
);
343 /* convert a slot's glyph image into a horizontal LCD bitmap */
345 ft_smooth_render_lcd( FT_Renderer render
,
348 const FT_Vector
* origin
)
352 error
= ft_smooth_render_generic( render
, slot
, mode
, origin
,
353 FT_RENDER_MODE_LCD
);
355 slot
->bitmap
.pixel_mode
= FT_PIXEL_MODE_LCD
;
361 /* convert a slot's glyph image into a vertical LCD bitmap */
363 ft_smooth_render_lcd_v( FT_Renderer render
,
366 const FT_Vector
* origin
)
370 error
= ft_smooth_render_generic( render
, slot
, mode
, origin
,
371 FT_RENDER_MODE_LCD_V
);
373 slot
->bitmap
.pixel_mode
= FT_PIXEL_MODE_LCD_V
;
379 FT_CALLBACK_TABLE_DEF
380 const FT_Renderer_Class ft_smooth_renderer_class
=
384 sizeof( FT_RendererRec
),
390 0, /* module specific interface */
392 (FT_Module_Constructor
)ft_smooth_init
,
393 (FT_Module_Destructor
) 0,
394 (FT_Module_Requester
) 0
397 FT_GLYPH_FORMAT_OUTLINE
,
399 (FT_Renderer_RenderFunc
) ft_smooth_render
,
400 (FT_Renderer_TransformFunc
)ft_smooth_transform
,
401 (FT_Renderer_GetCBoxFunc
) ft_smooth_get_cbox
,
402 (FT_Renderer_SetModeFunc
) ft_smooth_set_mode
,
404 (FT_Raster_Funcs
*) &ft_grays_raster
408 FT_CALLBACK_TABLE_DEF
409 const FT_Renderer_Class ft_smooth_lcd_renderer_class
=
413 sizeof( FT_RendererRec
),
419 0, /* module specific interface */
421 (FT_Module_Constructor
)ft_smooth_init
,
422 (FT_Module_Destructor
) 0,
423 (FT_Module_Requester
) 0
426 FT_GLYPH_FORMAT_OUTLINE
,
428 (FT_Renderer_RenderFunc
) ft_smooth_render_lcd
,
429 (FT_Renderer_TransformFunc
)ft_smooth_transform
,
430 (FT_Renderer_GetCBoxFunc
) ft_smooth_get_cbox
,
431 (FT_Renderer_SetModeFunc
) ft_smooth_set_mode
,
433 (FT_Raster_Funcs
*) &ft_grays_raster
438 FT_CALLBACK_TABLE_DEF
439 const FT_Renderer_Class ft_smooth_lcdv_renderer_class
=
443 sizeof( FT_RendererRec
),
449 0, /* module specific interface */
451 (FT_Module_Constructor
)ft_smooth_init
,
452 (FT_Module_Destructor
) 0,
453 (FT_Module_Requester
) 0
456 FT_GLYPH_FORMAT_OUTLINE
,
458 (FT_Renderer_RenderFunc
) ft_smooth_render_lcd_v
,
459 (FT_Renderer_TransformFunc
)ft_smooth_transform
,
460 (FT_Renderer_GetCBoxFunc
) ft_smooth_get_cbox
,
461 (FT_Renderer_SetModeFunc
) ft_smooth_set_mode
,
463 (FT_Raster_Funcs
*) &ft_grays_raster