-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
2731 lines (2333 loc) · 130 KB
/
MainWindow.xaml.cs
File metadata and controls
2731 lines (2333 loc) · 130 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using GlobalStructures;
using static GlobalStructures.GlobalTools;
using Direct2D;
using static Direct2D.D2DTools;
using DXGI;
using static DXGI.DXGITools;
using WIC;
using static WIC.WICTools;
using DWrite;
using static DWrite.DWriteTools;
using System.Text;
using Microsoft.UI.Xaml.Documents;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinUI3_SwapChainPanel_DWriteCore
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetUserDefaultLocaleName(StringBuilder lpLocaleName, int cchLocaleName);
public const int LOCALE_NAME_MAX_LENGTH = 85;
private IntPtr hWndMain = IntPtr.Zero;
ID2D1Factory m_pD2DFactory = null;
ID2D1Factory1 m_pD2DFactory1 = null;
IWICImagingFactory m_pWICImagingFactory = null;
IWICImagingFactory2 m_pWICImagingFactory2 = null;
IntPtr m_pD3D11DevicePtr = IntPtr.Zero; // Used in CreateSwapChain
ID3D11DeviceContext m_pD3D11DeviceContext = null; // Released in Clean : not used
IDXGIDevice1 m_pDXGIDevice = null;
ID2D1DeviceContext m_pD2DDeviceContext = null;
ID2D1DeviceContext3 m_pD2DDeviceContext3 = null;
IDXGISwapChain1 m_pDXGISwapChain1 = null;
ID2D1Bitmap1 m_pD2DTargetBitmap = null;
ID2D1SolidColorBrush m_pD2DMainBrush = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushRed = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushGreen = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushBlue = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushWhite = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushPink = null;
ID2D1LinearGradientBrush m_pD2DLinearGradientBrush1 = null;
ID2D1LinearGradientBrush m_pD2DLinearGradientBrush2 = null;
ID2D1Bitmap m_pD2DBitmap1 = null;
ID2D1BitmapBrush m_pD2DBitmapBrush1 = null;
ID2D1BitmapBrush m_pD2DBitmapBrushGold = null;
ID2D1LinearGradientBrush m_pSweepBrush = null;
ID2D1LinearGradientBrush m_pFadeBrush = null;
ID2D1Bitmap m_pD2DBitmap2 = null;
ID2D1Bitmap m_pStarBitmap = null;
IDWriteFactory7 m_pDWriteFactory7 = null;
ID2D1Geometry m_pD2DGeometry1 = null;
ID2D1Geometry m_pD2DGeometry2 = null;
ID2D1Geometry m_pD2DGeometry3 = null;
ID2D1Geometry m_pD2DGeometry4 = null;
ID2D1Geometry m_pD2DGeometry5 = null;
ID2D1Geometry m_pD2DGeometry6 = null;
ID2D1Geometry m_pD2DGeometry7 = null;
ID2D1Geometry m_pD2DGeometry8 = null;
float m_nComputedHeight1 = 0, m_nComputedHeight2 = 0, m_nComputedHeight3 = 0, m_nComputedHeight4 = 0,
m_nComputedHeight5 = 0, m_nComputedHeight6 = 0, m_nComputedHeight7 = 0;
IDWriteTextLayout m_pTextLayout = null;
CustomTextRenderer m_pCTR = null;
public System.Collections.ObjectModel.ObservableCollection<Font> CustomFonts = new System.Collections.ObjectModel.ObservableCollection<Font>();
public System.Collections.ObjectModel.ObservableCollection<Font> SystemFonts;// = new System.Collections.ObjectModel.ObservableCollection<Font>();
public double m_nXPos, m_nYPos, m_nWidth, m_nHeight = 0;
bool m_bUseDirectWrite = false;
public MainWindow()
{
this.InitializeComponent();
hWndMain = WinRT.Interop.WindowNative.GetWindowHandle(this);
this.Title = "WinUI 3 - Test DWriteCore";
Application.Current.Resources["ComboBoxBackgroundPointerOver"] = new SolidColorBrush(Microsoft.UI.Colors.RoyalBlue);
Application.Current.Resources["ComboBoxItemBackgroundSelected"] = new SolidColorBrush(Microsoft.UI.Colors.RoyalBlue);
Application.Current.Resources["ComboBoxItemBackgroundPointerOver"] = new SolidColorBrush(Microsoft.UI.Colors.BlueViolet);
double nDisplayWidth = (Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Width);
double nDisplayHeight = (Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Height);
m_nWidth = 1280;
m_nHeight = 700;
m_nXPos = (nDisplayWidth - m_nWidth) / 2;
m_nYPos = (nDisplayHeight - m_nHeight) / 2;
UpdateWindowSize(hWndMain);
m_pWICImagingFactory = (IWICImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(WICTools.CLSID_WICImagingFactory));
m_pWICImagingFactory2 = (IWICImagingFactory2)m_pWICImagingFactory;
HRESULT hr = CreateD2D1Factory();
if (SUCCEEDED(hr))
{
IntPtr pDWriteFactoryPtr = IntPtr.Zero;
if (m_bUseDirectWrite)
hr = DWriteCreateFactory(DWrite.DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED, ref CLSID_DWriteFactory7, out pDWriteFactoryPtr);
else
hr = DWriteCoreCreateFactory(DWrite.DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED, ref CLSID_DWriteFactory7, out pDWriteFactoryPtr);
if (SUCCEEDED(hr))
{
m_pDWriteFactory7 = Marshal.GetObjectForIUnknown(pDWriteFactoryPtr) as IDWriteFactory7;
if (pDWriteFactoryPtr != IntPtr.Zero)
Marshal.Release(pDWriteFactoryPtr);
string sExePath = AppContext.BaseDirectory;
var pFontCollectionLoader = new FontCollectionLoader();
hr = m_pDWriteFactory7.RegisterFontCollectionLoader(pFontCollectionLoader);
//hr = m_pDWriteFactory7.RegisterFontFileLoader(pFontCollectionLoader);
string sFullPath = System.IO.Path.Combine(sExePath, "Assets");
var pFontCollectionKey = Marshal.StringToHGlobalUni(sFullPath);
IDWriteFontCollection pFontCollection = null;
hr = m_pDWriteFactory7.CreateCustomFontCollection(pFontCollectionLoader, pFontCollectionKey, (sFullPath.Length+1)*2, out pFontCollection);
if (SUCCEEDED(hr))
{
Marshal.FreeHGlobal(pFontCollectionKey);
IDWriteFontCollection2 pFontCollection2 = (IDWriteFontCollection2)pFontCollection;
if (pFontCollection2 != null)
{
IDWriteFontSet1 pFontSet1;
hr = pFontCollection2.GetFontSet(out pFontSet1);
if (SUCCEEDED(hr))
{
hr = LoadFonts(pFontSet1, CustomFonts, true);
SafeRelease(ref pFontSet1);
}
}
//IDWriteFontCollection pFontCollection = null;
//hr = m_pDWriteFactory7.GetSystemFontCollection(out pFontCollection);
//uint nFonts = pFontCollection.GetFontFamilyCount();
IDWriteFontSet2 pFontSet2 = null;
hr = m_pDWriteFactory7.GetSystemFontSet7(false, out pFontSet2);
if (SUCCEEDED(hr))
{
var unorderedSystemFonts = new System.Collections.ObjectModel.ObservableCollection<Font>();
hr = LoadFonts(pFontSet2, unorderedSystemFonts, false);
SystemFonts = new System.Collections.ObjectModel.ObservableCollection<Font>(unorderedSystemFonts.OrderBy(x => x.Name));
unorderedSystemFonts.Clear();
SafeRelease(ref pFontSet2);
}
string sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Lovely Home.ttf");
hr = CreateDWriteTextGeometry("This is a text with slow animated gradient", sPathFont, 70.0f, true, false, false, out m_pD2DGeometry1, out m_nComputedHeight1);
//string sPathFont = "E:\\test\\NotoNastaliqUrdu-Regular.ttf";
//hr = CreateTextGeometry("بچے اپنے فیصلے چاہتے پہلے", sPathFont, 60.0f, false, false, out m_pD2DGeometry1, out m_nComputedHeight1);
//hr = CreateDWriteTextGeometry("بچے اپنے فیصلے چاہتے پہلے", sPathFont, 60.0f, true, false, false, out m_pD2DGeometry1, out m_nComputedHeight1);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Lemon Shake.ttf");
CreateDWriteTextGeometry("This is a text with shadow", sPathFont, 80.0f, true, false, false, out m_pD2DGeometry2, out m_nComputedHeight2);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Play Story.otf");
CreateDWriteTextGeometry("This is a text with turbulence", sPathFont, 60.0f, true, false, false, out m_pD2DGeometry3, out m_nComputedHeight3);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Simplisicky Fill.ttf");
CreateDWriteTextGeometry("This is a text with glowing", sPathFont, 80.0f, true, false, false, out m_pD2DGeometry4, out m_nComputedHeight4);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Bloody Scene.otf");
CreateDWriteTextGeometry("This is a text with Bitmap brush", sPathFont, 70.0f, true, false, false, out m_pD2DGeometry5, out m_nComputedHeight5);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Love Craft.ttf");
CreateDWriteTextGeometry("This is a sample text", sPathFont, 60.0f, true, false, false, out m_pD2DGeometry6, out m_nComputedHeight6);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\MagicSchoolOne.ttf");
CreateDWriteTextGeometry("This is a collapsing text", sPathFont, 90.0f, true, false, false, out m_pD2DGeometry7, out m_nComputedHeight7);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\The Golden Flower.ttf");
CreateDWriteTextGeometry("This is a gold text", sPathFont, 90.0f, true, false, false, out m_pD2DGeometry8, out m_nComputedHeight7);
//string sFontName = "Segoe UI Emoji";
//string sFontName = "Tolkien";
//string sString = "This is a text from Layout";
string sString = "";
for (int nCode = 0x1F980; nCode <= 0x1F980 + 15; nCode++)
sString += char.ConvertFromUtf32(nCode);
// crashes in DWriteCore with nStringLength = sString.Length
//sString = "بچے اپنے فیصلے چاہتے پہلے";
//sString = "بچے اپنے فیصلے چاہتے پہلےبچے اپنے فیصلے چاہتے پہلے";
uint nStringLength = (uint)(sString.Length);
IDWriteTextFormat3 pTextFormat3 = null;
IDWriteTextFormat pTextFormat = null;
//hr = m_pDWriteFactory7.CreateTextFormat(sFontName, pFontCollection2,
hr = m_pDWriteFactory7.CreateTextFormat6("Segoe UI Emoji", null, IntPtr.Zero, 0, 30.0f,
//hr = m_pDWriteFactory7.CreateTextFormat("E:\\test\\NotoNastaliqUrdu-Regular.ttf", null,
//hr = m_pDWriteFactory7.CreateTextFormat("Noto Nastaliq Urdu", null,
// DWRITE_FONT_WEIGHT.DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE.DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH.DWRITE_FONT_STRETCH_NORMAL,
// 30,
// "",
// //"ur-PK",
// out pTextFormat);
//DWRITE_FONT_AXIS_VALUE[] axes = null;
//hr = m_pDWriteFactory7.CreateTextFormat6("Noto Nastaliq Urdu", null, IntPtr.Zero, 0, 30.0f,
//"ur-PK", // locale
"", // locale
out pTextFormat3);
if (m_bUseDirectWrite || (!ContainsStrongArabicCharacters(sString) && !m_bUseDirectWrite))
{
if (SUCCEEDED(hr))
{
hr = pTextFormat3.SetTextAlignment(DWRITE_TEXT_ALIGNMENT.DWRITE_TEXT_ALIGNMENT_CENTER);
hr = pTextFormat3.SetReadingDirection(DWRITE_READING_DIRECTION.DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
//hr = pTextFormat.SetFlowDirection(DWRITE_FLOW_DIRECTION.DWRITE_FLOW_DIRECTION_TOP_TO_BOTTOM);
hr = pTextFormat3.SetAutomaticFontAxes(DWRITE_AUTOMATIC_FONT_AXES.DWRITE_AUTOMATIC_FONT_AXES_OPTICAL_SIZE);
hr = m_pDWriteFactory7.CreateTextLayout(sString, nStringLength, pTextFormat3, 800, 60, out m_pTextLayout);
if (SUCCEEDED(hr))
{
if (m_pTextLayout != null)
{
IDWriteTypography pTypography = null;
hr = m_pDWriteFactory7.CreateTypography(out pTypography);
if (SUCCEEDED(hr))
{
DWRITE_FONT_FEATURE ff = new DWRITE_FONT_FEATURE(
DWRITE_FONT_FEATURE_TAG.DWRITE_FONT_FEATURE_TAG_MARK_POSITIONING, 1);
hr = pTypography.AddFontFeature(ff);
if (SUCCEEDED(hr))
{
DWRITE_TEXT_RANGE tr = new DWRITE_TEXT_RANGE(0, (uint)nStringLength);
hr = m_pTextLayout.SetTypography(pTypography, tr);
}
SafeRelease(ref pTypography);
}
IDWriteTextLayout4 pTextLayout4 = (IDWriteTextLayout4)m_pTextLayout;
//m_pTextLayout.SetLocaleName("ur-PK", new DWRITE_TEXT_RANGE
//{
// startPosition = 0,
// length = (uint)sString.Length
//});
//var fontFamilyName = new StringBuilder(19);
//var f = pTextLayout4.SetCharacterSpacing(10, 5, 1, new DWRITE_TEXT_RANGE());
DWRITE_TEXT_METRICS textMetrics;
// hr = -2003283967 0x88985001 DWRITE_E_UNEXPECTED
// -2003283957 0x8898500B DWRITE_E_FLOWDIRECTIONCONFLICTS
// Microsoft C++ exception: ?? ::st_panic at memory location 0x000000729757B5A0.
hr = pTextLayout4.GetMetrics(out textMetrics);
}
//IDWriteTypography pTypography = null;
//hr = m_pDWriteFactory7.CreateTypography(out pTypography);
//if (SUCCEEDED(hr))
//{
// DWRITE_FONT_FEATURE ff = new DWRITE_FONT_FEATURE (
// DWRITE_FONT_FEATURE_TAG.DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_7, 1);
// hr = pTypography.AddFontFeature(ff);
// if (SUCCEEDED(hr))
// {
// DWRITE_TEXT_RANGE tr = new DWRITE_TEXT_RANGE (0, (uint)nStringLength);
// hr = m_pTextLayout.SetTypography(pTypography, tr);
// }
// SafeRelease(ref pTypography);
//}
}
SafeRelease(ref pTextFormat);
}
SafeRelease(ref pFontCollection);
}
}
}
ContentFrame.Navigate(typeof(MainPage));
NavView.SelectedItem = NavView.MenuItems[0];
hr = CreateDeviceContext();
hr = CreateDeviceResources();
hr = CreateSwapChain(IntPtr.Zero);
if (SUCCEEDED(hr))
{
hr = ConfigureSwapChain(hWndMain);
var pageInstance = ContentFrame.Content as MainPage;
ISwapChainPanelNative panelNative = WinRT.CastExtensions.As<ISwapChainPanelNative>(pageInstance.scp1);
hr = panelNative.SetSwapChain(m_pDXGISwapChain1);
pageInstance.scp1.SizeChanged += scp1_SizeChanged;
}
m_pCTR = new CustomTextRenderer((IDWriteFactory4)m_pDWriteFactory7, (ID2D1DeviceContext4)m_pD2DDeviceContext3, new ColorF(ColorF.Enum.Orange, 1.0f), hWndMain);
CompositionTarget.Rendering += CompositionTarget_Rendering;
}
this.Closed += MainWindow_Closed;
}
int m_nTextEffect = (int)TEXT_EFFECT.GRADIENT;
public enum TEXT_EFFECT : int
{
GRADIENT = 0,
SHADOW = 1,
TURBULENCE = 2,
GLOWING = 3,
BITMAP = 4,
SCROLLING = 5,
COLLAPSE = 6,
POINT_DIFFUSE_LIGHTING = 7,
GOLD = 8,
WAVES = 9,
SCALE = 10,
}
private void NavView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.InvokedItem is NavItem navItem)
{
switch (navItem.Tag)
{
case "Gradient":
m_nTextEffect = (int)TEXT_EFFECT.GRADIENT;
break;
case "Shadow":
m_nTextEffect = (int)TEXT_EFFECT.SHADOW;
break;
case "Turbulence":
m_nTextEffect = (int)TEXT_EFFECT.TURBULENCE;
break;
case "Waves":
m_nTextEffect = (int)TEXT_EFFECT.WAVES;
_animClock.Restart();
break;
case "Glowing":
m_nTextEffect = (int)TEXT_EFFECT.GLOWING;
break;
case "Bitmap":
m_nTextEffect = (int)TEXT_EFFECT.BITMAP;
break;
case "Scrolling":
m_nTextEffect = (int)TEXT_EFFECT.SCROLLING;
break;
case "Collapse":
m_nTextEffect = (int)TEXT_EFFECT.COLLAPSE;
break;
case "Point-diffuse lighting":
m_nTextEffect = (int)TEXT_EFFECT.POINT_DIFFUSE_LIGHTING;
break;
case "Gold":
m_nTextEffect = (int)TEXT_EFFECT.GOLD;
_animClock.Restart();
break;
case "Scale":
m_nTextEffect = (int)TEXT_EFFECT.SCALE;
_animClock.Restart();
break;
}
}
}
private void UpdateWindowSize(IntPtr hWnd)
{
//uint nDPI = GetDpiForWindow(hWnd);
//double nScaleX = (double)nDPI / 96.0f;
//double nScaleY = (double)nDPI / 96.0f;
this.AppWindow.MoveAndResize(new Windows.Graphics.RectInt32((int)(m_nXPos), (int)(m_nYPos), (int)(m_nWidth), (int)(m_nHeight)));
// this.AppWindow.MoveAndResize(new Windows.Graphics.RectInt32((int)(m_nXPos * nScaleX), (int)(m_nYPos * nScaleY), (int)(m_nWidth * nScaleX), (int)(m_nHeight * nScaleY)));
//Console.Beep(5000, 10);
}
private HRESULT LoadFonts(IDWriteFontSet pFontSet, System.Collections.ObjectModel.ObservableCollection<Font> pFonts, bool bCustom)
{
HRESULT hr = HRESULT.S_OK;
string sExePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
uint nFontCount = pFontSet.GetFontCount();
for (uint i = 0; i < nFontCount; i++)
{
string sPath = "";
string sFamilyName = "";
string sFullName = "";
string sFontWeight = "Normal";
int nWeightValue = 400;
string sStyle = "Normal";
string sStretch = "Normal";
bool bPropertyExists = false;
IDWriteLocalizedStrings pLocalizedStrings = null;
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_FULL_NAME,
out bPropertyExists, out pLocalizedStrings);
if (SUCCEEDED(hr))
{
uint nIndex = 0;
bool bExists = false;
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
int nDefaultLocaleSuccess = GetUserDefaultLocaleName(sbLocaleName, LOCALE_NAME_MAX_LENGTH);
if (nDefaultLocaleSuccess > 0)
{
hr = pLocalizedStrings.FindLocaleName(sbLocaleName.ToString(), out nIndex, out bExists);
}
if (hr == HRESULT.S_OK && !bExists)
{
hr = pLocalizedStrings.FindLocaleName("en-us", out nIndex, out bExists);
}
if (!bExists)
nIndex = 0;
hr = pLocalizedStrings.GetString(nIndex, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
sFullName = sbLocaleName.ToString();
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_FAMILY_NAME,
out bPropertyExists, out pLocalizedStrings);
if (SUCCEEDED(hr))
{
uint nIndex = 0;
bool bExists = false;
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
int nDefaultLocaleSuccess = GetUserDefaultLocaleName(sbLocaleName, LOCALE_NAME_MAX_LENGTH);
if (nDefaultLocaleSuccess > 0)
{
hr = pLocalizedStrings.FindLocaleName(sbLocaleName.ToString(), out nIndex, out bExists);
}
if (hr == HRESULT.S_OK && !bExists)
{
hr = pLocalizedStrings.FindLocaleName("en-us", out nIndex, out bExists);
}
if (!bExists)
nIndex = 0;
hr = pLocalizedStrings.GetString(nIndex, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
sFamilyName = sbLocaleName.ToString();
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_WEIGHT,
out bPropertyExists, out pLocalizedStrings);
if (SUCCEEDED(hr))
{
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
hr = pLocalizedStrings.GetString(0, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
string sWeightString = sbLocaleName.ToString();
Int32.TryParse(sWeightString, out nWeightValue);
sFontWeight = ConvertStringToFontWeightString(sWeightString);
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_STYLE,
out bPropertyExists, out pLocalizedStrings);
if (SUCCEEDED(hr))
{
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
hr = pLocalizedStrings.GetString(0, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
string sStyleString = sbLocaleName.ToString();
sStyle = ConvertStringToFontStyleString(sStyleString);
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_STRETCH,
out bPropertyExists, out pLocalizedStrings);
if (SUCCEEDED(hr))
{
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
hr = pLocalizedStrings.GetString(0, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
string sStretchString = sbLocaleName.ToString();
sStretch = ConvertStringToFontStretchString(sStretchString);
SafeRelease(ref pLocalizedStrings);
}
IDWriteFontFaceReference pFontFaceReference = null;
hr = pFontSet.GetFontFaceReference(i, out pFontFaceReference);
if (SUCCEEDED(hr))
{
IDWriteFontFace3 pFontFace3;
hr = pFontFaceReference.CreateFontFace(out pFontFace3);
if (SUCCEEDED(hr))
{
uint nNbFiles = 0;
hr = pFontFace3.GetFiles(ref nNbFiles, null);
IDWriteFontFile[] fontFiles = new IDWriteFontFile[nNbFiles];
hr = pFontFace3.GetFiles(ref nNbFiles, fontFiles);
if (SUCCEEDED(hr))
{
IDWriteFontFileLoader pFontFileLoader = null;
hr = fontFiles[0].GetLoader(out pFontFileLoader);
if (SUCCEEDED(hr))
{
IntPtr pFontFileReferenceKey = IntPtr.Zero;
IDWriteLocalFontFileLoader pLocalFontFileLoader = null;
try
{
pLocalFontFileLoader = (IDWriteLocalFontFileLoader)pFontFileLoader;
}
catch (System.Exception ex)
{
string sError = ex.Message + "\r\n" + "HRESULT = 0x" + string.Format("{0:X}", ex.HResult);
System.Diagnostics.Debug.WriteLine(sError);
}
if (pLocalFontFileLoader != null)
{
int nFontFileReferenceKeySize = 0;
hr = fontFiles[0].GetReferenceKey(out pFontFileReferenceKey, out nFontFileReferenceKeySize);
if (SUCCEEDED(hr))
{
uint nFilePathLength = 0;
hr = pLocalFontFileLoader.GetFilePathLengthFromKey(pFontFileReferenceKey, (uint)nFontFileReferenceKeySize, out nFilePathLength);
if (SUCCEEDED(hr))
{
StringBuilder sbFilePath = new StringBuilder((int)nFilePathLength + 1);
hr = pLocalFontFileLoader.GetFilePathFromKey(pFontFileReferenceKey, (uint)nFontFileReferenceKeySize, sbFilePath, nFilePathLength + 1);
if (SUCCEEDED(hr))
{
sPath = sbFilePath.ToString();
}
}
}
}
SafeRelease(ref pFontFileLoader);
}
}
SafeRelease(ref pFontFace3);
}
SafeRelease(ref pFontFaceReference);
}
if (sPath != "")
{
string sRelativePath = "";
if (sPath.StartsWith(sExePath, StringComparison.CurrentCultureIgnoreCase))
sRelativePath = sPath.Substring(sExePath.Length);//.TrimStart(System.IO.Path.DirectorySeparatorChar);
else
sRelativePath = sPath;
if (bCustom)
pFonts.Add(new Font(sFullName, sPath, sRelativePath + "#" + sFamilyName, sFontWeight, nWeightValue, sStyle, sStretch));
else
pFonts.Add(new Font(sFullName, sPath, sRelativePath + "#" + sFullName, sFontWeight, nWeightValue, sStyle, sStretch));
}
}
return hr;
}
private static string ConvertStringToFontWeightString(string sString)
{
string sFWString = sString switch
{
"100" => "Thin",
"200" => "ExtraLight",
"300" => "Light",
"350" => "SemiLight",
"400" => "Normal",
"500" => "Medium",
"600" => "SemiBold",
"700" => "Bold",
"800" => "ExtraBold",
"900" => "Black",
"950" => "ExtraBlack",
_ => "Normal",
};
return sFWString;
}
private static string ConvertStringToFontStyleString(string sString)
{
string sFWString = sString switch
{
"0" => "Normal",
"1" => "Oblique",
"2" => "Italic",
_ => "Normal",
};
return sFWString;
}
private static string ConvertStringToFontStretchString(string sString)
{
string sFWString = sString switch
{
"0" => "Undefined",
"1" => "UltraCondensed",
"2" => "ExtraCondensed",
"3" => "Condensed",
"4" => "SemiCondensed",
"5" => "Normal",
"6" => "SemiExpanded",
"7" => "Expanded",
"8" => "ExtraExpanded",
"9" => "UltraExpanded",
_ => "Normal",
};
return sFWString;
}
private void CompositionTarget_Rendering(object sender, object e)
{
HRESULT hr = HRESULT.S_OK;
hr = Render();
}
HRESULT Render()
{
HRESULT hr = HRESULT.S_OK;
if (m_pD2DDeviceContext != null)
{
m_pD2DDeviceContext.BeginDraw();
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Identity());
m_pD2DDeviceContext.GetSize(out D2D1_SIZE_F size);
m_pD2DDeviceContext.Clear(new ColorF(ColorF.Enum.Black, 1.0f));
D2D1_RECT_F backgroundRect = new D2D1_RECT_F(0.0f, 0.0f, size.width, size.height);
m_pD2DLinearGradientBrush2.SetStartPoint(Point2F(0, 0));
m_pD2DLinearGradientBrush2.SetEndPoint(Point2F(0, size.height));
m_pD2DDeviceContext.FillRectangle(ref backgroundRect, m_pD2DLinearGradientBrush2);
//if (m_pD2DBitmap1 != null)
//{
// m_pD2DBitmap1.GetSize(out D2D1_SIZE_F sizeBmpBackground);
// D2D1_RECT_F destRectBackground = new D2D1_RECT_F(0.0f, 0.0f, size.width, size.height);
// D2D1_RECT_F sourceRectBackground = new D2D1_RECT_F(0.0f, 0.0f, sizeBmpBackground.width, sizeBmpBackground.height);
// //m_pD2DDeviceContext.DrawBitmap(m_pD2DBitmap1, ref destRectBackground, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE.D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, ref sourceRectBackground);
//}
//m_pD2DDeviceContext.FillRectangle(RectF(10.0f, 10.0f, 200.0f, 200.0f), m_pD2DSolidColorBrushPink);
//m_pD2DDeviceContext.FillEllipse(Ellipse(new Direct2D.D2D1_POINT_2F(300, 300), 100.0f, 100.0f), m_pD2DSolidColorBrushRed);
// Test IDWriteTextLayout
//if (m_pTextLayout != null)
//{
// if (m_pCTR != null)
// {
// hr = m_pTextLayout.Draw(IntPtr.Zero, m_pCTR, 0, 0);
// if (SUCCEEDED(hr))
// {
// if (m_pCTR.IsDWriteCore)
// {
// hr = m_pCTR.DrawBitmapRenderTarget(Point2F(0, 0));
// }
// }
// }
// // DWrite only
// // m_pD2DDeviceContext.DrawTextLayout(Point2F(100, 100), m_pTextLayout, m_pD2DSolidColorBrushPink,
// // D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NO_SNAP | D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
//}
if (m_nTextEffect == (int)TEXT_EFFECT.GRADIENT)
{
if (m_pD2DGeometry1 != null)
{
CenterGeometry(m_pD2DDeviceContext, m_pD2DGeometry1, 1.0f);
m_pD2DDeviceContext.DrawGeometry(m_pD2DGeometry1, m_pD2DMainBrush, 2.0f);
// Animate gradient
var translateMatrix = Matrix3x2F.Translation(new D2D1_SIZE_F(0, m_nY1));
m_pD2DLinearGradientBrush1.SetTransform(translateMatrix);
m_nY1 += 1.0f;
if (m_nY1 >= m_nComputedHeight1 * 1.33f)
{
m_nY1 = 0.0f;
}
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry1, m_pD2DLinearGradientBrush1);
}
}
else if (m_nTextEffect == (int)TEXT_EFFECT.SHADOW)
{
if (m_pD2DGeometry2 != null)
{
CenterGeometry(m_pD2DDeviceContext, m_pD2DGeometry2, 0.99f);
// Save transform
//m_pD2DDeviceContext.GetTransform(out var savedTransform);
//D2D1_MATRIX_3X2_F mSavedTransform = ToClass(savedTransform);
//m_pD2DDeviceContext.SetTransform(Matrix3x2F.Identity());
m_pD2DGeometry2.GetBounds(null, out var geoBounds);
// Padding
float shadowOffset = Math.Abs(m_nShadowTranslate);
float shadowBlur = 3.0f; // default D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION
float padding = shadowBlur * 3 + shadowOffset;
D2D1_RECT_F inflatedBounds = new D2D1_RECT_F(
geoBounds.left - padding,
geoBounds.top - padding,
geoBounds.right + padding,
geoBounds.bottom + padding
);
D2D1_SIZE_F rtSize = new D2D1_SIZE_F(inflatedBounds.right - inflatedBounds.left, inflatedBounds.bottom - inflatedBounds.top);
Direct2D.D2D1_SIZE_U rtSizeU = SizeU((uint)Math.Ceiling(rtSize.width), (uint)Math.Ceiling(rtSize.height));
hr = m_pD2DDeviceContext.CreateCompatibleRenderTarget(
ref rtSize,
ref rtSizeU,
PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED),
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE,
out ID2D1BitmapRenderTarget pCompatibleRenderTarget);
if (SUCCEEDED(hr))
{
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
// Move geometry into bitmap space
pCompatibleRenderTarget.SetTransform(Matrix3x2F.Translation(-inflatedBounds.left, -inflatedBounds.top));
pCompatibleRenderTarget.FillGeometry(m_pD2DGeometry2, m_pD2DMainBrush);
pCompatibleRenderTarget.EndDraw(out _, out _);
ID2D1Bitmap pCompatibleBitmap = null;
pCompatibleRenderTarget.GetBitmap(out pCompatibleBitmap);
ID2D1Effect pShadowEffect = null;
m_pD2DDeviceContext.CreateEffect(CLSID_D2D1Shadow, out pShadowEffect);
pShadowEffect.SetInput(0, pCompatibleBitmap);
//SetEffectFloat(pShadowEffect, (uint)D2D1_SHADOW_PROP.D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION, 4.0f);
// RGBA
//float[] aFloatArray = {0.0f, 0x14/255.0f, 0.0f, 1.0f };
//SetEffectFloatArray(pShadowEffect, (uint)D2D1_SHADOW_PROP.D2D1_SHADOW_PROP_COLOR, aFloatArray);
var ptShadowPos = Point2F(inflatedBounds.left + m_nShadowTranslate, inflatedBounds.top + m_nShadowTranslate);
//m_pD2DDeviceContext.SetTransform(mSavedTransform);
// No sourceRectangle (fixes clipping)
m_pD2DDeviceContext.DrawImage((ID2D1Image)pShadowEffect, ref ptShadowPos,
IntPtr.Zero,
D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
// Animate shadow
m_nShadowTranslate += 0.2f * m_nShadowTranslateDirection;
if (m_nShadowTranslate >= 16.0f && m_nShadowTranslateDirection > 0 || m_nShadowTranslate <= 4.0f && m_nShadowTranslateDirection < 0)
{
m_nShadowTranslateDirection = -m_nShadowTranslateDirection;
}
SafeRelease(ref pShadowEffect);
SafeRelease(ref pCompatibleBitmap);
SafeRelease(ref pCompatibleRenderTarget);
}
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry2, m_pD2DSolidColorBrushGreen);
}
}
else if (m_nTextEffect == (int)TEXT_EFFECT.TURBULENCE)
{
if (m_pD2DGeometry3 != null)
{
CenterGeometry(m_pD2DDeviceContext, m_pD2DGeometry3, 0.99f);
m_pD2DGeometry3.GetBounds(null, out var geoBounds);
// Padding
float padding = 40.0f;
var inflatedBounds = new D2D1_RECT_F(
geoBounds.left - padding,
geoBounds.top - padding,
geoBounds.right + padding,
geoBounds.bottom + padding);
D2D1_SIZE_F rtSize = new D2D1_SIZE_F(inflatedBounds.right - inflatedBounds.left, inflatedBounds.bottom - inflatedBounds.top);
Direct2D.D2D1_SIZE_U rtSizeU = SizeU((uint)Math.Ceiling(rtSize.width), (uint)Math.Ceiling(rtSize.height));
hr = m_pD2DDeviceContext.CreateCompatibleRenderTarget(
ref rtSize,
ref rtSizeU,
PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED),
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE,
out ID2D1BitmapRenderTarget pCompatibleRenderTarget);
if (SUCCEEDED(hr))
{
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
// Move geometry into bitmap space
pCompatibleRenderTarget.SetTransform(Matrix3x2F.Translation(-inflatedBounds.left, -inflatedBounds.top));
pCompatibleRenderTarget.FillGeometry(m_pD2DGeometry3, m_pD2DSolidColorBrushWhite);
pCompatibleRenderTarget.EndDraw(out _, out _);
// Get bitmap of text
pCompatibleRenderTarget.GetBitmap(out ID2D1Bitmap pTextBitmap);
m_pD2DDeviceContext.CreateEffect(D2DTools.CLSID_D2D1Turbulence, out ID2D1Effect pTurbulenceEffect);
SetEffectFloatArray(pTurbulenceEffect, (uint)D2D1_TURBULENCE_PROP.D2D1_TURBULENCE_PROP_BASE_FREQUENCY, new float[] { m_nTurbulenceBaseFrequencyX, 0.0f });
SetEffectInt(pTurbulenceEffect, (uint)D2D1_TURBULENCE_PROP.D2D1_TURBULENCE_PROP_NUM_OCTAVES, 1);
SetEffectFloatArray(pTurbulenceEffect, (uint)D2D1_TURBULENCE_PROP.D2D1_TURBULENCE_PROP_SIZE, new float[] { rtSize.width, rtSize.height });
m_pD2DDeviceContext.CreateEffect(D2DTools.CLSID_D2D1DisplacementMap, out ID2D1Effect pDisplacementMapEffect);
pDisplacementMapEffect.SetInput(0, pTextBitmap); // original text
pDisplacementMapEffect.SetInput(1, (ID2D1Image)pTurbulenceEffect);
SetEffectFloat(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_SCALE, 12.0f); // wave amplitude in pixels
SetEffectInt(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_X_CHANNEL_SELECT, (uint)D2D1_CHANNEL_SELECTOR.D2D1_CHANNEL_SELECTOR_R);
SetEffectInt(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_Y_CHANNEL_SELECT, (uint)D2D1_CHANNEL_SELECTOR.D2D1_CHANNEL_SELECTOR_G);
m_pD2DDeviceContext.DrawImage(
(ID2D1Image)pDisplacementMapEffect,
Point2F(inflatedBounds.left, inflatedBounds.top),
IntPtr.Zero,
D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR,
D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
m_nTurbulenceBaseFrequencyX += 0.0005f * m_nTurbulenceDirection;
if (m_nTurbulenceBaseFrequencyX >= 0.02f && m_nTurbulenceDirection > 0 ||
m_nTurbulenceBaseFrequencyX <= 0.0f && m_nTurbulenceDirection < 0)
{
m_nTurbulenceDirection = -m_nTurbulenceDirection;
}
SafeRelease(ref pDisplacementMapEffect);
SafeRelease(ref pTurbulenceEffect);
SafeRelease(ref pTextBitmap);
SafeRelease(ref pCompatibleRenderTarget);
}
}
}
else if (m_nTextEffect == (int)TEXT_EFFECT.GLOWING)
{
if (m_pD2DGeometry4 != null)
{
CenterGeometry(m_pD2DDeviceContext, m_pD2DGeometry4, 0.99f);
// Save transform
//m_pD2DDeviceContext.GetTransform(out var savedTransform);
//D2D1_MATRIX_3X2_F mSavedTransform = ToClass(savedTransform);
//m_pD2DDeviceContext.SetTransform(Matrix3x2F.Identity());
m_pD2DGeometry4.GetBounds(null, out var geoBounds);
// Padding
float shadowOffset = Math.Abs(m_nShadowTranslate);
float shadowBlur = 20.0f; // default D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION
float padding = shadowBlur * 3 + shadowOffset;
D2D1_RECT_F inflatedBounds = new D2D1_RECT_F(
geoBounds.left - padding,
geoBounds.top - padding,
geoBounds.right + padding,
geoBounds.bottom + padding
);
D2D1_SIZE_F rtSize = new D2D1_SIZE_F(inflatedBounds.right - inflatedBounds.left, inflatedBounds.bottom - inflatedBounds.top);
Direct2D.D2D1_SIZE_U rtSizeU = SizeU((uint)Math.Ceiling(rtSize.width), (uint)Math.Ceiling(rtSize.height));
hr = m_pD2DDeviceContext.CreateCompatibleRenderTarget(ref rtSize, ref rtSizeU, PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED),
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, out ID2D1BitmapRenderTarget pCompatibleRenderTarget);
if (SUCCEEDED(hr))
{
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
// Move geometry into bitmap space
pCompatibleRenderTarget.SetTransform(Matrix3x2F.Translation(-inflatedBounds.left, -inflatedBounds.top));
pCompatibleRenderTarget.FillGeometry(m_pD2DGeometry4, m_pD2DSolidColorBrushPink);
hr = pCompatibleRenderTarget.EndDraw(out UInt64 tag11, out UInt64 tag21);
ID2D1Bitmap pCompatibleBitmap = null;
hr = pCompatibleRenderTarget.GetBitmap(out pCompatibleBitmap);
ID2D1Effect pShadowEffect = null;
hr = m_pD2DDeviceContext.CreateEffect(CLSID_D2D1Shadow, out pShadowEffect);
pShadowEffect.SetInput(0, pCompatibleBitmap);
SetEffectFloat(pShadowEffect, (uint)D2D1_SHADOW_PROP.D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION, m_nShadowStandardDeviation);
Windows.UI.Color ShadowColor = Microsoft.UI.Colors.DeepPink;
float[] aFloatArray = { (float)((float)ShadowColor.R / 255.0f), (float)((float)ShadowColor.G / 255.0f), (float)((float)ShadowColor.B / 255.0f), 1.0f };
SetEffectFloatArray(pShadowEffect, (uint)D2D1_SHADOW_PROP.D2D1_SHADOW_PROP_COLOR, aFloatArray);
// Animate
m_nShadowStandardDeviation += 0.5f * m_nShadowDirection;
if (m_nShadowStandardDeviation >= 20.0f && m_nShadowDirection > 0 ||
m_nShadowStandardDeviation <= 0.0f && m_nShadowDirection < 0)
{
m_nShadowDirection = -m_nShadowDirection;
}
ID2D1Effect pBrightnessEffect = null;
hr = m_pD2DDeviceContext.CreateEffect(D2DTools.CLSID_D2D1Brightness, out pBrightnessEffect);
pBrightnessEffect.SetInput(0, (ID2D1Image)pShadowEffect);
float[] aFloatArray1 = { 0.15f, 0.70f };
SetEffectFloatArray(pBrightnessEffect, (uint)D2D1_BRIGHTNESS_PROP.D2D1_BRIGHTNESS_PROP_WHITE_POINT, aFloatArray1);
var ptShadowPos = Point2F(inflatedBounds.left, inflatedBounds.top);
//m_pD2DDeviceContext.SetTransform(mSavedTransform);
m_pD2DDeviceContext.DrawImage((ID2D1Image)pBrightnessEffect, ptShadowPos, IntPtr.Zero, D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
SafeRelease(ref pBrightnessEffect);
SafeRelease(ref pShadowEffect);
SafeRelease(ref pCompatibleBitmap);
SafeRelease(ref pCompatibleRenderTarget);
}
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry4, m_pD2DSolidColorBrushWhite);
}
}
else if (m_nTextEffect == (int)TEXT_EFFECT.BITMAP)
{
if (m_pD2DGeometry5 != null)
{
CenterGeometry(m_pD2DDeviceContext, m_pD2DGeometry5, 1.0f);
// Save transform
//m_pD2DDeviceContext.GetTransform(out var savedTransform);
//D2D1_MATRIX_3X2_F mSavedTransform = ToClass(savedTransform);
m_pD2DGeometry5.GetBounds(null, out var geoBounds);
// Animate the bitmap brush
m_imageScrollX += 0.6f; // horizontal speed
m_imageScrollY += 0.25f; // vertical speed
// prevent float overflow
if (m_imageScrollX > 10000) m_imageScrollX = 0;
if (m_imageScrollY > 10000) m_imageScrollY = 0;
var brushTransform = Matrix3x2F.Translation(m_imageScrollX, m_imageScrollY);
m_pD2DBitmapBrush1.SetTransform(brushTransform);
// Draw text outline
m_pD2DDeviceContext.DrawGeometry(m_pD2DGeometry5, m_pD2DMainBrush, 2.0f);
// Clip to text geometry
D2D1_LAYER_PARAMETERS lp = LayerParameters(InfiniteRect(), m_pD2DGeometry5);
m_pD2DDeviceContext.PushLayer(ref lp);
// Fill text with animated bitmap
m_pD2DDeviceContext.FillRectangle(ref geoBounds, m_pD2DBitmapBrush1);
m_pD2DDeviceContext.PopLayer();
SafeRelease(ref lp.geometricMask);
}
}
else if (m_nTextEffect == (int)TEXT_EFFECT.SCROLLING)
{
if (m_pD2DGeometry6 != null)
{
CenterGeometry(m_pD2DDeviceContext, m_pD2DGeometry6, 1.0f);
// Save transform
m_pD2DDeviceContext.GetTransform(out var savedTransform);
D2D1_MATRIX_3X2_F mSavedTransform = ToClass(savedTransform);
m_pFadeBrush.SetStartPoint(new Direct2D.D2D1_POINT_2F(0, 0));
m_pFadeBrush.SetEndPoint(new Direct2D.D2D1_POINT_2F(size.width, 0));
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Identity());
var lp = new D2D1_LAYER_PARAMETERS
{
contentBounds = new D2D1_RECT_F(0, 0, size.width, size.height),
opacity = 1.0f,
opacityBrush = m_pFadeBrush,
layerOptions = D2D1_LAYER_OPTIONS.D2D1_LAYER_OPTIONS_NONE
};
m_pD2DDeviceContext.PushLayer(lp, null);
D2D1_MATRIX_3X2_F scrollTransform = new D2D1_MATRIX_3X2_F
{
_11 = mSavedTransform._11,
_12 = mSavedTransform._12,
_21 = mSavedTransform._21,
_22 = mSavedTransform._22,
_31 = mSavedTransform._31 + m_nXScroll,
_32 = mSavedTransform._32 // Keep centered Y
};
m_pD2DDeviceContext.SetTransform(scrollTransform);
m_pD2DDeviceContext.DrawGeometry(m_pD2DGeometry6, m_pD2DMainBrush, 2.0f);
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry6, m_pD2DSolidColorBrushPink);
m_nXScroll -= 2.0f;
if (m_nXScroll <= -(size.width))