You are here

function csstidy_optimise::dissolve_short_font in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg_css_compress/csstidy/class.csstidy_optimise.inc \csstidy_optimise::dissolve_short_font()

Dissolve font property

@version 1.3

Parameters

string $str_value:

Return value

array

See also

merge_font()

1 call to csstidy_optimise::dissolve_short_font()
csstidy_optimise::shorthands in advagg_css_compress/csstidy/class.csstidy_optimise.inc
Optimises shorthands @access public @version 1.0

File

advagg_css_compress/csstidy/class.csstidy_optimise.inc, line 845

Class

csstidy_optimise
CSS Optimising Class

Code

function dissolve_short_font($str_value) {
  $font_prop_default =& $GLOBALS['csstidy']['font_prop_default'];
  $font_weight = array(
    'normal',
    'bold',
    'bolder',
    'lighter',
    100,
    200,
    300,
    400,
    500,
    600,
    700,
    800,
    900,
  );
  $font_variant = array(
    'normal',
    'small-caps',
  );
  $font_style = array(
    'normal',
    'italic',
    'oblique',
  );
  $important = '';
  $return = array(
    'font-style' => null,
    'font-variant' => null,
    'font-weight' => null,
    'font-size' => null,
    'line-height' => null,
    'font-family' => null,
  );
  if (csstidy::is_important($str_value)) {
    $important = '!important';
    $str_value = csstidy::gvw_important($str_value);
  }
  $have['style'] = false;
  $have['variant'] = false;
  $have['weight'] = false;
  $have['size'] = false;

  // Detects if font-family consists of several words w/o quotes
  $multiwords = false;

  // Workaround with multiple font-family
  $str_value = csstidy_optimise::explode_ws(',', trim($str_value));
  $str_value[0] = csstidy_optimise::explode_ws(' ', trim($str_value[0]));
  for ($j = 0; $j < count($str_value[0]); $j++) {
    if ($have['weight'] === false && in_array($str_value[0][$j], $font_weight)) {
      $return['font-weight'] = $str_value[0][$j];
      $have['weight'] = true;
    }
    elseif ($have['variant'] === false && in_array($str_value[0][$j], $font_variant)) {
      $return['font-variant'] = $str_value[0][$j];
      $have['variant'] = true;
    }
    elseif ($have['style'] === false && in_array($str_value[0][$j], $font_style)) {
      $return['font-style'] = $str_value[0][$j];
      $have['style'] = true;
    }
    elseif ($have['size'] === false && (is_numeric($str_value[0][$j][0]) || $str_value[0][$j][0] === null || $str_value[0][$j][0] === '.')) {
      $size = csstidy_optimise::explode_ws('/', trim($str_value[0][$j]));
      $return['font-size'] = $size[0];
      if (isset($size[1])) {
        $return['line-height'] = $size[1];
      }
      else {
        $return['line-height'] = '';

        // don't add 'normal' !
      }
      $have['size'] = true;
    }
    else {
      if (isset($return['font-family'])) {
        $return['font-family'] .= ' ' . $str_value[0][$j];
        $multiwords = true;
      }
      else {
        $return['font-family'] = $str_value[0][$j];
      }
    }
  }

  // add quotes if we have several qords in font-family
  if ($multiwords !== false) {
    $return['font-family'] = '"' . $return['font-family'] . '"';
  }
  $i = 1;
  while (isset($str_value[$i])) {
    $return['font-family'] .= ',' . trim($str_value[$i]);
    $i++;
  }

  // Fix for 100 and more font-size
  if ($have['size'] === false && isset($return['font-weight']) && is_numeric($return['font-weight'][0])) {
    $return['font-size'] = $return['font-weight'];
    unset($return['font-weight']);
  }
  foreach ($font_prop_default as $font_prop => $default_value) {
    if ($return[$font_prop] !== null) {
      $return[$font_prop] = $return[$font_prop] . $important;
    }
    else {
      $return[$font_prop] = $default_value . $important;
    }
  }
  return $return;
}