forked from mathog/libUEMF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwmf-inout.cpp.example
More file actions
3227 lines (2890 loc) · 127 KB
/
wmf-inout.cpp.example
File metadata and controls
3227 lines (2890 loc) · 127 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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
* @brief Windows-only Enhanced Metafile input and output.
*/
/* Authors:
* Ulf Erikson <ulferikson@users.sf.net>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* Copyright (C) 2006-2008 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*
* References:
* - How to Create & Play Enhanced Metafiles in Win32
* http://support.microsoft.com/kb/q145999/
* - INFO: Windows Metafile Functions & Aldus Placeable Metafiles
* http://support.microsoft.com/kb/q66949/
* - Metafile Functions
* http://msdn.microsoft.com/library/en-us/gdi/metafile_0whf.asp
* - Metafile Structures
* http://msdn.microsoft.com/library/en-us/gdi/metafile_5hkj.asp
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <png.h> //This must precede text_reassemble.h or it blows up in pngconf.h when compiling
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define EMF_DRIVER //work around SPStyle issue, MUST be EMF, not WMF
#include "sp-root.h"
#include "sp-path.h"
#include "style.h"
#include "print.h"
#include "extension/system.h"
#include "extension/print.h"
#include "extension/db.h"
#include "extension/input.h"
#include "extension/output.h"
#include "display/drawing.h"
#include "display/drawing-item.h"
#include "unit-constants.h"
#include "clear-n_.h"
#include "document.h"
#include "libunicode-convert/unicode-convert.h"
#include "wmf-inout.h"
#include "wmf-print.h"
#define PRINT_WMF "org.inkscape.print.wmf"
#ifndef U_PS_JOIN_MASK
#define U_PS_JOIN_MASK (U_PS_JOIN_BEVEL|U_PS_JOIN_MITER|U_PS_JOIN_ROUND)
#endif
namespace Inkscape {
namespace Extension {
namespace Internal {
static U_RECT16 rc_old;
static bool clipset = false;
static uint32_t BLTmode=0;
/** Construct a PNG in memory from an RGB from the WMF file
from:
http://www.lemoda.net/c/write-png/
which was based on:
http://stackoverflow.com/questions/1821806/how-to-encode-png-to-buffer-using-libpng
gcc -Wall -o testpng testpng.c -lpng
Originally here, but moved up
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
*/
/* Given "bitmap", this returns the pixel of bitmap at the point
("x", "y"). */
pixel_t * Wmf::pixel_at (bitmap_t * bitmap, int x, int y)
{
return bitmap->pixels + bitmap->width * y + x;
}
/* Write "bitmap" to a PNG file specified by "path"; returns 0 on
success, non-zero on error. */
void
Wmf::my_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
PMEMPNG p=(PMEMPNG)png_get_io_ptr(png_ptr);
size_t nsize = p->size + length;
/* allocate or grow buffer */
if(p->buffer){ p->buffer = (char *) realloc(p->buffer, nsize); }
else{ p->buffer = (char *) malloc(nsize); }
if(!p->buffer){ png_error(png_ptr, "Write Error"); }
/* copy new bytes to end of buffer */
memcpy(p->buffer + p->size, data, length);
p->size += length;
}
void Wmf::toPNG(PMEMPNG accum, int width, int height, const char *px){
bitmap_t bmStore;
bitmap_t *bitmap = &bmStore;
accum->buffer=NULL; // PNG constructed in memory will end up here, caller must free().
accum->size=0;
bitmap->pixels=(pixel_t *)px;
bitmap->width = width;
bitmap->height = height;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_byte ** row_pointers = NULL;
/* The following number is set by trial and error only. I cannot
see where it it is documented in the libpng manual.
*/
int pixel_size = 3;
int depth = 8;
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL){
accum->buffer=NULL;
return;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL){
png_destroy_write_struct (&png_ptr, &info_ptr);
accum->buffer=NULL;
return;
}
/* Set up error handling. */
if (setjmp (png_jmpbuf (png_ptr))) {
png_destroy_write_struct (&png_ptr, &info_ptr);
accum->buffer=NULL;
return;
}
/* Set image attributes. */
png_set_IHDR (
png_ptr,
info_ptr,
bitmap->width,
bitmap->height,
depth,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
/* Initialize rows of PNG. */
row_pointers = (png_byte **) png_malloc (png_ptr, bitmap->height * sizeof (png_byte *));
for (y = 0; y < bitmap->height; ++y) {
png_byte *row =
(png_byte *) png_malloc (png_ptr, sizeof (uint8_t) * bitmap->width * pixel_size);
row_pointers[bitmap->height - y - 1] = row; // Row order in WMF is reversed.
for (x = 0; x < bitmap->width; ++x) {
pixel_t * pixel = pixel_at (bitmap, x, y);
*row++ = pixel->red; // R & B channels were set correctly by DIB_to_RGB
*row++ = pixel->green;
*row++ = pixel->blue;
}
}
/* Write the image data to memory */
png_set_rows (png_ptr, info_ptr, row_pointers);
png_set_write_fn(png_ptr, accum, my_png_write_data, NULL);
png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
for (y = 0; y < bitmap->height; y++) {
png_free (png_ptr, row_pointers[y]);
}
png_free (png_ptr, row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
}
/* convert an WMF RGB(A) color to 0RGB
inverse of gethexcolor() in emf-print.cpp
*/
uint32_t Wmf::sethexcolor(U_COLORREF color){
uint32_t out;
out = (U_RGBAGetR(color) << 16) +
(U_RGBAGetG(color) << 8 ) +
(U_RGBAGetB(color) );
return(out);
}
Wmf::Wmf (void) // The null constructor
{
return;
}
Wmf::~Wmf (void) //The destructor
{
return;
}
bool
Wmf::check (Inkscape::Extension::Extension * /*module*/)
{
if (NULL == Inkscape::Extension::db.get(PRINT_WMF))
return FALSE;
return TRUE;
}
void
Wmf::print_document_to_file(SPDocument *doc, const gchar *filename)
{
Inkscape::Extension::Print *mod;
SPPrintContext context;
const gchar *oldconst;
gchar *oldoutput;
unsigned int ret;
doc->ensureUpToDate();
mod = Inkscape::Extension::get_print(PRINT_WMF);
oldconst = mod->get_param_string("destination");
oldoutput = g_strdup(oldconst);
mod->set_param_string("destination", filename);
/* Start */
context.module = mod;
/* fixme: This has to go into module constructor somehow */
/* Create new arena */
mod->base = doc->getRoot();
Inkscape::Drawing drawing;
mod->dkey = SPItem::display_key_new(1);
mod->root = mod->base->invoke_show(drawing, mod->dkey, SP_ITEM_SHOW_DISPLAY);
drawing.setRoot(mod->root);
/* Print document */
ret = mod->begin(doc);
if (ret) {
g_free(oldoutput);
throw Inkscape::Extension::Output::save_failed();
}
mod->base->invoke_print(&context);
ret = mod->finish();
/* Release arena */
mod->base->invoke_hide(mod->dkey);
mod->base = NULL;
mod->root = NULL; // deleted by invoke_hide
/* end */
mod->set_param_string("destination", oldoutput);
g_free(oldoutput);
return;
}
void
Wmf::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *filename)
{
Inkscape::Extension::Extension * ext;
ext = Inkscape::Extension::db.get(PRINT_WMF);
if (ext == NULL)
return;
bool new_val = mod->get_param_bool("textToPath");
bool new_FixPPTCharPos = mod->get_param_bool("FixPPTCharPos"); // character position bug
// reserve FixPPT2 for opacity bug. Currently WMF does not export opacity values
bool new_FixPPTDashLine = mod->get_param_bool("FixPPTDashLine"); // dashed line bug
bool new_FixPPTGrad2Polys = mod->get_param_bool("FixPPTGrad2Polys"); // gradient bug
bool new_FixPPTPatternAsHatch = mod->get_param_bool("FixPPTPatternAsHatch"); // force all patterns as standard WMF hatch
TableGen( //possibly regenerate the unicode-convert tables
mod->get_param_bool("TnrToSymbol"),
mod->get_param_bool("TnrToWingdings"),
mod->get_param_bool("TnrToZapfDingbats"),
mod->get_param_bool("UsePUA")
);
ext->set_param_bool("FixPPTCharPos",new_FixPPTCharPos); // Remember to add any new ones to PrintWmf::init or a mysterious failure will result!
ext->set_param_bool("FixPPTDashLine",new_FixPPTDashLine);
ext->set_param_bool("FixPPTGrad2Polys",new_FixPPTGrad2Polys);
ext->set_param_bool("FixPPTPatternAsHatch",new_FixPPTPatternAsHatch);
ext->set_param_bool("textToPath", new_val);
print_document_to_file(doc, filename);
return;
}
enum drawmode {DRAW_PAINT, DRAW_PATTERN, DRAW_IMAGE}; // apply to either fill or stroke
/* WMF has no worldTranform, so this always returns 1.0. Retain it to keep WMF and WMF in sync as much as possible.*/
double Wmf::current_scale(PWMF_CALLBACK_DATA d){
return 1.0;
}
/* WMF has no worldTranform, so this always returns an Identity rotation matrix, but the offsets may have values.*/
std::string Wmf::current_matrix(PWMF_CALLBACK_DATA d, double x, double y, int useoffset){
std::stringstream cxform;
double scale = current_scale(d);
cxform << "\"matrix(";
cxform << 1.0/scale; cxform << ",";
cxform << 0.0; cxform << ",";
cxform << 0.0; cxform << ",";
cxform << 1.0/scale; cxform << ",";
if(useoffset){ cxform << x; cxform << ","; cxform << y; }
else { cxform << "0,0"; }
cxform << ")\"";
return(cxform.str());
}
/* WMF has no worldTranform, so this always returns 0. Retain it to keep WMF and WMF in sync as much as possible.*/
double Wmf::current_rotation(PWMF_CALLBACK_DATA d){
return 0.0;
}
/* Add another 100 blank slots to the hatches array.
*/
void Wmf::enlarge_hatches(PWMF_CALLBACK_DATA d){
d->hatches.size += 100;
d->hatches.strings = (char **) realloc(d->hatches.strings,d->hatches.size * sizeof(char *));
}
/* See if the pattern name is already in the list. If it is return its position (1->n, not 1-n-1)
*/
int Wmf::in_hatches(PWMF_CALLBACK_DATA d, char *test){
int i;
for(i=0; i<d->hatches.count; i++){
if(strcmp(test,d->hatches.strings[i])==0)return(i+1);
}
return(0);
}
/* (Conditionally) add a hatch. If a matching hatch already exists nothing happens. If one
does not exist it is added to the hatches list and also entered into <defs>.
This is also used to add the path part of the hatches, which they reference with a xlink:href
*/
uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hatchColor){
char hatchname[64]; // big enough
char hpathname[64]; // big enough
char hbkname[64]; // big enough
char tmpcolor[8];
char bkcolor[8];
uint32_t idx;
switch(hatchType){
case U_HS_SOLIDTEXTCLR:
case U_HS_DITHEREDTEXTCLR:
sprintf(tmpcolor,"%6.6X",sethexcolor(d->dc[d->level].textColor));
break;
case U_HS_SOLIDBKCLR:
case U_HS_DITHEREDBKCLR:
sprintf(tmpcolor,"%6.6X",sethexcolor(d->dc[d->level].bkColor));
break;
default:
sprintf(tmpcolor,"%6.6X",sethexcolor(hatchColor));
break;
}
/* For both bkMode types set the PATH + FOREGROUND COLOR for the indicated standard hatch.
This will be used late to compose, or recompose the transparent or opaque final hatch.*/
std::string refpath; // used to reference later the path pieces which are about to be created
sprintf(hpathname,"WMFhpath%d_%s",hatchType,tmpcolor);
idx = in_hatches(d,hpathname);
if(!idx){ // add path/color if not already present
if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); }
d->hatches.strings[d->hatches.count++]=strdup(hpathname);
*(d->defs) += "\n";
switch(hatchType){
case U_HS_HORIZONTAL:
*(d->defs) += " <path id=\"";
*(d->defs) += hpathname;
*(d->defs) += "\" d=\"M 0 0 6 0\" style=\"fill:none;stroke:#";
*(d->defs) += tmpcolor;
*(d->defs) += "\" />\n";
break;
case U_HS_VERTICAL:
*(d->defs) += " <path id=\"";
*(d->defs) += hpathname;
*(d->defs) += "\" d=\"M 0 0 0 6\" style=\"fill:none;stroke:#";
*(d->defs) += tmpcolor;
*(d->defs) += "\" />\n";
break;
case U_HS_FDIAGONAL:
*(d->defs) += " <line id=\"sub";
*(d->defs) += hpathname;
*(d->defs) += "\" x1=\"-1\" y1=\"-1\" x2=\"7\" y2=\"7\" stroke=\"#";
*(d->defs) += tmpcolor;
*(d->defs) += "\"/>\n";
break;
case U_HS_BDIAGONAL:
*(d->defs) += " <line id=\"sub";
*(d->defs) += hpathname;
*(d->defs) += "\" x1=\"-1\" y1=\"7\" x2=\"7\" y2=\"-1\" stroke=\"#";
*(d->defs) += tmpcolor;
*(d->defs) += "\"/>\n";
break;
case U_HS_CROSS:
*(d->defs) += " <path id=\"";
*(d->defs) += hpathname;
*(d->defs) += "\" d=\"M 0 0 6 0 M 0 0 0 6\" style=\"fill:none;stroke:#";
*(d->defs) += tmpcolor;
*(d->defs) += "\" />\n";
break;
case U_HS_DIAGCROSS:
*(d->defs) += " <line id=\"subfd";
*(d->defs) += hpathname;
*(d->defs) += "\" x1=\"-1\" y1=\"-1\" x2=\"7\" y2=\"7\" stroke=\"#";
*(d->defs) += tmpcolor;
*(d->defs) += "\"/>\n";
*(d->defs) += " <line id=\"subbd";
*(d->defs) += hpathname;
*(d->defs) += "\" x1=\"-1\" y1=\"7\" x2=\"7\" y2=\"-1\" stroke=\"#";
*(d->defs) += tmpcolor;
*(d->defs) += "\"/>\n";
break;
case U_HS_SOLIDCLR:
case U_HS_DITHEREDCLR:
case U_HS_SOLIDTEXTCLR:
case U_HS_DITHEREDTEXTCLR:
case U_HS_SOLIDBKCLR:
case U_HS_DITHEREDBKCLR:
default:
*(d->defs) += " <path id=\"";
*(d->defs) += hpathname;
*(d->defs) += "\" d=\"M 0 0 6 0 6 6 0 6 z\" style=\"fill:#";
*(d->defs) += tmpcolor;
*(d->defs) += ";stroke:none";
*(d->defs) += "\" />\n";
break;
}
}
// References to paths possibly just created above. These will be used in the actual patterns.
switch(hatchType){
case U_HS_HORIZONTAL:
case U_HS_VERTICAL:
case U_HS_CROSS:
case U_HS_SOLIDCLR:
case U_HS_DITHEREDCLR:
case U_HS_SOLIDTEXTCLR:
case U_HS_DITHEREDTEXTCLR:
case U_HS_SOLIDBKCLR:
case U_HS_DITHEREDBKCLR:
default:
refpath += " <use xlink:href=\"#";
refpath += hpathname;
refpath += "\" />\n";
break;
case U_HS_FDIAGONAL:
case U_HS_BDIAGONAL:
refpath += " <use xlink:href=\"#sub";
refpath += hpathname;
refpath += "\" />\n";
refpath += " <use xlink:href=\"#sub";
refpath += hpathname;
refpath += "\" transform=\"translate(6,0)\" />\n";
refpath += " <use xlink:href=\"#sub";
refpath += hpathname;
refpath += "\" transform=\"translate(-6,0)\" />\n";
break;
case U_HS_DIAGCROSS:
refpath += " <use xlink:href=\"#subfd";
refpath += hpathname;
refpath += "\" />\n";
refpath += " <use xlink:href=\"#subfd";
refpath += hpathname;
refpath += "\" transform=\"translate(6,0)\"/>\n";
refpath += " <use xlink:href=\"#subfd";
refpath += hpathname;
refpath += "\" transform=\"translate(-6,0)\"/>\n";
refpath += " <use xlink:href=\"#subbd";
refpath += hpathname;
refpath += "\" />\n";
refpath += " <use xlink:href=\"#subbd";
refpath += hpathname;
refpath += "\" transform=\"translate(6,0)\"/>\n";
refpath += " <use xlink:href=\"#subbd";
refpath += hpathname;
refpath += "\" transform=\"translate(-6,0)\"/>\n";
break;
}
if(d->dc[d->level].bkMode == U_TRANSPARENT || hatchType >= U_HS_SOLIDCLR){
sprintf(hatchname,"WMFhatch%d_%s",hatchType,tmpcolor);
sprintf(hpathname,"WMFhpath%d_%s",hatchType,tmpcolor);
idx = in_hatches(d,hatchname);
if(!idx){ // add it if not already present
if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); }
d->hatches.strings[d->hatches.count++]=strdup(hatchname);
*(d->defs) += "\n";
*(d->defs) += " <pattern id=\"";
*(d->defs) += hatchname;
*(d->defs) += "\" xlink:href=\"#WMFhbasepattern\">\n";
*(d->defs) += refpath;
*(d->defs) += " </pattern>\n";
idx = d->hatches.count;
}
}
else { // bkMode==U_OPAQUE
/* Set up an object in the defs for this background, if there is not one already there */
sprintf(bkcolor,"%6.6X",sethexcolor(d->dc[d->level].bkColor));
sprintf(hbkname,"WMFhbkclr_%s",bkcolor);
idx = in_hatches(d,hbkname);
if(!idx){ // add path/color if not already present. Hatchtype is not needed in the name.
if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); }
d->hatches.strings[d->hatches.count++]=strdup(hbkname);
*(d->defs) += "\n";
*(d->defs) += " <rect id=\"";
*(d->defs) += hbkname;
*(d->defs) += "\" x=\"0\" y=\"0\" width=\"6\" height=\"6\" fill=\"#";
*(d->defs) += bkcolor;
*(d->defs) += "\" />\n";
}
// this is the pattern, its name will show up in Inkscape's pattern selector
sprintf(hatchname,"WMFhatch%d_%s_%s",hatchType,tmpcolor,bkcolor);
idx = in_hatches(d,hatchname);
if(!idx){ // add it if not already present
if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); }
d->hatches.strings[d->hatches.count++]=strdup(hatchname);
*(d->defs) += "\n";
*(d->defs) += " <pattern id=\"";
*(d->defs) += hatchname;
*(d->defs) += "\" xlink:href=\"#WMFhbasepattern\">\n";
*(d->defs) += " <use xlink:href=\"#";
*(d->defs) += hbkname;
*(d->defs) += "\" />\n";
*(d->defs) += refpath;
*(d->defs) += " </pattern>\n";
idx = d->hatches.count;
}
}
return(idx-1);
}
/* Add another 100 blank slots to the images array.
*/
void Wmf::enlarge_images(PWMF_CALLBACK_DATA d){
d->images.size += 100;
d->images.strings = (char **) realloc(d->images.strings,d->images.size * sizeof(char *));
}
/* See if the image string is already in the list. If it is return its position (1->n, not 1-n-1)
*/
int Wmf::in_images(PWMF_CALLBACK_DATA d, char *test){
int i;
for(i=0; i<d->images.count; i++){
if(strcmp(test,d->images.strings[i])==0)return(i+1);
}
return(0);
}
/* (Conditionally) add an image from a DIB. If a matching image already exists nothing happens. If one
does not exist it is added to the images list and also entered into <defs>.
*/
uint32_t Wmf::add_dib_image(PWMF_CALLBACK_DATA d, const char *dib, uint32_t iUsage){
uint32_t idx;
char imagename[64]; // big enough
char xywh[64]; // big enough
int dibparams;
MEMPNG mempng; // PNG in memory comes back in this
mempng.buffer = NULL;
char *rgba_px = NULL; // RGBA pixels
const char *px = NULL; // DIB pixels
const U_RGBQUAD *ct = NULL; // DIB color table
int32_t width, height, colortype, numCt, invert;
if((iUsage != U_DIB_RGB_COLORS) ||
!(dibparams = wget_DIB_params( // this returns pointers and values, but allocates no memory
dib,
&px,
&ct,
&numCt,
&width,
&height,
&colortype,
&invert
))
){
if(!DIB_to_RGBA(
px, // DIB pixel array
ct, // DIB color table
numCt, // DIB color table number of entries
&rgba_px, // U_RGBA pixel array (32 bits), created by this routine, caller must free.
width, // Width of pixel array in record
height, // Height of pixel array in record
colortype, // DIB BitCount Enumeration
numCt, // Color table used if not 0
invert // If DIB rows are in opposite order from RGBA rows
) &&
rgba_px
){
toPNG( // Get the image from the RGBA px into mempng
&mempng,
width, height, // of the SRC bitmap
rgba_px
);
free(rgba_px);
}
}
gchar *base64String;
if(dibparams == U_BI_JPEG || dibparams==U_BI_PNG){
base64String = g_base64_encode((guchar*) px, numCt );
idx = in_images(d, (char *) base64String);
}
else if(mempng.buffer){
base64String = g_base64_encode((guchar*) mempng.buffer, mempng.size );
free(mempng.buffer);
idx = in_images(d, (char *) base64String);
}
else {
// insert a random 3x4 blotch otherwise
width = 3;
height = 4;
base64String = g_strdup("iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAA3NCSVQICAjb4U/gAAAALElEQVQImQXBQQ2AMAAAsUJQMSWI2H8qME1yMshojwrvGB8XcHKvR1XtOTc/8HENumHCsOMAAAAASUVORK5CYII=");
idx = in_images(d, (char *) base64String);
}
if(!idx){ // add it if not already present - we looked at the actual data for comparison
if(d->images.count == d->images.size){ enlarge_images(d); }
idx = d->images.count;
d->images.strings[d->images.count++]=strdup(base64String);
sprintf(imagename,"WMFimage%d",idx++);
sprintf(xywh," x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" ",width,height); // reuse this buffer
*(d->defs) += "\n";
*(d->defs) += " <image id=\"";
*(d->defs) += imagename;
*(d->defs) += "\"\n ";
*(d->defs) += xywh;
*(d->defs) += "\n";
if(dibparams == U_BI_JPEG){ *(d->defs) += " xlink:href=\"data:image/jpeg;base64,"; }
else { *(d->defs) += " xlink:href=\"data:image/png;base64,"; }
*(d->defs) += base64String;
*(d->defs) += "\"\n";
*(d->defs) += " />\n";
*(d->defs) += "\n";
*(d->defs) += " <pattern id=\"";
*(d->defs) += imagename;
*(d->defs) += "_ref\"\n ";
*(d->defs) += xywh;
*(d->defs) += "\n patternUnits=\"userSpaceOnUse\"";
*(d->defs) += " >\n";
*(d->defs) += " <use id=\"";
*(d->defs) += imagename;
*(d->defs) += "_ign\" ";
*(d->defs) += " xlink:href=\"#";
*(d->defs) += imagename;
*(d->defs) += "\" />\n";
*(d->defs) += " ";
*(d->defs) += " </pattern>\n";
}
g_free(base64String);
return(idx-1);
}
/* (Conditionally) add an image from a Bitmap16. If a matching image already exists nothing happens. If one
does not exist it is added to the images list and also entered into <defs>.
*/
uint32_t Wmf::add_bm16_image(PWMF_CALLBACK_DATA d, U_BITMAP16 Bm16, const char *px){
uint32_t idx;
char imagename[64]; // big enough
char xywh[64]; // big enough
MEMPNG mempng; // PNG in memory comes back in this
mempng.buffer = NULL;
char *rgba_px = NULL; // RGBA pixels
const U_RGBQUAD *ct = NULL; // color table, always NULL here
int32_t width, height, colortype, numCt, invert;
numCt = 0;
width = Bm16.Width; // bitmap width in pixels.
height = Bm16.Height; // bitmap height in scan lines.
colortype = Bm16.BitsPixel; // seems to be BitCount Enumeration
invert = 0;
if(colortype < 16)return(0xFFFFFFFF); // these would need a colortable if they were a dib, no idea what bm16 is supposed to do instead.
if(!DIB_to_RGBA(// This is not really a dib, but close enough...
px, // DIB pixel array
ct, // DIB color table (always NULL here)
numCt, // DIB color table number of entries (always 0)
&rgba_px, // U_RGBA pixel array (32 bits), created by this routine, caller must free.
width, // Width of pixel array
height, // Height of pixel array
colortype, // DIB BitCount Enumeration
numCt, // Color table used if not 0
invert // If DIB rows are in opposite order from RGBA rows
) && rgba_px)
{
toPNG( // Get the image from the RGBA px into mempng
&mempng,
width, height, // of the SRC bitmap
rgba_px
);
free(rgba_px);
}
gchar *base64String;
if(mempng.buffer){
base64String = g_base64_encode((guchar*) mempng.buffer, mempng.size );
free(mempng.buffer);
}
else {
// insert a random 3x4 blotch otherwise
width = 3;
height = 4;
base64String = g_strdup("iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAA3NCSVQICAjb4U/gAAAALElEQVQImQXBQQ2AMAAAsUJQMSWI2H8qME1yMshojwrvGB8XcHKvR1XtOTc/8HENumHCsOMAAAAASUVORK5CYII=");
}
idx = in_images(d, (char *) base64String);
if(!idx){ // add it if not already present - we looked at the actual data for comparison
if(d->images.count == d->images.size){ enlarge_images(d); }
idx = d->images.count;
d->images.strings[d->images.count++]=g_strdup(base64String);
sprintf(imagename,"WMFimage%d",idx++);
sprintf(xywh," x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" ",width,height); // reuse this buffer
*(d->defs) += "\n";
*(d->defs) += " <image id=\"";
*(d->defs) += imagename;
*(d->defs) += "\"\n ";
*(d->defs) += xywh;
*(d->defs) += "\n";
*(d->defs) += " xlink:href=\"data:image/png;base64,";
*(d->defs) += base64String;
*(d->defs) += "\"\n";
*(d->defs) += " />\n";
*(d->defs) += "\n";
*(d->defs) += " <pattern id=\"";
*(d->defs) += imagename;
*(d->defs) += "_ref\"\n ";
*(d->defs) += xywh;
*(d->defs) += "\n patternUnits=\"userSpaceOnUse\"";
*(d->defs) += " >\n";
*(d->defs) += " <use id=\"";
*(d->defs) += imagename;
*(d->defs) += "_ign\" ";
*(d->defs) += " xlink:href=\"#";
*(d->defs) += imagename;
*(d->defs) += "\" />\n";
*(d->defs) += " </pattern>\n";
}
g_free(base64String);
return(idx-1);
}
void
Wmf::output_style(PWMF_CALLBACK_DATA d)
{
// SVGOStringStream tmp_id;
SVGOStringStream tmp_style;
char tmp[1024] = {0};
float fill_rgb[3];
sp_color_get_rgb_floatv( &(d->dc[d->level].style.fill.value.color), fill_rgb );
float stroke_rgb[3];
sp_color_get_rgb_floatv(&(d->dc[d->level].style.stroke.value.color), stroke_rgb);
// for U_WMR_BITBLT with no image, try to approximate some of these operations/
// Assume src color is "white"
if(d->dwRop3){
switch(d->dwRop3){
case U_PATINVERT: // treat all of these as black
case U_SRCINVERT:
case U_DSTINVERT:
case U_BLACKNESS:
case U_SRCERASE:
case U_NOTSRCCOPY:
fill_rgb[0]=fill_rgb[1]=fill_rgb[2]=0.0;
break;
case U_SRCCOPY: // treat all of these as white
case U_NOTSRCERASE:
case U_PATCOPY:
case U_WHITENESS:
fill_rgb[0]=fill_rgb[1]=fill_rgb[2]=1.0;
break;
case U_SRCPAINT: // use the existing color
case U_SRCAND:
case U_MERGECOPY:
case U_MERGEPAINT:
case U_PATPAINT:
default:
break;
}
d->dwRop3 = 0; // might as well reset it here, it must be set for each BITBLT
}
// Implement some of these, the ones where the original screen color does not matter.
// The options that merge screen and pen colors cannot be done correctly because we
// have no way of knowing what color is already on the screen. For those just pass the
// pen color through.
switch(d->dwRop2){
case U_R2_BLACK:
fill_rgb[0] = fill_rgb[1] = fill_rgb[2] = 0.0;
stroke_rgb[0]= stroke_rgb[1]= stroke_rgb[2] = 0.0;
break;
case U_R2_NOTMERGEPEN:
case U_R2_MASKNOTPEN:
break;
case U_R2_NOTCOPYPEN:
fill_rgb[0] = 1.0 - fill_rgb[0];
fill_rgb[1] = 1.0 - fill_rgb[1];
fill_rgb[2] = 1.0 - fill_rgb[2];
stroke_rgb[0] = 1.0 - stroke_rgb[0];
stroke_rgb[1] = 1.0 - stroke_rgb[1];
stroke_rgb[2] = 1.0 - stroke_rgb[2];
break;
case U_R2_MASKPENNOT:
case U_R2_NOT:
case U_R2_XORPEN:
case U_R2_NOTMASKPEN:
case U_R2_NOTXORPEN:
case U_R2_NOP:
case U_R2_MERGENOTPEN:
case U_R2_COPYPEN:
case U_R2_MASKPEN:
case U_R2_MERGEPENNOT:
case U_R2_MERGEPEN:
break;
case U_R2_WHITE:
fill_rgb[0] = fill_rgb[1] = fill_rgb[2] = 1.0;
stroke_rgb[0]= stroke_rgb[1]= stroke_rgb[2] = 1.0;
break;
default:
break;
}
// tmp_id << "\n\tid=\"" << (d->id++) << "\"";
// *(d->outsvg) += tmp_id.str().c_str();
*(d->outsvg) += "\n\tstyle=\"";
if (!d->dc[d->level].fill_set || ( d->mask & U_DRAW_NOFILL)) { // nofill are lines and arcs
tmp_style << "fill:none;";
} else {
switch(d->dc[d->level].fill_mode){
// both of these use the url(#) method
case DRAW_PATTERN:
snprintf(tmp, 1023, "fill:url(#%s); ",d->hatches.strings[d->dc[d->level].fill_idx]);
tmp_style << tmp;
break;
case DRAW_IMAGE:
snprintf(tmp, 1023, "fill:url(#WMFimage%d_ref); ",d->dc[d->level].fill_idx);
tmp_style << tmp;
break;
case DRAW_PAINT:
default: // <-- this should never happen, but just in case...
snprintf(
tmp, 1023,
"fill:#%02x%02x%02x;",
SP_COLOR_F_TO_U(fill_rgb[0]),
SP_COLOR_F_TO_U(fill_rgb[1]),
SP_COLOR_F_TO_U(fill_rgb[2])
);
tmp_style << tmp;
break;
}
snprintf(
tmp, 1023,
"fill-rule:%s;",
(d->dc[d->level].style.fill_rule.value == 0 ? "evenodd" : "nonzero")
);
tmp_style << tmp;
tmp_style << "fill-opacity:1;";
// if the stroke is the same as the fill, and the right size not to change the end size of the object, do not do it separately
if(
(d->dc[d->level].fill_set ) &&
(d->dc[d->level].stroke_set ) &&
(d->dc[d->level].style.stroke_width.value == 1 ) &&
(d->dc[d->level].fill_mode == d->dc[d->level].stroke_mode) &&
(
(d->dc[d->level].fill_mode != DRAW_PAINT) ||
(
(fill_rgb[0]==stroke_rgb[0]) &&
(fill_rgb[1]==stroke_rgb[1]) &&
(fill_rgb[2]==stroke_rgb[2])
)
)
){
d->dc[d->level].stroke_set = false;
}
}
if (!d->dc[d->level].stroke_set) {
tmp_style << "stroke:none;";
} else {
switch(d->dc[d->level].stroke_mode){
// both of these use the url(#) method
case DRAW_PATTERN:
snprintf(tmp, 1023, "stroke:url(#%s); ",d->hatches.strings[d->dc[d->level].stroke_idx]);
tmp_style << tmp;
break;
case DRAW_IMAGE:
snprintf(tmp, 1023, "stroke:url(#WMFimage%d_ref); ",d->dc[d->level].stroke_idx);
tmp_style << tmp;
break;
case DRAW_PAINT:
default: // <-- this should never happen, but just in case...
snprintf(
tmp, 1023,
"stroke:#%02x%02x%02x;",
SP_COLOR_F_TO_U(stroke_rgb[0]),
SP_COLOR_F_TO_U(stroke_rgb[1]),
SP_COLOR_F_TO_U(stroke_rgb[2])
);
tmp_style << tmp;
break;
}
if(d->dc[d->level].style.stroke_width.value){
tmp_style << "stroke-width:" <<
MAX( 0.001, d->dc[d->level].style.stroke_width.value ) << "px;";
}
else { // In a WMF a 0 width pixel means "1 pixel"
tmp_style << "stroke-width:" << pix_to_abs_size( d, 1 ) << "px;";
}
tmp_style << "stroke-linecap:" <<
(
d->dc[d->level].style.stroke_linecap.computed == 0 ? "butt" :
d->dc[d->level].style.stroke_linecap.computed == 1 ? "round" :
d->dc[d->level].style.stroke_linecap.computed == 2 ? "square" :
"unknown"
) << ";";
tmp_style << "stroke-linejoin:" <<
(
d->dc[d->level].style.stroke_linejoin.computed == 0 ? "miter" :
d->dc[d->level].style.stroke_linejoin.computed == 1 ? "round" :
d->dc[d->level].style.stroke_linejoin.computed == 2 ? "bevel" :
"unknown"
) << ";";
// Set miter limit if known, even if it is not needed immediately (not miter)
tmp_style << "stroke-miterlimit:" <<
MAX( 2.0, d->dc[d->level].style.stroke_miterlimit.value ) << ";";
if (d->dc[d->level].style.stroke_dasharray_set &&
d->dc[d->level].style.stroke_dash.n_dash && d->dc[d->level].style.stroke_dash.dash)
{
tmp_style << "stroke-dasharray:";
for (int i=0; i<d->dc[d->level].style.stroke_dash.n_dash; i++) {
if (i)
tmp_style << ",";
tmp_style << d->dc[d->level].style.stroke_dash.dash[i];
}
tmp_style << ";";
tmp_style << "stroke-dashoffset:0;";
} else {
tmp_style << "stroke-dasharray:none;";
}
tmp_style << "stroke-opacity:1;";
}
tmp_style << "\" ";
if (clipset)
tmp_style << "\n\tclip-path=\"url(#clipWmfPath" << d->id << ")\" ";
clipset = false;