__init__.py 127 KB

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