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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <algorithm>
#include <string.h>
#include <osl/thread.h>
#include <tools/debug.hxx>
#include <tools/fract.hxx>
#include <tools/stream.hxx>
#include <tools/helpers.hxx>
#include <vcl/dibtools.hxx>
#include <vcl/virdev.hxx>
#include <vcl/graph.hxx>
#include <vcl/lineinfo.hxx>
#include <rtl/strbuf.hxx>
#include <sal/log.hxx>
#include <osl/diagnose.h>
#include <svmconverter.hxx>
#include <memory>
#include <o3tl/make_unique.hxx>
// Inlines
static void ImplReadRect( SvStream& rIStm, tools::Rectangle& rRect )
{
Point aTL;
Point aBR;
ReadPair( rIStm, aTL );
ReadPair( rIStm, aBR );
rRect = tools::Rectangle( aTL, aBR );
}
static bool ImplReadPoly(SvStream& rIStm, tools::Polygon& rPoly)
{
sal_Int32 nSize32(0);
rIStm.ReadInt32(nSize32);
sal_uInt16 nSize = nSize32;
const size_t nMaxPossiblePoints = rIStm.remainingSize() / 2 * sizeof(sal_Int32);
if (nSize > nMaxPossiblePoints)
{
SAL_WARN("vcl.gdi", "svm record claims to have: " << nSize << " points, but only " << nMaxPossiblePoints << " possible");
return false;
}
rPoly = tools::Polygon(nSize);
for (sal_uInt16 i = 0; i < nSize && rIStm.good(); ++i)
ReadPair(rIStm, rPoly[i]);
return rIStm.good();
}
static bool ImplReadPolyPoly(SvStream& rIStm, tools::PolyPolygon& rPolyPoly)
{
bool bSuccess = true;
tools::Polygon aPoly;
sal_Int32 nPolyCount32(0);
rIStm.ReadInt32(nPolyCount32);
sal_uInt16 nPolyCount = static_cast<sal_uInt16>(nPolyCount32);
for (sal_uInt16 i = 0; i < nPolyCount && rIStm.good(); ++i)
{
if (!ImplReadPoly(rIStm, aPoly))
{
bSuccess = false;
break;
}
rPolyPoly.Insert(aPoly);
}
return bSuccess && rIStm.good();
}
static void ImplReadColor( SvStream& rIStm, Color& rColor )
{
sal_Int16 nVal(0);
rIStm.ReadInt16( nVal ); rColor.SetRed( sal::static_int_cast<sal_uInt8>(static_cast<sal_uInt16>(nVal) >> 8) );
rIStm.ReadInt16( nVal ); rColor.SetGreen( sal::static_int_cast<sal_uInt8>(static_cast<sal_uInt16>(nVal) >> 8) );
rIStm.ReadInt16( nVal ); rColor.SetBlue( sal::static_int_cast<sal_uInt8>(static_cast<sal_uInt16>(nVal) >> 8) );
}
static bool ImplReadMapMode(SvStream& rIStm, MapMode& rMapMode)
{
sal_Int16 nUnit(0);
rIStm.ReadInt16(nUnit);
Point aOrg;
ReadPair(rIStm, aOrg);
sal_Int32 nXNum(0), nXDenom(0), nYNum(0), nYDenom(0);
rIStm.ReadInt32(nXNum).ReadInt32(nXDenom).ReadInt32(nYNum).ReadInt32(nYDenom);
if (!rIStm.good() || nXDenom <= 0 || nYDenom <= 0 || nXNum <= 0 || nYNum <= 0)
{
SAL_WARN("vcl.gdi", "Parsing error: invalid mapmode fraction");
return false;
}
if (nUnit < sal_Int16(MapUnit::Map100thMM) || nUnit > sal_Int16(MapUnit::LAST))
{
SAL_WARN("vcl.gdi", "Parsing error: invalid mapmode");
return false;
}
rMapMode = MapMode(static_cast<MapUnit>(nUnit), aOrg, Fraction(nXNum, nXDenom), Fraction(nYNum, nYDenom));
return true;
}
static void ImplReadUnicodeComment( sal_uInt32 nStrmPos, SvStream& rIStm, OUString& rString )
{
sal_uInt32 nOld = rIStm.Tell();
if ( nStrmPos )
{
sal_uInt16 nType;
sal_uInt32 nActionSize;
std::size_t nStringLen;
rIStm.Seek( nStrmPos );
rIStm .ReadUInt16( nType )
.ReadUInt32( nActionSize );
nStringLen = (nActionSize - 4) >> 1;
if ( nStringLen && ( nType == GDI_UNICODE_COMMENT ) )
rString = read_uInt16s_ToOUString(rIStm, nStringLen);
}
rIStm.Seek( nOld );
}
static void ImplSkipActions(SvStream& rIStm, sal_uLong nSkipCount)
{
sal_Int32 nActionSize;
sal_Int16 nType;
for (sal_uLong i = 0; i < nSkipCount; ++i)
{
rIStm.ReadInt16(nType).ReadInt32(nActionSize);
if (!rIStm.good() || nActionSize < 4)
break;
rIStm.SeekRel(nActionSize - 4);
}
}
static void ImplReadExtendedPolyPolygonAction(SvStream& rIStm, tools::PolyPolygon& rPolyPoly)
{
rPolyPoly.Clear();
sal_uInt16 nPolygonCount(0);
rIStm.ReadUInt16( nPolygonCount );
if (!nPolygonCount)
return;
const size_t nMinRecordSize = sizeof(sal_uInt16);
const size_t nMaxRecords = rIStm.remainingSize() / nMinRecordSize;
if (nPolygonCount > nMaxRecords)
{
SAL_WARN("vcl.gdi", "Parsing error: " << nMaxRecords <<
" max possible entries, but " << nPolygonCount << " claimed, truncating");
nPolygonCount = nMaxRecords;
}
for(sal_uInt16 a(0); a < nPolygonCount; a++)
{
sal_uInt16 nPointCount(0);
rIStm.ReadUInt16(nPointCount);
const size_t nMinPolygonSize = sizeof(sal_Int32) * 2;
const size_t nMaxPolygons = rIStm.remainingSize() / nMinPolygonSize;
if (nPointCount > nMaxPolygons)
{
SAL_WARN("vcl.gdi", "Parsing error: " << nMaxPolygons <<
" max possible entries, but " << nPointCount << " claimed, truncating");
nPointCount = nMaxPolygons;
}
tools::Polygon aCandidate(nPointCount);
if (nPointCount)
{
for(sal_uInt16 b(0); b < nPointCount; b++)
{
ReadPair( rIStm , aCandidate[b] );
}
sal_uInt8 bHasFlags(int(false));
rIStm.ReadUChar( bHasFlags );
if(bHasFlags)
{
sal_uInt8 aPolyFlags(0);
for(sal_uInt16 c(0); c < nPointCount; c++)
{
rIStm.ReadUChar( aPolyFlags );
aCandidate.SetFlags(c, static_cast<PolyFlags>(aPolyFlags));
}
}
}
rPolyPoly.Insert(aCandidate);
}
}
SVMConverter::SVMConverter( SvStream& rStm, GDIMetaFile& rMtf )
{
if( !rStm.GetError() )
{
ImplConvertFromSVM1( rStm, rMtf );
}
}
namespace
{
sal_Int32 SkipActions(sal_Int32 i, sal_Int32 nFollowingActionCount, sal_Int32 nActions)
{
sal_Int32 remainingActions = nActions - i;
if (nFollowingActionCount < 0)
nFollowingActionCount = remainingActions;
return std::min(remainingActions, nFollowingActionCount);
}
}
#define LF_FACESIZE 32
void static lcl_error( SvStream& rIStm, const SvStreamEndian& nOldFormat, const sal_uLong& nPos)
{
rIStm.SetError(SVSTREAM_FILEFORMAT_ERROR);
rIStm.SetEndian(nOldFormat);
rIStm.Seek(nPos);
return;
}
void SVMConverter::ImplConvertFromSVM1( SvStream& rIStm, GDIMetaFile& rMtf )
{
const sal_uLong nPos = rIStm.Tell();
const SvStreamEndian nOldFormat = rIStm.GetEndian();
rIStm.SetEndian( SvStreamEndian::LITTLE );
char aCode[ 5 ];
Size aPrefSz;
// read header
rIStm.ReadBytes(aCode, sizeof(aCode)); // Identifier
sal_Int16 nSize(0);
rIStm.ReadInt16( nSize ); // Size
sal_Int16 nVersion(0);
rIStm.ReadInt16( nVersion ); // Version
sal_Int32 nTmp32(0);
rIStm.ReadInt32( nTmp32 );
if (nTmp32 < 0)
{
SAL_WARN("vcl.gdi", "svm: value for width should be positive");
lcl_error(rIStm, nOldFormat, nPos);
return;
}
aPrefSz.setWidth( nTmp32 ); // PrefSize.Width()
rIStm.ReadInt32( nTmp32 );
if (nTmp32 < 0)
{
SAL_WARN("vcl.gdi", "svm: value for height should be positive");
lcl_error(rIStm, nOldFormat, nPos);
return;
}
aPrefSz.setHeight( nTmp32 ); // PrefSize.Height()
// check header-magic and version
if( rIStm.GetError()
|| ( memcmp( aCode, "SVGDI", sizeof( aCode ) ) != 0 )
|| ( nVersion != 200 ) )
{
SAL_WARN("vcl.gdi", "svm: wrong check for header-magic and version");
lcl_error(rIStm, nOldFormat, nPos);
return;
}
LineInfo aLineInfo( LineStyle::NONE, 0 );
std::stack<std::unique_ptr<LineInfo>> aLIStack;
ScopedVclPtrInstance< VirtualDevice > aFontVDev;
rtl_TextEncoding eActualCharSet = osl_getThreadTextEncoding();
bool bFatLine = false;
tools::Polygon aActionPoly;
tools::Rectangle aRect;
Point aPt, aPt1;
Size aSz;
Color aActionColor;
sal_uInt32 nUnicodeCommentStreamPos = 0;
sal_Int32 nUnicodeCommentActionNumber = 0;
rMtf.SetPrefSize(aPrefSz);
MapMode aMapMode;
if (ImplReadMapMode(rIStm, aMapMode)) // MapMode
rMtf.SetPrefMapMode(aMapMode);
sal_Int32 nActions(0);
rIStm.ReadInt32(nActions); // Action count
if (nActions < 0)
{
SAL_WARN("vcl.gdi", "svm claims negative action count (" << nActions << ")");
nActions = 0;
}
const size_t nMinActionSize = sizeof(sal_uInt16) + sizeof(sal_Int32);
const size_t nMaxPossibleActions = rIStm.remainingSize() / nMinActionSize;
if (static_cast<sal_uInt32>(nActions) > nMaxPossibleActions)
{
SAL_WARN("vcl.gdi", "svm claims more actions (" << nActions << ") than stream could provide, truncating");
nActions = nMaxPossibleActions;
}
size_t nLastPolygonAction(0);
for (sal_Int32 i = 0; i < nActions && rIStm.good(); ++i)
{
sal_Int16 nType(0);
rIStm.ReadInt16(nType);
sal_Int32 nActBegin = rIStm.Tell();
sal_Int32 nActionSize(0);
rIStm.ReadInt32(nActionSize);
SAL_WARN_IF( ( nType > 33 ) && ( nType < 1024 ), "vcl.gdi", "Unknown GDIMetaAction while converting!" );
switch( nType )
{
case GDI_PIXEL_ACTION:
{
ReadPair( rIStm, aPt );
ImplReadColor( rIStm, aActionColor );
rMtf.AddAction( new MetaPixelAction( aPt, aActionColor ) );
}
break;
case GDI_POINT_ACTION:
{
ReadPair( rIStm, aPt );
rMtf.AddAction( new MetaPointAction( aPt ) );
}
break;
case GDI_LINE_ACTION:
{
ReadPair( rIStm, aPt );
ReadPair( rIStm, aPt1 );
rMtf.AddAction( new MetaLineAction( aPt, aPt1, aLineInfo ) );
}
break;
case GDI_LINEJOIN_ACTION :
{
sal_Int16 nLineJoin(0);
rIStm.ReadInt16( nLineJoin );
aLineInfo.SetLineJoin(static_cast<basegfx::B2DLineJoin>(nLineJoin));
}
break;
case GDI_LINECAP_ACTION :
{
sal_Int16 nLineCap(0);
rIStm.ReadInt16( nLineCap );
aLineInfo.SetLineCap(static_cast<css::drawing::LineCap>(nLineCap));
}
break;
case GDI_LINEDASHDOT_ACTION :
{
sal_Int16 a(0);
sal_Int32 b(0);
rIStm.ReadInt16( a ); aLineInfo.SetDashCount(a);
rIStm.ReadInt32( b ); aLineInfo.SetDashLen(b);
rIStm.ReadInt16( a ); aLineInfo.SetDotCount(a);
rIStm.ReadInt32( b ); aLineInfo.SetDotLen(b);
rIStm.ReadInt32( b ); aLineInfo.SetDistance(b);
if(((aLineInfo.GetDashCount() && aLineInfo.GetDashLen())
|| (aLineInfo.GetDotCount() && aLineInfo.GetDotLen()))
&& aLineInfo.GetDistance())
{
aLineInfo.SetStyle(LineStyle::Dash);
}
}
break;
case GDI_EXTENDEDPOLYGON_ACTION :
{
// read the tools::PolyPolygon in every case
tools::PolyPolygon aInputPolyPolygon;
ImplReadExtendedPolyPolygonAction(rIStm, aInputPolyPolygon);
// now check if it can be set somewhere
if(nLastPolygonAction < rMtf.GetActionSize())
{
MetaPolyLineAction* pPolyLineAction = dynamic_cast< MetaPolyLineAction* >(rMtf.GetAction(nLastPolygonAction));
if(pPolyLineAction)
{
// replace MetaPolyLineAction when we have a single polygon. Do not rely on the
// same point count; the originally written GDI_POLYLINE_ACTION may have been
// Subdivided for better quality for older usages
if(1 == aInputPolyPolygon.Count())
{
rMtf.ReplaceAction(
new MetaPolyLineAction(
aInputPolyPolygon.GetObject(0),
pPolyLineAction->GetLineInfo()),
nLastPolygonAction);
}
}
else
{
MetaPolyPolygonAction* pPolyPolygonAction = dynamic_cast< MetaPolyPolygonAction* >(rMtf.GetAction(nLastPolygonAction));
if(pPolyPolygonAction)
{
// replace MetaPolyPolygonAction when we have a curved polygon. Do rely on the
// same sub-polygon count
if(pPolyPolygonAction->GetPolyPolygon().Count() == aInputPolyPolygon.Count())
{
rMtf.ReplaceAction(
new MetaPolyPolygonAction(
aInputPolyPolygon),
nLastPolygonAction);
}
}
else
{
MetaPolygonAction* pPolygonAction = dynamic_cast< MetaPolygonAction* >(rMtf.GetAction(nLastPolygonAction));
if(pPolygonAction)
{
// replace MetaPolygonAction
if(1 == aInputPolyPolygon.Count())
{
rMtf.ReplaceAction(
new MetaPolygonAction(
aInputPolyPolygon.GetObject(0)),
nLastPolygonAction);
}
}
}
}
}
}
break;
case GDI_RECT_ACTION:
{
ImplReadRect( rIStm, aRect );
sal_Int32 nTmp(0), nTmp1(0);
rIStm.ReadInt32( nTmp ).ReadInt32( nTmp1 );
if( nTmp || nTmp1 )
rMtf.AddAction( new MetaRoundRectAction( aRect, nTmp, nTmp1 ) );
else
{
rMtf.AddAction( new MetaRectAction( aRect ) );
if( bFatLine )
rMtf.AddAction( new MetaPolyLineAction( aRect, aLineInfo ) );
}
}
break;
case GDI_ELLIPSE_ACTION:
{
ImplReadRect( rIStm, aRect );
if( bFatLine )
{
const tools::Polygon aPoly( aRect.Center(), aRect.GetWidth() >> 1, aRect.GetHeight() >> 1 );
rMtf.AddAction( new MetaPushAction( PushFlags::LINECOLOR ) );
rMtf.AddAction( new MetaLineColorAction( COL_TRANSPARENT, false ) );
rMtf.AddAction( new MetaPolygonAction( aPoly ) );
rMtf.AddAction( new MetaPopAction() );
rMtf.AddAction( new MetaPolyLineAction( aPoly, aLineInfo ) );
}
else
rMtf.AddAction( new MetaEllipseAction( aRect ) );
}
break;
case GDI_ARC_ACTION:
{
ImplReadRect( rIStm, aRect );
ReadPair( rIStm, aPt );
ReadPair( rIStm, aPt1 );
if( bFatLine )
{
const tools::Polygon aPoly( aRect, aPt, aPt1, PolyStyle::Arc );
rMtf.AddAction( new MetaPushAction( PushFlags::LINECOLOR ) );
rMtf.AddAction( new MetaLineColorAction( COL_TRANSPARENT, false ) );
rMtf.AddAction( new MetaPolygonAction( aPoly ) );
rMtf.AddAction( new MetaPopAction() );
rMtf.AddAction( new MetaPolyLineAction( aPoly, aLineInfo ) );
}
else
rMtf.AddAction( new MetaArcAction( aRect, aPt, aPt1 ) );
}
break;
case GDI_PIE_ACTION:
{
ImplReadRect( rIStm, aRect );
ReadPair( rIStm, aPt );
ReadPair( rIStm, aPt1 );
if( bFatLine )
{
const tools::Polygon aPoly( aRect, aPt, aPt1, PolyStyle::Pie );
rMtf.AddAction( new MetaPushAction( PushFlags::LINECOLOR ) );
rMtf.AddAction( new MetaLineColorAction( COL_TRANSPARENT, false ) );
rMtf.AddAction( new MetaPolygonAction( aPoly ) );
rMtf.AddAction( new MetaPopAction() );
rMtf.AddAction( new MetaPolyLineAction( aPoly, aLineInfo ) );
}
else
rMtf.AddAction( new MetaPieAction( aRect, aPt, aPt1 ) );
}
break;
case GDI_INVERTRECT_ACTION:
case GDI_HIGHLIGHTRECT_ACTION:
{
ImplReadRect( rIStm, aRect );
rMtf.AddAction( new MetaPushAction( PushFlags::RASTEROP ) );
rMtf.AddAction( new MetaRasterOpAction( RasterOp::Invert ) );
rMtf.AddAction( new MetaRectAction( aRect ) );
rMtf.AddAction( new MetaPopAction() );
}
break;
case GDI_POLYLINE_ACTION:
{
if (ImplReadPoly(rIStm, aActionPoly))
{
nLastPolygonAction = rMtf.GetActionSize();
if( bFatLine )
rMtf.AddAction( new MetaPolyLineAction( aActionPoly, aLineInfo ) );
else
rMtf.AddAction( new MetaPolyLineAction( aActionPoly ) );
}
}
break;
case GDI_POLYGON_ACTION:
{
if (ImplReadPoly(rIStm, aActionPoly))
{
if( bFatLine )
{
rMtf.AddAction( new MetaPushAction( PushFlags::LINECOLOR ) );
rMtf.AddAction( new MetaLineColorAction( COL_TRANSPARENT, false ) );
rMtf.AddAction( new MetaPolygonAction( aActionPoly ) );
rMtf.AddAction( new MetaPopAction() );
rMtf.AddAction( new MetaPolyLineAction( aActionPoly, aLineInfo ) );
}
else
{
nLastPolygonAction = rMtf.GetActionSize();
rMtf.AddAction( new MetaPolygonAction( aActionPoly ) );
}
}
}
break;
case GDI_POLYPOLYGON_ACTION:
{
tools::PolyPolygon aPolyPoly;
if (ImplReadPolyPoly(rIStm, aPolyPoly))
{
if( bFatLine )
{
rMtf.AddAction( new MetaPushAction( PushFlags::LINECOLOR ) );
rMtf.AddAction( new MetaLineColorAction( COL_TRANSPARENT, false ) );
rMtf.AddAction( new MetaPolyPolygonAction( aPolyPoly ) );
rMtf.AddAction( new MetaPopAction() );
for( sal_uInt16 nPoly = 0, nCount = aPolyPoly.Count(); nPoly < nCount; nPoly++ )
rMtf.AddAction( new MetaPolyLineAction( aPolyPoly[ nPoly ], aLineInfo ) );
}
else
{
nLastPolygonAction = rMtf.GetActionSize();
rMtf.AddAction( new MetaPolyPolygonAction( aPolyPoly ) );
}
}
}
break;
case GDI_FONT_ACTION:
{
vcl::Font aFont;
char aName[LF_FACESIZE+1];
ImplReadColor( rIStm, aActionColor ); aFont.SetColor( aActionColor );
ImplReadColor( rIStm, aActionColor ); aFont.SetFillColor( aActionColor );
size_t nRet = rIStm.ReadBytes(aName, LF_FACESIZE);
aName[nRet] = 0;
aFont.SetFamilyName( OUString( aName, strlen(aName), rIStm.GetStreamCharSet() ) );
sal_Int32 nWidth(0), nHeight(0);
rIStm.ReadInt32(nWidth).ReadInt32(nHeight);
sal_Int16 nCharOrient(0), nLineOrient(0);
rIStm.ReadInt16(nCharOrient).ReadInt16(nLineOrient);
sal_Int16 nCharSet(0), nFamily(0), nPitch(0), nAlign(0), nWeight(0), nUnderline(0), nStrikeout(0);
rIStm.ReadInt16(nCharSet).ReadInt16(nFamily).ReadInt16(nPitch).ReadInt16(nAlign).ReadInt16(nWeight).ReadInt16(nUnderline).ReadInt16(nStrikeout);
bool bItalic(false), bOutline(false), bShadow(false), bTransparent(false);
rIStm.ReadCharAsBool(bItalic).ReadCharAsBool(bOutline).ReadCharAsBool(bShadow).ReadCharAsBool(bTransparent);
aFont.SetFontSize( Size( nWidth, nHeight ) );
aFont.SetCharSet( static_cast<rtl_TextEncoding>(nCharSet) );
aFont.SetFamily( static_cast<FontFamily>(nFamily) );
aFont.SetPitch( static_cast<FontPitch>(nPitch) );
aFont.SetAlignment( static_cast<FontAlign>(nAlign) );
aFont.SetWeight( ( nWeight == 1 ) ? WEIGHT_LIGHT : ( nWeight == 2 ) ? WEIGHT_NORMAL :
( nWeight == 3 ) ? WEIGHT_BOLD : WEIGHT_DONTKNOW );
aFont.SetUnderline( static_cast<FontLineStyle>(nUnderline) );
aFont.SetStrikeout( static_cast<FontStrikeout>(nStrikeout) );
aFont.SetItalic( bItalic ? ITALIC_NORMAL : ITALIC_NONE );
aFont.SetOutline( bOutline );
aFont.SetShadow( bShadow );
aFont.SetOrientation( nLineOrient );
aFont.SetTransparent( bTransparent );
eActualCharSet = aFont.GetCharSet();
if ( eActualCharSet == RTL_TEXTENCODING_DONTKNOW )
eActualCharSet = osl_getThreadTextEncoding();
rMtf.AddAction( new MetaFontAction( aFont ) );
rMtf.AddAction( new MetaTextAlignAction( aFont.GetAlignment() ) );
rMtf.AddAction( new MetaTextColorAction( aFont.GetColor() ) );
rMtf.AddAction( new MetaTextFillColorAction( aFont.GetFillColor(), !aFont.IsTransparent() ) );
// #106172# Track font relevant data in shadow VDev
aFontVDev->SetFont( aFont );
}
break;
case GDI_TEXT_ACTION:
{
sal_Int32 nIndex(0), nLen(0), nTmp(0);
ReadPair( rIStm, aPt ).ReadInt32( nIndex ).ReadInt32( nLen ).ReadInt32( nTmp );
if (nTmp > 0)
{
OString aByteStr = read_uInt8s_ToOString(rIStm, nTmp);
sal_uInt8 nTerminator = 0;
rIStm.ReadUChar( nTerminator );
SAL_WARN_IF( nTerminator != 0, "vcl.gdi", "expected string to be NULL terminated" );
OUString aStr(OStringToOUString(aByteStr, eActualCharSet));
if ( nUnicodeCommentActionNumber == i )
ImplReadUnicodeComment( nUnicodeCommentStreamPos, rIStm, aStr );
rMtf.AddAction( new MetaTextAction( aPt, aStr, nIndex, nLen ) );
}
if (nActionSize < 24)
rIStm.SetError(SVSTREAM_FILEFORMAT_ERROR);
else
rIStm.Seek(nActBegin + nActionSize);
}
break;
case GDI_TEXTARRAY_ACTION:
{
sal_Int32 nIndex(0), nLen(0), nAryLen(0), nTmp(0);
ReadPair( rIStm, aPt ).ReadInt32( nIndex ).ReadInt32( nLen ).ReadInt32( nTmp ).ReadInt32( nAryLen );
if (nTmp > 0)
{
OString aByteStr = read_uInt8s_ToOString(rIStm, nTmp);
sal_uInt8 nTerminator = 0;
rIStm.ReadUChar( nTerminator );
SAL_WARN_IF( nTerminator != 0, "vcl.gdi", "expected string to be NULL terminated" );
OUString aStr(OStringToOUString(aByteStr, eActualCharSet));
std::unique_ptr<long[]> pDXAry;
sal_Int32 nDXAryLen = 0;
if (nAryLen > 0)
{
const size_t nMinRecordSize = sizeof(sal_Int32);
const size_t nMaxRecords = rIStm.remainingSize() / nMinRecordSize;
if (static_cast<sal_uInt32>(nAryLen) > nMaxRecords)
{
SAL_WARN("vcl.gdi", "Parsing error: " << nMaxRecords <<
" max possible entries, but " << nAryLen << " claimed, truncating");
nAryLen = nMaxRecords;
}
sal_Int32 nStrLen( aStr.getLength() );
nDXAryLen = std::max(nAryLen, nStrLen);
if (nDXAryLen < nLen)
{
//MetaTextArrayAction ctor expects pDXAry to be >= nLen if set, so if this can't
//be achieved, don't read it, it's utterly broken.
SAL_WARN("vcl.gdi", "dxary too short, discarding completely");
rIStm.SeekRel(sizeof(sal_Int32) * nDXAryLen);
nLen = 0;
nIndex = 0;
}
else
{
pDXAry.reset(new long[nDXAryLen]);
for (sal_Int32 j = 0; j < nAryLen; ++j)
{
rIStm.ReadInt32( nTmp );
pDXAry[ j ] = nTmp;
}
// #106172# Add last DX array elem, if missing
if( nAryLen != nStrLen )
{
if (nAryLen+1 == nStrLen && nIndex >= 0)
{
std::unique_ptr<long[]> pTmpAry(new long[nStrLen]);
aFontVDev->GetTextArray( aStr, pTmpAry.get(), nIndex, nLen );
// now, the difference between the
// last and the second last DX array
// is the advancement for the last
// glyph. Thus, to complete our meta
// action's DX array, just add that
// difference to last elem and store
// in very last.
if( nStrLen > 1 )
pDXAry[ nStrLen-1 ] = pDXAry[ nStrLen-2 ] + pTmpAry[ nStrLen-1 ] - pTmpAry[ nStrLen-2 ];
else
pDXAry[ nStrLen-1 ] = pTmpAry[ nStrLen-1 ]; // len=1: 0th position taken to be 0
}
#ifdef DBG_UTIL
else
OSL_FAIL("More than one DX array element missing on SVM import");
#endif
}
}
}
if ( nUnicodeCommentActionNumber == i )
ImplReadUnicodeComment( nUnicodeCommentStreamPos, rIStm, aStr );
rMtf.AddAction( new MetaTextArrayAction( aPt, aStr, pDXAry.get(), nIndex, nLen ) );
}
if (nActionSize < 24)
rIStm.SetError(SVSTREAM_FILEFORMAT_ERROR);
else
rIStm.Seek(nActBegin + nActionSize);
}
break;
case GDI_STRETCHTEXT_ACTION:
{
sal_Int32 nIndex(0), nLen(0), nWidth(0), nTmp(0);
ReadPair( rIStm, aPt ).ReadInt32( nIndex ).ReadInt32( nLen ).ReadInt32( nTmp ).ReadInt32( nWidth );
if (nTmp > 0)
{
OString aByteStr = read_uInt8s_ToOString(rIStm, nTmp);
sal_uInt8 nTerminator = 0;
rIStm.ReadUChar( nTerminator );
SAL_WARN_IF( nTerminator != 0, "vcl.gdi", "expected string to be NULL terminated" );
OUString aStr(OStringToOUString(aByteStr, eActualCharSet));
if ( nUnicodeCommentActionNumber == i )
ImplReadUnicodeComment( nUnicodeCommentStreamPos, rIStm, aStr );
rMtf.AddAction( new MetaStretchTextAction( aPt, nWidth, aStr, nIndex, nLen ) );
}
if (nActionSize < 28)
rIStm.SetError(SVSTREAM_FILEFORMAT_ERROR);
else
rIStm.Seek(nActBegin + nActionSize);
}
break;
case GDI_BITMAP_ACTION:
{
Bitmap aBmp;
ReadPair( rIStm, aPt );
ReadDIB(aBmp, rIStm, true);
rMtf.AddAction( new MetaBmpAction( aPt, aBmp ) );
}
break;
case GDI_BITMAPSCALE_ACTION:
{
Bitmap aBmp;
ReadPair( rIStm, aPt );
ReadPair( rIStm, aSz );
ReadDIB(aBmp, rIStm, true);
rMtf.AddAction( new MetaBmpScaleAction( aPt, aSz, aBmp ) );
}
break;
case GDI_BITMAPSCALEPART_ACTION:
{
Bitmap aBmp;
Size aSz2;
ReadPair( rIStm, aPt );
ReadPair( rIStm, aSz );
ReadPair( rIStm, aPt1 );
ReadPair( rIStm, aSz2 );
ReadDIB(aBmp, rIStm, true);
rMtf.AddAction( new MetaBmpScalePartAction( aPt, aSz, aPt1, aSz2, aBmp ) );
}
break;
case GDI_PEN_ACTION:
{
sal_Int32 nPenWidth;
sal_Int16 nPenStyle;
ImplReadColor( rIStm, aActionColor );
rIStm.ReadInt32( nPenWidth ).ReadInt16( nPenStyle );
aLineInfo.SetStyle( nPenStyle ? LineStyle::Solid : LineStyle::NONE );
aLineInfo.SetWidth( nPenWidth );
bFatLine = nPenStyle && !aLineInfo.IsDefault();
rMtf.AddAction( new MetaLineColorAction( aActionColor, nPenStyle != 0 ) );
}
break;
case GDI_FILLBRUSH_ACTION:
{
sal_Int16 nBrushStyle;
ImplReadColor( rIStm, aActionColor );
rIStm.SeekRel( 6 );
rIStm.ReadInt16( nBrushStyle );
rMtf.AddAction( new MetaFillColorAction( aActionColor, nBrushStyle != 0 ) );
rIStm.SeekRel( 2 );
}
break;
case GDI_MAPMODE_ACTION:
{
if (ImplReadMapMode(rIStm, aMapMode))
{
rMtf.AddAction(new MetaMapModeAction(aMapMode));
// #106172# Track font relevant data in shadow VDev
aFontVDev->SetMapMode(aMapMode);
};
}
break;
case GDI_CLIPREGION_ACTION:
{
vcl::Region aRegion;
sal_Int16 nRegType;
sal_Int16 bIntersect;
bool bClip = false;
rIStm.ReadInt16( nRegType ).ReadInt16( bIntersect );
ImplReadRect( rIStm, aRect );
switch( nRegType )
{
case 0:
break;
case 1:
{
tools::Rectangle aRegRect;
ImplReadRect( rIStm, aRegRect );
aRegion = vcl::Region( aRegRect );
bClip = true;
}
break;
case 2:
{
if (ImplReadPoly(rIStm, aActionPoly))
{
aRegion = vcl::Region( aActionPoly );
bClip = true;
}
}
break;
case 3:
{
bool bSuccess = true;
tools::PolyPolygon aPolyPoly;
sal_Int32 nPolyCount32(0);
rIStm.ReadInt32(nPolyCount32);
sal_uInt16 nPolyCount(nPolyCount32);
for (sal_uInt16 j = 0; j < nPolyCount && rIStm.good(); ++j)
{
if (!ImplReadPoly(rIStm, aActionPoly))
{
bSuccess = false;
break;
}
aPolyPoly.Insert(aActionPoly);
}
if (bSuccess)
{
aRegion = vcl::Region( aPolyPoly );
bClip = true;
}
}
break;
}
if( bIntersect )
aRegion.Intersect( aRect );
rMtf.AddAction( new MetaClipRegionAction( aRegion, bClip ) );
}
break;
case GDI_MOVECLIPREGION_ACTION:
{
sal_Int32 nTmp(0), nTmp1(0);
rIStm.ReadInt32( nTmp ).ReadInt32( nTmp1 );
rMtf.AddAction( new MetaMoveClipRegionAction( nTmp, nTmp1 ) );
}
break;
case GDI_ISECTCLIPREGION_ACTION:
{
ImplReadRect( rIStm, aRect );
rMtf.AddAction( new MetaISectRectClipRegionAction( aRect ) );
}
break;
case GDI_RASTEROP_ACTION:
{
RasterOp eRasterOp;
sal_Int16 nRasterOp;
rIStm.ReadInt16( nRasterOp );
switch( nRasterOp )
{
case 1:
eRasterOp = RasterOp::Invert;
break;
case 4:
case 5:
eRasterOp = RasterOp::Xor;
break;
default:
eRasterOp = RasterOp::OverPaint;
break;
}
rMtf.AddAction( new MetaRasterOpAction( eRasterOp ) );
}
break;
case GDI_PUSH_ACTION:
{
aLIStack.push(o3tl::make_unique<LineInfo>(aLineInfo));
rMtf.AddAction( new MetaPushAction( PushFlags::ALL ) );
// #106172# Track font relevant data in shadow VDev
aFontVDev->Push();
}
break;
case GDI_POP_ACTION:
{
std::unique_ptr<LineInfo> xLineInfo;
if (!aLIStack.empty())
{
xLineInfo = std::move(aLIStack.top());
aLIStack.pop();
}
// restore line info
if (xLineInfo)
{
aLineInfo = *xLineInfo;
xLineInfo.reset();
bFatLine = ( LineStyle::NONE != aLineInfo.GetStyle() ) && !aLineInfo.IsDefault();
}
rMtf.AddAction( new MetaPopAction() );
// #106172# Track font relevant data in shadow VDev
aFontVDev->Pop();
}
break;
case GDI_GRADIENT_ACTION:
{
Color aStartCol;
Color aEndCol;
sal_Int16 nStyle;
sal_Int16 nAngle;
sal_Int16 nBorder;
sal_Int16 nOfsX;
sal_Int16 nOfsY;
sal_Int16 nIntensityStart;
sal_Int16 nIntensityEnd;
ImplReadRect( rIStm, aRect );
rIStm.ReadInt16( nStyle );
ImplReadColor( rIStm, aStartCol );
ImplReadColor( rIStm, aEndCol );
rIStm.ReadInt16( nAngle ).ReadInt16( nBorder ).ReadInt16( nOfsX ).ReadInt16( nOfsY ).ReadInt16( nIntensityStart ).ReadInt16( nIntensityEnd );
Gradient aGrad( static_cast<GradientStyle>(nStyle), aStartCol, aEndCol );
aGrad.SetAngle( nAngle );
aGrad.SetBorder( nBorder );
aGrad.SetOfsX( nOfsX );
aGrad.SetOfsY( nOfsY );
aGrad.SetStartIntensity( nIntensityStart );
aGrad.SetEndIntensity( nIntensityEnd );
rMtf.AddAction( new MetaGradientAction( aRect, aGrad ) );
}
break;
case GDI_TRANSPARENT_COMMENT:
{
tools::PolyPolygon aPolyPoly;
sal_Int32 nFollowingActionCount(0);
sal_Int16 nTrans(0);
ReadPolyPolygon( rIStm, aPolyPoly );
rIStm.ReadInt16( nTrans ).ReadInt32( nFollowingActionCount );
ImplSkipActions( rIStm, nFollowingActionCount );
rMtf.AddAction( new MetaTransparentAction( aPolyPoly, nTrans ) );
i = SkipActions(i, nFollowingActionCount, nActions);
}
break;
case GDI_FLOATTRANSPARENT_COMMENT:
{
GDIMetaFile aMtf;
Point aPos;
Size aSize;
Gradient aGradient;
sal_Int32 nFollowingActionCount(0);
ReadGDIMetaFile( rIStm, aMtf );
ReadPair( rIStm, aPos );
ReadPair( rIStm, aSize );
ReadGradient( rIStm, aGradient );
rIStm.ReadInt32( nFollowingActionCount );
ImplSkipActions( rIStm, nFollowingActionCount );
rMtf.AddAction( new MetaFloatTransparentAction( aMtf, aPos, aSize, aGradient ) );
i = SkipActions(i, nFollowingActionCount, nActions);
}
break;
case GDI_HATCH_COMMENT:
{
tools::PolyPolygon aPolyPoly;
Hatch aHatch;
sal_Int32 nFollowingActionCount(0);
ReadPolyPolygon( rIStm, aPolyPoly );
ReadHatch( rIStm, aHatch );
rIStm.ReadInt32( nFollowingActionCount );
ImplSkipActions( rIStm, nFollowingActionCount );
rMtf.AddAction( new MetaHatchAction( aPolyPoly, aHatch ) );
i = SkipActions(i, nFollowingActionCount, nActions);
}
break;
case GDI_REFPOINT_COMMENT:
{
Point aRefPoint;
bool bSet;
sal_Int32 nFollowingActionCount(0);
ReadPair( rIStm, aRefPoint );
rIStm.ReadCharAsBool( bSet ).ReadInt32( nFollowingActionCount );
ImplSkipActions( rIStm, nFollowingActionCount );
rMtf.AddAction( new MetaRefPointAction( aRefPoint, bSet ) );
i = SkipActions(i, nFollowingActionCount, nActions);
// #106172# Track font relevant data in shadow VDev
if( bSet )
aFontVDev->SetRefPoint( aRefPoint );
else
aFontVDev->SetRefPoint();
}
break;
case GDI_TEXTLINECOLOR_COMMENT:
{
Color aColor;
bool bSet;
sal_Int32 nFollowingActionCount(0);
ReadColor( rIStm, aColor );
rIStm.ReadCharAsBool( bSet ).ReadInt32( nFollowingActionCount );
ImplSkipActions( rIStm, nFollowingActionCount );
rMtf.AddAction( new MetaTextLineColorAction( aColor, bSet ) );
i = SkipActions(i, nFollowingActionCount, nActions);
}
break;
case GDI_TEXTLINE_COMMENT:
{
Point aStartPt;
sal_Int32 nWidth(0);
sal_uInt32 nStrikeout(0);
sal_uInt32 nUnderline(0);
sal_Int32 nFollowingActionCount(0);
ReadPair( rIStm, aStartPt );
rIStm.ReadInt32(nWidth ).ReadUInt32(nStrikeout).ReadUInt32(nUnderline).ReadInt32(nFollowingActionCount);
ImplSkipActions(rIStm, nFollowingActionCount);
rMtf.AddAction( new MetaTextLineAction( aStartPt, nWidth,
static_cast<FontStrikeout>(nStrikeout),
static_cast<FontLineStyle>(nUnderline),
LINESTYLE_NONE ) );
i = SkipActions(i, nFollowingActionCount, nActions);
}
break;
case GDI_GRADIENTEX_COMMENT:
{
tools::PolyPolygon aPolyPoly;
Gradient aGradient;
sal_Int32 nFollowingActionCount(0);
ReadPolyPolygon( rIStm, aPolyPoly );
ReadGradient( rIStm, aGradient );
rIStm.ReadInt32( nFollowingActionCount );
ImplSkipActions( rIStm, nFollowingActionCount );
rMtf.AddAction( new MetaGradientExAction( aPolyPoly, aGradient ) );
i = SkipActions(i, nFollowingActionCount, nActions);
}
break;
case GDI_COMMENT_COMMENT:
{
std::vector<sal_uInt8> aData;
OString aComment = read_uInt16_lenPrefixed_uInt8s_ToOString(rIStm);
sal_Int32 nValue(0);
sal_uInt32 nDataSize(0);
rIStm.ReadInt32(nValue).ReadUInt32(nDataSize);
if (nDataSize)
{
const size_t nMaxPossibleData = rIStm.remainingSize();
if (nDataSize > nMaxPossibleActions)
{
SAL_WARN("vcl.gdi", "svm record claims to have: " << nDataSize << " data, but only " << nMaxPossibleData << " possible");
nDataSize = nMaxPossibleActions;
}
aData.resize(nDataSize);
nDataSize = rIStm.ReadBytes(aData.data(), nDataSize);
}
sal_Int32 nFollowingActionCount(0);
rIStm.ReadInt32(nFollowingActionCount);
ImplSkipActions( rIStm, nFollowingActionCount );
rMtf.AddAction(new MetaCommentAction(aComment, nValue, aData.data(), nDataSize));
i = SkipActions(i, nFollowingActionCount, nActions);
}
break;
case GDI_UNICODE_COMMENT:
{
nUnicodeCommentActionNumber = i + 1;
nUnicodeCommentStreamPos = rIStm.Tell() - 6;
if (nActionSize < 4)
rIStm.SetError(SVSTREAM_FILEFORMAT_ERROR);
else
rIStm.SeekRel(nActionSize - 4);
}
break;
default:
if (nActionSize < 4)
rIStm.SetError(SVSTREAM_FILEFORMAT_ERROR);
else
rIStm.SeekRel(nActionSize - 4);
break;
}
}
rIStm.SetEndian( nOldFormat );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|