/framework/experimental/graphic/GraphicHardBrokenLine.php

http://zoop.googlecode.com/ · PHP · 302 lines · 128 code · 33 blank · 141 comment · 13 complexity · aa585fa85e7631798de9109fab639033 MD5 · raw file

  1. <?php
  2. class GraphicHardBrokenLine extends GraphicObject
  3. {
  4. var $members;
  5. var $alignment;
  6. var $lineSpacing;
  7. var $doneDrawing;
  8. var $topIndent;
  9. var $bottomIndent;
  10. function GraphicHardBrokenLine()
  11. {
  12. $this->members = array();
  13. $this->alignment = 'left';
  14. $this->doneDrawing = 0;
  15. }
  16. function getUnwrappedWidth()
  17. {
  18. $total = 0;
  19. for($curMember = 0; $curMember < count($this->members); $curMember++)
  20. {
  21. // echo get_class($this->members[$curMember]);
  22. $total += $this->members[$curMember]->getUnwrappedWidth();
  23. }
  24. return $total;
  25. }
  26. function setBottomIndent($indent)
  27. {
  28. $this->bottomIndent = $indent;
  29. }
  30. function setLineSpacing($lineSpacing)
  31. {
  32. $this->lineSpacing = $lineSpacing;
  33. }
  34. function addMember($member)
  35. {
  36. $this->members[] = $member;
  37. }
  38. function setAlignment($alignment)
  39. {
  40. $this->alignment = $alignment;
  41. }
  42. function isBreakable()
  43. {
  44. return $this->members[0]->isBreakable();
  45. }
  46. function getMinHeight($width)
  47. {
  48. if($this->isBreakable())
  49. {
  50. return $this->members[0]->getMinHeight($width);
  51. }
  52. else
  53. {
  54. return $this->getHeight($width);
  55. }
  56. }
  57. function doneDrawing()
  58. {
  59. return $this->doneDrawing;
  60. }
  61. function forcePageBreak()
  62. {
  63. return $this->members[0]->forcePageBreak();
  64. }
  65. function draw($x, $y, $width, $reallyDraw = 1)
  66. {
  67. // echo "drawing hard broken line: $x, $y, $width, $reallyDraw<br>";
  68. // $width += 20;
  69. // if there is nothing in it then just return 0
  70. if(count($this->members) == 0)
  71. return 0;
  72. /*
  73. // if this is not an inline item then it should be all by itself and we
  74. // need to just draw it and return
  75. if( !$this->members[0]->isInline() )
  76. {
  77. assert( count($this->members[0]) == 1);
  78. switch($this->alignment)
  79. {
  80. case 'left':
  81. $curx = $x;
  82. break;
  83. case 'center':
  84. $curx = $x + (($width - $this->members[0]->getWidth()) / 2);
  85. break;
  86. case 'right':
  87. $curx = $x + $width - $this->members[0]->getWidth();
  88. break;
  89. default:
  90. trigger_error('invalid alignment specified: ' . $this->alignment);
  91. break;
  92. }
  93. $height = $this->members[0]->draw($curx, $y, $width, $reallyDraw);
  94. $this->doneDrawing = $this->members[0]->doneDrawing();
  95. return $height;
  96. }
  97. */
  98. assert($this->members[0]->isInline());
  99. //
  100. // this is a series of one or more inline items
  101. // we need to draw them all and wrap them to the width
  102. //
  103. //
  104. // break everything up into soft broken lines
  105. //
  106. $curMemberNumber = 0;
  107. $lines = array();
  108. $curLine = 0;
  109. //$lines[$curLine] = &new GraphicSoftBrokenLine();
  110. $curPos = 0;
  111. $remainingWidth = $width;
  112. while($curMemberNumber < count($this->members))
  113. {
  114. // get and verify the current member
  115. $curMember = $this->members[$curMemberNumber];
  116. assert(is_a($curMember, 'GraphicTextRun'));
  117. // figure out how much if any of this member will fit onto the current line
  118. $fitsLength = $curMember->getFitsLength($curPos, $remainingWidth);
  119. // echo "$curPos, $remainingWidth $fitsLength<br>";
  120. assert($curMember->getLength() >= $curPos + $fitsLength);
  121. // if it all fits add it to the soft broken line, bump the member number and continue from the top
  122. if($curPos + $fitsLength == $curMember->getLength())
  123. {
  124. if(!isset($lines[$curLine]))
  125. $lines[$curLine] = &new GraphicSoftBrokenLine();
  126. $lines[$curLine]->addEntry($curMember, $curPos, $fitsLength);
  127. $remainingWidth -= $curMember->getPartWidth($curPos, $fitsLength);
  128. $curMemberNumber++;
  129. $curPos = 0;
  130. }
  131. // if only part fits, add that part, bump the line number and continue from the top
  132. else if($fitsLength > 0)
  133. {
  134. if(!isset($lines[$curLine]))
  135. $lines[$curLine] = &new GraphicSoftBrokenLine();
  136. $lines[$curLine]->addEntry($curMember, $curPos, $fitsLength);
  137. $curPos += $fitsLength;
  138. $curLine++;
  139. $remainingWidth = $width;
  140. // if($this->bottomIndent)
  141. // $remainingWidth -= $this->bottomIndent;
  142. }
  143. // if none fits bump the line number and continue from the top
  144. else
  145. {
  146. if($remainingWidth == $width)
  147. trigger_error("single word will not fit in space");
  148. $curLine++;
  149. $remainingWidth = $width;
  150. }
  151. }
  152. /*
  153. while(true)
  154. {
  155. // if everything is already handled then just break out of the loop
  156. if($curMember >= count($this->members))
  157. break;
  158. // figure out how much of this member will fit onto the current line
  159. $fitsLength = $this->members[$curMember]->getFitsLength($curPos, $remainingWidth);
  160. // if the whole thing fits on the line
  161. if($curPos + $fitsLength >= $this->members[$curMember]->getLength())
  162. {
  163. // if this is the first item on the line then first create the line
  164. if(!isset($lines[$curLine]))
  165. $lines[$curLine] = &new GraphicSoftBrokenLine();
  166. $lines[$curLine]->addEntry($this->members[$curMember], $curPos, $fitsLength);
  167. $remainingWidth -= $this->members[$curMember]->getPartWidth($curPos, $fitsLength);
  168. $curMember++;
  169. $curPos = 0;
  170. }
  171. // if it doesn't all fit but some fits
  172. else if($fitsLength > 0)
  173. {
  174. // if this is the first item on the line then first create the line
  175. if(!isset($lines[$curLine]))
  176. $lines[$curLine] = &new GraphicSoftBrokenLine();
  177. $lines[$curLine]->addEntry($this->members[$curMember], $curPos, $fitsLength);
  178. $curPos += $fitsLength;
  179. $curLine++;
  180. $remainingWidth = $width;
  181. if($this->bottomIndent)
  182. $remainingWidth -= $this->bottomIndent;
  183. }
  184. // if none of it will fit???
  185. else
  186. {
  187. echo "$curPos $fitsLength " . $this->members[$curMember]->getLength() . "<br>";
  188. BUG("This should never happen. Most likely you are trying to squish some text into to small of a space");
  189. die();
  190. $curLine++;
  191. $remainingWidth = $width;
  192. }
  193. }
  194. //
  195. // now draw each of the lines
  196. //
  197. $cury = $y;
  198. //if($reallyDraw)
  199. // echo 'soft lines = ' . count($lines) . '<br>';
  200. foreach($lines as $lineKey => $dummyLine)
  201. {
  202. $thisLine = &$lines[$lineKey];
  203. switch($this->alignment)
  204. {
  205. case 'left':
  206. $curx = $x;
  207. if($this->bottomIndent && $lineKey > 0)
  208. $curx += $this->bottomIndent;
  209. break;
  210. case 'center':
  211. $curx = $x + ( ($width - $thisLine->getWidth()) / 2 );
  212. break;
  213. case 'right':
  214. $curx = $x + ( $width - $thisLine->getWidth() );
  215. break;
  216. default:
  217. trigger_error('invalid alignment specified: ' . $this->alignment);
  218. break;
  219. }
  220. $thisLine->draw($curx, $cury, 0, $reallyDraw);
  221. $cury += $thisLine->getHeight(0) + $this->lineSpacing;
  222. }
  223. $this->doneDrawing = 1;
  224. return $cury - $y;
  225. */
  226. $cury = $y;
  227. foreach(array_keys($lines) as $lineKey)
  228. {
  229. $thisLine = $lines[$lineKey];
  230. $curx = $x;
  231. $cury += 12;
  232. $thisLine->draw($curx, $cury, NULL, $reallyDraw);
  233. }
  234. return $cury - $y;
  235. }
  236. public function drawRenderTree($indentLevel = 0)
  237. {
  238. $tabs = '';
  239. for($i = 0; $i < $indentLevel; $i++)
  240. $tabs .= '&nbsp;&nbsp;&nbsp;&nbsp;';
  241. // draw the open tag
  242. echo $tabs . '&lt;' . get_class($this) . '&gt;<br>';
  243. foreach(array_keys($this->members) as $lineKey)
  244. {
  245. $this->members[$lineKey]->drawRenderTree($indentLevel + 1);
  246. }
  247. // draw the close tag
  248. echo $tabs . '&lt;/' . get_class($this) . '&gt;<br>';
  249. }
  250. }