Skip to content

Commit

Permalink
Added additional tests for TabArray
Browse files Browse the repository at this point in the history
  • Loading branch information
leifgehrmann committed Oct 21, 2023
1 parent 1d12c01 commit 935a7f2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pangocffi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _dlopen(dl_name: str, generated_ffi, names: List[str]):
from .glyph_item_iter import GlyphItemIter # noqa
from .attribute import Attribute # noqa
from .attr_list import AttrList # noqa
from .tabarray import TabArray # noqa
from .tab_array import TabArray # noqa
from .layout_run import LayoutRun # noqa
from .layout_iter import LayoutIter # noqa
from .layout import Layout # noqa
Expand Down
13 changes: 9 additions & 4 deletions pangocffi/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,17 @@ def _del_attributes(self) -> None:
:param attrs: a :class:`AttrList`
"""

def _get_tabs(self) -> TabArray:
def _get_tabs(self) -> Optional[TabArray]:
tabs_pointer = pango.pango_layout_get_tabs(self._pointer)
return TabArray(tabs_pointer)
if tabs_pointer == ffi.NULL:
return None
return TabArray.from_pointer(tabs_pointer)

def _set_tabs(self, tabs: TabArray) -> None:
pango.pango_layout_set_tabs(self._pointer, tabs._pointer)
def _set_tabs(self, tabs: Optional[TabArray]) -> None:
if tabs is None:
pango.pango_layout_set_tabs(self._pointer, ffi.NULL)
else:
pango.pango_layout_set_tabs(self._pointer, tabs._pointer)

tabs: str = property(_get_tabs, _set_tabs)
"""
Expand Down
File renamed without changes.
Binary file not shown.
79 changes: 79 additions & 0 deletions tests/acceptance/test_tab_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pangocffi
from pangocffi import Layout, TabArray, TabAlign
from tests.context_creator import ContextCreator
import locale


def test_tab_array_decimal_places():
"""
In this test, we're aligning a list of labels and numbers.
The list of numbers is deliberately ugly, as ideally all numbers would be
given the same number of decimal places. But this test confirms that the
numbers are aligned at the comma.
"""

width = 100
height = 100
# CFFIs and Contexts
context_creator = ContextCreator.create_pdf(
'tests/acceptance/output/tab-array-decimal-places.pdf', width, height
)
pangocairo = context_creator.pangocairo
cairo_context = context_creator.cairo_context

lines = [
['Berlin', 891.3],
['Hamburg', 755.2],
['München', 310.7],
['Stuttgart', 207.33],
['Düsseldorf', 217.41],
['Bremen', 326.7],
['Dresden', 328.8],
['Hannover', 204.007],
['Wiesbaden', 203.9],
['Kiel', 118.6],
['Magdeburg', 201.012],
['Mainz', 97.7],
['Erfurt', 269.2],
]

locale.setlocale(locale.LC_ALL, 'de_DE')

text = '\t<span font_weight="bold">Name</span>' \
'\t<span font_weight="bold"> Fläche (km²)</span>\n'
for line in lines:
text += "\t" + line[0]
text += "\t" + locale.format_string("%f", line[1])\
.rstrip('0').rstrip(',')
text += '\n'

# Using pangocairocffi, this would be `pangocairocffi.create_layout()` with
# a cairocffi context object.
layout = Layout.from_pointer(
pangocairo.pango_cairo_create_layout(cairo_context)
)

mm_to_inches = 72 / 25.4

layout.width = pangocffi.units_from_double(width * mm_to_inches)
layout.apply_markup(text)

# With this configuration, the tabs will align the text at 30mm and 70mm
# intervals. In more simple terms: The first items that are tabbed should
# appear right-aligned at the 30mm position, and all ',' (which will
# be part of a number) should appear at the 70mm position.
tabarray = TabArray()
tabarray.tabs = [
(TabAlign.RIGHT, pangocffi.units_from_double(30 * mm_to_inches)),
(TabAlign.DECIMAL, pangocffi.units_from_double(45 * mm_to_inches))
]
decimal_point = [None, ',']
tabarray.decimal_point = decimal_point

layout.tabs = tabarray

# Using pangocairocffi, this would be `pangocairocffi.show_layout(layout)`
pangocairo.pango_cairo_show_layout(cairo_context, layout.pointer)

context_creator.close()
15 changes: 15 additions & 0 deletions tests/functional/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ffi,
Attribute,
AttrList,
TabArray,
)
import unittest

Expand Down Expand Up @@ -92,3 +93,17 @@ def test_set_attributes(self):
# Resetting the attributes
layout.attributes = None
assert layout.attributes is None

def test_set_tabs(self):
context = Context()
layout = Layout(context)

assert layout.tabs is None

tab_array = TabArray()

layout.tabs = tab_array
assert layout.tabs is not None

layout.tabs = None
assert layout.tabs is None
43 changes: 43 additions & 0 deletions tests/functional/test_tab_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from pangocffi import (
TabArray, TabAlign
)
import unittest


class TestTabArray(unittest.TestCase):
def test_identity(self):
tab_array = TabArray()
assert len(tab_array) == 0
tabs = [(TabAlign.LEFT, 1), (TabAlign.LEFT, 10)]
tab_array.tabs = tabs
assert len(tab_array) == len(tabs)
assert tab_array.tabs == tabs

decimal_point = [':', ',']
tab_array.decimal_point = decimal_point
assert tab_array.decimal_point == decimal_point

assert tab_array.positions_in_pixels is False
tab_array.positions_in_pixels = True
assert tab_array.positions_in_pixels is True

def test_zero_size(self):
tab_array = TabArray()
assert len(tab_array) == 0
assert tab_array.tabs == []

def test_decimal_point_none(self):
tab_array = TabArray()

tabs = [(TabAlign.LEFT, 1), (TabAlign.LEFT, 10)]
tab_array.tabs = tabs
decimal_point = [None, None]
tab_array.decimal_point = decimal_point
assert tab_array.decimal_point == decimal_point

def test_decimal_point_index_error(self):
tab_array = TabArray()

tab_array.tabs = [(TabAlign.LEFT, 1), (TabAlign.LEFT, 10)]
with self.assertRaises(IndexError):
tab_array.decimal_point = [',']
36 changes: 0 additions & 36 deletions tests/functional/test_tabarray.py

This file was deleted.

0 comments on commit 935a7f2

Please sign in to comment.