-
Notifications
You must be signed in to change notification settings - Fork 18
/
textWrap.js
167 lines (144 loc) · 5.62 KB
/
textWrap.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
import measure from "./textWidth.js";
import defaultSplit from "./textSplit.js";
import stringify from "./stringify.js";
import {trimRight} from "./trim.js";
/**
@function textWrap
@desc Based on the defined styles and dimensions, breaks a string into an array of strings for each line of text.
*/
export default function() {
let fontFamily = "sans-serif",
fontSize = 10,
fontWeight = 400,
height = 200,
lineHeight,
maxLines = null,
overflow = false,
split = defaultSplit,
width = 200;
/**
The inner return object and wraps the text and returns the line data array.
@private
*/
function textWrap(sentence) {
sentence = stringify(sentence);
if (lineHeight === void 0) lineHeight = Math.ceil(fontSize * 1.4);
const words = split(sentence);
const style = {
"font-family": fontFamily,
"font-size": fontSize,
"font-weight": fontWeight,
"line-height": lineHeight
};
let line = 1,
textProg = "",
truncated = false,
widthProg = 0;
const lineData = [],
sizes = measure(words, style),
space = measure(" ", style);
for (let i = 0; i < words.length; i++) {
let word = words[i];
const wordWidth = sizes[words.indexOf(word)];
word += sentence.slice(textProg.length + word.length).match("^( |\n)*", "g")[0];
if (textProg.slice(-1) === "\n" || widthProg + wordWidth > width) {
if (!i && !overflow) {
truncated = true;
break;
}
if (lineData.length >= line) lineData[line - 1] = trimRight(lineData[line - 1]);
line++;
if (lineHeight * line > height || wordWidth > width && !overflow || maxLines && line > maxLines) {
truncated = true;
break;
}
widthProg = 0;
lineData.push(word);
}
else if (!i) lineData[0] = word;
else lineData[line - 1] += word;
textProg += word;
widthProg += wordWidth;
widthProg += word.match(/[\s]*$/g)[0].length * space;
}
return {
lines: lineData,
sentence, truncated,
widths: measure(lineData, style),
words
};
}
/**
@memberof textWrap
@desc If *value* is specified, sets the font family accessor to the specified function or string and returns this generator. If *value* is not specified, returns the current font family.
@param {Function|String} [*value* = "sans-serif"]
*/
textWrap.fontFamily = function(_) {
return arguments.length ? (fontFamily = _, textWrap) : fontFamily;
};
/**
@memberof textWrap
@desc If *value* is specified, sets the font size accessor to the specified function or number and returns this generator. If *value* is not specified, returns the current font size.
@param {Function|Number} [*value* = 10]
*/
textWrap.fontSize = function(_) {
return arguments.length ? (fontSize = _, textWrap) : fontSize;
};
/**
@memberof textWrap
@desc If *value* is specified, sets the font weight accessor to the specified function or number and returns this generator. If *value* is not specified, returns the current font weight.
@param {Function|Number|String} [*value* = 400]
*/
textWrap.fontWeight = function(_) {
return arguments.length ? (fontWeight = _, textWrap) : fontWeight;
};
/**
@memberof textWrap
@desc If *value* is specified, sets height limit to the specified value and returns this generator. If *value* is not specified, returns the current value.
@param {Number} [*value* = 200]
*/
textWrap.height = function(_) {
return arguments.length ? (height = _, textWrap) : height;
};
/**
@memberof textWrap
@desc If *value* is specified, sets the line height accessor to the specified function or number and returns this generator. If *value* is not specified, returns the current line height accessor, which is 1.1 times the [font size](#textWrap.fontSize) by default.
@param {Function|Number} [*value*]
*/
textWrap.lineHeight = function(_) {
return arguments.length ? (lineHeight = _, textWrap) : lineHeight;
};
/**
@memberof textWrap
@desc If *value* is specified, sets the maximum number of lines allowed when wrapping.
@param {Function|Number} [*value*]
*/
textWrap.maxLines = function(_) {
return arguments.length ? (maxLines = _, textWrap) : maxLines;
};
/**
@memberof textWrap
@desc If *value* is specified, sets the overflow to the specified boolean and returns this generator. If *value* is not specified, returns the current overflow value.
@param {Boolean} [*value* = false]
*/
textWrap.overflow = function(_) {
return arguments.length ? (overflow = _, textWrap) : overflow;
};
/**
@memberof textWrap
@desc If *value* is specified, sets the word split function to the specified function and returns this generator. If *value* is not specified, returns the current word split function.
@param {Function} [*value*] A function that, when passed a string, is expected to return that string split into an array of words to textWrap. The default split function splits strings on the following characters: `-`, `/`, `;`, `:`, `&`
*/
textWrap.split = function(_) {
return arguments.length ? (split = _, textWrap) : split;
};
/**
@memberof textWrap
@desc If *value* is specified, sets width limit to the specified value and returns this generator. If *value* is not specified, returns the current value.
@param {Number} [*value* = 200]
*/
textWrap.width = function(_) {
return arguments.length ? (width = _, textWrap) : width;
};
return textWrap;
}