Skip to content

Commit 66faddf

Browse files
committed
Add is_monospace method.
1 parent 5f1b4b9 commit 66faddf

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ with open("fonts/MyFont.ttf") as fh:
7070
- [`get_vertical_metrics`](#get_vertical_metrics)
7171
- [`get_weight`](#get_weight)
7272
- [`get_width`](#get_width)
73+
- [`is_monospace`](#is_monospace)
7374
- [`is_static`](#is_static)
7475
- [`is_variable`](#is_variable)
7576
- [`rename`](#rename)
@@ -557,6 +558,17 @@ Gets the font width value and name.
557558
width = font.get_width()
558559
```
559560

561+
#### `is_monospace`
562+
```python
563+
"""
564+
Determines if the font is a monospace font.
565+
566+
:returns: True if monospace font, False otherwise.
567+
:rtype: bool
568+
"""
569+
mono = font.is_monospace()
570+
```
571+
560572
#### `is_static`
561573
```python
562574
"""

fontbro/font.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,19 @@ def get_width(
12411241
width["value"] = width_value
12421242
return width
12431243

1244+
def is_monospace(
1245+
self,
1246+
) -> bool:
1247+
"""
1248+
Determines if the font is a monospace font.
1249+
1250+
:returns: True if monospace font, False otherwise.
1251+
:rtype: bool
1252+
"""
1253+
font = self.get_ttfont()
1254+
widths = {metrics[0] for metrics in font["hmtx"].metrics.values()}
1255+
return len(widths) == 1
1256+
12441257
def is_static(
12451258
self,
12461259
) -> bool:

tests/test_monospace.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from tests import AbstractTestCase
2+
3+
4+
class MonospaceTestCase(AbstractTestCase):
5+
"""
6+
This class describes a monospace test case.
7+
"""
8+
9+
def test_is_monospace(self):
10+
with self._get_font("/Inter/static/Inter-Regular.ttf") as font:
11+
self.assertFalse(font.is_monospace())
12+
with self._get_font("/Noto_Sans_TC/NotoSansTC-Regular.otf") as font:
13+
self.assertFalse(font.is_monospace())
14+
with self._get_font("/Open_Sans/static/OpenSans-Regular.ttf") as font:
15+
self.assertFalse(font.is_monospace())
16+
with self._get_font("/Roboto_Mono/static/RobotoMono-Regular.ttf") as font:
17+
self.assertTrue(font.is_monospace())

0 commit comments

Comments
 (0)