1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
pub extern crate regex;
pub extern crate treexml;
pub extern crate env_logger;
pub mod directions;
pub mod grid;
pub mod attrs {
use regex::Regex;
fn split_attr(attr: &str) -> Vec<(String, String)> {
lazy_static! {
static ref KV: Regex =
Regex::new(r#"([a-zA-Z0-9_-]*)=(?:'([^']*)'|"([^"]*)")"#)
.unwrap_or_else(|e| panic!("ill-formatted regex: {}", e));
}
let mut attrs = Vec::new();
for cap in KV.captures_iter(attr) {
debug!("cargo:warning=cap: '{:?}' cap.get(0): '{:?}'", cap, cap.get(0));
attrs.push((cap.get(1).unwrap_or_else(|| panic!("no cap at 1")).as_str().to_string(),
cap.get(2).or(cap.get(3)).unwrap_or_else(|| panic!("no cap at 2/3")).as_str().to_string()));
}
attrs
}
pub fn input_attr(attrs: &mut Option<Vec<(String, String)>>, attr: &str) {
let new = split_attr(attr);
if new.is_empty() { return; }
match *attrs {
Some(ref mut attrs) => {
attrs.extend(new);
}
None => {
*attrs = Some(new);
}
}
}
}
pub mod svg;
pub mod path;
pub use path::Path;
pub mod find_path;
pub mod text;
pub mod find_text;
pub mod scene {
use path::{Path};
use format;
use grid::{Grid};
use text::{Text};
pub struct SceneOpts {
pub path_infer_id: bool,
pub text_infer_id: bool,
pub unforged: Unforgeable,
}
pub struct Unforgeable {
_hidden: ()
}
impl Default for SceneOpts {
fn default() -> Self {
SceneOpts {
path_infer_id: true,
text_infer_id: true,
unforged: Unforgeable { _hidden: () },
}
}
}
pub struct Scene {
paths: Vec<Path>,
texts: Vec<Text>,
width: u32,
height: u32,
}
impl Scene {
pub fn paths(&self) -> &[Path] { &self.paths }
pub fn texts(&self) -> &[Text] { &self.texts }
pub fn width(&self) -> u32 { self.width }
pub fn height(&self) -> u32 { self.height }
}
impl Grid {
pub fn into_scene(mut self, format: &format::Table, opts: Option<SceneOpts>) -> Scene {
use find_path::{find_closed_path, find_unclosed_path};
use find_text::{find_text};
use grid::{Pt};
let opts = opts.unwrap_or(Default::default());
let mut paths = vec![];
let mut texts = vec![];
for row in 1...self.height {
for col in 1...self.width {
loop {
let pt = Pt(col as i32, row as i32);
if let Some(mut p) = find_closed_path(&self, format, pt) {
if opts.path_infer_id {
p.infer_id(&self);
}
p.attach_attributes(pt, &self);
debug!("pt {:?} => closed path {:?}", pt, p);
self.remove_path(&p);
paths.push(p);
} else {
break;
}
}
}
}
for row in 1...self.height {
for col in 1...self.width {
loop {
let pt = Pt(col as i32, row as i32);
if let Some(mut p) = find_unclosed_path(&self, format, pt) {
if opts.path_infer_id {
p.infer_id(&self);
}
p.attach_attributes(pt, &self);
debug!("pt {:?} => unclosed path {:?}", pt, p);
self.remove_path(&p);
paths.push(p);
} else {
break;
}
}
}
}
for row in 1...self.height {
for col in 1...self.width {
loop {
let pt = Pt(col as i32, row as i32);
if let Some(mut txt) = find_text(&self, pt) {
if opts.text_infer_id {
txt.infer_id(&self);
}
txt.attach_attributes(pt, &self);
debug!("txt {:?} => text {:?}", pt, txt);
self.remove_text(&txt);
texts.push(txt);
} else {
break;
}
}
}
}
Scene { paths: paths, texts: texts, width: self.width, height: self.height }
}
}
}
pub use self::scene::Scene;
use treexml::{Element};
use svg::{ToElement};
impl ToElement for Scene {
fn to_element(&self) -> Element {
let mut s = String::new();
let mut e = Element::new("pre");
e.attributes.insert("style".to_string(),
"font-size: 50%;".to_string());
s.push_str(&format!("width: {} height: {}\n", self.width(), self.height()));
for (i, p) in self.paths().iter().enumerate() {
s.push_str(&format!("path[{}]: Path {{ id: {:?}, closed: {:?}, attrs: {:?}\n",
i, p.id, p.closed, p.attrs));
for (j, step) in p.steps.iter().enumerate() {
s.push_str(&format!(" path[{}][{}]: {:?},\n", i, j, step));
}
s.push_str(&"}\n");
}
for (i, t) in self.texts().iter().enumerate() {
s.push_str(&format!("text[{}]: Text {{ \
id: {:?} pt: ({},{}), content: {}, attrs: {:?} \
}}\n",
i, t.id, t.pt.col(), t.pt.row(), t.content, t.attrs));
}
e.text = Some(s);
e
}
}
pub mod test_data;
pub mod format;
pub mod default_input;
pub mod render;
#[cfg(build = "release")]
#[test]
fn end_to_end_basics() {
const PRINT_INTERMEDIATE_SCENE_STRUCTURE: bool = false;
use grid::{Grid};
use render::{RenderS};
use render::svg::{SvgRender};
use svg::{IntoElement, ToElement};
use treexml::{Document, Element};
use std::path::{Path};
use std::fs::{File};
use std::io::{Write};
let _ = ::env_logger::init();
let mut html_doc = Document::default();
let mut html_body = Element::new("body");
for &(name, d) in &test_data::ALL {
println!("processing {}", name);
let r = SvgRender {
x_scale: 8, y_scale: 13,
font_family: "Menlo".to_string(), font_size: 13,
show_gridlines: true,
infer_rect_elements: true,
name: name.to_string(),
format_table: Default::default(),
};
html_body.children.push({
let mut e = Element::new("h3");
e.text = Some(name.to_string());
e
});
html_body.children.push({
let mut e = Element::new("pre");
e.attributes.insert("style".to_string(),
"border:1px dotted black;".to_string());
e.text = Some(format!("{}", d));
e
});
let s = d.parse::<Grid>().unwrap().into_scene(&Default::default(),
Default::default());
if PRINT_INTERMEDIATE_SCENE_STRUCTURE {
html_body.children.push(s.to_element());
}
let elem = r.render_s(&s);
html_body.children.push(elem.into_element());
html_body.children.push(Element::new("hr"));
}
let mut html_elem = Element::new("html");
html_elem.children.push(html_body);
html_doc.root = Some(html_elem);
let mut f = File::create(Path::new("basics.html")).unwrap();
write!(&mut f, "{}", html_doc).unwrap();
}