4 class XML_SvgToPDF_Text extends XML_SvgToPDF_Base {
9 function fromXmlNode($node) {
11 parent::fromXmlNode($node);
14 if (empty($this->children) || empty($this->children[0]->content)) {
18 // modify the alignment of the if this block content of the first child is "=="
20 if (substr($this->children[0]->content,0,2) == '==') {
21 $this->style['text-anchor'] = 'justify';
22 $this->children[0]->content = substr($this->children[0]->content,2);
26 function transform() {
29 if (empty($this->transform)) {
32 if (preg_match('/scale\(([0-9e.-]+),([0-9e.-]+)\)/',$this->transform,$args)) {
33 $xscale = $args[1]; // do we use this??? = what do do about 'e'?
35 $this->style['font-size'] *= $args[1];
41 function writePDF($pdf,$data) {
44 $font = strtolower($this->style['font-family']);
45 $font = trim($font, "'");
48 'times new roman' => 'times',
49 'courier new' => 'courier',
52 if (isset($map[$font])) {
58 if (preg_match('/big5/i',$this->style['font-family'])) {
60 $font = 'arial'; // default if not big4
66 if ($this->style['font-weight'] == 'bold') {
69 if (@$this->style['font-style'] == 'italic') {
73 if (@$this->style['text-decoration'] == 'underline') {
77 $size = $this->style['font-size'] * 0.85;
79 $pdf->setFont($font,$weight,$size);
81 switch(@$this->style['text-anchor']) {
91 foreach ($this->children as $child) {
92 if (!@$child->content) {
95 $l = $pdf->getStringWidth($child->content);
96 $max = ($l > $max) ? $l : $max;
99 $this->justifyLen = $max;
106 $f = $this->toColor(@$this->style['fill']);
108 $pdf->setTextColor($f[0],$f[1],$f[2]);
114 $x = $this->x + @$this->xx;
115 $y = $this->y + @$this->yy;
116 if (empty($this->children)) {
120 foreach($this->children as $i=>$c) {
122 $xx = $c->x !== false ? $c->x + @$this->xx : $x;
123 $yy = $c->y !== false ? $c->y + @$this->yy : $y + ($lineno * $size * 1.3);
126 if ($ffont == 'ARIALUNI') { //) && preg_match('/[\x7f-\xff]+/',$val)) {
127 $pdf->setFont('ARIALUNI' ,
131 if (isset($c->args)) {
134 foreach($c->args as $v) {
136 $args[] = $pdf->PageNo();
144 // echo "GET: $v : '$val'<BR>";
145 $args[] = trim($this->getValue($data,trim($v))); // removes trailing CRLF...
149 $has_template = preg_match('/%s/', $val);
151 $val = trim(vsprintf($val,$args));
153 if ($has_template && ($ffont == 'ARIALUNI') && preg_match('/[\x7f-\xff]+/',$val)) {
154 //require_once 'Text/ZhDetect.php';
155 //$detect = new Text_zhDetect;
156 //$type = $detect->guess($val);
159 // $val = @iconv('utf8', 'GB2312//IGNORE', $val);
160 // $pdf->setFont('GB' ,
164 // $val = @iconv('utf8', 'BIG5//IGNORE', $val);
165 $pdf->setFont('ARIALUNI' ,
170 $val = @iconv('utf8','ascii//ignore',$val);
178 if ((!@$this->children[$i+1] || !strlen(trim(@$this->children[$i+1]->content))) && ($align == 'J')) {
182 $yoffset += $this->multiLine($pdf, str_replace("\r", "", explode("\n",$val)),
184 ($yy / 3.543307) + $yoffset,
185 ($size / 3.543307) + 1,
197 * get a value from the data
200 * 'fred' => 'somevalue'
202 * getValue ($data,'fred') returns 'somevalue'
204 * value can also be a method = eg. getFred()
206 * @param object|array $data
207 * @param string $v key to get.
214 function getValue($data,$v) {
216 // print_R(array("GET VALUE: ", $v));
219 if ($v[strlen($v)-1] != ')') {
221 $data = (array) $data;
222 //echo "<PRE>";print_r(array_keys($data));
223 if (false === strpos($v,'.')) {
224 if (!isset($data[$v])) {
225 // echo "missing $v\n";
228 if (is_array($data[$v]) || is_object($data[$v])) {
229 // echo "array/object $v\n";
232 //echo "returning $v\n";
236 foreach(explode('.', $v) as $vv) {
237 if (!is_array($dd)) {
240 if (!isset($dd[$vv])) {
243 $dd = is_object($dd[$vv]) ? ((array) $dd[$vv]) : $dd[$vv];
245 //echo "ATTEMPT: $v: got $dd\n";
247 if (is_array($dd) || is_object($dd)) {
253 if (!is_object($data)) {
256 $method = substr($v,0,-2);
258 if (is_object($data) && method_exists($data,$method)) {
259 // echo "call $method<BR>";
260 $ret = $data->$method();
261 // echo "done $method $ret<BR>";
262 if (is_array($ret) || is_object($ret)) {
272 return "no method $method in ".get_class($data);
278 function breakLines($pdf,$str,$x,$y,$h,$align) {
279 // do the estimation...
282 $total = $pdf->getStringWidth($str . ' ');
284 $charsize = $total/$len;
286 $max_chars = floor(($this->maxWidth / 3.543307) / $charsize);
287 //echo "LEN: $len, $total, $charsize, $max_chars";
288 $lines = explode("\n",wordwrap($str,$max_chars));
290 return $this->multiLine($pdf,$lines,$x,$y,$h,$align);
292 var $maxWidth = false;
294 function multiLine($pdf,$lines,$x,$y,$h,$align) {
295 // now dealing with mm
296 ///XML_SvgToPDF::debug("MULTILINE " .implode("\n",$lines) . " $x, $y, $h");
299 foreach ($lines as $l=>$v) {
301 if ($this->maxWidth !== false && ($pdf->getStringWidth($v) > ($this->maxWidth / 3.543307))) {
302 $yoffset += $this->breakLines($pdf,$v,$x,$y + ($l * $h) + $yoffset, $h,$align);
305 XML_SvgToPDF::debug("TEXT: $x,$y, $l * $h + $yoffset,$v");
307 if ($align == 'M') { // center
308 $xoffset = -1 * ($pdf->getStringWidth($v) / 2);
310 if ($align == 'E') { // right/end
311 $xoffset = -1 * $pdf->getStringWidth($v);
314 if ($align == 'J' ) { // justified (eg. started with ==
315 $this->justify($pdf, $x , $y + ($l * $h) + $yoffset , $v, $this->justifyLen);
320 $y + ($l * $h) + $yoffset ,
324 return ($l * $h) + $yoffset;
329 function justify($pdf,$x,$y,$text,$len) {
330 if (!strlen(trim($text))) {
333 $bits = explode(' ', $text);
334 $textlen = $pdf->getStringWidth(implode('',$bits));
335 $spacesize = ($len - $textlen) / (count($bits) -1);
336 foreach($bits as $word) {
337 $pdf->text($x , $y ,$word );
338 $x += $spacesize + $pdf->getStringWidth($word);