-
Notifications
You must be signed in to change notification settings - Fork 45
Implement an internal generator for FiggleFonts class #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46c1eb5
Use source generator to generate the built-in fonts
jonathanou 5d993d6
Generate sample text in the font property's documentation
jonathanou 0131d1e
Add header and documentation
jonathanou d2e77c4
Add one more comment
jonathanou 39d8a5a
Fix grammer
jonathanou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <IsRoslynComponent>true</IsRoslynComponent> | ||
| <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.Analyzers"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" /> | ||
| <PackageReference Include="IsExternalInit" PrivateAssets="all" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Figgle\Figgle.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // Copyright Drew Noakes. Licensed under the Apache-2.0 license. See the LICENSE file for more details. | ||
|
|
||
| using System; | ||
| using System.Collections.Immutable; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Security; | ||
| using System.Text; | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Figgle.Fonts.Generator; | ||
|
|
||
| /// <summary> | ||
| /// This is an internal source generator that populates the FiggleFonts class with properties for each bundled font. | ||
| /// It is not intended for public use, but rather to automate the generation of the FiggleFonts class | ||
| /// </summary> | ||
| [Generator(LanguageNames.CSharp)] | ||
| internal sealed class FiggleFontGenerator : IIncrementalGenerator | ||
| { | ||
| private static readonly string[] _newLineCharacters = ["\r\n", "\n"]; | ||
|
|
||
| private static readonly DiagnosticDescriptor _errorParsingFontFileDiagnostic = new( | ||
| "FIGGLE_IMPL001", | ||
| "Font Parsing Error", | ||
| "Failed to parse font '{0}'. Ensure it is a valid FIGlet font file.", | ||
| "FiggleFonts", | ||
| DiagnosticSeverity.Error, | ||
| isEnabledByDefault: true); | ||
|
|
||
| private const string Header = | ||
| """ | ||
| // Copyright Drew Noakes. Licensed under the Apache-2.0 license. See the LICENSE file for more details. | ||
|
|
||
| // <auto-generated> | ||
| // This code was generated by Figgle.Fonts.Generator. | ||
| // | ||
| // Changes to this file may cause incorrect behavior and will be lost if | ||
| // the code is regenerated. | ||
| // </auto-generated> | ||
|
|
||
| """; | ||
|
|
||
| public void Initialize(IncrementalGeneratorInitializationContext context) | ||
| { | ||
| var fontMemberNamesProvider = context.AdditionalTextsProvider | ||
| .Where(file => Path.GetFileName(file.Path).Equals("Aliases.csv", StringComparison.OrdinalIgnoreCase)) | ||
| .Select(static (file, cancellationToken) => | ||
| { | ||
| var csvFileContent = file.GetText(cancellationToken)?.ToString(); | ||
| if (csvFileContent is null) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| var fontInfos = ImmutableArray.CreateBuilder<FontInfo>(); | ||
| var entries = csvFileContent.Split(_newLineCharacters, StringSplitOptions.RemoveEmptyEntries); | ||
| foreach (var entry in entries) | ||
| { | ||
| var components = entry.Split(','); | ||
| if (components.Length == 2) | ||
| { | ||
| var fontName = components[0].Trim(); | ||
| var memberName = components[1].Trim(); | ||
| fontInfos.Add(new FontInfo(fontName, memberName)); | ||
| } | ||
| } | ||
|
|
||
| return fontInfos.ToImmutable(); | ||
| }); | ||
|
|
||
| var parsedFontsProvider = context.AdditionalTextsProvider | ||
| .Where(file => file.Path.EndsWith(".flf", StringComparison.OrdinalIgnoreCase)) | ||
| .Select(static (file, cancellationToken) => | ||
| { | ||
| var fontContent = file.GetText(cancellationToken)?.ToString(); | ||
| return new ParsedFont( | ||
| Path.GetFileNameWithoutExtension(file.Path), | ||
| fontContent is null ? null : FiggleFontParser.ParseString(fontContent)); | ||
| }) | ||
| .Collect(); | ||
|
|
||
| context.RegisterSourceOutput(fontMemberNamesProvider.Combine(parsedFontsProvider), (context, pair) => | ||
| { | ||
| var fontInfos = pair.Left; | ||
| var parsedFonts = pair.Right.ToImmutableDictionary( | ||
| keySelector: f => f.Name, | ||
| elementSelector: f => f.Font); | ||
|
|
||
| foreach (var kvp in parsedFonts) | ||
| { | ||
| if (kvp.Value is null) | ||
| { | ||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| _errorParsingFontFileDiagnostic, | ||
| Location.None, | ||
| kvp.Key)); | ||
| } | ||
| } | ||
|
|
||
| var source = $$""" | ||
| {{Header}} | ||
| namespace Figgle.Fonts; | ||
|
|
||
| partial class FiggleFonts | ||
| {{{RenderFiggleFontProperties(fontInfos, parsedFonts)}} | ||
| } | ||
| """; | ||
|
|
||
| // we expect only a single Alias.csv file, so we don't need to worry about making | ||
| // the source file name unique. | ||
| context.AddSource("FiggleFonts.g.cs", source); | ||
|
|
||
| static string RenderFiggleFontProperties( | ||
| ImmutableArray<FontInfo> fontInfos, | ||
| ImmutableDictionary<string, FiggleFont?> parsedFonts) | ||
| { | ||
| var builder = new StringBuilder(capacity: 4096); | ||
| var indentation = new string(' ', 4); | ||
| foreach (var fontInfo in fontInfos) | ||
| { | ||
| builder.Append($$""" | ||
|
|
||
|
|
||
| {{indentation}}/// <summary> | ||
| {{indentation}}/// Obtains the <see cref="Figgle.FiggleFont" /> for the font name "{{fontInfo.FontName}}". | ||
| {{indentation}}/// <example> | ||
| {{indentation}}/// <code> | ||
| {{indentation}}{{RenderSampleText(fontInfo.FontName, parsedFonts[fontInfo.FontName], indentation)}} | ||
| {{indentation}}/// </code> | ||
| {{indentation}}/// </example> | ||
| {{indentation}}/// </summary> | ||
| {{indentation}}public static FiggleFont {{fontInfo.MemberName}} => GetByName("{{fontInfo.FontName}}"); | ||
| """); | ||
| } | ||
|
|
||
| return builder.ToString(); | ||
| } | ||
|
|
||
| static string RenderSampleText(string fontName, FiggleFont? font, string indentation) | ||
| { | ||
| if (font is null) | ||
| { | ||
| return $"Failed to parse {fontName} into a {nameof(FiggleFont)}"; | ||
| } | ||
|
|
||
| var renderedText = font.Render(fontName); | ||
|
|
||
| return string.Join( | ||
| $"\r\n{indentation}", | ||
| renderedText | ||
| .Split(_newLineCharacters, StringSplitOptions.None) | ||
| .Select(line => $"/// {EscapeXmlSpecialCharacters(line)}")); | ||
| } | ||
|
|
||
| static string EscapeXmlSpecialCharacters(string text) | ||
| { | ||
| // SecurityElement conveniently has Escape that escapes XML special | ||
| // characters, so we can just reuse it. | ||
| return SecurityElement.Escape(text); | ||
|
drewnoakes marked this conversation as resolved.
|
||
| } | ||
| }); | ||
| } | ||
|
|
||
| private sealed record FontInfo(string FontName, string MemberName); | ||
|
|
||
| private sealed record ParsedFont(string Name, FiggleFont? Font); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| 1row,OneRow | ||
| 3-d,ThreeD | ||
| 3d_diagonal,ThreeDDiagonal | ||
| 3x5,ThreeByFive | ||
| 4max,FourMax | ||
| 5lineoblique,FiveLineOblique | ||
| amc3line,Amc3Line | ||
| amc3liv1,Amc3Liv1 | ||
| amcaaa01,AmcAaa01 | ||
| amcneko,AmcNeko | ||
| amcrazo2,AmcRazor2 | ||
| amcrazor,AmcRazor | ||
| amcslash,AmcSlash | ||
| amcslder,AmcSlder | ||
| amcthin,AmcThin | ||
| amctubes,AmcTubes | ||
| amcun1,AmcUn1 | ||
| barbwire,BarbWire | ||
| bigchief,BigChief | ||
| bigfig,BigFig | ||
| broadway_kb,BroadwayKB | ||
| calgphy2,Caligraphy2 | ||
| catwalk,CatWalk | ||
| cyberlarge,CyberLarge | ||
| cybermedium,CyberMedium | ||
| cybersmall,CyberSmall | ||
| dancingfont,DancingFont | ||
| defleppard,DefLeppard | ||
| dietcola,DietCola | ||
| dosrebel,DosRebel | ||
| dotmatrix,DotMatrix | ||
| doubleshorts,DoubleShorts | ||
| drpepper,DRPepper | ||
| dwhistled,DWhistled | ||
| eftichess,EftiChess | ||
| eftifont,EftiFont | ||
| eftipiti,EftiPiti | ||
| eftirobot,EftiRobot | ||
| eftitalic,EftiItalic | ||
| eftiwall,EftiWall | ||
| eftiwater,EftiWater | ||
| flowerpower,FlowerPower | ||
| fourtops,FourTops | ||
| funface,FunFace | ||
| funfaces,FunFaces | ||
| georgi16,Georgia16 | ||
| Georgia11,Georgia11 | ||
| graffiti,Graffiti | ||
| henry3d,Henry3d | ||
| horizontalleft,HorizontalLeft | ||
| horizontalright,HorizontalRight | ||
| impossible,Impossible | ||
| kontoslant,KontoSlant | ||
| larry3d,Larry3d | ||
| lildevil,LilDevil | ||
| lineblocks,LineBlocks | ||
| lockergnome,LockerGnome | ||
| maxfour,MaxFour | ||
| mshebrew210,Mshebrew210 | ||
| nancyj,NancyJ | ||
| nancyj-fancy,NancyJFancy | ||
| nancyj-improved,NancyJImproved | ||
| nancyj-underlined,NancyJUnderlined | ||
| nscript,NScript | ||
| ntgreek,NTGreek | ||
| nvscript,NVScript | ||
| oldbanner,OldBanner | ||
| os2,OS2 | ||
| ogre,Ogre | ||
| peaksslant,PeaksSlant | ||
| rectangles,Rectangles | ||
| rowancap,RowanCap | ||
| santaclara,SantaClara | ||
| sblood,SBlood | ||
| slant,Slant | ||
| slscript,ScriptSlant | ||
| serifcap,SerifCap | ||
| smallcaps,SmallCaps | ||
| smisome1,IsometricSmall | ||
| smkeyboard,KeyboardSmall | ||
| smpoison,PoisonSmall | ||
| smscript,ScriptSmall | ||
| smshadow,ShadowSmall | ||
| smslant,SlantSmall | ||
| smtengwar,TengwarSmall | ||
| standard,Standard | ||
| threepoint,ThreePoint | ||
| ticksslant,TicksSlant | ||
| tinker-toy,TinkerToy | ||
| twopoint,TwoPoint | ||
| usaflag,UsaFlag | ||
| wetletter,WetLetter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.