vendor/amenadiel/jpgraph/src/text/Text.php line 301

  1. <?php
  2. /**
  3.  * JPGraph v4.0.3
  4.  */
  5. namespace Amenadiel\JpGraph\Text;
  6. //use Amenadiel\JpGraph\Graph\Graph;
  7. use Amenadiel\JpGraph\Util;
  8. /**
  9.  * File:        JPGRAPH_TEXT.INC.PHP
  10.  * // Description: Class to handle text as object in the graph.
  11.  * //              The low level text layout engine is handled by the GD class
  12.  * // Created:     2001-01-08 (Refactored to separate file 2008-08-01)
  13.  * // Ver:         $Id: jpgraph_text.inc.php 1844 2009-09-26 17:05:31Z ljp $
  14.  * //
  15.  * // Copyright (c) Asial Corporation. All rights reserved.
  16.  */
  17. /**
  18.  * @class Text
  19.  * // Description: Arbitrary text object that can be added to the graph
  20.  */
  21. class Text //extends Graph
  22. {
  23.     public $t;
  24.     public $x      0;
  25.     public $y      0;
  26.     public $halign 'left';
  27.     public $valign 'top';
  28.     public $color  = [000];
  29.     public $hide   false;
  30.     public $dir    0;
  31.     public $iScalePosY;
  32.     public $iScalePosX;
  33.     public $iWordwrap          0;
  34.     public $font_family        FF_DEFAULT;
  35.     public $font_style         FS_NORMAL// old. FF_FONT1
  36.     public $boxed              false// Should the text be boxed
  37.     protected $paragraph_align 'left';
  38.     protected $icornerradius   0;
  39.     protected $ishadowwidth    3;
  40.     protected $fcolor          'white';
  41.     protected $bcolor          'black';
  42.     protected $shadow          false;
  43.     protected $iCSIMarea       '';
  44.     protected $iCSIMalt        '';
  45.     protected $iCSIMtarget     '';
  46.     protected $iCSIMWinTarget  '';
  47.     private $iBoxType          1// Which variant of filled box around text we want
  48.     // for __get, __set
  49.     private $_margin;
  50.     private $_font_size 8// old. 12
  51.     /**
  52.      * CONSTRUCTOR.
  53.      *
  54.      * @param mixed $aTxt
  55.      * @param mixed $aXAbsPos
  56.      * @param mixed $aYAbsPos
  57.      */
  58.     // Create new text at absolute pixel coordinates
  59.     public function __construct($aTxt ''$aXAbsPos 0$aYAbsPos 0)
  60.     {
  61.         if (!is_string($aTxt)) {
  62.             Util\JpGraphError::RaiseL(25050); //('First argument to Text::Text() must be s atring.');
  63.         }
  64.         $this->t      $aTxt;
  65.         $this->x      round($aXAbsPos);
  66.         $this->y      round($aYAbsPos);
  67.         $this->margin 0;
  68.     }
  69.     /**
  70.      * PUBLIC METHODS.
  71.      *
  72.      * @param mixed $aTxt
  73.      */
  74.     // Set the string in the text object
  75.     public function Set($aTxt)
  76.     {
  77.         $this->$aTxt;
  78.     }
  79.     // Alias for Pos()
  80.     public function SetPos($aXAbsPos 0$aYAbsPos 0$aHAlign 'left'$aVAlign 'top')
  81.     {
  82.         //$this->Pos($aXAbsPos,$aYAbsPos,$aHAlign,$aVAlign);
  83.         $this->x      $aXAbsPos;
  84.         $this->y      $aYAbsPos;
  85.         $this->halign $aHAlign;
  86.         $this->valign $aVAlign;
  87.     }
  88.     public function SetScalePos($aX$aY)
  89.     {
  90.         $this->iScalePosX $aX;
  91.         $this->iScalePosY $aY;
  92.     }
  93.     // Specify alignment for the text
  94.     public function Align($aHAlign$aVAlign 'top'$aParagraphAlign '')
  95.     {
  96.         $this->halign $aHAlign;
  97.         $this->valign $aVAlign;
  98.         if ($aParagraphAlign != '') {
  99.             $this->paragraph_align $aParagraphAlign;
  100.         }
  101.     }
  102.     // Alias
  103.     public function SetAlign($aHAlign$aVAlign 'top'$aParagraphAlign '')
  104.     {
  105.         $this->Align($aHAlign$aVAlign$aParagraphAlign);
  106.     }
  107.     // Specifies the alignment for a multi line text
  108.     public function ParagraphAlign($aAlign)
  109.     {
  110.         $this->paragraph_align $aAlign;
  111.     }
  112.     // Specifies the alignment for a multi line text
  113.     public function SetParagraphAlign($aAlign)
  114.     {
  115.         $this->paragraph_align $aAlign;
  116.     }
  117.     public function SetShadow($aShadowColor 'gray'$aShadowWidth 3)
  118.     {
  119.         $this->ishadowwidth $aShadowWidth;
  120.         $this->shadow       $aShadowColor;
  121.         $this->boxed        true;
  122.     }
  123.     public function SetWordWrap($aCol)
  124.     {
  125.         $this->iWordwrap $aCol;
  126.     }
  127.     // Specify that the text should be boxed. fcolor=frame color, bcolor=border color,
  128.     // $shadow=drop shadow should be added around the text.
  129.     public function SetBox($aFrameColor = [255255255], $aBorderColor = [000], $aShadowColor false$aCornerRadius 4$aShadowWidth 3)
  130.     {
  131.         if ($aFrameColor === false) {
  132.             $this->boxed false;
  133.         } else {
  134.             $this->boxed true;
  135.         }
  136.         $this->fcolor $aFrameColor;
  137.         $this->bcolor $aBorderColor;
  138.         // For backwards compatibility when shadow was just true or false
  139.         if ($aShadowColor === true) {
  140.             $aShadowColor 'gray';
  141.         }
  142.         $this->shadow        $aShadowColor;
  143.         $this->icornerradius $aCornerRadius;
  144.         $this->ishadowwidth  $aShadowWidth;
  145.     }
  146.     public function SetBox2($aFrameColor = [255255255], $aBorderColor = [000], $aShadowColor false$aCornerRadius 4$aShadowWidth 3)
  147.     {
  148.         $this->iBoxType 2;
  149.         $this->SetBox($aFrameColor$aBorderColor$aShadowColor$aCornerRadius$aShadowWidth);
  150.     }
  151.     // Hide the text
  152.     public function Hide($aHide true)
  153.     {
  154.         $this->hide $aHide;
  155.     }
  156.     // This looks ugly since it's not a very orthogonal design
  157.     // but I added this "inverse" of Hide() to harmonize
  158.     // with some classes which I designed more recently (especially)
  159.     // jpgraph_gantt
  160.     public function Show($aShow true)
  161.     {
  162.         $this->hide = !$aShow;
  163.     }
  164.     // Specify font
  165.     public function SetFont($aFamily$aStyle FS_NORMAL$aSize 10)
  166.     {
  167.         $this->font_family $aFamily;
  168.         $this->font_style  $aStyle;
  169.         $this->font_size   $aSize;
  170.     }
  171.     // Center the text between $left and $right coordinates
  172.     public function Center($aLeft$aRight$aYAbsPos false)
  173.     {
  174.         $this->x      $aLeft + ($aRight $aLeft) / 2;
  175.         $this->halign 'center';
  176.         if (is_numeric($aYAbsPos)) {
  177.             $this->$aYAbsPos;
  178.         }
  179.     }
  180.     // Set text color
  181.     public function SetColor($aColor)
  182.     {
  183.         $this->color $aColor;
  184.     }
  185.     public function SetAngle($aAngle)
  186.     {
  187.         $this->SetOrientation($aAngle);
  188.     }
  189.     // Orientation of text. Note only TTF fonts can have an arbitrary angle
  190.     public function SetOrientation($aDirection 0)
  191.     {
  192.         if (is_numeric($aDirection)) {
  193.             $this->dir $aDirection;
  194.         } elseif ($aDirection == 'h') {
  195.             $this->dir 0;
  196.         } elseif ($aDirection == 'v') {
  197.             $this->dir 90;
  198.         } else {
  199.             Util\JpGraphError::RaiseL(25051);
  200.         }
  201.         //(" Invalid direction specified for text.");
  202.     }
  203.     // Total width of text
  204.     public function GetWidth($aImg)
  205.     {
  206.         $aImg->SetFont($this->font_family$this->font_style$this->raw_font_size);
  207.         $w $aImg->GetTextWidth($this->t$this->dir);
  208.         return $w;
  209.     }
  210.     // Hight of font
  211.     public function GetFontHeight($aImg)
  212.     {
  213.         $aImg->SetFont($this->font_family$this->font_style$this->raw_font_size);
  214.         $h $aImg->GetFontHeight();
  215.         return $h;
  216.     }
  217.     public function GetTextHeight($aImg)
  218.     {
  219.         $aImg->SetFont($this->font_family$this->font_style$this->raw_font_size);
  220.         $h $aImg->GetTextHeight($this->t$this->dir);
  221.         return $h;
  222.     }
  223.     public function GetHeight($aImg)
  224.     {
  225.         // Synonym for GetTextHeight()
  226.         $aImg->SetFont($this->font_family$this->font_style$this->raw_font_size);
  227.         $h $aImg->GetTextHeight($this->t$this->dir);
  228.         return $h;
  229.     }
  230.     // Set the margin which will be interpretated differently depending
  231.     // on the context.
  232.     public function SetMargin($aMarg)
  233.     {
  234.         $this->margin $aMarg;
  235.     }
  236.     public function StrokeWithScale($aImg$axscale$ayscale)
  237.     {
  238.         if ($this->iScalePosX === null || $this->iScalePosY === null) {
  239.             $this->Stroke($aImg);
  240.         } else {
  241.             $this->Stroke(
  242.                 $aImg,
  243.                 round($axscale->Translate($this->iScalePosX)),
  244.                 round($ayscale->Translate($this->iScalePosY))
  245.             );
  246.         }
  247.     }
  248.     public function SetCSIMTarget($aURITarget$aAlt ''$aWinTarget '')
  249.     {
  250.         $this->iCSIMtarget    $aURITarget;
  251.         $this->iCSIMalt       $aAlt;
  252.         $this->iCSIMWinTarget $aWinTarget;
  253.     }
  254.     public function GetCSIMareas()
  255.     {
  256.         if ($this->iCSIMtarget !== '') {
  257.             return $this->iCSIMarea;
  258.         }
  259.         return '';
  260.     }
  261.     // Display text in image
  262.     public function Stroke($aImg$x null$y null)
  263.     {
  264.         if (is_numeric($x)) {
  265.             $this->round($x);
  266.         }
  267.         if (is_numeric($y)) {
  268.             $this->round($y);
  269.         }
  270.         // Insert newlines
  271.         if ($this->iWordwrap 0) {
  272.             $this->wordwrap($this->t$this->iWordwrap"\n");
  273.         }
  274.         // If position been given as a fraction of the image size
  275.         // calculate the absolute position
  276.         if ($this->&& $this->0) {
  277.             $this->*= $aImg->width;
  278.         }
  279.         if ($this->&& $this->0) {
  280.             $this->*= $aImg->height;
  281.         }
  282.         $aImg->PushColor($this->color);
  283.         $aImg->SetFont($this->font_family$this->font_style$this->raw_font_size);
  284.         $aImg->SetTextAlign($this->halign$this->valign);
  285.         if ($this->boxed) {
  286.             if ($this->fcolor == 'nofill') {
  287.                 $this->fcolor false;
  288.             }
  289.             $oldweight $aImg->SetLineWeight(1);
  290.             if ($this->iBoxType == && $this->font_family FF_FONT2 2) {
  291.                 $bbox $aImg->StrokeBoxedText2(
  292.                     $this->x,
  293.                     $this->y,
  294.                     $this->t,
  295.                     $this->dir,
  296.                     $this->fcolor,
  297.                     $this->bcolor,
  298.                     $this->shadow,
  299.                     $this->paragraph_align,
  300.                     2,
  301.                     4,
  302.                     $this->icornerradius,
  303.                     $this->ishadowwidth
  304.                 );
  305.             } else {
  306.                 $bbox $aImg->StrokeBoxedText(
  307.                     $this->x,
  308.                     $this->y,
  309.                     $this->t,
  310.                     $this->dir,
  311.                     $this->fcolor,
  312.                     $this->bcolor,
  313.                     $this->shadow,
  314.                     $this->paragraph_align,
  315.                     3,
  316.                     3,
  317.                     $this->icornerradius,
  318.                     $this->ishadowwidth
  319.                 );
  320.             }
  321.             $aImg->SetLineWeight($oldweight);
  322.         } else {
  323.             $debug false;
  324.             $bbox  $aImg->StrokeText($this->x$this->y$this->t$this->dir$this->paragraph_align$debug);
  325.         }
  326.         // Create CSIM targets
  327.         $coords implode(',', [
  328.             $bbox[0], $bbox[1], $bbox[2], $bbox[3], $bbox[4], $bbox[5], $bbox[6], $bbox[7],
  329.         ]);
  330.         $this->iCSIMarea "<area shape=\"poly\" coords=\"${coords}\" href=\"";
  331.         $this->iCSIMarea .= htmlentities($this->iCSIMtarget) . '" ';
  332.         if (trim($this->iCSIMalt) != '') {
  333.             $this->iCSIMarea .= ' alt="' $this->iCSIMalt '" ';
  334.             $this->iCSIMarea .= ' title="' $this->iCSIMalt '" ';
  335.         }
  336.         if (trim($this->iCSIMWinTarget) != '') {
  337.             $this->iCSIMarea .= ' target="' $this->iCSIMWinTarget '" ';
  338.         }
  339.         $this->iCSIMarea .= " />\n";
  340.         $aImg->PopColor($this->color);
  341.     }
  342.     public function __get($name)
  343.     {
  344.         if (strpos($name'raw_') !== false) {
  345.             // if $name == 'raw_left_margin' , return $this->_left_margin;
  346.             $variable_name '_' str_replace('raw_'''$name);
  347.             return $this->{$variable_name};
  348.         }
  349.         $variable_name '_' $name;
  350.         if (isset($this->{$variable_name})) {
  351.             return $this->{$variable_name} * SUPERSAMPLING_SCALE;
  352.         }
  353.         Util\JpGraphError::RaiseL('25132'$name);
  354.     }
  355.     public function __set($name$value)
  356.     {
  357.         $this->{'_' $name} = $value;
  358.     }
  359. // @class