Parse the font family string into a structured array.

Parameters

string $css_string: The raw css string.

array $properties: The css properties to get.

string $type: The type of font file.

Return value

array Returns a key value array.

1 call to advagg_relocate_parse_css_font_face()
advagg_relocate_get_remote_font_data in advagg_relocate/advagg_relocate.advagg.inc
Gets external CSS files; caches it and returns css font rules.

File

advagg_relocate/advagg_relocate.advagg.inc, line 966

Code

function advagg_relocate_parse_css_font_face($css_string, array $properties, $type) {
    // Get the CSS that contains a font-family rule.
    $length = strlen($css_string);
    $property_position = 0;
    $lower = strtolower($css_string);
    $attributes = array();
    foreach ($properties as $property) {
        while (($property_position = strpos($lower, $property, $property_position)) !== FALSE) {
            // Find the start of the values for the property.
            $start_of_values = strpos($css_string, ':', $property_position);
            // Get the property at this location of the css.
            $property_in_loop = trim(substr($css_string, $property_position, $start_of_values - $property_position));
            // Make sure this property is one of the ones we're looking for.
            if ($property_in_loop !== $property) {
                $property_position += strlen($property);
                continue;
            }
            // Get position of last closing bracket plus 1 (start of this section).
            $start = strrpos($css_string, '}', -($length - $property_position));
            if ($start === FALSE) {
                // Property is in the first selector and a declaration block (full rule
                // set).
                $start = 0;
            }
            else {
                // Add one to start after the }.
                $start++;
            }
            // Get closing bracket (end of this section).
            $end = strpos($css_string, '}', $property_position);
            if ($end === FALSE) {
                // The end is the end of this file.
                $end = $length;
            }
            // Get closing ; in order to get end of the declaration of the property.
            $declaration_end_a = strpos($css_string, ';', $property_position);
            $declaration_end_b = strpos($css_string, '}', $property_position);
            if ($declaration_end_a === FALSE) {
                $declaration_end = $declaration_end_b;
            }
            else {
                $declaration_end = min($declaration_end_a, $declaration_end_b);
            }
            if ($declaration_end > $end) {
                $declaration_end = $end;
            }
            // Add one in order to capture the } when we ge the full rule set.
            $end++;
            // Advance position for the next run of the while loop.
            $property_position = $end;
            // Get values assigned to this property.
            $values_string = substr($css_string, $start_of_values + 1, $declaration_end - ($start_of_values + 1));
            // Parse values string into an array of values.
            $values_array = explode(',', $values_string);
            $values_array = array_map('trim', $values_array);
            foreach ($values_array as $key => $value) {
                if (stripos($value, "'{$type}'") !== FALSE || stripos($value, ".{$type}") !== FALSE) {
                    unset($values_array[$key]);
                    $values_array[$type] = $value;
                }
            }
            $attributes[$property][] = $values_array;
        }
    }
    // Make sure src is the last one.
    $temp = $attributes['src'];
    unset($attributes['src']);
    $attributes['src'] = $temp;
    // Parse attributes into an output array.
    $temp = array();
    $output = array();
    foreach ($attributes as $property => $values) {
        foreach ($values as $key => $value) {
            if ($property !== 'src') {
                $value = implode(',', $value);
                if (!isset($temp[$key])) {
                    $temp[$key] = '';
                }
                $temp[$key] .= "{$property}: {$value}; ";
            }
            else {
                $output[$temp[$key]] = $value;
            }
        }
    }
    return $output;
}