zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ftsmooth.c
Go to the documentation of this file.
1 /***************************************************************************/
2 /* */
3 /* ftsmooth.c */
4 /* */
5 /* Anti-aliasing renderer interface (body). */
6 /* */
7 /* Copyright 2000-2006, 2009-2012 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_DEBUG_H
21 #include FT_INTERNAL_OBJECTS_H
22 #include FT_OUTLINE_H
23 #include "ftsmooth.h"
24 #include "ftgrays.h"
25 #include "ftspic.h"
26 
27 #include "ftsmerrs.h"
28 
29 
30  /* initialize renderer -- init its raster */
31  static FT_Error
33  {
35 
36 
37  render->clazz->raster_class->raster_reset( render->raster,
38  library->raster_pool,
39  library->raster_pool_size );
40 
41  return 0;
42  }
43 
44 
45  /* sets render-specific mode */
46  static FT_Error
48  FT_ULong mode_tag,
50  {
51  /* we simply pass it to the raster */
52  return render->clazz->raster_class->raster_set_mode( render->raster,
53  mode_tag,
54  data );
55  }
56 
57  /* transform a given glyph image */
58  static FT_Error
60  FT_GlyphSlot slot,
61  const FT_Matrix* matrix,
62  const FT_Vector* delta )
63  {
64  FT_Error error = Smooth_Err_Ok;
65 
66 
67  if ( slot->format != render->glyph_format )
68  {
69  error = Smooth_Err_Invalid_Argument;
70  goto Exit;
71  }
72 
73  if ( matrix )
74  FT_Outline_Transform( &slot->outline, matrix );
75 
76  if ( delta )
77  FT_Outline_Translate( &slot->outline, delta->x, delta->y );
78 
79  Exit:
80  return error;
81  }
82 
83 
84  /* return the glyph's control box */
85  static void
87  FT_GlyphSlot slot,
88  FT_BBox* cbox )
89  {
90  FT_MEM_ZERO( cbox, sizeof ( *cbox ) );
91 
92  if ( slot->format == render->glyph_format )
93  FT_Outline_Get_CBox( &slot->outline, cbox );
94  }
95 
96 
97  /* convert a slot's glyph image into a bitmap */
98  static FT_Error
100  FT_GlyphSlot slot,
102  const FT_Vector* origin,
103  FT_Render_Mode required_mode )
104  {
105  FT_Error error;
106  FT_Outline* outline = NULL;
107  FT_BBox cbox;
108  FT_Pos width, height, pitch;
109 #ifndef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
110  FT_Pos height_org, width_org;
111 #endif
112  FT_Bitmap* bitmap;
113  FT_Memory memory;
114  FT_Int hmul = mode == FT_RENDER_MODE_LCD;
115  FT_Int vmul = mode == FT_RENDER_MODE_LCD_V;
116  FT_Pos x_shift, y_shift, x_left, y_top;
117 
119 
120 
121  /* check glyph image format */
122  if ( slot->format != render->glyph_format )
123  {
124  error = Smooth_Err_Invalid_Argument;
125  goto Exit;
126  }
127 
128  /* check mode */
129  if ( mode != required_mode )
130  return Smooth_Err_Cannot_Render_Glyph;
131 
132  outline = &slot->outline;
133 
134  /* translate the outline to the new origin if needed */
135  if ( origin )
136  FT_Outline_Translate( outline, origin->x, origin->y );
137 
138  /* compute the control box, and grid fit it */
139  FT_Outline_Get_CBox( outline, &cbox );
140 
141  cbox.xMin = FT_PIX_FLOOR( cbox.xMin );
142  cbox.yMin = FT_PIX_FLOOR( cbox.yMin );
143  cbox.xMax = FT_PIX_CEIL( cbox.xMax );
144  cbox.yMax = FT_PIX_CEIL( cbox.yMax );
145 
146  if ( cbox.xMin < 0 && cbox.xMax > FT_INT_MAX + cbox.xMin )
147  {
148  FT_ERROR(( "ft_smooth_render_generic: glyph too large:"
149  " xMin = %d, xMax = %d\n",
150  cbox.xMin >> 6, cbox.xMax >> 6 ));
151  return Smooth_Err_Raster_Overflow;
152  }
153  else
154  width = ( cbox.xMax - cbox.xMin ) >> 6;
155 
156  if ( cbox.yMin < 0 && cbox.yMax > FT_INT_MAX + cbox.yMin )
157  {
158  FT_ERROR(( "ft_smooth_render_generic: glyph too large:"
159  " yMin = %d, yMax = %d\n",
160  cbox.yMin >> 6, cbox.yMax >> 6 ));
161  return Smooth_Err_Raster_Overflow;
162  }
163  else
164  height = ( cbox.yMax - cbox.yMin ) >> 6;
165 
166  bitmap = &slot->bitmap;
167  memory = render->root.memory;
168 
169 #ifndef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
170  width_org = width;
171  height_org = height;
172 #endif
173 
174  /* release old bitmap buffer */
175  if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
176  {
177  FT_FREE( bitmap->buffer );
179  }
180 
181  /* allocate new one */
182  pitch = width;
183  if ( hmul )
184  {
185  width = width * 3;
186  pitch = FT_PAD_CEIL( width, 4 );
187  }
188 
189  if ( vmul )
190  height *= 3;
191 
192  x_shift = (FT_Int) cbox.xMin;
193  y_shift = (FT_Int) cbox.yMin;
194  x_left = (FT_Int)( cbox.xMin >> 6 );
195  y_top = (FT_Int)( cbox.yMax >> 6 );
196 
197 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
198 
199  if ( slot->library->lcd_filter_func )
200  {
201  FT_Int extra = slot->library->lcd_extra;
202 
203 
204  if ( hmul )
205  {
206  x_shift -= 64 * ( extra >> 1 );
207  width += 3 * extra;
208  pitch = FT_PAD_CEIL( width, 4 );
209  x_left -= extra >> 1;
210  }
211 
212  if ( vmul )
213  {
214  y_shift -= 64 * ( extra >> 1 );
215  height += 3 * extra;
216  y_top += extra >> 1;
217  }
218  }
219 
220 #endif
221 
222 #if FT_UINT_MAX > 0xFFFFU
223 
224  /* Required check is ( pitch * height < FT_ULONG_MAX ), */
225  /* but we care realistic cases only. Always pitch <= width. */
226  if ( width > 0x7FFF || height > 0x7FFF )
227  {
228  FT_ERROR(( "ft_smooth_render_generic: glyph too large: %u x %u\n",
229  width, height ));
230  return Smooth_Err_Raster_Overflow;
231  }
232 
233 #endif
234 
235  bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
236  bitmap->num_grays = 256;
237  bitmap->width = width;
238  bitmap->rows = height;
239  bitmap->pitch = pitch;
240 
241  /* translate outline to render it into the bitmap */
242  FT_Outline_Translate( outline, -x_shift, -y_shift );
243 
244  if ( FT_ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) )
245  goto Exit;
246 
248 
249  /* set up parameters */
250  params.target = bitmap;
251  params.source = outline;
252  params.flags = FT_RASTER_FLAG_AA;
253 
254 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
255 
256  /* implode outline if needed */
257  {
258  FT_Vector* points = outline->points;
259  FT_Vector* points_end = points + outline->n_points;
260  FT_Vector* vec;
261 
262 
263  if ( hmul )
264  for ( vec = points; vec < points_end; vec++ )
265  vec->x *= 3;
266 
267  if ( vmul )
268  for ( vec = points; vec < points_end; vec++ )
269  vec->y *= 3;
270  }
271 
272  /* render outline into the bitmap */
273  error = render->raster_render( render->raster, &params );
274 
275  /* deflate outline if needed */
276  {
277  FT_Vector* points = outline->points;
278  FT_Vector* points_end = points + outline->n_points;
279  FT_Vector* vec;
280 
281 
282  if ( hmul )
283  for ( vec = points; vec < points_end; vec++ )
284  vec->x /= 3;
285 
286  if ( vmul )
287  for ( vec = points; vec < points_end; vec++ )
288  vec->y /= 3;
289  }
290 
291  if ( slot->library->lcd_filter_func )
292  slot->library->lcd_filter_func( bitmap, mode, slot->library );
293 
294 #else /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
295 
296  /* render outline into bitmap */
297  error = render->raster_render( render->raster, &params );
298 
299  /* expand it horizontally */
300  if ( hmul )
301  {
302  FT_Byte* line = bitmap->buffer;
303  FT_UInt hh;
304 
305 
306  for ( hh = height_org; hh > 0; hh--, line += pitch )
307  {
308  FT_UInt xx;
309  FT_Byte* end = line + width;
310 
311 
312  for ( xx = width_org; xx > 0; xx-- )
313  {
314  FT_UInt pixel = line[xx-1];
315 
316 
317  end[-3] = (FT_Byte)pixel;
318  end[-2] = (FT_Byte)pixel;
319  end[-1] = (FT_Byte)pixel;
320  end -= 3;
321  }
322  }
323  }
324 
325  /* expand it vertically */
326  if ( vmul )
327  {
328  FT_Byte* read = bitmap->buffer + ( height - height_org ) * pitch;
329  FT_Byte* write = bitmap->buffer;
330  FT_UInt hh;
331 
332 
333  for ( hh = height_org; hh > 0; hh-- )
334  {
335  ft_memcpy( write, read, pitch );
336  write += pitch;
337 
338  ft_memcpy( write, read, pitch );
339  write += pitch;
340 
341  ft_memcpy( write, read, pitch );
342  write += pitch;
343  read += pitch;
344  }
345  }
346 
347 #endif /* !FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
348 
349  FT_Outline_Translate( outline, x_shift, y_shift );
350 
351  /*
352  * XXX: on 16bit system, we return an error for huge bitmap
353  * to prevent an overflow.
354  */
355  if ( x_left > FT_INT_MAX || y_top > FT_INT_MAX )
356  return Smooth_Err_Invalid_Pixel_Size;
357 
358  if ( error )
359  goto Exit;
360 
361  slot->format = FT_GLYPH_FORMAT_BITMAP;
362  slot->bitmap_left = (FT_Int)x_left;
363  slot->bitmap_top = (FT_Int)y_top;
364 
365  Exit:
366  if ( outline && origin )
367  FT_Outline_Translate( outline, -origin->x, -origin->y );
368 
369  return error;
370  }
371 
372 
373  /* convert a slot's glyph image into a bitmap */
374  static FT_Error
376  FT_GlyphSlot slot,
378  const FT_Vector* origin )
379  {
380  if ( mode == FT_RENDER_MODE_LIGHT )
381  mode = FT_RENDER_MODE_NORMAL;
382 
383  return ft_smooth_render_generic( render, slot, mode, origin,
385  }
386 
387 
388  /* convert a slot's glyph image into a horizontal LCD bitmap */
389  static FT_Error
391  FT_GlyphSlot slot,
393  const FT_Vector* origin )
394  {
395  FT_Error error;
396 
397  error = ft_smooth_render_generic( render, slot, mode, origin,
399  if ( !error )
401 
402  return error;
403  }
404 
405 
406  /* convert a slot's glyph image into a vertical LCD bitmap */
407  static FT_Error
409  FT_GlyphSlot slot,
411  const FT_Vector* origin )
412  {
413  FT_Error error;
414 
415  error = ft_smooth_render_generic( render, slot, mode, origin,
417  if ( !error )
419 
420  return error;
421  }
422 
423 
424  FT_DEFINE_RENDERER( ft_smooth_renderer_class,
425 
427  sizeof ( FT_RendererRec ),
428 
429  "smooth",
430  0x10000L,
431  0x20000L,
432 
433  0, /* module specific interface */
434 
438  ,
439 
441 
446 
448  )
449 
450 
451  FT_DEFINE_RENDERER( ft_smooth_lcd_renderer_class,
452 
455 
456  "smooth-lcd",
457  0x10000L,
458  0x20000L,
459 
460  0, /* module specific interface */
461 
462  (FT_Module_Constructor)ft_smooth_init,
465  ,
466 
468 
470  (FT_Renderer_TransformFunc)ft_smooth_transform,
471  (FT_Renderer_GetCBoxFunc) ft_smooth_get_cbox,
472  (FT_Renderer_SetModeFunc) ft_smooth_set_mode,
473 
475  )
476 
477  FT_DEFINE_RENDERER( ft_smooth_lcdv_renderer_class,
478 
480  sizeof ( FT_RendererRec ),
481 
482  "smooth-lcdv",
483  0x10000L,
484  0x20000L,
485 
486  0, /* module specific interface */
487 
488  (FT_Module_Constructor)ft_smooth_init,
489  (FT_Module_Destructor) 0,
490  (FT_Module_Requester) 0
491  ,
492 
493  FT_GLYPH_FORMAT_OUTLINE,
494 
496  (FT_Renderer_TransformFunc)ft_smooth_transform,
497  (FT_Renderer_GetCBoxFunc) ft_smooth_get_cbox,
498  (FT_Renderer_SetModeFunc) ft_smooth_set_mode,
499 
501  )
502 
503 
504 /* END */
#define FT_ALLOC(ptr, size)
Definition: ftmemory.h:260
#define FT_PIX_CEIL(x)
Definition: ftobjs.h:82
int FT_Error
Definition: fttypes.h:296
FT_ULong raster_pool_size
Definition: ftobjs.h:841
FT_Raster_Render_Func raster_render
Definition: ftobjs.h:666
smooth FT_Module_Constructor FT_Renderer_RenderFunc FT_Renderer_TransformFunc ft_smooth_transform
Definition: ftsmooth.c:456
unsigned long FT_ULong
Definition: fttypes.h:249
FT_BEGIN_HEADER typedef signed long FT_Pos
Definition: ftimage.h:59
#define FT_MEM_ZERO(dest, count)
Definition: ftmemory.h:208
#define NULL
Definition: ftobjs.h:61
signed int FT_Int
Definition: fttypes.h:216
int rows
Definition: ftimage.h:312
enum FT_Render_Mode_ FT_Render_Mode
FT_Module_Interface(* FT_Module_Requester)(FT_Module module, const char *name)
Definition: ftmodapi.h:126
unsigned char * buffer
Definition: ftimage.h:315
const FT_Bitmap * target
Definition: ftimage.h:1106
static void render(const Vertex_Buffer_Macrorenderer &macrorenderer, std::vector< Vertex_Buffer::Vertex_Buffer_Range * > &descriptors)
#define FT_RASTER_FLAG_AA
Definition: ftimage.h:1043
EGLSurface EGLint EGLint EGLint EGLint height
Definition: eglext.h:293
smooth lcd
Definition: ftsmooth.c:456
FT_Int bitmap_top
Definition: freetype.h:1622
FT_Library library
Definition: cffdrivr.c:409
int pitch
Definition: ftimage.h:314
FT_Library library
Definition: freetype.h:1607
smooth FT_Module_Constructor FT_Renderer_RenderFunc FT_Renderer_TransformFunc FT_Renderer_GetCBoxFunc ft_smooth_get_cbox
Definition: ftsmooth.c:456
FT_Raster raster
Definition: ftobjs.h:665
#define FT_GLYPH_OWN_BITMAP
Definition: ftobjs.h:376
static FT_Error ft_smooth_render(FT_Renderer render, FT_GlyphSlot slot, FT_Render_Mode mode, const FT_Vector *origin)
Definition: ftsmooth.c:375
if(!yyg->yy_init)
static FT_Error ft_smooth_render_generic(FT_Renderer render, FT_GlyphSlot slot, FT_Render_Mode mode, const FT_Vector *origin, FT_Render_Mode required_mode)
Definition: ftsmooth.c:99
FT_Outline outline
Definition: freetype.h:1624
#define FT_ERROR(varformat)
Definition: ftdebug.h:181
#define FT_PAD_CEIL(x, n)
Definition: ftobjs.h:78
unsigned char FT_Byte
Definition: fttypes.h:150
GLenum GLvoid ** params
Definition: gl2ext.h:806
FT_ModuleRec root
Definition: ftobjs.h:660
FT_Outline_Get_CBox(const FT_Outline *outline, FT_BBox *acbox)
Definition: ftoutln.c:460
FT_Bitmap bitmap
Definition: freetype.h:1620
#define FT_PIX_FLOOR(x)
Definition: ftobjs.h:80
#define FT_MODULE_RENDERER
Definition: ftmodapi.h:56
#define FT_FREE(ptr)
Definition: ftmemory.h:286
FT_Outline_Transform(const FT_Outline *outline, const FT_Matrix *matrix)
Definition: ftoutln.c:695
FT_Raster_Funcs * raster_class
Definition: ftrender.h:155
FT_Pos yMax
Definition: ftimage.h:119
smooth FT_Module_Constructor FT_Renderer_RenderFunc ft_smooth_render_lcd
Definition: ftsmooth.c:456
FT_Pos xMin
Definition: ftimage.h:118
smooth FT_Module_Constructor ft_smooth_init
Definition: ftsmooth.c:456
smooth FT_Module_Constructor FT_Renderer_RenderFunc FT_Renderer_TransformFunc FT_Renderer_GetCBoxFunc FT_Renderer_SetModeFunc ft_smooth_set_mode
Definition: ftsmooth.c:456
FT_Raster_ResetFunc raster_reset
Definition: ftimage.h:1292
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
FT_Error error
Definition: cffdrivr.c:407
EGLSurface EGLint EGLint EGLint width
Definition: eglext.h:293
FT_Pos x
Definition: ftimage.h:77
void(* FT_Module_Destructor)(FT_Module module)
Definition: ftmodapi.h:109
void * FT_Pointer
Definition: fttypes.h:307
FT_Raster_SetModeFunc raster_set_mode
Definition: ftimage.h:1293
FT_Pos y
Definition: ftimage.h:78
#define FT_MODULE_LIBRARY(x)
Definition: ftobjs.h:451
short num_grays
Definition: ftimage.h:316
FT_Vector * vec
Definition: ftbbox.c:579
FT_Pos xMax
Definition: ftimage.h:119
GLuint GLenum matrix
Definition: glew.h:13408
short n_points
Definition: ftimage.h:386
void(* FT_Renderer_GetCBoxFunc)(FT_Renderer renderer, FT_GlyphSlot slot, FT_BBox *cbox)
Definition: ftrender.h:101
typedefFT_BEGIN_HEADER struct FT_MemoryRec_ * FT_Memory
Definition: ftsystem.h:66
int width
Definition: ftimage.h:313
#define FT_DEFINE_RENDERER(class_,flags_, size_, name_, version_, requires_,interface_, init_, done_, get_interface_,glyph_format_, render_glyph_, transform_glyph_,get_glyph_cbox_, set_mode_, raster_class_)
Definition: ftobjs.h:1120
FT_Glyph_Format glyph_format
Definition: ftobjs.h:662
FT_Error(* FT_Renderer_SetModeFunc)(FT_Renderer renderer, FT_ULong mode_tag, FT_Pointer mode_ptr)
Definition: ftrender.h:107
static FT_Error ft_smooth_render_lcd_v(FT_Renderer render, FT_GlyphSlot slot, FT_Render_Mode mode, const FT_Vector *origin)
Definition: ftsmooth.c:408
sizeof(FT_AutofitterRec)
FT_Error(* FT_Module_Constructor)(FT_Module module)
Definition: ftmodapi.h:94
FT_Byte * raster_pool
Definition: ftobjs.h:839
FT_Int bitmap_left
Definition: freetype.h:1621
FT_Glyph_Format format
Definition: freetype.h:1618
FT_Memory memory
Definition: ftobjs.h:443
unsigned int FT_UInt
Definition: fttypes.h:227
FT_Slot_Internal internal
Definition: freetype.h:1637
FT_Error(* FT_Renderer_TransformFunc)(FT_Renderer renderer, FT_GlyphSlot slot, const FT_Matrix *matrix, const FT_Vector *delta)
Definition: ftrender.h:94
char pixel_mode
Definition: ftimage.h:317
#define FT_GRAYS_RASTER_GET
Definition: ftspic.h:28
GLuint GLuint end
Definition: glew.h:1239
FT_Outline_Translate(const FT_Outline *outline, FT_Pos xOffset, FT_Pos yOffset)
Definition: ftoutln.c:510
GLsizei GLfixed GLfixed GLfixed GLfixed const GLubyte * bitmap
Definition: glext.h:4510
GLuint GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glew.h:3337
#define FT_INT_MAX
Definition: ftstdlib.h:64
const void * source
Definition: ftimage.h:1107
FT_Renderer_Class * clazz
Definition: ftobjs.h:661
FT_Error(* FT_Renderer_RenderFunc)(FT_Renderer renderer, FT_GlyphSlot slot, FT_UInt mode, const FT_Vector *origin)
Definition: ftrender.h:88
FT_Pos yMin
Definition: ftimage.h:118
#define ft_memcpy
Definition: ftstdlib.h:81
GLenum mode
Definition: glew.h:2394
FT_Vector * points
Definition: ftimage.h:388
FT_Module_Constructor FT_GLYPH_FORMAT_OUTLINE
Definition: ftrend1.c:283