tav-media
Version:
Cross platform media editing framework
83 lines (82 loc) • 3.65 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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 PAGFont_1;
import { readFile } from './utils/common';
import { defaultFontNames } from './utils/font-family';
import { wasmAwaitRewind, wasmAsyncMethod, destroyVerify } from './utils/decorators';
import { PAGModule } from './pag-module';
let PAGFont = PAGFont_1 = class PAGFont {
constructor(wasmIns) {
this.isDestroyed = false;
this.wasmIns = wasmIns;
this.fontFamily = this.wasmIns.fontFamily;
this.fontStyle = this.wasmIns.fontStyle;
}
/**
* Create PAGFont instance.
*/
static create(fontFamily, fontStyle) {
const wasmIns = PAGModule._PAGFont._create(fontFamily, fontStyle);
if (!wasmIns)
throw new Error('Create PAGFont fail!');
return new PAGFont_1(wasmIns);
}
/**
* Register custom font family in the browser.
*/
static registerFont(family, data) {
return __awaiter(this, void 0, void 0, function* () {
const buffer = (yield readFile(data));
if (!buffer || !(buffer.byteLength > 0)) {
throw new Error('Initialize PAGFont data not be empty!');
}
const dataUint8Array = new Uint8Array(buffer);
const fontFace = new FontFace(family, dataUint8Array);
document.fonts.add(fontFace);
yield fontFace.load();
});
}
/**
* Register fallback font names from pag.
*/
static registerFallbackFontNames(fontNames = []) {
/**
* Cannot determine whether a certain word exists in the font on the web environment.
* Because the canvas has font fallback when drawing.
* The fonts registered here are mainly used to put words in a list in order, and the list can put up to UINT16_MAX words.
* The emoji font family also has emoji words.
*/
const vectorNames = new PAGModule.VectorString();
const names = fontNames.concat(defaultFontNames);
for (const name of names) {
vectorNames.push_back(name);
}
PAGModule._PAGFont._SetFallbackFontNames(vectorNames);
vectorNames.delete();
}
destroy() {
this.wasmIns.delete();
this.isDestroyed = true;
}
};
__decorate([
wasmAsyncMethod
], PAGFont, "registerFont", null);
PAGFont = PAGFont_1 = __decorate([
destroyVerify,
wasmAwaitRewind
], PAGFont);
export { PAGFont };