Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsFontFace.h" |
michael@0 | 6 | #include "nsIDOMCSSFontFaceRule.h" |
michael@0 | 7 | #include "nsCSSRules.h" |
michael@0 | 8 | #include "gfxFont.h" |
michael@0 | 9 | #include "gfxUserFontSet.h" |
michael@0 | 10 | #include "nsFontFaceLoader.h" |
michael@0 | 11 | #include "mozilla/gfx/2D.h" |
michael@0 | 12 | #include "zlib.h" |
michael@0 | 13 | |
michael@0 | 14 | nsFontFace::nsFontFace(gfxFontEntry* aFontEntry, |
michael@0 | 15 | gfxFontGroup* aFontGroup, |
michael@0 | 16 | uint8_t aMatchType) |
michael@0 | 17 | : mFontEntry(aFontEntry), |
michael@0 | 18 | mFontGroup(aFontGroup), |
michael@0 | 19 | mMatchType(aMatchType) |
michael@0 | 20 | { |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | nsFontFace::~nsFontFace() |
michael@0 | 24 | { |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 28 | // nsISupports |
michael@0 | 29 | |
michael@0 | 30 | NS_IMPL_ISUPPORTS(nsFontFace, nsIDOMFontFace) |
michael@0 | 31 | |
michael@0 | 32 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 33 | // nsIDOMFontFace |
michael@0 | 34 | |
michael@0 | 35 | /* readonly attribute boolean fromFontGroup; */ |
michael@0 | 36 | NS_IMETHODIMP |
michael@0 | 37 | nsFontFace::GetFromFontGroup(bool * aFromFontGroup) |
michael@0 | 38 | { |
michael@0 | 39 | *aFromFontGroup = |
michael@0 | 40 | (mMatchType & gfxTextRange::kFontGroup) != 0; |
michael@0 | 41 | return NS_OK; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | /* readonly attribute boolean fromLanguagePrefs; */ |
michael@0 | 45 | NS_IMETHODIMP |
michael@0 | 46 | nsFontFace::GetFromLanguagePrefs(bool * aFromLanguagePrefs) |
michael@0 | 47 | { |
michael@0 | 48 | *aFromLanguagePrefs = |
michael@0 | 49 | (mMatchType & gfxTextRange::kPrefsFallback) != 0; |
michael@0 | 50 | return NS_OK; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | /* readonly attribute boolean fromSystemFallback; */ |
michael@0 | 54 | NS_IMETHODIMP |
michael@0 | 55 | nsFontFace::GetFromSystemFallback(bool * aFromSystemFallback) |
michael@0 | 56 | { |
michael@0 | 57 | *aFromSystemFallback = |
michael@0 | 58 | (mMatchType & gfxTextRange::kSystemFallback) != 0; |
michael@0 | 59 | return NS_OK; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /* readonly attribute DOMString name; */ |
michael@0 | 63 | NS_IMETHODIMP |
michael@0 | 64 | nsFontFace::GetName(nsAString & aName) |
michael@0 | 65 | { |
michael@0 | 66 | if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
michael@0 | 67 | NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
michael@0 | 68 | aName = mFontEntry->mUserFontData->mRealName; |
michael@0 | 69 | } else { |
michael@0 | 70 | aName = mFontEntry->RealFaceName(); |
michael@0 | 71 | } |
michael@0 | 72 | return NS_OK; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | /* readonly attribute DOMString CSSFamilyName; */ |
michael@0 | 76 | NS_IMETHODIMP |
michael@0 | 77 | nsFontFace::GetCSSFamilyName(nsAString & aCSSFamilyName) |
michael@0 | 78 | { |
michael@0 | 79 | aCSSFamilyName = mFontEntry->FamilyName(); |
michael@0 | 80 | return NS_OK; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /* readonly attribute nsIDOMCSSFontFaceRule rule; */ |
michael@0 | 84 | NS_IMETHODIMP |
michael@0 | 85 | nsFontFace::GetRule(nsIDOMCSSFontFaceRule **aRule) |
michael@0 | 86 | { |
michael@0 | 87 | // check whether this font entry is associated with an @font-face rule |
michael@0 | 88 | // in the relevant font group's user font set |
michael@0 | 89 | nsCSSFontFaceRule* rule = nullptr; |
michael@0 | 90 | if (mFontEntry->IsUserFont()) { |
michael@0 | 91 | nsUserFontSet* fontSet = |
michael@0 | 92 | static_cast<nsUserFontSet*>(mFontGroup->GetUserFontSet()); |
michael@0 | 93 | if (fontSet) { |
michael@0 | 94 | rule = fontSet->FindRuleForEntry(mFontEntry); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | NS_IF_ADDREF(*aRule = rule); |
michael@0 | 99 | return NS_OK; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /* readonly attribute long srcIndex; */ |
michael@0 | 103 | NS_IMETHODIMP |
michael@0 | 104 | nsFontFace::GetSrcIndex(int32_t * aSrcIndex) |
michael@0 | 105 | { |
michael@0 | 106 | if (mFontEntry->IsUserFont()) { |
michael@0 | 107 | NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
michael@0 | 108 | *aSrcIndex = mFontEntry->mUserFontData->mSrcIndex; |
michael@0 | 109 | } else { |
michael@0 | 110 | *aSrcIndex = -1; |
michael@0 | 111 | } |
michael@0 | 112 | return NS_OK; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /* readonly attribute DOMString URI; */ |
michael@0 | 116 | NS_IMETHODIMP |
michael@0 | 117 | nsFontFace::GetURI(nsAString & aURI) |
michael@0 | 118 | { |
michael@0 | 119 | aURI.Truncate(); |
michael@0 | 120 | if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
michael@0 | 121 | NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
michael@0 | 122 | if (mFontEntry->mUserFontData->mURI) { |
michael@0 | 123 | nsAutoCString spec; |
michael@0 | 124 | mFontEntry->mUserFontData->mURI->GetSpec(spec); |
michael@0 | 125 | AppendUTF8toUTF16(spec, aURI); |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | return NS_OK; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /* readonly attribute DOMString localName; */ |
michael@0 | 132 | NS_IMETHODIMP |
michael@0 | 133 | nsFontFace::GetLocalName(nsAString & aLocalName) |
michael@0 | 134 | { |
michael@0 | 135 | if (mFontEntry->IsLocalUserFont()) { |
michael@0 | 136 | NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
michael@0 | 137 | aLocalName = mFontEntry->mUserFontData->mLocalName; |
michael@0 | 138 | } else { |
michael@0 | 139 | aLocalName.Truncate(); |
michael@0 | 140 | } |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | /* readonly attribute DOMString format; */ |
michael@0 | 145 | static void |
michael@0 | 146 | AppendToFormat(nsAString & aResult, const char* aFormat) |
michael@0 | 147 | { |
michael@0 | 148 | if (!aResult.IsEmpty()) { |
michael@0 | 149 | aResult.Append(','); |
michael@0 | 150 | } |
michael@0 | 151 | aResult.AppendASCII(aFormat); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | NS_IMETHODIMP |
michael@0 | 155 | nsFontFace::GetFormat(nsAString & aFormat) |
michael@0 | 156 | { |
michael@0 | 157 | aFormat.Truncate(); |
michael@0 | 158 | if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
michael@0 | 159 | NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
michael@0 | 160 | uint32_t formatFlags = mFontEntry->mUserFontData->mFormat; |
michael@0 | 161 | if (formatFlags & gfxUserFontSet::FLAG_FORMAT_OPENTYPE) { |
michael@0 | 162 | AppendToFormat(aFormat, "opentype"); |
michael@0 | 163 | } |
michael@0 | 164 | if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE) { |
michael@0 | 165 | AppendToFormat(aFormat, "truetype"); |
michael@0 | 166 | } |
michael@0 | 167 | if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE_AAT) { |
michael@0 | 168 | AppendToFormat(aFormat, "truetype-aat"); |
michael@0 | 169 | } |
michael@0 | 170 | if (formatFlags & gfxUserFontSet::FLAG_FORMAT_EOT) { |
michael@0 | 171 | AppendToFormat(aFormat, "embedded-opentype"); |
michael@0 | 172 | } |
michael@0 | 173 | if (formatFlags & gfxUserFontSet::FLAG_FORMAT_SVG) { |
michael@0 | 174 | AppendToFormat(aFormat, "svg"); |
michael@0 | 175 | } |
michael@0 | 176 | if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF) { |
michael@0 | 177 | AppendToFormat(aFormat, "woff"); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | return NS_OK; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | /* readonly attribute DOMString metadata; */ |
michael@0 | 184 | NS_IMETHODIMP |
michael@0 | 185 | nsFontFace::GetMetadata(nsAString & aMetadata) |
michael@0 | 186 | { |
michael@0 | 187 | aMetadata.Truncate(); |
michael@0 | 188 | if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
michael@0 | 189 | NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
michael@0 | 190 | const gfxUserFontData* userFontData = mFontEntry->mUserFontData; |
michael@0 | 191 | if (userFontData->mMetadata.Length() && userFontData->mMetaOrigLen) { |
michael@0 | 192 | nsAutoCString str; |
michael@0 | 193 | str.SetLength(userFontData->mMetaOrigLen); |
michael@0 | 194 | if (str.Length() == userFontData->mMetaOrigLen) { |
michael@0 | 195 | uLongf destLen = userFontData->mMetaOrigLen; |
michael@0 | 196 | if (uncompress((Bytef *)(str.BeginWriting()), &destLen, |
michael@0 | 197 | (const Bytef *)(userFontData->mMetadata.Elements()), |
michael@0 | 198 | userFontData->mMetadata.Length()) == Z_OK && |
michael@0 | 199 | destLen == userFontData->mMetaOrigLen) |
michael@0 | 200 | { |
michael@0 | 201 | AppendUTF8toUTF16(str, aMetadata); |
michael@0 | 202 | } |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | return NS_OK; |
michael@0 | 207 | } |