PageRenderTime 291ms CodeModel.GetById 103ms app.highlight 10ms RepoModel.GetById 91ms app.codeStats 0ms

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