__init__.py 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838
  1. # Copyright 2013 Google, Inc. All Rights Reserved.
  2. #
  3. # Google Author(s): Behdad Esfahbod
  4. from fontTools import config
  5. from fontTools.misc.roundTools import otRound
  6. from fontTools import ttLib
  7. from fontTools.ttLib.tables import otTables
  8. from fontTools.ttLib.tables.otBase import USE_HARFBUZZ_REPACKER
  9. from fontTools.otlLib.maxContextCalc import maxCtxFont
  10. from fontTools.pens.basePen import NullPen
  11. from fontTools.misc.loggingTools import Timer
  12. from fontTools.misc.cliTools import makeOutputFileName
  13. from fontTools.subset.util import _add_method, _uniq_sort
  14. from fontTools.subset.cff import *
  15. from fontTools.subset.svg import *
  16. from fontTools.varLib import varStore, multiVarStore # For monkey-patching
  17. from fontTools.ttLib.tables._n_a_m_e import NameRecordVisitor
  18. import sys
  19. import struct
  20. import array
  21. import logging
  22. from collections import Counter, defaultdict
  23. from functools import reduce
  24. from types import MethodType
  25. __usage__ = "pyftsubset font-file [glyph...] [--option=value]..."
  26. __doc__ = (
  27. """\
  28. pyftsubset -- OpenType font subsetter and optimizer
  29. pyftsubset is an OpenType font subsetter and optimizer, based on fontTools.
  30. It accepts any TT- or CFF-flavored OpenType (.otf or .ttf) or WOFF (.woff)
  31. font file. The subsetted glyph set is based on the specified glyphs
  32. or characters, and specified OpenType layout features.
  33. The tool also performs some size-reducing optimizations, aimed for using
  34. subset fonts as webfonts. Individual optimizations can be enabled or
  35. disabled, and are enabled by default when they are safe.
  36. Usage: """
  37. + __usage__
  38. + """
  39. At least one glyph or one of --gids, --gids-file, --glyphs, --glyphs-file,
  40. --text, --text-file, --unicodes, or --unicodes-file, must be specified.
  41. Args:
  42. font-file
  43. The input font file.
  44. glyph
  45. Specify one or more glyph identifiers to include in the subset. Must be
  46. PS glyph names, or the special string '*' to keep the entire glyph set.
  47. Initial glyph set specification
  48. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  49. These options populate the initial glyph set. Same option can appear
  50. multiple times, and the results are accummulated.
  51. --gids=<NNN>[,<NNN>...]
  52. Specify comma/whitespace-separated list of glyph IDs or ranges as decimal
  53. numbers. For example, --gids=10-12,14 adds glyphs with numbers 10, 11,
  54. 12, and 14.
  55. --gids-file=<path>
  56. Like --gids but reads from a file. Anything after a '#' on any line is
  57. ignored as comments.
  58. --glyphs=<glyphname>[,<glyphname>...]
  59. Specify comma/whitespace-separated PS glyph names to add to the subset.
  60. Note that only PS glyph names are accepted, not gidNNN, U+XXXX, etc
  61. that are accepted on the command line. The special string '*' will keep
  62. the entire glyph set.
  63. --glyphs-file=<path>
  64. Like --glyphs but reads from a file. Anything after a '#' on any line
  65. is ignored as comments.
  66. --text=<text>
  67. Specify characters to include in the subset, as UTF-8 string.
  68. --text-file=<path>
  69. Like --text but reads from a file. Newline character are not added to
  70. the subset.
  71. --unicodes=<XXXX>[,<XXXX>...]
  72. Specify comma/whitespace-separated list of Unicode codepoints or
  73. ranges as hex numbers, optionally prefixed with 'U+', 'u', etc.
  74. For example, --unicodes=41-5a,61-7a adds ASCII letters, so does
  75. the more verbose --unicodes=U+0041-005A,U+0061-007A.
  76. The special strings '*' will choose all Unicode characters mapped
  77. by the font.
  78. --unicodes-file=<path>
  79. Like --unicodes, but reads from a file. Anything after a '#' on any
  80. line in the file is ignored as comments.
  81. --ignore-missing-glyphs
  82. Do not fail if some requested glyphs or gids are not available in
  83. the font.
  84. --no-ignore-missing-glyphs
  85. Stop and fail if some requested glyphs or gids are not available
  86. in the font. [default]
  87. --ignore-missing-unicodes [default]
  88. Do not fail if some requested Unicode characters (including those
  89. indirectly specified using --text or --text-file) are not available
  90. in the font.
  91. --no-ignore-missing-unicodes
  92. Stop and fail if some requested Unicode characters are not available
  93. in the font.
  94. Note the default discrepancy between ignoring missing glyphs versus
  95. unicodes. This is for historical reasons and in the future
  96. --no-ignore-missing-unicodes might become default.
  97. Other options
  98. ^^^^^^^^^^^^^
  99. For the other options listed below, to see the current value of the option,
  100. pass a value of '?' to it, with or without a '='. In some environments,
  101. you might need to escape the question mark, like this: '--glyph-names\\?'.
  102. Examples::
  103. $ pyftsubset --glyph-names?
  104. Current setting for 'glyph-names' is: False
  105. $ pyftsubset --name-IDs=?
  106. Current setting for 'name-IDs' is: [0, 1, 2, 3, 4, 5, 6]
  107. $ pyftsubset --hinting? --no-hinting --hinting?
  108. Current setting for 'hinting' is: True
  109. Current setting for 'hinting' is: False
  110. Output options
  111. ^^^^^^^^^^^^^^
  112. --output-file=<path>
  113. The output font file. If not specified, the subsetted font
  114. will be saved in as font-file.subset.
  115. --flavor=<type>
  116. Specify flavor of output font file. May be 'woff' or 'woff2'.
  117. Note that WOFF2 requires the Brotli Python extension, available
  118. at https://github.com/google/brotli
  119. --with-zopfli
  120. Use the Google Zopfli algorithm to compress WOFF. The output is 3-8 %
  121. smaller than pure zlib, but the compression speed is much slower.
  122. The Zopfli Python bindings are available at:
  123. https://pypi.python.org/pypi/zopfli
  124. --harfbuzz-repacker
  125. By default, we serialize GPOS/GSUB using the HarfBuzz Repacker when
  126. uharfbuzz can be imported and is successful, otherwise fall back to
  127. the pure-python serializer. Set the option to force using the HarfBuzz
  128. Repacker (raises an error if uharfbuzz can't be found or fails).
  129. --no-harfbuzz-repacker
  130. Always use the pure-python serializer even if uharfbuzz is available.
  131. Glyph set expansion
  132. ^^^^^^^^^^^^^^^^^^^
  133. These options control how additional glyphs are added to the subset.
  134. --retain-gids
  135. Retain glyph indices; just empty glyphs not needed in-place.
  136. --notdef-glyph
  137. Add the '.notdef' glyph to the subset (ie, keep it). [default]
  138. --no-notdef-glyph
  139. Drop the '.notdef' glyph unless specified in the glyph set. This
  140. saves a few bytes, but is not possible for Postscript-flavored
  141. fonts, as those require '.notdef'. For TrueType-flavored fonts,
  142. this works fine as long as no unsupported glyphs are requested
  143. from the font.
  144. --notdef-outline
  145. Keep the outline of '.notdef' glyph. The '.notdef' glyph outline is
  146. used when glyphs not supported by the font are to be shown. It is not
  147. needed otherwise.
  148. --no-notdef-outline
  149. When including a '.notdef' glyph, remove its outline. This saves
  150. a few bytes. [default]
  151. --recommended-glyphs
  152. Add glyphs 0, 1, 2, and 3 to the subset, as recommended for
  153. TrueType-flavored fonts: '.notdef', 'NULL' or '.null', 'CR', 'space'.
  154. Some legacy software might require this, but no modern system does.
  155. --no-recommended-glyphs
  156. Do not add glyphs 0, 1, 2, and 3 to the subset, unless specified in
  157. glyph set. [default]
  158. --no-layout-closure
  159. Do not expand glyph set to add glyphs produced by OpenType layout
  160. features. Instead, OpenType layout features will be subset to only
  161. rules that are relevant to the otherwise-specified glyph set.
  162. --layout-features[+|-]=<feature>[,<feature>...]
  163. Specify (=), add to (+=) or exclude from (-=) the comma-separated
  164. set of OpenType layout feature tags that will be preserved.
  165. Glyph variants used by the preserved features are added to the
  166. specified subset glyph set. By default, 'calt', 'ccmp', 'clig', 'curs',
  167. 'dnom', 'frac', 'kern', 'liga', 'locl', 'mark', 'mkmk', 'numr', 'rclt',
  168. 'rlig', 'rvrn', and all features required for script shaping are
  169. preserved. To see the full list, try '--layout-features=?'.
  170. Use '*' to keep all features.
  171. Multiple --layout-features options can be provided if necessary.
  172. Examples:
  173. --layout-features+=onum,pnum,ss01
  174. * Keep the default set of features and 'onum', 'pnum', 'ss01'.
  175. --layout-features-='mark','mkmk'
  176. * Keep the default set of features but drop 'mark' and 'mkmk'.
  177. --layout-features='kern'
  178. * Only keep the 'kern' feature, drop all others.
  179. --layout-features=''
  180. * Drop all features.
  181. --layout-features='*'
  182. * Keep all features.
  183. --layout-features+=aalt --layout-features-=vrt2
  184. * Keep default set of features plus 'aalt', but drop 'vrt2'.
  185. --layout-scripts[+|-]=<script>[,<script>...]
  186. Specify (=), add to (+=) or exclude from (-=) the comma-separated
  187. set of OpenType layout script tags that will be preserved. LangSys tags
  188. can be appended to script tag, separated by '.', for example:
  189. 'arab.dflt,arab.URD,latn.TRK'. By default all scripts are retained ('*').
  190. Hinting options
  191. ^^^^^^^^^^^^^^^
  192. --hinting
  193. Keep hinting [default]
  194. --no-hinting
  195. Drop glyph-specific hinting and font-wide hinting tables, as well
  196. as remove hinting-related bits and pieces from other tables (eg. GPOS).
  197. See --hinting-tables for list of tables that are dropped by default.
  198. Instructions and hints are stripped from 'glyf' and 'CFF ' tables
  199. respectively. This produces (sometimes up to 30%) smaller fonts that
  200. are suitable for extremely high-resolution systems, like high-end
  201. mobile devices and retina displays.
  202. Optimization options
  203. ^^^^^^^^^^^^^^^^^^^^
  204. --desubroutinize
  205. Remove CFF use of subroutinizes. Subroutinization is a way to make CFF
  206. fonts smaller. For small subsets however, desubroutinizing might make
  207. the font smaller. It has even been reported that desubroutinized CFF
  208. fonts compress better (produce smaller output) WOFF and WOFF2 fonts.
  209. Also see note under --no-hinting.
  210. --no-desubroutinize [default]
  211. Leave CFF subroutinizes as is, only throw away unused subroutinizes.
  212. Font table options
  213. ^^^^^^^^^^^^^^^^^^
  214. --drop-tables[+|-]=<table>[,<table>...]
  215. Specify (=), add to (+=) or exclude from (-=) the comma-separated
  216. set of tables that will be be dropped.
  217. By default, the following tables are dropped:
  218. 'BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH'
  219. and Graphite tables: 'Feat', 'Glat', 'Gloc', 'Silf', 'Sill'.
  220. The tool will attempt to subset the remaining tables.
  221. Examples:
  222. --drop-tables-=BASE
  223. * Drop the default set of tables but keep 'BASE'.
  224. --drop-tables+=GSUB
  225. * Drop the default set of tables and 'GSUB'.
  226. --drop-tables=DSIG
  227. * Only drop the 'DSIG' table, keep all others.
  228. --drop-tables=
  229. * Keep all tables.
  230. --no-subset-tables+=<table>[,<table>...]
  231. Add to the set of tables that will not be subsetted.
  232. By default, the following tables are included in this list, as
  233. they do not need subsetting (ignore the fact that 'loca' is listed
  234. here): 'gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name',
  235. 'cvt ', 'fpgm', 'prep', 'VMDX', 'DSIG', 'CPAL', 'MVAR', 'cvar', 'STAT'.
  236. By default, tables that the tool does not know how to subset and are not
  237. specified here will be dropped from the font, unless --passthrough-tables
  238. option is passed.
  239. Example:
  240. --no-subset-tables+=FFTM
  241. * Keep 'FFTM' table in the font by preventing subsetting.
  242. --passthrough-tables
  243. Do not drop tables that the tool does not know how to subset.
  244. --no-passthrough-tables
  245. Tables that the tool does not know how to subset and are not specified
  246. in --no-subset-tables will be dropped from the font. [default]
  247. --hinting-tables[-]=<table>[,<table>...]
  248. Specify (=), add to (+=) or exclude from (-=) the list of font-wide
  249. hinting tables that will be dropped if --no-hinting is specified.
  250. Examples:
  251. --hinting-tables-=VDMX
  252. * Drop font-wide hinting tables except 'VDMX'.
  253. --hinting-tables=
  254. * Keep all font-wide hinting tables (but strip hints from glyphs).
  255. --legacy-kern
  256. Keep TrueType 'kern' table even when OpenType 'GPOS' is available.
  257. --no-legacy-kern
  258. Drop TrueType 'kern' table if OpenType 'GPOS' is available. [default]
  259. Font naming options
  260. ^^^^^^^^^^^^^^^^^^^
  261. These options control what is retained in the 'name' table. For numerical
  262. codes, see: http://www.microsoft.com/typography/otspec/name.htm
  263. --name-IDs[+|-]=<nameID>[,<nameID>...]
  264. Specify (=), add to (+=) or exclude from (-=) the set of 'name' table
  265. entry nameIDs that will be preserved. By default, only nameIDs between 0
  266. and 6 are preserved, the rest are dropped. Use '*' to keep all entries.
  267. Examples:
  268. --name-IDs+=7,8,9
  269. * Also keep Trademark, Manufacturer and Designer name entries.
  270. --name-IDs=
  271. * Drop all 'name' table entries.
  272. --name-IDs=*
  273. * keep all 'name' table entries
  274. --name-legacy
  275. Keep legacy (non-Unicode) 'name' table entries (0.x, 1.x etc.).
  276. XXX Note: This might be needed for some fonts that have no Unicode name
  277. entires for English. See: https://github.com/fonttools/fonttools/issues/146
  278. --no-name-legacy
  279. Drop legacy (non-Unicode) 'name' table entries [default]
  280. --name-languages[+|-]=<langID>[,<langID>]
  281. Specify (=), add to (+=) or exclude from (-=) the set of 'name' table
  282. langIDs that will be preserved. By default only records with langID
  283. 0x0409 (English) are preserved. Use '*' to keep all langIDs.
  284. --obfuscate-names
  285. Make the font unusable as a system font by replacing name IDs 1, 2, 3, 4,
  286. and 6 with dummy strings (it is still fully functional as webfont).
  287. Glyph naming and encoding options
  288. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  289. --glyph-names
  290. Keep PS glyph names in TT-flavored fonts. In general glyph names are
  291. not needed for correct use of the font. However, some PDF generators
  292. and PDF viewers might rely on glyph names to extract Unicode text
  293. from PDF documents.
  294. --no-glyph-names
  295. Drop PS glyph names in TT-flavored fonts, by using 'post' table
  296. version 3.0. [default]
  297. --legacy-cmap
  298. Keep the legacy 'cmap' subtables (0.x, 1.x, 4.x etc.).
  299. --no-legacy-cmap
  300. Drop the legacy 'cmap' subtables. [default]
  301. --symbol-cmap
  302. Keep the 3.0 symbol 'cmap'.
  303. --no-symbol-cmap
  304. Drop the 3.0 symbol 'cmap'. [default]
  305. Other font-specific options
  306. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  307. --recalc-bounds
  308. Recalculate font bounding boxes.
  309. --no-recalc-bounds
  310. Keep original font bounding boxes. This is faster and still safe
  311. for all practical purposes. [default]
  312. --recalc-timestamp
  313. Set font 'modified' timestamp to current time.
  314. --no-recalc-timestamp
  315. Do not modify font 'modified' timestamp. [default]
  316. --canonical-order
  317. Order tables as recommended in the OpenType standard. This is not
  318. required by the standard, nor by any known implementation.
  319. --no-canonical-order
  320. Keep original order of font tables. This is faster. [default]
  321. --prune-unicode-ranges
  322. Update the 'OS/2 ulUnicodeRange*' bits after subsetting. The Unicode
  323. ranges defined in the OpenType specification v1.7 are intersected with
  324. the Unicode codepoints specified in the font's Unicode 'cmap' subtables:
  325. when no overlap is found, the bit will be switched off. However, it will
  326. *not* be switched on if an intersection is found. [default]
  327. --no-prune-unicode-ranges
  328. Don't change the 'OS/2 ulUnicodeRange*' bits.
  329. --prune-codepage-ranges
  330. Update the 'OS/2 ulCodePageRange*' bits after subsetting. [default]
  331. --no-prune-codepage-ranges
  332. Don't change the 'OS/2 ulCodePageRange*' bits.
  333. --recalc-average-width
  334. Update the 'OS/2 xAvgCharWidth' field after subsetting.
  335. --no-recalc-average-width
  336. Don't change the 'OS/2 xAvgCharWidth' field. [default]
  337. --recalc-max-context
  338. Update the 'OS/2 usMaxContext' field after subsetting.
  339. --no-recalc-max-context
  340. Don't change the 'OS/2 usMaxContext' field. [default]
  341. --font-number=<number>
  342. Select font number for TrueType Collection (.ttc/.otc), starting from 0.
  343. --pretty-svg
  344. When subsetting SVG table, use lxml pretty_print=True option to indent
  345. the XML output (only recommended for debugging purposes).
  346. Application options
  347. ^^^^^^^^^^^^^^^^^^^
  348. --verbose
  349. Display verbose information of the subsetting process.
  350. --timing
  351. Display detailed timing information of the subsetting process.
  352. --xml
  353. Display the TTX XML representation of subsetted font.
  354. Example
  355. ^^^^^^^
  356. Produce a subset containing the characters ' !"#$%' without performing
  357. size-reducing optimizations::
  358. $ pyftsubset font.ttf --unicodes="U+0020-0025" \\
  359. --layout-features=* --glyph-names --symbol-cmap --legacy-cmap \\
  360. --notdef-glyph --notdef-outline --recommended-glyphs \\
  361. --name-IDs=* --name-legacy --name-languages=*
  362. """
  363. )
  364. log = logging.getLogger("fontTools.subset")
  365. def _log_glyphs(self, glyphs, font=None):
  366. self.info("Glyph names: %s", sorted(glyphs))
  367. if font:
  368. reverseGlyphMap = font.getReverseGlyphMap()
  369. self.info("Glyph IDs: %s", sorted(reverseGlyphMap[g] for g in glyphs))
  370. # bind "glyphs" function to 'log' object
  371. log.glyphs = MethodType(_log_glyphs, log)
  372. # I use a different timing channel so I can configure it separately from the
  373. # main module's logger
  374. timer = Timer(logger=logging.getLogger("fontTools.subset.timer"))
  375. def _dict_subset(d, glyphs):
  376. return {g: d[g] for g in glyphs}
  377. def _list_subset(l, indices):
  378. count = len(l)
  379. return [l[i] for i in indices if i < count]
  380. @_add_method(otTables.Coverage)
  381. def intersect(self, glyphs):
  382. """Returns ascending list of matching coverage values."""
  383. return [i for i, g in enumerate(self.glyphs) if g in glyphs]
  384. @_add_method(otTables.Coverage)
  385. def intersect_glyphs(self, glyphs):
  386. """Returns set of intersecting glyphs."""
  387. return set(g for g in self.glyphs if g in glyphs)
  388. @_add_method(otTables.Coverage)
  389. def subset(self, glyphs):
  390. """Returns ascending list of remaining coverage values."""
  391. indices = self.intersect(glyphs)
  392. self.glyphs = [g for g in self.glyphs if g in glyphs]
  393. return indices
  394. @_add_method(otTables.Coverage)
  395. def remap(self, coverage_map):
  396. """Remaps coverage."""
  397. self.glyphs = [self.glyphs[i] for i in coverage_map]
  398. @_add_method(otTables.ClassDef)
  399. def intersect(self, glyphs):
  400. """Returns ascending list of matching class values."""
  401. return _uniq_sort(
  402. ([0] if any(g not in self.classDefs for g in glyphs) else [])
  403. + [v for g, v in self.classDefs.items() if g in glyphs]
  404. )
  405. @_add_method(otTables.ClassDef)
  406. def intersect_class(self, glyphs, klass):
  407. """Returns set of glyphs matching class."""
  408. if klass == 0:
  409. return set(g for g in glyphs if g not in self.classDefs)
  410. return set(g for g, v in self.classDefs.items() if v == klass and g in glyphs)
  411. @_add_method(otTables.ClassDef)
  412. def subset(self, glyphs, remap=False, useClass0=True):
  413. """Returns ascending list of remaining classes."""
  414. self.classDefs = {g: v for g, v in self.classDefs.items() if g in glyphs}
  415. # Note: while class 0 has the special meaning of "not matched",
  416. # if no glyph will ever /not match/, we can optimize class 0 out too.
  417. # Only do this if allowed.
  418. indices = _uniq_sort(
  419. (
  420. [0]
  421. if ((not useClass0) or any(g not in self.classDefs for g in glyphs))
  422. else []
  423. )
  424. + list(self.classDefs.values())
  425. )
  426. if remap:
  427. self.remap(indices)
  428. return indices
  429. @_add_method(otTables.ClassDef)
  430. def remap(self, class_map):
  431. """Remaps classes."""
  432. self.classDefs = {g: class_map.index(v) for g, v in self.classDefs.items()}
  433. @_add_method(otTables.SingleSubst)
  434. def closure_glyphs(self, s, cur_glyphs):
  435. s.glyphs.update(v for g, v in self.mapping.items() if g in cur_glyphs)
  436. @_add_method(otTables.SingleSubst)
  437. def subset_glyphs(self, s):
  438. self.mapping = {
  439. g: v for g, v in self.mapping.items() if g in s.glyphs and v in s.glyphs
  440. }
  441. return bool(self.mapping)
  442. @_add_method(otTables.MultipleSubst)
  443. def closure_glyphs(self, s, cur_glyphs):
  444. for glyph, subst in self.mapping.items():
  445. if glyph in cur_glyphs:
  446. s.glyphs.update(subst)
  447. @_add_method(otTables.MultipleSubst)
  448. def subset_glyphs(self, s):
  449. self.mapping = {
  450. g: v
  451. for g, v in self.mapping.items()
  452. if g in s.glyphs and all(sub in s.glyphs for sub in v)
  453. }
  454. return bool(self.mapping)
  455. @_add_method(otTables.AlternateSubst)
  456. def closure_glyphs(self, s, cur_glyphs):
  457. s.glyphs.update(*(vlist for g, vlist in self.alternates.items() if g in cur_glyphs))
  458. @_add_method(otTables.AlternateSubst)
  459. def subset_glyphs(self, s):
  460. self.alternates = {
  461. g: [v for v in vlist if v in s.glyphs]
  462. for g, vlist in self.alternates.items()
  463. if g in s.glyphs and any(v in s.glyphs for v in vlist)
  464. }
  465. return bool(self.alternates)
  466. @_add_method(otTables.LigatureSubst)
  467. def closure_glyphs(self, s, cur_glyphs):
  468. s.glyphs.update(
  469. *(
  470. [seq.LigGlyph for seq in seqs if all(c in s.glyphs for c in seq.Component)]
  471. for g, seqs in self.ligatures.items()
  472. if g in cur_glyphs
  473. )
  474. )
  475. @_add_method(otTables.LigatureSubst)
  476. def subset_glyphs(self, s):
  477. self.ligatures = {g: v for g, v in self.ligatures.items() if g in s.glyphs}
  478. self.ligatures = {
  479. g: [
  480. seq
  481. for seq in seqs
  482. if seq.LigGlyph in s.glyphs and all(c in s.glyphs for c in seq.Component)
  483. ]
  484. for g, seqs in self.ligatures.items()
  485. }
  486. self.ligatures = {g: v for g, v in self.ligatures.items() if v}
  487. return bool(self.ligatures)
  488. @_add_method(otTables.ReverseChainSingleSubst)
  489. def closure_glyphs(self, s, cur_glyphs):
  490. if self.Format == 1:
  491. indices = self.Coverage.intersect(cur_glyphs)
  492. if not indices or not all(
  493. c.intersect(s.glyphs)
  494. for c in self.LookAheadCoverage + self.BacktrackCoverage
  495. ):
  496. return
  497. s.glyphs.update(self.Substitute[i] for i in indices)
  498. else:
  499. assert 0, "unknown format: %s" % self.Format
  500. @_add_method(otTables.ReverseChainSingleSubst)
  501. def subset_glyphs(self, s):
  502. if self.Format == 1:
  503. indices = self.Coverage.subset(s.glyphs)
  504. self.Substitute = _list_subset(self.Substitute, indices)
  505. # Now drop rules generating glyphs we don't want
  506. indices = [i for i, sub in enumerate(self.Substitute) if sub in s.glyphs]
  507. self.Substitute = _list_subset(self.Substitute, indices)
  508. self.Coverage.remap(indices)
  509. self.GlyphCount = len(self.Substitute)
  510. return bool(
  511. self.GlyphCount
  512. and all(
  513. c.subset(s.glyphs)
  514. for c in self.LookAheadCoverage + self.BacktrackCoverage
  515. )
  516. )
  517. else:
  518. assert 0, "unknown format: %s" % self.Format
  519. @_add_method(otTables.Device)
  520. def is_hinting(self):
  521. return self.DeltaFormat in (1, 2, 3)
  522. @_add_method(otTables.ValueRecord)
  523. def prune_hints(self):
  524. for name in ["XPlaDevice", "YPlaDevice", "XAdvDevice", "YAdvDevice"]:
  525. v = getattr(self, name, None)
  526. if v is not None and v.is_hinting():
  527. delattr(self, name)
  528. @_add_method(otTables.SinglePos)
  529. def subset_glyphs(self, s):
  530. if self.Format == 1:
  531. return len(self.Coverage.subset(s.glyphs))
  532. elif self.Format == 2:
  533. indices = self.Coverage.subset(s.glyphs)
  534. values = self.Value
  535. count = len(values)
  536. self.Value = [values[i] for i in indices if i < count]
  537. self.ValueCount = len(self.Value)
  538. return bool(self.ValueCount)
  539. else:
  540. assert 0, "unknown format: %s" % self.Format
  541. @_add_method(otTables.SinglePos)
  542. def prune_post_subset(self, font, options):
  543. if self.Value is None:
  544. assert self.ValueFormat == 0
  545. return True
  546. # Shrink ValueFormat
  547. if self.Format == 1:
  548. if not options.hinting:
  549. self.Value.prune_hints()
  550. self.ValueFormat = self.Value.getEffectiveFormat()
  551. elif self.Format == 2:
  552. if None in self.Value:
  553. assert self.ValueFormat == 0
  554. assert all(v is None for v in self.Value)
  555. else:
  556. if not options.hinting:
  557. for v in self.Value:
  558. v.prune_hints()
  559. self.ValueFormat = reduce(
  560. int.__or__, [v.getEffectiveFormat() for v in self.Value], 0
  561. )
  562. # Downgrade to Format 1 if all ValueRecords are the same
  563. if self.Format == 2 and all(v == self.Value[0] for v in self.Value):
  564. self.Format = 1
  565. self.Value = self.Value[0] if self.ValueFormat != 0 else None
  566. del self.ValueCount
  567. return True
  568. @_add_method(otTables.PairPos)
  569. def subset_glyphs(self, s):
  570. if self.Format == 1:
  571. indices = self.Coverage.subset(s.glyphs)
  572. pairs = self.PairSet
  573. count = len(pairs)
  574. self.PairSet = [pairs[i] for i in indices if i < count]
  575. for p in self.PairSet:
  576. p.PairValueRecord = [
  577. r for r in p.PairValueRecord if r.SecondGlyph in s.glyphs
  578. ]
  579. p.PairValueCount = len(p.PairValueRecord)
  580. # Remove empty pairsets
  581. indices = [i for i, p in enumerate(self.PairSet) if p.PairValueCount]
  582. self.Coverage.remap(indices)
  583. self.PairSet = _list_subset(self.PairSet, indices)
  584. self.PairSetCount = len(self.PairSet)
  585. return bool(self.PairSetCount)
  586. elif self.Format == 2:
  587. class1_map = [
  588. c
  589. for c in self.ClassDef1.subset(
  590. s.glyphs.intersection(self.Coverage.glyphs), remap=True
  591. )
  592. if c < self.Class1Count
  593. ]
  594. class2_map = [
  595. c
  596. for c in self.ClassDef2.subset(s.glyphs, remap=True, useClass0=False)
  597. if c < self.Class2Count
  598. ]
  599. self.Class1Record = [self.Class1Record[i] for i in class1_map]
  600. for c in self.Class1Record:
  601. c.Class2Record = [c.Class2Record[i] for i in class2_map]
  602. self.Class1Count = len(class1_map)
  603. self.Class2Count = len(class2_map)
  604. # If only Class2 0 left, no need to keep anything.
  605. return bool(
  606. self.Class1Count
  607. and (self.Class2Count > 1)
  608. and self.Coverage.subset(s.glyphs)
  609. )
  610. else:
  611. assert 0, "unknown format: %s" % self.Format
  612. @_add_method(otTables.PairPos)
  613. def prune_post_subset(self, font, options):
  614. if not options.hinting:
  615. attr1, attr2 = {
  616. 1: ("PairSet", "PairValueRecord"),
  617. 2: ("Class1Record", "Class2Record"),
  618. }[self.Format]
  619. self.ValueFormat1 = self.ValueFormat2 = 0
  620. for row in getattr(self, attr1):
  621. for r in getattr(row, attr2):
  622. if r.Value1:
  623. r.Value1.prune_hints()
  624. self.ValueFormat1 |= r.Value1.getEffectiveFormat()
  625. if r.Value2:
  626. r.Value2.prune_hints()
  627. self.ValueFormat2 |= r.Value2.getEffectiveFormat()
  628. return bool(self.ValueFormat1 | self.ValueFormat2)
  629. @_add_method(otTables.CursivePos)
  630. def subset_glyphs(self, s):
  631. if self.Format == 1:
  632. indices = self.Coverage.subset(s.glyphs)
  633. records = self.EntryExitRecord
  634. count = len(records)
  635. self.EntryExitRecord = [records[i] for i in indices if i < count]
  636. self.EntryExitCount = len(self.EntryExitRecord)
  637. return bool(self.EntryExitCount)
  638. else:
  639. assert 0, "unknown format: %s" % self.Format
  640. @_add_method(otTables.Anchor)
  641. def prune_hints(self):
  642. if self.Format == 2:
  643. self.Format = 1
  644. elif self.Format == 3:
  645. for name in ("XDeviceTable", "YDeviceTable"):
  646. v = getattr(self, name, None)
  647. if v is not None and v.is_hinting():
  648. setattr(self, name, None)
  649. if self.XDeviceTable is None and self.YDeviceTable is None:
  650. self.Format = 1
  651. @_add_method(otTables.CursivePos)
  652. def prune_post_subset(self, font, options):
  653. if not options.hinting:
  654. for rec in self.EntryExitRecord:
  655. if rec.EntryAnchor:
  656. rec.EntryAnchor.prune_hints()
  657. if rec.ExitAnchor:
  658. rec.ExitAnchor.prune_hints()
  659. return True
  660. @_add_method(otTables.MarkBasePos)
  661. def subset_glyphs(self, s):
  662. if self.Format == 1:
  663. mark_indices = self.MarkCoverage.subset(s.glyphs)
  664. self.MarkArray.MarkRecord = _list_subset(
  665. self.MarkArray.MarkRecord, mark_indices
  666. )
  667. self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
  668. base_indices = self.BaseCoverage.subset(s.glyphs)
  669. self.BaseArray.BaseRecord = _list_subset(
  670. self.BaseArray.BaseRecord, base_indices
  671. )
  672. self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
  673. # Prune empty classes
  674. class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
  675. self.ClassCount = len(class_indices)
  676. for m in self.MarkArray.MarkRecord:
  677. m.Class = class_indices.index(m.Class)
  678. for b in self.BaseArray.BaseRecord:
  679. b.BaseAnchor = _list_subset(b.BaseAnchor, class_indices)
  680. return bool(
  681. self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount
  682. )
  683. else:
  684. assert 0, "unknown format: %s" % self.Format
  685. @_add_method(otTables.MarkBasePos)
  686. def prune_post_subset(self, font, options):
  687. if not options.hinting:
  688. for m in self.MarkArray.MarkRecord:
  689. if m.MarkAnchor:
  690. m.MarkAnchor.prune_hints()
  691. for b in self.BaseArray.BaseRecord:
  692. for a in b.BaseAnchor:
  693. if a:
  694. a.prune_hints()
  695. return True
  696. @_add_method(otTables.MarkLigPos)
  697. def subset_glyphs(self, s):
  698. if self.Format == 1:
  699. mark_indices = self.MarkCoverage.subset(s.glyphs)
  700. self.MarkArray.MarkRecord = _list_subset(
  701. self.MarkArray.MarkRecord, mark_indices
  702. )
  703. self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
  704. ligature_indices = self.LigatureCoverage.subset(s.glyphs)
  705. self.LigatureArray.LigatureAttach = _list_subset(
  706. self.LigatureArray.LigatureAttach, ligature_indices
  707. )
  708. self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
  709. # Prune empty classes
  710. class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
  711. self.ClassCount = len(class_indices)
  712. for m in self.MarkArray.MarkRecord:
  713. m.Class = class_indices.index(m.Class)
  714. for l in self.LigatureArray.LigatureAttach:
  715. if l is None:
  716. continue
  717. for c in l.ComponentRecord:
  718. c.LigatureAnchor = _list_subset(c.LigatureAnchor, class_indices)
  719. return bool(
  720. self.ClassCount
  721. and self.MarkArray.MarkCount
  722. and self.LigatureArray.LigatureCount
  723. )
  724. else:
  725. assert 0, "unknown format: %s" % self.Format
  726. @_add_method(otTables.MarkLigPos)
  727. def prune_post_subset(self, font, options):
  728. if not options.hinting:
  729. for m in self.MarkArray.MarkRecord:
  730. if m.MarkAnchor:
  731. m.MarkAnchor.prune_hints()
  732. for l in self.LigatureArray.LigatureAttach:
  733. if l is None:
  734. continue
  735. for c in l.ComponentRecord:
  736. for a in c.LigatureAnchor:
  737. if a:
  738. a.prune_hints()
  739. return True
  740. @_add_method(otTables.MarkMarkPos)
  741. def subset_glyphs(self, s):
  742. if self.Format == 1:
  743. mark1_indices = self.Mark1Coverage.subset(s.glyphs)
  744. self.Mark1Array.MarkRecord = _list_subset(
  745. self.Mark1Array.MarkRecord, mark1_indices
  746. )
  747. self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
  748. mark2_indices = self.Mark2Coverage.subset(s.glyphs)
  749. self.Mark2Array.Mark2Record = _list_subset(
  750. self.Mark2Array.Mark2Record, mark2_indices
  751. )
  752. self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
  753. # Prune empty classes
  754. class_indices = _uniq_sort(v.Class for v in self.Mark1Array.MarkRecord)
  755. self.ClassCount = len(class_indices)
  756. for m in self.Mark1Array.MarkRecord:
  757. m.Class = class_indices.index(m.Class)
  758. for b in self.Mark2Array.Mark2Record:
  759. b.Mark2Anchor = _list_subset(b.Mark2Anchor, class_indices)
  760. return bool(
  761. self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount
  762. )
  763. else:
  764. assert 0, "unknown format: %s" % self.Format
  765. @_add_method(otTables.MarkMarkPos)
  766. def prune_post_subset(self, font, options):
  767. if not options.hinting:
  768. for m in self.Mark1Array.MarkRecord:
  769. if m.MarkAnchor:
  770. m.MarkAnchor.prune_hints()
  771. for b in self.Mark2Array.Mark2Record:
  772. for m in b.Mark2Anchor:
  773. if m:
  774. m.prune_hints()
  775. return True
  776. @_add_method(
  777. otTables.SingleSubst,
  778. otTables.MultipleSubst,
  779. otTables.AlternateSubst,
  780. otTables.LigatureSubst,
  781. otTables.ReverseChainSingleSubst,
  782. otTables.SinglePos,
  783. otTables.PairPos,
  784. otTables.CursivePos,
  785. otTables.MarkBasePos,
  786. otTables.MarkLigPos,
  787. otTables.MarkMarkPos,
  788. )
  789. def subset_lookups(self, lookup_indices):
  790. pass
  791. @_add_method(
  792. otTables.SingleSubst,
  793. otTables.MultipleSubst,
  794. otTables.AlternateSubst,
  795. otTables.LigatureSubst,
  796. otTables.ReverseChainSingleSubst,
  797. otTables.SinglePos,
  798. otTables.PairPos,
  799. otTables.CursivePos,
  800. otTables.MarkBasePos,
  801. otTables.MarkLigPos,
  802. otTables.MarkMarkPos,
  803. )
  804. def collect_lookups(self):
  805. return []
  806. @_add_method(
  807. otTables.SingleSubst,
  808. otTables.MultipleSubst,
  809. otTables.AlternateSubst,
  810. otTables.LigatureSubst,
  811. otTables.ReverseChainSingleSubst,
  812. otTables.ContextSubst,
  813. otTables.ChainContextSubst,
  814. otTables.ContextPos,
  815. otTables.ChainContextPos,
  816. )
  817. def prune_post_subset(self, font, options):
  818. return True
  819. @_add_method(
  820. otTables.SingleSubst, otTables.AlternateSubst, otTables.ReverseChainSingleSubst
  821. )
  822. def may_have_non_1to1(self):
  823. return False
  824. @_add_method(
  825. otTables.MultipleSubst,
  826. otTables.LigatureSubst,
  827. otTables.ContextSubst,
  828. otTables.ChainContextSubst,
  829. )
  830. def may_have_non_1to1(self):
  831. return True
  832. @_add_method(
  833. otTables.ContextSubst,
  834. otTables.ChainContextSubst,
  835. otTables.ContextPos,
  836. otTables.ChainContextPos,
  837. )
  838. def __subset_classify_context(self):
  839. class ContextHelper(object):
  840. def __init__(self, klass, Format):
  841. if klass.__name__.endswith("Subst"):
  842. Typ = "Sub"
  843. Type = "Subst"
  844. else:
  845. Typ = "Pos"
  846. Type = "Pos"
  847. if klass.__name__.startswith("Chain"):
  848. Chain = "Chain"
  849. InputIdx = 1
  850. DataLen = 3
  851. else:
  852. Chain = ""
  853. InputIdx = 0
  854. DataLen = 1
  855. ChainTyp = Chain + Typ
  856. self.Typ = Typ
  857. self.Type = Type
  858. self.Chain = Chain
  859. self.ChainTyp = ChainTyp
  860. self.InputIdx = InputIdx
  861. self.DataLen = DataLen
  862. self.LookupRecord = Type + "LookupRecord"
  863. if Format == 1:
  864. Coverage = lambda r: r.Coverage
  865. ChainCoverage = lambda r: r.Coverage
  866. ContextData = lambda r: (None,)
  867. ChainContextData = lambda r: (None, None, None)
  868. SetContextData = None
  869. SetChainContextData = None
  870. RuleData = lambda r: (r.Input,)
  871. ChainRuleData = lambda r: (r.Backtrack, r.Input, r.LookAhead)
  872. def SetRuleData(r, d):
  873. (r.Input,) = d
  874. (r.GlyphCount,) = (len(x) + 1 for x in d)
  875. def ChainSetRuleData(r, d):
  876. (r.Backtrack, r.Input, r.LookAhead) = d
  877. (
  878. r.BacktrackGlyphCount,
  879. r.InputGlyphCount,
  880. r.LookAheadGlyphCount,
  881. ) = (len(d[0]), len(d[1]) + 1, len(d[2]))
  882. elif Format == 2:
  883. Coverage = lambda r: r.Coverage
  884. ChainCoverage = lambda r: r.Coverage
  885. ContextData = lambda r: (r.ClassDef,)
  886. ChainContextData = lambda r: (
  887. r.BacktrackClassDef,
  888. r.InputClassDef,
  889. r.LookAheadClassDef,
  890. )
  891. def SetContextData(r, d):
  892. (r.ClassDef,) = d
  893. def SetChainContextData(r, d):
  894. (r.BacktrackClassDef, r.InputClassDef, r.LookAheadClassDef) = d
  895. RuleData = lambda r: (r.Class,)
  896. ChainRuleData = lambda r: (r.Backtrack, r.Input, r.LookAhead)
  897. def SetRuleData(r, d):
  898. (r.Class,) = d
  899. (r.GlyphCount,) = (len(x) + 1 for x in d)
  900. def ChainSetRuleData(r, d):
  901. (r.Backtrack, r.Input, r.LookAhead) = d
  902. (
  903. r.BacktrackGlyphCount,
  904. r.InputGlyphCount,
  905. r.LookAheadGlyphCount,
  906. ) = (len(d[0]), len(d[1]) + 1, len(d[2]))
  907. elif Format == 3:
  908. Coverage = lambda r: r.Coverage[0]
  909. ChainCoverage = lambda r: r.InputCoverage[0]
  910. ContextData = None
  911. ChainContextData = None
  912. SetContextData = None
  913. SetChainContextData = None
  914. RuleData = lambda r: r.Coverage
  915. ChainRuleData = lambda r: (
  916. r.BacktrackCoverage + r.InputCoverage + r.LookAheadCoverage
  917. )
  918. def SetRuleData(r, d):
  919. (r.Coverage,) = d
  920. (r.GlyphCount,) = (len(x) for x in d)
  921. def ChainSetRuleData(r, d):
  922. (r.BacktrackCoverage, r.InputCoverage, r.LookAheadCoverage) = d
  923. (
  924. r.BacktrackGlyphCount,
  925. r.InputGlyphCount,
  926. r.LookAheadGlyphCount,
  927. ) = (len(x) for x in d)
  928. else:
  929. assert 0, "unknown format: %s" % Format
  930. if Chain:
  931. self.Coverage = ChainCoverage
  932. self.ContextData = ChainContextData
  933. self.SetContextData = SetChainContextData
  934. self.RuleData = ChainRuleData
  935. self.SetRuleData = ChainSetRuleData
  936. else:
  937. self.Coverage = Coverage
  938. self.ContextData = ContextData
  939. self.SetContextData = SetContextData
  940. self.RuleData = RuleData
  941. self.SetRuleData = SetRuleData
  942. if Format == 1:
  943. self.Rule = ChainTyp + "Rule"
  944. self.RuleCount = ChainTyp + "RuleCount"
  945. self.RuleSet = ChainTyp + "RuleSet"
  946. self.RuleSetCount = ChainTyp + "RuleSetCount"
  947. self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else []
  948. elif Format == 2:
  949. self.Rule = ChainTyp + "ClassRule"
  950. self.RuleCount = ChainTyp + "ClassRuleCount"
  951. self.RuleSet = ChainTyp + "ClassSet"
  952. self.RuleSetCount = ChainTyp + "ClassSetCount"
  953. self.Intersect = lambda glyphs, c, r: (
  954. c.intersect_class(glyphs, r)
  955. if c
  956. else (set(glyphs) if r == 0 else set())
  957. )
  958. self.ClassDef = "InputClassDef" if Chain else "ClassDef"
  959. self.ClassDefIndex = 1 if Chain else 0
  960. self.Input = "Input" if Chain else "Class"
  961. elif Format == 3:
  962. self.Input = "InputCoverage" if Chain else "Coverage"
  963. if self.Format not in [1, 2, 3]:
  964. return None # Don't shoot the messenger; let it go
  965. if not hasattr(self.__class__, "_subset__ContextHelpers"):
  966. self.__class__._subset__ContextHelpers = {}
  967. if self.Format not in self.__class__._subset__ContextHelpers:
  968. helper = ContextHelper(self.__class__, self.Format)
  969. self.__class__._subset__ContextHelpers[self.Format] = helper
  970. return self.__class__._subset__ContextHelpers[self.Format]
  971. @_add_method(otTables.ContextSubst, otTables.ChainContextSubst)
  972. def closure_glyphs(self, s, cur_glyphs):
  973. c = self.__subset_classify_context()
  974. indices = c.Coverage(self).intersect(cur_glyphs)
  975. if not indices:
  976. return []
  977. cur_glyphs = c.Coverage(self).intersect_glyphs(cur_glyphs)
  978. if self.Format == 1:
  979. ContextData = c.ContextData(self)
  980. rss = getattr(self, c.RuleSet)
  981. rssCount = getattr(self, c.RuleSetCount)
  982. for i in indices:
  983. if i >= rssCount or not rss[i]:
  984. continue
  985. for r in getattr(rss[i], c.Rule):
  986. if not r:
  987. continue
  988. if not all(
  989. all(c.Intersect(s.glyphs, cd, k) for k in klist)
  990. for cd, klist in zip(ContextData, c.RuleData(r))
  991. ):
  992. continue
  993. chaos = set()
  994. for ll in getattr(r, c.LookupRecord):
  995. if not ll:
  996. continue
  997. seqi = ll.SequenceIndex
  998. if seqi in chaos:
  999. # TODO Can we improve this?
  1000. pos_glyphs = None
  1001. else:
  1002. if seqi == 0:
  1003. pos_glyphs = frozenset([c.Coverage(self).glyphs[i]])
  1004. else:
  1005. pos_glyphs = frozenset([r.Input[seqi - 1]])
  1006. lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
  1007. chaos.add(seqi)
  1008. if lookup.may_have_non_1to1():
  1009. chaos.update(range(seqi, len(r.Input) + 2))
  1010. lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
  1011. elif self.Format == 2:
  1012. ClassDef = getattr(self, c.ClassDef)
  1013. indices = ClassDef.intersect(cur_glyphs)
  1014. ContextData = c.ContextData(self)
  1015. rss = getattr(self, c.RuleSet)
  1016. rssCount = getattr(self, c.RuleSetCount)
  1017. for i in indices:
  1018. if i >= rssCount or not rss[i]:
  1019. continue
  1020. for r in getattr(rss[i], c.Rule):
  1021. if not r:
  1022. continue
  1023. if not all(
  1024. all(c.Intersect(s.glyphs, cd, k) for k in klist)
  1025. for cd, klist in zip(ContextData, c.RuleData(r))
  1026. ):
  1027. continue
  1028. chaos = set()
  1029. for ll in getattr(r, c.LookupRecord):
  1030. if not ll:
  1031. continue
  1032. seqi = ll.SequenceIndex
  1033. if seqi in chaos:
  1034. # TODO Can we improve this?
  1035. pos_glyphs = None
  1036. else:
  1037. if seqi == 0:
  1038. pos_glyphs = frozenset(
  1039. ClassDef.intersect_class(cur_glyphs, i)
  1040. )
  1041. else:
  1042. pos_glyphs = frozenset(
  1043. ClassDef.intersect_class(
  1044. s.glyphs, getattr(r, c.Input)[seqi - 1]
  1045. )
  1046. )
  1047. lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
  1048. chaos.add(seqi)
  1049. if lookup.may_have_non_1to1():
  1050. chaos.update(range(seqi, len(getattr(r, c.Input)) + 2))
  1051. lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
  1052. elif self.Format == 3:
  1053. if not all(x is not None and x.intersect(s.glyphs) for x in c.RuleData(self)):
  1054. return []
  1055. r = self
  1056. input_coverages = getattr(r, c.Input)
  1057. chaos = set()
  1058. for ll in getattr(r, c.LookupRecord):
  1059. if not ll:
  1060. continue
  1061. seqi = ll.SequenceIndex
  1062. if seqi in chaos:
  1063. # TODO Can we improve this?
  1064. pos_glyphs = None
  1065. else:
  1066. if seqi == 0:
  1067. pos_glyphs = frozenset(cur_glyphs)
  1068. else:
  1069. pos_glyphs = frozenset(
  1070. input_coverages[seqi].intersect_glyphs(s.glyphs)
  1071. )
  1072. lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
  1073. chaos.add(seqi)
  1074. if lookup.may_have_non_1to1():
  1075. chaos.update(range(seqi, len(input_coverages) + 1))
  1076. lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
  1077. else:
  1078. assert 0, "unknown format: %s" % self.Format
  1079. @_add_method(
  1080. otTables.ContextSubst,
  1081. otTables.ContextPos,
  1082. otTables.ChainContextSubst,
  1083. otTables.ChainContextPos,
  1084. )
  1085. def subset_glyphs(self, s):
  1086. c = self.__subset_classify_context()
  1087. if self.Format == 1:
  1088. indices = self.Coverage.subset(s.glyphs)
  1089. rss = getattr(self, c.RuleSet)
  1090. rssCount = getattr(self, c.RuleSetCount)
  1091. rss = [rss[i] for i in indices if i < rssCount]
  1092. for rs in rss:
  1093. if not rs:
  1094. continue
  1095. ss = getattr(rs, c.Rule)
  1096. ss = [
  1097. r
  1098. for r in ss
  1099. if r
  1100. and all(all(g in s.glyphs for g in glist) for glist in c.RuleData(r))
  1101. ]
  1102. setattr(rs, c.Rule, ss)
  1103. setattr(rs, c.RuleCount, len(ss))
  1104. # Prune empty rulesets
  1105. indices = [i for i, rs in enumerate(rss) if rs and getattr(rs, c.Rule)]
  1106. self.Coverage.remap(indices)
  1107. rss = _list_subset(rss, indices)
  1108. setattr(self, c.RuleSet, rss)
  1109. setattr(self, c.RuleSetCount, len(rss))
  1110. return bool(rss)
  1111. elif self.Format == 2:
  1112. if not self.Coverage.subset(s.glyphs):
  1113. return False
  1114. ContextData = c.ContextData(self)
  1115. klass_maps = [
  1116. x.subset(s.glyphs, remap=True) if x else None for x in ContextData
  1117. ]
  1118. # Keep rulesets for class numbers that survived.
  1119. indices = klass_maps[c.ClassDefIndex]
  1120. rss = getattr(self, c.RuleSet)
  1121. rssCount = getattr(self, c.RuleSetCount)
  1122. rss = [rss[i] for i in indices if i < rssCount]
  1123. del rssCount
  1124. # Delete, but not renumber, unreachable rulesets.
  1125. indices = getattr(self, c.ClassDef).intersect(self.Coverage.glyphs)
  1126. rss = [rss if i in indices else None for i, rss in enumerate(rss)]
  1127. for rs in rss:
  1128. if not rs:
  1129. continue
  1130. ss = getattr(rs, c.Rule)
  1131. ss = [
  1132. r
  1133. for r in ss
  1134. if r
  1135. and all(
  1136. all(k in klass_map for k in klist)
  1137. for klass_map, klist in zip(klass_maps, c.RuleData(r))
  1138. )
  1139. ]
  1140. setattr(rs, c.Rule, ss)
  1141. setattr(rs, c.RuleCount, len(ss))
  1142. # Remap rule classes
  1143. for r in ss:
  1144. c.SetRuleData(
  1145. r,
  1146. [
  1147. [klass_map.index(k) for k in klist]
  1148. for klass_map, klist in zip(klass_maps, c.RuleData(r))
  1149. ],
  1150. )
  1151. # Prune empty rulesets
  1152. rss = [rs if rs and getattr(rs, c.Rule) else None for rs in rss]
  1153. while rss and rss[-1] is None:
  1154. del rss[-1]
  1155. setattr(self, c.RuleSet, rss)
  1156. setattr(self, c.RuleSetCount, len(rss))
  1157. # TODO: We can do a second round of remapping class values based
  1158. # on classes that are actually used in at least one rule. Right
  1159. # now we subset classes to c.glyphs only. Or better, rewrite
  1160. # the above to do that.
  1161. return bool(rss)
  1162. elif self.Format == 3:
  1163. return all(x is not None and x.subset(s.glyphs) for x in c.RuleData(self))
  1164. else:
  1165. assert 0, "unknown format: %s" % self.Format
  1166. @_add_method(
  1167. otTables.ContextSubst,
  1168. otTables.ChainContextSubst,
  1169. otTables.ContextPos,
  1170. otTables.ChainContextPos,
  1171. )
  1172. def subset_lookups(self, lookup_indices):
  1173. c = self.__subset_classify_context()
  1174. if self.Format in [1, 2]:
  1175. for rs in getattr(self, c.RuleSet):
  1176. if not rs:
  1177. continue
  1178. for r in getattr(rs, c.Rule):
  1179. if not r:
  1180. continue
  1181. setattr(
  1182. r,
  1183. c.LookupRecord,
  1184. [
  1185. ll
  1186. for ll in getattr(r, c.LookupRecord)
  1187. if ll and ll.LookupListIndex in lookup_indices
  1188. ],
  1189. )
  1190. for ll in getattr(r, c.LookupRecord):
  1191. if not ll:
  1192. continue
  1193. ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
  1194. elif self.Format == 3:
  1195. setattr(
  1196. self,
  1197. c.LookupRecord,
  1198. [
  1199. ll
  1200. for ll in getattr(self, c.LookupRecord)
  1201. if ll and ll.LookupListIndex in lookup_indices
  1202. ],
  1203. )
  1204. for ll in getattr(self, c.LookupRecord):
  1205. if not ll:
  1206. continue
  1207. ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
  1208. else:
  1209. assert 0, "unknown format: %s" % self.Format
  1210. @_add_method(
  1211. otTables.ContextSubst,
  1212. otTables.ChainContextSubst,
  1213. otTables.ContextPos,
  1214. otTables.ChainContextPos,
  1215. )
  1216. def collect_lookups(self):
  1217. c = self.__subset_classify_context()
  1218. if self.Format in [1, 2]:
  1219. return [
  1220. ll.LookupListIndex
  1221. for rs in getattr(self, c.RuleSet)
  1222. if rs
  1223. for r in getattr(rs, c.Rule)
  1224. if r
  1225. for ll in getattr(r, c.LookupRecord)
  1226. if ll
  1227. ]
  1228. elif self.Format == 3:
  1229. return [ll.LookupListIndex for ll in getattr(self, c.LookupRecord) if ll]
  1230. else:
  1231. assert 0, "unknown format: %s" % self.Format
  1232. @_add_method(otTables.ExtensionSubst)
  1233. def closure_glyphs(self, s, cur_glyphs):
  1234. if self.Format == 1:
  1235. self.ExtSubTable.closure_glyphs(s, cur_glyphs)
  1236. else:
  1237. assert 0, "unknown format: %s" % self.Format
  1238. @_add_method(otTables.ExtensionSubst)
  1239. def may_have_non_1to1(self):
  1240. if self.Format == 1:
  1241. return self.ExtSubTable.may_have_non_1to1()
  1242. else:
  1243. assert 0, "unknown format: %s" % self.Format
  1244. @_add_method(otTables.ExtensionSubst, otTables.ExtensionPos)
  1245. def subset_glyphs(self, s):
  1246. if self.Format == 1:
  1247. return self.ExtSubTable.subset_glyphs(s)
  1248. else:
  1249. assert 0, "unknown format: %s" % self.Format
  1250. @_add_method(otTables.ExtensionSubst, otTables.ExtensionPos)
  1251. def prune_post_subset(self, font, options):
  1252. if self.Format == 1:
  1253. return self.ExtSubTable.prune_post_subset(font, options)
  1254. else:
  1255. assert 0, "unknown format: %s" % self.Format
  1256. @_add_method(otTables.ExtensionSubst, otTables.ExtensionPos)
  1257. def subset_lookups(self, lookup_indices):
  1258. if self.Format == 1:
  1259. return self.ExtSubTable.subset_lookups(lookup_indices)
  1260. else:
  1261. assert 0, "unknown format: %s" % self.Format
  1262. @_add_method(otTables.ExtensionSubst, otTables.ExtensionPos)
  1263. def collect_lookups(self):
  1264. if self.Format == 1:
  1265. return self.ExtSubTable.collect_lookups()
  1266. else:
  1267. assert 0, "unknown format: %s" % self.Format
  1268. @_add_method(otTables.Lookup)
  1269. def closure_glyphs(self, s, cur_glyphs=None):
  1270. if cur_glyphs is None:
  1271. cur_glyphs = frozenset(s.glyphs)
  1272. # Memoize
  1273. key = id(self)
  1274. doneLookups = s._doneLookups
  1275. count, covered = doneLookups.get(key, (0, None))
  1276. if count != len(s.glyphs):
  1277. count, covered = doneLookups[key] = (len(s.glyphs), set())
  1278. if cur_glyphs.issubset(covered):
  1279. return
  1280. covered.update(cur_glyphs)
  1281. for st in self.SubTable:
  1282. if not st:
  1283. continue
  1284. st.closure_glyphs(s, cur_glyphs)
  1285. @_add_method(otTables.Lookup)
  1286. def subset_glyphs(self, s):
  1287. self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs(s)]
  1288. self.SubTableCount = len(self.SubTable)
  1289. if hasattr(self, "MarkFilteringSet") and self.MarkFilteringSet is not None:
  1290. if self.MarkFilteringSet not in s.used_mark_sets:
  1291. self.MarkFilteringSet = None
  1292. self.LookupFlag &= ~0x10
  1293. else:
  1294. self.MarkFilteringSet = s.used_mark_sets.index(self.MarkFilteringSet)
  1295. return bool(self.SubTableCount)
  1296. @_add_method(otTables.Lookup)
  1297. def prune_post_subset(self, font, options):
  1298. ret = False
  1299. for st in self.SubTable:
  1300. if not st:
  1301. continue
  1302. if st.prune_post_subset(font, options):
  1303. ret = True
  1304. return ret
  1305. @_add_method(otTables.Lookup)
  1306. def subset_lookups(self, lookup_indices):
  1307. for s in self.SubTable:
  1308. s.subset_lookups(lookup_indices)
  1309. @_add_method(otTables.Lookup)
  1310. def collect_lookups(self):
  1311. return sum((st.collect_lookups() for st in self.SubTable if st), [])
  1312. @_add_method(otTables.Lookup)
  1313. def may_have_non_1to1(self):
  1314. return any(st.may_have_non_1to1() for st in self.SubTable if st)
  1315. @_add_method(otTables.LookupList)
  1316. def subset_glyphs(self, s):
  1317. """Returns the indices of nonempty lookups."""
  1318. return [i for i, l in enumerate(self.Lookup) if l and l.subset_glyphs(s)]
  1319. @_add_method(otTables.LookupList)
  1320. def prune_post_subset(self, font, options):
  1321. ret = False
  1322. for l in self.Lookup:
  1323. if not l:
  1324. continue
  1325. if l.prune_post_subset(font, options):
  1326. ret = True
  1327. return ret
  1328. @_add_method(otTables.LookupList)
  1329. def subset_lookups(self, lookup_indices):
  1330. self.ensureDecompiled()
  1331. self.Lookup = [self.Lookup[i] for i in lookup_indices if i < self.LookupCount]
  1332. self.LookupCount = len(self.Lookup)
  1333. for l in self.Lookup:
  1334. l.subset_lookups(lookup_indices)
  1335. @_add_method(otTables.LookupList)
  1336. def neuter_lookups(self, lookup_indices):
  1337. """Sets lookups not in lookup_indices to None."""
  1338. self.ensureDecompiled()
  1339. self.Lookup = [
  1340. l if i in lookup_indices else None for i, l in enumerate(self.Lookup)
  1341. ]
  1342. @_add_method(otTables.LookupList)
  1343. def closure_lookups(self, lookup_indices):
  1344. """Returns sorted index of all lookups reachable from lookup_indices."""
  1345. lookup_indices = _uniq_sort(lookup_indices)
  1346. recurse = lookup_indices
  1347. while True:
  1348. recurse_lookups = sum(
  1349. (self.Lookup[i].collect_lookups() for i in recurse if i < self.LookupCount),
  1350. [],
  1351. )
  1352. recurse_lookups = [
  1353. l
  1354. for l in recurse_lookups
  1355. if l not in lookup_indices and l < self.LookupCount
  1356. ]
  1357. if not recurse_lookups:
  1358. return _uniq_sort(lookup_indices)
  1359. recurse_lookups = _uniq_sort(recurse_lookups)
  1360. lookup_indices.extend(recurse_lookups)
  1361. recurse = recurse_lookups
  1362. @_add_method(otTables.Feature)
  1363. def subset_lookups(self, lookup_indices):
  1364. """ "Returns True if feature is non-empty afterwards."""
  1365. self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
  1366. # Now map them.
  1367. self.LookupListIndex = [lookup_indices.index(l) for l in self.LookupListIndex]
  1368. self.LookupCount = len(self.LookupListIndex)
  1369. # keep 'size' feature even if it contains no lookups; but drop any other
  1370. # empty feature (e.g. FeatureParams for stylistic set names)
  1371. # https://github.com/fonttools/fonttools/issues/2324
  1372. return self.LookupCount or isinstance(
  1373. self.FeatureParams, otTables.FeatureParamsSize
  1374. )
  1375. @_add_method(otTables.FeatureList)
  1376. def subset_lookups(self, lookup_indices):
  1377. """Returns the indices of nonempty features."""
  1378. # Note: Never ever drop feature 'pref', even if it's empty.
  1379. # HarfBuzz chooses shaper for Khmer based on presence of this
  1380. # feature. See thread at:
  1381. # http://lists.freedesktop.org/archives/harfbuzz/2012-November/002660.html
  1382. return [
  1383. i
  1384. for i, f in enumerate(self.FeatureRecord)
  1385. if (f.Feature.subset_lookups(lookup_indices) or f.FeatureTag == "pref")
  1386. ]
  1387. @_add_method(otTables.FeatureList)
  1388. def collect_lookups(self, feature_indices):
  1389. return sum(
  1390. (
  1391. self.FeatureRecord[i].Feature.LookupListIndex
  1392. for i in feature_indices
  1393. if i < self.FeatureCount
  1394. ),
  1395. [],
  1396. )
  1397. @_add_method(otTables.FeatureList)
  1398. def subset_features(self, feature_indices):
  1399. self.ensureDecompiled()
  1400. self.FeatureRecord = _list_subset(self.FeatureRecord, feature_indices)
  1401. self.FeatureCount = len(self.FeatureRecord)
  1402. return bool(self.FeatureCount)
  1403. @_add_method(otTables.FeatureTableSubstitution)
  1404. def subset_lookups(self, lookup_indices):
  1405. """Returns the indices of nonempty features."""
  1406. return [
  1407. r.FeatureIndex
  1408. for r in self.SubstitutionRecord
  1409. if r.Feature.subset_lookups(lookup_indices)
  1410. ]
  1411. @_add_method(otTables.FeatureVariations)
  1412. def subset_lookups(self, lookup_indices):
  1413. """Returns the indices of nonempty features."""
  1414. return sum(
  1415. (
  1416. f.FeatureTableSubstitution.subset_lookups(lookup_indices)
  1417. for f in self.FeatureVariationRecord
  1418. ),
  1419. [],
  1420. )
  1421. @_add_method(otTables.FeatureVariations)
  1422. def collect_lookups(self, feature_indices):
  1423. return sum(
  1424. (
  1425. r.Feature.LookupListIndex
  1426. for vr in self.FeatureVariationRecord
  1427. for r in vr.FeatureTableSubstitution.SubstitutionRecord
  1428. if r.FeatureIndex in feature_indices
  1429. ),
  1430. [],
  1431. )
  1432. @_add_method(otTables.FeatureTableSubstitution)
  1433. def subset_features(self, feature_indices):
  1434. self.ensureDecompiled()
  1435. self.SubstitutionRecord = [
  1436. r for r in self.SubstitutionRecord if r.FeatureIndex in feature_indices
  1437. ]
  1438. # remap feature indices
  1439. for r in self.SubstitutionRecord:
  1440. r.FeatureIndex = feature_indices.index(r.FeatureIndex)
  1441. self.SubstitutionCount = len(self.SubstitutionRecord)
  1442. return bool(self.SubstitutionCount)
  1443. @_add_method(otTables.FeatureVariations)
  1444. def subset_features(self, feature_indices):
  1445. self.ensureDecompiled()
  1446. for r in self.FeatureVariationRecord:
  1447. r.FeatureTableSubstitution.subset_features(feature_indices)
  1448. # Prune empty records at the end only
  1449. # https://github.com/fonttools/fonttools/issues/1881
  1450. while (
  1451. self.FeatureVariationRecord
  1452. and not self.FeatureVariationRecord[
  1453. -1
  1454. ].FeatureTableSubstitution.SubstitutionCount
  1455. ):
  1456. self.FeatureVariationRecord.pop()
  1457. self.FeatureVariationCount = len(self.FeatureVariationRecord)
  1458. return bool(self.FeatureVariationCount)
  1459. @_add_method(otTables.DefaultLangSys, otTables.LangSys)
  1460. def subset_features(self, feature_indices):
  1461. if self.ReqFeatureIndex in feature_indices:
  1462. self.ReqFeatureIndex = feature_indices.index(self.ReqFeatureIndex)
  1463. else:
  1464. self.ReqFeatureIndex = 65535
  1465. self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
  1466. # Now map them.
  1467. self.FeatureIndex = [
  1468. feature_indices.index(f) for f in self.FeatureIndex if f in feature_indices
  1469. ]
  1470. self.FeatureCount = len(self.FeatureIndex)
  1471. return bool(self.FeatureCount or self.ReqFeatureIndex != 65535)
  1472. @_add_method(otTables.DefaultLangSys, otTables.LangSys)
  1473. def collect_features(self):
  1474. feature_indices = self.FeatureIndex[:]
  1475. if self.ReqFeatureIndex != 65535:
  1476. feature_indices.append(self.ReqFeatureIndex)
  1477. return _uniq_sort(feature_indices)
  1478. @_add_method(otTables.Script)
  1479. def subset_features(self, feature_indices, keepEmptyDefaultLangSys=False):
  1480. if (
  1481. self.DefaultLangSys
  1482. and not self.DefaultLangSys.subset_features(feature_indices)
  1483. and not keepEmptyDefaultLangSys
  1484. ):
  1485. self.DefaultLangSys = None
  1486. self.LangSysRecord = [
  1487. l for l in self.LangSysRecord if l.LangSys.subset_features(feature_indices)
  1488. ]
  1489. self.LangSysCount = len(self.LangSysRecord)
  1490. return bool(self.LangSysCount or self.DefaultLangSys)
  1491. @_add_method(otTables.Script)
  1492. def collect_features(self):
  1493. feature_indices = [l.LangSys.collect_features() for l in self.LangSysRecord]
  1494. if self.DefaultLangSys:
  1495. feature_indices.append(self.DefaultLangSys.collect_features())
  1496. return _uniq_sort(sum(feature_indices, []))
  1497. @_add_method(otTables.ScriptList)
  1498. def subset_features(self, feature_indices, retain_empty):
  1499. # https://bugzilla.mozilla.org/show_bug.cgi?id=1331737#c32
  1500. self.ScriptRecord = [
  1501. s
  1502. for s in self.ScriptRecord
  1503. if s.Script.subset_features(feature_indices, s.ScriptTag == "DFLT")
  1504. or retain_empty
  1505. ]
  1506. self.ScriptCount = len(self.ScriptRecord)
  1507. return bool(self.ScriptCount)
  1508. @_add_method(otTables.ScriptList)
  1509. def collect_features(self):
  1510. return _uniq_sort(sum((s.Script.collect_features() for s in self.ScriptRecord), []))
  1511. # CBLC will inherit it
  1512. @_add_method(ttLib.getTableClass("EBLC"))
  1513. def subset_glyphs(self, s):
  1514. for strike in self.strikes:
  1515. for indexSubTable in strike.indexSubTables:
  1516. indexSubTable.names = [n for n in indexSubTable.names if n in s.glyphs]
  1517. strike.indexSubTables = [i for i in strike.indexSubTables if i.names]
  1518. self.strikes = [s for s in self.strikes if s.indexSubTables]
  1519. return True
  1520. # CBDT will inherit it
  1521. @_add_method(ttLib.getTableClass("EBDT"))
  1522. def subset_glyphs(self, s):
  1523. strikeData = [
  1524. {g: strike[g] for g in s.glyphs if g in strike} for strike in self.strikeData
  1525. ]
  1526. # Prune empty strikes
  1527. # https://github.com/fonttools/fonttools/issues/1633
  1528. self.strikeData = [strike for strike in strikeData if strike]
  1529. return True
  1530. @_add_method(ttLib.getTableClass("sbix"))
  1531. def subset_glyphs(self, s):
  1532. for strike in self.strikes.values():
  1533. strike.glyphs = {g: strike.glyphs[g] for g in s.glyphs if g in strike.glyphs}
  1534. return True
  1535. @_add_method(ttLib.getTableClass("GSUB"))
  1536. def closure_glyphs(self, s):
  1537. s.table = self.table
  1538. if self.table.ScriptList:
  1539. feature_indices = self.table.ScriptList.collect_features()
  1540. else:
  1541. feature_indices = []
  1542. if self.table.FeatureList:
  1543. lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
  1544. else:
  1545. lookup_indices = []
  1546. if getattr(self.table, "FeatureVariations", None):
  1547. lookup_indices += self.table.FeatureVariations.collect_lookups(feature_indices)
  1548. lookup_indices = _uniq_sort(lookup_indices)
  1549. if self.table.LookupList:
  1550. s._doneLookups = {}
  1551. while True:
  1552. orig_glyphs = frozenset(s.glyphs)
  1553. for i in lookup_indices:
  1554. if i >= self.table.LookupList.LookupCount:
  1555. continue
  1556. if not self.table.LookupList.Lookup[i]:
  1557. continue
  1558. self.table.LookupList.Lookup[i].closure_glyphs(s)
  1559. if orig_glyphs == s.glyphs:
  1560. break
  1561. del s._doneLookups
  1562. del s.table
  1563. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1564. def subset_glyphs(self, s):
  1565. s.glyphs = s.glyphs_gsubed
  1566. if self.table.LookupList:
  1567. lookup_indices = self.table.LookupList.subset_glyphs(s)
  1568. else:
  1569. lookup_indices = []
  1570. self.subset_lookups(lookup_indices)
  1571. return True
  1572. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1573. def retain_empty_scripts(self):
  1574. # https://github.com/fonttools/fonttools/issues/518
  1575. # https://bugzilla.mozilla.org/show_bug.cgi?id=1080739#c15
  1576. return self.__class__ == ttLib.getTableClass("GSUB")
  1577. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1578. def subset_lookups(self, lookup_indices):
  1579. """Retains specified lookups, then removes empty features, language
  1580. systems, and scripts."""
  1581. if self.table.LookupList:
  1582. self.table.LookupList.subset_lookups(lookup_indices)
  1583. if self.table.FeatureList:
  1584. feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
  1585. else:
  1586. feature_indices = []
  1587. if getattr(self.table, "FeatureVariations", None):
  1588. feature_indices += self.table.FeatureVariations.subset_lookups(lookup_indices)
  1589. feature_indices = _uniq_sort(feature_indices)
  1590. if self.table.FeatureList:
  1591. self.table.FeatureList.subset_features(feature_indices)
  1592. if getattr(self.table, "FeatureVariations", None):
  1593. self.table.FeatureVariations.subset_features(feature_indices)
  1594. if self.table.ScriptList:
  1595. self.table.ScriptList.subset_features(
  1596. feature_indices, self.retain_empty_scripts()
  1597. )
  1598. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1599. def neuter_lookups(self, lookup_indices):
  1600. """Sets lookups not in lookup_indices to None."""
  1601. if self.table.LookupList:
  1602. self.table.LookupList.neuter_lookups(lookup_indices)
  1603. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1604. def prune_lookups(self, remap=True):
  1605. """Remove (default) or neuter unreferenced lookups"""
  1606. if self.table.ScriptList:
  1607. feature_indices = self.table.ScriptList.collect_features()
  1608. else:
  1609. feature_indices = []
  1610. if self.table.FeatureList:
  1611. lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
  1612. else:
  1613. lookup_indices = []
  1614. if getattr(self.table, "FeatureVariations", None):
  1615. lookup_indices += self.table.FeatureVariations.collect_lookups(feature_indices)
  1616. lookup_indices = _uniq_sort(lookup_indices)
  1617. if self.table.LookupList:
  1618. lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
  1619. else:
  1620. lookup_indices = []
  1621. if remap:
  1622. self.subset_lookups(lookup_indices)
  1623. else:
  1624. self.neuter_lookups(lookup_indices)
  1625. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1626. def subset_feature_tags(self, feature_tags):
  1627. if self.table.FeatureList:
  1628. feature_indices = [
  1629. i
  1630. for i, f in enumerate(self.table.FeatureList.FeatureRecord)
  1631. if f.FeatureTag in feature_tags
  1632. ]
  1633. self.table.FeatureList.subset_features(feature_indices)
  1634. if getattr(self.table, "FeatureVariations", None):
  1635. self.table.FeatureVariations.subset_features(feature_indices)
  1636. else:
  1637. feature_indices = []
  1638. if self.table.ScriptList:
  1639. self.table.ScriptList.subset_features(
  1640. feature_indices, self.retain_empty_scripts()
  1641. )
  1642. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1643. def subset_script_tags(self, tags):
  1644. langsys = {}
  1645. script_tags = set()
  1646. for tag in tags:
  1647. script_tag, lang_tag = tag.split(".") if "." in tag else (tag, "*")
  1648. script_tags.add(script_tag.ljust(4))
  1649. langsys.setdefault(script_tag, set()).add(lang_tag.ljust(4))
  1650. if self.table.ScriptList:
  1651. self.table.ScriptList.ScriptRecord = [
  1652. s for s in self.table.ScriptList.ScriptRecord if s.ScriptTag in script_tags
  1653. ]
  1654. self.table.ScriptList.ScriptCount = len(self.table.ScriptList.ScriptRecord)
  1655. for record in self.table.ScriptList.ScriptRecord:
  1656. if record.ScriptTag in langsys and "* " not in langsys[record.ScriptTag]:
  1657. record.Script.LangSysRecord = [
  1658. l
  1659. for l in record.Script.LangSysRecord
  1660. if l.LangSysTag in langsys[record.ScriptTag]
  1661. ]
  1662. record.Script.LangSysCount = len(record.Script.LangSysRecord)
  1663. if "dflt" not in langsys[record.ScriptTag]:
  1664. record.Script.DefaultLangSys = None
  1665. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1666. def prune_features(self):
  1667. """Remove unreferenced features"""
  1668. if self.table.ScriptList:
  1669. feature_indices = self.table.ScriptList.collect_features()
  1670. else:
  1671. feature_indices = []
  1672. if self.table.FeatureList:
  1673. self.table.FeatureList.subset_features(feature_indices)
  1674. if getattr(self.table, "FeatureVariations", None):
  1675. self.table.FeatureVariations.subset_features(feature_indices)
  1676. if self.table.ScriptList:
  1677. self.table.ScriptList.subset_features(
  1678. feature_indices, self.retain_empty_scripts()
  1679. )
  1680. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1681. def prune_pre_subset(self, font, options):
  1682. # Drop undesired features
  1683. if "*" not in options.layout_scripts:
  1684. self.subset_script_tags(options.layout_scripts)
  1685. if "*" not in options.layout_features:
  1686. self.subset_feature_tags(options.layout_features)
  1687. # Neuter unreferenced lookups
  1688. self.prune_lookups(remap=False)
  1689. return True
  1690. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1691. def remove_redundant_langsys(self):
  1692. table = self.table
  1693. if not table.ScriptList or not table.FeatureList:
  1694. return
  1695. features = table.FeatureList.FeatureRecord
  1696. for s in table.ScriptList.ScriptRecord:
  1697. d = s.Script.DefaultLangSys
  1698. if not d:
  1699. continue
  1700. for lr in s.Script.LangSysRecord[:]:
  1701. l = lr.LangSys
  1702. # Compare d and l
  1703. if len(d.FeatureIndex) != len(l.FeatureIndex):
  1704. continue
  1705. if (d.ReqFeatureIndex == 65535) != (l.ReqFeatureIndex == 65535):
  1706. continue
  1707. if d.ReqFeatureIndex != 65535:
  1708. if features[d.ReqFeatureIndex] != features[l.ReqFeatureIndex]:
  1709. continue
  1710. for i in range(len(d.FeatureIndex)):
  1711. if features[d.FeatureIndex[i]] != features[l.FeatureIndex[i]]:
  1712. break
  1713. else:
  1714. # LangSys and default are equal; delete LangSys
  1715. s.Script.LangSysRecord.remove(lr)
  1716. @_add_method(ttLib.getTableClass("GSUB"), ttLib.getTableClass("GPOS"))
  1717. def prune_post_subset(self, font, options):
  1718. table = self.table
  1719. self.prune_lookups() # XXX Is this actually needed?!
  1720. if table.LookupList:
  1721. table.LookupList.prune_post_subset(font, options)
  1722. # XXX Next two lines disabled because OTS is stupid and
  1723. # doesn't like NULL offsets here.
  1724. # if not table.LookupList.Lookup:
  1725. # table.LookupList = None
  1726. if not table.LookupList:
  1727. table.FeatureList = None
  1728. if table.FeatureList:
  1729. self.remove_redundant_langsys()
  1730. # Remove unreferenced features
  1731. self.prune_features()
  1732. # XXX Next two lines disabled because OTS is stupid and
  1733. # doesn't like NULL offsets here.
  1734. # if table.FeatureList and not table.FeatureList.FeatureRecord:
  1735. # table.FeatureList = None
  1736. # Never drop scripts themselves as them just being available
  1737. # holds semantic significance.
  1738. # XXX Next two lines disabled because OTS is stupid and
  1739. # doesn't like NULL offsets here.
  1740. # if table.ScriptList and not table.ScriptList.ScriptRecord:
  1741. # table.ScriptList = None
  1742. if hasattr(table, "FeatureVariations"):
  1743. # drop FeatureVariations if there are no features to substitute
  1744. if table.FeatureVariations and not (
  1745. table.FeatureList and table.FeatureVariations.FeatureVariationRecord
  1746. ):
  1747. table.FeatureVariations = None
  1748. # downgrade table version if there are no FeatureVariations
  1749. if not table.FeatureVariations and table.Version == 0x00010001:
  1750. table.Version = 0x00010000
  1751. return True
  1752. @_add_method(ttLib.getTableClass("GDEF"))
  1753. def subset_glyphs(self, s):
  1754. glyphs = s.glyphs_gsubed
  1755. table = self.table
  1756. if table.LigCaretList:
  1757. indices = table.LigCaretList.Coverage.subset(glyphs)
  1758. table.LigCaretList.LigGlyph = _list_subset(table.LigCaretList.LigGlyph, indices)
  1759. table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
  1760. if table.MarkAttachClassDef:
  1761. table.MarkAttachClassDef.classDefs = {
  1762. g: v for g, v in table.MarkAttachClassDef.classDefs.items() if g in glyphs
  1763. }
  1764. if table.GlyphClassDef:
  1765. table.GlyphClassDef.classDefs = {
  1766. g: v for g, v in table.GlyphClassDef.classDefs.items() if g in glyphs
  1767. }
  1768. if table.AttachList:
  1769. indices = table.AttachList.Coverage.subset(glyphs)
  1770. GlyphCount = table.AttachList.GlyphCount
  1771. table.AttachList.AttachPoint = [
  1772. table.AttachList.AttachPoint[i] for i in indices if i < GlyphCount
  1773. ]
  1774. table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
  1775. if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef:
  1776. markGlyphSets = table.MarkGlyphSetsDef
  1777. for coverage in markGlyphSets.Coverage:
  1778. if coverage:
  1779. coverage.subset(glyphs)
  1780. s.used_mark_sets = [i for i, c in enumerate(markGlyphSets.Coverage) if c.glyphs]
  1781. markGlyphSets.Coverage = [c for c in markGlyphSets.Coverage if c.glyphs]
  1782. return True
  1783. def _pruneGDEF(font):
  1784. if "GDEF" not in font:
  1785. return
  1786. gdef = font["GDEF"]
  1787. table = gdef.table
  1788. if not hasattr(table, "VarStore"):
  1789. return
  1790. store = table.VarStore
  1791. usedVarIdxes = set()
  1792. # Collect.
  1793. table.collect_device_varidxes(usedVarIdxes)
  1794. if "GPOS" in font:
  1795. font["GPOS"].table.collect_device_varidxes(usedVarIdxes)
  1796. # Subset.
  1797. varidx_map = store.subset_varidxes(usedVarIdxes)
  1798. # Map.
  1799. table.remap_device_varidxes(varidx_map)
  1800. if "GPOS" in font:
  1801. font["GPOS"].table.remap_device_varidxes(varidx_map)
  1802. @_add_method(ttLib.getTableClass("GDEF"))
  1803. def prune_post_subset(self, font, options):
  1804. table = self.table
  1805. # XXX check these against OTS
  1806. if table.LigCaretList and not table.LigCaretList.LigGlyphCount:
  1807. table.LigCaretList = None
  1808. if table.MarkAttachClassDef and not table.MarkAttachClassDef.classDefs:
  1809. table.MarkAttachClassDef = None
  1810. if table.GlyphClassDef and not table.GlyphClassDef.classDefs:
  1811. table.GlyphClassDef = None
  1812. if table.AttachList and not table.AttachList.GlyphCount:
  1813. table.AttachList = None
  1814. if hasattr(table, "VarStore"):
  1815. _pruneGDEF(font)
  1816. if table.VarStore.VarDataCount == 0:
  1817. if table.Version == 0x00010003:
  1818. table.Version = 0x00010002
  1819. if (
  1820. not hasattr(table, "MarkGlyphSetsDef")
  1821. or not table.MarkGlyphSetsDef
  1822. or not table.MarkGlyphSetsDef.Coverage
  1823. ):
  1824. table.MarkGlyphSetsDef = None
  1825. if table.Version == 0x00010002:
  1826. table.Version = 0x00010000
  1827. return bool(
  1828. table.LigCaretList
  1829. or table.MarkAttachClassDef
  1830. or table.GlyphClassDef
  1831. or table.AttachList
  1832. or (table.Version >= 0x00010002 and table.MarkGlyphSetsDef)
  1833. or (table.Version >= 0x00010003 and table.VarStore)
  1834. )
  1835. @_add_method(ttLib.getTableClass("kern"))
  1836. def prune_pre_subset(self, font, options):
  1837. # Prune unknown kern table types
  1838. self.kernTables = [t for t in self.kernTables if hasattr(t, "kernTable")]
  1839. return bool(self.kernTables)
  1840. @_add_method(ttLib.getTableClass("kern"))
  1841. def subset_glyphs(self, s):
  1842. glyphs = s.glyphs_gsubed
  1843. for t in self.kernTables:
  1844. t.kernTable = {
  1845. (a, b): v
  1846. for (a, b), v in t.kernTable.items()
  1847. if a in glyphs and b in glyphs
  1848. }
  1849. self.kernTables = [t for t in self.kernTables if t.kernTable]
  1850. return bool(self.kernTables)
  1851. @_add_method(ttLib.getTableClass("vmtx"))
  1852. def subset_glyphs(self, s):
  1853. self.metrics = _dict_subset(self.metrics, s.glyphs)
  1854. for g in s.glyphs_emptied:
  1855. self.metrics[g] = (0, 0)
  1856. return bool(self.metrics)
  1857. @_add_method(ttLib.getTableClass("hmtx"))
  1858. def subset_glyphs(self, s):
  1859. self.metrics = _dict_subset(self.metrics, s.glyphs)
  1860. for g in s.glyphs_emptied:
  1861. self.metrics[g] = (0, 0)
  1862. return True # Required table
  1863. @_add_method(ttLib.getTableClass("hdmx"))
  1864. def subset_glyphs(self, s):
  1865. self.hdmx = {sz: _dict_subset(l, s.glyphs) for sz, l in self.hdmx.items()}
  1866. for sz in self.hdmx:
  1867. for g in s.glyphs_emptied:
  1868. self.hdmx[sz][g] = 0
  1869. return bool(self.hdmx)
  1870. @_add_method(ttLib.getTableClass("ankr"))
  1871. def subset_glyphs(self, s):
  1872. table = self.table.AnchorPoints
  1873. assert table.Format == 0, "unknown 'ankr' format %s" % table.Format
  1874. table.Anchors = {
  1875. glyph: table.Anchors[glyph] for glyph in s.glyphs if glyph in table.Anchors
  1876. }
  1877. return len(table.Anchors) > 0
  1878. @_add_method(ttLib.getTableClass("bsln"))
  1879. def closure_glyphs(self, s):
  1880. table = self.table.Baseline
  1881. if table.Format in (2, 3):
  1882. s.glyphs.add(table.StandardGlyph)
  1883. @_add_method(ttLib.getTableClass("bsln"))
  1884. def subset_glyphs(self, s):
  1885. table = self.table.Baseline
  1886. if table.Format in (1, 3):
  1887. baselines = {
  1888. glyph: table.BaselineValues.get(glyph, table.DefaultBaseline)
  1889. for glyph in s.glyphs
  1890. }
  1891. if len(baselines) > 0:
  1892. mostCommon, _cnt = Counter(baselines.values()).most_common(1)[0]
  1893. table.DefaultBaseline = mostCommon
  1894. baselines = {glyph: b for glyph, b in baselines.items() if b != mostCommon}
  1895. if len(baselines) > 0:
  1896. table.BaselineValues = baselines
  1897. else:
  1898. table.Format = {1: 0, 3: 2}[table.Format]
  1899. del table.BaselineValues
  1900. return True
  1901. @_add_method(ttLib.getTableClass("lcar"))
  1902. def subset_glyphs(self, s):
  1903. table = self.table.LigatureCarets
  1904. if table.Format in (0, 1):
  1905. table.Carets = {
  1906. glyph: table.Carets[glyph] for glyph in s.glyphs if glyph in table.Carets
  1907. }
  1908. return len(table.Carets) > 0
  1909. else:
  1910. assert False, "unknown 'lcar' format %s" % table.Format
  1911. @_add_method(ttLib.getTableClass("gvar"))
  1912. def prune_pre_subset(self, font, options):
  1913. if options.notdef_glyph and not options.notdef_outline:
  1914. self.variations[font.glyphOrder[0]] = []
  1915. return True
  1916. @_add_method(ttLib.getTableClass("gvar"))
  1917. def subset_glyphs(self, s):
  1918. self.variations = _dict_subset(self.variations, s.glyphs)
  1919. self.glyphCount = len(self.variations)
  1920. return bool(self.variations)
  1921. def _remap_index_map(s, varidx_map, table_map):
  1922. map_ = {k: varidx_map[v] for k, v in table_map.mapping.items()}
  1923. # Emptied glyphs are remapped to:
  1924. # if GID <= last retained GID, 0/0: delta set for 0/0 is expected to exist & zeros compress well
  1925. # if GID > last retained GID, major/minor of the last retained glyph: will be optimized out by table compiler
  1926. last_idx = varidx_map[table_map.mapping[s.last_retained_glyph]]
  1927. for g, i in s.reverseEmptiedGlyphMap.items():
  1928. map_[g] = last_idx if i > s.last_retained_order else 0
  1929. return map_
  1930. @_add_method(ttLib.getTableClass("HVAR"))
  1931. def subset_glyphs(self, s):
  1932. table = self.table
  1933. used = set()
  1934. advIdxes_ = set()
  1935. retainAdvMap = False
  1936. if table.AdvWidthMap:
  1937. table.AdvWidthMap.mapping = _dict_subset(table.AdvWidthMap.mapping, s.glyphs)
  1938. used.update(table.AdvWidthMap.mapping.values())
  1939. else:
  1940. used.update(s.reverseOrigGlyphMap.values())
  1941. advIdxes_ = used.copy()
  1942. retainAdvMap = s.options.retain_gids
  1943. if table.LsbMap:
  1944. table.LsbMap.mapping = _dict_subset(table.LsbMap.mapping, s.glyphs)
  1945. used.update(table.LsbMap.mapping.values())
  1946. if table.RsbMap:
  1947. table.RsbMap.mapping = _dict_subset(table.RsbMap.mapping, s.glyphs)
  1948. used.update(table.RsbMap.mapping.values())
  1949. varidx_map = table.VarStore.subset_varidxes(
  1950. used, retainFirstMap=retainAdvMap, advIdxes=advIdxes_
  1951. )
  1952. if table.AdvWidthMap:
  1953. table.AdvWidthMap.mapping = _remap_index_map(s, varidx_map, table.AdvWidthMap)
  1954. if table.LsbMap:
  1955. table.LsbMap.mapping = _remap_index_map(s, varidx_map, table.LsbMap)
  1956. if table.RsbMap:
  1957. table.RsbMap.mapping = _remap_index_map(s, varidx_map, table.RsbMap)
  1958. # TODO Return emptiness...
  1959. return True
  1960. @_add_method(ttLib.getTableClass("VVAR"))
  1961. def subset_glyphs(self, s):
  1962. table = self.table
  1963. used = set()
  1964. advIdxes_ = set()
  1965. retainAdvMap = False
  1966. if table.AdvHeightMap:
  1967. table.AdvHeightMap.mapping = _dict_subset(table.AdvHeightMap.mapping, s.glyphs)
  1968. used.update(table.AdvHeightMap.mapping.values())
  1969. else:
  1970. used.update(s.reverseOrigGlyphMap.values())
  1971. advIdxes_ = used.copy()
  1972. retainAdvMap = s.options.retain_gids
  1973. if table.TsbMap:
  1974. table.TsbMap.mapping = _dict_subset(table.TsbMap.mapping, s.glyphs)
  1975. used.update(table.TsbMap.mapping.values())
  1976. if table.BsbMap:
  1977. table.BsbMap.mapping = _dict_subset(table.BsbMap.mapping, s.glyphs)
  1978. used.update(table.BsbMap.mapping.values())
  1979. if table.VOrgMap:
  1980. table.VOrgMap.mapping = _dict_subset(table.VOrgMap.mapping, s.glyphs)
  1981. used.update(table.VOrgMap.mapping.values())
  1982. varidx_map = table.VarStore.subset_varidxes(
  1983. used, retainFirstMap=retainAdvMap, advIdxes=advIdxes_
  1984. )
  1985. if table.AdvHeightMap:
  1986. table.AdvHeightMap.mapping = _remap_index_map(s, varidx_map, table.AdvHeightMap)
  1987. if table.TsbMap:
  1988. table.TsbMap.mapping = _remap_index_map(s, varidx_map, table.TsbMap)
  1989. if table.BsbMap:
  1990. table.BsbMap.mapping = _remap_index_map(s, varidx_map, table.BsbMap)
  1991. if table.VOrgMap:
  1992. table.VOrgMap.mapping = _remap_index_map(s, varidx_map, table.VOrgMap)
  1993. # TODO Return emptiness...
  1994. return True
  1995. @_add_method(ttLib.getTableClass("VORG"))
  1996. def subset_glyphs(self, s):
  1997. self.VOriginRecords = {
  1998. g: v for g, v in self.VOriginRecords.items() if g in s.glyphs
  1999. }
  2000. self.numVertOriginYMetrics = len(self.VOriginRecords)
  2001. return True # Never drop; has default metrics
  2002. @_add_method(ttLib.getTableClass("opbd"))
  2003. def subset_glyphs(self, s):
  2004. table = self.table.OpticalBounds
  2005. if table.Format == 0:
  2006. table.OpticalBoundsDeltas = {
  2007. glyph: table.OpticalBoundsDeltas[glyph]
  2008. for glyph in s.glyphs
  2009. if glyph in table.OpticalBoundsDeltas
  2010. }
  2011. return len(table.OpticalBoundsDeltas) > 0
  2012. elif table.Format == 1:
  2013. table.OpticalBoundsPoints = {
  2014. glyph: table.OpticalBoundsPoints[glyph]
  2015. for glyph in s.glyphs
  2016. if glyph in table.OpticalBoundsPoints
  2017. }
  2018. return len(table.OpticalBoundsPoints) > 0
  2019. else:
  2020. assert False, "unknown 'opbd' format %s" % table.Format
  2021. @_add_method(ttLib.getTableClass("post"))
  2022. def prune_pre_subset(self, font, options):
  2023. if not options.glyph_names:
  2024. self.formatType = 3.0
  2025. return True # Required table
  2026. @_add_method(ttLib.getTableClass("post"))
  2027. def subset_glyphs(self, s):
  2028. self.extraNames = [] # This seems to do it
  2029. return True # Required table
  2030. @_add_method(ttLib.getTableClass("prop"))
  2031. def subset_glyphs(self, s):
  2032. prop = self.table.GlyphProperties
  2033. if prop.Format == 0:
  2034. return prop.DefaultProperties != 0
  2035. elif prop.Format == 1:
  2036. prop.Properties = {
  2037. g: prop.Properties.get(g, prop.DefaultProperties) for g in s.glyphs
  2038. }
  2039. mostCommon, _cnt = Counter(prop.Properties.values()).most_common(1)[0]
  2040. prop.DefaultProperties = mostCommon
  2041. prop.Properties = {
  2042. g: prop for g, prop in prop.Properties.items() if prop != mostCommon
  2043. }
  2044. if len(prop.Properties) == 0:
  2045. del prop.Properties
  2046. prop.Format = 0
  2047. return prop.DefaultProperties != 0
  2048. return True
  2049. else:
  2050. assert False, "unknown 'prop' format %s" % prop.Format
  2051. def _paint_glyph_names(paint, colr):
  2052. result = set()
  2053. def callback(paint):
  2054. if paint.Format in {
  2055. otTables.PaintFormat.PaintGlyph,
  2056. otTables.PaintFormat.PaintColrGlyph,
  2057. }:
  2058. result.add(paint.Glyph)
  2059. paint.traverse(colr, callback)
  2060. return result
  2061. @_add_method(ttLib.getTableClass("COLR"))
  2062. def closure_glyphs(self, s):
  2063. if self.version > 0:
  2064. # on decompiling COLRv1, we only keep around the raw otTables
  2065. # but for subsetting we need dicts with fully decompiled layers;
  2066. # we store them temporarily in the C_O_L_R_ instance and delete
  2067. # them after we have finished subsetting.
  2068. self.ColorLayers = self._decompileColorLayersV0(self.table)
  2069. self.ColorLayersV1 = {
  2070. rec.BaseGlyph: rec.Paint
  2071. for rec in self.table.BaseGlyphList.BaseGlyphPaintRecord
  2072. }
  2073. decompose = s.glyphs
  2074. while decompose:
  2075. layers = set()
  2076. for g in decompose:
  2077. for layer in self.ColorLayers.get(g, []):
  2078. layers.add(layer.name)
  2079. if self.version > 0:
  2080. paint = self.ColorLayersV1.get(g)
  2081. if paint is not None:
  2082. layers.update(_paint_glyph_names(paint, self.table))
  2083. layers -= s.glyphs
  2084. s.glyphs.update(layers)
  2085. decompose = layers
  2086. @_add_method(ttLib.getTableClass("COLR"))
  2087. def subset_glyphs(self, s):
  2088. from fontTools.colorLib.unbuilder import unbuildColrV1
  2089. from fontTools.colorLib.builder import buildColrV1, populateCOLRv0
  2090. # only include glyphs after COLR closure, which in turn comes after cmap and GSUB
  2091. # closure, but importantly before glyf/CFF closures. COLR layers can refer to
  2092. # composite glyphs, and that's ok, since glyf/CFF closures happen after COLR closure
  2093. # and take care of those. If we also included glyphs resulting from glyf/CFF closures
  2094. # when deciding which COLR base glyphs to retain, then we may end up with a situation
  2095. # whereby a COLR base glyph is kept, not because directly requested (cmap)
  2096. # or substituted (GSUB) or referenced by another COLRv1 PaintColrGlyph, but because
  2097. # it corresponds to (has same GID as) a non-COLR glyph that happens to be used as a
  2098. # component in glyf or CFF table. Best case scenario we retain more glyphs than
  2099. # required; worst case we retain incomplete COLR records that try to reference
  2100. # glyphs that are no longer in the final subset font.
  2101. # https://github.com/fonttools/fonttools/issues/2461
  2102. s.glyphs = s.glyphs_colred
  2103. self.ColorLayers = {
  2104. g: self.ColorLayers[g] for g in s.glyphs if g in self.ColorLayers
  2105. }
  2106. if self.version == 0:
  2107. return bool(self.ColorLayers)
  2108. colorGlyphsV1 = unbuildColrV1(self.table.LayerList, self.table.BaseGlyphList)
  2109. self.table.LayerList, self.table.BaseGlyphList = buildColrV1(
  2110. {g: colorGlyphsV1[g] for g in colorGlyphsV1 if g in s.glyphs}
  2111. )
  2112. del self.ColorLayersV1
  2113. if self.table.ClipList is not None:
  2114. clips = self.table.ClipList.clips
  2115. self.table.ClipList.clips = {g: clips[g] for g in clips if g in s.glyphs}
  2116. layersV0 = self.ColorLayers
  2117. if not self.table.BaseGlyphList.BaseGlyphPaintRecord:
  2118. # no more COLRv1 glyphs: downgrade to version 0
  2119. self.version = 0
  2120. del self.table
  2121. return bool(layersV0)
  2122. populateCOLRv0(
  2123. self.table,
  2124. {g: [(layer.name, layer.colorID) for layer in layersV0[g]] for g in layersV0},
  2125. )
  2126. del self.ColorLayers
  2127. # TODO: also prune ununsed varIndices in COLR.VarStore
  2128. return True
  2129. @_add_method(ttLib.getTableClass("CPAL"))
  2130. def prune_post_subset(self, font, options):
  2131. # Keep whole "CPAL" if "SVG " is present as it may be referenced by the latter
  2132. # via 'var(--color{palette_entry_index}, ...)' CSS color variables.
  2133. # For now we just assume this is the case by the mere presence of "SVG " table,
  2134. # for parsing SVG to collect all the used indices is too much work...
  2135. # TODO(anthrotype): Do The Right Thing (TM).
  2136. if "SVG " in font:
  2137. return True
  2138. colr = font.get("COLR")
  2139. if not colr: # drop CPAL if COLR was subsetted to empty
  2140. return False
  2141. colors_by_index = defaultdict(list)
  2142. def collect_colors_by_index(paint):
  2143. if hasattr(paint, "PaletteIndex"): # either solid colors...
  2144. colors_by_index[paint.PaletteIndex].append(paint)
  2145. elif hasattr(paint, "ColorLine"): # ... or gradient color stops
  2146. for stop in paint.ColorLine.ColorStop:
  2147. colors_by_index[stop.PaletteIndex].append(stop)
  2148. if colr.version == 0:
  2149. for layers in colr.ColorLayers.values():
  2150. for layer in layers:
  2151. colors_by_index[layer.colorID].append(layer)
  2152. else:
  2153. if colr.table.LayerRecordArray:
  2154. for layer in colr.table.LayerRecordArray.LayerRecord:
  2155. colors_by_index[layer.PaletteIndex].append(layer)
  2156. for record in colr.table.BaseGlyphList.BaseGlyphPaintRecord:
  2157. record.Paint.traverse(colr.table, collect_colors_by_index)
  2158. # don't remap palette entry index 0xFFFF, this is always the foreground color
  2159. # https://github.com/fonttools/fonttools/issues/2257
  2160. retained_palette_indices = set(colors_by_index.keys()) - {0xFFFF}
  2161. for palette in self.palettes:
  2162. palette[:] = [c for i, c in enumerate(palette) if i in retained_palette_indices]
  2163. assert len(palette) == len(retained_palette_indices)
  2164. for new_index, old_index in enumerate(sorted(retained_palette_indices)):
  2165. for record in colors_by_index[old_index]:
  2166. if hasattr(record, "colorID"): # v0
  2167. record.colorID = new_index
  2168. elif hasattr(record, "PaletteIndex"): # v1
  2169. record.PaletteIndex = new_index
  2170. else:
  2171. raise AssertionError(record)
  2172. self.numPaletteEntries = len(self.palettes[0])
  2173. if self.version == 1:
  2174. kept_labels = []
  2175. for i, label in enumerate(self.paletteEntryLabels):
  2176. if i in retained_palette_indices:
  2177. kept_labels.append(label)
  2178. self.paletteEntryLabels = kept_labels
  2179. return bool(self.numPaletteEntries)
  2180. @_add_method(otTables.MathGlyphConstruction)
  2181. def closure_glyphs(self, glyphs):
  2182. variants = set()
  2183. for v in self.MathGlyphVariantRecord:
  2184. variants.add(v.VariantGlyph)
  2185. if self.GlyphAssembly:
  2186. for p in self.GlyphAssembly.PartRecords:
  2187. variants.add(p.glyph)
  2188. return variants
  2189. @_add_method(otTables.MathVariants)
  2190. def closure_glyphs(self, s):
  2191. glyphs = frozenset(s.glyphs)
  2192. variants = set()
  2193. if self.VertGlyphCoverage:
  2194. indices = self.VertGlyphCoverage.intersect(glyphs)
  2195. for i in indices:
  2196. variants.update(self.VertGlyphConstruction[i].closure_glyphs(glyphs))
  2197. if self.HorizGlyphCoverage:
  2198. indices = self.HorizGlyphCoverage.intersect(glyphs)
  2199. for i in indices:
  2200. variants.update(self.HorizGlyphConstruction[i].closure_glyphs(glyphs))
  2201. s.glyphs.update(variants)
  2202. @_add_method(ttLib.getTableClass("VARC"))
  2203. def subset_glyphs(self, s):
  2204. indices = self.table.Coverage.subset(s.glyphs)
  2205. self.table.VarCompositeGlyphs.VarCompositeGlyph = _list_subset(
  2206. self.table.VarCompositeGlyphs.VarCompositeGlyph, indices
  2207. )
  2208. return bool(self.table.VarCompositeGlyphs.VarCompositeGlyph)
  2209. @_add_method(ttLib.getTableClass("VARC"))
  2210. def closure_glyphs(self, s):
  2211. if self.table.VarCompositeGlyphs is None:
  2212. return
  2213. glyphMap = {glyphName: i for i, glyphName in enumerate(self.table.Coverage.glyphs)}
  2214. glyphRecords = self.table.VarCompositeGlyphs.VarCompositeGlyph
  2215. glyphs = s.glyphs
  2216. covered = set()
  2217. new = set(glyphs)
  2218. while new:
  2219. oldNew = new
  2220. new = set()
  2221. for glyphName in oldNew:
  2222. if glyphName in covered:
  2223. continue
  2224. idx = glyphMap.get(glyphName)
  2225. if idx is None:
  2226. continue
  2227. glyph = glyphRecords[idx]
  2228. for comp in glyph.components:
  2229. name = comp.glyphName
  2230. glyphs.add(name)
  2231. if name not in covered:
  2232. new.add(name)
  2233. @_add_method(ttLib.getTableClass("VARC"))
  2234. def prune_post_subset(self, font, options):
  2235. table = self.table
  2236. store = table.MultiVarStore
  2237. if store is not None:
  2238. usedVarIdxes = set()
  2239. table.collect_varidxes(usedVarIdxes)
  2240. varidx_map = store.subset_varidxes(usedVarIdxes)
  2241. table.remap_varidxes(varidx_map)
  2242. axisIndicesList = table.AxisIndicesList.Item
  2243. if axisIndicesList is not None:
  2244. usedIndices = set()
  2245. for glyph in table.VarCompositeGlyphs.VarCompositeGlyph:
  2246. for comp in glyph.components:
  2247. if comp.axisIndicesIndex is not None:
  2248. usedIndices.add(comp.axisIndicesIndex)
  2249. usedIndices = sorted(usedIndices)
  2250. table.AxisIndicesList.Item = _list_subset(axisIndicesList, usedIndices)
  2251. mapping = {old: new for new, old in enumerate(usedIndices)}
  2252. for glyph in table.VarCompositeGlyphs.VarCompositeGlyph:
  2253. for comp in glyph.components:
  2254. if comp.axisIndicesIndex is not None:
  2255. comp.axisIndicesIndex = mapping[comp.axisIndicesIndex]
  2256. conditionList = table.ConditionList
  2257. if conditionList is not None:
  2258. conditionTables = conditionList.ConditionTable
  2259. usedIndices = set()
  2260. for glyph in table.VarCompositeGlyphs.VarCompositeGlyph:
  2261. for comp in glyph.components:
  2262. if comp.conditionIndex is not None:
  2263. usedIndices.add(comp.conditionIndex)
  2264. usedIndices = sorted(usedIndices)
  2265. conditionList.ConditionTable = _list_subset(conditionTables, usedIndices)
  2266. mapping = {old: new for new, old in enumerate(usedIndices)}
  2267. for glyph in table.VarCompositeGlyphs.VarCompositeGlyph:
  2268. for comp in glyph.components:
  2269. if comp.conditionIndex is not None:
  2270. comp.conditionIndex = mapping[comp.conditionIndex]
  2271. return True
  2272. @_add_method(ttLib.getTableClass("MATH"))
  2273. def closure_glyphs(self, s):
  2274. if self.table.MathVariants:
  2275. self.table.MathVariants.closure_glyphs(s)
  2276. @_add_method(otTables.MathItalicsCorrectionInfo)
  2277. def subset_glyphs(self, s):
  2278. indices = self.Coverage.subset(s.glyphs)
  2279. self.ItalicsCorrection = _list_subset(self.ItalicsCorrection, indices)
  2280. self.ItalicsCorrectionCount = len(self.ItalicsCorrection)
  2281. return bool(self.ItalicsCorrectionCount)
  2282. @_add_method(otTables.MathTopAccentAttachment)
  2283. def subset_glyphs(self, s):
  2284. indices = self.TopAccentCoverage.subset(s.glyphs)
  2285. self.TopAccentAttachment = _list_subset(self.TopAccentAttachment, indices)
  2286. self.TopAccentAttachmentCount = len(self.TopAccentAttachment)
  2287. return bool(self.TopAccentAttachmentCount)
  2288. @_add_method(otTables.MathKernInfo)
  2289. def subset_glyphs(self, s):
  2290. indices = self.MathKernCoverage.subset(s.glyphs)
  2291. self.MathKernInfoRecords = _list_subset(self.MathKernInfoRecords, indices)
  2292. self.MathKernCount = len(self.MathKernInfoRecords)
  2293. return bool(self.MathKernCount)
  2294. @_add_method(otTables.MathGlyphInfo)
  2295. def subset_glyphs(self, s):
  2296. if self.MathItalicsCorrectionInfo:
  2297. self.MathItalicsCorrectionInfo.subset_glyphs(s)
  2298. if self.MathTopAccentAttachment:
  2299. self.MathTopAccentAttachment.subset_glyphs(s)
  2300. if self.MathKernInfo:
  2301. self.MathKernInfo.subset_glyphs(s)
  2302. if self.ExtendedShapeCoverage:
  2303. self.ExtendedShapeCoverage.subset(s.glyphs)
  2304. return True
  2305. @_add_method(otTables.MathVariants)
  2306. def subset_glyphs(self, s):
  2307. if self.VertGlyphCoverage:
  2308. indices = self.VertGlyphCoverage.subset(s.glyphs)
  2309. self.VertGlyphConstruction = _list_subset(self.VertGlyphConstruction, indices)
  2310. self.VertGlyphCount = len(self.VertGlyphConstruction)
  2311. if self.HorizGlyphCoverage:
  2312. indices = self.HorizGlyphCoverage.subset(s.glyphs)
  2313. self.HorizGlyphConstruction = _list_subset(self.HorizGlyphConstruction, indices)
  2314. self.HorizGlyphCount = len(self.HorizGlyphConstruction)
  2315. return True
  2316. @_add_method(ttLib.getTableClass("MATH"))
  2317. def subset_glyphs(self, s):
  2318. s.glyphs = s.glyphs_mathed
  2319. if self.table.MathGlyphInfo:
  2320. self.table.MathGlyphInfo.subset_glyphs(s)
  2321. if self.table.MathVariants:
  2322. self.table.MathVariants.subset_glyphs(s)
  2323. return True
  2324. @_add_method(ttLib.getTableModule("glyf").Glyph)
  2325. def remapComponentsFast(self, glyphidmap):
  2326. if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
  2327. return # Not composite
  2328. data = self.data = bytearray(self.data)
  2329. i = 10
  2330. more = 1
  2331. while more:
  2332. flags = (data[i] << 8) | data[i + 1]
  2333. glyphID = (data[i + 2] << 8) | data[i + 3]
  2334. # Remap
  2335. glyphID = glyphidmap[glyphID]
  2336. data[i + 2] = glyphID >> 8
  2337. data[i + 3] = glyphID & 0xFF
  2338. i += 4
  2339. flags = int(flags)
  2340. if flags & 0x0001:
  2341. i += 4 # ARG_1_AND_2_ARE_WORDS
  2342. else:
  2343. i += 2
  2344. if flags & 0x0008:
  2345. i += 2 # WE_HAVE_A_SCALE
  2346. elif flags & 0x0040:
  2347. i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
  2348. elif flags & 0x0080:
  2349. i += 8 # WE_HAVE_A_TWO_BY_TWO
  2350. more = flags & 0x0020 # MORE_COMPONENTS
  2351. @_add_method(ttLib.getTableClass("glyf"))
  2352. def closure_glyphs(self, s):
  2353. glyphSet = self.glyphs
  2354. decompose = s.glyphs
  2355. while decompose:
  2356. components = set()
  2357. for g in decompose:
  2358. if g not in glyphSet:
  2359. continue
  2360. gl = glyphSet[g]
  2361. for c in gl.getComponentNames(self):
  2362. components.add(c)
  2363. components -= s.glyphs
  2364. s.glyphs.update(components)
  2365. decompose = components
  2366. @_add_method(ttLib.getTableClass("glyf"))
  2367. def prune_pre_subset(self, font, options):
  2368. if options.notdef_glyph and not options.notdef_outline:
  2369. g = self[self.glyphOrder[0]]
  2370. # Yay, easy!
  2371. g.__dict__.clear()
  2372. g.data = b""
  2373. return True
  2374. @_add_method(ttLib.getTableClass("glyf"))
  2375. def subset_glyphs(self, s):
  2376. self.glyphs = _dict_subset(self.glyphs, s.glyphs)
  2377. if not s.options.retain_gids:
  2378. indices = [i for i, g in enumerate(self.glyphOrder) if g in s.glyphs]
  2379. glyphmap = {o: n for n, o in enumerate(indices)}
  2380. for v in self.glyphs.values():
  2381. if hasattr(v, "data"):
  2382. v.remapComponentsFast(glyphmap)
  2383. Glyph = ttLib.getTableModule("glyf").Glyph
  2384. for g in s.glyphs_emptied:
  2385. self.glyphs[g] = Glyph()
  2386. self.glyphs[g].data = b""
  2387. self.glyphOrder = [
  2388. g for g in self.glyphOrder if g in s.glyphs or g in s.glyphs_emptied
  2389. ]
  2390. # Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
  2391. return True
  2392. @_add_method(ttLib.getTableClass("glyf"))
  2393. def prune_post_subset(self, font, options):
  2394. remove_hinting = not options.hinting
  2395. for v in self.glyphs.values():
  2396. v.trim(remove_hinting=remove_hinting)
  2397. return True
  2398. @_add_method(ttLib.getTableClass("cmap"))
  2399. def closure_glyphs(self, s):
  2400. tables = [t for t in self.tables if t.isUnicode()]
  2401. # Close glyphs
  2402. for table in tables:
  2403. if table.format == 14:
  2404. for varSelector, cmap in table.uvsDict.items():
  2405. if varSelector not in s.unicodes_requested:
  2406. continue
  2407. glyphs = {g for u, g in cmap if u in s.unicodes_requested}
  2408. if None in glyphs:
  2409. glyphs.remove(None)
  2410. s.glyphs.update(glyphs)
  2411. else:
  2412. cmap = table.cmap
  2413. intersection = s.unicodes_requested.intersection(cmap.keys())
  2414. s.glyphs.update(cmap[u] for u in intersection)
  2415. # Calculate unicodes_missing
  2416. s.unicodes_missing = s.unicodes_requested.copy()
  2417. for table in tables:
  2418. s.unicodes_missing.difference_update(table.cmap)
  2419. @_add_method(ttLib.getTableClass("cmap"))
  2420. def prune_pre_subset(self, font, options):
  2421. if not options.legacy_cmap:
  2422. # Drop non-Unicode / non-Symbol cmaps
  2423. self.tables = [t for t in self.tables if t.isUnicode() or t.isSymbol()]
  2424. if not options.symbol_cmap:
  2425. self.tables = [t for t in self.tables if not t.isSymbol()]
  2426. # TODO(behdad) Only keep one subtable?
  2427. # For now, drop format=0 which can't be subset_glyphs easily?
  2428. self.tables = [t for t in self.tables if t.format != 0]
  2429. self.numSubTables = len(self.tables)
  2430. return True # Required table
  2431. @_add_method(ttLib.getTableClass("cmap"))
  2432. def subset_glyphs(self, s):
  2433. s.glyphs = None # We use s.glyphs_requested and s.unicodes_requested only
  2434. tables_format12_bmp = []
  2435. table_plat0_enc3 = {} # Unicode platform, Unicode BMP only, keyed by language
  2436. table_plat3_enc1 = {} # Windows platform, Unicode BMP, keyed by language
  2437. for t in self.tables:
  2438. if t.platformID == 0 and t.platEncID == 3:
  2439. table_plat0_enc3[t.language] = t
  2440. if t.platformID == 3 and t.platEncID == 1:
  2441. table_plat3_enc1[t.language] = t
  2442. if t.format == 14:
  2443. # TODO(behdad) We drop all the default-UVS mappings
  2444. # for glyphs_requested. So it's the caller's responsibility to make
  2445. # sure those are included.
  2446. t.uvsDict = {
  2447. v: [
  2448. (u, g)
  2449. for u, g in l
  2450. if g in s.glyphs_requested or u in s.unicodes_requested
  2451. ]
  2452. for v, l in t.uvsDict.items()
  2453. if v in s.unicodes_requested
  2454. }
  2455. t.uvsDict = {v: l for v, l in t.uvsDict.items() if l}
  2456. elif t.isUnicode():
  2457. t.cmap = {
  2458. u: g
  2459. for u, g in t.cmap.items()
  2460. if g in s.glyphs_requested or u in s.unicodes_requested
  2461. }
  2462. # Collect format 12 tables that hold only basic multilingual plane
  2463. # codepoints.
  2464. if t.format == 12 and t.cmap and max(t.cmap.keys()) < 0x10000:
  2465. tables_format12_bmp.append(t)
  2466. else:
  2467. t.cmap = {u: g for u, g in t.cmap.items() if g in s.glyphs_requested}
  2468. # Fomat 12 tables are redundant if they contain just the same BMP codepoints
  2469. # their little BMP-only encoding siblings contain.
  2470. for t in tables_format12_bmp:
  2471. if (
  2472. t.platformID == 0 # Unicode platform
  2473. and t.platEncID == 4 # Unicode full repertoire
  2474. and t.language in table_plat0_enc3 # Have a BMP-only sibling?
  2475. and table_plat0_enc3[t.language].cmap == t.cmap
  2476. ):
  2477. t.cmap.clear()
  2478. elif (
  2479. t.platformID == 3 # Windows platform
  2480. and t.platEncID == 10 # Unicode full repertoire
  2481. and t.language in table_plat3_enc1 # Have a BMP-only sibling?
  2482. and table_plat3_enc1[t.language].cmap == t.cmap
  2483. ):
  2484. t.cmap.clear()
  2485. self.tables = [t for t in self.tables if (t.cmap if t.format != 14 else t.uvsDict)]
  2486. self.numSubTables = len(self.tables)
  2487. # TODO(behdad) Convert formats when needed.
  2488. # In particular, if we have a format=12 without non-BMP
  2489. # characters, convert it to format=4 if there's not one.
  2490. return True # Required table
  2491. @_add_method(ttLib.getTableClass("DSIG"))
  2492. def prune_pre_subset(self, font, options):
  2493. # Drop all signatures since they will be invalid
  2494. self.usNumSigs = 0
  2495. self.signatureRecords = []
  2496. return True
  2497. @_add_method(ttLib.getTableClass("maxp"))
  2498. def prune_pre_subset(self, font, options):
  2499. if not options.hinting:
  2500. if self.tableVersion == 0x00010000:
  2501. self.maxZones = 1
  2502. self.maxTwilightPoints = 0
  2503. self.maxStorage = 0
  2504. self.maxFunctionDefs = 0
  2505. self.maxInstructionDefs = 0
  2506. self.maxStackElements = 0
  2507. self.maxSizeOfInstructions = 0
  2508. return True
  2509. @_add_method(ttLib.getTableClass("name"))
  2510. def prune_post_subset(self, font, options):
  2511. visitor = NameRecordVisitor()
  2512. visitor.visit(font)
  2513. nameIDs = set(options.name_IDs) | visitor.seen
  2514. if "*" in options.name_IDs:
  2515. nameIDs |= {n.nameID for n in self.names if n.nameID < 256}
  2516. self.names = [n for n in self.names if n.nameID in nameIDs]
  2517. if not options.name_legacy:
  2518. # TODO(behdad) Sometimes (eg Apple Color Emoji) there's only a macroman
  2519. # entry for Latin and no Unicode names.
  2520. self.names = [n for n in self.names if n.isUnicode()]
  2521. # TODO(behdad) Option to keep only one platform's
  2522. if "*" not in options.name_languages:
  2523. # TODO(behdad) This is Windows-platform specific!
  2524. self.names = [n for n in self.names if n.langID in options.name_languages]
  2525. if options.obfuscate_names:
  2526. namerecs = []
  2527. for n in self.names:
  2528. if n.nameID in [1, 4]:
  2529. n.string = ".\x7f".encode("utf_16_be") if n.isUnicode() else ".\x7f"
  2530. elif n.nameID in [2, 6]:
  2531. n.string = "\x7f".encode("utf_16_be") if n.isUnicode() else "\x7f"
  2532. elif n.nameID == 3:
  2533. n.string = ""
  2534. elif n.nameID in [16, 17, 18]:
  2535. continue
  2536. namerecs.append(n)
  2537. self.names = namerecs
  2538. return True # Required table
  2539. @_add_method(ttLib.getTableClass("head"))
  2540. def prune_post_subset(self, font, options):
  2541. # Force re-compiling head table, to update any recalculated values.
  2542. return True
  2543. # TODO(behdad) OS/2 ulCodePageRange?
  2544. # TODO(behdad) Drop AAT tables.
  2545. # TODO(behdad) Drop unneeded GSUB/GPOS Script/LangSys entries.
  2546. # TODO(behdad) Drop empty GSUB/GPOS, and GDEF if no GSUB/GPOS left
  2547. # TODO(behdad) Drop GDEF subitems if unused by lookups
  2548. # TODO(behdad) Avoid recursing too much (in GSUB/GPOS and in CFF)
  2549. # TODO(behdad) Text direction considerations.
  2550. # TODO(behdad) Text script / language considerations.
  2551. # TODO(behdad) Optionally drop 'kern' table if GPOS available
  2552. # TODO(behdad) Implement --unicode='*' to choose all cmap'ed
  2553. # TODO(behdad) Drop old-spec Indic scripts
  2554. class Options(object):
  2555. class OptionError(Exception):
  2556. pass
  2557. class UnknownOptionError(OptionError):
  2558. pass
  2559. # spaces in tag names (e.g. "SVG ", "cvt ") are stripped by the argument parser
  2560. _drop_tables_default = [
  2561. "BASE",
  2562. "JSTF",
  2563. "DSIG",
  2564. "EBDT",
  2565. "EBLC",
  2566. "EBSC",
  2567. "PCLT",
  2568. "LTSH",
  2569. ]
  2570. _drop_tables_default += ["Feat", "Glat", "Gloc", "Silf", "Sill"] # Graphite
  2571. _no_subset_tables_default = [
  2572. "avar",
  2573. "fvar",
  2574. "gasp",
  2575. "head",
  2576. "hhea",
  2577. "maxp",
  2578. "vhea",
  2579. "OS/2",
  2580. "loca",
  2581. "name",
  2582. "cvt",
  2583. "fpgm",
  2584. "prep",
  2585. "VDMX",
  2586. "DSIG",
  2587. "CPAL",
  2588. "MVAR",
  2589. "cvar",
  2590. "STAT",
  2591. ]
  2592. _hinting_tables_default = ["cvt", "cvar", "fpgm", "prep", "hdmx", "VDMX"]
  2593. # Based on HarfBuzz shapers
  2594. _layout_features_groups = {
  2595. # Default shaper
  2596. "common": ["rvrn", "ccmp", "liga", "locl", "mark", "mkmk", "rlig"],
  2597. "fractions": ["frac", "numr", "dnom"],
  2598. "horizontal": ["calt", "clig", "curs", "kern", "rclt"],
  2599. "vertical": ["valt", "vert", "vkrn", "vpal", "vrt2"],
  2600. "ltr": ["ltra", "ltrm"],
  2601. "rtl": ["rtla", "rtlm"],
  2602. "rand": ["rand"],
  2603. "justify": ["jalt"],
  2604. "private": ["Harf", "HARF", "Buzz", "BUZZ"],
  2605. "east_asian_spacing": ["chws", "vchw", "halt", "vhal"],
  2606. # Complex shapers
  2607. "arabic": [
  2608. "init",
  2609. "medi",
  2610. "fina",
  2611. "isol",
  2612. "med2",
  2613. "fin2",
  2614. "fin3",
  2615. "cswh",
  2616. "mset",
  2617. "stch",
  2618. ],
  2619. "hangul": ["ljmo", "vjmo", "tjmo"],
  2620. "tibetan": ["abvs", "blws", "abvm", "blwm"],
  2621. "indic": [
  2622. "nukt",
  2623. "akhn",
  2624. "rphf",
  2625. "rkrf",
  2626. "pref",
  2627. "blwf",
  2628. "half",
  2629. "abvf",
  2630. "pstf",
  2631. "cfar",
  2632. "vatu",
  2633. "cjct",
  2634. "init",
  2635. "pres",
  2636. "abvs",
  2637. "blws",
  2638. "psts",
  2639. "haln",
  2640. "dist",
  2641. "abvm",
  2642. "blwm",
  2643. ],
  2644. }
  2645. _layout_features_default = _uniq_sort(
  2646. sum(iter(_layout_features_groups.values()), [])
  2647. )
  2648. def __init__(self, **kwargs):
  2649. self.drop_tables = self._drop_tables_default[:]
  2650. self.no_subset_tables = self._no_subset_tables_default[:]
  2651. self.passthrough_tables = False # keep/drop tables we can't subset
  2652. self.hinting_tables = self._hinting_tables_default[:]
  2653. self.legacy_kern = False # drop 'kern' table if GPOS available
  2654. self.layout_closure = True
  2655. self.layout_features = self._layout_features_default[:]
  2656. self.layout_scripts = ["*"]
  2657. self.ignore_missing_glyphs = False
  2658. self.ignore_missing_unicodes = True
  2659. self.hinting = True
  2660. self.glyph_names = False
  2661. self.legacy_cmap = False
  2662. self.symbol_cmap = False
  2663. self.name_IDs = [
  2664. 0,
  2665. 1,
  2666. 2,
  2667. 3,
  2668. 4,
  2669. 5,
  2670. 6,
  2671. ] # https://github.com/fonttools/fonttools/issues/1170#issuecomment-364631225
  2672. self.name_legacy = False
  2673. self.name_languages = [0x0409] # English
  2674. self.obfuscate_names = False # to make webfont unusable as a system font
  2675. self.retain_gids = False
  2676. self.notdef_glyph = True # gid0 for TrueType / .notdef for CFF
  2677. self.notdef_outline = False # No need for notdef to have an outline really
  2678. self.recommended_glyphs = False # gid1, gid2, gid3 for TrueType
  2679. self.recalc_bounds = False # Recalculate font bounding boxes
  2680. self.recalc_timestamp = False # Recalculate font modified timestamp
  2681. self.prune_unicode_ranges = True # Clear unused 'ulUnicodeRange' bits
  2682. self.prune_codepage_ranges = True # Clear unused 'ulCodePageRange' bits
  2683. self.recalc_average_width = False # update 'xAvgCharWidth'
  2684. self.recalc_max_context = False # update 'usMaxContext'
  2685. self.canonical_order = None # Order tables as recommended
  2686. self.flavor = None # May be 'woff' or 'woff2'
  2687. self.with_zopfli = False # use zopfli instead of zlib for WOFF 1.0
  2688. self.desubroutinize = False # Desubroutinize CFF CharStrings
  2689. self.harfbuzz_repacker = USE_HARFBUZZ_REPACKER.default
  2690. self.verbose = False
  2691. self.timing = False
  2692. self.xml = False
  2693. self.font_number = -1
  2694. self.pretty_svg = False
  2695. self.lazy = True
  2696. self.set(**kwargs)
  2697. def set(self, **kwargs):
  2698. for k, v in kwargs.items():
  2699. if not hasattr(self, k):
  2700. raise self.UnknownOptionError("Unknown option '%s'" % k)
  2701. setattr(self, k, v)
  2702. def parse_opts(self, argv, ignore_unknown=[]):
  2703. posargs = []
  2704. passthru_options = []
  2705. for a in argv:
  2706. orig_a = a
  2707. if not a.startswith("--"):
  2708. posargs.append(a)
  2709. continue
  2710. a = a[2:]
  2711. i = a.find("=")
  2712. op = "="
  2713. if i == -1:
  2714. if a.startswith("no-"):
  2715. k = a[3:]
  2716. if k == "canonical-order":
  2717. # reorderTables=None is faster than False (the latter
  2718. # still reorders to "keep" the original table order)
  2719. v = None
  2720. else:
  2721. v = False
  2722. else:
  2723. k = a
  2724. v = True
  2725. if k.endswith("?"):
  2726. k = k[:-1]
  2727. v = "?"
  2728. else:
  2729. k = a[:i]
  2730. if k[-1] in "-+":
  2731. op = k[-1] + "=" # Op is '-=' or '+=' now.
  2732. k = k[:-1]
  2733. v = a[i + 1 :]
  2734. ok = k
  2735. k = k.replace("-", "_")
  2736. if not hasattr(self, k):
  2737. if ignore_unknown is True or ok in ignore_unknown:
  2738. passthru_options.append(orig_a)
  2739. continue
  2740. else:
  2741. raise self.UnknownOptionError("Unknown option '%s'" % a)
  2742. ov = getattr(self, k)
  2743. if v == "?":
  2744. print("Current setting for '%s' is: %s" % (ok, ov))
  2745. continue
  2746. if isinstance(ov, bool):
  2747. v = bool(v)
  2748. elif isinstance(ov, int):
  2749. v = int(v)
  2750. elif isinstance(ov, str):
  2751. v = str(v) # redundant
  2752. elif isinstance(ov, list):
  2753. if isinstance(v, bool):
  2754. raise self.OptionError(
  2755. "Option '%s' requires values to be specified using '='" % a
  2756. )
  2757. vv = v.replace(",", " ").split()
  2758. if vv == [""]:
  2759. vv = []
  2760. vv = [int(x, 0) if len(x) and x[0] in "0123456789" else x for x in vv]
  2761. if op == "=":
  2762. v = vv
  2763. elif op == "+=":
  2764. v = ov
  2765. v.extend(vv)
  2766. elif op == "-=":
  2767. v = ov
  2768. for x in vv:
  2769. if x in v:
  2770. v.remove(x)
  2771. else:
  2772. assert False
  2773. setattr(self, k, v)
  2774. return posargs + passthru_options
  2775. class Subsetter(object):
  2776. class SubsettingError(Exception):
  2777. pass
  2778. class MissingGlyphsSubsettingError(SubsettingError):
  2779. pass
  2780. class MissingUnicodesSubsettingError(SubsettingError):
  2781. pass
  2782. def __init__(self, options=None):
  2783. if not options:
  2784. options = Options()
  2785. self.options = options
  2786. self.unicodes_requested = set()
  2787. self.glyph_names_requested = set()
  2788. self.glyph_ids_requested = set()
  2789. def populate(self, glyphs=[], gids=[], unicodes=[], text=""):
  2790. self.unicodes_requested.update(unicodes)
  2791. if isinstance(text, bytes):
  2792. text = text.decode("utf_8")
  2793. text_utf32 = text.encode("utf-32-be")
  2794. nchars = len(text_utf32) // 4
  2795. for u in struct.unpack(">%dL" % nchars, text_utf32):
  2796. self.unicodes_requested.add(u)
  2797. self.glyph_names_requested.update(glyphs)
  2798. self.glyph_ids_requested.update(gids)
  2799. def _prune_pre_subset(self, font):
  2800. for tag in self._sort_tables(font):
  2801. if (
  2802. tag.strip() in self.options.drop_tables
  2803. or (
  2804. tag.strip() in self.options.hinting_tables
  2805. and not self.options.hinting
  2806. )
  2807. or (tag == "kern" and (not self.options.legacy_kern and "GPOS" in font))
  2808. ):
  2809. log.info("%s dropped", tag)
  2810. del font[tag]
  2811. continue
  2812. clazz = ttLib.getTableClass(tag)
  2813. if hasattr(clazz, "prune_pre_subset"):
  2814. with timer("load '%s'" % tag):
  2815. table = font[tag]
  2816. with timer("prune '%s'" % tag):
  2817. retain = table.prune_pre_subset(font, self.options)
  2818. if not retain:
  2819. log.info("%s pruned to empty; dropped", tag)
  2820. del font[tag]
  2821. continue
  2822. else:
  2823. log.info("%s pruned", tag)
  2824. def _closure_glyphs(self, font):
  2825. realGlyphs = set(font.getGlyphOrder())
  2826. self.orig_glyph_order = glyph_order = font.getGlyphOrder()
  2827. self.glyphs_requested = set()
  2828. self.glyphs_requested.update(self.glyph_names_requested)
  2829. self.glyphs_requested.update(
  2830. glyph_order[i] for i in self.glyph_ids_requested if i < len(glyph_order)
  2831. )
  2832. self.glyphs_missing = set()
  2833. self.glyphs_missing.update(self.glyphs_requested.difference(realGlyphs))
  2834. self.glyphs_missing.update(
  2835. i for i in self.glyph_ids_requested if i >= len(glyph_order)
  2836. )
  2837. if self.glyphs_missing:
  2838. log.info("Missing requested glyphs: %s", self.glyphs_missing)
  2839. if not self.options.ignore_missing_glyphs:
  2840. raise self.MissingGlyphsSubsettingError(self.glyphs_missing)
  2841. self.glyphs = self.glyphs_requested.copy()
  2842. self.unicodes_missing = set()
  2843. if "cmap" in font:
  2844. with timer("close glyph list over 'cmap'"):
  2845. font["cmap"].closure_glyphs(self)
  2846. self.glyphs.intersection_update(realGlyphs)
  2847. self.glyphs_cmaped = frozenset(self.glyphs)
  2848. if self.unicodes_missing:
  2849. missing = ["U+%04X" % u for u in self.unicodes_missing]
  2850. log.info("Missing glyphs for requested Unicodes: %s", missing)
  2851. if not self.options.ignore_missing_unicodes:
  2852. raise self.MissingUnicodesSubsettingError(missing)
  2853. del missing
  2854. if self.options.notdef_glyph:
  2855. if "glyf" in font:
  2856. self.glyphs.add(font.getGlyphName(0))
  2857. log.info("Added gid0 to subset")
  2858. else:
  2859. self.glyphs.add(".notdef")
  2860. log.info("Added .notdef to subset")
  2861. if self.options.recommended_glyphs:
  2862. if "glyf" in font:
  2863. for i in range(min(4, len(font.getGlyphOrder()))):
  2864. self.glyphs.add(font.getGlyphName(i))
  2865. log.info("Added first four glyphs to subset")
  2866. if "MATH" in font:
  2867. with timer("close glyph list over 'MATH'"):
  2868. log.info(
  2869. "Closing glyph list over 'MATH': %d glyphs before", len(self.glyphs)
  2870. )
  2871. log.glyphs(self.glyphs, font=font)
  2872. font["MATH"].closure_glyphs(self)
  2873. self.glyphs.intersection_update(realGlyphs)
  2874. log.info(
  2875. "Closed glyph list over 'MATH': %d glyphs after", len(self.glyphs)
  2876. )
  2877. log.glyphs(self.glyphs, font=font)
  2878. self.glyphs_mathed = frozenset(self.glyphs)
  2879. if self.options.layout_closure and "GSUB" in font:
  2880. with timer("close glyph list over 'GSUB'"):
  2881. log.info(
  2882. "Closing glyph list over 'GSUB': %d glyphs before", len(self.glyphs)
  2883. )
  2884. log.glyphs(self.glyphs, font=font)
  2885. font["GSUB"].closure_glyphs(self)
  2886. self.glyphs.intersection_update(realGlyphs)
  2887. log.info(
  2888. "Closed glyph list over 'GSUB': %d glyphs after", len(self.glyphs)
  2889. )
  2890. log.glyphs(self.glyphs, font=font)
  2891. self.glyphs_gsubed = frozenset(self.glyphs)
  2892. for table in ("COLR", "bsln"):
  2893. if table in font:
  2894. with timer("close glyph list over '%s'" % table):
  2895. log.info(
  2896. "Closing glyph list over '%s': %d glyphs before",
  2897. table,
  2898. len(self.glyphs),
  2899. )
  2900. log.glyphs(self.glyphs, font=font)
  2901. font[table].closure_glyphs(self)
  2902. self.glyphs.intersection_update(realGlyphs)
  2903. log.info(
  2904. "Closed glyph list over '%s': %d glyphs after",
  2905. table,
  2906. len(self.glyphs),
  2907. )
  2908. log.glyphs(self.glyphs, font=font)
  2909. setattr(self, f"glyphs_{table.lower()}ed", frozenset(self.glyphs))
  2910. if "VARC" in font:
  2911. with timer("close glyph list over 'VARC'"):
  2912. log.info(
  2913. "Closing glyph list over 'VARC': %d glyphs before", len(self.glyphs)
  2914. )
  2915. log.glyphs(self.glyphs, font=font)
  2916. font["VARC"].closure_glyphs(self)
  2917. self.glyphs.intersection_update(realGlyphs)
  2918. log.info(
  2919. "Closed glyph list over 'VARC': %d glyphs after", len(self.glyphs)
  2920. )
  2921. log.glyphs(self.glyphs, font=font)
  2922. self.glyphs_glyfed = frozenset(self.glyphs)
  2923. if "glyf" in font:
  2924. with timer("close glyph list over 'glyf'"):
  2925. log.info(
  2926. "Closing glyph list over 'glyf': %d glyphs before", len(self.glyphs)
  2927. )
  2928. log.glyphs(self.glyphs, font=font)
  2929. font["glyf"].closure_glyphs(self)
  2930. self.glyphs.intersection_update(realGlyphs)
  2931. log.info(
  2932. "Closed glyph list over 'glyf': %d glyphs after", len(self.glyphs)
  2933. )
  2934. log.glyphs(self.glyphs, font=font)
  2935. self.glyphs_glyfed = frozenset(self.glyphs)
  2936. if "CFF " in font:
  2937. with timer("close glyph list over 'CFF '"):
  2938. log.info(
  2939. "Closing glyph list over 'CFF ': %d glyphs before", len(self.glyphs)
  2940. )
  2941. log.glyphs(self.glyphs, font=font)
  2942. font["CFF "].closure_glyphs(self)
  2943. self.glyphs.intersection_update(realGlyphs)
  2944. log.info(
  2945. "Closed glyph list over 'CFF ': %d glyphs after", len(self.glyphs)
  2946. )
  2947. log.glyphs(self.glyphs, font=font)
  2948. self.glyphs_cffed = frozenset(self.glyphs)
  2949. self.glyphs_retained = frozenset(self.glyphs)
  2950. order = font.getReverseGlyphMap()
  2951. self.reverseOrigGlyphMap = {g: order[g] for g in self.glyphs_retained}
  2952. self.last_retained_order = max(self.reverseOrigGlyphMap.values())
  2953. self.last_retained_glyph = font.getGlyphOrder()[self.last_retained_order]
  2954. self.glyphs_emptied = frozenset()
  2955. if self.options.retain_gids:
  2956. self.glyphs_emptied = {
  2957. g
  2958. for g in realGlyphs - self.glyphs_retained
  2959. if order[g] <= self.last_retained_order
  2960. }
  2961. self.reverseEmptiedGlyphMap = {g: order[g] for g in self.glyphs_emptied}
  2962. if not self.options.retain_gids:
  2963. new_glyph_order = [g for g in glyph_order if g in self.glyphs_retained]
  2964. else:
  2965. new_glyph_order = [
  2966. g for g in glyph_order if font.getGlyphID(g) <= self.last_retained_order
  2967. ]
  2968. # We'll call font.setGlyphOrder() at the end of _subset_glyphs when all
  2969. # tables have been subsetted. Below, we use the new glyph order to get
  2970. # a map from old to new glyph indices, which can be useful when
  2971. # subsetting individual tables (e.g. SVG) that refer to GIDs.
  2972. self.new_glyph_order = new_glyph_order
  2973. self.glyph_index_map = {
  2974. order[new_glyph_order[i]]: i for i in range(len(new_glyph_order))
  2975. }
  2976. log.info("Retaining %d glyphs", len(self.glyphs_retained))
  2977. del self.glyphs
  2978. def _subset_glyphs(self, font):
  2979. self.used_mark_sets = []
  2980. for tag in self._sort_tables(font):
  2981. clazz = ttLib.getTableClass(tag)
  2982. if tag.strip() in self.options.no_subset_tables:
  2983. log.info("%s subsetting not needed", tag)
  2984. elif hasattr(clazz, "subset_glyphs"):
  2985. with timer("subset '%s'" % tag):
  2986. table = font[tag]
  2987. self.glyphs = self.glyphs_retained
  2988. retain = table.subset_glyphs(self)
  2989. del self.glyphs
  2990. if not retain:
  2991. log.info("%s subsetted to empty; dropped", tag)
  2992. del font[tag]
  2993. else:
  2994. log.info("%s subsetted", tag)
  2995. elif self.options.passthrough_tables:
  2996. log.info("%s NOT subset; don't know how to subset", tag)
  2997. else:
  2998. log.warning("%s NOT subset; don't know how to subset; dropped", tag)
  2999. del font[tag]
  3000. with timer("subset GlyphOrder"):
  3001. font.setGlyphOrder(self.new_glyph_order)
  3002. def _prune_post_subset(self, font):
  3003. tableTags = font.keys()
  3004. # Prune the name table last because when we're pruning the name table,
  3005. # we visit each table in the font to see what name table records are
  3006. # still in use.
  3007. if "name" in tableTags:
  3008. tableTags.remove("name")
  3009. tableTags.append("name")
  3010. for tag in tableTags:
  3011. if tag == "GlyphOrder":
  3012. continue
  3013. if tag == "OS/2":
  3014. if self.options.prune_unicode_ranges:
  3015. old_uniranges = font[tag].getUnicodeRanges()
  3016. new_uniranges = font[tag].recalcUnicodeRanges(font, pruneOnly=True)
  3017. if old_uniranges != new_uniranges:
  3018. log.info(
  3019. "%s Unicode ranges pruned: %s", tag, sorted(new_uniranges)
  3020. )
  3021. if self.options.prune_codepage_ranges and font[tag].version >= 1:
  3022. # codepage range fields were added with OS/2 format 1
  3023. # https://learn.microsoft.com/en-us/typography/opentype/spec/os2#version-1
  3024. old_codepages = font[tag].getCodePageRanges()
  3025. new_codepages = font[tag].recalcCodePageRanges(font, pruneOnly=True)
  3026. if old_codepages != new_codepages:
  3027. log.info(
  3028. "%s CodePage ranges pruned: %s",
  3029. tag,
  3030. sorted(new_codepages),
  3031. )
  3032. if self.options.recalc_average_width:
  3033. old_avg_width = font[tag].xAvgCharWidth
  3034. new_avg_width = font[tag].recalcAvgCharWidth(font)
  3035. if old_avg_width != new_avg_width:
  3036. log.info("%s xAvgCharWidth updated: %d", tag, new_avg_width)
  3037. if self.options.recalc_max_context:
  3038. max_context = maxCtxFont(font)
  3039. if max_context != font[tag].usMaxContext:
  3040. font[tag].usMaxContext = max_context
  3041. log.info("%s usMaxContext updated: %d", tag, max_context)
  3042. clazz = ttLib.getTableClass(tag)
  3043. if hasattr(clazz, "prune_post_subset"):
  3044. with timer("prune '%s'" % tag):
  3045. table = font[tag]
  3046. retain = table.prune_post_subset(font, self.options)
  3047. if not retain:
  3048. log.info("%s pruned to empty; dropped", tag)
  3049. del font[tag]
  3050. else:
  3051. log.info("%s pruned", tag)
  3052. def _sort_tables(self, font):
  3053. tagOrder = ["GDEF", "GPOS", "GSUB", "fvar", "avar", "gvar", "name", "glyf"]
  3054. tagOrder = {t: i + 1 for i, t in enumerate(tagOrder)}
  3055. tags = sorted(font.keys(), key=lambda tag: tagOrder.get(tag, 0))
  3056. return [t for t in tags if t != "GlyphOrder"]
  3057. def subset(self, font):
  3058. self._prune_pre_subset(font)
  3059. self._closure_glyphs(font)
  3060. self._subset_glyphs(font)
  3061. self._prune_post_subset(font)
  3062. @timer("load font")
  3063. def load_font(fontFile, options, checkChecksums=0, dontLoadGlyphNames=False, lazy=True):
  3064. font = ttLib.TTFont(
  3065. fontFile,
  3066. checkChecksums=checkChecksums,
  3067. recalcBBoxes=options.recalc_bounds,
  3068. recalcTimestamp=options.recalc_timestamp,
  3069. lazy=lazy,
  3070. fontNumber=options.font_number,
  3071. )
  3072. # Hack:
  3073. #
  3074. # If we don't need glyph names, change 'post' class to not try to
  3075. # load them. It avoid lots of headache with broken fonts as well
  3076. # as loading time.
  3077. #
  3078. # Ideally ttLib should provide a way to ask it to skip loading
  3079. # glyph names. But it currently doesn't provide such a thing.
  3080. #
  3081. if dontLoadGlyphNames:
  3082. post = ttLib.getTableClass("post")
  3083. saved = post.decode_format_2_0
  3084. post.decode_format_2_0 = post.decode_format_3_0
  3085. f = font["post"]
  3086. if f.formatType == 2.0:
  3087. f.formatType = 3.0
  3088. post.decode_format_2_0 = saved
  3089. return font
  3090. @timer("compile and save font")
  3091. def save_font(font, outfile, options):
  3092. if options.with_zopfli and options.flavor == "woff":
  3093. from fontTools.ttLib import sfnt
  3094. sfnt.USE_ZOPFLI = True
  3095. font.flavor = options.flavor
  3096. font.cfg[USE_HARFBUZZ_REPACKER] = options.harfbuzz_repacker
  3097. font.save(outfile, reorderTables=options.canonical_order)
  3098. def parse_unicodes(s):
  3099. import re
  3100. s = re.sub(r"0[xX]", " ", s)
  3101. s = re.sub(r"[<+>,;&#\\xXuU\n ]", " ", s)
  3102. l = []
  3103. for item in s.split():
  3104. fields = item.split("-")
  3105. if len(fields) == 1:
  3106. l.append(int(item, 16))
  3107. else:
  3108. start, end = fields
  3109. l.extend(range(int(start, 16), int(end, 16) + 1))
  3110. return l
  3111. def parse_gids(s):
  3112. l = []
  3113. for item in s.replace(",", " ").split():
  3114. fields = item.split("-")
  3115. if len(fields) == 1:
  3116. l.append(int(fields[0]))
  3117. else:
  3118. l.extend(range(int(fields[0]), int(fields[1]) + 1))
  3119. return l
  3120. def parse_glyphs(s):
  3121. return s.replace(",", " ").split()
  3122. def usage():
  3123. print("usage:", __usage__, file=sys.stderr)
  3124. print("Try pyftsubset --help for more information.\n", file=sys.stderr)
  3125. @timer("make one with everything (TOTAL TIME)")
  3126. def main(args=None):
  3127. """OpenType font subsetter and optimizer"""
  3128. from os.path import splitext
  3129. from fontTools import configLogger
  3130. if args is None:
  3131. args = sys.argv[1:]
  3132. if "--help" in args:
  3133. print(__doc__)
  3134. return 0
  3135. options = Options()
  3136. try:
  3137. args = options.parse_opts(
  3138. args,
  3139. ignore_unknown=[
  3140. "gids",
  3141. "gids-file",
  3142. "glyphs",
  3143. "glyphs-file",
  3144. "text",
  3145. "text-file",
  3146. "unicodes",
  3147. "unicodes-file",
  3148. "output-file",
  3149. ],
  3150. )
  3151. except options.OptionError as e:
  3152. usage()
  3153. print("ERROR:", e, file=sys.stderr)
  3154. return 2
  3155. if len(args) < 2:
  3156. usage()
  3157. return 1
  3158. configLogger(level=logging.INFO if options.verbose else logging.WARNING)
  3159. if options.timing:
  3160. timer.logger.setLevel(logging.DEBUG)
  3161. else:
  3162. timer.logger.disabled = True
  3163. fontfile = args[0]
  3164. args = args[1:]
  3165. subsetter = Subsetter(options=options)
  3166. outfile = None
  3167. glyphs = []
  3168. gids = []
  3169. unicodes = []
  3170. wildcard_glyphs = False
  3171. wildcard_unicodes = False
  3172. text = ""
  3173. for g in args:
  3174. if g == "*":
  3175. wildcard_glyphs = True
  3176. continue
  3177. if g.startswith("--output-file="):
  3178. outfile = g[14:]
  3179. continue
  3180. if g.startswith("--text="):
  3181. text += g[7:]
  3182. continue
  3183. if g.startswith("--text-file="):
  3184. with open(g[12:], encoding="utf-8") as f:
  3185. text += f.read().replace("\n", "")
  3186. continue
  3187. if g.startswith("--unicodes="):
  3188. if g[11:] == "*":
  3189. wildcard_unicodes = True
  3190. else:
  3191. unicodes.extend(parse_unicodes(g[11:]))
  3192. continue
  3193. if g.startswith("--unicodes-file="):
  3194. with open(g[16:]) as f:
  3195. for line in f.readlines():
  3196. unicodes.extend(parse_unicodes(line.split("#")[0]))
  3197. continue
  3198. if g.startswith("--gids="):
  3199. gids.extend(parse_gids(g[7:]))
  3200. continue
  3201. if g.startswith("--gids-file="):
  3202. with open(g[12:]) as f:
  3203. for line in f.readlines():
  3204. gids.extend(parse_gids(line.split("#")[0]))
  3205. continue
  3206. if g.startswith("--glyphs="):
  3207. if g[9:] == "*":
  3208. wildcard_glyphs = True
  3209. else:
  3210. glyphs.extend(parse_glyphs(g[9:]))
  3211. continue
  3212. if g.startswith("--glyphs-file="):
  3213. with open(g[14:]) as f:
  3214. for line in f.readlines():
  3215. glyphs.extend(parse_glyphs(line.split("#")[0]))
  3216. continue
  3217. glyphs.append(g)
  3218. dontLoadGlyphNames = not options.glyph_names and not glyphs
  3219. lazy = options.lazy
  3220. font = load_font(
  3221. fontfile, options, dontLoadGlyphNames=dontLoadGlyphNames, lazy=lazy
  3222. )
  3223. if outfile is None:
  3224. ext = "." + options.flavor.lower() if options.flavor is not None else None
  3225. outfile = makeOutputFileName(
  3226. fontfile, extension=ext, overWrite=True, suffix=".subset"
  3227. )
  3228. with timer("compile glyph list"):
  3229. if wildcard_glyphs:
  3230. glyphs.extend(font.getGlyphOrder())
  3231. if wildcard_unicodes:
  3232. for t in font["cmap"].tables:
  3233. if t.isUnicode():
  3234. unicodes.extend(t.cmap.keys())
  3235. if t.format == 14:
  3236. unicodes.extend(t.uvsDict.keys())
  3237. assert "" not in glyphs
  3238. log.info("Text: '%s'" % text)
  3239. log.info("Unicodes: %s", unicodes)
  3240. log.info("Glyphs: %s", glyphs)
  3241. log.info("Gids: %s", gids)
  3242. subsetter.populate(glyphs=glyphs, gids=gids, unicodes=unicodes, text=text)
  3243. subsetter.subset(font)
  3244. save_font(font, outfile, options)
  3245. if options.verbose:
  3246. import os
  3247. log.info("Input font:% 7d bytes: %s" % (os.path.getsize(fontfile), fontfile))
  3248. log.info("Subset font:% 7d bytes: %s" % (os.path.getsize(outfile), outfile))
  3249. if options.xml:
  3250. font.saveXML(sys.stdout)
  3251. font.close()
  3252. __all__ = [
  3253. "Options",
  3254. "Subsetter",
  3255. "load_font",
  3256. "save_font",
  3257. "parse_gids",
  3258. "parse_glyphs",
  3259. "parse_unicodes",
  3260. "main",
  3261. ]