@leafer-ui/text 1.0.0-rc.4 → 1.0.0-rc.6
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 +4 -4
- package/src/CharLayout.ts +15 -11
- package/src/TextClip.ts +42 -22
- package/src/TextConvert.ts +2 -2
- package/src/TextLayout.ts +7 -2
- package/src/TextRows.ts +15 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer-ui/text",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.6",
|
|
4
4
|
"description": "@leafer-ui/text",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
"leaferjs"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@leafer-ui/core": "1.0.0-rc.
|
|
25
|
+
"@leafer-ui/core": "1.0.0-rc.6"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@leafer/interface": "1.0.0-rc.
|
|
29
|
-
"@leafer-ui/interface": "1.0.0-rc.
|
|
28
|
+
"@leafer/interface": "1.0.0-rc.6",
|
|
29
|
+
"@leafer-ui/interface": "1.0.0-rc.6"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/CharLayout.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { ITextCharData, ITextData, ITextDrawData } from '@leafer-ui/interface'
|
|
1
|
+
import { ITextCharData, ITextData, ITextDrawData, ITextRowData } from '@leafer-ui/interface'
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
const CharMode = 0 // data: [{char:'a', x: 0}, {char:'b', x: 5}, {char:'d', x:20}]
|
|
5
5
|
const WordMode = 1 // data: [{ char:'ab', x: 0}, { char:'d', x:20}]
|
|
6
|
-
const
|
|
6
|
+
const TextMode = 2 // text: 'ab c'
|
|
7
7
|
|
|
8
8
|
export function layoutChar(drawData: ITextDrawData, style: ITextData, width: number, _height: number): void {
|
|
9
9
|
|
|
@@ -16,18 +16,13 @@ export function layoutChar(drawData: ITextDrawData, style: ITextData, width: num
|
|
|
16
16
|
|
|
17
17
|
indentWidth = paraIndent && row.paraStart ? paraIndent : 0
|
|
18
18
|
addWordWidth = (width && textAlign === 'justify' && row.words.length > 1) ? (width - row.width - indentWidth) / (row.words.length - 1) : 0
|
|
19
|
-
mode = (letterSpacing || row.isOverflow) ? CharMode : (addWordWidth > 0.01 ? WordMode :
|
|
19
|
+
mode = (letterSpacing || row.isOverflow) ? CharMode : (addWordWidth > 0.01 ? WordMode : TextMode)
|
|
20
|
+
if (row.isOverflow && !letterSpacing) row.textMode = true
|
|
20
21
|
|
|
21
|
-
if (mode ===
|
|
22
|
+
if (mode === TextMode) {
|
|
22
23
|
|
|
23
|
-
row.text = ''
|
|
24
24
|
row.x += indentWidth
|
|
25
|
-
|
|
26
|
-
row.words.forEach(word => {
|
|
27
|
-
word.data.forEach(char => {
|
|
28
|
-
row.text += char.char
|
|
29
|
-
})
|
|
30
|
-
})
|
|
25
|
+
toTextChar(row)
|
|
31
26
|
|
|
32
27
|
} else {
|
|
33
28
|
|
|
@@ -63,6 +58,15 @@ export function layoutChar(drawData: ITextDrawData, style: ITextData, width: num
|
|
|
63
58
|
|
|
64
59
|
}
|
|
65
60
|
|
|
61
|
+
function toTextChar(row: ITextRowData): void {
|
|
62
|
+
row.text = ''
|
|
63
|
+
row.words.forEach(word => {
|
|
64
|
+
word.data.forEach(char => {
|
|
65
|
+
row.text += char.char
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
66
70
|
function toWordChar(data: ITextCharData[], charX: number, wordChar: ITextCharData): number {
|
|
67
71
|
data.forEach(char => {
|
|
68
72
|
wordChar.char += char.char
|
package/src/TextClip.ts
CHANGED
|
@@ -1,39 +1,59 @@
|
|
|
1
1
|
import { Platform } from '@leafer/core'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { ITextCharData, ITextData, ITextDrawData, ITextRowData } from '@leafer-ui/interface'
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
export function clipText(drawData: ITextDrawData,
|
|
6
|
+
export function clipText(drawData: ITextDrawData, style: ITextData): void {
|
|
7
7
|
|
|
8
8
|
const { rows, overflow } = drawData
|
|
9
|
+
let { textOverflow } = style
|
|
9
10
|
rows.splice(overflow)
|
|
10
11
|
|
|
12
|
+
|
|
11
13
|
if (textOverflow !== 'hide') {
|
|
12
14
|
if (textOverflow === 'ellipsis') textOverflow = '...'
|
|
13
15
|
|
|
16
|
+
let char: ITextCharData, charRight: number
|
|
14
17
|
const ellipsisWidth = Platform.canvas.measureText(textOverflow).width
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
const right = style.x + style.width - ellipsisWidth
|
|
19
|
+
const list = style.textWrap === 'none' ? rows : [rows[overflow - 1]]
|
|
20
|
+
|
|
21
|
+
list.forEach(row => {
|
|
22
|
+
|
|
23
|
+
if (row.isOverflow && row.data) {
|
|
24
|
+
|
|
25
|
+
let end = row.data.length - 1
|
|
26
|
+
|
|
27
|
+
for (let i = end; i > -1; i--) {
|
|
28
|
+
char = row.data[i]
|
|
29
|
+
charRight = char.x + char.width
|
|
30
|
+
if (i === end && charRight < right) {
|
|
31
|
+
break
|
|
32
|
+
} else if (charRight < right && char.char !== ' ') {
|
|
33
|
+
row.data.splice(i + 1)
|
|
34
|
+
row.width -= char.width
|
|
35
|
+
break
|
|
36
|
+
}
|
|
37
|
+
row.width -= char.width
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
row.width += ellipsisWidth
|
|
41
|
+
row.data.push({ char: textOverflow, x: charRight })
|
|
42
|
+
|
|
43
|
+
if (row.textMode) toTextChar(row)
|
|
31
44
|
}
|
|
32
|
-
row.width -= char.width
|
|
33
|
-
}
|
|
34
45
|
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
|
|
47
|
+
})
|
|
48
|
+
|
|
37
49
|
}
|
|
38
50
|
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function toTextChar(row: ITextRowData): void {
|
|
54
|
+
row.text = ''
|
|
55
|
+
row.data.forEach(char => {
|
|
56
|
+
row.text += char.char
|
|
57
|
+
})
|
|
58
|
+
row.data = null
|
|
39
59
|
}
|
package/src/TextConvert.ts
CHANGED
|
@@ -20,7 +20,7 @@ export const TextConvert: ITextConvertModule = {
|
|
|
20
20
|
let width = style.__getInput('width') || 0
|
|
21
21
|
let height = style.__getInput('height') || 0
|
|
22
22
|
|
|
23
|
-
const { textDecoration,
|
|
23
|
+
const { textDecoration, __font, padding } = style
|
|
24
24
|
|
|
25
25
|
if (padding) {
|
|
26
26
|
const [top, right, bottom, left] = MathHelper.fourNumber(padding)
|
|
@@ -47,7 +47,7 @@ export const TextConvert: ITextConvertModule = {
|
|
|
47
47
|
|
|
48
48
|
layoutChar(drawData, style, width, height) // set char.x
|
|
49
49
|
|
|
50
|
-
if (drawData.overflow) clipText(drawData,
|
|
50
|
+
if (drawData.overflow) clipText(drawData, style)
|
|
51
51
|
|
|
52
52
|
if (textDecoration !== 'none') decorationText(drawData, style)
|
|
53
53
|
|
package/src/TextLayout.ts
CHANGED
|
@@ -4,14 +4,14 @@ 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, __letterSpacing, textAlign, verticalAlign, paraSpacing
|
|
7
|
+
const { __lineHeight, __baseLine, __letterSpacing, __clipText, textAlign, verticalAlign, paraSpacing } = 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 (
|
|
14
|
+
if (__clipText && realHeight > height) {
|
|
15
15
|
realHeight = Math.max(height, __lineHeight)
|
|
16
16
|
drawData.overflow = rows.length
|
|
17
17
|
} else {
|
|
@@ -68,6 +68,11 @@ export function layoutText(drawData: ITextDrawData, style: ITextData): void {
|
|
|
68
68
|
if (rowX < bounds.x) bounds.x = rowX
|
|
69
69
|
if (rowWidth > bounds.width) bounds.width = rowWidth
|
|
70
70
|
|
|
71
|
+
// clip nowrap
|
|
72
|
+
if (__clipText && width && width < rowWidth) {
|
|
73
|
+
row.isOverflow = true
|
|
74
|
+
if (!drawData.overflow) drawData.overflow = rows.length
|
|
75
|
+
}
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
bounds.y = y
|
package/src/TextRows.ts
CHANGED
|
@@ -27,6 +27,9 @@ export function createRows(drawData: ITextDrawData, content: string, style: ITex
|
|
|
27
27
|
|
|
28
28
|
if (charMode) {
|
|
29
29
|
|
|
30
|
+
const wrap = style.textWrap !== 'none'
|
|
31
|
+
const breakAll = style.textWrap === 'break'
|
|
32
|
+
|
|
30
33
|
paraStart = true
|
|
31
34
|
lastCharType = null
|
|
32
35
|
startCharSize = charWidth = charSize = wordWidth = rowWidth = 0
|
|
@@ -61,18 +64,25 @@ export function createRows(drawData: ITextDrawData, content: string, style: ITex
|
|
|
61
64
|
|
|
62
65
|
realWidth = paraStart && paraIndent ? width - paraIndent : width
|
|
63
66
|
|
|
64
|
-
if (width && rowWidth + wordWidth + charWidth > realWidth) { // wrap
|
|
65
|
-
|
|
66
|
-
if (!afterBreak) afterBreak = charType === Letter && lastCharType == After // split ,S
|
|
67
|
+
if (wrap && (width && rowWidth + wordWidth + charWidth > realWidth)) { // wrap
|
|
67
68
|
|
|
68
|
-
if (
|
|
69
|
+
if (breakAll) {
|
|
69
70
|
|
|
70
71
|
if (wordWidth) addWord() // break
|
|
71
72
|
addRow()
|
|
72
73
|
|
|
73
74
|
} else {
|
|
75
|
+
if (!afterBreak) afterBreak = charType === Letter && lastCharType == After // split ,S
|
|
74
76
|
|
|
75
|
-
|
|
77
|
+
if (langBreak || afterBreak || charType === Break || charType === Before || charType === Single || (wordWidth + charWidth > realWidth)) {
|
|
78
|
+
|
|
79
|
+
if (wordWidth) addWord() // break
|
|
80
|
+
addRow()
|
|
81
|
+
|
|
82
|
+
} else {
|
|
83
|
+
|
|
84
|
+
addRow()
|
|
85
|
+
}
|
|
76
86
|
}
|
|
77
87
|
|
|
78
88
|
}
|