/
context2d.js
366 lines (309 loc) · 7.15 KB
/
context2d.js
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
'use strict';
/*!
* Canvas - Context2d
* Copyright (c) 2010 LearnBoost <tj@learnboost.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var canvas = require('./bindings')
, Context2d = canvas.CanvasRenderingContext2d
, CanvasGradient = canvas.CanvasGradient
, CanvasPattern = canvas.CanvasPattern
, ImageData = canvas.ImageData;
var parseCssFont = require('parse-css-font');
var unitsCss = require('units-css');
/**
* Export `Context2d` as the module.
*/
var Context2d = exports = module.exports = Context2d;
/**
* Cache color string RGBA values.
*/
var cache = {};
/**
* Text baselines.
*/
var baselines = ['alphabetic', 'top', 'bottom', 'middle', 'ideographic', 'hanging'];
/**
* Parse font `str`.
*
* @param {String} str
* @return {Object}
* @api private
*/
var parseFont = exports.parseFont = function(str) {
var parsedFont;
// Try to parse the font string using parse-css-font.
// It will throw an exception if it fails.
try {
parsedFont = parseCssFont(str);
}
catch (e) {
// Invalid
return;
}
// Cached
if (cache[str]) return cache[str];
// Parse size into value and unit using units-css
var size = unitsCss.parse(parsedFont.size);
// TODO: dpi
// TODO: remaining unit conversion
switch (size.unit) {
case 'pt':
size.value /= .75;
break;
case 'in':
size.value *= 96;
break;
case 'mm':
size.value *= 96.0 / 25.4;
break;
case 'cm':
size.value *= 96.0 / 2.54;
break;
}
// Populate font object
var font = {
weight: parsedFont.weight,
style: parsedFont.style,
size: size.value,
unit: size.unit,
family: parsedFont.family[0]
};
return cache[str] = font;
};
/**
* Enable or disable image smoothing.
*
* @api public
*/
Context2d.prototype.__defineSetter__('imageSmoothingEnabled', function(val){
this._imageSmoothing = !! val;
this.patternQuality = val ? 'best' : 'fast';
});
/**
* Get image smoothing value.
*
* @api public
*/
Context2d.prototype.__defineGetter__('imageSmoothingEnabled', function(val){
return !! this._imageSmoothing;
});
/**
* Create a pattern from `Image` or `Canvas`.
*
* @param {Image|Canvas} image
* @param {String} repetition
* @return {CanvasPattern}
* @api public
*/
Context2d.prototype.createPattern = function(image, repetition){
// TODO Use repetition (currently always 'repeat')
return new CanvasPattern(image);
};
/**
* Create a linear gradient at the given point `(x0, y0)` and `(x1, y1)`.
*
* @param {Number} x0
* @param {Number} y0
* @param {Number} x1
* @param {Number} y1
* @return {CanvasGradient}
* @api public
*/
Context2d.prototype.createLinearGradient = function(x0, y0, x1, y1){
return new CanvasGradient(x0, y0, x1, y1);
};
/**
* Create a radial gradient at the given point `(x0, y0)` and `(x1, y1)`
* and radius `r0` and `r1`.
*
* @param {Number} x0
* @param {Number} y0
* @param {Number} r0
* @param {Number} x1
* @param {Number} y1
* @param {Number} r1
* @return {CanvasGradient}
* @api public
*/
Context2d.prototype.createRadialGradient = function(x0, y0, r0, x1, y1, r1){
return new CanvasGradient(x0, y0, r0, x1, y1, r1);
};
/**
* Reset transform matrix to identity, then apply the given args.
*
* @param {...}
* @api public
*/
Context2d.prototype.setTransform = function(){
this.resetTransform();
this.transform.apply(this, arguments);
};
/**
* Set the fill style with the given css color string.
*
* @api public
*/
Context2d.prototype.__defineSetter__('fillStyle', function(val){
if (!val) return;
if ('CanvasGradient' == val.constructor.name
|| 'CanvasPattern' == val.constructor.name) {
this.lastFillStyle = val;
this._setFillPattern(val);
} else if ('string' == typeof val) {
this._setFillColor(val);
}
});
/**
* Get previous fill style.
*
* @return {CanvasGradient|String}
* @api public
*/
Context2d.prototype.__defineGetter__('fillStyle', function(){
return this.lastFillStyle || this.fillColor;
});
/**
* Set the stroke style with the given css color string.
*
* @api public
*/
Context2d.prototype.__defineSetter__('strokeStyle', function(val){
if (!val) return;
if ('CanvasGradient' == val.constructor.name
|| 'CanvasPattern' == val.constructor.name) {
this.lastStrokeStyle = val;
this._setStrokePattern(val);
} else if ('string' == typeof val) {
this._setStrokeColor(val);
}
});
/**
* Get previous stroke style.
*
* @return {CanvasGradient|String}
* @api public
*/
Context2d.prototype.__defineGetter__('strokeStyle', function(){
return this.lastStrokeStyle || this.strokeColor;
});
/**
* Register `font` for usage.
*
* @param {Font} font
* @api public
*/
Context2d.prototype.addFont = function(font) {
this._fonts = this._fonts || {};
if (this._fonts[font.name]) return;
this._fonts[font.name] = font;
};
/**
* Set font.
*
* @see exports.parseFont()
* @api public
*/
Context2d.prototype.__defineSetter__('font', function(val){
if (!val) return;
if ('string' == typeof val) {
var font;
if (font = parseFont(val)) {
this.lastFontString = val;
var fonts = this._fonts;
if (fonts && fonts[font.family]) {
var fontObj = fonts[font.family];
var type = font.weight + '-' + font.style;
var fontFace = fontObj.getFace(type);
this._setFontFace(fontFace, font.size);
} else {
this._setFont(
font.weight
, font.style
, font.size
, font.unit
, font.family);
}
}
}
});
/**
* Get the current font.
*
* @api public
*/
Context2d.prototype.__defineGetter__('font', function(){
return this.lastFontString || '10px sans-serif';
});
/**
* Set text baseline.
*
* @api public
*/
Context2d.prototype.__defineSetter__('textBaseline', function(val){
if (!val) return;
var n = baselines.indexOf(val);
if (~n) {
this.lastBaseline = val;
this._setTextBaseline(n);
}
});
/**
* Get the current baseline setting.
*
* @api public
*/
Context2d.prototype.__defineGetter__('textBaseline', function(){
return this.lastBaseline || 'alphabetic';
});
/**
* Set text alignment.
*
* @api public
*/
Context2d.prototype.__defineSetter__('textAlign', function(val){
switch (val) {
case 'center':
this._setTextAlignment(0);
this.lastTextAlignment = val;
break;
case 'left':
case 'start':
this._setTextAlignment(-1);
this.lastTextAlignment = val;
break;
case 'right':
case 'end':
this._setTextAlignment(1);
this.lastTextAlignment = val;
break;
}
});
/**
* Get the current font.
*
* @see exports.parseFont()
* @api public
*/
Context2d.prototype.__defineGetter__('textAlign', function(){
return this.lastTextAlignment || 'start';
});
/**
* Create `ImageData` with the given dimensions or
* `ImageData` instance for dimensions.
*
* @param {Number|ImageData} width
* @param {Number} height
* @return {ImageData}
* @api public
*/
Context2d.prototype.createImageData = function(width, height){
if ('ImageData' == width.constructor.name) {
height = width.height;
width = width.width;
}
return new ImageData(new Uint8ClampedArray(width * height * 4), width, height);
};