@@ -48,6 +48,8 @@ struct TranslationState : public expand_data {
4848 int message = 0 ;
4949 } special_char_handler_state = {};
5050
51+ std::size_t figure_number = 0 ;
52+
5153 html::HtmlWriter out;
5254};
5355
@@ -168,7 +170,6 @@ thing and just put ... instead of this
168170 return 1 ;
169171
170172 /* Mac specials (MV): */
171-
172173 case 0xf020 :
173174 out.out () << " " ;
174175 return 1 ;
@@ -218,10 +219,8 @@ void output_from_unicode(wvParseStruct *ps, std::uint16_t eachchar,
218219 auto *data = (TranslationState *)ps->userData ;
219220 auto &out = data->out ;
220221
221- // TODO static
222- static char cached_outputtype[33 ]; /* Last outputtype */
223- static GIConv g_iconv_handle = (GIConv)-1 ; /* Cached iconv descriptor */
224- static int need_swapping;
222+ GIConv g_iconv_handle = (GIConv)-1 ;
223+ int need_swapping;
225224 gchar *ibuf, *obuf;
226225 std::size_t ibuflen, obuflen, len, count, i;
227226 std::uint8_t buffer[2 ], buffer2[5 ];
@@ -230,12 +229,7 @@ void output_from_unicode(wvParseStruct *ps, std::uint16_t eachchar,
230229 return ;
231230 }
232231
233- if ((g_iconv_handle == (GIConv)-1 ) ||
234- strcmp (cached_outputtype, outputtype) != 0 ) {
235- if ((g_iconv_handle != (GIConv)-1 )) {
236- g_iconv_close (g_iconv_handle);
237- }
238-
232+ {
239233 g_iconv_handle = g_iconv_open (outputtype, " UCS-2" );
240234 if (g_iconv_handle == (GIConv)-1 ) {
241235 std::cerr << " g_iconv_open fail: " << errno
@@ -244,9 +238,6 @@ void output_from_unicode(wvParseStruct *ps, std::uint16_t eachchar,
244238 return ;
245239 }
246240
247- /* safe to cache the output type here */
248- strcpy (cached_outputtype, outputtype);
249-
250241 /* Determining if unicode biteorder is swapped (glibc < 2.2) */
251242 need_swapping = 1 ;
252243
@@ -295,6 +286,9 @@ void output_from_unicode(wvParseStruct *ps, std::uint16_t eachchar,
295286 out.out () << buffer2[i];
296287 }
297288 }
289+
290+ // TODO iconv could be cached
291+ { g_iconv_close (g_iconv_handle); }
298292}
299293
300294// / Originally from `wvWare.c` `wvStrangeNoGraphicData`
@@ -315,7 +309,7 @@ void strange_no_graphic_data(wvParseStruct *ps, int graphicstype) {
315309// / https://github.com/opendocument-app/wvWare/blob/c015326b001f1ad6dfb1f5e718461c16c56cca5f/wvWare.c#L1239-L1287
316310// / simplified to HTML output
317311void print_graphics (wvParseStruct *ps, int graphicstype, int width, int height,
318- char * source) {
312+ const std::string & source) {
319313 // upstream converts to PNG, we just use the original format as the browser
320314 // should support them
321315
@@ -329,15 +323,15 @@ void print_graphics(wvParseStruct *ps, int graphicstype, int width, int height,
329323 << source << R"( "/><br/>)" ;
330324}
331325
332- int handle_bitmap (wvParseStruct * /* ps*/ , char *name, BitmapBlip *bitmap) {
326+ void handle_bitmap (wvParseStruct * /* ps*/ , const std::string &name,
327+ BitmapBlip *bitmap) {
333328 wvStream *pwv = bitmap->m_pvBits ;
334329 FILE *fd = nullptr ;
335330 std::size_t size = 0 , i;
336331
337- fd = fopen (name, " wb" );
332+ fd = fopen (name. c_str () , " wb" );
338333 if (fd == nullptr ) {
339- fprintf (stderr, " \n Cannot open %s for writing\n " , name);
340- exit (1 );
334+ throw std::runtime_error (" Cannot open " + name + " file for writing" );
341335 }
342336 size = wvStream_size (pwv);
343337 wvStream_rewind (pwv);
@@ -346,11 +340,10 @@ int handle_bitmap(wvParseStruct * /*ps*/, char *name, BitmapBlip *bitmap) {
346340 fputc (read_8ubit (pwv), fd);
347341 }
348342 fclose (fd);
349- wvTrace ((" Name is %s\n " , name));
350- return 0 ;
351343}
352344
353- int handle_metafile (wvParseStruct * /* ps*/ , char *name, MetaFileBlip *bitmap) {
345+ int handle_metafile (wvParseStruct * /* ps*/ , const char *name,
346+ MetaFileBlip *bitmap) {
354347 wvStream *pwv = bitmap->m_pvBits ;
355348 FILE *fd = nullptr ;
356349 std::size_t size = 0 , i;
@@ -364,12 +357,14 @@ int handle_metafile(wvParseStruct * /*ps*/, char *name, MetaFileBlip *bitmap) {
364357 size = wvStream_size (pwv);
365358 wvStream_rewind (pwv);
366359
367- if (bitmap->m_fCompression == msocompressionDeflate)
360+ if (bitmap->m_fCompression == msocompressionDeflate) {
368361 decompressf = setdecom ();
362+ }
369363
370364 if (!decompressf) {
371- for (i = 0 ; i < size; i++)
365+ for (i = 0 ; i < size; i++) {
372366 fputc (read_8ubit (pwv), fd);
367+ }
373368 } else /* decompress here */
374369 {
375370 FILE *tmp = tmpfile ();
@@ -385,49 +380,51 @@ int handle_metafile(wvParseStruct * /*ps*/, char *name, MetaFileBlip *bitmap) {
385380
386381 rewind (out);
387382
388- for (i = 0 ; i < bitmap->m_cb ; i++)
383+ for (i = 0 ; i < bitmap->m_cb ; i++) {
389384 fputc (fgetc (out), fd);
385+ }
390386
391387 fclose (out);
392388 }
393389
394390 fclose (fd);
395- wvTrace ((" Name is %s\n " , name));
396391 return 0 ;
397392}
398393
399- char *html_graphic (wvParseStruct *ps, Blip *blip) {
400- char *name;
394+ std::string figure_name (wvParseStruct *ps) {
395+ auto *data = (TranslationState *)ps->userData ;
396+
397+ std::size_t number = data->figure_number ++;
398+ std::string name = " figure" + std::to_string (number);
399+
400+ return name;
401+ }
402+
403+ std::string html_graphic (wvParseStruct *ps, Blip *blip) {
404+ std::string name;
401405 wvStream *fd;
402406 char test[3 ];
403407
404- // TODO handle figure name
405- name = nullptr ;
406- if (name == nullptr ) {
407- return nullptr ;
408- }
408+ name = figure_name (ps);
409409
410410 /*
411411 temp hack to test older included bmps in word 6 and 7,
412412 should be wrapped in a modern escher strucure before getting
413413 to here, and then handled as normal
414414 */
415- wvTrace ((" type is %d\n " , blip->type ));
416415 switch (blip->type ) {
417416 case msoblipJPEG:
418417 case msoblipDIB:
419418 case msoblipPNG:
420419 fd = (blip->blip .bitmap .m_pvBits );
421420 test[2 ] = ' \0 ' ;
422- test[0 ] = read_8ubit (fd);
421+ test[0 ] = ( char ) read_8ubit (fd);
423422
424- test[1 ] = read_8ubit (fd);
423+ test[1 ] = ( char ) read_8ubit (fd);
425424 wvStream_rewind (fd);
426425 if (!(strcmp (test, " BM" ))) {
427- wvAppendStr (&name, " .bmp" );
428- if (0 != handle_bitmap (ps, name, &blip->blip .bitmap )) {
429- return nullptr ;
430- }
426+ name += " .bmp" ;
427+ handle_bitmap (ps, name, &blip->blip .bitmap );
431428 return name;
432429 }
433430 default :
@@ -436,40 +433,28 @@ char *html_graphic(wvParseStruct *ps, Blip *blip) {
436433
437434 switch (blip->type ) {
438435 case msoblipWMF:
439- wvAppendStr (&name, " .wmf" );
440- if (0 != handle_metafile (ps, name, &blip->blip .metafile )) {
441- return nullptr ;
442- }
436+ name += " .wmf" ;
437+ handle_metafile (ps, name.c_str (), &blip->blip .metafile );
443438 break ;
444439 case msoblipEMF:
445- wvAppendStr (&name, " .emf" );
446- if (0 != handle_metafile (ps, name, &blip->blip .metafile )) {
447- return nullptr ;
448- }
440+ name += " .emf" ;
441+ handle_metafile (ps, name.c_str (), &blip->blip .metafile );
449442 break ;
450443 case msoblipPICT:
451- wvAppendStr (&name, " .pict" );
452- if (0 != handle_metafile (ps, name, &blip->blip .metafile )) {
453- return nullptr ;
454- }
444+ name += " .pict" ;
445+ handle_metafile (ps, name.c_str (), &blip->blip .metafile );
455446 break ;
456447 case msoblipJPEG:
457- wvAppendStr (&name, " .jpg" );
458- if (0 != handle_bitmap (ps, name, &blip->blip .bitmap )) {
459- return nullptr ;
460- }
448+ name += " .jpg" ;
449+ handle_bitmap (ps, name.c_str (), &blip->blip .bitmap );
461450 break ;
462451 case msoblipDIB:
463- wvAppendStr (&name, " .dib" );
464- if (0 != handle_bitmap (ps, name, &blip->blip .bitmap )) {
465- return nullptr ;
466- }
452+ name += " .dib" ;
453+ handle_bitmap (ps, name.c_str (), &blip->blip .bitmap );
467454 break ;
468455 case msoblipPNG:
469- wvAppendStr (&name, " .png" );
470- if (0 != handle_bitmap (ps, name, &blip->blip .bitmap )) {
471- return nullptr ;
472- }
456+ name += " .png" ;
457+ handle_bitmap (ps, name.c_str (), &blip->blip .bitmap );
473458 break ;
474459 }
475460 return name;
@@ -702,7 +687,6 @@ int special_char_handler(wvParseStruct *ps, std::uint16_t eachchar, CHP *achp) {
702687 case 0x01 : {
703688 wvStream *f;
704689 Blip blip;
705- char *name;
706690 long p = wvStream_tell (ps->data );
707691
708692 if (achp->fOle2 != 0 ) {
@@ -713,10 +697,9 @@ int special_char_handler(wvParseStruct *ps, std::uint16_t eachchar, CHP *achp) {
713697 wvGetPICF (wvQuerySupported (&ps->fib , nullptr ), &picf, ps->data );
714698 f = picf.rgb ;
715699 if (wv0x01 (&blip, f, picf.lcb - picf.cbHeader ) != 0 ) {
716- name = html_graphic (ps, &blip);
700+ std::string name = html_graphic (ps, &blip);
717701 print_graphics (ps, 0x01 , (int )wvTwipsToHPixels (picf.dxaGoal ),
718702 (int )wvTwipsToVPixels (picf.dyaGoal ), name);
719- wvFree (name);
720703 } else {
721704 strange_no_graphic_data (ps, 0x01 );
722705 }
@@ -726,7 +709,6 @@ int special_char_handler(wvParseStruct *ps, std::uint16_t eachchar, CHP *achp) {
726709 }
727710 case 0x08 : {
728711 Blip blip;
729- char *name;
730712 if (wvQuerySupported (&ps->fib , nullptr ) == WORD8) {
731713 if (ps->nooffspa > 0 ) {
732714 fspa =
@@ -739,23 +721,21 @@ int special_char_handler(wvParseStruct *ps, std::uint16_t eachchar, CHP *achp) {
739721
740722 data->props = fspa;
741723 if (wv0x08 (&blip, (int )fspa->spid , ps) != 0 ) {
742- name = html_graphic (ps, &blip);
724+ std::string name = html_graphic (ps, &blip);
743725 print_graphics (
744726 ps, 0x08 ,
745727 (int )wvTwipsToHPixels ((short )(fspa->xaRight - fspa->xaLeft )),
746728 (int )wvTwipsToVPixels ((short )(fspa->yaBottom - fspa->yaTop )),
747729 name);
748- wvFree (name);
749730 } else {
750731 strange_no_graphic_data (ps, 0x08 );
751732 }
752733 } else {
753734 std::cerr << " nooffspa was <=0! Ignoring.\n " ;
754735 }
755736 } else {
756- FDOA *fdoa;
757737 std::cerr << " pre word8 0x08 graphic, unsupported at the moment\n " ;
758- fdoa =
738+ FDOA * fdoa =
759739 wvGetFDOAFromCP (ps->currentcp , ps->fdoa , ps->fdoapos , ps->nooffdoa );
760740 data->props = fdoa;
761741 }
@@ -823,7 +803,7 @@ Html html::translate_wvware_oldms_file(
823803 HtmlResourceLocator resourceLocator =
824804 local_resource_locator (output_path, config);
825805
826- auto output_file_path = output_path + " /document.html" ;
806+ std::string output_file_path = output_path + " /document.html" ;
827807
828808 std::ofstream ostream (output_file_path, std::ios::out);
829809 if (!ostream.is_open ()) {
0 commit comments