summaryrefslogtreecommitdiff
path: root/vcl/source/filter/wmf/winwmf.cxx
blob: e02606e20b8c25a062c51d348cc0f13f2b3d5b08 (plain)
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
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
/* -*- 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 "winmtf.hxx"

#include <boost/scoped_array.hpp>
#include <boost/optional.hpp>
#include <vcl/gdimtf.hxx>
#include <vcl/wmf.hxx>
#include <rtl/crc.h>
#include <rtl/tencinfo.h>
#include <osl/endian.h>
#include <vcl/svapp.hxx>
#include <vcl/dibtools.hxx>

//====================== MS-Windows-defines ===============================

#define W_META_SETBKCOLOR           0x0201
#define W_META_SETBKMODE            0x0102
#define W_META_SETMAPMODE           0x0103
#define W_META_SETROP2              0x0104
#define W_META_SETRELABS            0x0105
#define W_META_SETPOLYFILLMODE      0x0106
#define W_META_SETSTRETCHBLTMODE    0x0107
#define W_META_SETTEXTCHAREXTRA     0x0108
#define W_META_SETTEXTCOLOR         0x0209
#define W_META_SETTEXTJUSTIFICATION 0x020A
#define W_META_SETWINDOWORG         0x020B
#define W_META_SETWINDOWEXT         0x020C
#define W_META_SETVIEWPORTORG       0x020D
#define W_META_SETVIEWPORTEXT       0x020E
#define W_META_OFFSETWINDOWORG      0x020F
#define W_META_SCALEWINDOWEXT       0x0410
#define W_META_OFFSETVIEWPORTORG    0x0211
#define W_META_SCALEVIEWPORTEXT     0x0412
#define W_META_LINETO               0x0213
#define W_META_MOVETO               0x0214
#define W_META_EXCLUDECLIPRECT      0x0415
#define W_META_INTERSECTCLIPRECT    0x0416
#define W_META_ARC                  0x0817
#define W_META_ELLIPSE              0x0418
#define W_META_FLOODFILL            0x0419
#define W_META_PIE                  0x081A
#define W_META_RECTANGLE            0x041B
#define W_META_ROUNDRECT            0x061C
#define W_META_PATBLT               0x061D
#define W_META_SAVEDC               0x001E
#define W_META_SETPIXEL             0x041F
#define W_META_OFFSETCLIPRGN        0x0220
#define W_META_TEXTOUT              0x0521
#define W_META_BITBLT               0x0922
#define W_META_STRETCHBLT           0x0B23
#define W_META_POLYGON              0x0324
#define W_META_POLYLINE             0x0325
#define W_META_ESCAPE               0x0626
#define W_META_RESTOREDC            0x0127
#define W_META_FILLREGION           0x0228
#define W_META_FRAMEREGION          0x0429
#define W_META_INVERTREGION         0x012A
#define W_META_PAINTREGION          0x012B
#define W_META_SELECTCLIPREGION     0x012C
#define W_META_SELECTOBJECT         0x012D
#define W_META_SETTEXTALIGN         0x012E
#define W_META_DRAWTEXT             0x062F
#define W_META_CHORD                0x0830
#define W_META_SETMAPPERFLAGS       0x0231
#define W_META_EXTTEXTOUT           0x0a32
#define W_META_SETDIBTODEV          0x0d33
#define W_META_SELECTPALETTE        0x0234
#define W_META_REALIZEPALETTE       0x0035
#define W_META_ANIMATEPALETTE       0x0436
#define W_META_SETPALENTRIES        0x0037
#define W_META_POLYPOLYGON          0x0538
#define W_META_RESIZEPALETTE        0x0139
#define W_META_DIBBITBLT            0x0940
#define W_META_DIBSTRETCHBLT        0x0b41
#define W_META_DIBCREATEPATTERNBRUSH 0x0142
#define W_META_STRETCHDIB           0x0f43
#define W_META_EXTFLOODFILL         0x0548
#define W_META_RESETDC              0x014C
#define W_META_STARTDOC             0x014D
#define W_META_STARTPAGE            0x004F
#define W_META_ENDPAGE              0x0050
#define W_META_ABORTDOC             0x0052
#define W_META_ENDDOC               0x005E
#define W_META_DELETEOBJECT         0x01f0
#define W_META_CREATEPALETTE        0x00f7
#define W_META_CREATEBRUSH          0x00F8
#define W_META_CREATEPATTERNBRUSH   0x01F9
#define W_META_CREATEPENINDIRECT    0x02FA
#define W_META_CREATEFONTINDIRECT   0x02FB
#define W_META_CREATEBRUSHINDIRECT  0x02FC
#define W_META_CREATEBITMAPINDIRECT 0x02FD
#define W_META_CREATEBITMAP         0x06FE
#define W_META_CREATEREGION         0x06FF

static void GetWinExtMax( const Point& rSource, Rectangle& rPlaceableBound, const sal_Int16 nMapMode )
{
    Point aSource( rSource );
    if ( nMapMode == MM_HIMETRIC )
        aSource.Y() = -rSource.Y();
    if ( aSource.X() < rPlaceableBound.Left() )
        rPlaceableBound.Left() = aSource.X();
    if ( aSource.X() > rPlaceableBound.Right() )
        rPlaceableBound.Right() = aSource.X();
    if ( aSource.Y() < rPlaceableBound.Top() )
        rPlaceableBound.Top() = aSource.Y();
    if ( aSource.Y() > rPlaceableBound.Bottom() )
        rPlaceableBound.Bottom() = aSource.Y();
}

static void GetWinExtMax( const Rectangle& rSource, Rectangle& rPlaceableBound, const sal_Int16 nMapMode )
{
    GetWinExtMax( rSource.TopLeft(), rPlaceableBound, nMapMode );
    GetWinExtMax( rSource.BottomRight(), rPlaceableBound, nMapMode );
}

//=================== Methods of WMFReader ==============================

inline Point WMFReader::ReadPoint()
{
    short nX = 0, nY = 0;
    *pWMF >> nX >> nY;
    return Point( nX, nY );
}

// ------------------------------------------------------------------------

inline Point WMFReader::ReadYX()
{
    short nX = 0, nY = 0;
    *pWMF >> nY >> nX;
    return Point( nX, nY );
}

// ------------------------------------------------------------------------

Rectangle WMFReader::ReadRectangle()
{
    Point aBR, aTL;
    aBR = ReadYX();
    aTL = ReadYX();
    aBR.X()--;
    aBR.Y()--;
    return Rectangle( aTL, aBR );
}

// ------------------------------------------------------------------------

Size WMFReader::ReadYXExt()
{
    short nW=0, nH=0;
    *pWMF >> nH >> nW;
    return Size( nW, nH );
}

// ------------------------------------------------------------------------

void WMFReader::ReadRecordParams( sal_uInt16 nFunc )
{
    switch( nFunc )
    {
        case W_META_SETBKCOLOR:
        {
            pOut->SetBkColor( ReadColor() );
        }
        break;

        case W_META_SETBKMODE:
        {
            sal_uInt16 nDat = 0;
            *pWMF >> nDat;
            pOut->SetBkMode( nDat );
        }
        break;

        // !!!
        case W_META_SETMAPMODE:
        {
            sal_Int16 nMapMode = 0;
            *pWMF >> nMapMode;
            pOut->SetMapMode( nMapMode );
        }
        break;

        case W_META_SETROP2:
        {
            sal_uInt16 nROP2 = 0;
            *pWMF >> nROP2;
            pOut->SetRasterOp( nROP2 );
        }
        break;

        case W_META_SETTEXTCOLOR:
        {
            pOut->SetTextColor( ReadColor() );
        }
        break;

        case W_META_SETWINDOWORG:
        {
            pOut->SetWinOrg( ReadYX() );
        }
        break;

        case W_META_SETWINDOWEXT:
        {
            short nWidth = 0, nHeight = 0;
            *pWMF >> nHeight >> nWidth;
            pOut->SetWinExt( Size( nWidth, nHeight ) );
        }
        break;

        case W_META_OFFSETWINDOWORG:
        {
            short nXAdd = 0, nYAdd = 0;
            *pWMF >> nYAdd >> nXAdd;
            pOut->SetWinOrgOffset( nXAdd, nYAdd );
        }
        break;

        case W_META_SCALEWINDOWEXT:
        {
            short nXNum = 0, nXDenom = 0, nYNum = 0, nYDenom = 0;
            *pWMF >> nYDenom >> nYNum >> nXDenom >> nXNum;
            pOut->ScaleWinExt( (double)nXNum / nXDenom, (double)nYNum / nYDenom );
        }
        break;

        case W_META_SETVIEWPORTORG:
        case W_META_SETVIEWPORTEXT:
        break;

        case W_META_OFFSETVIEWPORTORG:
        {
            short nXAdd = 0, nYAdd = 0;
            *pWMF >> nYAdd >> nXAdd;
            pOut->SetDevOrgOffset( nXAdd, nYAdd );
        }
        break;

        case W_META_SCALEVIEWPORTEXT:
        {
            short nXNum = 0, nXDenom = 0, nYNum = 0, nYDenom = 0;
            *pWMF >> nYDenom >> nYNum >> nXDenom >> nXNum;
            pOut->ScaleDevExt( (double)nXNum / nXDenom, (double)nYNum / nYDenom );
        }
        break;

        case W_META_LINETO:
        {
            pOut->LineTo( ReadYX() );
        }
        break;

        case W_META_MOVETO:
        {
            pOut->MoveTo( ReadYX() );
        }
        break;

        case W_META_INTERSECTCLIPRECT:
        {
            pOut->IntersectClipRect( ReadRectangle() );
        }
        break;

        case W_META_RECTANGLE:
        {
            pOut->DrawRect( ReadRectangle() );
        }
        break;

        case W_META_ROUNDRECT:
        {
            Size aSize( ReadYXExt() );
            pOut->DrawRoundRect( ReadRectangle(), Size( aSize.Width() / 2, aSize.Height() / 2 ) );
        }
        break;

        case W_META_ELLIPSE:
        {
            pOut->DrawEllipse( ReadRectangle() );
        }
        break;

        case W_META_ARC:
        {
            Point aEnd( ReadYX() );
            Point aStart( ReadYX() );
            Rectangle aRect( ReadRectangle() );
            aRect.Justify();
            pOut->DrawArc( aRect, aStart, aEnd );
        }
        break;

        case W_META_PIE:
        {
            Point     aEnd( ReadYX() );
            Point     aStart( ReadYX() );
            Rectangle aRect( ReadRectangle() );
            aRect.Justify();

            // #i73608# OutputDevice deviates from WMF
            // semantics. start==end means full ellipse here.
            if( aStart == aEnd )
                pOut->DrawEllipse( aRect );
            else
                pOut->DrawPie( aRect, aStart, aEnd );
        }
        break;

        case W_META_CHORD:
        {
            Point aEnd( ReadYX() );
            Point aStart( ReadYX() );
            Rectangle aRect( ReadRectangle() );
            aRect.Justify();
            pOut->DrawChord( aRect, aStart, aEnd );
        }
        break;

        case W_META_POLYGON:
        {
            sal_uInt16 nPoints = 0;
            *pWMF >> nPoints;
            Polygon aPoly( nPoints );
            for( sal_uInt16 i = 0; i < nPoints; i++ )
                aPoly[ i ] = ReadPoint();
            pOut->DrawPolygon( aPoly );
        }
        break;

        case W_META_POLYPOLYGON:
        {
            bool bRecordOk = true;
            sal_uInt16 nPolyCount(0);
            // Number of polygons:
            *pWMF >> nPolyCount;
            if (nPolyCount && pWMF->good())
            {
                // Number of points of each polygon. Determine total number of points
                boost::scoped_array<sal_uInt16> xPolygonPointCounts(new sal_uInt16[nPolyCount]);
                sal_uInt16* pnPoints = xPolygonPointCounts.get();
                PolyPolygon aPolyPoly(nPolyCount, nPolyCount);
                sal_uInt16 nPoints = 0;
                for (sal_uInt16 a = 0; a < nPolyCount && pWMF->good(); ++a)
                {
                    *pWMF >> pnPoints[a];

                    if (pnPoints[a] > SAL_MAX_UINT16 - nPoints)
                    {
                        bRecordOk = false;
                        break;
                    }

                    nPoints += pnPoints[a];
                }

                SAL_WARN_IF(!bRecordOk, "vcl.filter", "polypolygon record has more polygons than we can handle");

                bRecordOk &= pWMF->good();

                if (!bRecordOk)
                {
                    pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
                    break;
                }

                // Polygon points are:
                for (sal_uInt16 a = 0; a < nPolyCount && pWMF->good(); ++a)
                {
                    const sal_uInt16 nPointCount(pnPoints[a]);
                    boost::scoped_array<Point> xPolygonPoints(new Point[nPointCount]);
                    Point* pPtAry = xPolygonPoints.get();

                    for(sal_uInt16 b(0); b < nPointCount && pWMF->good(); ++b)
                    {
                        pPtAry[b] = ReadPoint();
                    }

                    aPolyPoly.Insert(Polygon(nPointCount, pPtAry));
                }

                bRecordOk &= pWMF->good();

                if (!bRecordOk)
                {
                    pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
                    break;
                }

                pOut->DrawPolyPolygon( aPolyPoly );
            }
        }
        break;

        case W_META_POLYLINE:
        {
            sal_uInt16 nPoints = 0;
            *pWMF >> nPoints;
            Polygon aPoly( nPoints );
            for(sal_uInt16 i = 0; i < nPoints; i++ )
                aPoly[ i ] = ReadPoint();
            pOut->DrawPolyLine( aPoly );
        }
        break;

        case W_META_SAVEDC:
        {
            pOut->Push();
        }
        break;

        case W_META_RESTOREDC:
        {
            pOut->Pop();
        }
        break;

        case W_META_SETPIXEL:
        {
            const Color aColor = ReadColor();
            pOut->DrawPixel( ReadYX(), aColor );
        }
        break;

        case W_META_OFFSETCLIPRGN:
        {
            pOut->MoveClipRegion( ReadYXExt() );
        }
        break;

        case W_META_TEXTOUT:
        {
            sal_uInt16 nLength = 0;
            *pWMF >> nLength;
            if ( nLength )
            {
                char*   pChar = new char[ ( nLength + 1 ) &~ 1 ];
                pWMF->Read( pChar, ( nLength + 1 ) &~ 1 );
                OUString aText( pChar, nLength, pOut->GetCharSet() );
                delete[] pChar;
                Point aPosition( ReadYX() );
                pOut->DrawText( aPosition, aText );
            }
        }
        break;

        case W_META_EXTTEXTOUT:
        {
            sal_uInt16  nLen = 0, nOptions = 0;
            sal_Int32   nRecordPos, nRecordSize = 0, nOriginalTextLen, nNewTextLen;
            Point       aPosition;
            Rectangle   aRect;
            sal_Int32*  pDXAry = NULL;

            pWMF->SeekRel(-6);
            nRecordPos = pWMF->Tell();
            *pWMF >> nRecordSize;
            pWMF->SeekRel(2);
            aPosition = ReadYX();
            *pWMF >> nLen >> nOptions;

            sal_Int32 nTextLayoutMode = TEXT_LAYOUT_DEFAULT;
            if ( nOptions & ETO_RTLREADING )
                nTextLayoutMode = TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_TEXTORIGIN_LEFT;
            pOut->SetTextLayoutMode( nTextLayoutMode );
            DBG_ASSERT( ( nOptions & ( ETO_PDY | ETO_GLYPH_INDEX ) ) == 0, "SJ: ETO_PDY || ETO_GLYPH_INDEX in WMF" );

            // Nur wenn der Text auch Zeichen enthaelt, macht die Ausgabe Sinn
            if( nLen )
            {
                nOriginalTextLen = nLen;
                if( nOptions & ETO_CLIPPED )
                {
                    const Point aPt1( ReadPoint() );
                    const Point aPt2( ReadPoint() );
                    aRect = Rectangle( aPt1, aPt2 );
                }
                char* pChar = new char[ ( nOriginalTextLen + 1 ) &~ 1 ];
                pWMF->Read( pChar, ( nOriginalTextLen + 1 ) &~ 1 );
                OUString aText( pChar, (sal_uInt16)nOriginalTextLen, pOut->GetCharSet() );// after this conversion the text may contain
                nNewTextLen = aText.getLength();                                          // less character (japanese version), so the
                delete[] pChar;                                                         // dxAry will not fit

                if ( nNewTextLen )
                {
                    sal_uInt32  nMaxStreamPos = nRecordPos + ( nRecordSize << 1 );
                    sal_Int32   nDxArySize =  nMaxStreamPos - pWMF->Tell();
                    sal_Int32   nDxAryEntries = nDxArySize >> 1;
                    bool        bUseDXAry = false;

                    if ( ( ( nDxAryEntries % nOriginalTextLen ) == 0 ) && ( nNewTextLen <= nOriginalTextLen ) )
                    {
                        sal_Int16 nDx = 0, nDxTmp = 0;
                        sal_uInt16 i; //needed just outside the for
                        pDXAry = new sal_Int32[ nNewTextLen ];
                        for (i = 0; i < nNewTextLen; i++ )
                        {
                            if ( pWMF->Tell() >= nMaxStreamPos )
                                break;
                            *pWMF >> nDx;
                            if ( nNewTextLen != nOriginalTextLen )
                            {
                                sal_Unicode nUniChar = aText[i];
                                OString aTmp(&nUniChar, 1, pOut->GetCharSet());
                                if ( aTmp.getLength() > 1 )
                                {
                                    sal_Int32 nDxCount = aTmp.getLength() - 1;
                                    if ( ( ( nDxCount * 2 ) + pWMF->Tell() ) > nMaxStreamPos )
                                        break;
                                    while ( nDxCount-- )
                                    {
                                        *pWMF >> nDxTmp;
                                        nDx = nDx + nDxTmp;
                                    }
                                }
                            }
                            pDXAry[ i ] = nDx;
                        }
                        if ( i == nNewTextLen )
                            bUseDXAry = true;
                    }
                    if ( pDXAry && bUseDXAry )
                        pOut->DrawText( aPosition, aText, pDXAry );
                    else
                        pOut->DrawText( aPosition, aText );
                }
            }
            delete[] pDXAry;

        }
        break;

        case W_META_SELECTOBJECT:
        {
            sal_Int16   nObjIndex = 0;
            *pWMF >> nObjIndex;
            pOut->SelectObject( nObjIndex );
        }
        break;

        case W_META_SETTEXTALIGN:
        {
            sal_uInt16  nAlign = 0;
            *pWMF >> nAlign;
            pOut->SetTextAlign( nAlign );
        }
        break;

        case W_META_BITBLT:
        {
            // 0-3   : nWinROP                      #93454#
            // 4-5   : y offset of source bitmap
            // 6-7   : x offset of source bitmap
            // 8-9   : used height of source bitmap
            // 10-11 : used width  of source bitmap
            // 12-13 : destination position y (in pixel)
            // 14-15 : destination position x (in pixel)
            // 16-17 : dont know
            // 18-19 : Width Bitmap in Pixel
            // 20-21 : Height Bitmap in Pixel
            // 22-23 : bytes per scanline
            // 24    : planes
            // 25    : bitcount

            sal_Int32   nWinROP = 0;
            sal_uInt16  nSx = 0, nSy = 0, nSxe = 0, nSye = 0, nDontKnow = 0, nWidth = 0, nHeight = 0, nBytesPerScan = 0;
            sal_uInt8   nPlanes, nBitCount;

            *pWMF >> nWinROP
                  >> nSy >> nSx >> nSye >> nSxe;
            Point aPoint( ReadYX() );
            *pWMF >> nDontKnow >> nWidth >> nHeight >> nBytesPerScan >> nPlanes >> nBitCount;

            if ( nWidth && nHeight && ( nPlanes == 1 ) && ( nBitCount == 1 ) )
            {
                Bitmap aBmp( Size( nWidth, nHeight ), nBitCount );
                BitmapWriteAccess* pAcc;
                pAcc = aBmp.AcquireWriteAccess();
                if ( pAcc )
                {
                    for (sal_uInt16 y = 0; y < nHeight; y++ )
                    {
                        sal_uInt16 x = 0;
                        for (sal_uInt16 scan = 0; scan < nBytesPerScan; scan++ )
                        {
                            sal_Int8 nEightPixels = 0;
                            *pWMF >> nEightPixels;
                            for (sal_Int8 i = 7; i >= 0; i-- )
                            {
                                if ( x < nWidth )
                                {
                                    pAcc->SetPixelIndex( y, x, (nEightPixels>>i)&1 );
                                }
                                x++;
                            }
                        }
                    }
                    aBmp.ReleaseAccess( pAcc );
                    if ( nSye && nSxe &&
                        ( ( nSx + nSxe ) <= aBmp.GetSizePixel().Width() ) &&
                            ( ( nSy + nSye <= aBmp.GetSizePixel().Height() ) ) )
                    {
                        Rectangle aCropRect( Point( nSx, nSy ), Size( nSxe, nSye ) );
                        aBmp.Crop( aCropRect );
                    }
                    Rectangle aDestRect( aPoint, Size( nSxe, nSye ) );
                    aBmpSaveList.push_back( new BSaveStruct( aBmp, aDestRect, nWinROP, pOut->GetFillStyle () ) );
                }
            }
        }
        break;

        case W_META_STRETCHBLT:
        case W_META_DIBBITBLT:
        case W_META_DIBSTRETCHBLT:
        case W_META_STRETCHDIB:
        {
            sal_Int32   nWinROP = 0;
            sal_uInt16  nSx = 0, nSy = 0, nSxe = 0, nSye = 0, nUsage = 0;
            Bitmap      aBmp;

            *pWMF >> nWinROP;

            if( nFunc == W_META_STRETCHDIB )
                *pWMF >> nUsage;

            // nSye and nSxe is the number of pixels that has to been used
            // If they are set to zero, it is as indicator not to scale the bitmap later
            //
            if( nFunc == W_META_STRETCHDIB || nFunc == W_META_STRETCHBLT || nFunc == W_META_DIBSTRETCHBLT )
                *pWMF >> nSye >> nSxe;

            // nSy and nx is the offset of the first pixel
            *pWMF >> nSy >> nSx;

            if( nFunc == W_META_STRETCHDIB || nFunc == W_META_DIBBITBLT || nFunc == W_META_DIBSTRETCHBLT )
            {
                if ( nWinROP == PATCOPY )
                    *pWMF >> nUsage;    // i don't know anything of this parameter, so its called nUsage
                                        // pOut->DrawRect( Rectangle( ReadYX(), aDestSize ), sal_False );

                Size aDestSize( ReadYXExt() );
                if ( aDestSize.Width() && aDestSize.Height() )  // #92623# do not try to read buggy bitmaps
                {
                    Rectangle aDestRect( ReadYX(), aDestSize );
                    if ( nWinROP != PATCOPY )
                        ReadDIB(aBmp, *pWMF, false);

                    // test if it is sensible to crop
                    if ( nSye && nSxe &&
                        ( ( nSx + nSxe ) <= aBmp.GetSizePixel().Width() ) &&
                            ( ( nSy + nSye <= aBmp.GetSizePixel().Height() ) ) )
                    {
                        Rectangle aCropRect( Point( nSx, nSy ), Size( nSxe, nSye ) );
                        aBmp.Crop( aCropRect );
                    }
                    aBmpSaveList.push_back( new BSaveStruct( aBmp, aDestRect, nWinROP, pOut->GetFillStyle () ) );
                }
            }
        }
        break;

        case W_META_DIBCREATEPATTERNBRUSH:
        {
            Bitmap  aBmp;
            BitmapReadAccess* pBmp;
            sal_uInt32  nRed = 0, nGreen = 0, nBlue = 0, nCount = 1;
            sal_uInt16  nFunction = 0;

            *pWMF >> nFunction >> nFunction;

            ReadDIB(aBmp, *pWMF, false);
            pBmp = aBmp.AcquireReadAccess();
            if ( pBmp )
            {
                for ( sal_Int32 y = 0; y < pBmp->Height(); y++ )
                {
                    for ( sal_Int32 x = 0; x < pBmp->Width(); x++ )
                    {
                        const BitmapColor aColor( pBmp->GetColor( y, x ) );

                        nRed += aColor.GetRed();
                        nGreen += aColor.GetGreen();
                        nBlue += aColor.GetBlue();
                    }
                }
                nCount = pBmp->Height() * pBmp->Width();
                if ( !nCount )
                    nCount++;
                aBmp.ReleaseAccess( pBmp );
            }
            Color aColor( (sal_uInt8)( nRed / nCount ), (sal_uInt8)( nGreen / nCount ), (sal_uInt8)( nBlue / nCount ) );
            pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( aColor, sal_False ) );
        }
        break;

        case W_META_DELETEOBJECT:
        {
            sal_Int16 nIndex = 0;
            *pWMF >> nIndex;
            pOut->DeleteObject( nIndex );
        }
        break;

        case W_META_CREATEPALETTE:
        {
            pOut->CreateObject( GDI_DUMMY );
        }
        break;

        case W_META_CREATEBRUSH:
        {
            pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( Color( COL_WHITE ), sal_False ) );
        }
        break;

        case W_META_CREATEPATTERNBRUSH:
        {
            pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( Color( COL_WHITE ), sal_False ) );
        }
        break;

        case W_META_CREATEPENINDIRECT:
        {
            LineInfo    aLineInfo;
            sal_uInt16      nStyle = 0, nWidth = 0, nHeight = 0;

            *pWMF >> nStyle >> nWidth >> nHeight;

            if ( nWidth )
                aLineInfo.SetWidth( nWidth );

            sal_Bool bTransparent = sal_False;
            switch( nStyle & 0xFF )
            {
                case PS_DASHDOTDOT :
                    aLineInfo.SetStyle( LINE_DASH );
                    aLineInfo.SetDashCount( 1 );
                    aLineInfo.SetDotCount( 2 );
                    aLineInfo.SetDashLen( 150 );
                    aLineInfo.SetDotLen( 30 );
                    aLineInfo.SetDistance( 50 );
                break;
                case PS_DASHDOT :
                    aLineInfo.SetStyle( LINE_DASH );
                    aLineInfo.SetDashCount( 1 );
                    aLineInfo.SetDotCount( 1 );
                    aLineInfo.SetDashLen( 150 );
                    aLineInfo.SetDotLen( 30 );
                    aLineInfo.SetDistance( 90 );
                break;
                case PS_DOT :
                    aLineInfo.SetStyle( LINE_DASH );
                    aLineInfo.SetDashCount( 0 );
                    aLineInfo.SetDotCount( 1 );
                    aLineInfo.SetDotLen( 30 );
                    aLineInfo.SetDistance( 50 );
                break;
                case PS_DASH :
                    aLineInfo.SetStyle( LINE_DASH );
                    aLineInfo.SetDashCount( 1 );
                    aLineInfo.SetDotCount( 0 );
                    aLineInfo.SetDashLen( 225 );
                    aLineInfo.SetDistance( 100 );
                break;
                case PS_NULL :
                    bTransparent = sal_True;
                    aLineInfo.SetStyle( LINE_NONE );
                break;
                default :
                case PS_INSIDEFRAME :
                case PS_SOLID :
                    aLineInfo.SetStyle( LINE_SOLID );
            }
            switch( nStyle & 0xF00 )
            {
                case PS_ENDCAP_ROUND :
                    aLineInfo.SetLineCap( com::sun::star::drawing::LineCap_ROUND );
                break;
                case PS_ENDCAP_SQUARE :
                    aLineInfo.SetLineCap( com::sun::star::drawing::LineCap_SQUARE );
                break;
                case PS_ENDCAP_FLAT :
                default :
                    aLineInfo.SetLineCap( com::sun::star::drawing::LineCap_BUTT );
            }
            switch( nStyle & 0xF000 )
            {
                case PS_JOIN_ROUND :
                    aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_ROUND );
                break;
                case PS_JOIN_MITER :
                    aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_MITER );
                break;
                case PS_JOIN_BEVEL :
                    aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_BEVEL );
                break;
                default :
                    aLineInfo.SetLineJoin ( basegfx::B2DLINEJOIN_NONE );
            }
            pOut->CreateObject( GDI_PEN, new WinMtfLineStyle( ReadColor(), aLineInfo, bTransparent ) );
        }
        break;

        case W_META_CREATEBRUSHINDIRECT:
        {
            sal_uInt16  nStyle = 0;
            *pWMF >> nStyle;
            pOut->CreateObject( GDI_BRUSH, new WinMtfFillStyle( ReadColor(), ( nStyle == BS_HOLLOW ) ? sal_True : sal_False ) );
        }
        break;

        case W_META_CREATEFONTINDIRECT:
        {
            Size    aFontSize;
            char    lfFaceName[ LF_FACESIZE ];
            sal_Int16   lfEscapement = 0, lfOrientation = 0, lfWeight = 0;  // ( formerly sal_uInt16 )

            LOGFONTW aLogFont;
            aFontSize = ReadYXExt();
            *pWMF >> lfEscapement >> lfOrientation >> lfWeight
                    >> aLogFont.lfItalic >> aLogFont.lfUnderline >> aLogFont.lfStrikeOut >> aLogFont.lfCharSet >> aLogFont.lfOutPrecision
                        >> aLogFont.lfClipPrecision >> aLogFont.lfQuality >> aLogFont.lfPitchAndFamily;
            pWMF->Read( lfFaceName, LF_FACESIZE );
            aLogFont.lfWidth = aFontSize.Width();
            aLogFont.lfHeight = aFontSize.Height();
            aLogFont.lfEscapement = lfEscapement;
            aLogFont.lfOrientation = lfOrientation;
            aLogFont.lfWeight = lfWeight;

            rtl_TextEncoding eCharSet;
            if ( ( aLogFont.lfCharSet == OEM_CHARSET ) || ( aLogFont.lfCharSet == DEFAULT_CHARSET ) )
                eCharSet = osl_getThreadTextEncoding();
            else
                eCharSet = rtl_getTextEncodingFromWindowsCharset( aLogFont.lfCharSet );
            if ( eCharSet == RTL_TEXTENCODING_DONTKNOW )
                eCharSet = osl_getThreadTextEncoding();
            if ( eCharSet == RTL_TEXTENCODING_SYMBOL )
                eCharSet = RTL_TEXTENCODING_MS_1252;
            aLogFont.alfFaceName = OUString( lfFaceName, strlen(lfFaceName), eCharSet );

            pOut->CreateObject( GDI_FONT, new WinMtfFontStyle( aLogFont ) );
        }
        break;

        case W_META_CREATEBITMAPINDIRECT:
        {
            pOut->CreateObject( GDI_DUMMY );
        }
        break;

        case W_META_CREATEBITMAP:
        {
            pOut->CreateObject( GDI_DUMMY );
        }
        break;

        case W_META_CREATEREGION:
        {
            pOut->CreateObject( GDI_DUMMY );
        }
        break;

        case W_META_EXCLUDECLIPRECT :
        {
            pOut->ExcludeClipRect( ReadRectangle() );
        }
        break;

        case W_META_PATBLT:
        {
            sal_uInt32 nROP = 0, nOldROP = 0;
            *pWMF >> nROP;
            Size aSize = ReadYXExt();
            nOldROP = pOut->SetRasterOp( nROP );
            pOut->DrawRect( Rectangle( ReadYX(), aSize ), sal_False );
            pOut->SetRasterOp( nOldROP );
        }
        break;

        case W_META_SELECTCLIPREGION:
        {
            sal_Int16 nObjIndex = 0;
            *pWMF >> nObjIndex;
            if ( !nObjIndex )
            {
                PolyPolygon aEmptyPolyPoly;
                pOut->SetClipPath( aEmptyPolyPoly, RGN_COPY, sal_True );
            }
        }
        break;

        case W_META_ESCAPE :
        {
            // nRecSize has been checked previously to be greater than 3
            sal_uInt64 nMetaRecSize = static_cast< sal_uInt64 >( nRecSize - 2 ) * 2;
            sal_uInt64 nMetaRecEndPos = pWMF->Tell() + nMetaRecSize;

            // taking care that nRecSize does not exceed the maximal stream position
            if ( nMetaRecEndPos > nEndPos )
            {
                pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
                break;
            }
            if ( nRecSize >= 4 )    // minimal escape length
            {
                sal_uInt16  nMode = 0, nLen = 0;
                *pWMF >> nMode
                      >> nLen;
                if ( ( nMode == W_MFCOMMENT ) && ( nLen >= 4 ) )
                {
                    sal_uInt32 nNewMagic = 0; // we have to read int32 for
                    *pWMF >> nNewMagic;   // META_ESCAPE_ENHANCED_METAFILE CommentIdentifier

                    if( nNewMagic == 0x2c2a4f4f &&  nLen >= 14 )
                    {
                        sal_uInt16 nMagic2 = 0;
                        *pWMF >> nMagic2;
                        if( nMagic2 == 0x0a ) // 2nd half of magic
                        {                     // continue with private escape
                            sal_uInt32 nCheck = 0, nEsc = 0;
                            *pWMF >> nCheck
                                  >> nEsc;

                            sal_uInt32 nEscLen = nLen - 14;
                            if ( nEscLen <= ( nRecSize * 2 ) )
                            {
#ifdef OSL_BIGENDIAN
                                sal_uInt32 nTmp = OSL_SWAPDWORD( nEsc );
                                sal_uInt32 nCheckSum = rtl_crc32( 0, &nTmp, 4 );
#else
                                sal_uInt32 nCheckSum = rtl_crc32( 0, &nEsc, 4 );
#endif
                                sal_Int8* pData = NULL;

                                if ( ( static_cast< sal_uInt64 >( nEscLen ) + pWMF->Tell() ) > nMetaRecEndPos )
                                {
                                    pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
                                    break;
                                }
                                if ( nEscLen > 0 )
                                {
                                    pData = new sal_Int8[ nEscLen ];
                                    pWMF->Read( pData, nEscLen );
                                    nCheckSum = rtl_crc32( nCheckSum, pData, nEscLen );
                                }
                                if ( nCheck == nCheckSum )
                                {
                                    switch( nEsc )
                                    {
                                        case PRIVATE_ESCAPE_UNICODE :
                                        {
                                            // we will use text instead of polygons only if we have the correct font
                                            if ( Application::GetDefaultDevice()->IsFontAvailable( pOut->GetFont().GetName() ) )
                                            {
                                                Point  aPt;
                                                OUString aString;
                                                sal_uInt32  nStringLen, nDXCount;
                                                sal_Int32* pDXAry = NULL;
                                                SvMemoryStream aMemoryStream( nEscLen );
                                                aMemoryStream.Write( pData, nEscLen );
                                                aMemoryStream.Seek( STREAM_SEEK_TO_BEGIN );
                                                //#fdo39428 SvStream no longer supports operator>>(long&)
                                                sal_Int32 nTmpX(0), nTmpY(0);
                                                aMemoryStream >> nTmpX
                                                              >> nTmpY
                                                              >> nStringLen;
                                                 aPt.X() = nTmpX;
                                                 aPt.Y() = nTmpY;

                                                if ( ( static_cast< sal_uInt64 >( nStringLen ) * sizeof( sal_Unicode ) ) < ( nEscLen - aMemoryStream.Tell() ) )
                                                {

                                                    aString = read_uInt16s_ToOUString(aMemoryStream, nStringLen);
                                                    aMemoryStream >> nDXCount;
                                                    if ( ( static_cast< sal_uInt64 >( nDXCount ) * sizeof( sal_Int32 ) ) >= ( nEscLen - aMemoryStream.Tell() ) )
                                                        nDXCount = 0;
                                                    if ( nDXCount )
                                                        pDXAry = new sal_Int32[ nDXCount ];
                                                    for  (sal_uInt32 i = 0; i < nDXCount; i++ )
                                                        aMemoryStream >> pDXAry[ i ];
                                                    aMemoryStream >> nSkipActions;
                                                    pOut->DrawText( aPt, aString, pDXAry );
                                                    delete[] pDXAry;
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                                delete[] pData;
                            }
                        }
                    }
                    else if ( (nNewMagic == static_cast< sal_uInt32 >(0x43464D57)) && (nLen >= 34) && ( (sal_Int32)(nLen + 10) <= (sal_Int32)(nRecSize * 2) ))
                    {
                        sal_uInt32 nComType = 0, nVersion = 0, nFlags = 0, nComRecCount = 0,
                                   nCurRecSize = 0, nRemainingSize = 0, nEMFTotalSize = 0;
                        sal_uInt16 nCheck = 0;

                        *pWMF >> nComType >> nVersion >> nCheck >> nFlags
                              >> nComRecCount >> nCurRecSize
                              >> nRemainingSize >> nEMFTotalSize; // the nRemainingSize is not mentioned in MSDN documentation
                                                                  // but it seems to be required to read in data produced by OLE

                        if( nComType == 0x01 && nVersion == 0x10000 && nComRecCount )
                        {
                            if( !nEMFRec )
                            {   // first EMF comment
                                nEMFRecCount    = nComRecCount;
                                nEMFSize        = nEMFTotalSize;
                                pEMFStream = new SvMemoryStream( nEMFSize );
                            }
                            else if( ( nEMFRecCount != nComRecCount ) || ( nEMFSize != nEMFTotalSize ) ) // add additional checks here
                            {
                                // total records should be the same as in previous comments
                                nEMFRecCount = 0xFFFFFFFF;
                                delete pEMFStream;
                                pEMFStream = NULL;
                            }
                            nEMFRec++;

                            if( pEMFStream && nCurRecSize + 34 > nLen )
                            {
                                nEMFRecCount = 0xFFFFFFFF;
                                delete pEMFStream;
                                pEMFStream = NULL;
                            }

                            if( pEMFStream )
                            {
                                sal_Int8* pBuf = new sal_Int8[ nCurRecSize ];
                                sal_uInt32 nCount = pWMF->Read( pBuf, nCurRecSize );
                                if( nCount == nCurRecSize )
                                    pEMFStream->Write( pBuf, nCount );
                                delete[] pBuf;
                            }
                        }
                    }
                }
            }
        }
        break;

        case W_META_SETRELABS:
        case W_META_SETPOLYFILLMODE:
        case W_META_SETSTRETCHBLTMODE:
        case W_META_SETTEXTCHAREXTRA:
        case W_META_SETTEXTJUSTIFICATION:
        case W_META_FLOODFILL :
        case W_META_FILLREGION:
        case W_META_FRAMEREGION:
        case W_META_INVERTREGION:
        case W_META_PAINTREGION:
        case W_META_DRAWTEXT:
        case W_META_SETMAPPERFLAGS:
        case W_META_SETDIBTODEV:
        case W_META_SELECTPALETTE:
        case W_META_REALIZEPALETTE:
        case W_META_ANIMATEPALETTE:
        case W_META_SETPALENTRIES:
        case W_META_RESIZEPALETTE:
        case W_META_EXTFLOODFILL:
        case W_META_RESETDC:
        case W_META_STARTDOC:
        case W_META_STARTPAGE:
        case W_META_ENDPAGE:
        case W_META_ABORTDOC:
        case W_META_ENDDOC:
        break;
    }
}

// ------------------------------------------------------------------------

sal_Bool WMFReader::ReadHeader()
{
    sal_Size nStrmPos = pWMF->Tell();

    sal_uInt32 nPlaceableMetaKey(0);
    // Einlesen des METAFILEHEADER, falls vorhanden
    *pWMF >> nPlaceableMetaKey;
    if (!pWMF->good())
        return false;

    Rectangle aPlaceableBound;

    if (nPlaceableMetaKey == 0x9ac6cdd7L)
    { //TODO do some real error handling here
        sal_Int16 nVal;

        // Skip reserved bytes
        pWMF->SeekRel(2);

        // BoundRect
        *pWMF >> nVal;
        aPlaceableBound.Left() = nVal;
        *pWMF >> nVal;
        aPlaceableBound.Top() = nVal;
        *pWMF >> nVal;
        aPlaceableBound.Right() = nVal;
        *pWMF >> nVal;
        aPlaceableBound.Bottom() = nVal;

        // inch
        *pWMF >> nUnitsPerInch;

        // reserved
        pWMF->SeekRel( 4 );

        // Skip and don't check the checksum
        pWMF->SeekRel( 2 );
    }
    else
    {
        nUnitsPerInch = 96;


        if (   pExternalHeader != NULL
            && pExternalHeader->xExt > 0
            && pExternalHeader->yExt > 0
            && (pExternalHeader->mapMode == MM_ISOTROPIC || pExternalHeader->mapMode == MM_ANISOTROPIC))
        {
            // #n417818#: If we have an external header then overwrite the bounds!
            Rectangle aExtRect(0, 0,
                          (double) pExternalHeader->xExt * 567 * nUnitsPerInch / 1440000,
                          (double) pExternalHeader->yExt * 567 * nUnitsPerInch / 1440000);
            aPlaceableBound = aExtRect;
            pOut->SetMapMode( pExternalHeader->mapMode );
        }
        else
        {
            pWMF->Seek( nStrmPos + 18 );    // set the streampos to the start of the metaactions
            GetPlaceableBound( aPlaceableBound, pWMF );
        }

        pWMF->Seek( nStrmPos );
    }

    pOut->SetWinOrg( aPlaceableBound.TopLeft() );
    Size aWMFSize( labs( aPlaceableBound.GetWidth() ), labs( aPlaceableBound.GetHeight() ) );
    pOut->SetWinExt( aWMFSize );

    Size aDevExt( 10000, 10000 );
    if( ( labs( aWMFSize.Width() ) > 1 ) && ( labs( aWMFSize.Height() ) > 1 ) )
    {
        const Fraction  aFrac( 1, nUnitsPerInch );
        MapMode         aWMFMap( MAP_INCH, Point(), aFrac, aFrac );
        Size            aSize100( OutputDevice::LogicToLogic( aWMFSize, aWMFMap, MAP_100TH_MM ) );
        aDevExt = Size( labs( aSize100.Width() ), labs( aSize100.Height() ) );
    }
    pOut->SetDevExt( aDevExt );

    // Einlesen des METAHEADER
    sal_uInt32 nMetaKey(0);
    *pWMF >> nMetaKey; // Typ und Headergroesse
    if (!pWMF->good())
        return false;
    if (nMetaKey != 0x00090001)
    {
        sal_uInt16 aNextWord(0);
        *pWMF >> aNextWord;
        if (nMetaKey != 0x10000 || aNextWord != 0x09)
        {
            pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
            return false;
        }
    }

    pWMF->SeekRel( 2 ); // Version (von Windows)
    pWMF->SeekRel( 4 ); // Size (der Datei in Words)
    pWMF->SeekRel( 2 ); // NoObjects (Maximale Anzahl der gleichzeitigen Objekte)
    pWMF->SeekRel( 4 ); // MaxRecord (Groesse des groessten Records in Words)
    pWMF->SeekRel( 2 ); // NoParameters (Unused

    return pWMF->good();
}

void WMFReader::ReadWMF()
{
    sal_uInt16  nFunction;
    sal_uLong   nPos, nPercent, nLastPercent;

    nSkipActions = 0;
    nCurrentAction = 0;
    nUnicodeEscapeAction = 0;

    pEMFStream      = NULL;
    nEMFRecCount    = 0;
    nEMFRec         = 0;
    nEMFSize        = 0;

    sal_Bool bEMFAvailable = sal_False;

    pOut->SetMapMode( MM_ANISOTROPIC );
    pOut->SetWinOrg( Point() );
    pOut->SetWinExt( Size( 1, 1 ) );
    pOut->SetDevExt( Size( 10000, 10000 ) );

    nEndPos=pWMF->Seek( STREAM_SEEK_TO_END );
    pWMF->Seek( nStartPos );
    Callback( (sal_uInt16) ( nLastPercent = 0 ) );

    if ( ReadHeader( ) )
    {

        nPos = pWMF->Tell();

        if( nEndPos - nStartPos )
        {
            while( true )
            {
                nCurrentAction++;
                nPercent = ( nPos - nStartPos ) * 100 / ( nEndPos - nStartPos );

                if( nLastPercent + 4 <= nPercent )
                {
                    Callback( (sal_uInt16) nPercent );
                    nLastPercent = nPercent;
                }
                *pWMF >> nRecSize >> nFunction;

                if(  pWMF->GetError()
                  || ( nRecSize < 3 )
                  || (  nRecSize  == 3
                     && nFunction == 0
                     )
                  || pWMF->IsEof()
                  )
                {
                    if( pWMF->IsEof() )
                        pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );

                    break;
                }
                if ( !bEMFAvailable )
                {
                    if(   !aBmpSaveList.empty()
                      && ( nFunction != W_META_STRETCHDIB    )
                      && ( nFunction != W_META_DIBBITBLT     )
                      && ( nFunction != W_META_DIBSTRETCHBLT )
                      )
                    {
                        pOut->ResolveBitmapActions( aBmpSaveList );
                    }

                    if ( !nSkipActions )
                        ReadRecordParams( nFunction );
                    else
                        nSkipActions--;

                    if( pEMFStream && nEMFRecCount == nEMFRec )
                    {
                        GDIMetaFile aMeta;
                        pEMFStream->Seek( 0 );
                        EnhWMFReader* pEMFReader = new EnhWMFReader ( *pEMFStream, aMeta );
                        bEMFAvailable = pEMFReader->ReadEnhWMF();
                        delete pEMFReader; // destroy first!!!

                        if( bEMFAvailable )
                        {
                            pOut->AddFromGDIMetaFile( aMeta );
                            pOut->SetrclFrame( Rectangle( Point(0, 0), aMeta.GetPrefSize()));

                            // the stream needs to be set to the wmf end position,
                            // otherwise the GfxLink that is created will be incorrect
                            // (leading to graphic loss after swapout/swapin).
                            // so we will proceed normally, but are ignoring further wmf
                            // records
                        }
                        else
                        {
                            // something went wrong
                            // continue with WMF, don't try this again
                            delete pEMFStream;
                            pEMFStream = NULL;
                        }
                    }
                }
                nPos += nRecSize * 2;
                if ( nPos <= nEndPos )
                    pWMF->Seek( nPos  );
                else
                    pWMF->SetError( SVSTREAM_FILEFORMAT_ERROR );
            }
        }
        else
            pWMF->SetError( SVSTREAM_GENERALERROR );

        if( !pWMF->GetError() && !aBmpSaveList.empty() )
            pOut->ResolveBitmapActions( aBmpSaveList );
    }
    if ( pWMF->GetError() )
        pWMF->Seek( nStartPos );
}

// ------------------------------------------------------------------------

sal_Bool WMFReader::GetPlaceableBound( Rectangle& rPlaceableBound, SvStream* pStm )
{
    sal_Bool bRet = sal_True;

    Rectangle aBound;
    aBound.Left()   = RECT_MAX;
    aBound.Top()    = RECT_MAX;
    aBound.Right()  = RECT_MIN;
    aBound.Bottom() = RECT_MIN;

    sal_uInt32 nPos = pStm->Tell();
    sal_uInt32 nEnd = pStm->Seek( STREAM_SEEK_TO_END );

    pStm->Seek( nPos );

    boost::optional<Point> aWinOrg;
    boost::optional<Size>  aWinExt;

    boost::optional<Point> aViewportOrg;
    boost::optional<Size>  aViewportExt;

    if( nEnd - nPos )
    {
        sal_Int16 nMapMode = MM_ANISOTROPIC;
        sal_uInt16 nFunction;
        sal_uInt32 nRSize;

        while( bRet )
        {
            *pStm >> nRSize >> nFunction;

            if( pStm->GetError() || ( nRSize < 3 ) || ( nRSize==3 && nFunction==0 ) || pStm->IsEof() )
            {
                if( pStm->IsEof() )
                {
                    pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
                    bRet = sal_False;
                }
                break;
            }
            switch( nFunction )
            {
                case W_META_SETWINDOWORG:
                {
                    aWinOrg = ReadYX();
                }
                break;

                case W_META_SETWINDOWEXT:
                {
                    sal_Int16 nWidth(0), nHeight(0);
                    *pStm >> nHeight >> nWidth;
                    rPlaceableBound.SetSize( Size( nWidth, nHeight ) );
                }
                break;

                case W_META_SETVIEWPORTORG:
                {
                    aViewportOrg = ReadYX();
                }
                break;

                case W_META_SETVIEWPORTEXT:
                {
                    sal_Int16 nWidth(0), nHeight(0);
                    *pStm >> nHeight >> nWidth;
                    aViewportExt = Size(nWidth, nHeight);
                }
                break;

                case W_META_SETMAPMODE :
                    *pStm >> nMapMode;
                break;

                case W_META_MOVETO:
                case W_META_LINETO:
                    GetWinExtMax( ReadYX(), aBound, nMapMode );
                break;

                case W_META_RECTANGLE:
                case W_META_INTERSECTCLIPRECT:
                case W_META_EXCLUDECLIPRECT :
                case W_META_ELLIPSE:
                    GetWinExtMax( ReadRectangle(), aBound, nMapMode );
                break;

                case W_META_ROUNDRECT:
                    ReadYXExt(); // size
                    GetWinExtMax( ReadRectangle(), aBound, nMapMode );
                break;

                case W_META_ARC:
                case W_META_PIE:
                case W_META_CHORD:
                    ReadYX(); // end
                    ReadYX(); // start
                    GetWinExtMax( ReadRectangle(), aBound, nMapMode );
                break;

                case W_META_POLYGON:
                {
                    sal_uInt16 nPoints;
                    *pStm >> nPoints;
                    for(sal_uInt16 i = 0; i < nPoints; i++ )
                        GetWinExtMax( ReadPoint(), aBound, nMapMode );
                }
                break;

                case W_META_POLYPOLYGON:
                {
                    bool bRecordOk = true;
                    sal_uInt16 nPoly, nPoints = 0;
                    *pStm >> nPoly;
                    for(sal_uInt16 i = 0; i < nPoly; i++ )
                    {
                        sal_uInt16 nP = 0;
                        *pStm >> nP;
                        if (nP > SAL_MAX_UINT16 - nPoints)
                        {
                            bRecordOk = false;
                            break;
                        }
                        nPoints += nP;
                    }

                    SAL_WARN_IF(!bRecordOk, "vcl.filter", "polypolygon record has more polygons than we can handle");

                    bRecordOk = bRecordOk && pStm->good();

                    if (!bRecordOk)
                    {
                        pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
                        bRet = sal_False;
                        break;
                    }

                    for (sal_uInt16 i = 0; i < nPoints; i++ )
                        GetWinExtMax( ReadPoint(), aBound, nMapMode );

                    bRecordOk &= pStm->good();

                    if (!bRecordOk)
                    {
                        pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
                        bRet = sal_False;
                        break;
                    }
                }
                break;

                case W_META_POLYLINE:
                {
                    sal_uInt16 nPoints;
                    *pStm >> nPoints;
                    for(sal_uInt16 i = 0; i < nPoints; i++ )
                        GetWinExtMax( ReadPoint(), aBound, nMapMode );
                }
                break;

                case W_META_SETPIXEL:
                {
                    ReadColor();
                    GetWinExtMax( ReadYX(), aBound, nMapMode );
                }
                break;

                case W_META_TEXTOUT:
                {
                    sal_uInt16 nLength;
                    *pStm >> nLength;
                    // todo: we also have to take care of the text width
                    if ( nLength )
                    {
                        pStm->SeekRel( ( nLength + 1 ) &~ 1 );
                        GetWinExtMax( ReadYX(), aBound, nMapMode );
                    }
                }
                break;

                case W_META_EXTTEXTOUT:
                {
                    sal_uInt16  nLen, nOptions;
                    Point       aPosition;

                    aPosition = ReadYX();
                    *pStm >> nLen >> nOptions;
                    // todo: we also have to take care of the text width
                    if( nLen )
                        GetWinExtMax( aPosition, aBound, nMapMode );
                }
                break;
                case W_META_BITBLT:
                case W_META_STRETCHBLT:
                case W_META_DIBBITBLT:
                case W_META_DIBSTRETCHBLT:
                case W_META_STRETCHDIB:
                {
                    sal_Int32   nWinROP;
                    sal_uInt16  nSx, nSy, nSxe, nSye, nUsage;
                    *pStm >> nWinROP;

                    if( nFunction == W_META_STRETCHDIB )
                        *pStm >> nUsage;

                    // nSye and nSxe is the number of pixels that has to been used
                    if( nFunction == W_META_STRETCHDIB || nFunction == W_META_STRETCHBLT || nFunction == W_META_DIBSTRETCHBLT )
                        *pStm >> nSye >> nSxe;
                    else
                        nSye = nSxe = 0;    // set this to zero as indicator not to scale the bitmap later

                    // nSy and nx is the offset of the first pixel
                    *pStm >> nSy >> nSx;

                    if( nFunction == W_META_STRETCHDIB || nFunction == W_META_DIBBITBLT || nFunction == W_META_DIBSTRETCHBLT )
                    {
                        if ( nWinROP == PATCOPY )
                            *pStm >> nUsage;    // i don't know anything of this parameter, so its called nUsage
                                                // pOut->DrawRect( Rectangle( ReadYX(), aDestSize ), sal_False );

                        Size aDestSize( ReadYXExt() );
                        if ( aDestSize.Width() && aDestSize.Height() )  // #92623# do not try to read buggy bitmaps
                        {
                            Rectangle aDestRect( ReadYX(), aDestSize );
                            GetWinExtMax( aDestRect, aBound, nMapMode );
                        }
                    }
                }
                break;

                case W_META_PATBLT:
                {
                    sal_uInt32 nROP;
                    *pStm >> nROP;
                    Size aSize = ReadYXExt();
                    GetWinExtMax( Rectangle( ReadYX(), aSize ), aBound, nMapMode );
                }
                break;
            }
            nPos += nRSize * 2;
             if ( nPos <= nEnd )
                 pStm->Seek( nPos );
             else
             {
                 pStm->SetError( SVSTREAM_FILEFORMAT_ERROR );
                 bRet = sal_False;
             }

        }
    }
    else
    {
        pStm->SetError( SVSTREAM_GENERALERROR );
        bRet = sal_False;
    }

    if (bRet)
    {
        if (aWinOrg && aWinExt)
        {
            rPlaceableBound = Rectangle(*aWinOrg, *aWinExt);
        }
        else if (aViewportOrg && aViewportExt)
        {
            rPlaceableBound = Rectangle(*aViewportOrg, *aViewportExt);
        }
        else
        {
            rPlaceableBound = aBound;
        }
    }

    return bRet;
}

WMFReader::~WMFReader()
{
    if( pEMFStream )
        delete pEMFStream;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */