1 /***************************************************************************/
5 /* The FreeType glyph rasterizer interface (body). */
7 /* Copyright 1996-2016 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_DEBUG_H
21 #include FT_INTERNAL_OBJECTS_H
30 /* initialize renderer -- init its raster */
32 ft_raster1_init( FT_Renderer render
)
34 FT_Library library
= FT_MODULE_LIBRARY( render
);
37 render
->clazz
->raster_class
->raster_reset( render
->raster
,
39 library
->raster_pool_size
);
45 /* set render-specific mode */
47 ft_raster1_set_mode( FT_Renderer render
,
51 /* we simply pass it to the raster */
52 return render
->clazz
->raster_class
->raster_set_mode( render
->raster
,
58 /* transform a given glyph image */
60 ft_raster1_transform( FT_Renderer render
,
62 const FT_Matrix
* matrix
,
63 const FT_Vector
* delta
)
65 FT_Error error
= FT_Err_Ok
;
68 if ( slot
->format
!= render
->glyph_format
)
70 error
= FT_THROW( Invalid_Argument
);
75 FT_Outline_Transform( &slot
->outline
, matrix
);
78 FT_Outline_Translate( &slot
->outline
, delta
->x
, delta
->y
);
85 /* return the glyph's control box */
87 ft_raster1_get_cbox( FT_Renderer render
,
91 FT_MEM_ZERO( cbox
, sizeof ( *cbox
) );
93 if ( slot
->format
== render
->glyph_format
)
94 FT_Outline_Get_CBox( &slot
->outline
, cbox
);
98 /* convert a slot's glyph image into a bitmap */
100 ft_raster1_render( FT_Renderer render
,
103 const FT_Vector
* origin
)
108 FT_UInt width
, height
, pitch
;
112 FT_Raster_Params params
;
115 /* check glyph image format */
116 if ( slot
->format
!= render
->glyph_format
)
118 error
= FT_THROW( Invalid_Argument
);
122 /* check rendering mode */
123 if ( mode
!= FT_RENDER_MODE_MONO
)
125 /* raster1 is only capable of producing monochrome bitmaps */
126 return FT_THROW( Cannot_Render_Glyph
);
129 outline
= &slot
->outline
;
131 /* translate the outline to the new origin if needed */
133 FT_Outline_Translate( outline
, origin
->x
, origin
->y
);
135 /* compute the control box, and grid fit it */
136 FT_Outline_Get_CBox( outline
, &cbox0
);
138 /* undocumented but confirmed: bbox values get rounded */
140 cbox
.xMin
= FT_PIX_ROUND( cbox0
.xMin
);
141 cbox
.yMin
= FT_PIX_ROUND( cbox0
.yMin
);
142 cbox
.xMax
= FT_PIX_ROUND( cbox0
.xMax
);
143 cbox
.yMax
= FT_PIX_ROUND( cbox0
.yMax
);
145 cbox
.xMin
= FT_PIX_FLOOR( cbox
.xMin
);
146 cbox
.yMin
= FT_PIX_FLOOR( cbox
.yMin
);
147 cbox
.xMax
= FT_PIX_CEIL( cbox
.xMax
);
148 cbox
.yMax
= FT_PIX_CEIL( cbox
.yMax
);
151 /* If either `width' or `height' round to 0, try */
152 /* explicitly rounding up/down. In the case of */
153 /* glyphs containing only one very narrow feature, */
154 /* this gives the drop-out compensation in the scan */
155 /* conversion code a chance to do its stuff. */
156 width
= (FT_UInt
)( ( cbox
.xMax
- cbox
.xMin
) >> 6 );
159 cbox
.xMin
= FT_PIX_FLOOR( cbox0
.xMin
);
160 cbox
.xMax
= FT_PIX_CEIL( cbox0
.xMax
);
162 width
= (FT_UInt
)( ( cbox
.xMax
- cbox
.xMin
) >> 6 );
165 height
= (FT_UInt
)( ( cbox
.yMax
- cbox
.yMin
) >> 6 );
168 cbox
.yMin
= FT_PIX_FLOOR( cbox0
.yMin
);
169 cbox
.yMax
= FT_PIX_CEIL( cbox0
.yMax
);
171 height
= (FT_UInt
)( ( cbox
.yMax
- cbox
.yMin
) >> 6 );
174 if ( width
> FT_USHORT_MAX
|| height
> FT_USHORT_MAX
)
176 error
= FT_THROW( Invalid_Argument
);
180 bitmap
= &slot
->bitmap
;
181 memory
= render
->root
.memory
;
183 /* release old bitmap buffer */
184 if ( slot
->internal
->flags
& FT_GLYPH_OWN_BITMAP
)
186 FT_FREE( bitmap
->buffer
);
187 slot
->internal
->flags
&= ~FT_GLYPH_OWN_BITMAP
;
190 pitch
= ( ( width
+ 15 ) >> 4 ) << 1;
191 bitmap
->pixel_mode
= FT_PIXEL_MODE_MONO
;
193 bitmap
->width
= width
;
194 bitmap
->rows
= height
;
195 bitmap
->pitch
= (int)pitch
;
197 if ( FT_ALLOC_MULT( bitmap
->buffer
, pitch
, height
) )
200 slot
->internal
->flags
|= FT_GLYPH_OWN_BITMAP
;
202 /* translate outline to render it into the bitmap */
203 FT_Outline_Translate( outline
, -cbox
.xMin
, -cbox
.yMin
);
205 /* set up parameters */
206 params
.target
= bitmap
;
207 params
.source
= outline
;
210 /* render outline into the bitmap */
211 error
= render
->raster_render( render
->raster
, ¶ms
);
213 FT_Outline_Translate( outline
, cbox
.xMin
, cbox
.yMin
);
218 slot
->format
= FT_GLYPH_FORMAT_BITMAP
;
219 slot
->bitmap_left
= (FT_Int
)( cbox
.xMin
>> 6 );
220 slot
->bitmap_top
= (FT_Int
)( cbox
.yMax
>> 6 );
227 FT_DEFINE_RENDERER( ft_raster1_renderer_class
,
230 sizeof ( FT_RendererRec
),
236 0, /* module specific interface */
238 (FT_Module_Constructor
)ft_raster1_init
,
239 (FT_Module_Destructor
) 0,
240 (FT_Module_Requester
) 0
243 FT_GLYPH_FORMAT_OUTLINE
,
245 (FT_Renderer_RenderFunc
) ft_raster1_render
,
246 (FT_Renderer_TransformFunc
)ft_raster1_transform
,
247 (FT_Renderer_GetCBoxFunc
) ft_raster1_get_cbox
,
248 (FT_Renderer_SetModeFunc
) ft_raster1_set_mode
,
250 (FT_Raster_Funcs
*) &FT_STANDARD_RASTER_GET