Skip to content

Commit

Permalink
fix: Fix issue with CFF Font
Browse files Browse the repository at this point in the history
This is a temporary fix, harfbuzz should be able to give CFF/TrueType info, we "just" need a way to figure this out
  • Loading branch information
romgere committed Jun 3, 2024
1 parent d394328 commit 2c5a515
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/services/font-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Service from '@ember/service';
import config from 'text2stl/config/environment';
import { inject as service } from '@ember/service';
import * as opentype from 'opentype.js';

import type HarfbuzzService from 'text2stl/services/harfbuzz';
import type { HBFont, HBFace } from 'harfbuzzjs/hbjs';
Expand Down Expand Up @@ -88,6 +89,7 @@ type GoogleFontApiResponse = {
export interface FaceAndFont {
font: HBFont;
face: HBFace;
opentype: opentype.Font; // This is only used to determine outline type (TrueType / CFF). TODO: find how to do that with harfbuzz.
}

export default class FontManagerService extends Service {
Expand Down Expand Up @@ -133,6 +135,8 @@ export default class FontManagerService extends Service {

fontCache: Record<string, FaceAndFont> = {};

opentype = opentype; // For easy mock

_emojiFont?: FaceAndFont;

get emojiFont() {
Expand Down Expand Up @@ -241,6 +245,7 @@ export default class FontManagerService extends Service {
return {
font: this.harfbuzz.hb.createFont(face),
face,
opentype: this.opentype.parse(buffer),
};
}

Expand Down
3 changes: 2 additions & 1 deletion app/services/text-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class TextMakerService extends Service {
// Following is only to manage "cff" font & detect hole shape
const paths2D: Path2D[] = [];
let path2D = new Path2D();

console.log(glyphPath);
// https://github.com/opentypejs/opentype.js#path-commands
for (let i = 0; i < glyphPath.length; i++) {
const command = glyphPath[i];
Expand Down Expand Up @@ -412,6 +412,7 @@ export default class TextMakerService extends Service {
lineText.glyphs[info.g],
x, // x offset
y - oy, // y offset
font.opentype.outlinesFormat === 'cff',
),
);
ox += spacing + info.ax;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"@esri/calcite-components": "^1.9.1",
"harfbuzzjs": "^0.3.4",
"matter-js": "^0.17.1",
"opentype.js": "^1.3.3",
"poly-decomp": "^0.3.0",
"three": "^0.137.4"
},
Expand Down
3 changes: 3 additions & 0 deletions tests/helpers/mock-font-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Service, { inject as service } from '@ember/service';
import loadFont from 'text2stl/tests/helpers/load-font';
import type { Font } from 'opentype.js';

import type { FaceAndFont } from 'text2stl/services/font-manager';

Expand All @@ -25,6 +26,7 @@ class FakeFontManager extends Service {
return {
font: this.harfbuzz.hb.createFont(face),
face,
opentype: { outlinesFormat: 'trueType' } as Font,
};
}

Expand All @@ -38,6 +40,7 @@ class FakeFontManager extends Service {
this.emojiFont = {
font: this.harfbuzz.hb.createFont(face),
face,
opentype: { outlinesFormat: 'trueType' } as Font,
};
}
}
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8943,6 +8943,14 @@ onetime@^5.1.0, onetime@^5.1.2:
dependencies:
mimic-fn "^2.1.0"

opentype.js@^1.3.3:
version "1.3.4"
resolved "https://registry.npmjs.org/opentype.js/-/opentype.js-1.3.4.tgz#1c0e72e46288473cc4a4c6a2dc60fd7fe6020d77"
integrity sha512-d2JE9RP/6uagpQAVtJoF0pJJA/fgai89Cc50Yp0EJHk+eLp6QQ7gBoblsnubRULNY132I0J1QKMJ+JTbMqz4sw==
dependencies:
string.prototype.codepointat "^0.2.1"
tiny-inflate "^1.0.3"

opter@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/opter/-/opter-1.1.0.tgz#2598aebb60b3f1a7322af1097086f4e798c84a76"
Expand Down Expand Up @@ -10524,6 +10532,11 @@ string-width@^2.1.0:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"

string.prototype.codepointat@^0.2.1:
version "0.2.1"
resolved "https://registry.npmjs.org/string.prototype.codepointat/-/string.prototype.codepointat-0.2.1.tgz#004ad44c8afc727527b108cd462b4d971cd469bc"
integrity sha512-2cBVCj6I4IOvEnjgO/hWqXjqBGsY+zwPmHl12Srk9IXSZ56Jwwmy+66XO5Iut/oQVR7t5ihYdLB0GMa4alEUcg==

string.prototype.matchall@^4.0.5, string.prototype.matchall@^4.0.6:
version "4.0.11"
resolved "https://registry.npmjs.org/string.prototype.matchall/-/string.prototype.matchall-4.0.11.tgz#1092a72c59268d2abaad76582dccc687c0297e0a"
Expand Down Expand Up @@ -10927,6 +10940,11 @@ [email protected]:
globalyzer "0.1.0"
globrex "^0.1.2"

tiny-inflate@^1.0.3:
version "1.0.3"
resolved "https://registry.npmjs.org/tiny-inflate/-/tiny-inflate-1.0.3.tgz#122715494913a1805166aaf7c93467933eea26c4"
integrity sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw==

tiny-lr@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/tiny-lr/-/tiny-lr-2.0.0.tgz#863659d7ce1ed201a117d8197d7f8b9a27bdc085"
Expand Down

0 comments on commit 2c5a515

Please sign in to comment.