@leafer-ui/text 1.0.4 → 1.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer-ui/text",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "@leafer-ui/text",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -13,19 +13,19 @@
13
13
  ],
14
14
  "repository": {
15
15
  "type": "git",
16
- "url": "https://github.com/leaferjs/ui.git"
16
+ "url": "https://github.com/leaferjs/leafer-ui.git"
17
17
  },
18
- "homepage": "https://github.com/leaferjs/ui/tree/main/packages/partner/text",
19
- "bugs": "https://github.com/leaferjs/ui/issues",
18
+ "homepage": "https://github.com/leaferjs/leafer-ui/tree/main/packages/partner/text",
19
+ "bugs": "https://github.com/leaferjs/leafer-ui/issues",
20
20
  "keywords": [
21
21
  "leafer-ui",
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.0.4"
25
+ "@leafer/core": "1.0.6"
26
26
  },
27
27
  "devDependencies": {
28
- "@leafer/interface": "1.0.4",
29
- "@leafer-ui/interface": "1.0.4"
28
+ "@leafer/interface": "1.0.6",
29
+ "@leafer-ui/interface": "1.0.6"
30
30
  }
31
31
  }
@@ -23,14 +23,11 @@ export function getDrawData(content: string, style: ITextData): ITextDrawData {
23
23
  const { textDecoration, __font, __padding: padding } = style
24
24
 
25
25
  if (padding) {
26
- if (width) {
27
- x = padding[left]
28
- width -= (padding[right] + padding[left])
29
- }
30
- if (height) {
31
- y = padding[top]
32
- height -= (padding[top] + padding[bottom])
33
- }
26
+ if (width) x = padding[left], width -= (padding[right] + padding[left])
27
+ else if (!style.autoSizeAlign) x = padding[left]
28
+
29
+ if (height) y = padding[top], height -= (padding[top] + padding[bottom])
30
+ else if (!style.autoSizeAlign) y = padding[top]
34
31
  }
35
32
 
36
33
  const drawData: ITextDrawData = {
@@ -58,23 +55,17 @@ export function getDrawData(content: string, style: ITextData): ITextDrawData {
58
55
 
59
56
 
60
57
  function padAutoText(padding: number[], drawData: ITextDrawData, style: ITextData, width: number, height: number): void {
61
- if (!width) {
58
+ if (!width && style.autoSizeAlign) {
62
59
  switch (style.textAlign) {
63
- case 'left':
64
- offsetText(drawData, 'x', padding[left])
65
- break
66
- case 'right':
67
- offsetText(drawData, 'x', -padding[right])
60
+ case 'left': offsetText(drawData, 'x', padding[left]); break
61
+ case 'right': offsetText(drawData, 'x', -padding[right])
68
62
  }
69
63
  }
70
64
 
71
- if (!height) {
65
+ if (!height && style.autoSizeAlign) {
72
66
  switch (style.verticalAlign) {
73
- case 'top':
74
- offsetText(drawData, 'y', padding[top])
75
- break
76
- case 'bottom':
77
- offsetText(drawData, 'y', -padding[bottom])
67
+ case 'top': offsetText(drawData, 'y', padding[top]); break
68
+ case 'bottom': offsetText(drawData, 'y', -padding[bottom])
78
69
  }
79
70
  }
80
71
  }
package/src/TextLayout.ts CHANGED
@@ -4,7 +4,7 @@ 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, __clipText, textAlign, verticalAlign, paraSpacing } = style
7
+ const { __lineHeight, __baseLine, __letterSpacing, __clipText, textAlign, verticalAlign, paraSpacing, autoSizeAlign } = 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
@@ -14,20 +14,17 @@ export function layoutText(drawData: ITextDrawData, style: ITextData): void {
14
14
  if (__clipText && realHeight > height) {
15
15
  realHeight = Math.max(height, __lineHeight)
16
16
  drawData.overflow = rows.length
17
- } else {
17
+ } else if (height || autoSizeAlign) {
18
18
  switch (verticalAlign) {
19
- case 'middle':
20
- y += (height - realHeight) / 2
21
- break
22
- case 'bottom':
23
- y += (height - realHeight)
19
+ case 'middle': y += (height - realHeight) / 2; break
20
+ case 'bottom': y += (height - realHeight)
24
21
  }
25
22
  }
26
23
  starY += y
27
24
 
28
25
  // textAlign
29
26
 
30
- let row: ITextRowData, rowX: number, rowWidth: number
27
+ let row: ITextRowData, rowX: number, rowWidth: number, layoutWidth = (width || autoSizeAlign) ? width : drawData.maxWidth
31
28
 
32
29
  for (let i = 0, len = rows.length; i < len; i++) {
33
30
  row = rows[i]
@@ -35,11 +32,8 @@ export function layoutText(drawData: ITextDrawData, style: ITextData): void {
35
32
 
36
33
  if (row.width < width || (row.width > width && !__clipText)) {
37
34
  switch (textAlign) {
38
- case 'center':
39
- row.x += (width - row.width) / 2
40
- break
41
- case 'right':
42
- row.x += width - row.width
35
+ case 'center': row.x += (layoutWidth - row.width) / 2; break
36
+ case 'right': row.x += layoutWidth - row.width
43
37
  }
44
38
  }
45
39
 
package/src/TextRows.ts CHANGED
@@ -12,13 +12,14 @@ const { Letter, Single, Before, After, Symbol, Break } = CharType
12
12
 
13
13
  let word: ITextWordData, row: ITextRowData, wordWidth: number, rowWidth: number, realWidth: number
14
14
  let char: string, charWidth: number, startCharSize: number, charSize: number, charType: CharType, lastCharType: CharType, langBreak: boolean, afterBreak: boolean, paraStart: boolean
15
- let textDrawData: ITextDrawData, rows: ITextRowData[] = [], bounds: IBoundsData
15
+ let textDrawData: ITextDrawData, rows: ITextRowData[] = [], bounds: IBoundsData, findMaxWidth: boolean
16
16
 
17
17
  export function createRows(drawData: ITextDrawData, content: string, style: ITextData): void {
18
18
 
19
19
  textDrawData = drawData
20
20
  rows = drawData.rows
21
21
  bounds = drawData.bounds
22
+ findMaxWidth = !bounds.width && !style.autoSizeAlign
22
23
 
23
24
  const { __letterSpacing, paraIndent, textCase } = style
24
25
  const { canvas } = Platform
@@ -126,7 +127,9 @@ export function createRows(drawData: ITextDrawData, content: string, style: ITex
126
127
 
127
128
  content.split('\n').forEach(content => {
128
129
  textDrawData.paraNumber++
129
- rows.push({ x: paraIndent || 0, text: content, width: canvas.measureText(content).width, paraStart: true })
130
+ rowWidth = canvas.measureText(content).width
131
+ rows.push({ x: paraIndent || 0, text: content, width: rowWidth, paraStart: true })
132
+ if (findMaxWidth) setMaxWidth()
130
133
  })
131
134
 
132
135
  }
@@ -162,7 +165,12 @@ function addRow(): void {
162
165
 
163
166
  row.width = rowWidth
164
167
  if (bounds.width) trimRight(row)
168
+ else if (findMaxWidth) setMaxWidth()
165
169
  rows.push(row)
166
170
  row = { words: [] }
167
171
  rowWidth = 0
168
172
  }
173
+
174
+ function setMaxWidth(): void {
175
+ if (rowWidth > (textDrawData.maxWidth || 0)) textDrawData.maxWidth = rowWidth
176
+ }