Home | History | Annotate | Download | only in raster
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  ftrend1.c                                                              */
      4 /*                                                                         */
      5 /*    The FreeType glyph rasterizer interface (body).                      */
      6 /*                                                                         */
      7 /*  Copyright 1996-2015 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 "ftrend1.h"
     24 #include "ftraster.h"
     25 #include "rastpic.h"
     26 
     27 #include "rasterrs.h"
     28 
     29 
     30   /* initialize renderer -- init its raster */
     31   static FT_Error
     32   ft_raster1_init( FT_Renderer  render )
     33   {
     34     FT_Library  library = FT_MODULE_LIBRARY( render );
     35 
     36 
     37     render->clazz->raster_class->raster_reset( render->raster,
     38                                                library->raster_pool,
     39                                                library->raster_pool_size );
     40 
     41     return FT_Err_Ok;
     42   }
     43 
     44 
     45   /* set render-specific mode */
     46   static FT_Error
     47   ft_raster1_set_mode( FT_Renderer  render,
     48                        FT_ULong     mode_tag,
     49                        FT_Pointer   data )
     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 
     58   /* transform a given glyph image */
     59   static FT_Error
     60   ft_raster1_transform( FT_Renderer       render,
     61                         FT_GlyphSlot      slot,
     62                         const FT_Matrix*  matrix,
     63                         const FT_Vector*  delta )
     64   {
     65     FT_Error error = FT_Err_Ok;
     66 
     67 
     68     if ( slot->format != render->glyph_format )
     69     {
     70       error = FT_THROW( Invalid_Argument );
     71       goto Exit;
     72     }
     73 
     74     if ( matrix )
     75       FT_Outline_Transform( &slot->outline, matrix );
     76 
     77     if ( delta )
     78       FT_Outline_Translate( &slot->outline, delta->x, delta->y );
     79 
     80   Exit:
     81     return error;
     82   }
     83 
     84 
     85   /* return the glyph's control box */
     86   static void
     87   ft_raster1_get_cbox( FT_Renderer   render,
     88                        FT_GlyphSlot  slot,
     89                        FT_BBox*      cbox )
     90   {
     91     FT_MEM_ZERO( cbox, sizeof ( *cbox ) );
     92 
     93     if ( slot->format == render->glyph_format )
     94       FT_Outline_Get_CBox( &slot->outline, cbox );
     95   }
     96 
     97 
     98   /* convert a slot's glyph image into a bitmap */
     99   static FT_Error
    100   ft_raster1_render( FT_Renderer       render,
    101                      FT_GlyphSlot      slot,
    102                      FT_Render_Mode    mode,
    103                      const FT_Vector*  origin )
    104   {
    105     FT_Error     error;
    106     FT_Outline*  outline;
    107     FT_BBox      cbox, cbox0;
    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 = FT_THROW( Invalid_Argument );
    119       goto Exit;
    120     }
    121 
    122     /* check rendering mode */
    123     if ( mode != FT_RENDER_MODE_MONO )
    124     {
    125       /* raster1 is only capable of producing monochrome bitmaps */
    126       return FT_THROW( Cannot_Render_Glyph );
    127     }
    128 
    129     outline = &slot->outline;
    130 
    131     /* translate the outline to the new origin if needed */
    132     if ( origin )
    133       FT_Outline_Translate( outline, origin->x, origin->y );
    134 
    135     /* compute the control box, and grid fit it */
    136     FT_Outline_Get_CBox( outline, &cbox0 );
    137 
    138     /* undocumented but confirmed: bbox values get rounded */
    139 #if 1
    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 );
    144 #else
    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 );
    149 #endif
    150 
    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 );
    157     if ( width == 0 )
    158     {
    159       cbox.xMin = FT_PIX_FLOOR( cbox0.xMin );
    160       cbox.xMax = FT_PIX_CEIL( cbox0.xMax );
    161 
    162       width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 );
    163     }
    164 
    165     height = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 );
    166     if ( height == 0 )
    167     {
    168       cbox.yMin = FT_PIX_FLOOR( cbox0.yMin );
    169       cbox.yMax = FT_PIX_CEIL( cbox0.yMax );
    170 
    171       height = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 );
    172     }
    173 
    174     if ( width > FT_USHORT_MAX || height > FT_USHORT_MAX )
    175     {
    176       error = FT_THROW( Invalid_Argument );
    177       goto Exit;
    178     }
    179 
    180     bitmap = &slot->bitmap;
    181     memory = render->root.memory;
    182 
    183     /* release old bitmap buffer */
    184     if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
    185     {
    186       FT_FREE( bitmap->buffer );
    187       slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
    188     }
    189 
    190     pitch              = ( ( width + 15 ) >> 4 ) << 1;
    191     bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
    192 
    193     bitmap->width = width;
    194     bitmap->rows  = height;
    195     bitmap->pitch = (int)pitch;
    196 
    197     if ( FT_ALLOC_MULT( bitmap->buffer, pitch, height ) )
    198       goto Exit;
    199 
    200     slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
    201 
    202     /* translate outline to render it into the bitmap */
    203     FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
    204 
    205     /* set up parameters */
    206     params.target = bitmap;
    207     params.source = outline;
    208     params.flags  = 0;
    209 
    210     /* render outline into the bitmap */
    211     error = render->raster_render( render->raster, &params );
    212 
    213     FT_Outline_Translate( outline, cbox.xMin, cbox.yMin );
    214 
    215     if ( error )
    216       goto Exit;
    217 
    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 );
    221 
    222   Exit:
    223     return error;
    224   }
    225 
    226 
    227   FT_DEFINE_RENDERER( ft_raster1_renderer_class,
    228 
    229       FT_MODULE_RENDERER,
    230       sizeof ( FT_RendererRec ),
    231 
    232       "raster1",
    233       0x10000L,
    234       0x20000L,
    235 
    236       0,    /* module specific interface */
    237 
    238       (FT_Module_Constructor)ft_raster1_init,
    239       (FT_Module_Destructor) 0,
    240       (FT_Module_Requester)  0
    241     ,
    242 
    243     FT_GLYPH_FORMAT_OUTLINE,
    244 
    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,
    249 
    250     (FT_Raster_Funcs*)    &FT_STANDARD_RASTER_GET
    251   )
    252 
    253 
    254 /* END */
    255