-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhersheyexample.html
executable file
·209 lines (176 loc) · 6.14 KB
/
hersheyexample.html
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
<!DOCTYPE html>
<html>
<head>
<title>hersheytext.js Font Render Example</title>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<style type="text/css">
body {
font-family: sans-serif;
}
fieldset {
position: absolute;
left: 440px;
background-color: #fff;
}
fieldset input, fieldset select {
margin-bottom: 1em;
display: block;
}
textarea {
width: 100%;
height: 8em;
}
</style>
</head>
<body>
<fieldset><legend>Options</legend>
<input id="fonttext" type="text" value="Hello World!">
<select id="fontselect">
<option id="loading">Loading Fonts...</option>
</select>
</fieldset>
<div id="main" >
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="450" height="280"></svg>
</div>
<label>Raw SVG content of text</label><br>
<textarea></textarea>
<script type="text/javascript">
// Load the font JSON data
$.getJSON('hersheytext.min.json', function(fonts){
// Font data is loaded!
$('#loading').remove();
// Populate font choices
for (let id in fonts) {
$('<option>').text(fonts[id].name).val(id).appendTo('#fontselect');
}
$('#fontselect').change(function() {
$('#textexample').remove(); // Kill the old one (if any)
// Render some text into the SVG area with it
renderText($('#fonttext').val(), {
font: fonts[$(this).val()],
pos: {x: 0, y: 0},
scale: 2,
charWidth: 8,
wrapWidth: 70,
centerWidth: 200,
target: '#main svg',
id: 'textexample'
});
// REQUIRED: Refresh DOM to reinstate node status with XML namespaces
// TODO: There must be a better way to do this! :P
$('#main').html($('#main').html());
$('textarea').val($('#main svg')[0].outerHTML);
}).attr('size', 10).change(); // Trigger initial run
// Re-render on keypress
$('input').on('input', function(e){
$('#fontselect').change();
});
});
/**
* Render a string of text in a Hershey engraving font to a given SVG element
*
* @param {string} s
* Text string to be rendered
* @param {object} options
* Object of named options:
* font {obj}: [Required] Font object containing path elements for font
* id {string}: [Required] ID to give the final g(roup) SVG DOM object
* pos {object}: [Required] {X, Y} object of where to place the final object within the SVG
* charWidth {int}: [Optional] Base width given to each character
* charHeight {int}: [Optional] Base height given to each character (when wrapping)
* scale {int}: [Optional] Scale to multiply size of everything by
* wrapWidth {int}: [Optional] Max line size at which to wrap to the next line
* centerWidth {int}: [Optional] Width to center multiline text inside of
* centerHeight {int}: [Optional] Height to center text inside of vertically
*
* @returns {boolean}
* Operates directly on the given target given in options, returns false if
* required option missing or other failure.
*/
function renderText(s, options) {
try {
const font = options.font.chars;
options.charWidth = options.charWidth ? options.charWidth : 10;
options.charHeight = options.charHeight ? options.charHeight : 28;
const offset = { left: 0, top: 0 };
options.scale = options.scale ? options.scale : 1;
// Create central group
const $group = $('<g>').attr({
id: options.id,
style: 'stroke:#000000; fill:none;',
transform:
'scale(' + options.scale + ') ' +
'translate(' + options.pos.x + ',' + options.pos.y + ')'
});
$(options.target).prepend($group);
// Initial Line container
let lineCount = 0;
let $groupLine = $('<g>').attr('id', options.id + '-line-' + lineCount);
$group.prepend($groupLine);
// Move through each word
const words = s.split(' ');
for(let w in words) {
const word = words[w];
// Move through each letter
for(let i in word) {
const index = word.charCodeAt(i) - 33;
// Only print in range chars
let charOffset = options.charWidth;
if (font[index]){
charOffset = font[index].o;
// Add the char to the DOM
$groupLine.prepend(
$('<path>').attr({
d: font[index].d,
style: 'stroke:#000000; fill:none;',
fill: 'none',
transform: 'translate(' + offset.left + ', ' + offset.top + ')'
})
);
}
// Add space between
offset.left+= charOffset + options.charWidth;
}
// Wrap words to width
if (options.wrapWidth) {
if (offset.left > options.wrapWidth) {
if (options.centerWidth) {
const c = (options.centerWidth / 2) - (offset.left / 2);
$groupLine.attr('transform', 'translate(' + c + ',0)');
}
offset.left = 0;
offset.top += options.charHeight;
// New Line container
lineCount++;
$groupLine = $('<g>').attr('id', options.id + '-line-' + lineCount);
$group.prepend($groupLine);
} else {
offset.left += options.charWidth*2; // Add regular space
}
} else {
offset.left += options.charWidth*2; // Add regular space
}
}
if (options.centerWidth) {
const c = (options.centerWidth / 2) - (offset.left / 2);
$groupLine.attr('transform', 'translate(' + c + ',0)');
}
if (options.centerHeight) {
const c = (options.centerHeight / 2) - ((options.charHeight*(lineCount+1)) / 2) + options.pos.y;
$group.attr({
transform:
'scale(' + options.scale + ') ' +
'translate(' + options.pos.x + ',' + c + ')'
});
}
} catch(e) {
console.error(e);
return false; // Error!
}
return true; // We should be all good!
}
</script>
</body>
</html>