Imported Upstream version 2.10.4
[platform/upstream/freetype2.git] / src / raster / ftrend1.c
1 /****************************************************************************
2  *
3  * ftrend1.c
4  *
5  *   The FreeType glyph rasterizer interface (body).
6  *
7  * Copyright (C) 1996-2020 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
18
19 #include <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftobjs.h>
21 #include <freetype/ftoutln.h>
22 #include "ftrend1.h"
23 #include "ftraster.h"
24
25 #include "rasterrs.h"
26
27
28   /* initialize renderer -- init its raster */
29   static FT_Error
30   ft_raster1_init( FT_Renderer  render )
31   {
32     render->clazz->raster_class->raster_reset( render->raster, NULL, 0 );
33
34     return FT_Err_Ok;
35   }
36
37
38   /* set render-specific mode */
39   static FT_Error
40   ft_raster1_set_mode( FT_Renderer  render,
41                        FT_ULong     mode_tag,
42                        FT_Pointer   data )
43   {
44     /* we simply pass it to the raster */
45     return render->clazz->raster_class->raster_set_mode( render->raster,
46                                                          mode_tag,
47                                                          data );
48   }
49
50
51   /* transform a given glyph image */
52   static FT_Error
53   ft_raster1_transform( FT_Renderer       render,
54                         FT_GlyphSlot      slot,
55                         const FT_Matrix*  matrix,
56                         const FT_Vector*  delta )
57   {
58     FT_Error error = FT_Err_Ok;
59
60
61     if ( slot->format != render->glyph_format )
62     {
63       error = FT_THROW( Invalid_Argument );
64       goto Exit;
65     }
66
67     if ( matrix )
68       FT_Outline_Transform( &slot->outline, matrix );
69
70     if ( delta )
71       FT_Outline_Translate( &slot->outline, delta->x, delta->y );
72
73   Exit:
74     return error;
75   }
76
77
78   /* return the glyph's control box */
79   static void
80   ft_raster1_get_cbox( FT_Renderer   render,
81                        FT_GlyphSlot  slot,
82                        FT_BBox*      cbox )
83   {
84     FT_ZERO( cbox );
85
86     if ( slot->format == render->glyph_format )
87       FT_Outline_Get_CBox( &slot->outline, cbox );
88   }
89
90
91   /* convert a slot's glyph image into a bitmap */
92   static FT_Error
93   ft_raster1_render( FT_Renderer       render,
94                      FT_GlyphSlot      slot,
95                      FT_Render_Mode    mode,
96                      const FT_Vector*  origin )
97   {
98     FT_Error     error   = FT_Err_Ok;
99     FT_Outline*  outline = &slot->outline;
100     FT_Bitmap*   bitmap  = &slot->bitmap;
101     FT_Memory    memory  = render->root.memory;
102     FT_Pos       x_shift = 0;
103     FT_Pos       y_shift = 0;
104
105     FT_Raster_Params  params;
106
107
108     /* check glyph image format */
109     if ( slot->format != render->glyph_format )
110     {
111       error = FT_THROW( Invalid_Argument );
112       goto Exit;
113     }
114
115     /* check rendering mode */
116     if ( mode != FT_RENDER_MODE_MONO )
117     {
118       /* raster1 is only capable of producing monochrome bitmaps */
119       return FT_THROW( Cannot_Render_Glyph );
120     }
121
122     /* release old bitmap buffer */
123     if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
124     {
125       FT_FREE( bitmap->buffer );
126       slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
127     }
128
129     if ( ft_glyphslot_preset_bitmap( slot, mode, origin ) )
130     {
131       error = FT_THROW( Raster_Overflow );
132       goto Exit;
133     }
134
135     /* allocate new one */
136     if ( FT_ALLOC_MULT( bitmap->buffer, bitmap->rows, bitmap->pitch ) )
137       goto Exit;
138
139     slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
140
141     x_shift = -slot->bitmap_left * 64;
142     y_shift = ( (FT_Int)bitmap->rows - slot->bitmap_top ) * 64;
143
144     if ( origin )
145     {
146       x_shift += origin->x;
147       y_shift += origin->y;
148     }
149
150     /* translate outline to render it into the bitmap */
151     if ( x_shift || y_shift )
152       FT_Outline_Translate( outline, x_shift, y_shift );
153
154     /* set up parameters */
155     params.target = bitmap;
156     params.source = outline;
157     params.flags  = FT_RASTER_FLAG_DEFAULT;
158
159     /* render outline into the bitmap */
160     error = render->raster_render( render->raster, &params );
161
162   Exit:
163     if ( !error )
164       /* everything is fine; the glyph is now officially a bitmap */
165       slot->format = FT_GLYPH_FORMAT_BITMAP;
166     else if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
167     {
168       FT_FREE( bitmap->buffer );
169       slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
170     }
171
172     if ( x_shift || y_shift )
173       FT_Outline_Translate( outline, -x_shift, -y_shift );
174
175     return error;
176   }
177
178
179   FT_DEFINE_RENDERER(
180     ft_raster1_renderer_class,
181
182       FT_MODULE_RENDERER,
183       sizeof ( FT_RendererRec ),
184
185       "raster1",
186       0x10000L,
187       0x20000L,
188
189       NULL,    /* module specific interface */
190
191       (FT_Module_Constructor)ft_raster1_init,  /* module_init   */
192       (FT_Module_Destructor) NULL,             /* module_done   */
193       (FT_Module_Requester)  NULL,             /* get_interface */
194
195     FT_GLYPH_FORMAT_OUTLINE,
196
197     (FT_Renderer_RenderFunc)   ft_raster1_render,     /* render_glyph    */
198     (FT_Renderer_TransformFunc)ft_raster1_transform,  /* transform_glyph */
199     (FT_Renderer_GetCBoxFunc)  ft_raster1_get_cbox,   /* get_glyph_cbox  */
200     (FT_Renderer_SetModeFunc)  ft_raster1_set_mode,   /* set_mode        */
201
202     (FT_Raster_Funcs*)&ft_standard_raster             /* raster_class    */
203   )
204
205
206 /* END */