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 "nsFontFace.h"
40 : #include "nsIDOMCSSFontFaceRule.h"
41 : #include "nsCSSRules.h"
42 : #include "gfxUserFontSet.h"
43 : #include "zlib.h"
44 :
45 0 : nsFontFace::nsFontFace(gfxFontEntry* aFontEntry,
46 : PRUint8 aMatchType,
47 : nsCSSFontFaceRule* aRule)
48 : : mFontEntry(aFontEntry),
49 : mRule(aRule),
50 0 : mMatchType(aMatchType)
51 : {
52 0 : }
53 :
54 0 : nsFontFace::~nsFontFace()
55 : {
56 0 : }
57 :
58 : ////////////////////////////////////////////////////////////////////////
59 : // nsISupports
60 :
61 0 : NS_IMPL_ISUPPORTS1(nsFontFace, nsIDOMFontFace)
62 :
63 : ////////////////////////////////////////////////////////////////////////
64 : // nsIDOMFontFace
65 :
66 : /* readonly attribute boolean fromFontGroup; */
67 : NS_IMETHODIMP
68 0 : nsFontFace::GetFromFontGroup(bool * aFromFontGroup)
69 : {
70 : *aFromFontGroup =
71 0 : (mMatchType & gfxTextRange::kFontGroup) != 0;
72 0 : return NS_OK;
73 : }
74 :
75 : /* readonly attribute boolean fromLanguagePrefs; */
76 : NS_IMETHODIMP
77 0 : nsFontFace::GetFromLanguagePrefs(bool * aFromLanguagePrefs)
78 : {
79 : *aFromLanguagePrefs =
80 0 : (mMatchType & gfxTextRange::kPrefsFallback) != 0;
81 0 : return NS_OK;
82 : }
83 :
84 : /* readonly attribute boolean fromSystemFallback; */
85 : NS_IMETHODIMP
86 0 : nsFontFace::GetFromSystemFallback(bool * aFromSystemFallback)
87 : {
88 : *aFromSystemFallback =
89 0 : (mMatchType & gfxTextRange::kSystemFallback) != 0;
90 0 : return NS_OK;
91 : }
92 :
93 : /* readonly attribute DOMString name; */
94 : NS_IMETHODIMP
95 0 : nsFontFace::GetName(nsAString & aName)
96 : {
97 0 : if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
98 0 : NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
99 0 : aName = mFontEntry->mUserFontData->mRealName;
100 : } else {
101 0 : aName = mFontEntry->RealFaceName();
102 : }
103 0 : return NS_OK;
104 : }
105 :
106 : /* readonly attribute DOMString CSSFamilyName; */
107 : NS_IMETHODIMP
108 0 : nsFontFace::GetCSSFamilyName(nsAString & aCSSFamilyName)
109 : {
110 0 : aCSSFamilyName = mFontEntry->FamilyName();
111 0 : return NS_OK;
112 : }
113 :
114 : /* readonly attribute nsIDOMCSSFontFaceRule rule; */
115 : NS_IMETHODIMP
116 0 : nsFontFace::GetRule(nsIDOMCSSFontFaceRule **aRule)
117 : {
118 0 : NS_IF_ADDREF(*aRule = mRule.get());
119 0 : return NS_OK;
120 : }
121 :
122 : /* readonly attribute long srcIndex; */
123 : NS_IMETHODIMP
124 0 : nsFontFace::GetSrcIndex(PRInt32 * aSrcIndex)
125 : {
126 0 : if (mFontEntry->IsUserFont()) {
127 0 : NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
128 0 : *aSrcIndex = mFontEntry->mUserFontData->mSrcIndex;
129 : } else {
130 0 : *aSrcIndex = -1;
131 : }
132 0 : return NS_OK;
133 : }
134 :
135 : /* readonly attribute DOMString URI; */
136 : NS_IMETHODIMP
137 0 : nsFontFace::GetURI(nsAString & aURI)
138 : {
139 0 : aURI.Truncate();
140 0 : if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
141 0 : NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
142 0 : if (mFontEntry->mUserFontData->mURI) {
143 0 : nsCAutoString spec;
144 0 : mFontEntry->mUserFontData->mURI->GetSpec(spec);
145 0 : AppendUTF8toUTF16(spec, aURI);
146 : }
147 : }
148 0 : return NS_OK;
149 : }
150 :
151 : /* readonly attribute DOMString localName; */
152 : NS_IMETHODIMP
153 0 : nsFontFace::GetLocalName(nsAString & aLocalName)
154 : {
155 0 : if (mFontEntry->IsLocalUserFont()) {
156 0 : NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
157 0 : aLocalName = mFontEntry->mUserFontData->mLocalName;
158 : } else {
159 0 : aLocalName.Truncate();
160 : }
161 0 : return NS_OK;
162 : }
163 :
164 : /* readonly attribute DOMString format; */
165 : static void
166 0 : AppendToFormat(nsAString & aResult, const char* aFormat)
167 : {
168 0 : if (!aResult.IsEmpty()) {
169 0 : aResult.AppendASCII(",");
170 : }
171 0 : aResult.AppendASCII(aFormat);
172 0 : }
173 :
174 : NS_IMETHODIMP
175 0 : nsFontFace::GetFormat(nsAString & aFormat)
176 : {
177 0 : aFormat.Truncate();
178 0 : if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
179 0 : NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
180 0 : PRUint32 formatFlags = mFontEntry->mUserFontData->mFormat;
181 0 : if (formatFlags & gfxUserFontSet::FLAG_FORMAT_OPENTYPE) {
182 0 : AppendToFormat(aFormat, "opentype");
183 : }
184 0 : if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE) {
185 0 : AppendToFormat(aFormat, "truetype");
186 : }
187 0 : if (formatFlags & gfxUserFontSet::FLAG_FORMAT_TRUETYPE_AAT) {
188 0 : AppendToFormat(aFormat, "truetype-aat");
189 : }
190 0 : if (formatFlags & gfxUserFontSet::FLAG_FORMAT_EOT) {
191 0 : AppendToFormat(aFormat, "embedded-opentype");
192 : }
193 0 : if (formatFlags & gfxUserFontSet::FLAG_FORMAT_SVG) {
194 0 : AppendToFormat(aFormat, "svg");
195 : }
196 0 : if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF) {
197 0 : AppendToFormat(aFormat, "woff");
198 : }
199 : }
200 0 : return NS_OK;
201 : }
202 :
203 : /* readonly attribute DOMString metadata; */
204 : NS_IMETHODIMP
205 0 : nsFontFace::GetMetadata(nsAString & aMetadata)
206 : {
207 0 : aMetadata.Truncate();
208 0 : if (mFontEntry->IsUserFont() && !mFontEntry->IsLocalUserFont()) {
209 0 : NS_ASSERTION(mFontEntry->mUserFontData, "missing userFontData");
210 0 : const gfxUserFontData* userFontData = mFontEntry->mUserFontData;
211 0 : if (userFontData->mMetadata.Length() && userFontData->mMetaOrigLen) {
212 0 : nsCAutoString str;
213 0 : str.SetLength(userFontData->mMetaOrigLen);
214 0 : if (str.Length() == userFontData->mMetaOrigLen) {
215 0 : uLongf destLen = userFontData->mMetaOrigLen;
216 0 : if (uncompress((Bytef *)(str.BeginWriting()), &destLen,
217 0 : (const Bytef *)(userFontData->mMetadata.Elements()),
218 0 : userFontData->mMetadata.Length()) == Z_OK &&
219 : destLen == userFontData->mMetaOrigLen)
220 : {
221 0 : AppendUTF8toUTF16(str, aMetadata);
222 : }
223 : }
224 : }
225 : }
226 0 : return NS_OK;
227 : }
|