| 1 | from pathlib import Path |
| 2 | import runpy |
| 3 | import shutil |
| 4 | from tempfile import TemporaryDirectory |
| 5 | import unittest |
| 6 | import xml.etree.ElementTree as ET |
| 7 | |
| 8 | from native_xml import ns |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parent.parent |
| 11 | FIXTURE = ROOT / 'corpus/ink-edit' |
| 12 | compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare'] |
| 13 | |
| 14 | |
| 15 | def native_page(variant): |
| 16 | with TemporaryDirectory() as temporary: |
| 17 | read = Path(temporary) / 'read' |
| 18 | shutil.copytree(FIXTURE / variant / 'cold/read', read) |
| 19 | compare(FIXTURE / variant / 'candidate', read) |
| 20 | page, = (ET.parse(path).getroot() for path in sorted((FIXTURE / variant / 'cold/read').glob('page-*.xml'))) |
| 21 | return page |
| 22 | |
| 23 | |
| 24 | class InkEditTest(unittest.TestCase): |
| 25 | def test_a_rust_drawing_renders_natively_at_its_stroke_extent(self): |
| 26 | page = native_page('drawing') |
| 27 | drawing, = page.findall('one:InkDrawing', ns) |
| 28 | position, size = drawing.find('one:Position', ns), drawing.find('one:Size', ns) |
| 29 | self.assertAlmostEqual(float(position.get('x')), 300.0, delta=0.03) |
| 30 | self.assertAlmostEqual(float(position.get('y')), 120.0, delta=0.03) |
| 31 | self.assertAlmostEqual(float(size.get('width')), 60.0, delta=0.06) |
| 32 | self.assertAlmostEqual(float(size.get('height')), 60.0, delta=0.06) |
| 33 | self.assertTrue((FIXTURE / 'drawing/cold/read/page-000.png').exists()) |
| 34 | |
| 35 | def test_rust_handwriting_renders_natively_as_an_ink_paragraph(self): |
| 36 | page = native_page('handwriting') |
| 37 | self.assertEqual(len(list(page.iter('{%s}InkParagraph' % ns['one']))) + len(list(page.iter('{%s}InkDrawing' % ns['one']))), 1) |
| 38 | |
| 39 | |
| 40 | if __name__ == '__main__': |
| 41 | unittest.main() |