-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwvware_wrapper.cpp
More file actions
982 lines (852 loc) · 26.9 KB
/
Copy pathwvware_wrapper.cpp
File metadata and controls
982 lines (852 loc) · 26.9 KB
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
#include <odr/internal/html/wvware_wrapper.hpp>
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/internal/html/html_service.hpp>
#include <odr/internal/html/html_writer.hpp>
#include <odr/internal/html/image_file.hpp>
#include <odr/internal/oldms_wvware/wvware_oldms_file.hpp>
#include <odr/internal/util/file_util.hpp>
#include <wv/wv.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <stdexcept>
namespace odr::internal::html {
/// A lot of this code is duplicated from wvWare, mostly from `wvWare.c` and
/// `wvHtml.c`.
///
/// wvWare is writing to stdout, while we want to write to a file. Also, wvWare
/// is configurable to write not only HTML but also other formats. We only need
/// HTML.
///
/// We decided to duplicate the code instead of changing upstream wvWare code
/// because it is rather an application not a library, it is quite outdated and
/// not actively developed, and written in C. Duplication allows for a clean
/// separation between wvWare and our code while also being able to write modern
/// C++ code.
///
/// A copy of wvWare can be found here:
/// https://github.com/opendocument-app/wvWare
namespace {
/// Extension of `expand_data` see
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wv.h#L2776-L2814
/// to allow for more state variables.
struct TranslationState : expand_data {
explicit TranslationState(HtmlWriter _out, HtmlResources &_resources,
const HtmlConfig &_config, std::string _cache_path)
: expand_data{}, out(std::move(_out)), resources(&_resources),
config{&_config}, cache_path{std::move(_cache_path)} {}
char *charset = nullptr;
PAP *ppap = nullptr;
struct {
int message = 0;
} special_char_handler_state = {};
std::size_t figure_number = 0;
HtmlWriter out;
HtmlResources *resources;
const HtmlConfig *config;
std::string cache_path;
};
std::string figure_name(const wvParseStruct *ps) {
auto *data = static_cast<TranslationState *>(ps->userData);
const std::size_t number = data->figure_number++;
std::string name = "figure" + std::to_string(number);
return name;
}
std::string figure_cache_path(const wvParseStruct *ps,
const std::string &name) {
const auto *data = static_cast<TranslationState *>(ps->userData);
return data->cache_path + "/" + name;
}
/// Originally from `text.c` `wvConvertUnicodeToHtml`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/text.c#L1999-L2154
int convert_unicode_to_html(const wvParseStruct *ps,
const std::uint16_t char16) {
auto *data = static_cast<TranslationState *>(ps->userData);
auto &out = data->out;
switch (char16) {
case 11:
out.out() << "<br>";
return 1;
case 31: /* non-required hyphen */
out.out() << "­"; /*vladimir@lukianov.name HTML 4.01 spec*/
return 1;
case 30:
case 45:
case 0x2013:
out.out() << "-"; /* en-dash */
return 1;
case 12:
case 13:
case 14:
case 7:
return 1;
case 34:
out.out() << """;
return 1;
case 38:
out.out() << "&";
return 1;
case 60:
out.out() << "<";
return 1;
case 62:
out.out() << ">";
return 1;
/*
german characters, im assured that this is the right way to handle them
by Markus Schulte <markus@dom.de>
As the output encoding for HTML was chosen as UTF-8,
we don't need Ä etc. etc. I removed all but sz
-- MV 6.4.2000
*/
case 0xdf:
out.out() << "ß";
return 1;
/* end german characters */
case 0x2026:
out.out() << "…";
return 1;
case 0x2019:
out.out() << "'";
return 1;
case 0x2215:
out.out() << "/";
return 1;
case 0xF8E7: /* without this, things should work in theory, but not for me */
out.out() << "_";
return 1;
case 0x2018:
out.out() << "`";
return 1;
/* Windows specials (MV): */
case 0x0160:
out.out() << "Š";
return 1;
case 0x0161:
out.out() << "š";
return 1;
case 0x2014:
out.out() << "—";
return 1;
case 0x201c:
out.out() << "“"; /* inverted double quotation mark */
return 1;
case 0x201d:
out.out() << "”"; /* double q.m. */
return 1;
case 0x201e:
out.out() << "„"; /* below double q.m. */
return 1;
case 0x2020:
out.out() << "†";
return 1;
case 0x2021:
out.out() << "‡";
return 1;
case 0x2022:
out.out() << "•";
return 1;
case 0x0152:
out.out() << "Œ";
return 1;
case 0x0153:
out.out() << "œ";
return 1;
case 0x0178:
out.out() << "Ÿ";
return 1;
case 0x2030:
out.out() << "‰";
return 1;
case 0x20ac:
out.out() << "€";
return 1;
/* Mac specials (MV): */
case 0xf020:
out.out() << " ";
return 1;
case 0xf02c:
out.out() << ",";
return 1;
case 0xf028:
out.out() << "(";
return 1;
case 0xf03e:
out.out() << ">";
return 1;
case 0xf067:
out.out() << "γ";
return 1;
case 0xf064:
out.out() << "δ";
return 1;
case 0xf072:
out.out() << "ρ";
return 1;
case 0xf073:
out.out() << "σ";
return 1;
case 0xf0ae:
out.out() << "→"; /* right arrow */
return 1;
case 0xf0b6:
out.out() << "∂"; /* partial deriv. */
return 1;
case 0xf0b3:
out.out() << "≥";
return 1;
default:
break;
}
return 0;
}
/// Originally from `text.c` `wvOutputFromUnicode`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/text.c#L757-L840
void output_from_unicode(const wvParseStruct *ps, const std::uint16_t eachchar,
const char *outputtype) {
auto *data = static_cast<TranslationState *>(ps->userData);
HtmlWriter &out = data->out;
GIConv g_iconv_handle{};
int need_swapping{};
gchar *ibuf{}, *obuf{};
std::size_t ibuflen{}, obuflen{}, len{}, count{}, i{};
std::uint8_t buffer[2], buffer2[5];
if (convert_unicode_to_html(ps, eachchar) != 0) {
return;
}
{
g_iconv_handle = g_iconv_open(outputtype, "UCS-2");
if (g_iconv_handle == reinterpret_cast<GIConv>(-1)) {
std::cerr << "g_iconv_open fail: " << errno
<< ", cannot convert UCS-2 to " << outputtype << "\n";
out.out() << "?";
return;
}
/* Determining if unicode biteorder is swapped (glibc < 2.2) */
need_swapping = 1;
buffer[0] = 0x20;
buffer[1] = 0;
ibuf = reinterpret_cast<gchar *>(buffer);
obuf = reinterpret_cast<gchar *>(buffer2);
ibuflen = 2;
obuflen = 5;
count = g_iconv(g_iconv_handle, &ibuf, &ibuflen, &obuf, &obuflen);
if (count != static_cast<std::size_t>(-1)) {
need_swapping = buffer2[0] != 0x20;
}
}
if (need_swapping) {
buffer[0] = (eachchar >> 8) & 0x00ff;
buffer[1] = eachchar & 0x00ff;
} else {
buffer[0] = eachchar & 0x00ff;
buffer[1] = (eachchar >> 8) & 0x00ff;
}
ibuf = reinterpret_cast<gchar *>(buffer);
obuf = reinterpret_cast<gchar *>(buffer2);
ibuflen = 2;
len = obuflen = 5;
count = g_iconv(g_iconv_handle, &ibuf, &ibuflen, &obuf, &obuflen);
if (count == (std::size_t)-1) {
std::cerr << "iconv failed, errno: " << errno << ", char: 0x" << std::hex
<< eachchar << ", UCS-2 -> " << outputtype << "\n";
/* I'm torn here - do i just announce the failure, continue, or copy over to
* the other buffer? */
/* errno is usually 84 (illegal byte sequence)
should i reverse the bytes and try again? */
out.out() << ibuf[1];
} else {
len = len - obuflen;
for (i = 0; i < len; i++) {
out.out() << buffer2[i];
}
}
// TODO iconv could be cached
g_iconv_close(g_iconv_handle);
}
/// Originally from `wvWare.c` `wvStrangeNoGraphicData`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L661-L676
/// simplified to HTML output
void strange_no_graphic_data(wvParseStruct *ps, int graphicstype) {
auto *data = static_cast<TranslationState *>(ps->userData);
auto &out = data->out;
std::cerr << "Strange No Graphic Data in the 0x01/0x08 graphic\n";
// TODO
out.out() << R"(<img alt=")" << std::hex << graphicstype
<< R"( graphic" src="StrangeNoGraphicData"/><br/>)";
}
/// Originally from `wvWare.c` `wvPrintGraphics`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L1239-L1287
/// simplified to HTML output
void print_graphics(wvParseStruct *ps, int /*graphicstype*/, int width,
int height, const std::string &name) {
// upstream converts to PNG, we just use the original format as the browser
// should support them
auto *data = static_cast<TranslationState *>(ps->userData);
auto &out = data->out;
std::string path = figure_cache_path(ps, name);
File file(path);
odr::HtmlResource resource =
HtmlResource::create(HtmlResourceType::image, "image/jpg", name, name,
file, false, false, true);
HtmlResourceLocation resource_location =
data->config->resource_locator(resource, *data->config);
data->resources->emplace_back(std::move(resource), resource_location);
out.write_element_begin(
"img", HtmlElementOptions()
.set_close_type(HtmlCloseType::trailing)
.set_attributes([&](const HtmlAttributeWriterCallback &clb) {
clb("alt", "Error: image not found or unsupported");
if (resource_location.has_value()) {
clb("src", resource_location.value());
} else {
clb("src", [&](std::ostream &o) {
translate_image_src(file, o, *data->config);
});
}
})
.set_style("width:" + std::to_string(width) +
"px;height:" + std::to_string(height) + "px"));
out.write_element_begin(
"br", HtmlElementOptions().set_close_type(HtmlCloseType::trailing));
}
void dump_bitmap(wvParseStruct *ps, const std::string &name,
BitmapBlip *bitmap) {
std::string path = figure_cache_path(ps, name);
wvStream *pwv = bitmap->m_pvBits;
std::size_t size = wvStream_size(pwv);
wvStream_rewind(pwv);
std::ofstream bitmap_out(path, std::ios::binary);
if (bitmap_out.fail()) {
throw std::runtime_error("Cannot open " + path + " file for writing");
}
for (std::size_t i = 0; i < size; i++) {
std::uint8_t byte = read_8ubit(pwv);
bitmap_out.put(static_cast<char>(byte));
}
}
int dump_metafile(wvParseStruct *ps, const std::string &name,
MetaFileBlip *bitmap) {
std::string path = figure_cache_path(ps, name);
FILE *fd = fopen(path.c_str(), "wb");
if (fd == nullptr) {
throw std::runtime_error("Cannot open " + path + " file for writing");
}
wvStream *pwv = bitmap->m_pvBits;
std::size_t size = wvStream_size(pwv);
wvStream_rewind(pwv);
std::uint8_t decompressf = 0;
if (bitmap->m_fCompression == msocompressionDeflate) {
decompressf = setdecom();
}
if (decompressf == 0) {
for (std::size_t i = 0; i < size; i++) {
fputc(read_8ubit(pwv), fd);
}
} else {
FILE *tmp = tmpfile();
FILE *out = tmpfile();
for (std::size_t i = 0; i < size; i++) {
fputc(read_8ubit(pwv), tmp);
}
rewind(tmp);
decompress(tmp, out, bitmap->m_cbSave, bitmap->m_cb);
fclose(tmp);
rewind(out);
for (std::size_t i = 0; i < bitmap->m_cb; i++) {
fputc(fgetc(out), fd);
}
fclose(out);
}
fclose(fd);
return 0;
}
std::string html_graphic(wvParseStruct *ps, Blip *blip) {
std::string name = figure_name(ps);
/*
temp hack to test older included bmps in word 6 and 7,
should be wrapped in a modern escher strucure before getting
to here, and then handled as normal
*/
switch (blip->type) {
case msoblipJPEG:
case msoblipDIB:
case msoblipPNG: {
wvStream *fd = (blip->blip.bitmap.m_pvBits);
char test[3];
test[0] = static_cast<char>(read_8ubit(fd));
test[1] = static_cast<char>(read_8ubit(fd));
test[2] = '\0';
wvStream_rewind(fd);
if (strcmp(test, "BM") == 0) {
name += ".bmp";
dump_bitmap(ps, name, &blip->blip.bitmap);
return name;
}
}
default:
break;
}
switch (blip->type) {
case msoblipWMF:
name += ".wmf";
dump_metafile(ps, name, &blip->blip.metafile);
break;
case msoblipEMF:
name += ".emf";
dump_metafile(ps, name, &blip->blip.metafile);
break;
case msoblipPICT:
name += ".pict";
dump_metafile(ps, name, &blip->blip.metafile);
break;
case msoblipJPEG:
name += ".jpg";
dump_bitmap(ps, name, &blip->blip.bitmap);
break;
case msoblipDIB:
name += ".dib";
dump_bitmap(ps, name, &blip->blip.bitmap);
break;
case msoblipPNG:
name += ".png";
dump_bitmap(ps, name, &blip->blip.bitmap);
break;
default:
break;
}
return name;
}
/// Originally from `wvWare.c` `myelehandler`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L503-L599
int element_handler(wvParseStruct *ps, wvTag tag, void *props, int /*dirty*/) {
auto *data = static_cast<TranslationState *>(ps->userData);
data->anSttbfAssoc = &ps->anSttbfAssoc;
data->lfo = &ps->lfo;
data->lfolvl = ps->lfolvl;
data->lvl = ps->lvl;
data->nolfo = &ps->nolfo;
data->nooflvl = &ps->nooflvl;
data->stsh = &ps->stsh;
data->lst = &ps->lst;
data->noofLST = &ps->noofLST;
data->liststartnos = &ps->liststartnos;
data->listnfcs = &ps->listnfcs;
data->finallvl = &ps->finallvl;
data->fib = &ps->fib;
data->dop = &ps->dop;
data->intable = &ps->intable;
data->cellbounds = &ps->cellbounds;
data->nocellbounds = &ps->nocellbounds;
data->endcell = &ps->endcell;
data->vmerges = &ps->vmerges;
data->norows = &ps->norows;
data->nextpap = &ps->nextpap;
if (data->charset == nullptr) {
data->charset = wvAutoCharset(ps);
}
data->props = props;
switch (tag) {
case PARABEGIN: {
S16 tilfo = 0;
/* test begin */
if (*(data->endcell) != 0) {
tilfo = ((PAP *)(data->props))->ilfo;
((PAP *)(data->props))->ilfo = 0;
}
/* test end */
data->ppap = (PAP *)data->props;
wvBeginPara(data);
if (tilfo != 0) {
((PAP *)(data->props))->ilfo = tilfo;
}
} break;
case PARAEND: {
S16 tilfo = 0;
/* test begin */
if (*(data->endcell) != 0) {
tilfo = ((PAP *)(data->props))->ilfo;
((PAP *)(data->props))->ilfo = 0;
}
/* test end */
wvEndCharProp(data); /* danger will break in the future */
wvEndPara(data);
if (tilfo != 0) {
((PAP *)(data->props))->ilfo = tilfo;
}
wvCopyPAP(&data->lastpap, (PAP *)(data->props));
} break;
case CHARPROPBEGIN:
wvBeginCharProp(data, data->ppap);
break;
case CHARPROPEND:
wvEndCharProp(data);
break;
case SECTIONBEGIN:
wvBeginSection(data);
break;
case SECTIONEND:
wvEndSection(data);
break;
case COMMENTBEGIN:
wvBeginComment(data);
break;
case COMMENTEND:
wvEndComment(data);
break;
default:
break;
}
return 0;
}
/// Originally from `wvWare.c` `mydochandler`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L601-L659
int document_handler(wvParseStruct *ps, wvTag tag) {
auto *data = static_cast<TranslationState *>(ps->userData);
data->anSttbfAssoc = &ps->anSttbfAssoc;
data->lfo = &ps->lfo;
data->lfolvl = ps->lfolvl;
data->lvl = ps->lvl;
data->nolfo = &ps->nolfo;
data->nooflvl = &ps->nooflvl;
data->stsh = &ps->stsh;
data->lst = &ps->lst;
data->noofLST = &ps->noofLST;
data->liststartnos = &ps->liststartnos;
data->listnfcs = &ps->listnfcs;
data->finallvl = &ps->finallvl;
data->fib = &ps->fib;
data->dop = &ps->dop;
data->intable = &ps->intable;
data->cellbounds = &ps->cellbounds;
data->nocellbounds = &ps->nocellbounds;
data->endcell = &ps->endcell;
data->vmerges = &ps->vmerges;
data->norows = &ps->norows;
wvSetEntityConverter(data);
data->filename = ps->filename;
data->whichcell = 0;
data->whichrow = 0;
data->asep = nullptr;
wvInitPAP(&data->lastpap);
data->nextpap = nullptr;
data->ps = ps;
if (data->charset == nullptr) {
data->charset = wvAutoCharset(ps);
}
switch (tag) {
case DOCBEGIN:
wvBeginDocument(data);
break;
case DOCEND:
wvEndDocument(data);
break;
default:
break;
}
return 0;
}
/// Originally from `wvWare.c` `myCharProc`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L1556-L1605
int char_handler(wvParseStruct *ps, std::uint16_t eachchar,
const std::uint8_t chartype, const std::uint16_t lid) {
const auto *data = static_cast<TranslationState *>(ps->userData);
switch (eachchar) {
case 19:
ps->fieldstate++;
ps->fieldmiddle = 0;
fieldCharProc(ps, eachchar, chartype, lid); /* temp */
return 0;
case 20:
fieldCharProc(ps, eachchar, chartype, lid);
ps->fieldmiddle = 1;
return 0;
case 21:
ps->fieldmiddle = 0;
ps->fieldstate--;
fieldCharProc(ps, eachchar, chartype, lid); /* temp */
return 0;
case 0x08:
std::cerr << "hmm did we loose the fSpec flag ?, this is possibly a bug\n";
break;
default:
break;
}
if (ps->fieldstate != 0 && fieldCharProc(ps, eachchar, chartype, lid) != 0) {
return 0;
}
// from `wvOutputHtmlChar`
{
char *outputtype =
data->charset != nullptr ? data->charset : wvAutoCharset(ps);
if (chartype != 0) {
eachchar = wvHandleCodePage(eachchar, lid);
}
output_from_unicode(ps, eachchar, outputtype);
}
return 0;
}
/// Originally from `wvWare.c` `mySpecCharProc`
/// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L1289-L1553
int special_char_handler(wvParseStruct *ps, std::uint16_t eachchar, CHP *achp) {
auto *data = static_cast<TranslationState *>(ps->userData);
auto &state = data->special_char_handler_state;
auto &out = data->out;
PICF picf{};
FSPA *fspa = nullptr;
switch (eachchar) {
case 19:
// field began
ps->fieldstate++;
ps->fieldmiddle = 0;
fieldCharProc(ps, eachchar, 0, 0x400); /* temp */
return 0;
case 20:
if (achp->fOle2 != 0) {
std::cerr << "this field has an associated embedded object of id "
<< achp->fcPic_fcObj_lTagObj << "\n";
}
fieldCharProc(ps, eachchar, 0, 0x400); /* temp */
ps->fieldmiddle = 1;
return 0;
case 21:
ps->fieldstate--;
ps->fieldmiddle = 0;
fieldCharProc(ps, eachchar, 0, 0x400); /* temp */
return 0;
default:
break;
}
if (ps->fieldstate) {
if (fieldCharProc(ps, eachchar, 0, 0x400))
return 0;
}
switch (eachchar) {
case 0x05:
/* this should be handled by the COMMENTBEGIN and COMMENTEND events */
return 0;
case 0x01: {
wvStream *f;
Blip blip;
long p = wvStream_tell(ps->data);
if (achp->fOle2 != 0) {
return 0;
}
wvStream_goto(ps->data, achp->fcPic_fcObj_lTagObj);
wvGetPICF(wvQuerySupported(&ps->fib, nullptr), &picf, ps->data);
f = picf.rgb;
if (wv0x01(&blip, f, picf.lcb - picf.cbHeader) != 0) {
std::string name = html_graphic(ps, &blip);
print_graphics(ps, 0x01, static_cast<int>(wvTwipsToHPixels(picf.dxaGoal)),
static_cast<int>(wvTwipsToVPixels(picf.dyaGoal)), name);
} else {
strange_no_graphic_data(ps, 0x01);
}
wvStream_goto(ps->data, p);
return 0;
}
case 0x08: {
Blip blip;
if (wvQuerySupported(&ps->fib, nullptr) == WORD8) {
if (ps->nooffspa > 0) {
fspa =
wvGetFSPAFromCP(ps->currentcp, ps->fspa, ps->fspapos, ps->nooffspa);
if (fspa == nullptr) {
std::cerr << "No fspa! Insanity abounds!\n";
return 0;
}
data->props = fspa;
if (wv0x08(&blip, static_cast<int>(fspa->spid), ps) != 0) {
std::string name = html_graphic(ps, &blip);
print_graphics(ps, 0x08,
static_cast<int>(wvTwipsToHPixels(
static_cast<short>(fspa->xaRight - fspa->xaLeft))),
static_cast<int>(wvTwipsToVPixels(
static_cast<short>(fspa->yaBottom - fspa->yaTop))),
name);
} else {
strange_no_graphic_data(ps, 0x08);
}
} else {
std::cerr << "nooffspa was <=0! Ignoring.\n";
}
} else {
std::cerr << "pre word8 0x08 graphic, unsupported at the moment\n";
FDOA *fdoa =
wvGetFDOAFromCP(ps->currentcp, ps->fdoa, ps->fdoapos, ps->nooffdoa);
data->props = fdoa;
}
// Potentially relevant disabled code section in `wvWare.c`?
// https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L1443-L1459
return 0;
}
case 0x28: {
std::uint16_t symbol[6] = {'S', 'y', 'm', 'b', 'o', 'l'};
std::uint16_t wingdings[9] = {'W', 'i', 'n', 'g', 'd', 'i', 'n', 'g', 's'};
std::uint16_t mtextra[8] = {'M', 'T', ' ', 'E', 'x', 't', 'r', 'a'};
if (memcmp(symbol, ps->fonts.ffn[achp->ftcSym].xszFfn, 12) == 0) {
if (state.message == 0 && strcasecmp("UTF-8", data->charset) != 0) {
std::cerr << "Symbol font detected (too late sorry!), rerun wvHtml "
"with option --charset utf-8\noption to support correct "
"symbol font conversion to a viewable format.\n";
state.message++;
}
output_from_unicode(ps, wvConvertSymbolToUnicode(achp->xchSym - 61440),
data->charset);
return 0;
}
if (memcmp(mtextra, ps->fonts.ffn[achp->ftcSym].xszFfn, 16) == 0) {
if (state.message == 0 && strcasecmp("UTF-8", data->charset) != 0) {
std::cerr
<< "MT Extra font detected (too late sorry!), rerun wvHtml with option --charset utf-8\n\
option to support correct symbol font conversion to a viewable format.\n";
state.message++;
}
output_from_unicode(ps, wvConvertMTExtraToUnicode(achp->xchSym - 61440),
data->charset);
return 0;
}
if (memcmp(wingdings, ps->fonts.ffn[achp->ftcSym].xszFfn, 18) == 0) {
if (state.message == 0) {
std::cerr << "Wingdings font detected, i need a mapping table to "
"unicode for this\n";
state.message++;
}
} else {
if (state.message == 0) {
char *fontname = wvWideStrToMB(ps->fonts.ffn[achp->ftcSym].xszFfn);
std::cerr << "Special font " << fontname
<< ", I need a mapping table to unicode for this\n";
wvFree(fontname);
out.out() << "*";
state.message++;
}
return 0;
}
}
default:
break;
}
return 0;
}
class HtmlServiceImpl final : public HtmlService {
public:
HtmlServiceImpl(WvWareLegacyMicrosoftFile oldms_file, HtmlConfig config,
std::string cache_path, std::shared_ptr<Logger> logger)
: HtmlService(std::move(config), std::move(logger)),
m_oldms_file{std::move(oldms_file)},
m_cache_path{std::move(cache_path)} {
m_views.emplace_back(
std::make_shared<HtmlView>(*this, "document", 0, "document.html"));
}
[[nodiscard]] const HtmlViews &list_views() const override { return m_views; }
void warmup() const override {
std::lock_guard lock(m_mutex);
if (m_warm) {
return;
}
std::ofstream document_out(m_cache_path + "/document.html");
HtmlWriter out(document_out, config());
m_resources = write_document(out);
m_warm = true;
}
bool exists(const std::string &path) const override {
if (std::ranges::any_of(m_views, [&path](const auto &view) {
return view.path() == path;
})) {
return true;
}
warmup();
if (std::ranges::any_of(m_resources, [&path](const auto &pair) {
const auto &[resource, location] = pair;
return location.has_value() && location.value() == path;
})) {
return true;
}
return false;
}
std::string mimetype(const std::string &path) const override {
if (std::ranges::any_of(m_views, [&path](const auto &view) {
return view.path() == path;
})) {
return "text/html";
}
warmup();
for (const auto &[resource, location] : m_resources) {
if (location.has_value() && location.value() == path) {
return resource.mime_type();
}
}
throw FileNotFound("Unknown path: " + path);
}
void write(const std::string &path, std::ostream &out) const override {
for (const auto &view : m_views) {
if (view.path() == path) {
HtmlWriter writer(out, config());
write_html(path, writer);
return;
}
}
warmup();
for (const auto &[resource, location] : m_resources) {
if (location.has_value() && location.value() == path) {
resource.write_resource(out);
return;
}
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_html(const std::string &path,
HtmlWriter &out) const override {
warmup();
util::file::pipe(m_cache_path + "/" + path, out.out());
return m_resources;
}
HtmlResources write_document(HtmlWriter &out) const {
HtmlResources resources;
wvParseStruct &ps = m_oldms_file.parse_struct();
wvSetElementHandler(&ps, element_handler);
wvSetDocumentHandler(&ps, document_handler);
wvSetCharHandler(&ps, char_handler);
wvSetSpecialCharHandler(&ps, special_char_handler);
state_data handle;
TranslationState translation_state(out, resources, config(), m_cache_path);
wvInitStateData(&handle);
translation_state.sd = &handle;
ps.userData = &translation_state;
out.write_begin();
out.write_header_begin();
out.write_header_charset("UTF-8");
out.write_header_target("_blank");
out.write_header_title("odr");
out.write_header_viewport(
"width=device-width,initial-scale=1.0,user-scalable=yes");
out.write_header_end();
out.write_body_begin();
if (wvHtml(&ps) != 0) {
throw std::runtime_error("wvHtml failed");
}
out.write_body_end();
out.write_end();
return resources;
}
protected:
WvWareLegacyMicrosoftFile m_oldms_file;
std::string m_cache_path;
HtmlViews m_views;
mutable std::mutex m_mutex;
mutable bool m_warm = false;
mutable HtmlResources m_resources;
};
} // namespace
} // namespace odr::internal::html
namespace odr::internal {
HtmlService html::create_wvware_oldms_service(
const WvWareLegacyMicrosoftFile &oldms_file, const std::string &cache_path,
HtmlConfig config, std::shared_ptr<Logger> logger) {
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
oldms_file, std::move(config), cache_path, std::move(logger)));
}
} // namespace odr::internal