Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复导出svg时设置的字体类型不生效 #438

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions packages/core/ServersPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,11 @@ class ServersPlugin {

saveSvg() {
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
const option = this._getSaveSvgOption();
const dataUrl = this.canvas.toSVG(option);
const { fontOption, svgOption } = this._getSaveSvgOption();
fabric.fontPaths = {
...fontOption,
};
const dataUrl = this.canvas.toSVG(svgOption);
const fileStr = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(dataUrl)}`;
this.editor.hooksEntity.hookSaveAfter.callAsync(fileStr, () => {
downFile(fileStr, 'svg');
Expand Down Expand Up @@ -266,15 +269,35 @@ class ServersPlugin {

_getSaveSvgOption() {
const workspace = this.canvas.getObjects().find((item) => item.id === 'workspace');
let fontFamilyArry = this.canvas
.getObjects()
.filter((item) => item.type == 'textbox')
.map((item) => (item as fabric.Text).fontFamily || '');
fontFamilyArry = Array.from(new Set(fontFamilyArry));

const fontList = this.editor.getPlugin('FontPlugin')?.cacheList ?? [];

const fontEntry: Record<string, string> = {};

for (const font of fontFamilyArry) {
if (font) {
const item = fontList.find((item: { name: string }) => item.name === font);
fontEntry[font] = item.file;
}
}

const { left, top, width, height } = workspace as fabric.Object;
return {
width,
height,
viewBox: {
x: left,
y: top,
fontOption: fontEntry,
svgOption: {
width,
height,
viewBox: {
x: left,
y: top,
width,
height,
},
},
};
}
Expand All @@ -283,6 +306,7 @@ class ServersPlugin {
const workspace = this.canvas
.getObjects()
.find((item: fabric.Object) => item.id === 'workspace');

const { left, top, width, height } = workspace as fabric.Object;
const option = {
name: 'New Image',
Expand Down
Loading