Skip to content

Commit 9a9c7b9

Browse files
typst css: font-weight and font-style
1 parent af8db66 commit 9a9c7b9

File tree

9 files changed

+236
-8
lines changed

9 files changed

+236
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
format:
3+
html:
4+
quality: 1
5+
pdf:
6+
quality: na
7+
typst:
8+
quality: 2
9+
comment: "td, span only"
10+
dashboard:
11+
quality: 1
12+
docx:
13+
quality: na
14+
pptx:
15+
quality: na
16+
keep-typ: true
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- '#block\[\s*#set text\(style: "italic"\); Italic div\s*\]'
23+
- []
24+
---
25+
26+
::: {style="font-style: italic"}
27+
28+
Italic div
29+
30+
:::
31+
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
format:
3+
html:
4+
quality: 1
5+
pdf:
6+
quality: na
7+
typst:
8+
quality: 2
9+
comment: "td, span only"
10+
dashboard:
11+
quality: 1
12+
docx:
13+
quality: na
14+
pptx:
15+
quality: na
16+
keep-typ: true
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- '\[#set text\(style: "italic"\); B\]'
23+
- []
24+
---
25+
26+
```{=html}
27+
<table>
28+
<tr><td>A</td><td style="font-style: italic;">B</td></tr>
29+
</table>
30+
```
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
format:
3+
html:
4+
quality: 1
5+
pdf:
6+
quality: na
7+
typst:
8+
quality: 2
9+
comment: "td, span only"
10+
dashboard:
11+
quality: 1
12+
docx:
13+
quality: na
14+
pptx:
15+
quality: na
16+
keep-typ: true
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- '\[#set text\(style: "normal"\); B\]'
23+
- []
24+
---
25+
26+
```{=html}
27+
<table>
28+
<tr><td>A</td><td style="font-style: normal;">B</td></tr>
29+
</table>
30+
```
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
format:
3+
html:
4+
quality: 1
5+
pdf:
6+
quality: na
7+
typst:
8+
quality: 2
9+
comment: "td, span only"
10+
dashboard:
11+
quality: 1
12+
docx:
13+
quality: na
14+
pptx:
15+
quality: na
16+
keep-typ: true
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- '\[#set text\(style: "oblique"\); B\]'
23+
- []
24+
---
25+
26+
```{=html}
27+
<table>
28+
<tr><td>A</td><td style="font-style: oblique;">B</td></tr>
29+
</table>
30+
```
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
format:
3+
html:
4+
quality: 1
5+
pdf:
6+
quality: na
7+
typst:
8+
quality: 2
9+
comment: "td, span only"
10+
dashboard:
11+
quality: 1
12+
docx:
13+
quality: na
14+
pptx:
15+
quality: na
16+
keep-typ: true
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- '#block\[\s*#set text\(weight: "semibold"\); Demi-bold div\s*\]'
23+
- []
24+
---
25+
26+
::: {style="font-weight: demi-bold"}
27+
28+
Demi-bold div
29+
30+
:::
31+
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
format:
3+
html:
4+
quality: 1
5+
pdf:
6+
quality: na
7+
typst:
8+
quality: 2
9+
comment: "td, span only"
10+
dashboard:
11+
quality: 1
12+
docx:
13+
quality: na
14+
pptx:
15+
quality: na
16+
keep-typ: true
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- '\[#set text\(fill: rgb\(255, 0, 255\)\); B\]'
23+
- []
24+
---
25+
26+
```{=html}
27+
<table>
28+
<tr><td>A</td><td style="color: magenta;">B</td></tr>
29+
</table>
30+
```
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
format:
3+
html:
4+
quality: 1
5+
pdf:
6+
quality: na
7+
typst:
8+
quality: 2
9+
comment: "td, span only"
10+
dashboard:
11+
quality: 1
12+
docx:
13+
quality: na
14+
pptx:
15+
quality: na
16+
keep-typ: true
17+
_quarto:
18+
tests:
19+
typst:
20+
ensureTypstFileRegexMatches:
21+
-
22+
- '\[#set text\(weight: "extralight"\); B\]'
23+
- []
24+
---
25+
26+
```{=html}
27+
<table>
28+
<tr><td>A</td><td style="font-weight: ultra-light;">B</td></tr>
29+
</table>
30+
```
31+

src/resources/filters/modules/typst_css.lua

+9-8
Original file line numberDiff line numberDiff line change
@@ -598,40 +598,39 @@ end
598598
local same_weights = {
599599
'thin',
600600
'light',
601-
'normal',
602601
'regular',
603602
'medium',
604603
'bold',
605604
'black',
606605
}
607606

608607
local weight_synonyms = {
608+
['normal'] = 'regular',
609609
['ultra-light'] = 'extra-light',
610610
['demi-bold'] = 'semi-bold',
611611
['ultra-bold'] = 'extra-bold',
612612
}
613613

614614
local dashed_weights = {
615615
'extra-light',
616-
'ultra-light',
617616
'semi-bold',
618-
'demi-bold',
619617
'extra-bold',
620-
'ultra-bold',
621618
}
622619

623620
local function translate_font_weight(w, warnings)
624621
if not w then return nil end
625622
local num = tonumber(w)
626623
if num and 1 <= num and num <= 1000 then
627624
return num
628-
elseif tcontains(same_weights, w) then
625+
end
626+
w = weight_synonyms[w] or w
627+
if tcontains(same_weights, w) then
629628
return w
630-
elseif tcontains(dashed_weights, w) then
631-
w = weight_synonyms[w] or w
629+
end
630+
if tcontains(dashed_weights, w) then
632631
return w:gsub('-', '')
633632
else
634-
output_warning(warnings, 'invalid font weight ' .. tostring(w))
633+
output_warning(null, 'invalid font weight ' .. tostring(w))
635634
return nil
636635
end
637636
end
@@ -770,6 +769,8 @@ local function expand_side_shorthand(items, context, warnings)
770769
end
771770

772771
return {
772+
quote = quote,
773+
dequote = dequote,
773774
set_brand_mode = set_brand_mode,
774775
parse_color = parse_color,
775776
parse_opacity = parse_opacity,

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

+8
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ function render_typst_css_property_processing()
170170
opacity = _quarto.format.typst.css.parse_opacity(v, _warnings)
171171
elseif k == 'font-size' then
172172
cell.attributes['typst:text:size'] = _quarto.format.typst.css.translate_length(v, _warnings)
173+
elseif k == 'font-weight' then
174+
cell.attributes['typst:text:weight'] = _quarto.format.typst.css.quote(_quarto.format.typst.css.translate_font_weight(v, _warnings))
175+
elseif k == 'font-style' then
176+
cell.attributes['typst:text:style'] = _quarto.format.typst.css.quote(v)
173177
elseif k == 'vertical-align' then
174178
local a = translate_vertical_align(v)
175179
if a then table.insert(aligns, a) end
@@ -306,6 +310,10 @@ function render_typst_css_property_processing()
306310
div.attributes['typst:text:font'] = _quarto.format.typst.css.translate_font_family_list(v)
307311
elseif k == 'font-size' then
308312
div.attributes['typst:text:size'] = _quarto.format.typst.css.translate_length(v, _warnings)
313+
elseif k == 'font-weight' then
314+
div.attributes['typst:text:weight'] = _quarto.format.typst.css.quote(_quarto.format.typst.css.translate_font_weight(v, _warnings))
315+
elseif k == 'font-style' then
316+
div.attributes['typst:text:style'] = _quarto.format.typst.css.quote(v)
309317
elseif k == 'background-color' then
310318
div.attributes['typst:fill'] = _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(v, _warnings), nil, _warnings)
311319
elseif k == 'color' then

0 commit comments

Comments
 (0)