Skip to content

Commit

Permalink
Add LineHeight as a 2023.6 font attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacky720 committed Aug 5, 2023
1 parent 3e9675d commit 43195b1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
11 changes: 11 additions & 0 deletions UndertaleModLib/Models/UndertaleFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public class UndertaleFont : UndertaleNamedResource, IDisposable
/// <value><c>0</c> if SDF is disabled for this font.</value>
public uint SDFSpread { get; set; }

/// <remarks>
/// Was introduced in GM 2023.6.
/// </remarks>
public uint LineHeight { get; set; }

/// <summary>
/// The glyphs that this font uses.
/// </summary>
Expand Down Expand Up @@ -292,6 +297,8 @@ public void Serialize(UndertaleWriter writer)
writer.Write(Ascender);
if (writer.undertaleData.IsVersionAtLeast(2023, 2))
writer.Write(SDFSpread);
if (writer.undertaleData.IsVersionAtLeast(2023, 6))
writer.Write(LineHeight);
writer.WriteUndertaleObject(Glyphs);
}

Expand Down Expand Up @@ -326,6 +333,8 @@ public void Unserialize(UndertaleReader reader)
Ascender = reader.ReadUInt32();
if (reader.undertaleData.IsVersionAtLeast(2023, 2))
SDFSpread = reader.ReadUInt32();
if (reader.undertaleData.IsVersionAtLeast(2023, 6))
LineHeight = reader.ReadUInt32();
Glyphs = reader.ReadUndertaleObject<UndertalePointerList<Glyph>>();
}

Expand All @@ -339,6 +348,8 @@ public static uint UnserializeChildObjectCount(UndertaleReader reader)
skipSize += 4; // Ascender
if (reader.undertaleData.IsVersionAtLeast(2023, 2))
skipSize += 4; // SDFSpread
if (reader.undertaleData.IsVersionAtLeast(2023, 6))
skipSize += 4; // LineHeight

reader.Position += skipSize;

Expand Down
57 changes: 57 additions & 0 deletions UndertaleModLib/UndertaleChunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ public class UndertaleChunkFONT : UndertaleListChunk<UndertaleFont>
public byte[] Padding;

private static bool checkedFor2022_2;
private static bool checkedFor2023_6;
private void CheckForGM2022_2(UndertaleReader reader)
{
/* This code performs four checks to identify GM2022.2.
Expand Down Expand Up @@ -509,6 +510,57 @@ private void CheckForGM2022_2(UndertaleReader reader)
checkedFor2022_2 = true;
}

private void CheckForGM2023_6(UndertaleReader reader)
{
// This is basically the same as the 2022.2 check, but adapted for the LineHeight value instead of Ascender.

// We already know whether the version is more or less than 2023.2 due to PSEM. Checking a shorter range narrows possibility of error.
if (!reader.undertaleData.IsVersionAtLeast(2023, 2) || reader.undertaleData.IsVersionAtLeast(2023, 6))
{
checkedFor2023_6 = true;
return;
}

long positionToReturn = reader.Position;
bool GMS2023_6 = false;

if (reader.ReadUInt32() > 0) // Font count
{
uint firstFontPointer = reader.ReadUInt32();
reader.AbsPosition = firstFontPointer + 56; // Two more values: SDFSpread and LineHeight. 48 + 4 + 4 = 56.
uint glyphsLength = reader.ReadUInt32();
GMS2023_6 = true;
if ((glyphsLength * 4) > this.Length)
{
GMS2023_6 = false;
}
else if (glyphsLength != 0)
{
List<uint> glyphPointers = new List<uint>((int)glyphsLength);
for (uint i = 0; i < glyphsLength; i++)
glyphPointers.Add(reader.ReadUInt32());
foreach (uint pointer in glyphPointers)
{
if (reader.AbsPosition != pointer)
{
GMS2023_6 = false;
break;
}

reader.Position += 14;
ushort kerningLength = reader.ReadUInt16();
reader.Position += (uint)4 * kerningLength; // combining read/write would apparently break
}
}

}
if (GMS2023_6)
reader.undertaleData.SetGMS2Version(2023, 6);
reader.Position = positionToReturn;

checkedFor2023_6 = true;
}

internal override void SerializeChunk(UndertaleWriter writer)
{
base.SerializeChunk(writer);
Expand All @@ -528,6 +580,9 @@ internal override void UnserializeChunk(UndertaleReader reader)
if (!checkedFor2022_2)
CheckForGM2022_2(reader);

if (!checkedFor2023_6)
CheckForGM2023_6(reader);

base.UnserializeChunk(reader);

Padding = reader.ReadBytes(512);
Expand All @@ -536,8 +591,10 @@ internal override void UnserializeChunk(UndertaleReader reader)
internal override uint UnserializeObjectCount(UndertaleReader reader)
{
checkedFor2022_2 = false;
checkedFor2023_6 = false;

CheckForGM2022_2(reader);
CheckForGM2023_6(reader);

return base.UnserializeObjectCount(reader);
}
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModTool/Converters/IsVersionAtLeastConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
if (ver.Groups[3].Value != "")
release = uint.Parse(ver.Groups[3].Value);
if (ver.Groups[4].Value != "")
release = uint.Parse(ver.Groups[4].Value);
build = uint.Parse(ver.Groups[4].Value);

if (mainWindow.Data.IsVersionAtLeast(major, minor, release, build))
return Visibility.Visible;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<local:TextBoxDark Grid.Row="12" Grid.Column="1" Margin="3" Text="{Binding SDFSpread}"
Visibility="{Binding Mode=OneTime, Converter={StaticResource IsVersionAtLeastConverter}, ConverterParameter=2023.2}"/>

<TextBlock Grid.Row="13" Grid.Column="0" Margin="3" Text="Line height"
Visibility="{Binding Mode=OneTime, Converter={StaticResource IsVersionAtLeastConverter}, ConverterParameter=2023.6}"/>
<local:TextBoxDark Grid.Row="13" Grid.Column="1" Margin="3" Text="{Binding LineHeight}"
Visibility="{Binding Mode=OneTime, Converter={StaticResource IsVersionAtLeastConverter}, ConverterParameter=2023.6}"/>

<TextBlock Grid.Row="13" Grid.ColumnSpan="2" Margin="3,30,3,3" HorizontalAlignment="Center" Foreground="DarkGray" FontStyle="Italic" TextWrapping="Wrap" TextAlignment="Center">
Hint: You can click on any glyph here to highlight it in the "Glyphs".
</TextBlock>
Expand Down

0 comments on commit 43195b1

Please sign in to comment.