Skip to content

Commit f351de0

Browse files
committed
Simplify OT feature propogation
1 parent 491ed3f commit f351de0

File tree

5 files changed

+24
-67
lines changed

5 files changed

+24
-67
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
setup(
3737
name="dwriteshapepy",
38-
version= '1.0.6',
38+
version= '1.0.7',
3939
description="Python extension for Windows DirectWrite shaping, modeled after uharfbuzz ",
4040
long_description=long_description,
4141
long_description_content_type='text/markdown',

src/cpp/DWriteShapeInternal.cpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,6 @@ namespace DWriteShapeInternal
5858

5959
Font::~Font()
6060
{
61-
// Clear any previous features
62-
for (auto& it : features_)
63-
{
64-
if(it.features != nullptr)
65-
{
66-
delete[] it.features;
67-
}
68-
}
69-
features_.clear();
70-
7161
SafeRelease(&fontFaceReference_);
7262
SafeRelease(&fontFace_);
7363

@@ -117,50 +107,13 @@ namespace DWriteShapeInternal
117107
return S_OK;
118108
}
119109

120-
HRESULT Font::SetFontFeatures(const std::vector<DWRITE_TYPOGRAPHIC_FEATURES>& features, const std::vector<UINT32>& featureRangeLengths)
121-
{
122-
fontRealized_ = false;
123-
124-
// Clear any previous features
125-
for (auto& it : features_)
126-
{
127-
if(it.features != nullptr)
128-
{
129-
delete[] it.features;
130-
}
131-
}
132-
features_.clear();
133-
134-
// Copy features from client
135-
for (auto& it : features)
136-
{
137-
DWRITE_TYPOGRAPHIC_FEATURES localFeatures;
138-
localFeatures.featureCount = it.featureCount;
139-
DWRITE_FONT_FEATURE* pLocalFeatures = new DWRITE_FONT_FEATURE[it.featureCount];
140-
141-
localFeatures.featureCount = it.featureCount;
142-
for(UINT32 i = 0; i < localFeatures.featureCount; i++ )
143-
{
144-
pLocalFeatures[i].nameTag = it.features[i].nameTag;
145-
pLocalFeatures[i].parameter = it.features[i].parameter;
146-
}
147-
localFeatures.features = pLocalFeatures;
148-
149-
features_.push_back(localFeatures);
150-
}
151-
152-
featureRangeLengths_ = featureRangeLengths;
153-
154-
return S_OK;
155-
}
156-
157-
HRESULT Font::Shape(const std::wstring& text, const std::wstring& localeName, float fontEmSize, TextRunShapeOutput& output)
110+
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)
158111
{
159112
TextRun* textRun = nullptr;
160113

161114
IFR(RealizeFont());
162115

163-
textRun = new TextRun(face_.dwriteFactory_, fontFace_, face_.textAnalyzer_, features_, featureRangeLengths_);
116+
textRun = new TextRun(face_.dwriteFactory_, fontFace_, face_.textAnalyzer_, features, featureRangeLengths);
164117

165118
// Set text and locale
166119
IFR(textRun->SetText(text, localeName));

src/cpp/DWriteShapeInternal.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,15 @@ namespace DWriteShapeInternal
3232
HRESULT SetVariationAxis(const std::vector<DWRITE_FONT_AXIS_VALUE>& axisValues);
3333
std::string GlyphToString(uint16_t glyphId);
3434

35-
HRESULT SetFontFeatures(const std::vector<DWRITE_TYPOGRAPHIC_FEATURES>& features, const std::vector<UINT32>& featureRangeLengths);
36-
HRESULT Shape(const std::wstring& text, const std::wstring& localeName, float fontEmSize, TextRunShapeOutput& output);
35+
HRESULT 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);
3736

3837
UINT16 GetDesignUnitsPerEm();
3938

4039
private:
4140
Face& face_;
4241

4342
std::vector<DWRITE_FONT_AXIS_VALUE> axisValues_;
44-
std::vector<DWRITE_TYPOGRAPHIC_FEATURES> features_;
45-
std::vector<UINT32> featureRangeLengths_;
46-
43+
4744
DWRITE_FONT_METRICS dwriteFontMetrics_ = { 0 };
4845

4946
IDWriteFontFaceReference1* fontFaceReference_ = nullptr;

src/cpp/DWriteShapeLib.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,12 @@ bool Face_::Create(const char* fontData, unsigned int fontDataSize, unsigned int
259259
//
260260

261261
Font_::Font_(Face_* pface) : pface_(pface)
262-
{
262+
{
263263
}
264264

265265
Font_::~Font_()
266266
{
267+
this->ClearFeatures();
267268
}
268269

269270
bool Font_::Create()
@@ -321,8 +322,7 @@ bool Font_::SetVariations(const std::vector<hb_variation_t> &axisValues)
321322

322323
bool Font_::SetFeatures(uint32_t textLength, const std::vector<hb_feature_t>& features)
323324
{
324-
std::vector<DWRITE_TYPOGRAPHIC_FEATURES> typographicFeatures;
325-
std::vector<UINT32> featureRangeLengths;
325+
this->ClearFeatures();
326326

327327
if (textLength == 0 || features.size() == 0)
328328
return true;
@@ -396,12 +396,12 @@ bool Font_::SetFeatures(uint32_t textLength, const std::vector<hb_feature_t>& f
396396
typographicFeature.featureCount = static_cast<UINT32>(localFeatures.size());
397397
typographicFeature.features = pLocalFeatures;
398398

399-
typographicFeatures.push_back(typographicFeature);
399+
typographicFeatures_.push_back(typographicFeature);
400400

401401
// Calculate featureRangeLengths
402402
if (i > 0)
403403
{
404-
featureRangeLengths.push_back(featureRangeLength);
404+
featureRangeLengths_.push_back(featureRangeLength);
405405
featureRangeLength = 0;
406406
}
407407
}
@@ -410,16 +410,18 @@ bool Font_::SetFeatures(uint32_t textLength, const std::vector<hb_feature_t>& f
410410
}
411411

412412
// Final length
413-
featureRangeLengths.push_back(featureRangeLength);
413+
featureRangeLengths_.push_back(featureRangeLength);
414414
featureRangeLength = 0;
415415

416-
assert(typographicFeatures.size() == featureRangeLengths.size());
416+
assert(typographicFeatures_.size() == featureRangeLengths_.size());
417417

418-
// Set features in DWriteShape::Font object
419-
HRESULT hr = pfont_->SetFontFeatures(typographicFeatures, featureRangeLengths);
418+
return true;
419+
}
420420

421+
void Font_::ClearFeatures()
422+
{
421423
// Clean up
422-
for (auto& it : typographicFeatures)
424+
for (auto& it : typographicFeatures_)
423425
{
424426
if (it.features != nullptr)
425427
{
@@ -428,7 +430,8 @@ bool Font_::SetFeatures(uint32_t textLength, const std::vector<hb_feature_t>& f
428430
}
429431
}
430432

431-
return hr == S_OK;
433+
typographicFeatures_.clear();
434+
featureRangeLengths_.clear();
432435
}
433436

434437
void Font_::GlyphToString(uint16_t glyphId, char* string, unsigned int size)
@@ -454,7 +457,7 @@ bool Font_::Shape(Buffer_* buffer)
454457
float fontEmSize = this->GetFontEmSize();
455458

456459
// Shape DWriteShape::Font
457-
HRESULT hr = pfont_->Shape(text, buffer->Locale(), fontEmSize, output);
460+
HRESULT hr = pfont_->Shape(text, buffer->Locale(), fontEmSize, output, typographicFeatures_, featureRangeLengths_);
458461

459462
buffer->SetOutput(output);
460463

src/cpp/DWriteShapeLib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Font_
8080

8181
bool SetVariations(const std::vector<hb_variation_t> &axisValues);
8282
bool SetFeatures(uint32_t textLength, const std::vector<hb_feature_t>& features);
83+
void ClearFeatures();
8384

8485
void GlyphToString(uint16_t glyphId, char* string, unsigned int size);
8586

@@ -93,6 +94,9 @@ class Font_
9394

9495
float fontEmSize_ = 0;
9596
bool fontEmSizeSet_ = false;
97+
98+
std::vector<DWRITE_TYPOGRAPHIC_FEATURES> typographicFeatures_;
99+
std::vector<UINT32> featureRangeLengths_;
96100
};
97101

98102
void Shape(Font_* font, Buffer_* buffer, const std::vector<hb_feature_t>& features);

0 commit comments

Comments
 (0)