Skip to content

Commit

Permalink
Merge pull request bluewaysw#303 from jirkakunze/290_ttfdriver_rework…
Browse files Browse the repository at this point in the history
…_algorithm_for_creating_FontID

290 ttfdriver rework algorithm for creating font bluewaysw#290 bluewaysw#300
  • Loading branch information
bluewaysw authored Dec 3, 2023
2 parents ce83aac + 0768cc7 commit e4e9d21
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 70 deletions.
50 changes: 25 additions & 25 deletions CInclude/fontID.h
Original file line number Diff line number Diff line change
Expand Up @@ -1227,40 +1227,40 @@ typedef word FontID;
#define FID_INVALID 0x0000

typedef byte SerifFace;
#define SF_SLAB 0x00c0
#define SF_MODERN 0x0080
#define SF_TRANS 0x0040
#define SF_OLD 0x0000
#define SF_SLAB 0xc0
#define SF_MODERN 0x80
#define SF_TRANS 0x40
#define SF_OLD 0x00

typedef byte SansFace;
#define SF_A_CLOSED 0x0080
#define SF_A_OPEN 0x0000
#define SF_A_CLOSED 0x80
#define SF_A_OPEN 0x00

typedef byte ScriptFace;
#define SF_CURSIVE 0x0080
#define SF_CALLIGRAPHIC 0x0000
#define SF_CURSIVE 0x80
#define SF_CALLIGRAPHIC 0x00

typedef word FontGroup;
#define FG_NON_PORTABLE 0x0e00
#define FG_SPECIAL 0x0c00
#define FG_MONO 0x0a00
#define FG_SYMBOL 0x0800
#define FG_ORNAMENT 0x0600
#define FG_SCRIPT 0x0400
#define FG_SANS_SERIF 0x0200
#define FG_SERIF 0x0000
#define FG_SPECIAL 0x0c00
#define FG_MONO 0x0a00
#define FG_SYMBOL 0x0800
#define FG_ORNAMENT 0x0600
#define FG_SCRIPT 0x0400
#define FG_SANS_SERIF 0x0200
#define FG_SERIF 0x0000

typedef byte FontFamily;
#define FF_NON_PORTABLE 0x0007
#define FF_SPECIAL 0x0006
#define FF_MONO 0x0005
#define FF_SYMBOL 0x0004
#define FF_ORNAMENT 0x0003
#define FF_SCRIPT 0x0002
#define FF_SANS_SERIF 0x0001
#define FF_SERIF 0x0000
#define FF_NON_PORTABLE 0x07
#define FF_SPECIAL 0x06
#define FF_MONO 0x05
#define FF_SYMBOL 0x04
#define FF_ORNAMENT 0x03
#define FF_SCRIPT 0x02
#define FF_SANS_SERIF 0x01
#define FF_SERIF 0x00

typedef byte FontMap;
#define FM_DONT_USE 0x00ff
#define FM_EXACT 0x0000
#define FM_DONT_USE 0xff
#define FM_EXACT 0x00
#endif /* __FONTID_H */
115 changes: 99 additions & 16 deletions Driver/Font/TrueType/Adapter/ttinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ static sword getFontIDAvailIndex(
FontID fontID,
MemHandle fontInfoBlock );

static Boolean getFontID( const char* familiyName,
FontID* fontID );
static Boolean getFontID( TRUETYPE_VARS, FontID* fontID );

static FontWeight mapFontWeight( TT_Short weightClass );

static TextStyle mapTextStyle( const char* subfamily );
static TextStyle mapTextStyle( const char* subfamily );

static FontGroup mapFontGroup( TRUETYPE_VARS );

static word getNameFromNameTable(
TRUETYPE_VARS,
Expand All @@ -64,6 +65,8 @@ static int strlen( const char* str );

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

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

static int strcmp( const char* s1, const char* s2 );


Expand Down Expand Up @@ -295,7 +298,7 @@ EC( ECCheckFileHandle( truetypeFile ) );
if ( getNameFromNameTable( trueTypeVars, STYLE_NAME, STYLE_NAME_ID ) == 0 )
goto Fail;

mappedFont = getFontID( FAMILY_NAME, &fontID );
mappedFont = getFontID( trueTypeVars, &fontID );
availIndex = getFontIDAvailIndex( fontID, fontInfoBlock );

/* if we have an new font FontAvailEntry, FontInfo and Outline must be created */
Expand Down Expand Up @@ -463,13 +466,13 @@ EC( ECCheckFileHandle( truetypeFile ) );

static word toHash( const char* str )
{
word i;
dword hash = strlen( str );
word i;
word hash = strlen( str );

for ( i = 0; i < strlen( str ); ++i )
hash = ( ( hash * 7 ) % 65535 ) + str[i];
hash = hash * 7 + str[i];

return (word) hash;
return hash % 240 + 15;
}


Expand Down Expand Up @@ -556,6 +559,63 @@ static TextStyle mapTextStyle( const char* subfamily )
}


/********************************************************************
* mapFontGroup
********************************************************************
* SYNOPSIS: Determines the font goup on different parameters.
*
* PARAMETERS: TRUETYPE_VARS Pointer to truetypevar block.
*
* RETURNS: FontGroup Group of the given font.
*
* SIDE EFFECTS: none
*
* STRATEGY:
*
* REVISION HISTORY:
* Date Name Description
* ---- ---- -----------
* 28/11/23 JK Initial Revision
*******************************************************************/

#define B_FAMILY_TYPE 0
#define B_PROPORTION 3
static FontGroup mapFontGroup( TRUETYPE_VARS )
{
/* The font group of a TrueType font cannot be determined exactly. */
/* This implementation is therefore more of an approximation than */
/* an exact determination. */

/* recognize FF_MONO from panose fields */
if( FACE_PROPERTIES.os2->panose[B_PROPORTION] == 9 ) //Monospaced
return FG_MONO;

/* recognize FF_SANS_SERIF, FF_SERIF, FF_SYMBOL and FF_ORNAMENT from sFamilyClass */
switch( FACE_PROPERTIES.os2->sFamilyClass >> 8 )
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 7:
return FG_SERIF;
case 8:
return FG_SANS_SERIF;
case 9:
return FG_ORNAMENT;
case 12:
return FG_SYMBOL;
}

/* recognize FF_SCRIPT from panose fields */
if( FACE_PROPERTIES.os2->panose[B_FAMILY_TYPE] == 2 ) //Script
return FG_SCRIPT;

return FG_NON_PORTABLE;
}


/********************************************************************
* getFontID
********************************************************************
Expand All @@ -565,7 +625,7 @@ static TextStyle mapTextStyle( const char* subfamily )
*
* PARAMETERS: familyName* Font family name.
*
* RETURNS: FontID FontID found geos.ini or calculated.
* RETURNS: FontID FontID found in geos.ini or calculated.
*
* SIDE EFFECTS: none
*
Expand All @@ -577,15 +637,23 @@ static TextStyle mapTextStyle( const char* subfamily )
* 21/01/23 JK Initial Revision
*******************************************************************/

static Boolean getFontID( const char* familyName, FontID* fontID )
static Boolean getFontID( TRUETYPE_VARS, FontID* fontID )
{
char familyName[FID_NAME_LEN];


/* clean up family name */
strcpyname( familyName, trueTypeVars->familyName );

/* get FontID from geos.ini */
if( !InitFileReadInteger( FONTMAPPING_CATEGORY, familyName, fontID ) )
{
*fontID = ( FM_TRUETYPE | (*fontID & 0x0fff) );
return TRUE;
}

*fontID = MAKE_FONTID( familyName );
/* generate FontID */
*fontID = MAKE_FONTID( mapFontGroup( trueTypeVars ), trueTypeVars->familyName );
return FALSE;
}

Expand Down Expand Up @@ -665,7 +733,7 @@ static word getNameFromNameTable( TRUETYPE_VARS, char* name, TT_UShort nameID )
char* str;


for( n = 0; n < FACE_PROPERTIES.num_Names; n++ )
for( n = 0; n < FACE_PROPERTIES.num_Names; ++n )
{
TT_Get_Name_ID( FACE, n, &platformID, &encodingID, &languageID, &id );
if( id != nameID )
Expand All @@ -677,7 +745,7 @@ static word getNameFromNameTable( TRUETYPE_VARS, char* name, TT_UShort nameID )
{
TT_Get_Name_String( FACE, n, &str, &nameLength );

for (i = 1; str != 0 && i < nameLength; i += 2)
for( i = 1; str != 0 && i < nameLength; i += 2 )
*name++ = str[i];
*name = 0;
return nameLength >> 1;
Expand All @@ -688,7 +756,7 @@ static word getNameFromNameTable( TRUETYPE_VARS, char* name, TT_UShort nameID )
{
TT_Get_Name_String( FACE, n, &str, &nameLength );

for (i = 0; str != 0 && i < nameLength; i++)
for( i = 0; str != 0 && i < nameLength; ++i )
*name++ = str[i];
*name = 0;
return nameLength;
Expand All @@ -697,7 +765,7 @@ static word getNameFromNameTable( TRUETYPE_VARS, char* name, TT_UShort nameID )
{
TT_Get_Name_String( FACE, n, &str, &nameLength );

for (i = 1; str != 0 && i < nameLength; i += 2)
for( i = 1; str != 0 && i < nameLength; i += 2 )
*name++ = str[i];
*name = 0;
return nameLength >> 1;
Expand Down Expand Up @@ -945,7 +1013,22 @@ static int strlen( const char* str )

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


static void strcpyname( char* dest, const char* source )
{
while( *source != '\0' )
{
if( *source != ' ' )
{
*dest = *source;
++dest;
}
++source;
}
*dest = '\0'; // stringending
}


Expand Down
4 changes: 2 additions & 2 deletions Driver/Font/TrueType/Adapter/ttinit.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@
#define DESCENT( value ) ( value / 4 ) // 25% of size
#define DEFAULT_UNDER_THICK( value ) ( value / 10 ) // 10% of size
#define DEFAULT_UNDER_POSITION( value ) ( value / 10 ) // -10% of size
#define SAFETY( value ) ( value / 40 ) // 2.5% of size
#define SAFETY( value ) ( value / 40 ) // 2.5% of size


/***********************************************************************
* macro for calculating font ID
***********************************************************************/

#define MAKE_FONTID( family ) ( FM_TRUETYPE | ( 0x0fff & toHash ( family )))
#define MAKE_FONTID( fontGroup, familyName ) ( FM_TRUETYPE | fontGroup | ( 0x00ff & toHash ( familyName )))


/***********************************************************************
Expand Down
55 changes: 28 additions & 27 deletions Driver/Font/TrueType/Adapter/ttpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,47 +479,39 @@ static void CalcTransformMatrix( TransMatrix* transMatrix,
WWFixedAsDWord pointSize,
TextStyle stylesToImplement )
{
WWFixedAsDWord scalePointSize = GrUDivWWFixed( pointSize, MakeWWFixed( STANDARD_GRIDSIZE ) );
WWFixedAsDWord scaleFactor = GrUDivWWFixed( pointSize, MakeWWFixed( STANDARD_GRIDSIZE ) );

transMatrix->TM_e11.WWF_frac = FractionOf( scalePointSize );
transMatrix->TM_e11.WWF_int = IntegerOf( scalePointSize );
transMatrix->TM_e11.WWF_frac = FractionOf( scaleFactor );
transMatrix->TM_e11.WWF_int = IntegerOf( scaleFactor );
transMatrix->TM_e12.WWF_frac = 0;
transMatrix->TM_e12.WWF_int = 0;
transMatrix->TM_e21.WWF_frac = 0;
transMatrix->TM_e21.WWF_int = 0;
transMatrix->TM_e22.WWF_frac = FractionOf( scalePointSize );
transMatrix->TM_e22.WWF_int = IntegerOf( scalePointSize );
transMatrix->TM_e22.WWF_frac = FractionOf( scaleFactor );
transMatrix->TM_e22.WWF_int = IntegerOf( scaleFactor );
transMatrix->TM_e31.DWF_frac = 0;
transMatrix->TM_e31.DWF_int = 0;
transMatrix->TM_e32.DWF_frac = 0;
transMatrix->TM_e32.DWF_int = 0;

/* add styles if needed */
/* add styles if needed (order of processed styles is important) */
if( stylesToImplement & TS_BOLD )
{
WWFixedAsDWord scaleScript = GrMulWWFixed( scalePointSize, BOLD_FACTOR );
scaleFactor = GrMulWWFixed( scaleFactor, BOLD_FACTOR );


transMatrix->TM_e11.WWF_frac = FractionOf( scaleScript );
transMatrix->TM_e11.WWF_int = IntegerOf( scaleScript );
}

if( stylesToImplement & TS_ITALIC )
{
transMatrix->TM_e12.WWF_frac = FractionOf( ITALIC_FACTOR );
transMatrix->TM_e12.WWF_int = IntegerOf( ITALIC_FACTOR );
transMatrix->TM_e11.WWF_frac = FractionOf( scaleFactor );
transMatrix->TM_e11.WWF_int = IntegerOf( scaleFactor );
}

if( stylesToImplement & ( TS_SUBSCRIPT | TS_SUPERSCRIPT ) )
{
WWFixedAsDWord translation;


transMatrix->TM_e11.WWF_frac = FractionOf( SCRIPT_FACTOR );
transMatrix->TM_e11.WWF_int = IntegerOf( SCRIPT_FACTOR );

transMatrix->TM_e22.WWF_frac = FractionOf( SCRIPT_FACTOR );
transMatrix->TM_e22.WWF_int = IntegerOf( SCRIPT_FACTOR );
scaleFactor = GrMulWWFixed( scaleFactor, SCRIPT_FACTOR );

transMatrix->TM_e11.WWF_frac = transMatrix->TM_e22.WWF_frac = FractionOf( scaleFactor );
transMatrix->TM_e11.WWF_int = transMatrix->TM_e22.WWF_int = IntegerOf( scaleFactor );

if( stylesToImplement & TS_SUPERSCRIPT )
translation = GrMulWWFixed( SUPERSCRIPT_OFFSET, (dword)STANDARD_GRIDSIZE << 16 );
Expand All @@ -531,6 +523,15 @@ static void CalcTransformMatrix( TransMatrix* transMatrix,
transMatrix->TM_e32.DWF_int = IntegerOf( translation );
}

if( stylesToImplement & TS_ITALIC )
{
WWFixedAsDWord shearFactor = GrMulWWFixed( scaleFactor, ITALIC_FACTOR );


transMatrix->TM_e21.WWF_frac = FractionOf( shearFactor );
transMatrix->TM_e21.WWF_int = IntegerOf( shearFactor );
}

//TODO:
//width & weight einarbeiten
}
Expand Down Expand Up @@ -563,14 +564,14 @@ static void WriteComment( TRUETYPE_VARS, GStateHandle gstate )

TT_Get_Glyph_Metrics( GLYPH, &GLYPH_METRICS );

params[0] = GLYPH_METRICS.advance >> 6;
params[0] = GLYPH_METRICS.advance;
params[1] = 0;
params[2] = GLYPH_BBOX.xMin >> 6;
params[3] = GLYPH_BBOX.yMin >> 6;
params[4] = GLYPH_BBOX.xMax >> 6;
params[5] = GLYPH_BBOX.yMax >> 6;
params[2] = GLYPH_BBOX.xMin;
params[3] = GLYPH_BBOX.yMin;
params[4] = GLYPH_BBOX.xMax;
params[5] = GLYPH_BBOX.yMax;

GrComment( gstate, &params, NUM_PARAMS );
GrComment( gstate, params, NUM_PARAMS * sizeof( word ) );
}


Expand Down

0 comments on commit e4e9d21

Please sign in to comment.