Coverage for app / documents / visuals / registry.py: 97%

34 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""One class per visual kind, registered: how it reads its fence and lays 

2itself out. ``render.prepare`` is the only caller.""" 

3 

4from __future__ import annotations 

5 

6from abc import ABC, abstractmethod 

7from dataclasses import dataclass 

8from typing import Any, ClassVar 

9 

10from reportlab.graphics.shapes import Drawing 

11 

12from .theme import Theme 

13 

14 

15@dataclass(frozen=True) 

16class Layout: 

17 """A visual laid out at one width, in points.""" 

18 

19 drawing: Drawing 

20 width: float 

21 height: float 

22 #: Clip what is drawn to the frame (a map's outlines run past it). 

23 clip: bool = False 

24 #: A line under it naming what it draws on, when that depends on the 

25 #: layout (a map's outlines are drawn only at some scales). 

26 credit: str | None = None 

27 #: Where a tall one may be cut across pages, in points down from its 

28 #: top (between rows); none, and it is scaled to fit one page. 

29 breaks: tuple[float, ...] = () 

30 #: Drawn above each part after the first (a sequence diagram's 

31 #: participants, a Gantt chart's dates), as wide as the drawing. 

32 header: Drawing | None = None 

33 

34 

35class Visual(ABC): 

36 kind: ClassVar[str] 

37 #: What the PDF calls it in a note: "Chart not shown — …". 

38 noun: ClassVar[str] 

39 #: A diagram may run to most of a page; a chart to half of one. 

40 tall: ClassVar[bool] = False 

41 #: Laid out again at a narrower width (a list item, a quote); one that 

42 #: does not is scaled down instead. 

43 fills_width: ClassVar[bool] = True 

44 

45 @abstractmethod 

46 def read(self, source: str) -> Any: 

47 """The fence read as fe-core reads it; ``fence.VisualError`` says why 

48 it cannot be drawn.""" 

49 

50 def title(self, spec: Any) -> str | None: 

51 return None 

52 

53 def credit(self, spec: Any) -> str | None: 

54 """A line under it naming what it draws on (a map's outlines).""" 

55 return None 

56 

57 def cost(self, spec: Any) -> int: 

58 """What laying it out costs against the document's budget 

59 (``render.Budget.layout``): a force layout's nodes, squared.""" 

60 return 0 

61 

62 @abstractmethod 

63 def layout( 

64 self, spec: Any, width: float, max_height: float, theme: Theme 

65 ) -> Layout: 

66 """It drawn to ``width``, within ``max_height`` where it can be.""" 

67 

68 

69VISUALS: dict[str, Visual] = {} 

70 

71 

72def register_visual(cls: type[Visual]) -> type[Visual]: 

73 VISUALS[cls.kind] = cls() 

74 return cls