import { GameObjectBaseType, GameObjectData } from '@gedit/render-engine'; import { getBackgroundConfig, getVisible, setGameObjectBaseData, setGameObjectEffect, getFillGradient, } from '../utils'; import { fixTextWordWrap } from '../utils/text-wrap'; import { TextStyle } from '../utils/pixi-bug-fix'; import { PixiGameScene } from '../../common'; import type { ShadowSchema, SizeSchema } from '@gedit/utils'; import { ColorSchema, LineJoin } from '@gedit/canvas-draw'; import { PixiGameObjectBase } from '../pixi-base-game-object'; export interface TextSchema { fontFamily: string; fontSize: number; color: string; lineHeight: number; lineSpacing: number; letterSpacing: number; content: string; align: 'left' | 'right' | 'center' | 'justify'; verticalAlign: 'top' | 'middle' | 'bottom'; backgroundColor: string; bold: boolean; italic: boolean; shadow?: ShadowSchema; wordWrap?: boolean; // 是否自动换行 stroke?: string; lineJoin?: LineJoin; strokeThickness?: number; replaceMark?: string; roundPixels?: boolean; } export interface TextGameObjectData extends Required { [GameObjectBaseType.TEXT]: TextSchema; } let lockTextWrapFixed = false; export class TextGameObject extends PixiGameObjectBase< TextGameObjectData, PixiGameScene, PIXI.Container > { static type = GameObjectBaseType.TEXT; public text: PIXI.Text; protected textureCanvas: HTMLCanvasElement; protected fillCanvas: HTMLCanvasElement = document.createElement('canvas'); rendererObject: PIXI.Container = new PIXI.Container(); protected mask: PIXI.Graphics = new PIXI.Graphics(); protected oldSize: { width: number; height: number }; protected dataJSON: string; _width: number = 200; _height: number = 50; get width(): number { return this.data.size.changeWidth ? this.data.size.width : this._width || this.text.width; } set width(value: number) { this._width = value; this.text.width = value; } get height(): number { return this.data.size.changeHeight ? this.data.size.height : this._height || this.text.height; } set height(value: number) { this._height = value; this.text.height = value; } getOrigin(): { x: number; y: number } { return this.data.origin; } create(): void { // PIXI.Text.experimentalLetterSpacing = true; // PIXI.Text.nextLineHeightBehavior = true; if (!lockTextWrapFixed) { fixTextWordWrap(); lockTextWrapFixed = true; } this.text = new PIXI.Text({ text: '' }); // this.text.mask = this.mask; this.textureCanvas = this.scene.rootTextureCanvas; this.rendererObject.addChild(this.mask); this.rendererObject.addChild(this.text); // @ts-ignore this.rendererObject.isText = true; this.scene.container.addChild?.(this.rendererObject); super.create.apply(this, arguments); } dispose(): void { this.rendererObject?.destroy(); super.dispose(); } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } updateText(content: string): void { this.update({ ...this.data, text: { ...this.data.text, content, }, }); } update(data: TextGameObjectData): void { const text = this.text; if (!text) { return; } const textData = data.text; setGameObjectBaseData(this, data, false, true); const dataJSON = JSON.stringify({ text: textData, blendMode: data.blendMode, effect: data.effect, size: data.size, scale: data.scale, origin: data.origin, }); this.data = data; if (dataJSON !== this.dataJSON) { this.dataJSON = dataJSON; text.mask = null; const style = text.style as TextStyle; text.text = textData.content; style.fontFamily = textData.fontFamily; style.fontSize = textData.fontSize; // style.stroke = textData.color; style.fontWeight = textData.bold ? 'bold' : 'normal'; style.fontStyle = textData.italic ? 'italic' : 'normal'; style.align = textData.align; style.letterSpacing = textData.letterSpacing || 0; style.leading = textData.lineSpacing || 0; style.lineHeight = textData.lineHeight || textData.fontSize * 1.5; // 文字倾斜被截,需要 padding, https://github.com/pixijs/pixijs/issues/8969 // style.padding = (textData.italic ? textData.fontSize : 0); style.stroke = { color: textData.stroke ?? 'rgba(0,0,0,0)', join: textData.lineJoin || 'miter', width: textData.strokeThickness ?? 0, }; if (textData.shadow && textData.shadow.color) { const shadow = textData.shadow; // style.dropShadow = true; const { color, offsetX = 0, offsetY = 0, blur = 0 } = shadow; const colorAndAlpha = getBackgroundConfig(color); style.dropShadow = { color: colorAndAlpha.backgroundColor, alpha: colorAndAlpha.backgroundAlpha, blur, distance: Math.sqrt(offsetX * offsetX + offsetY * offsetY), angle: offsetX ? Math.atan(offsetY / offsetX) : Math.PI / 2, }; } else { style.dropShadow = false; } const distance = typeof style?.dropShadow === 'object' ? style.dropShadow?.distance ?? 0 : 0; const blur = typeof style.dropShadow === 'object' ? style.dropShadow?.blur ?? 0 : 0; // 只有 stroke 的宽会影响文字的实际宽高; const strokeWidth = style.stroke.width ?? 0; const shadowOffset = distance + blur; const offset = (style.padding ?? 0) + strokeWidth + shadowOffset + (style.fontStyle === 'italic' ? style.fontSize : 0); const { size, origin } = data; if (size.changeWidth) { style.wordWrap = true; // style.breakWords = true; // 初始文字长度 192 style.wordWrapWidth = size.width || 192; this._width = size.width || 192; } else { style.wordWrap = false; this._width = text.width - offset; } // const lineHeight = textData.lineHeight || textData.fontSize * 1.5; if (size.changeHeight) { this._height = size.height; } else { this._height = size.changeWidth || text.text.match('\n') ? Math.max(text.height - offset, text.style.lineHeight) : text.style.lineHeight; } // 效果设置给 text setGameObjectEffect(text, data); text.roundPixels = !!textData.roundPixels; const { verticalAlign = 'top' } = textData; // 文本的宽高,带上 padding 和描边等; // const textHeight = text.height; // const textWidth = text.width; // 实际文本的宽高; const textActualWidth = text.width - offset; const textActualHeight = text.height - offset; // mask 性能损耗很大;https://pixijs.io/guides/production/performance-tips.html this.mask.clear(); if (size.changeHeight && size.height < textActualHeight) { this.mask.rect( -origin.x * this._width, -origin.y * this._height, this._width, this._height ); this.mask.fill({ color: 0xfff000, alpha: 1, }); text.mask = this.mask; } // container 没有 anchor,手动定位; text.x = -(this._width ?? 50) * (origin.x ?? 0.5) - offset; // const lineLength = Math.floor((text.height - fontProperties.fontSize) / lineHeight); text.y = -(this._height ?? 50) * (origin.y ?? 0.5) - offset; if (typeof text.style.wordWrapWidth === 'number') { const widthDiff = text.style.wordWrapWidth - textActualWidth; if (widthDiff > 0 && style.wordWrap) { switch (textData.align) { case 'right': text.x += widthDiff; break; case 'center': text.x += widthDiff / 2; break; default: break; } } } if (textActualHeight && size.changeHeight) { switch (verticalAlign) { case 'middle': text.y += (size.height - textActualHeight) / 2; break; case 'bottom': text.y += size.height - textActualHeight; break; default: break; } } let fillOffsetY = verticalAlign === 'middle' ? -this._height / 2 + textActualHeight / 2 : 0; fillOffsetY = verticalAlign === 'bottom' ? -this._height + textActualHeight : fillOffsetY; let fillOffsetX = textData.align === 'right' ? this._width - textActualWidth : 0; fillOffsetX = textData.align === 'center' ? (this._width - textActualWidth) / 2 : fillOffsetX; const textBox = { ...size, x: text.x, y: text.y, offsetX: fillOffsetX, offsetY: fillOffsetY, textWidth: textActualWidth, textHeight: textActualHeight, }; try { const dataColor = JSON.parse(textData.color) as ColorSchema; if (dataColor.mode === 'fill') { style.fill = dataColor.colors[0].color; } else { style.fill = { fill: getFillGradient( this.fillCanvas, { ...data.size, x: fillOffsetX, y: fillOffsetY, }, dataColor, offset ), textBox, }; } } catch (e) { style.fill = textData.color || '#000'; } if (!size.changeWidth) { size.width = this._width; } if (!size.changeHeight) { size.height = this._height; } } this.rendererObject.visible = getVisible(data, this); super.update(data); } getRendererObject(): PIXI.Container | undefined { return this.rendererObject; } get rendererText(): PIXI.Text | undefined { return this.text; } getDisplaySize(): SizeSchema { return { width: this.width, height: this.height, }; } }