Skip to content

优化字体加载实现方式以便支持小游戏环境 #1806

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

Open
wants to merge 1 commit into
base: Master3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
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
53 changes: 3 additions & 50 deletions src/layaAir/laya/loaders/TTFFontLoader.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,9 @@
import { ILaya } from "../../ILaya";
import { LayaEnv } from "../../LayaEnv";
import { IResourceLoader, ILoadTask, Loader } from "../net/Loader";
import { URL } from "../net/URL";
import { Browser } from "../utils/Browser";
import { Utils } from "../utils/Utils";

const testString = "LayaTTFFont";

import { ILoadTask, IResourceLoader, Loader } from "../net/Loader";
class TTFFontLoader implements IResourceLoader {

load(task: ILoadTask) {
let fontName = Utils.replaceFileExtension(Utils.getBaseName(task.url), "");
if (LayaEnv.isConch) {
return task.loader.fetch(task.url, "arraybuffer").then(data => {
if (data)
(window as any)["conch"].registerFont(fontName, data);
return { family: fontName };
});
}
else if ((window as any).FontFace) {
let fontFace: any = new (window as any).FontFace(fontName, "url('" + URL.postFormatURL(URL.formatURL(task.url)) + "')");
(document as any).fonts.add(fontFace);
return fontFace.load().then(() => {
return fontFace;
});
}
else {
let fontTxt = "40px " + fontName;
let txtWidth = Browser.measureText(testString, fontTxt).width;

let fontStyle: any = Browser.createElement("style");
fontStyle.type = "text/css";
document.body.appendChild(fontStyle);
fontStyle.textContent = "@font-face { font-family:'" + fontName + "'; src:url('" + URL.postFormatURL(URL.formatURL(task.url)) + "');}";

return new Promise((resolve) => {
let checkComplete = () => {
if (Browser.measureText(testString, fontTxt).width != txtWidth)
complete();
};
let complete = () => {
ILaya.systemTimer.clear(this, checkComplete);
ILaya.systemTimer.clear(this, complete);

resolve({ family: fontName });
};

ILaya.systemTimer.once(10000, this, complete);
ILaya.systemTimer.loop(20, this, checkComplete);
});
}
return task.loader.fetch(task.url, "font", task.progress.createCallback(), task.options);
}
}

Loader.registerLoader(["ttf", "woff", "woff2", "otf"], TTFFontLoader, Loader.TTF);
Loader.registerLoader(["ttf", "woff", "woff2", "otf"], TTFFontLoader, Loader.TTF);
45 changes: 45 additions & 0 deletions src/layaAir/laya/net/Downloader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ILaya } from '../../ILaya';
import { Event } from "../events/Event";
import { Browser } from "../utils/Browser";
import { ImgUtils } from "../utils/ImgUtils";
import { Utils } from '../utils/Utils';
import { HttpRequest } from "./HttpRequest";
import { WorkerLoader } from "./WorkerLoader";

Expand Down Expand Up @@ -150,6 +152,49 @@ export class Downloader {
owner.$ref = audio; //保持引用避免gc掉
}

font(owner: any, url: string, originalUrl: string, onProgress: (progress: number) => void, onComplete: (data: any, error?: string) => void): void {
let fontName = Utils.replaceFileExtension(Utils.getBaseName(url), "");
if ((window as any).conch) {
this.common(owner, url, originalUrl, "arraybuffer", onProgress, (data, error) => {
if (error || !data) {
onComplete(null, error);
return;
}
(window as any).conch.registerFont(fontName, data);
onComplete({ family: fontName });
});
} else if ((window as any).FontFace) {
let fontFace: any = new (window as any).FontFace(fontName, "url('" + url + "')");
fontFace.load()
.catch((err: Error) => onComplete(null, err.message))
.then(() => {
(document as any).fonts.add(fontFace);
onComplete(fontFace);
});
} else {
const testString = "LayaTTFFont";
let fontTxt = "40px " + fontName;
let txtWidth = Browser.measureText(testString, fontTxt).width;

let fontStyle: any = Browser.createElement("style");
fontStyle.type = "text/css";
document.body.appendChild(fontStyle);
fontStyle.textContent = "@font-face { font-family:'" + fontName + "'; src:url('" + url + "');}";

let checkComplete = () => {
if (Browser.measureText(testString, fontTxt).width != txtWidth)
complete();
};
let complete = () => {
ILaya.systemTimer.clear(this, checkComplete);
ILaya.systemTimer.clear(this, complete);
onComplete({ family: fontName });
};
ILaya.systemTimer.once(10000, this, complete);
ILaya.systemTimer.loop(20, this, checkComplete);
}
}

/**
* @en Pool of HttpRequest instances.
* @zh HttpRequest实例池。
Expand Down
9 changes: 7 additions & 2 deletions src/layaAir/laya/net/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ interface ContentTypeMap {
"xml": XML,
"arraybuffer": ArrayBuffer,
"image": HTMLImageElement | ImageBitmap,
"sound": HTMLAudioElement
"sound": HTMLAudioElement,
"font": FontFace,
}

var typeIdCounter = 0;
Expand Down Expand Up @@ -751,6 +752,10 @@ export class Loader extends EventDispatcher {
Loader.downloader.audio(item, url, item.originalUrl, item.onProgress, (data: any, error: string) =>
this.completeItem(item, data, error));
}
else if (item.contentType == "font") {
Loader.downloader.font(item, url, item.originalUrl, item.onProgress, (data: any, error: string) =>
this.completeItem(item, data, error));
}
else {
let preloadedContent = Loader.preLoadedMap[item.url];
if (preloadedContent) {
Expand Down Expand Up @@ -1487,4 +1492,4 @@ interface DownloadItem {
startTime?: number;
onComplete: (content: any) => void;
onProgress: ProgressCallback;
}
}