Skip to content

Commit e11cbb2

Browse files
typst brand yml: handle font families as possible lists
fixes #11933
1 parent 31e496f commit e11cbb2

File tree

7 files changed

+92
-32
lines changed

7 files changed

+92
-32
lines changed

src/resources/filters/modules/typst_css.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,28 @@ local function translate_font_weight(w, warnings)
636636
end
637637
end
638638

639+
local function dequote(s)
640+
return s:gsub('^["\']', ''):gsub('["\']$', '')
641+
end
642+
643+
local function quote(s)
644+
return '"' .. s .. '"'
645+
end
646+
647+
local function translate_font_family_list(sl)
648+
if sl == nil then
649+
return '()'
650+
end
651+
local strings = {}
652+
for s in sl:gmatch('([^,]+)') do
653+
s = s:gsub('^%s+', '')
654+
table.insert(strings, quote(dequote(s)))
655+
end
656+
local trailcomma = #strings == 1 and ',' or ''
657+
return '(' .. table.concat(strings, ', ') .. trailcomma .. ')'
658+
end
659+
660+
639661
local function translate_border_style(v, _warnings)
640662
local dash
641663
if v == 'none' then
@@ -763,6 +785,7 @@ return {
763785
translate_border_style = translate_border_style,
764786
translate_border_color = translate_border_color,
765787
translate_font_weight = translate_font_weight,
788+
translate_font_family_list = translate_font_family_list,
766789
consume_width = consume_width,
767790
consume_style = consume_style,
768791
consume_color = consume_color

src/resources/filters/quarto-post/typst-brand-yaml.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function render_typst_brand_yaml()
126126
if headings and next(headings) then
127127
quarto.doc.include_text('in-header', table.concat({
128128
'#show heading: set text(',
129-
conditional_entry('font', headings.family),
129+
conditional_entry('font', headings.family and _quarto.modules.typst.css.translate_font_family_list(headings.family), false),
130130
conditional_entry('weight', _quarto.modules.typst.css.translate_font_weight(headings.weight)),
131131
conditional_entry('style', headings.style),
132132
conditional_entry('fill', headings.color, false),
@@ -147,7 +147,7 @@ function render_typst_brand_yaml()
147147
if monospaceInline and next(monospaceInline) then
148148
quarto.doc.include_text('in-header', table.concat({
149149
'#show raw.where(block: false): set text(',
150-
conditional_entry('font', monospaceInline.family),
150+
conditional_entry('font', monospaceInline.family and _quarto.modules.typst.css.translate_font_family_list(monospaceInline.family), false),
151151
conditional_entry('weight', _quarto.modules.typst.css.translate_font_weight(monospaceInline.weight)),
152152
conditional_entry('size', monospaceInline.size, false),
153153
conditional_entry('fill', monospaceInline.color, false),
@@ -166,7 +166,7 @@ function render_typst_brand_yaml()
166166
if monospaceBlock and next(monospaceBlock) then
167167
quarto.doc.include_text('in-header', table.concat({
168168
'#show raw.where(block: true): set text(',
169-
conditional_entry('font', monospaceBlock.family),
169+
conditional_entry('font', monospaceBlock.family and _quarto.modules.typst.css.translate_font_family_list(monospaceBlock.family), false),
170170
conditional_entry('weight', _quarto.modules.typst.css.translate_font_weight(monospaceBlock.weight)),
171171
conditional_entry('size', monospaceBlock.size, false),
172172
conditional_entry('fill', monospaceBlock.color, false),
@@ -318,7 +318,7 @@ function render_typst_brand_yaml()
318318
local base = _quarto.modules.brand.get_typography(brandMode, 'base')
319319
if base and next(base) then
320320
meta.brand.typography.base = {
321-
family = base.family,
321+
family = base.family and pandoc.RawInline('typst', _quarto.modules.typst.css.translate_font_family_list(base.family)),
322322
size = base.size,
323323
}
324324
end
@@ -332,8 +332,9 @@ function render_typst_brand_yaml()
332332
color = color and pandoc.RawInline('typst', color)
333333
local weight = _quarto.modules.typst.css.translate_font_weight(headings.weight or base.weight)
334334
weight = weight and pandoc.RawInline('typst', tostring(quote_string(weight)))
335+
local family = headings.family or base.family
335336
meta.brand.typography.headings = {
336-
family = headings.family or base.family,
337+
family = family and pandoc.RawInline('typst', _quarto.modules.typst.css.translate_font_family_list(family)),
337338
weight = weight,
338339
style = headings.style or base.style,
339340
decoration = headings.decoration or base.decoration,

src/resources/filters/quarto-post/typst-css-property-processing.lua

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ function render_typst_css_property_processing()
3232
end
3333
end
3434

35-
local function dequote(s)
36-
return s:gsub('^["\']', ''):gsub('["\']$', '')
37-
end
38-
39-
local function quote(s)
40-
return '"' .. s .. '"'
41-
end
42-
4335
local function translate_vertical_align(va)
4436
if va == 'top' then
4537
return 'top'
@@ -270,15 +262,6 @@ function render_typst_css_property_processing()
270262
end
271263
return span
272264
end
273-
274-
local function translate_string_list(sl)
275-
local strings = {}
276-
for s in sl:gmatch('([^,]+)') do
277-
s = s:gsub('^%s+', '')
278-
table.insert(strings, quote(dequote(s)))
279-
end
280-
return '(' .. table.concat(strings, ', ') ..')'
281-
end
282265

283266
return {
284267
Table = function(tab)
@@ -288,7 +271,7 @@ function render_typst_css_property_processing()
288271
for clause in tabstyle:gmatch('([^;]+)') do
289272
local k, v = to_kv(clause)
290273
if k == 'font-family' then
291-
tab.attributes['typst:text:font'] = translate_string_list(v)
274+
tab.attributes['typst:text:font'] = _quarto.format.typst.css.translate_font_family_list(v)
292275
end
293276
if k == 'font-size' then
294277
tab.attributes['typst:text:size'] = _quarto.format.typst.css.translate_length(v, _warnings)
@@ -320,7 +303,7 @@ function render_typst_css_property_processing()
320303
for clause in divstyle:gmatch('([^;]+)') do
321304
local k, v = to_kv(clause)
322305
if k == 'font-family' then
323-
div.attributes['typst:text:font'] = translate_string_list(v)
306+
div.attributes['typst:text:font'] = _quarto.format.typst.css.translate_font_family_list(v)
324307
elseif k == 'font-size' then
325308
div.attributes['typst:text:size'] = _quarto.format.typst.css.translate_length(v, _warnings)
326309
elseif k == 'background-color' then

src/resources/formats/typst/pandoc/quarto/typst-show.typ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $endif$
3838
$if(mainfont)$
3939
font: ("$mainfont$",),
4040
$elseif(brand.typography.base.family)$
41-
font: ("$brand.typography.base.family$",),
41+
font: $brand.typography.base.family$,
4242
$endif$
4343
$if(fontsize)$
4444
fontsize: $fontsize$,
@@ -47,7 +47,7 @@ $elseif(brand.typography.base.size)$
4747
$endif$
4848
$if(title)$
4949
$if(brand.typography.headings.family)$
50-
heading-family: ("$brand.typography.headings.family$",),
50+
heading-family: $brand.typography.headings.family$,
5151
$endif$
5252
$if(brand.typography.headings.weight)$
5353
heading-weight: $brand.typography.headings.weight$,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
format:
3+
typst:
4+
keep-typ: true
5+
brand:
6+
typography:
7+
fonts:
8+
- family: Barrio
9+
source: google
10+
- family: Roboto
11+
source: google
12+
- family: Inconsolata
13+
source: google
14+
base: InvalidFont, Roboto,
15+
headings: Barrio, Times New Roman
16+
monospace: InvalidFont, Inconsolata
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- 'font: \("InvalidFont", "Roboto"\),'
23+
- '#show raw.where\(block: false\): set text\(font: \("InvalidFont", "Inconsolata"\), \)'
24+
- '#show raw.where\(block: true\): set text\(font: \("InvalidFont", "Inconsolata"\), \)'
25+
- []
26+
ensurePdfRegexMatches:
27+
-
28+
- 'Base is \("invalidfont", "roboto"\)'
29+
- 'Heading is \("barrio", "times new roman"\)'
30+
- []
31+
---
32+
33+
```{=typst}
34+
#set text(fallback: false)
35+
```
36+
37+
## Base font
38+
39+
Base is `#context text.font`{=typst}
40+
41+
{{< lipsum 1 >}}
42+
43+
## Heading is `#context text.font`{=typst}
44+
45+
46+
::: {style="font-family: Barrio"}
47+
{{< lipsum 1 >}}
48+
:::
49+
50+
```js
51+
const fib = num => num < 2 ? num : fib(num-1) + fib(num - 2);
52+
console.log(fib(12))
53+
```

tests/docs/smoke-all/typst/brand-yaml/typography/kitchen-sink-1/brand-typography.qmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ _quarto:
3232
- 'heading-style: "normal",$'
3333
- 'heading-color: rgb\("#0b8005"\),$'
3434
- 'heading-line-height: 0\.25em,$'
35-
- '^#show heading: set text\(font: "Montserrat", weight: 800, style: "normal", fill: rgb\("#0b8005"\), \)$'
35+
- '^#show heading: set text\(font: \("Montserrat",\), weight: 800, style: "normal", fill: rgb\("#0b8005"\), \)$'
3636
- '^#show heading: set par\(leading: 0.25em\)$'
3737

38-
- '^#show raw.where\(block: false\): set text\(font: "Inconsolata", weight: 600, size: 0.57\*14pt, fill: rgb\(8, 111, 15\), \)$'
38+
- '^#show raw.where\(block: false\): set text\(font: \("Inconsolata",\), weight: 600, size: 0.57\*14pt, fill: rgb\(8, 111, 15\), \)$'
3939
- '^#show raw.where\(block: false\): content => highlight\(fill: rgb\(255, 250, 224\), content\)$'
4040

41-
- '^#show raw.where\(block: true\): set text\(font: "Inconsolata", size: 18pt, fill: rgb\(245, 255, 250\), \)$'
41+
- '^#show raw.where\(block: true\): set text\(font: \("Inconsolata",\), size: 18pt, fill: rgb\(245, 255, 250\), \)$'
4242
- '^#show raw.where\(block: true\): set block\(fill: rgb\("#77aae1"\)\)$'
4343
- '^#show raw.where\(block: true\): set par\(leading: 1\.25em\)$'
4444

tests/docs/smoke-all/typst/brand-yaml/typography/kitchen-sink-2/brand-typography.qmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ _quarto:
3030
- 'heading-style: "normal",$'
3131
- 'heading-color: rgb\("#042f02"\),$'
3232
- 'heading-line-height: 0\.25em,$'
33-
- '^#show heading: set text\(font: "Raleway", weight: 500, style: "normal", fill: rgb\("#042f02"\), \)$'
33+
- '^#show heading: set text\(font: \("Raleway",\), weight: 500, style: "normal", fill: rgb\("#042f02"\), \)$'
3434
- '^#show heading: set par\(leading: 0.25em\)$'
3535

36-
- '^#show raw.where\(block: false\): set text\(font: "Space Mono", weight: 400, size: 0.75\*12pt, fill: rgb\(8, 111, 15\), \)$'
36+
- '^#show raw.where\(block: false\): set text\(font: \("Space Mono",\), weight: 400, size: 0.75\*12pt, fill: rgb\(8, 111, 15\), \)$'
3737
- '^#show raw.where\(block: false\): content => highlight\(fill: rgb\(255, 250, 224\), content\)$'
3838

39-
- '^#show raw.where\(block: true\): set text\(font: "Space Mono", weight: 400, size: 0.75\*12pt, fill: rgb\("#eee"\), \)$'
39+
- '^#show raw.where\(block: true\): set text\(font: \("Space Mono",\), weight: 400, size: 0.75\*12pt, fill: rgb\("#eee"\), \)$'
4040
- '^#show raw.where\(block: true\): set block\(fill: rgb\("#0a3c07"\)\)$'
4141

4242
- '^#show link: set text\(weight: 200, fill: maroon, \)$'

0 commit comments

Comments
 (0)