ui-sys 0.1.3

Native bindings to the minimalist, cross-platform, widget set `libui`
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
// 12 february 2017
#include "uipriv_windows.hpp"
#include "attrstr.hpp"

// TODO this whole file needs cleanup

// yep, even when it supports C++11, it doesn't support C++11
// we require MSVC 2013; this was added in MSVC 2015 (https://msdn.microsoft.com/en-us/library/wfa0edys.aspx)
#ifdef _MSC_VER
#if _MSC_VER < 1900
#define noexcept
#endif
#endif

// we need to collect all the background parameters and add them all at once
// TODO consider having background parameters in the drawing effects
// TODO contextual alternates override ligatures?
// TODO rename this struct to something that isn't exclusively foreach-ing?
struct foreachParams {
	const uint16_t *s;
	size_t len;
	IDWriteTextLayout *layout;
	std::vector<struct drawTextBackgroundParams *> *backgroundParams;
};

static std::hash<double> doubleHash;

// we need to combine color and underline style into one unit for IDWriteLayout::SetDrawingEffect()
// we also want to combine identical effects, which DirectWrite doesn't seem to provide a way to do
// we can at least try to goad it into doing so if we can deduplicate effects once they're all computed
// so what we do is use this class to store in-progress effects, much like uiprivCombinedFontAttr on the OS X code
// we then deduplicate them later while converting them into a form suitable for drawing with; see applyEffectsAttributes() below
class combinedEffectsAttr : public IUnknown {
	ULONG refcount;
	uiAttribute *colorAttr;
	uiAttribute *underlineAttr;
	uiAttribute *underlineColorAttr;

	void setAttribute(uiAttribute *a)
	{
		if (a == NULL)
			return;
		switch (uiAttributeGetType(a)) {
		case uiAttributeTypeColor:
			if (this->colorAttr != NULL)
				uiprivAttributeRelease(this->colorAttr);
			this->colorAttr = uiprivAttributeRetain(a);
			break;
		case uiAttributeTypeUnderline:
			if (this->underlineAttr != NULL)
				uiprivAttributeRelease(this->underlineAttr);
			this->underlineAttr = uiprivAttributeRetain(a);
			break;
		case uiAttributeTypeUnderlineColor:
			if (this->underlineAttr != NULL)
				uiprivAttributeRelease(this->underlineAttr);
			this->underlineColorAttr = uiprivAttributeRetain(a);
			break;
		}
	}

	// this is needed by applyEffectsAttributes() below
	// TODO doesn't uiprivAttributeEqual() already do this; if it doesn't, make it so; if (or when) it does, fix all platforms to avoid this extra check
	static bool attrEqual(uiAttribute *a, uiAttribute *b)
	{
		if (a == NULL && b == NULL)
			return true;
		if (a == NULL || b == NULL)
			return false;
		return uiprivAttributeEqual(a, b);
	}
public:
	combinedEffectsAttr(uiAttribute *a)
	{
		this->refcount = 1;
		this->colorAttr = NULL;
		this->underlineAttr = NULL;
		this->underlineColorAttr = NULL;
		this->setAttribute(a);
	}

	~combinedEffectsAttr()
	{
		if (this->colorAttr != NULL)
			uiprivAttributeRelease(this->colorAttr);
		if (this->underlineAttr != NULL)
			uiprivAttributeRelease(this->underlineAttr);
		if (this->underlineColorAttr != NULL)
			uiprivAttributeRelease(this->underlineColorAttr);
	}

	// IUnknown
	virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject)
	{
		if (ppvObject == NULL)
			return E_POINTER;
		if (riid == IID_IUnknown) {
			this->AddRef();
			*ppvObject = this;
			return S_OK;
		}
		*ppvObject = NULL;
		return E_NOINTERFACE;
	}

	virtual ULONG STDMETHODCALLTYPE AddRef(void)
	{
		this->refcount++;
		return this->refcount;
	}

	virtual ULONG STDMETHODCALLTYPE Release(void)
	{
		this->refcount--;
		if (this->refcount == 0) {
			delete this;
			return 0;
		}
		return this->refcount;
	}

	combinedEffectsAttr *cloneWith(uiAttribute *a)
	{
		combinedEffectsAttr *b;

		b = new combinedEffectsAttr(this->colorAttr);
		b->setAttribute(this->underlineAttr);
		b->setAttribute(this->underlineColorAttr);
		b->setAttribute(a);
		return b;
	}

	// and these are also needed by applyEffectsAttributes() below
	size_t hash(void) const noexcept
	{
		size_t ret = 0;
		double r, g, b, a;
		uiUnderlineColor colorType;

		if (this->colorAttr != NULL) {
			uiAttributeColor(this->colorAttr, &r, &g, &b, &a);
			ret ^= doubleHash(r);
			ret ^= doubleHash(g);
			ret ^= doubleHash(b);
			ret ^= doubleHash(a);
		}
		if (this->underlineAttr != NULL)
			ret ^= (size_t) uiAttributeUnderline(this->underlineAttr);
		if (this->underlineColorAttr != NULL) {
			uiAttributeUnderlineColor(this->underlineColorAttr, &colorType, &r, &g, &b, &a);
			ret ^= (size_t) colorType;
			ret ^= doubleHash(r);
			ret ^= doubleHash(g);
			ret ^= doubleHash(b);
			ret ^= doubleHash(a);
		}
		return ret;
	}

	bool equals(const combinedEffectsAttr *b) const
	{
		if (b == NULL)
			return false;
		return combinedEffectsAttr::attrEqual(this->colorAttr, b->colorAttr) &&
			combinedEffectsAttr::attrEqual(this->underlineAttr, b->underlineAttr) &&
			combinedEffectsAttr::attrEqual(this->underlineColorAttr, b->underlineColorAttr);
	}

	drawingEffectsAttr *toDrawingEffectsAttr(void)
	{
		drawingEffectsAttr *dea;
		double r, g, b, a;
		uiUnderlineColor colorType;

		dea = new drawingEffectsAttr;
		if (this->colorAttr != NULL) {
			uiAttributeColor(this->colorAttr, &r, &g, &b, &a);
			dea->setColor(r, g, b, a);
		}
		if (this->underlineAttr != NULL)
			dea->setUnderline(uiAttributeUnderline(this->underlineAttr));
		if (this->underlineColorAttr != NULL) {
			uiAttributeUnderlineColor(this->underlineColorAttr, &colorType, &r, &g, &b, &a);
			// TODO see if Microsoft has any standard colors for these
			switch (colorType) {
			case uiUnderlineColorSpelling:
				// TODO consider using the GtkTextView style property error-underline-color here if Microsoft has no preference
				r = 1.0;
				g = 0.0;
				b = 0.0;
				a = 1.0;
				break;
			case uiUnderlineColorGrammar:
				r = 0.0;
				g = 1.0;
				b = 0.0;
				a = 1.0;
				break;
			case uiUnderlineColorAuxiliary:
				r = 0.0;
				g = 0.0;
				b = 1.0;
				a = 1.0;
				break;
			}
			dea->setUnderlineColor(r, g, b, a);
		}
		return dea;
	}
};

// also needed by applyEffectsAttributes() below
// TODO provide all the fields of std::hash and std::equal_to?
class applyEffectsHash {
public:
	typedef combinedEffectsAttr *ceaptr;
	size_t operator()(applyEffectsHash::ceaptr const &cea) const noexcept
	{
		return cea->hash();
	}
};

class applyEffectsEqualTo {
public:
	typedef combinedEffectsAttr *ceaptr;
	bool operator()(const applyEffectsEqualTo::ceaptr &a, const applyEffectsEqualTo::ceaptr &b) const
	{
		return a->equals(b);
	}
};

static HRESULT addEffectAttributeToRange(struct foreachParams *p, size_t start, size_t end, uiAttribute *attr)
{
	IUnknown *u;
	combinedEffectsAttr *cea;
	DWRITE_TEXT_RANGE range;
	size_t diff;
	HRESULT hr;

	while (start < end) {
		hr = p->layout->GetDrawingEffect(start, &u, &range);
		if (hr != S_OK)
			return hr;
		cea = (combinedEffectsAttr *) u;
		if (cea == NULL)
			cea = new combinedEffectsAttr(attr);
		else
			cea = cea->cloneWith(attr);
		// clamp range within [start, end)
		if (range.startPosition < start) {
			diff = start - range.startPosition;
			range.startPosition = start;
			range.length -= diff;
		}
		if ((range.startPosition + range.length) > end)
			range.length = end - range.startPosition;
		hr = p->layout->SetDrawingEffect(cea, range);
		// SetDrawingEffect will AddRef(), so Release() our copy
		// (and we're abandoning early if that failed, so this will make sure things are cleaned up in that case)
		cea->Release();
		if (hr != S_OK)
			return hr;
		start += range.length;
	}
	return S_OK;
}

static void addBackgroundParams(struct foreachParams *p, size_t start, size_t end, const uiAttribute *attr)
{
	struct drawTextBackgroundParams *params;

	params = uiprivNew(struct drawTextBackgroundParams);
	params->start = start;
	params->end = end;
	uiAttributeColor(attr, &(params->r), &(params->g), &(params->b), &(params->a));
	p->backgroundParams->push_back(params);
}

static uiForEach processAttribute(const uiAttributedString *s, const uiAttribute *attr, size_t start, size_t end, void *data)
{
	struct foreachParams *p = (struct foreachParams *) data;
	DWRITE_TEXT_RANGE range;
	WCHAR *wfamily;
	BOOL hasUnderline;
	IDWriteTypography *dt;
	HRESULT hr;

	start = uiprivAttributedStringUTF8ToUTF16(s, start);
	end = uiprivAttributedStringUTF8ToUTF16(s, end);
	range.startPosition = start;
	range.length = end - start;
	switch (uiAttributeGetType(attr)) {
	case uiAttributeTypeFamily:
		wfamily = toUTF16(uiAttributeFamily(attr));
		hr = p->layout->SetFontFamilyName(wfamily, range);
		if (hr != S_OK)
			logHRESULT(L"error applying family name attribute", hr);
		uiprivFree(wfamily);
		break;
	case uiAttributeTypeSize:
		hr = p->layout->SetFontSize(
// TODO unify with fontmatch.cpp and/or attrstr.hpp
#define pointSizeToDWriteSize(size) (size * (96.0 / 72.0))
			pointSizeToDWriteSize(uiAttributeSize(attr)),
			range);
		if (hr != S_OK)
			logHRESULT(L"error applying size attribute", hr);
		break;
	case uiAttributeTypeWeight:
		hr = p->layout->SetFontWeight(
			uiprivWeightToDWriteWeight(uiAttributeWeight(attr)),
			range);
		if (hr != S_OK)
			logHRESULT(L"error applying weight attribute", hr);
		break;
	case uiAttributeTypeItalic:
		hr = p->layout->SetFontStyle(
			uiprivItalicToDWriteStyle(uiAttributeItalic(attr)),
			range);
		if (hr != S_OK)
			logHRESULT(L"error applying italic attribute", hr);
		break;
	case uiAttributeTypeStretch:
		hr = p->layout->SetFontStretch(
			uiprivStretchToDWriteStretch(uiAttributeStretch(attr)),
			range);
		if (hr != S_OK)
			logHRESULT(L"error applying stretch attribute", hr);
		break;
	case uiAttributeTypeUnderline:
		// mark that we have an underline; otherwise, DirectWrite will never call our custom renderer's DrawUnderline() method
		hasUnderline = FALSE;
		if (uiAttributeUnderline(attr) != uiUnderlineNone)
			hasUnderline = TRUE;
		hr = p->layout->SetUnderline(hasUnderline, range);
		if (hr != S_OK)
			logHRESULT(L"error applying underline attribute", hr);
		// and fall through to set the underline style through the drawing effect
	case uiAttributeTypeColor:
	case uiAttributeTypeUnderlineColor:
		// TODO const-correct this properly
		hr = addEffectAttributeToRange(p, start, end, (uiAttribute *) attr);
		if (hr != S_OK)
			logHRESULT(L"error applying effect (color, underline, or underline color) attribute", hr);
		break;
	case uiAttributeTypeBackground:
		addBackgroundParams(p, start, end, attr);
		break;
	case uiAttributeTypeFeatures:
		// only generate an attribute if not NULL
		// TODO do we still need to do this or not...
		if (uiAttributeFeatures(attr) == NULL)
			break;
		dt = uiprivOpenTypeFeaturesToIDWriteTypography(uiAttributeFeatures(attr));
		hr = p->layout->SetTypography(dt, range);
		if (hr != S_OK)
			logHRESULT(L"error applying features attribute", hr);
		dt->Release();
		break;
	}
	return uiForEachContinue;
}

static HRESULT applyEffectsAttributes(struct foreachParams *p)
{
	IUnknown *u;
	combinedEffectsAttr *cea;
	drawingEffectsAttr *dea;
	DWRITE_TEXT_RANGE range;
	// here's the magic: this std::unordered_map will deduplicate all of our combinedEffectsAttrs, mapping all identical ones to a single drawingEffectsAttr
	// because drawingEffectsAttr is the *actual* drawing effect we want for rendering, we also replace the combinedEffectsAttrs with them in the IDWriteTextLayout at the same time
	// note the use of our custom hash and equal_to implementations
	std::unordered_map<combinedEffectsAttr *, drawingEffectsAttr *,
		applyEffectsHash, applyEffectsEqualTo> effects;
	HRESULT hr;

	// go through, replacing every combinedEffectsAttr with the proper drawingEffectsAttr
	range.startPosition = 0;
	// and in case this while loop never runs, make hr valid to start with
	hr = S_OK;
	while (range.startPosition < p->len) {
		hr = p->layout->GetDrawingEffect(range.startPosition, &u, &range);
		if (hr != S_OK)
			// note that we are breaking instead of returning; this allows us to clean up on failure
			break;
		cea = (combinedEffectsAttr *) u;
		if (cea != NULL) {
			auto diter = effects.find(cea);
			if (diter != effects.end())
				dea = diter->second;
			else {
				dea = cea->toDrawingEffectsAttr();
				effects.insert({cea, dea});
			}
			hr = p->layout->SetDrawingEffect(dea, range);
			// don't release dea; we need the reference that's inside the map
			// (we don't take extra references on lookup, so this will be fine)
			if (hr != S_OK)
				break;
		}
		range.startPosition += range.length;
	}

	// and clean up, finally destroying the combinedEffectAttrs too
	// we do this in the case of failure as well, to make sure everything is properly cleaned up
	for (auto iter = effects.begin(); iter != effects.end(); iter++) {
		iter->first->Release();
		iter->second->Release();
	}
	return hr;
}

void uiprivAttributedStringApplyAttributesToDWriteTextLayout(uiDrawTextLayoutParams *p, IDWriteTextLayout *layout, std::vector<struct drawTextBackgroundParams *> **backgroundParams)
{
	struct foreachParams fep;
	HRESULT hr;

	fep.s = uiprivAttributedStringUTF16String(p->String);
	fep.len = uiprivAttributedStringUTF16Len(p->String);
	fep.layout = layout;
	fep.backgroundParams = new std::vector<struct drawTextBackgroundParams *>;
	uiAttributedStringForEachAttribute(p->String, processAttribute, &fep);
	hr = applyEffectsAttributes(&fep);
	if (hr != S_OK)
		logHRESULT(L"error applying effects attributes", hr);
	*backgroundParams = fep.backgroundParams;
}