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

label processing string & remove shared label info #16292

Open
wants to merge 4 commits into
base: develop
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
62 changes: 0 additions & 62 deletions cocos/2d/assembler/label/bmfont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,68 +50,6 @@ export const bmfont: IAssembler = {
// Fill All
fillMeshVertices3D(node, renderer, comp.renderData!, tempColor);
},

appendQuad (comp: Label, spriteFrame: SpriteFrame, rect: Rect, rotated: boolean, x: number, y: number, scale: number) {
const renderData = comp.renderData;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没用了么?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重复函数

if (!renderData) {
return;
}

const dataOffset = renderData.dataLength;

renderData.dataLength += 4;
renderData.resize(renderData.dataLength, renderData.dataLength / 2 * 3);

const dataList = renderData.data;
const texW = spriteFrame.width;
const texH = spriteFrame.height;

const rectWidth = rect.width;
const rectHeight = rect.height;

let l = 0;
let b = 0;
let t = 0;
let r = 0;
if (!rotated) {
l = (rect.x) / texW;
r = (rect.x + rectWidth) / texW;
b = (rect.y + rectHeight) / texH;
t = (rect.y) / texH;

dataList[dataOffset].u = l;
dataList[dataOffset].v = b;
dataList[dataOffset + 1].u = r;
dataList[dataOffset + 1].v = b;
dataList[dataOffset + 2].u = l;
dataList[dataOffset + 2].v = t;
dataList[dataOffset + 3].u = r;
dataList[dataOffset + 3].v = t;
} else {
l = (rect.x) / texW;
r = (rect.x + rectHeight) / texW;
b = (rect.y + rectWidth) / texH;
t = (rect.y) / texH;

dataList[dataOffset].u = l;
dataList[dataOffset].v = t;
dataList[dataOffset + 1].u = l;
dataList[dataOffset + 1].v = b;
dataList[dataOffset + 2].u = r;
dataList[dataOffset + 2].v = t;
dataList[dataOffset + 3].u = r;
dataList[dataOffset + 3].v = b;
}

dataList[dataOffset].x = x;
dataList[dataOffset].y = y - rectHeight * scale;
dataList[dataOffset + 1].x = x + rectWidth * scale;
dataList[dataOffset + 1].y = y - rectHeight * scale;
dataList[dataOffset + 2].x = x;
dataList[dataOffset + 2].y = y;
dataList[dataOffset + 3].x = x + rectWidth * scale;
dataList[dataOffset + 3].y = y;
},
};

js.addon(bmfont, bmfontUtils);
178 changes: 89 additions & 89 deletions cocos/2d/assembler/label/bmfontUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,104 +24,130 @@

import { JSB } from 'internal:constants';
import { error } from '@base/debug';
import { IConfig, FontAtlas } from '../../assets/bitmap-font';
import { FontAtlas, BitmapFont } from '../../assets/bitmap-font';
import { SpriteFrame } from '../../assets/sprite-frame';
import { Rect } from '../../../core';
import { Rect, Vec2 } from '../../../core';
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 _comp: Label | null = null;
let _uiTrans: UITransform | null = null;

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

export const bmfontUtils = {

updateProcessingData (style: TextStyle, layout: TextLayout,
outputLayoutData: TextOutputLayoutData, outputRenderData: TextOutputRenderData,
comp: Label, trans: UITransform): void {
style.fontSize = comp.fontSize;
style.actualFontSize = comp.fontSize;
style.originFontSize = _fntConfig ? _fntConfig.fontSize : comp.fontSize;
layout.horizontalAlign = comp.horizontalAlign;
layout.verticalAlign = comp.verticalAlign;
layout.spacingX = comp.spacingX;
updateLayoutProcessingData (
style: TextStyle,
layout: TextLayout,
outputLayoutData: TextOutputLayoutData,
comp: Label,
trans: UITransform,
): void {
style.fontSize = comp.fontSize; //both
style.actualFontSize = comp.fontSize; //both
layout.horizontalAlign = comp.horizontalAlign; //both
layout.verticalAlign = comp.verticalAlign; //both
layout.spacingX = comp.spacingX; // layout only
const overflow = comp.overflow;
layout.overFlow = overflow;
layout.lineHeight = comp.lineHeight;

outputLayoutData.nodeContentSize.width = trans.width;
outputLayoutData.nodeContentSize.height = trans.height;
layout.overFlow = overflow; // both
layout.lineHeight = comp.lineHeight; // both

// should wrap text
if (overflow === Overflow.NONE) {
layout.wrapping = false;
outputLayoutData.nodeContentSize.width += shareLabelInfo.margin * 2;
outputLayoutData.nodeContentSize.height += shareLabelInfo.margin * 2;
layout.wrapping = false; // both
} else if (overflow === Overflow.RESIZE_HEIGHT) {
layout.wrapping = true;
outputLayoutData.nodeContentSize.height += shareLabelInfo.margin * 2;
} else {
layout.wrapping = comp.enableWrapText;
}
outputRenderData.uiTransAnchorX = trans.anchorX;
outputRenderData.uiTransAnchorY = trans.anchorY;
const fontAsset = comp.font as BitmapFont;
style.fntConfig = fontAsset.fntConfig; // layout only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该直接把 font 传下来,不应该拆成多个传递

style.originFontSize = fontAsset.fntConfig?.fontSize; //both
style.fontAtlas = fontAsset.fontDefDictionary;
if (!style.fontAtlas) {
style.fontAtlas = _defaultFontAtlas;
}

style.isOutlined = false;
style.outlineWidth = 0;

style.hash = '';
},

// render Only
updateRenderProcessingData (
style: TextStyle,
outputRenderData: TextOutputRenderData,
comp: Label,
anchor: Readonly<Vec2>,
): void {
// render info
outputRenderData.uiTransAnchorX = anchor.x;
outputRenderData.uiTransAnchorY = anchor.y;

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 {
// 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();

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

style.spriteFrame = _spriteFrame;
style.fntConfig = _fntConfig;
style.fontFamily = shareLabelInfo.fontFamily;
// TextProcessing
processing.processingString(true, style, layout, outputLayoutData, comp.string);

style.color.set(comp.color);
comp.actualFontSize = style.actualFontSize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

组件上为什么要缓存这个变量

trans.setContentSize(outputLayoutData.nodeContentSize);
},

updateRenderData (comp: Label): void {
if (!comp.renderData) {
return;
}

if (_comp === comp) { return; }

if (comp.renderData.vertDirty) {
_comp = comp;
_uiTrans = _comp.node._uiProps.uiTransformComp!;
this.updateLayoutData(comp);// Todo: move to layout manager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新渲染时,需要先更新布局?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要,只是在此PR中未进行流程分离,在流程分离 PR 中移除了此步

const renderData = comp.renderData;

const processing = TextProcessing.instance;
const style = comp.textStyle;
const layout = comp.textLayout;
const outputLayoutData = comp.textLayoutData;
const outputRenderData = comp.textRenderData;
style.fontScale = view.getScaleX();
this._updateFontFamily(comp);

this.updateProcessingData(style, layout, outputLayoutData, outputRenderData, comp, _uiTrans);
const anchor = comp.node._uiProps.uiTransformComp!.anchorPoint;
this.updateRenderProcessingData(style, outputRenderData, comp, anchor);

this._updateLabelInfo(comp);

style.fontDesc = shareLabelInfo.fontDesc;

// TextProcessing
processing.processingString(true, style, layout, outputLayoutData, comp.string);
// generateVertex
this.resetRenderData(comp);
outputRenderData.quadCount = 0;
processing.generateRenderInfo(true, style, layout, outputLayoutData, outputRenderData,
comp.string, this.generateVertexData);
processing.generateRenderInfo(
true,
style,
layout,
outputLayoutData,
outputRenderData,
comp.string,
this.generateVertexData,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么会需要回调?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于在两种类型中,顶点处理方式无法统一,所以使用回调函数

);

renderData.dataLength = outputRenderData.quadCount;
renderData.resize(renderData.dataLength, renderData.dataLength / 2 * 3);
Expand All @@ -132,17 +158,12 @@ export const bmfontUtils = {

const indexCount = renderData.indexCount;
this.createQuadIndices(indexCount);
renderData.chunk.setIndexBuffer(QUAD_INDICES);
renderData.chunk.setIndexBuffer(QUAD_INDICES!);

_comp.actualFontSize = style.actualFontSize;
_uiTrans.setContentSize(outputLayoutData.nodeContentSize);
this.updateUVs(comp);// dirty need
this.updateColor(comp); // dirty need

renderData.vertDirty = false;
_comp = null;

this._resetProperties();
}

if (comp.spriteFrame) {
Expand Down Expand Up @@ -195,8 +216,17 @@ export const bmfontUtils = {
},

// callBack function
generateVertexData (style: TextStyle, outputLayoutData: TextOutputLayoutData, outputRenderData: TextOutputRenderData, offset: number,
spriteFrame: SpriteFrame, rect: Rect, rotated: boolean, x: number, y: number): void {
generateVertexData (
style: TextStyle,
outputLayoutData: TextOutputLayoutData,
outputRenderData: TextOutputRenderData,
offset: number,
spriteFrame: SpriteFrame,
rect: Rect,
rotated: boolean,
x: number,
y: number,
): void {
const dataOffset = offset;
const scale = style.bmfontScale;

Expand Down Expand Up @@ -251,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
Loading
Loading