Skip to content

Commit dcb216a

Browse files
committed
[css-text] Improvements to justification script.
1 parent 42ccbe5 commit dcb216a

1 file changed

Lines changed: 74 additions & 19 deletions

File tree

css-text/justify.html

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ <h1>Universal Default Justification Experiment</h1>
1010

1111
<p id="render"></p>
1212

13-
<form>
13+
<form action="">
1414
<label>Text to Justify: <textarea name=text id=text>서울특별시(서울特別市)는 한반도</textarea></label>
15-
<label>Space-CJK Threshold: <input id=cjk name=cjk value=1></label>
16-
<!-- <label>Space-SEA Threshold: <input id=sea name=sea value=3></label> -->
15+
<label>Space-CJK Threshold: <input id=cjk name=cjk value=0.67></label>
16+
<!-- <label>Space-SEA Threshold: <input id=sea name=sea value=2.67></label> -->
1717
<label>Split Hangul-Han: <input type=checkbox name=splitHangul id=splitHangul></label>
18-
<p onclick="render()" style="color: green; font-weight: bold; cursor: pointer;">Render!</p>
18+
<ul>
19+
<li class="button" style="color: green" onclick="render()">Render!</li>
20+
<li class="button" style="color: orange" onclick="save()" >Save!</li>
21+
<li class="button" style="color: red" onclick="window.location = window.location.href.match('^[^#\?]+')">Reset!</li>
22+
</ul>
1923
</form>
2024

2125
<style>
@@ -31,11 +35,20 @@ <h1>Universal Default Justification Experiment</h1>
3135
margin: 1.5em auto 2em;
3236
white-space: nowrap;
3337
position: relative;
38+
font-size: 200%;
3439
}
40+
.button {
41+
font-weight: bold; cursor: pointer;
42+
border: solid; border-radius: 0.5em; padding: 0.1em 0.25em;
43+
}
44+
ul, li { margin: 1em 0; padding: 0; list-style: none; text-align: center; }
45+
li { display: inline; }
3546
</style>
3647

3748
<script>
38-
charCats = {
49+
'use strict';
50+
51+
var charCats = {
3952
// copied from http://fluxbb.org/forums/viewtopic.php?id=3866
4053
'space' :
4154
' ' // U+0020
@@ -75,51 +88,55 @@ <h1>Universal Default Justification Experiment</h1>
7588
var numSpaces = 0; // number of spaces
7689
var numCJK = 0; // number of stretchable gaps around CJK
7790
for (var c of charList) {
91+
var n = c.nextSibling;
7892
if (c.textContent.match(charCats['space'])) {
7993
c.setAttribute('class', 'space');
8094
numSpaces++;
95+
if (n && n.textContent.match(charCats['han']) ||
96+
(n.textContent.match(charCats['hangul']) && !splitHangul)) {
97+
numCJK++;
98+
}
8199
}
82100
else {
83-
n = c.nextSibling;
84101
if (!n) break; // no space after last char
85102
if (c.textContent.match(charCats['han']) ||
86103
(c.textContent.match(charCats['hangul']) && !splitHangul)) {
87104
c.setAttribute('class', 'cjk');
88105
numCJK++;
89106
}
90-
else if (n && n.textContent.match(charCats['han']) ||
107+
else if (n.textContent.match(charCats['han']) ||
91108
(n.textContent.match(charCats['hangul']) && !splitHangul)) {
92109
numCJK++;
93110
}
94111
}
95112
}
96-
//alert(charList.length + ',' + numSpaces + ',' + numCJK);
113+
console.log(charList.length + ' chars: ' + numSpaces + ' spaces, ' + numCJK + ' cjk gaps');
97114

98115
/* Calculate Space */
99-
renderWidth = renderer.offsetWidth;
116+
var renderWidth = renderer.offsetWidth;
100117
renderWidth -= getComputedStyle(renderer).borderLeftWidth.slice(0,-2);
101118
renderWidth -= getComputedStyle(renderer).borderRightWidth.slice(0,-2);
102119
renderWidth -= getComputedStyle(renderer).paddingLeft.slice(0,-2);
103120
renderWidth -= getComputedStyle(renderer).paddingRight.slice(0,-2);
104121

105-
lastChar = charList[charList.length-1];
106-
contentSize = lastChar.offsetLeft + lastChar.offsetWidth;
122+
var lastChar = charList[charList.length-1];
123+
var contentSize = lastChar.offsetLeft + lastChar.offsetWidth;
107124

108-
spaceToFill = renderWidth - contentSize;
109-
x = spaceToFill;
125+
var spaceToFill = renderWidth - contentSize;
126+
var x = spaceToFill;
110127

111128
/* Split Space */
112-
fontSize = getComputedStyle(renderer).fontSize.slice(0,-2);
113-
cjkSpaceLimit = document.getElementById('cjk').value * fontSize;
114-
//seaSpaceLimit = document.getElementById('sea').value * fontSize;
129+
var fontSize = getComputedStyle(renderer).fontSize.slice(0,-2);
130+
var cjkSpaceLimit = document.getElementById('cjk').value * fontSize;
131+
//var seaSpaceLimit = document.getElementById('sea').value * fontSize;
115132

116-
addToSpace = spaceToFill / numSpaces;
133+
var addToSpace = spaceToFill / numSpaces;
117134
if (addToSpace > cjkSpaceLimit)
118135
addToSpace = cjkSpaceLimit;
119136
spaceToFill -= addToSpace * numSpaces;
120137

121-
addToCJK = spaceToFill / (numCJK+numSpaces);
122-
//alert(renderWidth + ' - ' + contentSize + ' = ' + x + ' = ' + numSpaces + '*' + addToSpace + '+' + (numCJK+numSpaces) + '*' + addToCJK);
138+
var addToCJK = spaceToFill / (numCJK+numSpaces);
139+
console.log(renderWidth + ' - ' + contentSize + ' = ' + x + ' = ' + numSpaces + '*' + addToSpace + ' + ' + (numCJK+numSpaces) + '*' + addToCJK);
123140

124141
document.getElementById('renderStyle').textContent =
125142
'.space { padding-right: ' + (addToSpace+addToCJK) + 'px; }\n' +
@@ -128,3 +145,41 @@ <h1>Universal Default Justification Experiment</h1>
128145
}
129146
</script>
130147

148+
<script>
149+
/* Onload */
150+
/* Decode query and load into inputs */
151+
var query = window.location.hash.slice(1);
152+
if (!query) {
153+
query = window.location.href.match('\\?[^\\?]+');
154+
}
155+
if (query) {
156+
var pairs = decodeURIComponent(query[0].slice(1)).split('&&');
157+
for (var pair of pairs) {
158+
var [name, value] = pair.split('=');
159+
var field = document.getElementById(name);
160+
field.value = value;
161+
if (field.type == 'checkbox') {
162+
field.setAttribute('checked', 'checked');
163+
}
164+
}
165+
}
166+
/* Then render it*/
167+
render();
168+
169+
function save() {
170+
var pairs = [];
171+
var fields = document.getElementsByTagName('input');
172+
for (var i = 0; i < fields.length; i++) {
173+
if (fields[i].type == 'checkbox') {
174+
pairs.push(fields[i].id + '=' + fields[i].id);
175+
}
176+
else {
177+
pairs.push(fields[i].id + '=' + fields[i].value);
178+
}
179+
}
180+
// put this one last so URL is more hackable
181+
pairs.push('text=' + document.getElementById('text').value);
182+
var location = window.location.href.match('^[^#\?]+');
183+
window.location = location[0] + '?' + pairs.join('&&');
184+
}
185+
</script>

0 commit comments

Comments
 (0)