-
Notifications
You must be signed in to change notification settings - Fork 3
/
emf-inout.cpp.example
3634 lines (3235 loc) · 151 KB
/
emf-inout.cpp.example
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 <[email protected]>
* Jon A. Cruz <[email protected]>
* David Mathog
* 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <libuemf/symbol_convert.h>
#include "document.h"
#include "sp-root.h" // even though it is included indirectly by wmf-inout.h
#include "sp-path.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 "clear-n_.h"
#include "svg/svg.h"
#include "util/units.h" // even though it is included indirectly by emf-inout.h
#include "inkscape.h" // even though it is included indirectly by emf-inout.h
#include "emf-print.h"
#include "emf-inout.h"
#define PRINT_EMF "org.inkscape.print.emf"
#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 uint32_t ICMmode = 0; // not used yet, but code to read it from EMF implemented
static uint32_t BLTmode = 0;
float faraway = 10000000; // used in "exclude" clips, hopefully well outside any real drawing!
Emf::Emf (void) // The null constructor
{
return;
}
Emf::~Emf (void) //The destructor
{
return;
}
bool
Emf::check (Inkscape::Extension::Extension * /*module*/)
{
if (NULL == Inkscape::Extension::db.get(PRINT_EMF))
return FALSE;
return TRUE;
}
void
Emf::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_EMF);
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);
(void) 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
Emf::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *filename)
{
Inkscape::Extension::Extension * ext;
ext = Inkscape::Extension::db.get(PRINT_EMF);
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 EMF 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_FixPPTLinGrad = mod->get_param_bool("FixPPTLinGrad"); // allow native rectangular linear gradient
bool new_FixPPTPatternAsHatch = mod->get_param_bool("FixPPTPatternAsHatch"); // force all patterns as standard EMF hatch
bool new_FixImageRot = mod->get_param_bool("FixImageRot"); // remove rotations on images
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 PrintEmf::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("FixPPTLinGrad",new_FixPPTLinGrad);
ext->set_param_bool("FixPPTPatternAsHatch",new_FixPPTPatternAsHatch);
ext->set_param_bool("FixImageRot",new_FixImageRot);
ext->set_param_bool("textToPath", new_val);
print_document_to_file(doc, filename);
return;
}
/* given the transformation matrix from worldTransform return the scale in the matrix part. Assumes that the
matrix is not used to skew, invert, or make another distorting transformation. */
double Emf::current_scale(PEMF_CALLBACK_DATA d){
double scale =
d->dc[d->level].worldTransform.eM11 * d->dc[d->level].worldTransform.eM22 -
d->dc[d->level].worldTransform.eM12 * d->dc[d->level].worldTransform.eM21;
if(scale <= 0.0)scale=1.0; /* something is dreadfully wrong with the matrix, but do not crash over it */
scale=sqrt(scale);
return(scale);
}
/* given the transformation matrix from worldTransform and the current x,y position in inkscape coordinates,
generate an SVG transform that gives the same amount of rotation, no scaling, and maps x,y back onto x,y. This is used for
rotating objects when the location of at least one point in that object is known. Returns:
"matrix(a,b,c,d,e,f)" (WITH the double quotes)
*/
std::string Emf::current_matrix(PEMF_CALLBACK_DATA d, double x, double y, int useoffset){
SVGOStringStream cxform;
double scale = current_scale(d);
cxform << "\"matrix(";
cxform << d->dc[d->level].worldTransform.eM11/scale; cxform << ",";
cxform << d->dc[d->level].worldTransform.eM12/scale; cxform << ",";
cxform << d->dc[d->level].worldTransform.eM21/scale; cxform << ",";
cxform << d->dc[d->level].worldTransform.eM22/scale; cxform << ",";
if(useoffset){
/* for the "new" coordinates drop the worldtransform translations, not used here */
double newx = x * d->dc[d->level].worldTransform.eM11/scale + y * d->dc[d->level].worldTransform.eM21/scale;
double newy = x * d->dc[d->level].worldTransform.eM12/scale + y * d->dc[d->level].worldTransform.eM22/scale;
cxform << x - newx; cxform << ",";
cxform << y - newy;
}
else {
cxform << "0,0";
}
cxform << ")\"";
return(cxform.str());
}
/* given the transformation matrix from worldTransform return the rotation angle in radians.
counter clocwise from the x axis. */
double Emf::current_rotation(PEMF_CALLBACK_DATA d){
return -std::atan2(d->dc[d->level].worldTransform.eM12, d->dc[d->level].worldTransform.eM11);
}
/* Add another 100 blank slots to the hatches array.
*/
void Emf::enlarge_hatches(PEMF_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 Emf::in_hatches(PEMF_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 Emf::add_hatch(PEMF_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,"EMFhpath%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,"EMFhatch%d_%s",hatchType,tmpcolor);
sprintf(hpathname,"EMFhpath%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=\"#EMFhbasepattern\">\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,"EMFhbkclr_%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,"EMFhatch%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=\"#EMFhbasepattern\">\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 Emf::enlarge_images(PEMF_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 Emf::in_images(PEMF_CALLBACK_DATA d, const 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. 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>.
U_EMRCREATEMONOBRUSH records only work when the bitmap is monochrome. If we hit one that isn't
set idx to 2^32-1 and let the caller handle it.
*/
uint32_t Emf::add_image(PEMF_CALLBACK_DATA d, void *pEmr, uint32_t cbBits, uint32_t cbBmi,
uint32_t iUsage, uint32_t offBits, uint32_t offBmi){
uint32_t idx;
char imagename[64]; // big enough
char imrotname[64]; // big enough
char xywh[64]; // big enough
int dibparams = U_BI_UNKNOWN; // type of image not yet determined
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
U_RGBQUAD ct2[2];
uint32_t width, height, colortype, numCt, invert; // if needed these values will be set in get_DIB_params
if(cbBits && cbBmi && (iUsage == U_DIB_RGB_COLORS)){
// next call returns pointers and values, but allocates no memory
dibparams = get_DIB_params((const char *)pEmr, offBits, offBmi, &px, (const U_RGBQUAD **) &ct,
&numCt, &width, &height, &colortype, &invert);
if(dibparams ==U_BI_RGB){
// U_EMRCREATEMONOBRUSH uses text/bk colors instead of what is in the color map.
if(((PU_EMR)pEmr)->iType == U_EMR_CREATEMONOBRUSH){
if(numCt==2){
ct2[0] = U_RGB2BGR(d->dc[d->level].textColor);
ct2[1] = U_RGB2BGR(d->dc[d->level].bkColor);
ct = &ct2[0];
}
else { // This record is invalid, nothing more to do here, let caller handle it
return(U_EMR_INVALID);
}
}
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
)){
toPNG( // Get the image from the RGBA px into mempng
&mempng,
width, height, // of the SRC bitmap
rgba_px
);
free(rgba_px);
}
}
}
gchar *base64String=NULL;
if(dibparams == U_BI_JPEG || dibparams==U_BI_PNG){ // image was binary png or jpg in source file
base64String = g_base64_encode((guchar*) px, numCt );
}
else if(mempng.buffer){ // image was DIB in source file, converted to png in this routine
base64String = g_base64_encode((guchar*) mempng.buffer, mempng.size );
free(mempng.buffer);
}
else { // unknown or unsupported image type or failed conversion, insert the common bad image picture
width = 3;
height = 4;
base64String = bad_image_png();
}
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,"EMFimage%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 += " preserveAspectRatio=\"none\"\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);//wait until this point to free because it might be a duplicate image
/* image allows the inner image to be rotated nicely, load this one second only if needed
imagename retained from above
Here comes a dreadful hack. How do we determine if this rotation of the base image has already
been loaded? The image names contain no identifying information, they are just numbered sequentially.
So the rotated name is EMFrotimage###_XXXXXX, where ### is the number of the referred to image, and
XXXX is the rotation in radians x 1000000 and truncated. That is then stored in BASE64 as the "image".
The corresponding SVG generated though is not for an image, but a reference to an image.
The name of the pattern MUST stil be EMFimage###_ref or output_style() will not be able to use it.
*/
if(current_rotation(d) >= 0.00001 || current_rotation(d) <= -0.00001){ /* some rotation, allow a little rounding error around 0 degrees */
int tangle = round(current_rotation(d)*1000000.0);
sprintf(imrotname,"EMFrotimage%d_%d",idx-1,tangle);
base64String = g_base64_encode((guchar*) imrotname, strlen(imrotname) );
idx = in_images(d, (char *) base64String); // scan for this "image"
if(!idx){
if(d->images.count == d->images.size){ enlarge_images(d); }
idx = d->images.count;
d->images.strings[d->images.count++]=strdup(base64String);
sprintf(imrotname,"EMFimage%d",idx++);
d->defs += "\n";
d->defs += " <pattern\n";
d->defs += " id=\"";
d->defs += imrotname;
d->defs += "_ref\"\n";
d->defs += " xlink:href=\"#";
d->defs += imagename;
d->defs += "_ref\"\n";
d->defs += " patternTransform=";
d->defs += current_matrix(d, 0.0, 0.0, 0); //j use offset 0,0
d->defs += " />\n";
}
g_free(base64String);
}
return(idx-1);
}
/* Add another 100 blank slots to the gradients array.
*/
void Emf::enlarge_gradients(PEMF_CALLBACK_DATA d){
d->gradients.size += 100;
d->gradients.strings = (char **) realloc(d->gradients.strings,d->gradients.size * sizeof(char *));
}
/* See if the gradient name is already in the list. If it is return its position (1->n, not 1-n-1)
*/
int Emf::in_gradients(PEMF_CALLBACK_DATA d, const char *test){
int i;
for(i=0; i<d->gradients.count; i++){
if(strcmp(test,d->gradients.strings[i])==0)return(i+1);
}
return(0);
}
U_COLORREF trivertex_to_colorref(U_TRIVERTEX tv){
U_COLORREF uc;
uc.Red = tv.Red >> 8;
uc.Green = tv.Green >> 8;
uc.Blue = tv.Blue >> 8;
uc.Reserved = tv.Alpha >> 8; // Not used
return(uc);
}
/* (Conditionally) add a gradient. If a matching gradient already exists nothing happens. If one
does not exist it is added to the gradients list and also entered into <defs>.
Only call this with H or V gradient, not a triangle.
*/
uint32_t Emf::add_gradient(PEMF_CALLBACK_DATA d, uint32_t gradientType, U_TRIVERTEX tv1, U_TRIVERTEX tv2){
char hgradname[64]; // big enough
char tmpcolor1[8];
char tmpcolor2[8];
char gradc;
uint32_t idx;
std::string x2,y2;
U_COLORREF gradientColor1 = trivertex_to_colorref(tv1);
U_COLORREF gradientColor2 = trivertex_to_colorref(tv2);
sprintf(tmpcolor1,"%6.6X",sethexcolor(gradientColor1));
sprintf(tmpcolor2,"%6.6X",sethexcolor(gradientColor2));
switch(gradientType){
case U_GRADIENT_FILL_RECT_H:
gradc='H';
x2="100";
y2="0";
break;
case U_GRADIENT_FILL_RECT_V:
gradc='V';
x2="0";
y2="100";
break;
default: // this should never happen, but fill these in to avoid compiler warnings
gradc='!';
x2="0";
y2="0";
break;
}
/* Even though the gradient was defined as Horizontal or Vertical if the rectangle is rotated it needs to
be at some other alignment, and that needs gradienttransform. Set the name using the same sort of hack
as for add_image.
*/
int tangle = round(current_rotation(d)*1000000.0);
sprintf(hgradname,"LinGrd%c_%s_%s_%d",gradc,tmpcolor1,tmpcolor2,tangle);
idx = in_gradients(d,hgradname);
if(!idx){ // gradient does not yet exist
if(d->gradients.count == d->gradients.size){ enlarge_gradients(d); }
d->gradients.strings[d->gradients.count++]=strdup(hgradname);
idx = d->gradients.count;
SVGOStringStream stmp;
stmp << " <linearGradient id=\"";
stmp << hgradname;
stmp << "\" x1=\"";
stmp << pix_to_x_point(d, tv1.x , tv1.y);
stmp << "\" y1=\"";
stmp << pix_to_y_point(d, tv1.x , tv1.y);
stmp << "\" x2=\"";
if(gradc=='H'){ // UR corner
stmp << pix_to_x_point(d, tv2.x , tv1.y);
stmp << "\" y2=\"";
stmp << pix_to_y_point(d, tv2.x , tv1.y);
}
else { // LL corner
stmp << pix_to_x_point(d, tv1.x , tv2.y);
stmp << "\" y2=\"";
stmp << pix_to_y_point(d, tv1.x , tv2.y);
}
stmp << "\" gradientTransform=\"(1,0,0,1,0,0)\"";
stmp << " gradientUnits=\"userSpaceOnUse\"\n";
stmp << ">\n";
stmp << " <stop offset=\"0\" style=\"stop-color:#";
stmp << tmpcolor1;
stmp << ";stop-opacity:1\" />\n";
stmp << " <stop offset=\"1\" style=\"stop-color:#";
stmp << tmpcolor2;
stmp << ";stop-opacity:1\" />\n";
stmp << " </linearGradient>\n";
d->defs += stmp.str().c_str();
}
return(idx-1);
}
/* Add another 100 blank slots to the clips array.
*/
void Emf::enlarge_clips(PEMF_CALLBACK_DATA d){
d->clips.size += 100;
d->clips.strings = (char **) realloc(d->clips.strings,d->clips.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 Emf::in_clips(PEMF_CALLBACK_DATA d, const char *test){
int i;
for(i=0; i<d->clips.count; i++){
if(strcmp(test,d->clips.strings[i])==0)return(i+1);
}
return(0);
}
/* (Conditionally) add a clip.
If a matching clip already exists nothing happens
If one does exist it is added to the clips list, entered into <defs>.
*/
void Emf::add_clips(PEMF_CALLBACK_DATA d, const char *clippath, unsigned int logic){
int op = combine_ops_to_livarot(logic);
Geom::PathVector combined_vect;
char *combined = NULL;
if (op >= 0 && d->dc[d->level].clip_id) {
unsigned int real_idx = d->dc[d->level].clip_id - 1;
Geom::PathVector old_vect = sp_svg_read_pathv(d->clips.strings[real_idx]);
Geom::PathVector new_vect = sp_svg_read_pathv(clippath);
combined_vect = sp_pathvector_boolop(new_vect, old_vect, (bool_op) op , (FillRule) fill_oddEven, (FillRule) fill_oddEven);
combined = sp_svg_write_path(combined_vect);
}
else {
combined = strdup(clippath); // COPY operation, erases everything and starts a new one
}
uint32_t idx = in_clips(d, combined);
if(!idx){ // add clip if not already present
if(d->clips.count == d->clips.size){ enlarge_clips(d); }
d->clips.strings[d->clips.count++]=strdup(combined);
d->dc[d->level].clip_id = d->clips.count; // one more than the slot where it is actually stored
SVGOStringStream tmp_clippath;
tmp_clippath << "\n<clipPath";
tmp_clippath << "\n\tclipPathUnits=\"userSpaceOnUse\" ";
tmp_clippath << "\n\tid=\"clipEmfPath" << d->dc[d->level].clip_id << "\"";
tmp_clippath << " >";
tmp_clippath << "\n\t<path d=\"";
tmp_clippath << combined;
tmp_clippath << "\"";
tmp_clippath << "\n\t/>";
tmp_clippath << "\n</clipPath>";
d->outdef += tmp_clippath.str().c_str();
}
else {
d->dc[d->level].clip_id = idx;
}
free(combined);
}
void
Emf::output_style(PEMF_CALLBACK_DATA d, int iType)
{
// 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_EMR_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: // invert pattern
fill_rgb[0] = 1.0 - fill_rgb[0];
fill_rgb[1] = 1.0 - fill_rgb[1];
fill_rgb[2] = 1.0 - fill_rgb[2];
break;
case U_SRCINVERT: // treat all of these as black
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_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:
case U_PATCOPY:
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 (iType == U_EMR_STROKEPATH || !d->dc[d->level].fill_set) {
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(#EMFimage%d_ref); ",d->dc[d->level].fill_idx);
tmp_style << tmp;
break;
case DRAW_LINEAR_GRADIENT:
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 (iType == U_EMR_FILLPATH || !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(#EMFimage%d_ref); ",d->dc[d->level].stroke_idx);
tmp_style << tmp;
break;
case DRAW_LINEAR_GRADIENT:
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;
}
tmp_style << "stroke-width:" <<
MAX( 0.001, d->dc[d->level].style.stroke_width.value ) << "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_dasharray.values.empty() )
{
tmp_style << "stroke-dasharray:";
for (unsigned i=0; i<d->dc[d->level].style.stroke_dasharray.values.size(); i++) {
if (i)
tmp_style << ",";
tmp_style << d->dc[d->level].style.stroke_dasharray.values[i];
}
tmp_style << ";";
tmp_style << "stroke-dashoffset:0;";
} else {
tmp_style << "stroke-dasharray:none;";
}
tmp_style << "stroke-opacity:1;";
}
tmp_style << "\" ";
if (d->dc[d->level].clip_id)
tmp_style << "\n\tclip-path=\"url(#clipEmfPath" << d->dc[d->level].clip_id << ")\" ";
d->outsvg += tmp_style.str().c_str();
}
double
Emf::_pix_x_to_point(PEMF_CALLBACK_DATA d, double px)
{
double scale = (d->dc[d->level].ScaleInX ? d->dc[d->level].ScaleInX : 1.0);
double tmp;