Skip to content

Support specifying a font name for external font source generation#24

Merged
drewnoakes merged 7 commits intodrewnoakes:masterfrom
jonathanou:font-name-metadata
May 26, 2025
Merged

Support specifying a font name for external font source generation#24
drewnoakes merged 7 commits intodrewnoakes:masterfrom
jonathanou:font-name-metadata

Conversation

@jonathanou
Copy link
Copy Markdown
Collaborator

The source generator can now use external fonts when the consuming project includes the font file as an additional file:

<ItemGroup>
  <AdditionalFiles Include="myfont.flf" />
</ItemGroup>

However, if there are font files with the same file name but under different folders, the source generator will not be able to distinguish which font file to use and will use the first one found.

To make it easier for users to explicitly specify a font name for the source generator to identify, we can define a new item metadata FontName for additional files:

<ItemGroup>
  <AdditionalFiles Include="myfont.flf" FontName="MyExternalFont" />
</ItemGroup>

The user can then specify MyExternalFont as the font name in the GenerateFiggleText attribute:

[GenerateFiggleText("Member", "MyExternalFont", "Hello world")]

Implementation

  • Create Figgle.Generator.props and add a CompilerVisibleItemMetadata for FontName under AdditionalFiles.
  • Update Figgle.Generator.csproj to pack Figgle.Generator.props into the nuget package under the build folder.
    • Per nuget rules, consuming projects will auto import the props file under the build folder when referencing the nuget package.
  • Update the generator code to check for the optional FontName metadata and prioritize it to match the specified font name from GenerateFiggleText.
    • If the FontName metadata is not set, fall back to the original behavior of using the font's filename to match.

Testing

  • Added a new unit test to verify the generator is looking for FontName metadata and prioritizing it over the font's filename.
  • Added a new test project Figgle.Generator.AcceptanceTests to simulate consuming the source generator nuget package by referencing the generator project as a source generator and importing Figgle.Generator.props manually.
  • Created a new test in Figgle.Generator.AcceptanceTests that verifies the source-generated string using the external font with an explicit FontName metadata matches the expected value.

@jonathanou jonathanou changed the title Add support for explicitly specify a font name for source generation Add support for explicitly specifying a font name for source generation May 24, 2025
@jonathanou jonathanou changed the title Add support for explicitly specifying a font name for source generation Support explicitly specifying a font name for source generation May 24, 2025
@jonathanou jonathanou changed the title Support explicitly specifying a font name for source generation Support specifying a font name for source generation May 24, 2025
<ItemGroup>
<None Update="ANSI Shadow.flf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DependentUpon>FiggleSourceGeneratorTests_ExternalFonts.cs</DependentUpon>
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: I put the font file under the test file that uses it to reduce clutter in the Solution Explorer:

image

namespace Figgle.Generator.Tests;

public class FiggleSourceGeneratorTests
public partial class FiggleSourceGeneratorTests
Copy link
Copy Markdown
Collaborator Author

@jonathanou jonathanou May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: The code for testing external fonts was getting a bit much and was cluttering the original test file.

To improve code organization I split the external font tests and support code to another file. Using partial here allows the new test file to still access the private helpers like ValidateOutput.

@jonathanou jonathanou changed the title Support specifying a font name for source generation Support specifying a font name for external font source generation May 24, 2025
Copy link
Copy Markdown
Owner

@drewnoakes drewnoakes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work on this one. Thanks very much 👍


namespace Figgle.Generator.AcceptanceTests;

[GenerateFiggleText("HelloWorld", "My External Font", "Hello World!")]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible extension to this would be to generate const strings for any external font names, so that if the file is removed or the build item removed, then the code wouldn't compile. Some open questions (like how to create a valid C# member name from an arbitrary file name) but thought I'd throw it out there. This PR is great as is.

@drewnoakes drewnoakes merged commit 8896bd5 into drewnoakes:master May 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants