freetype2/src/base/ftcalc.c

959 lines
21 KiB
C
Raw Normal View History

1999-12-17 00:11:37 +01:00
/***************************************************************************/
/* */
/* ftcalc.c */
/* */
/* Arithmetic computations (body). */
/* */
/* Copyright 1996-2006, 2008, 2012 by */
1999-12-17 00:11:37 +01:00
/* 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 */
1999-12-17 00:11:37 +01:00
/* 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. */
/* */
/***************************************************************************/
/*************************************************************************/
/* */
/* Support for 1-complement arithmetic has been totally dropped in this */
/* release. You can still write your own code if you need it. */
/* */
/*************************************************************************/
/*************************************************************************/
/* */
/* Implementing basic computation routines. */
1999-12-17 00:11:37 +01:00
/* */
/* FT_MulDiv(), FT_MulFix(), FT_DivFix(), FT_RoundFix(), FT_CeilFix(), */
/* and FT_FloorFix() are declared in freetype.h. */
1999-12-17 00:11:37 +01:00
/* */
/*************************************************************************/
2000-12-08 17:17:16 +01:00
#include <ft2build.h>
#include FT_GLYPH_H
2000-12-08 17:17:16 +01:00
#include FT_INTERNAL_CALC_H
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_OBJECTS_H
1999-12-17 00:11:37 +01:00
#ifdef FT_MULFIX_INLINED
#undef FT_MulFix
#endif
1999-12-17 00:11:37 +01:00
/* we need to define a 64-bits data type here */
2001-06-16 09:48:30 +02:00
#ifdef FT_LONG64
typedef FT_INT64 FT_Int64;
2001-06-16 09:48:30 +02:00
#else
typedef struct FT_Int64_
{
FT_UInt32 lo;
FT_UInt32 hi;
} FT_Int64;
2001-06-16 09:48:30 +02:00
#endif /* FT_LONG64 */
/*************************************************************************/
/* */
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
/* messages during execution. */
/* */
#undef FT_COMPONENT
#define FT_COMPONENT trace_calc
/* The following three functions are available regardless of whether */
/* FT_LONG64 is defined. */
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Fixed )
FT_RoundFix( FT_Fixed a )
{
return ( a >= 0 ) ? ( a + 0x8000L ) & ~0xFFFFL
: -((-a + 0x8000L ) & ~0xFFFFL );
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Fixed )
FT_CeilFix( FT_Fixed a )
{
return ( a >= 0 ) ? ( a + 0xFFFFL ) & ~0xFFFFL
: -((-a + 0xFFFFL ) & ~0xFFFFL );
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Fixed )
FT_FloorFix( FT_Fixed a )
{
return ( a >= 0 ) ? a & ~0xFFFFL
: -((-a) & ~0xFFFFL );
}
#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( FT_Int32 )
FT_Sqrt32( FT_Int32 x )
1999-12-17 00:11:37 +01:00
{
FT_UInt32 val, root, newroot, mask;
1999-12-17 00:11:37 +01:00
root = 0;
mask = (FT_UInt32)0x40000000UL;
val = (FT_UInt32)x;
1999-12-17 00:11:37 +01:00
do
{
newroot = root + mask;
2000-01-07 16:02:05 +01:00
if ( newroot <= val )
1999-12-17 00:11:37 +01:00
{
val -= newroot;
2000-01-07 16:02:05 +01:00
root = newroot + mask;
1999-12-17 00:11:37 +01:00
}
1999-12-17 00:11:37 +01:00
root >>= 1;
mask >>= 2;
} while ( mask != 0 );
1999-12-17 00:11:37 +01:00
return root;
}
2000-05-29 22:46:12 +02:00
#endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
1999-12-17 00:11:37 +01:00
#ifdef FT_LONG64
1999-12-17 00:11:37 +01:00
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long )
FT_MulDiv( FT_Long a,
FT_Long b,
FT_Long c )
1999-12-17 00:11:37 +01:00
{
FT_Int s;
FT_Long d;
2000-05-30 00:40:57 +02:00
2001-05-08 15:52:13 +02:00
1999-12-17 00:11:37 +01:00
s = 1;
if ( a < 0 ) { a = -a; s = -1; }
1999-12-17 00:11:37 +01:00
if ( b < 0 ) { b = -b; s = -s; }
if ( c < 0 ) { c = -c; s = -s; }
d = (FT_Long)( c > 0 ? ( (FT_Int64)a * b + ( c >> 1 ) ) / c
: 0x7FFFFFFFL );
2001-05-08 15:52:13 +02:00
return ( s > 0 ) ? d : -d;
1999-12-17 00:11:37 +01:00
}
#ifdef TT_USE_BYTECODE_INTERPRETER
/* documentation is in ftcalc.h */
FT_BASE_DEF( FT_Long )
FT_MulDiv_No_Round( FT_Long a,
FT_Long b,
FT_Long c )
{
FT_Int s;
FT_Long d;
s = 1;
if ( a < 0 ) { a = -a; s = -1; }
if ( b < 0 ) { b = -b; s = -s; }
if ( c < 0 ) { c = -c; s = -s; }
d = (FT_Long)( c > 0 ? (FT_Int64)a * b / c
: 0x7FFFFFFFL );
return ( s > 0 ) ? d : -d;
}
#endif /* TT_USE_BYTECODE_INTERPRETER */
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long )
FT_MulFix( FT_Long a,
FT_Long b )
1999-12-17 00:11:37 +01:00
{
#ifdef FT_MULFIX_ASSEMBLER
return FT_MULFIX_ASSEMBLER( a, b );
#else
FT_Int s = 1;
2001-05-08 15:52:13 +02:00
FT_Long c;
1999-12-17 00:11:37 +01:00
if ( a < 0 )
{
a = -a;
s = -1;
}
if ( b < 0 )
{
b = -b;
s = -s;
}
1999-12-17 00:11:37 +01:00
c = (FT_Long)( ( (FT_Int64)a * b + 0x8000L ) >> 16 );
return ( s > 0 ) ? c : -c;
#endif /* FT_MULFIX_ASSEMBLER */
1999-12-17 00:11:37 +01:00
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long )
FT_DivFix( FT_Long a,
FT_Long b )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
FT_UInt32 q;
1999-12-17 00:11:37 +01:00
s = 1;
if ( a < 0 ) { a = -a; s = -1; }
if ( b < 0 ) { b = -b; s = -s; }
1999-12-17 00:11:37 +01:00
if ( b == 0 )
2000-05-30 00:40:57 +02:00
/* check for division by 0 */
2000-05-29 22:46:12 +02:00
q = 0x7FFFFFFFL;
else
1999-12-17 00:11:37 +01:00
/* compute result directly */
q = (FT_UInt32)( ( ( (FT_Int64)a << 16 ) + ( b >> 1 ) ) / b );
1999-12-17 00:11:37 +01:00
return ( s < 0 ? -(FT_Long)q : (FT_Long)q );
1999-12-17 00:11:37 +01:00
}
#else /* !FT_LONG64 */
2001-04-26 02:21:48 +02:00
static void
ft_multo64( FT_UInt32 x,
FT_UInt32 y,
FT_Int64 *z )
{
FT_UInt32 lo1, hi1, lo2, hi2, lo, hi, i1, i2;
2001-04-26 02:21:48 +02:00
2000-10-31 21:42:18 +01:00
lo1 = x & 0x0000FFFFU; hi1 = x >> 16;
lo2 = y & 0x0000FFFFU; hi2 = y >> 16;
lo = lo1 * lo2;
i1 = lo1 * hi2;
i2 = lo2 * hi1;
hi = hi1 * hi2;
2001-04-26 02:21:48 +02:00
/* Check carry overflow of i1 + i2 */
i1 += i2;
hi += (FT_UInt32)( i1 < i2 ) << 16;
2001-04-26 02:21:48 +02:00
hi += i1 >> 16;
i1 = i1 << 16;
2001-04-26 02:21:48 +02:00
/* Check carry overflow of i1 + lo */
lo += i1;
hi += ( lo < i1 );
2001-04-26 02:21:48 +02:00
z->lo = lo;
z->hi = hi;
}
2001-04-26 02:21:48 +02:00
static FT_UInt32
ft_div64by32( FT_UInt32 hi,
FT_UInt32 lo,
FT_UInt32 y )
2001-04-26 02:21:48 +02:00
{
FT_UInt32 r, q;
FT_Int i;
2001-04-26 02:21:48 +02:00
q = 0;
r = hi;
if ( r >= y )
return (FT_UInt32)0x7FFFFFFFL;
i = 32;
2001-04-26 02:21:48 +02:00
do
{
r <<= 1;
q <<= 1;
r |= lo >> 31;
if ( r >= y )
2001-04-26 02:21:48 +02:00
{
r -= y;
q |= 1;
}
lo <<= 1;
} while ( --i );
return q;
2001-04-26 02:21:48 +02:00
}
static void
FT_Add64( FT_Int64* x,
FT_Int64* y,
FT_Int64 *z )
2001-04-26 02:21:48 +02:00
{
register FT_UInt32 lo, hi;
2001-04-26 02:21:48 +02:00
lo = x->lo + y->lo;
hi = x->hi + y->hi + ( lo < x->lo );
2001-04-26 02:21:48 +02:00
z->lo = lo;
z->hi = hi;
}
/* documentation is in freetype.h */
/* The FT_MulDiv function has been optimized thanks to ideas from */
/* Graham Asher. The trick is to optimize computation when everything */
/* fits within 32-bits (a rather common case). */
/* */
/* we compute 'a*b+c/2', then divide it by 'c'. (positive values) */
/* */
/* 46340 is FLOOR(SQRT(2^31-1)). */
/* */
/* if ( a <= 46340 && b <= 46340 ) then ( a*b <= 0x7FFEA810 ) */
/* */
/* 0x7FFFFFFF - 0x7FFEA810 = 0x157F0 */
/* */
/* if ( c < 0x157F0*2 ) then ( a*b+c/2 <= 0x7FFFFFFF ) */
/* */
/* and 2*0x157F0 = 176096 */
/* */
FT_EXPORT_DEF( FT_Long )
FT_MulDiv( FT_Long a,
FT_Long b,
FT_Long c )
1999-12-17 00:11:37 +01:00
{
long s;
1999-12-17 00:11:37 +01:00
/* XXX: this function does not allow 64-bit arguments */
1999-12-17 00:11:37 +01:00
if ( a == 0 || b == c )
return a;
s = a; a = FT_ABS( a );
s ^= b; b = FT_ABS( b );
s ^= c; c = FT_ABS( c );
1999-12-17 00:11:37 +01:00
Minor cleanups to remove compiler warnings. * include/freetype/cache/ftcmanag.h (FTC_MAX_BYTES_DEFAULT): Use `L' for constant. * include/freetype/config/ftoption.h (FT_RENDER_POOL_SIZE): Ditto. * src/base/ftcalc.c (FT_MulDiv): Use `L' for constant. * src/base/ftglyph.c (FT_Glyph_Get_CBox): Remove `error' variable. * src/base/fttrigon.c (ft_trig_arctan_table): Use `L' for constants. * src/base/ftobjs.c (FT_Done_Size): Fix return value. (FT_Set_Char_Size, FT_Set_Pixel_Sizes, FT_Get_Kerning): Remove unused `memory' variable. * src/autohint/ahglyph.c (ah_get_orientation): Use `L' for constant. * src/autohint/ahhint.c (ah_hint_edges_3, ah_hinter_align_edge_points): Remove unused `before' and `after' variables. (ah_hinter_align_weak_points): Remove unused `edge_limit' variable. (ah_hinter_load): Remove unused `new_advance', `start_contour', and `metrics' variables. * src/cff/cffload.c (CFF_Load_Encoding): Remove dead code to avoid compiler warning. * src/cff/cffobjs.c (CFF_Init_Face): Remove unused `base_offset' variable. * src/cff/cffgload.c (CFF_Parse_CharStrings): Remove unused `outline' variable. (cff_compute_bias): Use `U' for constant. * src/cid/cidload.c (cid_decrypt): Ditto. * src/psaux/psobjs.c (T1_Decrypt): Ditto. * src/psaux/t1decode.c (T1_Decoder_Parse_CharStrings): Ditto. * src/sfnt/ttload.c (TT_Load_Kern): Remove unused `version' variable. * src/sfnt/ttsbit.c (TT_Load_SBit_Image): Remove unused `top' variable. * src/truetype/ttgload.c (load_truetype_glyph): Remove unused `num_contours' and `ins_offset' variables. (compute_glyph_metrics): Remove unused `Top' and `x_scale' variables. (TT_Load_Glyph): Remove unused `memory' variable. * src/smooth/ftgrays.c (grays_raster_render): Use `L' for constants.
2001-06-18 16:23:45 +02:00
if ( a <= 46340L && b <= 46340L && c <= 176095L && c > 0 )
2000-05-30 00:40:57 +02:00
a = ( a * b + ( c >> 1 ) ) / c;
else if ( (FT_Int32)c > 0 )
1999-12-17 00:11:37 +01:00
{
FT_Int64 temp, temp2;
2000-05-30 00:40:57 +02:00
ft_multo64( (FT_Int32)a, (FT_Int32)b, &temp );
temp2.hi = 0;
temp2.lo = (FT_UInt32)(c >> 1);
FT_Add64( &temp, &temp2, &temp );
a = ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
1999-12-17 00:11:37 +01:00
}
else
2000-05-30 00:40:57 +02:00
a = 0x7FFFFFFFL;
1999-12-17 00:11:37 +01:00
return ( s < 0 ? -a : a );
1999-12-17 00:11:37 +01:00
}
#ifdef TT_USE_BYTECODE_INTERPRETER
FT_BASE_DEF( FT_Long )
FT_MulDiv_No_Round( FT_Long a,
FT_Long b,
FT_Long c )
{
long s;
if ( a == 0 || b == c )
return a;
s = a; a = FT_ABS( a );
s ^= b; b = FT_ABS( b );
s ^= c; c = FT_ABS( c );
if ( a <= 46340L && b <= 46340L && c > 0 )
a = a * b / c;
else if ( (FT_Int32)c > 0 )
{
FT_Int64 temp;
ft_multo64( (FT_Int32)a, (FT_Int32)b, &temp );
a = ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
}
else
a = 0x7FFFFFFFL;
return ( s < 0 ? -a : a );
}
#endif /* TT_USE_BYTECODE_INTERPRETER */
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long )
FT_MulFix( FT_Long a,
FT_Long b )
1999-12-17 00:11:37 +01:00
{
#ifdef FT_MULFIX_ASSEMBLER
return FT_MULFIX_ASSEMBLER( a, b );
#elif 0
/*
* This code is nonportable. See comment below.
*
* However, on a platform where right-shift of a signed quantity fills
* the leftmost bits by copying the sign bit, it might be faster.
*/
FT_Long sa, sb;
FT_ULong ua, ub;
if ( a == 0 || b == 0x10000L )
return a;
/*
* This is a clever way of converting a signed number `a' into its
* absolute value (stored back into `a') and its sign. The sign is
* stored in `sa'; 0 means `a' was positive or zero, and -1 means `a'
* was negative. (Similarly for `b' and `sb').
*
* Unfortunately, it doesn't work (at least not portably).
*
* It makes the assumption that right-shift on a negative signed value
2012-01-16 18:00:24 +01:00
* fills the leftmost bits by copying the sign bit. This is wrong.
* According to K&R 2nd ed, section `A7.8 Shift Operators' on page 206,
* the result of right-shift of a negative signed value is
* implementation-defined. At least one implementation fills the
* leftmost bits with 0s (i.e., it is exactly the same as an unsigned
* right shift). This means that when `a' is negative, `sa' ends up
* with the value 1 rather than -1. After that, everything else goes
* wrong.
*/
2005-11-12 08:34:40 +01:00
sa = ( a >> ( sizeof ( a ) * 8 - 1 ) );
a = ( a ^ sa ) - sa;
2005-11-12 08:34:40 +01:00
sb = ( b >> ( sizeof ( b ) * 8 - 1 ) );
b = ( b ^ sb ) - sb;
ua = (FT_ULong)a;
ub = (FT_ULong)b;
if ( ua <= 2048 && ub <= 1048576L )
ua = ( ua * ub + 0x8000U ) >> 16;
else
{
FT_ULong al = ua & 0xFFFFU;
ua = ( ua >> 16 ) * ub + al * ( ub >> 16 ) +
( ( al * ( ub & 0xFFFFU ) + 0x8000U ) >> 16 );
}
sa ^= sb,
2005-11-12 08:34:40 +01:00
ua = (FT_ULong)(( ua ^ sa ) - sa);
return (FT_Long)ua;
2005-11-12 08:34:40 +01:00
#else /* 0 */
1999-12-17 00:11:37 +01:00
FT_Long s;
2000-03-28 13:17:58 +02:00
FT_ULong ua, ub;
2000-05-29 22:46:12 +02:00
1999-12-17 00:11:37 +01:00
if ( a == 0 || b == 0x10000L )
return a;
s = a; a = FT_ABS( a );
s ^= b; b = FT_ABS( b );
1999-12-17 00:11:37 +01:00
2000-03-28 13:17:58 +02:00
ua = (FT_ULong)a;
ub = (FT_ULong)b;
if ( ua <= 2048 && ub <= 1048576L )
ua = ( ua * ub + 0x8000UL ) >> 16;
1999-12-17 00:11:37 +01:00
else
{
FT_ULong al = ua & 0xFFFFUL;
1999-12-17 00:11:37 +01:00
ua = ( ua >> 16 ) * ub + al * ( ub >> 16 ) +
( ( al * ( ub & 0xFFFFUL ) + 0x8000UL ) >> 16 );
1999-12-17 00:11:37 +01:00
}
* src/base/ftdbgmem.c (ft_mem_table_resize, ft_mem_table_new, ft_mem_table_set, ft_mem_debug_alloc, ft_mem_debug_free, ft_mem_debug_realloc, ft_mem_debug_done, FT_Alloc_Debug, FT_Realloc_Debug, FT_Free_Debug): Fix compiler warnings. * src/base/ftcalc.c (FT_MulFix): Ditto. * src/cff/cffdrivr.c (cff_get_name_index): Ditto. * src/cff/cffobjs.c (CFF_Size_Get_Global_Funcs, CFF_Size_Init, CFF_GlyphSlot_Init): Ditto. * src/cid/cidobjs.c (CID_GlyphSlot_Init, CID_Size_Get_Globals_Funcs): Ditto. * src/type1/t1objs.c (T1_Size_Get_Globals_Funcs, T1_GlyphSlot_Init): Ditto. * src/pshinter/pshmod.c (pshinter_interface): Use `static const'. * src/winfonts/winfnt.c (FNT_Get_Next_Char): Remove unused variables. * include/freetype/internal/psaux.h (T1_Builder_Funcs): Renamed to... (T1_Builder_FuncsRec): This. (T1_Builder_Funcs): New typedef. (PSAux_Interface): Remove compiler warnings. * src/psaux/psauxmod.c (t1_builder_funcs), src/psaux/psobjs.h (t1_builder_funcs): Updated. * src/pshinter/pshglob.h (PSH_Blue_Align): Replaced with ... (PSH_BLUE_ALIGN_{NONE,TOP,BOT}): New defines. (PSH_AlignmentRec): Updated. * include/freetype/internal/ftstream.h (GET_Char, GET_Byte): Fix typo. * include/freetype/internal/ftgloadr.h (FT_SubGlyph): Ditto. * src/base/ftstream (FT_Get_Char): Rename to... (FT_Stream_Get_Char): This. * src/base/ftnames.c (FT_Get_Sfnt_Name): s/index/idx/ -- `index' is a built-in function in gcc, causing warning messages with gcc 3.0. * src/autohint/ahglyph.c (ah_outline_load): Ditto. * src/autohint/ahglobal.c (ah_hinter_compute_blues): Ditto. * src/cache/ftcmanag.c (ftc_family_table_alloc, ftc_family_table_free, FTC_Manager_Done, FTC_Manager_Register_Cache): Ditto. * src/cff/cffload.c (cff_new_index, cff_done_index, cff_explicit_index, CFF_Access_Element, CFF_Forget_Element, CFF_Get_Name, CFF_Get_String, CFF_Load_SubFont, CFF_Load_Font, CFF_Done_Font): Ditto. * src/psaux/psobjs.c (PS_Table_Add, PS_Parser_LoadField): Ditto. * src/psaux/t1decode.c (T1_Decoder_Parse_Charstrings): Ditto. * src/pshinter/pshrec.c (ps_mask_test_bit, ps_mask_clear_bit, ps_mask_set_bit, ps_dimension_add_t1stem, ps_hints_t1stem3, * src/pshinter/pshalgo1.c (psh1_hint_table_record, psh1_hint_table_record_mask, psh1_hint_table_activate_mask): Ditto. * src/pshinter/pshalgo2.c (psh2_hint_table_record, psh2_hint_table_record_mask, psh2_hint_table_activate_mask): Ditto. * src/sfnt/ttpost.c (Load_Format_20, Load_Format_25, TT_Get_PS_Name): Ditto. * src/truetype/ttgload.c (TT_Get_Metrics, Get_HMetrics, load_truetype_glyph): Ditto. * src/type1/t1load.c (parse_subrs, T1_Open_Face): Ditto. * src/type1/t1afm.c (T1_Get_Kerning): Ditto. * include/freetype/cache/ftcmanag.h (ftc_family_table_free): Ditto.
2002-03-07 22:59:59 +01:00
return ( s < 0 ? -(FT_Long)ua : (FT_Long)ua );
2005-11-12 08:34:40 +01:00
#endif /* 0 */
1999-12-17 00:11:37 +01:00
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long )
FT_DivFix( FT_Long a,
FT_Long b )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
FT_UInt32 q;
1999-12-17 00:11:37 +01:00
/* XXX: this function does not allow 64-bit arguments */
s = (FT_Int32)a; a = FT_ABS( a );
s ^= (FT_Int32)b; b = FT_ABS( b );
1999-12-17 00:11:37 +01:00
if ( (FT_UInt32)b == 0 )
2000-05-30 00:40:57 +02:00
{
/* check for division by 0 */
q = (FT_UInt32)0x7FFFFFFFL;
2000-05-30 00:40:57 +02:00
}
else if ( ( a >> 16 ) == 0 )
1999-12-17 00:11:37 +01:00
{
/* compute result directly */
q = (FT_UInt32)( ( a << 16 ) + ( b >> 1 ) ) / (FT_UInt32)b;
1999-12-17 00:11:37 +01:00
}
else
{
/* we need more bits; we have to do it by hand */
FT_Int64 temp, temp2;
1999-12-17 00:11:37 +01:00
temp.hi = (FT_Int32) ( a >> 16 );
temp.lo = (FT_UInt32)( a << 16 );
2001-04-26 02:21:48 +02:00
temp2.hi = 0;
temp2.lo = (FT_UInt32)( b >> 1 );
FT_Add64( &temp, &temp2, &temp );
q = ft_div64by32( temp.hi, temp.lo, (FT_Int32)b );
1999-12-17 00:11:37 +01:00
}
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
}
#if 0
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( void )
FT_MulTo64( FT_Int32 x,
FT_Int32 y,
FT_Int64 *z )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
1999-12-17 00:11:37 +01:00
s = x; x = FT_ABS( x );
s ^= y; y = FT_ABS( y );
1999-12-17 00:11:37 +01:00
2001-04-26 02:21:48 +02:00
ft_multo64( x, y, z );
1999-12-17 00:11:37 +01:00
if ( s < 0 )
{
z->lo = (FT_UInt32)-(FT_Int32)z->lo;
z->hi = ~z->hi + !( z->lo );
1999-12-17 00:11:37 +01:00
}
}
2001-10-18 11:51:09 +02:00
/* apparently, the second version of this code is not compiled correctly */
/* on Mac machines with the MPW C compiler.. tsk, tsk, tsk... */
2001-10-18 11:51:09 +02:00
#if 1
2001-10-18 11:51:09 +02:00
FT_EXPORT_DEF( FT_Int32 )
FT_Div64by32( FT_Int64* x,
FT_Int32 y )
{
FT_Int32 s;
FT_UInt32 q, r, i, lo;
s = x->hi;
if ( s < 0 )
{
x->lo = (FT_UInt32)-(FT_Int32)x->lo;
x->hi = ~x->hi + !x->lo;
2001-10-18 11:51:09 +02:00
}
s ^= y; y = FT_ABS( y );
2001-10-18 11:51:09 +02:00
/* Shortcut */
if ( x->hi == 0 )
{
if ( y > 0 )
q = x->lo / y;
else
q = 0x7FFFFFFFL;
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
}
r = x->hi;
lo = x->lo;
if ( r >= (FT_UInt32)y ) /* we know y is to be treated as unsigned here */
return ( s < 0 ? 0x80000001UL : 0x7FFFFFFFUL );
/* Return Max/Min Int32 if division overflow. */
/* This includes division by zero! */
2001-10-18 11:51:09 +02:00
q = 0;
for ( i = 0; i < 32; i++ )
{
r <<= 1;
q <<= 1;
r |= lo >> 31;
if ( r >= (FT_UInt32)y )
{
r -= y;
q |= 1;
}
lo <<= 1;
}
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
}
#else /* 0 */
FT_EXPORT_DEF( FT_Int32 )
FT_Div64by32( FT_Int64* x,
FT_Int32 y )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
2001-04-26 02:21:48 +02:00
FT_UInt32 q;
1999-12-17 00:11:37 +01:00
2000-05-30 00:40:57 +02:00
1999-12-17 00:11:37 +01:00
s = x->hi;
if ( s < 0 )
{
x->lo = (FT_UInt32)-(FT_Int32)x->lo;
x->hi = ~x->hi + !x->lo;
1999-12-17 00:11:37 +01:00
}
s ^= y; y = FT_ABS( y );
1999-12-17 00:11:37 +01:00
/* Shortcut */
if ( x->hi == 0 )
{
2000-05-30 00:40:57 +02:00
if ( y > 0 )
q = ( x->lo + ( y >> 1 ) ) / y;
2000-05-30 00:40:57 +02:00
else
q = 0x7FFFFFFFL;
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
1999-12-17 00:11:37 +01:00
}
2001-04-26 02:21:48 +02:00
q = ft_div64by32( x->hi, x->lo, y );
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
1999-12-17 00:11:37 +01:00
}
#endif /* 0 */
#endif /* 0 */
#endif /* FT_LONG64 */
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( void )
FT_Matrix_Multiply( const FT_Matrix* a,
FT_Matrix *b )
{
FT_Fixed xx, xy, yx, yy;
if ( !a || !b )
return;
xx = FT_MulFix( a->xx, b->xx ) + FT_MulFix( a->xy, b->yx );
xy = FT_MulFix( a->xx, b->xy ) + FT_MulFix( a->xy, b->yy );
yx = FT_MulFix( a->yx, b->xx ) + FT_MulFix( a->yy, b->yx );
yy = FT_MulFix( a->yx, b->xy ) + FT_MulFix( a->yy, b->yy );
b->xx = xx; b->xy = xy;
b->yx = yx; b->yy = yy;
}
/* documentation is in ftglyph.h */
FT_EXPORT_DEF( FT_Error )
FT_Matrix_Invert( FT_Matrix* matrix )
{
FT_Pos delta, xx, yy;
if ( !matrix )
return FT_Err_Invalid_Argument;
/* compute discriminant */
delta = FT_MulFix( matrix->xx, matrix->yy ) -
FT_MulFix( matrix->xy, matrix->yx );
if ( !delta )
return FT_Err_Invalid_Argument; /* matrix can't be inverted */
matrix->xy = - FT_DivFix( matrix->xy, delta );
matrix->yx = - FT_DivFix( matrix->yx, delta );
xx = matrix->xx;
yy = matrix->yy;
matrix->xx = FT_DivFix( yy, delta );
matrix->yy = FT_DivFix( xx, delta );
return FT_Err_Ok;
}
/* documentation is in ftcalc.h */
FT_BASE_DEF( void )
FT_Matrix_Multiply_Scaled( const FT_Matrix* a,
FT_Matrix *b,
FT_Long scaling )
{
FT_Fixed xx, xy, yx, yy;
FT_Long val = 0x10000L * scaling;
if ( !a || !b )
return;
xx = FT_MulDiv( a->xx, b->xx, val ) + FT_MulDiv( a->xy, b->yx, val );
xy = FT_MulDiv( a->xx, b->xy, val ) + FT_MulDiv( a->xy, b->yy, val );
yx = FT_MulDiv( a->yx, b->xx, val ) + FT_MulDiv( a->yy, b->yx, val );
yy = FT_MulDiv( a->yx, b->xy, val ) + FT_MulDiv( a->yy, b->yy, val );
b->xx = xx; b->xy = xy;
b->yx = yx; b->yy = yy;
}
/* documentation is in ftcalc.h */
FT_BASE_DEF( void )
FT_Vector_Transform_Scaled( FT_Vector* vector,
const FT_Matrix* matrix,
FT_Long scaling )
{
FT_Pos xz, yz;
FT_Long val = 0x10000L * scaling;
if ( !vector || !matrix )
return;
xz = FT_MulDiv( vector->x, matrix->xx, val ) +
FT_MulDiv( vector->y, matrix->xy, val );
yz = FT_MulDiv( vector->x, matrix->yx, val ) +
FT_MulDiv( vector->y, matrix->yy, val );
vector->x = xz;
vector->y = yz;
}
2004-12-14 00:16:59 +01:00
/* documentation is in ftcalc.h */
FT_BASE_DEF( FT_Int32 )
FT_SqrtFixed( FT_Int32 x )
{
FT_UInt32 root, rem_hi, rem_lo, test_div;
FT_Int count;
2000-10-31 21:42:18 +01:00
root = 0;
if ( x > 0 )
{
rem_hi = 0;
rem_lo = x;
count = 24;
do
{
rem_hi = ( rem_hi << 2 ) | ( rem_lo >> 30 );
rem_lo <<= 2;
root <<= 1;
test_div = ( root << 1 ) + 1;
if ( rem_hi >= test_div )
{
rem_hi -= test_div;
root += 1;
}
} while ( --count );
}
2000-09-12 00:50:13 +02:00
return (FT_Int32)root;
}
1999-12-17 00:11:37 +01:00
/* documentation is in ftcalc.h */
FT_BASE_DEF( FT_Int )
ft_corner_orientation( FT_Pos in_x,
FT_Pos in_y,
FT_Pos out_x,
FT_Pos out_y )
{
FT_Long result; /* avoid overflow on 16-bit system */
/* deal with the trivial cases quickly */
if ( in_y == 0 )
{
if ( in_x >= 0 )
result = out_y;
else
result = -out_y;
}
else if ( in_x == 0 )
{
if ( in_y >= 0 )
result = -out_x;
else
result = out_x;
}
else if ( out_y == 0 )
{
if ( out_x >= 0 )
result = in_y;
else
result = -in_y;
}
else if ( out_x == 0 )
{
if ( out_y >= 0 )
result = -in_x;
else
result = in_x;
}
else /* general case */
{
#ifdef FT_LONG64
FT_Int64 delta = (FT_Int64)in_x * out_y - (FT_Int64)in_y * out_x;
if ( delta == 0 )
result = 0;
else
result = 1 - 2 * ( delta < 0 );
#else
FT_Int64 z1, z2;
/* XXX: this function does not allow 64-bit arguments */
ft_multo64( (FT_Int32)in_x, (FT_Int32)out_y, &z1 );
ft_multo64( (FT_Int32)in_y, (FT_Int32)out_x, &z2 );
if ( z1.hi > z2.hi )
result = +1;
else if ( z1.hi < z2.hi )
result = -1;
else if ( z1.lo > z2.lo )
result = +1;
else if ( z1.lo < z2.lo )
result = -1;
else
result = 0;
#endif
}
/* XXX: only the sign of return value, +1/0/-1 must be used */
return (FT_Int)result;
}
/* documentation is in ftcalc.h */
FT_BASE_DEF( FT_Int )
ft_corner_is_flat( FT_Pos in_x,
FT_Pos in_y,
FT_Pos out_x,
FT_Pos out_y )
{
FT_Pos ax = in_x;
FT_Pos ay = in_y;
FT_Pos d_in, d_out, d_corner;
if ( ax < 0 )
ax = -ax;
if ( ay < 0 )
ay = -ay;
d_in = ax + ay;
ax = out_x;
if ( ax < 0 )
ax = -ax;
ay = out_y;
if ( ay < 0 )
ay = -ay;
d_out = ax + ay;
ax = out_x + in_x;
if ( ax < 0 )
ax = -ax;
ay = out_y + in_y;
if ( ay < 0 )
ay = -ay;
d_corner = ax + ay;
return ( d_in + d_out - d_corner ) < ( d_corner >> 4 );
}
1999-12-17 00:11:37 +01:00
/* END */