import { assert, release } from "../../base/utilities/Debug"; import { ASObject} from "../../avm2/nat/ASObject"; import { transformASValueToJS } from "../../avm2/nat/transformASValueToJS"; import { transformJSValueToAS } from "../../avm2/nat/transformJSValueToAS"; import { ASArray } from "../../avm2/nat/ASArray"; import { axCoerceString } from "../../avm2/run/axCoerceString"; import { EventDispatcher } from "../events/EventDispatcher"; import { TextFormat } from "./TextFormat"; /** * Copyright 2014 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Class: StyleSheet export interface Style { color?: string; display?: string; fontFamily?: string; fontSize?: any; // number | string fontStyle?: string; fontWeight?: string; kerning?: any; // number | string leading?: any; // number | string letterSpacing?: any; // number | string marginLeft?: any; // number | string marginRight?: any; // number | string textAlign?: string; textDecoration?: string; textIndent?: any; // number | string } export class StyleSheet extends EventDispatcher { static classInitializer: any = null; static classSymbols: string [] = null; static instanceSymbols: string [] = null; constructor () { super(); this.clear(); } private _rules: { [key: string]: Style; }; get styleNames(): ASArray { var styles = this._rules; var names = []; for (var name in styles) { if (styles[name]) { names.push(name); } } return this.sec.createArrayUnsafe(names); } getStyle(styleName: string): Style { styleName = axCoerceString(styleName); var style = this._rules[styleName.toLowerCase()]; if (!style) { return this.sec.createObject(); // note that documentation is lying about `null`; } return transformJSValueToAS(this.sec, style, false); } applyStyle(textFormat: TextFormat, styleName: string): TextFormat { styleName = axCoerceString(styleName); var style = this._rules[styleName.toLowerCase()]; if (style) { return textFormat.transform(style); } return textFormat; } setStyle(styleName: string, styleObject: Style) { if (typeof styleObject !== 'object') { return; } styleName = axCoerceString(styleName); this._rules[styleName.toLowerCase()] = transformASValueToJS(this.sec, styleObject, false); } hasStyle(styleName: string): boolean { return !!this._rules[styleName.toLowerCase()]; } clear() { this._rules = Object.create(null); } transform(formatObject: ASObject): TextFormat { if (typeof formatObject !== 'object') { return null; } formatObject = transformASValueToJS(this.sec, formatObject, false); var textFormat = new TextFormat(); textFormat.transform(