Skip to content

Commit

Permalink
remove shared label info
Browse files Browse the repository at this point in the history
  • Loading branch information
LinYunMo committed Sep 20, 2023
1 parent ee2fd7a commit 42222c1
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 297 deletions.
97 changes: 30 additions & 67 deletions cocos/2d/assembler/label/bmfontUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,21 @@
*/

import { JSB } from 'internal:constants';
import { IConfig, FontAtlas } from '../../assets/bitmap-font';
import { FontAtlas, BitmapFont } from '../../assets/bitmap-font';
import { SpriteFrame } from '../../assets/sprite-frame';
import { Rect, Vec2 } from '../../../core';
import { error } from '@base/debug';

Check failure on line 29 in cocos/2d/assembler/label/bmfontUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

`@base/debug` import should occur before import of `../../assets/bitmap-font`
import { Label, Overflow, CacheMode } from '../../components/label';
import { UITransform } from '../../framework/ui-transform';
import { LetterAtlas, shareLabelInfo } from './font-utils';
import { dynamicAtlasManager } from '../../utils/dynamic-atlas/atlas-manager';
import { TextProcessing } from './text-processing';
import { TextOutputLayoutData, TextOutputRenderData } from './text-output-data';
import { TextStyle } from './text-style';
import { TextLayout } from './text-layout';
import { view } from '../../../ui/view';

const _defaultLetterAtlas = new LetterAtlas(64, 64);
const _defaultFontAtlas = new FontAtlas(null);

let _fntConfig: IConfig | null = null;
let _spriteFrame: SpriteFrame|null = null;
let QUAD_INDICES;

export const bmfontUtils = {
Expand All @@ -55,7 +51,6 @@ export const bmfontUtils = {
): void {
style.fontSize = comp.fontSize; //both
style.actualFontSize = comp.fontSize; //both
style.originFontSize = _fntConfig ? _fntConfig.fontSize : comp.fontSize; //both
layout.horizontalAlign = comp.horizontalAlign; //both
layout.verticalAlign = comp.verticalAlign; //both
layout.spacingX = comp.spacingX; // layout only
Expand All @@ -69,20 +64,23 @@ export const bmfontUtils = {
// should wrap text
if (overflow === Overflow.NONE) {
layout.wrapping = false; // both
outputLayoutData.nodeContentSize.width += shareLabelInfo.margin * 2;
outputLayoutData.nodeContentSize.height += shareLabelInfo.margin * 2;
} else if (overflow === Overflow.RESIZE_HEIGHT) {
layout.wrapping = true;
outputLayoutData.nodeContentSize.height += shareLabelInfo.margin * 2;
} else {
layout.wrapping = comp.enableWrapText;
}
const fontAsset = comp.font as BitmapFont;
style.fntConfig = fontAsset.fntConfig; // layout only
style.originFontSize = fontAsset.fntConfig?.fontSize; //both
style.fontAtlas = fontAsset.fontDefDictionary;
if (!style.fontAtlas) {
style.fontAtlas = _defaultFontAtlas;
}

shareLabelInfo.lineHeight = comp.lineHeight;
shareLabelInfo.fontSize = comp.fontSize;
style.isOutlined = false;
style.outlineWidth = 0;

style.fntConfig = _fntConfig; // layout only
style.fontFamily = shareLabelInfo.fontFamily; // layout only
style.hash = '';
},

// render Only
Expand All @@ -96,33 +94,30 @@ export const bmfontUtils = {
outputRenderData.uiTransAnchorX = anchor.x;
outputRenderData.uiTransAnchorY = anchor.y;

style.spriteFrame = _spriteFrame; // render only
if (comp.font instanceof BitmapFont) {
const fontAsset = comp.font;
style.spriteFrame = fontAsset.spriteFrame;
dynamicAtlasManager.packToDynamicAtlas(comp, style.spriteFrame);
}
style.color.set(comp.color); // render only
},

// 进行统一调用
updateLayoutData (comp: Label): void {
if (comp.layoutDirty) {
const trans = comp.node._uiProps.uiTransformComp!;
const processing = TextProcessing.instance;
const style = comp.textStyle;
const layout = comp.textLayout;
const outputLayoutData = comp.textLayoutData;
style.fontScale = view.getScaleX();
this._updateFontFamily(comp);

this.updateLayoutProcessingData(style, layout, outputLayoutData, comp, trans);

this._updateLabelInfo(comp);
// Todo: dirtyFlag
const trans = comp.node._uiProps.uiTransformComp!;
const processing = TextProcessing.instance;
const style = comp.textStyle;
const layout = comp.textLayout;
const outputLayoutData = comp.textLayoutData;
style.fontScale = view.getScaleX();

style.fontDesc = shareLabelInfo.fontDesc;
this.updateLayoutProcessingData(style, layout, outputLayoutData, comp, trans);

// TextProcessing
processing.processingString(true, style, layout, outputLayoutData, comp.string);
// TextProcessing
processing.processingString(true, style, layout, outputLayoutData, comp.string);

comp.actualFontSize = style.actualFontSize;
trans.setContentSize(outputLayoutData.nodeContentSize);
}
comp.actualFontSize = style.actualFontSize;
trans.setContentSize(outputLayoutData.nodeContentSize);
},

updateRenderData (comp: Label): void {
Expand All @@ -131,7 +126,7 @@ export const bmfontUtils = {
}

if (comp.renderData.vertDirty) {
this.updateLayoutData(comp);// 需要注意的是要防止在两个函数中间被修改 // 但是这里的修改应该是不会影响到排版的
this.updateLayoutData(comp);// Todo: move to layout manager
const renderData = comp.renderData;
const processing = TextProcessing.instance;
const style = comp.textStyle;
Expand Down Expand Up @@ -169,8 +164,6 @@ export const bmfontUtils = {
this.updateColor(comp); // dirty need

renderData.vertDirty = false;

this._resetProperties();
}

if (comp.spriteFrame) {
Expand Down Expand Up @@ -288,37 +281,7 @@ export const bmfontUtils = {
dataList[dataOffset + 3].y = y;
},

_updateFontFamily (comp): void {
const fontAsset = comp.font;
_spriteFrame = fontAsset.spriteFrame;
_fntConfig = fontAsset.fntConfig;
shareLabelInfo.fontAtlas = fontAsset.fontDefDictionary;
if (!shareLabelInfo.fontAtlas) {
if (comp.cacheMode === CacheMode.CHAR) {
shareLabelInfo.fontAtlas = _defaultLetterAtlas;
} else {
shareLabelInfo.fontAtlas = _defaultFontAtlas;
}
}

dynamicAtlasManager.packToDynamicAtlas(comp, _spriteFrame);
// TODO update material and uv
},

_updateLabelInfo (comp): void {
// clear
shareLabelInfo.hash = '';
shareLabelInfo.margin = 0;
},

_resetProperties (): void {
_fntConfig = null;
_spriteFrame = null;
shareLabelInfo.hash = '';
shareLabelInfo.margin = 0;
},

createQuadIndices (indexCount): void {
createQuadIndices (indexCount: number): void {
if (indexCount % 6 !== 0) {
error('illegal index count!');
return;
Expand Down
70 changes: 23 additions & 47 deletions cocos/2d/assembler/label/font-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
THE SOFTWARE.
*/

import { ccwindow } from '@base/global';
import { FontAtlas } from '../../assets/bitmap-font';
import { Color, macro, ImageData } from '../../../core';
import { warn, warnID } from '@base/debug';

Check failure on line 28 in cocos/2d/assembler/label/font-utils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

`@base/debug` import should occur before import of `../../assets/bitmap-font`
Expand All @@ -30,7 +31,7 @@ import { PixelFormat } from '../../../asset/assets/asset-enum';
import { BufferTextureCopy } from '../../../gfx';
import { safeMeasureText, BASELINE_RATIO, MIDDLE_RATIO, getBaselineOffset } from '../../utils/text-utils';
import { director, Director } from '../../../game/director';
import { ccwindow } from '@base/global';
import { TextStyle } from './text-style';

export interface ISharedLabelData {
canvas: HTMLCanvasElement;
Expand Down Expand Up @@ -88,12 +89,8 @@ export class CanvasPool {

interface ILabelInfo {
fontSize: number;
lineHeight: number;
hash: string;
fontFamily: string;
fontDesc: string;
hAlign: number;
vAlign: number;
color: Color;
isOutlined: boolean;
out: Color;
Expand Down Expand Up @@ -131,10 +128,10 @@ class LetterTexture {
public height = 0;
public offsetY = 0;
public hash: string;
constructor (char: string, labelInfo: ILabelInfo) {
constructor (char: string, labelInfo: ILabelInfo, hash: string) {
this.char = char;
this.labelInfo = labelInfo;
this.hash = `${char.charCodeAt(0)}${labelInfo.hash}`;
this.hash = `${char.charCodeAt(0)}${hash}`;
}

public updateRenderData (): void {
Expand Down Expand Up @@ -400,11 +397,22 @@ export class LetterAtlas {
return this.fontDefDictionary.letterDefinitions[key];
}

public getLetterDefinitionForChar (char: string, labelInfo: ILabelInfo): any {
const hash = char.charCodeAt(0).toString() + labelInfo.hash;
public getLetterDefinitionForChar (char: string, style: TextStyle, fontScale: number): any {
const styleHash = style.hash;
const hash = char.charCodeAt(0).toString() + styleHash;
let letter = this.fontDefDictionary.letterDefinitions[hash];
if (!letter) {
const temp = new LetterTexture(char, labelInfo);
const sharedLabelData: ILabelInfo = {
fontSize: style.fontSize,
fontFamily: style.fontFamily,
fontDesc: style.fontDesc,
color: style.color.clone(),
isOutlined: style.isOutlined,
out: style.outlineColor.clone(),
margin: style.outlineWidth,
fontScale,
};
const temp = new LetterTexture(char, sharedLabelData, styleHash);
temp.updateRenderData();
letter = this.insertLetterTexture(temp);
temp.destroy();
Expand All @@ -414,45 +422,13 @@ export class LetterAtlas {
}
}

export interface IShareLabelInfo {
fontAtlas: FontAtlas | LetterAtlas | null;
fontSize: number;
lineHeight: number;
hAlign: number;
vAlign: number;
hash: string;
fontFamily: string;
fontDesc: string;
color: Color;
isOutlined: boolean;
out: Color;
margin: number;
fontScale: number;
}

export const shareLabelInfo: IShareLabelInfo = {
fontAtlas: null,
fontSize: 0,
lineHeight: 0,
hAlign: 0,
vAlign: 0,
hash: '',
fontFamily: '',
fontDesc: 'Arial',
color: Color.WHITE.clone(),
isOutlined: false,
out: Color.WHITE.clone(),
margin: 0,
fontScale: 1,
};

export function computeHash (labelInfo: IShareLabelInfo): string {
export function computeHash (color: Color, isOutlined: boolean, margin: number, outlineColor: Color, fontSize: number, fontFamily: string): string {
const hashData = '';
const color = labelInfo.color.toHEX();
const colorHex = color.toHEX();
let out = '';
if (labelInfo.isOutlined && labelInfo.margin > 0) {
out = out + labelInfo.margin.toString() + labelInfo.out.toHEX();
if (isOutlined && margin > 0) {
out = out + margin.toString() + outlineColor.toHEX();
}

return hashData + labelInfo.fontSize.toString() + labelInfo.fontFamily + color + out;
return hashData + fontSize.toString() + fontFamily + colorHex + out;
}
Loading

0 comments on commit 42222c1

Please sign in to comment.