pomax / php-font-parser Goto Github PK
View Code? Open in Web Editor NEWThis is a PHP code library for parsing TTF/OTF fonts from file.
This is a PHP code library for parsing TTF/OTF fonts from file.
Can you add a list of supported characters to be called from the font object.
I am trying to get glyph paths of multi letters in one Ajax call.
Simply here is what I want:
[input] $text: "a sentence", $font: 'arial'
[output] array of glyphs
Here is my code:
// * VectorFont.php * //
require_once("fontparse.php");
class VectorFont {
protected $font;
public function loadFont($font)
{
$this->font = new \OTTTFont($font);
}
public function getGlyph($letter)
{
$data = null;
if($this->font) {
$data = $this->font->get_glyph($letter);
}
return $data;
}
}
// * test.php * //
getGlyphs('multiple letters', 'arial'); // memory exceed error
// receive multi-letter text as an input and return array of glyphs
public function getGlyphs($text, $font)
{
$glyphs = array();
$this->vectorFont = new VectorFont();
$this->vectorFont->loadFont($font.'.ttf');
for($i=0 ; $i<mb_strlen($text) ; $i++) {
// avoid error that occurs if call "get_glyph" with same letter more than once
$dupIndex = $this->findFirstDuplication(mb_substr($text, $i, 1), $text);
if($dupIndex >= 0 && $dupIndex < $i) {
// if duplicated letter existed previously, copy previous data
array_push($glyphs, $glyphs[$dupIndex]);
} else {
$glyph = $this->vectorFont->getGlyph(mb_substr($text, $i, 1));
if($glyph) {
array_push($glyphs, $glyph);
} else {
array_push($glyphs, "");
}
}
}
return $glyphs;
}
First of all, an error occurs if call "get_glyph" with same letter more than once. But I can write work around to avoid this problem, so this is not a big problem
But, I can hardly fix the memory issue. I don't understand why memory usage exceeds the limit even though font object is loaded just once at first. Do you have an idea to fix this problem?
parsing of type2 charstring 4-byte numbers ([255] + 32-bits) is incorrect. It should read in the full 32bit number, treated as a 2's complement pattern, and then record it as FIXED (signed short + unsigned short fraction).
Is there a reason for the font directory? Coulnd't be useful to load and parse the installed fonts?
the glyph fetcher currently caches on character, rather than on character per font, so using multiple fonts on the same run for the same character causes faulty runs.
When processing multiple glyphs, the processing speed is so slow and ,in my case, server's memory exceeds limit so cannot get desired result properly or quickly.
I hope this is a solution for this problem.
foreach ($text as $letter) {
while($glyph) {
if($glyph->letter == $letter) {
echo $glyph->d;
}
$glyph = nextGlygh();
}
}
Above algorithm makes server soooo busy because searching element in stdClass and looping over time allocates too much memory in the system. I don't know why the php creators wrote like this, but this is the nature of php just like you said.
$count = strlen($text);
$glyph = firstGlyph();
while($glyph && $count>0) {
foreach ($text as $letter) {
if($glyph->letter == $letter) {
$cache[$letter] = $glyph->d;
$count--;
}
}
$glyph = nextGlygh();
}
foreach ($text as $letter) {
echo $cache[$letter];
}
This algorithm is way faster and does not allocate wasteful memory. Hope this helps to enhance your library.
运行之后一直报异常
hi
Re your sample code which looks for letter "g" ... what if I needed to check for a glyph much higher up, eg U+232B for example?
Thanks, Ian
I downloaded and tried to test a font but encountered errors in the font test. The first was an easy fix. There is a missing parenthesis on line 34. But it was followed up by this:
font header data:
header:
{version: OTTO, number of tables: 14, search range: 128, entry selector: 3, range shift: 96}
tables:
{tag: BASE, checkSum: 1063407546, offset: 4284, length: 52}
{tag: CFF , checkSum: 838456334, offset: 4336, length: 15280}
{tag: DSIG, checkSum: 2007147259, offset: 22056, length: 5744}
{tag: GPOS, checkSum: 28576472, offset: 19616, length: 1554}
{tag: GSUB, checkSum: 1843949587, offset: 21172, length: 812}
{tag: LINO, checkSum: -253362844, offset: 21984, length: 72}
{tag: OS/2, checkSum: 2041581385, offset: 336, length: 96}
{tag: cmap, checkSum: 2129153568, offset: 2308, length: 932}
{tag: head, checkSum: -603480306, offset: 236, length: 54}
{tag: hhea, checkSum: 117441355, offset: 292, length: 36}
{tag: hmtx, checkSum: 105393449, offset: 3240, length: 1012}
{tag: maxp, checkSum: 16601088, offset: 328, length: 6}
{tag: name, checkSum: 1654205279, offset: 432, length: 1874}
{tag: post, checkSum: -4718542, offset: 4252, length: 32}
Fatal error: Call to a member function computeBounds() on a non-object in D:\websites\vitt.easypic.se\fonttest\Font parser\OTF data\Glyph classes\glyph.php on line 31
This is on Windows IIS 7.5 using PHP 5.3+
Hi,
When I am running "fonttest.php", I am getting an error:
Fatal error: Class 'Monolog\Formatter\LineFormatter' not found in /www/image_module_testbed_new/fontparser/Font parser/OTF data/Glyph classes/glyphrules.php on line 44
Font header is nicely printed out. But "$data = $font->get_glyph($letter);" makes such error.
And I can hardly fix it by myself.. Please help
The following code fails after the first letter is retrieved:
$text = "After all this time...";
$letters = str_split($text);
print_r($letters);
foreach($letters as $letter) {
$json = false;
while($json === false && count($fonts)>0) {
$font = new OTTTFont(array_pop($fonts));
echo "font header data:\n" . $font->toString() . "\n";
$data = $font->get_glyph($letter);
if($data!==false) {
$json = $data->toJSON();
}
}
}
The first letter is found, then the parser reports the font does not support "f". Splitting the string "fter all this time..." then finds the "f" but reports the font does not support "t", etc.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.