| 1 | import json |
| 2 | from pathlib import Path |
| 3 | import runpy |
| 4 | import subprocess |
| 5 | from tempfile import TemporaryDirectory |
| 6 | import unittest |
| 7 | import xml.etree.ElementTree as ET |
| 8 | |
| 9 | from document_model import EXPORTER, ordered_pages |
| 10 | from native_xml import ns |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parent.parent |
| 13 | FIXTURE = ROOT / 'corpus/native-ink/cold-ui-ink' |
| 14 | ink_extent = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['ink_extent'] |
| 15 | |
| 16 | |
| 17 | class InkReadTest(unittest.TestCase): |
| 18 | def test_decoded_ink_strokes_span_the_native_drawing_extents(self): |
| 19 | """An independent decoding of the stored stroke packets reproduces the native positions and sizes.""" |
| 20 | with TemporaryDirectory() as temporary: |
| 21 | exported = Path(temporary) / 'document' |
| 22 | subprocess.run([EXPORTER, FIXTURE / 'notebook/synthetic.one', exported], check=True) |
| 23 | document = json.loads((exported / 'document.json').read_text()) |
| 24 | (_, _, revision, page_id), = ordered_pages(document) |
| 25 | source_page = revision['nodes'][page_id] |
| 26 | native, = (ET.parse(path).getroot() for path in sorted((FIXTURE / 'read').glob('page-*.xml'))) |
| 27 | drawings = native.findall('one:InkDrawing', ns) |
| 28 | self.assertEqual(len(drawings), 2) |
| 29 | for drawing in drawings: |
| 30 | z = int(drawing.find('one:Position', ns).get('z')) |
| 31 | source = revision['nodes'][source_page['children'][z]] |
| 32 | self.assertEqual(source['kind']['type'], 'Ink') |
| 33 | position, size = drawing.find('one:Position', ns), drawing.find('one:Size', ns) |
| 34 | expected = [float(position.get('x')), float(position.get('y')), |
| 35 | float(size.get('width')) - 72 / 2540, float(size.get('height')) - 72 / 2540] |
| 36 | extent = ink_extent(revision, source) |
| 37 | for actual, wanted in zip(extent, expected): |
| 38 | self.assertAlmostEqual(actual, wanted, delta=0.002) |
| 39 | |
| 40 | |
| 41 | if __name__ == '__main__': |
| 42 | unittest.main() |