#include <ft2build.h>
#include "cidload.h"
#include "cidgload.h"
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_STREAM_H
#include FT_OUTLINE_H
#include "ciderrs.h"
#undef FT_COMPONENT
#define FT_COMPONENT trace_cidgload
FT_CALLBACK_DEF( FT_Error )
cid_load_glyph( T1_Decoder decoder,
FT_UInt glyph_index )
{
CID_Face face = (CID_Face)decoder->builder.face;
CID_FaceInfo cid = &face->cid;
FT_Byte* p;
FT_UInt fd_select;
FT_Stream stream = face->root.stream;
FT_Error error = 0;
FT_Byte* charstring = 0;
FT_Memory memory = face->root.memory;
FT_UInt glyph_length = 0;
#ifdef FT_CONFIG_OPTION_INCREMENTAL
if ( face->root.internal->incremental_interface )
{
FT_Data glyph_data;
error = face->root.internal->incremental_interface->funcs->get_glyph_data(
face->root.internal->incremental_interface->object,
glyph_index,
&glyph_data );
if ( error )
goto Exit;
p = (FT_Byte*)glyph_data.pointer;
fd_select = (FT_UInt)cid_get_offset( &p, (FT_Byte)cid->fd_bytes );
if ( glyph_data.length != 0 )
{
glyph_length = glyph_data.length - cid->fd_bytes;
FT_ALLOC( charstring, glyph_length );
if ( !error )
ft_memcpy( charstring, glyph_data.pointer + cid->fd_bytes,
glyph_length );
}
face->root.internal->incremental_interface->funcs->free_glyph_data(
face->root.internal->incremental_interface->object,
&glyph_data );
if ( error )
goto Exit;
}
else
#endif
{
FT_UInt entry_len = cid->fd_bytes + cid->gd_bytes;
FT_ULong off1;
if ( FT_STREAM_SEEK( cid->data_offset + cid->cidmap_offset +
glyph_index * entry_len ) ||
FT_FRAME_ENTER( 2 * entry_len ) )
goto Exit;
p = (FT_Byte*)stream->cursor;
fd_select = (FT_UInt) cid_get_offset( &p, (FT_Byte)cid->fd_bytes );
off1 = (FT_ULong)cid_get_offset( &p, (FT_Byte)cid->gd_bytes );
p += cid->fd_bytes;
glyph_length = (FT_UInt) cid_get_offset(
&p, (FT_Byte)cid->gd_bytes ) - off1;
FT_FRAME_EXIT();
if ( glyph_length == 0 )
goto Exit;
if ( FT_ALLOC( charstring, glyph_length ) )
goto Exit;
if ( FT_STREAM_READ_AT( cid->data_offset + off1,
charstring, glyph_length ) )
goto Exit;
}
{
CID_FaceDict dict;
CID_Subrs cid_subrs = face->subrs + fd_select;
FT_Int cs_offset;
decoder->num_subrs = cid_subrs->num_subrs;
decoder->subrs = cid_subrs->code;
decoder->subrs_len = 0;
dict = cid->font_dicts + fd_select;
decoder->font_matrix = dict->font_matrix;
decoder->font_offset = dict->font_offset;
decoder->lenIV = dict->private_dict.lenIV;
cs_offset = ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
if ( decoder->lenIV >= 0 )
cid_decrypt( charstring, glyph_length, 4330 );
error = decoder->funcs.parse_charstrings( decoder,
charstring + cs_offset,
glyph_length - cs_offset );
}
FT_FREE( charstring );
#ifdef FT_CONFIG_OPTION_INCREMENTAL
if ( !error &&
face->root.internal->incremental_interface &&
face->root.internal->incremental_interface->funcs->get_glyph_metrics )
{
FT_Incremental_MetricsRec metrics;
metrics.bearing_x = decoder->builder.left_bearing.x;
metrics.bearing_y = decoder->builder.left_bearing.y;
metrics.advance = decoder->builder.advance.x;
error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
face->root.internal->incremental_interface->object,
glyph_index, FALSE, &metrics );
decoder->builder.left_bearing.x = metrics.bearing_x;
decoder->builder.left_bearing.y = metrics.bearing_y;
decoder->builder.advance.x = metrics.advance;
decoder->builder.advance.y = 0;
}
#endif
Exit:
return error;
}
#if 0
FT_LOCAL_DEF( FT_Error )
cid_face_compute_max_advance( CID_Face face,
FT_Int* max_advance )
{
FT_Error error;
T1_DecoderRec decoder;
FT_Int glyph_index;
PSAux_Service psaux = (PSAux_Service)face->psaux;
*max_advance = 0;
error = psaux->t1_decoder_funcs->init( &decoder,
(FT_Face)face,
0,
0,
0,
0,
0,
cid_load_glyph );
if ( error )
return error;
decoder.builder.metrics_only = 1;
decoder.builder.load_points = 0;
for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
glyph_index++ )
{
error = cid_load_glyph( &decoder, glyph_index );
}
*max_advance = decoder.builder.advance.x;
return CID_Err_Ok;
}
#endif
FT_LOCAL_DEF( FT_Error )
cid_slot_load_glyph( CID_GlyphSlot glyph,
CID_Size size,
FT_Int glyph_index,
FT_Int32 load_flags )
{
FT_Error error;
T1_DecoderRec decoder;
CID_Face face = (CID_Face)glyph->root.face;
FT_Bool hinting;
PSAux_Service psaux = (PSAux_Service)face->psaux;
FT_Matrix font_matrix;
FT_Vector font_offset;
if ( load_flags & FT_LOAD_NO_RECURSE )
load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
glyph->x_scale = size->root.metrics.x_scale;
glyph->y_scale = size->root.metrics.y_scale;
glyph->root.outline.n_points = 0;
glyph->root.outline.n_contours = 0;
hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
( load_flags & FT_LOAD_NO_HINTING ) == 0 );
glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
{
error = psaux->t1_decoder_funcs->init( &decoder,
(FT_Face)face,
(FT_Size)size,
(FT_GlyphSlot)glyph,
0,
0,
hinting,
FT_LOAD_TARGET_MODE(load_flags),
cid_load_glyph );
decoder.builder.no_recurse = FT_BOOL(
( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ) );
error = cid_load_glyph( &decoder, glyph_index );
font_matrix = decoder.font_matrix;
font_offset = decoder.font_offset;
psaux->t1_decoder_funcs->done( &decoder );
}
if ( !error )
{
glyph->root.outline.flags &= FT_OUTLINE_OWNER;
glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
if ( load_flags & FT_LOAD_NO_RECURSE )
{
FT_Slot_Internal internal = glyph->root.internal;
glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
glyph->root.metrics.horiAdvance = decoder.builder.advance.x;
internal->glyph_matrix = font_matrix;
internal->glyph_delta = font_offset;
internal->glyph_transformed = 1;
}
else
{
FT_BBox cbox;
FT_Glyph_Metrics* metrics = &glyph->root.metrics;
metrics->horiAdvance = decoder.builder.advance.x;
glyph->root.linearHoriAdvance = decoder.builder.advance.x;
glyph->root.internal->glyph_transformed = 0;
metrics->vertBearingX = 0;
metrics->vertBearingY = 0;
metrics->vertAdvance = 0;
glyph->root.linearVertAdvance = 0;
glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
if ( size && size->root.metrics.y_ppem < 24 )
glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
FT_Outline_Transform( &glyph->root.outline, &font_matrix );
FT_Outline_Translate( &glyph->root.outline,
font_offset.x,
font_offset.y );
if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
{
FT_Int n;
FT_Outline* cur = decoder.builder.base;
FT_Vector* vec = cur->points;
FT_Fixed x_scale = glyph->x_scale;
FT_Fixed y_scale = glyph->y_scale;
if ( !hinting )
for ( n = cur->n_points; n > 0; n--, vec++ )
{
vec->x = FT_MulFix( vec->x, x_scale );
vec->y = FT_MulFix( vec->y, y_scale );
}
FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
if ( hinting )
{
metrics->horiAdvance = ( metrics->horiAdvance + 32 ) & -64;
metrics->vertAdvance = ( metrics->vertAdvance + 32 ) & -64;
metrics->vertBearingX = ( metrics->vertBearingX + 32 ) & -64;
metrics->vertBearingY = ( metrics->vertBearingY + 32 ) & -64;
}
}
FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
if ( hinting )
{
cbox.xMin &= -64;
cbox.yMin &= -64;
cbox.xMax = ( cbox.xMax + 63 ) & -64;
cbox.yMax = ( cbox.yMax + 63 ) & -64;
}
metrics->width = cbox.xMax - cbox.xMin;
metrics->height = cbox.yMax - cbox.yMin;
metrics->horiBearingX = cbox.xMin;
metrics->horiBearingY = cbox.yMax;
}
}
return error;
}