data.py 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. import re
  2. # validate.data
  3. # ---------------------------
  4. #
  5. # for validating specific miscellaneous user input data types.
  6. # this regex is designed to find negative results rather than positive ones.
  7. # (this is so the invalid characters can be read back out to the user.)
  8. postScriptNameRegex = "(?=[\[\](){}<>/%])[\u0021-\u007e]"
  9. def validatePostScriptName(string):
  10. """
  11. Ensures that a string is compliant with the restricted set of characters required in PostScript names.
  12. (https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-ids)
  13. (ASCII codes 33-126 apart from '[', ']', '(', ')', '{', '}', '<', '>', '/', '%'.)
  14. (..or U+21-U+7e, excluding 25, 28, 29, 2f, 3c, 3e, 5b, 5d, 7b, 7d.)
  15. """
  16. if len(string) > 63:
  17. raise ValueError("Your PostScript name is more than 63 characters. A PostScript name can be no longer than 63 characters")
  18. find = re.findall(postScriptNameRegex, string)
  19. if len(find) > 0:
  20. raise ValueError(f"Your PostScript name contains the following: {', '.join(find)}. This is not valid. It must only contain certain characters. These include alphanumeric characters and some symbols. Disallowed symbols are '[', ']', '(', ')', '{', '}', '<', '>', '/' and '%'. (In techspeak - only in the unicode range U+21-U+7e, minus the aforementioned symbols.)")