Skip to content

Commit

Permalink
Add get_vertical_metrics and set_vertical_metrics methods. (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo authored Jan 28, 2024
1 parent 32fa8b5 commit ac58a9a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ with open("fonts/MyFont.ttf") as fh:
- [`get_variable_instance_by_style_name`](#get_variable_instance_by_style_name)
- [`get_variable_instance_closest_to_coordinates`](#get_variable_instance_closest_to_coordinates)
- [`get_version`](#get_version)
- [`get_vertical_metrics`](#get_vertical_metrics)
- [`get_weight`](#get_weight)
- [`get_width`](#get_width)
- [`is_static`](#is_static)
Expand All @@ -85,6 +86,7 @@ with open("fonts/MyFont.ttf") as fh:
- [`set_style_flags`](#set_style_flags)
- [`set_style_flags_by_subfamily_name`](#set_style_flags_by_subfamily_name)
- [`set_style_name`](#set_style_name)
- [`set_vertical_metrics`](#set_vertical_metrics)
- [`subset`](#subset)
- [`to_sliced_variable`](#to_sliced_variable)
- [`to_static`](#to_static)
Expand Down Expand Up @@ -519,6 +521,18 @@ Gets the font version.
version = font.get_version()
```

#### `get_vertical_metrics`
```python
"""
Gets the font vertical metrics.
:returns: A dictionary containing the following vertical metrics:
"ascent", "cap_height", "x_height", "descent", "descender"
:rtype: dict
"""
metrics = font.get_vertical_metrics()
```

#### `get_weight`
```python
"""
Expand Down Expand Up @@ -785,6 +799,17 @@ Sets the style name updating the related font names records.
font.set_style_name(name="Bold Italic")
```

#### `set_vertical_metrics`
```python
"""
Sets the vertical metrics.
:param metrics: Keyword arguments representing the vertical metrics to set,
valid keys: "ascent", "cap_height", "x_height", "descent", "descender"
"""
font.set_vertical_metrics(ascent=1000, cap_height=750, x_height=500, descent=-250, descender=-500)
```

#### `subset`
```python
"""
Expand Down
46 changes: 46 additions & 0 deletions fontbro/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,26 @@ def get_version(self):
version = head.fontRevision
return version

def get_vertical_metrics(self):
"""
Gets the font vertical metrics.
:returns: A dictionary containing the following vertical metrics:
"ascent", "cap_height", "x_height", "descent", "descender"
:rtype: dict
"""
font = self.get_ttfont()
hhea = font.get("hhea")
os2 = font.get("OS/2")
metrics = {
"ascent": hhea.ascent if hhea else None,
"cap_height": os2.sCapHeight if os2 else None,
"x_height": os2.sxHeight if os2 else None,
"descent": hhea.descent if hhea else None,
"descender": os2.sTypoDescender if os2 else None,
}
return metrics

def get_weight(self):
"""
Gets the font weight value and name.
Expand Down Expand Up @@ -1494,6 +1514,32 @@ def set_style_name(self, name):
style_name=name,
)

def set_vertical_metrics(self, **metrics):
"""
Sets the vertical metrics.
:param metrics: Keyword arguments representing the vertical metrics to set,
valid keys: "ascent", "cap_height", "x_height", "descent", "descender"
"""
font = self.get_ttfont()
hhea = font.get("hhea")
os2 = font.get("OS/2")

if "ascent" in metrics:
hhea.ascent = metrics["ascent"]

if "cap_height" in metrics:
os2.sCapHeight = metrics["cap_height"]

if "x_height" in metrics:
os2.sxHeight = metrics["x_height"]

if "descent" in metrics:
hhea.descent = metrics["descent"]

if "descender" in metrics:
os2.sTypoDescender = metrics["descender"]

def subset(self, *, unicodes="", glyphs=None, text="", **options):
"""
Subsets the font using the given options (unicodes or glyphs or text),
Expand Down
39 changes: 39 additions & 0 deletions tests/test_vertical_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from tests import AbstractTestCase


class VerticalMetricsTestCase(AbstractTestCase):
"""
Test case for the font vertical-metrics.
"""

def test_get_vertical_metrics(self):
# font = self._get_font("/Roboto_Mono/static/RobotoMono-Regular.ttf")
# font = self._get_font("/Noto_Sans_TC/NotoSansTC-Regular.otf")
font = self._get_font("/Tourney/Tourney-VariableFont_wdth,wght.ttf")
vertical_metrics = font.get_vertical_metrics()
# print(vertical_metrics)
expected_vertical_metrics = {
"ascent": 1800,
"cap_height": 1400,
"x_height": 1080,
"descent": -400,
"descender": -400,
}
self.assertEqual(vertical_metrics, expected_vertical_metrics)

def test_set_vertical_metrics(self):
# font = self._get_font("/Roboto_Mono/static/RobotoMono-Regular.ttf")
# font = self._get_font("/Noto_Sans_TC/NotoSansTC-Regular.otf")
font = self._get_font("/Tourney/Tourney-VariableFont_wdth,wght.ttf")
vertical_metrics = {
"ascent": 1805,
"cap_height": 1405,
"x_height": 1085,
"descent": -405,
"descender": -405,
}
expected_vertical_metrics = vertical_metrics.copy()
font.set_vertical_metrics(**vertical_metrics)
vertical_metrics = font.get_vertical_metrics()
# print(vertical_metrics)
self.assertEqual(vertical_metrics, expected_vertical_metrics)

0 comments on commit ac58a9a

Please sign in to comment.