Skip to content

Commit

Permalink
Global "linebreak" function moved to "Typeset" namespace (call "Types…
Browse files Browse the repository at this point in the history
…et.linebreak" instead of just "linebreak")
  • Loading branch information
MacDada committed Dec 6, 2012
1 parent 340e19e commit ed4fa00
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 308 deletions.
6 changes: 3 additions & 3 deletions examples/article/browser-assist.js
Expand Up @@ -10,7 +10,7 @@ jQuery(function ($) {
format;

$('body').append(ruler);

format = formatter(function (str) {
if (str !== ' ') {
return ruler.text(str).width();
Expand All @@ -21,7 +21,7 @@ jQuery(function ($) {

function browserAssistTypeset(identifier, text, type, lineLengths, tolerance) {
var nodes = format[type](text),
breaks = linebreak(nodes, lineLengths, {tolerance: tolerance}),
breaks = Typeset.linebreak(nodes, lineLengths, {tolerance: tolerance}),
lines = [],
i, point, r, lineStart,
browserAssist = $(identifier).after('<ul></ul>'),
Expand All @@ -35,7 +35,7 @@ jQuery(function ($) {

for (var j = lineStart; j < nodes.length; j += 1) {
// After a line break, we skip any nodes unless they are boxes or forced breaks.
if (nodes[j].type === 'box' || (nodes[j].type === 'penalty' && nodes[j].penalty === -linebreak.defaults.infinity)) {
if (nodes[j].type === 'box' || (nodes[j].type === 'penalty' && nodes[j].penalty === -Typeset.linebreak.defaults.infinity)) {
lineStart = j;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/article/index.html
Expand Up @@ -133,7 +133,7 @@ <h2>Translations</h2>

for (var j = lineStart; j < nodes.length; j += 1) {
// After a line break, we skip any nodes unless they are boxes or forced breaks.
if (nodes[j].type === 'box' || (nodes[j].type === 'penalty' && nodes[j].penalty === -linebreak.infinity)) {
if (nodes[j].type === 'box' || (nodes[j].type === 'penalty' && nodes[j].penalty === -Typeset.linebreak.infinity)) {
lineStart = j;
break;
}
Expand Down Expand Up @@ -180,7 +180,7 @@ <h2>Translations</h2>

nodes = format[type](text);

breaks = linebreak(nodes, lineLengths, {tolerance: tolerance});
breaks = Typeset.linebreak(nodes, lineLengths, {tolerance: tolerance});

if (breaks.length !== 0) {
draw(context, nodes, breaks, lineLengths, center);
Expand Down
96 changes: 49 additions & 47 deletions src/formatter.js
@@ -1,4 +1,4 @@
/*global linebreak*/
/*global Typeset.linebreak*/

/*!
* Knuth and Plass line breaking algorithm in JavaScript
Expand All @@ -8,8 +8,10 @@
* All rights reserved.
*/
var formatter = function (measureText, options) {
var spaceWidth = measureText(' '),
o = {
var linebreak = Typeset.linebreak;

var spaceWidth = measureText(' '),
o = {
space: {
width: options && options.space.width || 3,
stretch: options && options.space.stretch || 6,
Expand All @@ -20,19 +22,19 @@ var formatter = function (measureText, options) {
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;
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));
// 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) {
words.forEach(function (word, index, array) {
var hyphenated = h.hyphenate(word);
if (hyphenated.length > 1 && word.length > 4) {
hyphenated.forEach(function (part, partIndex, partArray) {
Expand All @@ -44,7 +46,7 @@ var formatter = function (measureText, options) {
} 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));
Expand All @@ -56,16 +58,16 @@ var formatter = function (measureText, options) {
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;
});
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) {
words.forEach(function (word, index, array) {
var hyphenated = h.hyphenate(word);
if (hyphenated.length > 1 && word.length > 4) {
hyphenated.forEach(function (part, partIndex, partArray) {
Expand All @@ -79,20 +81,20 @@ var formatter = function (measureText, options) {
}
if (index === array.length - 1) {
nodes.push(linebreak.glue(0, linebreak.infinity, 0));
nodes.push(linebreak.penalty(0, -linebreak.infinity, 1));
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;
});
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) {
words.forEach(function (word, index, array) {
var hyphenated = h.hyphenate(word);
if (hyphenated.length > 1 && word.length > 4) {
hyphenated.forEach(function (part, partIndex, partArray) {
Expand All @@ -104,19 +106,19 @@ var formatter = function (measureText, options) {
} 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;
}
};

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 = {
Expand Down

0 comments on commit ed4fa00

Please sign in to comment.