mirror of
https://github.com/typst/typst
synced 2025-07-27 14:27:56 +08:00
Compare commits
1 Commits
0dd19b5f6c
...
d0d4e8e796
Author | SHA1 | Date | |
---|---|---|---|
|
d0d4e8e796 |
@ -300,7 +300,6 @@ impl GlyphFragment {
|
||||
);
|
||||
|
||||
let buffer = rustybuzz::shape_with_plan(font.rusty(), &plan, buffer);
|
||||
// TODO: deal with multiple glyphs.
|
||||
if buffer.len() != 1 {
|
||||
bail!(span, "did not get a single glyph after shaping {}", text);
|
||||
}
|
||||
|
@ -129,41 +129,44 @@ pub fn layout_symbol(
|
||||
ctx: &mut MathContext,
|
||||
styles: StyleChain,
|
||||
) -> SourceResult<()> {
|
||||
assert!(
|
||||
elem.text.len() <= 4 && elem.text.chars().count() == 1,
|
||||
"TODO: layout multi-char symbol"
|
||||
);
|
||||
let elem_char = elem
|
||||
.text
|
||||
.chars()
|
||||
.next()
|
||||
.expect("TODO: should an empty symbol value forbidden?");
|
||||
|
||||
// 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_char) {
|
||||
Some(c) if has_dtls_feat(ctx.font) => (c, styles.chain(&dtls)),
|
||||
_ => (elem_char, styles),
|
||||
};
|
||||
|
||||
let variant = styles.get(EquationElem::variant);
|
||||
let bold = styles.get(EquationElem::bold);
|
||||
let italic = styles.get(EquationElem::italic);
|
||||
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 };
|
||||
|
||||
let fragment: MathFragment =
|
||||
match GlyphFragment::new(ctx.font, styles, &text, elem.span()) {
|
||||
Ok(mut glyph) => {
|
||||
adjust_glyph_layout(&mut glyph, ctx, styles);
|
||||
glyph.into()
|
||||
}
|
||||
Err(_) => {
|
||||
// Not in the math font, fallback to normal inline text layout.
|
||||
// TODO: Should replace this with proper fallback in [`GlyphFragment::new`].
|
||||
layout_inline_text(&text, elem.span(), ctx, styles)?.into()
|
||||
}
|
||||
};
|
||||
ctx.push(fragment);
|
||||
}
|
||||
let style = MathStyle::select(unstyled_c, variant, bold, italic);
|
||||
let text: EcoString = to_style(unstyled_c, style).collect();
|
||||
|
||||
let fragment: MathFragment =
|
||||
match GlyphFragment::new(ctx.font, symbol_styles, &text, elem.span()) {
|
||||
Ok(mut glyph) => {
|
||||
adjust_glyph_layout(&mut glyph, ctx, styles);
|
||||
glyph.into()
|
||||
}
|
||||
Err(_) => {
|
||||
// Not in the math font, fallback to normal inline text layout.
|
||||
// TODO: Should replace this with proper fallback in [`GlyphFragment::new`].
|
||||
layout_inline_text(&text, elem.span(), ctx, styles)?.into()
|
||||
}
|
||||
};
|
||||
ctx.push(fragment);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ use serde::{Serialize, Serializer};
|
||||
use typst_syntax::{Span, Spanned, is_ident};
|
||||
use typst_utils::hash128;
|
||||
|
||||
use crate::diag::{DeprecationSink, SourceResult, StrResult, bail, error};
|
||||
use crate::diag::{DeprecationSink, SourceResult, StrResult, bail};
|
||||
use crate::foundations::{
|
||||
Array, Content, Func, NativeElement, NativeFunc, Packed, PlainText, Repr as _, cast,
|
||||
elem, func, scope, ty,
|
||||
@ -231,30 +231,15 @@ impl Symbol {
|
||||
// A list of modifiers, cleared & reused in each iteration.
|
||||
let mut modifiers = Vec::new();
|
||||
|
||||
let mut errors = ecow::eco_vec![];
|
||||
|
||||
// Validate the variants.
|
||||
'variants: for (i, &Spanned { ref v, span }) in variants.iter().enumerate() {
|
||||
for (i, &Spanned { ref v, span }) in variants.iter().enumerate() {
|
||||
modifiers.clear();
|
||||
|
||||
if v.1.is_empty() {
|
||||
errors.push(if v.0.is_empty() {
|
||||
error!(span, "empty default variant")
|
||||
} else {
|
||||
error!(span, "empty variant: {}", v.0.repr())
|
||||
});
|
||||
}
|
||||
|
||||
if !v.0.is_empty() {
|
||||
// Collect all modifiers.
|
||||
for modifier in v.0.split('.') {
|
||||
if !is_ident(modifier) {
|
||||
errors.push(error!(
|
||||
span,
|
||||
"invalid symbol modifier: {}",
|
||||
modifier.repr()
|
||||
));
|
||||
continue 'variants;
|
||||
bail!(span, "invalid symbol modifier: {}", modifier.repr());
|
||||
}
|
||||
modifiers.push(modifier);
|
||||
}
|
||||
@ -265,34 +250,29 @@ impl Symbol {
|
||||
|
||||
// Ensure that there are no duplicate modifiers.
|
||||
if let Some(ms) = modifiers.windows(2).find(|ms| ms[0] == ms[1]) {
|
||||
errors.push(error!(
|
||||
bail!(
|
||||
span, "duplicate modifier within variant: {}", ms[0].repr();
|
||||
hint: "modifiers are not ordered, so each one may appear only once"
|
||||
));
|
||||
continue 'variants;
|
||||
)
|
||||
}
|
||||
|
||||
// Check whether we had this set of modifiers before.
|
||||
let hash = hash128(&modifiers);
|
||||
if let Some(&i) = seen.get(&hash) {
|
||||
errors.push(if v.0.is_empty() {
|
||||
error!(span, "duplicate default variant")
|
||||
if v.0.is_empty() {
|
||||
bail!(span, "duplicate default variant");
|
||||
} else if v.0 == variants[i].v.0 {
|
||||
error!(span, "duplicate variant: {}", v.0.repr())
|
||||
bail!(span, "duplicate variant: {}", v.0.repr());
|
||||
} else {
|
||||
error!(
|
||||
bail!(
|
||||
span, "duplicate variant: {}", v.0.repr();
|
||||
hint: "variants with the same modifiers are identical, regardless of their order"
|
||||
)
|
||||
});
|
||||
continue 'variants;
|
||||
}
|
||||
}
|
||||
|
||||
seen.insert(hash, i);
|
||||
}
|
||||
if !errors.is_empty() {
|
||||
return Err(errors);
|
||||
}
|
||||
|
||||
let list = variants
|
||||
.into_iter()
|
||||
@ -406,6 +386,7 @@ pub struct SymbolVariant(EcoString, EcoString);
|
||||
|
||||
cast! {
|
||||
SymbolVariant,
|
||||
c: char => Self(EcoString::new(), c.into()),
|
||||
s: EcoString => Self(EcoString::new(), s),
|
||||
array: Array => {
|
||||
let mut iter = array.into_iter();
|
||||
|
@ -733,12 +733,10 @@ fn symbols_model(resolver: &dyn Resolver, group: &GroupData) -> SymbolsModel {
|
||||
name,
|
||||
markup_shorthand: shorthand(typst::syntax::ast::Shorthand::LIST),
|
||||
math_shorthand: shorthand(typst::syntax::ast::MathShorthand::LIST),
|
||||
// Matches `typst_layout::math::GlyphFragment::new`
|
||||
math_class: value.chars().next().and_then(|c| {
|
||||
math_class: value_char.and_then(|c| {
|
||||
typst_utils::default_math_class(c).map(math_class_name)
|
||||
}),
|
||||
value: value.into(),
|
||||
// Matches casting `Symbol` to `Accent`
|
||||
accent: value_char
|
||||
.is_some_and(|c| typst::math::Accent::combine(c).is_some()),
|
||||
alternates: symbol
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 558 B After Width: | Height: | Size: 511 B |
@ -21,10 +21,6 @@
|
||||
("lightning", "🖄"),
|
||||
("fly", "🖅"),
|
||||
)
|
||||
#let one = symbol(
|
||||
"1",
|
||||
("emoji", "1️")
|
||||
)
|
||||
|
||||
#envelope
|
||||
#envelope.stamped
|
||||
@ -32,8 +28,6 @@
|
||||
#envelope.stamped.pen
|
||||
#envelope.lightning
|
||||
#envelope.fly
|
||||
#one
|
||||
#one.emoji
|
||||
|
||||
--- symbol-constructor-empty ---
|
||||
// Error: 2-10 expected at least one variant
|
||||
@ -88,14 +82,6 @@
|
||||
("variant.duplicate", "y"),
|
||||
)
|
||||
|
||||
--- symbol-constructor-empty-variant ---
|
||||
// Error: 2:3-2:5 empty default variant
|
||||
// Error: 3:3-3:16 empty variant: "empty"
|
||||
#symbol(
|
||||
"",
|
||||
("empty", "")
|
||||
)
|
||||
|
||||
--- symbol-unknown-modifier ---
|
||||
// Error: 13-20 unknown symbol modifier
|
||||
#emoji.face.garbage
|
||||
|
Loading…
x
Reference in New Issue
Block a user