private function generate()

in src/sprites/PhutilSpriteSheet.php [57:250]


  private function generate() {
    if ($this->generated) {
      return;
    }

    $multi_row = true;
    $multi_col = true;
    $margin_w = 1;
    $margin_h = 1;

    $type = $this->type;
    switch ($type) {
      case self::TYPE_STANDARD:
        break;
      case self::TYPE_REPEAT_X:
        $multi_col = false;
        $margin_w = 0;

        $width = null;
        foreach ($this->sprites as $sprite) {
          if ($width === null) {
            $width = $sprite->getSourceW();
          } else if ($width !== $sprite->getSourceW()) {
            throw new Exception(
              pht(
                "All sprites in a '%s' sheet must have the same width.",
                'repeat-x'));
          }
        }
        break;
      case self::TYPE_REPEAT_Y:
        $multi_row = false;
        $margin_h = 0;

        $height = null;
        foreach ($this->sprites as $sprite) {
          if ($height === null) {
            $height = $sprite->getSourceH();
          } else if ($height !== $sprite->getSourceH()) {
            throw new Exception(
              pht(
                "All sprites in a '%s' sheet must have the same height.",
                'repeat-y'));
          }
        }
        break;
      default:
        throw new Exception(pht("Unknown sprite sheet type '%s'!", $type));
    }


    $css = array();
    if ($this->cssHeader) {
      $css[] = $this->cssHeader;
    }

    $out_w = 0;
    $out_h = 0;

    // Lay out the sprite sheet. We attempt to build a roughly square sheet
    // so it's easier to manage, since 2000x20 is more cumbersome for humans
    // to deal with than 200x200.
    //
    // To do this, we use a simple greedy algorithm, adding sprites one at a
    // time. For each sprite, if the sheet is at least as wide as it is tall
    // we create a new row. Otherwise, we try to add it to an existing row.
    //
    // This isn't optimal, but does a reasonable job in most cases and isn't
    // too messy.

    // Group the sprites by their sizes. We lay them out in the sheet as
    // boxes, but then put them into the boxes in the order they were added
    // so similar sprites end up nearby on the final sheet.
    $boxes = array();
    foreach (array_reverse($this->sprites) as $sprite) {
      $s_w = $sprite->getSourceW() + $margin_w;
      $s_h = $sprite->getSourceH() + $margin_h;
      $boxes[$s_w][$s_h][] = $sprite;
    }

    $rows = array();
    foreach ($this->sprites as $sprite) {
      $s_w = $sprite->getSourceW() + $margin_w;
      $s_h = $sprite->getSourceH() + $margin_h;

      // Choose a row for this sprite.
      $maybe = array();
      foreach ($rows as $key => $row) {
        if ($row['h'] < $s_h) {
          // We can only add it to a row if the row is at least as tall as the
          // sprite.
          continue;
        }
        // We prefer rows which have the same height as the sprite, and then
        // rows which aren't yet very wide.
        $wasted_v = ($row['h'] - $s_h);
        $wasted_h = ($row['w'] / $out_w);
        $maybe[$key] = $wasted_v + $wasted_h;
      }

      $row_key = null;
      if ($maybe && $multi_col) {
        // If there were any candidate rows, pick the best one.
        asort($maybe);
        $row_key = head_key($maybe);
      }

      if ($row_key !== null && $multi_row) {
        // If there's a candidate row, but adding the sprite to it would make
        // the sprite wider than it is tall, create a new row instead. This
        // generally keeps the sprite square-ish.
        if ($rows[$row_key]['w'] + $s_w > $out_h) {
          $row_key = null;
        }
      }

      if ($row_key === null) {
        // Add a new row.
        $rows[] = array(
          'w'       => 0,
          'h'       => $s_h,
          'boxes'   => array(),
        );
        $row_key = last_key($rows);
        $out_h += $s_h;
      }

      // Add the sprite box to the row.
      $row = $rows[$row_key];
      $row['w'] += $s_w;
      $row['boxes'][] = array($s_w, $s_h);
      $rows[$row_key] = $row;

      $out_w = max($row['w'], $out_w);
    }

    $images = array();
    foreach ($this->scales as $scale) {
      $img = imagecreatetruecolor($out_w * $scale, $out_h * $scale);
      imagesavealpha($img, true);
      imagefill($img, 0, 0, imagecolorallocatealpha($img, 0, 0, 0, 127));

      $images[$scale] = $img;
    }


    // Put the shorter rows first. At the same height, put the wider rows first.
    // This makes the resulting sheet more human-readable.
    foreach ($rows as $key => $row) {
      $rows[$key]['sort'] = $row['h'] + (1 - ($row['w'] / $out_w));
    }
    $rows = isort($rows, 'sort');

    $pos_x = 0;
    $pos_y = 0;
    $rules = array();
    foreach ($rows as $row) {
      $max_h = 0;
      foreach ($row['boxes'] as $box) {
        $sprite = array_pop($boxes[$box[0]][$box[1]]);

        foreach ($images as $scale => $img) {
          $src = $this->loadSource($sprite, $scale);
          imagecopy(
            $img,
            $src,
            $scale * $pos_x,                $scale * $pos_y,
            $scale * $sprite->getSourceX(), $scale * $sprite->getSourceY(),
            $scale * $sprite->getSourceW(), $scale * $sprite->getSourceH());
        }

        $rule = $sprite->getTargetCSS();
        $cssx = (-$pos_x).'px';
        $cssy = (-$pos_y).'px';

        $rules[$sprite->getName()] = "{$rule} {\n".
          "  background-position: {$cssx} {$cssy};\n}";

        $pos_x += $sprite->getSourceW() + $margin_w;
        $max_h = max($max_h, $sprite->getSourceH());
      }
      $pos_x = 0;
      $pos_y += $max_h + $margin_h;
    }

    // Generate CSS rules in input order.
    foreach ($this->sprites as $sprite) {
      $css[] = $rules[$sprite->getName()];
    }

    $this->images = $images;
    $this->css = implode("\n\n", $css)."\n";
    $this->generated = true;
  }