123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- /***************************************************************************/
- /* */
- /* ftsmooth.c */
- /* */
- /* Anti-aliasing renderer interface (body). */
- /* */
- /* Copyright 2000-2001, 2002 by */
- /* David Turner, Robert Wilhelm, and Werner Lemberg. */
- /* */
- /* This file is part of the FreeType project, and may only be used, */
- /* modified, and distributed under the terms of the FreeType project */
- /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
- /* this file you indicate that you have read the license and */
- /* understand and accept it fully. */
- /* */
- /***************************************************************************/
- #include <ft2build.h>
- #include FT_INTERNAL_OBJECTS_H
- #include FT_OUTLINE_H
- #include "ftsmooth.h"
- #include "ftgrays.h"
- #include "ftsmerrs.h"
- /* initialize renderer -- init its raster */
- static FT_Error
- ft_smooth_init( FT_Renderer render )
- {
- FT_Library library = FT_MODULE_LIBRARY( render );
- render->clazz->raster_class->raster_reset( render->raster,
- library->raster_pool,
- library->raster_pool_size );
- return 0;
- }
- /* sets render-specific mode */
- static FT_Error
- ft_smooth_set_mode( FT_Renderer render,
- FT_ULong mode_tag,
- FT_Pointer data )
- {
- /* we simply pass it to the raster */
- return render->clazz->raster_class->raster_set_mode( render->raster,
- mode_tag,
- data );
- }
- /* transform a given glyph image */
- static FT_Error
- ft_smooth_transform( FT_Renderer render,
- FT_GlyphSlot slot,
- FT_Matrix* matrix,
- FT_Vector* delta )
- {
- FT_Error error = Smooth_Err_Ok;
- if ( slot->format != render->glyph_format )
- {
- error = Smooth_Err_Invalid_Argument;
- goto Exit;
- }
- if ( matrix )
- FT_Outline_Transform( &slot->outline, matrix );
- if ( delta )
- FT_Outline_Translate( &slot->outline, delta->x, delta->y );
- Exit:
- return error;
- }
- /* return the glyph's control box */
- static void
- ft_smooth_get_cbox( FT_Renderer render,
- FT_GlyphSlot slot,
- FT_BBox* cbox )
- {
- FT_MEM_ZERO( cbox, sizeof ( *cbox ) );
- if ( slot->format == render->glyph_format )
- FT_Outline_Get_CBox( &slot->outline, cbox );
- }
- /* convert a slot's glyph image into a bitmap */
- static FT_Error
- ft_smooth_render_generic( FT_Renderer render,
- FT_GlyphSlot slot,
- FT_Render_Mode mode,
- FT_Vector* origin,
- FT_Render_Mode required_mode,
- FT_Int hmul,
- FT_Int vmul )
- {
- FT_Error error;
- FT_Outline* outline = NULL;
- FT_BBox cbox;
- FT_UInt width, height, pitch;
- FT_Bitmap* bitmap;
- FT_Memory memory;
- FT_Raster_Params params;
- /* check glyph image format */
- if ( slot->format != render->glyph_format )
- {
- error = Smooth_Err_Invalid_Argument;
- goto Exit;
- }
- /* check mode */
- if ( mode != required_mode )
- return Smooth_Err_Cannot_Render_Glyph;
- outline = &slot->outline;
- /* translate the outline to the new origin if needed */
- if ( origin )
- FT_Outline_Translate( outline, origin->x, origin->y );
- /* compute the control box, and grid fit it */
- FT_Outline_Get_CBox( outline, &cbox );
- cbox.xMin &= -64;
- cbox.yMin &= -64;
- cbox.xMax = ( cbox.xMax + 63 ) & -64;
- cbox.yMax = ( cbox.yMax + 63 ) & -64;
- width = ( cbox.xMax - cbox.xMin ) >> 6;
- height = ( cbox.yMax - cbox.yMin ) >> 6;
- bitmap = &slot->bitmap;
- memory = render->root.memory;
- /* release old bitmap buffer */
- if ( slot->flags & FT_GLYPH_OWN_BITMAP )
- {
- FT_FREE( bitmap->buffer );
- slot->flags &= ~FT_GLYPH_OWN_BITMAP;
- }
- /* allocate new one, depends on pixel format */
- pitch = width;
- if ( hmul )
- {
- width = width * hmul;
- pitch = ( width + 3 ) & -4;
- }
- if ( vmul )
- height *= vmul;
- bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
- bitmap->num_grays = 256;
- bitmap->width = width;
- bitmap->rows = height;
- bitmap->pitch = pitch;
- if ( FT_ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) )
- goto Exit;
- slot->flags |= FT_GLYPH_OWN_BITMAP;
- /* translate outline to render it into the bitmap */
- FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin );
- /* set up parameters */
- params.target = bitmap;
- params.source = outline;
- params.flags = FT_RASTER_FLAG_AA;
- /* implode outline if needed */
- {
- FT_Int n;
- FT_Vector* vec;
- if ( hmul )
- for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
- vec->x *= hmul;
- if ( vmul )
- for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
- vec->y *= vmul;
- }
- /* render outline into the bitmap */
- error = render->raster_render( render->raster, ¶ms );
- /* deflate outline if needed */
- {
- FT_Int n;
- FT_Vector* vec;
- if ( hmul )
- for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
- vec->x /= hmul;
- if ( vmul )
- for ( vec = outline->points, n = 0; n < outline->n_points; n++, vec++ )
- vec->y /= vmul;
- }
- FT_Outline_Translate( outline, cbox.xMin, cbox.yMin );
- if ( error )
- goto Exit;
- slot->format = FT_GLYPH_FORMAT_BITMAP;
- slot->bitmap_left = (FT_Int)( cbox.xMin >> 6 );
- slot->bitmap_top = (FT_Int)( cbox.yMax >> 6 );
- Exit:
- if ( outline && origin )
- FT_Outline_Translate( outline, -origin->x, -origin->y );
- return error;
- }
- /* convert a slot's glyph image into a bitmap */
- static FT_Error
- ft_smooth_render( FT_Renderer render,
- FT_GlyphSlot slot,
- FT_Render_Mode mode,
- FT_Vector* origin )
- {
- return ft_smooth_render_generic( render, slot, mode, origin,
- FT_RENDER_MODE_NORMAL,
- 0, 0 );
- }
- /* convert a slot's glyph image into a horizontal LCD bitmap */
- static FT_Error
- ft_smooth_render_lcd( FT_Renderer render,
- FT_GlyphSlot slot,
- FT_Render_Mode mode,
- FT_Vector* origin )
- {
- FT_Error error;
- error = ft_smooth_render_generic( render, slot, mode, origin,
- FT_RENDER_MODE_LCD,
- 3, 0 );
- if ( !error )
- slot->bitmap.pixel_mode = FT_PIXEL_MODE_LCD;
- return error;
- }
- /* convert a slot's glyph image into a vertical LCD bitmap */
- static FT_Error
- ft_smooth_render_lcd_v( FT_Renderer render,
- FT_GlyphSlot slot,
- FT_Render_Mode mode,
- FT_Vector* origin )
- {
- FT_Error error;
- error = ft_smooth_render_generic( render, slot, mode, origin,
- FT_RENDER_MODE_LCD_V,
- 0, 3 );
- if ( !error )
- slot->bitmap.pixel_mode = FT_PIXEL_MODE_LCD_V;
- return error;
- }
- FT_CALLBACK_TABLE_DEF
- const FT_Renderer_Class ft_smooth_renderer_class =
- {
- {
- ft_module_renderer,
- sizeof( FT_RendererRec ),
- "smooth",
- 0x10000L,
- 0x20000L,
- 0, /* module specific interface */
- (FT_Module_Constructor)ft_smooth_init,
- (FT_Module_Destructor) 0,
- (FT_Module_Requester) 0
- },
- FT_GLYPH_FORMAT_OUTLINE,
- (FT_Renderer_RenderFunc) ft_smooth_render,
- (FT_Renderer_TransformFunc)ft_smooth_transform,
- (FT_Renderer_GetCBoxFunc) ft_smooth_get_cbox,
- (FT_Renderer_SetModeFunc) ft_smooth_set_mode,
- (FT_Raster_Funcs*) &ft_grays_raster
- };
- FT_CALLBACK_TABLE_DEF
- const FT_Renderer_Class ft_smooth_lcd_renderer_class =
- {
- {
- ft_module_renderer,
- sizeof( FT_RendererRec ),
- "smooth-lcd",
- 0x10000L,
- 0x20000L,
- 0, /* module specific interface */
- (FT_Module_Constructor)ft_smooth_init,
- (FT_Module_Destructor) 0,
- (FT_Module_Requester) 0
- },
- FT_GLYPH_FORMAT_OUTLINE,
- (FT_Renderer_RenderFunc) ft_smooth_render_lcd,
- (FT_Renderer_TransformFunc)ft_smooth_transform,
- (FT_Renderer_GetCBoxFunc) ft_smooth_get_cbox,
- (FT_Renderer_SetModeFunc) ft_smooth_set_mode,
- (FT_Raster_Funcs*) &ft_grays_raster
- };
- FT_CALLBACK_TABLE_DEF
- const FT_Renderer_Class ft_smooth_lcdv_renderer_class =
- {
- {
- ft_module_renderer,
- sizeof( FT_RendererRec ),
- "smooth-lcdv",
- 0x10000L,
- 0x20000L,
- 0, /* module specific interface */
- (FT_Module_Constructor)ft_smooth_init,
- (FT_Module_Destructor) 0,
- (FT_Module_Requester) 0
- },
- FT_GLYPH_FORMAT_OUTLINE,
- (FT_Renderer_RenderFunc) ft_smooth_render_lcd_v,
- (FT_Renderer_TransformFunc)ft_smooth_transform,
- (FT_Renderer_GetCBoxFunc) ft_smooth_get_cbox,
- (FT_Renderer_SetModeFunc) ft_smooth_set_mode,
- (FT_Raster_Funcs*) &ft_grays_raster
- };
- /* END */
|