View source
<?php
class CSSCompression_Combine_Font {
private $Control;
private $Combine;
private $rfont = "/(^|(?<!\\\\);)(font|line)-(style|variant|weight|size|height|family):(.*?)((?<!\\\\);|\$)/";
private $groupings = array(
array(
'font-style',
'font-variant',
'font-weight',
'size/height',
'font-family',
),
array(
'font-style',
'font-variant',
'font-weight',
'font-size',
'font-family',
),
array(
'font-style',
'font-variant',
'size/height',
'font-family',
),
array(
'font-style',
'font-variant',
'font-size',
'font-family',
),
array(
'font-style',
'font-weight',
'size/height',
'font-family',
),
array(
'font-style',
'font-weight',
'font-size',
'font-family',
),
array(
'font-variant',
'font-weight',
'size/height',
'font-family',
),
array(
'font-variant',
'font-weight',
'font-size',
'font-family',
),
array(
'font-weight',
'size/height',
'font-family',
),
array(
'font-weight',
'font-size',
'font-family',
),
array(
'font-variant',
'size/height',
'font-family',
),
array(
'font-variant',
'font-size',
'font-family',
),
array(
'font-style',
'size/height',
'font-family',
),
array(
'font-style',
'font-size',
'font-family',
),
array(
'size/height',
'font-family',
),
array(
'font-size',
'font-family',
),
);
public function __construct(CSSCompression_Control $control, CSSCompression_Combine $combine) {
$this->Control = $control;
$this->Combine = $combine;
}
public function combine($val) {
$storage = $this
->storage($val);
foreach ($this->groupings as $props) {
if ($replace = $this->Combine
->searchDefinitions('font', $storage, $props)) {
break;
}
}
if ($replace) {
$pos = 0;
while (preg_match($this->rfont, $val, $match, PREG_OFFSET_CAPTURE, $pos)) {
if (!isset($storage['line-height']) && stripos($match[0][0], 'line-height') === 0) {
$pos = $match[0][1] + strlen($match[0][0]) - 1;
continue;
}
$colon = strlen($match[1][0]);
$val = substr_replace($val, $replace, $match[0][1] + $colon, strlen($match[0][0]) - $colon);
$pos = $match[0][1] + strlen($replace) - $colon - 1;
$replace = '';
}
}
return $val;
}
private function storage($val) {
$storage = array();
$pos = 0;
while (preg_match($this->rfont, $val, $match, PREG_OFFSET_CAPTURE, $pos)) {
$storage[$match[2][0] . '-' . $match[3][0]] = $match[4][0];
$pos = $match[0][1] + strlen($match[0][0]) - 1;
}
if (isset($storage['font-size']) && isset($storage['line-height'])) {
$storage['size/height'] = $storage['font-size'] . '/' . $storage['line-height'];
unset($storage['font-size'], $storage['line-height']);
}
return $storage;
}
public function access($method, $args) {
if (method_exists($this, $method)) {
return call_user_func_array(array(
$this,
$method,
), $args);
}
else {
throw new CSSCompression_Exception("Unknown method in Font Class - " . $method);
}
}
}