Source code elsie/slides/slide.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os.path
from typing import TYPE_CHECKING

from ..boxtree.box import Box
from ..boxtree.layout import Layout
from ..render import jupyter
from ..render.render import ExportedRenderUnit
from ..utils.geom import Rect
from .show import ShowInfo

if TYPE_CHECKING:
    from . import slidedeck


class Slide:
    def __init__(
        self,
        slides: "slidedeck.SlideDeck",
        index: int,
        width: int,
        height: int,
        styles,
        fs_cache,
        temp_cache,
        view_box,
        name: str,
        debug_boxes: bool,
    ):
        self.slides = slides
        self.name = name
        self.width = width
        self.height = height
        self.index = index
        self.view_box = view_box
        self.debug_boxes = debug_boxes
        self._box = Box(
            self,
            layout=Layout(x=0, y=0, width=width, height=height),
            styles=styles,
            show_info=ShowInfo(),
            name=name,
        )
        self.max_step = 1
        self.temp_cache = temp_cache
        self.fs_cache = fs_cache

    def box(self):
        return self._box

    def current_fragment(self) -> int:
        """Returns the current maximum fragment."""
        return self.max_step

    def prepare(self):
        rect = Rect(0, 0, self.width, self.height)
        self._box.layout.set_rect(rect)

    def steps(self):
        shows = [1]
        self._box._traverse(lambda box: shows.append(box._min_steps()))
        return max(value for value in shows if value)

    def make_render_unit(self, backend, step, export_type: str):
        return backend.create_render_unit(self, step, export_type)

    def _repr_html_(self):
        return jupyter.render_slide_html(self)


class ExternPdfSlide:
    def __init__(self, filename):
        self.filename = os.path.abspath(filename)

    def prepare(self):
        pass

    def steps(self):
        return 1

    def make_render_unit(self, backend, step, export_type):
        assert export_type == "pdf"
        return ExportedRenderUnit(self, step, self.filename, "pdf")