Skip to content

Instantly share code, notes, and snippets.

@rsms
Last active November 1, 2022 04:25
Show Gist options
  • Save rsms/0618d6cbce7977d6132ea01288f5161b to your computer and use it in GitHub Desktop.
Save rsms/0618d6cbce7977d6132ea01288f5161b to your computer and use it in GitHub Desktop.

Determining if a font is italic

To determine if a font is italic, you can look at fsSelection of the OS/2 table, checking the 0th and 9th bit:

For instance, here’s fsSelection for "Inter Medium Italic":

node -p '(129).toString(2).split("").reverse().join("")'
=> 10000001 == ITALIC | USE_TYPO_METRICS

Bit#  Value  Meaning

0     1      ITALIC  Font contains italic or oblique glyphs.
1     0      UNDERSCORE  glyphs are underscored.
2     0      NEGATIVE  glyphs have their foreground and background reversed.
3     0      OUTLINED  Outline (hollow) glyphs, otherwise they are solid.
4     0      STRIKEOUT glyphs are overstruck.
5     0      BOLD  glyphs are emboldened.
6     0      REGULAR glyphs are in the standard weight/style for the font.
7     1      USE_TYPO_METRICS
8     0      WWS The font has 'name' table strings consistent with a weight...
9     0      OBLIQUE Font contains oblique glyphs.
(10-15 are reserved)

and here’s fsSelection for "Inter Medium” (not italic):

node -p '(192).toString(2).split("").reverse().join("")'
=> 00000011 == REGULAR | USE_TYPO_METRICS

Bit#  Value  Meaning

0     0      ITALIC  Font contains italic or oblique glyphs.
1     0      UNDERSCORE  glyphs are underscored.
2     0      NEGATIVE  glyphs have their foreground and background reversed.
3     0      OUTLINED  Outline (hollow) glyphs, otherwise they are solid.
4     0      STRIKEOUT glyphs are overstruck.
5     0      BOLD  glyphs are emboldened.
6     1      REGULAR glyphs are in the standard weight/style for the font.
7     1      USE_TYPO_METRICS
8     0      WWS The font has 'name' table strings consistent with a weight...
9     0      OBLIQUE Font contains oblique glyphs.
(10-15 are reserved)

There's also macStyle in the head table, which can be checked as well to support really old fonts.

macStyle is 16 bits:

Bit#  Meaning
0     Bold
1     Italic
2     Underline
3     Outline
4     Shadow
5     Condensed
6     Extended
(7–15 are reserved)

Example: for "Inter Medium Italic", macStyle is 0100000, and for "Inter Medium" it's 0000000.

Note that the current OT spec states that fsSelection takes priority over macStyle (on Windows at least), and that PANOSE and post table should always be ignored when determining bold or italic. (i.e. italicAngle of the post table should be ignored.)

Determining weight of a font

Check the usWeightClass of the OS/2 table. For instance, for "Inter Medium" it's 500

Value  Description

100    Thin
200    Extra-light (Ultra-light)
300    Light
400    Normal (Regular)
500    Medium
600    Semi-bold (Demi-bold)
700    Bold
800    Extra-bold (Ultra-bold)
900    Black (Heavy)

Determining width of a font

Check the usWidthClass of the OS/2 table. For instance, for "Inter Medium" it's 5

Value Description      % of normal

1     Ultra-condensed  50%
2     Extra-condensed  62.5%
3     Condensed        75%
4     Semi-condensed   87.5%
5     Medium (normal)  100%
6     Semi-expanded    112.5%
7     Expanded         125%
8     Extra-expanded   150%
9     Ultra-expanded   200%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment