forked from snowie2000/mactype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directwrite.cpp
2117 lines (1985 loc) · 55.5 KB
/
directwrite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "directwrite.h"
#include "settings.h"
#include "dynCodeHelper.h"
void MyDebug(const TCHAR * sz, ...)
{
#ifdef DEBUG
TCHAR szData[512] = { 0 };
va_list args;
va_start(args, sz);
StringCchVPrintf(szData, sizeof(szData)-1, sz, args);
va_end(args);
OutputDebugString(szData);
#endif
}
#define SET_VAL(x, y) *(DWORD_PTR*)&(x) = *(DWORD_PTR*)&(y)
// To hook a method, add HOOK_MANUALLY() in hooklist.h and use this.
#define HOOK(obj, name, index) { \
if (!HOOK_##name.Link) { \
AutoEnableDynamicCodeGen dynHelper(true); \
SET_VAL(ORIG_##name, (*reinterpret_cast<void***>(obj.p))[index]); \
hook_demand_##name(false); \
if (!HOOK_##name.Link) { MyDebug(L"##name hook failed"); } \
} \
};
struct ComMethodHooker {
// The target function if it has been hooked
void*(*lpGetTargetFunc)();
// The method the vftable refers to
void*(*lpGetMethod)(IUnknown* obj);
// Hook the method
void(*lpHookFunc)(IUnknown* obj);
};
#define COM_METHOD_HOOKER(type, name, index) ComMethodHooker { \
[]() -> void* { \
if (!HOOK_##name.Link) \
return NULL; \
return HOOK_##name.Link->TargetProc; \
}, \
[](IUnknown* obj) -> void* { \
return (*reinterpret_cast<void***>(obj))[index]; \
}, \
[](IUnknown* obj) -> void { \
CComPtr<type> ptr = (type*)obj; \
HOOK(ptr, name, index); \
} \
}
#define COM_METHOD_HOOKER_EMPTY() ComMethodHooker { \
[]() -> void* { \
return NULL; \
}, \
[](IUnknown* obj) -> void* { \
return NULL; \
}, \
[](IUnknown* obj) -> void { \
return; \
} \
}
struct Params {
D2D1_TEXT_ANTIALIAS_MODE AntialiasMode;
// Don't access directly. Use Get(D2D|DW)RenderingParams().
IDWriteRenderingParams *RenderingParams;
FLOAT Gamma;
FLOAT EnhancedContrast;
FLOAT ClearTypeLevel;
DWRITE_PIXEL_GEOMETRY PixelGeometry;
// RenderingMode=6 is invalid for DWrite interface
DWRITE_RENDERING_MODE RenderingMode;
FLOAT GrayscaleEnhancedContrast;
DWRITE_GRID_FIT_MODE GridFitMode;
DWRITE_RENDERING_MODE1 RenderingMode1;
Params();
void CreateParams(IDWriteFactory* dw_factory);
};
//IDWriteFactory* g_pDWriteFactory = NULL;
CComPtr<IDWriteGdiInterop> g_pGdiInterop = NULL;
enum D2D1RenderTargetCategory {
D2D1_RENDER_TARGET_CATEGORY = 1,
// ID2D1DCRenderTarget, ID2D1HwndRenderTarget, ID2D1BitmapRenderTarget
D2D1_RENDER_TARGET1_CATEGORY,
D2D1_DEVICE_CONTEXT_CATEGORY
};
template<typename Intf>
inline HRESULT IfSupport(IUnknown* pUnknown, void(*lpFunc)(Intf*)) {
CComPtr<Intf> comObject;
HRESULT hr = pUnknown->QueryInterface(&comObject);
if (SUCCEEDED(hr)) {
lpFunc(comObject);
}
return hr;
}
void Params::CreateParams(IDWriteFactory *dw_factory)
{
IDWriteFactory3* dw3 = NULL;
IDWriteFactory2* dw2 = NULL;
IDWriteFactory1* dw1 = NULL;
IDWriteRenderingParams3* r3 = NULL;
IDWriteRenderingParams2* r2 = NULL;
IDWriteRenderingParams1* r1 = NULL;
IDWriteRenderingParams* r0 = NULL;
CComPtr<IDWriteFactory> pDWriteFactory;
if (NULL == dw_factory) {
ORIG_DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pDWriteFactory));
dw_factory = pDWriteFactory;
}
HRESULT hr = dw_factory->QueryInterface(&dw3);
if SUCCEEDED(hr) {
hr = dw3->CreateCustomRenderingParams(
this->Gamma,
this->EnhancedContrast,
this->GrayscaleEnhancedContrast,
this->ClearTypeLevel,
this->PixelGeometry,
this->RenderingMode1,
this->GridFitMode,
&r3);
dw3->Release();
if SUCCEEDED(hr) {
RenderingParams = r3;
return;
}
}
hr = dw_factory->QueryInterface(&dw2);
if SUCCEEDED(hr) {
hr = dw2->CreateCustomRenderingParams(
this->Gamma,
this->EnhancedContrast,
this->GrayscaleEnhancedContrast,
this->ClearTypeLevel,
this->PixelGeometry,
this->RenderingMode,
this->GridFitMode,
&r2);
dw2->Release();
if SUCCEEDED(hr) {
RenderingParams = r2;
return;
}
}
hr = dw_factory->QueryInterface(&dw1);
if SUCCEEDED(hr) {
hr = dw1->CreateCustomRenderingParams(
this->Gamma,
this->EnhancedContrast,
this->GrayscaleEnhancedContrast,
this->ClearTypeLevel,
this->PixelGeometry,
this->RenderingMode,
&r1);
dw1->Release();
if SUCCEEDED(hr) {
RenderingParams = r1;
return;
}
}
hr = dw_factory->CreateCustomRenderingParams(
this->Gamma,
this->EnhancedContrast,
this->ClearTypeLevel,
this->PixelGeometry,
this->RenderingMode,
&r0);
if (SUCCEEDED(hr)) {
RenderingParams = r0;
return;
}
RenderingParams = NULL;
}
Params::Params() {
//MessageBox(NULL, L"MakeParam", NULL, MB_OK);
const CGdippSettings* pSettings = CGdippSettings::GetInstanceNoInit();
//
Gamma = pSettings->GammaValueForDW(); //user defined value preferred.
//if (Gamma == 0)
// Gamma = pSettings->GammaValue()*pSettings->GammaValue() > 1.3 ? pSettings->GammaValue()*pSettings->GammaValue() / 2 : 0.7f;
EnhancedContrast = pSettings->ContrastForDW();
ClearTypeLevel = pSettings->ClearTypeLevelForDW();
switch (pSettings->GetFontSettings().GetAntiAliasMode())
{
case 2:
case 4:
PixelGeometry = DWRITE_PIXEL_GEOMETRY_RGB;
break;
case 3:
case 5:
PixelGeometry = DWRITE_PIXEL_GEOMETRY_BGR;
break;
default:
PixelGeometry = DWRITE_PIXEL_GEOMETRY_FLAT;
}
AntialiasMode = (D2D1_TEXT_ANTIALIAS_MODE)D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;
RenderingMode = (DWRITE_RENDERING_MODE)pSettings->RenderingModeForDW();
GrayscaleEnhancedContrast = pSettings->ContrastForDW();
switch (pSettings->GetFontSettings().GetHintingMode())
{
case 0: GridFitMode = DWRITE_GRID_FIT_MODE_DEFAULT;
break;
case 1: GridFitMode = DWRITE_GRID_FIT_MODE_DISABLED;
break;
default:
GridFitMode = DWRITE_GRID_FIT_MODE_ENABLED;
break;
}
RenderingMode1 = (DWRITE_RENDERING_MODE1)pSettings->RenderingModeForDW();
RenderingParams = NULL;
}
Params* GetD2DParams() {
static Params d2dParams;
return &d2dParams;
}
Params* GetDWParams() {
static Params dwParams = [] {
const CGdippSettings* pSettings = CGdippSettings::GetInstanceNoInit();
Params p;
if (pSettings->RenderingModeForDW() == 6) { //DW rendering in mode6 is horrible
p.RenderingMode = DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC;
p.RenderingMode1 = DWRITE_RENDERING_MODE1_NATURAL_SYMMETRIC;
}
//g_DWParams.Gamma = powf(g_D2DParams.Gamma, 1.0 / 3.0);
return p;
}();
return &dwParams;
}
IDWriteRenderingParams* GetD2DRenderingParams(IDWriteRenderingParams* default) {
Params* params = GetD2DParams();
static bool inited = [&] {
params->CreateParams(NULL);
return true;
}();
if (params->RenderingParams)
return params->RenderingParams;
else
return default;
}
IDWriteRenderingParams* GetDWRenderingParams(IDWriteRenderingParams* default) {
Params* params = GetDWParams();
static bool inited = [&] {
params->CreateParams(NULL);
return true;
}();
if (params->RenderingParams)
return params->RenderingParams;
else
return default;
}
// Hook the implementation rather than an interface.
// There are many versions but inside the DLL there's only one implementation
// that supports the latest available one. Older versions are just upcasts,
// they share the same vftable.
void HookFactory(ID2D1Factory* pD2D1Factory) {
static bool loaded = [&] {
HRESULT hr;
CComPtr<ID2D1Factory> ptr = pD2D1Factory;
// index is the index in the vftable. Their order is the same as
// methods declared in the header.
HOOK(ptr, CreateWicBitmapRenderTarget, 13);
HOOK(ptr, CreateHwndRenderTarget, 14);
HOOK(ptr, CreateDxgiSurfaceRenderTarget, 15);
HOOK(ptr, CreateDCRenderTarget, 16);
MyDebug(L"ID2D1Factory hooked");
CComPtr<ID2D1Factory1> ptr1;
hr = pD2D1Factory->QueryInterface(&ptr1);
if (SUCCEEDED(hr)){
HOOK(ptr1, CreateDevice1, 17);
MyDebug(L"ID2D1Factory1 hooked");
}
CComPtr<ID2D1Factory2> ptr2;
hr = pD2D1Factory->QueryInterface(&ptr2);
if (SUCCEEDED(hr)){
HOOK(ptr2, CreateDevice2, 27);
MyDebug(L"ID2D1Factory2 hooked");
}
CComPtr<ID2D1Factory3> ptr3;
hr = pD2D1Factory->QueryInterface(&ptr3);
if (SUCCEEDED(hr)){
HOOK(ptr3, CreateDevice3, 28);
MyDebug(L"ID2D1Factory3 hooked");
}
CComPtr<ID2D1Factory4> ptr4;
hr = pD2D1Factory->QueryInterface(&ptr4);
if (SUCCEEDED(hr)){
HOOK(ptr4, CreateDevice4, 29);
MyDebug(L"ID2D1Factory4 hooked");
}
CComPtr<ID2D1Factory5> ptr5;
hr = pD2D1Factory->QueryInterface(&ptr5);
if (SUCCEEDED(hr)){
HOOK(ptr5, CreateDevice5, 30);
MyDebug(L"ID2D1Factory5 hooked");
}
CComPtr<ID2D1Factory6> ptr6;
hr = pD2D1Factory->QueryInterface(&ptr6);
if (SUCCEEDED(hr)){
HOOK(ptr6, CreateDevice6, 31);
MyDebug(L"ID2D1Factory6 hooked");
}
CComPtr<ID2D1Factory7> ptr7;
hr = pD2D1Factory->QueryInterface(&ptr7);
if (SUCCEEDED(hr)){
HOOK(ptr7, CreateDevice7, 32);
MyDebug(L"ID2D1Factory7 hooked");
}
return true;
}();
}
void HookDevice(ID2D1Device* d2dDevice){
static bool loaded = [&] {
CComPtr<ID2D1Device> ptr = d2dDevice;
HOOK(ptr, CreateDeviceContext, 4);
MyDebug(L"ID2D1Device hooked");
CComPtr<ID2D1Device1> ptr2;
HRESULT hr = (d2dDevice)->QueryInterface(&ptr2);
if SUCCEEDED(hr) {
HOOK(ptr2, CreateDeviceContext2, 11);
MyDebug(L"ID2D1Device1 hooked");
}
CComPtr<ID2D1Device2> ptr3;
hr = (d2dDevice)->QueryInterface(&ptr3);
if SUCCEEDED(hr) {
HOOK(ptr3, CreateDeviceContext3, 12);
MyDebug(L"ID2D1Device2 hooked");
}
CComPtr<ID2D1Device3> ptr4;
hr = (d2dDevice)->QueryInterface(&ptr4);
if SUCCEEDED(hr) {
HOOK(ptr4, CreateDeviceContext4, 15);
MyDebug(L"ID2D1Device3 hooked");
}
CComPtr<ID2D1Device4> ptr5;
hr = (d2dDevice)->QueryInterface(&ptr5);
if SUCCEEDED(hr) {
HOOK(ptr5, CreateDeviceContext5, 16);
MyDebug(L"ID2D1Device4 hooked");
}
CComPtr<ID2D1Device5> ptr6;
hr = (d2dDevice)->QueryInterface(&ptr6);
if SUCCEEDED(hr) {
HOOK(ptr6, CreateDeviceContext6, 17);
MyDebug(L"ID2D1Device5 hooked");
}
CComPtr<ID2D1Device6> ptr7;
hr = (d2dDevice)->QueryInterface(&ptr7);
if SUCCEEDED(hr) {
HOOK(ptr7, CreateDeviceContext7, 18);
MyDebug(L"ID2D1Device6 hooked");
}
return true;
}();
}
// Hook the method if it has not been hooked. It's not thread safe.
void HookRenderTargetMethod(
ID2D1RenderTarget* pD2D1RenderTarget,
D2D1RenderTargetCategory hookCategory,
ComMethodHooker methodHookers[]
) {
void* method = methodHookers[hookCategory].lpGetMethod(pD2D1RenderTarget);
if (D2D1_RENDER_TARGET_CATEGORY != hookCategory) {
void* target = methodHookers[D2D1_RENDER_TARGET_CATEGORY].lpGetTargetFunc();
if (target != NULL && target == method) {
return;
}
}
if (D2D1_RENDER_TARGET1_CATEGORY != hookCategory) {
void* target = methodHookers[D2D1_RENDER_TARGET1_CATEGORY].lpGetTargetFunc();
if (target != NULL && target == method) {
return;
}
}
if (D2D1_DEVICE_CONTEXT_CATEGORY != hookCategory) {
void* target = methodHookers[D2D1_DEVICE_CONTEXT_CATEGORY].lpGetTargetFunc();
if (target != NULL && target == method) {
return;
}
}
methodHookers[hookCategory].lpHookFunc(pD2D1RenderTarget);
}
void HookRenderTarget(
ID2D1RenderTarget* pD2D1RenderTarget,
D2D1RenderTargetCategory hookCategory
){
MyDebug(L"HookRenderTarget %d", hookCategory);
static bool loaded = [&] {
CComPtr<ID2D1RenderTarget> ptr = pD2D1RenderTarget;
HOOK(ptr, CreateCompatibleRenderTarget, 12);
HOOK(ptr, D2D1RenderTarget_DrawTextLayout, 28);
ID2D1Factory* pD2D1Factory;
pD2D1RenderTarget->GetFactory(&pD2D1Factory);
if (pD2D1Factory)
HookFactory(pD2D1Factory);
// Actually it always implements ID2D1DeviceContext regardless of
// hookCategory.
CComPtr<ID2D1DeviceContext> ptr1;
HRESULT hr = pD2D1RenderTarget->QueryInterface(&ptr1);
if (SUCCEEDED(hr)) {
ID2D1Device* pD2D1Device;
ptr1->GetDevice(&pD2D1Device);
if (pD2D1Device)
HookDevice(pD2D1Device);
}
return true;
}();
// Some methods are duplicated across interfaces. Hook them whenever they're
// available from an instance. Make sure don't hook the same function
// multiple times.
//
// Up to two instances of the same function
static ComMethodHooker hookDrawText[] = {
COM_METHOD_HOOKER_EMPTY(),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_DrawText, 27),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_DrawText, 27),
COM_METHOD_HOOKER(ID2D1DeviceContext, D2D1DeviceContext_DrawText, 27)
};
// Up to three instances of the same function
static ComMethodHooker hookDrawGlyphRun[] = {
COM_METHOD_HOOKER_EMPTY(),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_DrawGlyphRun, 29),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget1_DrawGlyphRun, 29),
COM_METHOD_HOOKER(ID2D1DeviceContext, D2D1DeviceContext_DrawGlyphRun, 29)
};
static ComMethodHooker hookSetTextAntialiasMode[] = {
COM_METHOD_HOOKER_EMPTY(),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_SetTextAntialiasMode, 34),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_SetTextAntialiasMode, 34),
COM_METHOD_HOOKER(ID2D1DeviceContext, D2D1DeviceContext_SetTextAntialiasMode, 34)
};
static ComMethodHooker hookSetTextRenderingParams[] = {
COM_METHOD_HOOKER_EMPTY(),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_SetTextRenderingParams, 36),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_SetTextRenderingParams, 36),
COM_METHOD_HOOKER(ID2D1DeviceContext, D2D1DeviceContext_SetTextRenderingParams, 36)
};
// Note that there's a branch in the inheritance hierarchy in
// ID2D1RenderTarget. But the implementation always supports
// ID2D1DeviceContext, thus multiple inheritance should take place. The
// vftable for ID2D1DeviceContext is different if hookCategory is not
// D2D1_DEVICE_CONTEXT_CATEGORY. It consists of thunks each of which adjusts
// the this pointer and then jumps to the corresponding method for
// ID2D1RenderTarget (So we don't have to hook them as well), and the other
// functions for ID2D1DeviceContext.
static ComMethodHooker hookDrawGlyphRun1[] = {
COM_METHOD_HOOKER_EMPTY(),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget_DrawGlyphRun1, 82),
COM_METHOD_HOOKER(ID2D1RenderTarget, D2D1RenderTarget1_DrawGlyphRun1, 82),
COM_METHOD_HOOKER(ID2D1DeviceContext, D2D1DeviceContext_DrawGlyphRun1, 82)
};
if (hookCategory == D2D1_RENDER_TARGET_CATEGORY) {
static bool loaded1 = [&] {
CCriticalSectionLock __lock(CCriticalSectionLock::CS_DWRITE);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookDrawText);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookDrawGlyphRun);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookSetTextAntialiasMode);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookSetTextRenderingParams);
CComPtr<ID2D1DeviceContext> ptr;
HRESULT hr = pD2D1RenderTarget->QueryInterface(&ptr);
if (SUCCEEDED(hr)) {
HookRenderTargetMethod(ptr, hookCategory, hookDrawGlyphRun1);
}
return true;
}();
} else if (hookCategory == D2D1_RENDER_TARGET1_CATEGORY) {
static bool loaded2 = [&] {
CCriticalSectionLock __lock(CCriticalSectionLock::CS_DWRITE);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookDrawText);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookDrawGlyphRun);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookSetTextAntialiasMode);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookSetTextRenderingParams);
CComPtr<ID2D1DeviceContext> ptr;
HRESULT hr = pD2D1RenderTarget->QueryInterface(&ptr);
if (SUCCEEDED(hr)) {
HookRenderTargetMethod(ptr, hookCategory, hookDrawGlyphRun1);
}
return true;
}();
} else if (hookCategory == D2D1_DEVICE_CONTEXT_CATEGORY) {
static bool loaded3 = [&] {
CCriticalSectionLock __lock(CCriticalSectionLock::CS_DWRITE);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookDrawText);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookDrawGlyphRun);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookSetTextAntialiasMode);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookSetTextRenderingParams);
HookRenderTargetMethod(pD2D1RenderTarget, hookCategory, hookDrawGlyphRun1);
return true;
}();
}
pD2D1RenderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_DEFAULT);
if (GetD2DRenderingParams(NULL)) {
pD2D1RenderTarget->SetTextRenderingParams(GetD2DRenderingParams(NULL));
}
}
//DWrite hooks
HRESULT WINAPI IMPL_CreateGlyphRunAnalysis(
IDWriteFactory* This,
DWRITE_GLYPH_RUN const* glyphRun,
FLOAT pixelsPerDip,
DWRITE_MATRIX const* transform,
DWRITE_RENDERING_MODE renderingMode,
DWRITE_MEASURING_MODE measuringMode,
FLOAT baselineOriginX,
FLOAT baselineOriginY,
IDWriteGlyphRunAnalysis** glyphRunAnalysis)
{
HRESULT hr = E_FAIL;
if (FAILED(hr) && renderingMode != DWRITE_RENDERING_MODE_ALIASED) {
MyDebug(L"Try DW2");
IDWriteFactory2* f;
hr = This->QueryInterface(&f);
if (SUCCEEDED(hr)) {
DWRITE_MATRIX m = {};
if (transform) {
m = *transform;
m.m11 *= pixelsPerDip;
m.m12 *= pixelsPerDip;
m.m21 *= pixelsPerDip;
m.m22 *= pixelsPerDip;
m.dx *= pixelsPerDip;
m.dy *= pixelsPerDip;
}
else {
m.m11 = pixelsPerDip;
m.m22 = pixelsPerDip;
}
hr = f->CreateGlyphRunAnalysis(
glyphRun,
&m,
renderingMode,
measuringMode,
DWRITE_GRID_FIT_MODE_DEFAULT,
DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
f->Release();
}
}
if (FAILED(hr) && renderingMode != DWRITE_RENDERING_MODE_ALIASED) {
MyDebug(L"Try DW1");
Params* dwParams = GetDWParams();
DWRITE_MATRIX m;
DWRITE_MATRIX const* pm = transform;
if (dwParams->GridFitMode == DWRITE_GRID_FIT_MODE_DISABLED) {
if (transform) {
m = *transform;
m.m12 += 1.0f / 0xFFFF;
m.m21 += 1.0f / 0xFFFF;
}
else {
m = { 1, 1.0f / 0xFFFF, 1.0f / 0xFFFF, 1 };
}
pm = &m;
}
hr = ORIG_CreateGlyphRunAnalysis(
This,
glyphRun,
pixelsPerDip,
pm,
dwParams->RenderingMode,
measuringMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (FAILED(hr)) {
MyDebug(L"Try Original Params");
hr = ORIG_CreateGlyphRunAnalysis(
This,
glyphRun,
pixelsPerDip,
transform,
renderingMode,
measuringMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (SUCCEEDED(hr)) {
MyDebug(L"CreateGlyphRunAnalysis hooked");
static bool loaded = [&] {
CComPtr<IDWriteGlyphRunAnalysis> ptr = *glyphRunAnalysis;
HOOK(ptr, GetAlphaBlendParams, 5);
return true;
}();
}
return hr;
}
HRESULT WINAPI IMPL_GetGdiInterop(
IDWriteFactory* This,
IDWriteGdiInterop** gdiInterop
)
{
HRESULT hr = ORIG_GetGdiInterop(This, gdiInterop);
static bool loaded = [&] {
CComPtr<IDWriteGdiInterop> gdip = *gdiInterop;
HOOK(gdip, CreateBitmapRenderTarget, 7);
return true;
}();
MyDebug(L"IMPL_GetGdiInterop hooked");
return hr;
}
HRESULT WINAPI IMPL_CreateBitmapRenderTarget(
IDWriteGdiInterop* This,
HDC hdc,
UINT32 width,
UINT32 height,
IDWriteBitmapRenderTarget** renderTarget
)
{
HRESULT hr = ORIG_CreateBitmapRenderTarget(
This,
hdc,
width,
height,
renderTarget
);
if (SUCCEEDED(hr)) {
static bool loaded = [&] {
CComPtr<IDWriteBitmapRenderTarget> ptr = *renderTarget;
HOOK(ptr, BitmapRenderTarget_DrawGlyphRun, 3);
return true;
}();
}
MyDebug(L"CreateBitmapRenderTarget hooked");
return hr;
}
HRESULT WINAPI IMPL_GetAlphaBlendParams(
IDWriteGlyphRunAnalysis* This,
IDWriteRenderingParams* renderingParams,
FLOAT* blendGamma,
FLOAT* blendEnhancedContrast,
FLOAT* blendClearTypeLevel
)
{
HRESULT hr = E_FAIL;
if (FAILED(hr)) {
hr = ORIG_GetAlphaBlendParams(
This,
GetDWRenderingParams(renderingParams),
blendGamma,
blendEnhancedContrast,
blendClearTypeLevel
);
}
if (FAILED(hr)) {
hr = ORIG_GetAlphaBlendParams(
This,
renderingParams,
blendGamma,
blendEnhancedContrast,
blendClearTypeLevel
);
}
MyDebug(L"GetAlphaBlendParams hooked");
return hr;
}
HRESULT WINAPI IMPL_CreateGlyphRunAnalysis2(
IDWriteFactory2* This,
DWRITE_GLYPH_RUN const* glyphRun,
DWRITE_MATRIX const* transform,
DWRITE_RENDERING_MODE renderingMode,
DWRITE_MEASURING_MODE measuringMode,
DWRITE_GRID_FIT_MODE gridFitMode,
DWRITE_TEXT_ANTIALIAS_MODE antialiasMode,
FLOAT baselineOriginX,
FLOAT baselineOriginY,
IDWriteGlyphRunAnalysis** glyphRunAnalysis
)
{
HRESULT hr = E_FAIL;
if (FAILED(hr) && renderingMode != DWRITE_RENDERING_MODE_ALIASED) {
IDWriteFactory3* f;
hr = This->QueryInterface(&f);
if (SUCCEEDED(hr)) {
hr = f->CreateGlyphRunAnalysis(
glyphRun,
transform,
(DWRITE_RENDERING_MODE1)renderingMode,
measuringMode,
gridFitMode,
antialiasMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
f->Release();
}
}
Params* dwParams = GetDWParams();
if (FAILED(hr) && renderingMode != DWRITE_RENDERING_MODE_ALIASED) {
hr = ORIG_CreateGlyphRunAnalysis2(
This,
glyphRun,
transform,
dwParams->RenderingMode,
measuringMode,
dwParams->GridFitMode,
antialiasMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (FAILED(hr)) {
DWRITE_MATRIX m = {};
DWRITE_MATRIX const* pm = transform;
if (dwParams->GridFitMode == DWRITE_GRID_FIT_MODE_DISABLED) {
if (transform) {
m = *transform;
m.m12 += 1.0f / 0xFFFF;
m.m21 += 1.0f / 0xFFFF;
}
else {
m = { 1, 1.0f / 0xFFFF, 1.0f / 0xFFFF, 1 };
}
pm = &m;
}
hr = ORIG_CreateGlyphRunAnalysis2(
This,
glyphRun,
pm,
renderingMode,
measuringMode,
gridFitMode,
antialiasMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (FAILED(hr)) {
hr = ORIG_CreateGlyphRunAnalysis2(
This,
glyphRun,
transform,
renderingMode,
measuringMode,
gridFitMode,
antialiasMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (SUCCEEDED(hr)) {
MyDebug(L"CreateGlyphRunAnalysis2 hooked");
static bool loaded = [&] {
CComPtr<IDWriteGlyphRunAnalysis> ptr = *glyphRunAnalysis;
HOOK(ptr, GetAlphaBlendParams, 5);
return true;
}();
}
return hr;
}
HRESULT WINAPI IMPL_CreateGlyphRunAnalysis3(
IDWriteFactory3* This,
DWRITE_GLYPH_RUN const* glyphRun,
DWRITE_MATRIX const* transform,
DWRITE_RENDERING_MODE1 renderingMode,
DWRITE_MEASURING_MODE measuringMode,
DWRITE_GRID_FIT_MODE gridFitMode,
DWRITE_TEXT_ANTIALIAS_MODE antialiasMode,
FLOAT baselineOriginX,
FLOAT baselineOriginY,
IDWriteGlyphRunAnalysis** glyphRunAnalysis
)
{
MyDebug(L"CreateGlyphRunAnalysis3 hooked");
Params* dwParams = GetDWParams();
HRESULT hr = E_FAIL;
if (FAILED(hr) && renderingMode != DWRITE_RENDERING_MODE1_ALIASED) {
hr = ORIG_CreateGlyphRunAnalysis3(
This,
glyphRun,
transform,
dwParams->RenderingMode1,
measuringMode,
dwParams->GridFitMode,
antialiasMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (FAILED(hr)) {
MyDebug(L"try again with only transformation");
DWRITE_MATRIX m = {};
DWRITE_MATRIX const* pm = transform;
if (dwParams->GridFitMode == DWRITE_GRID_FIT_MODE_DISABLED) {
if (transform) {
m = *transform;
m.m12 += 1.0f / 0xFFFF;
m.m21 += 1.0f / 0xFFFF;
}
else {
m = { 1, 1.0f / 0xFFFF, 1.0f / 0xFFFF, 1 };
}
pm = &m;
}
hr = ORIG_CreateGlyphRunAnalysis3(
This,
glyphRun,
pm,
renderingMode,
measuringMode,
gridFitMode,
antialiasMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (FAILED(hr)) {
MyDebug(L"fallback to original params");
hr = ORIG_CreateGlyphRunAnalysis3(
This,
glyphRun,
transform,
renderingMode,
measuringMode,
gridFitMode,
antialiasMode,
baselineOriginX,
baselineOriginY,
glyphRunAnalysis
);
}
if (SUCCEEDED(hr)) {
MyDebug(L"CreateGlyphRunAnalysis3 hooked");
static bool loaded = [&] {
CComPtr<IDWriteGlyphRunAnalysis> ptr = *glyphRunAnalysis;
HOOK(ptr, GetAlphaBlendParams, 5);
return true;
}();
}
return hr;
}
//d2d1 hooks
HRESULT WINAPI IMPL_D2D1CreateDevice(
IDXGIDevice* dxgiDevice,
CONST D2D1_CREATION_PROPERTIES* creationProperties,
ID2D1Device** d2dDevice) {
HRESULT hr = ORIG_D2D1CreateDevice(
dxgiDevice,
creationProperties,
d2dDevice
);
if (SUCCEEDED(hr)) {
HookDevice(*d2dDevice);
}
MyDebug(L"IMPL_D2D1CreateDevice hooked");
return hr;
}
HRESULT WINAPI IMPL_D2D1CreateDeviceContext(
IDXGISurface* dxgiSurface,
CONST D2D1_CREATION_PROPERTIES* creationProperties,
ID2D1DeviceContext** d2dDeviceContext){
HRESULT hr = ORIG_D2D1CreateDeviceContext(
dxgiSurface,
creationProperties,
d2dDeviceContext
);
if SUCCEEDED(hr) {
HookRenderTarget(*d2dDeviceContext, D2D1_DEVICE_CONTEXT_CATEGORY);
}
MyDebug(L"IMPL_D2D1CreateDeviceContext hooked");
return hr;
}
HRESULT WINAPI IMPL_D2D1CreateFactory(
D2D1_FACTORY_TYPE factoryType,
REFIID riid,
const D2D1_FACTORY_OPTIONS* pFactoryOptions,
void** ppIFactory){
HRESULT hr = ORIG_D2D1CreateFactory(factoryType, riid, pFactoryOptions, ppIFactory);
if (SUCCEEDED(hr)) {
auto pUnknown = reinterpret_cast<IUnknown*>(*ppIFactory);
ID2D1Factory* pD2D1Factory;
HRESULT hr2 = pUnknown->QueryInterface(&pD2D1Factory);
if (SUCCEEDED(hr2)) {
HookFactory(pD2D1Factory);
pD2D1Factory->Release();
}
}
return hr;
}
HRESULT WINAPI IMPL_CreateWicBitmapRenderTarget(
ID2D1Factory* This,
IWICBitmap* target,
const D2D1_RENDER_TARGET_PROPERTIES* renderTargetProperties,
ID2D1RenderTarget** renderTarget
) {
HRESULT hr = ORIG_CreateWicBitmapRenderTarget(
This,
target,
renderTargetProperties,
renderTarget
);
if (SUCCEEDED(hr)) {
HookRenderTarget(*renderTarget, D2D1_RENDER_TARGET_CATEGORY);
}
MyDebug(L"IMPL_CreateWicBitmapRenderTarget hooked");
return hr;
}
HRESULT WINAPI IMPL_CreateHwndRenderTarget(
ID2D1Factory* This,
const D2D1_RENDER_TARGET_PROPERTIES* renderTargetProperties,
const D2D1_HWND_RENDER_TARGET_PROPERTIES* hwndRenderTargetProperties,
ID2D1HwndRenderTarget** hwndRenderTarget
) {
HRESULT hr = ORIG_CreateHwndRenderTarget(
This,
renderTargetProperties,
hwndRenderTargetProperties,
hwndRenderTarget
);
if (SUCCEEDED(hr)) {
HookRenderTarget(*hwndRenderTarget, D2D1_RENDER_TARGET1_CATEGORY);
}
MyDebug(L"IMPL_CreateHwndRenderTarget hooked");
return hr;
}
HRESULT WINAPI IMPL_CreateDxgiSurfaceRenderTarget(
ID2D1Factory* This,
IDXGISurface* dxgiSurface,
const D2D1_RENDER_TARGET_PROPERTIES* renderTargetProperties,
ID2D1RenderTarget** renderTarget
) {
HRESULT hr = ORIG_CreateDxgiSurfaceRenderTarget(
This,
dxgiSurface,
renderTargetProperties,
renderTarget
);