4 class XML_SvgToPDFAlt_Text extends XML_SvgToPDFAlt_Base {
6 function fromNode($node) {
7 parent::fromNode($node);
8 if (!isset($this->children[0]->content)) {
11 if (substr($this->children[0]->content,0,2) == '==') {
12 $this->style['text-anchor'] = 'justify';
13 $this->children[0]->content = substr($this->children[0]->content,2);
17 function transform() {
19 if (!@$this->transform) {
22 if (preg_match('/scale\(([0-9e.-]+),([0-9e.-]+)\)/',$this->transform,$args)) {
25 $this->style['font-size'] *= $args[1];
32 function writePDF($pdf,$data) {
35 $font = strtolower($this->style['font-family']);
38 'times new roman' => 'times',
39 'courier new' => 'courier',
42 if (isset($map[$font])) {
47 if (preg_match('/big5/i',$this->style['font-family'])) {
54 if ($this->style['font-weight'] == 'bold') {
57 if (@$this->style['font-style'] == 'italic') {
61 if (@$this->style['text-decoration'] == 'underline') {
65 $size = $this->style['font-size'] * 0.85;
67 $pdf->setFont($font,$weight,$size);
69 switch(@$this->style['text-anchor']) {
79 foreach ($this->children as $child) {
80 if (!@$child->content) {
83 $l = $pdf->getStringWidth($child->content);
84 $max = ($l > $max) ? $l : $max;
87 $this->justifyLen = $max;
94 $f = $this->toColor(@$this->style['fill']);
96 $pdf->setTextColor($f[0],$f[1],$f[2]);
102 $x = $this->x + @$this->xx;
103 $y = $this->y + @$this->yy;
104 if (!@$this->children) {
108 foreach($this->children as $i=>$c) {
110 $xx = isset($c->x) ? $c->x + @$this->xx : $x;
111 $yy = isset($c->y) ? $c->y + @$this->yy : $y + ($lineno * $size * 1.3);
113 $val = isset($c->content) ? $c->content : '';
115 if (isset($c->args)) {
118 foreach($c->args as $v) {
120 $args[] = $pdf->PageNo();
127 $args[] = $this->getValue($data,trim($v));
130 $has_template = preg_match('/%s/', $val);
132 $val = vsprintf($val,$args);
134 if ($has_template && ($font == 'Big5')) {
135 require_once 'Text/ZhDetect.php';
136 $detect = new Text_zhDetect;
137 $type = $detect->guess($val);
140 $val = @iconv('utf8', 'GB2312//IGNORE', $val);
146 $val = @iconv('utf8', 'BIG5//IGNORE', $val);
147 //var_dump(array($vv, $val));
148 $pdf->setFont('Big5' ,
157 if ($has_template && ($font == 'Big5')) {
160 $val = @iconv('utf8', 'utf16be//TRANSLIT', $val);
162 $pdf->setFont('Uni-hw' ,
170 if ((empty($this->children[$i+1]) || !isset($this->children[$i+1]->content) || !strlen(trim($this->children[$i+1]->content))) && ($align == 'J')) {
179 $yoffset += $this->multiLine($pdf,explode("\n",$val),
181 ($yy / 3.543307) + $yoffset,
182 ($size / 3.543307) + 1,
194 * get a value from the data
197 * 'fred' => 'somevalue'
199 * getValue ($data,'fred') returns 'somevalue'
201 * value can also be a method = eg. getFred()
203 * @param object|array $data
204 * @param string $v key to get.
211 function iconvert($str) {
212 if (is_object($str) || is_array($str)) {
215 return $str ;//. ' - ' . @iconv( "UTF-8" , "Big5//IGNORE", $str );
218 function getValue($data,$v) {
222 if ($v[strlen($v)-1] != ')') {
223 $data = (array) $data;
224 if (false === strpos($v,'.')) {
225 if (!isset($data[$v])) {
228 if (is_array($data[$v]) || is_object($data[$v])) {
232 return $this->iconvert(@$data[$v]);
235 foreach(explode('.', $v) as $vv) {
236 if (!is_array($dd)) {
239 if (!isset($dd[$vv])) {
242 $dd = is_object($dd[$vv]) ? ((array) $dd[$vv]) : $this->iconvert($dd[$vv]);
244 //echo "ATTEMPT: $v: got $dd\n";
246 if (is_array($dd) || is_object($dd)) {
249 return $this->iconvert($dd);
252 if (!is_object($data)) {
255 $method = substr($v,0,-2);
256 if (is_callable(array($data,$method))) {
257 $ret = $data->$method();
258 if (is_array($ret) || is_object($ret)) {
261 // not in original!!!
262 return $this->iconvert($ret);
268 return "no method $method in ".get_class($data);
274 function breakLines(&$pdf,$str,$x,$y,$h,$align) {
275 // do the estimation...
278 $total = $pdf->getStringWidth($str . ' ');
280 $charsize = $total/$len;
282 $max_chars = floor(($this->maxWidth / 3.543307) / $charsize);
283 //echo "LEN: $len, $total, $charsize, $max_chars";
284 $lines = explode("\n",wordwrap($str,$max_chars));
286 return $this->multiLine($pdf,$lines,$x,$y,$h,$align);
289 function multiLine(&$pdf,$lines,$x,$y,$h,$align) {
290 // now dealing with mm
291 XML_SvgToPDFAlt::debug("MULTILINE '" .implode("\n",$lines) . "' $x, $y, $h");
294 foreach ($lines as $l=>$v) {
296 if (@$this->maxWidth && ($pdf->getStringWidth($v) > ($this->maxWidth / 3.543307))) {
297 $yoffset += $this->breakLines($pdf,$v,$x,$y + ($l * $h) + $yoffset, $h,$align);
302 if ($align == 'M') { // center
303 $xoffset = -1 * ($pdf->getStringWidth($v) / 2);
305 if ($align == 'E') { // right/end
306 $xoffset = -1 * $pdf->getStringWidth($v);
309 if ($align == 'J' ) { // justified (eg. started with ==
310 $this->justify($pdf, $x , $y + ($l * $h) + $yoffset , $v, $this->justifyLen);
313 XML_SvgToPDFAlt::debug("TEXT: " . ( $xoffset + $x ) . "," .
314 ($y + ($l * $h) + $yoffset) . "," .
319 $y + ($l * $h) + $yoffset ,
323 return ($l * $h) + $yoffset;
328 function justify(&$pdf,$x,$y,$text,$len) {
329 if (!strlen(trim($text))) {
332 $bits = explode(' ', $text);
333 $textlen = $pdf->getStringWidth(implode('',$bits));
334 $spacesize = ($len - $textlen) / (count($bits) -1);
335 foreach($bits as $word) {
336 $pdf->text($x , $y ,$word );
337 $x += $spacesize + $pdf->getStringWidth($word);