Mercurial > projects > dynamin
comparison dynamin/painting/text_layout.d @ 85:ded31d46f75f
Add TextLayout. So far, it handles character formatting and does not draw.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 19 Jul 2010 18:37:14 -0500 |
parents | |
children | 41b430aa319f |
comparison
equal
deleted
inserted
replaced
84:7653269724db | 85:ded31d46f75f |
---|---|
1 module dynamin.painting.text_layout; | |
2 | |
3 import dynamin.core.list; | |
4 import dynamin.core.string; | |
5 import dynamin.painting.color; | |
6 import tango.text.convert.Utf; | |
7 import tango.io.Stdout; | |
8 | |
9 //{{{ character formatting types | |
10 /// The line style of an underline, strikethrough, or overline. | |
11 // TODO: add images of what these line styles look like | |
12 enum LineStyle { | |
13 /// | |
14 None, | |
15 /// | |
16 Single, | |
17 /// | |
18 Double, | |
19 /// | |
20 Dotted, | |
21 /// | |
22 Dashed, | |
23 /// | |
24 Wavy | |
25 } | |
26 | |
27 /// | |
28 enum SmallType { | |
29 // Specifies normal text. | |
30 Normal, | |
31 // Specifies text smaller than normal and raised above the normal baseline. | |
32 Superscript, | |
33 // Specifies text smaller than normal and lowered below the normal baseline. | |
34 Subscript | |
35 } | |
36 | |
37 /// A change in character formatting. | |
38 struct FormatChange { | |
39 uint index; | |
40 FormatType type; | |
41 FormatData data; | |
42 | |
43 static FormatChange opCall(uint i, FormatType t, FormatData d) { | |
44 FormatChange c; | |
45 c.index = i; | |
46 c.type = t; | |
47 c.data = d; | |
48 return c; | |
49 } | |
50 } | |
51 /** | |
52 * Returns true if data1 is equal to data2. | |
53 */ | |
54 bool formatDataEqual(FormatType type, FormatData data1, FormatData data2) { | |
55 if(type == FormatType.FontFamily) | |
56 return data1.family == data2.family; | |
57 else if(type == FormatType.FontSize) | |
58 return data1.size == data2.size; | |
59 else if(type == FormatType.Bold || type == FormatType.Italic) | |
60 return data1.on == data2.on; | |
61 else if(type == FormatType.Underline || type == FormatType.Strikethrough || | |
62 type == FormatType.Overline) | |
63 return data1.style == data2.style; | |
64 else if(type == FormatType.Small) | |
65 return data1.type == data2.type; | |
66 else if(type == FormatType.ForeColor || type == FormatType.BackColor) | |
67 return data1.color == data2.color; | |
68 else if(type == FormatType.Spacing) | |
69 return data1.multiple == data2.multiple; | |
70 else | |
71 throw new Exception("unknown type"); | |
72 } | |
73 | |
74 /// | |
75 enum FormatType : ubyte { | |
76 /// | |
77 FontFamily = 1, | |
78 /// | |
79 FontSize, | |
80 /// | |
81 Bold, | |
82 /// | |
83 Italic, | |
84 /// | |
85 Underline, | |
86 /// | |
87 Strikethrough, | |
88 /// | |
89 Overline, | |
90 /// | |
91 Small, | |
92 /// | |
93 ForeColor, | |
94 /// | |
95 BackColor, | |
96 /// | |
97 Spacing | |
98 } | |
99 | |
100 /// | |
101 union FormatData { | |
102 /// Valid for FontFamily | |
103 string family; | |
104 /// Valid for FontSize | |
105 double size; | |
106 /// Valid for Bold and Italic | |
107 bool on; | |
108 /// Valid for Underline, Strikethrough, and Overline | |
109 LineStyle style; | |
110 /// Valid for Small | |
111 SmallType type; | |
112 /// Valid for ForeColor and BackColor | |
113 Color color; | |
114 /// Valid for Spacing | |
115 double multiple; | |
116 } | |
117 | |
118 struct Format { | |
119 string fontFamily; // no default | |
120 double fontSize; // no default | |
121 bool bold = false; | |
122 bool italic = false; | |
123 LineStyle underline = LineStyle.None; | |
124 LineStyle strikethrough = LineStyle.None; | |
125 LineStyle overline = LineStyle.None; | |
126 SmallType small = SmallType.Normal; | |
127 Color foreColor = Color(255, 0, 0, 0); // black | |
128 Color backColor = Color( 0, 0, 0, 0); // transparent | |
129 double spacing = 1.0; | |
130 | |
131 FormatData getDataForType(FormatType type) { | |
132 FormatData data; | |
133 if(type == FormatType.FontFamily) | |
134 data.family = fontFamily; | |
135 else if(type == FormatType.FontSize) | |
136 data.size = fontSize; | |
137 else if(type == FormatType.Bold) | |
138 data.on = bold; | |
139 else if(type == FormatType.Italic) | |
140 data.on = italic; | |
141 else if(type == FormatType.Underline) | |
142 data.style = underline; | |
143 else if(type == FormatType.Strikethrough) | |
144 data.style = strikethrough; | |
145 else if(type == FormatType.Overline) | |
146 data.style = overline; | |
147 else if(type == FormatType.Small) | |
148 data.type = small; | |
149 else if(type == FormatType.ForeColor) | |
150 data.color = foreColor; | |
151 else if(type == FormatType.BackColor) | |
152 data.color = backColor; | |
153 else if(type == FormatType.Spacing) | |
154 data.multiple = spacing; | |
155 else | |
156 throw new Exception("unknown type"); | |
157 return data; | |
158 } | |
159 } | |
160 //}}} | |
161 | |
162 /// | |
163 enum TextAlignment { | |
164 /// | |
165 Left, | |
166 /// | |
167 Center, | |
168 /// | |
169 Right, | |
170 /// | |
171 Justify | |
172 } | |
173 | |
174 /// | |
175 enum TabStopType { | |
176 /// | |
177 Left, | |
178 /// | |
179 Center, | |
180 /// | |
181 Right, | |
182 /// | |
183 Decimal | |
184 } | |
185 | |
186 /// | |
187 struct TabStop { | |
188 /// | |
189 uint location; | |
190 /// | |
191 TabStopType type = TabStopType.Left; | |
192 /// | |
193 char leading = '.'; | |
194 } | |
195 | |
196 /** | |
197 * | |
198 */ | |
199 class TextLayout { | |
200 string text; | |
201 | |
202 // character formatting | |
203 List!(FormatChange) formatting; // Always sorted by FormatChange.index | |
204 Format initialFormat; | |
205 | |
206 // paragraph formatting | |
207 double lineSpacing = 1.0; | |
208 double defaultTabStopLocations = 0; // 0 means default tabs every (4 * width of character '0') | |
209 TabStop[] tabStops; | |
210 TextAlignment alignment = TextAlignment.Left; | |
211 | |
212 this(string fontFamily, double fontSize) { | |
213 formatting = new List!(FormatChange); | |
214 initialFormat.fontFamily = fontFamily; | |
215 initialFormat.fontSize = fontSize; | |
216 } | |
217 invariant { | |
218 // ensure that formatting is sorted correctly | |
219 uint index = 0; | |
220 foreach(change; formatting) { | |
221 assert(change.index >= index); | |
222 index = change.index; | |
223 } | |
224 } | |
225 | |
226 //{{{ character formatting | |
227 void setFontFamily(string family, uint start, uint length) { | |
228 FormatData data; | |
229 data.family = family; | |
230 setFormat(FormatType.FontFamily, data, start, length); | |
231 } | |
232 void setFontSize(double size, uint start, uint length) { | |
233 FormatData data; | |
234 data.size = size; | |
235 setFormat(FormatType.FontSize, data, start, length); | |
236 } | |
237 | |
238 void setBold(bool on, uint start, uint length) { | |
239 FormatData data; | |
240 data.on = on; | |
241 setFormat(FormatType.Bold, data, start, length); | |
242 } | |
243 void setItalic(bool on, uint start, uint length) { | |
244 FormatData data; | |
245 data.on = on; | |
246 setFormat(FormatType.Italic, data, start, length); | |
247 } | |
248 void setUnderline(LineStyle style, uint start, uint length) { | |
249 FormatData data; | |
250 data.style = style; | |
251 setFormat(FormatType.Underline, data, start, length); | |
252 } | |
253 void setStrikethrough(LineStyle style, uint start, uint length) { | |
254 FormatData data; | |
255 data.style = style; | |
256 setFormat(FormatType.Strikethrough, data, start, length); | |
257 } | |
258 void setOverline(LineStyle style, uint start, uint length) { | |
259 FormatData data; | |
260 data.style = style; | |
261 setFormat(FormatType.Overline, data, start, length); | |
262 } | |
263 | |
264 /// Sets the text either superscript or subscript. | |
265 void setSmall(SmallType type, uint start, uint length) { | |
266 FormatData data; | |
267 data.type = type; | |
268 setFormat(FormatType.Small, data, start, length); | |
269 } | |
270 // see http://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing for | |
271 // info on positioning superscript and subscript | |
272 | |
273 void setForeColor(Color color, uint start, uint length) { | |
274 FormatData data; | |
275 data.color = color; | |
276 setFormat(FormatType.ForeColor, data, start, length); | |
277 } | |
278 void setBackColor(Color color, uint start, uint length) { | |
279 FormatData data; | |
280 data.color = color; | |
281 setFormat(FormatType.BackColor, data, start, length); | |
282 } | |
283 | |
284 /** | |
285 * Sets the spacing between characters, given in multiples of a character's width. | |
286 * For example, a multiple of 2.0 would make drawn text take twice as much space | |
287 * horizontally, due to twice as much space being given to each character. | |
288 */ | |
289 void setSpacing(double multiple, uint start, uint length) { | |
290 FormatData data; | |
291 data.multiple = multiple; | |
292 setFormat(FormatType.Spacing, data, start, length); | |
293 } | |
294 | |
295 void setFormat(FormatType type, FormatData data, uint start, uint length) { | |
296 uint end = start + length; | |
297 checkIndex(start); | |
298 checkIndex(end); | |
299 | |
300 FormatData endData = getFormat(type, end); | |
301 | |
302 for(int i = formatting.count-1; i >= 0; --i) { | |
303 if(formatting[i].type == type && formatting[i].index <= end) { | |
304 if(formatting[i].index >= start) | |
305 formatting.removeAt(i); | |
306 else | |
307 break; | |
308 } | |
309 } | |
310 | |
311 if(!formatDataEqual(type, getFormat(type, start), data)) { | |
312 insertFormatChange(FormatChange(start, type, data)); | |
313 } | |
314 | |
315 // make sure that the formatting >= end stays the same as it was | |
316 if(!formatDataEqual(type, endData, getFormat(type, end))) { | |
317 insertFormatChange(FormatChange(end, type, endData)); | |
318 } | |
319 } | |
320 FormatData getFormat(FormatType type, uint index) { | |
321 checkIndex(index); | |
322 FormatData data = initialFormat.getDataForType(type); | |
323 for(int i = 0; i < formatting.count; ++i) { | |
324 if(formatting[i].index > index) | |
325 break; | |
326 if(formatting[i].type == type) | |
327 data = formatting[i].data; | |
328 } | |
329 return data; | |
330 } | |
331 private void insertFormatChange(FormatChange change) { | |
332 int i = 0; | |
333 while(i < formatting.count && formatting[i].index <= change.index) | |
334 ++i; | |
335 formatting.insert(change, i); | |
336 } | |
337 //}}} | |
338 private void checkIndex(uint index) { | |
339 if(index == 0) | |
340 return; | |
341 if(cropRight!(char)(text[0..index]).length != index) | |
342 throw new Exception("index must be at a valid code point, not inside one"); | |
343 } | |
344 } | |
345 | |
346 unittest { | |
347 auto t = new TextLayout("Tahoma", 15); | |
348 t.text = "How are you doing today?"; | |
349 t.setBold(true, 4, 3); // "are" | |
350 assert(t.formatting.count == 2); | |
351 t.setBold(true, 7, 4); // " you" | |
352 assert(t.formatting.count == 2); | |
353 t.setBold(true, 1, 3); // "ow " | |
354 assert(t.formatting.count == 2); | |
355 t.setBold(true, 8, 9); // "you doing" | |
356 assert(t.formatting.count == 2); | |
357 t.setBold(true, 0, 18); // "How are you doing " | |
358 assert(t.formatting.count == 2); | |
359 assert(t.formatting[0].type == FormatType.Bold); | |
360 assert(t.formatting[0].data.on == true); | |
361 assert(t.formatting[1].type == FormatType.Bold); | |
362 assert(t.formatting[1].data.on == false); | |
363 t.setBold(false, 0, 24); | |
364 assert(t.formatting.count == 0); | |
365 | |
366 t.setBold(true, 4, 3); // "are" | |
367 assert(t.formatting.count == 2); | |
368 t.setBold(true, 8, 3); // "you" | |
369 assert(t.formatting.count == 4); | |
370 t.setBold(true, 1, 16); // "ow are you doing" | |
371 assert(t.formatting.count == 2); | |
372 t.setBold(false, 4, 8); // "are you " | |
373 assert(t.formatting.count == 4); | |
374 assert(t.formatting[0].index == 1); | |
375 assert(t.formatting[0].data.on == true); | |
376 assert(t.formatting[1].index == 4); | |
377 assert(t.formatting[1].data.on == false); | |
378 assert(t.formatting[2].index == 12); | |
379 assert(t.formatting[2].data.on == true); | |
380 assert(t.formatting[3].index == 17); | |
381 assert(t.formatting[3].data.on == false); | |
382 t.setBold(false, 1, 3); // "ow " | |
383 assert(t.formatting.count == 2); | |
384 t.setUnderline(LineStyle.Double, 8, 9); // "you doing" | |
385 assert(t.formatting.count == 4); | |
386 t.setBold(false, 0, 20); // "How are you doing t" | |
387 assert(t.formatting.count == 2); | |
388 assert(t.formatting[0].type == FormatType.Underline); | |
389 assert(t.formatting[1].type == FormatType.Underline); | |
390 t.setUnderline(LineStyle.Single, 12, 11); // "doing today" | |
391 assert(t.formatting.count == 3); | |
392 assert(t.formatting[0].data.style == LineStyle.Double); | |
393 assert(t.formatting[1].data.style == LineStyle.Single); | |
394 assert(t.formatting[2].data.style == LineStyle.None); | |
395 t.setUnderline(LineStyle.None, 4, 14); // "are you doing " | |
396 assert(t.formatting.count == 2); | |
397 assert(t.formatting[0].data.style == LineStyle.Single); | |
398 assert(t.formatting[0].index == 18); | |
399 t.setUnderline(LineStyle.None, 4, 20); // "are you doing today?" | |
400 assert(t.formatting.count == 0); | |
401 } | |
402 |