Bug 1423742 - Stop using Encoding::ForName in gfxFontUtils. r=jfkthame

MozReview-Commit-ID: EBHo01D6we8

--HG--
extra : rebase_source : 2af3a42ba246204464248448d36e7c679e3c4a59
This commit is contained in:
Masatoshi Kimura 2017-12-07 06:21:59 +09:00
Родитель 71dd57ba46
Коммит f6959223e5
2 изменённых файлов: 59 добавлений и 60 удалений

Просмотреть файл

@ -1304,58 +1304,58 @@ gfxFontUtils::ReadCanonicalName(const char *aNameData, uint32_t aDataLen,
// encoding=roman, lang=english, in order that common entries will be found
// on the first search.
#define ANY 0xffff
const uint16_t ANY = 0xffff;
const gfxFontUtils::MacFontNameCharsetMapping gfxFontUtils::gMacFontNameCharsets[] =
{
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ENGLISH, "macintosh" },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ICELANDIC, "x-mac-icelandic" },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_TURKISH, "x-mac-turkish" },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_POLISH, "x-mac-ce" },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ROMANIAN, "x-mac-romanian" },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_CZECH, "x-mac-ce" },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_SLOVAK, "x-mac-ce" },
{ ENCODING_ID_MAC_ROMAN, ANY, "macintosh" },
{ ENCODING_ID_MAC_JAPANESE, LANG_ID_MAC_JAPANESE, "Shift_JIS" },
{ ENCODING_ID_MAC_JAPANESE, ANY, "Shift_JIS" },
{ ENCODING_ID_MAC_TRAD_CHINESE, LANG_ID_MAC_TRAD_CHINESE, "Big5" },
{ ENCODING_ID_MAC_TRAD_CHINESE, ANY, "Big5" },
{ ENCODING_ID_MAC_KOREAN, LANG_ID_MAC_KOREAN, "EUC-KR" },
{ ENCODING_ID_MAC_KOREAN, ANY, "EUC-KR" },
{ ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_ARABIC, "x-mac-arabic" },
{ ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_URDU, "x-mac-farsi" },
{ ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_FARSI, "x-mac-farsi" },
{ ENCODING_ID_MAC_ARABIC, ANY, "x-mac-arabic" },
{ ENCODING_ID_MAC_HEBREW, LANG_ID_MAC_HEBREW, "x-mac-hebrew" },
{ ENCODING_ID_MAC_HEBREW, ANY, "x-mac-hebrew" },
{ ENCODING_ID_MAC_GREEK, ANY, "x-mac-greek" },
{ ENCODING_ID_MAC_CYRILLIC, ANY, "x-mac-cyrillic" },
{ ENCODING_ID_MAC_DEVANAGARI, ANY, "x-mac-devanagari"},
{ ENCODING_ID_MAC_GURMUKHI, ANY, "x-mac-gurmukhi" },
{ ENCODING_ID_MAC_GUJARATI, ANY, "x-mac-gujarati" },
{ ENCODING_ID_MAC_SIMP_CHINESE, LANG_ID_MAC_SIMP_CHINESE, "gb18030" },
{ ENCODING_ID_MAC_SIMP_CHINESE, ANY, "gb18030" }
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ENGLISH, MACINTOSH_ENCODING },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ICELANDIC, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_TURKISH, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_POLISH, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_ROMANIAN, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_CZECH, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ROMAN, LANG_ID_MAC_SLOVAK, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ROMAN, ANY, MACINTOSH_ENCODING },
{ ENCODING_ID_MAC_JAPANESE, LANG_ID_MAC_JAPANESE, SHIFT_JIS_ENCODING },
{ ENCODING_ID_MAC_JAPANESE, ANY, SHIFT_JIS_ENCODING },
{ ENCODING_ID_MAC_TRAD_CHINESE, LANG_ID_MAC_TRAD_CHINESE, BIG5_ENCODING },
{ ENCODING_ID_MAC_TRAD_CHINESE, ANY, BIG5_ENCODING },
{ ENCODING_ID_MAC_KOREAN, LANG_ID_MAC_KOREAN, EUC_KR_ENCODING },
{ ENCODING_ID_MAC_KOREAN, ANY, EUC_KR_ENCODING },
{ ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_ARABIC, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_URDU, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ARABIC, LANG_ID_MAC_FARSI, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_ARABIC, ANY, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_HEBREW, LANG_ID_MAC_HEBREW, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_HEBREW, ANY, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_GREEK, ANY, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_CYRILLIC, ANY, X_MAC_CYRILLIC_ENCODING },
{ ENCODING_ID_MAC_DEVANAGARI, ANY, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_GURMUKHI, ANY, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_GUJARATI, ANY, X_USER_DEFINED_ENCODING },
{ ENCODING_ID_MAC_SIMP_CHINESE, LANG_ID_MAC_SIMP_CHINESE, GB18030_ENCODING },
{ ENCODING_ID_MAC_SIMP_CHINESE, ANY, GB18030_ENCODING }
};
const char* gfxFontUtils::gISOFontNameCharsets[] =
const Encoding* gfxFontUtils::gISOFontNameCharsets[] =
{
/* 0 */ "windows-1252", /* US-ASCII */
/* 0 */ WINDOWS_1252_ENCODING, /* US-ASCII */
/* 1 */ nullptr , /* spec says "ISO 10646" but does not specify encoding form! */
/* 2 */ "windows-1252" /* ISO-8859-1 */
/* 2 */ WINDOWS_1252_ENCODING /* ISO-8859-1 */
};
const char* gfxFontUtils::gMSFontNameCharsets[] =
const Encoding* gfxFontUtils::gMSFontNameCharsets[] =
{
/* [0] ENCODING_ID_MICROSOFT_SYMBOL */ "" ,
/* [1] ENCODING_ID_MICROSOFT_UNICODEBMP */ "" ,
/* [2] ENCODING_ID_MICROSOFT_SHIFTJIS */ "Shift_JIS" ,
/* [0] ENCODING_ID_MICROSOFT_SYMBOL */ UTF_16BE_ENCODING,
/* [1] ENCODING_ID_MICROSOFT_UNICODEBMP */ UTF_16BE_ENCODING,
/* [2] ENCODING_ID_MICROSOFT_SHIFTJIS */ SHIFT_JIS_ENCODING,
/* [3] ENCODING_ID_MICROSOFT_PRC */ nullptr ,
/* [4] ENCODING_ID_MICROSOFT_BIG5 */ "Big5" ,
/* [4] ENCODING_ID_MICROSOFT_BIG5 */ BIG5_ENCODING,
/* [5] ENCODING_ID_MICROSOFT_WANSUNG */ nullptr ,
/* [6] ENCODING_ID_MICROSOFT_JOHAB */ nullptr ,
/* [7] reserved */ nullptr ,
/* [8] reserved */ nullptr ,
/* [9] reserved */ nullptr ,
/*[10] ENCODING_ID_MICROSOFT_UNICODEFULL */ ""
/*[10] ENCODING_ID_MICROSOFT_UNICODEFULL */ UTF_16BE_ENCODING
};
struct MacCharsetMappingComparator
@ -1375,18 +1375,19 @@ struct MacCharsetMappingComparator
}
};
// Return the name of the charset we should use to decode a font name
// Return the Encoding object we should use to decode a font name
// given the name table attributes.
// Special return values:
// "" charset is UTF16BE, no need for a converter
// X_USER_DEFINED_ENCODING One of Mac legacy encodings that is not a part
// of Encoding Standard
// nullptr unknown charset, do not attempt conversion
const char*
const Encoding*
gfxFontUtils::GetCharsetForFontName(uint16_t aPlatform, uint16_t aScript, uint16_t aLanguage)
{
switch (aPlatform)
{
case PLATFORM_ID_UNICODE:
return "";
return UTF_16BE_ENCODING;
case PLATFORM_ID_MAC:
{
@ -1395,7 +1396,7 @@ gfxFontUtils::GetCharsetForFontName(uint16_t aPlatform, uint16_t aScript, uint16
size_t idx;
if (BinarySearchIf(gMacFontNameCharsets, 0, ArrayLength(gMacFontNameCharsets),
MacCharsetMappingComparator(searchValue), &idx)) {
return gMacFontNameCharsets[idx].mCharsetName;
return gMacFontNameCharsets[idx].mEncoding;
}
// no match, so try again finding one in any language
@ -1443,9 +1444,9 @@ gfxFontUtils::DecodeFontName(const char *aNameData, int32_t aByteLen,
return true;
}
const char *csName = GetCharsetForFontName(aPlatformCode, aScriptCode, aLangCode);
auto encoding = GetCharsetForFontName(aPlatformCode, aScriptCode, aLangCode);
if (!csName) {
if (!encoding) {
// nullptr -> unknown charset
#ifdef DEBUG
char warnBuf[128];
@ -1458,8 +1459,8 @@ gfxFontUtils::DecodeFontName(const char *aNameData, int32_t aByteLen,
return false;
}
if (csName[0] == 0) {
// empty charset name: data is utf16be, no need to instantiate a converter
if (encoding == UTF_16BE_ENCODING) {
// no need to instantiate a converter
uint32_t strLen = aByteLen / 2;
aName.SetLength(strLen);
#ifdef IS_LITTLE_ENDIAN
@ -1471,9 +1472,7 @@ gfxFontUtils::DecodeFontName(const char *aNameData, int32_t aByteLen,
return true;
}
nsDependentCString encodingName(csName);
if (StartsWith(encodingName, "x-mac-") &&
!encodingName.EqualsLiteral("x-mac-cyrillic")) {
if (encoding == X_USER_DEFINED_ENCODING) {
#ifdef XP_MACOSX
// Special case for macOS only: support legacy Mac encodings
// that aren't part of the Encoding Standard.
@ -1496,7 +1495,6 @@ gfxFontUtils::DecodeFontName(const char *aNameData, int32_t aByteLen,
return false;
}
auto encoding = Encoding::ForName(encodingName);
auto rv = encoding->DecodeWithoutBOMHandling(
AsBytes(MakeSpan(aNameData, aByteLen)), aName);
return NS_SUCCEEDED(rv);

Просмотреть файл

@ -10,6 +10,7 @@
#include "nsComponentManagerUtils.h"
#include "nsTArray.h"
#include "mozilla/Likely.h"
#include "mozilla/Encoding.h"
#include "mozilla/EndianUtils.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/UniquePtr.h"
@ -1006,24 +1007,24 @@ protected:
ReadNames(const char *aNameData, uint32_t aDataLen, uint32_t aNameID,
int32_t aLangID, int32_t aPlatformID, nsTArray<nsString>& aNames);
// convert opentype name-table platform/encoding/language values to a charset name
// we can use to convert the name data to unicode, or "" if data is UTF16BE
static const char*
// convert opentype name-table platform/encoding/language values to an
// Encoding object we can use to convert the name data to unicode
static const mozilla::Encoding*
GetCharsetForFontName(uint16_t aPlatform, uint16_t aScript, uint16_t aLanguage);
struct MacFontNameCharsetMapping {
uint16_t mEncoding;
uint16_t mScript;
uint16_t mLanguage;
const char *mCharsetName;
const mozilla::Encoding* mEncoding;
bool operator<(const MacFontNameCharsetMapping& rhs) const {
return (mEncoding < rhs.mEncoding) ||
((mEncoding == rhs.mEncoding) && (mLanguage < rhs.mLanguage));
return (mScript < rhs.mScript) ||
((mScript == rhs.mScript) && (mLanguage < rhs.mLanguage));
}
};
static const MacFontNameCharsetMapping gMacFontNameCharsets[];
static const char* gISOFontNameCharsets[];
static const char* gMSFontNameCharsets[];
static const mozilla::Encoding* gISOFontNameCharsets[];
static const mozilla::Encoding* gMSFontNameCharsets[];
};
#endif /* GFX_FONT_UTILS_H */