@idraw/renderer 0.4.0-beta.39 → 0.4.0-beta.40
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/dist/esm/calculator.d.ts +43 -0
- package/dist/esm/calculator.js +168 -0
- package/dist/esm/draw/box.d.ts +3 -0
- package/dist/esm/draw/box.js +22 -17
- package/dist/esm/draw/circle.js +0 -2
- package/dist/esm/draw/layout.js +1 -1
- package/dist/esm/draw/text.js +16 -123
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +13 -3
- package/dist/esm/view-visible/index.d.ts +22 -0
- package/dist/esm/view-visible/index.js +63 -0
- package/dist/esm/virtual-flat/index.d.ts +7 -0
- package/dist/esm/virtual-flat/index.js +45 -0
- package/dist/esm/virtual-flat/text.d.ts +2 -0
- package/dist/esm/virtual-flat/text.js +151 -0
- package/dist/index.global.js +999 -198
- package/dist/index.global.min.js +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { is, getGroupQueueByElementPosition, calcElementOriginRectInfo, originRectInfoToRangeRectInfo } from '@idraw/util';
|
|
2
|
+
import { calcVirtualTextDetail } from './text';
|
|
3
|
+
export function calcVirtualFlatDetail(elem, opts) {
|
|
4
|
+
let virtualDetail = {};
|
|
5
|
+
if (elem.type === 'text') {
|
|
6
|
+
virtualDetail = calcVirtualTextDetail(elem, opts);
|
|
7
|
+
}
|
|
8
|
+
return virtualDetail;
|
|
9
|
+
}
|
|
10
|
+
export function elementsToVirtualFlatMap(elements, opts) {
|
|
11
|
+
const virtualFlatMap = {};
|
|
12
|
+
const currentPosition = [];
|
|
13
|
+
const _walk = (elem) => {
|
|
14
|
+
const baseInfo = {
|
|
15
|
+
type: elem.type,
|
|
16
|
+
isVisibleInView: true,
|
|
17
|
+
position: [...currentPosition]
|
|
18
|
+
};
|
|
19
|
+
let originRectInfo = null;
|
|
20
|
+
const groupQueue = getGroupQueueByElementPosition(elements, currentPosition);
|
|
21
|
+
originRectInfo = calcElementOriginRectInfo(elem, {
|
|
22
|
+
groupQueue: groupQueue || []
|
|
23
|
+
});
|
|
24
|
+
const virtualItem = Object.assign(Object.assign(Object.assign({}, baseInfo), {
|
|
25
|
+
originRectInfo: originRectInfo,
|
|
26
|
+
rangeRectInfo: is.angle(elem.angle)
|
|
27
|
+
? originRectInfoToRangeRectInfo(originRectInfo)
|
|
28
|
+
: originRectInfo
|
|
29
|
+
}), calcVirtualFlatDetail(elem, opts));
|
|
30
|
+
virtualFlatMap[elem.uuid] = virtualItem;
|
|
31
|
+
if (elem.type === 'group') {
|
|
32
|
+
elem.detail.children.forEach((ele, i) => {
|
|
33
|
+
currentPosition.push(i);
|
|
34
|
+
_walk(ele);
|
|
35
|
+
currentPosition.pop();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
elements.forEach((elem, index) => {
|
|
40
|
+
currentPosition.push(index);
|
|
41
|
+
_walk(elem);
|
|
42
|
+
currentPosition.pop();
|
|
43
|
+
});
|
|
44
|
+
return virtualFlatMap;
|
|
45
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { enhanceFontFamliy, getDefaultElementDetailConfig } from '@idraw/util';
|
|
2
|
+
const detailConfig = getDefaultElementDetailConfig();
|
|
3
|
+
function isTextWidthWithinErrorRange(w0, w1, scale) {
|
|
4
|
+
if (scale < 0.5) {
|
|
5
|
+
if (w0 < w1 && (w0 - w1) / w0 > -0.15) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return w0 >= w1;
|
|
10
|
+
}
|
|
11
|
+
export function calcVirtualTextDetail(elem, opts) {
|
|
12
|
+
const { w, h } = elem;
|
|
13
|
+
const x = 0;
|
|
14
|
+
const y = 0;
|
|
15
|
+
const ctx = opts.tempContext;
|
|
16
|
+
const lines = [];
|
|
17
|
+
const detail = Object.assign(Object.assign({}, detailConfig), elem.detail);
|
|
18
|
+
const originFontSize = detail.fontSize || detailConfig.fontSize;
|
|
19
|
+
const fontSize = originFontSize;
|
|
20
|
+
if (fontSize < 2) {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
const originLineHeight = detail.lineHeight || originFontSize;
|
|
24
|
+
const lineHeight = originLineHeight;
|
|
25
|
+
ctx.textBaseline = 'top';
|
|
26
|
+
ctx.$setFont({
|
|
27
|
+
fontWeight: detail.fontWeight,
|
|
28
|
+
fontSize: fontSize,
|
|
29
|
+
fontFamily: enhanceFontFamliy(detail.fontFamily)
|
|
30
|
+
});
|
|
31
|
+
let detailText = detail.text.replace(/\r\n/gi, '\n');
|
|
32
|
+
if (detail.textTransform === 'lowercase') {
|
|
33
|
+
detailText = detailText.toLowerCase();
|
|
34
|
+
}
|
|
35
|
+
else if (detail.textTransform === 'uppercase') {
|
|
36
|
+
detailText = detailText.toUpperCase();
|
|
37
|
+
}
|
|
38
|
+
const fontHeight = lineHeight;
|
|
39
|
+
const detailTextList = detailText.split('\n');
|
|
40
|
+
let lineNum = 0;
|
|
41
|
+
detailTextList.forEach((itemText, idx) => {
|
|
42
|
+
if (detail.minInlineSize === 'maxContent') {
|
|
43
|
+
lines.push({
|
|
44
|
+
x,
|
|
45
|
+
y: 0,
|
|
46
|
+
text: itemText,
|
|
47
|
+
width: ctx.$undoPixelRatio(ctx.measureText(itemText).width)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
let lineText = '';
|
|
52
|
+
let splitStr = '';
|
|
53
|
+
let tempStrList = itemText.split(splitStr);
|
|
54
|
+
if (detail.wordBreak === 'normal') {
|
|
55
|
+
splitStr = ' ';
|
|
56
|
+
const wordList = itemText.split(splitStr);
|
|
57
|
+
tempStrList = [];
|
|
58
|
+
wordList.forEach((word, idx) => {
|
|
59
|
+
tempStrList.push(word);
|
|
60
|
+
if (idx < wordList.length - 1) {
|
|
61
|
+
tempStrList.push(splitStr);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (tempStrList.length === 1 && detail.overflow === 'visible') {
|
|
66
|
+
lines.push({
|
|
67
|
+
x,
|
|
68
|
+
y: 0,
|
|
69
|
+
text: tempStrList[0],
|
|
70
|
+
width: ctx.$undoPixelRatio(ctx.measureText(tempStrList[0]).width)
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
else if (tempStrList.length > 0) {
|
|
74
|
+
for (let i = 0; i < tempStrList.length; i++) {
|
|
75
|
+
if (isTextWidthWithinErrorRange(ctx.$doPixelRatio(w), ctx.measureText(lineText + tempStrList[i]).width, 1)) {
|
|
76
|
+
lineText += tempStrList[i] || '';
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
lines.push({
|
|
80
|
+
x,
|
|
81
|
+
y: 0,
|
|
82
|
+
text: lineText,
|
|
83
|
+
width: ctx.$undoPixelRatio(ctx.measureText(lineText).width)
|
|
84
|
+
});
|
|
85
|
+
lineText = tempStrList[i] || '';
|
|
86
|
+
lineNum++;
|
|
87
|
+
}
|
|
88
|
+
if ((lineNum + 1) * fontHeight > h && detail.overflow === 'hidden') {
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
if (tempStrList.length - 1 === i) {
|
|
92
|
+
if ((lineNum + 1) * fontHeight <= h) {
|
|
93
|
+
lines.push({
|
|
94
|
+
x,
|
|
95
|
+
y: 0,
|
|
96
|
+
text: lineText,
|
|
97
|
+
width: ctx.$undoPixelRatio(ctx.measureText(lineText).width)
|
|
98
|
+
});
|
|
99
|
+
if (idx < detailTextList.length - 1) {
|
|
100
|
+
lineNum++;
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
lines.push({
|
|
109
|
+
x,
|
|
110
|
+
y: 0,
|
|
111
|
+
text: '',
|
|
112
|
+
width: 0
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
let startY = 0;
|
|
118
|
+
let eachLineStartY = 0;
|
|
119
|
+
if (fontHeight > fontSize) {
|
|
120
|
+
eachLineStartY = (fontHeight - fontSize) / 2;
|
|
121
|
+
}
|
|
122
|
+
if (lines.length * fontHeight < h) {
|
|
123
|
+
if (elem.detail.verticalAlign === 'top') {
|
|
124
|
+
startY = 0;
|
|
125
|
+
}
|
|
126
|
+
else if (elem.detail.verticalAlign === 'bottom') {
|
|
127
|
+
startY += h - lines.length * fontHeight;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
startY += (h - lines.length * fontHeight) / 2;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
{
|
|
134
|
+
const _y = y + startY;
|
|
135
|
+
lines.forEach((line, i) => {
|
|
136
|
+
let _x = x;
|
|
137
|
+
if (detail.textAlign === 'center') {
|
|
138
|
+
_x = x + (w - line.width) / 2;
|
|
139
|
+
}
|
|
140
|
+
else if (detail.textAlign === 'right') {
|
|
141
|
+
_x = x + (w - line.width);
|
|
142
|
+
}
|
|
143
|
+
lines[i].x = _x;
|
|
144
|
+
lines[i].y = _y + fontHeight * i + eachLineStartY;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
const virtualTextDetail = {
|
|
148
|
+
textLines: lines
|
|
149
|
+
};
|
|
150
|
+
return virtualTextDetail;
|
|
151
|
+
}
|