~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/freetype/src/smooth/ftsmooth.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************/
 
2
/*                                                                         */
 
3
/*  ftsmooth.c                                                             */
 
4
/*                                                                         */
 
5
/*    Anti-aliasing renderer interface (body).                             */
 
6
/*                                                                         */
 
7
/*  Copyright 2000-2001, 2002, 2003, 2004 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 <ft2build.h>
 
20
#include FT_INTERNAL_OBJECTS_H
 
21
#include FT_OUTLINE_H
 
22
#include "ftsmooth.h"
 
23
#include "ftgrays.h"
 
24
 
 
25
#include "ftsmerrs.h"
 
26
 
 
27
 
 
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 );
 
33
 
 
34
 
 
35
    render->clazz->raster_class->raster_reset( render->raster,
 
36
                                               library->raster_pool,
 
37
                                               library->raster_pool_size );
 
38
 
 
39
    return 0;
 
40
  }
 
41
 
 
42
 
 
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
  }
 
54
 
 
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;
 
63
 
 
64
 
 
65
    if ( slot->format != render->glyph_format )
 
66
    {
 
67
      error = Smooth_Err_Invalid_Argument;
 
68
      goto Exit;
 
69
    }
 
70
 
 
71
    if ( matrix )
 
72
      FT_Outline_Transform( &slot->outline, matrix );
 
73
 
 
74
    if ( delta )
 
75
      FT_Outline_Translate( &slot->outline, delta->x, delta->y );
 
76
 
 
77
  Exit:
 
78
    return error;
 
79
  }
 
80
 
 
81
 
 
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
    FT_MEM_ZERO( cbox, sizeof ( *cbox ) );
 
89
 
 
90
    if ( slot->format == render->glyph_format )
 
91
      FT_Outline_Get_CBox( &slot->outline, cbox );
 
92
  }
 
93
 
 
94
 
 
95
  /* convert a slot's glyph image into a bitmap */
 
96
  static FT_Error
 
97
  ft_smooth_render_generic( FT_Renderer     render,
 
98
                            FT_GlyphSlot    slot,
 
99
                            FT_Render_Mode  mode,
 
100
                            FT_Vector*      origin,
 
101
                            FT_Render_Mode  required_mode,
 
102
                            FT_Int          hmul,
 
103
                            FT_Int          vmul )
 
104
  {
 
105
    FT_Error     error;
 
106
    FT_Outline*  outline = NULL;
 
107
    FT_BBox      cbox;
 
108
    FT_UInt      width, height, pitch;
 
109
    FT_Bitmap*   bitmap;
 
110
    FT_Memory    memory;
 
111
 
 
112
    FT_Raster_Params  params;
 
113
 
 
114
 
 
115
    /* check glyph image format */
 
116
    if ( slot->format != render->glyph_format )
 
117
    {
 
118
      error = Smooth_Err_Invalid_Argument;
 
119
      goto Exit;
 
120
    }
 
121
 
 
122
    /* check mode */
 
123
    if ( mode != required_mode )
 
124
      return Smooth_Err_Cannot_Render_Glyph;
 
125
 
 
126
    outline = &slot->outline;
 
127
 
 
128
    /* translate the outline to the new origin if needed */
 
129
    if ( origin )
 
130
      FT_Outline_Translate( outline, origin->x, origin->y );
 
131
 
 
132
    /* compute the control box, and grid fit it */
 
133
    FT_Outline_Get_CBox( outline, &cbox );
 
134
 
 
135
    cbox.xMin = FT_PIX_FLOOR( cbox.xMin );
 
136
    cbox.yMin = FT_PIX_FLOOR( cbox.yMin );
 
137
    cbox.xMax = FT_PIX_CEIL( cbox.xMax );
 
138
    cbox.yMax = FT_PIX_CEIL( cbox.yMax );
 
139
 
 
140
    width  = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 );
 
141
    height = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 );
 
142
    bitmap = &slot->bitmap;
 
143
    memory = render->root.memory;
 
144
 
 
145
    /* release old bitmap buffer */
 
146
    if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
 
147
    {
 
148
      FT_FREE( bitmap->buffer );
 
149
      slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
 
150
    }
 
151
 
 
152
    /* allocate new one, depends on pixel format */
 
153
    pitch = width;
 
154
    if ( hmul )
 
155
    {
 
156
      width = width * hmul;
 
157
      pitch = FT_PAD_CEIL( width, 4 );
 
158
    }
 
159
 
 
160
    if ( vmul )
 
161
      height *= vmul;
 
162
 
 
163
    bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
 
164
    bitmap->num_grays  = 256;
 
165
    bitmap->width      = width;
 
166
    bitmap->rows       = height;
 
167
    bitmap->pitch      = pitch;
 
168
 
 
169
    if ( FT_ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) )
 
170
      goto Exit;
 
171
 
 
172
    slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
 
173
 
 
174
    /* translate outline to render it into the bitmap */
 
175
    FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
 
176
 
 
177
    /* set up parameters */
 
178
    params.target = bitmap;
 
179
    params.source = outline;
 
180
    params.flags  = FT_RASTER_FLAG_AA;
 
181
 
 
182
    /* implode outline if needed */
 
183
    {
 
184
      FT_Int      n;
 
185
      FT_Vector*  vec;
 
186
 
 
187
 
 
188
      if ( hmul )
 
189
        for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
 
190
          vec->x *= hmul;
 
191
 
 
192
      if ( vmul )
 
193
        for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
 
194
          vec->y *= vmul;
 
195
    }
 
196
 
 
197
    /* render outline into the bitmap */
 
198
    error = render->raster_render( render->raster, &params );
 
199
 
 
200
    /* deflate outline if needed */
 
201
    {
 
202
      FT_Int      n;
 
203
      FT_Vector*  vec;
 
204
 
 
205
 
 
206
      if ( hmul )
 
207
        for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
 
208
          vec->x /= hmul;
 
209
 
 
210
      if ( vmul )
 
211
        for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
 
212
          vec->y /= vmul;
 
213
    }
 
214
 
 
215
    FT_Outline_Translate( outline, cbox.xMin, cbox.yMin );
 
216
 
 
217
    if ( error )
 
218
      goto Exit;
 
219
 
 
220
    slot->format      = FT_GLYPH_FORMAT_BITMAP;
 
221
    slot->bitmap_left = (FT_Int)( cbox.xMin >> 6 );
 
222
    slot->bitmap_top  = (FT_Int)( cbox.yMax >> 6 );
 
223
 
 
224
  Exit:
 
225
    if ( outline && origin )
 
226
      FT_Outline_Translate( outline, -origin->x, -origin->y );
 
227
 
 
228
    return error;
 
229
  }
 
230
 
 
231
 
 
232
  /* convert a slot's glyph image into a bitmap */
 
233
  static FT_Error
 
234
  ft_smooth_render( FT_Renderer     render,
 
235
                    FT_GlyphSlot    slot,
 
236
                    FT_Render_Mode  mode,
 
237
                    FT_Vector*      origin )
 
238
  {
 
239
    if ( mode == FT_RENDER_MODE_LIGHT )
 
240
      mode = FT_RENDER_MODE_NORMAL;
 
241
 
 
242
    return ft_smooth_render_generic( render, slot, mode, origin,
 
243
                                     FT_RENDER_MODE_NORMAL,
 
244
                                     0, 0 );
 
245
  }
 
246
 
 
247
 
 
248
  /* convert a slot's glyph image into a horizontal LCD bitmap */
 
249
  static FT_Error
 
250
  ft_smooth_render_lcd( FT_Renderer     render,
 
251
                        FT_GlyphSlot    slot,
 
252
                        FT_Render_Mode  mode,
 
253
                        FT_Vector*      origin )
 
254
  {
 
255
    FT_Error  error;
 
256
 
 
257
    error = ft_smooth_render_generic( render, slot, mode, origin,
 
258
                                      FT_RENDER_MODE_LCD,
 
259
                                      3, 0 );
 
260
    if ( !error )
 
261
      slot->bitmap.pixel_mode = FT_PIXEL_MODE_LCD;
 
262
 
 
263
    return error;
 
264
  }
 
265
 
 
266
 
 
267
  /* convert a slot's glyph image into a vertical LCD bitmap */
 
268
  static FT_Error
 
269
  ft_smooth_render_lcd_v( FT_Renderer     render,
 
270
                          FT_GlyphSlot    slot,
 
271
                          FT_Render_Mode  mode,
 
272
                          FT_Vector*      origin )
 
273
  {
 
274
    FT_Error  error;
 
275
 
 
276
    error = ft_smooth_render_generic( render, slot, mode, origin,
 
277
                                      FT_RENDER_MODE_LCD_V,
 
278
                                      0, 3 );
 
279
    if ( !error )
 
280
      slot->bitmap.pixel_mode = FT_PIXEL_MODE_LCD_V;
 
281
 
 
282
    return error;
 
283
  }
 
284
 
 
285
 
 
286
  FT_CALLBACK_TABLE_DEF
 
287
  const FT_Renderer_Class  ft_smooth_renderer_class =
 
288
  {
 
289
    {
 
290
      FT_MODULE_RENDERER,
 
291
      sizeof( FT_RendererRec ),
 
292
 
 
293
      "smooth",
 
294
      0x10000L,
 
295
      0x20000L,
 
296
 
 
297
      0,    /* module specific interface */
 
298
 
 
299
      (FT_Module_Constructor)ft_smooth_init,
 
300
      (FT_Module_Destructor) 0,
 
301
      (FT_Module_Requester)  0
 
302
    },
 
303
 
 
304
    FT_GLYPH_FORMAT_OUTLINE,
 
305
 
 
306
    (FT_Renderer_RenderFunc)   ft_smooth_render,
 
307
    (FT_Renderer_TransformFunc)ft_smooth_transform,
 
308
    (FT_Renderer_GetCBoxFunc)  ft_smooth_get_cbox,
 
309
    (FT_Renderer_SetModeFunc)  ft_smooth_set_mode,
 
310
 
 
311
    (FT_Raster_Funcs*)    &ft_grays_raster
 
312
  };
 
313
 
 
314
 
 
315
  FT_CALLBACK_TABLE_DEF
 
316
  const FT_Renderer_Class  ft_smooth_lcd_renderer_class =
 
317
  {
 
318
    {
 
319
      FT_MODULE_RENDERER,
 
320
      sizeof( FT_RendererRec ),
 
321
 
 
322
      "smooth-lcd",
 
323
      0x10000L,
 
324
      0x20000L,
 
325
 
 
326
      0,    /* module specific interface */
 
327
 
 
328
      (FT_Module_Constructor)ft_smooth_init,
 
329
      (FT_Module_Destructor) 0,
 
330
      (FT_Module_Requester)  0
 
331
    },
 
332
 
 
333
    FT_GLYPH_FORMAT_OUTLINE,
 
334
 
 
335
    (FT_Renderer_RenderFunc)   ft_smooth_render_lcd,
 
336
    (FT_Renderer_TransformFunc)ft_smooth_transform,
 
337
    (FT_Renderer_GetCBoxFunc)  ft_smooth_get_cbox,
 
338
    (FT_Renderer_SetModeFunc)  ft_smooth_set_mode,
 
339
 
 
340
    (FT_Raster_Funcs*)    &ft_grays_raster
 
341
  };
 
342
 
 
343
 
 
344
 
 
345
  FT_CALLBACK_TABLE_DEF
 
346
  const FT_Renderer_Class  ft_smooth_lcdv_renderer_class =
 
347
  {
 
348
    {
 
349
      FT_MODULE_RENDERER,
 
350
      sizeof( FT_RendererRec ),
 
351
 
 
352
      "smooth-lcdv",
 
353
      0x10000L,
 
354
      0x20000L,
 
355
 
 
356
      0,    /* module specific interface */
 
357
 
 
358
      (FT_Module_Constructor)ft_smooth_init,
 
359
      (FT_Module_Destructor) 0,
 
360
      (FT_Module_Requester)  0
 
361
    },
 
362
 
 
363
    FT_GLYPH_FORMAT_OUTLINE,
 
364
 
 
365
    (FT_Renderer_RenderFunc)   ft_smooth_render_lcd_v,
 
366
    (FT_Renderer_TransformFunc)ft_smooth_transform,
 
367
    (FT_Renderer_GetCBoxFunc)  ft_smooth_get_cbox,
 
368
    (FT_Renderer_SetModeFunc)  ft_smooth_set_mode,
 
369
 
 
370
    (FT_Raster_Funcs*)    &ft_grays_raster
 
371
  };
 
372
 
 
373
 
 
374
/* END */