Skip to content

Commit

Permalink
Added get_italic_angle method.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Apr 29, 2022
1 parent 86b84c0 commit e6fe6b8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ font = Font("fonts/MyFont.ttf")
- [`get_format`](#get_format)
- [`get_fingerprint`](#get_fingerprint)
- [`get_image`](#get_image)
- [`get_italic_angle`](#get_italic_angle)
- [`get_name`](#get_name)
- [`get_names`](#get_names)
- [`get_style_flag`](#get_style_flag)
Expand Down Expand Up @@ -178,6 +179,17 @@ some text using the given options.
img = font.get_image(text, size, color=(0, 0, 0, 255), background_color=(255, 255, 255, 255))
```

- #### get_italic_angle
```python
"""
Gets the font italic angle.
:returns: The angle value including backslant, italic and roman flags.
:rtype: dict or None
"""
italic_angle = font.get_italic_angle()
```

- #### get_name
```python
"""
Expand Down
20 changes: 20 additions & 0 deletions fontbro/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,26 @@ def get_image(
draw.text((0, 0), text, font=img_font, fill=color)
return img

def get_italic_angle(self):
"""
Gets the font italic angle.
:returns: The angle value including backslant, italic and roman flags.
:rtype: dict or None
"""
font = self.get_ttfont()
post = font.get("post")
if not post:
return None
italic_angle_value = post.italicAngle
italic_angle = {
"backslant": italic_angle_value > 0,
"italic": italic_angle_value < 0,
"roman": italic_angle_value == 0,
"value": italic_angle_value,
}
return italic_angle

@classmethod
def _get_name_id(cls, key):
if isinstance(key, int):
Expand Down
37 changes: 37 additions & 0 deletions tests/test_italic_angle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

from fontbro import Font
from tests import AbstractTestCase


class ItalicAngleTestCase(AbstractTestCase):
"""
This class describes an italic angle test case.
"""

def _test_font_italic_angle(self, filepath, **kwargs):
font = self._get_font(filepath)
italic_angle = font.get_italic_angle()
# print(filepath, weight)
expected_italic_angle = {
"backslant": False,
"italic": False,
"roman": False,
"value": 0.0,
}
expected_italic_angle.update(**kwargs)
self.assertEqual(italic_angle, expected_italic_angle)

def test_get_italic_angle_with_roman_font(self):
self._test_font_italic_angle(
filepath="/Roboto_Mono/static/RobotoMono-Regular.ttf",
value=0,
roman=True,
)

def test_get_italic_angle_with_italic_font(self):
self._test_font_italic_angle(
filepath="/Roboto_Mono/static/RobotoMono-Italic.ttf",
value=-10,
italic=True,
)

0 comments on commit e6fe6b8

Please sign in to comment.