/vendor/phenx/php-font-lib/classes/Font_Glyph_Outline_Simple.php

https://gitlab.com/techniconline/kmc · PHP · 331 lines · 249 code · 59 blank · 23 comment · 44 complexity · f132832a2f82c763f982dd197f6525fe MD5 · raw file

  1. <?php
  2. /**
  3. * @package php-font-lib
  4. * @link https://github.com/PhenX/php-font-lib
  5. * @author Fabien Ménager <fabien.menager@gmail.com>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. * @version $Id: Font_Table_glyf.php 46 2012-04-02 20:22:38Z fabien.menager $
  8. */
  9. /**
  10. * `glyf` font table.
  11. *
  12. * @package php-font-lib
  13. */
  14. class Font_Glyph_Outline_Simple extends Font_Glyph_Outline
  15. {
  16. const ON_CURVE = 0x01;
  17. const X_SHORT_VECTOR = 0x02;
  18. const Y_SHORT_VECTOR = 0x04;
  19. const REPEAT = 0x08;
  20. const THIS_X_IS_SAME = 0x10;
  21. const THIS_Y_IS_SAME = 0x20;
  22. public $instructions;
  23. public $points;
  24. function parseData()
  25. {
  26. parent::parseData();
  27. if (!$this->size) {
  28. return;
  29. }
  30. $font = $this->getFont();
  31. $noc = $this->numberOfContours;
  32. if ($noc == 0) {
  33. return;
  34. }
  35. $endPtsOfContours = $font->r(array(self::uint16, $noc));
  36. $instructionLength = $font->readUInt16();
  37. $this->instructions = $font->r(array(self::uint8, $instructionLength));
  38. $count = $endPtsOfContours[$noc - 1] + 1;
  39. // Flags
  40. $flags = array();
  41. for ($index = 0; $index < $count; $index++) {
  42. $flags[$index] = $font->readUInt8();
  43. if ($flags[$index] & self::REPEAT) {
  44. $repeats = $font->readUInt8();
  45. for ($i = 1; $i <= $repeats; $i++) {
  46. $flags[$index + $i] = $flags[$index];
  47. }
  48. $index += $repeats;
  49. }
  50. }
  51. $points = array();
  52. foreach ($flags as $i => $flag) {
  53. $points[$i]["onCurve"] = $flag & self::ON_CURVE;
  54. $points[$i]["endOfContour"] = in_array($i, $endPtsOfContours);
  55. }
  56. // X Coords
  57. $x = 0;
  58. for ($i = 0; $i < $count; $i++) {
  59. $flag = $flags[$i];
  60. if ($flag & self::THIS_X_IS_SAME) {
  61. if ($flag & self::X_SHORT_VECTOR) {
  62. $x += $font->readUInt8();
  63. }
  64. } else {
  65. if ($flag & self::X_SHORT_VECTOR) {
  66. $x -= $font->readUInt8();
  67. } else {
  68. $x += $font->readInt16();
  69. }
  70. }
  71. $points[$i]["x"] = $x;
  72. }
  73. // Y Coords
  74. $y = 0;
  75. for ($i = 0; $i < $count; $i++) {
  76. $flag = $flags[$i];
  77. if ($flag & self::THIS_Y_IS_SAME) {
  78. if ($flag & self::Y_SHORT_VECTOR) {
  79. $y += $font->readUInt8();
  80. }
  81. } else {
  82. if ($flag & self::Y_SHORT_VECTOR) {
  83. $y -= $font->readUInt8();
  84. } else {
  85. $y += $font->readInt16();
  86. }
  87. }
  88. $points[$i]["y"] = $y;
  89. }
  90. $this->points = $points;
  91. }
  92. public function splitSVGPath($path)
  93. {
  94. preg_match_all('/([a-z])|(-?\d+(?:\.\d+)?)/i', $path, $matches, PREG_PATTERN_ORDER);
  95. return $matches[0];
  96. }
  97. public function makePoints($path)
  98. {
  99. $path = $this->splitSVGPath($path);
  100. $l = count($path);
  101. $i = 0;
  102. $points = array();
  103. while ($i < $l) {
  104. switch ($path[$i]) {
  105. // moveTo
  106. case "M":
  107. $points[] = array(
  108. "onCurve" => true,
  109. "x" => $path[++$i],
  110. "y" => $path[++$i],
  111. "endOfContour" => false,
  112. );
  113. break;
  114. // lineTo
  115. case "L":
  116. $points[] = array(
  117. "onCurve" => true,
  118. "x" => $path[++$i],
  119. "y" => $path[++$i],
  120. "endOfContour" => false,
  121. );
  122. break;
  123. // quadraticCurveTo
  124. case "Q":
  125. $points[] = array(
  126. "onCurve" => false,
  127. "x" => $path[++$i],
  128. "y" => $path[++$i],
  129. "endOfContour" => false,
  130. );
  131. $points[] = array(
  132. "onCurve" => true,
  133. "x" => $path[++$i],
  134. "y" => $path[++$i],
  135. "endOfContour" => false,
  136. );
  137. break;
  138. // closePath
  139. /** @noinspection PhpMissingBreakStatementInspection */
  140. case "z":
  141. $points[count($points) - 1]["endOfContour"] = true;
  142. default:
  143. $i++;
  144. break;
  145. }
  146. }
  147. return $points;
  148. }
  149. function encode()
  150. {
  151. if (empty($this->points)) {
  152. return parent::encode();
  153. }
  154. return $this->size = $this->encodePoints($this->points);
  155. }
  156. public function encodePoints($points)
  157. {
  158. $endPtsOfContours = array();
  159. $flags = array();
  160. $coords_x = array();
  161. $coords_y = array();
  162. $last_x = 0;
  163. $last_y = 0;
  164. $xMin = $yMin = 0xFFFF;
  165. $xMax = $yMax = -0xFFFF;
  166. foreach ($points as $i => $point) {
  167. $flag = 0;
  168. if ($point["onCurve"]) {
  169. $flag |= self::ON_CURVE;
  170. }
  171. if ($point["endOfContour"]) {
  172. $endPtsOfContours[] = $i;
  173. }
  174. // Simplified, we could do some optimizations
  175. if ($point["x"] == $last_x) {
  176. $flag |= self::THIS_X_IS_SAME;
  177. } else {
  178. $x = intval($point["x"]);
  179. $xMin = min($x, $xMin);
  180. $xMax = max($x, $xMax);
  181. $coords_x[] = $x - $last_x; // int16
  182. }
  183. // Simplified, we could do some optimizations
  184. if ($point["y"] == $last_y) {
  185. $flag |= self::THIS_Y_IS_SAME;
  186. } else {
  187. $y = intval($point["y"]);
  188. $yMin = min($y, $yMin);
  189. $yMax = max($y, $yMax);
  190. $coords_y[] = $y - $last_y; // int16
  191. }
  192. $flags[] = $flag;
  193. $last_x = $point["x"];
  194. $last_y = $point["y"];
  195. }
  196. $font = $this->getFont();
  197. $l = 0;
  198. $l += $font->writeInt16(count($endPtsOfContours)); // endPtsOfContours
  199. $l += $font->writeFWord(isset($this->xMin) ? $this->xMin : $xMin); // xMin
  200. $l += $font->writeFWord(isset($this->yMin) ? $this->yMin : $yMin); // yMin
  201. $l += $font->writeFWord(isset($this->xMax) ? $this->xMax : $xMax); // xMax
  202. $l += $font->writeFWord(isset($this->yMax) ? $this->yMax : $yMax); // yMax
  203. // Simple glyf
  204. $l += $font->w(array(self::uint16, count($endPtsOfContours)), $endPtsOfContours); // endPtsOfContours
  205. $l += $font->writeUInt16(0); // instructionLength
  206. $l += $font->w(array(self::uint8, count($flags)), $flags); // flags
  207. $l += $font->w(array(self::int16, count($coords_x)), $coords_x); // xCoordinates
  208. $l += $font->w(array(self::int16, count($coords_y)), $coords_y); // yCoordinates
  209. return $l;
  210. }
  211. public function getSVGContours($points = null)
  212. {
  213. $path = "";
  214. if (!$points) {
  215. if (empty($this->points)) {
  216. $this->parseData();
  217. }
  218. $points = $this->points;
  219. }
  220. $length = count($points);
  221. $firstIndex = 0;
  222. $count = 0;
  223. for ($i = 0; $i < $length; $i++) {
  224. $count++;
  225. if ($points[$i]["endOfContour"]) {
  226. $path .= $this->getSVGPath($points, $firstIndex, $count);
  227. $firstIndex = $i + 1;
  228. $count = 0;
  229. }
  230. }
  231. return $path;
  232. }
  233. protected function getSVGPath($points, $startIndex, $count)
  234. {
  235. $offset = 0;
  236. $path = "";
  237. while ($offset < $count) {
  238. $point = $points[$startIndex + $offset % $count];
  239. $point_p1 = $points[$startIndex + ($offset + 1) % $count];
  240. if ($offset == 0) {
  241. $path .= "M{$point['x']},{$point['y']} ";
  242. }
  243. if ($point["onCurve"]) {
  244. if ($point_p1["onCurve"]) {
  245. $path .= "L{$point_p1['x']},{$point_p1['y']} ";
  246. $offset++;
  247. } else {
  248. $point_p2 = $points[$startIndex + ($offset + 2) % $count];
  249. if ($point_p2["onCurve"]) {
  250. $path .= "Q{$point_p1['x']},{$point_p1['y']},{$point_p2['x']},{$point_p2['y']} ";
  251. } else {
  252. $path .= "Q{$point_p1['x']},{$point_p1['y']}," . $this->midValue($point_p1['x'], $point_p2['x']) . "," . $this->midValue($point_p1['y'], $point_p2['y']) . " ";
  253. }
  254. $offset += 2;
  255. }
  256. } else {
  257. if ($point_p1["onCurve"]) {
  258. $path .= "Q{$point['x']},{$point['y']},{$point_p1['x']},{$point_p1['y']} ";
  259. } else {
  260. $path .= "Q{$point['x']},{$point['y']}," . $this->midValue($point['x'], $point_p1['x']) . "," . $this->midValue($point['y'], $point_p1['y']) . " ";
  261. }
  262. $offset++;
  263. }
  264. }
  265. $path .= "z ";
  266. return $path;
  267. }
  268. function midValue($a, $b)
  269. {
  270. return $a + ($b - $a) / 2;
  271. }
  272. }