Compare commits

...

3 Commits

Author SHA1 Message Date
Siddhant Agarwal
9e571580a8
Merge 3d7a289334b56f39a7b8a5bc005735e0b6484537 into 627f5b9d4f39f91affc529f276bb500d42887dae 2025-07-18 01:28:19 +08:00
Lachlan Kermode
627f5b9d4f
Add show rule for smallcaps in HTML (#6600) 2025-07-17 16:09:13 +00:00
siddhantdev
3d7a289334 Allow augment line at the beginning and end of a matrix
Allows augment lines at the begginnig and end of a matrix.
Augment 0 draws a line before the first column/row and augment n draws
a line after the last column/row of a matrix with n columns/rows.

-1 now draws a line after the last column/row of the matrix, not before it.
2025-07-10 13:59:45 +05:30
6 changed files with 78 additions and 20 deletions

View File

@ -14,8 +14,8 @@ use typst_library::model::{
RefElem, StrongElem, TableCell, TableElem, TermsElem,
};
use typst_library::text::{
HighlightElem, LinebreakElem, OverlineElem, RawElem, RawLine, SpaceElem, StrikeElem,
SubElem, SuperElem, UnderlineElem,
HighlightElem, LinebreakElem, OverlineElem, RawElem, RawLine, SmallcapsElem,
SpaceElem, StrikeElem, SubElem, SuperElem, UnderlineElem,
};
use typst_library::visualize::ImageElem;
@ -47,6 +47,7 @@ pub fn register(rules: &mut NativeRuleMap) {
rules.register(Html, OVERLINE_RULE);
rules.register(Html, STRIKE_RULE);
rules.register(Html, HIGHLIGHT_RULE);
rules.register(Html, SMALLCAPS_RULE);
rules.register(Html, RAW_RULE);
rules.register(Html, RAW_LINE_RULE);
@ -390,6 +391,20 @@ const STRIKE_RULE: ShowFn<StrikeElem> =
const HIGHLIGHT_RULE: ShowFn<HighlightElem> =
|elem, _, _| Ok(HtmlElem::new(tag::mark).with_body(Some(elem.body.clone())).pack());
const SMALLCAPS_RULE: ShowFn<SmallcapsElem> = |elem, _, styles| {
Ok(HtmlElem::new(tag::span)
.with_attr(
attr::style,
if elem.all.get(styles) {
"font-variant-caps: all-small-caps"
} else {
"font-variant-caps: small-caps"
},
)
.with_body(Some(elem.body.clone()))
.pack())
};
const RAW_RULE: ShowFn<RawElem> = |elem, _, styles| {
let lines = elem.lines.as_deref().unwrap_or_default();

View File

@ -87,7 +87,7 @@ pub fn layout_mat(
let augment = elem.augment.resolve(styles);
if let Some(aug) = &augment {
for &offset in &aug.hline.0 {
if offset == 0 || offset.unsigned_abs() >= rows.len() {
if offset > rows.len() as isize || offset.unsigned_abs() > rows.len() + 1 {
bail!(
span,
"cannot draw a horizontal line after row {} of a matrix with {} rows",
@ -98,7 +98,7 @@ pub fn layout_mat(
}
for &offset in &aug.vline.0 {
if offset == 0 || offset.unsigned_abs() >= ncols {
if offset > ncols as isize || offset.unsigned_abs() > ncols + 1 {
bail!(
span,
"cannot draw a vertical line after column {} of a matrix with {} columns",
@ -165,7 +165,7 @@ fn layout_body(
..Default::default()
};
let (hline, vline, stroke) = match augment {
let (mut hline, mut vline, stroke) = match augment {
Some(augment) => {
// We need to get stroke here for ownership.
let stroke = augment.stroke.unwrap_or_default().unwrap_or(default_stroke);
@ -216,20 +216,48 @@ fn layout_body(
}
}
for line in hline.0.iter_mut() {
if *line < 0 {
*line += (nrows as isize) + 1
}
}
for line in vline.0.iter_mut() {
if *line < 0 {
*line += (ncols as isize) + 1
}
}
// For each row, combine maximum ascent and descent into a row height.
// Sum the row heights, then add the total height of the gaps between rows.
let total_height =
let mut total_height =
heights.iter().map(|&(a, b)| a + b).sum::<Abs>() + gap.y * (nrows - 1) as f64;
if hline.0.contains(&0_isize) {
total_height += gap.y;
}
if hline.0.contains(&(nrows as isize)) {
total_height += gap.y;
}
// Width starts at zero because it can't be calculated until later
let mut frame = Frame::soft(Size::new(Abs::zero(), total_height));
let mut x = Abs::zero();
if vline.0.contains(&(0_isize)) {
frame.push(
Point::with_x(x + half_gap.x),
line_item(total_height, true, stroke.clone(), span),
);
x += gap.x;
}
for (index, col) in cols.into_iter().enumerate() {
let AlignmentResult { points, width: rcol } = alignments(&col);
let mut y = Abs::zero();
let mut y = if hline.0.contains(&0_isize) { gap.y } else { Abs::zero() };
for (cell, &(ascent, descent)) in col.into_iter().zip(&heights) {
let cell = cell.into_line_frame(&points, alternator);
@ -251,9 +279,7 @@ fn layout_body(
x += rcol;
// If a vertical line should be inserted after this column
if vline.0.contains(&(index as isize + 1))
|| vline.0.contains(&(1 - ((ncols - index) as isize)))
{
if vline.0.contains(&(index as isize + 1)) {
frame.push(
Point::with_x(x + half_gap.x),
line_item(total_height, true, stroke.clone(), span),
@ -264,16 +290,17 @@ fn layout_body(
x += gap.x;
}
// Once all the columns are laid out, the total width can be calculated
let total_width = x - gap.x;
let total_width = if !(vline.0.contains(&(ncols as isize))) { x - gap.x } else { x };
// This allows the horizontal lines to be laid out
for line in hline.0 {
let real_line =
if line < 0 { nrows - line.unsigned_abs() } else { line as usize };
let offset = (heights[0..real_line].iter().map(|&(a, b)| a + b).sum::<Abs>()
+ gap.y * (real_line - 1) as f64)
+ half_gap.y;
let offset = if line == 0 {
gap.y
} else {
(heights[0..line as usize].iter().map(|&(a, b)| a + b).sum::<Abs>()
+ gap.y * (line - 1) as f64)
+ half_gap.y
};
frame.push(
Point::with_y(offset),

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p><span style="font-variant-caps: small-caps">Test 012</span><br><span style="font-variant-caps: all-small-caps">Test 012</span></p>
</body>
</html>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -96,12 +96,18 @@ $ mat(column-gap: #1em, 1, 2; 3, 4)
$ mat(10, 2, 3, 4; 5, 6, 7, 8; augment: #3) $,
$ mat(10, 2, 3, 4; 5, 6, 7, 8; augment: #(-1)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(hline: 0)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(hline: -4)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(hline: 2)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(hline: -1)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(hline: 1, vline: 1)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(hline: -2, vline: -2)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(vline: 2, stroke: 1pt + blue)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(vline: -1, stroke: 1pt + blue)) $,
$ mat(10, 2, 3, 4; 5, 6, 7, 8; augment: #4) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(vline: 3, stroke: 1pt + blue)) $,
$ mat(100, 2, 3; 4, 5, 6; 7, 8, 9; augment: #(vline: 0, stroke: 1pt + blue)) $,
$ mat(10, 2, 3, 4; 5, 6, 7, 8; augment: #(vline: -5, stroke: 1pt + blue)) $,
)
--- math-mat-augment-set ---
@ -115,8 +121,8 @@ $ mat(1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 1) $
#set math.mat(augment: none)
--- math-mat-augment-line-out-of-bounds ---
// Error: 3-37 cannot draw a vertical line after column 3 of a matrix with 3 columns
$ mat(1, 0, 0; 0, 1, 1; augment: #3) $,
// Error: 3-37 cannot draw a vertical line after column 4 of a matrix with 3 columns
$ mat(1, 0, 0; 0, 1, 1; augment: #4) $,
--- math-mat-align ---
$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: #left) $

View File

@ -11,6 +11,6 @@
#show smallcaps: set text(fill: red)
#smallcaps[Smallcaps]
--- smallcaps-all ---
--- smallcaps-all render html ---
#smallcaps(all: false)[Test 012] \
#smallcaps(all: true)[Test 012]