Listing available fonts in PHP

2019-02-09 07:50发布

问题:

I'd like to query what fonts are available on a *nix-like system, ideally using PHP 5.2 or 5.3, but there doesn't seem to be many libraries available to do it in a system-independent way. php-font-lib just scans a directory (code, demo) but I'd hope there is a more elegant way to do it. (In any case, scanning a folder is only part of the task; font files still need to be opened to retrieve name and other metadata).

Background: I am rendering PDFs on a webserver using Inkscape, and would like to determine what fonts are available to render text elements. I'm developing on OS 10.6.8 and deploying on Linux.

Edit: I've found a PHP wrapper for Pango, which is an advanced font renderer often used with Cairo; both of these are used in Inkscape. The wrapper is quite new, so I am not yet sure whether it can produce a font listing - but it does look interesting :).

回答1:

I've just found a cross-platform command, fc-list. Works on OS X 10.6.8 and my Ubuntu VPS, and could easily be invoked by PHP. On my Linux machine I get a list in this format:

Nimbus Sans L:style=Regular Italic
URW Palladio L:style=Roman
Century Schoolbook L:style=Bold Italic
Nimbus Sans L:style=Bold
URW Chancery L:style=Medium Italic
Nimbus Roman No9 L:style=Regular
Century Schoolbook L:style=Bold
Century Schoolbook L:style=Italic
Nimbus Sans L:style=Regular
URW Palladio L:style=Italic
Nimbus Sans L:style=Bold Condensed
URW Gothic L:style=Demi
Nimbus Roman No9 L:style=Medium Italic
Nimbus Mono L:style=Bold Oblique
Dingbats:style=Regular
...continues...

On the Mac it looks like this:

URW Gothic L:style=Book Oblique
Fixed:style=ja
URW Palladio L:style=Bold Italic
Heiti SC,黑體\-簡,黒体\-簡,Heiti\-간체,黑体\-简:style=Light,細體,Mager,Fein,Ohut,Fin,Leggero,ライト,가는체,Licht,Tynn,Leve,Светлый,细体,Fina
Arial Narrow:style=Bold Italic,Negreta cursiva,tučné kurzíva,fed kursiv,Fett Kursiv,Έντονα Πλάγια,Negrita Cursiva,Lihavoitu Kursivoi,Gras Italique,Félkövér dőlt,Grassetto Corsivo,Vet Cursief,Halvfet Kursiv,Pogrubiona kursywa,Negrito Itálico,Полужирный Курсив,Tučná kurzíva,Fet Kursiv,Kalın İtalik,Krepko poševno,Lodi etzana
Fixed:style=Regular
Helvetica:style=Bold Oblique
Arial:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,đậm,Lodia
KufiStandardGK:style=Regular,標準體,Ordinær,Normal,Normaali,Regolare,レギュラー,일반체,Regulier,Обычный,常规体
Courier:style=Regular,標準體,Ordinær,Normal,Normaali,Regolare,レギュラー,일반체,Regulier,Обычный,常规体
New Century Schoolbook:style=Italic
Menlo:style=Bold
Luxi Sans:style=Bold Oblique
...


回答2:

Heh! I do an hour or so of searching, and find nothing satisfying; then like buses, several answers are found at once. Here's another method, from ImageMagick's convert -list font command (apparently renamed from convert -list type prior to v6.3.5-7 [source]). I think I prefer this over fc-list, as the output looks like it could be parsed as YAML.

From Linux:

Path: /usr/lib/ImageMagick-6.5.7/config/type-ghostscript.xml
    Font: AvantGarde-Book
        family: AvantGarde
        style: Normal
        stretch: Normal
        weight: 400
        glyphs: /usr/share/fonts/type1/gsfonts/a010013l.pfb
    Font: AvantGarde-BookOblique
        family: AvantGarde
        style: Oblique
        stretch: Normal
        weight: 400
        glyphs: /usr/share/fonts/type1/gsfonts/a010033l.pfb
...

And from the Mac:

Path: /opt/local/etc/ImageMagick/type-ghostscript.xml
    Font: AvantGarde-Book
        family: AvantGarde
        style: Normal
        stretch: Normal
        weight: 400
        glyphs: /opt/local/share/fonts/urw-fonts/a010013l.pfb
    Font: AvantGarde-BookOblique
        family: AvantGarde
        style: Oblique
        stretch: Normal
        weight: 400
        glyphs: /opt/local/share/fonts/urw-fonts/a010033l.pfb
...
Path: System Fonts
    Font: .Aqua-Kana-Bold
        family: .Aqua Kana
        style: Normal
        stretch: Normal
        weight: 600
        glyphs: /System/Library/Fonts/AquaKana.ttc
    Font: .Aqua-Kana-Regular
        family: .Aqua Kana
        style: Normal
        stretch: Normal
        weight: 300
        glyphs: /System/Library/Fonts/AquaKana.ttc


回答3:

This also yields results, although it just gets the font name as per the Font: line from convert output:

<?php
    $imagick = new Imagick();
    $fonts = $imagick->queryFonts();
    foreach($fonts as $font)
    {
        echo $font;
    }
?>


标签: php fonts