texy = $texy;
$this->consonants = array_flip($this->consonants);
$this->vowels = array_flip($this->vowels);
$this->before_r = array_flip($this->before_r);
$this->before_l = array_flip($this->before_l);
$this->before_h = array_flip($this->before_h);
$this->doubleVowels = array_flip($this->doubleVowels);
$texy->registerPostLine(array($this, 'postLine'), 'longwords');
}
public function postLine($text)
{
return preg_replace_callback(
'#[^\ \n\t\x14\x15\x16\x{2013}\x{2014}\x{ad}-]{'.$this->wordLimit.',}#u',
array($this, 'pattern'),
$text
);
}
/**
* Callback for long words.
* (c) David Grudl
* @param array
* @return string
*/
private function pattern($matches)
{
list($mWord) = $matches;
// [0] => lllloooonnnnggggwwwoorrdddd
$chars = array();
preg_match_all(
'#['.TEXY_MARK.']+|.#u',
$mWord,
$chars
);
$chars = $chars[0];
if (count($chars) < $this->wordLimit) return $mWord;
$consonants = $this->consonants;
$vowels = $this->vowels;
$before_r = $this->before_r;
$before_l = $this->before_l;
$before_h = $this->before_h;
$doubleVowels = $this->doubleVowels;
$s = array();
$trans = array();
$s[] = '';
$trans[] = -1;
foreach ($chars as $key => $char) {
if (ord($char{0}) < 32) continue;
$s[] = $char;
$trans[] = $key;
}
$s[] = '';
$len = count($s) - 2;
$positions = array();
$a = 0; $last = 1;
while (++$a < $len) {
$hyphen = self::DONT; // Do not hyphenate
do {
if ($s[$a] === "\xC2\xA0") { $a++; continue 2; } // here and after never
if ($s[$a] === '.') { $hyphen = self::HERE; break; }
if (isset($consonants[$s[$a]])) { // consonants
if (isset($vowels[$s[$a+1]])) {
if (isset($vowels[$s[$a-1]])) $hyphen = self::HERE;
break;
}
if (($s[$a] === 's') && ($s[$a-1] === 'n') && isset($consonants[$s[$a+1]])) { $hyphen = self::AFTER; break; }
if (isset($consonants[$s[$a+1]]) && isset($vowels[$s[$a-1]])) {
if ($s[$a+1] === 'r') {
$hyphen = isset($before_r[$s[$a]]) ? self::HERE : self::AFTER;
break;
}
if ($s[$a+1] === 'l') {
$hyphen = isset($before_l[$s[$a]]) ? self::HERE : self::AFTER;
break;
}
if ($s[$a+1] === 'h') { // CH
$hyphen = isset($before_h[$s[$a]]) ? self::DONT : self::AFTER;
break;
}
$hyphen = self::AFTER;
break;
}
break;
} // end of consonants
if (($s[$a] === 'u') && isset($doubleVowels[$s[$a-1]])) { $hyphen = self::AFTER; break; }
if (isset($vowels[$s[$a]]) && isset($vowels[$s[$a-1]])) { $hyphen = self::HERE; break; }
} while(0);
if ($hyphen === self::DONT && ($a - $last > $this->wordLimit*0.6)) $positions[] = $last = $a-1; // Hyphenate here
if ($hyphen === self::HERE) $positions[] = $last = $a-1; // Hyphenate here
if ($hyphen === self::AFTER) { $positions[] = $last = $a; $a++; } // Hyphenate after
} // while
$a = end($positions);
if (($a === $len-1) && isset($consonants[$s[$len]]))
array_pop($positions);
$syllables = array();
$last = 0;
foreach ($positions as $pos) {
if ($pos - $last > $this->wordLimit*0.6) {
$syllables[] = implode('', array_splice($chars, 0, $trans[$pos] - $trans[$last]));
$last = $pos;
}
}
$syllables[] = implode('', $chars);
//$s = implode("\xC2\xAD", $syllables); // insert shy
//$s = str_replace(array("\xC2\xAD\xC2\xA0", "\xC2\xA0\xC2\xAD"), array(' ', ' '), $s); // shy+nbsp = normal space
return implode("\xC2\xAD", $syllables);;
}
}