Skip to content

Commit ed55ad1

Browse files
committed
Better addResponsiveText
1 parent eac1dbd commit ed55ad1

File tree

3 files changed

+12
-50
lines changed

3 files changed

+12
-50
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## 0.2.0
8+
### Changed
9+
- `addResponsiveText()` changed the parser to be passive, faster, and more accurate.
10+
11+
### Removed
12+
- Two private methods that were being used in `setTextFont()`.
13+
714
## 0.1.7
815
### Added
916
- `changeCanvasSize()` to change the canvas' width/height.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "canvas-constructor",
3-
"version": "0.1.7",
3+
"version": "0.2.0",
44
"description": "A ES6 class for node-canvas with built-in functions and chained methods.",
55
"main": "index.js",
66
"scripts": {

src/canvas.js

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ class CanvasConstructor {
88

99
this.width = width;
1010
this.height = height;
11-
12-
this.font = {
13-
style: '',
14-
size: 21.33,
15-
font: ''
16-
};
1711
}
1812

1913
/**
@@ -204,12 +198,12 @@ class CanvasConstructor {
204198
* .toBuffer();
205199
*/
206200
addResponsiveText(text, x, y, maxWidth) {
207-
const { style = '', size, font } = this.font;
208-
if (isNaN(size)) throw new TypeError('The parameter size must be a valid number.');
201+
const [, style = '', size, font] = /(\w+ )?(\d+)(.+)/.exec(this.context.font);
202+
const currentSize = parseInt(size);
209203
const { width } = this.measureText(text);
210-
const newLength = maxWidth > width ? size : (maxWidth / width) * size;
204+
const newLength = maxWidth > width ? currentSize : (maxWidth / width) * currentSize;
211205
return this
212-
.setTextFont(`${style}${newLength}px ${font}`)
206+
.setTextFont(style + newLength + font)
213207
.addText(text, x, y);
214208
}
215209

@@ -771,45 +765,6 @@ class CanvasConstructor {
771765
return this;
772766
}
773767

774-
/**
775-
* Parses the font.
776-
* @param {string} string A string.
777-
* @returns {void}
778-
* @private
779-
*/
780-
_parseFontString(string) {
781-
const data = /([^\d]+)?([\d\w]+) (.+)?/.exec(string);
782-
if (data === null) return;
783-
this.font.style = data[1] || '';
784-
this.font.size = this._parseFontSize(data[2]);
785-
this.font.font = data[3] || '';
786-
}
787-
788-
/**
789-
* Parses the font's size
790-
* @param {string} string The string with a number and a unit.
791-
* @returns {number}
792-
* @private
793-
*/
794-
_parseFontSize(string) {
795-
const data = /(\d+)(\w+)/.exec(string);
796-
if (data === null) return 21.33;
797-
let size = parseFloat(data[1]);
798-
const unit = data[2];
799-
switch (unit) {
800-
case 'pt': size /= 0.75; break;
801-
case 'pc': size *= 16; break;
802-
case 'in': size *= 96; break;
803-
case 'cm': size *= 96.0 / 2.54; break;
804-
case 'mm': size *= 96.0 / 25.4; break;
805-
case 'em': size /= 0.75; break;
806-
case 'rem': size *= 21.33 / 0.75; break;
807-
case 'q': size *= 96 / 25.4 / 4; break;
808-
}
809-
810-
return size;
811-
}
812-
813768
}
814769

815770
module.exports = CanvasConstructor;

0 commit comments

Comments
 (0)