Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ clap = { version = "4.4", features = ["derive", "env", "wrap_help"] }
clap_complete = "4.2.1"
clap_mangen = "0.2.10"
codespan-reporting = "0.11"
codex = { git = "https://github.com/typst/codex", rev = "9ac86f9" }
codex = { git = "https://github.com/typst/codex", rev = "775d828" }
color-print = "0.3.6"
comemo = "0.5.0"
csv = "1"
Expand Down
4 changes: 2 additions & 2 deletions crates/typst-eval/src/markup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ impl Eval for ast::Escape<'_> {
type Output = Value;

fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
Ok(Value::Symbol(Symbol::single(self.get())))
Ok(Value::Symbol(Symbol::runtime_char(self.get())))
}
}

impl Eval for ast::Shorthand<'_> {
type Output = Value;

fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
Ok(Value::Symbol(Symbol::single(self.get())))
Ok(Value::Symbol(Symbol::runtime_char(self.get())))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/typst-eval/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Eval for ast::MathShorthand<'_> {
type Output = Value;

fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
Ok(Value::Symbol(Symbol::single(self.get())))
Ok(Value::Symbol(Symbol::runtime_char(self.get())))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/typst-ide/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub enum CompletionKind {
/// A font family.
Font,
/// A symbol.
Symbol(char),
Symbol(EcoString),
}

/// Complete in comments. Or rather, don't!
Expand Down Expand Up @@ -450,7 +450,7 @@ fn field_access_completions(
for modifier in symbol.modifiers() {
if let Ok(modified) = symbol.clone().modified((), modifier) {
ctx.completions.push(Completion {
kind: CompletionKind::Symbol(modified.get()),
kind: CompletionKind::Symbol(modified.get().into()),
label: modifier.into(),
apply: None,
detail: None,
Expand Down Expand Up @@ -1381,7 +1381,7 @@ impl<'a> CompletionContext<'a> {
kind: kind.unwrap_or_else(|| match value {
Value::Func(_) => CompletionKind::Func,
Value::Type(_) => CompletionKind::Type,
Value::Symbol(s) => CompletionKind::Symbol(s.get()),
Value::Symbol(s) => CompletionKind::Symbol(s.get().into()),
_ => CompletionKind::Constant,
}),
label,
Expand Down
10 changes: 8 additions & 2 deletions crates/typst-layout/src/inline/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub fn line<'a>(

// Trim the line at the end, if necessary for this breakpoint.
let trim = range.start + breakpoint.trim(full).len();
let trimmed_range = range.start..trim;

// Collect the items for the line.
let mut items = Items::new();
Expand Down Expand Up @@ -179,7 +180,8 @@ pub fn line<'a>(
trim_weak_spacing(&mut items);

// Deal with CJ characters at line boundaries.
adjust_cj_at_line_boundaries(p, full, &mut items);
// Use the trimmed range for robust boundary checks.
adjust_cj_at_line_boundaries(p, trimmed_range, &mut items);

// Compute the line's width.
let width = items.iter().map(Item::natural_width).sum();
Expand Down Expand Up @@ -319,7 +321,11 @@ fn collect_range<'a>(
///
/// See Requirements for Chinese Text Layout, Section 3.1.6.3 Compression of
/// punctuation marks at line start or line end.
fn adjust_cj_at_line_boundaries(p: &Preparation, text: &str, items: &mut Items) {
///
/// The `range` should only contain regular texts, with linebreaks trimmed.
fn adjust_cj_at_line_boundaries(p: &Preparation, range: Range, items: &mut Items) {
let text = &p.text[range];

if text.starts_with(BEGIN_PUNCT_PAT)
|| (p.config.cjk_latin_spacing && text.starts_with(is_of_cj_script))
{
Expand Down
36 changes: 22 additions & 14 deletions crates/typst-layout/src/math/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rustybuzz::{BufferFlags, UnicodeBuffer};
use ttf_parser::GlyphId;
use ttf_parser::math::{GlyphAssembly, GlyphConstruction, GlyphPart};
use typst_library::World;
use typst_library::diag::warning;
use typst_library::foundations::StyleChain;
use typst_library::diag::{At, HintedStrResult, SourceResult, bail, warning};
use typst_library::foundations::{Repr, StyleChain};
use typst_library::introspection::Tag;
use typst_library::layout::{
Abs, Axes, Axis, Corner, Em, Frame, FrameItem, Point, Size, VAlignment,
Expand Down Expand Up @@ -307,7 +307,7 @@ impl GlyphFragment {
styles: StyleChain,
c: char,
span: Span,
) -> Option<Self> {
) -> SourceResult<Option<Self>> {
Self::new(ctx.engine.world, styles, c.encode_utf8(&mut [0; 4]), span)
}

Expand All @@ -318,18 +318,22 @@ impl GlyphFragment {
styles: StyleChain,
text: &str,
span: Span,
) -> Option<GlyphFragment> {
) -> SourceResult<Option<GlyphFragment>> {
assert!(text.graphemes(true).count() == 1);

let (c, font, mut glyph) = shape(
let Some((c, font, mut glyph)) = shape(
world,
variant(styles),
features(styles),
language(styles),
styles.get(TextElem::fallback),
text,
families(styles).collect(),
)?;
)
.at(span)?
else {
return Ok(None);
};
glyph.span.0 = span;

let limits = Limits::for_char(c);
Expand Down Expand Up @@ -369,7 +373,7 @@ impl GlyphFragment {
modifiers: FrameModifiers::get_in(styles),
};
fragment.update_glyph();
Some(fragment)
Ok(Some(fragment))
}

/// Sets element id and boxes in appropriate way without changing other
Expand Down Expand Up @@ -847,7 +851,7 @@ fn shape(
fallback: bool,
text: &str,
families: Vec<&FontFamily>,
) -> Option<(char, Font, Glyph)> {
) -> HintedStrResult<Option<(char, Font, Glyph)>> {
let mut used = vec![];
let buffer = UnicodeBuffer::new();
shape_glyph(
Expand All @@ -874,7 +878,7 @@ fn shape_glyph<'a>(
fallback: bool,
text: &str,
mut families: impl Iterator<Item = &'a FontFamily> + Clone,
) -> Option<(char, Font, Glyph)> {
) -> HintedStrResult<Option<(char, Font, Glyph)>> {
// Find the next available family.
let book = world.book();
let mut selection = None;
Expand Down Expand Up @@ -913,9 +917,9 @@ fn shape_glyph<'a>(
span: (Span::detached(), 0),
};
let c = text.chars().next().unwrap();
return Some((c, font, glyph));
return Ok(Some((c, font, glyph)));
}
return None;
return Ok(None);
};

// This font has been exhausted and will not be used again.
Expand Down Expand Up @@ -944,9 +948,13 @@ fn shape_glyph<'a>(

let buffer = rustybuzz::shape_with_plan(font.rusty(), &plan, buffer);
match buffer.len() {
0 => return None,
0 => return Ok(None),
1 => {}
_ => unreachable!(),
// TODO: Deal with multiple glyphs.
_ => bail!(
"shaping the text `{}` yielded more than one glyph", text.repr();
hint: "please report this as a bug",
),
}

let info = buffer.glyph_infos()[0];
Expand All @@ -969,7 +977,7 @@ fn shape_glyph<'a>(
span: (Span::detached(), 0),
};
let c = text[cluster..].chars().next().unwrap();
Some((c, font, glyph))
Ok(Some((c, font, glyph)))
} else {
shape_glyph(
world,
Expand Down
2 changes: 1 addition & 1 deletion crates/typst-layout/src/math/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn layout_body(
// way too big.
// This will never panic as a paren will never shape into nothing.
let paren =
GlyphFragment::new_char(ctx, styles.chain(&denom_style), '(', Span::detached())
GlyphFragment::new_char(ctx, styles.chain(&denom_style), '(', Span::detached())?
.unwrap();

for (column, col) in columns.iter().zip(&mut cols) {
Expand Down
68 changes: 37 additions & 31 deletions crates/typst-layout/src/math/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn layout_inline_text(

// This won't panic as ASCII digits and '.' will never end up as
// nothing after shaping.
let glyph = GlyphFragment::new_char(ctx, styles, c, span).unwrap();
let glyph = GlyphFragment::new_char(ctx, styles, c, span)?.unwrap();
fragments.push(glyph.into());
}
let frame = MathRun::new(fragments).into_frame(styles);
Expand Down Expand Up @@ -129,41 +129,47 @@ pub fn layout_symbol(
ctx: &mut MathContext,
styles: StyleChain,
) -> SourceResult<()> {
// Switch dotless char to normal when we have the dtls OpenType feature.
// This should happen before the main styling pass.
let dtls = style_dtls();
let (unstyled_c, symbol_styles) = match (try_dotless(elem.text), ctx.font().clone()) {
(Some(c), font) if has_dtls_feat(&font) => (c, styles.chain(&dtls)),
_ => (elem.text, styles),
};

let variant = styles.get(EquationElem::variant);
let bold = styles.get(EquationElem::bold);
let italic = styles.get(EquationElem::italic);

let style = MathStyle::select(unstyled_c, variant, bold, italic);
let text: EcoString = to_style(unstyled_c, style).collect();

if let Some(mut glyph) =
GlyphFragment::new(ctx.engine.world, symbol_styles, &text, elem.span())
{
if glyph.class == MathClass::Large {
if styles.get(EquationElem::size) == MathSize::Display {
let height = glyph
.item
.font
.math()
.display_operator_min_height
.at(glyph.item.size);
glyph.stretch_vertical(ctx, height);
};
// TeXbook p 155. Large operators are always vertically centered on
// the axis.
glyph.center_on_axis();
let dtls = style_dtls();
let has_dtls_feat = has_dtls_feat(ctx.font());
for cluster in elem.text.graphemes(true) {
// Switch dotless char to normal when we have the dtls OpenType feature.
// This should happen before the main styling pass.
let mut enable_dtls = false;
let text: EcoString = cluster
.chars()
.flat_map(|mut c| {
if has_dtls_feat && let Some(d) = try_dotless(c) {
enable_dtls = true;
c = d;
}
to_style(c, MathStyle::select(c, variant, bold, italic))
})
.collect();
let styles = if enable_dtls { styles.chain(&dtls) } else { styles };

if let Some(mut glyph) =
GlyphFragment::new(ctx.engine.world, styles, &text, elem.span())?
{
if glyph.class == MathClass::Large {
if styles.get(EquationElem::size) == MathSize::Display {
let height = glyph
.item
.font
.math()
.display_operator_min_height
.at(glyph.item.size);
glyph.stretch_vertical(ctx, height);
};
// TeXbook p 155. Large operators are always vertically centered on
// the axis.
glyph.center_on_axis();
}
ctx.push(glyph);
}
ctx.push(glyph);
}

Ok(())
}

Expand Down
Loading
Loading