Skip to content

Commit

Permalink
Merge pull request #78 from chearon/add-tests
Browse files Browse the repository at this point in the history
Add initial test suite
  • Loading branch information
khaledhosny authored Apr 25, 2023
2 parents 94d09ba + c59e248 commit 94f5a78
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ jobs:

- name: Setup Node.js
uses: actions/setup-node@v3
- name: NPM install
run: npm install
- name: Run tests
run: npm test
- name: Test hb.wasm
run: node examples/hbjs.example.node.js
- name: Test hb-subset.wasm
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Minimal version of HarfBuzz for JavaScript use",
"main": "index.js",
"scripts": {
"test": "node examples/hbjs.example.node.js"
"test": "mocha test/index.js"
},
"keywords": [
"harfbuzz",
Expand All @@ -29,5 +29,9 @@
"bugs": {
"url": "https://github.com/harfbuzz/harfbuzzjs/issues"
},
"homepage": "https://github.com/harfbuzz/harfbuzzjs#readme"
"homepage": "https://github.com/harfbuzz/harfbuzzjs#readme",
"devDependencies": {
"chai": "^4.3.7",
"mocha": "^10.2.0"
}
}
Binary file added test/fonts/noto/NotoSans-Regular.ttf
Binary file not shown.
Binary file added test/fonts/noto/NotoSansArabic-Variable.ttf
Binary file not shown.
93 changes: 93 additions & 0 deletions test/fonts/noto/OFL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright 2015-2021 Google LLC. All Rights Reserved.

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
125 changes: 125 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const fs = require('fs');
const path = require('path');
const {expect} = require('chai');
let hb;

before(async function () {
hb = await require('..');
});

afterEach(function () {
if (this.blob) this.blob.destroy();
if (this.face) this.face.destroy();
if (this.font) this.font.destroy();
if (this.buffer) this.buffer.destroy();
this.blob = this.face = this.font = this.buffer = null;
});

describe('Face', function () {
it('collectUnicodes reflects codepoints supported by the font', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
const codepoints = [...this.face.collectUnicodes()];
expect(codepoints).to.include('a'.codePointAt(0));
expect(codepoints).not.to.include('ا'.codePointAt(0));
});

it('exposes upem', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
expect(this.face.upem).to.equal(1000);
});

it('getAxisInfos returns details of a variable font', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
this.face = hb.createFace(this.blob);
expect(this.face.getAxisInfos()).to.deep.equal({
wght: {min: 100, default: 400, max: 900},
wdth: {min: 62.5, default: 100, max: 100}
});
});

it('getAxisInfos returns an empty object for a non-variable font', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
expect(Object.keys(this.face.getAxisInfos())).to.have.lengthOf(0);
});
});

describe('Font', function () {
it('glyphName returns names for glyph ids', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
this.font = hb.createFont(this.face);
expect(this.font.glyphName(20)).to.equal('one');
});

it('setScale affects advances', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
this.font = hb.createFont(this.face);
this.buffer = hb.createBuffer();
this.buffer.addText('a');
this.buffer.guessSegmentProperties();
this.font.setScale(1000 * 2, 1000 * 2);
hb.shape(this.font, this.buffer)
const glyphs = this.buffer.json();
expect(glyphs[0].ax).to.equal(561 * 2);
});

it('setVariations affects advances', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSansArabic-Variable.ttf')));
this.face = hb.createFace(this.blob);
this.font = hb.createFont(this.face);
this.font.setVariations({'wght': 789});
this.buffer = hb.createBuffer();
this.buffer.addText('آلو');
this.buffer.guessSegmentProperties();
hb.shape(this.font, this.buffer)
const glyphs = this.buffer.json();
expect(glyphs[0].ax).to.equal(526);
});
});

describe('Buffer', function () {
it('setDirection controls direction of glyphs', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
this.font = hb.createFont(this.face);
this.buffer = hb.createBuffer();
this.buffer.addText('rtl');
this.buffer.setDirection('rtl');
hb.shape(this.font, this.buffer)
const glyphs = this.buffer.json();
expect(glyphs[0].g).to.equal(79); // l
expect(glyphs[1].g).to.equal(87); // t
expect(glyphs[2].g).to.equal(85); // r
});

it('setClusterLevel affects cluster merging', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
this.font = hb.createFont(this.face);
this.buffer = hb.createBuffer();
this.buffer.setClusterLevel(1);
this.buffer.addText('x́');
this.buffer.guessSegmentProperties();
hb.shape(this.font, this.buffer)
const glyphs = this.buffer.json();
expect(glyphs[0].cl).to.equal(0);
expect(glyphs[1].cl).to.equal(1);
});

it('setFlags with PRESERVE_DEFAULT_IGNORABLES affects glyph ids', function () {
this.blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
this.face = hb.createFace(this.blob);
this.font = hb.createFont(this.face);
this.buffer = hb.createBuffer();
this.buffer.addText('\u200dhi');
this.buffer.setFlags(['PRESERVE_DEFAULT_IGNORABLES']);
this.buffer.guessSegmentProperties();
hb.shape(this.font, this.buffer)
const glyphs = this.buffer.json();
expect(glyphs[0].g).not.to.equal(3 /* space */);
});
});

0 comments on commit 94f5a78

Please sign in to comment.