Skip to content

Commit

Permalink
use fraction units for typst column layouts
Browse files Browse the repository at this point in the history
fixes #11578
  • Loading branch information
gordonwoodhull committed Dec 11, 2024
1 parent a35d0c9 commit 95ce1e1
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/resources/filters/layout/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,13 @@ function layout_cells(float_or_div, cells)
end
rows[#rows]:insert(cell)
end
-- convert width units to percentages
widthsToPercent(rows, layoutCols)

if _quarto.format.isTypstOutput() then
widthsToFraction(rows, layoutCols)
else
-- convert width units to percentages
widthsToPercent(rows, layoutCols)
end

-- check for layout
elseif layout ~= nil then
-- parse the layout
Expand Down
49 changes: 44 additions & 5 deletions src/resources/filters/layout/width.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ function parseLayoutWidths(figLayout, figureCount)
return cols:map(function(width)
figureLayoutCount = figureLayoutCount + 1
if type(width) == "number" then
if numericTotal ~= 0 then
width = round((width / numericTotal) * 100, 2)
elseif width <= 1 then
width = round(width * 100, 2)
if _quarto.format.isTypstOutput() then
width = tostring(width) .. "fr"
else
if numericTotal ~= 0 then
width = round((width / numericTotal) * 100, 2)
elseif width <= 1 then
width = round(width * 100, 2)
end
width = tostring(width) .. "%"
end
width = tostring(width) .. "%"
end
-- negative widths are "spacers" so we need to bump our total fig count
if isSpacerWidth(width) then
Expand Down Expand Up @@ -119,6 +123,41 @@ function widthsToPercent(layout, cols)
end


-- convert widths to typst fractions
function widthsToFraction(layout, cols)

-- for each row
for _,row in ipairs(layout) do

-- initialize widths with 0 or length string
-- currently we assume the width unit is appropriate for the output format
local widths = pandoc.List()
for _,fig in ipairs(row) do
widths[#widths+1] = 0
local width = attribute(fig, "width", nil)
if width then
widths[#widths] = width
end
end

-- create virtual fig widths as needed and note the total width
local defaultWidth = "1fr"
for i=1,cols do
if (i > #widths) or widths[i] == 0 then
widths[i] = defaultWidth
end
end
-- allocate widths
for i,fig in ipairs(row) do
local width = widths[i];
fig.attr.attributes["width"] = width
fig.attr.attributes["height"] = nil
end

end
end


-- elements with a percentage width and no height have a 'layout percent'
-- which means then should be laid out at a higher level in the tree than
-- the individual figure element
Expand Down
30 changes: 30 additions & 0 deletions tests/docs/smoke-all/typst/layout/fraction-layout.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: "Fraction layout"
format:
typst:
keep-typ: true
_quarto:
tests:
typst:
ensureTypstFileRegexMatches:
-
['#grid\((\r\n?|\n)columns: \(1fr, 2fr, 2fr, 1fr, 3fr, 1fr, 1fr\), gutter: 1em, rows: 1,']
---

::: {layout="[1, 2, 2, 1, 3, 1, 1]"}

![Placeholder]({{< placeholder 200 >}})

![Placeholder]({{< placeholder 200 >}})

![Placeholder]({{< placeholder 200 >}})

![Placeholder]({{< placeholder 200 >}})

![Placeholder]({{< placeholder 200 >}})

![Placeholder]({{< placeholder 200 >}})

![Placeholder]({{< placeholder 200 >}})

:::
35 changes: 35 additions & 0 deletions tests/docs/smoke-all/typst/layout/overflowing-callout-7.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: Test
format: typst
keep-md: true
keep-typ: true
_quarto:
tests:
typst:
ensureTypstFileRegexMatches:
-
['#grid\((\r\n?|\n)columns: \(1fr, 1fr, 100pt, 1fr, 1fr, 1fr, 1fr\), gutter: 1em, rows: 1,']

---

::: {.callout-note}

## Plots

::: {layout-ncol=7}

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}}){width="100pt"}

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})
:::
:::
33 changes: 33 additions & 0 deletions tests/docs/smoke-all/typst/layout/overflowing-callout.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Test
format: typst
keep-md: true
keep-typ: true
_quarto:
tests:
typst:
ensureTypstFileRegexMatches:
-
['#grid\((\r\n?|\n)columns: \(1fr, 1fr, 1fr, 1fr, 1fr\), gutter: 1em, rows: 1,']
---

::: {.callout-note}

## Plots

::: {layout-ncol=5}

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})

![]({{< placeholder format=svg >}})
:::

a) Beschreiben Sie die 5 Verteilungsformen. [5 Punkte]

:::

0 comments on commit 95ce1e1

Please sign in to comment.