mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-04 21:13:22 -05:00
Merge #3728
This commit is contained in:
commit
2a281c8d4c
2 files changed
+80
-23
No files matched your search
+49
-23
@@ -242,15 +242,29 @@ pub fn ole_frame_accepts(m: &cadcraft_geom::Mat3) -> bool {
|
||||
m.b.abs() < t && m.c.abs() < t && m.a > 0.0 && (m.a - m.d).abs() <= t
|
||||
}
|
||||
|
||||
/// How many characters a single-line text value draws, as the fonts read it: a `%%` code is the
|
||||
/// one character it stands for (`%%d`, `%%p`, `%%c`, `%%%`, `%%nnn`) or none (the underline and
|
||||
/// overline toggles `%%u` and `%%o`), `\U+XXXX` is one character, and invisible formatting
|
||||
/// characters (joiners, directional marks) take no room.
|
||||
fn drawn_chars(value: &str) -> usize {
|
||||
/// What a single-line text value draws, as the fonts read it.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
struct Drawn {
|
||||
/// How many characters take room.
|
||||
chars: usize,
|
||||
/// Whether any of them is underlined, and whether any is overlined.
|
||||
underlined: bool,
|
||||
overlined: bool,
|
||||
}
|
||||
|
||||
/// What a single-line text value draws, as the fonts read it: a `%%` code is the one character
|
||||
/// it stands for (`%%d`, `%%p`, `%%c`, `%%%`, `%%nnn`) or none (`%%u` and `%%o`, which turn the
|
||||
/// underline and the overline on and off for the characters after them), `\U+XXXX` is one
|
||||
/// character, and invisible formatting characters (joiners, directional marks) take no room.
|
||||
fn drawn_text(value: &str) -> Drawn {
|
||||
let invisible = |c: char| matches!(c, '\u{200B}'..='\u{200F}' | '\u{202A}'..='\u{202E}' | '\u{2060}'..='\u{2069}' | '\u{FEFF}' | '\u{061C}');
|
||||
let chars: Vec<char> = value.chars().collect();
|
||||
let (mut i, mut drawn) = (0usize, 0usize);
|
||||
// The toggles as they stand, and what the characters drawn so far have had.
|
||||
let (mut under, mut over) = (false, false);
|
||||
let mut out = Drawn::default();
|
||||
while let Some(&c) = chars.get(i) {
|
||||
let before = drawn;
|
||||
let escaped = (c == '\\' && matches!(chars.get(i + 1), Some('U' | 'u')) && chars.get(i + 2) == Some(&'+'))
|
||||
.then(|| chars.get(i + 3..i + 7))
|
||||
.flatten()
|
||||
@@ -262,7 +276,8 @@ fn drawn_chars(value: &str) -> usize {
|
||||
i += 7;
|
||||
} else if c == '%' && chars.get(i + 1) == Some(&'%') {
|
||||
match chars.get(i + 2).map_or(' ', char::to_ascii_lowercase) {
|
||||
'u' | 'o' => i += 3,
|
||||
'u' => (under, i) = (!under, i + 3),
|
||||
'o' => (over, i) = (!over, i + 3),
|
||||
'd' | 'p' | 'c' | '%' => (drawn, i) = (drawn + 1, i + 3),
|
||||
digit if digit.is_ascii_digit() => {
|
||||
let digits = chars.iter().skip(i + 2).take(3).take_while(|d| d.is_ascii_digit()).count();
|
||||
@@ -275,15 +290,19 @@ fn drawn_chars(value: &str) -> usize {
|
||||
drawn += usize::from(!invisible(c));
|
||||
i += 1;
|
||||
}
|
||||
if drawn > before {
|
||||
(out.underlined, out.overlined) = (out.underlined || under, out.overlined || over);
|
||||
}
|
||||
}
|
||||
drawn
|
||||
Drawn { chars: drawn, ..out }
|
||||
}
|
||||
|
||||
/// Bounds of a single-line TEXT / ATTDEF / ATTRIB of `len` drawn characters, following its
|
||||
/// Bounds of a single-line TEXT / ATTDEF / ATTRIB that draws `drawn`, following its
|
||||
/// justification the same way the renderer places it (the stroke font is about 0.88 of the height
|
||||
/// per character). Empty when nothing is drawn.
|
||||
fn text_box(t: &crate::Text, len: usize) -> Bounds2 {
|
||||
/// per character), its underline and overline included. Empty when nothing is drawn.
|
||||
fn text_box(t: &crate::Text, drawn: Drawn) -> Bounds2 {
|
||||
use crate::{HAlign, VAlign};
|
||||
let len = drawn.chars;
|
||||
if len == 0 {
|
||||
return Bounds2::EMPTY;
|
||||
}
|
||||
@@ -340,15 +359,22 @@ fn text_box(t: &crate::Text, len: usize) -> Bounds2 {
|
||||
HAlign::Center | HAlign::Middle => -w / 2.0,
|
||||
HAlign::Right => -w,
|
||||
};
|
||||
// The box from its bottom to its top. Bottom justification lifts the line by a third of the
|
||||
// height it is drawn at: the box starts where the shortest line would and ends where the
|
||||
// tallest would.
|
||||
let (y0, y1) = match (t.halign, t.valign) {
|
||||
(HAlign::Middle, _) | (_, VAlign::Middle) => (-h / 2.0, h / 2.0),
|
||||
(_, VAlign::Baseline) => (0.0, h),
|
||||
(_, VAlign::Bottom) => (low / 3.0, h / 3.0 + h),
|
||||
(_, VAlign::Top) => (-h, 0.0),
|
||||
// Where the baseline of a line drawn `drawn_h` high lies. Bottom justification lifts the line
|
||||
// by a third of the height it is drawn at.
|
||||
let baseline = |drawn_h: f64| match (t.halign, t.valign) {
|
||||
(HAlign::Middle, _) | (_, VAlign::Middle) => -drawn_h / 2.0,
|
||||
(_, VAlign::Baseline) => 0.0,
|
||||
(_, VAlign::Bottom) => drawn_h / 3.0,
|
||||
(_, VAlign::Top) => -drawn_h,
|
||||
};
|
||||
// The fonts draw an underline a fifth of the drawn height below the baseline and an overline
|
||||
// a fifth of it above the line's top.
|
||||
let below = if drawn.underlined { 0.2 } else { 0.0 };
|
||||
let above = if drawn.overlined { 1.2 } else { 1.0 };
|
||||
// The box from its bottom to its top: it starts where the lowest of the shortest and the
|
||||
// tallest possible line would and ends where the highest of the two would.
|
||||
let [(b0, t0), (b1, t1)] = [low, h].map(|drawn_h| (baseline(drawn_h) - below * drawn_h, baseline(drawn_h) + above * drawn_h));
|
||||
let (y0, y1) = (b0.min(b1), t0.max(t1));
|
||||
let local = [Vec2::new(dx, y0), Vec2::new(dx + w, y0), Vec2::new(dx + w, y1), Vec2::new(dx, y1)];
|
||||
// Backwards and upside-down text is mirrored about the point it is placed by.
|
||||
let (mx, my) = (if t.backwards { -1.0 } else { 1.0 }, if t.upside_down { -1.0 } else { 1.0 });
|
||||
@@ -484,7 +510,7 @@ fn constant_box(a: &crate::Attrib, upright: bool) -> Bounds2 {
|
||||
let turn = |r: f64| if upright { 0.0 } else { r };
|
||||
match a.multiline_text() {
|
||||
Some(m) if !blank(&m.contents) => mtext_box(&crate::MText { rotation: turn(m.rotation), ..m }),
|
||||
None if !blank(&a.text.value) => text_box(&crate::Text { rotation: turn(a.text.rotation), ..a.text.clone() }, drawn_chars(&a.text.value)),
|
||||
None if !blank(&a.text.value) => text_box(&crate::Text { rotation: turn(a.text.rotation), ..a.text.clone() }, drawn_text(&a.text.value)),
|
||||
_ => Bounds2::EMPTY,
|
||||
}
|
||||
}
|
||||
@@ -675,10 +701,10 @@ fn bounds_in<'a>(
|
||||
EntityKind::Text(t) if blank(&t.value) => Bounds2::EMPTY,
|
||||
EntityKind::MText(t) if blank(&t.contents) => Bounds2::EMPTY,
|
||||
// Text that matches the layout's orientation is drawn upright (#2498).
|
||||
EntityKind::Text(t) if e.common.matches_layout_orientation() => text_box(&crate::Text { rotation: 0.0, ..t.clone() }, drawn_chars(&t.value)),
|
||||
EntityKind::Text(t) if e.common.matches_layout_orientation() => text_box(&crate::Text { rotation: 0.0, ..t.clone() }, drawn_text(&t.value)),
|
||||
EntityKind::MText(t) if e.common.matches_layout_orientation() => mtext_box(&crate::MText { rotation: 0.0, ..t.clone() }),
|
||||
EntityKind::Text(t) => text_box(t, drawn_chars(&t.value)),
|
||||
EntityKind::AttDef(a) => text_box(&a.text, drawn_chars(&a.tag)),
|
||||
EntityKind::Text(t) => text_box(t, drawn_text(&t.value)),
|
||||
EntityKind::AttDef(a) => text_box(&a.text, drawn_text(&a.tag)),
|
||||
EntityKind::MText(t) => mtext_box(t),
|
||||
EntityKind::Insert(ins) => {
|
||||
if depth >= MAX_BLOCK_DEPTH {
|
||||
@@ -727,7 +753,7 @@ fn bounds_in<'a>(
|
||||
if !a.invisible && layer_counts(d, attrib_layer(defs, a), off_counts, frozen) {
|
||||
b = b.union(&match a.multiline_text() {
|
||||
Some(m) => mtext_box(&m),
|
||||
None => text_box(&a.text, drawn_chars(&a.text.value)),
|
||||
None => text_box(&a.text, drawn_text(&a.text.value)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -377,6 +377,37 @@ fn text_that_draws_nothing_has_no_extents() {
|
||||
}
|
||||
}
|
||||
|
||||
/// An underline is drawn below the baseline and an overline above the top of the line: the
|
||||
/// extents hold them, however the text is justified, for plain text and for Aligned text, plain
|
||||
/// or mirrored. A toggle that is off again before any character draws nothing and changes nothing.
|
||||
#[test]
|
||||
fn text_extents_hold_underlines_and_overlines() {
|
||||
use cadcraft_doc::HAlign::{Aligned, Left};
|
||||
use cadcraft_doc::VAlign::{Baseline, Bottom, Middle, Top};
|
||||
for halign in [Left, Aligned] {
|
||||
for valign in [Baseline, Bottom, Middle, Top] {
|
||||
for flags in [(false, false), (false, true)] {
|
||||
let of = |value: &str| drawn_and_extents_at(halign, valign, value, 1.0, 1.0, 20.0, flags);
|
||||
let (plain_drawn, plain_extents) = of("AB");
|
||||
// The last underlines only its second letter.
|
||||
for value in ["%%uAB", "%%oAB", "%%u%%oAB", "A%%uB"] {
|
||||
let (drawn, extents) = of(value);
|
||||
assert!(
|
||||
drawn.height() > plain_drawn.height(),
|
||||
"{halign:?} {valign:?} {flags:?} {value:?}: the line is drawn outside the letters"
|
||||
);
|
||||
assert!(
|
||||
extents.min.y <= drawn.min.y + 1e-6 && extents.max.y >= drawn.max.y - 1e-6,
|
||||
"{halign:?} {valign:?} {flags:?} {value:?}: drawn {drawn:?} outside extents {extents:?}"
|
||||
);
|
||||
}
|
||||
let (_, off) = of("%%u%%uAB");
|
||||
assert_eq!((off.min.y, off.max.y), (plain_extents.min.y, plain_extents.max.y), "{halign:?} {valign:?} {flags:?}: no line, same box");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hatch_pattern_and_solid() {
|
||||
let mut d = Drawing::new_imperial();
|
||||
|
||||
Reference in new issue
Block a user