import {IAsset, AssetLibrary, URLLoaderDataFormat, ParserBase, ParserUtils, ResourceDependency} from "@awayjs/core"; import {Font, TesselatedFontTable, FontStyleName} from "@awayjs/scene"; import * as opentype from "opentype.js" /** * FontParser should parse Fonts into TesselatedFontTable for usage with webGL, or just load the Font as css class for usage with canvas and no webGL */ export class FontParser extends ParserBase { private _useWebGL:boolean=false; /** * Creates a new TextureAtlasParser object. * @param uri The url or id of the data or file to be parsed. * @param extra The holder for extra contextual data that the parser might need. */ constructor(useWebGL:boolean=true) { super(URLLoaderDataFormat.ARRAY_BUFFER); this._useWebGL=useWebGL; } /** * Indicates whether or not a given file extension is supported by the parser. * @param extension The file extension of a potential file to be parsed. * @return Whether or not the given file type is supported. */ public static supportsType(extension:string):boolean { extension = extension.toLowerCase(); var supports:boolean = ((extension == "ttf")||(extension == "otf")); if(supports){ console.log("FontParse encountered file with supported extension: = " + extension); } return ((extension == "ttf")||(extension == "otf")); } /** * Tests whether a data block can be parsed by the parser. * @param data The data block to potentially be parsed. * @return Whether or not the given data is supported. */ public static supportsData(data:any):boolean { console.log("ParserFont = "+ParserUtils.toString(data, 20)); try { /* var content:string = ParserUtils.toString(data); if(content.indexOf("font") != -1 || content.indexOf("Font") != -1){ console.log("supportsData fnt"); return true; } */ return true; } catch (e) { return false; } } /** * @inheritDoc */ public _iResolveDependency(resourceDependency:ResourceDependency) { } /** * @inheritDoc */ public _iResolveDependencyFailure(resourceDependency:ResourceDependency) { } private sortKeys(dict) { var keys = []; for (var key in dict) { keys.push(key); } keys.sort(); return keys; } /** * @inheritDoc */ public _pProceedParsing():boolean { //console.log("proceed parsing = "+this._iFileName); //opentype=window["opentype"]; if(opentype){ //console.log("parsing font = "+this._iFileName+" / bytelength = "+this._pGetByteData().getBytesAvailable()); var font_name:string=""; var font_style_name:string=""; var font = opentype.parse(this.data); var tablename, table, property; /* */ for (tablename in font.tables) { table = font.tables[tablename]; if (tablename == 'name') { var properties = this.sortKeys(table); for (var i = 0; i < properties.length; i++) { var property = properties[i]; var translations = table[property]; var langs = this.sortKeys(translations); for (var j = 0; j < langs.length; j++) { var lang = langs[j]; if(property=="fontFamily"){ font_name=translations[lang]; } else if(property=="fontSubfamily"){ font_style_name=translations[lang]; } console.log(" "+property+": "+lang+" : "+translations[lang]); } } } } if(font_name==""){ console.log("FontParser.ts '"+this._iFileName+"': Could not read fontname !!!") } if(font_style_name==""){ console.log("FontParser.ts '"+this._iFileName+"': Could not read font_style_name !!!") } if(font_style_name=="Regular"){ font_style_name=FontStyleName.STANDART; } if(font_style_name=="Normal"){ font_style_name=FontStyleName.STANDART; } var new_font:Font=AssetLibrary.getAsset(font_name); var newfont:Boolean = false; if(new_font==undefined){ new_font = new Font(); newfont=true; } new_font.name=font_name; var new_font_style:TesselatedFontTable = new_font.get_font_table(font_style_name, TesselatedFontTable.assetType, font); /* for(var i=0; inew_font, new_font.name); return ParserBase.PARSING_DONE; } }