Skip to content

Commit

Permalink
Allow height auto to inherit without rescaling
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicEricEE committed Oct 6, 2024
1 parent 1c302c6 commit 441b90d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The package comes with a single `dropcap` function that takes content and a few

| Parameter | Description | Default |
|------------------|-------------------------------------------------------------------|---------|
| `height` | The height of the dropped capital in lines or as length. | `2` |
| `height` | The height of the dropped capital in lines, as length, or `auto`. | `2` |
| `justify` | Whether the text should be justified. | `auto` |
| `gap` | The space between the first letter and the text. | `0pt` |
| `hanging-indent` | The indent of lines after the first. | `0pt` |
Expand Down
40 changes: 25 additions & 15 deletions src/droplet.typ
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@

// Sets the font size so the resulting text height matches the given height.
//
// If not specified otherwise in "text-args", the top and bottom edge of the
// resulting text element will be set to "bounds".
//
// Parameters:
// - height: The target height of the resulting text.
// - text-args: Arguments to be passed to the underlying text element.
// - text-args: Named arguments to be passed to the underlying text element.
// - body: The content of the text element.
//
// Returns: The text with the set font size.
// Returns: The text with the adjusted size.
#let sized(height, ..text-args, body) = context {
let styled-text = text.with(
top-edge: "bounds",
bottom-edge: "bounds",
..text-args.named(),
body
)

let styled-text = text.with(..text-args.named(), body)
let factor = height / measure(styled-text(1em)).height
styled-text(factor * 1em)
}
Expand All @@ -47,7 +38,8 @@
//
// Parameters:
// - height: The height of the first letter. Can be given as the number of
// lines (integer) or as a length.
// lines (integer) or as a length. If set to `auto`, no scaling is
// applied.
// - justify: Whether to justify the text next to the first letter.
// - gap: The space between the first letter and the text.
// - hanging-indent: The indent of lines after the first line.
Expand All @@ -71,6 +63,18 @@
..text-args,
body
) = layout(bounds => {
let text-args = text-args

if height != auto {
// Set default top and bottom edge to "bounds" if not specified.
if "top-edge" not in text-args.named() {
text-args = arguments(..text-args, top-edge: "bounds")
}
if "bottom-edge" not in text-args.named() {
text-args = arguments(..text-args, bottom-edge: "bounds")
}
}

let (letter, rest) = if text-args.pos() == () {
extract(body)
} else {
Expand All @@ -82,7 +86,13 @@
letter = transform(letter)
}

let letter-height = resolve-height(height)
let letter-height = if height == auto {
// Don't rescale if height is set to auto.
measure(text(..text-args.named(), letter)).height
} else {
resolve-height(height)
}

let depth = resolve-height(depth)

// Create dropcap with the height of sample content.
Expand Down Expand Up @@ -124,7 +134,7 @@
height - measure(new).height - par.leading.to-absolute()
)

if top-position >= letter-height + depth and height > prev-height {
if top-position >= letter-height + depth - 1e-6pt and height > prev-height {
// Limit reached, new element doesn't fit anymore
split(rest, index - 1)
break
Expand Down
Binary file modified tests/explicit/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tests/explicit/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
#dropcap(square(size: 1em), gap: 1em, height: 3, lorem(13))
#dropcap(square(), height: 14pt, gap: 1em, lorem(10))
#dropcap[\#1][The winner has won what was to win.]
#dropcap(height: auto, gap: 1em)[
#figure(rect(), caption: [Rect])
][
To the left is a rectangle inside a figure with a caption, that this text is wrapped around.
]

0 comments on commit 441b90d

Please sign in to comment.