1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is Mozilla Foundation.
17 : * Portions created by the Initial Developer are Copyright (C) 2011
18 : * the Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : * Jonathan Kew <jfkthame@gmail.com>
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #define _IMPL_NS_LAYOUT
38 :
39 : #include "nsFontFaceList.h"
40 : #include "nsFontFace.h"
41 : #include "nsFontFaceLoader.h"
42 : #include "nsIFrame.h"
43 : #include "gfxFont.h"
44 :
45 0 : nsFontFaceList::nsFontFaceList()
46 : {
47 0 : mFontFaces.Init();
48 0 : }
49 :
50 0 : nsFontFaceList::~nsFontFaceList()
51 : {
52 0 : }
53 :
54 : ////////////////////////////////////////////////////////////////////////
55 : // nsISupports
56 :
57 0 : NS_IMPL_ISUPPORTS1(nsFontFaceList, nsIDOMFontFaceList)
58 :
59 : ////////////////////////////////////////////////////////////////////////
60 : // nsIDOMFontFaceList
61 :
62 : /* nsIDOMFontFace item (in unsigned long index); */
63 : struct FindByIndexData {
64 : PRUint32 mTarget;
65 : PRUint32 mCurrent;
66 : nsIDOMFontFace* mResult;
67 : };
68 :
69 : static PLDHashOperator
70 0 : FindByIndex(gfxFontEntry* aKey, nsIDOMFontFace* aData, void* aUserData)
71 : {
72 0 : FindByIndexData* data = static_cast<FindByIndexData*>(aUserData);
73 0 : if (data->mCurrent == data->mTarget) {
74 0 : data->mResult = aData;
75 0 : return PL_DHASH_STOP;
76 : }
77 0 : data->mCurrent++;
78 0 : return PL_DHASH_NEXT;
79 : }
80 :
81 : NS_IMETHODIMP
82 0 : nsFontFaceList::Item(PRUint32 index, nsIDOMFontFace **_retval NS_OUTPARAM)
83 : {
84 0 : NS_ENSURE_TRUE(index < mFontFaces.Count(), NS_ERROR_INVALID_ARG);
85 : FindByIndexData userData;
86 0 : userData.mTarget = index;
87 0 : userData.mCurrent = 0;
88 0 : userData.mResult = nsnull;
89 0 : mFontFaces.EnumerateRead(FindByIndex, &userData);
90 0 : NS_ASSERTION(userData.mResult != nsnull, "null entry in nsFontFaceList?");
91 0 : NS_IF_ADDREF(*_retval = userData.mResult);
92 0 : return NS_OK;
93 : }
94 :
95 : /* readonly attribute unsigned long length; */
96 : NS_IMETHODIMP
97 0 : nsFontFaceList::GetLength(PRUint32 *aLength)
98 : {
99 0 : *aLength = mFontFaces.Count();
100 0 : return NS_OK;
101 : }
102 :
103 : ////////////////////////////////////////////////////////////////////////
104 : // nsFontFaceList
105 :
106 : nsresult
107 0 : nsFontFaceList::AddFontsFromTextRun(gfxTextRun* aTextRun,
108 : PRUint32 aOffset, PRUint32 aLength,
109 : nsIFrame* aFrame)
110 : {
111 0 : gfxTextRun::GlyphRunIterator iter(aTextRun, aOffset, aLength);
112 0 : while (iter.NextRun()) {
113 0 : gfxFontEntry *fe = iter.GetGlyphRun()->mFont->GetFontEntry();
114 : // if we have already listed this face, just make sure the match type is
115 : // recorded
116 : nsFontFace* existingFace =
117 0 : static_cast<nsFontFace*>(mFontFaces.GetWeak(fe));
118 0 : if (existingFace) {
119 0 : existingFace->AddMatchType(iter.GetGlyphRun()->mMatchType);
120 : } else {
121 : // A new font entry we haven't seen before;
122 : // check whether this font entry is associated with an @font-face rule
123 0 : nsRefPtr<nsCSSFontFaceRule> rule;
124 : nsUserFontSet* fontSet =
125 0 : static_cast<nsUserFontSet*>(aFrame->PresContext()->GetUserFontSet());
126 0 : if (fontSet) {
127 0 : rule = fontSet->FindRuleForEntry(fe);
128 : }
129 : nsCOMPtr<nsFontFace> ff =
130 0 : new nsFontFace(fe, iter.GetGlyphRun()->mMatchType, rule);
131 0 : if (!mFontFaces.Put(fe, ff)) {
132 0 : return NS_ERROR_OUT_OF_MEMORY;
133 : }
134 : }
135 : }
136 :
137 0 : return NS_OK;
138 : }
|