Make the lookup case insensitive
CodeBendor opened this issue · comments
private static FiggleFont FontFactory(string name)
{
using var stream = typeof(FiggleFonts).GetTypeInfo().Assembly.GetManifestResourceStream("Figgle.Fonts.zip");
using var zip = new ZipArchive(stream, ZipArchiveMode.Read);
var entry = zip.Entries.ToDictionary(x=> x.FullName, x => x)
.FirstOrDefault(x => x.Key.ToLowerInvariant().Replace(" ", string.Empty) == (name+".flf").ToLowerInvariant().Replace(" ", string.Empty)).Value;
if (entry == null)
throw new FiggleException($"No embedded font exists with name \"{name}\".");
using var entryStream = entry.Open();
return FiggleFontParser.Parse(entryStream, _stringPool);
}
public static FiggleFont Parse(string name)
{
var x = FontFactory(name);
return x;
}
This adds a public Parse
method. I'm curious why you need that, given all fonts embedded within the assembly are already exposed as properties on this type.
The main point of the comment was to make the lookup within the zip file case insensitive (even space insensitive).
Parse was just a suggestion... I used it in my command line version of the code. All the predefined lookups in the FiggleFonts class felt unnecessarily cumbersome. If the fonts are in the zip file, return a result, if not punt.
public static FiggleFont SomeName => Lookup("somename");
Why? I wanted to use more fonts that I'd found and not have to predefined them. So I just wanted a quick way to look them up:
// simplified code
public static void Main(string[] args) {
string message = args[0];
string font = args[1];
try {
var ff = FiggleFonts.Parse(font); // maybe there was another way to do this...
Console.WriteLine(ff.Render(message));
} catch(Exception ex) {
Console.WriteLine($"Exception: {ex.Message}");
Console.WriteLine($"Couldn't find font {font}");
}
}
I used it in my command line version of the code
That makes sense.
I wanted to use more fonts that I'd found and not have to predefined them
I'd expect you to do that with FiggleFontParser
directly. FiggleFonts
is intended to make it easy to access the bundled fonts. Perhaps the readme can be extended with an example that shows this.
An example of reading custom fonts was added to the README some time ago.
https://github.com/drewnoakes/figgle#loading-external-fonts
I'll close this out.