-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectWriteCustomFont.cpp
More file actions
557 lines (447 loc) · 16 KB
/
Copy pathDirectWriteCustomFont.cpp
File metadata and controls
557 lines (447 loc) · 16 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
#pragma once
#include "DirectWriteCustomFont.h"
// フォントコレクションローダー
WRL::ComPtr <CustomFontCollectionLoader> pFontCollectionLoader = nullptr;
//=============================================================================
// カスタムファイルローダー
//=============================================================================
class CustomFontFileEnumerator : public IDWriteFontFileEnumerator
{
public:
CustomFontFileEnumerator(IDWriteFactory* factory, const std::vector<std::wstring>& fontFilePaths)
: refCount_(0), factory_(factory), fontFilePaths_(fontFilePaths), currentFileIndex_(-1)
{
factory_->AddRef();
}
~CustomFontFileEnumerator()
{
factory_->Release();
}
IFACEMETHODIMP QueryInterface(REFIID iid, void** ppvObject) override
{
if (iid == __uuidof(IUnknown) || iid == __uuidof(IDWriteFontCollectionLoader))
{
*ppvObject = this;
AddRef();
return S_OK;
}
else
{
*ppvObject = nullptr;
return E_NOINTERFACE;
}
}
IFACEMETHODIMP_(ULONG) AddRef() override
{
return InterlockedIncrement(&refCount_);
}
IFACEMETHODIMP_(ULONG) Release() override
{
ULONG newCount = InterlockedDecrement(&refCount_);
if (newCount == 0)
delete this;
return newCount;
}
IFACEMETHODIMP MoveNext(OUT BOOL* hasCurrentFile) override {
if (++currentFileIndex_ < static_cast<int>(fontFilePaths_.size())) {
*hasCurrentFile = TRUE;
return S_OK;
}
else {
*hasCurrentFile = FALSE;
return S_OK;
}
}
IFACEMETHODIMP GetCurrentFontFile(OUT IDWriteFontFile** fontFile) override
{
// フォントファイルを読み込む
return factory_->CreateFontFileReference(fontFilePaths_[currentFileIndex_].c_str(), nullptr, fontFile);
}
private:
ULONG refCount_;
// DirectWriteファクトリ
IDWriteFactory* factory_;
// フォントファイルのパス
std::vector<std::wstring> fontFilePaths_;
// 現在のファイルインデックス
int currentFileIndex_;
};
//=============================================================================
// カスタムフォントコレクションローダー
//=============================================================================
class CustomFontCollectionLoader : public IDWriteFontCollectionLoader
{
public:
// コンストラクタ
CustomFontCollectionLoader() : refCount_(0) {}
// IUnknown メソッド
IFACEMETHODIMP QueryInterface(REFIID iid, void** ppvObject) override
{
if (iid == __uuidof(IUnknown) || iid == __uuidof(IDWriteFontCollectionLoader))
{
*ppvObject = this;
AddRef();
return S_OK;
}
else
{
*ppvObject = nullptr;
return E_NOINTERFACE;
}
}
IFACEMETHODIMP_(ULONG) AddRef() override
{
return InterlockedIncrement(&refCount_);
}
IFACEMETHODIMP_(ULONG) Release() override
{
ULONG newCount = InterlockedDecrement(&refCount_);
if (newCount == 0)
delete this;
return newCount;
}
// IDWriteFontCollectionLoader メソッド
IFACEMETHODIMP CreateEnumeratorFromKey
(
IDWriteFactory* factory,
void const* collectionKey,
UINT32 collectionKeySize,
OUT IDWriteFontFileEnumerator** fontFileEnumerator) override
{
// 読み込むフォントファイルのパスを渡す
std::vector<std::wstring> fontFilePaths(std::begin(FontList::FontPath), std::end(FontList::FontPath));
// カスタムフォントファイル列挙子の作成
*fontFileEnumerator = new (std::nothrow) CustomFontFileEnumerator(factory, fontFilePaths);
// メモリ不足の場合はエラーを返す
if (*fontFileEnumerator == nullptr) { return E_OUTOFMEMORY; }
return S_OK;
}
private:
ULONG refCount_;
};
// 初期化処理
HRESULT DirectWriteCustomFont::Init(IDXGISwapChain* swapChain)
{
HRESULT result = S_OK;
// Direct2Dファクトリ情報の初期化
result = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, pD2DFactory.GetAddressOf());
if (FAILED(result)) { return result; }
// バックバッファの取得
result = swapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
if (FAILED(result)) { return result; }
// dpiの設定
FLOAT dpiX;
FLOAT dpiY;
pD2DFactory->GetDesktopDpi(&dpiX, &dpiY);
// レンダーターゲットの作成
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED), dpiX, dpiY);
// サーフェスに描画するレンダーターゲットを作成
result = pD2DFactory->CreateDxgiSurfaceRenderTarget(pBackBuffer.Get(), &props, pRenderTarget.GetAddressOf());
if (FAILED(result)) { return result; }
// アンチエイリアシングモードの設定
pRenderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE);
// IDWriteFactoryの作成
result = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(pDWriteFactory.GetAddressOf()));
if (FAILED(result)) { return result; }
// カスタムフォントコレクションローダー
pFontCollectionLoader = new CustomFontCollectionLoader();
// カスタムフォントコレクションローダーの作成
result = pDWriteFactory->RegisterFontCollectionLoader(pFontCollectionLoader.Get());
if (FAILED(result)) { return result; }
// フォントファイルの読み込み
result = FontLoader();
if (FAILED(result)) { return result; }
// フォントを設定
result = SetFont(Setting);
if (FAILED(result)) { return result; }
return result;
}
// 指定されたパスのフォントを読み込む
HRESULT DirectWriteCustomFont::FontLoader()
{
HRESULT result = S_OK;
// カスタムフォントコレクションの作成
result = pDWriteFactory->CreateCustomFontCollection
(
pFontCollectionLoader.Get(),
pFontFileList.data(),
pFontFileList.size(),
&fontCollection
);
if (FAILED(result)) { return result; }
// フォント名を取得
result = GetFontFamilyName(fontCollection.Get());
if (FAILED(result)) { return result; }
return S_OK;
}
// フォント名を取得する
std::wstring DirectWriteCustomFont::GetFontName(int num)
{
// フォント名のリストが空だった場合
if (fontNamesList.empty())
{
return nullptr;
}
// リストのサイズを超えていた場合
if (num >= static_cast<int>(fontNamesList.size()))
{
return fontNamesList[0];
}
return fontNamesList[num];
}
// 読み込んだフォント名の数を取得する
int DirectWriteCustomFont::GetFontNameNum()
{
return fontNamesList.size();
}
// フォント設定
// 第1引数:フォントデータ構造体
HRESULT DirectWriteCustomFont::SetFont(FontData set)
{
HRESULT result = S_OK;
// 設定をコピー
Setting = set;
//関数CreateTextFormat()
//第1引数:フォント名(L"メイリオ", L"Arial", L"Meiryo UI"等)
//第2引数:フォントコレクション(nullptr)
//第3引数:フォントの太さ(DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_BOLD等)
//第4引数:フォントスタイル(DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STYLE_OBLIQUE, DWRITE_FONT_STYLE_ITALIC)
//第5引数:フォントの幅(DWRITE_FONT_STRETCH_NORMAL,DWRITE_FONT_STRETCH_EXTRA_EXPANDED等)
//第6引数:フォントサイズ(20, 30等)
//第7引数:ロケール名(L"")
//第8引数:テキストフォーマット(&g_pTextFormat)
result = pDWriteFactory->CreateTextFormat
(
GetFontFileNameWithoutExtension(Setting.font.c_str()),
fontCollection.Get(),
Setting.fontWeight,
Setting.fontStyle,
Setting.fontStretch,
Setting.fontSize,
Setting.localeName,
pTextFormat.GetAddressOf()
);
if (FAILED(result)) { return result; }
//関数SetTextAlignment()
//第1引数:テキストの配置(DWRITE_TEXT_ALIGNMENT_LEADING:前, DWRITE_TEXT_ALIGNMENT_TRAILING:後, DWRITE_TEXT_ALIGNMENT_CENTER:中央,
// DWRITE_TEXT_ALIGNMENT_JUSTIFIED:行いっぱい)
result = pTextFormat->SetTextAlignment(Setting.textAlignment);
if (FAILED(result)) { return result; }
//関数CreateSolidColorBrush()
//第1引数:フォント色(D2D1::ColorF(D2D1::ColorF::Black):黒, D2D1::ColorF(D2D1::ColorF(0.0f, 0.2f, 0.9f, 1.0f)):RGBA指定)
result = pRenderTarget->CreateSolidColorBrush(Setting.Color, pBrush.GetAddressOf());
if (FAILED(result)) { return result; }
// 影用のブラシを作成
result = pRenderTarget->CreateSolidColorBrush(Setting.shadowColor, pShadowBrush.GetAddressOf());
if (FAILED(result)) { return result; }
return result;
}
//=================================================================================================================================
// フォント設定
// 第1引数:フォント名(L"メイリオ", L"Arial", L"Meiryo UI"等)
// 第2引数:フォントの太さ(DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_BOLD等)
// 第3引数:フォントスタイル(DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STYLE_OBLIQUE, DWRITE_FONT_STYLE_ITALIC)
// 第4引数:フォントの幅(DWRITE_FONT_STRETCH_NORMAL,DWRITE_FONT_STRETCH_EXTRA_EXPANDED等)
// 第5引数:フォントサイズ(20, 30等)
// 第6引数:ロケール名(L"ja-jp"等)
// 第7引数:テキストの配置(DWRITE_TEXT_ALIGNMENT_LEADING:前, 等)
// 第8引数:フォントの色(D2D1::ColorF(D2D1::ColorF::Black):黒, D2D1::ColorF(D2D1::ColorF(0.0f, 0.2f, 0.9f, 1.0f)):RGBA指定等)
// 第9引数:影の色(D2D1::ColorF(D2D1::ColorF::Black):黒, D2D1::ColorF(D2D1::ColorF(0.0f, 0.2f, 0.9f, 1.0f)):RGBA指定等)
// 第10引数:影のオフセット(D2D1::Point2F(2.0f, 2.0f):右下に2ピクセルずらす)
//=================================================================================================================================
HRESULT DirectWriteCustomFont::SetFont(WCHAR const* fontname, DWRITE_FONT_WEIGHT fontWeight, DWRITE_FONT_STYLE fontStyle,
DWRITE_FONT_STRETCH fontStretch, FLOAT fontSize, WCHAR const* localeName,
DWRITE_TEXT_ALIGNMENT textAlignment, D2D1_COLOR_F Color, D2D1_COLOR_F shadowColor, D2D1_POINT_2F shadowOffset)
{
HRESULT result = S_OK;
pDWriteFactory->CreateTextFormat(GetFontFileNameWithoutExtension(fontname), fontCollection.Get(), fontWeight, fontStyle, fontStretch, fontSize, localeName, &pTextFormat);
if (FAILED(result)) { return result; }
pTextFormat->SetTextAlignment(textAlignment);
if (FAILED(result)) { return result; }
pRenderTarget->CreateSolidColorBrush(Color, pBrush.GetAddressOf());
if (FAILED(result)) { return result; }
pRenderTarget->CreateSolidColorBrush(shadowColor, pShadowBrush.GetAddressOf());
if (FAILED(result)) { return result; }
return result;
}
//====================================
// 文字描画
// string:文字列
// pos:描画ポジション
// options:テキストの整形
//====================================
HRESULT DirectWriteCustomFont::DrawString(std::string str, D3DXVECTOR2 pos, D2D1_DRAW_TEXT_OPTIONS options, bool shadow)
{
HRESULT result = S_OK;
// 文字列の変換
std::wstring wstr = StringToWString(str.c_str());
// ターゲットサイズの取得
D2D1_SIZE_F TargetSize = pRenderTarget->GetSize();
// テキストレイアウトを作成
result = pDWriteFactory->CreateTextLayout(wstr.c_str(), wstr.size(), pTextFormat.Get(), TargetSize.width, TargetSize.height, pTextLayout.GetAddressOf());
if (FAILED(result)) { return result; }
// 描画位置の確定
D2D1_POINT_2F pounts;
pounts.x = pos.x;
pounts.y = pos.y;
// 描画の開始
pRenderTarget->BeginDraw();
// 影を描画する場合
if (shadow)
{
// 影の描画
pRenderTarget->DrawTextLayout(D2D1::Point2F(pounts.x - Setting.shadowOffset.x, pounts.y - Setting.shadowOffset.y),
pTextLayout.Get(),
pShadowBrush.Get(),
options);
}
// 描画処理
pRenderTarget->DrawTextLayout(pounts, pTextLayout.Get(), pBrush.Get(), options);
// 描画の終了
result = pRenderTarget->EndDraw();
if (FAILED(result)) { return result; }
return S_OK;
}
//====================================
// 文字描画
// string:文字列
// rect:領域指定
// options:テキストの整形
//====================================
HRESULT DirectWriteCustomFont::DrawString(std::string str, D2D1_RECT_F rect, D2D1_DRAW_TEXT_OPTIONS options, bool shadow)
{
HRESULT result = S_OK;
// 文字列の変換
std::wstring wstr = StringToWString(str.c_str());
// 描画の開始
pRenderTarget->BeginDraw();
if (shadow)
{
// 影の描画
pRenderTarget->DrawText(wstr.c_str(),
wstr.size(),
pTextFormat.Get(),
D2D1::RectF(rect.left - Setting.shadowOffset.x, rect.top - Setting.shadowOffset.y, rect.right - Setting.shadowOffset.x, rect.bottom - Setting.shadowOffset.y),
pShadowBrush.Get(), options);
}
// 描画処理
pRenderTarget->DrawText(wstr.c_str(), wstr.size(), pTextFormat.Get(), rect, pBrush.Get(), options);
// 描画の終了
result = pRenderTarget->EndDraw();
if (FAILED(result)) { return result; }
return S_OK;
}
// フォント名を取得
HRESULT DirectWriteCustomFont::GetFontFamilyName(IDWriteFontCollection* customFontCollection, const WCHAR* locale)
{
HRESULT result = S_OK;
// フォントファミリー名一覧をリセット
std::vector<std::wstring>().swap(fontNamesList);
// フォントの数を取得
UINT32 familyCount = customFontCollection->GetFontFamilyCount();
for (UINT32 i = 0; i < familyCount; i++)
{
// フォントファミリーの取得
WRL::ComPtr <IDWriteFontFamily> fontFamily = nullptr;
result = customFontCollection->GetFontFamily(i, fontFamily.GetAddressOf());
if (FAILED(result)) { return result; }
// フォントファミリー名の一覧を取得
WRL::ComPtr <IDWriteLocalizedStrings> familyNames = nullptr;
result = fontFamily->GetFamilyNames(familyNames.GetAddressOf());
if (FAILED(result)) { return result; }
// 指定されたロケールに対応するインデックスを検索
UINT32 index = 0;
BOOL exists = FALSE;
result = familyNames->FindLocaleName(locale, &index, &exists);
if (FAILED(result)) { return result; }
// 指定されたロケールが見つからなかった場合は、デフォルトのロケールを使用
if (!exists)
{
result = familyNames->FindLocaleName(L"en-us", &index, &exists);
if (FAILED(result)) { return result; }
}
// フォントファミリー名の長さを取得
UINT32 length = 0;
result = familyNames->GetStringLength(index, &length);
if (FAILED(result)) { return result; }
// フォントファミリー名の取得
WCHAR* name = new WCHAR[length + 1];
result = familyNames->GetString(index, name, length + 1);
if (FAILED(result)) { return result; }
// フォントファミリー名を追加
fontNamesList.push_back(name);
// フォントファミリー名の破棄
delete[] name;
}
return result;
}
// 全てのフォント名を取得
HRESULT DirectWriteCustomFont::GetAllFontFamilyName(IDWriteFontCollection* customFontCollection)
{
HRESULT result = S_OK;
// フォントファミリー名一覧をリセット
std::vector<std::wstring>().swap(fontNamesList);
// フォントファミリーの数を取得
UINT32 familyCount = customFontCollection->GetFontFamilyCount();
for (UINT32 i = 0; i < familyCount; i++)
{
// フォントファミリーの取得
WRL::ComPtr <IDWriteFontFamily> fontFamily = nullptr;
result = customFontCollection->GetFontFamily(i, fontFamily.GetAddressOf());
if (FAILED(result)) { return result; }
// フォントファミリー名の一覧を取得
WRL::ComPtr <IDWriteLocalizedStrings> familyNames = nullptr;
result = fontFamily->GetFamilyNames(familyNames.GetAddressOf());
if (FAILED(result)) { return result; }
// フォントファミリー名の数を取得
UINT32 nameCount = familyNames->GetCount();
// フォントファミリー名の数だけ繰り返す
for (UINT32 j = 0; j < nameCount; ++j)
{
// フォントファミリー名の長さを取得
UINT32 length = 0;
result = familyNames->GetStringLength(j, &length);
if (FAILED(result)) { return result; }
// フォントファミリー名の取得
WCHAR* name = new WCHAR[length + 1];
result = familyNames->GetString(j, name, length + 1);
if (FAILED(result)) { return result; }
// フォントファミリー名を追加
fontNamesList.push_back(name);
// フォントファミリー名の破棄
delete[] name;
}
}
return result;
}
// 拡張子を除くファイル名を返す
WCHAR* DirectWriteCustomFont::GetFontFileNameWithoutExtension(const std::wstring& filePath)
{
// 末尾から検索してファイル名と拡張子の位置を取得
size_t start = filePath.find_last_of(L"/\\") + 1;
size_t end = filePath.find_last_of(L'.');
// ファイル名を取得
std::wstring fileNameWithoutExtension = filePath.substr(start, end - start).c_str();
// 新しいWCHAR配列を作成
WCHAR* fileName = new WCHAR[fileNameWithoutExtension.length() + 1];
// 文字列をコピー
wcscpy_s(fileName, fileNameWithoutExtension.length() + 1, fileNameWithoutExtension.c_str());
// ファイル名を返す
return fileName;
}
// stringをwstringへ変換する
std::wstring DirectWriteCustomFont::StringToWString(std::string oString)
{
// SJIS → wstring
int iBufferSize = MultiByteToWideChar(CP_ACP, 0, oString.c_str(), -1, (wchar_t*)NULL, 0);
// バッファの取得
wchar_t* cpUCS2 = new wchar_t[iBufferSize];
// SJIS → wstring
MultiByteToWideChar(CP_ACP, 0, oString.c_str(), -1, cpUCS2, iBufferSize);
// stringの生成
std::wstring oRet(cpUCS2, cpUCS2 + iBufferSize - 1);
// バッファの破棄
delete[] cpUCS2;
// 変換結果を返す
return(oRet);
}