-
Notifications
You must be signed in to change notification settings - Fork 3
/
emf-print.cpp.example
2206 lines (1936 loc) · 90.3 KB
/
emf-print.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 Enhanced Metafile printing
*//*
* Authors:
* Ulf Erikson <[email protected]>
* Jon A. Cruz <[email protected]>
* Abhishek Sharma
* David Mathog
*
* Copyright (C) 2006-2009 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 <string.h>
#include <glibmm/miscutils.h>
#include <libuemf/symbol_convert.h>
#include <2geom/sbasis-to-bezier.h>
#include <2geom/path.h>
#include <2geom/pathvector.h>
#include <2geom/rect.h>
#include <2geom/curves.h>
#include <sp-clippath.h>
#include "helper/geom.h"
#include "helper/geom-curves.h"
#include "sp-item.h"
#include "util/units.h"
#include "style.h"
#include "inkscape-version.h"
#include "sp-root.h"
#include "extension/system.h"
#include "extension/print.h"
#include "document.h"
#include "path-prefix.h"
#include "sp-pattern.h"
#include "sp-image.h"
#include "sp-gradient.h"
#include "sp-radial-gradient.h"
#include "sp-linear-gradient.h"
#include "display/cairo-utils.h"
#include "sp-shape.h"
#include "splivarot.h" // pieces for union on shapes
#include "2geom/svg-path-parser.h" // to get from SVG text to Geom::Path
#include "display/canvas-bpath.h" // for SPWindRule
#include "display/cairo-utils.h" // for Inkscape::Pixbuf::PF_CAIRO
#include "emf-print.h"
namespace Inkscape {
namespace Extension {
namespace Internal {
#define PXPERMETER 2835
/* globals */
static double PX2WORLD;
static bool FixPPTCharPos, FixPPTDashLine, FixPPTGrad2Polys, FixPPTLinGrad, FixPPTPatternAsHatch, FixImageRot;
static EMFTRACK *et = NULL;
static EMFHANDLES *eht = NULL;
void PrintEmf::smuggle_adxkyrtl_out(const char *string, uint32_t **adx, double *ky, int *rtl, int *ndx, float scale)
{
float fdx;
int i;
uint32_t *ladx;
const char *cptr = &string[strlen(string) + 1]; // this works because of the first fake terminator
*adx = NULL;
*ky = 0.0; // set a default value
sscanf(cptr, "%7d", ndx);
if (!*ndx) {
return; // this could happen with an empty string
}
cptr += 7;
ladx = (uint32_t *) malloc(*ndx * sizeof(uint32_t));
if (!ladx) {
g_message("Out of memory");
}
*adx = ladx;
for (i = 0; i < *ndx; i++, cptr += 7, ladx++) {
sscanf(cptr, "%7f", &fdx);
*ladx = (uint32_t) round(fdx * scale);
}
cptr++; // skip 2nd fake terminator
sscanf(cptr, "%7f", &fdx);
*ky = fdx;
cptr += 7; // advance over ky and its space
sscanf(cptr, "%07d", rtl);
}
PrintEmf::PrintEmf()
{
// all of the class variables are initialized elsewhere, many in PrintEmf::Begin,
}
unsigned int PrintEmf::setup(Inkscape::Extension::Print * /*mod*/)
{
return TRUE;
}
unsigned int PrintEmf::begin(Inkscape::Extension::Print *mod, SPDocument *doc)
{
U_SIZEL szlDev, szlMm;
U_RECTL rclBounds, rclFrame;
char *rec;
gchar const *utf8_fn = mod->get_param_string("destination");
// Typically PX2WORLD is 1200/90, using inkscape's default dpi
PX2WORLD = 1200.0 / Inkscape::Util::Quantity::convert(1.0, "in", "px");
FixPPTCharPos = mod->get_param_bool("FixPPTCharPos");
FixPPTDashLine = mod->get_param_bool("FixPPTDashLine");
FixPPTGrad2Polys = mod->get_param_bool("FixPPTGrad2Polys");
FixPPTLinGrad = mod->get_param_bool("FixPPTLinGrad");
FixPPTPatternAsHatch = mod->get_param_bool("FixPPTPatternAsHatch");
FixImageRot = mod->get_param_bool("FixImageRot");
(void) emf_start(utf8_fn, 1000000, 250000, &et); // Initialize the et structure
(void) htable_create(128, 128, &eht); // Initialize the eht structure
char *ansi_uri = (char *) utf8_fn;
// width and height in px
_width = doc->getWidth().value("px");
_height = doc->getHeight().value("px");
_doc_unit_scale = Inkscape::Util::Quantity::convert(1, &doc->getSVGUnit(), "px");
// initialize a few global variables
hbrush = hbrushOld = hpen = 0;
htextalignment = U_TA_BASELINE | U_TA_LEFT;
use_stroke = use_fill = simple_shape = usebk = false;
Inkscape::XML::Node *nv = sp_repr_lookup_name(doc->rroot, "sodipodi:namedview");
if (nv) {
const char *p1 = nv->attribute("pagecolor");
char *p2;
uint32_t lc = strtoul(&p1[1], &p2, 16); // it looks like "#ABC123"
if (*p2) {
lc = 0;
}
gv.bgc = _gethexcolor(lc);
gv.rgb[0] = (float) U_RGBAGetR(gv.bgc) / 255.0;
gv.rgb[1] = (float) U_RGBAGetG(gv.bgc) / 255.0;
gv.rgb[2] = (float) U_RGBAGetB(gv.bgc) / 255.0;
}
bool pageBoundingBox;
pageBoundingBox = mod->get_param_bool("pageBoundingBox");
Geom::Rect d;
if (pageBoundingBox) {
d = Geom::Rect::from_xywh(0, 0, _width, _height);
} else {
SPItem *doc_item = doc->getRoot();
Geom::OptRect bbox = doc_item->desktopVisualBounds();
if (bbox) {
d = *bbox;
}
}
d *= Geom::Scale(Inkscape::Util::Quantity::convert(1, "px", "in"));
float dwInchesX = d.width();
float dwInchesY = d.height();
// dwInchesX x dwInchesY in micrometer units, 1200 dpi/25.4 -> dpmm
(void) drawing_size((int) ceil(dwInchesX * 25.4), (int) ceil(dwInchesY * 25.4),1200.0/25.4, &rclBounds, &rclFrame);
// set up the reference device as 100 X A4 horizontal, (1200 dpi/25.4 -> dpmm). Extra digits maintain dpi better in EMF
int MMX = 216;
int MMY = 279;
(void) device_size(MMX, MMY, 1200.0 / 25.4, &szlDev, &szlMm);
int PixelsX = szlDev.cx;
int PixelsY = szlDev.cy;
// set up the description: (version string)0(file)00
char buff[1024];
memset(buff, 0, sizeof(buff));
char *p1 = strrchr(ansi_uri, '\\');
char *p2 = strrchr(ansi_uri, '/');
char *p = MAX(p1, p2);
if (p) {
p++;
} else {
p = ansi_uri;
}
snprintf(buff, sizeof(buff) - 1, "Inkscape %s (%s)\1%s\1", Inkscape::version_string, __DATE__, p);
uint16_t *Description = U_Utf8ToUtf16le(buff, 0, NULL);
int cbDesc = 2 + wchar16len(Description); // also count the final terminator
(void) U_Utf16leEdit(Description, '\1', '\0'); // swap the temporary \1 characters for nulls
// construct the EMRHEADER record and append it to the EMF in memory
rec = U_EMRHEADER_set(rclBounds, rclFrame, NULL, cbDesc, Description, szlDev, szlMm, 0);
free(Description);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at EMRHEADER");
}
// Simplest mapping mode, supply all coordinates in pixels
rec = U_EMRSETMAPMODE_set(U_MM_TEXT);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at EMRSETMAPMODE");
}
// In earlier versions this was used to scale from inkscape's dpi of 90 to
// the files 1200 dpi, taking into account PX2WORLD which was 20. Now PX2WORLD
// is set so that this matrix is unitary. The usual value of PX2WORLD is 1200/90,
// but might be different if the internal dpi is changed.
U_XFORM worldTransform;
worldTransform.eM11 = 1.0;
worldTransform.eM12 = 0.0;
worldTransform.eM21 = 0.0;
worldTransform.eM22 = 1.0;
worldTransform.eDx = 0;
worldTransform.eDy = 0;
rec = U_EMRMODIFYWORLDTRANSFORM_set(worldTransform, U_MWT_LEFTMULTIPLY);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at EMRMODIFYWORLDTRANSFORM");
}
if (1) {
snprintf(buff, sizeof(buff) - 1, "Screen=%dx%dpx, %dx%dmm", PixelsX, PixelsY, MMX, MMY);
rec = textcomment_set(buff);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at textcomment_set 1");
}
char *oldlocale = g_strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
snprintf(buff, sizeof(buff) - 1, "Drawing=%.1lfx%.1lfpx, %.1lfx%.1lfmm", _width, _height, Inkscape::Util::Quantity::convert(dwInchesX, "in", "mm"), Inkscape::Util::Quantity::convert(dwInchesY, "in", "mm"));
setlocale(LC_NUMERIC, oldlocale);
g_free(oldlocale);
rec = textcomment_set(buff);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at textcomment_set 1");
}
}
/* set some parameters, else the program that reads the EMF may default to other values */
rec = U_EMRSETBKMODE_set(U_TRANSPARENT);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at U_EMRSETBKMODE_set");
}
hpolyfillmode = U_WINDING;
rec = U_EMRSETPOLYFILLMODE_set(U_WINDING);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at U_EMRSETPOLYFILLMODE_set");
}
// Text alignment: (only changed if RTL text is encountered )
// - (x,y) coordinates received by this filter are those of the point where the text
// actually starts, and already takes into account the text object's alignment;
// - for this reason, the EMF text alignment must always be TA_BASELINE|TA_LEFT.
htextalignment = U_TA_BASELINE | U_TA_LEFT;
rec = U_EMRSETTEXTALIGN_set(U_TA_BASELINE | U_TA_LEFT);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at U_EMRSETTEXTALIGN_set");
}
htextcolor_rgb[0] = htextcolor_rgb[1] = htextcolor_rgb[2] = 0.0;
rec = U_EMRSETTEXTCOLOR_set(U_RGB(0, 0, 0));
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at U_EMRSETTEXTCOLOR_set");
}
rec = U_EMRSETROP2_set(U_R2_COPYPEN);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::begin at U_EMRSETROP2_set");
}
/* miterlimit is set with eah pen, so no need to check for it changes as in WMF */
return 0;
}
unsigned int PrintEmf::finish(Inkscape::Extension::Print * /*mod*/)
{
do_clip_if_present(NULL); // Terminate any open clip.
char *rec;
if (!et) {
return 0;
}
// earlier versions had flush of fill here, but it never executed and was removed
rec = U_EMREOF_set(0, NULL, et); // generate the EOF record
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::finish");
}
(void) emf_finish(et, eht); // Finalize and write out the EMF
emf_free(&et); // clean up
htable_free(&eht); // clean up
return 0;
}
unsigned int PrintEmf::comment(
Inkscape::Extension::Print * /*module*/,
const char * /*comment*/)
{
if (!et) {
return 0;
}
// earlier versions had flush of fill here, but it never executed and was removed
return 0;
}
// fcolor is defined when gradients are being expanded, it is the color of one stripe or ring.
int PrintEmf::create_brush(SPStyle const *style, PU_COLORREF fcolor)
{
float rgb[3];
char *rec;
U_LOGBRUSH lb;
uint32_t brush, fmode;
MFDrawMode fill_mode;
Inkscape::Pixbuf *pixbuf;
uint32_t brushStyle;
int hatchType;
U_COLORREF hatchColor;
U_COLORREF bkColor;
uint32_t width = 0; // quiets a harmless compiler warning, initialization not otherwise required.
uint32_t height = 0;
if (!et) {
return 0;
}
// set a default fill in case we can't figure out a better way to do it
fmode = U_ALTERNATE;
fill_mode = DRAW_PAINT;
brushStyle = U_BS_SOLID;
hatchType = U_HS_SOLIDCLR;
bkColor = U_RGB(0, 0, 0);
if (fcolor) {
hatchColor = *fcolor;
} else {
hatchColor = U_RGB(0, 0, 0);
}
if (!fcolor && style) {
if (style->fill.isColor()) {
fill_mode = DRAW_PAINT;
#if 0
// opacity not supported by EMF
float opacity = SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
if (opacity <= 0.0) {
opacity = 0.0; // basically the same as no fill
}
#endif
sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
hatchColor = U_RGB(255 * rgb[0], 255 * rgb[1], 255 * rgb[2]);
fmode = style->fill_rule.computed == 0 ? U_WINDING : (style->fill_rule.computed == 2 ? U_ALTERNATE : U_ALTERNATE);
} else if (SP_IS_PATTERN(SP_STYLE_FILL_SERVER(style))) { // must be paint-server
SPPaintServer *paintserver = style->fill.value.href->getObject();
SPPattern *pat = SP_PATTERN(paintserver);
double dwidth = pat->width();
double dheight = pat->height();
width = dwidth;
height = dheight;
brush_classify(pat, 0, &pixbuf, &hatchType, &hatchColor, &bkColor);
if (pixbuf) {
fill_mode = DRAW_IMAGE;
} else { // pattern
fill_mode = DRAW_PATTERN;
if (hatchType == -1) { // Not a standard hatch, so force it to something
hatchType = U_HS_CROSS;
hatchColor = U_RGB(0xFF, 0xC3, 0xC3);
}
}
if (FixPPTPatternAsHatch) {
if (hatchType == -1) { // image or unclassified
fill_mode = DRAW_PATTERN;
hatchType = U_HS_DIAGCROSS;
hatchColor = U_RGB(0xFF, 0xC3, 0xC3);
}
}
brushStyle = U_BS_HATCHED;
} else if (SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style))) { // must be a gradient
// currently we do not do anything with gradients, the code below just sets the color to the average of the stops
SPPaintServer *paintserver = style->fill.value.href->getObject();
SPLinearGradient *lg = NULL;
SPRadialGradient *rg = NULL;
if (SP_IS_LINEARGRADIENT(paintserver)) {
lg = SP_LINEARGRADIENT(paintserver);
SP_GRADIENT(lg)->ensureVector(); // when exporting from commandline, vector is not built
fill_mode = DRAW_LINEAR_GRADIENT;
} else if (SP_IS_RADIALGRADIENT(paintserver)) {
rg = SP_RADIALGRADIENT(paintserver);
SP_GRADIENT(rg)->ensureVector(); // when exporting from commandline, vector is not built
fill_mode = DRAW_RADIAL_GRADIENT;
} else {
// default fill
}
if (rg) {
if (FixPPTGrad2Polys) {
return hold_gradient(rg, fill_mode);
} else {
hatchColor = avg_stop_color(rg);
}
} else if (lg) {
if (FixPPTGrad2Polys || FixPPTLinGrad) {
return hold_gradient(lg, fill_mode);
} else {
hatchColor = avg_stop_color(lg);
}
}
}
} else { // if (!style)
// default fill
}
lb = logbrush_set(brushStyle, hatchColor, hatchType);
switch (fill_mode) {
case DRAW_LINEAR_GRADIENT: // fill with average color unless gradients are converted to slices
case DRAW_RADIAL_GRADIENT: // ditto
case DRAW_PAINT:
case DRAW_PATTERN:
// SVG text has no background attribute, so OPAQUE mode ALWAYS cancels after the next draw, otherwise it would mess up future text output.
if (usebk) {
rec = U_EMRSETBKCOLOR_set(bkColor);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_brush at U_EMRSETBKCOLOR_set");
}
rec = U_EMRSETBKMODE_set(U_OPAQUE);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_brush at U_EMRSETBKMODE_set");
}
}
rec = createbrushindirect_set(&brush, eht, lb);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_brush at createbrushindirect_set");
}
break;
case DRAW_IMAGE:
char *px;
char *rgba_px;
uint32_t cbPx;
uint32_t colortype;
PU_RGBQUAD ct;
int numCt;
U_BITMAPINFOHEADER Bmih;
PU_BITMAPINFO Bmi;
rgba_px = (char *) pixbuf->pixels(); // Do NOT free this!!!
colortype = U_BCBM_COLOR32;
(void) RGBA_to_DIB(&px, &cbPx, &ct, &numCt, rgba_px, width, height, width * 4, colortype, 0, 1);
// pixbuf can be either PF_CAIRO or PF_GDK, and these have R and B bytes swapped
if (pixbuf->pixelFormat() == Inkscape::Pixbuf::PF_CAIRO) { swapRBinRGBA(px, width * height); }
Bmih = bitmapinfoheader_set(width, height, 1, colortype, U_BI_RGB, 0, PXPERMETER, PXPERMETER, numCt, 0);
Bmi = bitmapinfo_set(Bmih, ct);
rec = createdibpatternbrushpt_set(&brush, eht, U_DIB_RGB_COLORS, Bmi, cbPx, px);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_brush at createdibpatternbrushpt_set");
}
free(px);
free(Bmi); // ct will be NULL because of colortype
break;
}
hbrush = brush; // need this later for destroy_brush
rec = selectobject_set(brush, eht);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_brush at selectobject_set");
}
if (fmode != hpolyfillmode) {
hpolyfillmode = fmode;
rec = U_EMRSETPOLYFILLMODE_set(fmode);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_brush at U_EMRSETPOLYdrawmode_set");
}
}
return 0;
}
void PrintEmf::destroy_brush()
{
char *rec;
// before an object may be safely deleted it must no longer be selected
// select in a stock object to deselect this one, the stock object should
// never be used because we always select in a new one before drawing anythingrestore previous brush, necessary??? Would using a default stock object not work?
rec = selectobject_set(U_NULL_BRUSH, eht);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::destroy_brush at selectobject_set");
}
if (hbrush) {
rec = deleteobject_set(&hbrush, eht);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::destroy_brush");
}
hbrush = 0;
}
}
int PrintEmf::create_pen(SPStyle const *style, const Geom::Affine &transform)
{
U_EXTLOGPEN *elp;
U_NUM_STYLEENTRY n_dash = 0;
U_STYLEENTRY *dash = NULL;
char *rec = NULL;
int linestyle = U_PS_SOLID;
int linecap = 0;
int linejoin = 0;
uint32_t pen;
uint32_t brushStyle;
Inkscape::Pixbuf *pixbuf;
int hatchType;
U_COLORREF hatchColor;
U_COLORREF bkColor;
uint32_t width, height;
char *px = NULL;
char *rgba_px;
uint32_t cbPx = 0;
uint32_t colortype;
PU_RGBQUAD ct = NULL;
int numCt = 0;
U_BITMAPINFOHEADER Bmih;
PU_BITMAPINFO Bmi = NULL;
if (!et) {
return 0;
}
// set a default stroke in case we can't figure out a better way to do it
brushStyle = U_BS_SOLID;
hatchColor = U_RGB(0, 0, 0);
hatchType = U_HS_HORIZONTAL;
bkColor = U_RGB(0, 0, 0);
if (style) {
float rgb[3];
if (SP_IS_PATTERN(SP_STYLE_STROKE_SERVER(style))) { // must be paint-server
SPPaintServer *paintserver = style->stroke.value.href->getObject();
SPPattern *pat = SP_PATTERN(paintserver);
double dwidth = pat->width();
double dheight = pat->height();
width = dwidth;
height = dheight;
brush_classify(pat, 0, &pixbuf, &hatchType, &hatchColor, &bkColor);
if (pixbuf) {
brushStyle = U_BS_DIBPATTERN;
rgba_px = (char *) pixbuf->pixels(); // Do NOT free this!!!
colortype = U_BCBM_COLOR32;
(void) RGBA_to_DIB(&px, &cbPx, &ct, &numCt, rgba_px, width, height, width * 4, colortype, 0, 1);
// pixbuf can be either PF_CAIRO or PF_GDK, and these have R and B bytes swapped
if (pixbuf->pixelFormat() == Inkscape::Pixbuf::PF_CAIRO) { swapRBinRGBA(px, width * height); }
Bmih = bitmapinfoheader_set(width, height, 1, colortype, U_BI_RGB, 0, PXPERMETER, PXPERMETER, numCt, 0);
Bmi = bitmapinfo_set(Bmih, ct);
} else { // pattern
brushStyle = U_BS_HATCHED;
if (usebk) { // OPAQUE mode ALWAYS cancels after the next draw, otherwise it would mess up future text output.
rec = U_EMRSETBKCOLOR_set(bkColor);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_pen at U_EMRSETBKCOLOR_set");
}
rec = U_EMRSETBKMODE_set(U_OPAQUE);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_pen at U_EMRSETBKMODE_set");
}
}
if (hatchType == -1) { // Not a standard hatch, so force it to something
hatchType = U_HS_CROSS;
hatchColor = U_RGB(0xFF, 0xC3, 0xC3);
}
}
if (FixPPTPatternAsHatch) {
if (hatchType == -1) { // image or unclassified
brushStyle = U_BS_HATCHED;
hatchType = U_HS_DIAGCROSS;
hatchColor = U_RGB(0xFF, 0xC3, 0xC3);
}
}
} else if (SP_IS_GRADIENT(SP_STYLE_STROKE_SERVER(style))) { // must be a gradient
// currently we do not do anything with gradients, the code below has no net effect.
SPPaintServer *paintserver = style->stroke.value.href->getObject();
if (SP_IS_LINEARGRADIENT(paintserver)) {
SPLinearGradient *lg = SP_LINEARGRADIENT(paintserver);
SP_GRADIENT(lg)->ensureVector(); // when exporting from commandline, vector is not built
Geom::Point p1(lg->x1.computed, lg->y1.computed);
Geom::Point p2(lg->x2.computed, lg->y2.computed);
if (lg->gradientTransform_set) {
p1 = p1 * lg->gradientTransform;
p2 = p2 * lg->gradientTransform;
}
hatchColor = avg_stop_color(lg);
} else if (SP_IS_RADIALGRADIENT(paintserver)) {
SPRadialGradient *rg = SP_RADIALGRADIENT(paintserver);
SP_GRADIENT(rg)->ensureVector(); // when exporting from commandline, vector is not built
double r = rg->r.computed;
Geom::Point c(rg->cx.computed, rg->cy.computed);
Geom::Point xhandle_point(r, 0);
Geom::Point yhandle_point(0, -r);
yhandle_point += c;
xhandle_point += c;
if (rg->gradientTransform_set) {
c = c * rg->gradientTransform;
yhandle_point = yhandle_point * rg->gradientTransform;
xhandle_point = xhandle_point * rg->gradientTransform;
}
hatchColor = avg_stop_color(rg);
} else {
// default fill
}
} else if (style->stroke.isColor()) { // test last, always seems to be set, even for other types above
sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
brushStyle = U_BS_SOLID;
hatchColor = U_RGB(255 * rgb[0], 255 * rgb[1], 255 * rgb[2]);
hatchType = U_HS_SOLIDCLR;
} else {
// default fill
}
using Geom::X;
using Geom::Y;
Geom::Point zero(0, 0);
Geom::Point one(1, 1);
Geom::Point p0(zero * transform);
Geom::Point p1(one * transform);
Geom::Point p(p1 - p0);
double scale = sqrt((p[X] * p[X]) + (p[Y] * p[Y])) / sqrt(2);
if (!style->stroke_width.computed) {
return 0; //if width is 0 do not (reset) the pen, it should already be NULL_PEN
}
uint32_t linewidth = MAX(1, (uint32_t) round(scale * style->stroke_width.computed * PX2WORLD));
if (style->stroke_linecap.computed == 0) {
linecap = U_PS_ENDCAP_FLAT;
} else if (style->stroke_linecap.computed == 1) {
linecap = U_PS_ENDCAP_ROUND;
} else if (style->stroke_linecap.computed == 2) {
linecap = U_PS_ENDCAP_SQUARE;
}
if (style->stroke_linejoin.computed == 0) {
linejoin = U_PS_JOIN_MITER;
} else if (style->stroke_linejoin.computed == 1) {
linejoin = U_PS_JOIN_ROUND;
} else if (style->stroke_linejoin.computed == 2) {
linejoin = U_PS_JOIN_BEVEL;
}
if (!style->stroke_dasharray.values.empty()) {
if (FixPPTDashLine) { // will break up line into many smaller lines. Override gradient if that was set, cannot do both.
brushStyle = U_BS_SOLID;
hatchType = U_HS_HORIZONTAL;
} else {
unsigned i = 0;
while ((linestyle != U_PS_USERSTYLE) && (i < style->stroke_dasharray.values.size())) {
if (style->stroke_dasharray.values[i] > 0.00000001) {
linestyle = U_PS_USERSTYLE;
}
i++;
}
if (linestyle == U_PS_USERSTYLE) {
n_dash = style->stroke_dasharray.values.size();
dash = new uint32_t[n_dash];
for (i = 0; i < n_dash; i++) {
dash[i] = style->stroke_dasharray.values[i];
}
}
}
}
elp = extlogpen_set(
U_PS_GEOMETRIC | linestyle | linecap | linejoin,
linewidth,
brushStyle,
hatchColor,
hatchType,
n_dash,
dash);
} else { // if (!style)
linejoin = 0;
elp = extlogpen_set(
linestyle,
1,
U_BS_SOLID,
U_RGB(0, 0, 0),
U_HS_HORIZONTAL,
0,
NULL);
}
rec = extcreatepen_set(&pen, eht, Bmi, cbPx, px, elp);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_pen at extcreatepen_set");
}
free(elp);
if (Bmi) {
free(Bmi);
}
if (px) {
free(px); // ct will always be NULL
}
rec = selectobject_set(pen, eht);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_pen at selectobject_set");
}
hpen = pen; // need this later for destroy_pen
if (linejoin == U_PS_JOIN_MITER) {
float miterlimit = style->stroke_miterlimit.value; // This is a ratio.
if (miterlimit < 1) {
miterlimit = 1;
}
rec = U_EMRSETMITERLIMIT_set((uint32_t) miterlimit);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::create_pen at U_EMRSETMITERLIMIT_set");
}
}
if (n_dash) {
delete[] dash;
}
return 0;
}
// set the current pen to the stock object NULL_PEN and then delete the defined pen object, if there is one.
void PrintEmf::destroy_pen()
{
char *rec = NULL;
// before an object may be safely deleted it must no longer be selected
// select in a stock object to deselect this one, the stock object should
// never be used because we always select in a new one before drawing anythingrestore previous brush, necessary??? Would using a default stock object not work?
rec = selectobject_set(U_NULL_PEN, eht);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::destroy_pen at selectobject_set");
}
if (hpen) {
rec = deleteobject_set(&hpen, eht);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::destroy_pen");
}
hpen = 0;
}
}
/* Return a Path consisting of just the corner points of the single path in a a PathVector. If the
PathVector has more than one path, or that one path is open, or any of its segments are curved, then the
returned PathVector is . If the input path is already just straight lines and vertices the output will be the
same as the sole path in the input. */
Geom::Path PrintEmf::pathv_to_simple_polygon(Geom::PathVector const &pathv, int *vertices)
{
Geom::Point P1_trail;
Geom::Point P1;
Geom::Point P1_lead;
Geom::Point v1,v2;
Geom::Path output;
Geom::Path bad;
Geom::PathVector pv = pathv_to_linear_and_cubic_beziers(pathv);
Geom::PathVector::const_iterator pit = pv.begin();
Geom::PathVector::const_iterator pit2 = pv.begin();
++pit2;
*vertices = 0;
if(pit->end_closed() != pit->end_default())return(bad); // path must be closed
if(pit2 != pv.end())return(bad); // there may only be one path
P1_trail = pit->finalPoint();
Geom::Path::const_iterator cit = pit->begin();
P1 = cit->initialPoint();
for(;cit != pit->end_closed();++cit) {
if (!is_straight_curve(*cit)) {
*vertices = 0;
return(bad);
}
P1_lead = cit->finalPoint();
if(Geom::are_near(P1_lead, P1, 1e-5))continue; // duplicate points at the same coordinate
v1 = unit_vector(P1 - P1_trail);
v2 = unit_vector(P1_lead - P1 );
if(Geom::are_near(dot(v1,v2), 1.0, 1e-5)){ // P1 is within a straight line
P1 = P1_lead;
continue;
}
// P1 is the center point of a turn of some angle
if(!*vertices){
output.start( P1 );
output.close( pit->closed() );
}
*vertices += 1;
Geom::LineSegment ls(P1_trail, P1);
output.append(ls);
P1_trail = P1;
P1 = P1_lead;
}
return(output);
}
/* Returns the simplified PathVector (no matter what).
Sets is_rect if it is a rectangle.
Sets angle that will rotate side closest to horizontal onto horizontal.
*/
Geom::Path PrintEmf::pathv_to_rect(Geom::PathVector const &pathv, bool *is_rect, double *angle)
{
Geom::Point P1_trail;
Geom::Point P1;
Geom::Point P1_lead;
Geom::Point v1,v2;
int vertices;
Geom::Path pR = pathv_to_simple_polygon(pathv, &vertices);
*is_rect = false;
if(vertices==4){ // or else it cannot be a rectangle
int vertex_count=0;
/* Get the ends of the LAST line segment.
Find minimum rotation to align rectangle with X,Y axes. (Very degenerate if it is rotated 45 degrees.) */
*angle = 10.0; /* must be > than the actual angle in radians. */
for(Geom::Path::const_iterator cit = pR.begin(); cit != pR.end_open(); ++cit){
P1_trail = cit->initialPoint();
P1 = cit->finalPoint();
v1 = unit_vector(P1 - P1_trail);
if(v1[Geom::X] > 0){ // only check the 1 or 2 points on vectors aimed the same direction as unit X
double ang = asin(v1[Geom::Y]); // because component is rotation by ang of {1,0| vector
if(fabs(ang) < fabs(*angle))*angle = -ang; // y increases down, flips sign on angle
}
}
/* For increased numerical stability, snap the angle to the nearest 1/100th of a degree. */
double convert = 36000.0/ (2.0 * M_PI);
*angle = round(*angle * convert)/convert;
for(Geom::Path::const_iterator cit = pR.begin(); cit != pR.end_open();++cit) {
P1_lead = cit->finalPoint();
v1 = unit_vector(P1 - P1_trail);
v2 = unit_vector(P1_lead - P1 );
// P1 is center of a turn that is not 90 degrees. Limit comes from cos(89.9) = .001745
if(!Geom::are_near(dot(v1,v2), 0.0, 2e-3))break;
P1_trail = P1;
P1 = P1_lead;
vertex_count++;
}
if(vertex_count == 4){
*is_rect=true;
}
}
return(pR);
}
/* Compare a vector with a rectangle's orientation (angle needed to rotate side(s)
closest to horizontal to exactly horizontal) and return:
0 none of the following
1 parallel to horizontal
2 parallel to vertical
3 antiparallel to horizontal
4 antiparallel to vertical
*/
int PrintEmf::vector_rect_alignment(double angle, Geom::Point vtest){
int stat = 0;
Geom::Point v1 = Geom::unit_vector(vtest); // unit vector to test alignment
Geom::Point v2 = Geom::Point(1,0) * Geom::Rotate(-angle); // unit horizontal side (sign change because Y increases DOWN)
if( Geom::are_near(dot(v1,v2), 1.0, 1e-5)){ stat = 1; }
else if(Geom::are_near(dot(v1,v2),-1.0, 1e-5)){ stat = 2; }
if(!stat){
v2 = Geom::Point(0,1) * Geom::Rotate(-angle); // unit vertical side
if( Geom::are_near(dot(v1,v2), 1.0, 1e-5)){ stat = 3; }
else if(Geom::are_near(dot(v1,v2),-1.0, 1e-5)){ stat = 4; }
}
return(stat);
}
/* retrieve the point at the indicated corner:
0 UL (and default)
1 UR
2 LR
3 LL
Needed because the start can be any point, and the direction could run either
clockwise or counterclockwise. This should work even if the corners of the rectangle
are slightly displaced.
*/
Geom::Point PrintEmf::get_pathrect_corner(Geom::Path pathRect, double angle, int corner){
Geom::Point center(0,0);
for(Geom::Path::const_iterator cit = pathRect.begin(); cit != pathRect.end_open(); ++cit) {
center += cit->initialPoint()/4.0;
}
int LR; // 1 if Left, 0 if Right
int UL; // 1 if Lower, 0 if Upper (as viewed on screen, y coordinates increase downwards)
switch(corner){
case 1: //UR
LR = 0;
UL = 0;
break;
case 2: //LR
LR = 0;
UL = 1;
break;
case 3: //LL
LR = 1;
UL = 1;
break;
default: //UL
LR = 1;
UL = 0;
break;
}
Geom::Point v1 = Geom::Point(1,0) * Geom::Rotate(-angle); // unit horizontal side (sign change because Y increases DOWN)
Geom::Point v2 = Geom::Point(0,1) * Geom::Rotate(-angle); // unit vertical side (sign change because Y increases DOWN)
Geom::Point P1;
for(Geom::Path::const_iterator cit = pathRect.begin(); cit != pathRect.end_open(); ++cit) {
P1 = cit->initialPoint();
if ( ( LR == (dot(P1 - center,v1) > 0 ? 0 : 1) )
&& ( UL == (dot(P1 - center,v2) > 0 ? 1 : 0) ) ) break;
}
return(P1);
}
U_TRIVERTEX PrintEmf::make_trivertex(Geom::Point Pt, U_COLORREF uc){
U_TRIVERTEX tv;
using Geom::X;
using Geom::Y;
tv.x = (int32_t) round(Pt[X]);
tv.y = (int32_t) round(Pt[Y]);
tv.Red = uc.Red << 8;
tv.Green = uc.Green << 8;
tv.Blue = uc.Blue << 8;
tv.Alpha = uc.Reserved << 8; // EMF will ignore this
return(tv);
}
/* Examine clip. If there is a (new) one then apply it. If there is one and it is the
same as the preceding one, leave the preceding one active. If style is NULL
terminate the current clip, if any, and return.
*/
void PrintEmf::do_clip_if_present(SPStyle const *style){
char *rec;
static SPClipPath *scpActive = NULL;
if(!style){
if(scpActive){ // clear the existing clip
rec = U_EMRRESTOREDC_set(-1);
if (!rec || emf_append((PU_ENHMETARECORD)rec, et, U_REC_FREE)) {
g_error("Fatal programming error in PrintEmf::fill at U_EMRRESTOREDC_set");
}
scpActive=NULL;
}
} else {
/* The current implementation converts only one level of clipping. If there were more
clips further up the stack they should be combined with the pathvector using "and". Since this
comes up rarely, and would involve a lot of searching (all the way up the stack for every
draw operation), it has not yet been implemented.
Note, to debug this section of code use print statements on sp_svg_write_path(combined_pathvector).
*/
/* find the first clip_ref at object or up the stack. There may not be one. */