4 class XML_SvgToPDF_Text extends XML_SvgToPDF_Base {
11 function fromXmlNode($node) {
13 parent::fromXmlNode($node);
16 if (empty($this->children) || empty($this->children[0]->content)) {
20 // modify the alignment of the if this block content of the first child is "=="
22 if (substr($this->children[0]->content,0,2) == '==') {
23 $this->style['text-anchor'] = 'justify';
24 $this->children[0]->content = substr($this->children[0]->content,2);
28 function transform() {
31 if (empty($this->transform)) {
34 if (preg_match('/scale\(([0-9e.-]+),([0-9e.-]+)\)/',$this->transform,$args)) {
35 $xscale = $args[1]; // do we use this??? = what do do about 'e'?
37 $this->style['font-size'] *= $args[1];
43 function writePDF($pdf,$data) {
46 $font = strtolower($this->style['font-family']);
47 $font = trim($font, "'");
50 'times new roman' => 'times',
51 'courier new' => 'courier',
54 if (isset($map[$font])) {
60 if (preg_match('/(big5)/i',$this->style['font-family'])) {
62 $font = 'arial'; // default if not big4
68 if ($this->style['font-weight'] == 'bold') {
71 if (@$this->style['font-style'] == 'italic') {
75 if (@$this->style['text-decoration'] == 'underline') {
80 $size = floatval(str_replace("px","",$this->style['font-size'])) * 0.85;
81 $pdf->setFont($font,$weight,$size."px");
83 switch(@$this->style['text-anchor']) {
93 foreach ($this->children as $child) {
94 if (!@$child->content) {
97 $l = $pdf->getStringWidth($child->content);
98 $max = ($l > $max) ? $l : $max;
101 $this->justifyLen = $max;
108 $f = $this->toColor(@$this->style['fill']);
110 $pdf->setTextColor($f[0],$f[1],$f[2]);
116 $x = $this->x + @$this->xx;
117 $y = $this->y + @$this->yy;
118 if (empty($this->children)) {
122 foreach($this->children as $i=>$c) {
124 $xx = $c->x !== false ? $c->x + @$this->xx : $x;
125 $yy = $c->y !== false ? $c->y + @$this->yy : $y + ($lineno * $size * 1.3);
128 if ($ffont == 'ARIALUNI') { //) && preg_match('/[\x7f-\xff]+/',$val)) {
129 $pdf->setFont('ARIALUNI' ,
133 if (isset($c->args)) {
136 foreach($c->args as $v) {
138 $args[] = $pdf->PageNo();
146 // echo "GET: $v : '$val'<BR>";
147 $args[] = trim($this->getValue($data,trim($v))); // removes trailing CRLF...
151 $has_template = preg_match('/%s/', $val);
153 $val = trim(vsprintf($val,$args));
155 if ( preg_match('/[\x7f-\xff]+/',$val)) {
156 //require_once 'Text/ZhDetect.php';
157 //$detect = new Text_zhDetect;
158 //$type = $detect->guess($val);
161 // $val = @iconv('utf8', 'GB2312//IGNORE', $val);
162 // $pdf->setFont('GB' ,
166 // $val = @iconv('utf8', 'BIG5//IGNORE', $val);
167 $pdf->setFont('ARIALUNI' ,
172 $val = @iconv('utf8','ascii//ignore',$val);
180 if ((!@$this->children[$i+1] || !strlen(trim(@$this->children[$i+1]->content))) && ($align == 'J')) {
184 $yoffset += $this->multiLine($pdf, str_replace("\r", "", explode("\n",$val)),
186 ($yy / 3.543307) + $yoffset,
187 ($size / 3.543307) + 1,
199 * get a value from the data
202 * 'fred' => 'somevalue'
204 * getValue ($data,'fred') returns 'somevalue'
206 * value can also be a method = eg. getFred()
208 * @param object|array $data
209 * @param string $v key to get.
216 function getValue($data,$v) {
218 // print_R(array("GET VALUE: ", $v));
221 if ($v[strlen($v)-1] != ')') {
223 $data = (array) $data;
224 //echo "<PRE>";print_r(array_keys($data));
225 if (false === strpos($v,'.')) {
226 if (!isset($data[$v])) {
227 // echo "missing $v\n";
230 if (is_array($data[$v]) || is_object($data[$v])) {
231 // echo "array/object $v\n";
234 //echo "returning $v\n";
238 foreach(explode('.', $v) as $vv) {
239 if (!is_array($dd)) {
242 if (!isset($dd[$vv])) {
245 $dd = is_object($dd[$vv]) ? ((array) $dd[$vv]) : $dd[$vv];
247 //echo "ATTEMPT: $v: got $dd\n";
249 if (is_array($dd) || is_object($dd)) {
255 if (!is_object($data)) {
258 $method = substr($v,0,-2);
260 if (is_object($data) && method_exists($data,$method)) {
261 // echo "call $method<BR>";
262 $ret = $data->$method();
263 // echo "done $method $ret<BR>";
264 if (is_array($ret) || is_object($ret)) {
274 return "no method $method in ".get_class($data);
280 function breakLines($pdf,$str,$x,$y,$h,$align) {
281 // do the estimation...
284 $total = $pdf->getStringWidth($str . ' ');
286 $charsize = $total/$len;
288 $max_chars = floor(($this->maxWidth / 3.543307) / $charsize);
289 //echo "LEN: $len, $total, $charsize, $max_chars";
290 $lines = explode("\n",wordwrap($str,$max_chars));
292 return $this->multiLine($pdf,$lines,$x,$y,$h,$align);
294 var $maxWidth = false;
296 function multiLine($pdf,$lines,$x,$y,$h,$align) {
297 // now dealing with mm
298 ///XML_SvgToPDF::debug("MULTILINE " .implode("\n",$lines) . " $x, $y, $h");
301 foreach ($lines as $l=>$v) {
303 if ($this->maxWidth !== false && ($pdf->getStringWidth($v) > ($this->maxWidth / 3.543307))) {
304 $yoffset += $this->breakLines($pdf,$v,$x,$y + ($l * $h) + $yoffset, $h,$align);
307 XML_SvgToPDF::debug("TEXT: $x,$y, $l * $h + $yoffset,$v");
309 if ($align == 'M') { // center
310 $xoffset = -1 * ($pdf->getStringWidth($v) / 2);
312 if ($align == 'E') { // right/end
313 $xoffset = -1 * $pdf->getStringWidth($v);
316 if ($align == 'J' ) { // justified (eg. started with ==
317 $this->justify($pdf, $x , $y + ($l * $h) + $yoffset , $v, $this->justifyLen);
322 $y + ($l * $h) + $yoffset ,
326 return ($l * $h) + $yoffset;
331 function justify($pdf,$x,$y,$text,$len) {
332 if (!strlen(trim($text))) {
335 $bits = explode(' ', $text);
336 $textlen = $pdf->getStringWidth(implode('',$bits));
337 $spacesize = ($len - $textlen) / (count($bits) -1);
338 foreach($bits as $word) {
339 $pdf->text($x , $y ,$word );
340 $x += $spacesize + $pdf->getStringWidth($word);