|
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 "nsFontFace.h" |
|
6 #include "nsIDOMCSSFontFaceRule.h" |
|
7 #include "nsCSSRules.h" |
|
8 #include "gfxFont.h" |
|
9 #include "gfxUserFontSet.h" |
|
10 #include "nsFontFaceLoader.h" |
|
11 #include "mozilla/gfx/2D.h" |
|
12 #include "zlib.h" |
|
13 |
|
14 nsFontFace::nsFontFace(gfxFontEntry* aFontEntry, |
|
15 gfxFontGroup* aFontGroup, |
|
16 uint8_t aMatchType) |
|
17 : mFontEntry(aFontEntry), |
|
18 mFontGroup(aFontGroup), |
|
19 mMatchType(aMatchType) |
|
20 { |
|
21 } |
|
22 |
|
23 nsFontFace::~nsFontFace() |
|
24 { |
|
25 } |
|
26 |
|
27 //////////////////////////////////////////////////////////////////////// |
|
28 // nsISupports |
|
29 |
|
30 NS_IMPL_ISUPPORTS(nsFontFace, nsIDOMFontFace) |
|
31 |
|
32 //////////////////////////////////////////////////////////////////////// |
|
33 // nsIDOMFontFace |
|
34 |
|
35 /* readonly attribute boolean fromFontGroup; */ |
|
36 NS_IMETHODIMP |
|
37 nsFontFace::GetFromFontGroup(bool * aFromFontGroup) |
|
38 { |
|
39 *aFromFontGroup = |
|
40 (mMatchType & gfxTextRange::kFontGroup) != 0; |
|
41 return NS_OK; |
|
42 } |
|
43 |
|
44 /* readonly attribute boolean fromLanguagePrefs; */ |
|
45 NS_IMETHODIMP |
|
46 nsFontFace::GetFromLanguagePrefs(bool * aFromLanguagePrefs) |
|
47 { |
|
48 *aFromLanguagePrefs = |
|
49 (mMatchType & gfxTextRange::kPrefsFallback) != 0; |
|
50 return NS_OK; |
|
51 } |
|
52 |
|
53 /* readonly attribute boolean fromSystemFallback; */ |
|
54 NS_IMETHODIMP |
|
55 nsFontFace::GetFromSystemFallback(bool * aFromSystemFallback) |
|
56 { |
|
57 *aFromSystemFallback = |
|
58 (mMatchType & gfxTextRange::kSystemFallback) != 0; |
|
59 return NS_OK; |
|
60 } |
|
61 |
|
62 /* readonly attribute DOMString name; */ |
|
63 NS_IMETHODIMP |
|
64 nsFontFace::GetName(nsAString & aName) |
|
65 { |
|
66 if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
|
67 NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
|
68 aName = mFontEntry->mUserFontData->mRealName; |
|
69 } else { |
|
70 aName = mFontEntry->RealFaceName(); |
|
71 } |
|
72 return NS_OK; |
|
73 } |
|
74 |
|
75 /* readonly attribute DOMString CSSFamilyName; */ |
|
76 NS_IMETHODIMP |
|
77 nsFontFace::GetCSSFamilyName(nsAString & aCSSFamilyName) |
|
78 { |
|
79 aCSSFamilyName = mFontEntry->FamilyName(); |
|
80 return NS_OK; |
|
81 } |
|
82 |
|
83 /* readonly attribute nsIDOMCSSFontFaceRule rule; */ |
|
84 NS_IMETHODIMP |
|
85 nsFontFace::GetRule(nsIDOMCSSFontFaceRule **aRule) |
|
86 { |
|
87 // check whether this font entry is associated with an @font-face rule |
|
88 // in the relevant font group's user font set |
|
89 nsCSSFontFaceRule* rule = nullptr; |
|
90 if (mFontEntry->IsUserFont()) { |
|
91 nsUserFontSet* fontSet = |
|
92 static_cast<nsUserFontSet*>(mFontGroup->GetUserFontSet()); |
|
93 if (fontSet) { |
|
94 rule = fontSet->FindRuleForEntry(mFontEntry); |
|
95 } |
|
96 } |
|
97 |
|
98 NS_IF_ADDREF(*aRule = rule); |
|
99 return NS_OK; |
|
100 } |
|
101 |
|
102 /* readonly attribute long srcIndex; */ |
|
103 NS_IMETHODIMP |
|
104 nsFontFace::GetSrcIndex(int32_t * aSrcIndex) |
|
105 { |
|
106 if (mFontEntry->IsUserFont()) { |
|
107 NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
|
108 *aSrcIndex = mFontEntry->mUserFontData->mSrcIndex; |
|
109 } else { |
|
110 *aSrcIndex = -1; |
|
111 } |
|
112 return NS_OK; |
|
113 } |
|
114 |
|
115 /* readonly attribute DOMString URI; */ |
|
116 NS_IMETHODIMP |
|
117 nsFontFace::GetURI(nsAString & aURI) |
|
118 { |
|
119 aURI.Truncate(); |
|
120 if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
|
121 NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
|
122 if (mFontEntry->mUserFontData->mURI) { |
|
123 nsAutoCString spec; |
|
124 mFontEntry->mUserFontData->mURI->GetSpec(spec); |
|
125 AppendUTF8toUTF16(spec, aURI); |
|
126 } |
|
127 } |
|
128 return NS_OK; |
|
129 } |
|
130 |
|
131 /* readonly attribute DOMString localName; */ |
|
132 NS_IMETHODIMP |
|
133 nsFontFace::GetLocalName(nsAString & aLocalName) |
|
134 { |
|
135 if (mFontEntry->IsLocalUserFont()) { |
|
136 NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
|
137 aLocalName = mFontEntry->mUserFontData->mLocalName; |
|
138 } else { |
|
139 aLocalName.Truncate(); |
|
140 } |
|
141 return NS_OK; |
|
142 } |
|
143 |
|
144 /* readonly attribute DOMString format; */ |
|
145 static void |
|
146 AppendToFormat(nsAString & aResult, const char* aFormat) |
|
147 { |
|
148 if (!aResult.IsEmpty()) { |
|
149 aResult.Append(','); |
|
150 } |
|
151 aResult.AppendASCII(aFormat); |
|
152 } |
|
153 |
|
154 NS_IMETHODIMP |
|
155 nsFontFace::GetFormat(nsAString & aFormat) |
|
156 { |
|
157 aFormat.Truncate(); |
|
158 if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
|
159 NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
|
160 uint32_t formatFlags = mFontEntry->mUserFontData->mFormat; |
|
161 if (formatFlags & gfxUserFontSet::FLAG_FORMAT_OPENTYPE) { |
|
162 AppendToFormat(aFormat, "opentype"); |
|
163 } |
|
164 if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE) { |
|
165 AppendToFormat(aFormat, "truetype"); |
|
166 } |
|
167 if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE_AAT) { |
|
168 AppendToFormat(aFormat, "truetype-aat"); |
|
169 } |
|
170 if (formatFlags & gfxUserFontSet::FLAG_FORMAT_EOT) { |
|
171 AppendToFormat(aFormat, "embedded-opentype"); |
|
172 } |
|
173 if (formatFlags & gfxUserFontSet::FLAG_FORMAT_SVG) { |
|
174 AppendToFormat(aFormat, "svg"); |
|
175 } |
|
176 if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF) { |
|
177 AppendToFormat(aFormat, "woff"); |
|
178 } |
|
179 } |
|
180 return NS_OK; |
|
181 } |
|
182 |
|
183 /* readonly attribute DOMString metadata; */ |
|
184 NS_IMETHODIMP |
|
185 nsFontFace::GetMetadata(nsAString & aMetadata) |
|
186 { |
|
187 aMetadata.Truncate(); |
|
188 if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) { |
|
189 NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData"); |
|
190 const gfxUserFontData* userFontData = mFontEntry->mUserFontData; |
|
191 if (userFontData->mMetadata.Length() && userFontData->mMetaOrigLen) { |
|
192 nsAutoCString str; |
|
193 str.SetLength(userFontData->mMetaOrigLen); |
|
194 if (str.Length() == userFontData->mMetaOrigLen) { |
|
195 uLongf destLen = userFontData->mMetaOrigLen; |
|
196 if (uncompress((Bytef *)(str.BeginWriting()), &destLen, |
|
197 (const Bytef *)(userFontData->mMetadata.Elements()), |
|
198 userFontData->mMetadata.Length()) == Z_OK && |
|
199 destLen == userFontData->mMetaOrigLen) |
|
200 { |
|
201 AppendUTF8toUTF16(str, aMetadata); |
|
202 } |
|
203 } |
|
204 } |
|
205 } |
|
206 return NS_OK; |
|
207 } |