Skip to content

Commit

Permalink
Merge pull request #595 from jirkakunze/ttf-driver-rework-memory-mana…
Browse files Browse the repository at this point in the history
…gement

TTF driver: rework memory management
  • Loading branch information
bluewaysw authored Jul 8, 2024
2 parents f1cb728 + e7e7bf2 commit 36084ec
Show file tree
Hide file tree
Showing 23 changed files with 692 additions and 555 deletions.
2 changes: 2 additions & 0 deletions Driver/Font/TrueType/Adapter/ttcharmapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ CharMapFlags GeosCharMapFlag( word geosChar );

char getGeosCharForIndex( word ttIndex );

Boolean isGeosCharPair( word ttIndex_1, word ttIndex_2 );


#endif /* _TTCHARMAPPER_H_ */
27 changes: 14 additions & 13 deletions Driver/Font/TrueType/Adapter/ttinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ttinit.h"
#include "ttadapter.h"
#include "ttcharmapper.h"
#include "ttmemory.h"
#include "ftxkern.h"
#include <fileEnum.h>
#include <geos.h>
Expand All @@ -28,6 +29,7 @@
#include <ec.h>
#include <initfile.h>
#include <unicode.h>
#include <fontID.h>


static word DetectFontFiles( MemHandle* fileEnumBlock );
Expand Down Expand Up @@ -63,9 +65,9 @@ static word GetKernCount( TRUETYPE_VARS );

static word toHash( const char* str );

static int strlen( const char* str );
static word strlen( const char* str );

static void strcpy( char* dest, const char* source );
static char* strcpy( char* dest, const char* source );

static void strcpyname( char* dest, const char* source );

Expand Down Expand Up @@ -732,7 +734,7 @@ static sword getFontIDAvailIndex( FontID fontID, MemHandle fontInfoBlock )
ConstructOptr( fontInfoBlock, sizeof(LMemBlockHeader))) );
elements = LMemGetChunkSizePtr( fontsAvailEntrys ) / sizeof( FontsAvailEntry );

for( element = 0; element < elements; element++ )
for( element = 0; element < elements; ++element )
if( fontsAvailEntrys[element].FAE_fontID == fontID )
return element;

Expand Down Expand Up @@ -1003,6 +1005,7 @@ static word GetKernCount( TRUETYPE_VARS )
{
TT_Kerning kerningDir;
word table;
TT_Kern_0_Pair* pairs;
word numGeosKernPairs = 0;

if( TT_Get_Kerning_Directory( FACE, &kerningDir ) )
Expand All @@ -1019,16 +1022,13 @@ static word GetKernCount( TRUETYPE_VARS )
if( kerningDir.tables->format != 0 )
continue;

/* We only support decreasing the character spacing.*/
if( kerningDir.tables->t.kern0.pairs[i].value > 0 )
continue;
pairs = GEO_LOCK( kerningDir.tables->t.kern0.pairsBlock );

for( i = 0; i < kerningDir.tables->t.kern0.nPairs; ++i )
{
if( isGeosCharPair( kerningDir.tables->t.kern0.pairs[i].left,
kerningDir.tables->t.kern0.pairs[i].right ) )
if( isGeosCharPair( pairs[i].left, pairs[i].right ) )
++numGeosKernPairs;
}

GEO_UNLOCK( kerningDir.tables->t.kern0.pairsBlock );
}

Fail:
Expand All @@ -1041,19 +1041,20 @@ static word GetKernCount( TRUETYPE_VARS )
/* cycle. Therefore, the required functions are reimplemented here.*/
/*******************************************************************/

static int strlen( const char* str )
static word strlen( const char* str )
{
const char *s;

for ( s = str; *s; ++s )
;
return( s - str );
return( s - str );
}


static void strcpy( char* dest, const char* source )
static char* strcpy( char* dest, const char* source )
{
while( (*dest++ = *source++) != '\0' );
return dest;
}


Expand Down
19 changes: 11 additions & 8 deletions Driver/Font/TrueType/Adapter/ttwidths.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <heap.h>
#include "ttwidths.h"
#include "ttcharmapper.h"
#include "ttmemory.h"
#include "ttinit.h"
#include "freetype.h"
#include "ftxkern.h"
Expand Down Expand Up @@ -301,7 +302,7 @@ EC( ECCheckBounds( (void*)charTableEntry ) );
(CTF_IS_FIRST_KERN | CTF_IS_SECOND_KERN))
break;

kernPair++;
++kernPair;
}
}

Expand Down Expand Up @@ -336,6 +337,7 @@ static void ConvertKernPairs( TRUETYPE_VARS, FontBuf* fontBuf )
{
TT_Kerning kerningDir;
word table;
TT_Kern_0_Pair* pairs;

KernPair* kernPair = (KernPair*) ( ( (byte*)fontBuf ) + fontBuf->FB_kernPairs );
BBFixed* kernValue = (BBFixed*) ( ( (byte*)fontBuf ) + fontBuf->FB_kernValues );
Expand All @@ -359,16 +361,15 @@ EC( ECCheckBounds( (void*)kernValue ) );
if( kerningDir.tables->format != 0 )
continue;

pairs = GEO_LOCK( kerningDir.tables->t.kern0.pairsBlock );
EC( ECCheckBounds( pairs ) );

for( i = 0; i < kerningDir.tables->t.kern0.nPairs; ++i )
{
char left = getGeosCharForIndex( kerningDir.tables->t.kern0.pairs[i].left );
char right = getGeosCharForIndex( kerningDir.tables->t.kern0.pairs[i].right );
char left = getGeosCharForIndex( pairs[i].left );
char right = getGeosCharForIndex( pairs[i].right );


/* We only support decreasing the character spacing.*/
if( kerningDir.tables->t.kern0.pairs[i].value > 0 )
continue;

if( left && right )
{
WWFixedAsDWord scaledKernValue;
Expand All @@ -377,14 +378,16 @@ EC( ECCheckBounds( (void*)kernValue ) );
kernPair->KP_charRight = right;

/* save scaled kerning value */
scaledKernValue = SCALE_WORD( kerningDir.tables->t.kern0.pairs[i].value, SCALE_WIDTH );
scaledKernValue = SCALE_WORD( pairs[i].value, SCALE_WIDTH );
kernValue->BBF_int = IntegerOf( scaledKernValue );
kernValue->BBF_frac = FractionOf( scaledKernValue ) >> 8;

++kernPair;
++kernValue;
}
}

GEO_UNLOCK( kerningDir.tables->t.kern0.pairsBlock );
}
}

Expand Down
49 changes: 41 additions & 8 deletions Driver/Font/TrueType/FreeType/freetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,27 +343,34 @@

struct TT_Header_
{
#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_Fixed Table_Version;
TT_Fixed Font_Revision;

TT_Long CheckSum_Adjust;
TT_Long Magic_Number;
#endif

TT_UShort Flags;
TT_UShort Units_Per_EM;

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_Long Created [2];
TT_Long Modified[2];
#endif

TT_FWord xMin;
TT_FWord yMin;
TT_FWord xMax;
TT_FWord yMax;

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_UShort Mac_Style;
TT_UShort Lowest_Rec_PPEM;

TT_Short Font_Direction;
#endif

TT_Short Index_To_Loc_Format;
TT_Short Glyph_Data_Format;
};
Expand All @@ -389,6 +396,8 @@

TT_FWord min_Left_Side_Bearing; /* minimum left-sb */
TT_FWord min_Right_Side_Bearing; /* minimum right-sb */

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_FWord xMax_Extent; /* xmax extents */
TT_FWord caret_Slope_Rise;
TT_FWord caret_Slope_Run;
Expand All @@ -398,6 +407,7 @@
Reserved2,
Reserved3,
Reserved4;
#endif

TT_Short metric_Data_Format;
TT_UShort number_Of_HMetrics;
Expand All @@ -406,8 +416,8 @@
/* but they're used to connect the metrics header to the relevant */
/* `HMTX' or `VMTX' table. */

void* long_metrics;
void* short_metrics;
MemHandle long_metrics_block;
MemHandle short_metrics_block;
};

typedef struct TT_Horizontal_Header_ TT_Horizontal_Header;
Expand All @@ -430,6 +440,8 @@

TT_FWord min_Top_Side_Bearing; /* minimum left-sb or top-sb */
TT_FWord min_Bottom_Side_Bearing; /* minimum right-sb or bottom-sb */

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_FWord yMax_Extent; /* xmax or ymax extents */
TT_FWord caret_Slope_Rise;
TT_FWord caret_Slope_Run;
Expand All @@ -439,6 +451,7 @@
Reserved2,
Reserved3,
Reserved4;
#endif

TT_Short metric_Data_Format;
TT_UShort number_Of_VMetrics;
Expand All @@ -447,8 +460,8 @@
/* but they're used to connect the metrics header to the relevant */
/* `HMTX' or `VMTX' table. */

void* long_metrics;
void* short_metrics;
MemHandle long_metrics_block;
MemHandle short_metrics_block;
};

typedef struct TT_Vertical_Header_ TT_Vertical_Header;
Expand All @@ -468,6 +481,8 @@
TT_UShort usWeightClass;
TT_UShort usWidthClass;
TT_Short fsType;

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_FWord ySubscriptXSize;
TT_FWord ySubscriptYSize;
TT_FWord ySubscriptXOffset;
Expand All @@ -478,30 +493,36 @@
TT_FWord ySuperscriptYOffset;
TT_FWord yStrikeoutSize;
TT_FWord yStrikeoutPosition;
TT_Short sFamilyClass;
#endif

TT_Short sFamilyClass;
TT_Byte panose[10];

#ifdef TT_CONFIG_OPTION_SUPPORT_UNICODE_RANGES
TT_ULong ulUnicodeRange1; /* Bits 0-31 */
TT_ULong ulUnicodeRange2; /* Bits 32-63 */
TT_ULong ulUnicodeRange3; /* Bits 64-95 */
TT_ULong ulUnicodeRange4; /* Bits 96-127 */
#endif

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_Char achVendID[4];

TT_UShort fsSelection;
TT_UShort usFirstCharIndex;
TT_UShort usLastCharIndex;
#endif

TT_Short sTypoAscender;
TT_Short sTypoDescender;
TT_Short sTypoLineGap;
TT_UShort usWinAscent;
TT_UShort usWinDescent;

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
/* only version 1 tables: */

TT_ULong ulCodePageRange1; /* Bits 0-31 */
TT_ULong ulCodePageRange2; /* Bits 32-63 */
#endif
};

typedef struct TT_OS2_ TT_OS2;
Expand All @@ -511,15 +532,21 @@

struct TT_Postscript_
{
#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_Fixed FormatType;
TT_Fixed italicAngle;
#endif

TT_FWord underlinePosition;
TT_FWord underlineThickness;
TT_ULong isFixedPitch;

#ifdef TT_CONFIG_OPTION_SUPPORT_OPTIONAL_FIELDS
TT_ULong minMemType42;
TT_ULong maxMemType42;
TT_ULong minMemType1;
TT_ULong maxMemType1;
#endif

/* Glyph names follow in the file, but we don't */
/* load them by default. See the ftxpost.c extension. */
Expand Down Expand Up @@ -565,8 +592,14 @@
TT_Horizontal_Header* horizontal; /* TrueType horizontal header */
TT_OS2* os2; /* TrueType OS/2 table */
TT_Postscript* postscript; /* TrueType Postscript table */

#ifdef TT_CONFIG_OPTION_PROCESS_HDMX
TT_Hdmx* hdmx; /* TrueType hor. dev. metr. table */
#endif

#ifdef TT_CONFIG_OPTION_PROCESS_VMTX
TT_Vertical_Header* vertical; /* TT Vertical header, if present */
#endif
};

typedef struct TT_Face_Properties_ TT_Face_Properties;
Expand Down
Loading

0 comments on commit 36084ec

Please sign in to comment.