Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from codetent/font-test
Browse files Browse the repository at this point in the history
Add methods for finding a font with the right size
  • Loading branch information
Akuli committed Jun 16, 2019
2 parents 9f86712 + bbbf3de commit 916ae22
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
3 changes: 2 additions & 1 deletion teek/_font.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import itertools

import teek
from teek._tcl_calls import to_tcl

flatten = itertools.chain.from_iterable

Expand Down Expand Up @@ -113,7 +114,7 @@ def to_tcl(self):
"""
Returns the font description passed to ``Font(font_description)``.
"""
return self._font_description
return to_tcl(self._font_description)

def measure(self, text):
"""
Expand Down
45 changes: 30 additions & 15 deletions tests/test_font.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import pytest

import teek
from teek._tcl_calls import to_tcl


def get_test_family():
"""Get font family with actual size equal to given size
"""
for family in teek.Font.families():
font = teek.Font((family, 42))

if font.size == 42:
return family

return teek.Font.families()[0]


def test_font_magic_new_method():
Expand All @@ -15,9 +28,10 @@ def test_font_magic_new_method():


def test_repr_eq_hash():
font = teek.Font(('Helvetica', 12))
font_family = get_test_family()
font = teek.Font((font_family, 12))
named_font = teek.NamedFont('asda')
assert repr(font) == "Font(('Helvetica', 12))"
assert repr(font) == "Font(('%s', 12))" % font_family
assert repr(named_font) == "NamedFont('asda')"

another_named_font = teek.NamedFont('asda')
Expand All @@ -37,9 +51,9 @@ def all_names():


def test_from_and_to_tcl():
description = ["Helvetica", 42, "bold"]
description = [get_test_family(), 42, 'bold']
descriptiony_font = teek.Font(description)
assert descriptiony_font.to_tcl() is description
assert descriptiony_font.to_tcl() == to_tcl(description)
assert teek.Font.from_tcl(description) == descriptiony_font

teek.tcl_eval(None, 'font create test_font_name')
Expand All @@ -50,18 +64,19 @@ def test_from_and_to_tcl():


def test_properties():
anonymous_font = teek.Font(("Helvetica", 42, "bold", "underline"))
font_family = get_test_family()
anonymous_font = teek.Font((font_family, 42, 'bold', 'underline'))
named_font = teek.NamedFont(
family='Helvetica', size=42, weight='bold', underline=True)
family=font_family, size=42, weight='bold', underline=True)

# just to make debugging easier because these facts are needed below
assert not isinstance(anonymous_font, teek.NamedFont)
assert isinstance(named_font, teek.NamedFont)

for font in [anonymous_font, named_font]:
assert font.size == 42
assert font.weight == "bold"
assert font.slant == "roman"
assert font.weight == 'bold'
assert font.slant == 'roman'
assert font.underline is True
assert font.overstrike is False

Expand All @@ -71,7 +86,7 @@ def test_properties():

# test setting error
with pytest.raises(AttributeError) as error:
anonymous_font.weight = "normal"
anonymous_font.weight = 'normal'
assert '.to_named_font()' in str(error.value)

# test successful setting
Expand All @@ -84,11 +99,11 @@ def test_properties():


def test_measure():
assert teek.Font(('Helvetica', 42, 'bold')).measure('') == 0
assert teek.Font((get_test_family(), 42, 'bold')).measure('') == 0


def test_metrics():
metrics = teek.Font(('Helvetica', 42, 'bold')).metrics()
metrics = teek.Font((get_test_family(), 42, 'bold')).metrics()
assert isinstance(metrics['ascent'], int)
assert isinstance(metrics['descent'], int)
assert isinstance(metrics['linespace'], int)
Expand Down Expand Up @@ -120,7 +135,7 @@ def fonts_are_similar(font1, font2):


def test_to_named_font():
anonymous = teek.Font(('Helvetica', 42))
anonymous = teek.Font((get_test_family(), 42))
named = anonymous.to_named_font()
assert isinstance(named, teek.NamedFont)
assert fonts_are_similar(anonymous, named)
Expand All @@ -130,9 +145,9 @@ def test_to_named_font():
assert named != named2 # it is a copy
assert fonts_are_similar(named, named2)

named2.weight = 'bold'
assert named.weight != named2.weight
named2.weight = 'normal'
named2.size = 18
assert named.size != named2.size
named2.size = 42
assert fonts_are_similar(named, named2)

named.delete()
Expand Down

0 comments on commit 916ae22

Please sign in to comment.