|
1 | 1 | extern crate libfonthelper; |
2 | 2 |
|
3 | | -use libfonthelper::{get_fonts, types::FontMap}; |
| 3 | +use libfonthelper::{font::Font, Fonts}; |
4 | 4 | use neon::{prelude::*, task::Task}; |
5 | 5 |
|
6 | 6 | struct Worker { |
7 | 7 | dirs: Vec<String>, |
8 | 8 | } |
9 | 9 |
|
10 | 10 | impl Task for Worker { |
11 | | - type Output = FontMap; |
| 11 | + type Output = Vec<Font>; |
12 | 12 | type Error = String; |
13 | 13 | type JsEvent = JsObject; |
14 | 14 |
|
15 | | - fn perform(&self) -> Result<FontMap, String> { |
16 | | - match get_fonts(&self.dirs) { |
| 15 | + fn perform(&self) -> Result<Vec<Font>, String> { |
| 16 | + match Fonts::new(&self.dirs) { |
17 | 17 | Err(err) => return Err(String::from(err.to_string())), |
18 | | - Ok(fonts) => return Ok(fonts), |
| 18 | + Ok(fonts) => { |
| 19 | + let mut map: Vec<Font> = Vec::new(); |
| 20 | + for font in fonts { |
| 21 | + map.push(font); |
| 22 | + } |
| 23 | + |
| 24 | + return Ok(map); |
| 25 | + } |
19 | 26 | }; |
20 | 27 | } |
21 | 28 |
|
22 | | - fn complete(self, mut cx: TaskContext, result: Result<FontMap, String>) -> JsResult<JsObject> { |
| 29 | + fn complete( |
| 30 | + self, |
| 31 | + mut cx: TaskContext, |
| 32 | + result: Result<Vec<Font>, String>, |
| 33 | + ) -> JsResult<JsObject> { |
23 | 34 | let js_fonts = JsObject::new(&mut cx); |
24 | 35 |
|
25 | | - for (path, font) in result.unwrap() { |
26 | | - let js_font_entries = JsArray::new(&mut cx, font.len() as u32); |
27 | | - |
28 | | - for (index, entry) in font.iter().enumerate() { |
29 | | - let js_font_entry = JsObject::new(&mut cx); |
30 | | - |
31 | | - let id = cx.string(&entry.id); |
32 | | - let postscript = cx.string(&entry.postscript); |
33 | | - let family = cx.string(&entry.family); |
34 | | - let style = cx.string(&entry.style); |
35 | | - let weight = cx.number(i32::from(entry.weight)); |
36 | | - let stretch = cx.number(i32::from(entry.stretch)); |
37 | | - let italic = cx.boolean(entry.italic); |
38 | | - |
39 | | - js_font_entry.set(&mut cx, "id", id)?; |
40 | | - js_font_entry.set(&mut cx, "postscript", postscript)?; |
41 | | - js_font_entry.set(&mut cx, "family", family)?; |
42 | | - js_font_entry.set(&mut cx, "style", style)?; |
43 | | - js_font_entry.set(&mut cx, "weight", weight)?; |
44 | | - js_font_entry.set(&mut cx, "stretch", stretch)?; |
45 | | - js_font_entry.set(&mut cx, "italic", italic)?; |
46 | | - |
47 | | - js_font_entries.set(&mut cx, index.to_string().as_str(), js_font_entry)?; |
| 36 | + match result { |
| 37 | + Err(err) => println!("Cannot get fonts, error: {}", err), |
| 38 | + Ok(fonts) => { |
| 39 | + for font in fonts { |
| 40 | + if (font.path.eq("")) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + let js_font_entries = JsArray::new(&mut cx, font.entries.len() as u32); |
| 45 | + |
| 46 | + for (index, entry) in font.entries.iter().enumerate() { |
| 47 | + let js_font_entry = JsObject::new(&mut cx); |
| 48 | + |
| 49 | + let id = cx.string(&entry.id); |
| 50 | + let postscript = cx.string(&entry.postscript); |
| 51 | + let family = cx.string(&entry.family); |
| 52 | + let style = cx.string(&entry.style); |
| 53 | + let weight = cx.number(i32::from(entry.weight)); |
| 54 | + let stretch = cx.number(i32::from(entry.stretch)); |
| 55 | + let italic = cx.boolean(entry.italic); |
| 56 | + |
| 57 | + js_font_entry.set(&mut cx, "id", id)?; |
| 58 | + js_font_entry.set(&mut cx, "postscript", postscript)?; |
| 59 | + js_font_entry.set(&mut cx, "family", family)?; |
| 60 | + js_font_entry.set(&mut cx, "style", style)?; |
| 61 | + js_font_entry.set(&mut cx, "weight", weight)?; |
| 62 | + js_font_entry.set(&mut cx, "stretch", stretch)?; |
| 63 | + js_font_entry.set(&mut cx, "italic", italic)?; |
| 64 | + |
| 65 | + js_font_entries.set(&mut cx, index.to_string().as_str(), js_font_entry)?; |
| 66 | + } |
| 67 | + |
| 68 | + js_fonts.set(&mut cx, font.path.as_str(), js_font_entries)?; |
| 69 | + } |
48 | 70 | } |
49 | | - |
50 | | - js_fonts.set(&mut cx, path.as_str(), js_font_entries)?; |
51 | 71 | } |
52 | 72 |
|
53 | 73 | Ok(js_fonts) |
|
0 commit comments