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
//! Shows the coordinates of a figure as numbers
//!
//! Usage:
//! cargo run --example show -- 'A?[b,=]-[:60]B-[:-60]C?[b]'
//!
//! When a figure looks wrong, use this to check where the atoms were placed and where each
//! line was drawn from and to.
use lina::{Style, to_figure};
const DEFAULT_FONT: &str = "font/latinmodern-math.otf";
fn main() {
let argument: Vec<String> = std::env::args().skip(1).collect();
if argument.is_empty() {
eprintln!("分子を 1 つ以上わたしてください");
std::process::exit(1);
}
let data = match std::fs::read(DEFAULT_FONT) {
Ok(data) => data,
Err(reason) => {
eprintln!("フォントを読めません: {DEFAULT_FONT}: {reason}");
std::process::exit(1);
}
};
let font = lina::load_font(data).expect("フォントを読み込めません");
let style = Style::default();
for source in &argument {
println!("{source}");
let figure = match to_figure(source, &font, &style) {
Ok(figure) => figure,
Err(reason) => {
println!(" {reason}");
continue;
}
};
for (order, atom) in figure.atom.iter().enumerate() {
let mark: String = atom.label.placed.iter().map(|placed| placed.character).collect();
let mark = if mark.is_empty() {"印なし".to_string()} else {mark};
println!(" 原子 {order} ({:.3}, {:.3}) {mark}", atom.x, atom.y);
}
for line in &figure.line {
println!(
" 線 ({:.3}, {:.3}) - ({:.3}, {:.3})",
line.x1, line.y1, line.x2, line.y2
);
}
for wedge in &figure.wedge {
let filled = if wedge.filled {"塗る"} else {"塗らない"};
println!(
" くさび ({:.3}, {:.3}) から幅 {:.3} {filled}",
wedge.tip.0,
wedge.tip.1,
((wedge.base[0].0 - wedge.base[1].0).powi(2)
+ (wedge.base[0].1 - wedge.base[1].1).powi(2))
.sqrt()
);
}
for circle in &figure.circle {
println!(" 丸 ({:.3}, {:.3}) 半径 {:.3}", circle.x, circle.y, circle.radius);
}
println!();
}
}