Skip to content

Commit 269b86b

Browse files
Merge pull request #2 from nextml-code/support_font
feature: support text+font
2 parents 108ba17 + 70fb20a commit 269b86b

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const context = far.far(canvas, {y: -farAway, scale: 2}).getContext("2d");
7070

7171
## development
7272

73-
### run exampl
73+
### run example
7474

7575
```bash
7676
npm run example

example/example.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ image.data.onload = function () {
103103
ctx.stroke();
104104
ctx.restore();
105105

106+
ctx.save();
107+
ctx.fillStyle = "#F08";
108+
ctx.fillText("example", rectangle.x, rectangle.y + 10);
109+
ctx.font = "bold 48px serif";
110+
ctx.strokeStyle = "#0F8";
111+
ctx.strokeText("far", rectangle.x, rectangle.y + 48);
112+
ctx.restore();
113+
106114
ctx.restore();
107115
});
108116
}

src/index.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
2323
};
2424

2525
_context.lineWidth = s.distance(_context.lineWidth);
26-
// FIXME text default size
26+
_context.font = `${s.distance(10)}px sans-serif`;
2727

2828
const notSupported = (name) => {
2929
throw new Error(`${name} not supported`);
@@ -32,9 +32,8 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
3232
throw new Error(`${name} not implemented yet`);
3333
};
3434

35-
/* FIXME
36-
- font
37-
- all text stuff
35+
/* TODO
36+
- measureText
3837
- Path2d, farContext.Path2D ?
3938
*/
4039

@@ -67,10 +66,32 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
6766
notImplementedYet("filter");
6867
},
6968
get font() {
70-
notImplementedYet("font");
69+
const font_ = _context.font.split(" ").filter((a) => a.trim());
70+
71+
if (![2, 3].includes(font_.length)) {
72+
notSupported("font(!'[<style> ]<size> <face>')");
73+
} else {
74+
const [style, size, face] = font_.length == 3 ? font_ : ["", ...font_];
75+
76+
const sizeValue = parseFloat(size.match(/[0-9\.]/g).join(""));
77+
const sizeUnit = size.match(/[A-Za-z]/g).join("");
78+
79+
return `${style} ${s.inv.distance(sizeValue)}${sizeUnit} ${face}`;
80+
}
7181
},
7282
set font(font) {
73-
notImplementedYet("font");
83+
const font_ = font.split(" ").filter((a) => a.trim());
84+
85+
if (![2, 3].includes(font_.length)) {
86+
notSupported("font(!'[<style> ]<size> <face>')");
87+
} else {
88+
const [style, size, face] = font_.length == 3 ? font_ : ["", ...font_];
89+
90+
const sizeValue = parseFloat(size.match(/[0-9\.]/g).join(""));
91+
const sizeUnit = size.match(/[A-Za-z]/g).join("");
92+
93+
_context.font = `${style} ${s.distance(sizeValue)}${sizeUnit} ${face}`;
94+
}
7495
},
7596
get fontKerning() {
7697
return _context.fontKerning;
@@ -228,7 +249,7 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
228249
if (args.length === 0) {
229250
return _context.clip();
230251
} else if (typeof args[0] === "object") {
231-
// FIXME Path2D
252+
// TODO Path2D
232253
notImplementedYet("clip(Path2D, .)");
233254
} else {
234255
return _context.clip(...args);
@@ -316,7 +337,7 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
316337
if (args.length === 0) {
317338
return _context.fill();
318339
} else if (typeof args[0] === "object") {
319-
// FIXME Path2D
340+
// TODO Path2D
320341
notImplementedYet("fill(Path2D, .)");
321342
} else {
322343
return _context.fill(...args);
@@ -330,8 +351,13 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
330351
s.distance(height)
331352
);
332353
},
333-
fillText(text, x, y, maxWidth) {
334-
return _context.fillText(text, s.x(x), s.y(y), s.distance(maxWidth));
354+
fillText(text, x, y, maxWidth = undefined) {
355+
return _context.fillText(
356+
text,
357+
s.x(x),
358+
s.y(y),
359+
isDefined(maxWidth) ? s.distance(maxWidth) : undefined
360+
);
335361
},
336362
getContextAttributes() {
337363
return _context.getContextAttributes();
@@ -420,8 +446,13 @@ const getFarContext2d = (canvas, { x = 0, y = 0, scale = 1 } = {}) => {
420446
s.distance(height)
421447
);
422448
},
423-
strokeText(text, x, y, maxWidth) {
424-
return _context.strokeText(text, s.x(x), s.y(y), s.distance(maxWidth));
449+
strokeText(text, x, y, maxWidth = undefined) {
450+
return _context.strokeText(
451+
text,
452+
s.x(x),
453+
s.y(y),
454+
isDefined(maxWidth) ? s.distance(maxWidth) : undefined
455+
);
425456
},
426457
transform(a, b, c, d, e, f) {
427458
notSupported("transform");

0 commit comments

Comments
 (0)