Skip to content

Commit c3560e7

Browse files
committed
Editor: per-Font "logical height" property, support custom user value
This moves a "TTF fonts height used in the game logic" from General Settings to Font, therefore allowing to set this value individually per each Font. Add a new "font definition" type: a custom user-defined value, useful to quickly fixup weird fonts. Remove "default TTF font adjustment" from General Settings, leave only one in Font.
1 parent 3850116 commit c3560e7

File tree

18 files changed

+119
-63
lines changed

18 files changed

+119
-63
lines changed

Common/ac/gamestructdefines.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,20 @@ enum SpeechStyle
134134
// Contemporary font flags
135135
#define FFLG_SIZEMULTIPLIER 0x01 // size data means multiplier
136136
#define FFLG_DEFLINESPACING 0x02 // linespacing derived from the font height
137-
// Font load flags, primarily for backward compatibility:
138-
// REPORTNOMINALHEIGHT: get_font_height should return nominal font's height,
139-
// eq to "font size" parameter, otherwise returns real pixel height.
140-
#define FFLG_REPORTNOMINALHEIGHT 0x04
141-
// ASCENDFIXUP: do the TTF ascender fixup, where font's ascender is resized
142-
// to the nominal font's height.
137+
// Use nominal font's import size as a font's height in all the game logic.
138+
// If not set will use real font's height reported by the font file.
139+
#define FFLG_LOGICALNOMINALHEIGHT 0x04
140+
// Do the TTF ascender fixup, where font's ascender is resized
141+
// to the nominal font's height. This is primarily for backwards compatibility
142+
// with older games.
143143
#define FFLG_ASCENDERFIXUP 0x08
144+
// Use a user-provided custom value as a font's height in all the game logic.
145+
// If not set will use real font's height reported by the font file.
146+
#define FFLG_LOGICALCUSTOMHEIGHT 0x10
144147
// Collection of flags defining fully backward compatible TTF fixup
145-
#define FFLG_TTF_BACKCOMPATMASK (FFLG_REPORTNOMINALHEIGHT | FFLG_ASCENDERFIXUP)
148+
#define FFLG_TTF_BACKCOMPATMASK (FFLG_LOGICALNOMINALHEIGHT | FFLG_ASCENDERFIXUP)
146149
// Collection of flags defining font's load mode
147-
#define FFLG_LOADMODEMASK (FFLG_REPORTNOMINALHEIGHT | FFLG_ASCENDERFIXUP)
150+
#define FFLG_LOADMODEMASK (FFLG_LOGICALNOMINALHEIGHT | FFLG_LOGICALCUSTOMHEIGHT | FFLG_ASCENDERFIXUP)
148151
// Font outline types
149152
#define FONT_OUTLINE_NONE -1
150153
#define FONT_OUTLINE_AUTO -10
@@ -306,7 +309,10 @@ struct FontInfo
306309
int SizeMultiplier;
307310
// Outlining font index, or auto-outline flag
308311
int Outline;
309-
// Custom vertical render offset, used mainly for fixing broken fonts
312+
// Custom logical height (referred to when measuring text),
313+
// used mainly for handling broken fonts
314+
int CustomHeight;
315+
// Custom vertical render offset, used mainly for handling broken fonts
310316
int YOffset;
311317
// Custom line spacing between two lines of text (0 = use font height)
312318
int LineSpacing;

Common/font/agsfontrenderer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ struct FontMetrics
8383
// Real font's height, equals to reported ascender + descender.
8484
// This is what you normally think as a font's height.
8585
int RealHeight = 0;
86-
// Compatible height, equals to either NominalHeight or RealHeight,
86+
// Custom height value provided by user.
87+
// Meant to be used as a "logical height" in case of bad font data.
88+
int CustomHeight = 0;
89+
// Logical height, equals to either NominalHeight, RealHeight or CustomHeight,
8790
// selected depending on the game settings.
8891
// This property is used in calculating linespace, etc.
89-
int CompatHeight = 0;
92+
int LogicalHeight = 0;
9093
// Maximal bounding rectangle of a font's glyph, this tells the actual
9194
// graphical bounds that may be occupied by any font's glyphs.
9295
// BBox is given in pixels, relative to our "pen" position (top-left).

Common/font/fonts.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,15 @@ static void font_post_init(int font_number)
156156
font.Metrics.RealHeight = font.Metrics.NominalHeight;
157157
font.Metrics.VExtent = std::make_pair(0, font.Metrics.RealHeight - 1);
158158
}
159-
// Use either nominal or real pixel height to define font's logical height
159+
// Use either nominal, real pixel height or a user-provided value to define font's logical height
160160
// and default linespacing; logical height = nominal height is compatible with the old games
161-
font.Metrics.CompatHeight = (font.Info.Flags & FFLG_REPORTNOMINALHEIGHT) != 0 ?
162-
font.Metrics.NominalHeight : font.Metrics.RealHeight;
161+
font.Metrics.CustomHeight = font.Info.CustomHeight;
162+
if ((font.Info.Flags & FFLG_LOGICALNOMINALHEIGHT) != 0)
163+
font.Metrics.LogicalHeight = font.Metrics.NominalHeight;
164+
else if ((font.Info.Flags & FFLG_LOGICALCUSTOMHEIGHT) != 0)
165+
font.Metrics.LogicalHeight = font.Metrics.CustomHeight;
166+
else
167+
font.Metrics.LogicalHeight = font.Metrics.RealHeight;
163168

164169
if (font.Info.Outline != FONT_OUTLINE_AUTO)
165170
{
@@ -182,7 +187,7 @@ static void font_post_init(int font_number)
182187
{
183188
// Calculate default linespacing from the font height + outline thickness.
184189
font.Info.Flags |= FFLG_DEFLINESPACING;
185-
font.LineSpacingCalc = font.Metrics.CompatHeight + 2 * font.Info.AutoOutlineThickness;
190+
font.LineSpacingCalc = font.Metrics.LogicalHeight + 2 * font.Info.AutoOutlineThickness;
186191
}
187192
}
188193

@@ -368,7 +373,7 @@ int get_font_height(int font_number)
368373
{
369374
if (!assert_font_number(font_number))
370375
return 0;
371-
return fonts[font_number].Metrics.CompatHeight;
376+
return fonts[font_number].Metrics.LogicalHeight;
372377
}
373378

374379
// Returns a max value between the chosen font height (this may be a compat height,
@@ -377,15 +382,15 @@ static int get_font_height_with_outline(int font_number, bool surf_height = fals
377382
{
378383
const int self_height = surf_height ?
379384
fonts[font_number].Metrics.ExtentHeight() :
380-
fonts[font_number].Metrics.CompatHeight;
385+
fonts[font_number].Metrics.LogicalHeight;
381386
const int outline = fonts[font_number].Info.Outline;
382387
if (outline < 0 || static_cast<uint32_t>(outline) >= fonts.size())
383388
{ // FONT_OUTLINE_AUTO or FONT_OUTLINE_NONE
384389
return self_height + 2 * fonts[font_number].Info.AutoOutlineThickness;
385390
}
386391
const int outline_height = surf_height ?
387392
fonts[outline].Metrics.ExtentHeight() :
388-
fonts[outline].Metrics.CompatHeight;
393+
fonts[outline].Metrics.LogicalHeight;
389394
return std::max(self_height, outline_height);
390395
}
391396

@@ -396,6 +401,13 @@ int get_font_height_outlined(int font_number)
396401
return get_font_height_with_outline(font_number);
397402
}
398403

404+
int get_font_real_height(int font_number)
405+
{
406+
if (!assert_font_number(font_number))
407+
return 0;
408+
return fonts[font_number].Metrics.RealHeight;
409+
}
410+
399411
int get_font_surface_height(int font_number)
400412
{
401413
if (!assert_font_number(font_number))

Common/font/fonts.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ int get_text_width_outlined(const char *text, int font_number);
6161
// note that this won't be a nominal font's height, but the max of each met glyph's graphical height.
6262
int get_text_height(const char *text, int font_number);
6363
// Get font's height; this value is used for logical arrangement of UI elements;
64-
// note that this is a "formal" font height, that may have different value
65-
// depending on compatibility mode (used when running old games);
64+
// note that this value may depend on font settings, and return either nominal
65+
// import size, or real font's height, or a user-supplied custom value.
6666
int get_font_height(int font_number);
6767
// Get the height of the given font with corresponding outlining
6868
int get_font_height_outlined(int font_number);
69+
// Get the real dont's height as reported by the font file
70+
int get_font_real_height(int font_number);
6971
// Get font's surface height: this always returns the height enough to accomodate
7072
// any font's letters (glyphs) on a bitmap or a texture
7173
int get_font_surface_height(int font_number);

Common/font/ttffontrenderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int TTFFontRenderer::GetTextHeight(const char * /*text*/, int fontNumber)
5757
{
5858
// Compatibility mode: if we required to return "nominal font height",
5959
// then ask alfont to return one the font was loaded with
60-
if ((_fontData[fontNumber].Params.LoadMode & FFLG_REPORTNOMINALHEIGHT) != 0)
60+
if ((_fontData[fontNumber].Params.LoadMode & FFLG_LOGICALNOMINALHEIGHT) != 0)
6161
return alfont_get_font_height(_fontData[fontNumber].AlFont);
6262
else
6363
return alfont_get_font_real_height(_fontData[fontNumber].AlFont);
@@ -116,7 +116,7 @@ static void FillMetrics(ALFONT_FONT *alfptr, FontMetrics *metrics)
116116
{
117117
metrics->NominalHeight = alfont_get_font_height(alfptr);
118118
metrics->RealHeight = alfont_get_font_real_height(alfptr);
119-
metrics->CompatHeight = metrics->NominalHeight; // just set to default here
119+
metrics->LogicalHeight = metrics->NominalHeight; // just set to default here
120120
{
121121
Rect ttf_bbox;
122122
alfont_get_font_bbox(alfptr, &ttf_bbox.Left, &ttf_bbox.Top, &ttf_bbox.Right, &ttf_bbox.Bottom);

Common/font/wfnfontrenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void WFNFontRenderer::GetFontMetrics(int fontNumber, FontMetrics *metrics)
179179
const char *height_test_string = "ZHwypgfjqhkilIK";
180180
metrics->NominalHeight = GetTextHeight(height_test_string, fontNumber);
181181
metrics->RealHeight = font->GetHeight() * size_mult;
182-
metrics->CompatHeight = metrics->NominalHeight; // just set to default here
182+
metrics->LogicalHeight = metrics->NominalHeight; // just set to default here
183183
metrics->BBox = font->GetBBox() * size_mult;
184184
metrics->VExtent = std::make_pair(metrics->BBox.Top, metrics->BBox.Bottom);
185185
// fix it up to be *not less* than realheight

Common/game/main_game_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,9 @@ HError GameDataExtReader::ReadBlock(Stream *in, int /*block_id*/, const String &
863863
static_cast<enum FontInfo::AutoOutlineStyle>(in->ReadInt32());
864864
// since kGameVersion_363
865865
finfo.CharacterSpacing = in->ReadInt32();
866+
finfo.CustomHeight = in->ReadInt32();
866867
in->ReadInt32(); // reserved
867868
in->ReadInt32();
868-
in->ReadInt32();
869869
}
870870
}
871871
else if (ext_id.CompareNoCase("v360_cursors") == 0)

Editor/AGS.Editor/AGSEditor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ public class AGSEditor
115115
* 3.6.2.6 - Settings.GameFPS.
116116
* 3.6.2.9 - Sprite.TransparentColorIndex (can select transparent palette index).
117117
* 3.6.3 - Settings.GUIHandleOnlyLeftMouseButton, Font.CharacterSpacing.
118+
* 3.6.3.3 - Moved FontHeightDefinition property from General Settings to Font.
118119
*/
119-
public const int LATEST_XML_VERSION_INDEX = 3060300;
120+
public const int LATEST_XML_VERSION_INDEX = 3060303;
120121
/*
121122
* LATEST_USER_DATA_VERSION is the last version of the user data file that used a
122123
* 4-point-4-number string to identify the version of AGS that saved the file.

Editor/AGS.Editor/Components/FontsComponent.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public override void CommandClick(string controlID)
5151
newItem.PointSize = copyFrom.PointSize;
5252
newItem.SourceFilename = Utilities.GetRelativeToProjectPath(copyFrom.SourceFilename);
5353
newItem.ProjectFilename = $"agsfnt{newItem.ID}.{Path.GetExtension(copyFrom.ProjectFilename)}";
54-
newItem.TTFMetricsFixup = _agsEditor.CurrentGame.Settings.TTFMetricsFixup; // use defaults
5554
items.Add(newItem);
5655
Utilities.CopyFont(copyFrom.ID, newItem.ID);
5756
Factory.NativeProxy.OnFontAdded(_agsEditor.CurrentGame, newItem.ID);

Editor/AGS.Editor/DataFileWriter.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,8 +1484,10 @@ public static bool SaveThisGameToFile(string fileName, Game game, CompileMessage
14841484
int flags = 0;
14851485
if (game.Fonts[i].PointSize == 0)
14861486
flags |= NativeConstants.FFLG_SIZEMULTIPLIER;
1487-
if (game.Settings.TTFHeightDefinedBy == FontHeightDefinition.NominalHeight)
1488-
flags |= NativeConstants.FFLG_REPORTNOMINALHEIGHT;
1487+
if (game.Fonts[i].HeightDefinedBy == FontHeightDefinition.NominalHeight)
1488+
flags |= NativeConstants.FFLG_LOGICALNOMINALHEIGHT;
1489+
if (game.Fonts[i].HeightDefinedBy == FontHeightDefinition.CustomValue)
1490+
flags |= NativeConstants.FFLG_LOGICALCUSTOMHEIGHT;
14891491
if (game.Fonts[i].TTFMetricsFixup == FontMetricsFixup.SetAscenderToHeight)
14901492
flags |= NativeConstants.FFLG_ASCENDERFIXUP;
14911493
writer.Write(flags);
@@ -1838,9 +1840,9 @@ private static void WriteExt_360Fonts(BinaryWriter writer, WriteExtEntities ents
18381840
writer.Write((int)game.Fonts[i].AutoOutlineStyle);
18391841
// Since 3.6.3
18401842
writer.Write(game.Fonts[i].CharacterSpacing);
1843+
writer.Write(game.Fonts[i].CustomHeightValue);
18411844
writer.Write((int)0); // reserved
18421845
writer.Write((int)0);
1843-
writer.Write((int)0);
18441846
}
18451847
}
18461848

0 commit comments

Comments
 (0)