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
use json::{JsonValue, object};

#[derive(Clone, Debug)]
pub struct Head {
    pub field: String,
    pub title: String,
    pub note: String,
    pub width: i32,
}

impl Head {
    pub fn new(field: &str, title: &str, note: &str, mut width: i32) -> Self {
        if width == 0 {
            width = 10;
        }
        Self {
            field: field.to_string(),
            title: title.to_string(),
            note: note.to_string(),
            width,
        }
    }
}

pub mod write;
pub mod read;

#[derive(Debug, Clone)]
pub struct Cell {
    /// 单元格内容
    pub value: String,
    /// 行
    pub row: usize,
    /// 列
    pub col: usize,
    /// 样式
    pub style: Style,
    /// 跨行
    pub rowspan: usize,
    /// 跨列
    pub colspan: usize,

    /// 图像
    pub is_image: bool,
    pub image: String,
    pub image_from_col: u32,
    pub image_from_col_off: usize,
    pub image_from_row: u32,
    pub image_from_row_off: usize,
    pub image_to_col: u32,
    pub image_to_col_off: usize,
    pub image_to_row: u32,
    pub image_to_row_off: usize,
    pub image_n: String,
    pub image_h: f64,
    pub image_w: f64,

    pub image_h_w: usize,
    pub image_w_w: usize,

    pub image_x: f64,
    pub image_y: f64,

    pub image_x_w: usize,
    pub image_y_w: usize,
    /// 状态
    pub state: u8,
}

impl Cell {
    pub fn default() -> Self {
        Self {
            value: "".to_string(),
            image: "".to_string(),
            image_from_col: 0,
            image_from_col_off: 0,
            image_from_row: 0,
            image_from_row_off: 0,
            image_to_col: 0,
            image_to_col_off: 0,
            image_to_row: 0,
            image_to_row_off: 0,
            image_n: "".to_string(),
            image_h: 0.0,
            image_w: 0.0,

            image_h_w: 0,
            image_w_w: 0,
            image_x: 0.0,
            image_y: 0.0,
            image_x_w: 0,
            row: 0,
            col: 0,
            style: Style {
                height: 0.0,
                width: 0.0,
                font_size: 16.0,
                background_color: "".to_string(),
                text_align: "".to_string(),
                vertical_align: "".to_string(),
                text_wrap: "".to_string(),
                font_family: "".to_string(),
                font_weight: "".to_string(),
                font_style: "".to_string(),
                text_decoration: "".to_string(),
                font_strike: "".to_string(),
                color: "".to_string(),
                border_top: "".to_string(),
                border_right: "".to_string(),
                border_bottom: "".to_string(),
                border_left: "".to_string(),
            },
            rowspan: 1,
            colspan: 1,
            state: 1,
            image_y_w: 0,
            is_image: false,
        }
    }
    pub fn json(self) -> JsonValue {
        if self.is_image {
            object! {
            value:self.value,
            row:self.row,
            col:self.col,
            rowspan:self.rowspan,
            colspan:self.colspan,
            style:self.style.json(),

            is_image: self.is_image,
            image: self.image.to_string(),
            image_n: self.image_n,
            image_from_col: self.image_from_col,
            image_from_col_off: self.image_from_col_off,
            image_from_row: self.image_from_row,
            image_from_row_off: self.image_from_row_off,

            image_to_col: self.image_to_col,
            image_to_col_off: self.image_to_col_off,
            image_to_row: self.image_to_row,
            image_to_row_off: self.image_to_row_off,
            image_h:  self.image_h/32743.37,
            image_w:  self.image_w/70823.0,

            image_h_w:  self.image_h_w,
            image_w_w:  self.image_w_w,

            image_x_w:  self.image_x_w,
            image_y_w:  self.image_y_w,

            image_x:  self.image_x/70823.0,
            image_y:  self.image_y/32743.37,
            state:  self.state,
        }
        } else {
            object! {
            value:self.value,
            row:self.row,
            col:self.col,
            rowspan:self.rowspan,
            colspan:self.colspan,
            style:self.style.json(),

            is_image: self.is_image,
            state:  self.state,
        }
        }
    }
}

#[derive(Debug, Clone)]
pub struct Style {
    pub height: f64,
    pub width: f64,
    pub font_size: f64,
    pub background_color: String,
    pub text_align: String,
    /// 垂直文本对齐
    pub vertical_align: String,

    pub text_wrap: String,
    /// 字体
    pub font_family: String,
    /// 文本的粗细
    font_weight: String,
    /// 字体样式
    font_style: String,
    /// 文本下方添加下划线
    text_decoration: String,
    /// 字体的删除线样式
    pub font_strike: String,
    pub color: String,

    border_top: String,
    border_right: String,
    border_bottom: String,
    border_left: String,
}

impl Style {
    fn json(self) -> JsonValue {
        let res = object! {
            height:format!("{}",self.height*0.3612),
            width:format!("{}",self.width*2.54),
            "font-family":self.font_family,
            "font-weight":self.font_weight,
            "font-size":format!("{}",self.font_size),
            "text-decoration":self.text_decoration,
            "font-strike":self.font_strike,
            color:self.color,
            "vertical-align":self.vertical_align,
            "text-wrap":self.text_wrap,
            "text-align":self.text_align,
            "border-top":self.border_top,
            "border-right":self.border_right,
            "border-bottom":self.border_bottom,
            "border-left":self.border_left
        };
        let mut list = res.clone();
        for (key, value) in res.entries() {
            if value.is_empty() {
                list.remove(key);
            }
        }
        list
    }
}