From 06fe69973a261c4104c027c8b22bb7445cdda285 Mon Sep 17 00:00:00 2001 From: Olegs Jeremejevs Date: Mon, 23 Jul 2018 22:21:30 +0300 Subject: [PATCH] bdfjs: Initial commit (#27507) --- types/bdfjs/bdfjs-tests.ts | 18 +++++++ types/bdfjs/index.d.ts | 108 +++++++++++++++++++++++++++++++++++++ types/bdfjs/tsconfig.json | 23 ++++++++ types/bdfjs/tslint.json | 1 + 4 files changed, 150 insertions(+) create mode 100644 types/bdfjs/bdfjs-tests.ts create mode 100644 types/bdfjs/index.d.ts create mode 100644 types/bdfjs/tsconfig.json create mode 100644 types/bdfjs/tslint.json diff --git a/types/bdfjs/bdfjs-tests.ts b/types/bdfjs/bdfjs-tests.ts new file mode 100644 index 0000000000..1f20259018 --- /dev/null +++ b/types/bdfjs/bdfjs-tests.ts @@ -0,0 +1,18 @@ +import { parse, draw, trim, Font, Bitmap } from "bdfjs"; + +parse(); // $ExpectError +parse(123); // $ExpectError +parse('abc'); // $ExpectType Font +parse('abc', { allprops: true, onlymeta: true }); // $ExpectType Font +const font: Font = parse(Buffer.from([1, 2, 3])); + +draw(); // $ExpectError +draw(123); // $ExpectError +draw(font); // $ExpectError +draw(font, 123); // $ExpectError +draw(font, 'abc', { kerningBias: 123 }); // $ExpectType Bitmap | undefined +const bitmap: Bitmap = draw(font, 'abc')!; + +trim(); // $ExpectError +trim(123); // $ExpectError +trim(bitmap); // $ExpectType Bitmap diff --git a/types/bdfjs/index.d.ts b/types/bdfjs/index.d.ts new file mode 100644 index 0000000000..c9ac5eaedd --- /dev/null +++ b/types/bdfjs/index.d.ts @@ -0,0 +1,108 @@ +// Type definitions for bdfjs 0.0 +// Project: https://github.com/zswang/bdfjs +// Definitions by: Olegs Jeremejevs +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +// https://www.x.org/docs/BDF/bdf.pdf +// https://www.adobe.com/content/dam/acom/en/devnet/font/pdfs/5005.BDF_Spec.pdf + +export interface ParseOptions { + allprops?: boolean; + onlymeta?: boolean; +} + +export interface BoundingBox { + width: number; + height: number; + x: number; + y: number; +} + +export interface Glyph { + bitmap: number[][]; + boundingBox: BoundingBox; + bytes: number[]; + char: string; + code: number; + deviceWidthX: number; + deviceWidthY: number; + name: string; + scalableWidthX: number; + scalableWidthY: number; +} + +export interface Properties { + defaultChar: number; + fontAscent: number; + fontDescent: number; + + addStyleName?: string; + averageWidth?: number; + capHeight?: number; + charsetEncoding?: string; + charsetRegistry?: string; + copyright?: string; + faceName?: string; + familyName?: string; + font?: string; + fontnameRegistry?: string; + fontVersion?: string; + foundry?: string; + notice?: string; + pixelSize?: number; + pointSize?: number; + resolutionX?: number; + resolutionY?: number; + setwidthName?: string; + slant?: string; + spacing?: string; + weightName?: string; + xHeight?: number; + + [key: string]: string | number | undefined; +} + +export interface Size { + points: number; + resolutionX: number; + resolutionY: number; +} + +export interface Meta { + boundingBox: BoundingBox; + name: string; + // The spec says that `properties` is optional, but then goes on to point out + // that without `defaultChar`, `fontAscent` and `fontDescent` it's not really + // a valid font, so I chose to not mark them as a maybe. + properties: Properties; + size: Size; + totalChars: number; + version: string; +} + +export interface Font { + meta: Meta; + glyphs: { [key: number]: Glyph }; // This is NOT a list +} + +export function parse(text: string | Buffer, options?: ParseOptions): Font; + +export interface DrawOptions { + kerningBias?: number; +} + +export interface Bitmap { + width: number; + height: number; + [row: number]: number[]; // Yep, this how the library does it, unfortunately +} + +export function draw( + font: Font, + text: string, + options?: DrawOptions +): Bitmap | undefined; + +export function trim(bitmap: Bitmap): Bitmap; diff --git a/types/bdfjs/tsconfig.json b/types/bdfjs/tsconfig.json new file mode 100644 index 0000000000..f1fe551e09 --- /dev/null +++ b/types/bdfjs/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "bdfjs-tests.ts" + ] +} diff --git a/types/bdfjs/tslint.json b/types/bdfjs/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/bdfjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }