@leafer-ui/text 1.0.0-beta.9 → 1.0.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -5
- package/src/TextConvert.ts +7 -5
- package/src/TextLayout.ts +29 -16
- package/src/TextRows.ts +14 -3
- package/types/index.d.ts +5 -0
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer-ui/text",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-rc.3",
|
|
4
4
|
"description": "@leafer-ui/text",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "src/index.ts",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
8
9
|
"files": [
|
|
9
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"types",
|
|
12
|
+
"dist"
|
|
10
13
|
],
|
|
11
14
|
"repository": {
|
|
12
15
|
"type": "git",
|
|
@@ -19,10 +22,10 @@
|
|
|
19
22
|
"leaferjs"
|
|
20
23
|
],
|
|
21
24
|
"dependencies": {
|
|
22
|
-
"@leafer-ui/core": "1.0.0-
|
|
25
|
+
"@leafer-ui/core": "1.0.0-rc.3"
|
|
23
26
|
},
|
|
24
27
|
"devDependencies": {
|
|
25
|
-
"@leafer/interface": "1.0.0-
|
|
26
|
-
"@leafer-ui/interface": "1.0.0-
|
|
28
|
+
"@leafer/interface": "1.0.0-rc.3",
|
|
29
|
+
"@leafer-ui/interface": "1.0.0-rc.3"
|
|
27
30
|
}
|
|
28
31
|
}
|
package/src/TextConvert.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MathHelper, Platform } from '@leafer/core'
|
|
2
2
|
|
|
3
|
-
import { ITextData, ITextDrawData } from '@leafer-ui/interface'
|
|
3
|
+
import { ITextConvertModule, ITextData, ITextDrawData } from '@leafer-ui/interface'
|
|
4
4
|
|
|
5
5
|
import { createRows } from './TextRows'
|
|
6
6
|
import { layoutChar } from './CharLayout'
|
|
@@ -9,16 +9,18 @@ import { clipText } from './TextClip'
|
|
|
9
9
|
import { decorationText } from './TextDecoration'
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
export const TextConvert = {
|
|
12
|
+
export const TextConvert: ITextConvertModule = {
|
|
13
13
|
|
|
14
14
|
getDrawData(content: string, style: ITextData): ITextDrawData {
|
|
15
15
|
|
|
16
16
|
if (typeof content !== 'string') content = String(content)
|
|
17
17
|
|
|
18
18
|
let x = 0, y = 0
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
|
|
20
|
+
let width = style.__getInput('width') || 0
|
|
21
|
+
let height = style.__getInput('height') || 0
|
|
22
|
+
|
|
23
|
+
const { textDecoration, textOverflow, __font, padding } = style
|
|
22
24
|
|
|
23
25
|
if (padding) {
|
|
24
26
|
const [top, right, bottom, left] = MathHelper.fourNumber(padding)
|
package/src/TextLayout.ts
CHANGED
|
@@ -4,32 +4,30 @@ import { ITextData, ITextDrawData, ITextRowData } from '@leafer-ui/interface'
|
|
|
4
4
|
export function layoutText(drawData: ITextDrawData, style: ITextData): void {
|
|
5
5
|
|
|
6
6
|
const { rows, bounds } = drawData
|
|
7
|
-
const { __lineHeight, __baseLine, textAlign, verticalAlign, paraSpacing, textOverflow } = style
|
|
7
|
+
const { __lineHeight, __baseLine, __letterSpacing, textAlign, verticalAlign, paraSpacing, textOverflow } = style
|
|
8
8
|
|
|
9
9
|
let { x, y, width, height } = bounds, realHeight = __lineHeight * rows.length + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0)
|
|
10
10
|
let starY: number = __baseLine
|
|
11
11
|
|
|
12
12
|
// verticalAlign
|
|
13
13
|
|
|
14
|
-
if (height) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
y += (height - realHeight)
|
|
25
|
-
}
|
|
14
|
+
if (textOverflow !== 'show' && realHeight > height) {
|
|
15
|
+
realHeight = Math.max(height, __lineHeight)
|
|
16
|
+
drawData.overflow = rows.length
|
|
17
|
+
} else {
|
|
18
|
+
switch (verticalAlign) {
|
|
19
|
+
case 'middle':
|
|
20
|
+
y += (height - realHeight) / 2
|
|
21
|
+
break
|
|
22
|
+
case 'bottom':
|
|
23
|
+
y += (height - realHeight)
|
|
26
24
|
}
|
|
27
|
-
starY += y
|
|
28
25
|
}
|
|
26
|
+
starY += y
|
|
29
27
|
|
|
30
28
|
// textAlign
|
|
31
29
|
|
|
32
|
-
let row: ITextRowData
|
|
30
|
+
let row: ITextRowData, rowX: number, rowWidth: number
|
|
33
31
|
|
|
34
32
|
for (let i = 0, len = rows.length; i < len; i++) {
|
|
35
33
|
row = rows[i]
|
|
@@ -54,7 +52,22 @@ export function layoutText(drawData: ITextDrawData, style: ITextData): void {
|
|
|
54
52
|
drawData.overflow = i + 1
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
|
|
55
|
+
rowX = row.x
|
|
56
|
+
rowWidth = row.width
|
|
57
|
+
|
|
58
|
+
if (__letterSpacing < 0) { // letterSpacing < 0, like -20% -100%
|
|
59
|
+
if (row.width < 0) {
|
|
60
|
+
rowWidth = -row.width + style.fontSize + __letterSpacing
|
|
61
|
+
rowX -= rowWidth
|
|
62
|
+
rowWidth += style.fontSize
|
|
63
|
+
} else {
|
|
64
|
+
rowWidth -= __letterSpacing
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (rowX < bounds.x) bounds.x = rowX
|
|
69
|
+
if (rowWidth > bounds.width) bounds.width = rowWidth
|
|
70
|
+
|
|
58
71
|
}
|
|
59
72
|
|
|
60
73
|
bounds.y = y
|
package/src/TextRows.ts
CHANGED
|
@@ -11,7 +11,7 @@ const { trimRight } = TextRowHelper
|
|
|
11
11
|
const { Letter, Single, Before, After, Symbol, Break } = CharType
|
|
12
12
|
|
|
13
13
|
let word: ITextWordData, row: ITextRowData, wordWidth: number, rowWidth: number, realWidth: number
|
|
14
|
-
let char: string, charWidth: number, charType: CharType, lastCharType: CharType, langBreak: boolean, afterBreak: boolean, paraStart: boolean
|
|
14
|
+
let char: string, charWidth: number, startCharSize: number, charSize: number, charType: CharType, lastCharType: CharType, langBreak: boolean, afterBreak: boolean, paraStart: boolean
|
|
15
15
|
let textDrawData: ITextDrawData, rows: ITextRowData[] = [], bounds: IBoundsData
|
|
16
16
|
|
|
17
17
|
export function createRows(drawData: ITextDrawData, content: string, style: ITextData): void {
|
|
@@ -29,7 +29,7 @@ export function createRows(drawData: ITextDrawData, content: string, style: ITex
|
|
|
29
29
|
|
|
30
30
|
paraStart = true
|
|
31
31
|
lastCharType = null
|
|
32
|
-
wordWidth = rowWidth = 0
|
|
32
|
+
startCharSize = charWidth = charSize = wordWidth = rowWidth = 0
|
|
33
33
|
word = { data: [] }, row = { words: [] }
|
|
34
34
|
|
|
35
35
|
for (let i = 0, len = content.length; i < len; i++) {
|
|
@@ -51,7 +51,10 @@ export function createRows(drawData: ITextDrawData, content: string, style: ITex
|
|
|
51
51
|
if (charType === Letter && textCase !== 'none') char = getTextCase(char, textCase, !wordWidth)
|
|
52
52
|
|
|
53
53
|
charWidth = canvas.measureText(char).width
|
|
54
|
-
if (__letterSpacing)
|
|
54
|
+
if (__letterSpacing) {
|
|
55
|
+
if (__letterSpacing < 0) charSize = charWidth
|
|
56
|
+
charWidth += __letterSpacing
|
|
57
|
+
}
|
|
55
58
|
|
|
56
59
|
langBreak = (charType === Single && (lastCharType === Single || lastCharType === Letter)) || (lastCharType === Single && charType !== After) // break U字 文字 or 字U 字( 字* exclude 字。
|
|
57
60
|
afterBreak = ((charType === Before || charType === Single) && (lastCharType === Symbol || lastCharType === After)) // split >( =文 。哈 ;文
|
|
@@ -121,6 +124,7 @@ export function createRows(drawData: ITextDrawData, content: string, style: ITex
|
|
|
121
124
|
|
|
122
125
|
|
|
123
126
|
function addChar(char: string, width: number): void {
|
|
127
|
+
if (charSize && !startCharSize) startCharSize = charSize
|
|
124
128
|
word.data.push({ char, width })
|
|
125
129
|
wordWidth += width
|
|
126
130
|
}
|
|
@@ -139,6 +143,13 @@ function addRow(): void {
|
|
|
139
143
|
row.paraStart = true
|
|
140
144
|
paraStart = false
|
|
141
145
|
}
|
|
146
|
+
|
|
147
|
+
if (charSize) { // letterSpacing < 0, like -20% -100%
|
|
148
|
+
row.startCharSize = startCharSize
|
|
149
|
+
row.endCharSize = charSize
|
|
150
|
+
startCharSize = 0
|
|
151
|
+
}
|
|
152
|
+
|
|
142
153
|
row.width = rowWidth
|
|
143
154
|
if (bounds.width) trimRight(row)
|
|
144
155
|
rows.push(row)
|