您当前的位置:首页 > 计算机 > 编程开发 > Python

How to list all supported glyphs of a given font

时间:07-12来源:作者:点击数:

For a TrueType font, you can use thettfdumputility which is available from TeXlive.

ttfdump -t cmap -i /usr/share/fonts/truetype/freefont/FreeSerif.ttf |
perl -CS -ne 'print chr(hex($1)) if /Char (0x[[:xdigit:]]+)/ and hex($1) != 0xffff; END {print "\n"}'

Experimentally, this only seems to list code points below U+FFFF. I don't know if this is a bug inttfdumpor if this is because extra work is needed to reach other planes.

For any font that is supported by Freetype, the Freetype library makes this information available, but there doesn't seem to be a readily available command line utility to query it. Here's a one-liner using the Freetype-py Python bindings, which you can install withpip3 install --user freetype-py.

python3 -c 'import freetype, sys; stdout = open(1, mode="w", encoding="utf8"); face = freetype.Face(sys.argv[1]); stdout.write("".join(sorted([chr(c) for c, g in face.get_chars() if c]) + ["\n"]))' /usr/share/fonts/truetype/freefont/FreeSerif.ttf

For fonts that could be used in Linux with fontconfig (almost all).

There are two related questions that usually come from fonts and Unicode:

  1. List fonts that could render (contain) a glyph (well, actually, an Unicode code point):
    fc-list :charset=1f63f | less      # all installed fonts that have 1f63f 
    
  2. List Unicode code points that an specific font covers:
  • Look at the table of charset for the font:
     fc-query /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf  | less
    
  • List Unicode character ranges for all supported code points (glyphs?):
     fc-query --format='%{charset}\n' /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf | less
    

Both commands could work with an non-installed font file. If the font has been installed, the commandfc-matchdoes the same work.

The list is not exactly "each glyph", it needs additional work to get there, but is a reasonable clear list of what a font contains.

Since looking to glyphs is a visual task, you need a GUI application to see the glyphs of a font. Printing them on the command line could be done with any font that is configured by the terminal application, not the font you mean to use.

fontcharlist

You can try the following script (called fontcharlist):

#!/bin/bash -

Usage() { echo "$0 FontFile"; exit 1; }
SayError() { local error=$1; shift; echo "$0: $@"; exit "$error"; }

[ "$#" -ne 1 ] && Usage

width=70
fontfile="$1"

[ -f "$fontfile" ] || SayError 4 'File not found'

list=$(fc-query --format='%{charset}\n' "$fontfile")

for    range in $list
do     IFS=- read start end <<<"$range"
       if    [ "$end" ]
       then
             start=$((16#$start))
         end=$((16#$end))
         for((i=start;i<=end;i++)); do
         printf -v char '\\U%x' "$i"
         printf '%b' "$char"
         done
       else
         printf '%b' "\\U$start"
       fi
done | grep -oP '.{'"$width"'}'

And get a list of characters in a font like this:

$  ./fontcharlist /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcde
fghijklmnopqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌ
ÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒ
ēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘ
řŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏƐƑƒƓƔƕƖƗƘƙƚƛƜƝƞ
ƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǃǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟǠǡǢǣǦǧǨǩǪǫǬǭǮǯ
ǰǴǵǶǸǹǼǽǾǿȀȁȂȃȄȅȆȇȈȉȊȋȌȍȎȏȐȑȒȓȔȕȖȗȘșȚțȜȝȞȟȠȡȤȥȦȧȨȩȪȫȬȭȮȯȰȱȲȳȴȵȶȷȸȹȺȻȼȽ
ȾȿɀɁɃɄɅɌɍɐɑɒɓɔɕɖɗɘəɚɛɜɝɞɟɠɡɢɣɤɥɦɧɨɩɪɫɬɭɮɯɰɱɲɳɴɵɶɷɸɹɺɻɼɽɾɿʀʁʂʃʄʅʆʇʈʉʊʋʌ
ʍʎʏʐʑʒʓʔʕʖʗʘʙʚʛʜʝʞʟʠʡʢʣʤʥʦʧʨʩʪʫʬʭʮʯʰʱʲʳʴʵʶʷʸʹʻʼʽʾʿˀˁˆˇˈˉˌˍˎˏːˑ˒˓˖˗˘˙˚˛
˜˝˞ˠˡˢˣˤ˥˦˧˨˩ˮ˳̀́̂̃̄̅̆̇̈̉
ʹ͵Ͷͷͺͻͼͽ;Ϳ΄΅Ά·ΈΉΊΌΎΏΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩΪΫάέήίΰαβγδεζ
ηθικλμνξοπρςστυφχψωϊϋόύώϐϑϒϓϔϕϖϗϘϙϚϛϜϝϞϟϠϡϰϱϲϳϴϵ϶ϷϸϹϺϻϼϽϾϿЀЁЂЃЄЅІЇЈЉЊЋ
ЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяѐё
ђѓєѕіїјљњћќѝўџѢѣѲѳҐґҒғҔҕҖҗҘҙҚқҢңҤҥҪҫҬҭҮүҰұҲҳҺһӀӁӂӃӄӇӈӋӌӏӐӑӒӓӔӕӖӗӘәӚӛӜӝ
ӞӟӠӡӢӣӤӥӦӧӨөӪӫӬӭӮӯӰӱӲӳӴӵӶӷӸӹԐԑԚԛԜԝԱԲԳԴԵԶԷԸԹԺԻԼԽԾԿՀՁՂՃՄՅՆՇՈՉՊՋՌՍՎՏՐՑՒՓՔ
ՕՖՙ՚՛՜՝՞՟աբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆև։֊؆؇؉؊،ؕ؛؟ءآأؤإئابةتثج
حخدذرزسشصضطظعغـفقكلمنهوىيًٌٍَُِّْٓٔ٠١٢٣٤٥٦٧٨٩٪٫٬٭ٴٹٺٻپٿڀڃڄچڇڑژڤکگھی۰
۱۲۳۴۵۶۷۸۹฿ກຂຄງຈຊຍດຕຖທນບປຜຝພຟມຢຣລວສຫອຮຯະັາຳິີຶືຸູົຼ່້აბგდევზთიკლმნო
პჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶჷჸჹჺ჻ჼᴂᴈᴉᴔᴖᴗᴝᴞᴟᴬᴭᴮᴰᴱᴲᴳᴴᴵᴶᴷᴸᴹᴺᴻᴼᴾᴿᵀᵁᵂᵃᵄᵅᵆᵇᵈᵉᵊᵋ
ᵌᵍᵎᵏᵐᵑᵒᵓᵔᵕᵖᵗᵘᵙᵚᵛᵢᵣᵤᵥᵷᵸᵻᶅᶛᶜᶝᶞᶟᶠᶡᶢᶣᶤᶥᶦᶧᶨᶩᶪᶫᶬᶭᶮᶯᶰᶱᶲᶳᶴᶵᶶᶷᶹᶺᶻᶼᶽᶾᶿḀḁḂḃḄḅḆḇḈḉ
ḊḋḌḍḎḏḐḑḒḓḘḙḚḛḜḝḞḟḠḡḢḣḤḥḦḧḨḩḪḫḬḭḰḱḲḳḴḵḶḷḸḹḺḻḼḽḾḿṀṁṂṃṄṅṆṇṈṉṊṋṌṍṔṕṖṗṘṙṚṛ
ṜṝṞṟṠṡṢṣṨṩṪṫṬṭṮṯṰṱṲṳṴṵṶṷṸṹṼṽṾṿẀẁẂẃẄẅẆẇẈẉẊẋẌẍẎẏẐẑẒẓẔẕẖẗẘẙẛẟẠạẬậẰằẶặẸẹẼẽ
ỆệỊịỌọỘộỚớỜờỠỡỢợỤụỨứỪừỮữỰựỲỳỴỵỸỹἀἁἂἃἄἅἆἇἈἉἊἋἌἍἎἏἐἑἒἓἔἕἘἙἚἛἜἝἠἡἢἣἤἥἦἧἨἩ
ἪἫἬἭἮἯἰἱἲἳἴἵἶἷἸἹἺἻἼἽἾἿὀὁὂὃὄὅὈὉὊὋὌὍὐὑὒὓὔὕὖὗὙὛὝὟὠὡὢὣὤὥὦὧὨὩὪὫὬὭὮὯὰάὲέὴήὶί
ὸόὺύὼώᾀᾁᾂᾃᾄᾅᾆᾇᾈᾉᾊᾋᾌᾍᾎᾏᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾞᾟᾠᾡᾢᾣᾤᾥᾦᾧᾨᾩᾪᾫᾬᾭᾮᾯᾰᾱᾲᾳᾴᾶᾷᾸᾹᾺΆᾼ᾽ι᾿῀
῁ῂῃῄῆῇῈΈῊΉῌ῍῎῏ῐῑῒΐῖῗῘῙῚΊ῝῞῟ῠῡῢΰῤῥῦῧῨῩῪΎῬ῭΅`ῲῳῴῶῷῸΌῺΏῼ´῾           ‐‑‒–
—―‖‗‘’‚‛“”„‟†‡•‣… ‰‱′″‴‵‶‷‹›‼‽‾‿⁅⁆⁇⁈⁉⁋ ⁰ⁱ⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ⁿ₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎ₐₑ
ₒₓₔₕₖₗₘₙₚₛₜ₠₡₢₣₤₥₦₧₨₩₪₫€₭₮₯₰₱₲₳₴₵₸₹₺₽ℂ℅ℍℎℏℕ№℗ℙℚℝ™ℤΩKÅ℮ⅈ⅐⅑⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟
↉←↑→↓↔↕↖↗↘↙↚↛↜↝↞↟↠↡↢↣↤↥↦↧↨↩↪↫↬↭↮↯↰↱↲↳↴↵↶↷↸↹↺↻↼↽↾↿⇀⇁⇂⇃⇄⇅⇆⇇⇈⇉⇊⇋⇌⇍⇎⇏⇐⇑⇒⇓⇔
⇕⇖⇗⇘⇙⇚⇛⇜⇝⇞⇟⇠⇡⇢⇣⇤⇥⇦⇧⇨⇩⇪⇫⇬⇭⇮⇯⇰⇱⇲⇳⇴⇵⇶⇷⇸⇹⇺⇻⇼⇽⇾⇿∀∁∂∃∄∅∆∇∈∉∊∋∌∍∎∏∐∑−∓∕∗∘∙√∛∜
∝∞∟∠∣∧∨∩∪∫∬∭∴∵∶∷∸∹∺∻∼∽≁≂≃≄≅≆≇≈≉≊≋≌≍≎≏≐≑≒≓≔≕≖≗≘≙≚≛≜≝≞≟≠≡≢≣≤≥≦≧≨≩≭≮≯≰≱≲≳
≴≵≶≷≸≹≺≻≼≽≾≿⊀⊁⊂⊃⊄⊅⊆⊇⊈⊉⊊⊋⊍⊎⊏⊐⊑⊒⊓⊔⊕⊖⊗⊘⊙⊚⊛⊜⊝⊞⊟⊠⊡⊢⊣⊤⊥⊲⊳⊴⊵⊸⋂⋃⋄⋅⋆⋍⋎⋏⋐⋑⋚⋛⋜⋝⋞⋟
⋠⋡⋢⋣⋤⋥⋦⋧⋨⋩⋯⌀⌁⌂⌃⌄⌅⌆⌈⌉⌊⌋⌌⌍⌎⌏⌐⌑⌒⌓⌔⌕⌘⌙⌜⌝⌞⌟⌠⌡⌥⌦⌧⌨⌫⌵⌶⌷⌸⌹⌺⌻⌼⌽⌾⌿⍀⍁⍂⍃⍄⍅⍆⍇⍈⍉⍊⍋⍌⍍
⍎⍏⍐⍑⍒⍓⍔⍕⍖⍗⍘⍙⍚⍛⍜⍝⍞⍟⍠⍡⍢⍣⍤⍥⍦⍧⍨⍩⍪⍫⍬⍭⍮⍯⍰⍱⍲⍳⍴⍵⍶⍷⍸⍹⍺⍽⎀⎁⎂⎃⎈⎉⎊⎋⎕⎛⎜⎝⎞⎟⎠⎡⎢⎣⎤⎥⎦⎧⎨⎩
⎪⎫⎬⎭⎮⏎⏏␣─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽
┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿▀▁▂▃
▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿◀◁◂◃◄◅◆◇◈◉
◊○◌◍◎●◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮◯◰◱◲◳◴◵◶◷◸◹◺◻◼◽◾◿☀☁☂☃☄★☆☇☈☉☊☋☌☍☎☏
☐☑☒☓☔☕☖☗☘☙☚☛☜☝☞☟☠☡☢☣☤☥☦☧☨☩☪☫☬☭☮☯☸☹☺☻☼☽☾☿♀♁♂♃♄♅♆♇♈♉♊♋♌♍♎♏♐♑♒♓♔♕♖♗♘♙♚♛♜♝
♞♟♠♡♢♣♤♥♦♧♨♩♪♫♬♭♮♯♰♱♲♳♴♵♶♷♸♹♺♻♼♽♾♿⚀⚁⚂⚃⚄⚅⚆⚇⚈⚉⚊⚋⚐⚑⚒⚓⚔⚕⚖⚗⚘⚙⚚⚛⚜⚠⚡⚰⚱✁✂✃✄✆✇✈
✉✌✍✎✏✐✑✒✓✔✕✖✗✘✙✚✛✜✝✞✟✠✡✢✣✤✥✦✧✩✪✫✬✭✮✯✰✱✲✳✴✵✶✷✸✹✺✻✼✽✾✿❀❁❂❃❄❅❆❇❈❉❊❋❍❏❐❑❒❖
❘❙❚❛❜❝❞❡❢❣❤❥❦❧❨❩❪❫❬❭❮❯❰❱❲❳❴❵➔➘➙➚➛➜➝➞➟➠➡➢➣➤➥➦➧➨➩➪➫➬➭➮➯➱➲➳➴➵➶➷➸➹➺➻➼➽➾⟂⟅⟆
⟜⟠⟦⟧⟨⟩⟪⟫⟵⟶⟷⦇⦈⦗⦘⧫⧺⧻⨀⨯⩪⩫⬅⬆⬇⬈⬉⬊⬋⬌⬍⬒⬓⬔⬕⬖⬗⬘⬙⬚ⱤⱭⱮⱯⱰⱵⱶⱷⱹⱺⱼⱽⱾⱿ⸘⸟⸢⸣⸤⸥⸮꜈꜉꜊꜋꜌꜍꜎꜏꜐
꜑꜒꜓꜔꜕꜖ꜛꜜꜝꜞꜟꜢꜣꜤꜥꜦꜧ꞉꞊ꞋꞌꞍꞎꞐꞑꞪꟸꟹfiflﭒﭓﭔﭕﭖﭗﭘﭙﭚﭛﭜﭝﭞﭟﭠﭡﭢﭣﭤﭥﭦﭧﭨﭩﭪﭫﭬﭭﭮﭯﭰﭱﭲﭳﭴﭵﭶﭷﭸ
ﭹﭺﭻﭼﭽﭾﭿﮀﮁﮊﮋﮌﮍﮎﮏﮐﮑﮒﮓﮔﮕﮞﮟﮪﮫﮬﮭﯨﯩﯼﯽﯾﯿﹰﹱﹲﹳﹴﹶﹷﹸﹹﹺﹻﹼﹽﹾﹿﺀﺁﺂﺃﺄﺅﺆﺇﺈﺉﺊﺋﺌﺍﺎﺏﺐﺑﺒﺓﺔﺕ
ﺖﺗﺘﺙﺚﺛﺜﺝﺞﺟﺠﺡﺢﺣﺤﺥﺦﺧﺨﺩﺪﺫﺬﺭﺮﺯﺰﺱﺲﺳﺴﺵﺶﺷﺸﺹﺺﺻﺼﺽﺾﺿﻀﻁﻂﻃﻄﻅﻆﻇﻈﻉﻊﻋﻌﻍﻎﻏﻐﻑﻒﻓﻔﻕﻖﻗﻘﻙﻚﻛ
ﻜﻝﻞﻟﻠﻡﻢﻣﻤﻥﻦﻧﻨﻩﻪﻫﻬﻭﻮﻯﻰﻱﻲﻳﻴﻵﻶﻷﻸﻹﻺﻻﻼ�

To actually see the exact same glyphs from a font usexfd:

xfd -fa /usr/share/fonts/truetype/unifont/unifont_upper.ttf

Which is part of thex11-utilspackage.

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
    无相关信息
栏目更新
栏目热门