-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(font): extend font declaration (#66)
* create font face declarations and extract font faces from text document
- Loading branch information
Showing
22 changed files
with
624 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { FontFace, FontPitch } from '../style'; | ||
import { FontFaceDeclarations } from './FontFaceDeclarations'; | ||
|
||
describe(FontFaceDeclarations.name, () => { | ||
const testFontFamily = 'someFontFamily'; | ||
const testFontName = 'someFontName'; | ||
const testFontPitch = FontPitch.Variable; | ||
|
||
let fontFaceDeclarations: FontFaceDeclarations; | ||
|
||
beforeEach(() => { | ||
fontFaceDeclarations = new FontFaceDeclarations(); | ||
}); | ||
|
||
describe('font face', () => { | ||
it('return an empty list by default', () => { | ||
const fonts = fontFaceDeclarations.getAll(); | ||
|
||
expect(fonts).toEqual([]); | ||
}); | ||
|
||
it('create and return new font', () => { | ||
const font = fontFaceDeclarations.create(testFontName, testFontFamily, testFontPitch); | ||
|
||
expect(font).toBeInstanceOf(FontFace); | ||
expect(font.getFontFamily()).toBe(testFontFamily); | ||
expect(font.getFontPitch()).toBe(testFontPitch); | ||
expect(font.getName()).toBe(testFontName); | ||
}); | ||
|
||
it('create a font only once', () => { | ||
const font1 = fontFaceDeclarations.create(testFontName); | ||
const font2 = fontFaceDeclarations.create(testFontName); | ||
const fonts = fontFaceDeclarations.getAll(); | ||
|
||
expect(font1).toBe(font2); | ||
expect(fonts.length).toBe(1); | ||
expect(fonts[0]).toBe(font1); | ||
}); | ||
|
||
it('get previously created font', () => { | ||
const font1 = fontFaceDeclarations.create(testFontName); | ||
const font2 = fontFaceDeclarations.get(testFontName); | ||
|
||
expect(font1).toBe(font2); | ||
}); | ||
|
||
it('return undefined if unknown font is requested', () => { | ||
fontFaceDeclarations.create(testFontName); | ||
|
||
const font = fontFaceDeclarations.get('unknownFontName'); | ||
|
||
expect(font).toBeUndefined(); | ||
}); | ||
|
||
it('delete font', () => { | ||
fontFaceDeclarations.create(testFontName); | ||
expect(fontFaceDeclarations.getAll().length).toBe(1); | ||
|
||
fontFaceDeclarations.delete(testFontName); | ||
expect(fontFaceDeclarations.getAll().length).toBe(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { FontFace, FontPitch } from '../style'; | ||
|
||
/** | ||
* This class contains all font face declarations of a document. | ||
* | ||
* It is used to manage the fonts that are used in the document. | ||
* | ||
* @example | ||
* document.getFontFaceDeclarations() | ||
* .create('FreeSans', 'FreeSans', FontPitch.Variable); | ||
* | ||
* @since 0.8.0 | ||
*/ | ||
export class FontFaceDeclarations { | ||
private readonly fontFaces: Map<string, FontFace> = new Map(); | ||
|
||
/** | ||
* Creates a {@link FontFace} object with the given name. | ||
* If a font with this name already exists, the existing font will be returned. | ||
* | ||
* @example | ||
* const fontFaceDeclarations = new FontFaceDeclarations(); | ||
* fontFaceDeclarations.create('FreeSans', 'FreeSans', FontPitch.Variable); | ||
* | ||
* @param {string} name The unique name for the font | ||
* @param {string} [fontFamily] The name of the font family | ||
* @param {FontPitch} [fontPitch] Indicator whether the font has a fixed or variable width | ||
* @returns {FontFace} A new `FontFace` object with the specified properties | ||
* or an existing font face, if one with the specified name exists | ||
* @since 0.8.0 | ||
*/ | ||
public create (name: string, fontFamily?: string, fontPitch?: FontPitch): FontFace { | ||
let fontFace = this.fontFaces.get(name); | ||
|
||
if (fontFace !== undefined) { | ||
return fontFace; | ||
} | ||
|
||
fontFace = new FontFace(name, fontFamily, fontPitch); | ||
this.fontFaces.set(name, fontFace); | ||
|
||
return fontFace; | ||
} | ||
|
||
/** | ||
* The `get()` method returns a specified element from a Map object. | ||
* | ||
* @example | ||
* const fontFaceDeclarations = new FontFaceDeclarations(); | ||
* fontFaceDeclarations.create('FreeSans'); | ||
* fontFaceDeclarations.get('UnknownFont'); // undefined | ||
* fontFaceDeclarations.get('FreeSans'); // FreeSans font | ||
* | ||
* @param {string} name The name of the requested font | ||
* @returns {FontFace | undefined} The `FontFace` object associated with the specified name | ||
* or `undefined` if there is no font with this name | ||
* @since 0.8.0 | ||
*/ | ||
public get (name: string): FontFace | undefined { | ||
return this.fontFaces.get(name); | ||
} | ||
|
||
/** | ||
* The `getAll()` method returns a new `Array` object that contains the fonts of the document. | ||
* | ||
* @example | ||
* const fontFaceDeclarations = new FontFaceDeclarations(); | ||
* fontFaceDeclarations.create('FreeSans'); | ||
* fontFaceDeclarations.create('Symbol'); | ||
* fontFaceDeclarations.getAll(); // [FreeSans, Symbol] | ||
* | ||
* @returns {FontFace[]} A new `Array` object that contains the fonts of the document | ||
* @since 0.8.0 | ||
*/ | ||
public getAll (): FontFace[] { | ||
return [...this.fontFaces.values()]; | ||
} | ||
|
||
/** | ||
* The `delete()` method removes the specified font from the font face declarations. | ||
* | ||
* @example | ||
* var myMap = new Map(); | ||
* const fontFaceDeclarations = new FontFaceDeclarations(); | ||
* fontFaceDeclarations.create('FreeSans'); | ||
* fontFaceDeclarations.create('Symbol'); | ||
* fontFaceDeclarations.delete('FreeSans'); | ||
* fontFaceDeclarations.get('FreeSans'); // undefined | ||
* | ||
* @param {string} name The name of the font to remove from the font face declarations | ||
* @returns {Meta} The `Meta` object | ||
* @since 0.8.0 | ||
*/ | ||
public delete (name: string): FontFaceDeclarations { | ||
this.fontFaces.delete(name); | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.