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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "orcus/spreadsheet/styles.hpp"
#include "orcus/string_pool.hpp"
#include "ostream_utils.hpp"
#include <functional>
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <vector>
#include <map>
namespace orcus { namespace spreadsheet {
font_t::font_t() = default;
font_t::font_t(const font_t& other) = default;
font_t::~font_t() = default;
font_t& font_t::operator=(const font_t& other) = default;
bool font_t::operator==(const font_t& other) const
{
if (name != other.name)
return false;
if (name_asian != other.name_asian)
return false;
if (name_complex != other.name_complex)
return false;
if (size != other.size)
return false;
if (size_asian != other.size_asian)
return false;
if (size_complex != other.size_complex)
return false;
if (bold != other.bold)
return false;
if (bold_asian != other.bold_asian)
return false;
if (bold_complex != other.bold_complex)
return false;
if (italic != other.italic)
return false;
if (italic_asian != other.italic_asian)
return false;
if (italic_complex != other.italic_complex)
return false;
if (underline_style != other.underline_style)
return false;
if (underline_width != other.underline_width)
return false;
if (underline_mode != other.underline_mode)
return false;
if (underline_type != other.underline_type)
return false;
if (underline_color != other.underline_color)
return false;
if (color != other.color)
return false;
if (strikethrough_style != other.strikethrough_style)
return false;
if (strikethrough_width != other.strikethrough_width)
return false;
if (strikethrough_type != other.strikethrough_type)
return false;
if (strikethrough_text != other.strikethrough_text)
return false;
return true;
}
bool font_t::operator!=(const font_t& other) const
{
return !operator==(other);
}
void font_t::reset()
{
*this = font_t();
}
std::size_t font_t::hash::operator()(const font_t& v) const
{
std::size_t hash_value = 0u;
if (v.name)
hash_value |= std::hash<std::string_view>{}(*v.name);
if (v.size)
hash_value |= std::hash<double>{}(*v.size);
if (v.bold)
hash_value |= std::hash<bool>{}(*v.bold);
if (v.italic)
hash_value |= std::hash<bool>{}(*v.italic);
return hash_value;
}
fill_t::fill_t() = default;
void fill_t::reset()
{
*this = fill_t();
}
border_attrs_t::border_attrs_t() = default;
void border_attrs_t::reset()
{
*this = border_attrs_t();
}
border_t::border_t() = default;
void border_t::reset()
{
*this = border_t();
}
protection_t::protection_t() = default;
void protection_t::reset()
{
*this = protection_t();
}
number_format_t::number_format_t() = default;
void number_format_t::reset()
{
*this = number_format_t();
}
bool number_format_t::operator== (const number_format_t& other) const noexcept
{
return identifier == other.identifier && format_string == other.format_string;
}
bool number_format_t::operator!= (const number_format_t& other) const noexcept
{
return !operator== (other);
}
cell_format_t::cell_format_t() :
font(0),
fill(0),
border(0),
protection(0),
number_format(0),
style_xf(0),
hor_align(hor_alignment_t::unknown),
ver_align(ver_alignment_t::unknown),
apply_num_format(false),
apply_font(false),
apply_fill(false),
apply_border(false),
apply_alignment(false),
apply_protection(false)
{
}
void cell_format_t::reset()
{
*this = cell_format_t();
}
cell_style_t::cell_style_t() :
xf(0), builtin(0)
{
}
void cell_style_t::reset()
{
*this = cell_style_t();
}
std::ostream& operator<< (std::ostream& os, const color_t& c)
{
::orcus::detail::ostream_format_guard ifs(os);
os << std::uppercase;
os << "(ARGB:"
<< ' ' << std::hex << std::setfill('0') << std::setw(2) << int(c.alpha & 0xFF)
<< ' ' << std::hex << std::setfill('0') << std::setw(2) << int(c.red & 0xFF)
<< ' ' << std::hex << std::setfill('0') << std::setw(2) << int(c.green & 0xFF)
<< ' ' << std::hex << std::setfill('0') << std::setw(2) << int(c.blue & 0xFF)
<< ")";
return os;
}
struct styles::impl
{
std::vector<font_t> fonts;
std::vector<fill_t> fills;
std::vector<border_t> borders;
std::vector<protection_t> protections;
std::vector<number_format_t> number_formats;
std::vector<cell_format_t> cell_style_formats;
std::vector<cell_format_t> cell_formats;
std::vector<cell_format_t> dxf_formats;
std::vector<cell_style_t> cell_styles;
std::map<std::size_t, std::size_t> cell_styles_map; // style xf to style position in `cell_styles`
string_pool str_pool;
};
styles::styles() : mp_impl(std::make_unique<impl>()) {}
styles::~styles() {}
void styles::reserve_font_store(size_t n)
{
mp_impl->fonts.reserve(n);
}
std::size_t styles::append_font(const font_t& font)
{
mp_impl->fonts.emplace_back(font);
return mp_impl->fonts.size() - 1;
}
void styles::reserve_fill_store(size_t n)
{
mp_impl->fills.reserve(n);
}
std::size_t styles::append_fill(const fill_t& fill)
{
mp_impl->fills.emplace_back(fill);
return mp_impl->fills.size() - 1;
}
void styles::reserve_border_store(size_t n)
{
mp_impl->borders.reserve(n);
}
std::size_t styles::append_border(const border_t& border)
{
mp_impl->borders.emplace_back(border);
return mp_impl->borders.size() - 1;
}
std::size_t styles::append_protection(const protection_t& protection)
{
mp_impl->protections.emplace_back(protection);
return mp_impl->protections.size() - 1;
}
void styles::reserve_number_format_store(size_t n)
{
mp_impl->number_formats.reserve(n);
}
std::size_t styles::append_number_format(const number_format_t& nf)
{
if (nf.format_string)
{
number_format_t copied = nf;
copied.format_string = mp_impl->str_pool.intern(*nf.format_string).first;
mp_impl->number_formats.emplace_back(copied);
}
else
mp_impl->number_formats.emplace_back(nf);
return mp_impl->number_formats.size() - 1;
}
void styles::reserve_cell_style_format_store(size_t n)
{
mp_impl->cell_style_formats.reserve(n);
}
size_t styles::append_cell_style_format(const cell_format_t& cf)
{
mp_impl->cell_style_formats.push_back(cf);
return mp_impl->cell_style_formats.size() - 1;
}
void styles::reserve_cell_format_store(size_t n)
{
mp_impl->cell_formats.reserve(n);
}
size_t styles::append_cell_format(const cell_format_t& cf)
{
mp_impl->cell_formats.push_back(cf);
return mp_impl->cell_formats.size() - 1;
}
void styles::reserve_diff_cell_format_store(size_t n)
{
mp_impl->dxf_formats.reserve(n);
}
size_t styles::append_diff_cell_format(const cell_format_t& cf)
{
mp_impl->dxf_formats.push_back(cf);
return mp_impl->dxf_formats.size() - 1;
}
void styles::reserve_cell_style_store(size_t n)
{
mp_impl->cell_styles.reserve(n);
}
void styles::append_cell_style(const cell_style_t& cs)
{
mp_impl->cell_styles.push_back(cs);
}
const font_t* styles::get_font(size_t index) const
{
if (index >= mp_impl->fonts.size())
return nullptr;
return &mp_impl->fonts[index];
}
const cell_format_t* styles::get_cell_format(size_t index) const
{
if (index >= mp_impl->cell_formats.size())
return nullptr;
return &mp_impl->cell_formats[index];
}
const fill_t* styles::get_fill(size_t index) const
{
if (index >= mp_impl->fills.size())
return nullptr;
return &mp_impl->fills[index];
}
const border_t* styles::get_border(size_t index) const
{
if (index >= mp_impl->borders.size())
return nullptr;
return &mp_impl->borders[index];
}
const protection_t* styles::get_protection(size_t index) const
{
if (index >= mp_impl->protections.size())
return nullptr;
return &mp_impl->protections[index];
}
const number_format_t* styles::get_number_format(size_t index) const
{
if (index >= mp_impl->number_formats.size())
return nullptr;
return &mp_impl->number_formats[index];
}
const cell_format_t* styles::get_cell_style_format(size_t index) const
{
if (index >= mp_impl->cell_style_formats.size())
return nullptr;
return &mp_impl->cell_style_formats[index];
}
const cell_format_t* styles::get_dxf_format(size_t index) const
{
if (index >= mp_impl->dxf_formats.size())
return nullptr;
return &mp_impl->dxf_formats[index];
}
const cell_style_t* styles::get_cell_style(size_t index) const
{
if (index >= mp_impl->cell_styles.size())
return nullptr;
return &mp_impl->cell_styles[index];
}
const cell_style_t* styles::get_cell_style_by_xf(size_t xfid) const
{
auto it = mp_impl->cell_styles_map.find(xfid);
if (it == mp_impl->cell_styles_map.end())
return nullptr;
auto index = it->second;
return &mp_impl->cell_styles[index];
}
size_t styles::get_font_count() const
{
return mp_impl->fonts.size();
}
size_t styles::get_fill_count() const
{
return mp_impl->fills.size();
}
size_t styles::get_border_count() const
{
return mp_impl->borders.size();
}
size_t styles::get_protection_count() const
{
return mp_impl->protections.size();
}
size_t styles::get_number_format_count() const
{
return mp_impl->number_formats.size();
}
size_t styles::get_cell_formats_count() const
{
return mp_impl->cell_formats.size();
}
size_t styles::get_cell_style_formats_count() const
{
return mp_impl->cell_style_formats.size();
}
size_t styles::get_dxf_count() const
{
return mp_impl->dxf_formats.size();
}
size_t styles::get_cell_styles_count() const
{
return mp_impl->cell_styles.size();
}
void styles::clear()
{
mp_impl = std::make_unique<impl>();
}
void styles::finalize_import()
{
for (std::size_t i = 0; i < mp_impl->cell_styles.size(); ++i)
{
const auto& entry = mp_impl->cell_styles[i];
mp_impl->cell_styles_map.insert_or_assign(entry.xf, i);
}
}
}}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|