-
Notifications
You must be signed in to change notification settings - Fork 78
/
formatter.js
130 lines (120 loc) · 5.14 KB
/
formatter.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
/*global Typeset.linebreak*/
/*!
* Knuth and Plass line breaking algorithm in JavaScript
*
* Licensed under the new BSD License.
* Copyright 2009-2010, Bram Stein
* All rights reserved.
*/
var formatter = function (measureText, options) {
var linebreak = Typeset.linebreak;
var spaceWidth = measureText(' '),
o = {
space: {
width: options && options.space.width || 3,
stretch: options && options.space.stretch || 6,
shrink: options && options.space.shrink || 9
}
},
h = new Hypher(Hypher.en),
hyphenWidth = measureText('-'),
hyphenPenalty = 100;
return {
center: function (text) {
var nodes = [],
words = text.split(/\s/),
spaceStretch = (spaceWidth * o.space.width) / o.space.stretch,
spaceShrink = (spaceWidth * o.space.width) / o.space.shrink;
// Although not specified in the Knuth and Plass whitepaper, this box is necessary
// to keep the glue from disappearing.
nodes.push(linebreak.box(0, ''));
nodes.push(linebreak.glue(0, 12, 0));
words.forEach(function (word, index, array) {
var hyphenated = h.hyphenate(word);
if (hyphenated.length > 1 && word.length > 4) {
hyphenated.forEach(function (part, partIndex, partArray) {
nodes.push(linebreak.box(measureText(part), part));
if (partIndex !== partArray.length - 1) {
nodes.push(linebreak.penalty(hyphenWidth, hyphenPenalty, 1));
}
});
} else {
nodes.push(linebreak.box(measureText(word), word));
}
if (index === array.length - 1) {
nodes.push(linebreak.glue(0, 12, 0));
nodes.push(linebreak.penalty(0, -linebreak.infinity, 0));
} else {
nodes.push(linebreak.glue(0, 12, 0));
nodes.push(linebreak.penalty(0, 0, 0));
nodes.push(linebreak.glue(spaceWidth, -24, 0));
nodes.push(linebreak.box(0, ''));
nodes.push(linebreak.penalty(0, linebreak.infinity, 0));
nodes.push(linebreak.glue(0, 12, 0));
}
});
return nodes;
},
justify: function (text) {
var nodes = [],
words = text.split(/\s/),
spaceStretch = (spaceWidth * o.space.width) / o.space.stretch,
spaceShrink = (spaceWidth * o.space.width) / o.space.shrink;
words.forEach(function (word, index, array) {
var hyphenated = h.hyphenate(word);
if (hyphenated.length > 1 && word.length > 4) {
hyphenated.forEach(function (part, partIndex, partArray) {
nodes.push(linebreak.box(measureText(part), part));
if (partIndex !== partArray.length - 1) {
nodes.push(linebreak.penalty(hyphenWidth, hyphenPenalty, 1));
}
});
} else {
nodes.push(linebreak.box(measureText(word), word));
}
if (index === array.length - 1) {
nodes.push(linebreak.glue(0, linebreak.infinity, 0));
nodes.push(linebreak.penalty(0, -linebreak.infinity, 1));
} else {
nodes.push(linebreak.glue(spaceWidth, spaceStretch, spaceShrink));
}
});
return nodes;
},
left: function (text) {
var nodes = [],
words = text.split(/\s/),
spaceStretch = (spaceWidth * o.space.width) / o.space.stretch,
spaceShrink = (spaceWidth * o.space.width) / o.space.shrink;
words.forEach(function (word, index, array) {
var hyphenated = h.hyphenate(word);
if (hyphenated.length > 1 && word.length > 4) {
hyphenated.forEach(function (part, partIndex, partArray) {
nodes.push(linebreak.box(measureText(part), part));
if (partIndex !== partArray.length - 1) {
nodes.push(linebreak.penalty(hyphenWidth, hyphenPenalty, 1));
}
});
} else {
nodes.push(linebreak.box(measureText(word), word));
}
if (index === array.length - 1) {
nodes.push(linebreak.glue(0, linebreak.infinity, 0));
nodes.push(linebreak.penalty(0, -linebreak.infinity, 1));
} else {
nodes.push(linebreak.glue(0, 12, 0));
nodes.push(linebreak.penalty(0, 0, 0));
nodes.push(linebreak.glue(spaceWidth, -12, 0));
}
});
return nodes;
}
};
};
formatter.defaults = {
space: {
width: 3,
stretch: 6,
shrink: 9
}
};