diff --git a/src/TTFFont.js b/src/TTFFont.js index 6aa0937a..e2e07435 100644 --- a/src/TTFFont.js +++ b/src/TTFFont.js @@ -9,6 +9,7 @@ import TTFGlyph from './glyph/TTFGlyph'; import CFFGlyph from './glyph/CFFGlyph'; import SBIXGlyph from './glyph/SBIXGlyph'; import COLRGlyph from './glyph/COLRGlyph'; +import CBDTGlyph from './glyph/CBDTGlyph'; import GlyphVariationProcessor from './glyph/GlyphVariationProcessor'; import TTFSubset from './subset/TTFSubset'; import CFFSubset from './subset/CFFSubset'; @@ -414,6 +415,9 @@ export default class TTFFont { } else if ((this.directory.tables.COLR) && (this.directory.tables.CPAL)) { this._glyphs[glyph] = new COLRGlyph(glyph, characters, this); + } else if (this.directory.tables.CBLC || this.directory.tables.EBLC) { + this._glyphs[glyph] = new CBDTGlyph(glyph, characters, this); + } else { this._getBaseGlyph(glyph, characters); } diff --git a/src/glyph/CBDTGlyph.js b/src/glyph/CBDTGlyph.js new file mode 100644 index 00000000..4b38b4ec --- /dev/null +++ b/src/glyph/CBDTGlyph.js @@ -0,0 +1,25 @@ +import Glyph from './Glyph'; +import Path from './Path'; + +/** + * Represents a bitmap glyph in the CBDT/CBLC format (e.g. color emoji). + * CBDT glyphs are bitmap-based and have no vector outlines. + * Metrics (advanceWidth etc.) come from hmtx via the base Glyph._getMetrics(). + */ +export default class CBDTGlyph extends Glyph { + type = 'CBDT'; + + _getPath() { + // CBDT glyphs are bitmap-based, no vector outlines + return new Path(); + } + + _getCBox() { + return this.path.cbox; + } + + _getBBox() { + return this.path.bbox; + } +} + diff --git a/src/glyph/COLRGlyph.js b/src/glyph/COLRGlyph.js index 85d594b6..3b4bea02 100644 --- a/src/glyph/COLRGlyph.js +++ b/src/glyph/COLRGlyph.js @@ -36,6 +36,14 @@ export default class COLRGlyph extends Glyph { get layers() { let cpal = this._font.CPAL; let colr = this._font.COLR; + + // COLR v1 fonts (e.g. OpenMoji COLRv1) use paint-based records + // instead of v0 baseGlyphRecord. fontkit only supports v0, so + // return null to let callers fall back gracefully. + if (!colr || !colr.baseGlyphRecord) { + return null; + } + let low = 0; let high = colr.baseGlyphRecord.length - 1; diff --git a/src/glyph/TTFGlyph.js b/src/glyph/TTFGlyph.js index 8dc1c75b..02709b6b 100644 --- a/src/glyph/TTFGlyph.js +++ b/src/glyph/TTFGlyph.js @@ -273,7 +273,13 @@ export default class TTFGlyph extends Glyph { if (glyph.numberOfContours < 0) { // resolve composite glyphs for (let component of glyph.components) { - let contours = this._font.getGlyph(component.glyphID)._getContours(); + // In COLR fonts, getGlyph() may return a COLRGlyph which doesn't + // have _getContours(). Use _getBaseGlyph() to get the TTF outline. + let componentGlyph = this._font._getBaseGlyph(component.glyphID) || this._font.getGlyph(component.glyphID); + if (!componentGlyph || typeof componentGlyph._getContours !== 'function') { + continue; + } + let contours = componentGlyph._getContours(); for (let i = 0; i < contours.length; i++) { let contour = contours[i]; for (let j = 0; j < contour.length; j++) { diff --git a/test/data/NotoColorEmoji/NotoColorEmoji.ttf b/test/data/NotoColorEmoji/NotoColorEmoji.ttf new file mode 100644 index 00000000..943741df Binary files /dev/null and b/test/data/NotoColorEmoji/NotoColorEmoji.ttf differ diff --git a/test/data/NotoColorEmoji/OFL.txt b/test/data/NotoColorEmoji/OFL.txt new file mode 100644 index 00000000..d952d62c --- /dev/null +++ b/test/data/NotoColorEmoji/OFL.txt @@ -0,0 +1,92 @@ +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. diff --git a/test/data/OpenMoji/LICENSE.txt b/test/data/OpenMoji/LICENSE.txt new file mode 100644 index 00000000..260f674b --- /dev/null +++ b/test/data/OpenMoji/LICENSE.txt @@ -0,0 +1,13 @@ +OpenMoji +https://openmoji.org/ + +Copyright 2018-2024 OpenMoji project +https://github.com/hfg-gmuend/openmoji + +OpenMoji graphics are licensed under the Creative Commons +Attribution-ShareAlike License 4.0 (CC BY-SA 4.0) + +https://creativecommons.org/licenses/by-sa/4.0/ + +Attribution: All emojis designed by OpenMoji – the open-source emoji +and icon project. License: CC BY-SA 4.0 diff --git a/test/data/OpenMoji/OpenMoji-color-cbdt.ttf b/test/data/OpenMoji/OpenMoji-color-cbdt.ttf new file mode 100644 index 00000000..1a6e96e7 Binary files /dev/null and b/test/data/OpenMoji/OpenMoji-color-cbdt.ttf differ diff --git a/test/data/OpenMoji/OpenMoji-color-colr1_svg.ttf b/test/data/OpenMoji/OpenMoji-color-colr1_svg.ttf new file mode 100644 index 00000000..a1093c03 Binary files /dev/null and b/test/data/OpenMoji/OpenMoji-color-colr1_svg.ttf differ diff --git a/test/data/OpenMoji/OpenMoji-color-glyf_colr_0.ttf b/test/data/OpenMoji/OpenMoji-color-glyf_colr_0.ttf new file mode 100644 index 00000000..30c435bc Binary files /dev/null and b/test/data/OpenMoji/OpenMoji-color-glyf_colr_0.ttf differ diff --git a/test/data/Twemoji/LICENSE.md b/test/data/Twemoji/LICENSE.md new file mode 100644 index 00000000..d7eb8ee1 --- /dev/null +++ b/test/data/Twemoji/LICENSE.md @@ -0,0 +1,39 @@ +## License for the Code + +Copyright 2016-2018, Mozilla Foundation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + + +## License for the Visual Design + +The Emoji art comes from [Twemoji](https://twitter.github.io/twemoji), +and is used and redistributed under the CC-BY-4.0 +[license terms](https://github.com/twitter/twemoji#license) +offered by the Twemoji project. + +### Creative Commons Attribution 4.0 International (CC BY 4.0) +https://creativecommons.org/licenses/by/4.0/ + +#### You are free to: +**Share** — copy and redistribute the material in any medium or format + +**Adapt** — remix, transform, and build upon the material for any purpose, even commercially. + +#### Under the following terms: +**Attribution** — You must give appropriate credit, provide a link to the license, +and indicate if changes were made. + +**No additional restrictions** — You may not apply legal terms or technological measures +that legally restrict others from doing anything the license permits. diff --git a/test/data/Twemoji/Twemoji.Mozilla.ttf b/test/data/Twemoji/Twemoji.Mozilla.ttf new file mode 100644 index 00000000..9f45178e Binary files /dev/null and b/test/data/Twemoji/Twemoji.Mozilla.ttf differ diff --git a/test/glyphs.js b/test/glyphs.js index 498004c1..b982db89 100644 --- a/test/glyphs.js +++ b/test/glyphs.js @@ -362,4 +362,195 @@ describe('glyphs', function () { assert.equal(glyph.bbox.maxY, 656); }); }); + + describe('CBDT glyphs', function () { + let notoEmojiPath = new URL('data/NotoColorEmoji/NotoColorEmoji.ttf', import.meta.url); + let font = fontkit.openSync(notoEmojiPath); + + it('should get a CBDTGlyph', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.equal(glyph.type, 'CBDT'); + }); + + it('should not crash on layout', function () { + assert.doesNotThrow(() => { + font.layout('😀'); + }); + }); + + it('should have an empty path', function () { + let glyph = font.glyphsForString('😀')[0]; + let svg = glyph.path.toSVG(); + assert.ok(svg.length <= 20, 'Expected minimal/empty path, got: ' + svg); + }); + + it('should have valid advance width', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.ok(glyph.advanceWidth > 0, 'Expected positive advance width'); + }); + + it('should get glyph by ID', function () { + let glyphId = font._cmapProcessor.lookup(0x1F600); + let glyph = font.getGlyph(glyphId); + assert.ok(glyph !== null, 'Expected non-null glyph for ID ' + glyphId); + }); + }); + + describe('CBDT glyphs (OpenMoji)', function () { + let font = fontkit.openSync(new URL('data/OpenMoji/OpenMoji-color-cbdt.ttf', import.meta.url)); + + it('should get a CBDTGlyph', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.equal(glyph.type, 'CBDT'); + }); + + it('should not crash on layout', function () { + assert.doesNotThrow(() => { + font.layout('😀'); + }); + }); + + it('should handle ZWJ sequences', function () { + let run = font.layout('👨‍👩‍👧‍👦'); + assert.ok(run.glyphs.length >= 1, 'Expected at least 1 glyph for ZWJ family'); + }); + + it('should handle skin tone modifiers', function () { + let run = font.layout('👋🏽'); + assert.ok(run.glyphs.length >= 1, 'Expected at least 1 glyph for skin tone emoji'); + }); + + it('should handle flag sequences', function () { + let run = font.layout('🇺🇸'); + assert.ok(run.glyphs.length >= 1, 'Expected at least 1 glyph for flag'); + }); + }); + + describe('COLR glyphs (Twemoji)', function () { + let font = fontkit.openSync(new URL('data/Twemoji/Twemoji.Mozilla.ttf', import.meta.url)); + + it('should get a COLRGlyph', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.equal(glyph.type, 'COLR'); + }); + + it('should not crash on layout', function () { + assert.doesNotThrow(() => { + font.layout('😀'); + }); + }); + + it('should have valid advance width', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.ok(glyph.advanceWidth > 0, 'Expected positive advance width'); + }); + + it('should handle ZWJ sequences', function () { + let run = font.layout('👨‍👩‍👧‍👦'); + assert.ok(run.glyphs.length >= 1, 'Expected at least 1 glyph for ZWJ family'); + }); + + it('should handle skin tone modifiers', function () { + let run = font.layout('👋🏽'); + assert.ok(run.glyphs.length >= 1, 'Expected at least 1 glyph for skin tone emoji'); + }); + + it('should handle flag sequences', function () { + let run = font.layout('🇺🇸'); + assert.ok(run.glyphs.length >= 1, 'Expected at least 1 glyph for flag'); + }); + }); + + describe('COLR glyphs (OpenMoji COLRv1+SVG)', function () { + let font = fontkit.openSync(new URL('data/OpenMoji/OpenMoji-color-colr1_svg.ttf', import.meta.url)); + + it('should get a COLRGlyph', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.equal(glyph.type, 'COLR'); + }); + + it('should not crash on layout', function () { + assert.doesNotThrow(() => { + font.layout('😀'); + }); + }); + + it('should handle ZWJ sequences', function () { + let run = font.layout('👨‍👩‍👧‍👦'); + assert.ok(run.glyphs.length >= 1, 'Expected at least 1 glyph for ZWJ family'); + }); + }); + + describe('COLR v1 glyphs — null baseGlyphRecord (OpenMoji COLRv1)', function () { + let font = fontkit.openSync(new URL('data/OpenMoji/OpenMoji-color-colr1_svg.ttf', import.meta.url)); + + it('should return null layers for COLR v1 glyphs (no v0 baseGlyphRecord)', function () { + // COLR v1 uses paint-based records; fontkit only supports v0. + // The layers getter must return null instead of crashing with: + // TypeError: Cannot read properties of null (reading 'length') + let glyph = font.glyphsForString('😀')[0]; + assert.equal(glyph.type, 'COLR'); + assert.equal(glyph.layers, null); + }); + + it('should not crash when accessing path on COLR v1 glyph', function () { + let glyph = font.glyphsForString('😀')[0]; + // Accessing .path triggers _getContours internally + assert.doesNotThrow(() => { + let path = glyph.path; + }); + }); + }); + + describe('COLR v0 glyphs — composite glyph _getContours (OpenMoji COLRv0)', function () { + let font = fontkit.openSync(new URL('data/OpenMoji/OpenMoji-color-glyf_colr_0.ttf', import.meta.url)); + + it('should get COLR type glyph with layers', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.equal(glyph.type, 'COLR'); + assert.ok(glyph.layers != null, 'Expected non-null layers for COLR v0'); + assert.ok(glyph.layers.length > 0, 'Expected at least 1 layer'); + }); + + it('should not crash on layout (composite glyph resolution)', function () { + // OpenMoji COLR v0 has composite TTF glyphs whose components may + // reference glyph IDs that getGlyph() resolves as COLRGlyph. + // COLRGlyph has no _getContours(), so composite resolution + // must fall back to _getBaseGlyph() or skip gracefully. + // Without the fix this throws: + // TypeError: this._font.getGlyph(...)._getContours is not a function + assert.doesNotThrow(() => { + font.layout('😀🚀✅👨‍👩‍👧‍👦'); + }); + }); + + it('should render path without crashing', function () { + let run = font.layout('😀'); + for (let glyph of run.glyphs) { + assert.doesNotThrow(() => { + glyph.path; + }); + } + }); + }); + + describe('SBIX glyphs (Apple emoji)', function () { + let font = fontkit.openSync(new URL('data/ss-emoji/ss-emoji-apple.ttf', import.meta.url)); + + it('should get a SBIXGlyph', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.equal(glyph.type, 'SBIX'); + }); + + it('should not crash on layout', function () { + assert.doesNotThrow(() => { + font.layout('😀'); + }); + }); + + it('should have valid advance width', function () { + let glyph = font.glyphsForString('😀')[0]; + assert.ok(glyph.advanceWidth > 0, 'Expected positive advance width'); + }); + }); });