Skip to content

Commit bc6b56b

Browse files
committed
feat: Refactor the setFont and registerFont functions
1 parent 98c6060 commit bc6b56b

File tree

7 files changed

+54
-76
lines changed

7 files changed

+54
-76
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v6.1.2
2+
* Add FFChart component, support most demos of echarts.js. Data visualization video can be made
3+
* Support the synthesis of dynamic data change charts.
4+
* Fix the problem of setScale initialization.
5+
* Refactor the setFont and registerFont functions.
6+
17
## v5.5.6
28
* FFVtuber supports dual types of sequence frame and video.
39
* Delete the addVtuber method of FFCreator.

lib/node/chart.js

+4-25
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
*/
2222
const echarts = require('echarts');
2323
const FFImage = require('./image');
24-
const Utils = require('../utils/utils');
24+
const CanvasUtil = require('../utils/canvas');
2525
const TimelineUpdate = require('../timeline/update');
26-
const { createCanvas, registerFont, Texture } = require('inkpaint');
26+
const { createCanvas, Texture } = require('inkpaint');
2727

2828
const echartsPolyfill = () => {
2929
echarts.Model.prototype.isAnimationEnabled = () => true;
@@ -71,29 +71,8 @@ class FFChart extends FFImage {
7171
* @param {string} font - text font file path
7272
* @public
7373
*/
74-
setFont(fontFamily) {
75-
if (/.*\.(ttf|otf|svg|woff|woff2|eot)$/gi.test(fontFamily)) {
76-
this.setFontByFile(fontFamily);
77-
} else {
78-
const { ctx } = this;
79-
ctx.font = fontFamily;
80-
}
81-
}
82-
83-
/**
84-
* Set font if the font is a file
85-
* @param {string} file - text font file path
86-
* @private
87-
*/
88-
setFontByFile(file) {
89-
const fontFamily = 'f' + Utils.toHash(file);
90-
if (!Utils.storage[fontFamily]) {
91-
registerFont(file, { family: fontFamily });
92-
Utils.storage[fontFamily] = true;
93-
}
94-
95-
const { ctx } = this;
96-
ctx.font = fontFamily;
74+
setFont(font) {
75+
CanvasUtil.setFont(font, fontFamily => (this.ctx.font = fontFamily));
9776
}
9877

9978
/**

lib/node/subtitle.js

+6-24
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const probe = require('ffmpeg-probe');
1616
const forEach = require('lodash/forEach');
1717
const FFTween = require('../animate/tween');
1818
const DateUtil = require('../utils/date');
19-
const TimelineUpdate = require('../timeline/update');
19+
const CanvasUtil = require('../utils/canvas');
2020
const Materials = require('../utils/materials');
21-
const Utils = require('../utils/utils');
22-
const { registerFont, ProxyObj, Text } = require('inkpaint');
21+
const TimelineUpdate = require('../timeline/update');
22+
const { ProxyObj, Text } = require('inkpaint');
2323

2424
const DIS = 40;
2525
const SIGN = '_$_';
@@ -146,29 +146,11 @@ class FFSubtitle extends FFNode {
146146

147147
/**
148148
* Set text font file path
149-
* @param {string} file - text font file path
150-
* @public
151-
*/
152-
setFont(fontFamily) {
153-
if (/.*\.(ttf|otf|svg|woff|woff2|eot)$/gi.test(fontFamily)) {
154-
this.setFontByFile(fontFamily);
155-
} else {
156-
this.setStyle({ fontFamily });
157-
}
158-
}
159-
160-
/**
161-
* Set font if the font is a file
162-
* @param {string} file - text font file path
149+
* @param {string} font - text font file path
163150
* @public
164151
*/
165-
setFontByFile(file) {
166-
const fontFamily = 'f' + Utils.toHash(file);
167-
if (!Utils.storage[fontFamily]) {
168-
registerFont(file, { family: fontFamily });
169-
Utils.storage[fontFamily] = true;
170-
}
171-
this.setStyle({ fontFamily });
152+
setFont(font) {
153+
CanvasUtil.setFont(font, fontFamily => this.setStyle({ fontFamily }));
172154
}
173155

174156
setStartTime(startTime = 0) {

lib/node/text.js

+5-24
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* @class
1515
*/
1616
const FFNode = require('./node');
17-
const Utils = require('../utils/utils');
1817
const isArray = require('lodash/isArray');
19-
const { registerFont, ProxyObj, Text } = require('inkpaint');
18+
const CanvasUtil = require('../utils/canvas');
19+
const { ProxyObj, Text } = require('inkpaint');
2020

2121
class FFText extends FFNode {
2222
constructor(conf = { text: '', style: { fontSize: 28 } }) {
@@ -65,30 +65,11 @@ class FFText extends FFNode {
6565

6666
/**
6767
* Set text font file path
68-
* @param {string} file - text font file path
68+
* @param {string} font - text font file path
6969
* @public
7070
*/
71-
setFont(fontFamily) {
72-
if (/.*\.(ttf|otf|svg|woff|woff2|eot)$/gi.test(fontFamily)) {
73-
this.setFontByFile(fontFamily);
74-
} else {
75-
this.setStyle({ fontFamily });
76-
}
77-
}
78-
79-
/**
80-
* Set font if the font is a file
81-
* @param {string} file - text font file path
82-
* @public
83-
*/
84-
setFontByFile(file) {
85-
const fontFamily = 'f' + Utils.toHash(file);
86-
if (!Utils.storage[fontFamily]) {
87-
registerFont(file, { family: fontFamily });
88-
Utils.storage[fontFamily] = true;
89-
}
90-
91-
this.setStyle({ fontFamily });
71+
setFont(font) {
72+
CanvasUtil.setFont(font, fontFamily => this.setStyle({ fontFamily }));
9273
}
9374

9475
/**

lib/utils/canvas.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
*
1010
* @object
1111
*/
12-
13-
const { createCanvas, createImageData } = require('inkpaint');
12+
const FS = require('./fs');
13+
const Utils = require('./utils');
14+
const { createCanvas, createImageData, registerFont } = require('inkpaint');
1415

1516
const CanvasUtil = {
1617
canvas: null,
@@ -85,6 +86,26 @@ const CanvasUtil = {
8586
return canvas;
8687
},
8788

89+
/**
90+
* Set fonts and register new font files
91+
* @param {string} font - text font file path
92+
* @param {function} setFontFunc - Callback hook for element setting font
93+
* @public
94+
*/
95+
setFont(font, setFontFunc) {
96+
if (FS.isFont(font)) {
97+
const fontFamily = 'f' + Utils.toHash(font);
98+
if (!Utils.storage[fontFamily]) {
99+
registerFont(font, { family: fontFamily });
100+
Utils.storage[fontFamily] = true;
101+
}
102+
103+
setFontFunc(fontFamily);
104+
} else {
105+
setFontFunc(font);
106+
}
107+
},
108+
88109
fillRect({ canvas, color }) {
89110
const context = canvas.getContext('2d');
90111
context.fillStyle = color;

lib/utils/fs.js

+9
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ const FS = {
118118
getCacheFilePath(dir) {
119119
return path.join(dir, `%012d.${this.format}`);
120120
},
121+
122+
/**
123+
* Check if it is a font file
124+
* @param {string} font - input file
125+
* @public
126+
*/
127+
isFont(font) {
128+
return /.*\.(ttf|otf|svg|woff|woff2|eot)$/gi.test(font);
129+
},
121130
};
122131

123132
module.exports = FS;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ffcreator",
3-
"version": "6.0.5",
3+
"version": "6.1.2",
44
"description": "FFCreator is a lightweight and flexible short video production library",
55
"main": "lib/index.js",
66
"types": "types/index.d.ts",

0 commit comments

Comments
 (0)