From 8ef4d9a7db3fa5bdca6bf30570620bb2c70be809 Mon Sep 17 00:00:00 2001 From: Rand Scullard Date: Thu, 2 Jun 2016 09:05:18 -0400 Subject: [PATCH] Add definitions for fontfaceobserver (#9453) --- fontfaceobserver/fontfaceobserver-tests.ts | 46 +++++++++++++++++++ .../fontfaceobserver-tests.ts.tscparams | 1 + fontfaceobserver/fontfaceobserver.d.ts | 33 +++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 fontfaceobserver/fontfaceobserver-tests.ts create mode 100644 fontfaceobserver/fontfaceobserver-tests.ts.tscparams create mode 100644 fontfaceobserver/fontfaceobserver.d.ts diff --git a/fontfaceobserver/fontfaceobserver-tests.ts b/fontfaceobserver/fontfaceobserver-tests.ts new file mode 100644 index 0000000000..1ea9a7a222 --- /dev/null +++ b/fontfaceobserver/fontfaceobserver-tests.ts @@ -0,0 +1,46 @@ +/// + +function test1() { + var font = new FontFaceObserver('My Family', { + weight: 400 + }); + + font.load().then(function () { + console.log('Font is available'); + }, function () { + console.log('Font is not available'); + }); +} + +function test2() { + var font = new FontFaceObserver('My Family'); + + font.load('中国').then(function () { + console.log('Font is available'); + }, function () { + console.log('Font is not available'); + }); +} + +function test3() { + var font = new FontFaceObserver('My Family'); + + font.load(null, 5000).then(function () { + console.log('Font is available'); + }, function () { + console.log('Font is not available after waiting 5 seconds'); + }); +} + +function test4() { + var fontA = new FontFaceObserver('Family A'); + var fontB = new FontFaceObserver('Family B'); + + fontA.load().then(function () { + console.log('Family A is available'); + }); + + fontB.load().then(function () { + console.log('Family B is available'); + }); +} diff --git a/fontfaceobserver/fontfaceobserver-tests.ts.tscparams b/fontfaceobserver/fontfaceobserver-tests.ts.tscparams new file mode 100644 index 0000000000..14fce22a5c --- /dev/null +++ b/fontfaceobserver/fontfaceobserver-tests.ts.tscparams @@ -0,0 +1 @@ +--target ES6 diff --git a/fontfaceobserver/fontfaceobserver.d.ts b/fontfaceobserver/fontfaceobserver.d.ts new file mode 100644 index 0000000000..8f461dbd4f --- /dev/null +++ b/fontfaceobserver/fontfaceobserver.d.ts @@ -0,0 +1,33 @@ +// Type definitions for fontfaceobserver +// Project: https://github.com/bramstein/fontfaceobserver +// Definitions by: Rand Scullard +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + + +declare namespace FontFaceObserver { + interface FontVariant { + weight?: number | string; + style?: string; + stretch?: string; + } +} + +declare class FontFaceObserver { + /** + * Creates a new FontFaceObserver. + * @param fontFamilyName Name of the font family to observe. + * @param variant Description of the font variant to observe. If a property is not present it will default to normal. + */ + constructor(fontFamilyName: string, variant?: FontFaceObserver.FontVariant); + + /** + * Starts observing the loading of the specified font. Immediately returns a new Promise that resolves when the font is available and rejected when the font is not available. + * @param testString If your font doesn't contain latin characters you can pass a custom test string. + * @param timeout The default timeout for giving up on font loading is 3 seconds. You can increase or decrease this by passing a number of milliseconds. + */ + load(testString?: string, timeout?: number): Promise; +} + +declare module "fontfaceobserver" { + export = FontFaceObserver; +}