2012-05-21 15:12:37 +04: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 10:31:36 +04:00
|
|
|
|
2011-06-16 10:31:36 +04:00
|
|
|
#include "nsFontFace.h"
|
2011-06-16 10:31:36 +04:00
|
|
|
#include "nsIDOMCSSFontFaceRule.h"
|
|
|
|
#include "nsCSSRules.h"
|
2014-09-16 13:58:12 +04:00
|
|
|
#include "gfxTextRun.h"
|
2011-06-16 10:31:37 +04:00
|
|
|
#include "gfxUserFontSet.h"
|
2012-12-19 13:42:25 +04:00
|
|
|
#include "nsFontFaceLoader.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-10-04 14:46:54 +04:00
|
|
|
#include "decode.h"
|
2011-06-16 10:31:37 +04:00
|
|
|
#include "zlib.h"
|
2014-10-02 06:32:05 +04:00
|
|
|
#include "mozilla/dom/FontFaceSet.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2011-06-16 10:31:36 +04:00
|
|
|
|
2011-06-16 10:31:37 +04:00
|
|
|
nsFontFace::nsFontFace(gfxFontEntry* aFontEntry,
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxFontGroup* aFontGroup,
|
|
|
|
uint8_t aMatchType)
|
2011-06-16 10:31:36 +04:00
|
|
|
: mFontEntry(aFontEntry),
|
2012-12-19 13:42:25 +04:00
|
|
|
mFontGroup(aFontGroup),
|
2011-08-14 21:39:56 +04:00
|
|
|
mMatchType(aMatchType)
|
2011-06-16 10:31:36 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFontFace::~nsFontFace()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsISupports
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsFontFace, nsIDOMFontFace)
|
2011-06-16 10:31:36 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIDOMFontFace
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsFontFace::GetFromFontGroup(bool * aFromFontGroup)
|
2011-06-16 10:31:36 +04:00
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
*aFromFontGroup =
|
|
|
|
(mMatchType & gfxTextRange::kFontGroup) != 0;
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsFontFace::GetFromLanguagePrefs(bool * aFromLanguagePrefs)
|
2011-06-16 10:31:36 +04:00
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
*aFromLanguagePrefs =
|
|
|
|
(mMatchType & gfxTextRange::kPrefsFallback) != 0;
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsFontFace::GetFromSystemFallback(bool * aFromSystemFallback)
|
2011-06-16 10:31:36 +04:00
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
*aFromSystemFallback =
|
|
|
|
(mMatchType & gfxTextRange::kSystemFallback) != 0;
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontFace::GetName(nsAString & aName)
|
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
|
|
|
|
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
|
|
|
|
aName = mFontEntry->mUserFontData->mRealName;
|
|
|
|
} else {
|
|
|
|
aName = mFontEntry->RealFaceName();
|
|
|
|
}
|
2011-06-16 10:31:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontFace::GetCSSFamilyName(nsAString & aCSSFamilyName)
|
|
|
|
{
|
|
|
|
aCSSFamilyName = mFontEntry->FamilyName();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontFace::GetRule(nsIDOMCSSFontFaceRule **aRule)
|
|
|
|
{
|
2012-12-19 13:42:25 +04: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()) {
|
2014-10-02 06:32:05 +04:00
|
|
|
FontFaceSet::UserFontSet* fontSet =
|
|
|
|
static_cast<FontFaceSet::UserFontSet*>(mFontGroup->GetUserFontSet());
|
2012-12-19 13:42:25 +04:00
|
|
|
if (fontSet) {
|
2014-10-02 06:32:05 +04:00
|
|
|
FontFaceSet* fontFaceSet = fontSet->GetFontFaceSet();
|
|
|
|
if (fontFaceSet) {
|
|
|
|
rule = fontFaceSet->FindRuleForEntry(mFontEntry);
|
|
|
|
}
|
2012-12-19 13:42:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aRule = rule);
|
2011-06-16 10:31:36 +04:00
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFontFace::GetSrcIndex(int32_t * aSrcIndex)
|
2011-06-16 10:31:36 +04:00
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
if (mFontEntry->IsUserFont()) {
|
|
|
|
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
|
|
|
|
*aSrcIndex = mFontEntry->mUserFontData->mSrcIndex;
|
|
|
|
} else {
|
|
|
|
*aSrcIndex = -1;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontFace::GetURI(nsAString & aURI)
|
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
aURI.Truncate();
|
|
|
|
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
|
|
|
|
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
|
|
|
|
if (mFontEntry->mUserFontData->mURI) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2016-08-31 13:10:10 +03:00
|
|
|
nsresult rv = mFontEntry->mUserFontData->mURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-06-16 10:31:37 +04:00
|
|
|
AppendUTF8toUTF16(spec, aURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontFace::GetLocalName(nsAString & aLocalName)
|
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
if (mFontEntry->IsLocalUserFont()) {
|
|
|
|
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
|
|
|
|
aLocalName = mFontEntry->mUserFontData->mLocalName;
|
|
|
|
} else {
|
|
|
|
aLocalName.Truncate();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
2011-06-16 10:31:37 +04:00
|
|
|
static void
|
|
|
|
AppendToFormat(nsAString & aResult, const char* aFormat)
|
|
|
|
{
|
|
|
|
if (!aResult.IsEmpty()) {
|
2013-05-07 18:25:21 +04:00
|
|
|
aResult.Append(',');
|
2011-06-16 10:31:37 +04:00
|
|
|
}
|
|
|
|
aResult.AppendASCII(aFormat);
|
|
|
|
}
|
|
|
|
|
2011-06-16 10:31:36 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontFace::GetFormat(nsAString & aFormat)
|
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
aFormat.Truncate();
|
|
|
|
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
|
|
|
|
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t formatFlags = mFontEntry->mUserFontData->mFormat;
|
2011-06-16 10:31:37 +04: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");
|
|
|
|
}
|
2014-10-04 14:36:05 +04:00
|
|
|
if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF2) {
|
|
|
|
AppendToFormat(aFormat, "woff2");
|
|
|
|
}
|
2011-06-16 10:31:37 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontFace::GetMetadata(nsAString & aMetadata)
|
|
|
|
{
|
2011-06-16 10:31:37 +04:00
|
|
|
aMetadata.Truncate();
|
|
|
|
if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
|
|
|
|
NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
|
2016-04-15 22:45:37 +03:00
|
|
|
const gfxUserFontData* userFontData = mFontEntry->mUserFontData.get();
|
2011-06-16 10:31:37 +04:00
|
|
|
if (userFontData->mMetadata.Length() && userFontData->mMetaOrigLen) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString str;
|
2011-06-16 10:31:37 +04:00
|
|
|
str.SetLength(userFontData->mMetaOrigLen);
|
|
|
|
if (str.Length() == userFontData->mMetaOrigLen) {
|
2014-10-04 14:46:54 +04:00
|
|
|
switch (userFontData->mCompression) {
|
|
|
|
case gfxUserFontData::kZlibCompression:
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case gfxUserFontData::kBrotliCompression:
|
|
|
|
{
|
|
|
|
size_t decodedSize = userFontData->mMetaOrigLen;
|
|
|
|
if (BrotliDecompressBuffer(userFontData->mMetadata.Length(),
|
|
|
|
userFontData->mMetadata.Elements(),
|
|
|
|
&decodedSize,
|
|
|
|
(uint8_t*)str.BeginWriting()) == 1 &&
|
|
|
|
decodedSize == userFontData->mMetaOrigLen) {
|
|
|
|
AppendUTF8toUTF16(str, aMetadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2011-06-16 10:31:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|