optimize text baseline on runtime platform (#10751)

* optimize text baseline on runtime platform

* move into minigame module
This commit is contained in:
PP
2022-04-14 16:42:40 +08:00
committed by GitHub
parent 0a4c8f9620
commit a3f997dfda
3 changed files with 37 additions and 0 deletions
+18
View File
@@ -4,6 +4,7 @@ declare module 'pal/minigame' {
// platform related
wx?: WeChatAPI;
tt?: ByteDanceAPI;
ral?: RuntimeAPI;
// system
isDevTool: boolean;
@@ -82,6 +83,23 @@ declare module 'pal/minigame' {
deltaY: number;
deltaZ: number;
}
interface ICANVAS_CONTEXT2D_TEXTBASELINE_ALPHABETIC {
name: string,
enable: number,
}
interface ICANVAS_CONTEXT2D_TEXTBASELINE_DEFAULT {
name: string,
alphabetic: number,
}
interface RuntimeAPI {
CANVAS_CONTEXT2D_TEXTBASELINE_ALPHABETIC: ICANVAS_CONTEXT2D_TEXTBASELINE_ALPHABETIC,
CANVAS_CONTEXT2D_TEXTBASELINE_DEFAULT: ICANVAS_CONTEXT2D_TEXTBASELINE_DEFAULT,
getFeaturePropertyInt(featureName: string): number;
setFeaturePropertyInt(featureName: string, value: number);
}
interface ByteDanceAPI {
getAudioContext?: () => AudioContext;
}
+18
View File
@@ -25,12 +25,30 @@
*/
import { RUNTIME_BASED } from 'internal:constants';
import { minigame } from 'pal/minigame';
import Pool from '../../core/utils/pool';
export const BASELINE_RATIO = 0.26;
let _BASELINE_OFFSET = 0;
if (RUNTIME_BASED) {
_BASELINE_OFFSET = BASELINE_RATIO * 2 / 3;
const ral = minigame.ral!;
const featureAlphabeticeName = ral.CANVAS_CONTEXT2D_TEXTBASELINE_ALPHABETIC.name;
const featureAlphabeticEnable = ral.CANVAS_CONTEXT2D_TEXTBASELINE_ALPHABETIC.enable;
const defaultBaselineName = ral.CANVAS_CONTEXT2D_TEXTBASELINE_DEFAULT.name;
const defaultIsAlphaBetic = ral.CANVAS_CONTEXT2D_TEXTBASELINE_DEFAULT.alphabetic;
if (ral.getFeaturePropertyInt(featureAlphabeticeName) === featureAlphabeticEnable) {
// if support alphabetic baseline, set default baseline to alphabetic
ral.setFeaturePropertyInt(defaultBaselineName, defaultIsAlphaBetic);
if (ral.getFeaturePropertyInt(defaultBaselineName) === defaultIsAlphaBetic) {
// if default baseline has been successfully set to alphabetic, _BASELINE_OFFSET should be 0.
_BASELINE_OFFSET = 0;
}
}
}
export const MIDDLE_RATIO = (BASELINE_RATIO + 1) / 2 - BASELINE_RATIO;
export function getBaselineOffset () {
+1
View File
@@ -9,6 +9,7 @@ declare let ral: any;
// @ts-expect-error can't init minigame when it's declared
const minigame: IMiniGame = {};
cloneObject(minigame, ral);
minigame.ral = ral;
// #region SystemInfo
const systemInfo = minigame.getSystemInfoSync();