UNPKG

@toplast/generator

Version:

Collage generator for Toplast

73 lines (72 loc) 3.14 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Base = void 0; const canvas_1 = require("canvas"); const path_1 = __importDefault(require("path")); const sharp_1 = __importDefault(require("sharp")); const ASSETS_SRC = path_1.default.resolve(__dirname, "..", "..", "assets"); class Base { constructor({ width, height }) { this.PREFIX = "data:image/png;base64,"; console.log(ASSETS_SRC); this.registerFonts(); this.canvas = (0, canvas_1.createCanvas)(width, height); this.context = this.canvas.getContext("2d"); } getImage() { return __awaiter(this, void 0, void 0, function* () { yield this.generateCanvas(); return this.toBase64(); }); } generateCanvas() { return __awaiter(this, void 0, void 0, function* () { throw new Error("Missing theme generate function"); }); } toBase64() { return __awaiter(this, void 0, void 0, function* () { const sharpBuffer = yield (0, sharp_1.default)(this.canvas.toBuffer()).png().toBuffer(); return this.PREFIX + sharpBuffer.toString("base64"); }); } addScalableText(text, xAxis, yAxis, maxWidth, style, startSize) { return __awaiter(this, void 0, void 0, function* () { this.context.font = style.replace("%S", startSize.toString()); let width = this.context.measureText(text).width; let size = startSize; while (width > maxWidth) { size--; this.context.font = style.replace("%S", size.toString()); width = this.context.measureText(text).width; if (size === 2) break; } this.context.fillText(text, xAxis, yAxis); }); } registerFonts() { (0, canvas_1.registerFont)(ASSETS_SRC + "/fonts/RobotoCondensed-Bold.ttf", { family: "RobotoCondensed-Bold", }); (0, canvas_1.registerFont)(ASSETS_SRC + "/fonts/RobotoCondensed-Light.ttf", { family: "RobotoCondensed-Light", }); (0, canvas_1.registerFont)(ASSETS_SRC + "/fonts/RobotoCondensed-Regular.ttf", { family: "RobotoCondensed", }); } } exports.Base = Base;