---
title: "Set variable font weight and optical size in C#"
url: https://recursiveai.org/onefont/cookbook/set-variable-font-axis/
description: "Set the weight and optical size of an OpenType variable font in C#. Create a face at custom wght and opsz values and read back the settings with OneFont."
version: "1.5.1"
---

# Set variable font weight and optical size in C#

**onefont** is a free, open-source font and text-shaping library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneFont) · [GitHub](https://github.com/recursive-ai-oss/onefont-csharp) · [Docs](https://recursiveai.org/onefont/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/cookbook/set-variable-font-axis/set-variable-font-axis.zip)

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`.

```shell
dotnet add package RecursiveAI.OneFont
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onefont/cookbook/set-variable-font-axis/set-variable-font-axis.zip) and `dotnet run`.

Creates a face for weight 450 and optical size 16 and prints the values it uses.

```text
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:

```csharp
var display = face.WithVariations(
    new FontVariation("wght", 600),
    new FontVariation("opsz", 48));
Console.WriteLine(display.Weight);
```

```text
600
```

## Related recipes

[Read variable font axes and ranges](https://recursiveai.org/onefont/cookbook/list-variable-font-axes/)

[Load a variable font named instance](https://recursiveai.org/onefont/cookbook/select-variable-font-named-instance/)

[Read normalized variation coordinates](https://recursiveai.org/onefont/cookbook/read-variable-font-normalized-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](https://learn.microsoft.com/en-us/typography/opentype/spec/dvaraxistag_wght) 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](https://learn.microsoft.com/en-us/typography/opentype/spec/dvaraxistag_opsz) 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](https://github.com/recursive-ai-oss/onefont-csharp/blob/8c71b3fc22254ad7ff2561eb3abd171f1abdea0c/docs/shaping.md#L137-L145).

## How do you specify weight without losing optical size?

Pass both settings together each time. The [OneFont API](https://github.com/recursive-ai-oss/onefont-csharp/blob/8c71b3fc22254ad7ff2561eb3abd171f1abdea0c/src/OneFont/FontFace.cs#L73-L86) 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.
