bug 467669 - pt 2 - find the @font-face rule (if any) responsible for the font entry. r=roc
authorJonathan Kew <jfkthame@gmail.com>
Wed, 15 Jun 2011 20:17:51 +0100
changeset 71152 8c602c722bca3d5e5a5f27c4ca0069e8ea3d60c1
parent 71151 592b0c82089d22d5d9b889cafdb7d82b7bf4a3ab
child 71153 05c732013acb529e62dd555248aeaf4094b032c8
push idunknown
push userunknown
push dateunknown
reviewersroc
bugs467669
milestone7.0a1
bug 467669 - pt 2 - find the @font-face rule (if any) responsible for the font entry. r=roc
layout/base/nsLayoutUtils.cpp
layout/inspector/src/nsFontFace.cpp
layout/inspector/src/nsFontFace.h
layout/inspector/src/nsFontFaceList.cpp
layout/inspector/src/nsFontFaceList.h
layout/style/nsFontFaceLoader.cpp
layout/style/nsFontFaceLoader.h
--- a/layout/base/nsLayoutUtils.cpp
+++ b/layout/base/nsLayoutUtils.cpp
@@ -4115,17 +4115,18 @@ nsLayoutUtils::GetFontFacesForText(nsIFr
     curr->EnsureTextRun();
     gfxTextRun* textRun = curr->GetTextRun();
     NS_ENSURE_TRUE(textRun, NS_ERROR_OUT_OF_MEMORY);
 
     gfxSkipCharsIterator iter(textRun->GetSkipChars());
     PRUint32 skipStart = iter.ConvertOriginalToSkipped(fstart - offset);
     PRUint32 skipEnd = iter.ConvertOriginalToSkipped(fend - offset);
     aFontFaceList->AddFontsFromTextRun(textRun,
-                                       skipStart, skipEnd - skipStart);
+                                       skipStart, skipEnd - skipStart,
+                                       curr);
   } while (aFollowContinuations &&
            (curr = static_cast<nsTextFrame*>(curr->GetNextContinuation())));
 
   return NS_OK;
 }
 
 /* static */
 void
--- a/layout/inspector/src/nsFontFace.cpp
+++ b/layout/inspector/src/nsFontFace.cpp
@@ -29,20 +29,26 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-#include "nsFontFace.h"
+#define _IMPL_NS_LAYOUT
 
-nsFontFace::nsFontFace(gfxFontEntry* aFontEntry)
-  : mFontEntry(aFontEntry)
+#include "nsFontFace.h"
+#include "nsIDOMCSSFontFaceRule.h"
+#include "nsCSSRules.h"
+
+nsFontFace::nsFontFace(gfxFontEntry* aFontEntry,
+                       nsCSSFontFaceRule* aRule)
+  : mFontEntry(aFontEntry),
+    mRule(aRule)
 {
 }
 
 nsFontFace::~nsFontFace()
 {
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -89,17 +95,18 @@ nsFontFace::GetCSSFamilyName(nsAString &
   aCSSFamilyName = mFontEntry->FamilyName();
   return NS_OK;
 }
 
 /* readonly attribute nsIDOMCSSFontFaceRule rule; */
 NS_IMETHODIMP
 nsFontFace::GetRule(nsIDOMCSSFontFaceRule **aRule)
 {
-  return NS_ERROR_NOT_IMPLEMENTED;
+  NS_IF_ADDREF(*aRule = mRule.get());
+  return NS_OK;
 }
 
 /* readonly attribute long srcIndex; */
 NS_IMETHODIMP
 nsFontFace::GetSrcIndex(PRInt32 * aSrcIndex)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
--- a/layout/inspector/src/nsFontFace.h
+++ b/layout/inspector/src/nsFontFace.h
@@ -36,24 +36,28 @@
 
 #ifndef __nsFontFace_h__
 #define __nsFontFace_h__
 
 #include "nsIDOMFontFace.h"
 
 #include "gfxFont.h"
 
+class nsCSSFontFaceRule;
+
 class nsFontFace : public nsIDOMFontFace
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIDOMFONTFACE
 
-  nsFontFace(gfxFontEntry* aFontEntry);
+  nsFontFace(gfxFontEntry* aFontEntry,
+             nsCSSFontFaceRule* aRule);
   virtual ~nsFontFace();
 
   gfxFontEntry* GetFontEntry() const { return mFontEntry.get(); }
 
 protected:
   nsRefPtr<gfxFontEntry> mFontEntry;
+  nsRefPtr<nsCSSFontFaceRule> mRule;
 };
 
 #endif // __nsFontFace_h__
--- a/layout/inspector/src/nsFontFaceList.cpp
+++ b/layout/inspector/src/nsFontFaceList.cpp
@@ -29,18 +29,22 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+#define _IMPL_NS_LAYOUT
+
 #include "nsFontFaceList.h"
 #include "nsFontFace.h"
+#include "nsFontFaceLoader.h"
+#include "nsIFrame.h"
 #include "gfxFont.h"
 
 nsFontFaceList::nsFontFaceList()
 {
   mFontFaces.Init();
 }
 
 nsFontFaceList::~nsFontFaceList()
@@ -96,23 +100,31 @@ nsFontFaceList::GetLength(PRUint32 *aLen
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////
 // nsFontFaceList
 
 nsresult
 nsFontFaceList::AddFontsFromTextRun(gfxTextRun* aTextRun,
-                                    PRUint32 aOffset, PRUint32 aLength)
+                                    PRUint32 aOffset, PRUint32 aLength,
+                                    nsIFrame* aFrame)
 {
   gfxTextRun::GlyphRunIterator iter(aTextRun, aOffset, aLength);
   while (iter.NextRun()) {
     gfxFontEntry *fe = iter.GetGlyphRun()->mFont->GetFontEntry();
     if (!mFontFaces.GetWeak(fe)) {
-      nsCOMPtr<nsFontFace> ff = new nsFontFace(fe);
+      // check whether this font entry is associated with an @font-face rule
+      nsRefPtr<nsCSSFontFaceRule> rule;
+      nsUserFontSet* fontSet =
+        static_cast<nsUserFontSet*>(aFrame->PresContext()->GetUserFontSet());
+      if (fontSet) {
+        rule = fontSet->FindRuleForEntry(fe);
+      }
+      nsCOMPtr<nsFontFace> ff = new nsFontFace(fe, rule);
       if (!mFontFaces.Put(fe, ff)) {
         return NS_ERROR_OUT_OF_MEMORY;
       }
     }
   }
 
   return NS_OK;
 }
--- a/layout/inspector/src/nsFontFaceList.h
+++ b/layout/inspector/src/nsFontFaceList.h
@@ -40,26 +40,28 @@
 #include "nsIDOMFontFaceList.h"
 #include "nsIDOMFontFace.h"
 #include "nsCOMPtr.h"
 #include "nsInterfaceHashtable.h"
 #include "nsHashKeys.h"
 #include "gfxFont.h"
 
 class gfxTextRun;
+class nsIFrame;
 
 class nsFontFaceList : public nsIDOMFontFaceList
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIDOMFONTFACELIST
 
   nsFontFaceList();
   virtual ~nsFontFaceList();
 
   nsresult AddFontsFromTextRun(gfxTextRun* aTextRun,
-                               PRUint32 aOffset, PRUint32 aLength);
+                               PRUint32 aOffset, PRUint32 aLength,
+                               nsIFrame* aFrame);
 
 protected:
   nsInterfaceHashtable<nsPtrHashKey<gfxFontEntry>,nsIDOMFontFace> mFontFaces;
 };
 
 #endif // __nsFontFaceList_h__
--- a/layout/style/nsFontFaceLoader.cpp
+++ b/layout/style/nsFontFaceLoader.cpp
@@ -681,8 +681,19 @@ nsUserFontSet::ReplaceFontEntry(gfxProxy
     if (mRules[i].mFontEntry == aProxy) {
       mRules[i].mFontEntry = aFontEntry;
       break;
     }
   }
   static_cast<gfxMixedFontFamily*>(aProxy->Family())->
     ReplaceFontEntry(aProxy, aFontEntry);
 }
+
+nsCSSFontFaceRule*
+nsUserFontSet::FindRuleForEntry(gfxFontEntry *aFontEntry)
+{
+  for (PRUint32 i = 0; i < mRules.Length(); ++i) {
+    if (mRules[i].mFontEntry == aFontEntry) {
+      return mRules[i].mContainer.mRule;
+    }
+  }
+  return nsnull;
+}
--- a/layout/style/nsFontFaceLoader.h
+++ b/layout/style/nsFontFaceLoader.h
@@ -81,16 +81,18 @@ public:
 
   PRBool UpdateRules(const nsTArray<nsFontFaceRuleContainer>& aRules);
 
   nsPresContext *GetPresContext() { return mPresContext; }
 
   virtual void ReplaceFontEntry(gfxProxyFontEntry *aProxy,
                                 gfxFontEntry *aFontEntry);
 
+  nsCSSFontFaceRule *FindRuleForEntry(gfxFontEntry *aFontEntry);
+
 protected:
   // The font-set keeps track of the collection of rules, and their
   // corresponding font entries (whether proxies or real entries),
   // so that we can update the set without having to throw away
   // all the existing fonts.
   struct FontFaceRuleRecord {
     nsRefPtr<gfxFontEntry>       mFontEntry;
     nsFontFaceRuleContainer      mContainer;