~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/gfx/src/freetype/nsFreeType.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ex: set tabstop=8 softtabstop=2 shiftwidth=2 expandtab: */
 
3
/* ***** BEGIN LICENSE BLOCK *****
 
4
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1 
 
5
 *
 
6
 * The contents of this file are subject to the Netscape Public License
 
7
 * Version 1.1 (the "License"); you may not use this file except in
 
8
 * compliance with the License. You may obtain a copy of the License at
 
9
 * http://www.mozilla.org/NPL/ 
 
10
 *
 
11
 * Software distributed under the License is distributed on an "AS IS" basis,
 
12
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
13
 * for the specific language governing rights and limitations under the
 
14
 * License.
 
15
 *
 
16
 * The Original Code is mozilla.org code.
 
17
 *
 
18
 * The Initial Developer of the Original Code is
 
19
 * Netscape Communications Corporation.
 
20
 * Portions created by the Initial Developer are Copyright (C) 1998
 
21
 * the Initial Developer. All Rights Reserved.
 
22
 *
 
23
 * Contributor(s):
 
24
 *   Brian Stell <bstell@netscape.com>
 
25
 *
 
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 NPL, 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 NPL, the GPL or the LGPL.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
#include "math.h"
 
41
#include "nsIPref.h"
 
42
#include "nsCOMPtr.h"
 
43
#include "nsIServiceManager.h"
 
44
#include "nsICharRepresentable.h"
 
45
#include "nsCompressedCharMap.h"
 
46
#include "nsICharsetConverterManager.h"
 
47
#include "nsIRenderingContext.h"
 
48
#include "nsFreeType.h"
 
49
 
 
50
#include <ft2build.h>
 
51
#include FT_FREETYPE_H
 
52
#include FT_GLYPH_H
 
53
#include FT_TRUETYPE_TABLES_H
 
54
#include FT_TRUETYPE_IDS_H
 
55
 
 
56
# define FREETYPE_PRINTF(x) \
 
57
            PR_BEGIN_MACRO \
 
58
              if (gFreeTypeDebug) { \
 
59
                printf x ; \
 
60
                printf(", %s %d\n", __FILE__, __LINE__); \
 
61
              } \
 
62
            PR_END_MACRO
 
63
 
 
64
PRUint32 gFreeTypeDebug = 0;
 
65
 
 
66
//
 
67
// these belong in nsFontFreeType
 
68
//
 
69
PRBool       nsFreeType2::gFreeType2Autohinted = PR_FALSE;
 
70
PRBool       nsFreeType2::gFreeType2Unhinted = PR_TRUE;
 
71
PRUint8      nsFreeType2::gAATTDarkTextMinValue = 64;
 
72
double       nsFreeType2::gAATTDarkTextGain = 0.8;
 
73
PRInt32      nsFreeType2::gAntiAliasMinimum = 8;
 
74
PRInt32      nsFreeType2::gEmbeddedBitmapMaximumHeight = 1000000;
 
75
nsHashtable* nsFreeType2::sFontFamilies = nsnull;
 
76
nsHashtable* nsFreeType2::sRange1CharSetNames = nsnull;
 
77
nsHashtable* nsFreeType2::sRange2CharSetNames = nsnull;
 
78
nsICharsetConverterManager* nsFreeType2::sCharSetManager = nsnull;
 
79
PRBool       nsFreeType2::gHasExtFunc = PR_TRUE;
 
80
 
 
81
extern nsulCodePageRangeCharSetName ulCodePageRange1CharSetNames[];
 
82
extern nsulCodePageRangeCharSetName ulCodePageRange2CharSetNames[];
 
83
extern nsTTFontFamilyEncoderInfo    gFontFamilyEncoderInfo[];
 
84
 
 
85
typedef int Error;
 
86
 
 
87
/*FT_CALLBACK_DEF*/ FT_Error nsFreeTypeFaceRequester(FTC_FaceID, FT_Library, 
 
88
                                                 FT_Pointer, FT_Face*);
 
89
static FT_Error nsFreeType2__DummyFunc();
 
90
 
 
91
static nsHashtable* gFreeTypeFaces = nsnull;
 
92
 
 
93
static NS_DEFINE_CID(kCharSetManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
 
94
 
 
95
//
 
96
// Define the FreeType2 functions we resolve at run time.
 
97
// see the comment near nsFreeType2::DoneFace() for more info
 
98
//
 
99
#define NS_FT2_OFFSET(f) (NS_PTR_TO_INT32(&((nsFreeType2*)0)->f))
 
100
FtFuncList nsFreeType2::FtFuncs [] = {
 
101
  {"FT_Done_Face",            NS_FT2_OFFSET(nsFT_Done_Face),            PR_TRUE},
 
102
  {"FT_Done_FreeType",        NS_FT2_OFFSET(nsFT_Done_FreeType),        PR_TRUE},
 
103
  {"FT_Done_Glyph",           NS_FT2_OFFSET(nsFT_Done_Glyph),           PR_TRUE},
 
104
  {"FT_Get_Char_Index",       NS_FT2_OFFSET(nsFT_Get_Char_Index),       PR_TRUE},
 
105
  {"FT_Get_Glyph",            NS_FT2_OFFSET(nsFT_Get_Glyph),            PR_TRUE},
 
106
  {"FT_Get_Sfnt_Table",       NS_FT2_OFFSET(nsFT_Get_Sfnt_Table),       PR_TRUE},
 
107
  {"FT_Glyph_Get_CBox",       NS_FT2_OFFSET(nsFT_Glyph_Get_CBox),       PR_TRUE},
 
108
  {"FT_Init_FreeType",        NS_FT2_OFFSET(nsFT_Init_FreeType),        PR_TRUE},
 
109
  {"FT_Load_Glyph",           NS_FT2_OFFSET(nsFT_Load_Glyph),           PR_TRUE},
 
110
  {"FT_New_Face",             NS_FT2_OFFSET(nsFT_New_Face),             PR_TRUE},
 
111
  {"FT_Outline_Decompose",    NS_FT2_OFFSET(nsFT_Outline_Decompose),    PR_TRUE},
 
112
  {"FT_Set_Charmap",          NS_FT2_OFFSET(nsFT_Set_Charmap),          PR_TRUE},
 
113
  {"FTC_Image_Cache_Lookup",  NS_FT2_OFFSET(nsFTC_Image_Cache_Lookup),  PR_TRUE},
 
114
  {"FTC_Manager_Lookup_Size", NS_FT2_OFFSET(nsFTC_Manager_Lookup_Size), PR_TRUE},
 
115
  {"FTC_Manager_Done",        NS_FT2_OFFSET(nsFTC_Manager_Done),        PR_TRUE},
 
116
  {"FTC_Manager_New",         NS_FT2_OFFSET(nsFTC_Manager_New),         PR_TRUE},
 
117
  {"FTC_Image_Cache_New",     NS_FT2_OFFSET(nsFTC_Image_Cache_New),     PR_TRUE},
 
118
// #ifdef MOZ_SVG
 
119
  {"FT_Glyph_Transform",      NS_FT2_OFFSET(nsFT_Glyph_Transform),      PR_TRUE},
 
120
  {"FT_Get_Kerning",          NS_FT2_OFFSET(nsFT_Get_Kerning),          PR_TRUE},
 
121
  {"FT_Glyph_Copy",           NS_FT2_OFFSET(nsFT_Glyph_Copy),           PR_TRUE},
 
122
  {"FT_Glyph_To_Bitmap",      NS_FT2_OFFSET(nsFT_Glyph_To_Bitmap),      PR_TRUE},
 
123
// #endif
 
124
  {"FT_Get_First_Char",       NS_FT2_OFFSET(nsFT_Get_First_Char),       PR_FALSE},
 
125
  {"FT_Get_Next_Char",        NS_FT2_OFFSET(nsFT_Get_Next_Char),        PR_FALSE},
 
126
  {nsnull,                    0, 0}
 
127
};
 
128
 
 
129
nsTTFontEncoderInfo FEI_Adobe_Symbol_Encoding = {
 
130
  "Adobe-Symbol-Encoding", TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, nsnull
 
131
};
 
132
nsTTFontEncoderInfo FEI_x_ttf_cmr = {
 
133
  "x-ttf-cmr", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
 
134
};
 
135
nsTTFontEncoderInfo FEI_x_ttf_cmmi = {
 
136
  "x-ttf-cmmi", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
 
137
};
 
138
nsTTFontEncoderInfo FEI_x_ttf_cmsy = {
 
139
  "x-ttf-cmsy", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
 
140
};
 
141
nsTTFontEncoderInfo FEI_x_ttf_cmex = {
 
142
  "x-ttf-cmex", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
 
143
};
 
144
nsTTFontEncoderInfo FEI_x_mathematica1 = {
 
145
  "x-mathematica1", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
 
146
};
 
147
nsTTFontEncoderInfo FEI_x_mathematica2 = {
 
148
  "x-mathematica2", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
 
149
};
 
150
nsTTFontEncoderInfo FEI_x_mathematica3 = {
 
151
  "x-mathematica3", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
 
152
};
 
153
nsTTFontEncoderInfo FEI_x_mathematica4 = {
 
154
  "x-mathematica4", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
 
155
};
 
156
nsTTFontEncoderInfo FEI_x_mathematica5 = {
 
157
  "x-mathematica5", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
 
158
};
 
159
nsTTFontEncoderInfo FEI_x_mtextra = {
 
160
  "x-mtextra", TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, nsnull
 
161
};
 
162
nsTTFontEncoderInfo FEI_windows_1252 = {
 
163
  "windows-1252", TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, nsnull
 
164
};
 
165
 
 
166
 
 
167
///////////////////////////////////////////////////////////////////////
 
168
//
 
169
// class nsFreeType2 data/functions
 
170
//
 
171
///////////////////////////////////////////////////////////////////////
 
172
 
 
173
NS_IMPL_ISUPPORTS1(nsFreeType2, nsIFreeType2)
 
174
 
 
175
//
 
176
// Since the Freetype2 library may not be available on the user's
 
177
// system we cannot link directly to it otherwise the whole app would
 
178
// fail to run on those systems.  Instead we access the FreeType2
 
179
// functions via function pointers.
 
180
//
 
181
// If we can load the Freetype2 library with PR_LoadLIbrary we to load 
 
182
// pointers to the Ft2 functions. If not, then the function pointers
 
183
// point to a dummy function which always returns an error.
 
184
//
 
185
 
 
186
NS_IMETHODIMP
 
187
nsFreeType2::DoneFace(FT_Face face)
 
188
 
189
  // call the FreeType2 function via the function pointer
 
190
  FT_Error error = nsFT_Done_Face(face);
 
191
  return error ? NS_ERROR_FAILURE : NS_OK;
 
192
 
193
 
 
194
NS_IMETHODIMP
 
195
nsFreeType2::DoneFreeType(FT_Library library)
 
196
 
197
  // call the FreeType2 function via the function pointer
 
198
  FT_Error error = nsFT_Done_FreeType(library);
 
199
  return error ? NS_ERROR_FAILURE : NS_OK;
 
200
 
201
 
 
202
NS_IMETHODIMP
 
203
nsFreeType2::DoneGlyph(FT_Glyph glyph)
 
204
 
205
  // call the FreeType2 function via the function pointer
 
206
  nsFT_Done_Glyph(glyph);
 
207
  return NS_OK;
 
208
 
209
 
 
210
NS_IMETHODIMP
 
211
nsFreeType2::GetCharIndex(FT_Face face, FT_ULong charcode, FT_UInt *index)
 
212
 
213
  // call the FreeType2 function via the function pointer
 
214
  *index = nsFT_Get_Char_Index(face, charcode);
 
215
  return NS_OK;
 
216
 
217
 
 
218
NS_IMETHODIMP
 
219
nsFreeType2::GetGlyph(FT_GlyphSlot slot, FT_Glyph *glyph)
 
220
 
221
  // call the FreeType2 function via the function pointer
 
222
  FT_Error error = nsFT_Get_Glyph(slot, glyph);
 
223
  return error ? NS_ERROR_FAILURE : NS_OK;
 
224
 
225
 
 
226
NS_IMETHODIMP
 
227
nsFreeType2::GetSfntTable(FT_Face face, FT_Sfnt_Tag tag, void** table)
 
228
 
229
  // call the FreeType2 function via the function pointer
 
230
  *table = nsFT_Get_Sfnt_Table(face, tag);
 
231
  return NS_OK;
 
232
 
233
 
 
234
NS_IMETHODIMP
 
235
nsFreeType2::GlyphGetCBox(FT_Glyph glyph, FT_UInt mode, FT_BBox *bbox)
 
236
 
237
  // call the FreeType2 function via the function pointer
 
238
  nsFT_Glyph_Get_CBox(glyph, mode, bbox);
 
239
  return NS_OK;
 
240
 
241
 
 
242
NS_IMETHODIMP
 
243
nsFreeType2::InitFreeType(FT_Library *library)
 
244
 
245
  // call the FreeType2 function via the function pointer
 
246
  FT_Error error = nsFT_Init_FreeType(library);
 
247
  return error ? NS_ERROR_FAILURE : NS_OK;
 
248
 
249
 
 
250
NS_IMETHODIMP
 
251
nsFreeType2::LoadGlyph(FT_Face face, FT_UInt glyph, FT_Int flags)
 
252
 
253
  // call the FreeType2 function via the function pointer
 
254
  FT_Error error = nsFT_Load_Glyph(face, glyph, flags);
 
255
  return error ? NS_ERROR_FAILURE : NS_OK;
 
256
 
257
 
 
258
NS_IMETHODIMP
 
259
nsFreeType2::NewFace(FT_Library library, const char *path,
 
260
                         FT_Long face_index, FT_Face *face)
 
261
 
262
  // call the FreeType2 function via the function pointer
 
263
  FT_Error error = nsFT_New_Face(library, path, face_index, face);
 
264
  return error ? NS_ERROR_FAILURE : NS_OK;
 
265
 
266
 
 
267
NS_IMETHODIMP
 
268
nsFreeType2::OutlineDecompose(FT_Outline *outline,
 
269
                              const FT_Outline_Funcs *funcs, void *user)
 
270
 
271
  // call the FreeType2 function via the function pointer
 
272
  FT_Error error = nsFT_Outline_Decompose(outline, funcs, user);
 
273
  return error ? NS_ERROR_FAILURE : NS_OK;
 
274
 
275
 
 
276
NS_IMETHODIMP
 
277
nsFreeType2::SetCharmap(FT_Face face, FT_CharMap  charmap)
 
278
 
279
  // call the FreeType2 function via the function pointer
 
280
  FT_Error error = nsFT_Set_Charmap(face, charmap);
 
281
  return error ? NS_ERROR_FAILURE : NS_OK;
 
282
 
283
 
 
284
NS_IMETHODIMP
 
285
nsFreeType2::ImageCacheLookup(FTC_Image_Cache cache, FTC_Image_Desc *desc,
 
286
                              FT_UInt glyphID, FT_Glyph *glyph)
 
287
 
288
  // call the FreeType2 function via the function pointer
 
289
  FT_Error error = nsFTC_Image_Cache_Lookup(cache, desc, glyphID, glyph);
 
290
  return error ? NS_ERROR_FAILURE : NS_OK;
 
291
 
292
 
 
293
NS_IMETHODIMP
 
294
nsFreeType2::ManagerLookupSize(FTC_Manager manager, FTC_Font font,
 
295
                               FT_Face *face, FT_Size *size)
 
296
 
297
  // call the FreeType2 function via the function pointer
 
298
  FT_Error error = nsFTC_Manager_Lookup_Size(manager, font, face, size);
 
299
  return error ? NS_ERROR_FAILURE : NS_OK;
 
300
 
301
 
 
302
NS_IMETHODIMP
 
303
nsFreeType2::ManagerDone(FTC_Manager manager)
 
304
 
305
  // call the FreeType2 function via the function pointer
 
306
  nsFTC_Manager_Done(manager);
 
307
  return NS_OK;
 
308
 
309
 
 
310
NS_IMETHODIMP
 
311
nsFreeType2::ManagerNew(FT_Library library, FT_UInt max_faces,
 
312
                        FT_UInt max_sizes, FT_ULong max_bytes,
 
313
                        FTC_Face_Requester requester, FT_Pointer req_data,
 
314
                        FTC_Manager *manager)
 
315
 
316
  // call the FreeType2 function via the function pointer
 
317
  FT_Error error = nsFTC_Manager_New(library, max_faces, max_sizes, max_bytes,
 
318
                                     requester, req_data, manager);
 
319
  return error ? NS_ERROR_FAILURE : NS_OK;
 
320
 
321
 
 
322
NS_IMETHODIMP
 
323
nsFreeType2::ImageCacheNew(FTC_Manager manager, FTC_Image_Cache *cache)
 
324
 
325
  // call the FreeType2 function via the function pointer
 
326
  FT_Error error = nsFTC_Image_Cache_New(manager, cache);
 
327
  return error ? NS_ERROR_FAILURE : NS_OK;
 
328
 
329
 
 
330
// #ifdef MOZ_SVG
 
331
NS_IMETHODIMP
 
332
nsFreeType2::GlyphTransform(FT_Glyph glyph, FT_Matrix * matrix, FT_Vector * delta)
 
333
{
 
334
  // call the FreeType2 function via the function pointer
 
335
  FT_Error error = nsFT_Glyph_Transform(glyph, matrix, delta);
 
336
  return error ? NS_ERROR_FAILURE : NS_OK;
 
337
}
 
338
 
 
339
NS_IMETHODIMP
 
340
nsFreeType2::GetKerning(FT_Face face, FT_UInt left_glyph, FT_UInt right_glyph,
 
341
                        FT_UInt kern_mode, FT_Vector *akerning)
 
342
{
 
343
  // call the FreeType2 function via the function pointer
 
344
  FT_Error error = nsFT_Get_Kerning(face, left_glyph, right_glyph,
 
345
                                    kern_mode, akerning);
 
346
  return error ? NS_ERROR_FAILURE : NS_OK;
 
347
}
 
348
 
 
349
NS_IMETHODIMP
 
350
nsFreeType2::GlyphCopy(FT_Glyph source, FT_Glyph *target)
 
351
{
 
352
  // call the FreeType2 function via the function pointer
 
353
  FT_Error error = nsFT_Glyph_Copy(source, target);
 
354
  return error ? NS_ERROR_FAILURE : NS_OK;
 
355
}
 
356
 
 
357
NS_IMETHODIMP
 
358
nsFreeType2::GlyphToBitmap(FT_Glyph *the_glyph, FT_Render_Mode render_mode,
 
359
                           FT_Vector * origin, FT_Bool destroy)
 
360
{
 
361
  // call the FreeType2 function via the function pointer
 
362
  FT_Error error = nsFT_Glyph_To_Bitmap(the_glyph, render_mode, origin, destroy);
 
363
  return error ? NS_ERROR_FAILURE : NS_OK;
 
364
}
 
365
 
 
366
// #endif
 
367
 
 
368
NS_IMETHODIMP
 
369
nsFreeType2::GetFirstChar(FT_Face face, FT_UInt *gindex, FT_ULong *charcode)
 
370
 
371
  // call the FreeType2 function via the function pointer
 
372
  *charcode = nsFT_Get_First_Char(face, gindex);
 
373
  return NS_OK;
 
374
 
375
 
 
376
NS_IMETHODIMP
 
377
nsFreeType2::GetNextChar(FT_Face face, FT_ULong charcode, FT_UInt *gindex, FT_ULong *ncharcode)
 
378
 
379
  // call the FreeType2 function via the function pointer
 
380
  *ncharcode = nsFT_Get_Next_Char(face, charcode, gindex);
 
381
  return NS_OK;
 
382
 
383
 
 
384
NS_IMETHODIMP
 
385
nsFreeType2::SupportsExtFunc(PRBool *res)
 
386
 
387
  *res = gHasExtFunc;
 
388
  return NS_OK;
 
389
 
390
 
 
391
NS_IMETHODIMP
 
392
nsFreeType2::GetImageCache(FTC_Image_Cache *aCache)
 
393
{
 
394
  *aCache = mImageCache;
 
395
  return NS_OK;
 
396
}
 
397
 
 
398
NS_IMETHODIMP
 
399
nsFreeType2::GetFTCacheManager(FTC_Manager *aManager)
 
400
 
401
  *aManager = mFTCacheManager;
 
402
  return NS_OK;
 
403
 
404
 
 
405
NS_IMETHODIMP
 
406
nsFreeType2::GetLibrary(FT_Library *aLibrary)
 
407
 
408
  *aLibrary = mFreeTypeLibrary;
 
409
  return NS_OK;
 
410
 
411
 
 
412
void
 
413
nsFreeType2::ClearFunctions()
 
414
{
 
415
  FtFuncList *p;
 
416
  void *ptr = this;
 
417
  for (p=FtFuncs; p->FuncName; p++) {
 
418
    *((PRFuncPtr*)((char*)ptr+p->FuncOffset)) = 
 
419
                        (PRFuncPtr)&nsFreeType2__DummyFunc;
 
420
  }
 
421
}
 
422
 
 
423
void
 
424
nsFreeType2::ClearGlobals()
 
425
{
 
426
  mSharedLib = nsnull;
 
427
  mFreeTypeLibrary = nsnull;
 
428
  mFTCacheManager  = nsnull;
 
429
  mImageCache      = nsnull;
 
430
}
 
431
 
 
432
// I would like to make this a static member function but the compilier 
 
433
// warning about converting a data pointer to a function pointer cannot
 
434
// distinguish static member functions from static data members
 
435
static FT_Error
 
436
nsFreeType2__DummyFunc()
 
437
{
 
438
  NS_ERROR("nsFreeType2__DummyFunc should never be called");
 
439
  return 1;
 
440
}
 
441
 
 
442
nsFreeType2::~nsFreeType2()
 
443
{
 
444
  FreeGlobals();
 
445
}
 
446
 
 
447
void
 
448
nsFreeType2::FreeGlobals()
 
449
{
 
450
  if (mFreeType2SharedLibraryName) {
 
451
    free(mFreeType2SharedLibraryName);
 
452
    mFreeType2SharedLibraryName = nsnull;
 
453
  }
 
454
  if (gFreeTypeFaces) {
 
455
    gFreeTypeFaces->Reset(nsFreeTypeFace::FreeFace, nsnull);
 
456
    delete gFreeTypeFaces;
 
457
    gFreeTypeFaces = nsnull;
 
458
  }
 
459
  // mImageCache released by cache manager
 
460
  if (mFTCacheManager) {
 
461
    // use "this->" to make sure it is obivious we are calling the member func
 
462
    this->ManagerDone(mFTCacheManager);
 
463
    mFTCacheManager = nsnull;
 
464
  }
 
465
  if (mFreeTypeLibrary) {
 
466
    // use "this->" to make sure it is obivious we are calling the member func
 
467
    this->DoneFreeType(mFreeTypeLibrary);
 
468
    mFreeTypeLibrary = nsnull;
 
469
  }
 
470
  
 
471
  if (sRange1CharSetNames)
 
472
    delete sRange1CharSetNames;
 
473
  if (sRange2CharSetNames)
 
474
    delete sRange2CharSetNames;
 
475
  if (sFontFamilies)
 
476
    delete sFontFamilies;
 
477
  
 
478
  NS_IF_RELEASE(sCharSetManager);
 
479
  
 
480
  // release any encoders that were created
 
481
  int i;
 
482
  for (i=0; gFontFamilyEncoderInfo[i].mFamilyName; i++) {
 
483
    nsTTFontFamilyEncoderInfo *ffei = &gFontFamilyEncoderInfo[i];
 
484
    nsTTFontEncoderInfo *fei = ffei->mEncodingInfo;
 
485
    NS_IF_RELEASE(fei->mConverter);
 
486
  }
 
487
 
 
488
  UnloadSharedLib();
 
489
  ClearFunctions();
 
490
  ClearGlobals();
 
491
}
 
492
 
 
493
nsresult
 
494
nsFreeType2::Init()
 
495
{
 
496
  // set all the globals to default values
 
497
  ClearGlobals();
 
498
 
 
499
  nsulCodePageRangeCharSetName *crn = nsnull;
 
500
  nsTTFontFamilyEncoderInfo *ff = gFontFamilyEncoderInfo;
 
501
  nsCOMPtr<nsIPref> mPref = do_GetService(NS_PREF_CONTRACTID);
 
502
  
 
503
  if (!mPref) {
 
504
    FreeGlobals();
 
505
    return NS_ERROR_FAILURE;
 
506
  }
 
507
  nsresult rv;
 
508
 
 
509
  PRBool enable_freetype2 = PR_TRUE;
 
510
  rv = mPref->GetBoolPref("font.FreeType2.enable", &enable_freetype2);
 
511
  if (NS_SUCCEEDED(rv)) {
 
512
    mEnableFreeType2 = enable_freetype2;
 
513
    FREETYPE_PRINTF(("mEnableFreeType2 = %d", mEnableFreeType2));
 
514
  }
 
515
 
 
516
  rv = mPref->GetCharPref("font.freetype2.shared-library",
 
517
                          &mFreeType2SharedLibraryName);
 
518
  if (NS_FAILED(rv)) {
 
519
    enable_freetype2 = PR_FALSE;
 
520
    FREETYPE_PRINTF((
 
521
                  "mFreeType2SharedLibraryName missing, FreeType2 disabled"));
 
522
    mFreeType2SharedLibraryName = nsnull;
 
523
  }
 
524
 
 
525
  PRBool freetype2_autohinted = PR_FALSE;
 
526
  rv = mPref->GetBoolPref("font.FreeType2.autohinted", &freetype2_autohinted);
 
527
  if (NS_SUCCEEDED(rv)) {
 
528
    gFreeType2Autohinted = freetype2_autohinted;
 
529
    FREETYPE_PRINTF(("gFreeType2Autohinted = %d", gFreeType2Autohinted));
 
530
  }
 
531
  
 
532
  PRBool freetype2_unhinted = PR_TRUE;
 
533
  rv = mPref->GetBoolPref("font.FreeType2.unhinted", &freetype2_unhinted);
 
534
  if (NS_SUCCEEDED(rv)) {
 
535
    gFreeType2Unhinted = freetype2_unhinted;
 
536
    FREETYPE_PRINTF(("gFreeType2Unhinted = %d", gFreeType2Unhinted));
 
537
  }
 
538
 
 
539
  PRInt32 int_val = 0;
 
540
  rv = mPref->GetIntPref("font.scale.tt_bitmap.dark_text.min", &int_val);
 
541
  if (NS_SUCCEEDED(rv)) {
 
542
    gAATTDarkTextMinValue = int_val;
 
543
    FREETYPE_PRINTF(("gAATTDarkTextMinValue = %d", gAATTDarkTextMinValue));
 
544
  }
 
545
 
 
546
  nsXPIDLCString str;
 
547
  rv = mPref->GetCharPref("font.scale.tt_bitmap.dark_text.gain",
 
548
                           getter_Copies(str));
 
549
  if (NS_SUCCEEDED(rv)) {
 
550
    gAATTDarkTextGain = atof(str.get());
 
551
    FREETYPE_PRINTF(("gAATTDarkTextGain = %g", gAATTDarkTextGain));
 
552
  }
 
553
 
 
554
  PRInt32 antialias_minimum = 8;
 
555
  rv = mPref->GetIntPref("font.antialias.min", &antialias_minimum);
 
556
  if (NS_SUCCEEDED(rv)) {
 
557
    gAntiAliasMinimum = antialias_minimum;
 
558
    FREETYPE_PRINTF(("gAntiAliasMinimum = %d", gAntiAliasMinimum));
 
559
  }
 
560
 
 
561
  PRInt32 embedded_bitmaps_maximum = 1000000;
 
562
  rv = mPref->GetIntPref("font.embedded_bitmaps.max",&embedded_bitmaps_maximum);
 
563
  if (NS_SUCCEEDED(rv)) {
 
564
    gEmbeddedBitmapMaximumHeight = embedded_bitmaps_maximum;
 
565
    FREETYPE_PRINTF(("gEmbeddedBitmapMaximumHeight = %d",
 
566
                             gEmbeddedBitmapMaximumHeight));
 
567
  }
 
568
  
 
569
  if (NS_FAILED(rv)) {
 
570
    mEnableFreeType2 = PR_FALSE;
 
571
    mFreeType2SharedLibraryName = nsnull;
 
572
    gFreeType2Autohinted = PR_FALSE;
 
573
    gFreeType2Unhinted = PR_TRUE;
 
574
    gAATTDarkTextMinValue = 64;
 
575
    gAATTDarkTextGain = 0.8;
 
576
    gAntiAliasMinimum = 8;
 
577
    gEmbeddedBitmapMaximumHeight = 1000000;
 
578
  }
 
579
  
 
580
  mPref = nsnull;
 
581
  
 
582
  if (!InitLibrary()) {
 
583
    FreeGlobals();
 
584
    return NS_ERROR_OUT_OF_MEMORY;
 
585
  }
 
586
  gFreeTypeFaces = new nsHashtable();
 
587
  if (!gFreeTypeFaces) {
 
588
    FreeGlobals();
 
589
    return NS_ERROR_OUT_OF_MEMORY;
 
590
  }
 
591
 
 
592
  sRange1CharSetNames = new nsHashtable();
 
593
  if (!sRange1CharSetNames) {
 
594
    FreeGlobals();
 
595
    return NS_ERROR_OUT_OF_MEMORY;
 
596
  }
 
597
  crn = ulCodePageRange1CharSetNames;
 
598
  while (crn->charsetName) {
 
599
    char buf[32];
 
600
    sprintf(buf, "0x%08lx", crn->bit);
 
601
    nsCStringKey key(buf);
 
602
    sRange1CharSetNames->Put(&key, (void*)crn->charsetName);
 
603
    crn++;
 
604
  }
 
605
 
 
606
  sRange2CharSetNames = new nsHashtable();
 
607
  if (!sRange2CharSetNames) {
 
608
    FreeGlobals();
 
609
    return NS_ERROR_OUT_OF_MEMORY;
 
610
  }
 
611
  crn = ulCodePageRange2CharSetNames;
 
612
  while (crn->charsetName) {
 
613
    char buf[32];
 
614
    sprintf(buf, "0x%08lx", crn->bit);
 
615
    nsCStringKey key(buf);
 
616
    sRange2CharSetNames->Put(&key, (void*)crn->charsetName);
 
617
    crn++;
 
618
  }
 
619
 
 
620
  sFontFamilies = new nsHashtable();
 
621
  if (!sFontFamilies) {
 
622
    FreeGlobals();
 
623
    return NS_ERROR_OUT_OF_MEMORY;
 
624
  }
 
625
  while (ff->mFamilyName) {
 
626
    nsCAutoString name(ff->mFamilyName);
 
627
    ToLowerCase(name);
 
628
    nsCStringKey key(name);
 
629
    sFontFamilies->Put(&key, (void*)ff);
 
630
    ff++;
 
631
  }
 
632
 
 
633
  return NS_OK;
 
634
}
 
635
 
 
636
PRBool
 
637
nsFreeType2::InitLibrary()
 
638
{
 
639
  FT_Error error;
 
640
#ifdef MOZ_MATHML
 
641
  // do not yet support MathML
 
642
  // goto cleanup_and_return;
 
643
#endif
 
644
 
 
645
  if (!mEnableFreeType2)
 
646
    return PR_FALSE;
 
647
 
 
648
  // since the library may not be available on any given system
 
649
  // failing to load is not considered a fatal error
 
650
  if (!LoadSharedLib())
 
651
    return PR_FALSE;
 
652
 
 
653
  // use "this->" to make sure it is obivious we are calling the member func
 
654
  nsresult rv = this->InitFreeType(&mFreeTypeLibrary);
 
655
  if (NS_FAILED(rv)) {
 
656
    FREETYPE_PRINTF(("\n\n*********\nFreeType initialization error = %d",
 
657
                         error));
 
658
    mFreeTypeLibrary = nsnull;
 
659
    goto cleanup_and_return;
 
660
  }
 
661
  // use "this->" to make sure it is obivious we are calling the member func
 
662
  rv = this->ManagerNew(mFreeTypeLibrary, 0, 0, 0, nsFreeTypeFaceRequester,
 
663
                         this, &mFTCacheManager);
 
664
  NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create FreeType Cache manager");
 
665
  if (NS_FAILED(rv))
 
666
    goto cleanup_and_return;
 
667
  // use "this->" to make sure it is obivious we are calling the member func
 
668
  rv = this->ImageCacheNew(mFTCacheManager, &mImageCache);
 
669
  NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create FreeType image cache");
 
670
  if (NS_FAILED(rv))
 
671
    goto cleanup_and_return;
 
672
  return PR_TRUE;
 
673
 
 
674
cleanup_and_return:
 
675
  // clean everything up but note that init was called
 
676
  FreeGlobals();
 
677
  return(PR_FALSE);
 
678
}
 
679
 
 
680
PRBool
 
681
nsFreeType2::LoadSharedLib()
 
682
{
 
683
  NS_ASSERTION(mSharedLib==nsnull, "library already loaded");
 
684
 
 
685
  if (!mFreeType2SharedLibraryName)
 
686
    return PR_FALSE;
 
687
  mSharedLib = PR_LoadLibrary(mFreeType2SharedLibraryName);
 
688
  // since the library may not be available on any given system
 
689
  // failing to load is not considered a fatal error
 
690
  if (!mSharedLib) {
 
691
    NS_WARNING("freetype library not found");
 
692
    return PR_FALSE;
 
693
  }
 
694
 
 
695
  // checking FT_Get_First_Char() and FT_Get_Next_Char() for fast lookup methods
 
696
  gHasExtFunc = PR_TRUE;
 
697
  FtFuncList *p;
 
698
  PRFuncPtr func;
 
699
  void *ptr = this;
 
700
  for (p=FtFuncs; p->FuncName; p++) {
 
701
    func = PR_FindFunctionSymbol(mSharedLib, p->FuncName);
 
702
    if (!func) {
 
703
      if (p->Required == PR_TRUE) {
 
704
        NS_WARNING("nsFreeType2::LoadSharedLib Error");
 
705
        ClearFunctions();
 
706
        return PR_FALSE;
 
707
      } else {
 
708
        // should not return, but disable fast lookup methods
 
709
        gHasExtFunc = PR_FALSE;
 
710
      }
 
711
    }
 
712
    *((PRFuncPtr*)((char*)ptr+p->FuncOffset)) = func;
 
713
  }
 
714
 
 
715
  return PR_TRUE;
 
716
 
 
717
}
 
718
 
 
719
void
 
720
nsFreeType2::UnloadSharedLib()
 
721
{
 
722
  if (mSharedLib)
 
723
    PR_UnloadLibrary(mSharedLib);
 
724
  mSharedLib = nsnull;
 
725
}
 
726
 
 
727
const char *
 
728
nsFreeType2::GetRange1CharSetName(unsigned long aBit)
 
729
{
 
730
  char buf[32];
 
731
  sprintf(buf, "0x%08lx", aBit);
 
732
  nsCStringKey key(buf);
 
733
  const char *charsetName = (const char *)sRange1CharSetNames->Get(&key);
 
734
  return charsetName;
 
735
}
 
736
 
 
737
const char *
 
738
nsFreeType2::GetRange2CharSetName(unsigned long aBit)
 
739
{
 
740
  char buf[32];
 
741
  sprintf(buf, "0x%08lx", aBit);
 
742
  nsCStringKey key(buf);
 
743
  const char *charsetName = (const char *)sRange2CharSetNames->Get(&key);
 
744
  return charsetName;
 
745
}
 
746
 
 
747
nsTTFontFamilyEncoderInfo*
 
748
nsFreeType2::GetCustomEncoderInfo(const char * aFamilyName)
 
749
{
 
750
  if (!sFontFamilies)
 
751
    return nsnull;
 
752
 
 
753
  nsTTFontFamilyEncoderInfo *ffei;
 
754
  nsCAutoString name(aFamilyName);
 
755
  ToLowerCase(name);
 
756
  nsCStringKey key(name);
 
757
  ffei = (nsTTFontFamilyEncoderInfo*)sFontFamilies->Get(&key);
 
758
  if (!ffei)
 
759
    return nsnull;
 
760
 
 
761
  // init the converter
 
762
  if (!ffei->mEncodingInfo->mConverter) {
 
763
    nsTTFontEncoderInfo *fei = ffei->mEncodingInfo;
 
764
    //
 
765
    // build the converter
 
766
    //
 
767
    nsICharsetConverterManager* charSetManager = GetCharSetManager();
 
768
    if (!charSetManager)
 
769
      return nsnull;
 
770
    nsresult res;
 
771
    res = charSetManager->GetUnicodeEncoderRaw(fei->mConverterName, &fei->mConverter);
 
772
    if (NS_FAILED(res)) {
 
773
      return nsnull;
 
774
    }
 
775
  }
 
776
  return ffei;
 
777
}
 
778
 
 
779
nsICharsetConverterManager*
 
780
nsFreeType2::GetCharSetManager()
 
781
{
 
782
  if (!sCharSetManager) {
 
783
    //
 
784
    // get the sCharSetManager
 
785
    //
 
786
    nsServiceManager::GetService(kCharSetManagerCID,
 
787
                                 NS_GET_IID(nsICharsetConverterManager),
 
788
                                 (nsISupports**) &sCharSetManager);
 
789
    NS_ASSERTION(sCharSetManager,"failed to create the charset manager");
 
790
  }
 
791
  return sCharSetManager;
 
792
}
 
793
 
 
794
PRUint16*
 
795
nsFreeType2::GetCCMap(nsFontCatalogEntry *aFce)
 
796
{
 
797
  nsCompressedCharMap ccmapObj;
 
798
  if (aFce->mFlags & FCE_FLAGS_SURROGATE) {
 
799
    ccmapObj.Extend();
 
800
  }
 
801
  ccmapObj.SetChars(aFce->mCCMap);
 
802
  return ccmapObj.NewCCMap();
 
803
}
 
804
 
 
805
///////////////////////////////////////////////////////////////////////
 
806
//
 
807
// class nsFreeTypeFace data/functions
 
808
//
 
809
///////////////////////////////////////////////////////////////////////
 
810
NS_IMPL_ISUPPORTS1(nsFreeTypeFace, nsITrueTypeFontCatalogEntry)
 
811
 
 
812
nsFreeTypeFace::nsFreeTypeFace()
 
813
{
 
814
}
 
815
 
 
816
nsresult nsFreeTypeFace::Init(nsFontCatalogEntry *aFce)
 
817
{
 
818
  NS_ASSERTION(aFce, "init of nsFreeTypeFace needs nsFontCatalogEntry");
 
819
  if (aFce)
 
820
    mFce = aFce;
 
821
  else {
 
822
    mFce = new nsFontCatalogEntry;
 
823
    NS_ASSERTION(mFce, "memory error while creating nsFontCatalogEntry");
 
824
    if (!mFce)
 
825
      return NS_ERROR_OUT_OF_MEMORY;
 
826
  }
 
827
  mCCMap = nsnull;
 
828
  return NS_OK;
 
829
}
 
830
 
 
831
nsFreeTypeFace::~nsFreeTypeFace()
 
832
{
 
833
  if (mCCMap)
 
834
    FreeCCMap(mCCMap);
 
835
}
 
836
 
 
837
NS_IMETHODIMP nsFreeTypeFace::GetFontCatalogType(
 
838
                              PRUint16 *aFontCatalogType)
 
839
{
 
840
  *aFontCatalogType = FONT_CATALOG_TRUETYPE;
 
841
  return NS_OK;
 
842
}
 
843
 
 
844
/* readonly attribute ACString fileName; */
 
845
NS_IMETHODIMP nsFreeTypeFace::GetFileName(nsACString & aFileName)
 
846
{
 
847
  aFileName.Assign(mFce->mFontFileName);
 
848
  return NS_OK;
 
849
}
 
850
 
 
851
/* readonly attribute ACString familyName; */
 
852
NS_IMETHODIMP nsFreeTypeFace::GetFamilyName(nsACString & aFamilyName)
 
853
{
 
854
  aFamilyName.Assign(mFce->mFamilyName);
 
855
  return NS_OK;
 
856
}
 
857
 
 
858
/* readonly attribute ACString styleName; */
 
859
NS_IMETHODIMP nsFreeTypeFace::GetStyleName(nsACString & aStyleName)
 
860
{
 
861
  aStyleName.Assign(mFce->mStyleName);
 
862
  return NS_OK;
 
863
}
 
864
 
 
865
/* readonly attribute ACString vendorID; */
 
866
NS_IMETHODIMP nsFreeTypeFace::GetVendorID(nsACString & aVendorID)
 
867
{
 
868
  aVendorID.Assign(mFce->mVendorID);
 
869
  return NS_OK;
 
870
}
 
871
 
 
872
/* readonly attribute short faceIndex; */
 
873
NS_IMETHODIMP nsFreeTypeFace::GetFaceIndex(PRInt16 *aFaceIndex)
 
874
{
 
875
  *aFaceIndex = mFce->mFaceIndex;
 
876
  return NS_OK;
 
877
}
 
878
 
 
879
/* readonly attribute short numFaces; */
 
880
NS_IMETHODIMP nsFreeTypeFace::GetNumFaces(PRInt16 *aNumFaces)
 
881
{
 
882
  *aNumFaces = mFce->mNumFaces;
 
883
  return NS_OK;
 
884
}
 
885
 
 
886
/* readonly attribute short numEmbeddedBitmaps; */
 
887
NS_IMETHODIMP nsFreeTypeFace::GetNumEmbeddedBitmaps(
 
888
                              PRInt16 *aNumEmbeddedBitmaps)
 
889
{
 
890
  *aNumEmbeddedBitmaps = mFce->mNumEmbeddedBitmaps;
 
891
  return NS_OK;
 
892
}
 
893
 
 
894
/* readonly attribute long numGlyphs; */
 
895
NS_IMETHODIMP nsFreeTypeFace::GetNumGlyphs(PRInt32 *aNumGlyphs)
 
896
{
 
897
  *aNumGlyphs = mFce->mNumGlyphs;
 
898
  return NS_OK;
 
899
}
 
900
 
 
901
/* readonly attribute long numUsableGlyphs; */
 
902
NS_IMETHODIMP nsFreeTypeFace::GetNumUsableGlyphs(
 
903
                              PRInt32 *aNumUsableGlyphs)
 
904
{
 
905
  *aNumUsableGlyphs = mFce->mNumUsableGlyphs;
 
906
  return NS_OK;
 
907
}
 
908
 
 
909
/* readonly attribute unsigned short weight; */
 
910
NS_IMETHODIMP nsFreeTypeFace::GetWeight(PRUint16 *aWeight)
 
911
{
 
912
  *aWeight = mFce->mWeight;
 
913
  return NS_OK;
 
914
}
 
915
 
 
916
/* readonly attribute unsigned short width; */
 
917
NS_IMETHODIMP nsFreeTypeFace::GetWidth(PRUint16 *aWidth)
 
918
{
 
919
  *aWidth = mFce->mWidth;
 
920
  return NS_OK;
 
921
}
 
922
 
 
923
/* readonly attribute unsigned long flags; */
 
924
NS_IMETHODIMP nsFreeTypeFace::GetFlags(PRUint32 *aFlags)
 
925
{
 
926
  *aFlags = mFce->mFlags;
 
927
  return NS_OK;
 
928
}
 
929
 
 
930
/* readonly attribute long long faceFlags; */
 
931
NS_IMETHODIMP nsFreeTypeFace::GetFaceFlags(PRInt64 *aFaceFlags)
 
932
{
 
933
  *aFaceFlags = mFce->mFaceFlags;
 
934
  return NS_OK;
 
935
}
 
936
 
 
937
/* readonly attribute long long styleFlags; */
 
938
NS_IMETHODIMP nsFreeTypeFace::GetStyleFlags(PRInt64 *aStyleFlags)
 
939
{
 
940
  *aStyleFlags = mFce->mStyleFlags;
 
941
  return NS_OK;
 
942
}
 
943
 
 
944
/* readonly attribute unsigned long codePageRange1; */
 
945
NS_IMETHODIMP nsFreeTypeFace::GetCodePageRange1(
 
946
                              PRUint32 *aCodePageRange1)
 
947
{
 
948
  *aCodePageRange1 = mFce->mCodePageRange1;
 
949
  return NS_OK;
 
950
}
 
951
 
 
952
/* readonly attribute unsigned long codePageRange2; */
 
953
NS_IMETHODIMP nsFreeTypeFace::GetCodePageRange2(
 
954
                              PRUint32 *aCodePageRange2)
 
955
{
 
956
  *aCodePageRange2 = mFce->mCodePageRange2;
 
957
  return NS_OK;
 
958
}
 
959
 
 
960
/* readonly attribute long long time; */
 
961
NS_IMETHODIMP nsFreeTypeFace::GetFileModTime(PRInt64 *aTime)
 
962
{
 
963
  *aTime = mFce->mMTime;
 
964
  return NS_OK;
 
965
}
 
966
 
 
967
/* void getCCMap (out unsigned long size,
 
968
 * [array, size_is (size), retval] out unsigned short ccMaps); */
 
969
NS_IMETHODIMP nsFreeTypeFace::GetCCMap(
 
970
                              PRUint32 *size, PRUint16 **ccMaps)
 
971
{
 
972
  *ccMaps = nsFreeType2::GetCCMap(mFce);
 
973
  *size = CCMAP_SIZE(*ccMaps);
 
974
  return NS_OK;
 
975
}
 
976
 
 
977
/* void getEmbeddedBitmapHeights (out unsigned long size,
 
978
 * [array, size_is (size), retval] out short heights); */
 
979
NS_IMETHODIMP nsFreeTypeFace::GetEmbeddedBitmapHeights(
 
980
                              PRUint32 *size, PRInt32 **heights)
 
981
{
 
982
  *heights = mFce->mEmbeddedBitmapHeights;
 
983
  *size = mFce->mNumEmbeddedBitmaps;
 
984
  return NS_OK;
 
985
}
 
986
 
 
987
PRBool
 
988
nsFreeTypeFace::FreeFace(nsHashKey* aKey, void* aData, void* aClosure)
 
989
{
 
990
  nsFreeTypeFace *face = (nsFreeTypeFace*) aData;
 
991
  NS_RELEASE(face);
 
992
 
 
993
  return PR_TRUE;
 
994
}
 
995
 
 
996
PRUint16 *
 
997
nsFreeTypeFace::GetCCMap()
 
998
{
 
999
  if (!mCCMap) {
 
1000
    mCCMap = nsFreeType2::GetCCMap(mFce);
 
1001
  }
 
1002
  return mCCMap;
 
1003
}
 
1004
 
 
1005
///////////////////////////////////////////////////////////////////////
 
1006
//
 
1007
// miscellaneous routines in alphabetic order
 
1008
//
 
1009
///////////////////////////////////////////////////////////////////////
 
1010
 
 
1011
/*FT_CALLBACK_DEF*/
 
1012
FT_Error
 
1013
nsFreeTypeFaceRequester(FTC_FaceID face_id, FT_Library lib,
 
1014
                  FT_Pointer request_data, FT_Face* aFace)
 
1015
{
 
1016
  nsFreeTypeFace *faceID = (nsFreeTypeFace *)face_id;
 
1017
  FT_Error fterror = 0;
 
1018
  nsFreeType2 *ft2 = (nsFreeType2 *)request_data;
 
1019
  nsresult rv;
 
1020
 
 
1021
  rv = ft2->NewFace(lib, faceID->GetFilename(), faceID->GetFaceIndex(), aFace);
 
1022
  if (NS_FAILED(rv))
 
1023
    return fterror;
 
1024
 
 
1025
  FT_Face face = *aFace;
 
1026
  FT_UShort platform_id = TT_PLATFORM_MICROSOFT;
 
1027
  FT_UShort encoding_id = TT_MS_ID_UNICODE_CS;
 
1028
  nsFontCatalogEntry* fce = faceID->GetFce();
 
1029
  nsTTFontFamilyEncoderInfo *ffei =
 
1030
                     nsFreeType2::GetCustomEncoderInfo(fce->mFamilyName);
 
1031
  if (ffei) {
 
1032
    platform_id = ffei->mEncodingInfo->mCmapPlatformID;
 
1033
    encoding_id = ffei->mEncodingInfo->mCmapEncoding;
 
1034
  }
 
1035
 
 
1036
  for (int i=0; i < face->num_charmaps; i++) {
 
1037
    if (face->charmaps[i]->platform_id == platform_id) {
 
1038
#if defined(TT_MS_ID_UCS_4)
 
1039
      if (face->charmaps[i]->encoding_id == TT_MS_ID_UCS_4) {
 
1040
        rv = ft2->SetCharmap(face, face->charmaps[i]);
 
1041
        if (NS_FAILED(rv)) {
 
1042
          FREETYPE_PRINTF(("failed to set cmap"));
 
1043
          ft2->DoneFace(face);
 
1044
          *aFace = nsnull;
 
1045
          fterror = 1;
 
1046
        }
 
1047
        // UCS_4 is the most prefered cmap since it supports surrogates
 
1048
        // so stop here to avoid the possibly of getting UNICODE_CS which
 
1049
        // is the 2nd prefered choice.
 
1050
        break;
 
1051
      } else
 
1052
#endif /* defined(TT_MS_ID_UCS_4) */
 
1053
      if (face->charmaps[i]->encoding_id == encoding_id) {
 
1054
        rv = ft2->SetCharmap(face, face->charmaps[i]);
 
1055
        if (NS_FAILED(rv)) {
 
1056
          FREETYPE_PRINTF(("failed to set cmap"));
 
1057
          ft2->DoneFace(face);
 
1058
          *aFace = nsnull;
 
1059
          fterror = 1;
 
1060
        }
 
1061
      }
 
1062
    }
 
1063
  }
 
1064
 
 
1065
  return fterror;
 
1066
}
 
1067
 
 
1068
nsFreeTypeFace *
 
1069
nsFreeTypeGetFaceID(nsFontCatalogEntry *aFce)
 
1070
{
 
1071
  // We need to have separate keys for the different faces in a ttc file.
 
1072
  // We append a slash and the face index to the file name to give us a 
 
1073
  // unique key for each ttc face.
 
1074
  nsCAutoString key_str(aFce->mFontFileName);
 
1075
  key_str.Append('/');
 
1076
  key_str.AppendInt(aFce->mFaceIndex);
 
1077
  nsCStringKey key(key_str);
 
1078
  nsFreeTypeFace *face = (nsFreeTypeFace *)gFreeTypeFaces->Get(&key);
 
1079
  if (!face) {
 
1080
    face = new nsFreeTypeFace;
 
1081
    NS_ASSERTION(face, "memory error while creating nsFreeTypeFace");
 
1082
    if (!face)
 
1083
      return nsnull;
 
1084
    NS_ADDREF(face);
 
1085
    nsresult rv = face->Init(aFce);
 
1086
    if (NS_FAILED(rv)) {
 
1087
      NS_RELEASE(face);
 
1088
      return nsnull;
 
1089
    }
 
1090
    gFreeTypeFaces->Put(&key, face);
 
1091
  }
 
1092
  return face;
 
1093
}
 
1094
 
 
1095
nsTTFontFamilyEncoderInfo gFontFamilyEncoderInfo[] = {
 
1096
  { "symbol",         &FEI_Adobe_Symbol_Encoding },
 
1097
  { "cmr10",          &FEI_x_ttf_cmr,            },
 
1098
  { "cmmi10",         &FEI_x_ttf_cmmi,           },
 
1099
  { "cmsy10",         &FEI_x_ttf_cmsy,           },
 
1100
  { "cmex10",         &FEI_x_ttf_cmex,           },
 
1101
  { "math1",          &FEI_x_mathematica1,       },
 
1102
  { "math1-bold",     &FEI_x_mathematica1,       },
 
1103
  { "math1mono",      &FEI_x_mathematica1,       },
 
1104
  { "math1mono-bold", &FEI_x_mathematica1,       },
 
1105
  { "math2",          &FEI_x_mathematica2,       },
 
1106
  { "math2-bold",     &FEI_x_mathematica2,       },
 
1107
  { "math2mono",      &FEI_x_mathematica2,       },
 
1108
  { "math2mono-bold", &FEI_x_mathematica2,       },
 
1109
  { "ahMn",           &FEI_x_mathematica3,       }, // weird name for Math3
 
1110
  { "math3",          &FEI_x_mathematica3,       },
 
1111
  { "math3-bold",     &FEI_x_mathematica3,       },
 
1112
  { "math3mono",      &FEI_x_mathematica3,       },
 
1113
  { "math3mono-bold", &FEI_x_mathematica3,       },
 
1114
  { "math4",          &FEI_x_mathematica4,       },
 
1115
  { "math4-bold",     &FEI_x_mathematica4,       },
 
1116
  { "math4mono",      &FEI_x_mathematica4,       },
 
1117
  { "math4mono-bold", &FEI_x_mathematica4,       },
 
1118
  { "math5",          &FEI_x_mathematica5,       },
 
1119
  { "math5-bold",     &FEI_x_mathematica5,       },
 
1120
  { "math5bold",      &FEI_x_mathematica5,       },
 
1121
  { "math5mono",      &FEI_x_mathematica5,       },
 
1122
  { "math5mono-bold", &FEI_x_mathematica5,       },
 
1123
  { "math5monobold",  &FEI_x_mathematica5,       },
 
1124
  { "mtextra",        &FEI_x_mtextra,            },
 
1125
  { "mt extra",       &FEI_x_mtextra,            },
 
1126
  { "wingdings",      &FEI_windows_1252,         },
 
1127
  { "webdings",       &FEI_windows_1252,         },
 
1128
  { nsnull },
 
1129
};
 
1130
 
 
1131
nsulCodePageRangeCharSetName ulCodePageRange1CharSetNames[] = {
 
1132
{ TT_OS2_CPR1_LATIN1,       "iso8859-1"         },
 
1133
{ TT_OS2_CPR1_LATIN2,       "iso8859-2"         },
 
1134
{ TT_OS2_CPR1_CYRILLIC,     "iso8859-5"         },
 
1135
{ TT_OS2_CPR1_GREEK,        "iso8859-7"         },
 
1136
{ TT_OS2_CPR1_TURKISH,      "iso8859-9"         },
 
1137
{ TT_OS2_CPR1_HEBREW,       "iso8859-8"         },
 
1138
{ TT_OS2_CPR1_ARABIC,       "iso8859-6"         },
 
1139
{ TT_OS2_CPR1_BALTIC,       "iso8859-13"        },
 
1140
{ TT_OS2_CPR1_VIETNAMESE,   "viscii1.1-1"       },
 
1141
{ TT_OS2_CPR1_THAI,         "tis620.2533-1"     },
 
1142
{ TT_OS2_CPR1_JAPANESE,     "jisx0208.1990-0"   },
 
1143
{ TT_OS2_CPR1_CHINESE_SIMP, "gb2312.1980-1"     },
 
1144
{ TT_OS2_CPR1_KO_WANSUNG,   "ksc5601.1992-3"    },
 
1145
{ TT_OS2_CPR1_CHINESE_TRAD, "big5-0"            },
 
1146
{ TT_OS2_CPR1_KO_JOHAB,     "ksc5601.1992-3"    },
 
1147
{ TT_OS2_CPR1_MAC_ROMAN,    "iso8859-1"         },
 
1148
{ TT_OS2_CPR1_OEM,          "fontspecific-0"    },
 
1149
{ TT_OS2_CPR1_SYMBOL,       "fontspecific-0"    },
 
1150
{ 0,                         nsnull             },
 
1151
};
 
1152
 
 
1153
nsulCodePageRangeCharSetName ulCodePageRange2CharSetNames[] = {
 
1154
{ TT_OS2_CPR2_GREEK,        "iso8859-7"         },
 
1155
{ TT_OS2_CPR2_RUSSIAN,      "koi8-r"            },
 
1156
{ TT_OS2_CPR2_NORDIC,       "iso8859-10"        },
 
1157
{ TT_OS2_CPR2_ARABIC,       "iso8859-6"         },
 
1158
{ TT_OS2_CPR2_CA_FRENCH,    "iso8859-1"         },
 
1159
{ TT_OS2_CPR2_HEBREW,       "iso8859-8"         },
 
1160
{ TT_OS2_CPR2_ICELANDIC,    "iso8859-1"         },
 
1161
{ TT_OS2_CPR2_PORTUGESE,    "iso8859-1"         },
 
1162
{ TT_OS2_CPR2_TURKISH,      "iso8859-9"         },
 
1163
{ TT_OS2_CPR2_CYRILLIC,     "iso8859-5"         },
 
1164
{ TT_OS2_CPR2_LATIN2,       "iso8859-2"         },
 
1165
{ TT_OS2_CPR2_BALTIC,       "iso8859-4"         },
 
1166
{ TT_OS2_CPR2_GREEK_437G,   "iso8859-7"         },
 
1167
{ TT_OS2_CPR2_ARABIC_708,   "iso8859-6"         },
 
1168
{ TT_OS2_CPR2_WE_LATIN1,    "iso8859-1"         },
 
1169
{ TT_OS2_CPR2_US,           "iso8859-1"         },
 
1170
{ 0,                         nsnull             },
 
1171
};
 
1172