Bug 463806 - [PATCH][@font-face] Downloaded font activation on Mac may fail due to...
[wine-gecko.git] / gfx / thebes / src / gfxOS2Platform.cpp
blobd1d7e597b492eda57da9aec5eb3db4f0f97b770a
1 /* vim: set sw=4 sts=4 et cin: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is OS/2 code in Thebes.
17 * The Initial Developer of the Original Code is
18 * Peter Weilbacher <mozilla@Weilbacher.org>.
19 * Portions created by the Initial Developer are Copyright (C) 2006-2007
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * author of code taken from gfxPlatformGtk:
24 * Masayuki Nakano <masayuki@d-toybox.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "gfxOS2Platform.h"
41 #include "gfxOS2Surface.h"
42 #include "gfxImageSurface.h"
43 #include "gfxOS2Fonts.h"
45 #include "gfxFontconfigUtils.h"
46 //#include <fontconfig/fontconfig.h>
48 /**********************************************************************
49 * class gfxOS2Platform
50 **********************************************************************/
51 gfxFontconfigUtils *gfxOS2Platform::sFontconfigUtils = nsnull;
53 gfxOS2Platform::gfxOS2Platform()
55 #ifdef DEBUG_thebes
56 printf("gfxOS2Platform::gfxOS2Platform()\n");
57 #endif
58 // this seems to be reasonably early in the process and only once,
59 // so it's a good place to initialize OS/2 cairo stuff
60 cairo_os2_init();
61 #ifdef DEBUG_thebes
62 printf(" cairo_os2_init() was called\n");
63 #endif
64 if (!sFontconfigUtils) {
65 sFontconfigUtils = gfxFontconfigUtils::GetFontconfigUtils();
69 gfxOS2Platform::~gfxOS2Platform()
71 #ifdef DEBUG_thebes
72 printf("gfxOS2Platform::~gfxOS2Platform()\n");
73 #endif
74 gfxFontconfigUtils::Shutdown();
75 sFontconfigUtils = nsnull;
77 // clean up OS/2 cairo stuff
78 cairo_os2_fini();
79 #ifdef DEBUG_thebes
80 printf(" cairo_os2_fini() was called\n");
81 #endif
84 already_AddRefed<gfxASurface>
85 gfxOS2Platform::CreateOffscreenSurface(const gfxIntSize& aSize,
86 gfxASurface::gfxImageFormat aImageFormat)
88 #ifdef DEBUG_thebes_2
89 printf("gfxOS2Platform::CreateOffscreenSurface(%d/%d, %d)\n",
90 aSize.width, aSize.height, aImageFormat);
91 #endif
92 gfxASurface *newSurface = nsnull;
94 // we only ever seem to get aImageFormat=0 or ImageFormatARGB32 but
95 // I don't really know if we need to differ between ARGB32 and RGB24 here
96 if (aImageFormat == gfxASurface::ImageFormatARGB32 ||
97 aImageFormat == gfxASurface::ImageFormatRGB24)
99 newSurface = new gfxOS2Surface(aSize, aImageFormat);
100 } else if (aImageFormat == gfxASurface::ImageFormatA8 ||
101 aImageFormat == gfxASurface::ImageFormatA1) {
102 newSurface = new gfxImageSurface(aSize, aImageFormat);
103 } else {
104 return nsnull;
107 NS_IF_ADDREF(newSurface);
108 return newSurface;
111 nsresult
112 gfxOS2Platform::GetFontList(const nsACString& aLangGroup,
113 const nsACString& aGenericFamily,
114 nsStringArray& aListOfFonts)
116 #ifdef DEBUG_thebes
117 char *langgroup = ToNewCString(aLangGroup),
118 *family = ToNewCString(aGenericFamily);
119 printf("gfxOS2Platform::GetFontList(%s, %s, ..)\n",
120 langgroup, family);
121 free(langgroup);
122 free(family);
123 #endif
124 return sFontconfigUtils->GetFontList(aLangGroup, aGenericFamily,
125 aListOfFonts);
128 nsresult gfxOS2Platform::UpdateFontList()
130 #ifdef DEBUG_thebes
131 printf("gfxOS2Platform::UpdateFontList()\n");
132 #endif
133 mCodepointsWithNoFonts.reset();
135 nsresult rv = sFontconfigUtils->UpdateFontList();
137 // initialize ranges of characters for which system-wide font search should be skipped
138 mCodepointsWithNoFonts.SetRange(0,0x1f); // C0 controls
139 mCodepointsWithNoFonts.SetRange(0x7f,0x9f); // C1 controls
140 return rv;
143 nsresult
144 gfxOS2Platform::ResolveFontName(const nsAString& aFontName,
145 FontResolverCallback aCallback,
146 void *aClosure, PRBool& aAborted)
148 #ifdef DEBUG_thebes
149 char *fontname = ToNewCString(aFontName);
150 printf("gfxOS2Platform::ResolveFontName(%s, ...)\n", fontname);
151 free(fontname);
152 #endif
153 return sFontconfigUtils->ResolveFontName(aFontName, aCallback, aClosure,
154 aAborted);
157 nsresult
158 gfxOS2Platform::GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName)
160 return sFontconfigUtils->GetStandardFamilyName(aFontName, aFamilyName);
163 gfxFontGroup *
164 gfxOS2Platform::CreateFontGroup(const nsAString &aFamilies,
165 const gfxFontStyle *aStyle,
166 gfxUserFontSet *aUserFontSet)
168 return new gfxOS2FontGroup(aFamilies, aStyle, aUserFontSet);
171 already_AddRefed<gfxOS2Font>
172 gfxOS2Platform::FindFontForChar(PRUint32 aCh, gfxOS2Font *aFont)
174 #ifdef DEBUG_thebes
175 printf("gfxOS2Platform::FindFontForChar(%d, ...)\n", aCh);
176 #endif
178 // is codepoint with no matching font? return null immediately
179 if (mCodepointsWithNoFonts.test(aCh)) {
180 return nsnull;
183 // the following is not very clever but it's a quick fix to search all fonts
184 // (one should instead cache the charmaps as done on Mac and Win)
186 // just continue to append all fonts known to the system
187 nsStringArray fontList;
188 nsCAutoString generic;
189 nsresult rv = GetFontList(aFont->GetStyle()->langGroup, generic, fontList);
190 if (NS_SUCCEEDED(rv)) {
191 // start at 3 to skip over the generic entries
192 for (int i = 3; i < fontList.Count(); i++) {
193 #ifdef DEBUG_thebes
194 printf("searching in entry i=%d (%s)\n",
195 i, NS_LossyConvertUTF16toASCII(*fontList[i]).get());
196 #endif
197 nsRefPtr<gfxOS2Font> font =
198 gfxOS2Font::GetOrMakeFont(*fontList[i], aFont->GetStyle());
199 if (!font)
200 continue;
201 FT_Face face = cairo_ft_scaled_font_lock_face(font->CairoScaledFont());
202 if (!face || !face->charmap) {
203 if (face)
204 cairo_ft_scaled_font_unlock_face(font->CairoScaledFont());
205 continue;
208 FT_UInt gid = FT_Get_Char_Index(face, aCh); // find the glyph id
209 if (gid != 0) {
210 // this is the font
211 cairo_ft_scaled_font_unlock_face(font->CairoScaledFont());
212 return font.forget();
217 // no match found, so add to the set of non-matching codepoints
218 mCodepointsWithNoFonts.set(aCh);
219 return nsnull;