mirror of
https://github.com/typst/typst
synced 2025-08-23 03:04:14 +08:00
Fix Unicode mapping of hyphenation artifacts (#6799)
This commit is contained in:
parent
727df723df
commit
2501da4bb6
@ -178,7 +178,8 @@ pub fn line<'a>(
|
|||||||
&& pred.dash == Some(Dash::Hard)
|
&& pred.dash == Some(Dash::Hard)
|
||||||
&& let Some(base) = pred.items.last_text()
|
&& let Some(base) = pred.items.last_text()
|
||||||
&& should_repeat_hyphen(base.lang, full)
|
&& should_repeat_hyphen(base.lang, full)
|
||||||
&& let Some(hyphen) = ShapedText::hyphen(engine, p.config.fallback, base, trim)
|
&& let Some(hyphen) =
|
||||||
|
ShapedText::hyphen(engine, p.config.fallback, base, trim, false)
|
||||||
{
|
{
|
||||||
items.push(Item::Text(hyphen), START_HYPHEN_IDX);
|
items.push(Item::Text(hyphen), START_HYPHEN_IDX);
|
||||||
}
|
}
|
||||||
@ -188,7 +189,8 @@ pub fn line<'a>(
|
|||||||
// Add a hyphen at the line end, if we ended on a soft hyphen.
|
// Add a hyphen at the line end, if we ended on a soft hyphen.
|
||||||
if dash == Some(Dash::Soft)
|
if dash == Some(Dash::Soft)
|
||||||
&& let Some(base) = items.last_text()
|
&& let Some(base) = items.last_text()
|
||||||
&& let Some(hyphen) = ShapedText::hyphen(engine, p.config.fallback, base, trim)
|
&& let Some(hyphen) =
|
||||||
|
ShapedText::hyphen(engine, p.config.fallback, base, trim, true)
|
||||||
{
|
{
|
||||||
items.push(Item::Text(hyphen), END_HYPHEN_IDX);
|
items.push(Item::Text(hyphen), END_HYPHEN_IDX);
|
||||||
}
|
}
|
||||||
@ -659,7 +661,7 @@ fn overhang(c: char) -> f64 {
|
|||||||
match c {
|
match c {
|
||||||
// Dashes.
|
// Dashes.
|
||||||
'–' | '—' => 0.2,
|
'–' | '—' => 0.2,
|
||||||
'-' => 0.55,
|
'-' | '\u{ad}' => 0.55,
|
||||||
|
|
||||||
// Punctuation.
|
// Punctuation.
|
||||||
'.' | ',' => 0.8,
|
'.' | ',' => 0.8,
|
||||||
|
@ -21,6 +21,11 @@ use unicode_script::{Script, UnicodeScript};
|
|||||||
use super::{Item, Range, SpanMapper, decorate};
|
use super::{Item, Range, SpanMapper, decorate};
|
||||||
use crate::modifiers::FrameModifyText;
|
use crate::modifiers::FrameModifyText;
|
||||||
|
|
||||||
|
const SHY: char = '\u{ad}';
|
||||||
|
const SHY_STR: &str = "\u{ad}";
|
||||||
|
const HYPHEN: char = '-';
|
||||||
|
const HYPHEN_STR: &str = "-";
|
||||||
|
|
||||||
/// The result of shaping text.
|
/// The result of shaping text.
|
||||||
///
|
///
|
||||||
/// This type contains owned or borrowed shaped text runs, which can be
|
/// This type contains owned or borrowed shaped text runs, which can be
|
||||||
@ -441,11 +446,15 @@ impl<'a> ShapedText<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates shaped text containing a hyphen.
|
/// Creates shaped text containing a hyphen.
|
||||||
|
///
|
||||||
|
/// If `soft` is true, the item will map to plain text as a soft hyphen.
|
||||||
|
/// Otherwise, it will map to a normal hyphen.
|
||||||
pub fn hyphen(
|
pub fn hyphen(
|
||||||
engine: &Engine,
|
engine: &Engine,
|
||||||
fallback: bool,
|
fallback: bool,
|
||||||
base: &ShapedText<'a>,
|
base: &ShapedText<'a>,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
|
soft: bool,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
let world = engine.world;
|
let world = engine.world;
|
||||||
let book = world.book();
|
let book = world.book();
|
||||||
@ -466,10 +475,11 @@ impl<'a> ShapedText<'a> {
|
|||||||
let glyph_id = ttf.glyph_index('-')?;
|
let glyph_id = ttf.glyph_index('-')?;
|
||||||
let x_advance = font.to_em(ttf.glyph_hor_advance(glyph_id)?);
|
let x_advance = font.to_em(ttf.glyph_hor_advance(glyph_id)?);
|
||||||
let size = base.styles.resolve(TextElem::size);
|
let size = base.styles.resolve(TextElem::size);
|
||||||
|
let (c, text) = if soft { (SHY, SHY_STR) } else { (HYPHEN, HYPHEN_STR) };
|
||||||
|
|
||||||
Some(ShapedText {
|
Some(ShapedText {
|
||||||
base: pos,
|
base: pos,
|
||||||
text: "",
|
text,
|
||||||
dir: base.dir,
|
dir: base.dir,
|
||||||
lang: base.lang,
|
lang: base.lang,
|
||||||
region: base.region,
|
region: base.region,
|
||||||
@ -484,9 +494,9 @@ impl<'a> ShapedText<'a> {
|
|||||||
y_offset: Em::zero(),
|
y_offset: Em::zero(),
|
||||||
size,
|
size,
|
||||||
adjustability: Adjustability::default(),
|
adjustability: Adjustability::default(),
|
||||||
range: pos..pos,
|
range: pos..pos + text.len(),
|
||||||
safe_to_break: true,
|
safe_to_break: true,
|
||||||
c: '-',
|
c,
|
||||||
is_justifiable: false,
|
is_justifiable: false,
|
||||||
script: Script::Common,
|
script: Script::Common,
|
||||||
}]),
|
}]),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user