# Hyphenopoly.js [](https://travis-ci.org/mnater/Hyphenopoly) [](https://coveralls.io/github/mnater/Hyphenopoly?branch=master) [](https://david-dm.org/mnater/Hyphenopoly) [](https://david-dm.org/mnater/Hyphenopoly?type=dev) [](https://npms.io/search?q=hyphenopoly) [](https://www.codacy.com/app/mnater/Hyphenopoly?utm_source=github.com&utm_medium=referral&utm_content=mnater/Hyphenopoly&utm_campaign=Badge_Grade) Hyphenopoly.js is a __JavaScript-polyfill for hyphenation in HTML__: it hyphenates text if the user agent does not support CSS-hyphenation at all or not for the required languages and it is a __Node.js-module__. The package consists of the following parts: - _Hyphenopoly_Loader.js_ (~27KB unpacked, ~4KB minified and compressed): feature-checks the client and loads other resources if necessary. - _Hyphenopoly.js_ (~45KB unpacked, ~4KB minified and compressed): does the whole DOM-foo and wraps (w)asm. - _hyphenEngine.wasm_ (~1KB uncompressed): wasm code for creating pattern trie and finding hyphenation points. - _hyphenEngine.asm.js_ (~11KB uncompressed, ~1KB minified and compressed): fallback for clients that don't support wasm. - _pattern.hpb_ (sizes differ! e.g. en-us.hpb: ~23KB uncompressed, ~14KB compressed): space saving binary format of the hyphenation patterns (including their license). - _hyphenopoly.module.js_: the node module ## Usage (Browser) Place all code for Hyphenopoly at the top of the header (immediately after the `
Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis proeliis cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt.
For which reason the Helvetii also surpass the rest of the Gauls in valor, as they contend with the Germans in almost daily battles, when they either repel them from their own territories, or themselves wage war on their frontiers.
Aus diesem Grund übertreffen auch die Helvetier die übrigen Gallier an Tapferkeit, weil sie sich in fast täglichen Gefechten mit den Germanen messen, wobei sie diese entweder von ihrem Gebiet fernhalten oder selbst in deren Gebiet kämpfen.
## Automatic hyphenation
The algorithm used for hyphenation was developed by Franklin M. Liang for TeX. It works more or less like this:
1. Load a set of precomputed language specific patterns. The patterns are stored in a structure called a trie, which is very efficient for this task.
2. Collect all patterns that are a substring of the word to be hyphenated.
3. Combine the numerical values between characters: higher values overwrite lower values.
4. Odd values are hyphenation points (except if the hyphenation point is left from leftmin and right from rightmin), replace them with a soft hyphen and drop the other values.
5. Repeat 2. - 4. for all words longer than minWordLength
Example:
````text
Hyphenation
h y p h e n a t i o n
h y3p h
h e2n
h e n a4
h e n5a t
1n a
n2a t
1t i o
2i o
o2n
h0y3p0h0e2n5a4t2i0o2n
Hy-phen-ation
````
The patterns are precomputed and available for many languages on CTAN. Hyphenopoly.js uses a proprietary binary format (including pattern licence, metadata and the patterns). Patterns are computed from a large list of hyphenated words by a program called patgen. They aim to find some hyphenation points – not all – because it's better to miss a hyphenation point then to have some false hyphenation points. Most patterns are really good but none is error free.
These pattern vary in size. This is mostly due to the different linguistic characteristics of the languages.
## Hyphenopoly.js vs. Hyphenator.js
[Hyphenator.js](https://github.com/mnater/Hyphenator) started 2007 and had evolved ever since.
But web browsers have evolved much more!
Almost all of them support [native hyphenation](https://developer.mozilla.org/en-US/docs/Web/CSS/hyphens) for a [specific set of languages](https://developer.mozilla.org/en-US/docs/Web/CSS/hyphens#Browser_compatibility). So it was time for something new!
Hyphenopoly.js is based on Hyphenator.js (they share some code) but - in favor of simplicity and speed – lacks many features of Hyphenator.js. Most of these features aren't needed in modern webdesign anymore:
- dropped support for usage as bookmarklet
- dropped support for frames
- dropped support for ancient browsers
- dropped caching of patterns in browser storage
- dropped breaking of non-textual content (urls, code etc.)
- and some more…
If you need one of those features use Hyphenator.js – or give some feedback and proof that the feature is really useful and should be implemented in Hyphenopoly.js
On the other hand Hyphenopoly has a much finer-grained configuration system that allows you to make settings based on CSS-classes.
And last but not least it is faster than Hyphenator.js