Changeset 2621 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-geopattern.php
- Timestamp:
- 02/25/2016 10:49:24 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-geopattern.php
r2611 r2621 7 7 * Generates Geopattern icons for Plugins. 8 8 * 9 * @package WordPressdotorg _Plugin_Directory9 * @package WordPressdotorg\Plugin_Directory 10 10 */ 11 11 class Plugin_Geopattern extends \RedeyeVentures\GeoPattern\GeoPattern { 12 12 13 var $slug; // Hashed to generate pattern 14 var $text; // Text to be overlaid 15 var $textcolor = 'black'; 13 /** 14 * Hashed to generate pattern. 15 * 16 * @var string 17 */ 18 public $slug; 16 19 17 function __construct($options=array()) { 20 /** 21 * Text to be overlaid. 22 * 23 * @var string 24 */ 25 public $text; 26 27 /** 28 * @var string 29 */ 30 public $textcolor = 'black'; 31 32 /** 33 * @param array $options 34 */ 35 function __construct( $options = array() ) { 18 36 parent::__construct( $options ); 19 37 20 if ( isset( $options['text'] ) ) 38 if ( isset( $options['text'] ) ) { 21 39 $this->text = $options['text']; 40 } 22 41 23 if ( isset( $options['textcolor'] ) ) 42 if ( isset( $options['textcolor'] ) ) { 24 43 $this->textcolor = $options['textcolor']; 44 } 25 45 26 46 // Replace the base SVG object with our own, so the dimensions are gettable. … … 28 48 } 29 49 50 /** 51 * @param $text 52 */ 30 53 function setText( $text ) { 31 54 $this->text = $text; 32 55 } 33 56 57 /** 58 * @param $color 59 */ 34 60 function setTextColor( $color ) { 35 61 $this->textcolor = $color; 36 62 } 37 63 64 /** 65 * 66 */ 38 67 function generateText() { 39 $size = min( $this->svg->getHeight(), $this->svg->getWidth() );68 $size = min( $this->svg->getHeight(), $this->svg->getWidth() ); 40 69 $text_height = floor( $size * 0.8 ) . 'px'; 41 70 … … 43 72 } 44 73 74 /** 75 * @param int $width 76 * @param int $height 77 * @return string 78 */ 45 79 public function toSVG( $width = 128, $height = 128 ) { 46 80 $this->svg = new Plugin_Geopattern_SVG(); 47 81 $this->generateBackground(); 48 82 49 // Work around a bug in 1.1.0: the hash-based pattern selection doesn't account for the size of the pattern array and can choose a null result. 50 $this->setGenerator( $this->patterns[$this->hexVal(20, 1) % count( $this->patterns )] ); 83 /* 84 * Work around a bug in 1.1.0: 85 * The hash-based pattern selection doesn't account for the size of the 86 * pattern array and can choose a null result. 87 */ 88 $this->setGenerator( $this->patterns[ $this->hexVal( 20, 1 ) % count( $this->patterns ) ] ); 51 89 52 90 $this->generatePattern(); … … 59 97 $inner = $this->svg; 60 98 61 // Outer SVG, containing the text and nested inner SVG. 62 // Needed because of aspect ratio problems with the background pattern. The outer is square, inner may be a different shape. 99 /* 100 * Outer SVG, containing the text and nested inner SVG. 101 * 102 * Needed because of aspect ratio problems with the background pattern. 103 * The outer is square, inner may be a different shape. 104 */ 63 105 $this->svg = new PluginSVG(); 64 106 $this->svg->setWidth( $width ); … … 69 111 70 112 return $this->svg->getString(); 71 72 }73 74 }75 76 // The base SVG class doesn't provide functions for getting dimensions, so..77 class Plugin_Geopattern_SVG extends \RedeyeVentures\GeoPattern\SVG {78 79 protected $viewbox;80 81 function getWidth() {82 return $this->width;83 }84 85 function getHeight() {86 return $this->height;87 }88 89 protected function getSvgHeader() {90 if ( $this->viewbox )91 return "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{$this->width}\" height=\"{$this->height}\" viewbox=\"{$this->viewbox}\" preserveAspectRatio=\"none\">";92 else93 return "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{$this->width}\" height=\"{$this->height}\">";94 }95 96 public function addText( $text, $x, $y, $text_anchor, $style, $args=array() ) {97 $element = new Plugin_Geopattern_SVGText($text, $x, $y, $text_anchor, $style, $args);98 $this->svgString .= $element;99 return $this;100 }101 102 public function addNested( $svg ) {103 if ( method_exists( $svg, 'getString' ) )104 $this->svgString .= $svg->getString();105 }106 107 public function setViewBox( $x, $y, $w, $h ) {108 $this->viewbox = esc_attr( "$x $y $w $h" );109 }110 111 }112 113 // Nor does it support text.114 class Plugin_Geopattern_SVGText extends \RedeyeVentures\GeoPattern\SVGElements\Base {115 protected $tag = 'text';116 protected $text;117 118 function __construct($text, $x, $y, $text_anchor, $style, $args=array()) {119 $this->elements = array(120 'x' => $x,121 'y' => $y,122 'text-anchor' => $text_anchor,123 'style' => $style,124 );125 $this->text = esc_html( $text );126 parent::__construct($args);127 }128 129 public function getString() {130 return "<{$this->tag}{$this->elementsToString()}{$this->argsToString()}>{$this->text}</{$this->tag}>";131 113 } 132 114 } 133
Note: See TracChangeset
for help on using the changeset viewer.