Optical size tunes the design for different type sizes. The program reads Newsreader.ttf from the working directory (the file comes in the zip); you can replace it with the path to any variable .ttf.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; var face = FontFace.Load( File.ReadAllBytes("Newsreader.ttf")); var textFace = face.WithVariations( FontVariation.Weight(450), FontVariation.OpticalSize(16)); foreach (var axis in textFace.Variations) Console.WriteLine( $"{axis.Tag}: {axis.Value}");
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Creates a face for weight 450 and optical size 16 and prints the values it uses.
wght: 450 opsz: 16
A font without wght or opsz ignores that setting; OneFont doesn’t synthesize a missing axis. The zip includes the sample’s SIL Open Font License.
Variations
Set axes by tag
Append this to pass the OpenType tags directly and print the selected weight:
var display = face.WithVariations( new FontVariation("wght", 600), new FontVariation("opsz", 48)); Console.WriteLine(display.Weight);
600Related recipes
Read variable font axes and ranges
Load a variable font named instance
Read normalized variation coordinates
The short answer
Call WithVariations(FontVariation.Weight(450), FontVariation.OpticalSize(16)) on a loaded FontFace. Keep the returned face and use it for shaping or for reading outlines and metrics. This selects an instance in memory, without editing the source font file. Variations reports the axis values on that face in the font’s own units, so it’s the right place to check what your application actually selected.
What is variable font weight?
The wght axis lets a font designer define a range from lighter to heavier strokes in one file. A static face has one design; a variable face with wght can also give you in-between weights such as 450, even if the font doesn’t ship a preset with that number. The OpenType weight-axis specification defines the scale, but the usable range is set by the file. In this Newsreader subset the range is 200–800, with a default of 400. Requesting 900 selects 800; it doesn’t extrapolate a heavier design.
What is font optical sizing?
Optical size (opsz) chooses the design suited to a particular type size. It isn’t a command to scale the output. A font might use more sturdy strokes in small text and finer details at display sizes. The OpenType optical-size specification defines opsz in typographic points. The snippet passes an explicit axis value and doesn’t set the size at which your application lays out or draws glyphs. A caption and a headline can use the same font file but select different optical-size values and draw at different sizes.
If both settings matter, choose them before measuring text: the selected instance can change glyph advances as well as outlines. Use the same face when measuring and placing the glyphs rather than measuring with the default and only varying the outlines at the end. See what OneFont varies.
How do you specify weight without losing optical size?
Pass both settings together each time. The OneFont API says “Every axis starts at its default (earlier calls are not accumulated)”. Calling WithVariations again with only weight does not retain optical size 16; in this font it goes back to 18. For a slider or style editor, keep the requested values in your app and supply the complete set when one value changes. If you send the same tag twice in one call, the last valid value for that tag wins. WithVariations ignores non-finite values rather than substituting a bound.