├── test ├── fixtures │ ├── fonts │ │ ├── invalid │ │ │ └── foo.ttf │ │ ├── noto │ │ │ ├── NotoSans-Regular.ttc │ │ │ └── LICENSE │ │ ├── open-sans │ │ │ ├── OpenSans-Regular.ttf │ │ │ └── Apache License.txt │ │ └── GuardianTextSansWeb │ │ │ ├── GuardianTextSansWeb-Bold.ttf │ │ │ └── LICENSE.md │ └── LICENSE.md └── test.js ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── package.json ├── index.js └── README.md /test/fixtures/fonts/invalid/foo.ttf: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .nyc_output 3 | coverage 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .nyc_output 2 | test 3 | coverage 4 | .travis.yml 5 | -------------------------------------------------------------------------------- /test/fixtures/fonts/noto/NotoSans-Regular.ttc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mapbox/fontmachine/HEAD/test/fixtures/fonts/noto/NotoSans-Regular.ttc -------------------------------------------------------------------------------- /test/fixtures/fonts/open-sans/OpenSans-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mapbox/fontmachine/HEAD/test/fixtures/fonts/open-sans/OpenSans-Regular.ttf -------------------------------------------------------------------------------- /test/fixtures/fonts/GuardianTextSansWeb/GuardianTextSansWeb-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mapbox/fontmachine/HEAD/test/fixtures/fonts/GuardianTextSansWeb/GuardianTextSansWeb-Bold.ttf -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | matrix: 4 | include: 5 | - node_js: 8 6 | script: 7 | - npm test 8 | - node_js: 10 9 | script: 10 | - npm test 11 | after_script: 12 | - npm run upload-coverage 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v0.3.0 4 | 5 | - Add support for Node 10. Drop support for Node 4 and Node 6. 6 | - Add `concurrency` option to the `makeGlyphs` function, to allow specifying the number of glyph ranges that will be processed in parallel. 7 | - Perform some code and documentation maintenance. 8 | 9 | ## v0.2.2 10 | 11 | - Upgraded node-fontnik from 0.5.0 to 0.5.2 12 | - Adds package-lock.json 13 | 14 | ## v0.2.1 15 | 16 | - Now using @mapbox/fontmachine namespace, `fontmachine` is deprecated 17 | 18 | ## v0.2.0 19 | 20 | - Upgraded node-fontnik from ^0.4.3 to ~0.5.0 21 | - Now only testing on node v4/v5 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2015, Mapbox 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /test/fixtures/LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2014 Guardian News & Media Ltd 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | ---- 16 | 17 | All fonts are the property of Schwartzco, Inc., t/a Commercial Type 18 | (https://commercialtype.com/), and may not be reproduced without 19 | permission. 20 | -------------------------------------------------------------------------------- /test/fixtures/fonts/GuardianTextSansWeb/LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2014 Guardian News & Media Ltd 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | ---- 16 | 17 | All fonts are the property of Schwartzco, Inc., t/a Commercial Type 18 | (https://commercialtype.com/), and may not be reproduced without 19 | permission. 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@mapbox/fontmachine", 3 | "version": "0.3.0", 4 | "description": "Make GL-ready pbfs and metadata for usage in fontstack API.", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/mapbox/fontmachine.git" 8 | }, 9 | "main": "index.js", 10 | "scripts": { 11 | "lint": "eslint .", 12 | "test": "npm run lint && nyc tap test/test.js", 13 | "coverage": "nyc report --reporter html && opener coverage/index.html", 14 | "upload-coverage": "nyc report --reporter json && codecov -f ./coverage/coverage-final.json" 15 | }, 16 | "author": "", 17 | "license": "ISC", 18 | "engines": { 19 | "node": ">=8" 20 | }, 21 | "dependencies": { 22 | "d3-queue": "^3.0.7", 23 | "fontnik": "^0.5.2" 24 | }, 25 | "devDependencies": { 26 | "@mapbox/eslint-config-mapbox": "^2.0.1", 27 | "@mapbox/glyph-pbf-composite": "0.0.2", 28 | "codecov": "^3.6.1", 29 | "eslint": "^6.7.2", 30 | "eslint-plugin-node": "^10.0.0", 31 | "nyc": "^14.1.1", 32 | "opener": "^1.5.1", 33 | "rewire": "^4.0.1", 34 | "sinon": "^7.5.0", 35 | "tap": "6.2.0" 36 | }, 37 | "eslintConfig": { 38 | "extends": "@mapbox/eslint-config-mapbox" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fontnik = require('fontnik'); 4 | const queue = require('d3-queue').queue; 5 | const zlib = require('zlib'); 6 | 7 | /** 8 | * Make all metadata (codepoints) and SDF PBFs necessary for Mapbox GL fontstacks. 9 | * @param {Object} opts Options object with required `font` and `filetype` properties, and any optional parameters. 10 | * @param {Buffer} opts.font The font file as a `Buffer`. 11 | * @param {string} opts.filetype Type of font (e.g. `'.ttf'` or `'.otf'`). 12 | * @param {number} [opts.concurrency] Concurrency to use when processing font into PBFs. If `undefined`, concurrency is unbounded. 13 | * @param {function(err, result)} callback Callback takes arguments (`error`, `result`). 14 | * 15 | * **Callback `result`:** 16 | * * `{string}` `name` The name of this font (concatenated family_name + style_name). 17 | * * `{Array}` `stack` An array of `{name: filename, data: buffer}` objects with SDF PBFs covering points 0-65535. 18 | * * `{Object}` `metadata` An object where `data` is a stringified codepoints result. 19 | * * `{name: string, data: Buffer}` `original` An object containing the original font file (named `"original{.filetype}"`) 20 | * 21 | */ 22 | function makeGlyphs(opts, callback) { 23 | // undefined (unset concurrency) means unbounded concurrency in d3-queue 24 | const q = queue(opts.concurrency); 25 | 26 | if (!(opts.font instanceof Buffer)) throw new Error('opts.font must be a Buffer'); 27 | if (typeof opts.filetype !== 'string') throw new Error('opts.filetype must be a String'); 28 | if (!(callback instanceof Function)) throw new Error('Callback must be a Function'); 29 | 30 | fontnik.load(opts.font, (err, faces) => { 31 | if (err) { 32 | return callback(err); 33 | } 34 | 35 | if (faces.length > 1) { 36 | return callback(new Error('Multiple faces in a font are not yet supported.')); 37 | } 38 | 39 | const metadata = faces[0]; 40 | 41 | for (let i = 0; i < 65536; (i = i + 256)) { 42 | q.defer(writeGlyphs, opts.font, i, Math.min(i + 255, 65535)); 43 | } 44 | 45 | q.awaitAll((err, results) => { 46 | if (err) return callback(err); 47 | return callback(null, { 48 | name: metadata.style_name ? [metadata.family_name, metadata.style_name].join(' ') : metadata.family_name, 49 | stack: results, 50 | metadata: Object.keys(metadata).reduce((prev, key) => { 51 | if (key !== 'points') { 52 | prev[key] = metadata[key]; 53 | } 54 | 55 | return prev; 56 | }, {}), 57 | codepoints: metadata.points, 58 | original: { 59 | name: 'original' + opts.filetype, 60 | data: opts.font 61 | } 62 | }); 63 | }); 64 | }); 65 | } 66 | 67 | function writeGlyphs(font, start, end, done) { 68 | fontnik.range({ font: font, start: start, end: end }, (err, data) => { 69 | if (err) { 70 | return done(err); 71 | } 72 | const name = [start, '-', end, '.pbf'].join(''); 73 | zlib.gzip(data, (err, res) => { 74 | if (err) { 75 | return done(err); 76 | } 77 | const result = { 78 | name: name, 79 | data: res 80 | }; 81 | return done(null, result); 82 | }); 83 | }); 84 | } 85 | 86 | module.exports = { 87 | makeGlyphs: makeGlyphs, 88 | writeGlyphs: writeGlyphs // exported only for testing 89 | }; 90 | -------------------------------------------------------------------------------- /test/fixtures/fonts/noto/LICENSE: -------------------------------------------------------------------------------- 1 | This Font Software is licensed under the SIL Open Font License, 2 | Version 1.1. 3 | 4 | This license is copied below, and is also available with a FAQ at: 5 | http://scripts.sil.org/OFL 6 | 7 | ----------------------------------------------------------- 8 | SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 9 | ----------------------------------------------------------- 10 | 11 | PREAMBLE 12 | The goals of the Open Font License (OFL) are to stimulate worldwide 13 | development of collaborative font projects, to support the font 14 | creation efforts of academic and linguistic communities, and to 15 | provide a free and open framework in which fonts may be shared and 16 | improved in partnership with others. 17 | 18 | The OFL allows the licensed fonts to be used, studied, modified and 19 | redistributed freely as long as they are not sold by themselves. The 20 | fonts, including any derivative works, can be bundled, embedded, 21 | redistributed and/or sold with any software provided that any reserved 22 | names are not used by derivative works. The fonts and derivatives, 23 | however, cannot be released under any other type of license. The 24 | requirement for fonts to remain under this license does not apply to 25 | any document created using the fonts or their derivatives. 26 | 27 | DEFINITIONS 28 | "Font Software" refers to the set of files released by the Copyright 29 | Holder(s) under this license and clearly marked as such. This may 30 | include source files, build scripts and documentation. 31 | 32 | "Reserved Font Name" refers to any names specified as such after the 33 | copyright statement(s). 34 | 35 | "Original Version" refers to the collection of Font Software 36 | components as distributed by the Copyright Holder(s). 37 | 38 | "Modified Version" refers to any derivative made by adding to, 39 | deleting, or substituting -- in part or in whole -- any of the 40 | components of the Original Version, by changing formats or by porting 41 | the Font Software to a new environment. 42 | 43 | "Author" refers to any designer, engineer, programmer, technical 44 | writer or other person who contributed to the Font Software. 45 | 46 | PERMISSION & CONDITIONS 47 | Permission is hereby granted, free of charge, to any person obtaining 48 | a copy of the Font Software, to use, study, copy, merge, embed, 49 | modify, redistribute, and sell modified and unmodified copies of the 50 | Font Software, subject to the following conditions: 51 | 52 | 1) Neither the Font Software nor any of its individual components, in 53 | Original or Modified Versions, may be sold by itself. 54 | 55 | 2) Original or Modified Versions of the Font Software may be bundled, 56 | redistributed and/or sold with any software, provided that each copy 57 | contains the above copyright notice and this license. These can be 58 | included either as stand-alone text files, human-readable headers or 59 | in the appropriate machine-readable metadata fields within text or 60 | binary files as long as those fields can be easily viewed by the user. 61 | 62 | 3) No Modified Version of the Font Software may use the Reserved Font 63 | Name(s) unless explicit written permission is granted by the 64 | corresponding Copyright Holder. This restriction only applies to the 65 | primary font name as presented to the users. 66 | 67 | 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font 68 | Software shall not be used to promote, endorse or advertise any 69 | Modified Version, except to acknowledge the contribution(s) of the 70 | Copyright Holder(s) and the Author(s) or with their explicit written 71 | permission. 72 | 73 | 5) The Font Software, modified or unmodified, in part or in whole, 74 | must be distributed entirely under this license, and must not be 75 | distributed under any other license. The requirement for fonts to 76 | remain under this license does not apply to any document created using 77 | the Font Software. 78 | 79 | TERMINATION 80 | This license becomes null and void if any of the above conditions are 81 | not met. 82 | 83 | DISCLAIMER 84 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 85 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF 86 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 87 | OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE 88 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 89 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 90 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 91 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM 92 | OTHER DEALINGS IN THE FONT SOFTWARE. 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED @mapbox/fontmachine 2 | 3 | [![Build Status](https://travis-ci.org/mapbox/fontmachine.svg?branch=master)](https://travis-ci.org/mapbox/fontmachine) [![codecov](https://codecov.io/gh/mapbox/fontmachine/branch/master/graph/badge.svg)](https://codecov.io/gh/mapbox/fontmachine) 4 | 5 | :warning: This code is deprecated, which means it is no longer maintained and will not receive updates. Instead of this library, we recommend using [node-fontnik](https://github.com/mapbox/node-fontnik) directly. 6 | 7 | Make GL-ready pbfs and metadata for usage in fontstack API. 8 | 9 | ### `makeGlyphs(opts, callback)` 10 | 11 | Make all metadata (codepoints) and SDF PBFs necessary for Mapbox GL fontstacks. 12 | 13 | ### Parameters 14 | 15 | | parameter | type | description | 16 | | -------------------- | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 17 | | `opts` | `Object` | Options object with required `font` and `filetype` properties, and any optional parameters. | 18 | | `opts.font` | `Buffer` | The font file as a `Buffer`. | 19 | | `opts.filetype` | `string` | Type of font (e.g. `'.ttf'` or `'.otf'`). | 20 | | `[opts.concurrency]` | `number` | _optional:_ Concurrency to use when processing font into PBFs. If `undefined`, concurrency is unbounded. | 21 | | `callback` | `function(err, result)` | Callback takes arguments (`error`, `result`). 22 | 23 | ### Callback `result` 24 | 25 | | property | type | description | 26 | | ---------- | ------------------------------------- | ------------------------------------------------------------------------------------------- | 27 | | `name` | `string` | The name of this font (concatenated family_name + style_name). | 28 | | `stack` | `Array<{name: string, data: Buffer}>` | An array of 256 `{name: filename, data: buffer}` objects with SDF PBFs covering points 0-65535. | 29 | | `metadata` | `{family_name: string, style_name: string}` | Metadata about the font. | 30 | | `codepoints` | `Array` | Array of codepoints covered by the font. 31 | | `original` | `{name: string, data: Buffer}` | An object containing the original font file (named `'original'`) | 32 | 33 | ## Installation 34 | 35 | Requires [nodejs](http://nodejs.org/). 36 | 37 | ```sh 38 | $ npm install @mapbox/fontmachine 39 | ``` 40 | 41 | ## Tests 42 | 43 | ```sh 44 | $ npm test 45 | ``` 46 | 47 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const zlib = require('zlib'); 6 | 7 | const sinon = require('sinon'); 8 | const test = require('tap').test; 9 | const fontnik = require('fontnik'); 10 | const rewire = require('rewire'); 11 | const fontmachine = rewire('../index.js'); 12 | 13 | const glyphComposite = require('@mapbox/glyph-pbf-composite'); 14 | const opensans = fs.readFileSync(path.resolve(path.join(__dirname, '/fixtures/fonts/open-sans/OpenSans-Regular.ttf'))); 15 | const guardianbold = fs.readFileSync(path.resolve(path.join(__dirname, '/fixtures/fonts/GuardianTextSansWeb/GuardianTextSansWeb-Bold.ttf'))); 16 | const noto = fs.readFileSync(path.resolve(path.join(__dirname, '/fixtures/fonts/noto/NotoSans-Regular.ttc'))); 17 | const invalid = fs.readFileSync(path.resolve(path.join(__dirname, '/fixtures/fonts/invalid/foo.ttf'))); 18 | 19 | test('handle undefined style_name', (t) => { 20 | fontmachine.makeGlyphs({ font: guardianbold, filetype: '.ttf' }, (err, font) => { 21 | t.ifError(err); 22 | t.ok(font, 'returns a font'); 23 | t.equal(font.name, '?'); 24 | t.ok(font.metadata, 'metadata exists'); 25 | t.ok(font.metadata instanceof Object, 'metadata is object'); 26 | t.equal(font.metadata.family_name, '?'); 27 | t.equal(font.metadata.style_name, undefined); 28 | t.ok(font.codepoints, 'codepoints exists'); 29 | t.ok(Array.isArray(font.codepoints), 'codepoints is array'); 30 | t.equal(font.codepoints.length, 227, 'right number of codepoints'); 31 | t.ok(font.original, 'original font file exists'); 32 | t.equal(font.original.name, 'original.ttf'); 33 | t.deepEqual(font.original.data, guardianbold, 'Font files equal eachother'); 34 | t.ok(font.stack, 'pbf stack exists'); 35 | t.equal(font.stack.length, 256, 'font stack has the right size'); 36 | t.ok(Array.isArray(font.stack), 'pbf stack is array'); 37 | t.end(); 38 | }); 39 | }); 40 | 41 | test('handle api misuse opts.font', (t) => { 42 | t.throws(() => { 43 | fontmachine.makeGlyphs({ font: 'STRING' }); 44 | }, /opts.font must be a Buffer/); 45 | t.end(); 46 | }); 47 | 48 | test('handle api misuse opts.filetype', (t) => { 49 | t.throws(() => { 50 | fontmachine.makeGlyphs({ font: Buffer.alloc(0), filetype: null }); 51 | }, /opts.filetype must be a String/); 52 | t.end(); 53 | }); 54 | 55 | test('handle api misuse callback', (t) => { 56 | t.throws(() => { 57 | fontmachine.makeGlyphs({ font: Buffer.alloc(0), filetype: '.ttf' }, 'callback'); 58 | }, /Callback must be a Function/); 59 | t.end(); 60 | }); 61 | 62 | test('handle not a font file', (t) => { 63 | fontmachine.makeGlyphs({ font: invalid, filetype: '.ttf' }, (err) => { 64 | t.ok(err); 65 | t.equal(err.message, 'could not open font file', 'Handles file that is not a font'); 66 | t.end(); 67 | }); 68 | }); 69 | 70 | test('handle multifont', (t) => { 71 | fontmachine.makeGlyphs({ font: noto, filetype: '.ttc' }, (err) => { 72 | t.ok(err); 73 | t.equal(err.message, 'Multiple faces in a font are not yet supported.', 'Calls callback with an error'); 74 | t.end(); 75 | }); 76 | }); 77 | 78 | test('calls-back with fontnik.range error', (t) => { 79 | const mockError = new Error(); 80 | sinon.stub(fontnik, 'range').callsFake((_, cb) => cb(mockError)); 81 | 82 | fontmachine.makeGlyphs({ font: opensans, filetype: '.ttf' }, (err, font) => { 83 | t.ok(err, 'has an error'); 84 | t.equal(err, mockError, 'error passed along from fontnik.range'); 85 | t.notOk(font, 'does not have font result'); 86 | sinon.restore(); 87 | t.done(); 88 | }); 89 | }); 90 | 91 | test('calls-back with zlib.gzip error', (t) => { 92 | const mockError = new Error(); 93 | sinon.stub(zlib, 'gzip').callsFake((_, cb) => cb(mockError)); 94 | 95 | fontmachine.makeGlyphs({ font: opensans, filetype: '.ttf' }, (err, font) => { 96 | t.ok(err, 'has an error'); 97 | t.equal(err, mockError, 'error passed along from zlib.gzip'); 98 | t.notOk(font, 'does not have font result'); 99 | sinon.restore(); 100 | t.done(); 101 | }); 102 | }); 103 | 104 | test('font machine 256-511', (t) => { 105 | fontmachine.makeGlyphs({ font: opensans, filetype: '.ttf' }, (err, font) => { 106 | t.ifError(err); 107 | t.ok(font, 'returns a font'); 108 | t.equal(font.name, 'Open Sans Regular'); 109 | t.ok(font.metadata, 'metadata exists'); 110 | t.ok(font.metadata instanceof Object, 'metadata is object'); 111 | t.equal(font.metadata.family_name, 'Open Sans'); 112 | t.equal(font.metadata.style_name, 'Regular'); 113 | t.ok(font.codepoints, 'codepoints exists'); 114 | t.ok(Array.isArray(font.codepoints), 'codepoints is array'); 115 | t.equal(font.codepoints.length, 882, 'right number of codepoints'); 116 | t.ok(font.original, 'original font file exists'); 117 | t.equal(font.original.name, 'original.ttf'); 118 | t.deepEqual(font.original.data, opensans, 'Font files equal eachother'); 119 | t.ok(font.stack, 'pbf stack exists'); 120 | t.equal(font.stack.length, 256, 'font stack has the right size'); 121 | t.ok(Array.isArray(font.stack), 'pbf stack is array'); 122 | const stack512 = font.stack.filter((stack) => { 123 | return stack.name === '256-511.pbf'; 124 | })[0]; 125 | zlib.gunzip(stack512.data, (err, rawBody) => { 126 | t.ifError(err, 'gunzips successfully'); 127 | const decodedStack = glyphComposite.decode(rawBody); 128 | t.ok(decodedStack.stacks, 'stack exists'); 129 | t.equal(decodedStack.stacks.length, 1, 'Only one stack'); 130 | t.ok(decodedStack.stacks[0].glyphs, 'glyphs exist'); 131 | t.equal(decodedStack.stacks[0].glyphs.length, 140, 'There are glyphs'); 132 | t.equal(decodedStack.stacks[0].name, 'Open Sans Regular'); 133 | t.equal(decodedStack.stacks[0].range, '256-511'); 134 | t.equal(decodedStack.stacks[0].glyphs[0].id, 256, 'correct first id'); 135 | t.equal(decodedStack.stacks[0].glyphs[0].width, 15, 'correct first width'); 136 | t.equal(decodedStack.stacks[0].glyphs[0].height, 20, 'correct first height'); 137 | t.equal(decodedStack.stacks[0].glyphs[0].left, 0, 'correct first left'); 138 | t.equal(decodedStack.stacks[0].glyphs[0].top, -6, 'correct first top'); 139 | t.equal(decodedStack.stacks[0].glyphs[0].advance, 15, 'correct first advance'); 140 | t.equal(decodedStack.stacks[0].glyphs[75].id, 331, 'correct 76th id'); 141 | t.equal(decodedStack.stacks[0].glyphs[75].width, 11, 'correct 76th width'); 142 | t.equal(decodedStack.stacks[0].glyphs[75].height, 19, 'correct 76th height'); 143 | t.equal(decodedStack.stacks[0].glyphs[75].left, 2, 'correct 76th left'); 144 | t.equal(decodedStack.stacks[0].glyphs[75].top, -13, 'correct 76th top'); 145 | t.equal(decodedStack.stacks[0].glyphs[75].advance, 14, 'correct 76th advance'); 146 | t.end(); 147 | }); 148 | }); 149 | }); 150 | 151 | test('font machine 7680-7935', (t) => { 152 | fontmachine.makeGlyphs({ font: opensans, filetype: '.ttf' }, (err, font) => { 153 | t.ifError(err); 154 | const stack512 = font.stack.filter((stack) => { 155 | return stack.name === '7680-7935.pbf'; 156 | })[0]; 157 | zlib.gunzip(stack512.data, (err, rawBody) => { 158 | t.ifError(err, 'gunzips successfully'); 159 | const decodedStack = glyphComposite.decode(rawBody); 160 | t.ok(decodedStack.stacks, 'stack exists'); 161 | t.equal(decodedStack.stacks.length, 1, 'Only one stack'); 162 | t.ok(decodedStack.stacks[0].glyphs, 'glyphs exist'); 163 | t.equal(decodedStack.stacks[0].glyphs.length, 100, 'There are glyphs'); 164 | t.equal(decodedStack.stacks[0].name, 'Open Sans Regular'); 165 | t.equal(decodedStack.stacks[0].range, '7680-7935'); 166 | t.equal(decodedStack.stacks[0].glyphs[0].id, 7680, 'correct first id'); 167 | t.equal(decodedStack.stacks[0].glyphs[0].width, 15, 'correct first width'); 168 | t.equal(decodedStack.stacks[0].glyphs[0].height, 24, 'correct first height'); 169 | t.equal(decodedStack.stacks[0].glyphs[0].left, 0, 'correct first left'); 170 | t.equal(decodedStack.stacks[0].glyphs[0].top, -9, 'correct first top'); 171 | t.equal(decodedStack.stacks[0].glyphs[0].advance, 15, 'correct first advance'); 172 | t.equal(decodedStack.stacks[0].glyphs[75].id, 7905, 'correct 76th id'); 173 | t.equal(decodedStack.stacks[0].glyphs[75].width, 14, 'correct 76th width'); 174 | t.equal(decodedStack.stacks[0].glyphs[75].height, 18, 'correct 76th height'); 175 | t.equal(decodedStack.stacks[0].glyphs[75].left, 1, 'correct 76th left'); 176 | t.equal(decodedStack.stacks[0].glyphs[75].top, -8, 'correct 76th top'); 177 | t.equal(decodedStack.stacks[0].glyphs[75].advance, 14, 'correct 76th advance'); 178 | t.end(); 179 | }); 180 | }); 181 | }); 182 | 183 | test('can set processing concurrency', (t) => { 184 | const opts = { 185 | font: opensans, 186 | filetype: '.ttf', 187 | concurrency: 1 // process glyph ranges serially 188 | }; 189 | 190 | const writeGlyphs = fontmachine.__get__('writeGlyphs'); 191 | const writeGlyphsSpy = sinon.spy(writeGlyphs); 192 | const unset = fontmachine.__set__('writeGlyphs', writeGlyphsSpy); 193 | 194 | fontmachine.makeGlyphs(opts, (err, font) => { 195 | t.ifError(err); 196 | t.ok(font, 'returns a font'); 197 | 198 | t.ok(writeGlyphsSpy.called, 'writeGlyphs is called'); 199 | 200 | const calls = writeGlyphsSpy.getCalls(); 201 | t.equal(calls.length, 256, 'writeGlyphs called expected number of times'); 202 | 203 | for (let i = 0; i < calls.length; i++) { 204 | // ensure that all calls to writeSpy were processed in sequential order 205 | const call = calls[i]; 206 | t.equal(call.args[1], i * 256, 'start argument'); 207 | t.equal(call.args[2], (i + 1) * 256 - 1, 'end argument'); 208 | } 209 | 210 | unset(); 211 | sinon.restore(); 212 | t.done(); 213 | }); 214 | }); 215 | -------------------------------------------------------------------------------- /test/fixtures/fonts/open-sans/Apache License.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------