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