eZ Publish  [4.0]
ezimagetextlayer.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZImageTextLayer class
00004 //
00005 // Created on: <03-Oct-2002 15:05:09 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezimagetextlayer.php
00032 */
00033 
00034 /*!
00035   \class eZImageTextLayer ezimagetextlayer.php
00036   \ingroup eZImageObject
00037   \brief Layer for text information and rendering
00038 
00039 */
00040 
00041 //include_once( 'lib/ezimage/classes/ezimagelayer.php' );
00042 //include_once( 'lib/ezimage/classes/ezimagefont.php' );
00043 
00044 class eZImageTextLayer extends eZImageLayer
00045 {
00046     /*!
00047      Constructor
00048     */
00049     function eZImageTextLayer( $imageObjectRef = null, $imageObject = null,
00050                                   $width = false, $height = false,
00051                                   $font = false, $boundingBox = null, $text = null, $textAngle = 0 )
00052     {
00053         $this->eZImageLayer( $imageObjectRef, $imageObject, $width, $height, $font );
00054         $this->Text = $text;
00055         $this->TextAngle = $textAngle;
00056         $this->TextBoundingBox = $boundingBox;
00057     }
00058 
00059     /*!
00060      \virtual
00061      Draws the text on the current image object.
00062     */
00063     function processImage()
00064     {
00065         $destinationImageObject = $this->imageObjectInternal();
00066         $bbox = $this->textBoundingBox();
00067         $this->clear();
00068         $font = $this->font();
00069         $this->drawText( $font, $this->textColor(), $this->text(), $bbox[6], -$bbox[7], $this->textAngle(),
00070                          $destinationImageObject );
00071         return true;
00072     }
00073 
00074     /*!
00075      Renders the text with the other layer data. It will perform something
00076      that will look like alphablending of the text.
00077 
00078      It will copy the area which it will render on from the other layer
00079      and render on it and then merge the result back on the other layer
00080      using the transparency value. This means that the original image data
00081      is kept and the actual text will be transparent.
00082     */
00083     function mergeLayer( $image, $layerData, $lastLayerData )
00084     {
00085         $position = $image->calculatePosition( $layerData['parameters'], $this->width(), $this->height() );
00086         $x = $position['x'];
00087         $y = $position['y'];
00088         $destinationImageObject = $image->imageObjectInternal( false );
00089         if ( $lastLayerData === null and
00090              $destinationImageObject === null )
00091         {
00092             $destinationImageObject = $image->imageObjectInternal();
00093             $bbox = $this->textBoundingBox();
00094             $font = $this->font();
00095             $this->drawText( $font, $this->textColor(), $this->text(), $bbox[6], -$bbox[7], $this->textAngle(),
00096                              $destinationImageObject );
00097         }
00098         else
00099         {
00100             $destinationImageObject = $image->imageObjectInternal();
00101             $imageObject = $this->imageObjectInternal();
00102             $bbox = $this->textBoundingBox();
00103             $image->copyImage( $imageObject, $destinationImageObject,
00104                                0, 0, $this->width(), $this->height(),
00105                                $x, $y );
00106             $font = $this->font();
00107             $this->drawText( $font, $this->textColor(), $this->text(), $bbox[6], -$bbox[7], $this->textAngle() );
00108             $image->mergeImage( $destinationImageObject, $imageObject,
00109                                 $x, $y,
00110                                 $this->width(), $this->height(), 0, 0,
00111                                 $image->getTransparencyPercent( $layerData['parameters'] ) );
00112         }
00113     }
00114 
00115     /*!
00116      Sets the current text to \a $text.
00117     */
00118     function setText( $text )
00119     {
00120         $this->Text = $text;
00121     }
00122 
00123     /*!
00124      \return the current text.
00125     */
00126     function text()
00127     {
00128         return $this->Text;
00129     }
00130 
00131     /*!
00132      Sets the angle of the text to \a $textAngle.
00133     */
00134     function setTextAngle( $textAngle )
00135     {
00136         $this->TextAngle = $textAngle;
00137     }
00138 
00139     /*!
00140      \return the current text angle.
00141     */
00142     function textAngle()
00143     {
00144         return $this->TextAngle;
00145     }
00146 
00147     /*!
00148      \return the current bounding box for the text. See the PHP function ImageTTFBBox for more info.
00149     */
00150     function textBoundingBox()
00151     {
00152         return $this->TextBoundingBox;
00153     }
00154 
00155     /*!
00156      Creates a new text layer with the text \a $text, font \a $font and adjustment
00157      \a $widthAdjustment and \a $heightAdjustment at the angle \a $angle and returns it.
00158     */
00159     static function createForText( $text, &$font, $widthAdjustment, $heightAdjustment, $angle,
00160                              $absoluteWidth = false, $absoluteHeight = false )
00161     {
00162         $Return = false;
00163         if ( !( $font instanceof eZImageFont ) )
00164             return $Return;
00165         if ( !function_exists( 'ImageTTFBBox' ) )
00166         {
00167             eZDebug::writeError( 'ImageTTFBBox function not in PHP, check PHP compilation', 'ezimagetextlayer.php' );
00168             return $Return;
00169         }
00170         $bbox = ImageTTFBBox( $font->pointSize(), $angle, $font->realFile(), $text );
00171 
00172         if ( !$bbox )
00173             return $Return;
00174 
00175         $xmin = min( $bbox[0], $bbox[2], $bbox[4], $bbox[6] );
00176         $xmax = max( $bbox[0], $bbox[2], $bbox[4], $bbox[6] );
00177         $ymin = min( $bbox[1], $bbox[3], $bbox[5], $bbox[7] );
00178         $ymax = max( $bbox[1], $bbox[3], $bbox[5], $bbox[7] );
00179 
00180         $width = abs( $xmax - $xmin );
00181         $height = abs( $ymax - $ymin );
00182         $width += $widthAdjustment;
00183         $height += $heightAdjustment;
00184 
00185         if ( $absoluteWidth !== false )
00186             $width = $absoluteWidth;
00187         if ( $absoluteHeight !== false)
00188             $height = $absoluteHeight;
00189 
00190         $layer = new eZImageTextLayer( null, null, $width, $height,
00191                                        $font, $bbox, $text, $angle );
00192         $layer->create( $width, $height, null );
00193         return $layer;
00194     }
00195 
00196     /// \privatesection
00197     public $TextBoundingBox;
00198     public $Text;
00199     public $Angle;
00200 }
00201 
00202 ?>