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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
/* -*- 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 "html_dumper.hpp"
#include "impl_types.hpp"
#include "number_format.hpp"
#include "orcus/spreadsheet/styles.hpp"
#include "orcus/spreadsheet/shared_strings.hpp"
#include "orcus/spreadsheet/document.hpp"
#include "orcus/spreadsheet/sheet.hpp"
#include <ixion/address.hpp>
#include <ixion/model_context.hpp>
#include <ixion/formula.hpp>
#include <ixion/formula_result.hpp>
#include <ixion/cell.hpp>
#include <sstream>
namespace orcus { namespace spreadsheet { namespace detail {
namespace {
void build_rgb_color(std::ostringstream& os, const color_t& color_value)
{
// Special colors.
if (color_value.alpha == 255 && color_value.red == 0 && color_value.green == 0 && color_value.blue == 0)
{
os << "black";
return;
}
if (color_value.alpha == 255 && color_value.red == 255 && color_value.green == 0 && color_value.blue == 0)
{
os << "red";
return;
}
if (color_value.alpha == 255 && color_value.red == 0 && color_value.green == 255 && color_value.blue == 0)
{
os << "green";
return;
}
if (color_value.alpha == 255 && color_value.red == 0 && color_value.green == 0 && color_value.blue == 255)
{
os << "blue";
return;
}
os << "rgb("
<< static_cast<short>(color_value.red) << ","
<< static_cast<short>(color_value.green) << ","
<< static_cast<short>(color_value.blue) << ")";
}
const char* css_style_global =
"table, td { "
"border-collapse : collapse; "
"}\n"
"table { "
"border-spacing : 0px; "
"}\n"
"td { "
"width : 1in; border: 1px solid lightgray; "
"}\n"
"td.empty { "
"color : white; "
"}\n";
class html_elem
{
public:
struct attr
{
std::string name;
std::string value;
attr(const std::string& _name, const std::string& _value) : name(_name), value(_value) {}
};
typedef std::vector<attr> attrs_type;
html_elem(std::ostream& strm, const char* name, const char* style = nullptr, const char* style_class = nullptr) :
m_strm(strm), m_name(name)
{
m_strm << '<' << m_name;
if (style)
m_strm << " style=\"" << style << "\"";
if (style_class)
m_strm << " class=\"" << style_class << "\"";
m_strm << '>';
}
html_elem(std::ostream& strm, const char* name, const attrs_type& attrs) :
m_strm(strm), m_name(name)
{
m_strm << '<' << m_name;
attrs_type::const_iterator it = attrs.begin(), it_end = attrs.end();
for (; it != it_end; ++it)
m_strm << " " << it->name << "=\"" << it->value << "\"";
m_strm << '>';
}
~html_elem()
{
m_strm << "</" << m_name << '>';
}
private:
std::ostream& m_strm;
const char* m_name;
};
void print_formatted_text(std::ostream& strm, const std::string& text, const format_runs_t& formats)
{
typedef html_elem elem;
const char* p_span = "span";
size_t pos = 0;
format_runs_t::const_iterator itr = formats.begin(), itr_end = formats.end();
for (; itr != itr_end; ++itr)
{
const format_run& run = *itr;
if (pos < run.pos)
{
// flush unformatted text.
strm << std::string(&text[pos], run.pos-pos);
pos = run.pos;
}
if (!run.size)
continue;
std::string style = "";
if (run.bold)
style += "font-weight: bold;";
else
style += "font-weight: normal;";
if (run.italic)
style += "font-style: italic;";
else
style += "font-style: normal;";
if (!run.font.empty())
{
style += "font-family: ";
style += run.font;
style += ";";
}
if (run.font_size)
{
std::ostringstream os;
os << "font-size: " << run.font_size << "pt;";
style += os.str();
}
const color_t& col = run.color;
if (col.red || col.green || col.blue)
{
std::ostringstream os;
os << "color: ";
build_rgb_color(os, col);
os << ";";
style += os.str();
}
if (style.empty())
strm << std::string(&text[pos], run.size);
else
{
elem span(strm, p_span, style.c_str());
strm << std::string(&text[pos], run.size);
}
pos += run.size;
}
if (pos < text.size())
{
// flush the remaining unformatted text.
strm << std::string(&text[pos], text.size() - pos);
}
}
void build_border_style(std::ostringstream& os, const char* style_name, const border_attrs_t& attrs)
{
if (!attrs.style || *attrs.style == border_style_t::none)
return;
os << style_name << ": ";
switch (*attrs.style)
{
case border_style_t::thin:
{
os << "solid 1px ";
break;
}
case border_style_t::medium:
{
os << "solid 2px ";
break;
}
case border_style_t::thick:
{
os << "solid 3px ";
break;
}
case border_style_t::hair:
{
os << "solid 0.5px ";
break;
}
case border_style_t::dotted:
{
os << "dotted 1px ";
break;
}
case border_style_t::dashed:
{
os << "dashed 1px ";
break;
}
case border_style_t::double_border:
{
os << "3px double ";
break;
}
case border_style_t::dash_dot:
{
// CSS doesn't support dash-dot.
os << "dashed 1px ";
break;
}
case border_style_t::dash_dot_dot:
{
// CSS doesn't support dash-dot-dot.
os << "dashed 1px ";
break;
}
case border_style_t::medium_dashed:
{
os << "dashed 2px ";
break;
}
case border_style_t::medium_dash_dot:
{
// CSS doesn't support dash-dot.
os << "dashed 2px ";
break;
}
case border_style_t::medium_dash_dot_dot:
{
// CSS doesn't support dash-dot-dot.
os << "dashed 2px ";
break;
}
case border_style_t::slant_dash_dot:
{
// CSS doesn't support dash-dot.
os << "dashed 2px ";
break;
}
default:;
}
build_rgb_color(os, *attrs.border_color);
os << "; ";
}
void build_style_string(std::string& str, const styles& styles, const cell_format_t& fmt)
{
std::ostringstream os;
{
const font_t* p = styles.get_font(fmt.font);
if (p)
{
if (p->name && !p->name.value().empty())
os << "font-family: " << *p->name << ";";
if (p->size)
os << "font-size: " << *p->size << "pt;";
if (p->bold && *p->bold)
os << "font-weight: bold;";
if (p->italic && *p->italic)
os << "font-style: italic;";
if (p->color)
{
const color_t& r = *p->color;
if (r.red || r.green || r.blue)
{
os << "color: ";
build_rgb_color(os, r);
os << ";";
}
}
}
}
{
const fill_t* p = styles.get_fill(fmt.fill);
if (p)
{
if (p->pattern_type && *p->pattern_type == fill_pattern_t::solid && p->fg_color)
{
const color_t& r = *p->fg_color;
os << "background-color: ";
build_rgb_color(os, r);
os << ";";
}
}
}
{
const border_t* p = styles.get_border(fmt.border);
if (p)
{
build_border_style(os, "border-top", p->top);
build_border_style(os, "border-bottom", p->bottom);
build_border_style(os, "border-left", p->left);
build_border_style(os, "border-right", p->right);
}
}
if (fmt.apply_alignment)
{
if (fmt.hor_align != hor_alignment_t::unknown)
{
os << "text-align: ";
switch (fmt.hor_align)
{
case hor_alignment_t::left:
os << "left";
break;
case hor_alignment_t::center:
os << "center";
break;
case hor_alignment_t::right:
os << "right";
break;
default:
;
}
os << ";";
}
if (fmt.ver_align != ver_alignment_t::unknown)
{
os << "vertical-align: ";
switch (fmt.ver_align)
{
case ver_alignment_t::top:
os << "top";
break;
case ver_alignment_t::middle:
os << "middle";
break;
case ver_alignment_t::bottom:
os << "bottom";
break;
default:
;
}
os << ";";
}
}
str += os.str();
}
void dump_html_head(std::ostream& os)
{
typedef html_elem elem;
const char* p_head = "head";
const char* p_style = "style";
elem elem_head(os, p_head);
{
elem elem_style(os, p_style);
os << css_style_global;
}
}
void build_html_elem_attributes(html_elem::attrs_type& attrs, const std::string& style, const merge_size* p_merge_size)
{
attrs.push_back(html_elem::attr("style", style));
if (p_merge_size)
{
if (p_merge_size->width > 1)
{
std::ostringstream os2;
os2 << p_merge_size->width;
attrs.push_back(html_elem::attr("colspan", os2.str()));
}
if (p_merge_size->height > 1)
{
std::ostringstream os2;
os2 << p_merge_size->height;
attrs.push_back(html_elem::attr("rowspan", os2.str()));
}
}
}
}
html_dumper::html_dumper(
const document& doc,
const col_merge_size_type& merge_ranges,
sheet_t sheet_id) :
m_doc(doc),
m_merge_ranges(merge_ranges),
m_sheet_id(sheet_id)
{
build_overlapped_ranges();
}
void html_dumper::dump(std::ostream& os) const
{
const sheet* sh = m_doc.get_sheet(m_sheet_id);
if (!sh)
return;
typedef html_elem elem;
const char* p_html = "html";
const char* p_body = "body";
const char* p_table = "table";
const char* p_tr = "tr";
const char* p_td = "td";
ixion::abs_range_t range = sh->get_data_range();
elem root(os, p_html);
dump_html_head(os);
{
elem elem_body(os, p_body);
if (!range.valid())
// Sheet is empty. Nothing to print.
return;
const ixion::model_context& cxt = m_doc.get_model_context();
const ixion::formula_name_resolver* resolver =
m_doc.get_formula_name_resolver(spreadsheet::formula_ref_context_t::global);
const shared_strings& sstrings = m_doc.get_shared_strings();
elem table(os, p_table);
row_t row_count = range.last.row + 1;
col_t col_count = range.last.column + 1;
for (row_t row = 0; row < row_count; ++row)
{
// Set the row height.
std::string row_style;
row_height_t rh = sh->get_row_height(row, nullptr, nullptr);
// Convert height from twip to inches.
if (rh != get_default_row_height())
{
std::string style;
double val = orcus::convert(rh, length_unit_t::twip, length_unit_t::inch);
std::ostringstream os_style;
os_style << "height: " << val << "in;";
row_style += os_style.str();
}
const char* style_str = nullptr;
if (!row_style.empty())
style_str = row_style.c_str();
elem tr(os, p_tr, style_str);
const detail::overlapped_col_index_type* p_overlapped = get_overlapped_ranges(row);
for (col_t col = 0; col < col_count; ++col)
{
ixion::abs_address_t pos(m_sheet_id, row, col);
const merge_size* p_merge_size = get_merge_size(row, col);
if (!p_merge_size && p_overlapped)
{
// Check if this cell is overlapped by a merged cell.
col_t overlapped_origin = -1;
col_t last_col = -1;
if (p_overlapped->search_tree(col, overlapped_origin, nullptr, &last_col).second && overlapped_origin >= 0)
{
// Skip all overlapped cells on this row.
col = last_col - 1;
continue;
}
}
size_t xf_id = sh->get_cell_format(row, col);
std::string style;
if (row == 0)
{
// Set the column width.
col_width_t cw = sh->get_col_width(col, nullptr, nullptr);
// Convert width from twip to inches.
if (cw != get_default_column_width())
{
double val = orcus::convert(cw, length_unit_t::twip, length_unit_t::inch);
std::ostringstream os_style;
os_style << "width: " << val << "in;";
style += os_style.str();
}
}
{
// Apply cell format.
const styles& styles = m_doc.get_styles();
const cell_format_t* fmt = styles.get_cell_format(xf_id);
if (fmt)
build_style_string(style, styles, *fmt);
}
ixion::celltype_t ct = cxt.get_celltype(pos);
if (ct == ixion::celltype_t::empty)
{
html_elem::attrs_type attrs;
build_html_elem_attributes(attrs, style, p_merge_size);
attrs.push_back(html_elem::attr("class", "empty"));
elem td(os, p_td, attrs);
os << '-'; // empty cell.
continue;
}
html_elem::attrs_type attrs;
build_html_elem_attributes(attrs, style, p_merge_size);
elem td(os, p_td, attrs);
switch (ct)
{
case ixion::celltype_t::string:
{
size_t sindex = cxt.get_string_identifier(pos);
const std::string* p = cxt.get_string(sindex);
assert(p);
const format_runs_t* pformat = sstrings.get_format_runs(sindex);
if (pformat)
print_formatted_text(os, *p, *pformat);
else
os << *p;
break;
}
case ixion::celltype_t::numeric:
format_to_file_output(os, cxt.get_numeric_value(pos));
break;
case ixion::celltype_t::boolean:
os << (cxt.get_boolean_value(pos) ? "true" : "false");
break;
case ixion::celltype_t::formula:
{
// print the formula and the formula result.
const ixion::formula_cell* cell = cxt.get_formula_cell(pos);
assert(cell);
const ixion::formula_tokens_store_ptr_t& ts = cell->get_tokens();
if (ts)
{
const ixion::formula_tokens_t& tokens = ts->get();
std::string formula;
if (resolver)
{
pos = cell->get_parent_position(pos);
formula = ixion::print_formula_tokens(
m_doc.get_model_context(), pos, *resolver, tokens);
}
else
formula = "???";
ixion::formula_group_t fg = cell->get_group_properties();
if (fg.grouped)
os << '{' << formula << '}';
else
os << formula;
try
{
ixion::formula_result res = cell->get_result_cache(
ixion::formula_result_wait_policy_t::throw_exception);
os << " (" << res.str(m_doc.get_model_context()) << ")";
}
catch (const std::exception&)
{
os << " (#RES!)";
}
}
break;
}
default:
;
}
}
}
}
}
const overlapped_col_index_type* html_dumper::get_overlapped_ranges(row_t row) const
{
overlapped_cells_type::const_iterator it = m_overlapped_ranges.find(row);
if (it == m_overlapped_ranges.end())
return nullptr;
return it->second.get();
}
const merge_size* html_dumper::get_merge_size(row_t row, col_t col) const
{
col_merge_size_type::const_iterator it_col = m_merge_ranges.find(col);
if (it_col == m_merge_ranges.end())
return nullptr;
merge_size_type& col_merge_sizes = *it_col->second;
merge_size_type::const_iterator it_row = col_merge_sizes.find(row);
if (it_row == col_merge_sizes.end())
return nullptr;
return &it_row->second;
}
void html_dumper::build_overlapped_ranges()
{
const sheet* sh = m_doc.get_sheet(m_sheet_id);
if (!sh)
return;
range_size_t sheet_size = m_doc.get_sheet_size();
detail::col_merge_size_type::const_iterator it_col = m_merge_ranges.begin(), it_col_end = m_merge_ranges.end();
for (; it_col != it_col_end; ++it_col)
{
col_t col = it_col->first;
const detail::merge_size_type& data = *it_col->second;
detail::merge_size_type::const_iterator it = data.begin(), it_end = data.end();
for (; it != it_end; ++it)
{
row_t row = it->first;
const detail::merge_size& item = it->second;
for (row_t i = 0; i < item.height; ++i, ++row)
{
// Get the container for this row.
detail::overlapped_cells_type::iterator it_cont = m_overlapped_ranges.find(row);
if (it_cont == m_overlapped_ranges.end())
{
auto p = std::make_unique<detail::overlapped_col_index_type>(0, sheet_size.columns, -1);
std::pair<detail::overlapped_cells_type::iterator, bool> r =
m_overlapped_ranges.insert(detail::overlapped_cells_type::value_type(row, std::move(p)));
if (!r.second)
{
// Insertion failed.
return;
}
it_cont = r.first;
}
detail::overlapped_col_index_type& cont = *it_cont->second;
cont.insert_back(col, col+item.width, col);
}
}
}
// Build trees.
for (auto& range : m_overlapped_ranges)
range.second->build_tree();
}
}}}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|