Skip to content

Commit

Permalink
label process string add
Browse files Browse the repository at this point in the history
  • Loading branch information
LinYunMo committed Sep 20, 2023
1 parent 909bb48 commit ee2fd7a
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 176 deletions.
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;
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);
123 changes: 80 additions & 43 deletions cocos/2d/assembler/label/bmfontUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { JSB } from 'internal:constants';
import { IConfig, FontAtlas } from '../../assets/bitmap-font';
import { SpriteFrame } from '../../assets/sprite-frame';
import { Rect } from '../../../core';
import { Rect, Vec2 } from '../../../core';
import { error } from '@base/debug';
import { Label, Overflow, CacheMode } from '../../components/label';
import { UITransform } from '../../framework/ui-transform';
Expand All @@ -40,34 +40,35 @@ 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;

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
style.originFontSize = _fntConfig ? _fntConfig.fontSize : 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;
layout.wrapping = false; // both
outputLayoutData.nodeContentSize.width += shareLabelInfo.margin * 2;
outputLayoutData.nodeContentSize.height += shareLabelInfo.margin * 2;
} else if (overflow === Overflow.RESIZE_HEIGHT) {
Expand All @@ -76,52 +77,82 @@ export const bmfontUtils = {
} else {
layout.wrapping = comp.enableWrapText;
}
outputRenderData.uiTransAnchorX = trans.anchorX;
outputRenderData.uiTransAnchorY = trans.anchorY;

shareLabelInfo.lineHeight = comp.lineHeight;
shareLabelInfo.fontSize = comp.fontSize;

style.spriteFrame = _spriteFrame;
style.fntConfig = _fntConfig;
style.fontFamily = shareLabelInfo.fontFamily;

style.color.set(comp.color);
style.fntConfig = _fntConfig; // layout only
style.fontFamily = shareLabelInfo.fontFamily; // layout only
},

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

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

if (comp.renderData.vertDirty) {
_comp = comp;
_uiTrans = _comp.node._uiProps.uiTransformComp!;
const renderData = comp.renderData;
// render Only
updateRenderProcessingData (
style: TextStyle,
outputRenderData: TextOutputRenderData,
comp: Label,
anchor: Readonly<Vec2>,
): void {
// render info
outputRenderData.uiTransAnchorX = anchor.x;
outputRenderData.uiTransAnchorY = anchor.y;

style.spriteFrame = _spriteFrame; // render only
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;
const outputRenderData = comp.textRenderData;
style.fontScale = view.getScaleX();
this._updateFontFamily(comp);

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

this._updateLabelInfo(comp);

style.fontDesc = shareLabelInfo.fontDesc;

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

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

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

if (comp.renderData.vertDirty) {
this.updateLayoutData(comp);// 需要注意的是要防止在两个函数中间被修改 // 但是这里的修改应该是不会影响到排版的
const renderData = comp.renderData;
const processing = TextProcessing.instance;
const style = comp.textStyle;
const layout = comp.textLayout;
const outputLayoutData = comp.textLayoutData;
const outputRenderData = comp.textRenderData;
const anchor = comp.node._uiProps.uiTransformComp!.anchorPoint;
this.updateRenderProcessingData(style, outputRenderData, comp, anchor);

// 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,
);

renderData.dataLength = outputRenderData.quadCount;
renderData.resize(renderData.dataLength, renderData.dataLength / 2 * 3);
Expand All @@ -134,13 +165,10 @@ export const bmfontUtils = {
this.createQuadIndices(indexCount);
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();
}
Expand Down Expand Up @@ -195,8 +223,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
Loading

0 comments on commit ee2fd7a

Please sign in to comment.