-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDWriteShapeLib.cpp
More file actions
488 lines (385 loc) · 9.62 KB
/
Copy pathDWriteShapeLib.cpp
File metadata and controls
488 lines (385 loc) · 9.62 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "DWriteShapeLib.h"
// https://stackoverflow.com/questions/955484/is-it-possible-to-convert-utf32-text-to-utf16-using-only-windows-api
unsigned int UTF32ToUTF16(char32_t cUTF32, wchar_t& h, wchar_t& l)
{
if (cUTF32 < 0x10000)
{
h = 0;
l = (wchar_t)cUTF32;
return cUTF32;
}
unsigned int t = cUTF32 - 0x10000;
h = (((t << 12) >> 22) + 0xD800);
l = (((t << 22) >> 22) + 0xDC00);
unsigned int ret = ((h << 16) | (l & 0x0000FFFF));
return ret;
}
//
// Buffer_ Implementations
//
Buffer_::Buffer_()
{
// Sniff default locale
std::wstring locale = L"en-US";
wchar_t szLocale[LOCALE_NAME_MAX_LENGTH];
if (GetUserDefaultLocaleName(szLocale, LOCALE_NAME_MAX_LENGTH) > 0)
{
locale = szLocale;
}
locale_ = locale;
}
Buffer_::~Buffer_()
{
if (localeMap_ != nullptr)
delete localeMap_;
}
void Buffer_::Add_Utf32(const uint32_t* text, int text_length, unsigned int item_offset, int item_length)
{
assert(text != nullptr);
if ((int)item_offset > text_length)
return;
std::u32string str(reinterpret_cast<const char32_t*>(text), text_length);
std::u32string str32;
if (item_length != -1)
{
str32.append(str, item_offset, item_length);
}
else
{
str32.append(str);
}
std::wstring str16;
for (auto& it : str32)
{
wchar_t h, l;
UTF32ToUTF16(it, h, l);
if (h != 0)
{
str16.push_back(h);
}
str16.push_back(l);
}
str_.append(str16);
}
void Buffer_::Add_Utf16(const uint16_t* text, int text_length, unsigned int item_offset, int item_length)
{
assert(text != nullptr);
if ((int)item_offset > text_length)
return;
std::wstring stdText(reinterpret_cast<const wchar_t*>(text), text_length);
if (item_length != -1)
{
str_.append(stdText, item_offset, item_length);
}
else
{
str_.append(stdText);
}
}
void Buffer_::Add_Utf8(const uint8_t* text, int text_length, unsigned int item_offset, int item_length)
{
assert(text != nullptr);
if ((int)item_offset > text_length)
return;
std::string str(reinterpret_cast<const char*>(text), text_length);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wstr = converter.from_bytes(str);
if (item_length != -1)
{
str_.append(wstr, item_offset, item_length);
}
else
{
str_.append(wstr);
}
}
void Buffer_::Add_Latin(const uint8_t* text, int text_length, unsigned int item_offset, int item_length)
{
assert(text != nullptr);
if ((int)item_offset > text_length)
return;
std::string str(reinterpret_cast<const char*>(text), text_length);
std::wstring wstr;
for (auto& it : str)
{
wchar_t ch = it & 0x00FF;
wstr.push_back(ch);
}
if (item_length != -1)
{
str_.append(wstr, item_offset, item_length);
}
else
{
str_.append(wstr);
}
}
int Buffer_::Length()
{
return static_cast<int>(str_.length());
}
const std::wstring& Buffer_::GetString()
{
return str_;
}
void Buffer_::SetLocale(const char* locale)
{
// Build Locale mapping
if(localeMap_ == nullptr)
localeMap_ = new LocaleMap();
if (localeMap_ == nullptr)
return;
std::string str = reinterpret_cast<const char*>(locale);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring inputLocale = converter.from_bytes(str);
std::wstring windowsLocale;
if (localeMap_->GetWindowsLocale(inputLocale, windowsLocale))
{
locale_ = windowsLocale;
}
else
{
locale_ = inputLocale;
}
}
const char* Buffer_::GetLocale()
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
locale8_ = converter.to_bytes(locale_);
return locale8_.c_str();
}
const std::wstring& Buffer_::Locale()
{
return locale_;
}
void Buffer_::SetOutput(TextRunShapeOutput& output)
{
for (size_t i = 0; i < output.glyphCount; i++)
{
glyph_position_t glyphPosition;
glyphPosition.x_advance = output.glyphAdvances[i];
glyphPosition.y_advance = 0; // for now until we support y advances
glyphPosition.x_offset = output.glyphOffsets[i].advanceOffset;
glyphPosition.y_offset = output.glyphOffsets[i].ascenderOffset;
glyphPositions_.push_back(glyphPosition);
glyph_info_t glyphInfo;
glyphInfo.cluster = output.glyphClusters[i];
glyphInfo.codepoint = output.glyphIndices[i];
glyphInfos_.push_back(glyphInfo);
}
}
glyph_info_t* Buffer_::GetGlyphInfos(unsigned int* count)
{
*count = static_cast<unsigned int>(glyphInfos_.size());
return glyphInfos_.data();
}
glyph_position_t* Buffer_::GetGlyphPositions(unsigned int* count)
{
*count = static_cast<unsigned int>(glyphPositions_.size());
return glyphPositions_.data();
}
//
// Face Implementations
//
Face_::Face_()
{
pface_ = new DWriteShapeInternal::Face();
if (pface_ == nullptr)
return;
HRESULT hr = pface_->Initialize();
}
Face_::~Face_()
{
if (pface_ != nullptr)
{
delete pface_;
pface_ = nullptr;
}
}
bool Face_::Create(const char* fontData, unsigned int fontDataSize, unsigned int ttcIndex)
{
HRESULT hr = pface_->OpenFontFile(fontData, fontDataSize, ttcIndex);
if (FAILED(hr))
return false;
return true;
}
//
// Font_ Implementations
//
Font_::Font_(Face_* pface) : pface_(pface)
{
}
Font_::~Font_()
{
this->ClearFeatures();
}
bool Font_::Create()
{
pfont_ = new DWriteShapeInternal::Font(*(pface_->pface_));
if (pfont_ == nullptr)
return false;
return true;
}
uint16_t Font_::GetDesignUnitsPerEm()
{
return pfont_->GetDesignUnitsPerEm();
}
void Font_::SetFontEmSize(float size)
{
fontEmSizeSet_ = true;
fontEmSize_ = size;
}
float Font_::GetFontEmSize()
{
float fontEmSize;
if (!fontEmSizeSet_)
{
fontEmSize = pfont_->GetDesignUnitsPerEm();
}
else
{
fontEmSize = fontEmSize_;
}
return fontEmSize;
}
bool Font_::SetVariations(const std::vector<hb_variation_t> &axisValues)
{
std::vector<DWRITE_FONT_AXIS_VALUE> dWriteAxisValues;
for (auto& it : axisValues)
{
DWRITE_FONT_AXIS_VALUE value;
// DWrite tags are opposite HarfBuzz tags
value.axisTag = static_cast<DWRITE_FONT_AXIS_TAG>(SWAPL(it.tag));
value.value = it.value;
dWriteAxisValues.push_back(value);
}
return (pfont_->SetVariationAxis(dWriteAxisValues) == S_OK);
}
bool Font_::SetFeatures(uint32_t textLength, const std::vector<hb_feature_t>& features)
{
this->ClearFeatures();
if (textLength == 0 || features.size() == 0)
return true;
std::vector<bool> featureRangeBounderies;
featureRangeBounderies.assign(textLength, false);
// beginning is always a boundry
featureRangeBounderies[0] = true;
// look though features and determine range boundries
for (auto& it : features)
{
if (it.start < featureRangeBounderies.size())
{
featureRangeBounderies[it.start] = true;
if (it.end < featureRangeBounderies.size())
{
featureRangeBounderies[it.end] = true;
}
}
}
// look at each boundry and determine features of influence
UINT32 featureRangeLength = 0;
for (unsigned int i = 0; i < featureRangeBounderies.size(); i++)
{
if (featureRangeBounderies[i])
{
// temporaraly store features in a vector
std::vector<DWRITE_FONT_FEATURE> localFeatures;
for (auto& it : features)
{
if (i >= it.start && i < it.end)
{
DWRITE_FONT_FEATURE localFeature;
// DWrite tags are opposite HarfBuzz tags
localFeature.nameTag = static_cast<DWRITE_FONT_FEATURE_TAG>(SWAPL(it.tag));
localFeature.parameter = it.value;
// Do we already have this feature for this range?
bool found = false;
for (auto& it1 : localFeatures)
{
if (it1.nameTag == localFeature.nameTag)
{
found = true;
// Update parameter to most recent one encountered
it1.parameter = it.value;
}
}
// If we don't already have it then add it
if (!found)
{
localFeatures.push_back(localFeature);
}
}
}
// allocate an array for DWRITE_FONT_FEATURE
DWRITE_FONT_FEATURE* pLocalFeatures = new DWRITE_FONT_FEATURE[localFeatures.size()];
for (int j = 0; j < localFeatures.size(); j++)
{
pLocalFeatures[j].nameTag = localFeatures[j].nameTag;
pLocalFeatures[j].parameter = localFeatures[j].parameter;
}
DWRITE_TYPOGRAPHIC_FEATURES typographicFeature;
typographicFeature.featureCount = static_cast<UINT32>(localFeatures.size());
typographicFeature.features = pLocalFeatures;
typographicFeatures_.push_back(typographicFeature);
// Calculate featureRangeLengths
if (i > 0)
{
featureRangeLengths_.push_back(featureRangeLength);
featureRangeLength = 0;
}
}
featureRangeLength++;
}
// Final length
featureRangeLengths_.push_back(featureRangeLength);
featureRangeLength = 0;
assert(typographicFeatures_.size() == featureRangeLengths_.size());
return true;
}
void Font_::ClearFeatures()
{
// Clean up
for (auto& it : typographicFeatures_)
{
if (it.features != nullptr)
{
delete[] it.features;
it.features = nullptr;
}
}
typographicFeatures_.clear();
featureRangeLengths_.clear();
}
void Font_::GlyphToString(uint16_t glyphId, char* string, unsigned int size)
{
try
{
std::string name = pfont_->GlyphToString(glyphId);
std::size_t len = name.copy(string, size);
string[len] = '\0';
}
catch (...)
{
return;
}
}
bool Font_::Shape(Buffer_* buffer)
{
TextRunShapeOutput output;
const std::wstring& text = buffer->GetString();
float fontEmSize = this->GetFontEmSize();
// Shape DWriteShape::Font
HRESULT hr = pfont_->Shape(text, buffer->Locale(), fontEmSize, output, typographicFeatures_, featureRangeLengths_);
buffer->SetOutput(output);
return true;
}
void Shape(Font_* font, Buffer_* buffer, const std::vector<hb_feature_t> &features)
{
assert(font != nullptr);
// Set font features
font->SetFeatures(buffer->Length(), features);
// Call Shape
font->Shape(buffer);
}