Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Name same-height .fon styles by style rather than a number

SeoJCalive0f5627989cb3
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index e15a3775..c4885863 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -486,8 +486,9 @@ extension, and the file must then have an NE header with FNT resources.
executable and turns each lit pixel into a straight, grid-aligned TrueType outline. The result retains the
original pixel shape, per-glyph advance, ascent and baseline; there is no smoothing or curve tracing. A FON with a
single font resource produces `Name.ttf` (or the corresponding OTF/WOFF/WOFF2/EOT extension). Several resources
- produce one file per strike, such as `Name-13px.ttf`, `Name-13px-2.ttf` and `Name-14px.ttf`; the font family names
- also distinguish the sizes and repeated heights. Existing output files follow the usual Skip, Rename and Overwrite
+ produce one file per strike, such as `Name-13px.ttf`, `Name-13px-Bold.ttf`, `Name-13px-2.ttf` and
+ `Name-14px.ttf`; the font family names distinguish heights, group different styles at the same height, and number
+ only repeated styles at the same height. Existing output files follow the usual Skip, Rename and Overwrite
policies independently for each strike. Invalid resources fail the whole input before output files are allocated.
Character mapping uses the Windows single-byte code page indicated by the resource charset, not the computer's
locale. Undefined bytes (including those that Windows decodes to Private Use code points in the Greek, Hebrew,
diff --git a/src/Filee.Engines/Fonts/FonOutlineBuilder.cs b/src/Filee.Engines/Fonts/FonOutlineBuilder.cs
index e4121873..1f95a694 100644
--- a/src/Filee.Engines/Fonts/FonOutlineBuilder.cs
+++ b/src/Filee.Engines/Fonts/FonOutlineBuilder.cs
@@ -91,7 +91,8 @@ internal static class FonOutlineBuilder
var baseName = resourceCount == 1 ? strike.Face :
$"{strike.Face} {height}px{(sameHeightOrdinal > 1 ? $"-{sameHeightOrdinal}" : "")}";
var family = weight is 400 or 700 ? baseName : $"{baseName} Weight{weight}";
- var style = (bold, strike.Italic) switch
+ var nameBold = weight == 700;
+ var style = (nameBold, strike.Italic) switch
{
(true, true) => "Bold Italic",
(true, false) => "Bold",
diff --git a/src/Filee.Engines/Fonts/FontConverter.cs b/src/Filee.Engines/Fonts/FontConverter.cs
index 32a875a1..527a9560 100644
--- a/src/Filee.Engines/Fonts/FontConverter.cs
+++ b/src/Filee.Engines/Fonts/FontConverter.cs
@@ -53,14 +53,15 @@ public sealed class FontConverter : IConverter
cancellationToken.ThrowIfCancellationRequested();
progress?.Report(0.1);
- // Same-height ordinals and the resource count are fixed now, so skipped outputs never rename the others.
+ // Same-height, same-style ordinals and the resource count are fixed now, so skipped outputs never rename the others.
var ordinals = new int[strikes.Count];
- var counts = new Dictionary<int, int>();
+ var counts = new Dictionary<(int Height, int Weight, bool Italic), int>();
for (var i = 0; i < strikes.Count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
- var height = strikes[i].PixelHeight;
- ordinals[i] = counts[height] = counts.GetValueOrDefault(height) + 1;
+ var strike = strikes[i];
+ var key = (strike.PixelHeight, strike.Weight, strike.Italic);
+ ordinals[i] = counts[key] = counts.GetValueOrDefault(key) + 1;
}
var results = new byte[strikes.Count][];
@@ -76,8 +77,18 @@ public sealed class FontConverter : IConverter
var outputs = new List<string>();
for (var i = 0; i < strikes.Count; i++)
{
+ var strike = strikes[i];
+ var style = (strike.Weight, strike.Italic) switch
+ {
+ (400, false) => "",
+ (700, false) => "-Bold",
+ (400, true) => "-Italic",
+ (700, true) => "-BoldItalic",
+ (_, false) => $"-Weight{strike.Weight}",
+ (_, true) => $"-Weight{strike.Weight}Italic",
+ };
var suffix = strikes.Count == 1 ? null
- : $"-{strikes[i].PixelHeight}px{(ordinals[i] > 1 ? $"-{ordinals[i]}" : "")}";
+ : $"-{strike.PixelHeight}px{style}{(ordinals[i] > 1 ? $"-{ordinals[i]}" : "")}";
if (step.Output.Allocate(step.To, suffix) is not { } output)
continue;
File.WriteAllBytes(output, results[i]);
diff --git a/tests/Filee.Engines.Tests/FonBuilder.cs b/tests/Filee.Engines.Tests/FonBuilder.cs
index a5232e13..e8992538 100644
--- a/tests/Filee.Engines.Tests/FonBuilder.cs
+++ b/tests/Filee.Engines.Tests/FonBuilder.cs
@@ -19,7 +19,7 @@ internal static class FonBuilder
}
private static byte[] Fnt(int version, int height = 13, bool fixedWidth = true, int first = 32,
- int last = 127, int? flags = null)
+ int last = 127, int? flags = null, int weight = 400, bool italic = false, string faceName = "Fixed")
{
var count = last - first + 1;
var entrySize = version == 2 ? 4 : flags is null ? 6 : 8 + (BitOperations.Log2((uint)flags.Value) - 2) * 2;
@@ -31,7 +31,8 @@ internal static class FonBuilder
Put(data, 0x42, (ushort)0);
Put(data, 0x4A, (ushort)Math.Min(height, 10));
Put(data, 0x4E, (ushort)2);
- Put(data, 0x53, (ushort)400);
+ Put(data, 0x53, (ushort)weight);
+ data[0x50] = italic ? (byte)1 : (byte)0;
data[0x55] = 0;
Put(data, 0x56, (ushort)(fixedWidth ? 8 : 0));
Put(data, 0x58, (ushort)height);
@@ -51,7 +52,7 @@ internal static class FonBuilder
}
}
for (var i = 0; i < 6; i++)
- data[face + i] = "Fixed\0"u8[i];
+ data[face + i] = i < faceName.Length ? (byte)faceName[i] : (byte)0;
for (var index = 0; index < count; index++)
{
@@ -158,6 +159,13 @@ internal static class FonBuilder
public static byte[] V3Proportional() => Container([Fnt(3, fixedWidth: false)]);
/// <summary>Builds three strikes, two at the same height.</summary>
public static byte[] Multi() => Container([Fnt(2), Fnt(3), Fnt(3, 14)]);
+ /// <summary>Builds mixed styles and duplicate style strikes at two heights.</summary>
+ public static byte[] MixedStyles() => Container([
+ Fnt(3, faceName: "Name"), Fnt(3, weight: 700, faceName: "Name"), Fnt(3, italic: true, faceName: "Name"),
+ Fnt(3, weight: 700, italic: true, faceName: "Name"), Fnt(3, weight: 600, faceName: "Name"),
+ Fnt(3, weight: 600, italic: true, faceName: "Name"), Fnt(3, faceName: "Name"),
+ Fnt(3, weight: 700, faceName: "Name"), Fnt(3, 14, faceName: "Name")
+ ]);
/// <summary>Builds the one- and 256-pixel strikes.</summary>
public static byte[] Edge() => Container([Fnt(3, 1, first: 32, last: 34), Fnt(3, 256, first: 32, last: 34)]);
/// <summary>Builds a v3 resource with the given unsupported glyph table flags.</summary>
diff --git a/tests/Filee.Engines.Tests/FonTests.cs b/tests/Filee.Engines.Tests/FonTests.cs
index 723afedc..6e2f157f 100644
--- a/tests/Filee.Engines.Tests/FonTests.cs
+++ b/tests/Filee.Engines.Tests/FonTests.cs
@@ -620,6 +620,33 @@ public class FonTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal(3, names.Select(n => n.Item3).Distinct().Count());
}
+ [Fact]
+ public async Task Multi_resource_same_height_styles_have_style_names_and_ordinals()
+ {
+ var source = Source(FonBuilder.MixedStyles(), out var folder);
+ var job = await fx.ConvertAsync([source], PresetFor("ttf"));
+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ var expected = new[]
+ {
+ ("Name-13px.ttf", "Name 13px", "Regular"),
+ ("Name-13px-Bold.ttf", "Name 13px", "Bold"),
+ ("Name-13px-Italic.ttf", "Name 13px", "Italic"),
+ ("Name-13px-BoldItalic.ttf", "Name 13px", "Bold Italic"),
+ ("Name-13px-Weight600.ttf", "Name 13px Weight600", "Regular"),
+ ("Name-13px-Weight600Italic.ttf", "Name 13px Weight600", "Italic"),
+ ("Name-13px-2.ttf", "Name 13px-2", "Regular"),
+ ("Name-13px-Bold-2.ttf", "Name 13px-2", "Bold"),
+ ("Name-14px.ttf", "Name 14px", "Regular"),
+ };
+ Assert.Equal(expected.Select(e => Path.Combine(folder, e.Item1)), job.Outputs);
+ foreach (var (file, family, style) in expected)
+ {
+ var font = FontFile.Load(File.ReadAllBytes(Path.Combine(folder, file)), TestContext.Current.CancellationToken);
+ Assert.Equal(family, Name(font, 1));
+ Assert.Equal(style, Name(font, 2));
+ }
+ }
+
/// <summary>Hands out <c>Name{suffix}.{ext}</c> paths in a folder and records each request.</summary>
private sealed class RecordingAllocator(string folder) : IOutputAllocator
{
@@ -649,10 +676,10 @@ public class FonTests(EngineFixture fx) : IClassFixture<EngineFixture>
var outputs = await new FontConverter().ConvertAsync(
new ConversionStep(source, "fon", "ttf", PresetFor("ttf"), allocator, fx.NewFolder()), null, TestContext.Current.CancellationToken);
- Assert.Equal(["-13px", "-13px-2", "-14px"], allocator.Suffixes);
+ Assert.Equal(["-13px", "-13px-BoldItalic", "-14px"], allocator.Suffixes);
Assert.Equal(3, outputs.Count);
var fonts = outputs.Select(o => FontFile.Load(File.ReadAllBytes(o))).ToArray();
- Assert.Equal(["Fixed 13px", "Fixed 13px-2", "Fixed 14px"], fonts.Select(f => Name(f, 1)));
+ Assert.Equal(["Fixed 13px", "Fixed 13px", "Fixed 14px"], fonts.Select(f => Name(f, 1)));
Assert.Equal(["Regular", "Bold Italic", "Regular"], fonts.Select(f => Name(f, 2)));
Assert.Equal(3, outputs.Select(o => Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(o)))).Distinct().Count());
Assert.Equal(fonts[0].Require("glyf"), fonts[1].Require("glyf")); // same pixels, still two fonts