1 | <?php |
||
2 | |||
3 | /** |
||
4 | * JPGraph v4.0.3 |
||
5 | */ |
||
6 | |||
7 | namespace Amenadiel\JpGraph\Text; |
||
8 | |||
9 | use Amenadiel\JpGraph\Util; |
||
10 | |||
11 | /** |
||
12 | * @class SuperScriptText |
||
13 | * // Description: Format a superscript text |
||
14 | */ |
||
15 | class SuperScriptText extends Text |
||
16 | { |
||
17 | private $iSuper = ''; |
||
18 | private $sfont_family = ''; |
||
19 | private $sfont_style = ''; |
||
20 | private $sfont_size = 8; |
||
21 | private $iSuperMargin = 2; |
||
22 | private $iVertOverlap = 4; |
||
23 | private $iSuperScale = 0.65; |
||
24 | private $iSDir = 0; |
||
25 | private $iSimple = false; |
||
26 | |||
27 | public function __construct($aTxt = '', $aSuper = '', $aXAbsPos = 0, $aYAbsPos = 0) |
||
28 | { |
||
29 | parent::__construct($aTxt, $aXAbsPos, $aYAbsPos); |
||
30 | $this->iSuper = $aSuper; |
||
31 | } |
||
32 | |||
33 | public function FromReal($aVal, $aPrecision = 2) |
||
34 | { |
||
35 | // Convert a floating point number to scientific notation |
||
36 | $neg = 1.0; |
||
37 | if ($aVal < 0) { |
||
38 | $neg = -1.0; |
||
39 | $aVal = -$aVal; |
||
40 | } |
||
41 | |||
42 | $l = floor(log10($aVal)); |
||
43 | $a = sprintf('%0.' . $aPrecision . 'f', round($aVal / pow(10, $l), $aPrecision)); |
||
44 | $a *= $neg; |
||
45 | if ($this->iSimple && ($a == 1 || $a == -1)) { |
||
46 | $a = ''; |
||
47 | } |
||
48 | |||
49 | if ($a != '') { |
||
50 | $this->t = $a . ' * 10'; |
||
51 | } else { |
||
52 | if ($neg == 1) { |
||
53 | $this->t = '10'; |
||
54 | } else { |
||
55 | $this->t = '-10'; |
||
56 | } |
||
57 | } |
||
58 | $this->iSuper = $l; |
||
59 | } |
||
60 | |||
61 | public function Set($aTxt, $aSuper = '') |
||
62 | { |
||
63 | $this->t = $aTxt; |
||
64 | $this->iSuper = $aSuper; |
||
65 | } |
||
66 | |||
67 | public function SetSuperFont($aFontFam, $aFontStyle = FS_NORMAL, $aFontSize = 8) |
||
68 | { |
||
69 | $this->sfont_family = $aFontFam; |
||
70 | $this->sfont_style = $aFontStyle; |
||
71 | $this->sfont_size = $aFontSize; |
||
72 | } |
||
73 | |||
74 | // Total width of text |
||
75 | public function GetWidth($aImg) |
||
76 | { |
||
77 | $aImg->SetFont($this->font_family, $this->font_style, $this->font_size); |
||
78 | $w = $aImg->GetTextWidth($this->t); |
||
79 | $aImg->SetFont($this->sfont_family, $this->sfont_style, $this->sfont_size); |
||
80 | $w += $aImg->GetTextWidth($this->iSuper); |
||
81 | $w += $this->iSuperMargin; |
||
82 | |||
83 | return $w; |
||
84 | } |
||
85 | |||
86 | // Hight of font (approximate the height of the text) |
||
87 | public function GetFontHeight($aImg) |
||
88 | { |
||
89 | $aImg->SetFont($this->font_family, $this->font_style, $this->font_size); |
||
90 | $h = $aImg->GetFontHeight(); |
||
91 | $aImg->SetFont($this->sfont_family, $this->sfont_style, $this->sfont_size); |
||
92 | $h += $aImg->GetFontHeight(); |
||
93 | |||
94 | return $h; |
||
95 | } |
||
96 | |||
97 | // Hight of text |
||
98 | public function GetTextHeight($aImg) |
||
99 | { |
||
100 | $aImg->SetFont($this->font_family, $this->font_style, $this->font_size); |
||
101 | $h = $aImg->GetTextHeight($this->t); |
||
102 | $aImg->SetFont($this->sfont_family, $this->sfont_style, $this->sfont_size); |
||
103 | $h += $aImg->GetTextHeight($this->iSuper); |
||
104 | |||
105 | return $h; |
||
106 | } |
||
107 | |||
108 | public function Stroke($aImg, $ax = -1, $ay = -1) |
||
109 | { |
||
110 | // To position the super script correctly we need different |
||
111 | // cases to handle the alignmewnt specified since that will |
||
112 | // determine how we can interpret the x,y coordinates |
||
113 | |||
114 | $w = parent::GetWidth($aImg); |
||
115 | $h = parent::GetTextHeight($aImg); |
||
116 | switch ($this->valign) { |
||
117 | case 'top': |
||
118 | $sy = $this->y; |
||
119 | |||
120 | break; |
||
121 | case 'center': |
||
122 | $sy = $this->y - $h / 2; |
||
123 | |||
124 | break; |
||
125 | case 'bottom': |
||
126 | $sy = $this->y - $h; |
||
127 | |||
128 | break; |
||
129 | default: |
||
130 | Util\JpGraphError::RaiseL(25052); //('PANIC: Internal error in SuperScript::Stroke(). Unknown vertical alignment for text'); |
||
131 | break; |
||
132 | } |
||
133 | |||
134 | switch ($this->halign) { |
||
135 | case 'left': |
||
136 | $sx = $this->x + $w; |
||
137 | |||
138 | break; |
||
139 | case 'center': |
||
140 | $sx = $this->x + $w / 2; |
||
141 | |||
142 | break; |
||
143 | case 'right': |
||
144 | $sx = $this->x; |
||
145 | |||
146 | break; |
||
147 | default: |
||
148 | Util\JpGraphError::RaiseL(25053); //('PANIC: Internal error in SuperScript::Stroke(). Unknown horizontal alignment for text'); |
||
149 | break; |
||
150 | } |
||
151 | |||
152 | $sx += $this->iSuperMargin; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
153 | $sy += $this->iVertOverlap; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
154 | |||
155 | // Should we automatically determine the font or |
||
156 | // has the user specified it explicetly? |
||
157 | if ($this->sfont_family == '') { |
||
158 | if ($this->font_family <= FF_FONT2) { |
||
159 | if ($this->font_family == FF_FONT0) { |
||
160 | $sff = FF_FONT0; |
||
161 | } elseif ($this->font_family == FF_FONT1) { |
||
162 | if ($this->font_style == FS_NORMAL) { |
||
163 | $sff = FF_FONT0; |
||
164 | } else { |
||
165 | $sff = FF_FONT1; |
||
166 | } |
||
167 | } else { |
||
168 | $sff = FF_FONT1; |
||
169 | } |
||
170 | $sfs = $this->font_style; |
||
171 | $sfz = $this->font_size; |
||
172 | } else { |
||
173 | // TTF fonts |
||
174 | $sff = $this->font_family; |
||
175 | $sfs = $this->font_style; |
||
176 | $sfz = floor($this->font_size * $this->iSuperScale); |
||
177 | if ($sfz < 8) { |
||
178 | $sfz = 8; |
||
179 | } |
||
180 | } |
||
181 | $this->sfont_family = $sff; |
||
182 | $this->sfont_style = $sfs; |
||
183 | $this->sfont_size = $sfz; |
||
184 | } else { |
||
185 | $sff = $this->sfont_family; |
||
186 | $sfs = $this->sfont_style; |
||
187 | $sfz = $this->sfont_size; |
||
188 | } |
||
189 | |||
190 | parent::Stroke($aImg, $ax, $ay); |
||
191 | |||
192 | // For the builtin fonts we need to reduce the margins |
||
193 | // since the bounding bx reported for the builtin fonts |
||
194 | // are much larger than for the TTF fonts. |
||
195 | if ($sff <= FF_FONT2) { |
||
196 | $sx -= 2; |
||
197 | $sy += 3; |
||
198 | } |
||
199 | |||
200 | $aImg->SetTextAlign('left', 'bottom'); |
||
201 | $aImg->SetFont($sff, $sfs, $sfz); |
||
202 | $aImg->PushColor($this->color); |
||
203 | $aImg->StrokeText($sx, $sy, $this->iSuper, $this->iSDir, 'left'); |
||
204 | $aImg->PopColor(); |
||
205 | } |
||
206 | } |
||
207 |