* set the font family and size * @param {string} font - a CSS font name * @param {number|string} [size=10] - size in px, or size + suffix (px, em, pt) * @returns {Text} this object for chaining * @example * font.setFont("Arial", 20); * font.setFont("Arial", "1.5em");
(font, size = 10)
| 231 | * font.setFont("Arial", "1.5em"); |
| 232 | */ |
| 233 | setFont(font, size = 10) { |
| 234 | // font name and type |
| 235 | const font_names = font.split(",").map((value) => { |
| 236 | value = value.trim(); |
| 237 | return !/(^".*"$)|(^'.*'$)/.test(value) ? '"' + value + '"' : value; |
| 238 | }); |
| 239 | |
| 240 | // font size |
| 241 | if (typeof size === "number") { |
| 242 | this.fontSize = size; |
| 243 | size += "px"; |
| 244 | } /* string */ else { |
| 245 | // extract the units and convert if necessary |
| 246 | const CSSval = size.match(/([-+]?[\d.]*)(.*)/); |
| 247 | this.fontSize = parseFloat(CSSval[1]); |
| 248 | if (CSSval[2]) { |
| 249 | this.fontSize *= toPX[runits.indexOf(CSSval[2])]; |
| 250 | } else { |
| 251 | // no unit define, assume px |
| 252 | size += "px"; |
| 253 | } |
| 254 | } |
| 255 | this.height = this.fontSize; |
| 256 | this.font = size + " " + font_names.join(","); |
| 257 | |
| 258 | this.isDirty = true; |
| 259 | |
| 260 | return this; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * change the text to be displayed |
no test coverage detected