Skip to content

Commit

Permalink
šŸ› Fix regression in drawn text leading
Browse files Browse the repository at this point in the history
  • Loading branch information
ariebovenberg committed Aug 15, 2023
1 parent 96e663d commit 47568c2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 114 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Features:
- šŸš§ Avoiding orphaned/widowed lines
- šŸš§ Tex-style line breaking
- šŸš§ Broader unicode support in text wrapping
- šŸš§ Headings (which stick to their paragraphs)
- Drawing operations
- āœ… Lines
- āœ… Rectangles
Expand Down
6 changes: 4 additions & 2 deletions src/pdfje/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from .fonts.registry import Registry
from .style import Span, Style, StyledMixin, StyleFull, StyleLike
from .typeset.common import Command, State, Stretch, splitlines
from .typeset.common import Command, State, Stretch, max_lead, splitlines
from .typeset.lines import Line as TextLine
from .typeset.words import parse as parse_words

Expand Down Expand Up @@ -367,8 +367,10 @@ def render(self, r: Registry, s: StyleFull, /) -> Streamable:
state = s.as_state(r)
yield b"BT\n%g %g Td\n" % self.loc.astuple()
yield from state
stretches = list(self.flatten(r, s))
lead = max_lead(stretches, state)
yield from _pick_renderer(self.align)(
into_lines(splitlines(self.flatten(r, s)), state), state.lead, 0
into_lines(splitlines(stretches), state), lead, 0
)
yield b"ET\n"

Expand Down
Empty file removed src/pdfje/text.py
Empty file.
10 changes: 10 additions & 0 deletions src/pdfje/typeset/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ class Stretch(NamedTuple):
txt: str


def max_lead(s: Iterable[Stretch], state: State) -> Pt:
# FUTURE: we apply commands elsewhere, so doing it also here
# is perhaps a bit wasteful
lead = state.lead
for cmd, _ in s:
state = cmd.apply(state)
lead = max(lead, state.lead)
return lead


def _encode_kerning(
txt: str,
kerning: Sequence[Kern],
Expand Down
14 changes: 2 additions & 12 deletions src/pdfje/typeset/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ..atoms import LiteralStr, Real
from ..common import BranchableIterator, Pt, Streamable, add_slots
from .common import State, Stretch
from .common import State, Stretch, max_lead
from .words import WithCmd, Word
from .words import parse as parse_words
from .words import render_kerned
Expand Down Expand Up @@ -61,16 +61,6 @@ def _indent_first(
yield from it


def _max_lead(s: Iterable[Stretch], state: State) -> Pt:
# TODO: we apply commands elsewhere, so doing it also here
# is perhaps a bit wasteful
lead = state.lead
for cmd, _ in s:
state = cmd.apply(state)
lead = max(lead, state.lead)
return lead


@add_slots
@dataclass(frozen=True)
class Wrapper:
Expand All @@ -94,7 +84,7 @@ def start(
return Wrapper(
BranchableIterator(_indent_first(words, indent)),
cmd.apply(state),
_max_lead(it, state),
max_lead(it, state),
)

def line(self, width: Pt) -> tuple[Line, Wrapper | WrapDone]:
Expand Down
100 changes: 0 additions & 100 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,106 +271,6 @@ def test_draw(outfile):
[
Page([Text((250, 400), "First!", Style(size=50))]),
Page([Text((50, 720), ZEN_OF_PYTHON)]),
Page(
[
Rect((50, 720), 72, -800),
Text(
(50, 720),
"""\
In olden times
when wishing
still helped
one, there
lived a king
whose daugh-
ters were all
beautiful, but
the young-
est was so
beautiful that
the sun itself,
which has
seen so much,
was astonished
whenever it
shone in her
face. Close by
the kingā€™s cas-
tle lay a
great dark for-
est, and under
an old lime-
tree in the
forest was a
well, and when
the day was
very warm, the
kingā€™s child
went out into
the forest and
sat down by
the side of the
cool fountain,
and when she
was bored she
took a golden
ball, and threw
it up on high
and caught it,
and this ball
was her favo-
rite plaything.""",
style=times_roman,
),
Rect((150, 720), 72, -800),
Text(
(150, 720),
"""\
In olden times
when wishing
still helped
one, there lived
a king whose
daughters were
all beautiful,
but the young-
est was so
beautiful that
the sun itself,
which has
seen so much,
was astonished
whenever it
shone in her
face. Close by
the kingā€™s cas-
tle lay a great
dark forest, and
under an old
lime-tree in the
forest was a
well, and when
the day was
very warm, the
kingā€™s child
went out into
the forest and
sat down by
the side of the
cool fountain,
and when she
was bored she
took a golden
ball, and threw
it up on high
and caught it,
and this ball
was her favo-
rite plaything.""",
style=times_roman,
),
]
),
Page(
[
Polyline(
Expand Down

0 comments on commit 47568c2

Please sign in to comment.