michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsFontFace.h" michael@0: #include "nsIDOMCSSFontFaceRule.h" michael@0: #include "nsCSSRules.h" michael@0: #include "gfxFont.h" michael@0: #include "gfxUserFontSet.h" michael@0: #include "nsFontFaceLoader.h" michael@0: #include "mozilla/gfx/2D.h" michael@0: #include "zlib.h" michael@0: michael@0: nsFontFace::nsFontFace(gfxFontEntry* aFontEntry, michael@0: gfxFontGroup* aFontGroup, michael@0: uint8_t aMatchType) michael@0: : mFontEntry(aFontEntry), michael@0: mFontGroup(aFontGroup), michael@0: mMatchType(aMatchType) michael@0: { michael@0: } michael@0: michael@0: nsFontFace::~nsFontFace() michael@0: { michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////// michael@0: // nsISupports michael@0: michael@0: NS_IMPL_ISUPPORTS(nsFontFace, nsIDOMFontFace) michael@0: michael@0: //////////////////////////////////////////////////////////////////////// michael@0: // nsIDOMFontFace michael@0: michael@0: /* readonly attribute boolean fromFontGroup; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetFromFontGroup(bool * aFromFontGroup) michael@0: { michael@0: *aFromFontGroup = michael@0: (mMatchType & gfxTextRange::kFontGroup) != 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute boolean fromLanguagePrefs; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetFromLanguagePrefs(bool * aFromLanguagePrefs) michael@0: { michael@0: *aFromLanguagePrefs = michael@0: (mMatchType & gfxTextRange::kPrefsFallback) != 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute boolean fromSystemFallback; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetFromSystemFallback(bool * aFromSystemFallback) michael@0: { michael@0: *aFromSystemFallback = michael@0: (mMatchType & gfxTextRange::kSystemFallback) != 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute DOMString name; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetName(nsAString & aName) michael@0: { michael@0: if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { michael@0: NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); michael@0: aName = mFontEntry->mUserFontData->mRealName; michael@0: } else { michael@0: aName = mFontEntry->RealFaceName(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute DOMString CSSFamilyName; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetCSSFamilyName(nsAString & aCSSFamilyName) michael@0: { michael@0: aCSSFamilyName = mFontEntry->FamilyName(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute nsIDOMCSSFontFaceRule rule; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetRule(nsIDOMCSSFontFaceRule **aRule) michael@0: { michael@0: // check whether this font entry is associated with an @font-face rule michael@0: // in the relevant font group's user font set michael@0: nsCSSFontFaceRule* rule = nullptr; michael@0: if (mFontEntry->IsUserFont()) { michael@0: nsUserFontSet* fontSet = michael@0: static_cast(mFontGroup->GetUserFontSet()); michael@0: if (fontSet) { michael@0: rule = fontSet->FindRuleForEntry(mFontEntry); michael@0: } michael@0: } michael@0: michael@0: NS_IF_ADDREF(*aRule = rule); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute long srcIndex; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetSrcIndex(int32_t * aSrcIndex) michael@0: { michael@0: if (mFontEntry->IsUserFont()) { michael@0: NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); michael@0: *aSrcIndex = mFontEntry->mUserFontData->mSrcIndex; michael@0: } else { michael@0: *aSrcIndex = -1; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute DOMString URI; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetURI(nsAString & aURI) michael@0: { michael@0: aURI.Truncate(); michael@0: if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { michael@0: NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); michael@0: if (mFontEntry->mUserFontData->mURI) { michael@0: nsAutoCString spec; michael@0: mFontEntry->mUserFontData->mURI->GetSpec(spec); michael@0: AppendUTF8toUTF16(spec, aURI); michael@0: } michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute DOMString localName; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetLocalName(nsAString & aLocalName) michael@0: { michael@0: if (mFontEntry->IsLocalUserFont()) { michael@0: NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); michael@0: aLocalName = mFontEntry->mUserFontData->mLocalName; michael@0: } else { michael@0: aLocalName.Truncate(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute DOMString format; */ michael@0: static void michael@0: AppendToFormat(nsAString & aResult, const char* aFormat) michael@0: { michael@0: if (!aResult.IsEmpty()) { michael@0: aResult.Append(','); michael@0: } michael@0: aResult.AppendASCII(aFormat); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetFormat(nsAString & aFormat) michael@0: { michael@0: aFormat.Truncate(); michael@0: if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { michael@0: NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); michael@0: uint32_t formatFlags = mFontEntry->mUserFontData->mFormat; michael@0: if (formatFlags & gfxUserFontSet::FLAG_FORMAT_OPENTYPE) { michael@0: AppendToFormat(aFormat, "opentype"); michael@0: } michael@0: if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE) { michael@0: AppendToFormat(aFormat, "truetype"); michael@0: } michael@0: if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE_AAT) { michael@0: AppendToFormat(aFormat, "truetype-aat"); michael@0: } michael@0: if (formatFlags & gfxUserFontSet::FLAG_FORMAT_EOT) { michael@0: AppendToFormat(aFormat, "embedded-opentype"); michael@0: } michael@0: if (formatFlags & gfxUserFontSet::FLAG_FORMAT_SVG) { michael@0: AppendToFormat(aFormat, "svg"); michael@0: } michael@0: if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF) { michael@0: AppendToFormat(aFormat, "woff"); michael@0: } michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* readonly attribute DOMString metadata; */ michael@0: NS_IMETHODIMP michael@0: nsFontFace::GetMetadata(nsAString & aMetadata) michael@0: { michael@0: aMetadata.Truncate(); michael@0: if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { michael@0: NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); michael@0: const gfxUserFontData* userFontData = mFontEntry->mUserFontData; michael@0: if (userFontData->mMetadata.Length() && userFontData->mMetaOrigLen) { michael@0: nsAutoCString str; michael@0: str.SetLength(userFontData->mMetaOrigLen); michael@0: if (str.Length() == userFontData->mMetaOrigLen) { michael@0: uLongf destLen = userFontData->mMetaOrigLen; michael@0: if (uncompress((Bytef *)(str.BeginWriting()), &destLen, michael@0: (const Bytef *)(userFontData->mMetadata.Elements()), michael@0: userFontData->mMetadata.Length()) == Z_OK && michael@0: destLen == userFontData->mMetaOrigLen) michael@0: { michael@0: AppendUTF8toUTF16(str, aMetadata); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return NS_OK; michael@0: }