Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function toCSS(Context $context)
 {
     $value = $this->key instanceof GenerateCSSInterface ? $this->key->toCSS($context) : $this->key;
     if ($this->operator) {
         $value .= $this->operator;
         $value .= $this->value instanceof GenerateCSSInterface ? $this->value->toCSS($context) : $this->value;
     }
     return '[' . $value . ']';
 }
Ejemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function compare(Node $other)
 {
     // we need to provide The context for those
     $context = new Context();
     if ($this->toCSS($context) === $other->toCSS($context)) {
         return 0;
     }
 }
Ejemplo n.º 3
0
 /**
  * Creates a SVG gradient
  *
  * @param Node $direction
  * @param Node ...$stop1
  * @return UrlNode
  * @throws CompilerException If the arguments are invalid
  */
 public function svggradient(Node $direction)
 {
     $numArgs = func_num_args();
     $arguments = func_get_args();
     if ($numArgs === 2) {
         // a list of colors
         if (is_array($arguments[1]->value) && count($arguments[1]->value) < 2) {
             throw new CompilerException('svg-gradient expects direction, start_color [start_position], [color position,]..., end_color [end_position]');
         }
         $stops = $arguments[1]->value;
     } elseif ($numArgs < 3) {
         throw new CompilerException('svg-gradient expects direction, start_color [start_position], [color position,]..., end_color [end_position]');
     } else {
         $stops = array_slice($arguments, 1);
     }
     $gradientType = 'linear';
     $rectangleDimension = 'x="0" y="0" width="1" height="1"';
     $renderEnv = new Context(['compress' => false]);
     $directionValue = $direction->toCSS($renderEnv);
     switch ($directionValue) {
         case 'to bottom':
             $gradientDirectionSvg = 'x1="0%" y1="0%" x2="0%" y2="100%"';
             break;
         case 'to right':
             $gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="0%"';
             break;
         case 'to bottom right':
             $gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="100%"';
             break;
         case 'to top right':
             $gradientDirectionSvg = 'x1="0%" y1="100%" x2="100%" y2="0%"';
             break;
         case 'ellipse':
         case 'ellipse at center':
             $gradientType = 'radial';
             $gradientDirectionSvg = 'cx="50%" cy="50%" r="75%"';
             $rectangleDimension = 'x="-50" y="-50" width="101" height="101"';
             break;
         default:
             throw new CompilerException("svg-gradient direction must be 'to bottom', 'to right', 'to bottom right', 'to top right' or 'ellipse at center'");
     }
     $returner = '<?xml version="1.0" ?>' . '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewBox="0 0 1 1" preserveAspectRatio="none">' . '<' . $gradientType . 'Gradient id="gradient" gradientUnits="userSpaceOnUse" ' . $gradientDirectionSvg . '>';
     for ($i = 0; $i < count($stops); $i++) {
         if ($stops[$i] instanceof ExpressionNode) {
             $color = $stops[$i]->value[0];
             $position = $stops[$i]->value[1];
         } else {
             $color = $stops[$i];
             $position = null;
         }
         if (!$color instanceof ColorNode || !(($i === 0 || $i + 1 === count($stops)) && $position === null) && !$position instanceof DimensionNode) {
             throw new CompilerException('svg-gradient expects direction, start_color [start_position], [color position,]..., end_color [end_position] or direction, color list');
         }
         if ($position) {
             $positionValue = $position->toCSS($renderEnv);
         } elseif ($i === 0) {
             $positionValue = '0%';
         } else {
             $positionValue = '100%';
         }
         $colorValue = $color->getColor()->toRGB();
         $alpha = $color->getAlpha(true);
         $returner .= '<stop offset="' . $positionValue . '" stop-color="' . $colorValue . '"' . ($alpha < 1 ? ' stop-opacity="' . $alpha . '"' : '') . '/>';
     }
     $returner .= '</' . $gradientType . 'Gradient><rect ' . $rectangleDimension . ' fill="url(#gradient)" /></svg>';
     $returner = Util::encodeURIComponent($returner);
     $returner = 'data:image/svg+xml,' . $returner;
     return new UrlNode(new QuotedNode("'" . $returner . "'", $returner, false));
 }
Ejemplo n.º 4
0
 /**
  * Compares with another node
  *
  * @param Node $other
  * @return integer|null
  */
 public function compare(Node $other)
 {
     if ($other instanceof QuotedNode && !$this->escaped && !$other->escaped) {
         return Util::numericCompare($this->value, $other->value);
     } else {
         $context = new Context();
         return $other->toCSS($context) === $this->toCSS($context) ? 0 : null;
     }
 }