Skip to content
This repository was archived by the owner on Jul 13, 2024. It is now read-only.

Commit 5fccc9c

Browse files
committed
#24 font testing
1 parent 9d8a558 commit 5fccc9c

File tree

4 files changed

+82
-35
lines changed

4 files changed

+82
-35
lines changed

packages/dtif-to-svg/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"author": "@bennodev19",
2020
"license": "MIT",
2121
"dependencies": {
22-
"d3-selection": "^3.0.0"
22+
"@pda/openapi-fetch": "workspace:^",
23+
"@remotion/paths": "^4.0.6",
24+
"d3-selection": "^3.0.0",
25+
"opentype.js": "^1.3.4",
26+
"webfontloader": "^1.6.28"
2327
},
2428
"peerDependencies": {
2529
"jsdom": "^22.1.0"
@@ -31,6 +35,7 @@
3135
"@types/d3-selection": "^3.0.5",
3236
"@types/jest": "^29.5.1",
3337
"@types/node": "^18.15.13",
38+
"@types/opentype.js": "^1.3.4",
3439
"eslint": "^8.38.0",
3540
"eslint-config-pda-base": "workspace:*",
3641
"jest": "^29.5.0",

packages/dtif-to-svg/src/__tests__/dtif-to-svg.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { dtifToSVG } from '..';
1+
import { renderText } from '..';
22

33
describe('dtif-to-svg library tests', () => {
44
it('should work', async () => {
55
// Arrange
66
const dtif = '';
77

88
// Act
9-
const result = await dtifToSVG();
9+
console.time();
10+
const result = await renderText(
11+
'http://fonts.gstatic.com/s/poppins/v20/pxiEyp8kv8JHgFVrFJDUc1NECPY.ttf',
12+
'From',
13+
96
14+
);
15+
console.timeEnd();
16+
17+
console.log(result);
1018

1119
// Assert
1220
expect(result).toBe('');

packages/dtif-to-svg/src/index.ts

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1+
import { RawFetchClientThrow } from '@pda/openapi-fetch';
12
import { d3 } from './d3';
23

3-
export async function dtifToSVG() {
4-
const svg = (await d3())
5-
.select('body')
6-
.append('svg')
7-
.attr('width', 500)
8-
.attr('height', 500);
9-
10-
const defs = svg.append('defs');
11-
12-
const gradient = defs
13-
.append('linearGradient')
14-
.attr('id', 'gradient')
15-
.attr('x1', '0%')
16-
.attr('y1', '0%')
17-
.attr('x2', '100%')
18-
.attr('y2', '100%');
19-
20-
gradient.append('stop').attr('offset', '0%').attr('stop-color', 'blue');
21-
22-
gradient.append('stop').attr('offset', '100%').attr('stop-color', 'red');
23-
24-
for (let i = 0; i < 100; i++) {
25-
svg
26-
.append('rect')
27-
.attr('x', i * 5)
28-
.attr('y', i * 5)
29-
.attr('width', 50)
30-
.attr('height', 50)
31-
.attr('fill', 'url(#gradient)');
32-
}
33-
34-
return (await d3()).select('body').html();
4+
import * as opentype from 'opentype.js';
5+
6+
const fetchClient = new RawFetchClientThrow();
7+
8+
async function loadFont(url: string): Promise<opentype.Font> {
9+
const buffer = await fetchClient.getThrow<ArrayBuffer>(url, {
10+
parseAs: 'arrayBuffer',
11+
});
12+
return opentype.parse(buffer);
13+
}
14+
15+
export async function renderText(url: string, input: string, fontSize = 72) {
16+
const font = await loadFont(url);
17+
18+
const textSvg = font.getPath(input, 0, 150, fontSize);
19+
const path = textSvg.toPathData(2);
20+
const textBB = textSvg.getBoundingBox();
21+
const width = textBB.x2 - textBB.x1;
22+
const height = textBB.y2 - textBB.y1;
23+
24+
const svg = (await d3()).create('svg').attr('viewBox', [0, 0, width, height]);
25+
26+
svg.append('path').attr('d', path);
27+
28+
return svg.node()?.outerHTML;
3529
}

pnpm-lock.yaml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)