Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit 8ea29da

Browse files
committed
Add basic text method for debug
1 parent d3f2f84 commit 8ea29da

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

src/mixins/text.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,69 @@ export default {
88
return (this._lineGap = 0);
99
},
1010

11+
text(text, x, y, options) {
12+
options = this._initOptions(x, y, options);
13+
14+
// if the wordSpacing option is specified, remove multiple consecutive spaces
15+
if(options.wordSpacing) {
16+
text = text.replace(/\s{2,}/g, ' ')
17+
}
18+
19+
// render paragraphs as single lines
20+
const lines = text.split('\n');
21+
22+
for (var i = 0; i < lines.length; i++) {
23+
this._line(lines[i], options);
24+
}
25+
26+
return this
27+
},
28+
29+
_initOptions(x = {}, y, options = {}) {
30+
if(typeof x === 'object') {
31+
options = x
32+
x = null
33+
}
34+
35+
// Update the current position
36+
if(x) this.x = x;
37+
if(y) this.y = y;
38+
39+
40+
options.columns = options.columns || 0
41+
options.columnGap = options.columnGap || 18 // 1/4 inch
42+
43+
return options
44+
},
45+
46+
_line(text, options = {}, wrapper) {
47+
this._fragment(text, this.x, this.y, options)
48+
},
49+
50+
51+
_fragment(text, x, y, options) {
52+
text = ('' + text).replace(/\n/g, '')
53+
54+
if (text.length === 0) return
55+
56+
// add current font to page if necessary
57+
if (this.page.fonts[this._font.id] == null) {
58+
this.page.fonts[this._font.id] = this._font.ref();
59+
}
60+
61+
// Glyph encoding and positioning
62+
const [encoded, positions] = this._font.encode(text, options.features);
63+
64+
// Pass down spacings to _glyphs method
65+
options.wordSpacing = options.wordSpacing || 0;
66+
options.characterSpacing = options.characterSpacing || 0;
67+
68+
// Adjust y to match coordinate flipping
69+
y = this.page.height - y - ((this._font.ascender / 1000) * this._fontSize);
70+
71+
this._glyphs(encoded, positions, x, y, options)
72+
},
73+
1174
_addGlyphs(glyphs, positions, x, y, options) {
1275
// add current font to page if necessary
1376
if (options == null) {

0 commit comments

Comments
 (0)