0

I have this problem across my mind, any help will be appreciated. Is there are PHP function, or library that can extract some glyphs form a font file?

Edit: By means to extract some glyphs, I wanted to create a new font file based on the glyphs extracted (so it has smaller bytes), or just get the glyphs and create base64 encoded font data from it so I can embed it via @font-face. I just wanted to get rid glyphs that has no use so CSS (base64) or font file will be loaded faster.

Habib Rosyad
  • 332
  • 1
  • 5
  • 15
  • 2
    extract them as what exactly? e.g. as a PNG image of the glyph at a particular font size and variant? – jcomeau_ictx Nov 23 '10 at 08:20
  • 1
    What kind of font file? There's lots of different formats (TTF, ABF, BDF....) – symcbean Nov 23 '10 at 12:42
  • Yeah, please be more specific – Pekka Nov 23 '10 at 12:48
  • It's should be TTF or OTF, and sorry I seems to make confusions here. By means to extract some glyphs, I wanted to create a new font file based on the glyphs extracted (so it has smaller bytes). Of course I know there are a lot of software out there that can do this magic like FontForge, or online like FontSquirrel @font-face generator, but it isn't likely made of PHP. – Habib Rosyad Nov 25 '10 at 05:16
  • I appreciate your question was about PHP, but just in case you can consider Python, there is many excellent font manipulation libraries, especially https://github.com/fonttools/fonttools – kontur Sep 29 '17 at 06:56

3 Answers3

0

I can understand your question two ways. Either you want to pull the image out of the font file directly, or you want to print with that font as text in an image.

But both have the same answer:

Try GD or ImageMagick to pull the image of a glyph from a font file using the font to print that character into the image. Don't open the font like an image, use the font as it was intended. You can then print the glyph at whatever size you want and use it however you want.

DampeS8N
  • 3,621
  • 17
  • 20
  • Sorry to make confusions here, but I greatly appreciate your answer. I have edited my question before with more detailed explanations. Anyway it doesn't has anything to do with print font as images. – Habib Rosyad Nov 25 '10 at 05:28
  • I wouldn't do what you want to do. Fonts are so small that it shouldn't matter, and forcing people to download new altered fonts means people who did have the other font will have to download it. – DampeS8N Nov 26 '10 at 13:37
  • Well, it does matter for the sake of page speed. If you don't use the glyphs why should you load it? Yes most of fonts size are small, but in case you need to use multiple fonts, this could be a problem. The new altered font is still the same font anyway, it just reduced by some glyphs, and so it has no problem in downloading it. – Habib Rosyad Nov 27 '10 at 18:26
  • Unless you are running a font site, where the point of the site is to display 100 fonts on each page, this shouldn't matter. And in that case, you should pre-bake an image of text in those fonts, not display them in the page. If your design requires so many special fonts, your design is probably not so good. I hate to fall back on being a /b/tard but seriously. You are doing it wrong. – DampeS8N Nov 29 '10 at 12:25
0

You are trying to make a subset font aren't you? This problem have been investigated heavily by the embedded font community.

Even that, there is not definite answer here. Last time I checked people write Perl/C programs to extract binary data from font files, based on TTF/OTF spec.

If you are using Latin font, don't brother coz it's small enough. If you are using CJK font like me, FontForge could help you copy and paste the gryphs by hand. Other than that you need to write a script/program by yourself.

timdream
  • 5,914
  • 5
  • 21
  • 24
  • Yes it should be subset font. Well, I'am just wondering if there's already more simple way available to make this happen instead of just relying on external executable programs/scripts (non-PHP). – Habib Rosyad Nov 25 '10 at 14:37
  • I would also love to know if there is any scriptable solution in PHP (even if it evolves `system()`) – timdream Nov 27 '10 at 14:36
0

I did it once using python : Here is the code snippet

def get_pdf_fontobjects(self, ):
    pdffile = self.pdffile
    pdf_font_objects = []
    p = subprocess.Popen(["pdffonts", pdffile], shell=False, stdout=subprocess.PIPE)
    for line in p.stdout:
        line = line.strip()
        g = re.search("^(\S+)\s.+?yes\s+(\d+)\s+(\d+)$", line)

        '''if g and g.group(1) in self.fonts2process:
            pdf_font_objects.append(g.group(2))'''

        if g:
            font_name = g.group(1)
            for font_pattern in self.fonts2process:
                if re.search(font_pattern, font_name):
                    pdf_font_objects.append(g.group(2))

    return pdf_font_objects

and font2process can be a list of font names: fonts2process = ['Arial-Unicode-MS', 'Shruti', 'ArialUnicodeMS', 'TT', 'Raavi', 'SolaimanLipi', 'AnjaliOldLipi', 'Vrinda', 'Surekh', 'AAAAAB+ArialUnicodeMS', 'AAAAAG+VArialUnicodeMS', 'DNCDIV+Tunga_00', 'JGTFDM+Tunga,Bold_00', 'Latha', 'Mangal', 'Surekh', 'Gautami' ]

But working on certain font without makers permission is illegal*

aryan singh
  • 151
  • 11