mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-03 22:41:19 +00:00
Compare commits
1
Commits
main
...
land/3726-bound
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07b219b707 |
@@ -836,6 +836,9 @@ pub fn regapp(ctx: &mut Ctx<'_>, a: Vec<Value>) -> Result<Flow, LispError> {
|
||||
Ok(Flow::Value(Value::str(name.trim())))
|
||||
}
|
||||
|
||||
/// The most bytes of text `textbox` measures.
|
||||
const MAX_TEXTBOX_TEXT: usize = 64 << 10;
|
||||
|
||||
/// `(textbox elist)`: the smallest box around a TEXT that `elist` describes (an `entget` list, or
|
||||
/// a partial one with at least the string, group 1), as its lower-left and upper-right corners
|
||||
/// relative to the insertion point, the text unrotated and left-justified. Groups the list
|
||||
@@ -856,6 +859,13 @@ pub fn textbox(ctx: &mut Ctx<'_>, a: Vec<Value>) -> Result<Flow, LispError> {
|
||||
if group(1).is_none() || group(0).is_some_and(|k| !matches!(&k, Value::Str(s) if s.eq_ignore_ascii_case("TEXT"))) {
|
||||
return nil();
|
||||
}
|
||||
// Shaping makes many points for each character, which the run's string budget does not
|
||||
// count: a text too long to measure in bounded memory is an error, not a box.
|
||||
if let Some(Value::Str(s)) = group(1)
|
||||
&& s.len() > MAX_TEXTBOX_TEXT
|
||||
{
|
||||
return Err(LispError::new(format!("textbox: text longer than {MAX_TEXTBOX_TEXT} bytes")));
|
||||
}
|
||||
let Ok(d) = ctx.s.doc() else { return nil() };
|
||||
let style = match group(7) {
|
||||
Some(Value::Str(s)) => s.to_string(),
|
||||
|
||||
@@ -57,6 +57,14 @@ fn textbox_measures_text_as_drawn() {
|
||||
let blank = textbox(&mut s, "'((1 . \" \"))").expect("spaces");
|
||||
assert!(blank[0] == 0.0 && blank[1] == 0.0 && blank[2] > 0.0 && blank[3] > 0.0, "{blank:?}");
|
||||
|
||||
// A text too long to shape in bounded memory is an error that names the function, and one
|
||||
// well under the limit still measures.
|
||||
let long_text = |n: usize| json!({ "code": format!("(textbox '((1 . \"{}\")))", "8".repeat(n)) });
|
||||
let refused = s.execute("lisp", &long_text(1_000_000)).unwrap();
|
||||
assert!(refused["error"].as_str().is_some_and(|e| e.contains("textbox")), "{refused}");
|
||||
let measured = s.execute("lisp", &long_text(1_000)).unwrap();
|
||||
assert!(measured["error"].is_null() && measured["value"] != "nil", "{measured}");
|
||||
|
||||
// Not a text: nil.
|
||||
assert_eq!(textbox(&mut s, "'((0 . \"LINE\") (10 0.0 0.0 0.0) (11 5.0 5.0 0.0) (1 . \"x\"))"), None);
|
||||
assert_eq!(textbox(&mut s, "'((0 . \"TEXT\") (40 . 1.0))"), None, "no string");
|
||||
|
||||
Reference in New Issue
Block a user