/system/helpers/dompdf/lib/php-font-lib/classes/Font_Glyph_Outline_Composite.php

https://github.com/iisafriyanti/pkm · PHP · 233 lines · 173 code · 37 blank · 23 comment · 32 complexity · d41abd5525510e8bc68a30b0ee053dbb 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. require_once dirname(__FILE__) . "/Font_Glyph_Outline_Component.php";
  10. /**
  11. * Composite glyph outline
  12. *
  13. * @package php-font-lib
  14. */
  15. class Font_Glyph_Outline_Composite extends Font_Glyph_Outline {
  16. const ARG_1_AND_2_ARE_WORDS = 0x0001;
  17. const ARGS_ARE_XY_VALUES = 0x0002;
  18. const ROUND_XY_TO_GRID = 0x0004;
  19. const WE_HAVE_A_SCALE = 0x0008;
  20. const MORE_COMPONENTS = 0x0020;
  21. const WE_HAVE_AN_X_AND_Y_SCALE = 0x0040;
  22. const WE_HAVE_A_TWO_BY_TWO = 0x0080;
  23. const WE_HAVE_INSTRUCTIONS = 0x0100;
  24. const USE_MY_METRICS = 0x0200;
  25. const OVERLAP_COMPOUND = 0x0400;
  26. /**
  27. * @var Font_Glyph_Outline_Component[]
  28. */
  29. public $components = array();
  30. function getGlyphIDs(){
  31. if (empty($this->components)) {
  32. $this->parseData();
  33. }
  34. $glyphIDs = array();
  35. foreach ($this->components as $_component) {
  36. $glyphIDs[] = $_component->glyphIndex;
  37. $_glyph = $this->table->data[$_component->glyphIndex];
  38. $glyphIDs = array_merge($glyphIDs, $_glyph->getGlyphIDs());
  39. }
  40. return $glyphIDs;
  41. }
  42. /*function parse() {
  43. //$this->parseData();
  44. }*/
  45. function parseData(){
  46. parent::parseData();
  47. $font = $this->getFont();
  48. do {
  49. $flags = $font->readUInt16();
  50. $glyphIndex = $font->readUInt16();
  51. $a = 1.0; $b = 0.0;
  52. $c = 0.0; $d = 1.0;
  53. $e = 0.0; $f = 0.0;
  54. $point_compound = null;
  55. $point_component = null;
  56. $instructions = null;
  57. if ($flags & self::ARG_1_AND_2_ARE_WORDS) {
  58. if ($flags & self::ARGS_ARE_XY_VALUES) {
  59. $e = $font->readInt16();
  60. $f = $font->readInt16();
  61. }
  62. else {
  63. $point_compound = $font->readUInt16();
  64. $point_component = $font->readUInt16();
  65. }
  66. }
  67. else {
  68. if ($flags & self::ARGS_ARE_XY_VALUES) {
  69. $e = $font->readInt8();
  70. $f = $font->readInt8();
  71. }
  72. else {
  73. $point_compound = $font->readUInt8();
  74. $point_component = $font->readUInt8();
  75. }
  76. }
  77. if ($flags & self::WE_HAVE_A_SCALE) {
  78. $a = $d = $font->readInt16();
  79. }
  80. elseif ($flags & self::WE_HAVE_AN_X_AND_Y_SCALE) {
  81. $a = $font->readInt16();
  82. $d = $font->readInt16();
  83. }
  84. elseif ($flags & self::WE_HAVE_A_TWO_BY_TWO) {
  85. $a = $font->readInt16();
  86. $b = $font->readInt16();
  87. $c = $font->readInt16();
  88. $d = $font->readInt16();
  89. }
  90. //if ($flags & self::WE_HAVE_INSTRUCTIONS) {
  91. //
  92. //}
  93. $component = new Font_Glyph_Outline_Component();
  94. $component->flags = $flags;
  95. $component->glyphIndex = $glyphIndex;
  96. $component->a = $a;
  97. $component->b = $b;
  98. $component->c = $c;
  99. $component->d = $d;
  100. $component->e = $e;
  101. $component->f = $f;
  102. $component->point_compound = $point_compound;
  103. $component->point_component = $point_component;
  104. $component->instructions = $instructions;
  105. $this->components[] = $component;
  106. } while ($flags & self::MORE_COMPONENTS);
  107. }
  108. function encode(){
  109. $font = $this->getFont();
  110. $gids = $font->getSubset();
  111. $size = $font->writeInt16(-1);
  112. $size += $font->writeFWord($this->xMin);
  113. $size += $font->writeFWord($this->yMin);
  114. $size += $font->writeFWord($this->xMax);
  115. $size += $font->writeFWord($this->yMax);
  116. foreach ($this->components as $_i => $_component) {
  117. $flags = 0;
  118. if ($_component->point_component === null && $_component->point_compound === null) {
  119. $flags |= self::ARGS_ARE_XY_VALUES;
  120. if (abs($_component->e) > 0x7F || abs($_component->f) > 0x7F) {
  121. $flags |= self::ARG_1_AND_2_ARE_WORDS;
  122. }
  123. }
  124. elseif ($_component->point_component > 0xFF || $_component->point_compound > 0xFF) {
  125. $flags |= self::ARG_1_AND_2_ARE_WORDS;
  126. }
  127. if ($_component->b == 0 && $_component->c == 0) {
  128. if ($_component->a == $_component->d) {
  129. if ($_component->a != 1.0) {
  130. $flags |= self::WE_HAVE_A_SCALE;
  131. }
  132. }
  133. else {
  134. $flags |= self::WE_HAVE_AN_X_AND_Y_SCALE;
  135. }
  136. }
  137. else {
  138. $flags |= self::WE_HAVE_A_TWO_BY_TWO;
  139. }
  140. if ($_i < count($this->components)-1) {
  141. $flags |= self::MORE_COMPONENTS;
  142. }
  143. $size += $font->writeUInt16($flags);
  144. $new_gid = array_search($_component->glyphIndex, $gids);
  145. $size += $font->writeUInt16($new_gid);
  146. if ($flags & self::ARG_1_AND_2_ARE_WORDS) {
  147. if ($flags & self::ARGS_ARE_XY_VALUES) {
  148. $size += $font->writeInt16($_component->e);
  149. $size += $font->writeInt16($_component->f);
  150. }
  151. else {
  152. $size += $font->writeUInt16($_component->point_compound);
  153. $size += $font->writeUInt16($_component->point_component);
  154. }
  155. }
  156. else {
  157. if ($flags & self::ARGS_ARE_XY_VALUES) {
  158. $size += $font->writeInt8($_component->e);
  159. $size += $font->writeInt8($_component->f);
  160. }
  161. else {
  162. $size += $font->writeUInt8($_component->point_compound);
  163. $size += $font->writeUInt8($_component->point_component);
  164. }
  165. }
  166. if ($flags & self::WE_HAVE_A_SCALE) {
  167. $size += $font->writeInt16($_component->a);
  168. }
  169. elseif ($flags & self::WE_HAVE_AN_X_AND_Y_SCALE) {
  170. $size += $font->writeInt16($_component->a);
  171. $size += $font->writeInt16($_component->d);
  172. }
  173. elseif ($flags & self::WE_HAVE_A_TWO_BY_TWO) {
  174. $size += $font->writeInt16($_component->a);
  175. $size += $font->writeInt16($_component->b);
  176. $size += $font->writeInt16($_component->c);
  177. $size += $font->writeInt16($_component->d);
  178. }
  179. }
  180. return $size;
  181. }
  182. public function getSVGContours(){
  183. $contours = array();
  184. /** @var Font_Table_glyf $glyph_data */
  185. $glyph_data = $this->getFont()->getTableObject("glyf");
  186. /** @var Font_Glyph_Outline[] $glyphs */
  187. $glyphs = $glyph_data->data;
  188. foreach ($this->components as $component) {
  189. $contours[] = array(
  190. "contours" => $glyphs[$component->glyphIndex]->getSVGContours(),
  191. "transform" => $component->getMatrix(),
  192. );
  193. }
  194. return $contours;
  195. }
  196. }