layout/inspector/nsFontFace.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/inspector/nsFontFace.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,207 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsFontFace.h"
     1.9 +#include "nsIDOMCSSFontFaceRule.h"
    1.10 +#include "nsCSSRules.h"
    1.11 +#include "gfxFont.h"
    1.12 +#include "gfxUserFontSet.h"
    1.13 +#include "nsFontFaceLoader.h"
    1.14 +#include "mozilla/gfx/2D.h"
    1.15 +#include "zlib.h"
    1.16 +
    1.17 +nsFontFace::nsFontFace(gfxFontEntry*      aFontEntry,
    1.18 +                       gfxFontGroup*      aFontGroup,
    1.19 +                       uint8_t            aMatchType)
    1.20 +  : mFontEntry(aFontEntry),
    1.21 +    mFontGroup(aFontGroup),
    1.22 +    mMatchType(aMatchType)
    1.23 +{
    1.24 +}
    1.25 +
    1.26 +nsFontFace::~nsFontFace()
    1.27 +{
    1.28 +}
    1.29 +
    1.30 +////////////////////////////////////////////////////////////////////////
    1.31 +// nsISupports
    1.32 +
    1.33 +NS_IMPL_ISUPPORTS(nsFontFace, nsIDOMFontFace)
    1.34 +
    1.35 +////////////////////////////////////////////////////////////////////////
    1.36 +// nsIDOMFontFace
    1.37 +
    1.38 +/* readonly attribute boolean fromFontGroup; */
    1.39 +NS_IMETHODIMP
    1.40 +nsFontFace::GetFromFontGroup(bool * aFromFontGroup)
    1.41 +{
    1.42 +  *aFromFontGroup =
    1.43 +    (mMatchType & gfxTextRange::kFontGroup) != 0;
    1.44 +  return NS_OK;
    1.45 +}
    1.46 +
    1.47 +/* readonly attribute boolean fromLanguagePrefs; */
    1.48 +NS_IMETHODIMP
    1.49 +nsFontFace::GetFromLanguagePrefs(bool * aFromLanguagePrefs)
    1.50 +{
    1.51 +  *aFromLanguagePrefs =
    1.52 +    (mMatchType & gfxTextRange::kPrefsFallback) != 0;
    1.53 +  return NS_OK;
    1.54 +}
    1.55 +
    1.56 +/* readonly attribute boolean fromSystemFallback; */
    1.57 +NS_IMETHODIMP
    1.58 +nsFontFace::GetFromSystemFallback(bool * aFromSystemFallback)
    1.59 +{
    1.60 +  *aFromSystemFallback =
    1.61 +    (mMatchType & gfxTextRange::kSystemFallback) != 0;
    1.62 +  return NS_OK;
    1.63 +}
    1.64 +
    1.65 +/* readonly attribute DOMString name; */
    1.66 +NS_IMETHODIMP
    1.67 +nsFontFace::GetName(nsAString & aName)
    1.68 +{
    1.69 +  if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
    1.70 +    NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
    1.71 +    aName = mFontEntry->mUserFontData->mRealName;
    1.72 +  } else {
    1.73 +    aName = mFontEntry->RealFaceName();
    1.74 +  }
    1.75 +  return NS_OK;
    1.76 +}
    1.77 +
    1.78 +/* readonly attribute DOMString CSSFamilyName; */
    1.79 +NS_IMETHODIMP
    1.80 +nsFontFace::GetCSSFamilyName(nsAString & aCSSFamilyName)
    1.81 +{
    1.82 +  aCSSFamilyName = mFontEntry->FamilyName();
    1.83 +  return NS_OK;
    1.84 +}
    1.85 +
    1.86 +/* readonly attribute nsIDOMCSSFontFaceRule rule; */
    1.87 +NS_IMETHODIMP
    1.88 +nsFontFace::GetRule(nsIDOMCSSFontFaceRule **aRule)
    1.89 +{
    1.90 +  // check whether this font entry is associated with an @font-face rule
    1.91 +  // in the relevant font group's user font set
    1.92 +  nsCSSFontFaceRule* rule = nullptr;
    1.93 +  if (mFontEntry->IsUserFont()) {
    1.94 +    nsUserFontSet* fontSet =
    1.95 +      static_cast<nsUserFontSet*>(mFontGroup->GetUserFontSet());
    1.96 +    if (fontSet) {
    1.97 +      rule = fontSet->FindRuleForEntry(mFontEntry);
    1.98 +    }
    1.99 +  }
   1.100 +
   1.101 +  NS_IF_ADDREF(*aRule = rule);
   1.102 +  return NS_OK;
   1.103 +}
   1.104 +
   1.105 +/* readonly attribute long srcIndex; */
   1.106 +NS_IMETHODIMP
   1.107 +nsFontFace::GetSrcIndex(int32_t * aSrcIndex)
   1.108 +{
   1.109 +  if (mFontEntry->IsUserFont()) {
   1.110 +    NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
   1.111 +    *aSrcIndex = mFontEntry->mUserFontData->mSrcIndex;
   1.112 +  } else {
   1.113 +    *aSrcIndex = -1;
   1.114 +  }
   1.115 +  return NS_OK;
   1.116 +}
   1.117 +
   1.118 +/* readonly attribute DOMString URI; */
   1.119 +NS_IMETHODIMP
   1.120 +nsFontFace::GetURI(nsAString & aURI)
   1.121 +{
   1.122 +  aURI.Truncate();
   1.123 +  if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
   1.124 +    NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
   1.125 +    if (mFontEntry->mUserFontData->mURI) {
   1.126 +      nsAutoCString spec;
   1.127 +      mFontEntry->mUserFontData->mURI->GetSpec(spec);
   1.128 +      AppendUTF8toUTF16(spec, aURI);
   1.129 +    }
   1.130 +  }
   1.131 +  return NS_OK;
   1.132 +}
   1.133 +
   1.134 +/* readonly attribute DOMString localName; */
   1.135 +NS_IMETHODIMP
   1.136 +nsFontFace::GetLocalName(nsAString & aLocalName)
   1.137 +{
   1.138 +  if (mFontEntry->IsLocalUserFont()) {
   1.139 +    NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
   1.140 +    aLocalName = mFontEntry->mUserFontData->mLocalName;
   1.141 +  } else {
   1.142 +    aLocalName.Truncate();
   1.143 +  }
   1.144 +  return NS_OK;
   1.145 +}
   1.146 +
   1.147 +/* readonly attribute DOMString format; */
   1.148 +static void
   1.149 +AppendToFormat(nsAString & aResult, const char* aFormat)
   1.150 +{
   1.151 +  if (!aResult.IsEmpty()) {
   1.152 +    aResult.Append(',');
   1.153 +  }
   1.154 +  aResult.AppendASCII(aFormat);
   1.155 +}
   1.156 +
   1.157 +NS_IMETHODIMP
   1.158 +nsFontFace::GetFormat(nsAString & aFormat)
   1.159 +{
   1.160 +  aFormat.Truncate();
   1.161 +  if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
   1.162 +    NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
   1.163 +    uint32_t formatFlags = mFontEntry->mUserFontData->mFormat;
   1.164 +    if (formatFlags & gfxUserFontSet::FLAG_FORMAT_OPENTYPE) {
   1.165 +      AppendToFormat(aFormat, "opentype");
   1.166 +    }
   1.167 +    if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE) {
   1.168 +      AppendToFormat(aFormat, "truetype");
   1.169 +    }
   1.170 +    if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE_AAT) {
   1.171 +      AppendToFormat(aFormat, "truetype-aat");
   1.172 +    }
   1.173 +    if (formatFlags & gfxUserFontSet::FLAG_FORMAT_EOT) {
   1.174 +      AppendToFormat(aFormat, "embedded-opentype");
   1.175 +    }
   1.176 +    if (formatFlags & gfxUserFontSet::FLAG_FORMAT_SVG) {
   1.177 +      AppendToFormat(aFormat, "svg");
   1.178 +    }
   1.179 +    if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF) {
   1.180 +      AppendToFormat(aFormat, "woff");
   1.181 +    }
   1.182 +  }
   1.183 +  return NS_OK;
   1.184 +}
   1.185 +
   1.186 +/* readonly attribute DOMString metadata; */
   1.187 +NS_IMETHODIMP
   1.188 +nsFontFace::GetMetadata(nsAString & aMetadata)
   1.189 +{
   1.190 +  aMetadata.Truncate();
   1.191 +  if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
   1.192 +    NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
   1.193 +    const gfxUserFontData* userFontData = mFontEntry->mUserFontData;
   1.194 +    if (userFontData->mMetadata.Length() && userFontData->mMetaOrigLen) {
   1.195 +      nsAutoCString str;
   1.196 +      str.SetLength(userFontData->mMetaOrigLen);
   1.197 +      if (str.Length() == userFontData->mMetaOrigLen) {
   1.198 +        uLongf destLen = userFontData->mMetaOrigLen;
   1.199 +        if (uncompress((Bytef *)(str.BeginWriting()), &destLen,
   1.200 +                       (const Bytef *)(userFontData->mMetadata.Elements()),
   1.201 +                       userFontData->mMetadata.Length()) == Z_OK &&
   1.202 +            destLen == userFontData->mMetaOrigLen)
   1.203 +        {
   1.204 +          AppendUTF8toUTF16(str, aMetadata);
   1.205 +        }
   1.206 +      }
   1.207 +    }
   1.208 +  }
   1.209 +  return NS_OK;
   1.210 +}

mercurial