File: format.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (257 lines) | stat: -rw-r--r-- 6,014 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
252
253
254
255
256
257
use lang:bs;
use lang:bs:macro;

/**
 * A formatted string literal for easy creation of Text objects.
 */
class FormatStr {
	// Concatenated string.
	package Str string;

	// All text effects to apply.
	package TextEffect[] effects;

	// Create.
	init(Str string, TextEffect[] effects) {
		init() {
			string = string;
			effects = effects;
		}
	}

	// Allow casting from regular strings.
	cast(Str string) {
		init() {
			string = string;
		}
	}

	// Create a copy with additional effects.
	FormatStr extend(TextEffect[] add) {
		var copy = Array<TextEffect>(effects);
		copy.append(add);
		FormatStr(string, copy);
	}
}

// Create a Text object from the formatted string.
Text text(FormatStr from, Font font) on Render {
	Text t(from.string, font);
	// Apply formatting.
	for (e in from.effects)
		t.effect(e);
	t;
}


/**
 * Builder class for the format. Called from syntax.
 */
class FormatBuilder {
	// String buffer for the string.
	private StrBuf buffer;

	// Number of characters in buffer.
	private Nat bufferLen;

	// Text effects.
	TextEffect[] effects;

	// Dynamic effects.
	lang:bs:Expr[] dynamicEffects;

	// Dummy string to get iterators from.
	private Str:Iter dummy;

	// Create.
	init() {
		init() {}
	}

	// Add a string.
	void addUnescape(Str str) {
		add(str.unescape());
	}

	// Add a string.
	void add(Str str) {
		buffer << str;
		bufferLen += str.end - str.begin;
	}

	// Make 'content' italic.
	void italic(FormatBuilder content) {
		var old = merge(content);
		addEffect(old, TextEffect:italic(dummy, dummy, true));
	}

	// Make 'content' bold.
	void bold(FormatBuilder content) {
		var old = merge(content);
		addEffect(old, TextEffect:weight(dummy, dummy, 800));
	}

	// Make 'content' underlined.
	void underline(FormatBuilder content) {
		var old = merge(content);
		addEffect(old, TextEffect:underline(dummy, dummy, true));
	}

	// Make 'content' strike-out.
	void strikeOut(FormatBuilder content) {
		var old = merge(content);
		addEffect(old, TextEffect:strikeOut(dummy, dummy, true));
	}

	// Set weight.
	void weight(FormatBuilder content, Int weight) {
		var old = merge(content);
		addEffect(old, TextEffect:weight(dummy, dummy, weight));
	}

	void weight(lang:bs:Block block, FormatBuilder content, lang:bs:Expr expr) {
		var old = merge(content);
		addEffect(block, old, "weight", expr);
	}

	// Make 'content' a different scale.
	void scale(FormatBuilder content, Float scale) {
		var old = merge(content);
		addEffect(old, TextEffect:scaleSize(dummy, dummy, scale));
	}

	void scale(lang:bs:Block block, FormatBuilder content, lang:bs:Expr expr) {
		var old = merge(content);
		addEffect(block, old, "scaleSize", expr);
	}

	// Make 'content' a different color.
	void color(FormatBuilder content, Float r, Float g, Float b) {
		var old = merge(content);
		addEffect(old, TextEffect:color(dummy, dummy, graphics:Color(r, g, b)));
	}

	void color(lang:bs:Block block, FormatBuilder content, lang:bs:Expr expr) {
		var old = merge(content);
		addEffect(block, old, "color", expr);
	}

	// Make 'content' a different fomt family.
	void font(FormatBuilder content, Str family) {
		var old = merge(content);
		addEffect(old, TextEffect:family(dummy, dummy, family));
	}

	void font(lang:bs:Block block, FormatBuilder content, lang:bs:Expr expr) {
		var old = merge(content);
		addEffect(block, old, "family", expr);
	}

	// Add effect, and update 'from' and 'to'.
	private void addEffect(Nat old, TextEffect e) {
		e.from = old;
		e.to = bufferLen;
		effects << e;
	}

	// Add a dynamic effect.
	private void addEffect(lang:bs:Block block, Nat old, Str name, lang:bs:Expr expr) {
		dynamicEffects << createAddExpr(block, old, bufferLen, name, expr);
	}

	// Merge content into 'buffer'. Returns old 'bufferLen' for convenience.
	private Nat merge(FormatBuilder other) {
		Nat oldLen = bufferLen;
		add(other.buffer.toS);

		for (e in other.effects) {
			e.from += oldLen;
			e.to += oldLen;
			effects << e;
		}

		oldLen;
	}

	// Get the content.
	Str toS() {
		buffer.toS;
	}
}

// Helper for creating syntax objects.
private lang:bs:Expr createAddExpr(lang:bs:Block block, Nat from, Nat to, Str name, lang:bs:Expr expr) {
	var pos = expr.pos;
	var c = ExprBlock(pos, block);

	var ctorName = core:lang:SrcName(pos);
	ctorName.add("core");
	ctorName.add("Str");
	ctorName.add("Iter");

	var iterExpr = namedExpr(block, ctorName, Actuals());

	var fullName = core:lang:SrcName(pos);
	fullName.add("ui");
	fullName.add("TextEffect");
	fullName.add(name);

	var actuals = Actuals();
	actuals.add(iterExpr);
	actuals.add(iterExpr);
	actuals.add(expr);
	var effectExpr = namedExpr(block, fullName, actuals);

	var tmpVar = Var(c, lang:SStr("tmp"), effectExpr);
	c.add(tmpVar);

	var tmpAccess = LocalVarAccess(pos, tmpVar.var);
	var assign = assignOperator(lang:SStr("="), 100);
	c.add(Operator(c, namedExpr(c, pos, "from", tmpAccess), assign, NumLiteral(pos, from.long)));
	c.add(Operator(c, namedExpr(c, pos, "to", tmpAccess), assign, NumLiteral(pos, to.long)));
	c.add(tmpAccess);

	c;
}

// Generate syntax for creating a FormatStr.
lang:bs:Expr formattedStr(lang:SrcPos pos, lang:bs:Block block, FormatBuilder builder) on Compiler {
	FormatStrRef object(FormatStr(builder.toS, builder.effects));

	if (builder.dynamicEffects.any) {
		// If we have dynamic effects, we need to add them here.
		var exprArray = ArrayInit(pos, block, builder.dynamicEffects);
		var extendCall = namedExpr(block, pos, "extend", object, Actuals(exprArray));
		extendCall;
	} else {
		object;
	}
}


/**
 * Reference to a FormatStr.
 */
class FormatStrRef extends Expr {
	private FormatStr object;

	init(FormatStr object) {
		init(lang:SrcPos()) { object = object; }
	}

	lang:ExprResult result() {
		lang:ExprResult(lang:Value(named{FormatStr}));
	}

	void code(lang:CodeGen state, lang:CodeResult result) {
		if (result.needed) {
			var v = result.location(state);
			state.l << core:asm:mov(v, core:asm:objPtr(object));
			result.created(state);
		}
	}

	Str toS() {
		"<formatted text>";
	}
}