ftrend1.c

Go to the documentation of this file.
00001 /***************************************************************************/
00002 /*                                                                         */
00003 /*  ftrend1.c                                                              */
00004 /*                                                                         */
00005 /*    The FreeType glyph rasterizer interface (body).                      */
00006 /*                                                                         */
00007 /*  Copyright 1996-2001, 2002, 2003, 2005, 2006 by                         */
00008 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
00009 /*                                                                         */
00010 /*  This file is part of the FreeType project, and may only be used,       */
00011 /*  modified, and distributed under the terms of the FreeType project      */
00012 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
00013 /*  this file you indicate that you have read the license and              */
00014 /*  understand and accept it fully.                                        */
00015 /*                                                                         */
00016 /***************************************************************************/
00017 
00018 
00019 #include <ft2build.h>
00020 #include FT_INTERNAL_OBJECTS_H
00021 #include FT_OUTLINE_H
00022 #include "ftrend1.h"
00023 #include "ftraster.h"
00024 #include "rastpic.h"
00025 
00026 #include "rasterrs.h"
00027 
00028 
00029   /* initialize renderer -- init its raster */
00030   static FT_Error
00031   ft_raster1_init( FT_Renderer  render )
00032   {
00033     FT_Library  library = FT_MODULE_LIBRARY( render );
00034 
00035 
00036     render->clazz->raster_class->raster_reset( render->raster,
00037                                                library->raster_pool,
00038                                                library->raster_pool_size );
00039 
00040     return Raster_Err_Ok;
00041   }
00042 
00043 
00044   /* set render-specific mode */
00045   static FT_Error
00046   ft_raster1_set_mode( FT_Renderer  render,
00047                        FT_ULong     mode_tag,
00048                        FT_Pointer   data )
00049   {
00050     /* we simply pass it to the raster */
00051     return render->clazz->raster_class->raster_set_mode( render->raster,
00052                                                          mode_tag,
00053                                                          data );
00054   }
00055 
00056 
00057   /* transform a given glyph image */
00058   static FT_Error
00059   ft_raster1_transform( FT_Renderer       render,
00060                         FT_GlyphSlot      slot,
00061                         const FT_Matrix*  matrix,
00062                         const FT_Vector*  delta )
00063   {
00064     FT_Error error = Raster_Err_Ok;
00065 
00066 
00067     if ( slot->format != render->glyph_format )
00068     {
00069       error = Raster_Err_Invalid_Argument;
00070       goto Exit;
00071     }
00072 
00073     if ( matrix )
00074       FT_Outline_Transform( &slot->outline, matrix );
00075 
00076     if ( delta )
00077       FT_Outline_Translate( &slot->outline, delta->x, delta->y );
00078 
00079   Exit:
00080     return error;
00081   }
00082 
00083 
00084   /* return the glyph's control box */
00085   static void
00086   ft_raster1_get_cbox( FT_Renderer   render,
00087                        FT_GlyphSlot  slot,
00088                        FT_BBox*      cbox )
00089   {
00090     FT_MEM_ZERO( cbox, sizeof ( *cbox ) );
00091 
00092     if ( slot->format == render->glyph_format )
00093       FT_Outline_Get_CBox( &slot->outline, cbox );
00094   }
00095 
00096 
00097   /* convert a slot's glyph image into a bitmap */
00098   static FT_Error
00099   ft_raster1_render( FT_Renderer       render,
00100                      FT_GlyphSlot      slot,
00101                      FT_Render_Mode    mode,
00102                      const FT_Vector*  origin )
00103   {
00104     FT_Error     error;
00105     FT_Outline*  outline;
00106     FT_BBox      cbox;
00107     FT_UInt      width, height, pitch;
00108     FT_Bitmap*   bitmap;
00109     FT_Memory    memory;
00110 
00111     FT_Raster_Params  params;
00112 
00113 
00114     /* check glyph image format */
00115     if ( slot->format != render->glyph_format )
00116     {
00117       error = Raster_Err_Invalid_Argument;
00118       goto Exit;
00119     }
00120 
00121     /* check rendering mode */
00122 #ifndef FT_CONFIG_OPTION_PIC
00123     if ( mode != FT_RENDER_MODE_MONO )
00124     {
00125       /* raster1 is only capable of producing monochrome bitmaps */
00126       if ( render->clazz == &ft_raster1_renderer_class )
00127         return Raster_Err_Cannot_Render_Glyph;
00128     }
00129     else
00130     {
00131       /* raster5 is only capable of producing 5-gray-levels bitmaps */
00132       if ( render->clazz == &ft_raster5_renderer_class )
00133         return Raster_Err_Cannot_Render_Glyph;
00134     }
00135 #else /* FT_CONFIG_OPTION_PIC */
00136     /* When PIC is enabled, we cannot get to the class object      */
00137     /* so instead we check the final character in the class name   */
00138     /* ("raster5" or "raster1"). Yes this is a hack.               */
00139     /* The "correct" thing to do is have different render function */
00140     /* for each of the classes.                                    */
00141     if ( mode != FT_RENDER_MODE_MONO )
00142     {
00143       /* raster1 is only capable of producing monochrome bitmaps */
00144       if ( render->clazz->root.module_name[6] == '1' )
00145         return Raster_Err_Cannot_Render_Glyph;
00146     }
00147     else
00148     {
00149       /* raster5 is only capable of producing 5-gray-levels bitmaps */
00150       if ( render->clazz->root.module_name[6] == '5' )
00151         return Raster_Err_Cannot_Render_Glyph;
00152     }
00153 #endif /* FT_CONFIG_OPTION_PIC */
00154 
00155     outline = &slot->outline;
00156 
00157     /* translate the outline to the new origin if needed */
00158     if ( origin )
00159       FT_Outline_Translate( outline, origin->x, origin->y );
00160 
00161     /* compute the control box, and grid fit it */
00162     FT_Outline_Get_CBox( outline, &cbox );
00163 
00164     cbox.xMin = FT_PIX_FLOOR( cbox.xMin );
00165     cbox.yMin = FT_PIX_FLOOR( cbox.yMin );
00166     cbox.xMax = FT_PIX_CEIL( cbox.xMax );
00167     cbox.yMax = FT_PIX_CEIL( cbox.yMax );
00168 
00169     width  = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 );
00170     height = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 );
00171     bitmap = &slot->bitmap;
00172     memory = render->root.memory;
00173 
00174     /* release old bitmap buffer */
00175     if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
00176     {
00177       FT_FREE( bitmap->buffer );
00178       slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
00179     }
00180 
00181     /* allocate new one, depends on pixel format */
00182     if ( !( mode & FT_RENDER_MODE_MONO ) )
00183     {
00184       /* we pad to 32 bits, only for backwards compatibility with FT 1.x */
00185       pitch              = FT_PAD_CEIL( width, 4 );
00186       bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
00187       bitmap->num_grays  = 256;
00188     }
00189     else
00190     {
00191       pitch              = ( ( width + 15 ) >> 4 ) << 1;
00192       bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
00193     }
00194 
00195     bitmap->width = width;
00196     bitmap->rows  = height;
00197     bitmap->pitch = pitch;
00198 
00199     if ( FT_ALLOC_MULT( bitmap->buffer, pitch, height ) )
00200       goto Exit;
00201 
00202     slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
00203 
00204     /* translate outline to render it into the bitmap */
00205     FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
00206 
00207     /* set up parameters */
00208     params.target = bitmap;
00209     params.source = outline;
00210     params.flags  = 0;
00211 
00212     if ( bitmap->pixel_mode == FT_PIXEL_MODE_GRAY )
00213       params.flags |= FT_RASTER_FLAG_AA;
00214 
00215     /* render outline into the bitmap */
00216     error = render->raster_render( render->raster, &params );
00217 
00218     FT_Outline_Translate( outline, cbox.xMin, cbox.yMin );
00219 
00220     if ( error )
00221       goto Exit;
00222 
00223     slot->format      = FT_GLYPH_FORMAT_BITMAP;
00224     slot->bitmap_left = (FT_Int)( cbox.xMin >> 6 );
00225     slot->bitmap_top  = (FT_Int)( cbox.yMax >> 6 );
00226 
00227   Exit:
00228     return error;
00229   }
00230 
00231 
00232   FT_DEFINE_RENDERER(ft_raster1_renderer_class,
00233     
00234       FT_MODULE_RENDERER,
00235       sizeof( FT_RendererRec ),
00236 
00237       "raster1",
00238       0x10000L,
00239       0x20000L,
00240 
00241       0,    /* module specific interface */
00242 
00243       (FT_Module_Constructor)ft_raster1_init,
00244       (FT_Module_Destructor) 0,
00245       (FT_Module_Requester)  0
00246     ,
00247 
00248     FT_GLYPH_FORMAT_OUTLINE,
00249 
00250     (FT_Renderer_RenderFunc)   ft_raster1_render,
00251     (FT_Renderer_TransformFunc)ft_raster1_transform,
00252     (FT_Renderer_GetCBoxFunc)  ft_raster1_get_cbox,
00253     (FT_Renderer_SetModeFunc)  ft_raster1_set_mode,
00254 
00255     (FT_Raster_Funcs*)    &FT_STANDARD_RASTER_GET
00256   )
00257 
00258 
00259   /* This renderer is _NOT_ part of the default modules; you will need */
00260   /* to register it by hand in your application.  It should only be    */
00261   /* used for backwards-compatibility with FT 1.x anyway.              */
00262   /*                                                                   */
00263   FT_DEFINE_RENDERER(ft_raster5_renderer_class,
00264   
00265     
00266       FT_MODULE_RENDERER,
00267       sizeof( FT_RendererRec ),
00268 
00269       "raster5",
00270       0x10000L,
00271       0x20000L,
00272 
00273       0,    /* module specific interface */
00274 
00275       (FT_Module_Constructor)ft_raster1_init,
00276       (FT_Module_Destructor) 0,
00277       (FT_Module_Requester)  0
00278     ,
00279 
00280     FT_GLYPH_FORMAT_OUTLINE,
00281 
00282     (FT_Renderer_RenderFunc)   ft_raster1_render,
00283     (FT_Renderer_TransformFunc)ft_raster1_transform,
00284     (FT_Renderer_GetCBoxFunc)  ft_raster1_get_cbox,
00285     (FT_Renderer_SetModeFunc)  ft_raster1_set_mode,
00286 
00287     (FT_Raster_Funcs*)    &FT_STANDARD_RASTER_GET
00288   )
00289 
00290 
00291 /* END */

Generated on Tue Jul 5 14:13:50 2011 for ROOT_528-00b_version by  doxygen 1.5.1