-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDWriteShapeInternal.cpp
More file actions
171 lines (124 loc) · 3.74 KB
/
Copy pathDWriteShapeInternal.cpp
File metadata and controls
171 lines (124 loc) · 3.74 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "DWriteShapeInternal.h"
namespace DWriteShapeInternal
{
Face::Face()
{
}
Face::~Face()
{
dwriteFactory_->UnregisterFontFileLoader(dwriteFontFileLoader_);
SafeRelease(&dwriteFontFileLoader_);
SafeRelease(&fontFile_);
SafeRelease(&textAnalyzer_);
SafeRelease(&dwriteFactory_);
}
HRESULT Face::Initialize()
{
// Create a shared DirectWrite factory.
IFR(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory6), reinterpret_cast<IUnknown**>(&dwriteFactory_)));
IFR(dwriteFactory_->CreateInMemoryFontFileLoader(&dwriteFontFileLoader_));
IFR(dwriteFactory_->RegisterFontFileLoader(dwriteFontFileLoader_));
IFR(dwriteFactory_->CreateTextAnalyzer(&textAnalyzer_));
return S_OK;
}
HRESULT Face::OpenFontFile(const void* fontData, UINT32 fontDataSize, UINT32 ttcIndex)
{
ttcIndex = ttcIndex;
IFR(dwriteFontFileLoader_->CreateInMemoryFontFileReference(dwriteFactory_, fontData, fontDataSize, nullptr, &fontFile_));
return S_OK;
};
HRESULT Face::OpenFontFile(const std::wstring& file, UINT32 ttcIndex)
{
ttcIndex_ = ttcIndex;
IFR(dwriteFactory_->CreateFontFileReference(file.c_str(), nullptr, &fontFile_));
return S_OK;
}
Font::Font(Face& face) : face_(face)
{
}
Font::~Font()
{
SafeRelease(&fontFaceReference_);
SafeRelease(&fontFace_);
if (postTable_ != nullptr)
delete postTable_;
}
HRESULT Font::SetVariationAxis(const std::vector<DWRITE_FONT_AXIS_VALUE>& axisValues)
{
fontRealized_ = false;
axisValues_ = axisValues;
return S_OK;
}
std::string Font::GlyphToString(uint16_t glyphId)
{
if(FAILED(this->RealizeFont()) || (postTable_ == nullptr))
{
return "";
}
return postTable_->GlyphToString(glyphId);
}
HRESULT Font::RealizeFont()
{
if (!fontRealized_)
{
const DWRITE_FONT_AXIS_VALUE* axis = axisValues_.data();
SafeRelease(&fontFaceReference_);
SafeRelease(&fontFace_);
IFR(face_.dwriteFactory_->CreateFontFaceReference(face_.fontFile_, face_.ttcIndex_, DWRITE_FONT_SIMULATIONS::DWRITE_FONT_SIMULATIONS_NONE, axis, static_cast<UINT32>(axisValues_.size()), &fontFaceReference_));
IFR(fontFaceReference_->CreateFontFace(&fontFace_));
fontFace_->GetMetrics(&dwriteFontMetrics_);
IFR(InitializePostTable());
fontRealized_ = true;
}
return S_OK;
}
HRESULT Font::Shape(const std::wstring& text, const std::wstring& localeName, float fontEmSize, TextRunShapeOutput& output, const std::vector<DWRITE_TYPOGRAPHIC_FEATURES>& features, const std::vector<UINT32>& featureRangeLengths)
{
TextRun* textRun = nullptr;
IFR(RealizeFont());
textRun = new TextRun(face_.dwriteFactory_, fontFace_, face_.textAnalyzer_, features, featureRangeLengths);
// Set text and locale
IFR(textRun->SetText(text, localeName));
// The call
IFR(textRun->ShapeRun(fontEmSize, output));
delete textRun;
return S_OK;
}
UINT16 Font::GetDesignUnitsPerEm()
{
if (FAILED(RealizeFont()))
return 0;
return dwriteFontMetrics_.designUnitsPerEm;
}
HRESULT Font::InitializePostTable()
{
UINT32 postTag = DWRITE_MAKE_OPENTYPE_TAG('p', 'o', 's', 't');
const void* postData;
UINT32 postSize = 0;
void* tableContext;
BOOL postExists;
if (postTable_ == nullptr)
{
HRESULT hr = fontFace_->TryGetFontTable(postTag, &postData, &postSize, &tableContext, &postExists);
if (SUCCEEDED(hr) && postExists)
{
postTable_ = new PostTable(postData, postSize);
}
else
{
postTable_ = new PostTable(nullptr, 0);
}
if (postTable_ == nullptr)
{
fontFace_->ReleaseFontTable(tableContext);
return E_FAIL;
}
postTable_->Initialize();
fontFace_->ReleaseFontTable(tableContext);
}
return S_OK;
}
}