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