Skip to content

Commit

Permalink
Merge pull request #1454 from Jacky720/fontlineheight
Browse files Browse the repository at this point in the history
Add LineHeight as a 2023.6 font attribute.
  • Loading branch information
colinator27 authored Aug 17, 2023
2 parents 3e9675d + 4ee5fd5 commit 7b876ad
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
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,10 +116,15 @@
<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.ColumnSpan="2" Margin="3,30,3,3" HorizontalAlignment="Center" Foreground="DarkGray" FontStyle="Italic" TextWrapping="Wrap" TextAlignment="Center">
<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="14" 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>
<Viewbox Grid.Row="14" Grid.ColumnSpan="2" Margin="3" Stretch="Uniform" StretchDirection="DownOnly">
<Viewbox Grid.Row="15" Grid.ColumnSpan="2" Margin="3" Stretch="Uniform" StretchDirection="DownOnly">
<Grid MouseDown="Grid_MouseDown" Cursor="Hand">
<Grid.Background>
<SolidColorBrush Color="Black"/>
Expand Down Expand Up @@ -199,7 +204,7 @@
</Grid>
</Viewbox>

<StackPanel Grid.Row="15" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center">
<StackPanel Grid.Row="16" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center">
<local:ButtonDark Margin="3" Width="220" Click="EditRectangleButton_Click">
<Button.Style>
<Style TargetType="Button">
Expand Down Expand Up @@ -235,8 +240,8 @@
<local:ButtonDark Margin="3" Width="200" Content="Create an empty glyph" Click="CreateGlyphButton_Click"/>
</StackPanel>

<TextBlock Name="GlyphsLabel" Grid.Row="16" Grid.ColumnSpan="2" Margin="3" HorizontalAlignment="Left">Glyphs:</TextBlock>
<Grid Grid.Row="17" Grid.ColumnSpan="2" MaxHeight="370" Margin="3">
<TextBlock Name="GlyphsLabel" Grid.Row="17" Grid.ColumnSpan="2" Margin="3" HorizontalAlignment="Left">Glyphs:</TextBlock>
<Grid Grid.Row="18" Grid.ColumnSpan="2" MaxHeight="370" Margin="3">
<local:DataGridDark x:Name="GlyphsGrid" ItemsSource="{Binding Glyphs, Mode=OneWay}"
AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" HeadersVisibility="Column" SelectionMode="Single" SelectionUnit="FullRow"
ScrollViewer.CanContentScroll="True"
Expand Down Expand Up @@ -441,19 +446,19 @@
</Border>
</Grid>

<TextBlock Grid.Row="18" Grid.ColumnSpan="2" Margin="3" TextWrapping="Wrap" HorizontalAlignment="Center" TextAlignment="Center">
<TextBlock Grid.Row="19" Grid.ColumnSpan="2" Margin="3" TextWrapping="Wrap" HorizontalAlignment="Center" TextAlignment="Center">
Note that the glyphs need to be specified in ascending order.<LineBreak/>
Press the button below to sort them appropriately.
</TextBlock>
<local:ButtonDark Grid.Row="19" Grid.ColumnSpan="2" Margin="3" Click="Button_Sort_Click"
<local:ButtonDark Grid.Row="20" Grid.ColumnSpan="2" Margin="3" Click="Button_Sort_Click"
Content="Sort glyphs" Width="200" FontSize="14" Style="{StaticResource glyphsOperationButtonStyle}"/>

<TextBlock Grid.Row="20" Grid.ColumnSpan="2" Margin="3,6,3,3" TextWrapping="Wrap" HorizontalAlignment="Center" TextAlignment="Center">
<TextBlock Grid.Row="21" Grid.ColumnSpan="2" Margin="3,6,3,3" TextWrapping="Wrap" HorizontalAlignment="Center" TextAlignment="Center">
Also, if you have added new characters or changed the character<LineBreak/>
of an existing glyph, you should update the font range.<LineBreak/>
Press the button below to do that.
</TextBlock>
<local:ButtonDark Grid.Row="21" Grid.ColumnSpan="2" Margin="3" Click="Button_UpdateRange_Click"
<local:ButtonDark Grid.Row="22" Grid.ColumnSpan="2" Margin="3" Click="Button_UpdateRange_Click"
Content="Update range" Width="200" FontSize="14" Style="{StaticResource glyphsOperationButtonStyle}"/>
</Grid>
</local:DataUserControl>

0 comments on commit 7b876ad

Please sign in to comment.