a770fa5a5e5f
mtext.rs
//! MTEXT: inline-format parsing, word wrap and attachment.
use cadcraft_geom::{Bounds2, Vec2};
use crate::{Shaped, TextFont, shape_line, text_width};
pub(crate) mod para;
/// Strip MTEXT formatting codes to plain text with `\n` paragraph breaks.
/// Stacked fractions `\S1/2;` become `1/2`; `\~` is a space; braces are dropped.
pub fn plain_mtext(s: &str) -> String {
let s = unescape_codes(s);
let s = s.as_ref();
let mut out = String::with_capacity(s.len());
use craft_dxf::mtext_tokens::{StackMode, Token, Tokens};
for token in Tokens::new(s, StackMode::Plain) {
match token {
Token::Control('P' | 'N') => out.push('\n'),
Token::Control('~') => out.push(' '),
Token::Control(c @ ('\\' | '{' | '}')) => out.push(c),
Token::Stack(raw) => {
let stack: String = raw.chars().map(|c| if matches!(c, '^' | '#') { '/' } else { c }).collect();
if out.chars().last().is_some_and(|c| c.is_ascii_digit()) && stack.starts_with(|c: char| c.is_ascii_digit()) {
out.push(' ');
}
out.push_str(stack.trim_end_matches('/'));
}
// An unknown code or a malformed `\U+` / `\M+` is shown as typed, as AutoCAD shows it:
// keep the backslash and read the code as text. A trailing lone backslash is dropped.
Token::Unknown(c) => {
out.push('\\');
if c != '\r' {
out.push(c);
}
}
Token::Char('\r') => {}
Token::Char(c) => out.push(c),
_ => {}
}
}
out
}
/// A colour set inline with `\C` (ACI) or `\c` (24-bit RGB as 0xRRGGBB).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MTextColor {
Aci(u16),
Rgb(u32),
}
/// MTEXT layout parameters.
#[derive(Clone, Debug)]
pub struct MTextParams {
pub insert: Vec2,
pub height: f64,
/// Column (reference rectangle) width; 0 = no wrapping.
pub width: f64,
/// 1..=9: TL TC TR ML MC MR BL BC BR.
pub attach: u8,
pub rotation: f64,
pub line_spacing: f64,
/// Exactly: every line pitch is `line_spacing` times the entity height, whatever the line's
/// tallest character; At least (`false`): taller characters push lines apart.
pub line_spacing_exact: bool,
/// Base font (the text style's), switched inline by `\f` / `\F`.
pub font: TextFont,
pub width_factor: f64,
pub oblique: f64,
/// Columns the text flows through (each `width` wide); `None`: one column.
pub columns: Option<para::MTextColumnSpec>,
}
impl MTextParams {
pub fn new(insert: Vec2, height: f64) -> Self {
MTextParams {
insert,
height,
width: 0.0,
attach: 1,
rotation: 0.0,
line_spacing: 1.0,
line_spacing_exact: false,
font: TextFont::Stroke,
width_factor: 1.0,
oblique: 0.0,
columns: None,
}
}
}
/// Geometry of one colour run (`color` = None: the entity's colour).
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MTextPiece {
pub color: Option<MTextColor>,
pub shaped: Shaped,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MTextLayout {
/// Every stroke and glyph outline as polylines, in world coordinates.
pub strokes: Vec<Vec<Vec2>>,
/// Geometry by colour run (strokes plus fillable glyph outlines).
pub pieces: Vec<MTextPiece>,
pub bounds: Bounds2,
/// Plain text of each laid-out line.
pub lines: Vec<String>,
/// Whether the geometry stops short: the layout reached its allowance of retained points
/// ([`Allowance`]) and the segments after that are not drawn.
pub truncated: bool,
}
/// The points all the segments of one layout may keep together, each counted for both copies the
/// layout holds: the allowance one shaped line has (`MAX_SHAPED_POINTS`), so a short repeated detailed glyph over many segments cannot multiply it.
pub(crate) struct Allowance(usize);
impl Allowance {
pub(crate) fn new() -> Self {
Allowance(crate::ttf::MAX_SHAPED_POINTS)
}
/// Whether any allowance is left, before a segment is shaped.
pub(crate) fn left(&self) -> bool {
self.0 > 0
}
/// Charge the points of `sh`; false (and nothing left) when it does not fit.
pub(crate) fn charge(&mut self, sh: &Shaped) -> bool {
// Kept twice: in the piece and again in the layout's flat `strokes`.
let n = 2 * (sh.strokes.iter().map(Vec::len).sum::<usize>() + sh.glyphs.iter().flatten().map(Vec::len).sum::<usize>());
match self.0.checked_sub(n) {
Some(rest) => {
self.0 = rest;
true
}
None => {
self.0 = 0;
false
}
}
}
}
/// Inline character format.
#[derive(Clone, Debug)]
struct Fmt {
font: TextFont,
height: f64,
color: Option<MTextColor>,
under: bool,
over: bool,
wf: f64,
oblique: f64,
/// `\A`: vertical alignment of tolerance stacks (`^`) on the line: 0 bottom, 1 centre, 2 top.
valign: u8,
}
/// One shaped unit of a word.
#[derive(Clone, Debug)]
enum Seg {
Text(String),
/// Stacked text: top, bottom, separator (`/` bar, `#` diagonal, `^` tolerance).
Stack(String, String, char),
}
#[derive(Clone, Debug)]
enum Atom {
Word(Vec<(Seg, usize)>),
Space(usize),
Para(usize),
}
/// Upper bound on parsed atoms (words, spaces and paragraph breaks) per MTEXT. Hostile contents
/// are cut here: once more than this many atoms are parsed, `parse` stops reading and the rest
/// of the contents is silently dropped from the layout. There is no warning to the caller.
const MAX_ATOMS: usize = 200_000;
/// Scale of stacked text relative to the surrounding text.
const STACK_SCALE: f64 = 0.7;
// Shared DXF escape handling and parameterized-command classification.
pub(crate) use craft_dxf::mtext::unescape_codes;
/// The line-spacing factor to lay out with: AutoCAD's 0.25..=4 range, 1 for a missing or
/// non-finite value.
pub(crate) fn sane_line_spacing(v: f64) -> f64 {
if v.is_finite() && v > 0.0 { v.clamp(0.25, 4.0) } else { 1.0 }
}
fn num(a: &str) -> Option<f64> {
a.trim().parse::<f64>().ok().filter(|v| v.is_finite())
}
/// `\fArial|b1|i0|c0|p34;` → the font, trying a bold/italic face first.
fn font_switch(a: &str) -> Option<TextFont> {
let mut parts = a.split('|');
let name = parts.next().unwrap_or("").trim();
if name.is_empty() {
return None;
}
let (mut bold, mut italic) = (false, false);
for p in parts {
match p {
"b1" => bold = true,
"i1" => italic = true,
_ => {}
}
}
let style = match (bold, italic) {
(true, true) => Some("bold italic"),
(true, false) => Some("bold"),
(false, true) => Some("italic"),
_ => None,
};
if let Some(st) = style {
for cand in [format!("{name} {st}"), format!("{name}-{}", st.replace(' ', "")), format!("{name}{}", if bold { "bd" } else { "i" })] {
if let Some(f) = crate::ttf::find(&cand) {
return Some(TextFont::Outline(f));
}
}
}
Some(TextFont::resolve(name))
}
/// Parse MTEXT contents into words, spaces and paragraph breaks with their formats.
fn parse(contents: &str, base: Fmt) -> (Vec<Atom>, Vec<Fmt>) {
use craft_dxf::mtext_tokens::{StackMode, Token, Tokens, split_stack};
let h_min = base.height * 1e-3;
let h_max = if (base.height * 1e3).is_finite() { base.height * 1e3 } else { f64::MAX };
let mut fmts = vec![base];
let mut cur = 0usize;
let mut stack: Vec<usize> = Vec::new();
let mut atoms: Vec<Atom> = Vec::new();
let mut word: Vec<(Seg, usize)> = Vec::new();
let push_char = |word: &mut Vec<(Seg, usize)>, c: char, f: usize| {
if let Some((Seg::Text(t), wf)) = word.last_mut()
&& *wf == f
{
t.push(c);
return;
}
word.push((Seg::Text(c.to_string()), f));
};
let end_word = |atoms: &mut Vec<Atom>, word: &mut Vec<(Seg, usize)>| {
if !word.is_empty() {
atoms.push(Atom::Word(std::mem::take(word)));
}
};
// A character read as text: a line feed breaks the paragraph and a space ends the word.
let text_char = |atoms: &mut Vec<Atom>, word: &mut Vec<(Seg, usize)>, c: char, f: usize| match c {
'\r' => {}
'\n' => {
end_word(atoms, word);
atoms.push(Atom::Para(f));
}
' ' => {
end_word(atoms, word);
atoms.push(Atom::Space(f));
}
c => push_char(word, c, f),
};
// Derive a new format from the current one.
let derive = |fmts: &mut Vec<Fmt>, cur: &mut usize, f: &dyn Fn(&mut Fmt)| {
let mut n = fmts.get(*cur).cloned().unwrap_or(Fmt {
font: TextFont::Stroke,
height: 1.0,
color: None,
under: false,
over: false,
wf: 1.0,
oblique: 0.0,
valign: 1,
});
f(&mut n);
if fmts.len() < 100_000 {
fmts.push(n);
*cur = fmts.len() - 1;
}
};
for token in Tokens::new(contents, StackMode::Structured) {
if atoms.len() > MAX_ATOMS {
break;
}
match token {
Token::Control('P' | 'N') => {
end_word(&mut atoms, &mut word);
atoms.push(Atom::Para(cur));
}
Token::Control('~') => push_char(&mut word, '\u{a0}', cur),
Token::Control(c @ ('\\' | '{' | '}')) => push_char(&mut word, c, cur),
Token::Control('L') => derive(&mut fmts, &mut cur, &|f| f.under = true),
Token::Control('l') => derive(&mut fmts, &mut cur, &|f| f.under = false),
Token::Control('O') => derive(&mut fmts, &mut cur, &|f| f.over = true),
Token::Control('o') => derive(&mut fmts, &mut cur, &|f| f.over = false),
Token::Stack(raw) => {
let (top, bottom, sep) = split_stack(raw);
word.push((Seg::Stack(top, bottom, sep.unwrap_or('/')), cur));
}
Token::Argument('f' | 'F', a) => {
if let Some(font) = font_switch(a) {
derive(&mut fmts, &mut cur, &|f| f.font = font.clone());
}
}
Token::Argument('H', a) => {
let (rel, v) = match a.trim().strip_suffix(['x', 'X']) {
Some(r) => (true, num(r)),
None => (false, num(a)),
};
if let Some(v) = v.filter(|v| *v > 0.0 && *v < 1e6) {
// Relative heights compound; keep the result within 1e-3..1e3 times the
// MTEXT's own height, never infinite.
derive(&mut fmts, &mut cur, &|f| f.height = if rel { (f.height * v).min(h_max).max(h_min) } else { v });
}
}
Token::Argument('W', a) => {
// `\W2x;` multiplies the current factor (like `\H2x;`); `\W2;` sets it.
let (rel, v) = match a.trim().strip_suffix(['x', 'X']) {
Some(r) => (true, num(r)),
None => (false, num(a)),
};
if let Some(v) = v.filter(|v| *v > 0.01 && *v < 100.0) {
// Relative factors compound; keep the result within the absolute range.
derive(&mut fmts, &mut cur, &|f| f.wf = if rel { (f.wf * v).clamp(0.01, 100.0) } else { v });
}
}
Token::Argument('Q', a) => {
if let Some(v) = num(a).filter(|v| v.abs() < 85.0) {
derive(&mut fmts, &mut cur, &|f| f.oblique = v.to_radians());
}
}
Token::Argument('C', a) => {
if let Ok(v) = a.trim().parse::<u16>() {
let col = if v == 0 || v >= 256 { None } else { Some(MTextColor::Aci(v)) };
derive(&mut fmts, &mut cur, &|f| f.color = col);
}
}
Token::Argument('c', a) => {
if let Ok(v) = a.trim().parse::<u32>() {
derive(&mut fmts, &mut cur, &|f| f.color = Some(MTextColor::Rgb(v & 0xFF_FFFF)));
}
}
Token::Argument('A', a) => {
if let Ok(v @ 0..=2) = a.trim().parse::<u8>() {
derive(&mut fmts, &mut cur, &|f| f.valign = v);
}
}
Token::OpenGroup => {
if stack.len() < 256 {
stack.push(cur);
}
}
Token::CloseGroup => {
if let Some(f) = stack.pop() {
cur = f;
}
}
// An unknown code or a malformed `\U+` / `\M+` is shown as typed, as AutoCAD shows it:
// keep the backslash and read the code as text (#2952).
Token::Unknown(c) => {
push_char(&mut word, '\\', cur);
text_char(&mut atoms, &mut word, c, cur);
}
Token::Char(c) => text_char(&mut atoms, &mut word, c, cur),
_ => {}
}
}
end_word(&mut atoms, &mut word);
(atoms, fmts)
}
/// `word` split where a line may wrap inside it: between two Thai words, as Thai writes no
/// spaces between words ([`crate::linebreak::thai_breaks`]). The pieces keep the word's segments
/// and formats in order and are laid out side by side, so the text looks the same and only gains
/// places to wrap. A word without Thai comes back whole.
///
/// Lines still wrap at spaces first, as before: only a word too wide for a line of its own (which
/// would run out of the column) is split, so text that fits keeps its line breaks, as its author
/// saw them.
fn split_thai(word: Vec<(Seg, usize)>) -> Vec<Vec<(Seg, usize)>> {
if !word.iter().any(|(seg, _)| matches!(seg, Seg::Text(t) if t.chars().any(crate::complex::is_thai))) {
return vec![word];
}
// The word's characters, a stacked fraction standing as one object replacement character
// (never next to a Thai break).
let text: String = word
.iter()
.map(|(seg, _)| match seg {
Seg::Text(t) => t.as_str(),
Seg::Stack(..) => "\u{FFFC}",
})
.collect();
let breaks = crate::linebreak::thai_breaks(&text);
if breaks.is_empty() {
return vec![word];
}
let mut pieces = Vec::with_capacity(breaks.len().saturating_add(1));
let mut breaks = breaks.iter().copied().peekable();
let mut piece: Vec<(Seg, usize)> = Vec::new();
// Character offset of the segment being split.
let mut at = 0usize;
for (seg, format) in word {
let Seg::Text(t) = seg else {
piece.push((seg, format));
at += 1;
continue;
};
let n = t.chars().count();
let (mut rest, mut rest_at) = (t.as_str(), at);
while let Some(b) = breaks.next_if(|&b| b < at + n) {
let k = b.saturating_sub(rest_at);
let byte = rest.char_indices().nth(k).map_or(rest.len(), |(i, _)| i);
let (head, tail) = rest.split_at_checked(byte).unwrap_or((rest, ""));
if !head.is_empty() {
piece.push((Seg::Text(head.to_string()), format));
}
if !piece.is_empty() {
pieces.push(std::mem::take(&mut piece));
}
(rest, rest_at) = (tail, b.max(rest_at));
}
if !rest.is_empty() {
piece.push((Seg::Text(rest.to_string()), format));
}
at += n;
}
if !piece.is_empty() {
pieces.push(piece);
}
pieces
}
fn seg_width(seg: &Seg, f: &Fmt) -> f64 {
match seg {
Seg::Text(t) => text_width(&f.font, t, f.height, f.wf),
Seg::Stack(top, bottom, sep) => {
let hs = f.height * STACK_SCALE;
let wt = text_width(&f.font, top, hs, f.wf);
let wb = text_width(&f.font, bottom, hs, f.wf);
match sep {
'#' => wt + wb + f.height * 0.5,
'^' => wt.max(wb),
_ => wt.max(wb) + f.height * 0.2,
}
}
}
}
/// Shape a segment with its left end at `x` on the baseline.
fn seg_shape(seg: &Seg, format: &Fmt, origin_x: f64) -> Shaped {
let height = format.height;
let mut out = Shaped::default();
let put = |text: &str, size: f64, at: Vec2| {
if text.is_empty() {
return Shaped::default();
}
let mut sh = shape_line(&format.font, text, size, format.wf, format.oblique);
sh.map(|point| point + at);
sh
};
let width = seg_width(seg, format);
match seg {
Seg::Text(text) => out.extend(put(text, height, Vec2::new(origin_x, 0.0))),
Seg::Stack(top, bottom, sep) => {
let hs = height * STACK_SCALE;
let wt = text_width(&format.font, top, hs, format.wf);
let wb = text_width(&format.font, bottom, hs, format.wf);
match sep {
'#' => {
out.extend(put(top, hs, Vec2::new(origin_x, height * 0.4)));
let sx = origin_x + wt + height * 0.1;
out.extend(put(bottom, hs, Vec2::new(sx + height * 0.4, -height * 0.05)));
out.strokes.push(vec![Vec2::new(sx, -height * 0.1), Vec2::new(sx + height * 0.3, height * 1.1)]);
}
'^' => {
// Tolerances: centred on the line, or the lower value on the baseline (\A0) or
// the upper value level with the text (\A2).
let (yt, yb) = match format.valign {
0 => (hs + height * 0.1, 0.0),
2 => (0.0, -hs - height * 0.1),
_ => (height * 0.55, height * 0.45 - hs),
};
out.extend(put(top, hs, Vec2::new(origin_x, yt)));
out.extend(put(bottom, hs, Vec2::new(origin_x, yb)));
}
_ => {
let bar = height * 0.45;
out.extend(put(top, hs, Vec2::new(origin_x + (width - wt) / 2.0, bar + height * 0.12)));
out.extend(put(bottom, hs, Vec2::new(origin_x + (width - wb) / 2.0, bar - height * 0.12 - hs)));
out.strokes.push(vec![Vec2::new(origin_x, bar), Vec2::new(origin_x + width, bar)]);
}
}
}
}
if format.under {
out.strokes.push(vec![Vec2::new(origin_x, -height * 0.2), Vec2::new(origin_x + width, -height * 0.2)]);
}
if format.over {
out.strokes.push(vec![Vec2::new(origin_x, height * 1.2), Vec2::new(origin_x + width, height * 1.2)]);
}
out
}
fn seg_plain(seg: &Seg) -> String {
match seg {
Seg::Text(t) => t.replace('\u{a0}', " "),
Seg::Stack(a, b, _) => format!("{a}/{b}"),
}
}
/// One laid-out piece of a line: a segment (`None`: the space between two words), its format,
/// x offset, width and the character offset of its text in [`Line::text`].
struct Item {
seg: Option<Seg>,
fmt: usize,
x: f64,
w: f64,
at: usize,
}
#[derive(Default)]
struct Line {
items: Vec<Item>,
width: f64,
max_h: f64,
text: String,
/// Running character count of `text`, kept incrementally so appending a segment need not
/// rescan the whole accumulated line (which is multiplicative on a long line) (CWE-407).
chars: usize,
/// Index of the paragraph the line belongs to.
para: usize,
}
impl Line {
/// Put the items in display order for right-to-left text: the Unicode Bidirectional
/// Algorithm over the line's text (paragraph direction `base`), pieces reordered by the level
/// of their first character, x offsets recomputed. Lines without right-to-left text are
/// left as they are.
fn reorder_bidi(&mut self, base: Option<bool>) {
if !crate::bidi::has_rtl(&self.text) {
return;
}
let levels = crate::bidi::char_levels(&self.text, base);
let item_levels: Vec<u8> = self.items.iter().map(|it| levels.get(it.at).copied().unwrap_or(0)).collect();
let order = crate::bidi::reorder(&item_levels);
let mut items: Vec<Option<Item>> = std::mem::take(&mut self.items).into_iter().map(Some).collect();
let mut x = 0.0;
for i in order {
if let Some(mut it) = items.get_mut(i).and_then(Option::take) {
it.x = x;
x += it.w;
self.items.push(it);
}
}
}
}
/// Lay out MTEXT with the stroke font. `attach` 1..=9 (TL, TC, TR, ML, MC, MR, BL, BC, BR).
pub fn layout_mtext(contents: &str, insert: Vec2, height: f64, width: f64, attach: u8, rotation: f64, line_spacing: f64) -> MTextLayout {
layout_mtext_with(contents, &MTextParams { insert, height, width, attach, rotation, line_spacing, ..MTextParams::new(insert, height) })
}
/// The lines of MTEXT without paragraph codes: words wrap at spaces to `width` (0: no wrapping),
/// and a word too wide for a line of its own wraps between its Thai words.
fn plain_lines(atoms: &[Atom], fmts: &[Fmt], base: &Fmt, width: f64) -> Vec<Line> {
let fmt = |index: usize| fmts.get(index).unwrap_or(base);
let mut lines = Vec::new();
let mut line = Line::default();
let mut pending_space = None;
let mut last_fmt = 0usize;
let mut paragraph = 0usize;
let finish = |lines: &mut Vec<Line>, line: &mut Line, format: &Fmt, paragraph: usize| {
if line.items.is_empty() {
line.max_h = format.height;
}
line.para = paragraph;
lines.push(std::mem::take(line));
};
for atom in atoms {
match atom {
Atom::Space(format) => {
if !line.items.is_empty() || pending_space.is_some() {
pending_space = Some(*format);
}
last_fmt = *format;
}
Atom::Para(format) => {
pending_space = None;
finish(&mut lines, &mut line, fmt(*format), paragraph);
paragraph += 1;
last_fmt = *format;
}
Atom::Word(word) => {
let word_width =
|segments: &[(Seg, usize)]| -> f64 { segments.iter().map(|(segment, format)| seg_width(segment, fmt(*format))).sum() };
// Wider than the column: Thai breaks between its words (see `split_thai`).
let split;
let pieces = if width > 0.0 && word_width(word) > width + 1e-9 {
split = split_thai(word.clone());
split.as_slice()
} else {
std::slice::from_ref(word)
};
for segments in pieces {
let word_width = word_width(segments);
let space_width = pending_space.map_or(0.0, |format| crate::space_width(&fmt(format).font, fmt(format).height, fmt(format).wf));
if width > 0.0 && !line.items.is_empty() && line.width + space_width + word_width > width + 1e-9 {
finish(&mut lines, &mut line, fmt(last_fmt), paragraph);
} else if !line.items.is_empty() || pending_space.is_some() {
if let Some(format) = pending_space {
let at = line.chars;
line.items.push(Item { seg: None, fmt: format, x: line.width, w: space_width, at });
line.text.push(' ');
line.chars += 1;
}
line.width += space_width;
}
pending_space = None;
for (segment, format) in segments {
let segment_width = seg_width(segment, fmt(*format));
line.max_h = line.max_h.max(fmt(*format).height);
let at = line.chars;
let plain = seg_plain(segment);
line.chars += plain.chars().count();
line.text.push_str(&plain);
line.items.push(Item { seg: Some(segment.clone()), fmt: *format, x: line.width, w: segment_width, at });
line.width += segment_width;
last_fmt = *format;
}
}
}
}
}
finish(&mut lines, &mut line, fmt(last_fmt), paragraph);
lines
}
/// Lay out MTEXT: inline formatting runs, stacked fractions, paragraphs, word wrap to the
/// column width and the attachment point.
pub fn layout_mtext_with(contents: &str, p: &MTextParams) -> MTextLayout {
let contents = unescape_codes(contents);
let contents = contents.as_ref();
let h = if p.height > 0.0 && p.height.is_finite() { p.height } else { 1.0 };
let wf = if p.width_factor.is_finite() && p.width_factor > 0.0 { p.width_factor } else { 1.0 };
let width = if p.width.is_finite() && p.width > 0.0 { p.width } else { 0.0 };
let rotation = if p.rotation.is_finite() { p.rotation } else { 0.0 };
let oblique = if p.oblique.is_finite() { p.oblique } else { 0.0 };
let base = Fmt { font: p.font.clone(), height: h, color: None, under: false, over: false, wf, oblique, valign: 1 };
if para::needed(contents, p) {
return para::layout(contents, p, &base);
}
let (atoms, fmts) = parse(contents, base.clone());
let fmt = |i: usize| fmts.get(i).unwrap_or(&base);
let mut lines = plain_lines(&atoms, &fmts, &base, width);
// Right-to-left text: each line in display order, its paragraph's direction from the
// paragraph's first strong character.
let mut bases: Vec<Option<bool>> = Vec::new();
for l in &lines {
if bases.len() <= l.para {
bases.resize(l.para + 1, None);
}
if let Some(b) = bases.get_mut(l.para)
&& b.is_none()
{
*b = crate::bidi::base_rtl(&l.text);
}
}
for l in &mut lines {
l.reorder_bidi(bases.get(l.para).copied().flatten());
}
let ls = sane_line_spacing(p.line_spacing);
// Baselines below the top of the first line.
let mut baselines = Vec::with_capacity(lines.len());
let mut y = 0.0;
for (i, l) in lines.iter().enumerate() {
let lh = if l.max_h > 0.0 { l.max_h } else { h };
y -= if i == 0 {
lh
} else if p.line_spacing_exact {
h * 5.0 / 3.0 * ls
} else {
lh * 5.0 / 3.0 * ls
};
baselines.push(y);
}
let block_h = -y;
let block_w = if width > 0.0 { width } else { lines.iter().map(|l| l.width).fold(0.0, f64::max) };
let col = (p.attach.clamp(1, 9) - 1) % 3;
let row = (p.attach.clamp(1, 9) - 1) / 3;
let top = match row {
0 => 0.0,
1 => block_h / 2.0,
_ => block_h,
};
let insert = p.insert;
let xf = |q: Vec2| insert + q.rotate(rotation);
let mut out = MTextLayout { lines: lines.iter().map(|l| l.text.clone()).collect(), ..Default::default() };
let mut allowance = Allowance::new();
for (l, base_y) in lines.iter().zip(baselines) {
let x0 = match col {
0 => 0.0,
1 => -l.width / 2.0,
_ => -l.width,
};
for it in &l.items {
let Some(sg) = &it.seg else { continue };
let fm = fmt(it.fmt);
if !allowance.left() {
out.truncated = true;
continue;
}
let mut sh = seg_shape(sg, fm, it.x);
if !allowance.charge(&sh) {
out.truncated = true;
continue;
}
let off = Vec2::new(x0, top + base_y);
sh.map(|q| xf(q + off));
match out.pieces.last_mut() {
Some(pc) if pc.color == fm.color => pc.shaped.extend(sh),
_ => out.pieces.push(MTextPiece { color: fm.color, shaped: sh }),
}
}
}
for pc in &out.pieces {
out.strokes.extend(pc.shaped.outlines());
}
let x0 = match col {
0 => 0.0,
1 => -block_w / 2.0,
_ => -block_w,
};
out.bounds = Bounds2::from_points(
[Vec2::new(x0, top), Vec2::new(x0 + block_w, top), Vec2::new(x0 + block_w, top - block_h), Vec2::new(x0, top - block_h)].map(xf),
);
out
}
#[cfg(test)]
mod tests {
use super::*;
/// Incremental character accounting preserves the text of a multi-word line without introducing wrapping.
#[test]
fn line_char_offsets_track_incrementally() {
// A multi-word line lays out correctly even though the per-line character count is now
// maintained incrementally rather than rescanned per segment (CWE-407, row 20).
let l = layout_mtext("alpha beta gamma", Vec2::ZERO, 1.0, 0.0, 1, 0.0, 1.0);
assert_eq!(l.lines.len(), 1, "no wrap width: one line");
assert_eq!(l.lines[0], "alpha beta gamma");
}
/// The points of all the segments of one layout are charged against one allowance.
#[test]
fn a_layout_shares_one_point_allowance() {
let big = Shaped { strokes: vec![vec![Vec2::ZERO; crate::ttf::MAX_SHAPED_POINTS / 4 + 1]], ..Default::default() };
let mut a = Allowance::new();
assert!(a.left() && a.charge(&big), "the first segment fits");
assert!(!a.charge(&big), "the second does not, though each alone would");
assert!(!a.left(), "and nothing is left to shape another");
assert!(!layout_mtext("plain text", Vec2::ZERO, 1.0, 0.0, 1, 0.0, 1.0).truncated);
}
#[test]
fn strips_formatting() {
assert_eq!(plain_mtext("{\\fArial|b1;Bold}\\Pline 2"), "Bold\nline 2");
assert_eq!(plain_mtext("1\\S1/2;\""), "1 1/2\"");
assert_eq!(plain_mtext("a\\~b\\\\c"), "a b\\c");
assert_eq!(plain_mtext("\\H2.5x;Big"), "Big");
assert_eq!(plain_mtext("unterminated \\H2"), "unterminated ");
assert_eq!(plain_mtext("\\U+0041B \\U+00E8"), "AB è");
assert_eq!(plain_mtext("\\U+12"), "\\U+12", "a short escape is shown as typed");
}
#[test]
fn unicode_escapes_are_decoded_in_every_parser() {
// The escape has no `;`, so it must not swallow the rest of the text.
assert_eq!(plain_mtext("A\\U+00B0 B C;D"), "A\u{b0} B C;D");
assert_eq!(lay("A\\U+00B0 B C;D", 0.0).lines, vec!["A\u{b0} B C;D"]);
// The paragraph path (`\p`) and the plain path agree.
assert_eq!(lay("\\pxql;A\\U+00B0 B C;D", 0.0).lines, vec!["A\u{b0} B C;D"]);
let scanned = para::Paragraphs::scan("\\U+00B0\\pi1;x\\U+0041 y;z");
assert!(scanned.formatted());
// A surrogate pair is one character; a lone surrogate is U+FFFD.
assert_eq!(plain_mtext("\\U+D83D\\U+DE00!"), "\u{1f600}!");
assert_eq!(lay("\\U+D83D\\U+DE00!", 0.0).lines, vec!["\u{1f600}!"]);
assert_eq!(plain_mtext("a\\U+D83Db"), "a\u{fffd}b");
assert_eq!(plain_mtext("a\\U+DE00b\\U+D83D"), "a\u{fffd}b\u{fffd}");
// A decoded backslash or brace is text, not markup; an escaped backslash is not an escape.
assert_eq!(plain_mtext("\\U+005CH2;x"), "\\H2;x");
assert_eq!(plain_mtext("\\\\U+0041"), "\\U+0041");
// `\M+n` double-byte characters, decoded like the DXF reader does.
assert_eq!(plain_mtext("\\M+5BCFE x;y"), "\u{4ef6} x;y");
// Malformed escapes are shown as typed, backslash included, as AutoCAD 2024 shows them.
for s in ["a\\U+12 b", "\\U+12x", "a\\U+12;b", "a\\U+123 b", "a\\U+ b", "a\\U+00G1b", "a\\M+ b"] {
assert_eq!(plain_mtext(s), s);
assert_eq!(lay(s, 0.0).lines, vec![s]);
assert_eq!(lay(&format!("\\pxql;{s}"), 0.0).lines, vec![s]);
}
// A valid escape takes exactly four digits.
assert_eq!(plain_mtext("a\\U+12345b"), "a\u{1234}5b");
}
#[test]
fn unknown_code_without_semicolon_keeps_the_text() {
// An unknown code is shown as typed, as AutoCAD 2024 shows it.
assert_eq!(plain_mtext("A\\Zbc d"), "A\\Zbc d");
assert_eq!(lay("A\\Zbc d", 0.0).lines, vec!["A\\Zbc d"]);
assert_eq!(lay("\\pxql;A\\Zbc d", 0.0).lines, vec!["A\\Zbc d"]);
// The text after it is read as usual: here a paragraph break.
assert_eq!(plain_mtext("A\\Z\\Pb"), "A\\Z\nb");
// A trailing lone backslash is dropped.
assert_eq!(plain_mtext("A\\"), "A");
// Codes with an argument still take it up to the `;`, or to the end.
assert_eq!(plain_mtext("A\\H2;B"), "AB");
assert_eq!(plain_mtext("A\\H2 B"), "A");
}
#[test]
fn non_finite_oblique_and_line_spacing_are_repaired() {
let run = |oblique: f64, spacing: f64| {
let p = MTextParams { oblique, line_spacing: spacing, ..MTextParams::new(Vec2::ZERO, 1.0) };
layout_mtext_with("AB\\PCD", &p)
};
let finite =
|l: &MTextLayout| l.bounds.min.x.is_finite() && l.bounds.max.x.is_finite() && l.bounds.min.y.is_finite() && l.bounds.max.y.is_finite();
let reference = run(0.0, 1.0);
for o in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
let l = run(o, 1.0);
assert!(finite(&l), "oblique {o}");
assert_eq!(l.bounds, reference.bounds, "oblique {o}");
}
for s in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, 0.0, -2.0] {
let l = run(0.0, s);
assert!(finite(&l), "spacing {s}");
assert_eq!(l.bounds, reference.bounds, "spacing {s}");
}
// Spacing is clamped to AutoCAD's 0.25..=4 range.
assert_eq!(run(0.0, 1e9).bounds, run(0.0, 4.0).bounds);
assert_eq!(run(0.0, 1e-9).bounds, run(0.0, 0.25).bounds);
assert!(run(0.0, 4.0).bounds.height() > reference.bounds.height());
// The paragraph layout takes the same path.
let p = MTextParams { oblique: f64::NAN, line_spacing: f64::NAN, ..MTextParams::new(Vec2::ZERO, 1.0) };
assert!(finite(&layout_mtext_with("\\pxql;AB\\PCD", &p)));
}
#[test]
fn relative_heights_do_not_run_away() {
let src = format!("{}A", "\\H999999x;".repeat(60));
let l = lay(&src, 0.0);
assert!(l.bounds.height().is_finite() && l.bounds.width().is_finite());
assert!(l.bounds.height() <= 1e3 * 1.5, "{}", l.bounds.height());
assert!(l.bounds.height() > 100.0, "the glyph must not vanish: {}", l.bounds.height());
// Shrinking is bounded too.
let tiny = lay(&format!("{}A", "\\H0.000001x;".repeat(60)), 0.0);
assert!(tiny.bounds.height() > 0.0);
}
/// The width factor of the last format `parse` derived.
fn last_wf(contents: &str, base_wf: f64) -> f64 {
let base = Fmt { font: TextFont::Stroke, height: 1.0, color: None, under: false, over: false, wf: base_wf, oblique: 0.0, valign: 1 };
parse(contents, base).1.last().map_or(f64::NAN, |f| f.wf)
}
#[test]
fn width_factor_x_is_relative() {
assert_eq!(last_wf("\\W2;a", 1.5), 2.0, "absolute sets the factor");
assert_eq!(last_wf("\\W2x;a", 1.5), 3.0, "x multiplies the current factor");
assert_eq!(last_wf("\\W2;\\W2x;a", 1.0), 4.0, "relative builds on the last absolute");
assert_eq!(last_wf("\\W0.5X;a", 1.0), 0.5, "capital X too");
// Hostile: a long relative chain stays inside the absolute range.
let chain = "\\W99x;".repeat(50);
assert_eq!(last_wf(&chain, 1.0), 100.0);
let down = "\\W0.02x;".repeat(50);
assert_eq!(last_wf(&down, 1.0), 0.01);
// Out of range or not a number: ignored.
assert_eq!(last_wf("\\W1e999x;a", 1.5), 1.5);
assert_eq!(last_wf("\\WNaNx;a", 1.5), 1.5);
assert_eq!(last_wf("\\Wx;a", 1.5), 1.5);
}
#[test]
fn wraps_to_width() {
let l = layout_mtext("the quick brown fox jumps over the lazy dog", Vec2::ZERO, 1.0, 8.0, 1, 0.0, 1.0);
assert!(l.lines.len() > 2);
assert!(l.bounds.max.y.abs() < 1e-9);
assert!((l.bounds.width() - 8.0).abs() < 1e-9);
}
#[test]
fn middle_centre_attach() {
let l = layout_mtext("AB", Vec2::new(5.0, 5.0), 1.0, 0.0, 5, 0.0, 1.0);
let c = l.bounds.center();
assert!((c.x - 5.0).abs() < 1e-9 && (c.y - 5.0).abs() < 1e-9);
}
#[test]
fn stroke_word_space_matches_text() {
let l = layout_mtext("A B", Vec2::ZERO, 1.0, 0.0, 1, 0.0, 1.0);
assert!((l.bounds.width() - crate::line_width("A B", 1.0, 1.0)).abs() < 1e-9);
}
fn lay(s: &str, width: f64) -> MTextLayout {
layout_mtext(s, Vec2::ZERO, 1.0, width, 1, 0.0, 1.0)
}
#[test]
fn indents_are_drawing_units() {
// An AutoCAD file's `\pi4.8634;` on 4.6652 high text in a 50.2 wide box indents the
// first line by 4.8634 units: the text still fits on one line.
let l = layout_mtext("\\pi4.8634;AAA BBB CCC", Vec2::ZERO, 4.6652, 50.2, 1, 0.0, 1.0);
assert_eq!(l.lines, vec!["AAA BBB CCC"]);
}
#[test]
fn paragraphs_make_lines() {
let l = lay("one\\Ptwo\\P\\Pfour", 0.0);
assert_eq!(l.lines, vec!["one", "two", "", "four"]);
// Four lines: cap height plus three pitches of 5/3.
assert!((l.bounds.height() - (1.0 + 3.0 * 5.0 / 3.0)).abs() < 1e-9);
}
#[test]
fn absolute_and_relative_height() {
let small = lay("AB", 0.0).bounds.width();
let abs = lay("\\H2;AB", 0.0);
assert!((abs.bounds.width() - 2.0 * small).abs() < 1e-6);
assert!((abs.bounds.height() - 2.0).abs() < 1e-9);
let rel = lay("{\\H3x;AB}AB", 0.0);
assert!((rel.bounds.width() - 4.0 * small).abs() < 1e-6, "{}", rel.bounds.width());
assert!((rel.bounds.height() - 3.0).abs() < 1e-9);
}
#[test]
fn colour_runs() {
let l = lay("A{\\C1;B}C", 0.0);
let cols: Vec<Option<MTextColor>> = l.pieces.iter().map(|p| p.color).collect();
assert_eq!(cols, vec![None, Some(MTextColor::Aci(1)), None]);
let l = lay("\\c16711680;R", 0.0);
assert_eq!(l.pieces.first().map(|p| p.color), Some(Some(MTextColor::Rgb(0x00FF_0000))));
}
#[test]
fn underline_and_overline() {
let plain = lay("AB", 0.0).strokes.len();
let u = lay("\\LAB\\l", 0.0);
assert_eq!(u.strokes.len(), plain + 1);
// Top-left attachment: the first baseline is one text height below the insert.
assert!(u.strokes.iter().any(|s| s.len() == 2 && s.iter().all(|p| (p.y + 1.2).abs() < 1e-9)));
let o = lay("\\OAB\\o", 0.0);
assert!(o.strokes.iter().any(|s| s.len() == 2 && s.iter().all(|p| (p.y - 0.2).abs() < 1e-9)));
}
#[test]
fn stacked_fraction_is_small_over_large() {
let l = lay("\\S1/2;", 0.0);
assert_eq!(l.lines, vec!["1/2"]);
// A horizontal bar plus numerator above it and denominator below.
let bar = l.strokes.iter().find(|s| s.len() == 2 && (s[0].y - s[1].y).abs() < 1e-12 && (s[0].y + 0.55).abs() < 1e-9);
assert!(bar.is_some());
let ys: Vec<f64> = l.strokes.iter().flatten().map(|p| p.y).collect();
let max = ys.iter().copied().fold(f64::MIN, f64::max);
let min = ys.iter().copied().fold(f64::MAX, f64::min);
assert!(max > 0.0 && min < -1.0, "{min}..{max}");
// Narrower than the two digits side by side at full size.
assert!(l.bounds.width() < crate::line_width("12", 1.0, 1.0));
}
#[test]
fn wrap_respects_runs_and_width() {
let l = lay("alpha {\\H2x;beta} gamma delta", 6.0);
assert!(l.lines.len() >= 2);
let w: f64 = crate::line_width("alpha", 1.0, 1.0);
assert!(w < 6.0);
assert_eq!(l.lines.first().map(String::as_str), Some("alpha"));
}
#[test]
fn font_switch_falls_back_to_stroke() {
let l = lay("{\\fNoSuchFontAnywhere|b1;AB}", 0.0);
assert!(!l.strokes.is_empty());
assert!(l.pieces.iter().all(|p| p.shaped.glyphs.is_empty()));
}
/// Stroke-font geometry that isn't a missing-glyph box (Latin letters here).
fn latin_points(l: &MTextLayout) -> Vec<Vec2> {
l.pieces.iter().flat_map(|p| p.shaped.strokes.iter()).filter(|s| !(s.len() == 5 && s.first() == s.last())).flatten().copied().collect()
}
#[test]
fn rtl_paragraph_lays_words_out_right_to_left() {
// A Persian paragraph: the first word is rightmost, the Latin word at the end of the
// sentence is leftmost (without any installed font the Persian letters are boxes).
let l = lay("سلام دنیا abc", 0.0);
let xs = latin_points(&l);
assert!(!xs.is_empty());
let w = l.bounds.width();
assert!(xs.iter().all(|p| p.x < w / 2.0), "abc on the left of a right-to-left line");
assert_eq!(l.lines, vec!["سلام دنیا abc"], "plain text stays in logical order");
// The same words in a left-to-right paragraph keep abc first.
let l = lay("abc سلام دنیا", 0.0);
assert!(latin_points(&l).iter().all(|p| p.x < l.bounds.width() / 2.0));
assert!((l.bounds.width() - w).abs() < 1e-9, "same width either way");
}
#[test]
fn rtl_wrapping_keeps_the_column_and_attachment() {
// Top-right attachment, wrapped to a 6-unit column: every line ends at the insertion
// point's x and nothing leaves the column.
let text = "قیمت 25 تومان و abc def";
let l = layout_mtext(text, Vec2::ZERO, 1.0, 6.0, 3, 0.0, 1.0);
assert!(l.lines.len() >= 2, "{:?}", l.lines);
assert!(l.bounds.max.x.abs() < 1e-9 && (l.bounds.width() - 6.0).abs() < 1e-9);
let pts: Vec<Vec2> = l.strokes.iter().flatten().copied().collect();
// Allow an installed font's overhang (a tenth of the height), so this holds on any machine.
assert!(pts.iter().all(|p| p.x <= 0.1 && p.x >= -6.0 - 0.1), "inside the column");
// Centred: the line box is centred on the insertion point.
let c = layout_mtext(text, Vec2::ZERO, 1.0, 0.0, 2, 0.0, 1.0);
assert!(c.bounds.center().x.abs() < 1e-9);
}
#[test]
fn hostile_contents_do_not_panic() {
for s in [
"\u{202E}{\\C1;سل}ام\\P\u{2067}",
"\\S\u{05D0}/ب;",
"\\",
"\\S",
"\\S/;",
"{{{{",
"}}}}",
"\\H;",
"\\H-1;x",
"\\Hx;",
"\\C999;a",
"\\f;",
"\\W0;a",
"\\Q90;a",
"\\P\\P\\P",
] {
let _ = lay(s, 1.0);
}
let long = "word ".repeat(5000);
assert!(lay(&long, 10.0).lines.len() > 100);
// Thai with stacks, format runs, stray marks and unpaired braces in one word.
for s in ["ก\\S1/2;ข{\\C1;ภาษา}ไทย\u{0E4C}\u{0E4C}", "{\\H2x;ภาษา}{\\H3x;ไทย}", "\u{0E33}\u{0E49}\\S/;ไทย}}", "ภาษาไทย\tภาษาไทย"]
{
let _ = lay(s, 0.5);
let _ = lay(&format!("\\pi1,l2,t3;{s}"), 0.5);
}
// Too long to look up: one unbreakable word, like a long Latin word.
assert_eq!(lay(&"ภาษาไทย".repeat(20_000), 3.0).lines.len(), 1);
}
/// Thai writes no spaces between words: a long line wraps between words, in the plain and the
/// paragraph layout, without fonts installed (the line breaks don't depend on glyph widths).
#[test]
fn thai_wraps_between_words() {
let text = "เหล็กกล่องขนาดมาตรฐานสำหรับโครงหลังคาเมทัลชีทแผ่นเรียบสีขาว";
let one = lay(text, 0.0);
assert_eq!(one.lines, vec![text]);
let wide = one.bounds.width();
for contents in [text.to_string(), format!("\\pi0,l0;{text}"), format!("{{\\C1;เหล็กกล่อง}}{}", text.trim_start_matches("เหล็กกล่อง"))]
{
let l = lay(&contents, wide / 3.0);
assert!(l.lines.len() >= 3, "{contents}: {:?}", l.lines);
assert_eq!(l.lines.concat(), text, "nothing lost or added: {:?}", l.lines);
for line in &l.lines {
let first = line.chars().next().unwrap_or(' ');
assert!(!crate::bidi::is_mark(first) && !matches!(first, 'ะ' | 'า' | 'ำ'), "{line}");
assert!(!line.ends_with(['เ', 'แ', 'โ', 'ใ', 'ไ']), "{line}");
}
}
// The pieces sit side by side with nothing between them: drawn with a Thai font they are
// as wide as the word laid out in one piece (up to kerning).
let pieces = split_thai(vec![(Seg::Text(text.to_string()), 0)]);
if one.pieces.iter().any(|p| !p.shaped.glyphs.is_empty()) {
let base = Fmt { font: TextFont::Stroke, height: 1.0, color: None, under: false, over: false, wf: 1.0, oblique: 0.0, valign: 1 };
let sum: f64 = pieces.iter().flatten().map(|(seg, _)| seg_width(seg, &base)).sum();
assert!((sum - wide).abs() < 0.01 * wide, "{sum} {wide}");
}
assert!(pieces.len() > 3);
// Latin and other words stay whole; stacks keep their place.
assert_eq!(split_thai(vec![(Seg::Text("hello".into()), 0)]).len(), 1);
let mixed = split_thai(vec![(Seg::Text("ภาษา".into()), 0), (Seg::Stack("1".into(), "2".into(), '/'), 0), (Seg::Text("ภาษาไทย".into()), 1)]);
let flat: Vec<String> = mixed.iter().map(|p| p.iter().map(|(s, _)| seg_plain(s)).collect()).collect();
assert_eq!(flat, ["ภาษา1/2ภาษา", "ไทย"]);
}
/// A Thai run that would fit a line at the left indent but not after a first-line indent or a
/// leading tab still breaks between its words instead of running out of the column. The indent
/// is well over the slack: with no Thai font the stroke font's pieces are narrower than the run
/// (each loses its trailing letter gap).
#[test]
fn thai_wraps_after_first_line_indent_and_tab() {
let text = "เหล็กกล่องขนาดมาตรฐานสำหรับโครงหลังคา";
let wide = lay(text, 0.0).bounds.width();
for contents in [format!("\\pi4,l0;{text}"), format!("\\pxt4;\t{text}")] {
let l = lay(&contents, wide + 1.0);
assert!(l.lines.len() >= 2, "{contents}: {:?}", l.lines);
assert_eq!(l.lines.concat().trim_start(), text, "{contents}: {:?}", l.lines);
}
}
/// Text that fits at its spaces keeps those line breaks: a note from the sample drawing
/// (frame, box section 50x50 mm, painted white) at every column width from one word up never
/// breaks inside ทาสีขาว or any other spaced word, in the plain and the paragraph layout.
#[test]
fn spaced_thai_keeps_its_space_breaks() {
let text = "โครงแนวนอน เหล็กกล่อง ขนาด 50x50 mm. ทาสีขาว";
let words: Vec<&str> = text.split(' ').collect();
let widest = words.iter().map(|w| lay(w, 0.0).bounds.width()).fold(0.0, f64::max);
let full = lay(text, 0.0).bounds.width();
let mut width = widest * 1.001;
while width < full * 1.05 {
for contents in [text.to_string(), format!("\\pxql;\\A1;{text}")] {
let l = lay(&contents, width);
assert_eq!(l.lines.join(" "), text, "{width}: {:?}", l.lines);
assert!(l.lines.iter().flat_map(|line| line.split(' ')).all(|w| words.contains(&w)), "{width}: {:?}", l.lines);
}
width *= 1.05;
}
// After a Latin word, a Thai run too long for any line breaks between its words, filling
// the line it starts on.
let long = "เหล็กกล่องขนาดมาตรฐานสำหรับโครงหลังคา";
let l = lay(&format!("abc {long}"), lay(long, 0.0).bounds.width() / 2.0);
assert!(l.lines.len() >= 3 && l.lines.first().is_some_and(|f| f.starts_with("abc ") && f.len() > 4), "{:?}", l.lines);
assert_eq!(l.lines.concat(), format!("abc {long}"));
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.