File: PangoLayout.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,984 kB
  • sloc: ansic: 261,420; cpp: 140,270; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (251 lines) | stat: -rw-r--r-- 7,245 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "PangoLayout.h"
#include "Core/Convert.h"
#include "Core/Utf.h"
#include <limits>

#ifdef GUI_GTK

namespace gui {

	static inline Rect fromPango(const PangoRectangle &r) {
		return Rect(Point(fromPango(r.x), fromPango(r.y)), Size(fromPango(r.width), fromPango(r.height)));
	}


	namespace pango {

		static PangoAttrList *layout_attrs(PangoLayout *l) {
			PangoAttrList *attrs = pango_layout_get_attributes(l);
			if (!attrs) {
				attrs = pango_attr_list_new();
				pango_layout_set_attributes(l, attrs);
				pango_attr_list_unref(attrs);
			}
			return attrs;
		}

		static inline guint16 pangoColor(Float v) {
			return guint16(v * 65535);
		}

		static inline size_t byteOffset(Str::Iter storm, const char *pango, Str::Iter goal) {
			size_t offset = 0;
			while (storm != Str::Iter() && storm.offset() < goal.offset()) {
				// Advance each iterator one codepoint.
				++storm;

				// Skip the first utf-8 byte...
				offset++;
				// ...and skip any continuation bytes.
				while (utf8::isCont(pango[offset]))
					offset++;
			}

			return offset;
		}

		static inline bool inRange(int prev, int curr, int target) {
			if (curr == prev)
				return target == prev;
			else if (curr > prev)
				return target > prev && target <= curr;
			else
				return target >= curr && target < prev;
		}

		PangoLayout *create(PangoContext *context, const Text *text) {
			PangoLayout *l = pango_layout_new(context);

			Font *font = text->font();

			pango_layout_set_wrap(l, PANGO_WRAP_WORD_CHAR);
			pango_layout_set_font_description(l, font->desc());
			updateBorder(l, text->layoutBorder());
			const char *utf8 = text->text()->utf8_str();
			int utf8Bytes = strlen(utf8);
			pango_layout_set_text(l, utf8, utf8Bytes);

			Float tabWidth = text->font()->tabWidth();
			if (tabWidth > 0) {
				PangoTabArray *array = pango_tab_array_new(1, false);
				pango_tab_array_set_tab(array, 0, PANGO_TAB_LEFT, toPango(tabWidth));
				pango_layout_set_tabs(l, array);
				pango_tab_array_free(array);
			}

			if (font->underline()) {
				PangoAttrList *attrs = layout_attrs(l);
				PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
				attr->start_index = 0;
				attr->end_index = utf8Bytes;
				pango_attr_list_change(attrs, attr);
			}

			if (font->strikeOut()) {
				PangoAttrList *attrs = layout_attrs(l);
				PangoAttribute *attr = pango_attr_strikethrough_new(TRUE);
				attr->start_index = 0;
				attr->end_index = utf8Bytes;
				pango_attr_list_change(attrs, attr);
			}

			return l;
		}

		void free(PangoLayout *layout) {
			g_object_unref(layout);
		}

		void updateBorder(PangoLayout *l, Size s) {
			int w = -1;
			int h = -1;
			if (s.w < std::numeric_limits<float>::max())
				w = toPango(s.w);
			if (s.h < std::numeric_limits<float>::max())
				h = toPango(s.h);
			pango_layout_set_width(l, w);
			pango_layout_set_height(l, h);
		}

		TextMgr::EffectResult addEffect(PangoLayout *l, const TextEffect &effect, Str *myText) {
			PangoAttrList *attrs = layout_attrs(l);

			Str::Iter begin = myText->posIter(effect.from);
			Str::Iter end = myText->posIter(effect.to);

			const char *text = pango_layout_get_text(l);
			size_t beginBytes = byteOffset(myText->begin(), text, begin);
			size_t endBytes = beginBytes + byteOffset(begin, text + beginBytes, end);

			PangoAttribute *attr = null;

			switch (effect.type) {
			case TextEffect::tColor:
				attr = pango_attr_foreground_new(pangoColor(effect.d0), pangoColor(effect.d1), pangoColor(effect.d2));
				if (effect.d3 < 1.0f) {
					attr->start_index = beginBytes;
					attr->end_index = endBytes;
					pango_attr_list_change(attrs, attr);

					attr = pango_attr_foreground_alpha_new(pangoColor(effect.d3));
				}
				break;
			case TextEffect::tUnderline:
				// There is SINGLE_LINE as well.
				attr = pango_attr_underline_new(effect.boolean() ? PANGO_UNDERLINE_SINGLE : PANGO_UNDERLINE_NONE);
				break;
			case TextEffect::tStrikeOut:
				attr = pango_attr_strikethrough_new(effect.boolean());
				break;
			case TextEffect::tItalic:
				attr = pango_attr_style_new(effect.boolean() ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
				break;
			case TextEffect::tWeight:
				attr = pango_attr_weight_new(PangoWeight(effect.integer()));
				break;
			case TextEffect::tFamily:
				attr = pango_attr_family_new(effect.family()->utf8_str());
				break;
			case TextEffect::tScaleSize:
				attr = pango_attr_scale_new(effect.d0);
				break;
			default:
#ifdef DEBUG
				WARNING(L"Unknown effect type: " << TO_S(myText, effect));
#endif
				break;
			}

			if (attr) {
				attr->start_index = beginBytes;
				attr->end_index = endBytes;
				pango_attr_list_change(attrs, attr);

				// Tell Pango to re-compute the layout since we changed the formatting. Otherwise we are
				// not able to modify the text formatting after we have rendered it once.
				pango_layout_context_changed(l);
			}

			return TextMgr::eApplied;
		}

		Size size(PangoLayout *l) {
			int w, h;
			pango_layout_get_size(l, &w, &h);
			return Size(fromPango(w), fromPango(h));
		}

		Array<TextLine *> *lineInfo(PangoLayout *l, Text *t) {
			Array<TextLine *> *result = new (t) Array<TextLine *>();
			const char *text = pango_layout_get_text(l);

			PangoLayoutIter *iter = pango_layout_get_iter(l);
			do {
				Float baseline = fromPango(pango_layout_iter_get_baseline(iter));

				PangoLayoutLine *line = pango_layout_iter_get_line_readonly(iter);
				int from = line->start_index;
				Str *z = new (t) Str(toWChar(t->engine(), text + from, line->length));
				*result << new (t) TextLine(baseline, z);
			} while (pango_layout_iter_next_line(iter));
			pango_layout_iter_free(iter);

			return result;
		}

		Array<Rect> *boundsOf(PangoLayout *l, Text *stormText, Str::Iter begin, Str::Iter end) {
			Array<Rect> *result = new (stormText) Array<Rect>();
			const char *text = pango_layout_get_text(l);
			size_t beginBytes = byteOffset(stormText->text()->begin(), text, begin);
			size_t endBytes = beginBytes + byteOffset(begin, text + beginBytes, end);

			if (beginBytes == endBytes)
				return result;

			PangoRectangle rect;

			PangoLayoutIter *iter = pango_layout_get_iter(l);
			int lastIndex = pango_layout_iter_get_index(iter);
			do {
				int index = pango_layout_iter_get_index(iter);
				if (inRange(lastIndex, index, int(beginBytes)))
					break;

				lastIndex = index;
			} while (pango_layout_iter_next_cluster(iter));

			pango_layout_iter_get_cluster_extents(iter, NULL, &rect);
			Rect lineBounds = fromPango(rect);
			int lineStart = pango_layout_iter_get_line_readonly(iter)->start_index;

			while (pango_layout_iter_next_cluster(iter)) {
				int index = pango_layout_iter_get_index(iter);
				if (inRange(lastIndex, index, int(endBytes)))
					break;
				lastIndex = index;

				pango_layout_iter_get_cluster_extents(iter, NULL, &rect);
				int currLine = pango_layout_iter_get_line_readonly(iter)->start_index;

				// New line?
				if (currLine != lineStart) {
					*result << lineBounds;
					lineBounds = fromPango(rect);
					lineStart = currLine;
				} else {
					lineBounds = lineBounds.include(fromPango(rect));
				}
			}

			*result << lineBounds;

			pango_layout_iter_free(iter);

			return result;
		}

	}
}

#endif