UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

313 lines (312 loc) 12.7 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Frame = void 0; const events_1 = require("events"); class Frame extends events_1.EventEmitter { constructor(opt) { var _a, _b, _c, _d, _e, _f, _g; super(); this.text = ''; this.align = 'A_LEFT'; this.textType = 'TFM_OVERFLOW'; this.children = new Array(); this.enabled = (_a = opt.enabled) !== null && _a !== void 0 ? _a : true; this.posX = opt.x; this.posY = opt.y; this.width = opt.width; this.height = opt.height; this.setText((_b = opt.text) !== null && _b !== void 0 ? _b : '', 'A_LEFT'); this.fontColor = (_c = opt.fontColor) !== null && _c !== void 0 ? _c : [1.0, 1.0, 1.0]; this.fontName = opt.font; this.bgColor = opt.bgColor; this.bgImageName = opt.bgImage; this.bgType = opt.bgType; this.borderRadius = (_d = opt.borderRadius) !== null && _d !== void 0 ? _d : 0; this.borderWidth = (_e = opt.borderWidth) !== null && _e !== void 0 ? _e : 0; this.borderColor = (_f = opt.borderColor) !== null && _f !== void 0 ? _f : [1, 1, 1, 1]; this.customDraw = opt.customDraw; this.layer = (_g = opt.layer) !== null && _g !== void 0 ? _g : 0; } enable() { this.enabled = true; } disable() { this.enabled = false; } setFramePosition(x, y) { this.posX = x; this.posY = y; } setFrameSize(width, height) { this.width = width; this.height = height; } setText(text, align, textType = 'TFM_OVERFLOW', fontColor) { this.text = text; this.align = align; this.textType = textType; if (fontColor) { this.fontColor = fontColor; } } setFontColor(fontColor) { this.fontColor = fontColor; } setFont(fontName) { this.fontName = fontName; this.font = undefined; } setFontData(fontData) { this.fontName = undefined; this.font = fontData; } setBgColor(color) { this.bgColor = color; } setBgImage(imageName, type = 'fit') { this.bgImageName = imageName; this.bgImage = undefined; this.bgType = type; } setBgImageData(imageData, type = 'fit') { this.bgImageName = undefined; this.bgImage = imageData; this.bgType = type; } setBgType(type) { this.bgType = type; } setBorderRadius(radius) { this.borderRadius = radius; } setBorderWidth(width) { this.borderWidth = width; } setBorderColor(color) { this.borderColor = color; } setCustomDraw(customDraw) { this.customDraw = customDraw; } resetFont() { this.font = undefined; } resetBgColor() { this.bgColor = undefined; } resetBgImage() { this.bgImage = undefined; this.bgType = undefined; } resetCustomDraw() { this.customDraw = undefined; } clear() { this.text = ''; this.align = 'A_LEFT'; this.textType = 'TFM_OVERFLOW'; this.fontColor = [1.0, 1.0, 1.0]; this.font = undefined; this.bgColor = undefined; this.bgImage = undefined; this.bgType = undefined; this.customDraw = undefined; } insert(...frames) { this.children.push(...frames); for (const frame of frames) { frame.on('layoutChanged', () => this.layoutChanged()); } this.layoutChanged(); } getLayers() { const uniqueLayers = new Set(); uniqueLayers.add(this.layer); for (const child of this.children) { for (const layer of child.getLayers()) { uniqueLayers.add(layer); } } return uniqueLayers; } layoutChanged() { this.emit('layoutChanged'); } displayImage(cod, resourceManager, cairo, ppX, ppY, scale, layer) { return __awaiter(this, void 0, void 0, function* () { if (this.enabled) { ppX += this.posX; ppY += this.posY; yield this.prepareResources(resourceManager); yield cod.cairo('cairo_save', cairo); yield this.clipDrawing(cod, cairo, scale, ppX, ppY); if (this.layer === layer) { yield this.displayOwnImage(cod, cairo, ppX, ppY, scale); } for (const child of this.children) { yield child.displayImage(cod, resourceManager, cairo, ppX, ppY, scale, layer); } yield cod.cairo('cairo_restore', cairo); } }); } prepareResources(resourceManager) { return __awaiter(this, void 0, void 0, function* () { if (this.bgImageName !== undefined) { this.bgImage = yield resourceManager.image(this.bgImageName); } if (this.fontName !== undefined) { this.font = yield resourceManager.font(this.fontName); } }); } displayOwnImage(cod, cairo, ppX, ppY, scale) { return __awaiter(this, void 0, void 0, function* () { if (!this.enabled) { return; } const promises = new Array(); if (this.font !== undefined) { promises.push(cod.cairo('cairo_set_font_face', cairo, this.font.var)); } else { promises.push(cod.cairo('cairo_set_font_face', cairo, 'NULL')); } if (this.bgColor !== undefined) { promises.push(this.drawFrame(cod, cairo, scale, ppX, ppY)); } if (this.bgImage !== undefined) { promises.push(this.drawImage(cod, cairo, scale, ppX, ppY)); } if (this.borderWidth > 0) { promises.push(this.drawBorder(cod, cairo, scale, ppX, ppY)); } if (this.text) { promises.push(this.drawText(cod, cairo, scale, ppX, ppY)); } if (this.customDraw) { promises.push(cod.cairo('cairo_identity_matrix', cairo)); promises.push(cod.cairo('cairo_translate', cairo, scale * ppX, scale * ppY)); promises.push(cod.cairo('cairo_scale', cairo, scale, scale)); promises.push(this.customDraw(cod, cairo, { width: this.width, height: this.height })); } yield Promise.all(promises); }); } clipDrawing(cod, cairo, scale, ppX, ppY) { return __awaiter(this, void 0, void 0, function* () { if (this.borderRadius === 0) { return; } yield Promise.all([ this.drawRectPath(cod, cairo, scale, ppX, ppY, this.borderRadius), cod.cairo('cairo_clip', cairo), ]); }); } drawFrame(cod, cairo, scale, ppX, ppY) { return __awaiter(this, void 0, void 0, function* () { if (this.bgColor) { const promises = [ cod.cairo('cairo_identity_matrix', cairo), cod.cairo('cairo_translate', cairo, scale * ppX, scale * ppY), cod.cairo('cairo_scale', cairo, scale, scale), cod.cairo('cairo_set_source_rgba', cairo, this.bgColor[0], this.bgColor[1], this.bgColor[2], this.bgColor[3]), cod.cairo('cairo_rectangle', cairo, 0, 0, this.width, this.height), cod.cairo('cairo_fill', cairo), cod.cairo('cairo_stroke', cairo), ]; yield Promise.all(promises); } else { throw new Error('Color of the frame is undefined.'); } }); } drawImage(cod, cairo, scale, ppX, ppY) { return __awaiter(this, void 0, void 0, function* () { if (this.bgImage === undefined) { return; } const bgImage = this.bgImage.var; const bgWidth = this.bgImage.width; const bgHeight = this.bgImage.height; const promises = new Array(); promises.push(cod.cairo('cairo_identity_matrix', cairo)); promises.push(cod.cairo('cairo_translate', cairo, scale * ppX, scale * ppY)); if (this.bgType === 'fill') { const sx = (scale * this.width) / bgWidth; const sy = (scale * this.height) / bgHeight; promises.push(cod.cairo('cairo_scale', cairo, sx, sy)); } else if (this.bgType === 'fit') { const sx = this.width / bgWidth; const sy = this.height / bgHeight; const scaleRatio = scale * Math.min(sx, sy); promises.push(cod.cairo('cairo_scale', cairo, scaleRatio, scaleRatio)); } else { promises.push(cod.cairo('cairo_scale', cairo, scale, scale)); } promises.push(cod.cairo('cairo_set_source_surface', cairo, bgImage, 0, 0)); promises.push(cod.cairo('cairo_paint', cairo)); yield Promise.all(promises); }); } drawText(cod, cairo, scale, ppX, ppY) { return __awaiter(this, void 0, void 0, function* () { const promises = [ cod.cairo('cairo_identity_matrix', cairo), cod.cairo('cairo_set_source_rgb', cairo, this.fontColor[0], this.fontColor[1], this.fontColor[2]), cod.writeText(cairo, '' + this.text, Math.floor(scale * ppX), Math.floor(scale * ppY), Math.floor(scale * this.width), Math.floor(scale * this.height), this.align, this.textType), ]; yield Promise.all(promises); }); } drawBorder(cod, cairo, scale, ppX, ppY) { return __awaiter(this, void 0, void 0, function* () { yield Promise.all([ this.drawRectPath(cod, cairo, scale, ppX, ppY, this.borderRadius), cod.cairo('cairo_set_source_rgba', cairo, this.borderColor[0], this.borderColor[1], this.borderColor[2], this.borderColor[3]), cod.cairo('cairo_set_line_width', cairo, this.borderWidth), cod.cairo('cairo_stroke', cairo), ]); }); } drawRectPath(cod, cairo, scale, ppX, ppY, radius) { return __awaiter(this, void 0, void 0, function* () { if (radius === 0) { return yield Promise.all([ cod.cairo('cairo_identity_matrix', cairo), cod.cairo('cairo_translate', cairo, scale * ppX, scale * ppY), cod.cairo('cairo_scale', cairo, scale, scale), cod.cairo('cairo_rectangle', cairo, 0, 0, this.width, this.height), ]); } else { const degrees = Math.PI / 180; return yield Promise.all([ cod.cairo('cairo_identity_matrix', cairo), cod.cairo('cairo_translate', cairo, scale * ppX, scale * ppY), cod.cairo('cairo_scale', cairo, scale, scale), cod.cairo('cairo_new_sub_path', cairo), cod.cairo('cairo_arc', cairo, this.width - radius, radius, radius, -90 * degrees, 0 * degrees), cod.cairo('cairo_arc', cairo, this.width - radius, this.height - radius, radius, 0 * degrees, 90 * degrees), cod.cairo('cairo_arc', cairo, radius, this.height - radius, radius, 90 * degrees, 180 * degrees), cod.cairo('cairo_arc', cairo, radius, radius, radius, 180 * degrees, 270 * degrees), cod.cairo('cairo_close_path', cairo), ]); } }); } } exports.Frame = Frame;