Add text-related APIs to image adapter
authorMatthias Schmidt <gravatronics@live.com>
Sun, 10 May 2015 08:04:59 +0000 (10:04 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 10 May 2015 08:04:59 +0000 (10:04 +0200)
wcfsetup/install/files/lib/system/image/adapter/GDImageAdapter.class.php
wcfsetup/install/files/lib/system/image/adapter/IImageAdapter.class.php
wcfsetup/install/files/lib/system/image/adapter/ImageAdapter.class.php
wcfsetup/install/files/lib/system/image/adapter/ImagickImageAdapter.class.php

index f38a9652b79459c83c0f025a043b16829b0d5c3a..1ab8ed2cb0eb064ec3c4befaec4b5c778ce14675 100644 (file)
@@ -214,21 +214,21 @@ class GDImageAdapter implements IImageAdapter {
                        case 'middleLeft':
                        case 'bottomLeft':
                                $x = $margin;
-                               break;
-               
+                       break;
+                       
                        case 'topCenter':
                        case 'middleCenter':
                        case 'bottomCenter':
                                $x = floor(($this->getWidth() - $textWidth) / 2);
-                               break;
-               
+                       break;
+                       
                        case 'topRight':
                        case 'middleRight':
                        case 'bottomRight':
                                $x = $this->getWidth() - $textWidth - $margin;
-                               break;
+                       break;
                }
-                       
+               
                // calculate y coordinate
                $y = 0;
                switch ($position) {
@@ -236,24 +236,43 @@ class GDImageAdapter implements IImageAdapter {
                        case 'topCenter':
                        case 'topRight':
                                $y = $margin + $firstLineHeight;
-                               break;
-               
+                       break;
+                       
                        case 'middleLeft':
                        case 'middleCenter':
                        case 'middleRight':
                                $y = floor(($this->getHeight() - $textHeight) / 2) + $firstLineHeight;
-                               break;
-               
+                       break;
+                       
                        case 'bottomLeft':
                        case 'bottomCenter':
                        case 'bottomRight':
                                $y = $this->getHeight() - $textHeight + $firstLineHeight - $margin;
-                               break;
+                       break;
                }
                
                $this->drawText($text, $x + $offsetX, $y + $offsetY, $font, $size, $opacity);
        }
        
+       /**
+        * @see \wcf\system\image\adapter\IImageAdapter::textFitsImage()
+        */
+       public function textFitsImage($text, $margin, $font, $size) {
+               $box = imagettfbbox($size, 0, $font, $text);
+               
+               $textWidth = abs($box[0] - $box[2]);
+               $textHeight = abs($box[7] - $box[1]);
+               
+               return ($textWidth + 2 * $margin <= $this->getWidth() && $textHeight + 2 * $margin <= $this->getHeight());
+       }
+       
+       /**
+        * @see \wcf\system\image\adapter\IImageAdapter::adjustFontSize()
+        */
+       public function adjustFontSize($text, $margin, $font, $size) {
+               // does nothing
+       }
+       
        /**
         * @see \wcf\system\image\adapter\IImageAdapter::setColor()
         */
index 4e459392f15b13eac1d4d2fadd8f24aeb64887d9..159d810536d03f5d4dd8ac99eeb23cd1cc991c52 100644 (file)
@@ -110,6 +110,30 @@ interface IImageAdapter {
         */
        public function drawTextRelative($text, $position, $margin, $offsetX, $offsetY, $font, $size, $opacity = 1);
        
+       /**
+        * Returns true if the given text fits the image.
+        * 
+        * @param       string          $text
+        * @param       integer         $margin
+        * @param       string          $font           path to TrueType font file
+        * @param       integer         $size           font size
+        * @return      integer
+        * @return      boolean
+        */
+       public function textFitsImage($text, $margin, $font, $size);
+       
+       /**
+        * Adjusts the given font size so that the given text fits on the current
+        * image. Returns 0 if no appropriate font size could be determined.
+        * 
+        * @param       string          $text
+        * @param       integer         $margin
+        * @param       string          $font           path to TrueType font file
+        * @param       integer         $size           font size
+        * @return      integer
+        */
+       public function adjustFontSize($text, $margin, $font, $size);
+       
        /**
         * Sets active color.
         * 
index b4464e7c627041faca122046c4b9523ead47317b..f9edb189ea8b0387fc39da664d44007d1086ca56 100644 (file)
@@ -167,6 +167,25 @@ class ImageAdapter implements IImageAdapter {
                $this->adapter->drawTextRelative($text, $position, $margin, $offsetX, $offsetY, $font, $size, $opacity);
        }
        
+       /**
+        * @see \wcf\system\image\adapter\IImageAdapter::textFitsImage()
+        */
+       public function textFitsImage($text, $margin, $font, $size) {
+               return $this->adapter->textFitsImage($text, $margin, $font, $size);
+       }
+       
+       /**
+        * @see \wcf\system\image\adapter\IImageAdapter::adjustFontSize()
+        */
+       public function adjustFontSize($text, $margin, $font, $size) {
+               // adjust font size
+               while ($size && !$this->textFitsImage($text, $margin, $font, $size)) {
+                       $size--;
+               }
+               
+               return $size;
+       }
+       
        /**
         * @see \wcf\system\image\adapter\IImageAdapter::setColor()
         */
index 35f66c449e397542ab6f13d01d6270e112777e1d..bcc50316acb7f5c1900bbf6b5460bd1fcf258cd6 100644 (file)
@@ -200,7 +200,7 @@ class ImagickImageAdapter implements IImageAdapter {
                
                if ($this->imagick->getImageFormat() == 'GIF') {
                        $this->imagick = $this->imagick->coalesceImages();
-                               
+                       
                        do {
                                $this->imagick->drawImage($draw);
                        }
@@ -268,6 +268,25 @@ class ImagickImageAdapter implements IImageAdapter {
                $this->drawText($text, $x + $offsetX, $y + $offsetY, $font, $size, $opacity);
        }
        
+       /**
+        * @see \wcf\system\image\adapter\IImageAdapter::textFitsImage()
+        */
+       public function textFitsImage($text, $margin, $font, $size) {
+               $draw = new \ImagickDraw();
+               $draw->setFont($font);
+               $draw->setFontSize($size);
+               $metrics = $this->imagick->queryFontMetrics($draw, $text);
+               
+               return ($metrics['textWidth'] + 2 * $margin <= $this->getWidth() && $metrics['textHeight'] + 2 * $margin <= $this->getHeight());
+       }
+       
+       /**
+        * @see \wcf\system\image\adapter\IImageAdapter::adjustFontSize()
+        */
+       public function adjustFontSize($text, $margin, $font, $size) {
+               // does nothing
+       }
+       
        /**
         * @see \wcf\system\image\adapter\IImageAdapter::setColor()
         */