PageRenderTime 40ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/experimental/graphic/GraphicTextRun.php

http://zoop.googlecode.com/
PHP | 325 lines | 254 code | 29 blank | 42 comment | 10 complexity | e7367cbfd5549e8affbe14fc42b90db0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. class GraphicTextRun extends GraphicObject
  3. {
  4. private $text;
  5. private $leftSpace;
  6. private $rightSpace;
  7. private $widthsCalced = false;
  8. private $length;
  9. function __construct($context)
  10. {
  11. parent::__construct($context);
  12. $this->text = '';
  13. $this->length = 0;
  14. $this->leftSpace = 0;
  15. $this->rightRight = 0;
  16. // $this->style = NULL;
  17. // $this->charWidths = array();
  18. // $this->totalWidth = -1;
  19. }
  20. public function setTextInfo($text)
  21. {
  22. // consolidate white space. Should this be done here or in the xml layer?
  23. // this is really an xml thing not a graphics thing
  24. $content = ereg_replace("[[:space:]]+", ' ', $text['content']);
  25. $this->text = $content;
  26. $this->length = strlen($this->text);
  27. $this->leftSpace = $text['leftTrim'];
  28. $this->rightSpace = $text['rightTrim'];
  29. }
  30. public function setStyle($style)
  31. {
  32. }
  33. function getLength()
  34. {
  35. return $this->length;
  36. }
  37. //
  38. // the drawing functions
  39. //
  40. private function isBreakChar($inChar)
  41. {
  42. $breakChars = array(' ' , '-');
  43. if(in_array($inChar, $breakChars))
  44. return true;
  45. else
  46. return false;
  47. }
  48. private function calcCharWidths()
  49. {
  50. if($this->widthsCalced)
  51. return;
  52. // assert($this->style !== NULL);
  53. // $this->context->setTextFont($this->style);
  54. // echo_r($this->text);
  55. $this->charWidths = $this->context->getCharWidths($this->text);
  56. $this->totalWidth = array_sum($this->charWidths);
  57. // echo_r($this->charWidths);
  58. // echo_r($this->totalWidth);
  59. $this->widthsCalced = true;
  60. }
  61. function getPartWidth($start, $length)
  62. {
  63. // assert($this->style !== NULL);
  64. $total = 0;
  65. for($i = $start; $i < $start + $length; $i++)
  66. $total += $this->charWidths[$i];
  67. return $total;
  68. // assert($this->style !== NULL);
  69. //
  70. // if($length == NULL)
  71. // $string = substr($this->text, $start);
  72. // else
  73. // $string = substr($this->text, $start, $length);
  74. //
  75. // $this->context->setTextFont($this->style);
  76. //
  77. // return $this->context->getStringWidth($string);
  78. }
  79. function getHeight()
  80. {
  81. return 12;
  82. // return $this->style->getTextSize() * $this->context->getLineHeightMultiplier();
  83. }
  84. // this function tell us how much of the string will fit onto the current line
  85. function getFitsLength($startPos, $remainingWidth)
  86. {
  87. $this->calcCharWidths();
  88. $lastGoodPos = -1;
  89. $width = 0;
  90. $textLen = strlen($this->text);
  91. for($i = $startPos; $i < $textLen; $i++)
  92. {
  93. $charWidth = $this->charWidths[$i];
  94. if($width + $charWidth > $remainingWidth)
  95. {
  96. if($lastGoodPos == -1)
  97. return $i - $startPos;
  98. else
  99. return $lastGoodPos - $startPos + 1;
  100. }
  101. if($this->isBreakChar($this->text{$i}))
  102. {
  103. $lastGoodPos = $i;
  104. }
  105. $width += $charWidth;
  106. }
  107. return $textLen - $startPos;
  108. }
  109. function drawPart($x, $y, $start = 0, $length = NULL)
  110. {
  111. // assert($this->style !== NULL);
  112. // echo "GraphicTextRun::drawPart : $x, $y, $start, $length<br>";
  113. $height = $this->getHeight();
  114. /*
  115. switch($height)
  116. {
  117. case 14:
  118. $tweak = $height * 0.12;
  119. break;
  120. default:
  121. $tweak = 0;
  122. break;
  123. }
  124. */
  125. // $y = $y - $tweak;
  126. if($length == NULL)
  127. $string = substr($this->text, $start);
  128. else
  129. $string = substr($this->text, $start, $length);
  130. // $this->context->setTextColor($this->style->color[0], $this->style->color[1], $this->style->color[2]);
  131. // $this->context->setTextFont($this->style);
  132. $this->context->addText($x, $y + $this->getHeight(), $string);
  133. }
  134. public function draw($x, $y, $width, $reallyDraw = true)
  135. {
  136. trigger_error("We don't actually use draw, we use draw part.");
  137. }
  138. //
  139. // debug stuff
  140. //
  141. public function getObjectTree($indentLevel = 0)
  142. {
  143. $tabs = '';
  144. for($i = 0; $i < $indentLevel; $i++)
  145. $tabs .= '&nbsp;&nbsp;&nbsp;&nbsp;';
  146. // draw the close tag
  147. echo $tabs . $this;
  148. }
  149. public function drawRenderTree($indentLevel = 0)
  150. {
  151. $tabs = '';
  152. for($i = 0; $i < $indentLevel; $i++)
  153. $tabs .= '&nbsp;&nbsp;&nbsp;&nbsp;';
  154. // draw the close tag
  155. echo $tabs . $this;
  156. }
  157. public function __toString()
  158. {
  159. return '&lt;GraphicTextRun text=" ' . $this->text . '"/&gt;<br>';
  160. }
  161. /*
  162. var $style;
  163. var $breakPoints;
  164. var $charWidths;
  165. var $totalWidth;
  166. function getUnwrappedWidth()
  167. {
  168. assert($this->style !== NULL);
  169. assert($this->totalWidth > -1);
  170. return $this->totalWidth;
  171. }
  172. function getWidth()
  173. {
  174. assert($this->style !== NULL);
  175. assert($this->totalWidth > -1);
  176. return $this->totalWidth;
  177. // $this->context->setTextFont($this->style);
  178. // return $this->context->getStringWidth($this->text);
  179. }
  180. function setText($text)
  181. {
  182. assert($this->style !== NULL);
  183. // we need to get rid of the excess white space in the middle
  184. $content = ereg_replace("[[:space:]]+", ' ', $text['content']);
  185. $this->text = $content;
  186. //echo 'START' . $this->text . "END<br>\n";
  187. $this->leftSpace = $text['leftTrim'];
  188. $this->rightSpace = $text['rightTrim'];
  189. $this->calcCharWidths();
  190. }
  191. function calcCharWidths()
  192. {
  193. assert($this->style !== NULL);
  194. $this->context->setTextFont($this->style);
  195. $len = strlen($this->text);
  196. $totalWidth = 0;
  197. for($i = 0; $i < $len; $i++)
  198. {
  199. if($this->context->useKerningHack())
  200. {
  201. if($i == $len - 1)
  202. {
  203. $width = $this->context->getStringWidth(substr($this->text, $i, 1)); // would it be better to use $this->text{$i} here?
  204. $totalWidth += $width;
  205. $this->charWidths[$i] = $width;
  206. }
  207. else
  208. {
  209. $thisAndNextWidth = $this->context->getStringWidth(substr($this->text, $i, 2));
  210. $justNextWidth = $this->context->getStringWidth(substr($this->text, $i+ 1, 1));
  211. $width = $thisAndNextWidth - $justNextWidth;
  212. $totalWidth += $width;
  213. $this->charWidths[$i] = $width;
  214. }
  215. }
  216. else
  217. {
  218. $width = $this->context->getStringWidth(substr($this->text, $i, 1)); // would it be better to use $this->text{$i} here?
  219. $totalWidth += $width;
  220. $this->charWidths[$i] = $width;
  221. }
  222. }
  223. $this->totalWidth = $totalWidth;
  224. }
  225. function addLeftSpace()
  226. {
  227. $this->text = ' ' . $this->text;
  228. }
  229. function getLeftSpace()
  230. {
  231. return $this->leftSpace;
  232. }
  233. function getRightSpace()
  234. {
  235. return $this->rightSpace;
  236. }
  237. function setStyle(&$style)
  238. {
  239. assert( is_a($style, 'GraphicTextStyle') );
  240. $this->style = $style;
  241. }
  242. function isInline()
  243. {
  244. return 1;
  245. }
  246. function draw($x, $y)
  247. {
  248. assert($this->style !== NULL);
  249. $this->context->setTextColor($this->style->color[0], $this->style->color[1], $this->style->color[2]);
  250. $this->context->setTextFont($this->style);
  251. $this->context->addText($x, $y + $this->getHeight(), $this->text);
  252. }
  253. function __toString()
  254. {
  255. $s = "<" . get_class($this) . ">";
  256. $s .= '~' . $this->text . '~';
  257. $s .= "</" . get_class($this) . ">";
  258. return $s;
  259. }
  260. */
  261. }