-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEmf2Pdf.cpp
More file actions
1530 lines (1466 loc) · 42.1 KB
/
Emf2Pdf.cpp
File metadata and controls
1530 lines (1466 loc) · 42.1 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
#include <locale.h>
#include "Emf2Pdf.h"
//#define _CONSOLE_DEBUG
#if defined(_CONSOLE_DEBUG)
#define PRINT_DBG printf
const char* emfNames[] = {
"EMR_HEADER", /*1*/
"EMR_POLYBEZIER", /*2*/
"EMR_POLYGON", /*3*/
"EMR_POLYLINE", /*4*/
"EMR_POLYBEZIERTO", /*5*/
"EMR_POLYLINETO", /*6*/
"EMR_POLYPOLYLINE", /*7*/
"EMR_POLYPOLYGON", /*8*/
"EMR_SETWINDOWEXTEX", /*9*/
"EMR_SETWINDOWORGEX", /*10*/
"EMR_SETVIEWPORTEXTEX", /*11*/
"EMR_SETVIEWPORTORGEX", /*12*/
"EMR_SETBRUSHORGEX", /*13*/
"EMR_EOF", /*14*/
"EMR_SETPIXELV", /*15*/
"EMR_SETMAPPERFLAGS", /*16*/
"EMR_SETMAPMODE", /*17*/
"EMR_SETBKMODE", /*18*/
"EMR_SETPOLYFILLMODE", /*19*/
"EMR_SETROP2", /*20*/
"EMR_SETSTRETCHBLTMODE", /*21*/
"EMR_SETTEXTALIGN", /*22*/
"EMR_SETCOLORADJUSTMENT", /*23*/
"EMR_SETTEXTCOLOR", /*24*/
"EMR_SETBKCOLOR", /*25*/
"EMR_OFFSETCLIPRGN", /*26*/
"EMR_MOVETOEX", /*27*/
"EMR_SETMETARGN", /*28*/
"EMR_EXCLUDECLIPRECT", /*29*/
"EMR_INTERSECTCLIPRECT", /*30*/
"EMR_SCALEVIEWPORTEXTEX", /*31*/
"EMR_SCALEWINDOWEXTEX", /*32*/
"EMR_SAVEDC", /*33*/
"EMR_RESTOREDC", /*34*/
"EMR_SETWORLDTRANSFORM", /*35*/
"EMR_MODIFYWORLDTRANSFORM", /*36*/
"EMR_SELECTOBJECT", /*37*/
"EMR_CREATEPEN", /*38*/
"EMR_CREATEBRUSHINDIRECT", /*39*/
"EMR_DELETEOBJECT", /*40*/
"EMR_ANGLEARC", /*41*/
"EMR_ELLIPSE", /*42*/
"EMR_RECTANGLE", /*43*/
"EMR_ROUNDRECT", /*44*/
"EMR_ARC", /*45*/
"EMR_CHORD", /*46*/
"EMR_PIE", /*47*/
"EMR_SELECTPALETTE", /*48*/
"EMR_CREATEPALETTE", /*49*/
"EMR_SETPALETTEENTRIES", /*50*/
"EMR_RESIZEPALETTE", /*51*/
"EMR_REALIZEPALETTE", /*52*/
"EMR_EXTFLOODFILL", /*53*/
"EMR_LINETO", /*54*/
"EMR_ARCTO", /*55*/
"EMR_POLYDRAW", /*56*/
"EMR_SETARCDIRECTION", /*57*/
"EMR_SETMITERLIMIT", /*58*/
"EMR_BEGINPATH", /*59*/
"EMR_ENDPATH", /*60*/
"EMR_CLOSEFIGURE", /*61*/
"EMR_FILLPATH", /*62*/
"EMR_STROKEANDFILLPATH", /*63*/
"EMR_STROKEPATH", /*64*/
"EMR_FLATTENPATH", /*65*/
"EMR_WIDENPATH", /*66*/
"EMR_SELECTCLIPPATH", /*67*/
"EMR_ABORTPATH", /*68*/
"RESERVED_69",/*69*/
"EMR_GDICOMMENT", /*70*/
"EMR_FILLRGN", /*71*/
"EMR_FRAMERGN", /*72*/
"EMR_INVERTRGN", /*73*/
"EMR_PAINTRGN", /*74*/
"EMR_EXTSELECTCLIPRGN", /*75*/
"EMR_BITBLT", /*76*/
"EMR_STRETCHBLT", /*77*/
"EMR_MASKBLT", /*78*/
"EMR_PLGBLT", /*79*/
"EMR_SETDIBITSTODEVICE", /*80*/
"EMR_STRETCHDIBITS", /*81*/
"EMR_EXTCREATEFONTINDIRECTW", /*82*/
"EMR_EXTTEXTOUTA", /*83*/
"EMR_EXTTEXTOUTW", /*84*/
"EMR_POLYBEZIER16", /*85*/
"EMR_POLYGON16", /*86*/
"EMR_POLYLINE16", /*87*/
"EMR_POLYBEZIERTO16", /*88*/
"EMR_POLYLINETO16", /*89*/
"EMR_POLYPOLYLINE16", /*90*/
"EMR_POLYPOLYGON16", /*91*/
"EMR_POLYDRAW16", /*92*/
"EMR_CREATEMONOBRUSH", /*93*/
"EMR_CREATEDIBPATTERNBRUSHPT", /*94*/
"EMR_EXTCREATEPEN", /*95*/
"EMR_POLYTEXTOUTA", /*96*/
"EMR_POLYTEXTOUTW", /*97*/
#if(WINVER >= 0x0400)
"EMR_SETICMMODE", /*98*/
"EMR_CREATECOLORSPACE", /*99*/
"EMR_SETCOLORSPACE", /*100*/
"EMR_DELETECOLORSPACE", /*101*/
"EMR_GLSRECORD", /*102*/
"EMR_GLSBOUNDEDRECORD", /*103*/
"EMR_PIXELFORMAT", /*104*/
#endif /* WINVER >= 0x0400 */
#if(WINVER >= 0x0500)
"EMR_RESERVED_105", /*105*/
"EMR_RESERVED_106", /*106*/
"EMR_RESERVED_107", /*107*/
"EMR_RESERVED_108", /*108*/
"EMR_RESERVED_109", /*109*/
"EMR_RESERVED_110", /*110*/
"EMR_COLORCORRECTPALETTE", /*111*/
"EMR_SETICMPROFILEA", /*112*/
"EMR_SETICMPROFILEW", /*113*/
"EMR_ALPHABLEND", /*114*/
"EMR_SETLAYOUT", /*115*/
"EMR_TRANSPARENTBLT", /*116*/
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN2K)
"EMR_RESERVED_117", /*117*/
#endif // (_WIN32_WINNT >= _WIN32_WINNT_WIN2K)
"EMR_GRADIENTFILL", /*118*/
"EMR_SETLINKEDUFIS", /*119*/
"EMR_RESERVED_120", /*120*/
"EMR_COLORMATCHTOTARGETW", /*121*/
"EMR_CREATECOLORSPACEW", /*122*/
#endif
"END"
};
#else
#define PRINT_DBG(...)
#endif
Emf2Pdf::Emf2Pdf(HPDF_Doc pdf)
{
this->pdf = pdf;
HPDF_UseUTFEncodings(pdf);
HPDF_SetCurrentEncoder(pdf, "UTF-8");
created = 0;
this->embedFont = true;
Reset();
}
void Emf2Pdf::Reset()
{
xScale = yScale = scale = 0;
inPath = false;
if (created != 0)
{
free(created);
}
created = 0;
nCreated = 0;
currX = currY = 0;
currentFont = 0;
/*currentFont.font = HPDF_GetFont(pdf, "Helvetica", 0);
currentFont.height = 14;
currentFont.width = 1;
currentFont.fakeBold = currentFont.fakeItalic = false;
currentFont.underline = currentFont.strike = false;
*/
currentPen.r = currentPen.g = currentPen.b = 0.f;
currentPen.width = 1;
currentPen.nDash = 0;
currentPen.join = HPDF_ROUND_JOIN;
currentPen.end = HPDF_ROUND_END;
currentBrush.solid = true;
currentBrush.r = currentBrush.g = currentBrush.b = 1.f;
textAlign = 0;
rText = gText = bText = 0.f;
rTextBck = gTextBck = bTextBck = 1.f;
textBKOpaque = false;
polyEO = false;
}
Emf2Pdf::~Emf2Pdf()
{
Reset();
}
size_t Emf2Pdf::Header()
{
size_t nRead = 0;
RECTL frame;
fread(&frame, sizeof(RECTL), 1, f); //bounds
fread(&frame, sizeof(RECTL), 1, f); // frame
PRINT_DBG(" Page size %ix%i (%ix%i)\r\n", frame.right, frame.bottom, frame.left, frame.top);
page = HPDF_AddPage(pdf);
//The dimensions are in 100ths of millimeter, must be 72ths of inch
// v/100 -> millimeter
// v/100/25.4 --> inch
// v/2540*72 --> 72ths of inch
// v*72/2540 --> 72ths of inch
// v*18/635 --> 72ths of inch
HPDF_Page_SetWidth(page, (HPDF_REAL)((frame.right-frame.left) * 18. / 635.));
HPDF_Page_SetHeight(page, (HPDF_REAL)((frame.bottom-frame.top) * 18. / 635.));
nRead += sizeof(RECTL) * 2;
fseek(f, 32, SEEK_CUR);
SIZEL dev;
fread(&dev, sizeof(SIZEL), 1, f); // frame 20-21
PRINT_DBG(" dev %ix%i\r\n", dev.cx, dev.cy);
xScale = (frame.right * 18.f) / (635.f * dev.cx);
yScale = (frame.bottom * 18.f) / (635.f * dev.cy);
scale = xScale > yScale ? xScale : yScale;
nRead += 32 + sizeof(SIZEL);
currHeight = HPDF_Page_GetHeight(page);
HPDF_Page_SetLineWidth(page, 1);
HPDF_Page_SetRGBStroke(page, 0, 0, 0);
HPDF_Page_SetDash(page, 0, 0, 0);
HPDF_Page_SetLineCap(page, HPDF_ROUND_END);
HPDF_Page_SetLineJoin(page, HPDF_ROUND_JOIN);
HPDF_Page_SetRGBFill(page, 1, 1, 1);
HPDF_Page_SetFontAndSize(page, HPDF_GetFont(pdf, "Helvetica", 0), 12);
return nRead;
}
size_t Emf2Pdf::MoveToEx()
{
POINTL p;
fread(&p, sizeof(POINTL), 1, f); // pos
currX = p.x * xScale;
currY = currHeight - p.y * yScale;
pathStartX = currX;
pathStartY = currY;
PRINT_DBG(" move to %i,%i\r\n", p.x, p.y);
if (inPath)
HPDF_Page_MoveTo(page, currX, currY);
return sizeof(POINTL);
}
size_t Emf2Pdf::LineTo()
{
POINTL p;
fread(&p, sizeof(POINTL), 1, f); // pos
PRINT_DBG(" line to %i,%i\r\n", p.x, p.y);
if (!inPath) HPDF_Page_MoveTo(page, currX, currY);
currX = p.x * xScale;
currY = currHeight - p.y * yScale;
HPDF_Page_LineTo(page, currX, currY);
if (!inPath) HPDF_Page_Stroke(page);
return sizeof(POINTL);
}
size_t Emf2Pdf::PolyBezier(unsigned long code)
{
size_t ret = sizeof(RECTL) + 4;
RECTL r;
fread(&r, sizeof(RECTL), 1, f); // pos
unsigned long i, nPoint, j;
fread(&nPoint, 4, 1, f); // pos
PRINT_DBG(" bezier %i points\r\n", nPoint);
for (i = 0; i < nPoint; i += 3)
{
HPDF_REAL x[3], y[3];
if (code == EMR_POLYBEZIERTO16)
{
POINTS pt[3];
fread(pt, sizeof(POINTS), 3, f);
ret += sizeof(POINTS) * 3;
for (j = 0; j < 3; j++)
{
x[j] = pt[j].x * xScale;
y[j] = currHeight - pt[j].y * yScale;
}
PRINT_DBG(" %i,%i %i,%i %i,%i\r\n", pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x, pt[2].y);
}
else
{
POINTL pt[3];
fread(pt, sizeof(POINTL), 3, f);
ret += sizeof(POINTL) * 3;
for (j = 0; j < 3; j++)
{
x[j] = pt[j].x * xScale;
y[j] = currHeight - pt[j].y * yScale;
}
PRINT_DBG(" %i,%i %i,%i %i,%i\r\n", pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x, pt[2].y);
}
HPDF_Page_CurveTo(page, x[0], y[0], x[1], y[1], x[2], y[2]);
currX = x[2];
currY = y[2];
}
return ret;
}
size_t Emf2Pdf::BeginPath()
{
inPath = true;
return 0;
}
size_t Emf2Pdf::EndPath()
{
inPath = false;
//HPDF_Page_EndPath(page);
return 0;
}
size_t Emf2Pdf::FillPath()
{
if (polyEO)
HPDF_Page_Eofill(page);
else
HPDF_Page_Fill(page);
return 0;
}
size_t Emf2Pdf::StrokePath()
{
HPDF_Page_Stroke(page);
return 0;
}
size_t Emf2Pdf::StrokeAndFillPath()
{
if (polyEO)
HPDF_Page_EofillStroke(page);
else
HPDF_Page_FillStroke(page);
return 0;
}
size_t Emf2Pdf::CloseFigure()
{
if (currX != pathStartX || currY != pathStartY)
{
#if defined(_CONSOLE_DEBUG)
POINTL p;
p.x = (LONG)(pathStartX / xScale);
p.y = (LONG)(currHeight - (pathStartY / yScale));
PRINT_DBG(" line to %i,%i\r\n", p.x, p.y);
#endif
currX = pathStartX;
currY = pathStartY;
HPDF_Page_LineTo(page, currX, currY);
}
return 0;
}
size_t Emf2Pdf::SelectClipPath()
{
// only copy supported
//HPDF_Page_Clip(page);
if (inPath || HPDF_Page_GetGMode(page) == 2)
HPDF_Page_EndPath(page);
return 0;
}
size_t Emf2Pdf::SelectObject()
{
unsigned long obj;
fread(&obj, 4, 1, f);
if (obj >= 0x80000000) {
PRINT_DBG(" Obj 0x%8X\r\n", obj);
switch (obj & 0xFF)
{
case WHITE_BRUSH:
currentBrush.solid = true;
currentBrush.r = currentBrush.g = currentBrush.b = 1.f;
HPDF_Page_SetRGBFill(page, currentBrush.r, currentBrush.g, currentBrush.b);
break;
case LTGRAY_BRUSH:
currentBrush.solid = true;
currentBrush.r = currentBrush.g = currentBrush.b = 0.75f;
HPDF_Page_SetRGBFill(page, currentBrush.r, currentBrush.g, currentBrush.b);
break;
case GRAY_BRUSH:
currentBrush.solid = true;
currentBrush.r = currentBrush.g = currentBrush.b = 0.5f;
HPDF_Page_SetRGBFill(page, currentBrush.r, currentBrush.g, currentBrush.b);
break;
case DKGRAY_BRUSH:
currentBrush.solid = true;
currentBrush.r = currentBrush.g = currentBrush.b = 0.25f;
HPDF_Page_SetRGBFill(page, currentBrush.r, currentBrush.g, currentBrush.b);
break;
case BLACK_BRUSH:
currentBrush.solid = true;
currentBrush.r = currentBrush.g = currentBrush.b = 0.f;
HPDF_Page_SetRGBFill(page, currentBrush.r, currentBrush.g, currentBrush.b);
break;
case NULL_BRUSH:
currentBrush.solid = false;
break;
case WHITE_PEN:
currentPen.width = 1 * scale;
currentPen.r = currentPen.g = currentPen.b = 1.f;
currentPen.nDash = 0;
currentPen.join = HPDF_ROUND_JOIN;
currentPen.end = HPDF_ROUND_END;
HPDF_Page_SetLineWidth(page, currentPen.width*scale);
HPDF_Page_SetRGBStroke(page, currentPen.r, currentPen.g, currentPen.b);
HPDF_Page_SetDash(page, 0, 0, 0);
HPDF_Page_SetLineCap(page, (HPDF_LineCap)currentPen.end);
HPDF_Page_SetLineJoin(page, (HPDF_LineJoin)currentPen.join);
break;
case BLACK_PEN:
currentPen.width = 1 * scale;
currentPen.r = currentPen.g = currentPen.b = 0.f;
currentPen.nDash = 0;
currentPen.join = HPDF_ROUND_JOIN;
currentPen.end = HPDF_ROUND_END;
HPDF_Page_SetLineWidth(page, currentPen.width*scale);
HPDF_Page_SetRGBStroke(page, currentPen.r, currentPen.g, currentPen.b);
HPDF_Page_SetDash(page, 0, 0, 0);
HPDF_Page_SetLineCap(page, (HPDF_LineCap)currentPen.end);
HPDF_Page_SetLineJoin(page, (HPDF_LineJoin)currentPen.join);
break;
case NULL_PEN:
currentPen.width = 0;
currentPen.r = currentPen.g = currentPen.b = 0.f;
break;
case OEM_FIXED_FONT:
case ANSI_FIXED_FONT:
case ANSI_VAR_FONT:
case SYSTEM_FONT:
case DEVICE_DEFAULT_FONT:
case DEFAULT_PALETTE:
case SYSTEM_FIXED_FONT:
case DEFAULT_GUI_FONT:
case DC_BRUSH:
case DC_PEN:
//TODO
break;
}
}
else {
PRINT_DBG(" Obj %i\r\n", obj);
if (obj < nCreated)
{
CreatedItem& itm = created[obj];
switch (itm.type)
{
case OBJ_PEN:
currentPen.width = itm.pen.width;
currentPen.r = itm.pen.r;
currentPen.g = itm.pen.g;
currentPen.b = itm.pen.b;
currentPen.nDash = itm.pen.nDash;
if (currentPen.nDash > 0)
memcpy(currentPen.dash, itm.pen.dash, 2 * itm.pen.nDash);
currentPen.end = itm.pen.end;
currentPen.join = itm.pen.join;
HPDF_Page_SetLineWidth(page, currentPen.width);
HPDF_Page_SetRGBStroke(page, currentPen.r, currentPen.g, currentPen.b);
HPDF_Page_SetDash(page, (const HPDF_UINT16*)currentPen.dash, currentPen.nDash, 0);
HPDF_Page_SetLineCap(page, (HPDF_LineCap)currentPen.end);
HPDF_Page_SetLineJoin(page, (HPDF_LineJoin)currentPen.join);
break;
case OBJ_BRUSH:
currentBrush.solid = itm.brush.solid;
currentBrush.r = itm.brush.r;
currentBrush.g = itm.brush.g;
currentBrush.b = itm.brush.b;
HPDF_Page_SetRGBFill(page, currentBrush.r, currentBrush.g, currentBrush.b);
break;
case OBJ_FONT:
currentFont = &itm.font;
HPDF_Page_SetFontAndSize(page, currentFont->font, currentFont->height);
SetEncoding(itm.font.encoding);
}
}
}
return 4;
}
size_t Emf2Pdf::SetTextColor()
{
unsigned char r, g, b, x;
fread(&r, 1, 1, f);
fread(&g, 1, 1, f);
fread(&b, 1, 1, f);
fread(&x, 1, 1, f);
rText = (float)r / 255.f;
gText = (float)g / 255.f;
bText = (float)b / 255.f;
PRINT_DBG(" color %i %i %i\r\n", r, g, b);
return 4;
}
size_t Emf2Pdf::SetBkColor()
{
unsigned char r, g, b, x;
fread(&r, 1, 1, f);
fread(&g, 1, 1, f);
fread(&b, 1, 1, f);
fread(&x, 1, 1, f);
rTextBck = (float)r / 255.f;
gTextBck = (float)g / 255.f;
bTextBck = (float)b / 255.f;
PRINT_DBG(" color %i %i %i\r\n", r, g, b);
return 4;
}
size_t Emf2Pdf::SetBkMode()
{
long v;
fread(&v, 4, 1, f);
PRINT_DBG(" mode %s\r\n", v==1?"Transparent" : "Opaque");
textBKOpaque = v != 1;
return 4;
}
void Emf2Pdf::SetCreatedIdx(unsigned long idx)
{
if (nCreated == 0)
{
nCreated = idx + 1;
created = (CreatedItem*)(malloc(sizeof(CreatedItem)*(idx + 1)));
}
else if (nCreated <= idx)
{
nCreated = idx + 1;
created = (CreatedItem*)(realloc(created, sizeof(CreatedItem)*(idx + 1)));
}
}
size_t Emf2Pdf::CreatePen()
{
unsigned long idx;
fread(&idx, 4, 1, f);
SetCreatedIdx(idx);
created[idx].type = OBJ_PEN;
unsigned long style, width, h;
fread(&style, 4, 1, f); // temp: ignore
fread(&width, 4, 1, f); // width
created[idx].pen.width = scale * width;
fread(&h, 4, 1, f); // to ignore
unsigned char r, g, b, x;
fread(&r, 1, 1, f);
fread(&g, 1, 1, f);
fread(&b, 1, 1, f);
fread(&x, 1, 1, f);
created[idx].pen.r = (float)r / 255.f;
created[idx].pen.g = (float)g / 255.f;
created[idx].pen.b = (float)b / 255.f;
PRINT_DBG(" %i width %i rgb %i %i %i\r\n", idx, width, r, g, b);
created[idx].pen.nDash = 0;
switch (style&PS_STYLE_MASK)
{
case PS_SOLID:
created[idx].pen.nDash = 0;
break;
case PS_DASH:
created[idx].pen.dash[0] = 5;
created[idx].pen.nDash = 1;
break;
case PS_DOT:
created[idx].pen.dash[0] = 2;
created[idx].pen.nDash = 1;
break;
case PS_DASHDOT:
created[idx].pen.dash[0] = 5;
created[idx].pen.dash[1] = 3;
created[idx].pen.dash[2] = 1;
created[idx].pen.dash[3] = 3;
created[idx].pen.nDash = 4;
break;
case PS_DASHDOTDOT:
created[idx].pen.dash[0] = 5;
created[idx].pen.dash[1] = 3;
created[idx].pen.dash[2] = 1;
created[idx].pen.dash[3] = 3;
created[idx].pen.dash[4] = 1;
created[idx].pen.dash[5] = 3;
created[idx].pen.nDash = 6;
break;
case PS_NULL:
created[idx].pen.width = 0;
break;
}
switch (style & PS_ENDCAP_MASK)
{
case PS_ENDCAP_ROUND:
created[idx].pen.end = HPDF_ROUND_END;
break;
case PS_ENDCAP_SQUARE:
created[idx].pen.end = HPDF_PROJECTING_SCUARE_END;
break;
case PS_ENDCAP_FLAT:
created[idx].pen.end = HPDF_BUTT_END;
break;
}
switch (style & PS_JOIN_MASK)
{
case PS_JOIN_ROUND:
created[idx].pen.join = HPDF_ROUND_JOIN;
break;
case PS_JOIN_BEVEL:
created[idx].pen.join = HPDF_BEVEL_JOIN;
break;
case PS_JOIN_MITER:
created[idx].pen.join = HPDF_MITER_JOIN;
break;
}
return 20;
}
size_t Emf2Pdf::CreateBrush()
{
unsigned long idx;
fread(&idx, 4, 1, f);
SetCreatedIdx(idx);
created[idx].type = OBJ_BRUSH;
unsigned long style;
fread(&style, 4, 1, f); // temp: ignore
unsigned char r, g, b, x;
fread(&r, 1, 1, f);
fread(&g, 1, 1, f);
fread(&b, 1, 1, f);
fread(&x, 1, 1, f);
created[idx].brush.solid = true;
created[idx].brush.r = (float)r / 255.f;
created[idx].brush.g = (float)g / 255.f;
created[idx].brush.b = (float)b / 255.f;
PRINT_DBG(" %i rgb %i %i %i\r\n", idx, r, g, b);
return 12;
}
size_t Emf2Pdf::SetPolyFillMode()
{
unsigned long nMode;
fread(&nMode, 4, 1, f);
polyEO = nMode == 1;
PRINT_DBG(" %s\r\n", polyEO ? "EvenOdd" : "winding");
return 4;
}
int Emf2Pdf::FindFont(wchar_t* faceName, bool bold, bool italic)
{
for (int i = 0; i < nInstalledFont; i++)
{
if (wcswcs(installedFont[i].name, faceName) == 0) continue;
bool isbold = wcswcs(installedFont[i].name, L" bold ");
if (isbold != bold) continue;
bool isitalic = wcswcs(installedFont[i].name, L" italic ");
if (isitalic != italic) continue;
return i;
}
return -1;
}
const char *Emf2Pdf::GetEncoding(unsigned char idx)
{
switch (idx)
{
case DEFAULT_CHARSET: return ("UTF-8"); break;
case SYMBOL_CHARSET: break;
case MAC_CHARSET: return ("MacRomanEncoding"); break;
case SHIFTJIS_CHARSET: return ("90ms-RKSJ-H"); break;
case HANGUL_CHARSET: return ("KSCms-UHC-H"); break;
case JOHAB_CHARSET: break;
case GB2312_CHARSET: break;
case CHINESEBIG5_CHARSET: break;
case THAI_CHARSET: return ("ISO8859-11"); break;
case OEM_CHARSET: break;
case EASTEUROPE_CHARSET: return "CP1250"; //Microsoft Windows Codepage 1250 (EE)
case RUSSIAN_CHARSET: return "CP1251"; //Microsoft Windows Codepage 1251 (Cyrl)
//case RUSSIAN_CHARSET: return ("KOI8-R"); break;
case ANSI_CHARSET: return "CP1252"; //Microsoft Windows Codepage 1252 (ANSI)
case GREEK_CHARSET: return "CP1253"; //Microsoft Windows Codepage 1253 (Greek)
case TURKISH_CHARSET: return "CP1254"; //Microsoft Windows Codepage 1254 (Turk)
case HEBREW_CHARSET: return "CP1255"; //Microsoft Windows Codepage 1255 (Hebr)
case ARABIC_CHARSET: return "CP1256"; //Microsoft Windows Codepage 1256 (Arab)
case BALTIC_CHARSET: return "CP1257"; //Microsoft Windows Codepage 1257 (BaltRim)
case VIETNAMESE_CHARSET: return "CP1258"; //Microsoft Windows Codepage 1258 (Viet)
}
return 0;
}
void Emf2Pdf::SetEncoding(unsigned char idx)
{
const char *name = GetEncoding(idx);
if (name)
{
HPDF_SetCurrentEncoder(pdf, name);
if (memcmp(name, "CP", 2) == 0)
{ //TEMP: Do it better
char tmp[10];
#ifdef _WINDOWS_
strcpy_s(tmp,10, name + 1);
#else
strcpy(tmp, name + 1);
#endif
*tmp = '.';
setlocale(LC_ALL, tmp);
}
}
}
size_t Emf2Pdf::CreatePDFFont()
{
InitInstalledFont();
size_t ret = 4;
unsigned long idx;
fread(&idx, 4, 1, f);
SetCreatedIdx(idx);
long height, width, weight;
fread(&height, 4, 1, f); ret += 4;
fread(&width, 4, 1, f); ret += 4;
fseek(f, 8, SEEK_CUR); ret += 8;
fread(&weight, 4, 1, f); ret += 4;
char cItalic, encoding, underline, strikeOut;
fread(&cItalic, 1, 1, f); ret += 1;
fread(&underline, 1, 1, f); ret += 1;
fread(&strikeOut, 1, 1, f); ret += 1;
fread(&encoding, 1, 1, f); ret += 1;
fseek(f, 4, SEEK_CUR); ret += 4;
wchar_t faceName[32];
fread(faceName, 2, 32, f); ret += 64;
wchar_t *tl = faceName;
while (*tl) *tl++ = towlower(*tl);
bool bold = weight > 550, italic = cItalic > 0;
bool fakeBold = false, fakeItalic = false;
PRINT_DBG(" \"%S\" %s%s%ix%i\r\n", faceName, bold ? "bold " : "", italic ? "italic " : "", height, width);
if (wcscmp(faceName, L"code ean13") == 0)
{
wcscpy_s<32>(faceName, L"ean-13");
}
int i = FindFont(faceName, bold, italic);
if (i < 0)
{
if (bold && italic)
{
i = FindFont(faceName, false, italic);
if (i >= 0) {
fakeBold = true;
}
else {
i = FindFont(faceName, bold, false);
if (i >= 0) {
fakeItalic = true;
}
else {
i = FindFont(faceName, false, false);
fakeItalic = true;
fakeBold = true;
}
}
}
else if (bold || italic)
{
i = FindFont(faceName, false, false);
fakeItalic = italic;
fakeBold = bold;
}
}
if (i >= 0)
{
created[idx].type = OBJ_FONT;
const char* loaded = HPDF_LoadTTFontFromFile(pdf, installedFont[i].path, this->embedFont);
created[idx].font.font = HPDF_GetFont(pdf, loaded, GetEncoding(encoding));
if (height < 0)
{
created[idx].font.height = -height * yScale;
}
else
{
HPDF_INT a = HPDF_Font_GetAscent(created[idx].font.font), d = HPDF_Font_GetDescent(created[idx].font.font);
HPDF_Box bx = HPDF_Font_GetBBox(created[idx].font.font);
float sc = (float)(a - d) / (float)(bx.top - bx.bottom);
if (sc == 0) sc = 1;
created[idx].font.height = height * sc * yScale;
}
if (created[idx].font.height > 100) created[idx].font.height = 100;
HPDF_Page_SetFontAndSize(page, created[idx].font.font, created[idx].font.height);
created[idx].font.width = 1;
if (width != 0)
{
float currWidth; // = (bx.right - bx.left) *created[idx].font.height / (bx.top - bx.bottom);
HPDF_Font_MeasureText(created[idx].font.font, (const HPDF_BYTE*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62,
1e10, created[idx].font.height, 0, 0, HPDF_FALSE, &currWidth);
currWidth /= 62;
created[idx].font.width = width * xScale / currWidth;
}
created[idx].font.fakeBold = fakeBold;
created[idx].font.fakeItalic = fakeItalic;
created[idx].font.underline = underline == 1;
created[idx].font.strike = strikeOut == 1;
created[idx].font.encoding = encoding;
created[idx].font.intalledIdx = i;
}
else
{
PRINT_DBG(" NON TROVATO");
}
return ret;
}
size_t Emf2Pdf::SetTextAlign()
{
unsigned long mode;
fread(&mode, 4, 1, f);
textAlign = (DWORD)(mode & 0xFFFF);
return 4;
}
size_t Emf2Pdf::PDFTextOut(unsigned long code)
{
if (currentFont == 0) return 0;
size_t ret = 20;
fseek(f, 16, SEEK_CUR); //bound and graphics mode
unsigned long gm;
fread(&gm, 4, 1, f);
float font_xScale, font_yScale;
fread(&font_xScale, 4, 1, f); ret += 4;
fread(&font_yScale, 4, 1, f); ret += 4;
POINTL pos;
fread(&pos, 8, 1, f); ret += 8;
float nItalic = currentFont->fakeItalic ? 0.333f : 0;
unsigned long nChar, strOff, dxOff;
RECTL rect;
fread(&nChar, 4, 1, f); ret += 4;
fread(&strOff, 4, 1, f); ret += 4; strOff -= 8;
unsigned long opts;
fread(&opts, 4, 1, f); ret += 4;//opts
fread(&rect, 16, 1, f); ret += 16;
fread(&dxOff, 4, 1, f); ret += 4; dxOff -= 8;
if (strOff > ret) fseek(f, (long)(strOff - ret), SEEK_CUR); ret = strOff;
char *str = (char*)malloc(sizeof(char)*(nChar + 1));
str[nChar] = 0;
size_t ll;
if (code == EMR_EXTTEXTOUTW)
{
wchar_t *strw = (wchar_t*)malloc(sizeof(wchar_t)*(nChar + 1));
strw[nChar] = 0;
fread(strw, sizeof(wchar_t), nChar, f); ret += sizeof(wchar_t)*nChar;
int utf16len = wcslen(strw);
wcstombs_s(&ll, str, nChar + 1, strw, nChar + 1);
if (ll <= 1 && utf16len > 0) {
//memcpy(strw, L"ñÑáéíóúñÑáéíóúñÑáéíóúñÑáéíóú", nChar*2); test with 2 byte unicode... works
//strw[nChar] = 0;
int utf8len = WideCharToMultiByte(CP_UTF8, 0, strw, utf16len, NULL, 0, NULL, NULL);
free(str);
str = (char*)malloc(sizeof(char)*(utf8len + 1));
str[utf8len] = 0;
wcstombs_s(&ll, str, nChar + 1, strw, nChar + 1);
ll = WideCharToMultiByte(CP_UTF8, 0, strw, utf16len, str, utf8len, 0, 0);
if (currentFont->encoding != 1)
{
currentFont->encoding = 1;
const char* loaded = HPDF_LoadTTFontFromFile(pdf, installedFont[currentFont->intalledIdx].path, this->embedFont);
currentFont->font = HPDF_GetFont(pdf, loaded, GetEncoding(currentFont->encoding));
HPDF_Page_SetFontAndSize(page, currentFont->font, currentFont->height);
SetEncoding(currentFont->encoding);
}
}
free(strw);
ll -= 1;
}
else
{
fread(str, 1, nChar, f); ret += nChar;
ll = strlen(str);
}
if (dxOff > ret) fseek(f, (long)(dxOff - ret), SEEK_CUR); ret = dxOff;
PRINT_DBG(" %i,%i -> %s\r\n", pos.x, pos.y, str);
if ((signed)ll <= 0) return ret;
unsigned long* dx = (unsigned long*)malloc(sizeof(unsigned long)*nChar);
fread(dx, sizeof(unsigned long), nChar, f); ret += sizeof(unsigned long)*nChar;
char out[5]; memset(out, 0, 5);
float startX = rect.right * xScale, endX = rect.left * xScale;
float x = pos.x * xScale;
float endW = 1e20f;
float y = currHeight - pos.y * yScale;
float size = HPDF_Page_GetCurrentFontSize(page);
unsigned int i,n,j;
float d;
if ((textAlign & TA_BASELINE) != TA_BASELINE)
{// baseline is the default for haru
if (textAlign & TA_BOTTOM)
{
y -= HPDF_Font_GetDescent(currentFont->font) * size / 1024;
}
else
{
y -= HPDF_Font_GetAscent(currentFont->font) * size / 1024;
}
}
for (int step = 0; step < 2; step++)
{
if (step == 0 && !textBKOpaque && (textAlign & TA_CENTER)!= TA_CENTER)
continue;
if (step == 0 && textBKOpaque)
{
HPDF_Page_SetRGBFill(page, rTextBck, gTextBck, bTextBck);
}
else
{
HPDF_Page_BeginText(page);
HPDF_Page_SetRGBFill(page, rText, gText, bText);
HPDF_Page_SetRGBStroke(page, rText, gText, bText);
HPDF_Page_SetLineWidth(page, size / 20.f);
if (currentFont->fakeBold)
HPDF_Page_SetTextRenderingMode(page, HPDF_FILL_THEN_STROKE);
HPDF_Page_SetTextMatrix(page, currentFont->width, 0, currentFont->width * nItalic, 1, 0, 0);
}
startX = rect.right * xScale, endX = rect.left * xScale;
x = pos.x * xScale;
if ((textAlign & TA_RIGHT)==TA_RIGHT)
{
if (opts & ETO_CLIPPED) x = rect.right * xScale;
endX = x + HPDF_Font_GetUnicodeWidth(currentFont->font, str[ll - 1]) * size / 1000;
endW = -1e20f;
if (opts & ETO_CLIPPED) endW = rect.left * xScale;
for (int i = (int)(ll - 1), j = nChar - 1; i >= 0; i--, j--)
{
//TODO: unicode!
out[0] = str[i];
x -= dx[j] * xScale;
if(step ==1) HPDF_Page_TextOut(page, x, y, out);
if (x < endW)
break;
}
startX = x;
}
else if((textAlign & TA_CENTER)==TA_CENTER && (step==1))
{
x = startX;
for (i = 0; i < nChar && i < ll; i++)
{
d = 0;
j = (unsigned char)str[i];
if (j >= 0b11110000) n = 4; else
if (j >= 0b11100000) n = 3; else
if (j >= 0b11000000) n = 2; else n = 1;
for (j = 0; j < n; j++, i++) {
out[j] = str[i];
d += dx[i];
}
if (step == 1) HPDF_Page_TextOut(page, x, y, out);
x += d * xScale;
if (x > endW)
{
x -= dx[i] * xScale;
i += 1;
break;
}
}
}
else
{
if (opts & ETO_CLIPPED) x = rect.left * xScale;
startX = x;
endW = 1e20f;
if (opts & ETO_CLIPPED) endW = rect.right * xScale;
//if (step == 1) HPDF_Page_TextOut(page, x, y, str);
for (i = 0; i < nChar && i < ll; i++)
{
d = 0;
j = (unsigned char)str[i];
if (j >= 0b11110000) n = 4; else
if (j >= 0b11100000) n = 3; else
if (j >= 0b11000000) n = 2; else n = 1;
for (j = 0; j < n; j++, i++) {
out[j] = str[i];
d += dx[i];
}
out[n] = 0;
i--;
if (step == 1) HPDF_Page_TextOut(page, x, y, out);
x += d * xScale;
if (x > endW)
{
x -= dx[i] * xScale;
i += 1;
break;
}
}
endX = x; // +HPDF_Font_GetUnicodeWidth(currentFont->font, str[i - 1]) * currentFont->height / 1000;
}
if (step == 0 && (textAlign & TA_CENTER)==TA_CENTER)
{
startX = (endX + startX) / 2;
}
if (step == 0 && textBKOpaque)
{
HPDF_Page_Rectangle(page, startX, y - size *0.2f, (endX-startX), size * 1.2f );
HPDF_Page_Fill(page);
}
}
free(str);
free(dx);
HPDF_Page_EndText(page);