mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Merge pull request #10524 from ruchirguitar/perf/ai-word-search-budget
.ai import: hold the page type's word searches to their own budget
This commit is contained in:
1 file changed
+162
-23
@@ -154,8 +154,19 @@ pub(super) fn made_only(
|
||||
if looks && infos.len().saturating_mul(shown.len()) <= MAX_PAIRS {
|
||||
let spelled: Vec<String> =
|
||||
content.iter().map(|c| if let Content::Made(node, ..) = c { spelling(&plain_text(node)) } else { String::new() }).collect();
|
||||
let made_type = |o: &[Arc<Node>]| covered(o, &content, |_| true) && has_made_words(o, &content, &spelled);
|
||||
// Past the word searches, type where made type draws is taken to be that type.
|
||||
let (mut checks, mut unchecked) = (WordChecks::new(), false);
|
||||
let mut made_type = |o: &[Arc<Node>]| {
|
||||
covered(o, &content, |_| true)
|
||||
&& has_made_words(o, &content, &spelled, &mut checks).unwrap_or_else(|| {
|
||||
unchecked = true;
|
||||
true
|
||||
})
|
||||
};
|
||||
let mut spare: Vec<Vec<Arc<Node>>> = shown.iter().filter(|o| !made_type(o)).cloned().collect();
|
||||
if unchecked {
|
||||
note_unchecked(warn);
|
||||
}
|
||||
fill_look_slots(infos, &mut content, &mut spare);
|
||||
for c in &content {
|
||||
if let Content::Page(parts) = c {
|
||||
@@ -173,7 +184,7 @@ pub(super) fn made_only(
|
||||
match see_page_type(&mut content, untaken) {
|
||||
Some(0) => {}
|
||||
Some(_) => warn.push(PAGE_TYPE_LEFT_OUT.into()),
|
||||
None => warn.push(PAGE_TYPE_UNCHECKED.into()),
|
||||
None => note_unchecked(warn),
|
||||
}
|
||||
drop_look_slots(infos, &mut content);
|
||||
fill_hidden(infos, stories, &mut content, hidden);
|
||||
@@ -274,17 +285,64 @@ fn fill_look_slots(infos: &[SlotInfo], content: &mut [Content], spare: &mut Vec<
|
||||
drop_look_slots(infos, content);
|
||||
}
|
||||
|
||||
/// How many more times a word of the page's type may be looked for in a text object's type. The
|
||||
/// page's type is weighed against where every text object lies up to `MAX_PLACE_PAIRS` pairs, a box
|
||||
/// against a box, but each text object a piece of it lies on can be searched for each of its words:
|
||||
/// those searches are held to `MAX_PAIRS` of their own.
|
||||
struct WordChecks(usize);
|
||||
|
||||
impl WordChecks {
|
||||
fn new() -> Self {
|
||||
WordChecks(MAX_PAIRS)
|
||||
}
|
||||
|
||||
/// One more search, if any are left.
|
||||
fn spend(&mut self) -> Option<()> {
|
||||
self.0 = self.0.checked_sub(1)?;
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Are no searches left?
|
||||
fn spent(&self) -> bool {
|
||||
self.0 == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Has made type that shows and draws where the page's text object `object` lies every word of it
|
||||
/// ([`spelling`], each slot's in `spelled`), in any order?
|
||||
fn has_made_words(object: &[Arc<Node>], content: &[Content], spelled: &[String]) -> bool {
|
||||
let Some(r) = extent(object) else { return true };
|
||||
/// ([`spelling`], each slot's in `spelled`), in any order? `None` once `checks` runs out before
|
||||
/// that is known.
|
||||
fn has_made_words(object: &[Arc<Node>], content: &[Content], spelled: &[String], checks: &mut WordChecks) -> Option<bool> {
|
||||
let Some(r) = extent(object) else { return Some(true) };
|
||||
if checks.spent() {
|
||||
return None;
|
||||
}
|
||||
let near: Vec<&String> = content
|
||||
.iter()
|
||||
.zip(spelled)
|
||||
.filter(|(c, _)| matches!(c, Content::Made(_, Some(region), _) if !region.intersect(r).is_zero_area()))
|
||||
.map(|(_, s)| s)
|
||||
.collect();
|
||||
object_words(object).iter().all(|w| near.iter().any(|s| s.contains(w.as_str())))
|
||||
for w in object_words(object) {
|
||||
let mut found = false;
|
||||
for s in &near {
|
||||
checks.spend()?;
|
||||
if s.contains(w.as_str()) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return Some(false);
|
||||
}
|
||||
}
|
||||
Some(true)
|
||||
}
|
||||
|
||||
/// Note once in `warn` that some of the page's type wasn't checked against the text objects.
|
||||
fn note_unchecked(warn: &mut Vec<String>) {
|
||||
if !warn.iter().any(|w| w == PAGE_TYPE_UNCHECKED) {
|
||||
warn.push(PAGE_TYPE_UNCHECKED.into());
|
||||
}
|
||||
}
|
||||
|
||||
/// The text slots for type that a look draws and that have nothing have no type of their own to hold
|
||||
@@ -438,9 +496,16 @@ pub(super) fn plan(
|
||||
let made_stories: std::collections::BTreeSet<u32> = infos.iter().zip(&made).filter(|(_, m)| m.is_some()).filter_map(|(i, _)| i.story).collect();
|
||||
let open: Vec<bool> = infos.iter().zip(&made).map(|(i, m)| m.is_none() && i.story.is_none_or(|s| !made_stories.contains(&s))).collect();
|
||||
let owner = match_page_objects(infos, &lines, &open, shown);
|
||||
// Where the made type that doesn't show lies, by slot.
|
||||
let hidden_at: Vec<(usize, Rect)> =
|
||||
infos.iter().zip(&made).enumerate().filter_map(|(k, (info, m))| Some((k, m.as_ref().filter(|_| !info.shown)?.1?))).collect();
|
||||
// Where the made type that doesn't show lies, and the letters of its lines (it has lines).
|
||||
let hidden_at: Vec<(Rect, Vec<String>)> = infos
|
||||
.iter()
|
||||
.zip(&made)
|
||||
.enumerate()
|
||||
.filter_map(|(k, (info, m))| {
|
||||
let place = m.as_ref().filter(|_| !info.shown)?.1?;
|
||||
Some((place, lines.get(k)?.as_ref()?.iter().map(|(text, _)| letters(text)).collect()))
|
||||
})
|
||||
.collect();
|
||||
// Type the text document makes, shown or not: the page's pieces of it aren't needed. Its box
|
||||
// stays that of type without a layout here: with text objects whose type isn't made stacked on
|
||||
// made type that has their words (the copies of an outlined heading), the page's pieces in the
|
||||
@@ -448,17 +513,34 @@ pub(super) fn plan(
|
||||
let mut content = made_content(infos, made, false);
|
||||
// Is `object` the type of a text object that doesn't show, as the page draws it: in its frame,
|
||||
// with its words? (A copied object's lines are where its original's are, so they don't say.)
|
||||
let hidden_type = |object: &[Arc<Node>]| {
|
||||
let Some(r) = extent(object) else { return false };
|
||||
// `None` once `checks` runs out before that is known.
|
||||
let hidden_type = |object: &[Arc<Node>], checks: &mut WordChecks| -> Option<bool> {
|
||||
let Some(r) = extent(object) else { return Some(false) };
|
||||
let texts: Vec<Vec<String>> =
|
||||
object.iter().filter_map(|n| if let NodeKind::Text(t) = &n.kind { Some(words(&t.plain_text())) } else { None }).collect();
|
||||
hidden_at.iter().any(|(k, place)| {
|
||||
place.intersect(r).area() >= 0.5 * r.area().max(1e-9)
|
||||
&& lines.get(*k).and_then(Option::as_ref).is_some_and(|l| {
|
||||
!texts.is_empty()
|
||||
&& texts.iter().all(|ws| !ws.is_empty() && ws.iter().all(|w| l.iter().any(|(text, _)| letters(text).contains(w.as_str()))))
|
||||
})
|
||||
})
|
||||
if texts.is_empty() || texts.iter().any(Vec::is_empty) {
|
||||
return Some(false);
|
||||
}
|
||||
for (place, l) in &hidden_at {
|
||||
if place.intersect(r).area() < 0.5 * r.area().max(1e-9) {
|
||||
continue;
|
||||
}
|
||||
let mut all = true;
|
||||
'words: for w in texts.iter().flatten() {
|
||||
for text in l {
|
||||
checks.spend()?;
|
||||
if text.contains(w.as_str()) {
|
||||
continue 'words;
|
||||
}
|
||||
}
|
||||
all = false;
|
||||
break;
|
||||
}
|
||||
if all {
|
||||
return Some(true);
|
||||
}
|
||||
}
|
||||
Some(false)
|
||||
};
|
||||
// The letters and digits of each slot's made type that shows, in any script, in lower case.
|
||||
let spelled: Vec<String> = content
|
||||
@@ -472,6 +554,12 @@ pub(super) fn plan(
|
||||
})
|
||||
.collect();
|
||||
let mut spare: Vec<Vec<Arc<Node>>> = vec![];
|
||||
// The word searches below are held to their own budget: past it, type where made type that shows
|
||||
// draws is taken to be that type (made type is there either way), and type where only made type
|
||||
// that doesn't show lies is spare type, as any type no text object takes: it goes into a slot
|
||||
// or with another text object's type, else it is left out with a note (taken as the hidden
|
||||
// type, it would be dropped unnoted).
|
||||
let (mut checks, mut unchecked) = (WordChecks::new(), false);
|
||||
for (j, object) in shown.iter().enumerate() {
|
||||
match owner.get(j).copied().flatten() {
|
||||
Some(k) => match content.get_mut(k) {
|
||||
@@ -479,13 +567,28 @@ pub(super) fn plan(
|
||||
Some(c) => *c = Content::Page(object.clone()),
|
||||
None => {}
|
||||
},
|
||||
// What lies where made type draws, with its words, is that type as the page draws it (in
|
||||
// another font, maybe, and in any script).
|
||||
None if covered(object, &content, |_| true) && has_made_words(object, &content, &spelled) => {}
|
||||
None if hidden_type(object) => {}
|
||||
None => spare.push(object.clone()),
|
||||
None => {
|
||||
// What lies where made type draws, with its words, is that type as the page draws it
|
||||
// (in another font, maybe, and in any script).
|
||||
let theirs = covered(object, &content, |_| true)
|
||||
&& has_made_words(object, &content, &spelled, &mut checks).unwrap_or_else(|| {
|
||||
unchecked = true;
|
||||
true
|
||||
});
|
||||
let hidden = !theirs
|
||||
&& hidden_type(object, &mut checks).unwrap_or_else(|| {
|
||||
unchecked = true;
|
||||
false
|
||||
});
|
||||
if !theirs && !hidden {
|
||||
spare.push(object.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if unchecked {
|
||||
note_unchecked(warn);
|
||||
}
|
||||
// The page's type that no made type takes is the spare type below.
|
||||
let _ = see_page_type(&mut content, shown);
|
||||
for (k, c) in content.iter_mut().enumerate() {
|
||||
@@ -600,3 +703,39 @@ fn fill_hidden(infos: &[SlotInfo], stories: &[Option<Arc<Story>>], content: &mut
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use vectorcraft_doc::{CharStyle, TextObject};
|
||||
|
||||
fn text(id: u64, s: &str) -> Node {
|
||||
let style = CharStyle { size: 12.0, ..CharStyle::default() };
|
||||
Node::new(NodeId(id), NodeKind::Text(Box::new(TextObject::point(Point::new(100.0, 100.0), s, style))))
|
||||
}
|
||||
|
||||
/// The page's type is searched for in every made text object that draws where it lies, word by
|
||||
/// word: those searches stop when their budget runs out, saying they didn't finish, however many
|
||||
/// made text objects lie there. A word is searched for only until a text object has it.
|
||||
#[test]
|
||||
fn word_searches_of_the_page_s_type_stop_when_their_budget_runs_out() {
|
||||
let everywhere = Rect::new(-1e4, -1e4, 1e4, 1e4);
|
||||
let slots = 50;
|
||||
let content: Vec<Content> = (0..slots).map(|k| Content::Made(Box::new(text(k, "Other type")), Some(everywhere), false)).collect();
|
||||
let spelled: Vec<String> =
|
||||
content.iter().map(|c| if let Content::Made(n, ..) = c { spelling(&plain_text(n)) } else { String::new() }).collect();
|
||||
// A word no made type has is searched for in each of them.
|
||||
let missing = vec![Arc::new(text(1000, "Alpha beta"))];
|
||||
let one_word_everywhere = slots as usize;
|
||||
let mut short = WordChecks(one_word_everywhere - 1);
|
||||
assert_eq!(has_made_words(&missing, &content, &spelled, &mut short), None, "past the budget the answer isn't known");
|
||||
assert!(short.spent());
|
||||
let mut enough = WordChecks(one_word_everywhere);
|
||||
assert_eq!(has_made_words(&missing, &content, &spelled, &mut enough), Some(false));
|
||||
// Words that the first made type has are found there, without searching the others.
|
||||
let present = vec![Arc::new(text(1001, "other TYPE"))];
|
||||
let mut few = WordChecks(one_word_everywhere);
|
||||
assert_eq!(has_made_words(&present, &content, &spelled, &mut few), Some(true));
|
||||
assert!(one_word_everywhere - few.0 < one_word_everywhere / 2, "{} searches", one_word_everywhere - few.0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user