Fix italic function in math (#3651)

This commit is contained in:
Laurenz 2024-03-13 14:30:59 +01:00 committed by GitHub
parent 48820fe69b
commit 28012b2f8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 13 deletions

View File

@ -11,7 +11,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::diag::SourceResult;
use crate::engine::Engine;
use crate::foundations::{Content, Packed, Smart, StyleChain};
use crate::foundations::{Content, Packed, StyleChain};
use crate::layout::{Abs, Axes, BoxElem, Em, Frame, LayoutMultiple, Regions, Size};
use crate::math::{
scaled_font_size, styled_char, EquationElem, FrameFragment, GlyphFragment,
@ -202,7 +202,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
let fragment = if let Some(mut glyph) = chars
.next()
.filter(|_| chars.next().is_none())
.map(|c| styled_char(styles, c))
.map(|c| styled_char(styles, c, true))
.and_then(|c| GlyphFragment::try_new(self, styles, c, span))
{
// A single letter that is available in the math font.
@ -234,7 +234,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
// Numbers aren't that difficult.
let mut fragments = vec![];
for c in text.chars() {
let c = styled_char(styles, c);
let c = styled_char(styles, c, false);
fragments.push(GlyphFragment::new(self, styles, c, span).into());
}
let frame = MathRun::new(fragments).into_frame(self, styles);
@ -244,13 +244,13 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
TextElem::set_top_edge(TopEdge::Metric(TopEdgeMetric::Bounds)),
TextElem::set_bottom_edge(BottomEdge::Metric(BottomEdgeMetric::Bounds)),
TextElem::set_size(TextSize(scaled_font_size(self, styles).into())),
EquationElem::set_italic(Smart::Custom(false)),
]
.map(|p| p.wrap());
// Anything else is handled by Typst's standard text layout.
let styles = styles.chain(&local);
let text: EcoString = text.chars().map(|c| styled_char(styles, c)).collect();
let text: EcoString =
text.chars().map(|c| styled_char(styles, c, false)).collect();
if text.contains(is_newline) {
let mut fragments = vec![];
for (i, piece) in text.split(is_newline).enumerate() {

View File

@ -9,7 +9,7 @@ use crate::foundations::StyleChain;
use crate::introspection::{Meta, MetaElem};
use crate::layout::{Abs, Corner, Em, Frame, FrameItem, Point, Size};
use crate::math::{
scaled_font_size, styled_char, EquationElem, Limits, MathContext, MathSize, Scaled,
scaled_font_size, EquationElem, Limits, MathContext, MathSize, Scaled,
};
use crate::syntax::Span;
use crate::text::{Font, Glyph, Lang, TextElem, TextItem};
@ -234,7 +234,6 @@ impl GlyphFragment {
c: char,
span: Span,
) -> Option<Self> {
let c = styled_char(styles, c);
let id = ctx.ttf.glyph_index(c)?;
let id = Self::adjust_glyph_index(ctx, id);
Some(Self::with_id(ctx, styles, c, id, span))

View File

@ -289,16 +289,19 @@ pub fn style_for_denominator(styles: StyleChain) -> [LazyHash<Style>; 2] {
///
/// <https://www.w3.org/TR/mathml-core/#new-text-transform-mappings>
/// <https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols>
pub fn styled_char(styles: StyleChain, c: char) -> char {
pub fn styled_char(styles: StyleChain, c: char, auto_italic: bool) -> char {
use MathVariant::*;
let variant = EquationElem::variant_in(styles);
let bold = EquationElem::bold_in(styles);
let italic = EquationElem::italic_in(styles).unwrap_or(matches!(
c,
'a'..='z' | 'ı' | 'ȷ' | 'A'..='Z' | 'α'..='ω' |
'∂' | 'ϵ' | 'ϑ' | 'ϰ' | 'ϕ' | 'ϱ' | 'ϖ'
));
let italic = EquationElem::italic_in(styles).unwrap_or(
auto_italic
&& matches!(
c,
'a'..='z' | 'ı' | 'ȷ' | 'A'..='Z' | 'α'..='ω' |
'∂' | 'ϵ' | 'ϑ' | 'ϰ' | 'ϕ' | 'ϱ' | 'ϖ'
),
);
if let Some(c) = basic_exception(c) {
return c;

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

@ -0,0 +1,4 @@
_abc $sin(x) "abc"$_ \
$italic(sin(x) "abc" #box[abc])$ \
*abc $sin(x) "abc"$* \
$bold(sin(x) "abc" #box[abc])$ \