function css_font($value, $root)
{
    // according to CSS 2.1 standard,
    // value of 'font' CSS property can be represented as follows:
    //   [ <'font-style'> || <'font-variant'> || <'font-weight'> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'> ] |
    //   caption | icon | menu | message-box | small-caption | status-bar | inherit
    // Note that font-family value, unlike other values, can contain spaces (in this case it should be quoted)
    // Breaking value by spaces, we'll break such multi-word families.
    // Replace all white space sequences with only one space;
    // Remove spaces after commas; it will allow us
    // to split value correctly using look-backward expressions
    $value = preg_replace("/\\s+/", " ", $value);
    $value = preg_replace("/,\\s+/", ",", $value);
    // Split value to subvalues by all whitespaces NOT preceeded by comma;
    // thus, we'll keep all alternative font-families together instead of breaking them.
    // Still we have a problem with multi-word family names.
    $subvalues = preg_split("/ /", $value);
    // Let's scan subvalues we've received and join values containing multiword family names
    $family_start = 0;
    $family_running = false;
    $family_double_quote = false;
    for ($i = 0; $i < count($subvalues); $i++) {
        $current_value = $subvalues[$i];
        if ($family_running) {
            $subvalues[$family_start] .= " " . $subvalues[$i];
            // Remove this subvalues from the subvalue list at all
            array_splice($subvalues, $i, 1);
            $i--;
        }
        // Check if current subvalue contains beginning of multi-word family name
        // We can detect it by searching for single or double quote without pair
        if ($family_running && $family_double_quote && !preg_match('/^[^"]*("[^"]*")*[^"]*$/', $current_value)) {
            $family_running = false;
        } elseif ($family_running && !$family_double_quote && !preg_match("/^[^']*('[^']*')*[^']*\$/", $current_value)) {
            $family_running = false;
        } elseif (!$family_running && !preg_match("/^[^']*('[^']*')*[^']*\$/", $current_value)) {
            $family_running = true;
            $family_start = $i;
            $family_double_quote = false;
        } elseif (!$family_running && !preg_match('/^[^"]*("[^"]*")*[^"]*$/', $current_value)) {
            $family_running = true;
            $family_start = $i;
            $family_double_quote = true;
        }
    }
    // Now process subvalues one-by-one.
    foreach ($subvalues as $subvalue) {
        $subvalue = trim(strtolower($subvalue));
        switch (detect_font_value_type($subvalue)) {
            case FONT_VALUE_STYLE:
                css_font_style($subvalue, $root);
                break;
            case FONT_VALUE_WEIGHT:
                css_font_weight($subvalue, $root);
                break;
            case FONT_VALUE_SIZE:
                css_font_size($subvalue, $root);
                break;
            case FONT_VALUE_FAMILY:
                css_font_family($subvalue, $root);
                break;
        }
    }
}
Example #2
0
function apply_css_rule_obj($properties, $baseurl, $root, &$pipeline)
{
    $pipeline->push_base_url($baseurl);
    foreach ($properties as $key => $value) {
        switch ($key) {
            case 'border':
                css_border($value, $root);
                break;
            case 'border-color':
                css_border_color($value, $root);
                break;
            case 'border-top':
                css_border_top($value, $root);
                break;
            case 'border-right':
                css_border_right($value, $root);
                break;
            case 'border-bottom':
                css_border_bottom($value, $root);
                break;
            case 'border-left':
                css_border_left($value, $root);
                break;
            case 'border-style':
                css_border_style($value, $root);
                break;
            case 'border-top-style':
                css_border_top_style($value, $root);
                break;
            case 'border-right-style':
                css_border_right_style($value, $root);
                break;
            case 'border-bottom-style':
                css_border_bottom_style($value, $root);
                break;
            case 'border-left-style':
                css_border_left_style($value, $root);
                break;
            case 'border-top-color':
                css_border_top_color($value, $root);
                break;
            case 'border-right-color':
                css_border_right_color($value, $root);
                break;
            case 'border-bottom-color':
                css_border_bottom_color($value, $root);
                break;
            case 'border-left-color':
                css_border_left_color($value, $root);
                break;
            case 'border-width':
                css_border_width($value, $root);
                break;
            case 'border-top-width':
                css_border_top_width($value, $root);
                break;
            case 'border-right-width':
                css_border_right_width($value, $root);
                break;
            case 'border-bottom-width':
                css_border_bottom_width($value, $root);
                break;
            case 'border-left-width':
                css_border_left_width($value, $root);
                break;
            case 'font':
                css_font($value, $root);
                break;
            case 'font-family':
                css_font_family($value, $root);
                break;
            case 'font-size':
                css_font_size($value, $root);
                break;
            case 'font-style':
                css_font_style($value, $root);
                break;
            case 'font-weight':
                css_font_weight($value, $root);
                break;
            case 'line-height':
                css_line_height($value, $root);
                break;
            default:
                $handler =& get_css_handler($key);
                if ($handler) {
                    $handler->replace($value, $pipeline);
                }
                break;
        }
    }
    $pipeline->pop_base_url();
}