Path: blob/master/src/applications/celerity/CeleritySpriteGenerator.php
12249 views
<?php12final class CeleritySpriteGenerator extends Phobject {34public function buildTokenSheet() {5$icons = $this->getDirectoryList('tokens_1x');6$scales = array(7'1x' => 1,8'2x' => 2,9);10$template = id(new PhutilSprite())11->setSourceSize(18, 18);1213$sprites = array();14$prefix = 'tokens_';15foreach ($icons as $icon) {16$sprite = id(clone $template)17->setName('tokens-'.$icon)18->setTargetCSS('.tokens-'.$icon);1920foreach ($scales as $scale_key => $scale) {21$path = $this->getPath($prefix.$scale_key.'/'.$icon.'.png');22$sprite->setSourceFile($path, $scale);23}24$sprites[] = $sprite;25}2627$sheet = $this->buildSheet('tokens', true);28$sheet->setScales($scales);29foreach ($sprites as $sprite) {30$sheet->addSprite($sprite);31}3233return $sheet;34}3536public function buildLoginSheet() {37$icons = $this->getDirectoryList('login_1x');38$scales = array(39'1x' => 1,40'2x' => 2,41);42$template = id(new PhutilSprite())43->setSourceSize(28, 28);4445$sprites = array();46$prefix = 'login_';47foreach ($icons as $icon) {48$sprite = id(clone $template)49->setName('login-'.$icon)50->setTargetCSS('.login-'.$icon);5152foreach ($scales as $scale_key => $scale) {53$path = $this->getPath($prefix.$scale_key.'/'.$icon.'.png');54$sprite->setSourceFile($path, $scale);55}56$sprites[] = $sprite;57}5859$sheet = $this->buildSheet('login', true);60$sheet->setScales($scales);61foreach ($sprites as $sprite) {62$sheet->addSprite($sprite);63}6465return $sheet;66}6768private function getPath($to_path = null) {69$root = dirname(phutil_get_library_root('phabricator'));70return $root.'/resources/sprite/'.$to_path;71}7273private function getDirectoryList($dir) {74$path = $this->getPath($dir);7576$result = array();7778$images = Filesystem::listDirectory($path, $include_hidden = false);79foreach ($images as $image) {80if (!preg_match('/\.png$/', $image)) {81throw new Exception(82pht(83"Expected file '%s' in '%s' to be a sprite source ending in '%s'.",84$image,85$path,86'.png'));87}88$result[] = substr($image, 0, -4);89}9091return $result;92}9394private function buildSheet(95$name,96$has_retina,97$type = null,98$extra_css = '') {99100$sheet = new PhutilSpriteSheet();101102$at = '@';103104switch ($type) {105case PhutilSpriteSheet::TYPE_STANDARD:106default:107$type = PhutilSpriteSheet::TYPE_STANDARD;108$repeat_rule = 'no-repeat';109break;110case PhutilSpriteSheet::TYPE_REPEAT_X:111$repeat_rule = 'repeat-x';112break;113case PhutilSpriteSheet::TYPE_REPEAT_Y:114$repeat_rule = 'repeat-y';115break;116}117118$retina_rules = null;119if ($has_retina) {120$retina_rules = <<<EOCSS121@media122only screen and (min-device-pixel-ratio: 1.5),123only screen and (-webkit-min-device-pixel-ratio: 1.5),124only screen and (min-resolution: 1.5dppx) {125.sprite-{$name}{$extra_css} {126background-image: url(/rsrc/image/sprite-{$name}-X2.png);127background-size: {X}px {Y}px;128}129}130EOCSS;131}132133$sheet->setSheetType($type);134$sheet->setCSSHeader(<<<EOCSS135/**136* @provides sprite-{$name}-css137* {$at}generated138*/139140.sprite-{$name}{$extra_css} {141background-image: url(/rsrc/image/sprite-{$name}.png);142background-repeat: {$repeat_rule};143}144145{$retina_rules}146147EOCSS148);149150return $sheet;151}152}153154155