tt_size_reset( TT_Size size ) { TT_Face face; FT_Error error = FT_Err_Ok; FT_Size_Metrics* metrics; size->ttmetrics.valid = FALSE; face = (TT_Face)size->root.face; metrics = &size->metrics; /* copy the result from base layer */ *metrics = size->root.metrics; if ( metrics->x_ppem < 1 || metrics->y_ppem < 1 ) return FT_THROW( Invalid_PPem ); /* This bit flag, if set, indicates that the ppems must be */ /* rounded to integers. Nearly all TrueType fonts have this bit */ /* set, as hinting won't work really well otherwise. */ /* */ if ( face->header.Flags & 8 ) { metrics->x_scale = FT_DivFix( metrics->x_ppem << 6, face->root.units_per_EM ); metrics->y_scale = FT_DivFix( metrics->y_ppem << 6, face->root.units_per_EM ); metrics->ascender = FT_PIX_ROUND( FT_MulFix( face->root.ascender, metrics->y_scale ) ); metrics->descender = FT_PIX_ROUND( FT_MulFix( face->root.descender, metrics->y_scale ) ); metrics->height = FT_PIX_ROUND( FT_MulFix( face->root.height, metrics->y_scale ) ); metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->root.max_advance_width, metrics->x_scale ) ); } /* compute new transformation */ if ( metrics->x_ppem >= metrics->y_ppem ) { size->ttmetrics.scale = metrics->x_scale; size->ttmetrics.ppem = metrics->x_ppem; size->ttmetrics.x_ratio = 0x10000L; size->ttmetrics.y_ratio = FT_DivFix( metrics->y_ppem, metrics->x_ppem ); } else { size->ttmetrics.scale = metrics->y_scale; size->ttmetrics.ppem = metrics->y_ppem; size->ttmetrics.x_ratio = FT_DivFix( metrics->x_ppem, metrics->y_ppem ); size->ttmetrics.y_ratio = 0x10000L; } #ifdef TT_USE_BYTECODE_INTERPRETER size->cvt_ready = -1; #endif /* TT_USE_BYTECODE_INTERPRETER */ if ( !error ) size->ttmetrics.valid = TRUE; return error; }
/* reset the blues table when the device transform changes */ static void psh_blues_scale_zones( PSH_Blues blues, FT_Fixed scale, FT_Pos delta ) { FT_UInt count; FT_UInt num; PSH_Blue_Table table = 0; /* */ /* Determine whether we need to suppress overshoots or */ /* not. We simply need to compare the vertical scale */ /* parameter to the raw bluescale value. Here is why: */ /* */ /* We need to suppress overshoots for all pointsizes. */ /* At 300dpi that satisfies: */ /* */ /* pointsize < 240*bluescale + 0.49 */ /* */ /* This corresponds to: */ /* */ /* pixelsize < 1000*bluescale + 49/24 */ /* */ /* scale*EM_Size < 1000*bluescale + 49/24 */ /* */ /* However, for normal Type 1 fonts, EM_Size is 1000! */ /* We thus only check: */ /* */ /* scale < bluescale + 49/24000 */ /* */ /* which we shorten to */ /* */ /* "scale < bluescale" */ /* */ /* Note that `blue_scale' is stored 1000 times its real */ /* value, and that `scale' converts from font units to */ /* fractional pixels. */ /* */ /* 1000 / 64 = 125 / 8 */ if ( scale >= 0x20C49BAL ) blues->no_overshoots = FT_BOOL( scale < blues->blue_scale * 8 / 125 ); else blues->no_overshoots = FT_BOOL( scale * 125 < blues->blue_scale * 8 ); /* */ /* The blue threshold is the font units distance under */ /* which overshoots are suppressed due to the BlueShift */ /* even if the scale is greater than BlueScale. */ /* */ /* It is the smallest distance such that */ /* */ /* dist <= BlueShift && dist*scale <= 0.5 pixels */ /* */ { FT_Int threshold = blues->blue_shift; while ( threshold > 0 && FT_MulFix( threshold, scale ) > 32 ) threshold--; blues->blue_threshold = threshold; } for ( num = 0; num < 4; num++ ) { PSH_Blue_Zone zone; switch ( num ) { case 0: table = &blues->normal_top; break; case 1: table = &blues->normal_bottom; break; case 2: table = &blues->family_top; break; default: table = &blues->family_bottom; break; } zone = table->zones; count = table->count; for ( ; count > 0; count--, zone++ ) { zone->cur_top = FT_MulFix( zone->org_top, scale ) + delta; zone->cur_bottom = FT_MulFix( zone->org_bottom, scale ) + delta; zone->cur_ref = FT_MulFix( zone->org_ref, scale ) + delta; zone->cur_delta = FT_MulFix( zone->org_delta, scale ); /* round scaled reference position */ zone->cur_ref = FT_PIX_ROUND( zone->cur_ref ); #if 0 if ( zone->cur_ref > zone->cur_top ) zone->cur_ref -= 64; else if ( zone->cur_ref < zone->cur_bottom ) zone->cur_ref += 64; #endif } } /* process the families now */ for ( num = 0; num < 2; num++ ) { PSH_Blue_Zone zone1, zone2; FT_UInt count1, count2; PSH_Blue_Table normal, family; switch ( num ) { case 0: normal = &blues->normal_top; family = &blues->family_top; break; default: normal = &blues->normal_bottom; family = &blues->family_bottom; } zone1 = normal->zones; count1 = normal->count; for ( ; count1 > 0; count1--, zone1++ ) { /* try to find a family zone whose reference position is less */ /* than 1 pixel far from the current zone */ zone2 = family->zones; count2 = family->count; for ( ; count2 > 0; count2--, zone2++ ) { FT_Pos Delta; Delta = zone1->org_ref - zone2->org_ref; if ( Delta < 0 ) Delta = -Delta; if ( FT_MulFix( Delta, scale ) < 64 ) { zone1->cur_top = zone2->cur_top; zone1->cur_bottom = zone2->cur_bottom; zone1->cur_ref = zone2->cur_ref; zone1->cur_delta = zone2->cur_delta; break; } } } } }
/* convert a slot's glyph image into a bitmap */ static FT_Error ft_raster1_render( FT_Renderer render, FT_GlyphSlot slot, FT_Render_Mode mode, const FT_Vector* origin ) { FT_Error error; FT_Outline* outline; FT_BBox cbox, cbox0; 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 = FT_THROW( Invalid_Argument ); goto Exit; } /* check rendering mode */ if ( mode != FT_RENDER_MODE_MONO ) { /* raster1 is only capable of producing monochrome bitmaps */ return FT_THROW( 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, &cbox0 ); /* undocumented but confirmed: bbox values get rounded */ #if 1 cbox.xMin = FT_PIX_ROUND( cbox0.xMin ); cbox.yMin = FT_PIX_ROUND( cbox0.yMin ); cbox.xMax = FT_PIX_ROUND( cbox0.xMax ); cbox.yMax = FT_PIX_ROUND( cbox0.yMax ); #else cbox.xMin = FT_PIX_FLOOR( cbox.xMin ); cbox.yMin = FT_PIX_FLOOR( cbox.yMin ); cbox.xMax = FT_PIX_CEIL( cbox.xMax ); cbox.yMax = FT_PIX_CEIL( cbox.yMax ); #endif /* If either `width' or `height' round to 0, try */ /* explicitly rounding up/down. In the case of */ /* glyphs containing only one very narrow feature, */ /* this gives the drop-out compensation in the scan */ /* conversion code a chance to do its stuff. */ width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 ); if ( width == 0 ) { cbox.xMin = FT_PIX_FLOOR( cbox0.xMin ); cbox.xMax = FT_PIX_CEIL( cbox0.xMax ); width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 ); }
static FT_Error Reset_Outline_Size( TT_Size size ) { TT_Face face; FT_Error error = TT_Err_Ok; FT_Size_Metrics* metrics; if ( size->ttmetrics.valid ) return TT_Err_Ok; face = (TT_Face)size->root.face; metrics = &size->metrics; if ( metrics->x_ppem < 1 || metrics->y_ppem < 1 ) return TT_Err_Invalid_PPem; /* compute new transformation */ if ( metrics->x_ppem >= metrics->y_ppem ) { size->ttmetrics.scale = metrics->x_scale; size->ttmetrics.ppem = metrics->x_ppem; size->ttmetrics.x_ratio = 0x10000L; size->ttmetrics.y_ratio = FT_MulDiv( metrics->y_ppem, 0x10000L, metrics->x_ppem ); } else { size->ttmetrics.scale = metrics->y_scale; size->ttmetrics.ppem = metrics->y_ppem; size->ttmetrics.x_ratio = FT_MulDiv( metrics->x_ppem, 0x10000L, metrics->y_ppem ); size->ttmetrics.y_ratio = 0x10000L; } /* Compute root ascender, descender, text height, and max_advance */ metrics->ascender = FT_PIX_ROUND( FT_MulFix( face->root.ascender, metrics->y_scale ) ); metrics->descender = FT_PIX_ROUND( FT_MulFix( face->root.descender, metrics->y_scale ) ); metrics->height = FT_PIX_ROUND( FT_MulFix( face->root.height, metrics->y_scale ) ); metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->root.max_advance_width, metrics->x_scale ) ); #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS /* set to `invalid' by default */ size->strike_index = 0xFFFFU; #endif #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER { TT_ExecContext exec; FT_UInt i, j; /* Scale the cvt values to the new ppem. */ /* We use by default the y ppem to scale the CVT. */ for ( i = 0; i < size->cvt_size; i++ ) size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale ); /* All twilight points are originally zero */ for ( j = 0; j < (FT_UInt)size->twilight.n_points; j++ ) { size->twilight.org[j].x = 0; size->twilight.org[j].y = 0; size->twilight.cur[j].x = 0; size->twilight.cur[j].y = 0; } /* clear storage area */ for ( i = 0; i < (FT_UInt)size->storage_size; i++ ) size->storage[i] = 0; size->GS = tt_default_graphics_state; /* get execution context and run prep program */ if ( size->debug ) exec = size->context; else exec = TT_New_Context( face ); /* debugging instances have their own context */ if ( !exec ) return TT_Err_Could_Not_Find_Context; TT_Load_Context( exec, face, size ); TT_Set_CodeRange( exec, tt_coderange_cvt, face->cvt_program, face->cvt_program_size ); TT_Clear_CodeRange( exec, tt_coderange_glyph ); exec->instruction_trap = FALSE; exec->top = 0; exec->callTop = 0; if ( face->cvt_program_size > 0 ) { error = TT_Goto_CodeRange( exec, tt_coderange_cvt, 0 ); if ( error ) goto End; if ( !size->debug ) error = face->interpreter( exec ); } else error = TT_Err_Ok; size->GS = exec->GS; /* save default graphics state */ End: TT_Save_Context( exec, size ); if ( !size->debug ) TT_Done_Context( exec ); /* debugging instances keep their context */ } #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ if ( !error ) size->ttmetrics.valid = TRUE; return error; }
static FT_Error af_loader_load_g( AF_Loader loader, AF_Scaler scaler, FT_UInt glyph_index, FT_Int32 load_flags, FT_UInt depth ) { FT_Error error; FT_Face face = loader->face; FT_GlyphLoader gloader = loader->gloader; AF_ScriptMetrics metrics = loader->metrics; AF_GlyphHints hints = &loader->hints; FT_GlyphSlot slot = face->glyph; FT_Slot_Internal internal = slot->internal; error = FT_Load_Glyph( face, glyph_index, load_flags ); if ( error ) goto Exit; loader->transformed = internal->glyph_transformed; if ( loader->transformed ) { FT_Matrix inverse; loader->trans_matrix = internal->glyph_matrix; loader->trans_delta = internal->glyph_delta; inverse = loader->trans_matrix; FT_Matrix_Invert( &inverse ); FT_Vector_Transform( &loader->trans_delta, &inverse ); } /* set linear metrics */ slot->linearHoriAdvance = slot->metrics.horiAdvance; slot->linearVertAdvance = slot->metrics.vertAdvance; switch ( slot->format ) { case FT_GLYPH_FORMAT_OUTLINE: /* translate the loaded glyph when an internal transform is needed */ if ( loader->transformed ) FT_Outline_Translate( &slot->outline, loader->trans_delta.x, loader->trans_delta.y ); /* copy the outline points in the loader's current */ /* extra points which is used to keep original glyph coordinates */ error = FT_GLYPHLOADER_CHECK_POINTS( gloader, slot->outline.n_points + 4, slot->outline.n_contours ); if ( error ) goto Exit; FT_ARRAY_COPY( gloader->current.outline.points, slot->outline.points, slot->outline.n_points ); FT_ARRAY_COPY( gloader->current.outline.contours, slot->outline.contours, slot->outline.n_contours ); FT_ARRAY_COPY( gloader->current.outline.tags, slot->outline.tags, slot->outline.n_points ); gloader->current.outline.n_points = slot->outline.n_points; gloader->current.outline.n_contours = slot->outline.n_contours; /* compute original horizontal phantom points (and ignore */ /* vertical ones) */ loader->pp1.x = hints->x_delta; loader->pp1.y = hints->y_delta; loader->pp2.x = FT_MulFix( slot->metrics.horiAdvance, hints->x_scale ) + hints->x_delta; loader->pp2.y = hints->y_delta; /* be sure to check for spacing glyphs */ if ( slot->outline.n_points == 0 ) goto Hint_Metrics; /* now load the slot image into the auto-outline and run the */ /* automatic hinting process */ metrics->clazz->script_hints_apply( hints, &gloader->current.outline, metrics ); /* we now need to hint the metrics according to the change in */ /* width/positioning that occured during the hinting process */ { #ifndef AF_USE_WARPER FT_Pos old_advance, old_rsb, old_lsb, new_lsb; FT_Pos pp1x_uh, pp2x_uh; AF_AxisHints axis = &hints->axis[AF_DIMENSION_HORZ]; AF_Edge edge1 = axis->edges; /* leftmost edge */ AF_Edge edge2 = edge1 + axis->num_edges - 1; /* rightmost edge */ if ( axis->num_edges > 1 && AF_HINTS_DO_ADVANCE( hints ) ) { old_advance = loader->pp2.x; old_rsb = old_advance - edge2->opos; old_lsb = edge1->opos; new_lsb = edge1->pos; /* remember unhinted values to later account */ /* for rounding errors */ pp1x_uh = new_lsb - old_lsb; pp2x_uh = edge2->pos + old_rsb; /* prefer too much space over too little space */ /* for very small sizes */ if ( old_lsb < 24 ) pp1x_uh -= 5; if ( old_rsb < 24 ) pp2x_uh += 5; loader->pp1.x = FT_PIX_ROUND( pp1x_uh ); loader->pp2.x = FT_PIX_ROUND( pp2x_uh ); slot->lsb_delta = loader->pp1.x - pp1x_uh; slot->rsb_delta = loader->pp2.x - pp2x_uh; } else #endif /* !AF_USE_WARPER */ { FT_Pos pp1x = loader->pp1.x; FT_Pos pp2x = loader->pp2.x; loader->pp1.x = FT_PIX_ROUND( loader->pp1.x ); loader->pp2.x = FT_PIX_ROUND( loader->pp2.x ); slot->lsb_delta = loader->pp1.x - pp1x; slot->rsb_delta = loader->pp2.x - pp2x; } } /* good, we simply add the glyph to our loader's base */ FT_GlyphLoader_Add( gloader ); break; case FT_GLYPH_FORMAT_COMPOSITE: { FT_UInt nn, num_subglyphs = slot->num_subglyphs; FT_UInt num_base_subgs, start_point; FT_SubGlyph subglyph; start_point = gloader->base.outline.n_points; /* first of all, copy the subglyph descriptors in the glyph loader */ error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs ); if ( error ) goto Exit; FT_ARRAY_COPY( gloader->current.subglyphs, slot->subglyphs, num_subglyphs ); gloader->current.num_subglyphs = num_subglyphs; num_base_subgs = gloader->base.num_subglyphs; /* now, read each subglyph independently */ for ( nn = 0; nn < num_subglyphs; nn++ ) { FT_Vector pp1, pp2; FT_Pos x, y; FT_UInt num_points, num_new_points, num_base_points; /* gloader.current.subglyphs can change during glyph loading due */ /* to re-allocation -- we must recompute the current subglyph on */ /* each iteration */ subglyph = gloader->base.subglyphs + num_base_subgs + nn; pp1 = loader->pp1; pp2 = loader->pp2; num_base_points = gloader->base.outline.n_points; error = af_loader_load_g( loader, scaler, subglyph->index, load_flags, depth + 1 ); if ( error ) goto Exit; /* recompute subglyph pointer */ subglyph = gloader->base.subglyphs + num_base_subgs + nn; if ( subglyph->flags & FT_SUBGLYPH_FLAG_USE_MY_METRICS ) { pp1 = loader->pp1; pp2 = loader->pp2; } else { loader->pp1 = pp1; loader->pp2 = pp2; } num_points = gloader->base.outline.n_points; num_new_points = num_points - num_base_points; /* now perform the transform required for this subglyph */ if ( subglyph->flags & ( FT_SUBGLYPH_FLAG_SCALE | FT_SUBGLYPH_FLAG_XY_SCALE | FT_SUBGLYPH_FLAG_2X2 ) ) { FT_Vector* cur = gloader->base.outline.points + num_base_points; FT_Vector* limit = cur + num_new_points; for ( ; cur < limit; cur++ ) FT_Vector_Transform( cur, &subglyph->transform ); } /* apply offset */ if ( !( subglyph->flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES ) ) { FT_Int k = subglyph->arg1; FT_UInt l = subglyph->arg2; FT_Vector* p1; FT_Vector* p2; if ( start_point + k >= num_base_points || l >= (FT_UInt)num_new_points ) { error = AF_Err_Invalid_Composite; goto Exit; } l += num_base_points; /* for now, only use the current point coordinates; */ /* we may consider another approach in the near future */ p1 = gloader->base.outline.points + start_point + k; p2 = gloader->base.outline.points + start_point + l; x = p1->x - p2->x; y = p1->y - p2->y; } else { x = FT_MulFix( subglyph->arg1, hints->x_scale ) + hints->x_delta; y = FT_MulFix( subglyph->arg2, hints->y_scale ) + hints->y_delta; x = FT_PIX_ROUND( x ); y = FT_PIX_ROUND( y ); } { FT_Outline dummy = gloader->base.outline; dummy.points += num_base_points; dummy.n_points = (short)num_new_points; FT_Outline_Translate( &dummy, x, y ); } } } break; default: /* we don't support other formats (yet?) */ error = AF_Err_Unimplemented_Feature; } Hint_Metrics: if ( depth == 0 ) { FT_BBox bbox; FT_Vector vvector; vvector.x = slot->metrics.vertBearingX - slot->metrics.horiBearingX; vvector.y = slot->metrics.vertBearingY - slot->metrics.horiBearingY; vvector.x = FT_MulFix( vvector.x, metrics->scaler.x_scale ); vvector.y = FT_MulFix( vvector.y, metrics->scaler.y_scale ); /* transform the hinted outline if needed */ if ( loader->transformed ) { FT_Outline_Transform( &gloader->base.outline, &loader->trans_matrix ); FT_Vector_Transform( &vvector, &loader->trans_matrix ); } /* we must translate our final outline by -pp1.x and compute */ /* the new metrics */ if ( loader->pp1.x ) FT_Outline_Translate( &gloader->base.outline, -loader->pp1.x, 0 ); FT_Outline_Get_CBox( &gloader->base.outline, &bbox ); bbox.xMin = FT_PIX_FLOOR( bbox.xMin ); bbox.yMin = FT_PIX_FLOOR( bbox.yMin ); bbox.xMax = FT_PIX_CEIL( bbox.xMax ); bbox.yMax = FT_PIX_CEIL( bbox.yMax ); slot->metrics.width = bbox.xMax - bbox.xMin; slot->metrics.height = bbox.yMax - bbox.yMin; slot->metrics.horiBearingX = bbox.xMin; slot->metrics.horiBearingY = bbox.yMax; slot->metrics.vertBearingX = FT_PIX_FLOOR( bbox.xMin + vvector.x ); slot->metrics.vertBearingY = FT_PIX_FLOOR( bbox.yMax + vvector.y ); /* for mono-width fonts (like Andale, Courier, etc.) we need */ /* to keep the original rounded advance width */ #if 0 if ( !FT_IS_FIXED_WIDTH( slot->face ) ) slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; else slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance, x_scale ); #else if ( !FT_IS_FIXED_WIDTH( slot->face ) ) slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; else slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance, metrics->scaler.x_scale ); #endif slot->metrics.vertAdvance = FT_MulFix( slot->metrics.vertAdvance, metrics->scaler.y_scale ); slot->metrics.horiAdvance = FT_PIX_ROUND( slot->metrics.horiAdvance ); slot->metrics.vertAdvance = FT_PIX_ROUND( slot->metrics.vertAdvance ); /* now copy outline into glyph slot */ FT_GlyphLoader_Rewind( internal->loader ); error = FT_GlyphLoader_CopyPoints( internal->loader, gloader ); if ( error ) goto Exit; slot->outline = internal->loader->base.outline; slot->format = FT_GLYPH_FORMAT_OUTLINE; } #ifdef DEBUG_HINTER af_debug_hinter = hinter; #endif Exit: return error; }