| |
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
| |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| |
4 |
| |
5 #include "nsFontFaceList.h" |
| |
6 #include "nsFontFace.h" |
| |
7 #include "nsFontFaceLoader.h" |
| |
8 #include "nsIFrame.h" |
| |
9 #include "gfxFont.h" |
| |
10 #include "mozilla/gfx/2D.h" |
| |
11 |
| |
12 nsFontFaceList::nsFontFaceList() |
| |
13 { |
| |
14 } |
| |
15 |
| |
16 nsFontFaceList::~nsFontFaceList() |
| |
17 { |
| |
18 } |
| |
19 |
| |
20 //////////////////////////////////////////////////////////////////////// |
| |
21 // nsISupports |
| |
22 |
| |
23 NS_IMPL_ISUPPORTS(nsFontFaceList, nsIDOMFontFaceList) |
| |
24 |
| |
25 //////////////////////////////////////////////////////////////////////// |
| |
26 // nsIDOMFontFaceList |
| |
27 |
| |
28 /* nsIDOMFontFace item (in unsigned long index); */ |
| |
29 struct FindByIndexData { |
| |
30 uint32_t mTarget; |
| |
31 uint32_t mCurrent; |
| |
32 nsIDOMFontFace* mResult; |
| |
33 }; |
| |
34 |
| |
35 static PLDHashOperator |
| |
36 FindByIndex(gfxFontEntry* aKey, nsIDOMFontFace* aData, void* aUserData) |
| |
37 { |
| |
38 FindByIndexData* data = static_cast<FindByIndexData*>(aUserData); |
| |
39 if (data->mCurrent == data->mTarget) { |
| |
40 data->mResult = aData; |
| |
41 return PL_DHASH_STOP; |
| |
42 } |
| |
43 data->mCurrent++; |
| |
44 return PL_DHASH_NEXT; |
| |
45 } |
| |
46 |
| |
47 NS_IMETHODIMP |
| |
48 nsFontFaceList::Item(uint32_t index, nsIDOMFontFace **_retval) |
| |
49 { |
| |
50 NS_ENSURE_TRUE(index < mFontFaces.Count(), NS_ERROR_INVALID_ARG); |
| |
51 FindByIndexData userData; |
| |
52 userData.mTarget = index; |
| |
53 userData.mCurrent = 0; |
| |
54 userData.mResult = nullptr; |
| |
55 mFontFaces.EnumerateRead(FindByIndex, &userData); |
| |
56 NS_ASSERTION(userData.mResult != nullptr, "null entry in nsFontFaceList?"); |
| |
57 NS_IF_ADDREF(*_retval = userData.mResult); |
| |
58 return NS_OK; |
| |
59 } |
| |
60 |
| |
61 /* readonly attribute unsigned long length; */ |
| |
62 NS_IMETHODIMP |
| |
63 nsFontFaceList::GetLength(uint32_t *aLength) |
| |
64 { |
| |
65 *aLength = mFontFaces.Count(); |
| |
66 return NS_OK; |
| |
67 } |
| |
68 |
| |
69 //////////////////////////////////////////////////////////////////////// |
| |
70 // nsFontFaceList |
| |
71 |
| |
72 nsresult |
| |
73 nsFontFaceList::AddFontsFromTextRun(gfxTextRun* aTextRun, |
| |
74 uint32_t aOffset, uint32_t aLength) |
| |
75 { |
| |
76 gfxTextRun::GlyphRunIterator iter(aTextRun, aOffset, aLength); |
| |
77 while (iter.NextRun()) { |
| |
78 gfxFontEntry *fe = iter.GetGlyphRun()->mFont->GetFontEntry(); |
| |
79 // if we have already listed this face, just make sure the match type is |
| |
80 // recorded |
| |
81 nsFontFace* existingFace = |
| |
82 static_cast<nsFontFace*>(mFontFaces.GetWeak(fe)); |
| |
83 if (existingFace) { |
| |
84 existingFace->AddMatchType(iter.GetGlyphRun()->mMatchType); |
| |
85 } else { |
| |
86 // A new font entry we haven't seen before |
| |
87 nsRefPtr<nsFontFace> ff = |
| |
88 new nsFontFace(fe, aTextRun->GetFontGroup(), |
| |
89 iter.GetGlyphRun()->mMatchType); |
| |
90 mFontFaces.Put(fe, ff); |
| |
91 } |
| |
92 } |
| |
93 |
| |
94 return NS_OK; |
| |
95 } |