1use std::rc::Rc;
2use image::*;
3
4pub mod pages;
5pub mod paths;
6pub mod images_map;
7pub mod letter;
8pub mod word;
9pub mod line;
10
11pub use pages::PageProps;
12pub use images_map::ImagesMap;
13pub use letter::Letter;
14pub use word::Word;
15pub use line::Line;
16
17
18pub static CHARS_ONLY_UPPER : [char; 32] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '?', '!', ',', '.', ';', ':'];
19
20pub static CHARS_UNDER : [char; 6] = ['g', 'j', 'p', 'q', 'y', ','];
21
22pub static CHARS : [char; 58] = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z', '?', '!', ',', '.', ';', ':'];
23
24pub struct Text<'a> {
25 raw: String,
26 lines: Vec<Line<'a>>,
27 page_props: &'a PageProps<'a>,
28 imgs_map: Rc<ImagesMap<'a>>,
29}
30
31impl<'a> Text<'a> {
32 pub fn new(page_props : &'a PageProps) -> Text<'a> {
33 Text {
34 raw: String::new(),
35 page_props,
36 lines: Vec::new(),
37 imgs_map: Rc::new(ImagesMap::new(page_props))
38 }
39 }
40
41 pub fn new_with_map(page_props : &'a PageProps, imgs_map : Rc<ImagesMap<'a>>) -> Text<'a> {
42 Text {
43 raw: String::new(),
44 page_props,
45 lines: Vec::new(),
46 imgs_map,
47 }
48 }
49
50 pub fn new_with_dic_name(page_props : &'a PageProps, dic_name: &'a str) -> Text<'a> {
51 Text {
52 raw: String::new(),
53 page_props,
54 lines: Vec::new(),
55 imgs_map: Rc::new(ImagesMap::new_with_dic_name(page_props, dic_name))
56 }
57 }
58
59 pub fn push_word(&mut self, s_word : &str) {
60 match self.lines.last_mut() {
61 Some(actual_line) => {
62 let word = Word::new(s_word, self.page_props, Rc::clone(&self.imgs_map));
63
64 if actual_line.width + word.width + self.page_props.space_width > self.page_props.line_max_width() {
65 self.lines.push(Line::new(self.page_props, Rc::clone(&self.imgs_map)));
66 self.push_word(s_word);
67 } else {
68 actual_line.push(word);
69 }
70 },
71 None => {
72 self.lines.push(Line::new(self.page_props, Rc::clone(&self.imgs_map)));
73 self.push_word(s_word);
74 }
75 }
76 }
77
78 pub fn imgs_map(&self) -> &ImagesMap {
79 &self.imgs_map
80 }
81
82 pub fn parse(&mut self, string: String) {
83 for s_word in string.split(' ') {
84 self.push_word(s_word);
85 }
86
87 self.raw = string;
88 }
89
90 pub fn parse_str(&mut self, str : &str) {
91 self.parse(String::from(str));
92 }
93
94 pub fn print(&self) {
95 for line in &self.lines {
96 for word in &line.words {
97 print!("{} ", word.get_raw());
98 }
99 println!();
100 }
101 }
102
103 pub fn to_img(&mut self) -> Vec<RgbaImage> {
104 let mut pages = Vec::new();
105
106 let mut page = self.page_props.white_page();
107
108 let mut y = self.page_props.margins;
109 for line in self.lines.iter_mut() {
110 let mut x = self.page_props.margins;
111 for word in line.words.iter_mut() {
112 for letter in word.letters.iter_mut() {
113 let l_img = &*letter.img();
114 imageops::overlay(&mut page, l_img, x as u32, y as u32);
115 x += letter.width();
116 }
117
118 x += self.page_props.space_width;
119 }
120
121 y += self.page_props.line_height;
122
123 if y + self.page_props.line_height >= self.page_props.canvas.height as f32 - self.page_props.margins {
124 pages.push(page);
125 page = self.page_props.white_page();
126 y = self.page_props.margins;
127 }
128
129 }
130
131 pages.push(page);
132 pages
133 }
134
135 pub fn to_files(&mut self, files_name : Option<&str>) {
136 let images = self.to_img();
137
138 match std::fs::create_dir(paths::output_dir()) {
139 Ok(_) => (),
140 Err(_) => ()
141 };
142
143 for (i, img) in images.iter().enumerate() {
144 img.save(paths::page_path(i + 1, files_name)).unwrap();
145 }
146 }
147
148 pub fn parse_to_png(&mut self, str : &str, files_name : Option<&str>) {
149 self.parse_str(str);
150 self.to_files(files_name);
151 }
152}