Skip to content

Commit 5250a3e

Browse files
AliceLannisteQiu-Jun
authored andcommitted
fix: 修复导出svg字体不生效
1 parent 67f7d20 commit 5250a3e

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

packages/core/ServersPlugin.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { fabric } from 'fabric';
1111
import Editor from './Editor';
1212
type IEditor = Editor;
1313
import { SelectEvent, SelectMode } from './eventType';
14+
import { off } from 'process';
15+
import { forIn } from 'lodash-es';
1416

1517
function transformText(objects: any) {
1618
if (!objects) return;
@@ -227,8 +229,11 @@ class ServersPlugin {
227229

228230
saveSvg() {
229231
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
230-
const option = this._getSaveSvgOption();
231-
const dataUrl = this.canvas.toSVG(option);
232+
const { fontOption, svgOption } = this._getSaveSvgOption();
233+
fabric.fontPaths = {
234+
...fontOption,
235+
};
236+
const dataUrl = this.canvas.toSVG(svgOption);
232237
const fileStr = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(dataUrl)}`;
233238
this.editor.hooksEntity.hookSaveAfter.callAsync(fileStr, () => {
234239
downFile(fileStr, 'svg');
@@ -263,15 +268,33 @@ class ServersPlugin {
263268

264269
_getSaveSvgOption() {
265270
const workspace = this.canvas.getObjects().find((item) => item.id === 'workspace');
271+
let fontFamilyArry = this.canvas
272+
.getObjects()
273+
.filter((item) => item.type == 'textbox')
274+
.map((item) => item.fontFamily);
275+
fontFamilyArry = Array.from(new Set(fontFamilyArry));
276+
277+
const fontList = this.editor.getPlugin('FontPlugin').cacheList;
278+
279+
const fontEntry = {};
280+
for (const font of fontFamilyArry) {
281+
const item = fontList.find((item) => item.name === font);
282+
fontEntry[font] = item.file;
283+
}
284+
285+
console.log('_getSaveSvgOption', fontEntry);
266286
const { left, top, width, height } = workspace as fabric.Object;
267287
return {
268-
width,
269-
height,
270-
viewBox: {
271-
x: left,
272-
y: top,
288+
fontOption: fontEntry,
289+
svgOption: {
273290
width,
274291
height,
292+
viewBox: {
293+
x: left,
294+
y: top,
295+
width,
296+
height,
297+
},
275298
},
276299
};
277300
}
@@ -280,6 +303,7 @@ class ServersPlugin {
280303
const workspace = this.canvas
281304
.getObjects()
282305
.find((item: fabric.Object) => item.id === 'workspace');
306+
console.log('getObjects', this.canvas.getObjects());
283307
const { left, top, width, height } = workspace as fabric.Object;
284308
const option = {
285309
name: 'New Image',

0 commit comments

Comments
 (0)