canvasimo
Version:
An HTML5 canvas drawing library, with 150+ useful methods, jQuery-like fluent interface, and cross-browser compatibility enhancements.
115 lines • 4.7 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var constants_1 = require("./constants");
var logger_1 = require("./logger");
exports.isPoint = function (point) {
return typeof point === 'object' && 'x' in point && 'y' in point;
};
exports.isTuplePoint = function (point) {
return typeof point === 'object' && Array.isArray(point) && point.length === 2;
};
var warnedAboutLineHeight = false;
exports.getFontParts = function (input, density, getter) {
if (!input) {
return constants_1.DEFAULT_FONT_PARTS;
}
var font = input.trim();
if (constants_1.MATCHES_SPECIAL_FONT.test(font)) {
return [font];
}
var matchFontSize = constants_1.MATCHES_FONT_SIZE.exec(font);
if (!matchFontSize) {
return constants_1.DEFAULT_FONT_PARTS;
}
var fontString = matchFontSize[0].trim();
var leadingSpaces = matchFontSize[1].length;
var size = matchFontSize[2];
var unit = matchFontSize[3];
var lineHeight = matchFontSize[4];
if (lineHeight && !warnedAboutLineHeight) {
logger_1.default.warn("Attempted to set the font line height with \"" + fontString + "\", " +
'but this is not supported by canvas. ' +
'Use the Canvasimo TextMultiline methods with the lineHeight parameter instead.');
warnedAboutLineHeight = true;
}
var fontSize = (unit !== '%' ? (getter ? parseFloat(size) / density : parseFloat(size) * density) : size) + unit;
var parts = font.substring(matchFontSize.index + leadingSpaces).split(constants_1.MATCHES_WHITESPACE);
var fontFamily = parts[parts.length - 1];
var optional = font.substring(0, matchFontSize.index);
var optionalParts = optional ? optional.split(constants_1.MATCHES_WHITESPACE) : null;
var fontStyle;
var fontVariant;
var fontWeight;
if (optionalParts) {
while (optionalParts.length) {
if (constants_1.MATCHES_FONT_STYLE.test(optionalParts[0])) {
fontStyle = optionalParts.splice(0, 1)[0];
}
else if (constants_1.MATCHES_FONT_VARIANT.test(optionalParts[0])) {
fontVariant = optionalParts.splice(0, 1)[0];
}
else if (constants_1.MATCHES_FONT_WEIGHT.test(optionalParts[0])) {
fontWeight = optionalParts.splice(0, 1)[0];
}
else if (constants_1.MATCHES_NORMAL.test(optionalParts[0])) {
optionalParts.splice(0, 1);
}
else {
return constants_1.DEFAULT_FONT_PARTS;
}
}
}
return [
fontStyle || constants_1.DEFAULT_FONT_PARTS[0],
fontVariant || constants_1.DEFAULT_FONT_PARTS[1],
fontWeight || constants_1.DEFAULT_FONT_PARTS[2],
fontSize,
fontFamily,
];
};
exports.formatFont = function (input, density, getter) {
return exports.getFontParts(input, density, getter).join(' ');
};
exports.forPoints = function (points, callback) {
if (!Array.isArray(points) || (typeof points[0] === 'number' && (points.length % 2) !== 0)) {
throw new Error(constants_1.INCORRECT_POINT_FORMAT);
}
if (!points.length || points.length === 1 || (typeof points[0] === 'number' && points.length < 4)) {
return;
}
var firstPoint = points[0];
var secondPoint = points[1];
if (exports.isPoint(firstPoint)) {
points.forEach(function (point, index) {
if (!exports.isPoint(point)) {
throw new Error("Expected point with format {x, y} but got " + point);
}
callback(point.x, point.y, index);
});
}
else if (exports.isTuplePoint(firstPoint)) {
points.forEach(function (point, index) {
if (!exports.isTuplePoint(point)) {
throw new Error("Expected point with format [x, y] but got " + point);
}
callback(point[0], point[1], index);
});
}
else if (typeof secondPoint === 'number') {
// tslint:disable-next-line:prefer-for-of
for (var i = 0; i < points.length; i += 2) {
var pointX = points[i];
var pointY = points[i + 1];
if (typeof pointX !== 'number' || typeof pointY !== 'number') {
throw new Error("Expected points to be an array of numbers but got " + pointX + ", " + pointY);
}
callback(pointX, pointY, i / 2);
}
}
else {
throw new Error(constants_1.INCORRECT_POINT_FORMAT);
}
};
exports.isFillRule = function (value) { return typeof value === 'string' &&
constants_1.MATCHES_SPECIAL_FILL.test(value); };
//# sourceMappingURL=utils.js.map
;