-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomTextRenderer.cs
301 lines (262 loc) · 14.5 KB
/
CustomTextRenderer.cs
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
using System;
using System.Runtime.InteropServices;
using System.Threading;
using GlobalStructures;
using static GlobalStructures.GlobalTools;
using Direct2D;
using static Direct2D.D2DTools;
using DWrite;
using WIC;
using DXGI;
using System.Diagnostics;
// Adapted from : https://github.com/microsoft/Windows-universal-samples/blob/main/Samples/DWriteColorGlyph/cpp/CustomTextRenderer.cpp
public class CustomTextRenderer : IDWriteTextRenderer, IDisposable
{
private readonly ID2D1DeviceContext4 m_pD2DDeviceContext;
private readonly IDWriteFactory4 m_pDwriteFactory;
private ID2D1SolidColorBrush m_pOutlineBrush;
private ID2D1SolidColorBrush m_pTempBrush;
private D2D1_COLOR_F m_DefaultColor;
private IntPtr m_hWndParent = IntPtr.Zero;
// DWriteCore only
private IDWriteBitmapRenderTarget3 m_pBitmapRenderTarget3 = null;
public bool IsDWriteCore = false;
private IDWriteRenderingParams m_pRenderingParams = null;
public CustomTextRenderer(IDWriteFactory4 pDWriteFactory, ID2D1DeviceContext4 pD2DDeviceContext, D2D1_COLOR_F DefaultColor, IntPtr hWnd)
{
HRESULT hr = HRESULT.S_OK;
m_pDwriteFactory = pDWriteFactory;
m_pD2DDeviceContext = pD2DDeviceContext;
m_DefaultColor = DefaultColor;
m_hWndParent = hWnd;
hr = m_pDwriteFactory.CreateRenderingParams(out m_pRenderingParams);
IDWriteGdiInterop pGdiInterop = null;
hr = m_pDwriteFactory.GetGdiInterop(out pGdiInterop);
if (hr == HRESULT.S_OK)
{
int nDisplayWidth = Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Width;
int nDisplayHeight = Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Height;
//int nDisplayWidth = 800;
//int nDisplayHeight = 200;
IDWriteBitmapRenderTarget pBitmapRenderTarget;
hr = pGdiInterop.CreateBitmapRenderTarget(IntPtr.Zero, nDisplayWidth, nDisplayHeight, out pBitmapRenderTarget);
if (hr == HRESULT.S_OK)
{
try
{
m_pBitmapRenderTarget3 = (IDWriteBitmapRenderTarget3)pBitmapRenderTarget;
IsDWriteCore = true;
}
catch (System.Exception)
{
IsDWriteCore = false;
SafeRelease(ref pBitmapRenderTarget);
}
}
SafeRelease(ref pGdiInterop);
}
hr = m_pD2DDeviceContext.CreateSolidColorBrush(m_DefaultColor, BrushProperties(), out m_pOutlineBrush);
hr = m_pD2DDeviceContext.CreateSolidColorBrush(new ColorF(ColorF.Enum.Black, 1.0f), BrushProperties(), out m_pTempBrush);
}
public HRESULT DrawGlyphRun(IntPtr clientDrawingContext, float baselineOriginX, float baselineOriginY, DWrite.DWRITE_MEASURING_MODE measuringMode,
ref DWRITE_GLYPH_RUN glyphRun, IntPtr glyphRunDescription, IntPtr clientDrawingEffect)
{
HRESULT hr = (HRESULT)DWRITE_HRESULT.DWRITE_E_NOCOLOR;
DWrite.D2D1_POINT_2F baselineOrigin = new DWrite.D2D1_POINT_2F { x = baselineOriginX, y = baselineOriginY };
DWrite.DWRITE_GLYPH_IMAGE_FORMATS supportedFormats = (DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_TRUETYPE |
DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_CFF |
DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_COLR |
DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_SVG |
DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_PNG |
DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_JPEG |
DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_TIFF |
DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_PREMULTIPLIED_B8G8R8A8);
hr = m_pDwriteFactory.TranslateColorGlyphRun4(baselineOrigin, ref glyphRun, glyphRunDescription, supportedFormats,
measuringMode, IntPtr.Zero, 0, out IDWriteColorGlyphRunEnumerator1 glyphRunEnumerator);
if (hr == (HRESULT)DWRITE_HRESULT.DWRITE_E_NOCOLOR)
{
if (!IsDWriteCore)
m_pD2DDeviceContext.DrawGlyphRun(Point2F(baselineOrigin.x, baselineOrigin.y), glyphRun, glyphRunDescription, m_pOutlineBrush, (Direct2D.DWRITE_MEASURING_MODE)measuringMode);
else
{
RECT blackBoxRect;
var foregroundColor = m_DefaultColor;
var nOldAntialiasMode = m_pBitmapRenderTarget3.GetTextAntialiasMode();
hr = m_pBitmapRenderTarget3.SetTextAntialiasMode(DWRITE_TEXT_ANTIALIAS_MODE.DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE);
hr = m_pBitmapRenderTarget3.DrawGlyphRun(baselineOrigin.x, baselineOrigin.y, measuringMode, glyphRun,
m_pRenderingParams, D2D1ColorFToCOLORREF(foregroundColor), out blackBoxRect);
hr = m_pBitmapRenderTarget3.SetTextAntialiasMode(nOldAntialiasMode);
//hr = m_pBitmapRenderTarget3.DrawGlyphRunWithColorSupport(baselineOrigin.x, baselineOrigin.y, measuringMode,
// glyphRun, m_RenderingParams, D2D1ColorFToCOLORREF(foregroundColor), 0, out blackBoxRect);
}
}
else
{
for (; ;)
{
bool bHaveRun;
hr = glyphRunEnumerator.MoveNext(out bHaveRun);
if (!bHaveRun)
break;
IntPtr pColorRun1 = IntPtr.Zero;
hr = glyphRunEnumerator.GetCurrentRun1(out pColorRun1);
DWRITE_COLOR_GLYPH_RUN1 colorRun1 = new DWRITE_COLOR_GLYPH_RUN1();
colorRun1 = (DWRITE_COLOR_GLYPH_RUN1)Marshal.PtrToStructure(pColorRun1, typeof(DWRITE_COLOR_GLYPH_RUN1));
DWrite.D2D1_POINT_2F currentBaselineOrigin = new DWrite.D2D1_POINT_2F { x = colorRun1.baselineOriginX, y = colorRun1.baselineOriginY };
if (!IsDWriteCore)
{
switch (colorRun1.glyphImageFormat)
{
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_PNG:
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_JPEG:
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_TIFF:
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_PREMULTIPLIED_B8G8R8A8:
{
m_pD2DDeviceContext.DrawColorBitmapGlyphRun((Direct2D.DWRITE_GLYPH_IMAGE_FORMATS)colorRun1.glyphImageFormat, Point2F(currentBaselineOrigin.x, currentBaselineOrigin.y), colorRun1.glyphRun, (Direct2D.DWRITE_MEASURING_MODE)measuringMode);
}
break;
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_SVG:
{
m_pD2DDeviceContext.DrawSvgGlyphRun(Point2F(currentBaselineOrigin.x, currentBaselineOrigin.y), colorRun1.glyphRun, m_pOutlineBrush, null, 0, (Direct2D.DWRITE_MEASURING_MODE)measuringMode);
}
break;
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_TRUETYPE:
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_CFF:
case DWrite.DWRITE_GLYPH_IMAGE_FORMATS.DWRITE_GLYPH_IMAGE_FORMATS_COLR:
default:
{
ID2D1Brush layerBrush;
if (colorRun1.paletteIndex == 0xFFFF)
{
// This run uses the current text color.
layerBrush = m_pOutlineBrush;
}
else
{
// This run specifies its own color.
D2D1_COLOR_F col = new D2D1_COLOR_F();
col.a = colorRun1.runColor.a;
col.r = colorRun1.runColor.r;
col.g = colorRun1.runColor.g;
col.b = colorRun1.runColor.b;
m_pTempBrush.SetColor(col);
layerBrush = m_pTempBrush;
}
m_pD2DDeviceContext.DrawGlyphRun(Point2F(currentBaselineOrigin.x, currentBaselineOrigin.y), colorRun1.glyphRun, glyphRunDescription, layerBrush, (Direct2D.DWRITE_MEASURING_MODE)measuringMode);
}
break;
}
}
else
{
var foregroundColor = new ColorF(colorRun1.runColor.r, colorRun1.runColor.g, colorRun1.runColor.b, colorRun1.runColor.a);
RECT blackBoxRect;
var nOldAntialiasMode = m_pBitmapRenderTarget3.GetTextAntialiasMode();
hr = m_pBitmapRenderTarget3.SetTextAntialiasMode(DWRITE_TEXT_ANTIALIAS_MODE.DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE);
//if (colorRun1.paletteIndex != 0)
hr = m_pBitmapRenderTarget3.DrawGlyphRunWithColorSupport(currentBaselineOrigin.x, currentBaselineOrigin.y, measuringMode,
colorRun1.glyphRun, m_pRenderingParams, D2D1ColorFToCOLORREF(foregroundColor), 0, out blackBoxRect);
hr = m_pBitmapRenderTarget3.SetTextAntialiasMode(nOldAntialiasMode);
}
}
}
return hr;
}
public HRESULT DrawBitmapRenderTarget(Direct2D.D2D1_POINT_2F pt)
{
HRESULT hr = HRESULT.S_OK;
IntPtr pBitmapData = IntPtr.Zero;
DWRITE_BITMAP_DATA_BGRA32 BitmapData = new DWRITE_BITMAP_DATA_BGRA32();
hr = m_pBitmapRenderTarget3.GetBitmapData(out BitmapData);
if (hr == HRESULT.S_OK)
{
Direct2D.D2D1_SIZE_U sizeBitmapU = new Direct2D.D2D1_SIZE_U();
sizeBitmapU.width = BitmapData.width;
sizeBitmapU.height = BitmapData.height;
uint nPitch = (sizeBitmapU.width * 4);
D2D1_BITMAP_PROPERTIES1 bitmapProperties1 = new D2D1_BITMAP_PROPERTIES1();
bitmapProperties1.pixelFormat = D2DTools.PixelFormat(DXGI.DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED);
bitmapProperties1.dpiX = 96;
bitmapProperties1.dpiY = 96;
bitmapProperties1.bitmapOptions = D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_NONE;
ID2D1Bitmap1 pBitmap1;
hr = m_pD2DDeviceContext.CreateBitmap(sizeBitmapU, BitmapData.pixels, nPitch, ref bitmapProperties1, out pBitmap1);
if (hr == HRESULT.S_OK)
{
D2D1_SIZE_F size = m_pD2DDeviceContext.GetSize();
D2D1_RECT_F imageRectangle = new D2D1_RECT_F();
imageRectangle.left = 0.0f;
imageRectangle.top = 0.0f;
imageRectangle.right = size.width;
imageRectangle.bottom = size.height;
//imageRectangle.right = Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Width;
//imageRectangle.bottom = Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Height;
// Quality loss with DWriteCore (bigger outline)
// while pBitmap1 seems OK if it is saved to file (SaveD2D1BitmapToFile from other sources)
// or displayed to screen DC from GetMemoryDC with BitBlt
//m_pD2DDeviceContext.Clear(null);
m_pD2DDeviceContext.DrawImage(pBitmap1, pt, ref imageRectangle, D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
SafeRelease(ref pBitmap1);
}
}
return hr;
}
public HRESULT DrawUnderline(IntPtr clientDrawingContext, float baselineOriginX, float baselineOriginY, DWRITE_UNDERLINE underline, IntPtr clientDrawingEffect)
{
return HRESULT.S_OK;
}
public HRESULT DrawStrikethrough(IntPtr clientDrawingContext, float baselineOriginX, float baselineOriginY, DWRITE_STRIKETHROUGH strikethrough, IntPtr clientDrawingEffect)
{
return HRESULT.S_OK;
}
public HRESULT DrawInlineObject(IntPtr clientDrawingContext, float originX, float originY, IDWriteInlineObject inlineObject, bool isSideways, bool isRightToLeft, IntPtr clientDrawingEffect)
{
return HRESULT.S_OK;
}
public HRESULT IsPixelSnappingDisabled(IntPtr clientDrawingContext, out bool isDisabled)
{
isDisabled = false;
return HRESULT.S_OK;
}
public HRESULT GetCurrentTransform(IntPtr clientDrawingContext, out DWRITE_MATRIX transform)
{
D2D1_MATRIX_3X2_F_STRUCT d2dTransform = new D2D1_MATRIX_3X2_F_STRUCT();
m_pD2DDeviceContext.GetTransform(out d2dTransform);
transform.m11 = d2dTransform._11;
transform.m12 = d2dTransform._12;
transform.m21 = d2dTransform._21;
transform.m22 = d2dTransform._22;
transform.dx = d2dTransform._31;
transform.dy = d2dTransform._32;
return HRESULT.S_OK;
}
public HRESULT GetPixelsPerDip(IntPtr clientDrawingContext, out float pixelsPerDip)
{
//if (m_pBitmapRenderTarget3 != null)
// pixelsPerDip = m_pBitmapRenderTarget3.GetPixelsPerDip();
//else
//{
// m_pD2DDeviceContext.GetDpi(out float nDpiX, out _);
// pixelsPerDip = nDpiX / 96.0f;
//}
uint nDPI = GetDpiForWindow(m_hWndParent);
pixelsPerDip = nDPI / 96.0f;
return HRESULT.S_OK;
}
private static uint D2D1ColorFToCOLORREF(D2D1_COLOR_F color)
{
byte red = (byte)(color.r * 255);
byte green = (byte)(color.g * 255);
byte blue = (byte)(color.b * 255);
return (uint)(red | (green << 8) | (blue << 16));
}
public void Dispose()
{
if (m_pOutlineBrush != null)
SafeRelease(ref m_pOutlineBrush);
if (m_pTempBrush != null)
SafeRelease(ref m_pTempBrush);
if (m_pBitmapRenderTarget3 != null)
SafeRelease(ref m_pBitmapRenderTarget3);
}
}