Source of file Line.php
Size: 4,895 Bytes - Last Modified: 2020-01-07T10:44:43+00:00
/var/www/docs.ssmods.com/process/src/code/views/Line.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | <?php class Line extends ViewableData { /** * @var ArrayList */ private $resizedImages; /** * Line constructor. */ public function __construct() { parent::__construct(); $this->resizedImages = new ArrayList(); } /** * Returns the height that the lines is optimized for. * @return int */ public static function getOptimizedLineWidth() { return (int) self::config()->get('optimized_line_width'); } /** * Returns the estimated height of this line. * @return int */ public static function getEstimatedLineHeight() { return (int) self::config()->get('estimated_line_height'); } /** * Returns the current real line height. * @return int */ public function getLineHeight() { return isset($this->resizedImages[0]) ? $this->getLineHeightWithoutMargin() + $this->resizedImages[0]->getMarginTop() : 0; } /** * Returns the line height without any margins. * @return int */ public function getLineHeightWithoutMargin() { return isset($this->resizedImages[0]) ? $this->resizedImages[0]->getHeight() : 0; } /** * Adds an image to this line. * This function does not resize the image. * @param ResizedImage $image */ public function addResizedImage(ResizedImage $image) { $this->resizedImages[] = $image; } /** * Returns all images in this line. * @return ArrayList */ public function getResizedImages() { return $this->resizedImages; } /** * Checks if the $image match still to this line without resizing any image. * @param ResizedImage $image * @return bool */ public function matchesToLine(ResizedImage $image) { return $image->getWidth() + $image->getMarginRight() + $this->getLineWidth() <= static::getOptimizedLineWidth(); } /** * Resize images so that all images match exactly the line's width. * @return void */ public function matchImagesWidth() { if ($this->getLineWidthWithoutMargin() !== 0) { $factor = (static::getOptimizedLineWidth() - $this->getCompleteLineMargin()) / $this->getLineWidthWithoutMargin(); if ($factor !== 1) { foreach ($this->resizedImages as $image) $image->changeWidth($image->getWidth() * $factor); } } } /** * Returns the line width. * @return int */ public function getLineWidth() { return $this->getLineWidthWithoutMargin() + $this->getCompleteLineMargin(); } /** * Returns the line width without any margin. * @return int */ public function getLineWidthWithoutMargin() { $lineWidth = 0; foreach ($this->resizedImages as $image) { $lineWidth += $image->getWidth(); } return $lineWidth; } /** * Returns the sum of the image's margin of this line. * F.i This line contains 4 images. Each has margin at the right side of 3px. * This function would return 12 for this case (3 + 3 + 3 + 3). * @return int */ public function getCompleteLineMargin() { $margin = 0; foreach ($this->resizedImages as $image) $margin += $image->getMarginRight(); return $margin; } /** * Calculates the difference between the estimated line height and the current, real line height. * @return int */ public function getDifferenceFromEstimatedHeight() { return abs(static::getEstimatedLineHeight() - $this->getLineHeight()); } /** * Clones this object. * Creates a deep copy of this object. * Attention: The $resizedImage does not create a deep copy of itself when it's cloned. * So the real image behind $resizedImage is still the same. */ public function __clone() { $clonedResizedImages = new ArrayList(); foreach ($this->resizedImages as $resizedImage) $clonedResizedImages[] = clone $resizedImage; $this->resizedImages = $clonedResizedImages; } /** * Sets the width, right margin and the top margin of this image in percentage of this line's width. */ public function setPercentageSizes() { $lineWidth = $this->getLineWidth(); foreach ($this->resizedImages as $image) { /** @var ResizedImage $image */ $image->setPercentageWidth($image->getWidth() / $lineWidth * 100); $image->setPercentageMarginRight($image->getMarginRight() / $lineWidth * 100); $image->setPercentageMarginTop($image->getMarginTop() / $lineWidth * 100); } } /** * This function is called when this object is rendered in a template. * @return HTMLText */ public function forTemplate() { $this->setPercentageSizes(); return $this->renderWith('Line'); } } |