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