Exemple #1
0
    def test_encoding_unicode(self):

        self.assertEqual(getEncoding(3, 0, None),
                         "utf_16_be")  # MS Symbol is Unicode as well
        self.assertEqual(getEncoding(3, 1, None), "utf_16_be")
        self.assertEqual(getEncoding(3, 10, None), "utf_16_be")
        self.assertEqual(getEncoding(0, 3, None), "utf_16_be")
Exemple #2
0
	def getEncoding(self, default='ascii'):
		"""Returns the Python encoding name for this name entry based on its platformID,
		platEncID, and langID.  If encoding for these values is not known, by default
		'ascii' is returned.  That can be overriden by passing a value to the default
		argument.
		"""
		return getEncoding(self.platformID, self.platEncID, self.langID, default)
Exemple #3
0
	def getEncoding(self, default='ascii'):
		"""Returns the Python encoding name for this name entry based on its platformID,
		platEncID, and langID.  If encoding for these values is not known, by default
		'ascii' is returned.  That can be overriden by passing a value to the default
		argument.
		"""
		return getEncoding(self.platformID, self.platEncID, self.langID, default)
Exemple #4
0
	def getEncoding(self, default=None):
		"""Returns the Python encoding name for this cmap subtable based on its platformID,
		platEncID, and language.  If encoding for these values is not known, by default
		None is returned.  That can be overriden by passing a value to the default
		argument.

		Note that if you want to choose a "preferred" cmap subtable, most of the time
		self.isUnicode() is what you want as that one only returns true for the modern,
		commonly used, Unicode-compatible triplets, not the legacy ones.
		"""
		return getEncoding(self.platformID, self.platEncID, self.language, default)
Exemple #5
0
def _makeMacName(name, nameID, language, font=None):
	"""Create a NameRecord for Apple platforms

	'language' is an arbitrary IETF BCP 47 language identifier such
	as 'en', 'de-CH', 'de-AT-1901', or 'fa-Latn'. When possible, we
	create a Macintosh NameRecord that is understood by old applications
	(platform ID 1 and an old-style Macintosh language enum). If this
	is not possible, we create a Unicode NameRecord (platform ID 0)
	whose language points to the font’s 'ltag' table. The latter
	can encode any string in any language, but legacy applications
	might not recognize the format (in which case they will ignore
	those names).

	'font' should be the TTFont for which you want to create a name.
	If 'font' is None, we only return NameRecords for legacy Macintosh;
	in that case, the result will be None for names that need to
	be encoded with an 'ltag' table.

	See the section “The language identifier” in Apple’s specification:
	https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
	"""
	macLang = _MAC_LANGUAGE_CODES.get(language.lower())
	macScript = _MAC_LANGUAGE_TO_SCRIPT.get(macLang)
	if macLang is not None and macScript is not None:
		encoding = getEncoding(1, macScript, macLang, default="ascii")
		# Check if we can actually encode this name. If we can't,
		# for example because we have no support for the legacy
		# encoding, or because the name string contains Unicode
		# characters that the legacy encoding cannot represent,
		# we fall back to encoding the name in Unicode and put
		# the language tag into the ltag table.
		try:
			_ = tobytes(name, encoding, errors="strict")
			return makeName(name, nameID, 1, macScript, macLang)
		except UnicodeEncodeError:
			pass
	if font is not None:
		ltag = font.tables.get("ltag")
		if ltag is None:
			ltag = font["ltag"] = newTable("ltag")
		# 0 = Unicode; 4 = “Unicode 2.0 or later semantics (non-BMP characters allowed)”
		# “The preferred platform-specific code for Unicode would be 3 or 4.”
		# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
		return makeName(name, nameID, 0, 4, ltag.addTag(language))
	else:
		log.warning("cannot store language %s into 'ltag' table "
		            "without having access to the TTFont object" %
		            language)
		return None
Exemple #6
0
def _makeMacName(name, nameID, language, font=None):
	"""Create a NameRecord for Apple platforms

	'language' is an arbitrary IETF BCP 47 language identifier such
	as 'en', 'de-CH', 'de-AT-1901', or 'fa-Latn'. When possible, we
	create a Macintosh NameRecord that is understood by old applications
	(platform ID 1 and an old-style Macintosh language enum). If this
	is not possible, we create a Unicode NameRecord (platform ID 0)
	whose language points to the font’s 'ltag' table. The latter
	can encode any string in any language, but legacy applications
	might not recognize the format (in which case they will ignore
	those names).

	'font' should be the TTFont for which you want to create a name.
	If 'font' is None, we only return NameRecords for legacy Macintosh;
	in that case, the result will be None for names that need to
	be encoded with an 'ltag' table.

	See the section “The language identifier” in Apple’s specification:
	https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
	"""
	macLang = _MAC_LANGUAGE_CODES.get(language.lower())
	macScript = _MAC_LANGUAGE_TO_SCRIPT.get(macLang)
	if macLang is not None and macScript is not None:
		encoding = getEncoding(1, macScript, macLang, default="ascii")
		# Check if we can actually encode this name. If we can't,
		# for example because we have no support for the legacy
		# encoding, or because the name string contains Unicode
		# characters that the legacy encoding cannot represent,
		# we fall back to encoding the name in Unicode and put
		# the language tag into the ltag table.
		try:
			_ = tobytes(name, encoding, errors="strict")
			return makeName(name, nameID, 1, macScript, macLang)
		except UnicodeEncodeError:
			pass
	if font is not None:
		ltag = font.tables.get("ltag")
		if ltag is None:
			ltag = font["ltag"] = newTable("ltag")
		# 0 = Unicode; 4 = “Unicode 2.0 or later semantics (non-BMP characters allowed)”
		# “The preferred platform-specific code for Unicode would be 3 or 4.”
		# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
		return makeName(name, nameID, 0, 4, ltag.addTag(language))
	else:
		log.warning("cannot store language %s into 'ltag' table "
		            "without having access to the TTFont object" %
		            language)
		return None
Exemple #7
0
 def asFea(self, indent=""):
     def escape(c, escape_pattern):
         # Also escape U+0022 QUOTATION MARK and U+005C REVERSE SOLIDUS
         if c >= 0x20 and c <= 0x7E and c not in (0x22, 0x5C):
             return unichr(c)
         else:
             return escape_pattern % c
     encoding = getEncoding(self.platformID, self.platEncID, self.langID)
     if encoding is None:
         raise FeatureLibError("Unsupported encoding", self.location)
     s = tobytes(self.string, encoding=encoding)
     if encoding == "utf_16_be":
         escaped_string = "".join([
             escape(byteord(s[i]) * 256 + byteord(s[i + 1]), r"\%04x")
             for i in range(0, len(s), 2)])
     else:
         escaped_string = "".join([escape(byteord(b), r"\%02x") for b in s])
     plat = simplify_name_attributes(
         self.platformID, self.platEncID, self.langID)
     if plat != "":
         plat += " "
     return "nameid {} {}\"{}\";".format(self.nameID, plat, escaped_string)
Exemple #8
0
 def asFea(self, indent=""):
     def escape(c, escape_pattern):
         # Also escape U+0022 QUOTATION MARK and U+005C REVERSE SOLIDUS
         if c >= 0x20 and c <= 0x7E and c not in (0x22, 0x5C):
             return unichr(c)
         else:
             return escape_pattern % c
     encoding = getEncoding(self.platformID, self.platEncID, self.langID)
     if encoding is None:
         raise FeatureLibError("Unsupported encoding", self.location)
     s = tobytes(self.string, encoding=encoding)
     if encoding == "utf_16_be":
         escaped_string = "".join([
             escape(byteord(s[i]) * 256 + byteord(s[i + 1]), r"\%04x")
             for i in range(0, len(s), 2)])
     else:
         escaped_string = "".join([escape(byteord(b), r"\%02x") for b in s])
     plat = simplify_name_attributes(
         self.platformID, self.platEncID, self.langID)
     if plat != "":
         plat += " "
     return "nameid {} {}\"{}\";".format(self.nameID, plat, escaped_string)
Exemple #9
0
 def test_extended_unknown(self):
     self.assertEqual(getEncoding(10, 11, 12), None)
     self.assertEqual(getEncoding(10, 11, 12, "ascii"), "ascii")
     self.assertEqual(getEncoding(10, 11, 12, default="ascii"), "ascii")
Exemple #10
0
 def test_extended_mac_encodings(self):
     encoding = getEncoding(1, 1, 0)  # Mac Japanese
     decoded = b'\xfe'.decode(encoding)
     self.assertEqual(decoded, unichr(0x2122))
Exemple #11
0
 def test_encoding_macroman_misc(self):
     self.assertEqual(getEncoding(1, 0, 17), "mac_turkish")
     self.assertEqual(getEncoding(1, 0, 37), "mac_romanian")
     self.assertEqual(getEncoding(1, 0, 45), "mac_roman")
	def test_encoding_unicode(self):

		self.assertEqual(getEncoding(3, 0, None), "utf_16_be") # MS Symbol is Unicode as well
		self.assertEqual(getEncoding(3, 1, None), "utf_16_be")
		self.assertEqual(getEncoding(3, 10, None), "utf_16_be")
		self.assertEqual(getEncoding(0, 3, None), "utf_16_be")
	def test_extended_unknown(self):
		self.assertEqual(getEncoding(10, 11, 12), None)
		self.assertEqual(getEncoding(10, 11, 12, "ascii"), "ascii")
		self.assertEqual(getEncoding(10, 11, 12, default="ascii"), "ascii")
	def test_extended_mac_encodings(self):
		encoding = getEncoding(1, 1, 0) # Mac Japanese
		decoded = b'\xfe'.decode(encoding)
		self.assertEqual(decoded, unichr(0x2122))
	def test_encoding_macroman_misc(self):
		self.assertEqual(getEncoding(1, 0, 17), "mac_turkish")
		self.assertEqual(getEncoding(1, 0, 37), "mac_romanian")
		self.assertEqual(getEncoding(1, 0, 45), "mac_roman")