ParFormat.php 9.43 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
<?php
/*
    PHPRtfLite
    Copyright 2007-2008 Denis Slaveckij <sinedas@gmail.com>
    Copyright 2010-2012 Steffen Zeidler <sigma_z@sigma-scripts.de>

    This file is part of PHPRtfLite.

    PHPRtfLite is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PHPRtfLite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with PHPRtfLite.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * paragraph formatting class for rtf documents.
 * @version     1.2
 * @author      Denis Slaveckij <sinedas@gmail.com>
 * @author      Steffen Zeidler <sigma_z@sigma-scripts.de>
 * @copyright   2007-2008 Denis Slaveckij, 2010-2012 Steffen Zeidler
 * @package     PHPRtfLite
 */
class PHPRtfLite_ParFormat
{

    /**
     * constants for text align
     */
    const TEXT_ALIGN_LEFT   = 'left';
    const TEXT_ALIGN_CENTER = 'center';
    const TEXT_ALIGN_RIGHT  = 'right';
    const TEXT_ALIGN_JUSTIFY = 'justify';


    /**
     * rtf color table
     * @var PHPRtfLite_DocHead_ColorTable
     */
    protected $_colorTable;

    /**
     * text alignment
     * @var string
     */
    protected $_alignment;

    /**
     * indention of first line
     * @var float
     */
    protected $_indentFirstLine = 0;

    /**
     * left indention of paragraph
     * @var float
     */
    protected $_indentLeft = 0;

    /**
     * right indention of paragraph
     * @var float
     */
    protected $_indentRight = 0;

    /**
     * space before paragraph
     * @var float
     */
    protected $_spaceBefore = 0;

    /**
     * space after paragraph
     * @var float
     */
    protected $_spaceAfter = 0;

    /**
     * space between each line of paragraph
     * @var float
     */
    protected $_spaceBetweenLines = 0;

    /**
     * shading of paragraph
     * @var integer
     */
    protected $_shading = 0;

    /**
     * background color of paragraph
     * @var string
     */
    protected $_backgroundColor;

    /**
     * border instance
     * @var PHPRtfLite_Border
     */
    protected $_border;


    /**
     * constructor
     *
     * @param   string  $alignment  represented by class constants TEXT_ALIGN_*<br>
     *   Possible values:<br>
     *     TEXT_ALIGN_LEFT      => 'left'    - left alignment<br>
     *     TEXT_ALIGN_RIGHT     => 'right'   - right alignment<br>
     *     TEXT_ALIGN_CENTER    => 'center'  - center alignment<br>
     *     TEXT_ALIGN_JUSTIFY   => 'justify' - justify alignment
     */
    public function __construct($alignment = self::TEXT_ALIGN_LEFT)
    {
        $this->_alignment = $alignment;
    }


    /**
     * sets text alignment
     *
     * @param   string  $alignment  represented by class constants TEXT_ALIGN_*<br>
     *   Possible values:<br>
     *     TEXT_ALIGN_LEFT      => 'left'    - left alignment<br>
     *     TEXT_ALIGN_RIGHT     => 'right'   - right alignment<br>
     *     TEXT_ALIGN_CENTER    => 'center'  - center alignment<br>
     *     TEXT_ALIGN_JUSTIFY   => 'justify' - justify alignment
     */
    public function setTextAlignment($alignment)
    {
        $this->_alignment = $alignment;
    }


    /**
     * gets text alignment
     *
     * @return string
     */
    public function getTextAlignment()
    {
        return $this->_alignment;
    }


    /**
     * sets first line indention in centimeter (default 0)
     *
     * @param   float   $indentFirst
     */
    public function setIndentFirstLine($indentFirst)
    {
        $this->_indentFirstLine = $indentFirst;
    }


    /**
     * gets first line indention in centimeter
     *
     * @return float
     */
    public function getIndentFirstLine()
    {
        return $this->_indentFirstLine;
    }


    /**
     * sets left indent in centimeter (default 0)
     *
     * @param   float   $indentLeft
     */
    public function setIndentLeft($indentLeft)
    {
        $this->_indentLeft = $indentLeft;
    }


    /**
     * gets left indent in centimeter
     *
     * @return float
     */
    public function getIndentLeft()
    {
        return $this->_indentLeft;
    }


    /**
     * sets right indent in centimeter (default 0)
     *
     * @param   float   $indentRight
     */
    public function setIndentRight($indentRight)
    {
        $this->_indentRight = $indentRight;
    }


    /**
     * gets right indent in centimeter
     *
     * @return float
     */
    public function getIndentRight()
    {
        return $this->_indentRight;
    }


    /**
     * sets the space before paragraph
     *
     * @param   float $spaceBefore space before
     */
    public function setSpaceBefore($spaceBefore)
    {
        $this->_spaceBefore = round($spaceBefore * PHPRtfLite::SPACE_IN_POINTS);
    }


    /**
     * gets the space before paragraph
     *
     * @return  integer
     */
    public function getSpaceBefore()
    {
        return $this->_spaceBefore;
    }


    /**
     * sets the space after paragraph
     *
     * @param integer $spaceAfter space after
     */
    public function setSpaceAfter($spaceAfter)
    {
        $this->_spaceAfter = round($spaceAfter * PHPRtfLite::SPACE_IN_POINTS);
    }


    /**
     * gets the space after paragraph
     *
     * @return integer
     */
    public function getSpaceAfter()
    {
        return $this->_spaceAfter;
    }


    /**
     * sets line space
     *
     * @param   float     $spaceBetweenLines  space between lines
     */
    public function setSpaceBetweenLines($spaceBetweenLines)
    {
        $this->_spaceBetweenLines = round($spaceBetweenLines * PHPRtfLite::SPACE_IN_LINES);
    }


    /**
     * gets line space
     *
     * @return  integer
     */
    public function getSpaceBetweenLines()
    {
        return $this->_spaceBetweenLines;
    }


    /**
     * sets shading
     *
     * @param   integer $shading shading in percents (from 0 till 100)
     */
    public function setShading($shading)
    {
        $this->_shading = $shading * 100;
    }


    /**
     * gets shading
     *
     * @return  integer
     */
    public function getShading()
    {
        return $this->_shading;
    }


    /**
     * sets rtf color table
     *
     * @param PHPRtfLite_DocHead_ColorTable $colorTable
     */
    public function setColorTable(PHPRtfLite_DocHead_ColorTable $colorTable)
    {
        if ($this->_backgroundColor) {
            $colorTable->add($this->_backgroundColor);
        }
        $this->_colorTable = $colorTable;
    }


    /**
     * sets background color
     *
     * @param   string  $backgroundColor
     */
    public function setBackgroundColor($backgroundColor)
    {
        $this->_backgroundColor = $backgroundColor;
        if ($this->_backgroundColor && $this->_colorTable) {
            $this->_colorTable->add($this->_backgroundColor);
        }
    }


    /**
     * gets background color
     *
     * @return  string
     */
    public function getBackgroundColor()
    {
        return $this->_backgroundColor;
    }


    /**
     * sets border of paragraph
     *
     * @param PHPRtfLite_Border $border
     */
    public function setBorder(PHPRtfLite_Border $border)
    {
        $this->_border = $border;
    }


    /**
     * gets border of paragraph
     *
     * @return PHPRtfLite_Border
     */
    public function getBorder()
    {
        return $this->_border;
    }


    /**
     * gets rtf code of paragraph
     *
     * @return  string  rtf code
     */
    public function getContent()
    {
        $content = '';

        switch ($this->_alignment) {
            case self::TEXT_ALIGN_RIGHT:
                $content .= '\qr ';
                break;

            case self::TEXT_ALIGN_CENTER:
                $content .= '\qc ';
                break;

            case self::TEXT_ALIGN_JUSTIFY:
                $content .= '\qj ';
                break;

            default:
                $content .= '\ql ';
                break;
        }

        if ($this->_indentFirstLine != 0) {
            $content .= '\fi' . PHPRtfLite_Unit::getUnitInTwips($this->_indentFirstLine) . ' ';
        }

        if ($this->_indentLeft > 0) {
            $content .= '\li' . PHPRtfLite_Unit::getUnitInTwips($this->_indentLeft) . ' ';
        }

        if ($this->_indentRight > 0) {
            $content .= '\ri' . PHPRtfLite_Unit::getUnitInTwips($this->_indentRight) .' ';
        }

        if ($this->_spaceBefore > 0) {
            $content .= '\sb' . $this->_spaceBefore.' ';
        }

        if ($this->_spaceAfter > 0) {
            $content .= '\sa' . $this->_spaceAfter.' ';
        }

        if ($this->_spaceBetweenLines > 0) {
            $content .= '\sl' . $this->_spaceBetweenLines.' ';
        }

        if ($this->_border) {
            $content .= $this->_border->getContent('\\');
        }

        if ($this->_shading > 0) {
            $content .= '\shading' . $this->_shading . ' ';
        }

        if ($this->_backgroundColor && $this->_colorTable) {
            $colorIndex = $this->_colorTable->getColorIndex($this->_backgroundColor);
            if ($colorIndex !== false) {
                $content .= '\cbpat' . $colorIndex . ' ';
            }
        }

        return $content;
    }

}