Skip to content

Commit abbe9c2

Browse files
scottrippeydevongovett
authored andcommitted
Set a global default language (#195)
1 parent 4ca73fe commit abbe9c2

File tree

3 files changed

+139
-23
lines changed

3 files changed

+139
-23
lines changed

src/TTFFont.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class TTFFont {
2525
}
2626

2727
constructor(stream, variationCoords = null) {
28-
this.defaultLanguage = 'en';
28+
this.defaultLanguage = null;
2929
this.stream = stream;
3030
this.variationCoords = variationCoords;
3131

@@ -45,6 +45,10 @@ export default class TTFFont {
4545
}
4646
}
4747

48+
setDefaultLanguage(lang = null) {
49+
this.defaultLanguage = lang;
50+
}
51+
4852
_getTable(table) {
4953
if (!(table.tag in this._tables)) {
5054
try {
@@ -84,39 +88,36 @@ export default class TTFFont {
8488
return result;
8589
}
8690

87-
/**
88-
* The unique PostScript name for this font
89-
* @type {string}
90-
*/
91-
get postscriptName() {
92-
let name = this.getName('postscriptName');
93-
if (name) {
94-
return name;
95-
}
96-
97-
let record = this.name.records.postscriptName;
98-
if (record) {
99-
let lang = Object.keys(record)[0];
100-
return record[lang];
101-
}
102-
103-
return null;
104-
}
105-
10691
/**
10792
* Gets a string from the font's `name` table
10893
* `lang` is a BCP-47 language code.
10994
* @return {string}
11095
*/
111-
getName(key, lang = this.defaultLanguage) {
112-
let record = this.name.records[key];
96+
getName(key, lang = this.defaultLanguage || fontkit.defaultLanguage) {
97+
let record = this.name && this.name.records[key];
11398
if (record) {
114-
return record[lang];
99+
// Attempt to retrieve the entry, depending on which translation is available:
100+
return (
101+
record[lang]
102+
|| record[this.defaultLanguage]
103+
|| record[fontkit.defaultLanguage]
104+
|| record['en']
105+
|| record[Object.keys(record)[0]] // Seriously, ANY language would be fine
106+
|| null
107+
);
115108
}
116109

117110
return null;
118111
}
119112

113+
/**
114+
* The unique PostScript name for this font, e.g. "Helvetica-Bold"
115+
* @type {string}
116+
*/
117+
get postscriptName() {
118+
return this.getName('postscriptName');
119+
}
120+
120121
/**
121122
* The font's full name, e.g. "Helvetica Bold"
122123
* @type {string}

src/base.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ fontkit.create = function(buffer, postscriptName) {
5252

5353
throw new Error('Unknown font format');
5454
};
55+
56+
fontkit.defaultLanguage = 'en';
57+
fontkit.setDefaultLanguage = function(lang = 'en') {
58+
fontkit.defaultLanguage = lang;
59+
};

test/i18n.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import assert from 'assert';
2+
3+
import fontkit from '../src';
4+
5+
describe('i18n', function() {
6+
describe('fontkit.setDefaultLanguage', function () {
7+
let font;
8+
before('load Amiri font', function() {
9+
font = fontkit.openSync(__dirname + '/data/amiri/amiri-regular.ttf');
10+
});
11+
after('reset default language', function () {
12+
fontkit.setDefaultLanguage();
13+
});
14+
15+
16+
it('font has "en" metadata properties', function() {
17+
assert.equal(font.fullName, 'Amiri');
18+
assert.equal(font.postscriptName, 'Amiri-Regular');
19+
assert.equal(font.familyName, 'Amiri');
20+
assert.equal(font.subfamilyName, 'Regular');
21+
assert.equal(font.copyright, 'Copyright (c) 2010-2017, Khaled Hosny <khaledhosny@eglug.org>.\nPortions copyright (c) 2010, Sebastian Kosch <sebastian@aldusleaf.org>.');
22+
assert.equal(font.version, 'Version 000.110 ');
23+
});
24+
25+
it('can set global default language to "ar"', function () {
26+
fontkit.setDefaultLanguage('ar');
27+
assert.equal(fontkit.defaultLanguage, 'ar');
28+
});
29+
30+
it('font now has "ar" metadata properties', function() {
31+
assert.equal(font.fullName, 'Amiri');
32+
assert.equal(font.postscriptName, 'Amiri-Regular');
33+
assert.equal(font.familyName, 'Amiri');
34+
assert.equal(font.subfamilyName, 'عادي');
35+
assert.equal(font.copyright, 'حقوق النشر 2010-2017، خالد حسني <khaledhosny@eglug.org>.');
36+
assert.equal(font.version, 'إصدارة 000٫110');
37+
});
38+
39+
it('can reset default language back to "en"', function () {
40+
fontkit.setDefaultLanguage();
41+
assert.equal(fontkit.defaultLanguage, "en");
42+
});
43+
});
44+
45+
describe('font.setDefaultLanguage', function () {
46+
let font;
47+
before('load Amiri font', function () {
48+
font = fontkit.openSync(__dirname + '/data/amiri/amiri-regular.ttf');
49+
});
50+
51+
it('font has "en" metadata properties', function() {
52+
assert.equal(font.fullName, 'Amiri');
53+
assert.equal(font.postscriptName, 'Amiri-Regular');
54+
assert.equal(font.familyName, 'Amiri');
55+
assert.equal(font.subfamilyName, 'Regular');
56+
assert.equal(font.copyright, 'Copyright (c) 2010-2017, Khaled Hosny <khaledhosny@eglug.org>.\nPortions copyright (c) 2010, Sebastian Kosch <sebastian@aldusleaf.org>.');
57+
assert.equal(font.version, 'Version 000.110 ');
58+
});
59+
60+
it('can set font\'s default language to "ar"', function () {
61+
font.setDefaultLanguage('ar');
62+
assert.equal(font.defaultLanguage, 'ar');
63+
});
64+
65+
it('font now has "ar" metadata properties', function() {
66+
assert.equal(font.fullName, 'Amiri');
67+
assert.equal(font.postscriptName, 'Amiri-Regular');
68+
assert.equal(font.familyName, 'Amiri');
69+
assert.equal(font.subfamilyName, 'عادي');
70+
assert.equal(font.copyright, 'حقوق النشر 2010-2017، خالد حسني <khaledhosny@eglug.org>.');
71+
assert.equal(font.version, 'إصدارة 000٫110');
72+
});
73+
74+
it('the font\'s language should not change when the global changes', function () {
75+
fontkit.setDefaultLanguage('en');
76+
77+
assert.equal(font.defaultLanguage, 'ar');
78+
assert.equal(font.subfamilyName, 'عادي');
79+
});
80+
81+
it('can reset default language back to "en"', function () {
82+
font.setDefaultLanguage();
83+
assert.equal(font.defaultLanguage, null);
84+
assert.equal(font.subfamilyName, 'Regular');
85+
});
86+
});
87+
88+
describe('backup languages', function () {
89+
let font;
90+
before('load Amiri font', function () {
91+
font = fontkit.openSync(__dirname + '/data/amiri/amiri-regular.ttf');
92+
});
93+
after('reset default language', function () {
94+
fontkit.setDefaultLanguage();
95+
});
96+
97+
it('if the font\'s default language isn\'t found, use the global language', function () {
98+
font.setDefaultLanguage('piglatin');
99+
fontkit.setDefaultLanguage('ar');
100+
101+
assert.equal(font.subfamilyName, 'عادي');
102+
});
103+
it('if the global language isn\'t found, use "en"', function () {
104+
font.setDefaultLanguage('piglatin');
105+
fontkit.setDefaultLanguage('klingon');
106+
107+
assert.equal(font.subfamilyName, 'Regular');
108+
});
109+
});
110+
});

0 commit comments

Comments
 (0)