validators.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. """Various low level data validators."""
  2. from __future__ import annotations
  3. import calendar
  4. from collections.abc import Mapping, Sequence
  5. from io import open
  6. import fontTools.misc.filesystem as fs
  7. from typing import Any, Type, Optional, Union
  8. from fontTools.annotations import IntFloat
  9. from fontTools.ufoLib.utils import numberTypes
  10. GenericDict = dict[str, tuple[Union[type, tuple[Type[Any], ...]], bool]]
  11. # -------
  12. # Generic
  13. # -------
  14. def isDictEnough(value: Any) -> bool:
  15. """
  16. Some objects will likely come in that aren't
  17. dicts but are dict-ish enough.
  18. """
  19. if isinstance(value, Mapping):
  20. return True
  21. for attr in ("keys", "values", "items"):
  22. if not hasattr(value, attr):
  23. return False
  24. return True
  25. def genericTypeValidator(value: Any, typ: Type[Any]) -> bool:
  26. """
  27. Generic. (Added at version 2.)
  28. """
  29. return isinstance(value, typ)
  30. def genericIntListValidator(values: Any, validValues: Sequence[int]) -> bool:
  31. """
  32. Generic. (Added at version 2.)
  33. """
  34. if not isinstance(values, (list, tuple)):
  35. return False
  36. valuesSet = set(values)
  37. validValuesSet = set(validValues)
  38. if valuesSet - validValuesSet:
  39. return False
  40. for value in values:
  41. if not isinstance(value, int):
  42. return False
  43. return True
  44. def genericNonNegativeIntValidator(value: Any) -> bool:
  45. """
  46. Generic. (Added at version 3.)
  47. """
  48. if not isinstance(value, int):
  49. return False
  50. if value < 0:
  51. return False
  52. return True
  53. def genericNonNegativeNumberValidator(value: Any) -> bool:
  54. """
  55. Generic. (Added at version 3.)
  56. """
  57. if not isinstance(value, numberTypes):
  58. return False
  59. if value < 0:
  60. return False
  61. return True
  62. def genericDictValidator(value: Any, prototype: GenericDict) -> bool:
  63. """
  64. Generic. (Added at version 3.)
  65. """
  66. # not a dict
  67. if not isinstance(value, Mapping):
  68. return False
  69. # missing required keys
  70. for key, (typ, required) in prototype.items():
  71. if not required:
  72. continue
  73. if key not in value:
  74. return False
  75. # unknown keys
  76. for key in value.keys():
  77. if key not in prototype:
  78. return False
  79. # incorrect types
  80. for key, v in value.items():
  81. prototypeType, required = prototype[key]
  82. if v is None and not required:
  83. continue
  84. if not isinstance(v, prototypeType):
  85. return False
  86. return True
  87. # --------------
  88. # fontinfo.plist
  89. # --------------
  90. # Data Validators
  91. def fontInfoStyleMapStyleNameValidator(value: Any) -> bool:
  92. """
  93. Version 2+.
  94. """
  95. options = ["regular", "italic", "bold", "bold italic"]
  96. return value in options
  97. def fontInfoOpenTypeGaspRangeRecordsValidator(value: Any) -> bool:
  98. """
  99. Version 3+.
  100. """
  101. if not isinstance(value, list):
  102. return False
  103. if len(value) == 0:
  104. return True
  105. validBehaviors = [0, 1, 2, 3]
  106. dictPrototype: GenericDict = dict(
  107. rangeMaxPPEM=(int, True), rangeGaspBehavior=(list, True)
  108. )
  109. ppemOrder = []
  110. for rangeRecord in value:
  111. if not genericDictValidator(rangeRecord, dictPrototype):
  112. return False
  113. ppem = rangeRecord["rangeMaxPPEM"]
  114. behavior = rangeRecord["rangeGaspBehavior"]
  115. ppemValidity = genericNonNegativeIntValidator(ppem)
  116. if not ppemValidity:
  117. return False
  118. behaviorValidity = genericIntListValidator(behavior, validBehaviors)
  119. if not behaviorValidity:
  120. return False
  121. ppemOrder.append(ppem)
  122. if ppemOrder != sorted(ppemOrder):
  123. return False
  124. return True
  125. def fontInfoOpenTypeHeadCreatedValidator(value: Any) -> bool:
  126. """
  127. Version 2+.
  128. """
  129. # format: 0000/00/00 00:00:00
  130. if not isinstance(value, str):
  131. return False
  132. # basic formatting
  133. if not len(value) == 19:
  134. return False
  135. if value.count(" ") != 1:
  136. return False
  137. strDate, strTime = value.split(" ")
  138. if strDate.count("/") != 2:
  139. return False
  140. if strTime.count(":") != 2:
  141. return False
  142. # date
  143. strYear, strMonth, strDay = strDate.split("/")
  144. if len(strYear) != 4:
  145. return False
  146. if len(strMonth) != 2:
  147. return False
  148. if len(strDay) != 2:
  149. return False
  150. try:
  151. intYear = int(strYear)
  152. intMonth = int(strMonth)
  153. intDay = int(strDay)
  154. except ValueError:
  155. return False
  156. if intMonth < 1 or intMonth > 12:
  157. return False
  158. monthMaxDay = calendar.monthrange(intYear, intMonth)[1]
  159. if intDay < 1 or intDay > monthMaxDay:
  160. return False
  161. # time
  162. strHour, strMinute, strSecond = strTime.split(":")
  163. if len(strHour) != 2:
  164. return False
  165. if len(strMinute) != 2:
  166. return False
  167. if len(strSecond) != 2:
  168. return False
  169. try:
  170. intHour = int(strHour)
  171. intMinute = int(strMinute)
  172. intSecond = int(strSecond)
  173. except ValueError:
  174. return False
  175. if intHour < 0 or intHour > 23:
  176. return False
  177. if intMinute < 0 or intMinute > 59:
  178. return False
  179. if intSecond < 0 or intSecond > 59:
  180. return False
  181. # fallback
  182. return True
  183. def fontInfoOpenTypeNameRecordsValidator(value: Any) -> bool:
  184. """
  185. Version 3+.
  186. """
  187. if not isinstance(value, list):
  188. return False
  189. dictPrototype: GenericDict = dict(
  190. nameID=(int, True),
  191. platformID=(int, True),
  192. encodingID=(int, True),
  193. languageID=(int, True),
  194. string=(str, True),
  195. )
  196. for nameRecord in value:
  197. if not genericDictValidator(nameRecord, dictPrototype):
  198. return False
  199. return True
  200. def fontInfoOpenTypeOS2WeightClassValidator(value: Any) -> bool:
  201. """
  202. Version 2+.
  203. """
  204. if not isinstance(value, int):
  205. return False
  206. if value < 0:
  207. return False
  208. return True
  209. def fontInfoOpenTypeOS2WidthClassValidator(value: Any) -> bool:
  210. """
  211. Version 2+.
  212. """
  213. if not isinstance(value, int):
  214. return False
  215. if value < 1:
  216. return False
  217. if value > 9:
  218. return False
  219. return True
  220. def fontInfoVersion2OpenTypeOS2PanoseValidator(values: Any) -> bool:
  221. """
  222. Version 2.
  223. """
  224. if not isinstance(values, (list, tuple)):
  225. return False
  226. if len(values) != 10:
  227. return False
  228. for value in values:
  229. if not isinstance(value, int):
  230. return False
  231. # XXX further validation?
  232. return True
  233. def fontInfoVersion3OpenTypeOS2PanoseValidator(values: Any) -> bool:
  234. """
  235. Version 3+.
  236. """
  237. if not isinstance(values, (list, tuple)):
  238. return False
  239. if len(values) != 10:
  240. return False
  241. for value in values:
  242. if not isinstance(value, int):
  243. return False
  244. if value < 0:
  245. return False
  246. # XXX further validation?
  247. return True
  248. def fontInfoOpenTypeOS2FamilyClassValidator(values: Any) -> bool:
  249. """
  250. Version 2+.
  251. """
  252. if not isinstance(values, (list, tuple)):
  253. return False
  254. if len(values) != 2:
  255. return False
  256. for value in values:
  257. if not isinstance(value, int):
  258. return False
  259. classID, subclassID = values
  260. if classID < 0 or classID > 14:
  261. return False
  262. if subclassID < 0 or subclassID > 15:
  263. return False
  264. return True
  265. def fontInfoPostscriptBluesValidator(values: Any) -> bool:
  266. """
  267. Version 2+.
  268. """
  269. if not isinstance(values, (list, tuple)):
  270. return False
  271. if len(values) > 14:
  272. return False
  273. if len(values) % 2:
  274. return False
  275. for value in values:
  276. if not isinstance(value, numberTypes):
  277. return False
  278. return True
  279. def fontInfoPostscriptOtherBluesValidator(values: Any) -> bool:
  280. """
  281. Version 2+.
  282. """
  283. if not isinstance(values, (list, tuple)):
  284. return False
  285. if len(values) > 10:
  286. return False
  287. if len(values) % 2:
  288. return False
  289. for value in values:
  290. if not isinstance(value, numberTypes):
  291. return False
  292. return True
  293. def fontInfoPostscriptStemsValidator(values: Any) -> bool:
  294. """
  295. Version 2+.
  296. """
  297. if not isinstance(values, (list, tuple)):
  298. return False
  299. if len(values) > 12:
  300. return False
  301. for value in values:
  302. if not isinstance(value, numberTypes):
  303. return False
  304. return True
  305. def fontInfoPostscriptWindowsCharacterSetValidator(value: Any) -> bool:
  306. """
  307. Version 2+.
  308. """
  309. validValues = list(range(1, 21))
  310. if value not in validValues:
  311. return False
  312. return True
  313. def fontInfoWOFFMetadataUniqueIDValidator(value: Any) -> bool:
  314. """
  315. Version 3+.
  316. """
  317. dictPrototype: GenericDict = dict(id=(str, True))
  318. if not genericDictValidator(value, dictPrototype):
  319. return False
  320. return True
  321. def fontInfoWOFFMetadataVendorValidator(value: Any) -> bool:
  322. """
  323. Version 3+.
  324. """
  325. dictPrototype: GenericDict = {
  326. "name": (str, True),
  327. "url": (str, False),
  328. "dir": (str, False),
  329. "class": (str, False),
  330. }
  331. if not genericDictValidator(value, dictPrototype):
  332. return False
  333. if "dir" in value and value.get("dir") not in ("ltr", "rtl"):
  334. return False
  335. return True
  336. def fontInfoWOFFMetadataCreditsValidator(value: Any) -> bool:
  337. """
  338. Version 3+.
  339. """
  340. dictPrototype: GenericDict = dict(credits=(list, True))
  341. if not genericDictValidator(value, dictPrototype):
  342. return False
  343. if not len(value["credits"]):
  344. return False
  345. dictPrototype = {
  346. "name": (str, True),
  347. "url": (str, False),
  348. "role": (str, False),
  349. "dir": (str, False),
  350. "class": (str, False),
  351. }
  352. for credit in value["credits"]:
  353. if not genericDictValidator(credit, dictPrototype):
  354. return False
  355. if "dir" in credit and credit.get("dir") not in ("ltr", "rtl"):
  356. return False
  357. return True
  358. def fontInfoWOFFMetadataDescriptionValidator(value: Any) -> bool:
  359. """
  360. Version 3+.
  361. """
  362. dictPrototype: GenericDict = dict(url=(str, False), text=(list, True))
  363. if not genericDictValidator(value, dictPrototype):
  364. return False
  365. for text in value["text"]:
  366. if not fontInfoWOFFMetadataTextValue(text):
  367. return False
  368. return True
  369. def fontInfoWOFFMetadataLicenseValidator(value: Any) -> bool:
  370. """
  371. Version 3+.
  372. """
  373. dictPrototype: GenericDict = dict(
  374. url=(str, False), text=(list, False), id=(str, False)
  375. )
  376. if not genericDictValidator(value, dictPrototype):
  377. return False
  378. if "text" in value:
  379. for text in value["text"]:
  380. if not fontInfoWOFFMetadataTextValue(text):
  381. return False
  382. return True
  383. def fontInfoWOFFMetadataTrademarkValidator(value: Any) -> bool:
  384. """
  385. Version 3+.
  386. """
  387. dictPrototype: GenericDict = dict(text=(list, True))
  388. if not genericDictValidator(value, dictPrototype):
  389. return False
  390. for text in value["text"]:
  391. if not fontInfoWOFFMetadataTextValue(text):
  392. return False
  393. return True
  394. def fontInfoWOFFMetadataCopyrightValidator(value: Any) -> bool:
  395. """
  396. Version 3+.
  397. """
  398. dictPrototype: GenericDict = dict(text=(list, True))
  399. if not genericDictValidator(value, dictPrototype):
  400. return False
  401. for text in value["text"]:
  402. if not fontInfoWOFFMetadataTextValue(text):
  403. return False
  404. return True
  405. def fontInfoWOFFMetadataLicenseeValidator(value: Any) -> bool:
  406. """
  407. Version 3+.
  408. """
  409. dictPrototype: GenericDict = {
  410. "name": (str, True),
  411. "dir": (str, False),
  412. "class": (str, False),
  413. }
  414. if not genericDictValidator(value, dictPrototype):
  415. return False
  416. if "dir" in value and value.get("dir") not in ("ltr", "rtl"):
  417. return False
  418. return True
  419. def fontInfoWOFFMetadataTextValue(value: Any) -> bool:
  420. """
  421. Version 3+.
  422. """
  423. dictPrototype: GenericDict = {
  424. "text": (str, True),
  425. "language": (str, False),
  426. "dir": (str, False),
  427. "class": (str, False),
  428. }
  429. if not genericDictValidator(value, dictPrototype):
  430. return False
  431. if "dir" in value and value.get("dir") not in ("ltr", "rtl"):
  432. return False
  433. return True
  434. def fontInfoWOFFMetadataExtensionsValidator(value: Any) -> bool:
  435. """
  436. Version 3+.
  437. """
  438. if not isinstance(value, list):
  439. return False
  440. if not value:
  441. return False
  442. for extension in value:
  443. if not fontInfoWOFFMetadataExtensionValidator(extension):
  444. return False
  445. return True
  446. def fontInfoWOFFMetadataExtensionValidator(value: Any) -> bool:
  447. """
  448. Version 3+.
  449. """
  450. dictPrototype: GenericDict = dict(
  451. names=(list, False), items=(list, True), id=(str, False)
  452. )
  453. if not genericDictValidator(value, dictPrototype):
  454. return False
  455. if "names" in value:
  456. for name in value["names"]:
  457. if not fontInfoWOFFMetadataExtensionNameValidator(name):
  458. return False
  459. for item in value["items"]:
  460. if not fontInfoWOFFMetadataExtensionItemValidator(item):
  461. return False
  462. return True
  463. def fontInfoWOFFMetadataExtensionItemValidator(value: Any) -> bool:
  464. """
  465. Version 3+.
  466. """
  467. dictPrototype: GenericDict = dict(
  468. id=(str, False), names=(list, True), values=(list, True)
  469. )
  470. if not genericDictValidator(value, dictPrototype):
  471. return False
  472. for name in value["names"]:
  473. if not fontInfoWOFFMetadataExtensionNameValidator(name):
  474. return False
  475. for val in value["values"]:
  476. if not fontInfoWOFFMetadataExtensionValueValidator(val):
  477. return False
  478. return True
  479. def fontInfoWOFFMetadataExtensionNameValidator(value: Any) -> bool:
  480. """
  481. Version 3+.
  482. """
  483. dictPrototype: GenericDict = {
  484. "text": (str, True),
  485. "language": (str, False),
  486. "dir": (str, False),
  487. "class": (str, False),
  488. }
  489. if not genericDictValidator(value, dictPrototype):
  490. return False
  491. if "dir" in value and value.get("dir") not in ("ltr", "rtl"):
  492. return False
  493. return True
  494. def fontInfoWOFFMetadataExtensionValueValidator(value: Any) -> bool:
  495. """
  496. Version 3+.
  497. """
  498. dictPrototype: GenericDict = {
  499. "text": (str, True),
  500. "language": (str, False),
  501. "dir": (str, False),
  502. "class": (str, False),
  503. }
  504. if not genericDictValidator(value, dictPrototype):
  505. return False
  506. if "dir" in value and value.get("dir") not in ("ltr", "rtl"):
  507. return False
  508. return True
  509. # ----------
  510. # Guidelines
  511. # ----------
  512. def guidelinesValidator(value: Any, identifiers: Optional[set[str]] = None) -> bool:
  513. """
  514. Version 3+.
  515. """
  516. if not isinstance(value, list):
  517. return False
  518. if identifiers is None:
  519. identifiers = set()
  520. for guide in value:
  521. if not guidelineValidator(guide):
  522. return False
  523. identifier = guide.get("identifier")
  524. if identifier is not None:
  525. if identifier in identifiers:
  526. return False
  527. identifiers.add(identifier)
  528. return True
  529. _guidelineDictPrototype: GenericDict = dict(
  530. x=((int, float), False),
  531. y=((int, float), False),
  532. angle=((int, float), False),
  533. name=(str, False),
  534. color=(str, False),
  535. identifier=(str, False),
  536. )
  537. def guidelineValidator(value: Any) -> bool:
  538. """
  539. Version 3+.
  540. """
  541. if not genericDictValidator(value, _guidelineDictPrototype):
  542. return False
  543. angle = value.get("angle")
  544. # angle must be between 0 and 360
  545. if angle is not None:
  546. if angle < 0:
  547. return False
  548. if angle > 360:
  549. return False
  550. # identifier must be 1 or more characters
  551. identifier = value.get("identifier")
  552. if identifier is not None and not identifierValidator(identifier):
  553. return False
  554. # color must follow the proper format
  555. color = value.get("color")
  556. if color is not None and not colorValidator(color):
  557. return False
  558. return True
  559. # -------
  560. # Anchors
  561. # -------
  562. def anchorsValidator(value: Any, identifiers: Optional[set[str]] = None) -> bool:
  563. """
  564. Version 3+.
  565. """
  566. if not isinstance(value, list):
  567. return False
  568. if identifiers is None:
  569. identifiers = set()
  570. for anchor in value:
  571. if not anchorValidator(anchor):
  572. return False
  573. identifier = anchor.get("identifier")
  574. if identifier is not None:
  575. if identifier in identifiers:
  576. return False
  577. identifiers.add(identifier)
  578. return True
  579. _anchorDictPrototype: GenericDict = dict(
  580. x=((int, float), False),
  581. y=((int, float), False),
  582. name=(str, False),
  583. color=(str, False),
  584. identifier=(str, False),
  585. )
  586. def anchorValidator(value: Any) -> bool:
  587. """
  588. Version 3+.
  589. """
  590. if not genericDictValidator(value, _anchorDictPrototype):
  591. return False
  592. x = value.get("x")
  593. y = value.get("y")
  594. # x and y must be present
  595. if x is None or y is None:
  596. return False
  597. # identifier must be 1 or more characters
  598. identifier = value.get("identifier")
  599. if identifier is not None and not identifierValidator(identifier):
  600. return False
  601. # color must follow the proper format
  602. color = value.get("color")
  603. if color is not None and not colorValidator(color):
  604. return False
  605. return True
  606. # ----------
  607. # Identifier
  608. # ----------
  609. def identifierValidator(value: Any) -> bool:
  610. """
  611. Version 3+.
  612. >>> identifierValidator("a")
  613. True
  614. >>> identifierValidator("")
  615. False
  616. >>> identifierValidator("a" * 101)
  617. False
  618. """
  619. validCharactersMin = 0x20
  620. validCharactersMax = 0x7E
  621. if not isinstance(value, str):
  622. return False
  623. if not value:
  624. return False
  625. if len(value) > 100:
  626. return False
  627. for c in value:
  628. i = ord(c)
  629. if i < validCharactersMin or i > validCharactersMax:
  630. return False
  631. return True
  632. # -----
  633. # Color
  634. # -----
  635. def colorValidator(value: Any) -> bool:
  636. """
  637. Version 3+.
  638. >>> colorValidator("0,0,0,0")
  639. True
  640. >>> colorValidator(".5,.5,.5,.5")
  641. True
  642. >>> colorValidator("0.5,0.5,0.5,0.5")
  643. True
  644. >>> colorValidator("1,1,1,1")
  645. True
  646. >>> colorValidator("2,0,0,0")
  647. False
  648. >>> colorValidator("0,2,0,0")
  649. False
  650. >>> colorValidator("0,0,2,0")
  651. False
  652. >>> colorValidator("0,0,0,2")
  653. False
  654. >>> colorValidator("1r,1,1,1")
  655. False
  656. >>> colorValidator("1,1g,1,1")
  657. False
  658. >>> colorValidator("1,1,1b,1")
  659. False
  660. >>> colorValidator("1,1,1,1a")
  661. False
  662. >>> colorValidator("1 1 1 1")
  663. False
  664. >>> colorValidator("1 1,1,1")
  665. False
  666. >>> colorValidator("1,1 1,1")
  667. False
  668. >>> colorValidator("1,1,1 1")
  669. False
  670. >>> colorValidator("1, 1, 1, 1")
  671. True
  672. """
  673. if not isinstance(value, str):
  674. return False
  675. parts = value.split(",")
  676. if len(parts) != 4:
  677. return False
  678. for part in parts:
  679. part = part.strip()
  680. converted = False
  681. number: IntFloat
  682. try:
  683. number = int(part)
  684. converted = True
  685. except ValueError:
  686. pass
  687. if not converted:
  688. try:
  689. number = float(part)
  690. converted = True
  691. except ValueError:
  692. pass
  693. if not converted:
  694. return False
  695. if not 0 <= number <= 1:
  696. return False
  697. return True
  698. # -----
  699. # image
  700. # -----
  701. pngSignature: bytes = b"\x89PNG\r\n\x1a\n"
  702. _imageDictPrototype: GenericDict = dict(
  703. fileName=(str, True),
  704. xScale=((int, float), False),
  705. xyScale=((int, float), False),
  706. yxScale=((int, float), False),
  707. yScale=((int, float), False),
  708. xOffset=((int, float), False),
  709. yOffset=((int, float), False),
  710. color=(str, False),
  711. )
  712. def imageValidator(value):
  713. """
  714. Version 3+.
  715. """
  716. if not genericDictValidator(value, _imageDictPrototype):
  717. return False
  718. # fileName must be one or more characters
  719. if not value["fileName"]:
  720. return False
  721. # color must follow the proper format
  722. color = value.get("color")
  723. if color is not None and not colorValidator(color):
  724. return False
  725. return True
  726. def pngValidator(
  727. path: Optional[str] = None,
  728. data: Optional[bytes] = None,
  729. fileObj: Optional[Any] = None,
  730. ) -> tuple[bool, Any]:
  731. """
  732. Version 3+.
  733. This checks the signature of the image data.
  734. """
  735. assert path is not None or data is not None or fileObj is not None
  736. if path is not None:
  737. with open(path, "rb") as f:
  738. signature = f.read(8)
  739. elif data is not None:
  740. signature = data[:8]
  741. elif fileObj is not None:
  742. pos = fileObj.tell()
  743. signature = fileObj.read(8)
  744. fileObj.seek(pos)
  745. if signature != pngSignature:
  746. return False, "Image does not begin with the PNG signature."
  747. return True, None
  748. # -------------------
  749. # layercontents.plist
  750. # -------------------
  751. def layerContentsValidator(
  752. value: Any, ufoPathOrFileSystem: Union[str, fs.base.FS]
  753. ) -> tuple[bool, Optional[str]]:
  754. """
  755. Check the validity of layercontents.plist.
  756. Version 3+.
  757. """
  758. if isinstance(ufoPathOrFileSystem, fs.base.FS):
  759. fileSystem = ufoPathOrFileSystem
  760. else:
  761. fileSystem = fs.osfs.OSFS(ufoPathOrFileSystem)
  762. bogusFileMessage = "layercontents.plist in not in the correct format."
  763. # file isn't in the right format
  764. if not isinstance(value, list):
  765. return False, bogusFileMessage
  766. # work through each entry
  767. usedLayerNames = set()
  768. usedDirectories = set()
  769. contents = {}
  770. for entry in value:
  771. # layer entry in the incorrect format
  772. if not isinstance(entry, list):
  773. return False, bogusFileMessage
  774. if not len(entry) == 2:
  775. return False, bogusFileMessage
  776. for i in entry:
  777. if not isinstance(i, str):
  778. return False, bogusFileMessage
  779. layerName, directoryName = entry
  780. # check directory naming
  781. if directoryName != "glyphs":
  782. if not directoryName.startswith("glyphs."):
  783. return (
  784. False,
  785. "Invalid directory name (%s) in layercontents.plist."
  786. % directoryName,
  787. )
  788. if len(layerName) == 0:
  789. return False, "Empty layer name in layercontents.plist."
  790. # directory doesn't exist
  791. if not fileSystem.exists(directoryName):
  792. return False, "A glyphset does not exist at %s." % directoryName
  793. # default layer name
  794. if layerName == "public.default" and directoryName != "glyphs":
  795. return (
  796. False,
  797. "The name public.default is being used by a layer that is not the default.",
  798. )
  799. # check usage
  800. if layerName in usedLayerNames:
  801. return (
  802. False,
  803. "The layer name %s is used by more than one layer." % layerName,
  804. )
  805. usedLayerNames.add(layerName)
  806. if directoryName in usedDirectories:
  807. return (
  808. False,
  809. "The directory %s is used by more than one layer." % directoryName,
  810. )
  811. usedDirectories.add(directoryName)
  812. # store
  813. contents[layerName] = directoryName
  814. # missing default layer
  815. foundDefault = "glyphs" in contents.values()
  816. if not foundDefault:
  817. return False, "The required default glyph set is not in the UFO."
  818. return True, None
  819. # ------------
  820. # groups.plist
  821. # ------------
  822. def groupsValidator(value: Any) -> tuple[bool, Optional[str]]:
  823. """
  824. Check the validity of the groups.
  825. Version 3+ (though it's backwards compatible with UFO 1 and UFO 2).
  826. >>> groups = {"A" : ["A", "A"], "A2" : ["A"]}
  827. >>> groupsValidator(groups)
  828. (True, None)
  829. >>> groups = {"" : ["A"]}
  830. >>> valid, msg = groupsValidator(groups)
  831. >>> valid
  832. False
  833. >>> print(msg)
  834. A group has an empty name.
  835. >>> groups = {"public.awesome" : ["A"]}
  836. >>> groupsValidator(groups)
  837. (True, None)
  838. >>> groups = {"public.kern1." : ["A"]}
  839. >>> valid, msg = groupsValidator(groups)
  840. >>> valid
  841. False
  842. >>> print(msg)
  843. The group data contains a kerning group with an incomplete name.
  844. >>> groups = {"public.kern2." : ["A"]}
  845. >>> valid, msg = groupsValidator(groups)
  846. >>> valid
  847. False
  848. >>> print(msg)
  849. The group data contains a kerning group with an incomplete name.
  850. >>> groups = {"public.kern1.A" : ["A"], "public.kern2.A" : ["A"]}
  851. >>> groupsValidator(groups)
  852. (True, None)
  853. >>> groups = {"public.kern1.A1" : ["A"], "public.kern1.A2" : ["A"]}
  854. >>> valid, msg = groupsValidator(groups)
  855. >>> valid
  856. False
  857. >>> print(msg)
  858. The glyph "A" occurs in too many kerning groups.
  859. """
  860. bogusFormatMessage = "The group data is not in the correct format."
  861. if not isDictEnough(value):
  862. return False, bogusFormatMessage
  863. firstSideMapping: dict[str, str] = {}
  864. secondSideMapping: dict[str, str] = {}
  865. for groupName, glyphList in value.items():
  866. if not isinstance(groupName, (str)):
  867. return False, bogusFormatMessage
  868. if not isinstance(glyphList, (list, tuple)):
  869. return False, bogusFormatMessage
  870. if not groupName:
  871. return False, "A group has an empty name."
  872. if groupName.startswith("public."):
  873. if not groupName.startswith("public.kern1.") and not groupName.startswith(
  874. "public.kern2."
  875. ):
  876. # unknown public.* name. silently skip.
  877. continue
  878. else:
  879. if len("public.kernN.") == len(groupName):
  880. return (
  881. False,
  882. "The group data contains a kerning group with an incomplete name.",
  883. )
  884. if groupName.startswith("public.kern1."):
  885. d = firstSideMapping
  886. else:
  887. d = secondSideMapping
  888. for glyphName in glyphList:
  889. if not isinstance(glyphName, str):
  890. return (
  891. False,
  892. "The group data %s contains an invalid member." % groupName,
  893. )
  894. if glyphName in d:
  895. return (
  896. False,
  897. 'The glyph "%s" occurs in too many kerning groups.' % glyphName,
  898. )
  899. d[glyphName] = groupName
  900. return True, None
  901. # -------------
  902. # kerning.plist
  903. # -------------
  904. def kerningValidator(data: Any) -> tuple[bool, Optional[str]]:
  905. """
  906. Check the validity of the kerning data structure.
  907. Version 3+ (though it's backwards compatible with UFO 1 and UFO 2).
  908. >>> kerning = {"A" : {"B" : 100}}
  909. >>> kerningValidator(kerning)
  910. (True, None)
  911. >>> kerning = {"A" : ["B"]}
  912. >>> valid, msg = kerningValidator(kerning)
  913. >>> valid
  914. False
  915. >>> print(msg)
  916. The kerning data is not in the correct format.
  917. >>> kerning = {"A" : {"B" : "100"}}
  918. >>> valid, msg = kerningValidator(kerning)
  919. >>> valid
  920. False
  921. >>> print(msg)
  922. The kerning data is not in the correct format.
  923. """
  924. bogusFormatMessage = "The kerning data is not in the correct format."
  925. if not isinstance(data, Mapping):
  926. return False, bogusFormatMessage
  927. for first, secondDict in data.items():
  928. if not isinstance(first, str):
  929. return False, bogusFormatMessage
  930. elif not isinstance(secondDict, Mapping):
  931. return False, bogusFormatMessage
  932. for second, value in secondDict.items():
  933. if not isinstance(second, str):
  934. return False, bogusFormatMessage
  935. elif not isinstance(value, numberTypes):
  936. return False, bogusFormatMessage
  937. return True, None
  938. # -------------
  939. # lib.plist/lib
  940. # -------------
  941. _bogusLibFormatMessage = "The lib data is not in the correct format: %s"
  942. def fontLibValidator(value: Any) -> tuple[bool, Optional[str]]:
  943. """
  944. Check the validity of the lib.
  945. Version 3+ (though it's backwards compatible with UFO 1 and UFO 2).
  946. >>> lib = {"foo" : "bar"}
  947. >>> fontLibValidator(lib)
  948. (True, None)
  949. >>> lib = {"public.awesome" : "hello"}
  950. >>> fontLibValidator(lib)
  951. (True, None)
  952. >>> lib = {"public.glyphOrder" : ["A", "C", "B"]}
  953. >>> fontLibValidator(lib)
  954. (True, None)
  955. >>> lib = "hello"
  956. >>> valid, msg = fontLibValidator(lib)
  957. >>> valid
  958. False
  959. >>> print(msg) # doctest: +ELLIPSIS
  960. The lib data is not in the correct format: expected a dictionary, ...
  961. >>> lib = {1: "hello"}
  962. >>> valid, msg = fontLibValidator(lib)
  963. >>> valid
  964. False
  965. >>> print(msg)
  966. The lib key is not properly formatted: expected str, found int: 1
  967. >>> lib = {"public.glyphOrder" : "hello"}
  968. >>> valid, msg = fontLibValidator(lib)
  969. >>> valid
  970. False
  971. >>> print(msg) # doctest: +ELLIPSIS
  972. public.glyphOrder is not properly formatted: expected list or tuple,...
  973. >>> lib = {"public.glyphOrder" : ["A", 1, "B"]}
  974. >>> valid, msg = fontLibValidator(lib)
  975. >>> valid
  976. False
  977. >>> print(msg) # doctest: +ELLIPSIS
  978. public.glyphOrder is not properly formatted: expected str,...
  979. """
  980. if not isDictEnough(value):
  981. reason = "expected a dictionary, found %s" % type(value).__name__
  982. return False, _bogusLibFormatMessage % reason
  983. for key, value in value.items():
  984. if not isinstance(key, str):
  985. return False, (
  986. "The lib key is not properly formatted: expected str, found %s: %r"
  987. % (type(key).__name__, key)
  988. )
  989. # public.glyphOrder
  990. if key == "public.glyphOrder":
  991. bogusGlyphOrderMessage = "public.glyphOrder is not properly formatted: %s"
  992. if not isinstance(value, (list, tuple)):
  993. reason = "expected list or tuple, found %s" % type(value).__name__
  994. return False, bogusGlyphOrderMessage % reason
  995. for glyphName in value:
  996. if not isinstance(glyphName, str):
  997. reason = "expected str, found %s" % type(glyphName).__name__
  998. return False, bogusGlyphOrderMessage % reason
  999. return True, None
  1000. # --------
  1001. # GLIF lib
  1002. # --------
  1003. def glyphLibValidator(value: Any) -> tuple[bool, Optional[str]]:
  1004. """
  1005. Check the validity of the lib.
  1006. Version 3+ (though it's backwards compatible with UFO 1 and UFO 2).
  1007. >>> lib = {"foo" : "bar"}
  1008. >>> glyphLibValidator(lib)
  1009. (True, None)
  1010. >>> lib = {"public.awesome" : "hello"}
  1011. >>> glyphLibValidator(lib)
  1012. (True, None)
  1013. >>> lib = {"public.markColor" : "1,0,0,0.5"}
  1014. >>> glyphLibValidator(lib)
  1015. (True, None)
  1016. >>> lib = {"public.markColor" : 1}
  1017. >>> valid, msg = glyphLibValidator(lib)
  1018. >>> valid
  1019. False
  1020. >>> print(msg)
  1021. public.markColor is not properly formatted.
  1022. """
  1023. if not isDictEnough(value):
  1024. reason = "expected a dictionary, found %s" % type(value).__name__
  1025. return False, _bogusLibFormatMessage % reason
  1026. for key, value in value.items():
  1027. if not isinstance(key, str):
  1028. reason = "key (%s) should be a string" % key
  1029. return False, _bogusLibFormatMessage % reason
  1030. # public.markColor
  1031. if key == "public.markColor":
  1032. if not colorValidator(value):
  1033. return False, "public.markColor is not properly formatted."
  1034. return True, None
  1035. if __name__ == "__main__":
  1036. import doctest
  1037. doctest.testmod()