forked from fex-team/kityminder-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.js
288 lines (254 loc) · 9.64 KB
/
text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
define(function(require, exports, module) {
var kity = require('../core/kity');
var utils = require('../core/utils');
var Minder = require('../core/minder');
var MinderNode = require('../core/node');
var Command = require('../core/command');
var Module = require('../core/module');
var Renderer = require('../core/render');
/**
* 针对不同系统、不同浏览器、不同字体做居中兼容性处理
* 暂时未增加Linux的处理
*/
var FONT_ADJUST = {
'safari': {
'微软雅黑,Microsoft YaHei': -0.17,
'楷体,楷体_GB2312,SimKai': -0.1,
'隶书, SimLi': -0.1,
'comic sans ms': -0.23,
'impact,chicago': -0.15,
'times new roman': -0.1,
'arial black,avant garde': -0.17,
'default': 0
},
'ie': {
10: {
'微软雅黑,Microsoft YaHei': -0.17,
'comic sans ms': -0.17,
'impact,chicago': -0.08,
'times new roman': 0.04,
'arial black,avant garde': -0.17,
'default': -0.15
},
11: {
'微软雅黑,Microsoft YaHei': -0.17,
'arial,helvetica,sans-serif': -0.17,
'comic sans ms': -0.17,
'impact,chicago': -0.08,
'times new roman': 0.04,
'sans-serif': -0.16,
'arial black,avant garde': -0.17,
'default': -0.15
}
},
'edge': {
'微软雅黑,Microsoft YaHei': -0.15,
'arial,helvetica,sans-serif': -0.17,
'comic sans ms': -0.17,
'impact,chicago': -0.08,
'sans-serif': -0.16,
'arial black,avant garde': -0.17,
'default': -0.15
},
'sg': {
'微软雅黑,Microsoft YaHei': -0.15,
'arial,helvetica,sans-serif': -0.05,
'comic sans ms': -0.22,
'impact,chicago': -0.16,
'times new roman': -0.03,
'arial black,avant garde': -0.22,
'default': -0.15
},
'chrome': {
'Mac': {
'andale mono': -0.05,
'comic sans ms': -0.3,
'impact,chicago': -0.13,
'times new roman': -0.1,
'arial black,avant garde': -0.17,
'default': 0
},
'Win': {
'微软雅黑,Microsoft YaHei': -0.15,
'arial,helvetica,sans-serif': -0.02,
'arial black,avant garde': -0.2,
'comic sans ms': -0.2,
'impact,chicago': -0.12,
'times new roman': -0.02,
'default': -0.15
},
'Lux': {
'andale mono': -0.05,
'comic sans ms': -0.3,
'impact,chicago': -0.13,
'times new roman': -0.1,
'arial black,avant garde': -0.17,
'default': 0
}
},
'firefox': {
'Mac': {
'微软雅黑,Microsoft YaHei': -0.2,
'宋体,SimSun': 0.05,
'comic sans ms': -0.2,
'impact,chicago': -0.15,
'arial black,avant garde': -0.17,
'times new roman': -0.1,
'default': 0.05
},
'Win': {
'微软雅黑,Microsoft YaHei': -0.16,
'andale mono': -0.17,
'arial,helvetica,sans-serif': -0.17,
'comic sans ms': -0.22,
'impact,chicago': -0.23,
'times new roman': -0.22,
'sans-serif': -0.22,
'arial black,avant garde': -0.17,
'default': -0.16
},
'Lux': {
"宋体,SimSun": -0.2,
"微软雅黑,Microsoft YaHei": -0.2,
"黑体, SimHei": -0.2,
"隶书, SimLi": -0.2,
"楷体,楷体_GB2312,SimKai": -0.2,
"andale mono": -0.2,
"arial,helvetica,sans-serif": -0.2,
"comic sans ms": -0.2,
"impact,chicago": -0.2,
"times new roman": -0.2,
"sans-serif": -0.2,
"arial black,avant garde": -0.2,
"default": -0.16
}
},
};
var TextRenderer = kity.createClass('TextRenderer', {
base: Renderer,
create: function() {
return new kity.Group().setId(utils.uuid('node_text'));
},
update: function(textGroup, node) {
function getDataOrStyle(name) {
return node.getData(name) || node.getStyle(name);
}
var nodeText = node.getText();
var textArr = nodeText ? nodeText.split('\n') : [' '];
var lineHeight = node.getStyle('line-height');
var fontSize = getDataOrStyle('font-size');
var fontFamily = getDataOrStyle('font-family') || 'default';
var height = (lineHeight * fontSize) * textArr.length - (lineHeight - 1) * fontSize;
var yStart = -height / 2;
var Browser = kity.Browser;
var adjust;
if (Browser.chrome || Browser.opera || Browser.bd ||Browser.lb === "chrome") {
adjust = FONT_ADJUST['chrome'][Browser.platform][fontFamily];
} else if (Browser.gecko) {
adjust = FONT_ADJUST['firefox'][Browser.platform][fontFamily];
} else if (Browser.sg) {
adjust = FONT_ADJUST['sg'][fontFamily];
} else if (Browser.safari) {
adjust = FONT_ADJUST['safari'][fontFamily];
} else if (Browser.ie) {
adjust = FONT_ADJUST['ie'][Browser.version][fontFamily];
} else if (Browser.edge) {
adjust = FONT_ADJUST['edge'][fontFamily];
} else if (Browser.lb) {
// 猎豹浏览器的ie内核兼容性模式下
adjust = 0.9;
}
textGroup.setTranslate(0, (adjust || 0) * fontSize);
var rBox = new kity.Box(),
r = Math.round;
this.setTextStyle(node, textGroup);
var textLength = textArr.length;
var textGroupLength = textGroup.getItems().length;
var i, ci, textShape, text;
if (textLength < textGroupLength) {
for (i = textLength, ci; ci = textGroup.getItem(i);) {
textGroup.removeItem(i);
}
} else if (textLength > textGroupLength) {
var growth = textLength - textGroupLength;
while (growth--) {
textShape = new kity.Text()
.setAttr('text-rendering', 'inherit');
if (kity.Browser.ie || kity.Browser.edge) {
textShape.setVerticalAlign('top');
} else {
textShape.setAttr('dominant-baseline', 'text-before-edge');
}
textGroup.addItem(textShape);
}
}
for (i = 0, text, textShape;
(text = textArr[i], textShape = textGroup.getItem(i)); i++) {
textShape.setContent(text);
if (kity.Browser.ie || kity.Browser.edge) {
textShape.fixPosition();
}
}
this.setTextStyle(node, textGroup);
var textHash = node.getText() +
['font-size', 'font-name', 'font-weight', 'font-style'].map(getDataOrStyle).join('/');
if (node._currentTextHash == textHash && node._currentTextGroupBox) return node._currentTextGroupBox;
node._currentTextHash = textHash;
return function() {
textGroup.eachItem(function(i, textShape) {
var y = yStart + i * fontSize * lineHeight;
textShape.setY(y);
var bbox = textShape.getBoundaryBox();
rBox = rBox.merge(new kity.Box(0, y, bbox.height && bbox.width || 1, fontSize));
});
var nBox = new kity.Box(r(rBox.x), r(rBox.y), r(rBox.width), r(rBox.height));
node._currentTextGroupBox = nBox;
return nBox;
};
},
setTextStyle: function(node, text) {
var hooks = TextRenderer._styleHooks;
hooks.forEach(function(hook) {
hook(node, text);
});
}
});
var TextCommand = kity.createClass({
base: Command,
execute: function(minder, text) {
var node = minder.getSelectedNode();
if (node) {
node.setText(text);
node.render();
minder.layout();
}
},
queryState: function(minder) {
return minder.getSelectedNodes().length == 1 ? 0 : -1;
},
queryValue: function(minder) {
var node = minder.getSelectedNode();
return node ? node.getText() : null;
}
});
utils.extend(TextRenderer, {
_styleHooks: [],
registerStyleHook: function(fn) {
TextRenderer._styleHooks.push(fn);
}
});
kity.extendClass(MinderNode, {
getTextGroup: function() {
return this.getRenderer('TextRenderer').getRenderShape();
}
});
Module.register('text', {
'commands': {
'text': TextCommand
},
'renderers': {
center: TextRenderer
}
});
module.exports = TextRenderer;
});