mirror of
https://github.com/typst/typst
synced 2025-05-15 01:25:28 +08:00
Never shrink lines in raw blocks (#3796)
This commit is contained in:
parent
cb24d10404
commit
444c8682ce
@ -73,7 +73,8 @@ pub(crate) fn layout_inline(
|
|||||||
let lines = linebreak(&engine, &p, region.x - p.hang);
|
let lines = linebreak(&engine, &p, region.x - p.hang);
|
||||||
|
|
||||||
// Stack the lines into one frame per region.
|
// Stack the lines into one frame per region.
|
||||||
finalize(&mut engine, &p, &lines, region, expand)
|
let shrink = ParElem::shrink_in(styles);
|
||||||
|
finalize(&mut engine, &p, &lines, region, expand, shrink)
|
||||||
}
|
}
|
||||||
|
|
||||||
let fragment = cached(
|
let fragment = cached(
|
||||||
@ -1191,6 +1192,7 @@ fn finalize(
|
|||||||
lines: &[Line],
|
lines: &[Line],
|
||||||
region: Size,
|
region: Size,
|
||||||
expand: bool,
|
expand: bool,
|
||||||
|
shrink: bool,
|
||||||
) -> SourceResult<Fragment> {
|
) -> SourceResult<Fragment> {
|
||||||
// Determine the paragraph's width: Full width of the region if we
|
// Determine the paragraph's width: Full width of the region if we
|
||||||
// should expand or there's fractional spacing, fit-to-width otherwise.
|
// should expand or there's fractional spacing, fit-to-width otherwise.
|
||||||
@ -1207,7 +1209,7 @@ fn finalize(
|
|||||||
// Stack the lines into one frame per region.
|
// Stack the lines into one frame per region.
|
||||||
let mut frames: Vec<Frame> = lines
|
let mut frames: Vec<Frame> = lines
|
||||||
.iter()
|
.iter()
|
||||||
.map(|line| commit(engine, p, line, width, region.y))
|
.map(|line| commit(engine, p, line, width, region.y, shrink))
|
||||||
.collect::<SourceResult<_>>()?;
|
.collect::<SourceResult<_>>()?;
|
||||||
|
|
||||||
// Prevent orphans.
|
// Prevent orphans.
|
||||||
@ -1243,6 +1245,7 @@ fn commit(
|
|||||||
line: &Line,
|
line: &Line,
|
||||||
width: Abs,
|
width: Abs,
|
||||||
full: Abs,
|
full: Abs,
|
||||||
|
shrink: bool,
|
||||||
) -> SourceResult<Frame> {
|
) -> SourceResult<Frame> {
|
||||||
let mut remaining = width - line.width - p.hang;
|
let mut remaining = width - line.width - p.hang;
|
||||||
let mut offset = Abs::zero();
|
let mut offset = Abs::zero();
|
||||||
@ -1289,12 +1292,12 @@ fn commit(
|
|||||||
let mut justification_ratio = 0.0;
|
let mut justification_ratio = 0.0;
|
||||||
let mut extra_justification = Abs::zero();
|
let mut extra_justification = Abs::zero();
|
||||||
|
|
||||||
let shrink = line.shrinkability();
|
let shrinkability = line.shrinkability();
|
||||||
let stretch = line.stretchability();
|
let stretch = line.stretchability();
|
||||||
if remaining < Abs::zero() && shrink > Abs::zero() {
|
if remaining < Abs::zero() && shrinkability > Abs::zero() && shrink {
|
||||||
// Attempt to reduce the length of the line, using shrinkability.
|
// Attempt to reduce the length of the line, using shrinkability.
|
||||||
justification_ratio = (remaining / shrink).max(-1.0);
|
justification_ratio = (remaining / shrinkability).max(-1.0);
|
||||||
remaining = (remaining + shrink).min(Abs::zero());
|
remaining = (remaining + shrinkability).min(Abs::zero());
|
||||||
} else if line.justify && fr.is_zero() {
|
} else if line.justify && fr.is_zero() {
|
||||||
// Attempt to increase the length of the line, using stretchability.
|
// Attempt to increase the length of the line, using stretchability.
|
||||||
if stretch > Abs::zero() {
|
if stretch > Abs::zero() {
|
||||||
|
@ -96,6 +96,15 @@ pub struct ParElem {
|
|||||||
#[resolve]
|
#[resolve]
|
||||||
pub hanging_indent: Length,
|
pub hanging_indent: Length,
|
||||||
|
|
||||||
|
/// Indicates wheter an overflowing line should be shrunk.
|
||||||
|
///
|
||||||
|
/// This property is set to `false` on raw blocks, because shrinking a line
|
||||||
|
/// could visually break the indentation.
|
||||||
|
#[ghost]
|
||||||
|
#[internal]
|
||||||
|
#[default(true)]
|
||||||
|
pub shrink: bool,
|
||||||
|
|
||||||
/// The contents of the paragraph.
|
/// The contents of the paragraph.
|
||||||
#[external]
|
#[external]
|
||||||
#[required]
|
#[required]
|
||||||
|
@ -16,7 +16,7 @@ use crate::foundations::{
|
|||||||
PlainText, Show, ShowSet, Smart, StyleChain, Styles, Synthesize, Value,
|
PlainText, Show, ShowSet, Smart, StyleChain, Styles, Synthesize, Value,
|
||||||
};
|
};
|
||||||
use crate::layout::{BlockElem, Em, HAlignment};
|
use crate::layout::{BlockElem, Em, HAlignment};
|
||||||
use crate::model::Figurable;
|
use crate::model::{Figurable, ParElem};
|
||||||
use crate::syntax::{split_newlines, LinkedNode, Span, Spanned};
|
use crate::syntax::{split_newlines, LinkedNode, Span, Spanned};
|
||||||
use crate::text::{
|
use crate::text::{
|
||||||
FontFamily, FontList, Hyphenate, Lang, LinebreakElem, LocalName, Region,
|
FontFamily, FontList, Hyphenate, Lang, LinebreakElem, LocalName, Region,
|
||||||
@ -440,13 +440,16 @@ impl Show for Packed<RawElem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ShowSet for Packed<RawElem> {
|
impl ShowSet for Packed<RawElem> {
|
||||||
fn show_set(&self, _: StyleChain) -> Styles {
|
fn show_set(&self, styles: StyleChain) -> Styles {
|
||||||
let mut out = Styles::new();
|
let mut out = Styles::new();
|
||||||
out.set(TextElem::set_overhang(false));
|
out.set(TextElem::set_overhang(false));
|
||||||
out.set(TextElem::set_hyphenate(Hyphenate(Smart::Custom(false))));
|
out.set(TextElem::set_hyphenate(Hyphenate(Smart::Custom(false))));
|
||||||
out.set(TextElem::set_size(TextSize(Em::new(0.8).into())));
|
out.set(TextElem::set_size(TextSize(Em::new(0.8).into())));
|
||||||
out.set(TextElem::set_font(FontList(vec![FontFamily::new("DejaVu Sans Mono")])));
|
out.set(TextElem::set_font(FontList(vec![FontFamily::new("DejaVu Sans Mono")])));
|
||||||
out.set(SmartQuoteElem::set_enabled(false));
|
out.set(SmartQuoteElem::set_enabled(false));
|
||||||
|
if self.block(styles) {
|
||||||
|
out.set(ParElem::set_shrink(false));
|
||||||
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BIN
tests/ref/layout/code-indent-shrink.png
Normal file
BIN
tests/ref/layout/code-indent-shrink.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.0 KiB |
28
tests/typ/layout/code-indent-shrink.typ
Normal file
28
tests/typ/layout/code-indent-shrink.typ
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// Spaces in raw blocks should not be shrunk
|
||||||
|
// as it would mess up the indentation of code
|
||||||
|
// https://github.com/typst/typst/issues/3191
|
||||||
|
|
||||||
|
---
|
||||||
|
#set par(justify: true)
|
||||||
|
|
||||||
|
#show raw.where(block: true): block.with(
|
||||||
|
fill: luma(240),
|
||||||
|
inset: 10pt,
|
||||||
|
)
|
||||||
|
|
||||||
|
#block(
|
||||||
|
width: 60%,
|
||||||
|
```py
|
||||||
|
for x in xs:
|
||||||
|
print("x=",x)
|
||||||
|
```
|
||||||
|
)
|
||||||
|
|
||||||
|
---
|
||||||
|
// In normal paragraphs, spaces should still be shrunk.
|
||||||
|
// The first line here serves as a reference, while the second
|
||||||
|
// uses non-breaking spaces to create an overflowing line
|
||||||
|
// (which should shrink).
|
||||||
|
~~~~No shrinking here
|
||||||
|
|
||||||
|
~~~~The~spaces~on~this~line~shrink
|
Loading…
x
Reference in New Issue
Block a user