summaryrefslogtreecommitdiff
path: root/core/fxge/win32/fx_win32_gdipext.cpp
blob: a83a4496fe39e71d3bafa4c42370f1a18338c376 (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
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include <windows.h>

#include <algorithm>
#include <memory>
#include <sstream>

#include "core/fxcrt/fx_system.h"
#include "core/fxge/cfx_gemodule.h"
#include "core/fxge/cfx_graphstatedata.h"
#include "core/fxge/cfx_pathdata.h"
#include "core/fxge/win32/cfx_windowsdib.h"
#include "core/fxge/win32/win32_int.h"
#include "third_party/base/ptr_util.h"

// Has to come before gdiplus.h
namespace Gdiplus {
using std::min;
using std::max;
}  // namespace Gdiplus

#include <gdiplus.h>  // NOLINT

// TODO(thestig): Remove the infrequently used ones.
using Gdiplus::CombineMode;
using Gdiplus::DashCap;
using Gdiplus::DashCapFlat;
using Gdiplus::DashCapRound;
using Gdiplus::FillModeAlternate;
using Gdiplus::FillModeWinding;
using Gdiplus::GdiplusStartupInput;
using Gdiplus::GdiplusStartupOutput;
using Gdiplus::GpBitmap;
using Gdiplus::GpBrush;
using Gdiplus::GpDashCap;
using Gdiplus::GpDashStyle;
using Gdiplus::GpFillMode;
using Gdiplus::GpFont;
using Gdiplus::GpFontCollection;
using Gdiplus::GpFontFamily;
using Gdiplus::GpGraphics;
using Gdiplus::GpImage;
using Gdiplus::GpLineCap;
using Gdiplus::GpLineJoin;
using Gdiplus::GpMatrix;
using Gdiplus::GpPath;
using Gdiplus::GpPen;
using Gdiplus::GpPoint;
using Gdiplus::GpPointF;
using Gdiplus::GpRect;
using Gdiplus::GpRegion;
using Gdiplus::GpSolidFill;
using Gdiplus::GpStatus;
using Gdiplus::GpStringFormat;
using Gdiplus::GpUnit;
using Gdiplus::ImageLockModeRead;
using Gdiplus::InterpolationMode;
using Gdiplus::InterpolationModeBilinear;
using Gdiplus::InterpolationModeHighQuality;
using Gdiplus::InterpolationModeNearestNeighbor;
using Gdiplus::LineCap;
using Gdiplus::LineCapFlat;
using Gdiplus::LineCapRound;
using Gdiplus::LineCapSquare;
using Gdiplus::LineJoin;
using Gdiplus::LineJoinBevel;
using Gdiplus::LineJoinMiterClipped;
using Gdiplus::LineJoinRound;
using Gdiplus::PaletteFlagsHasAlpha;
using Gdiplus::PathPointTypeBezier;
using Gdiplus::PathPointTypeCloseSubpath;
using Gdiplus::PathPointTypeLine;
using Gdiplus::PathPointTypeStart;
using Gdiplus::PixelOffsetMode;
using Gdiplus::PixelOffsetModeHalf;
using Gdiplus::REAL;
using Gdiplus::SmoothingMode;
using Gdiplus::SmoothingModeAntiAlias;
using Gdiplus::SmoothingModeNone;
using Gdiplus::TextRenderingHint;
using Gdiplus::UnitPixel;
using Gdiplus::UnitWorld;

#define GdiFillType2Gdip(fill_type) \
  (fill_type == ALTERNATE ? FillModeAlternate : FillModeWinding)

namespace {

enum {
  FuncId_GdipCreatePath2,
  FuncId_GdipSetPenDashStyle,
  FuncId_GdipSetPenDashArray,
  FuncId_GdipSetPenDashCap197819,
  FuncId_GdipSetPenLineJoin,
  FuncId_GdipSetPenWidth,
  FuncId_GdipCreateFromHDC,
  FuncId_GdipSetPageUnit,
  FuncId_GdipSetSmoothingMode,
  FuncId_GdipCreateSolidFill,
  FuncId_GdipFillPath,
  FuncId_GdipDeleteBrush,
  FuncId_GdipCreatePen1,
  FuncId_GdipSetPenMiterLimit,
  FuncId_GdipDrawPath,
  FuncId_GdipDeletePen,
  FuncId_GdipDeletePath,
  FuncId_GdipDeleteGraphics,
  FuncId_GdipCreateBitmapFromFileICM,
  FuncId_GdipCreateBitmapFromStreamICM,
  FuncId_GdipGetImageHeight,
  FuncId_GdipGetImageWidth,
  FuncId_GdipGetImagePixelFormat,
  FuncId_GdipBitmapLockBits,
  FuncId_GdipGetImagePaletteSize,
  FuncId_GdipGetImagePalette,
  FuncId_GdipBitmapUnlockBits,
  FuncId_GdipDisposeImage,
  FuncId_GdipFillRectangle,
  FuncId_GdipCreateBitmapFromScan0,
  FuncId_GdipSetImagePalette,
  FuncId_GdipSetInterpolationMode,
  FuncId_GdipDrawImagePointsI,
  FuncId_GdipCreateBitmapFromGdiDib,
  FuncId_GdiplusStartup,
  FuncId_GdipDrawLineI,
  FuncId_GdipResetClip,
  FuncId_GdipCreatePath,
  FuncId_GdipAddPathPath,
  FuncId_GdipSetPathFillMode,
  FuncId_GdipSetClipPath,
  FuncId_GdipGetClip,
  FuncId_GdipCreateRegion,
  FuncId_GdipGetClipBoundsI,
  FuncId_GdipSetClipRegion,
  FuncId_GdipWidenPath,
  FuncId_GdipAddPathLine,
  FuncId_GdipAddPathRectangle,
  FuncId_GdipDeleteRegion,
  FuncId_GdipGetDC,
  FuncId_GdipReleaseDC,
  FuncId_GdipSetPenLineCap197819,
  FuncId_GdipSetPenDashOffset,
  FuncId_GdipResetPath,
  FuncId_GdipCreateRegionPath,
  FuncId_GdipCreateFont,
  FuncId_GdipGetFontSize,
  FuncId_GdipCreateFontFamilyFromName,
  FuncId_GdipSetTextRenderingHint,
  FuncId_GdipDrawDriverString,
  FuncId_GdipCreateMatrix2,
  FuncId_GdipDeleteMatrix,
  FuncId_GdipSetWorldTransform,
  FuncId_GdipResetWorldTransform,
  FuncId_GdipDeleteFontFamily,
  FuncId_GdipDeleteFont,
  FuncId_GdipNewPrivateFontCollection,
  FuncId_GdipDeletePrivateFontCollection,
  FuncId_GdipPrivateAddMemoryFont,
  FuncId_GdipGetFontCollectionFamilyList,
  FuncId_GdipGetFontCollectionFamilyCount,
  FuncId_GdipSetTextContrast,
  FuncId_GdipSetPixelOffsetMode,
  FuncId_GdipGetImageGraphicsContext,
  FuncId_GdipDrawImageI,
  FuncId_GdipDrawImageRectI,
  FuncId_GdipDrawString,
  FuncId_GdipSetPenTransform,
};

LPCSTR g_GdipFuncNames[] = {
    "GdipCreatePath2",
    "GdipSetPenDashStyle",
    "GdipSetPenDashArray",
    "GdipSetPenDashCap197819",
    "GdipSetPenLineJoin",
    "GdipSetPenWidth",
    "GdipCreateFromHDC",
    "GdipSetPageUnit",
    "GdipSetSmoothingMode",
    "GdipCreateSolidFill",
    "GdipFillPath",
    "GdipDeleteBrush",
    "GdipCreatePen1",
    "GdipSetPenMiterLimit",
    "GdipDrawPath",
    "GdipDeletePen",
    "GdipDeletePath",
    "GdipDeleteGraphics",
    "GdipCreateBitmapFromFileICM",
    "GdipCreateBitmapFromStreamICM",
    "GdipGetImageHeight",
    "GdipGetImageWidth",
    "GdipGetImagePixelFormat",
    "GdipBitmapLockBits",
    "GdipGetImagePaletteSize",
    "GdipGetImagePalette",
    "GdipBitmapUnlockBits",
    "GdipDisposeImage",
    "GdipFillRectangle",
    "GdipCreateBitmapFromScan0",
    "GdipSetImagePalette",
    "GdipSetInterpolationMode",
    "GdipDrawImagePointsI",
    "GdipCreateBitmapFromGdiDib",
    "GdiplusStartup",
    "GdipDrawLineI",
    "GdipResetClip",
    "GdipCreatePath",
    "GdipAddPathPath",
    "GdipSetPathFillMode",
    "GdipSetClipPath",
    "GdipGetClip",
    "GdipCreateRegion",
    "GdipGetClipBoundsI",
    "GdipSetClipRegion",
    "GdipWidenPath",
    "GdipAddPathLine",
    "GdipAddPathRectangle",
    "GdipDeleteRegion",
    "GdipGetDC",
    "GdipReleaseDC",
    "GdipSetPenLineCap197819",
    "GdipSetPenDashOffset",
    "GdipResetPath",
    "GdipCreateRegionPath",
    "GdipCreateFont",
    "GdipGetFontSize",
    "GdipCreateFontFamilyFromName",
    "GdipSetTextRenderingHint",
    "GdipDrawDriverString",
    "GdipCreateMatrix2",
    "GdipDeleteMatrix",
    "GdipSetWorldTransform",
    "GdipResetWorldTransform",
    "GdipDeleteFontFamily",
    "GdipDeleteFont",
    "GdipNewPrivateFontCollection",
    "GdipDeletePrivateFontCollection",
    "GdipPrivateAddMemoryFont",
    "GdipGetFontCollectionFamilyList",
    "GdipGetFontCollectionFamilyCount",
    "GdipSetTextContrast",
    "GdipSetPixelOffsetMode",
    "GdipGetImageGraphicsContext",
    "GdipDrawImageI",
    "GdipDrawImageRectI",
    "GdipDrawString",
    "GdipSetPenTransform",
};
static_assert(FX_ArraySize(g_GdipFuncNames) ==
                  static_cast<size_t>(FuncId_GdipSetPenTransform) + 1,
              "g_GdipFuncNames has wrong size");

typedef GpStatus(WINGDIPAPI* FuncType_GdipCreatePath2)(GDIPCONST GpPointF*,
                                                       GDIPCONST BYTE*,
                                                       INT,
                                                       GpFillMode,
                                                       GpPath** path);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenDashStyle)(
    GpPen* pen,
    GpDashStyle dashstyle);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenDashArray)(GpPen* pen,
                                                           GDIPCONST REAL* dash,
                                                           INT count);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenDashCap197819)(
    GpPen* pen,
    GpDashCap dashCap);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenLineJoin)(GpPen* pen,
                                                          GpLineJoin lineJoin);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenWidth)(GpPen* pen, REAL width);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateFromHDC)(HDC hdc,
                                                         GpGraphics** graphics);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPageUnit)(GpGraphics* graphics,
                                                       GpUnit unit);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetSmoothingMode)(
    GpGraphics* graphics,
    SmoothingMode smoothingMode);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateSolidFill)(Gdiplus::ARGB color,
                                                           GpSolidFill** brush);
typedef GpStatus(WINGDIPAPI* FuncType_GdipFillPath)(GpGraphics* graphics,
                                                    GpBrush* brush,
                                                    GpPath* path);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeleteBrush)(GpBrush* brush);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreatePen1)(Gdiplus::ARGB color,
                                                      REAL width,
                                                      GpUnit unit,
                                                      GpPen** pen);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenMiterLimit)(GpPen* pen,
                                                            REAL miterLimit);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDrawPath)(GpGraphics* graphics,
                                                    GpPen* pen,
                                                    GpPath* path);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeletePen)(GpPen* pen);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeletePath)(GpPath* path);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeleteGraphics)(GpGraphics* graphics);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateBitmapFromFileICM)(
    GDIPCONST WCHAR* filename,
    GpBitmap** bitmap);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateBitmapFromStreamICM)(
    IStream* stream,
    GpBitmap** bitmap);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetImageWidth)(GpImage* image,
                                                         UINT* width);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetImageHeight)(GpImage* image,
                                                          UINT* height);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetImagePixelFormat)(
    GpImage* image,
    Gdiplus::PixelFormat* format);
typedef GpStatus(WINGDIPAPI* FuncType_GdipBitmapLockBits)(
    GpBitmap* bitmap,
    GDIPCONST GpRect* rect,
    UINT flags,
    Gdiplus::PixelFormat format,
    Gdiplus::BitmapData* lockedBitmapData);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetImagePalette)(
    GpImage* image,
    Gdiplus::ColorPalette* palette,
    INT size);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetImagePaletteSize)(GpImage* image,
                                                               INT* size);
typedef GpStatus(WINGDIPAPI* FuncType_GdipBitmapUnlockBits)(
    GpBitmap* bitmap,
    Gdiplus::BitmapData* lockedBitmapData);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDisposeImage)(GpImage* image);
typedef GpStatus(WINGDIPAPI* FuncType_GdipFillRectangle)(GpGraphics* graphics,
                                                         GpBrush* brush,
                                                         REAL x,
                                                         REAL y,
                                                         REAL width,
                                                         REAL height);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateBitmapFromScan0)(
    INT width,
    INT height,
    INT stride,
    Gdiplus::PixelFormat format,
    BYTE* scan0,
    GpBitmap** bitmap);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetImagePalette)(
    GpImage* image,
    GDIPCONST Gdiplus::ColorPalette* palette);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetInterpolationMode)(
    GpGraphics* graphics,
    InterpolationMode interpolationMode);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDrawImagePointsI)(
    GpGraphics* graphics,
    GpImage* image,
    GDIPCONST GpPoint* dstpoints,
    INT count);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateBitmapFromGdiDib)(
    GDIPCONST BITMAPINFO* gdiBitmapInfo,
    VOID* gdiBitmapData,
    GpBitmap** bitmap);
typedef Gdiplus::Status(WINAPI* FuncType_GdiplusStartup)(
    OUT uintptr_t* token,
    const GdiplusStartupInput* input,
    OUT GdiplusStartupOutput* output);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDrawLineI)(GpGraphics* graphics,
                                                     GpPen* pen,
                                                     int x1,
                                                     int y1,
                                                     int x2,
                                                     int y2);
typedef GpStatus(WINGDIPAPI* FuncType_GdipResetClip)(GpGraphics* graphics);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreatePath)(GpFillMode brushMode,
                                                      GpPath** path);
typedef GpStatus(WINGDIPAPI* FuncType_GdipAddPathPath)(
    GpPath* path,
    GDIPCONST GpPath* addingPath,
    BOOL connect);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPathFillMode)(GpPath* path,
                                                           GpFillMode fillmode);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetClipPath)(GpGraphics* graphics,
                                                       GpPath* path,
                                                       CombineMode combineMode);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetClip)(GpGraphics* graphics,
                                                   GpRegion* region);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateRegion)(GpRegion** region);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetClipBoundsI)(GpGraphics* graphics,
                                                          GpRect* rect);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetClipRegion)(
    GpGraphics* graphics,
    GpRegion* region,
    CombineMode combineMode);
typedef GpStatus(WINGDIPAPI* FuncType_GdipWidenPath)(GpPath* nativePath,
                                                     GpPen* pen,
                                                     GpMatrix* matrix,
                                                     REAL flatness);
typedef GpStatus(WINGDIPAPI* FuncType_GdipAddPathLine)(GpPath* path,
                                                       REAL x1,
                                                       REAL y1,
                                                       REAL x2,
                                                       REAL y2);
typedef GpStatus(WINGDIPAPI* FuncType_GdipAddPathRectangle)(GpPath* path,
                                                            REAL x,
                                                            REAL y,
                                                            REAL width,
                                                            REAL height);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeleteRegion)(GpRegion* region);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetDC)(GpGraphics* graphics,
                                                 HDC* hdc);
typedef GpStatus(WINGDIPAPI* FuncType_GdipReleaseDC)(GpGraphics* graphics,
                                                     HDC hdc);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenLineCap197819)(
    GpPen* pen,
    GpLineCap startCap,
    GpLineCap endCap,
    GpDashCap dashCap);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenDashOffset)(GpPen* pen,
                                                            REAL offset);
typedef GpStatus(WINGDIPAPI* FuncType_GdipResetPath)(GpPath* path);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateRegionPath)(GpPath* path,
                                                            GpRegion** region);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateFont)(
    GDIPCONST GpFontFamily* fontFamily,
    REAL emSize,
    INT style,
    Gdiplus::Unit unit,
    GpFont** font);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetFontSize)(GpFont* font,
                                                       REAL* size);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateFontFamilyFromName)(
    GDIPCONST WCHAR* name,
    GpFontCollection* fontCollection,
    GpFontFamily** FontFamily);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetTextRenderingHint)(
    GpGraphics* graphics,
    TextRenderingHint mode);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDrawDriverString)(
    GpGraphics* graphics,
    GDIPCONST UINT16* text,
    INT length,
    GDIPCONST GpFont* font,
    GDIPCONST GpBrush* brush,
    GDIPCONST Gdiplus::PointF* positions,
    INT flags,
    GDIPCONST GpMatrix* matrix);
typedef GpStatus(WINGDIPAPI* FuncType_GdipCreateMatrix2)(REAL m11,
                                                         REAL m12,
                                                         REAL m21,
                                                         REAL m22,
                                                         REAL dx,
                                                         REAL dy,
                                                         GpMatrix** matrix);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeleteMatrix)(GpMatrix* matrix);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetWorldTransform)(
    GpGraphics* graphics,
    GpMatrix* matrix);
typedef GpStatus(WINGDIPAPI* FuncType_GdipResetWorldTransform)(
    GpGraphics* graphics);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeleteFontFamily)(
    GpFontFamily* FontFamily);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeleteFont)(GpFont* font);
typedef GpStatus(WINGDIPAPI* FuncType_GdipNewPrivateFontCollection)(
    GpFontCollection** fontCollection);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDeletePrivateFontCollection)(
    GpFontCollection** fontCollection);
typedef GpStatus(WINGDIPAPI* FuncType_GdipPrivateAddMemoryFont)(
    GpFontCollection* fontCollection,
    GDIPCONST void* memory,
    INT length);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetFontCollectionFamilyList)(
    GpFontCollection* fontCollection,
    INT numSought,
    GpFontFamily* gpfamilies[],
    INT* numFound);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetFontCollectionFamilyCount)(
    GpFontCollection* fontCollection,
    INT* numFound);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetTextContrast)(GpGraphics* graphics,
                                                           UINT contrast);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPixelOffsetMode)(
    GpGraphics* graphics,
    PixelOffsetMode pixelOffsetMode);
typedef GpStatus(WINGDIPAPI* FuncType_GdipGetImageGraphicsContext)(
    GpImage* image,
    GpGraphics** graphics);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDrawImageI)(GpGraphics* graphics,
                                                      GpImage* image,
                                                      INT x,
                                                      INT y);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDrawImageRectI)(GpGraphics* graphics,
                                                          GpImage* image,
                                                          INT x,
                                                          INT y,
                                                          INT width,
                                                          INT height);
typedef GpStatus(WINGDIPAPI* FuncType_GdipDrawString)(
    GpGraphics* graphics,
    GDIPCONST WCHAR* str,
    INT length,
    GDIPCONST GpFont* font,
    GDIPCONST Gdiplus::RectF* layoutRect,
    GDIPCONST GpStringFormat* stringFormat,
    GDIPCONST GpBrush* brush);
typedef GpStatus(WINGDIPAPI* FuncType_GdipSetPenTransform)(GpPen* pen,
                                                           GpMatrix* matrix);
#define CallFunc(funcname) \
  ((FuncType_##funcname)GdiplusExt.m_Functions[FuncId_##funcname])

GpBrush* GdipCreateBrushImpl(DWORD argb) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  GpSolidFill* solidBrush = nullptr;
  CallFunc(GdipCreateSolidFill)((Gdiplus::ARGB)argb, &solidBrush);
  return solidBrush;
}

RetainPtr<CFX_DIBitmap> StretchMonoToGray(
    int dest_width,
    int dest_height,
    const RetainPtr<CFX_DIBitmap>& pSource,
    FX_RECT* pClipRect) {
  bool bFlipX = dest_width < 0;
  if (bFlipX)
    dest_width = -dest_width;

  bool bFlipY = dest_height < 0;
  if (bFlipY)
    dest_height = -dest_height;

  int result_width = pClipRect->Width();
  int result_height = pClipRect->Height();
  int result_pitch = (result_width + 3) / 4 * 4;
  auto pStretched = pdfium::MakeRetain<CFX_DIBitmap>();
  if (!pStretched->Create(result_width, result_height, FXDIB_8bppRgb))
    return nullptr;

  LPBYTE dest_buf = pStretched->GetBuffer();
  int src_width = pSource->GetWidth();
  int src_height = pSource->GetHeight();
  int y_unit = src_height / dest_height;
  int x_unit = src_width / dest_width;
  int area_unit = y_unit * x_unit;
  LPBYTE src_buf = pSource->GetBuffer();
  int src_pitch = pSource->GetPitch();
  for (int dest_y = 0; dest_y < result_height; dest_y++) {
    LPBYTE dest_scan = dest_buf + dest_y * result_pitch;
    int src_y_start = bFlipY ? (dest_height - 1 - dest_y - pClipRect->top)
                             : (dest_y + pClipRect->top);
    src_y_start = src_y_start * src_height / dest_height;
    LPBYTE src_scan = src_buf + src_y_start * src_pitch;
    for (int dest_x = 0; dest_x < result_width; dest_x++) {
      int sum = 0;
      int src_x_start = bFlipX ? (dest_width - 1 - dest_x - pClipRect->left)
                               : (dest_x + pClipRect->left);
      src_x_start = src_x_start * src_width / dest_width;
      int src_x_end = src_x_start + x_unit;
      LPBYTE src_line = src_scan;
      for (int src_y = 0; src_y < y_unit; src_y++) {
        for (int src_x = src_x_start; src_x < src_x_end; src_x++) {
          if (!(src_line[src_x / 8] & (1 << (7 - src_x % 8)))) {
            sum += 255;
          }
        }
        src_line += src_pitch;
      }
      dest_scan[dest_x] = 255 - sum / area_unit;
    }
  }
  return pStretched;
}

void OutputImageMask(GpGraphics* pGraphics,
                     BOOL bMonoDevice,
                     const RetainPtr<CFX_DIBitmap>& pBitmap,
                     int dest_left,
                     int dest_top,
                     int dest_width,
                     int dest_height,
                     FX_ARGB argb,
                     const FX_RECT* pClipRect) {
  ASSERT(pBitmap->GetBPP() == 1);
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  int src_width = pBitmap->GetWidth(), src_height = pBitmap->GetHeight();
  int src_pitch = pBitmap->GetPitch();
  uint8_t* scan0 = pBitmap->GetBuffer();
  if (src_width == 1 && src_height == 1) {
    if ((scan0[0] & 0x80) == 0)
      return;

    GpSolidFill* solidBrush;
    CallFunc(GdipCreateSolidFill)((Gdiplus::ARGB)argb, &solidBrush);
    if (dest_width < 0) {
      dest_width = -dest_width;
      dest_left -= dest_width;
    }
    if (dest_height < 0) {
      dest_height = -dest_height;
      dest_top -= dest_height;
    }
    CallFunc(GdipFillRectangle)(pGraphics, solidBrush, (float)dest_left,
                                (float)dest_top, (float)dest_width,
                                (float)dest_height);
    CallFunc(GdipDeleteBrush)(solidBrush);
    return;
  }
  if (!bMonoDevice && abs(dest_width) < src_width &&
      abs(dest_height) < src_height) {
    FX_RECT image_rect(dest_left, dest_top, dest_left + dest_width,
                       dest_top + dest_height);
    image_rect.Normalize();
    FX_RECT image_clip = image_rect;
    image_clip.Intersect(*pClipRect);
    if (image_clip.IsEmpty())
      return;

    image_clip.Offset(-image_rect.left, -image_rect.top);
    RetainPtr<CFX_DIBitmap> pStretched;
    if (src_width * src_height > 10000) {
      pStretched =
          StretchMonoToGray(dest_width, dest_height, pBitmap, &image_clip);
    } else {
      pStretched =
          pBitmap->StretchTo(dest_width, dest_height, false, &image_clip);
    }
    GpBitmap* bitmap;
    CallFunc(GdipCreateBitmapFromScan0)(image_clip.Width(), image_clip.Height(),
                                        (image_clip.Width() + 3) / 4 * 4,
                                        PixelFormat8bppIndexed,
                                        pStretched->GetBuffer(), &bitmap);
    int a;
    int r;
    int g;
    int b;
    std::tie(a, r, g, b) = ArgbDecode(argb);
    UINT pal[258];
    pal[0] = 0;
    pal[1] = 256;
    for (int i = 0; i < 256; i++) {
      pal[i + 2] = ArgbEncode(i * a / 255, r, g, b);
    }
    CallFunc(GdipSetImagePalette)(bitmap, (Gdiplus::ColorPalette*)pal);
    CallFunc(GdipDrawImageI)(pGraphics, bitmap,
                             image_rect.left + image_clip.left,
                             image_rect.top + image_clip.top);
    CallFunc(GdipDisposeImage)(bitmap);
    return;
  }
  GpBitmap* bitmap;
  CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
                                      PixelFormat1bppIndexed, scan0, &bitmap);
  UINT palette[4] = {PaletteFlagsHasAlpha, 2, 0, argb};
  CallFunc(GdipSetImagePalette)(bitmap, (Gdiplus::ColorPalette*)palette);
  Gdiplus::Point destinationPoints[] = {
      Gdiplus::Point(dest_left, dest_top),
      Gdiplus::Point(dest_left + dest_width, dest_top),
      Gdiplus::Point(dest_left, dest_top + dest_height)};
  CallFunc(GdipDrawImagePointsI)(pGraphics, bitmap, destinationPoints, 3);
  CallFunc(GdipDisposeImage)(bitmap);
}

void OutputImage(GpGraphics* pGraphics,
                 const RetainPtr<CFX_DIBitmap>& pBitmap,
                 const FX_RECT* pSrcRect,
                 int dest_left,
                 int dest_top,
                 int dest_width,
                 int dest_height) {
  int src_width = pSrcRect->Width(), src_height = pSrcRect->Height();
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  if (pBitmap->GetBPP() == 1 && (pSrcRect->left % 8)) {
    FX_RECT new_rect(0, 0, src_width, src_height);
    RetainPtr<CFX_DIBitmap> pCloned = pBitmap->Clone(pSrcRect);
    if (!pCloned)
      return;
    OutputImage(pGraphics, pCloned, &new_rect, dest_left, dest_top, dest_width,
                dest_height);
    return;
  }
  int src_pitch = pBitmap->GetPitch();
  uint8_t* scan0 = pBitmap->GetBuffer() + pSrcRect->top * src_pitch +
                   pBitmap->GetBPP() * pSrcRect->left / 8;
  GpBitmap* bitmap = nullptr;
  switch (pBitmap->GetFormat()) {
    case FXDIB_Argb:
      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
                                          PixelFormat32bppARGB, scan0, &bitmap);
      break;
    case FXDIB_Rgb32:
      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
                                          PixelFormat32bppRGB, scan0, &bitmap);
      break;
    case FXDIB_Rgb:
      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
                                          PixelFormat24bppRGB, scan0, &bitmap);
      break;
    case FXDIB_8bppRgb: {
      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
                                          PixelFormat8bppIndexed, scan0,
                                          &bitmap);
      UINT pal[258];
      pal[0] = 0;
      pal[1] = 256;
      for (int i = 0; i < 256; i++)
        pal[i + 2] = pBitmap->GetPaletteArgb(i);
      CallFunc(GdipSetImagePalette)(bitmap, (Gdiplus::ColorPalette*)pal);
      break;
    }
    case FXDIB_1bppRgb: {
      CallFunc(GdipCreateBitmapFromScan0)(src_width, src_height, src_pitch,
                                          PixelFormat1bppIndexed, scan0,
                                          &bitmap);
      break;
    }
  }
  if (dest_height < 0) {
    dest_height--;
  }
  if (dest_width < 0) {
    dest_width--;
  }
  Gdiplus::Point destinationPoints[] = {
      Gdiplus::Point(dest_left, dest_top),
      Gdiplus::Point(dest_left + dest_width, dest_top),
      Gdiplus::Point(dest_left, dest_top + dest_height)};
  CallFunc(GdipDrawImagePointsI)(pGraphics, bitmap, destinationPoints, 3);
  CallFunc(GdipDisposeImage)(bitmap);
}

GpPen* GdipCreatePenImpl(const CFX_GraphStateData* pGraphState,
                         const CFX_Matrix* pMatrix,
                         DWORD argb,
                         bool bTextMode) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  float width = pGraphState->m_LineWidth;
  if (!bTextMode) {
    float unit = pMatrix
                     ? 1.0f / ((pMatrix->GetXUnit() + pMatrix->GetYUnit()) / 2)
                     : 1.0f;
    width = std::max(width, unit);
  }
  GpPen* pPen = nullptr;
  CallFunc(GdipCreatePen1)((Gdiplus::ARGB)argb, width, UnitWorld, &pPen);
  LineCap lineCap = LineCapFlat;
  DashCap dashCap = DashCapFlat;
  bool bDashExtend = false;
  switch (pGraphState->m_LineCap) {
    case CFX_GraphStateData::LineCapButt:
      lineCap = LineCapFlat;
      break;
    case CFX_GraphStateData::LineCapRound:
      lineCap = LineCapRound;
      dashCap = DashCapRound;
      bDashExtend = true;
      break;
    case CFX_GraphStateData::LineCapSquare:
      lineCap = LineCapSquare;
      bDashExtend = true;
      break;
  }
  CallFunc(GdipSetPenLineCap197819)(pPen, lineCap, lineCap, dashCap);
  LineJoin lineJoin = LineJoinMiterClipped;
  switch (pGraphState->m_LineJoin) {
    case CFX_GraphStateData::LineJoinMiter:
      lineJoin = LineJoinMiterClipped;
      break;
    case CFX_GraphStateData::LineJoinRound:
      lineJoin = LineJoinRound;
      break;
    case CFX_GraphStateData::LineJoinBevel:
      lineJoin = LineJoinBevel;
      break;
  }
  CallFunc(GdipSetPenLineJoin)(pPen, lineJoin);
  if (pGraphState->m_DashCount) {
    float* pDashArray = FX_Alloc(
        float, pGraphState->m_DashCount + pGraphState->m_DashCount % 2);
    int nCount = 0;
    float on_leftover = 0, off_leftover = 0;
    for (int i = 0; i < pGraphState->m_DashCount; i += 2) {
      float on_phase = pGraphState->m_DashArray[i];
      float off_phase;
      if (i == pGraphState->m_DashCount - 1)
        off_phase = on_phase;
      else
        off_phase = pGraphState->m_DashArray[i + 1];
      on_phase /= width;
      off_phase /= width;
      if (on_phase + off_phase <= 0.00002f) {
        on_phase = 1.0f / 10;
        off_phase = 1.0f / 10;
      }
      if (bDashExtend) {
        if (off_phase < 1)
          off_phase = 0;
        else
          off_phase -= 1;
        on_phase += 1;
      }
      if (on_phase == 0 || off_phase == 0) {
        if (nCount == 0) {
          on_leftover += on_phase;
          off_leftover += off_phase;
        } else {
          pDashArray[nCount - 2] += on_phase;
          pDashArray[nCount - 1] += off_phase;
        }
      } else {
        pDashArray[nCount++] = on_phase + on_leftover;
        on_leftover = 0;
        pDashArray[nCount++] = off_phase + off_leftover;
        off_leftover = 0;
      }
    }
    CallFunc(GdipSetPenDashArray)(pPen, pDashArray, nCount);
    float phase = pGraphState->m_DashPhase;
    if (bDashExtend) {
      if (phase < 0.5f)
        phase = 0;
      else
        phase -= 0.5f;
    }
    CallFunc(GdipSetPenDashOffset)(pPen, phase);
    FX_Free(pDashArray);
    pDashArray = nullptr;
  }
  CallFunc(GdipSetPenMiterLimit)(pPen, pGraphState->m_MiterLimit);
  return pPen;
}

bool IsSmallTriangle(Gdiplus::PointF* points,
                     const CFX_Matrix* pMatrix,
                     int& v1,
                     int& v2) {
  int pairs[] = {1, 2, 0, 2, 0, 1};
  for (int i = 0; i < 3; i++) {
    int pair1 = pairs[i * 2];
    int pair2 = pairs[i * 2 + 1];

    CFX_PointF p1(points[pair1].X, points[pair1].Y);
    CFX_PointF p2(points[pair2].X, points[pair2].Y);
    if (pMatrix) {
      p1 = pMatrix->Transform(p1);
      p2 = pMatrix->Transform(p2);
    }

    CFX_PointF diff = p1 - p2;
    float distance_square = (diff.x * diff.x) + (diff.y * diff.y);
    if (distance_square < (1.0f * 2 + 1.0f / 4)) {
      v1 = i;
      v2 = pair1;
      return true;
    }
  }
  return false;
}

class GpStream final : public IStream {
 public:
  GpStream() : m_RefCount(1), m_ReadPos(0) {}
  ~GpStream() = default;

  // IUnknown
  HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid,
                                           void** ppvObject) override {
    if (iid == __uuidof(IUnknown) || iid == __uuidof(IStream) ||
        iid == __uuidof(ISequentialStream)) {
      *ppvObject = static_cast<IStream*>(this);
      AddRef();
      return S_OK;
    }
    return E_NOINTERFACE;
  }
  ULONG STDMETHODCALLTYPE AddRef() override {
    return (ULONG)InterlockedIncrement(&m_RefCount);
  }
  ULONG STDMETHODCALLTYPE Release() override {
    ULONG res = (ULONG)InterlockedDecrement(&m_RefCount);
    if (res == 0) {
      delete this;
    }
    return res;
  }

  // ISequentialStream
  HRESULT STDMETHODCALLTYPE Read(void* output,
                                 ULONG cb,
                                 ULONG* pcbRead) override {
    if (pcbRead)
      *pcbRead = 0;

    if (m_ReadPos >= m_InterStream.tellp())
      return HRESULT_FROM_WIN32(ERROR_END_OF_MEDIA);

    size_t bytes_left = m_InterStream.tellp() - m_ReadPos;
    size_t bytes_out =
        std::min(pdfium::base::checked_cast<size_t>(cb), bytes_left);
    memcpy(output, m_InterStream.str().c_str() + m_ReadPos, bytes_out);
    m_ReadPos += bytes_out;
    if (pcbRead)
      *pcbRead = (ULONG)bytes_out;

    return S_OK;
  }
  HRESULT STDMETHODCALLTYPE Write(const void* input,
                                  ULONG cb,
                                  ULONG* pcbWritten) override {
    if (cb <= 0) {
      if (pcbWritten)
        *pcbWritten = 0;
      return S_OK;
    }
    m_InterStream.write(reinterpret_cast<const char*>(input), cb);
    if (pcbWritten)
      *pcbWritten = cb;
    return S_OK;
  }

  // IStream
  HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER) override {
    return E_NOTIMPL;
  }
  HRESULT STDMETHODCALLTYPE CopyTo(IStream*,
                                   ULARGE_INTEGER,
                                   ULARGE_INTEGER*,
                                   ULARGE_INTEGER*) override {
    return E_NOTIMPL;
  }
  HRESULT STDMETHODCALLTYPE Commit(DWORD) override { return E_NOTIMPL; }
  HRESULT STDMETHODCALLTYPE Revert() override { return E_NOTIMPL; }
  HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER,
                                       ULARGE_INTEGER,
                                       DWORD) override {
    return E_NOTIMPL;
  }
  HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER,
                                         ULARGE_INTEGER,
                                         DWORD) override {
    return E_NOTIMPL;
  }
  HRESULT STDMETHODCALLTYPE Clone(IStream** stream) override {
    return E_NOTIMPL;
  }
  HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER liDistanceToMove,
                                 DWORD dwOrigin,
                                 ULARGE_INTEGER* lpNewFilePointer) override {
    std::streamoff start;
    std::streamoff new_read_position;
    switch (dwOrigin) {
      case STREAM_SEEK_SET:
        start = 0;
        break;
      case STREAM_SEEK_CUR:
        start = m_ReadPos;
        break;
      case STREAM_SEEK_END:
        if (m_InterStream.tellp() < 0)
          return STG_E_SEEKERROR;
        start = m_InterStream.tellp();
        break;
      default:
        return STG_E_INVALIDFUNCTION;
    }
    new_read_position = start + liDistanceToMove.QuadPart;
    if (new_read_position > m_InterStream.tellp())
      return STG_E_SEEKERROR;

    m_ReadPos = new_read_position;
    if (lpNewFilePointer)
      lpNewFilePointer->QuadPart = m_ReadPos;

    return S_OK;
  }
  HRESULT STDMETHODCALLTYPE Stat(STATSTG* pStatstg,
                                 DWORD grfStatFlag) override {
    if (!pStatstg)
      return STG_E_INVALIDFUNCTION;

    ZeroMemory(pStatstg, sizeof(STATSTG));

    if (m_InterStream.tellp() < 0)
      return STG_E_SEEKERROR;

    pStatstg->cbSize.QuadPart = m_InterStream.tellp();
    return S_OK;
  }

 private:
  LONG m_RefCount;
  std::streamoff m_ReadPos;
  std::ostringstream m_InterStream;
};

struct PREVIEW3_DIBITMAP {
  BITMAPINFO* pbmi;
  int Stride;
  LPBYTE pScan0;
  GpBitmap* pBitmap;
  Gdiplus::BitmapData* pBitmapData;
  GpStream* pStream;
};

PREVIEW3_DIBITMAP* LoadDIBitmap(WINDIB_Open_Args_ args) {
  GpBitmap* pBitmap;
  GpStream* pStream = nullptr;
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  Gdiplus::Status status = Gdiplus::Ok;
  if (args.flags == WINDIB_OPEN_PATHNAME) {
    status = CallFunc(GdipCreateBitmapFromFileICM)((wchar_t*)args.path_name,
                                                   &pBitmap);
  } else {
    if (args.memory_size == 0 || !args.memory_base)
      return nullptr;

    pStream = new GpStream;
    pStream->Write(args.memory_base, (ULONG)args.memory_size, nullptr);
    status = CallFunc(GdipCreateBitmapFromStreamICM)(pStream, &pBitmap);
  }
  if (status != Gdiplus::Ok) {
    if (pStream)
      pStream->Release();

    return nullptr;
  }
  UINT height, width;
  CallFunc(GdipGetImageHeight)(pBitmap, &height);
  CallFunc(GdipGetImageWidth)(pBitmap, &width);
  Gdiplus::PixelFormat pixel_format;
  CallFunc(GdipGetImagePixelFormat)(pBitmap, &pixel_format);
  int info_size = sizeof(BITMAPINFOHEADER);
  int bpp = 24;
  int dest_pixel_format = PixelFormat24bppRGB;
  if (pixel_format == PixelFormat1bppIndexed) {
    info_size += 8;
    bpp = 1;
    dest_pixel_format = PixelFormat1bppIndexed;
  } else if (pixel_format == PixelFormat8bppIndexed) {
    info_size += 1024;
    bpp = 8;
    dest_pixel_format = PixelFormat8bppIndexed;
  } else if (pixel_format == PixelFormat32bppARGB) {
    bpp = 32;
    dest_pixel_format = PixelFormat32bppARGB;
  }
  LPBYTE buf = FX_Alloc(BYTE, info_size);
  BITMAPINFOHEADER* pbmih = (BITMAPINFOHEADER*)buf;
  pbmih->biBitCount = bpp;
  pbmih->biCompression = BI_RGB;
  pbmih->biHeight = -(int)height;
  pbmih->biPlanes = 1;
  pbmih->biWidth = width;
  Gdiplus::Rect rect(0, 0, width, height);
  Gdiplus::BitmapData* pBitmapData = FX_Alloc(Gdiplus::BitmapData, 1);
  CallFunc(GdipBitmapLockBits)(pBitmap, &rect, ImageLockModeRead,
                               dest_pixel_format, pBitmapData);
  if (pixel_format == PixelFormat1bppIndexed ||
      pixel_format == PixelFormat8bppIndexed) {
    DWORD* ppal = (DWORD*)(buf + sizeof(BITMAPINFOHEADER));
    struct {
      UINT flags;
      UINT Count;
      DWORD Entries[256];
    } pal;
    int size = 0;
    CallFunc(GdipGetImagePaletteSize)(pBitmap, &size);
    CallFunc(GdipGetImagePalette)(pBitmap, (Gdiplus::ColorPalette*)&pal, size);
    int entries = pixel_format == PixelFormat1bppIndexed ? 2 : 256;
    for (int i = 0; i < entries; i++) {
      ppal[i] = pal.Entries[i] & 0x00ffffff;
    }
  }
  PREVIEW3_DIBITMAP* pInfo = FX_Alloc(PREVIEW3_DIBITMAP, 1);
  pInfo->pbmi = (BITMAPINFO*)buf;
  pInfo->pScan0 = (LPBYTE)pBitmapData->Scan0;
  pInfo->Stride = pBitmapData->Stride;
  pInfo->pBitmap = pBitmap;
  pInfo->pBitmapData = pBitmapData;
  pInfo->pStream = pStream;
  return pInfo;
}

void FreeDIBitmap(PREVIEW3_DIBITMAP* pInfo) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipBitmapUnlockBits)(pInfo->pBitmap, pInfo->pBitmapData);
  CallFunc(GdipDisposeImage)(pInfo->pBitmap);
  FX_Free(pInfo->pBitmapData);
  FX_Free((LPBYTE)pInfo->pbmi);
  if (pInfo->pStream)
    pInfo->pStream->Release();
  FX_Free(pInfo);
}

}  // namespace

void* CGdiplusExt::GdiAddFontMemResourceEx(void* pFontdata,
                                           uint32_t size,
                                           void* pdv,
                                           uint32_t* num_face) {
  if (!m_pGdiAddFontMemResourceEx)
    return nullptr;

  return m_pGdiAddFontMemResourceEx((PVOID)pFontdata, (DWORD)size, (PVOID)pdv,
                                    (DWORD*)num_face);
}

bool CGdiplusExt::GdiRemoveFontMemResourceEx(void* handle) {
  return m_pGdiRemoveFontMemResourseEx &&
         m_pGdiRemoveFontMemResourseEx((HANDLE)handle);
}

CGdiplusExt::CGdiplusExt() {}

CGdiplusExt::~CGdiplusExt() {
  FreeLibrary(m_GdiModule);
  FreeLibrary(m_hModule);
}

void CGdiplusExt::Load() {
  char buf[MAX_PATH];
  GetSystemDirectoryA(buf, MAX_PATH);
  ByteString dllpath = buf;
  dllpath += "\\GDIPLUS.DLL";
  m_hModule = LoadLibraryA(dllpath.c_str());
  if (!m_hModule)
    return;

  m_Functions.resize(FX_ArraySize(g_GdipFuncNames));
  for (size_t i = 0; i < FX_ArraySize(g_GdipFuncNames); ++i) {
    m_Functions[i] = GetProcAddress(m_hModule, g_GdipFuncNames[i]);
    if (!m_Functions[i]) {
      m_hModule = nullptr;
      return;
    }
  }

  uintptr_t gdiplusToken;
  GdiplusStartupInput gdiplusStartupInput;
  ((FuncType_GdiplusStartup)m_Functions[FuncId_GdiplusStartup])(
      &gdiplusToken, &gdiplusStartupInput, nullptr);
  m_GdiModule = LoadLibraryA("GDI32.DLL");
  if (!m_GdiModule)
    return;

  m_pGdiAddFontMemResourceEx =
      reinterpret_cast<FuncType_GdiAddFontMemResourceEx>(
          GetProcAddress(m_GdiModule, "AddFontMemResourceEx"));
  m_pGdiRemoveFontMemResourseEx =
      reinterpret_cast<FuncType_GdiRemoveFontMemResourceEx>(
          GetProcAddress(m_GdiModule, "RemoveFontMemResourceEx"));
}

LPVOID CGdiplusExt::LoadMemFont(LPBYTE pData, uint32_t size) {
  GpFontCollection* pCollection = nullptr;
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipNewPrivateFontCollection)(&pCollection);
  GpStatus status =
      CallFunc(GdipPrivateAddMemoryFont)(pCollection, pData, size);
  if (status == Gdiplus::Ok)
    return pCollection;

  CallFunc(GdipDeletePrivateFontCollection)(&pCollection);
  return nullptr;
}
void CGdiplusExt::DeleteMemFont(LPVOID pCollection) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipDeletePrivateFontCollection)((GpFontCollection**)&pCollection);
}
bool CGdiplusExt::GdipCreateBitmap(const RetainPtr<CFX_DIBitmap>& pBitmap,
                                   void** bitmap) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  Gdiplus::PixelFormat format;
  switch (pBitmap->GetFormat()) {
    case FXDIB_Rgb:
      format = PixelFormat24bppRGB;
      break;
    case FXDIB_Rgb32:
      format = PixelFormat32bppRGB;
      break;
    case FXDIB_Argb:
      format = PixelFormat32bppARGB;
      break;
    default:
      return false;
  }
  GpStatus status = CallFunc(GdipCreateBitmapFromScan0)(
      pBitmap->GetWidth(), pBitmap->GetHeight(), pBitmap->GetPitch(), format,
      pBitmap->GetBuffer(), (GpBitmap**)bitmap);
  return status == Gdiplus::Ok;
}
bool CGdiplusExt::GdipCreateFromImage(void* bitmap, void** graphics) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  GpStatus status = CallFunc(GdipGetImageGraphicsContext)(
      (GpBitmap*)bitmap, (GpGraphics**)graphics);
  return status == Gdiplus::Ok;
}
bool CGdiplusExt::GdipCreateFontFamilyFromName(const wchar_t* name,
                                               void* pFontCollection,
                                               void** pFamily) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  GpStatus status = CallFunc(GdipCreateFontFamilyFromName)(
      (GDIPCONST WCHAR*)name, (GpFontCollection*)pFontCollection,
      (GpFontFamily**)pFamily);
  return status == Gdiplus::Ok;
}
bool CGdiplusExt::GdipCreateFontFromFamily(void* pFamily,
                                           float font_size,
                                           int fontstyle,
                                           int flag,
                                           void** pFont) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  GpStatus status =
      CallFunc(GdipCreateFont)((GpFontFamily*)pFamily, font_size, fontstyle,
                               Gdiplus::Unit(flag), (GpFont**)pFont);
  return status == Gdiplus::Ok;
}
void CGdiplusExt::GdipGetFontSize(void* pFont, float* size) {
  REAL get_size;
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  GpStatus status = CallFunc(GdipGetFontSize)((GpFont*)pFont, &get_size);
  *size = (status == Gdiplus::Ok) ? static_cast<float>(get_size) : 0;
}
void CGdiplusExt::GdipSetTextRenderingHint(void* graphics, int mode) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipSetTextRenderingHint)((GpGraphics*)graphics,
                                     (TextRenderingHint)mode);
}
void CGdiplusExt::GdipSetPageUnit(void* graphics, uint32_t unit) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipSetPageUnit)((GpGraphics*)graphics, (GpUnit)unit);
}
bool CGdiplusExt::GdipDrawDriverString(void* graphics,
                                       unsigned short* text,
                                       int length,
                                       void* font,
                                       void* brush,
                                       void* positions,
                                       int flags,
                                       const void* matrix) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  GpStatus status = CallFunc(GdipDrawDriverString)(
      (GpGraphics*)graphics, (GDIPCONST UINT16*)text, (INT)length,
      (GDIPCONST GpFont*)font, (GDIPCONST GpBrush*)brush,
      (GDIPCONST Gdiplus::PointF*)positions, (INT)flags,
      (GDIPCONST GpMatrix*)matrix);
  return status == Gdiplus::Ok;
}
void CGdiplusExt::GdipCreateBrush(uint32_t fill_argb, void** pBrush) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipCreateSolidFill)((Gdiplus::ARGB)fill_argb,
                                (GpSolidFill**)pBrush);
}
void CGdiplusExt::GdipDeleteBrush(void* pBrush) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipDeleteBrush)((GpSolidFill*)pBrush);
}
void* CGdiplusExt::GdipCreateFontFromCollection(void* pFontCollection,
                                                float font_size,
                                                int fontstyle) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  int numFamilies = 0;
  GpStatus status = CallFunc(GdipGetFontCollectionFamilyCount)(
      (GpFontCollection*)pFontCollection, &numFamilies);
  if (status != Gdiplus::Ok)
    return nullptr;

  GpFontFamily* family_list[1];
  status = CallFunc(GdipGetFontCollectionFamilyList)(
      (GpFontCollection*)pFontCollection, 1, family_list, &numFamilies);
  if (status != Gdiplus::Ok)
    return nullptr;

  GpFont* pFont = nullptr;
  status = CallFunc(GdipCreateFont)(family_list[0], font_size, fontstyle,
                                    UnitPixel, &pFont);
  if (status != Gdiplus::Ok)
    return nullptr;

  return pFont;
}
void CGdiplusExt::GdipCreateMatrix(float a,
                                   float b,
                                   float c,
                                   float d,
                                   float e,
                                   float f,
                                   void** matrix) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipCreateMatrix2)(a, b, c, d, e, f, (GpMatrix**)matrix);
}
void CGdiplusExt::GdipDeleteMatrix(void* matrix) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipDeleteMatrix)((GpMatrix*)matrix);
}
void CGdiplusExt::GdipDeleteFontFamily(void* pFamily) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipDeleteFontFamily)((GpFontFamily*)pFamily);
}
void CGdiplusExt::GdipDeleteFont(void* pFont) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipDeleteFont)((GpFont*)pFont);
}
void CGdiplusExt::GdipSetWorldTransform(void* graphics, void* pMatrix) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipSetWorldTransform)((GpGraphics*)graphics, (GpMatrix*)pMatrix);
}
void CGdiplusExt::GdipDisposeImage(void* bitmap) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipDisposeImage)((GpBitmap*)bitmap);
}
void CGdiplusExt::GdipDeleteGraphics(void* graphics) {
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipDeleteGraphics)((GpGraphics*)graphics);
}
bool CGdiplusExt::StretchBitMask(HDC hDC,
                                 BOOL bMonoDevice,
                                 const RetainPtr<CFX_DIBitmap>& pBitmap,
                                 int dest_left,
                                 int dest_top,
                                 int dest_width,
                                 int dest_height,
                                 uint32_t argb,
                                 const FX_RECT* pClipRect,
                                 int flags) {
  ASSERT(pBitmap->GetBPP() == 1);
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  GpGraphics* pGraphics = nullptr;
  CallFunc(GdipCreateFromHDC)(hDC, &pGraphics);
  CallFunc(GdipSetPageUnit)(pGraphics, UnitPixel);
  if (flags & FXDIB_NOSMOOTH) {
    CallFunc(GdipSetInterpolationMode)(pGraphics,
                                       InterpolationModeNearestNeighbor);
  } else {
    CallFunc(GdipSetInterpolationMode)(pGraphics, InterpolationModeHighQuality);
  }
  OutputImageMask(pGraphics, bMonoDevice, pBitmap, dest_left, dest_top,
                  dest_width, dest_height, argb, pClipRect);
  CallFunc(GdipDeleteGraphics)(pGraphics);
  return true;
}
bool CGdiplusExt::StretchDIBits(HDC hDC,
                                const RetainPtr<CFX_DIBitmap>& pBitmap,
                                int dest_left,
                                int dest_top,
                                int dest_width,
                                int dest_height,
                                const FX_RECT* pClipRect,
                                int flags) {
  GpGraphics* pGraphics;
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipCreateFromHDC)(hDC, &pGraphics);
  CallFunc(GdipSetPageUnit)(pGraphics, UnitPixel);
  if (flags & FXDIB_NOSMOOTH) {
    CallFunc(GdipSetInterpolationMode)(pGraphics,
                                       InterpolationModeNearestNeighbor);
  } else if (pBitmap->GetWidth() > abs(dest_width) / 2 ||
             pBitmap->GetHeight() > abs(dest_height) / 2) {
    CallFunc(GdipSetInterpolationMode)(pGraphics, InterpolationModeHighQuality);
  } else {
    CallFunc(GdipSetInterpolationMode)(pGraphics, InterpolationModeBilinear);
  }
  FX_RECT src_rect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight());
  OutputImage(pGraphics, pBitmap, &src_rect, dest_left, dest_top, dest_width,
              dest_height);
  CallFunc(GdipDeleteGraphics)(pGraphics);
  CallFunc(GdipDeleteGraphics)(pGraphics);
  return true;
}

bool CGdiplusExt::DrawPath(HDC hDC,
                           const CFX_PathData* pPathData,
                           const CFX_Matrix* pObject2Device,
                           const CFX_GraphStateData* pGraphState,
                           uint32_t fill_argb,
                           uint32_t stroke_argb,
                           int fill_mode) {
  auto& pPoints = pPathData->GetPoints();
  if (pPoints.empty())
    return true;

  GpGraphics* pGraphics = nullptr;
  CGdiplusExt& GdiplusExt =
      ((CWin32Platform*)CFX_GEModule::Get()->GetPlatformData())->m_GdiplusExt;
  CallFunc(GdipCreateFromHDC)(hDC, &pGraphics);
  CallFunc(GdipSetPageUnit)(pGraphics, UnitPixel);
  CallFunc(GdipSetPixelOffsetMode)(pGraphics, PixelOffsetModeHalf);
  GpMatrix* pMatrix = nullptr;
  if (pObject2Device) {
    CallFunc(GdipCreateMatrix2)(pObject2Device->a, pObject2Device->b,
                                pObject2Device->c, pObject2Device->d,
                                pObject2Device->e, pObject2Device->f, &pMatrix);
    CallFunc(GdipSetWorldTransform)(pGraphics, pMatrix);
  }
  Gdiplus::PointF* points = FX_Alloc(Gdiplus::PointF, pPoints.size());
  BYTE* types = FX_Alloc(BYTE, pPoints.size());
  int nSubPathes = 0;
  bool bSubClose = false;
  int pos_subclose = 0;
  bool bSmooth = false;
  int startpoint = 0;
  for (size_t i = 0; i < pPoints.size(); i++) {
    points[i].X = pPoints[i].m_Point.x;
    points[i].Y = pPoints[i].m_Point.y;

    CFX_PointF pos = pPoints[i].m_Point;
    if (pObject2Device)
      pos = pObject2Device->Transform(pos);

    if (pos.x > 50000 * 1.0f)
      points[i].X = 50000 * 1.0f;
    if (pos.x < -50000 * 1.0f)
      points[i].X = -50000 * 1.0f;
    if (pos.y > 50000 * 1.0f)
      points[i].Y = 50000 * 1.0f;
    if (pos.y < -50000 * 1.0f)
      points[i].Y = -50000 * 1.0f;

    FXPT_TYPE point_type = pPoints[i].m_Type;
    if (point_type == FXPT_TYPE::MoveTo) {
      types[i] = PathPointTypeStart;
      nSubPathes++;
      bSubClose = false;
      startpoint = i;
    } else if (point_type == FXPT_TYPE::LineTo) {
      types[i] = PathPointTypeLine;
      if (pPoints[i - 1].IsTypeAndOpen(FXPT_TYPE::MoveTo) &&
          (i == pPoints.size() - 1 ||
           pPoints[i + 1].IsTypeAndOpen(FXPT_TYPE::MoveTo)) &&
          points[i].Y == points[i - 1].Y && points[i].X == points[i - 1].X) {
        points[i].X += 0.01f;
        continue;
      }
      if (!bSmooth && points[i].X != points[i - 1].X &&
          points[i].Y != points[i - 1].Y)
        bSmooth = true;
    } else if (point_type == FXPT_TYPE::BezierTo) {
      types[i] = PathPointTypeBezier;
      bSmooth = true;
    }
    if (pPoints[i].m_CloseFigure) {
      if (bSubClose)
        types[pos_subclose] &= ~PathPointTypeCloseSubpath;
      else
        bSubClose = true;
      pos_subclose = i;
      types[i] |= PathPointTypeCloseSubpath;
      if (!bSmooth && points[i].X != points[startpoint].X &&
          points[i].Y != points[startpoint].Y)
        bSmooth = true;
    }
  }
  if (fill_mode & FXFILL_NOPATHSMOOTH) {
    bSmooth = false;
    CallFunc(GdipSetSmoothingMode)(pGraphics, SmoothingModeNone);
  } else if (!(fill_mode & FXFILL_FULLCOVER)) {
    if (!bSmooth && (fill_mode & 3))
      bSmooth = true;

    if (bSmooth || (pGraphState && pGraphState->m_LineWidth > 2))
      CallFunc(GdipSetSmoothingMode)(pGraphics, SmoothingModeAntiAlias);
  }
  int new_fill_mode = fill_mode & 3;
  if (pPoints.size() == 4 && !pGraphState) {
    int v1, v2;
    if (IsSmallTriangle(points, pObject2Device, v1, v2)) {
      GpPen* pPen = nullptr;
      CallFunc(GdipCreatePen1)(fill_argb, 1.0f, UnitPixel, &pPen);
      CallFunc(GdipDrawLineI)(
          pGraphics, pPen, FXSYS_round(points[v1].X), FXSYS_round(points[v1].Y),
          FXSYS_round(points[v2].X), FXSYS_round(points[v2].Y));
      CallFunc(GdipDeletePen)(pPen);
      return true;
    }
  }
  GpPath* pGpPath = nullptr;
  CallFunc(GdipCreatePath2)(points, types, pPoints.size(),
                            GdiFillType2Gdip(new_fill_mode), &pGpPath);
  if (!pGpPath) {
    if (pMatrix)
      CallFunc(GdipDeleteMatrix)(pMatrix);

    FX_Free(points);
    FX_Free(types);
    CallFunc(GdipDeleteGraphics)(pGraphics);
    return false;
  }
  if (new_fill_mode) {
    GpBrush* pBrush = GdipCreateBrushImpl(fill_argb);
    CallFunc(GdipSetPathFillMode)(pGpPath, GdiFillType2Gdip(new_fill_mode));
    CallFunc(GdipFillPath)(pGraphics, pBrush, pGpPath);
    CallFunc(GdipDeleteBrush)(pBrush);
  }
  if (pGraphState && stroke_argb) {
    GpPen* pPen = GdipCreatePenImpl(pGraphState, pObject2Device, stroke_argb,
                                    !!(fill_mode & FX_STROKE_TEXT_MODE));
    if (nSubPathes == 1) {
      CallFunc(GdipDrawPath)(pGraphics, pPen, pGpPath);
    } else {
      int iStart = 0;
      for (size_t i = 0; i < pPoints.size(); i++) {
        if (i == pPoints.size() - 1 || types[i + 1] == PathPointTypeStart) {
          GpPath* pSubPath;
          CallFunc(GdipCreatePath2)(points + iStart, types + iStart,
                                    i - iStart + 1,
                                    GdiFillType2Gdip(new_fill_mode), &pSubPath);
          iStart = i + 1;
          CallFunc(GdipDrawPath)(pGraphics, pPen, pSubPath);
          CallFunc(GdipDeletePath)(pSubPath);
        }
      }
    }
    CallFunc(GdipDeletePen)(pPen);
  }
  if (pMatrix)
    CallFunc(GdipDeleteMatrix)(pMatrix);
  FX_Free(points);
  FX_Free(types);
  CallFunc(GdipDeletePath)(pGpPath);
  CallFunc(GdipDeleteGraphics)(pGraphics);
  return true;
}

RetainPtr<CFX_DIBitmap> CGdiplusExt::LoadDIBitmap(WINDIB_Open_Args_ args) {
  PREVIEW3_DIBITMAP* pInfo = ::LoadDIBitmap(args);
  if (!pInfo)
    return nullptr;

  int height = abs(pInfo->pbmi->bmiHeader.biHeight);
  int width = pInfo->pbmi->bmiHeader.biWidth;
  int dest_pitch = (width * pInfo->pbmi->bmiHeader.biBitCount + 31) / 32 * 4;
  LPBYTE pData = FX_Alloc2D(BYTE, dest_pitch, height);
  if (dest_pitch == pInfo->Stride) {
    memcpy(pData, pInfo->pScan0, dest_pitch * height);
  } else {
    for (int i = 0; i < height; i++) {
      memcpy(pData + dest_pitch * i, pInfo->pScan0 + pInfo->Stride * i,
             dest_pitch);
    }
  }
  RetainPtr<CFX_DIBitmap> pDIBitmap = FX_WindowsDIB_LoadFromBuf(
      pInfo->pbmi, pData, pInfo->pbmi->bmiHeader.biBitCount == 32);
  FX_Free(pData);
  FreeDIBitmap(pInfo);
  return pDIBitmap;
}