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
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/
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
16
* The Original Code is mozilla.org code.
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.
24
* Brian Stell <bstell@netscape.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 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.
38
* ***** END LICENSE BLOCK ***** */
43
#include "nsIServiceManager.h"
44
#include "nsICharRepresentable.h"
45
#include "nsCompressedCharMap.h"
46
#include "nsICharsetConverterManager.h"
47
#include "nsIRenderingContext.h"
48
#include "nsFreeType.h"
51
#include FT_FREETYPE_H
53
#include FT_TRUETYPE_TABLES_H
54
#include FT_TRUETYPE_IDS_H
56
# define FREETYPE_PRINTF(x) \
58
if (gFreeTypeDebug) { \
60
printf(", %s %d\n", __FILE__, __LINE__); \
64
PRUint32 gFreeTypeDebug = 0;
67
// these belong in nsFontFreeType
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;
81
extern nsulCodePageRangeCharSetName ulCodePageRange1CharSetNames[];
82
extern nsulCodePageRangeCharSetName ulCodePageRange2CharSetNames[];
83
extern nsTTFontFamilyEncoderInfo gFontFamilyEncoderInfo[];
87
/*FT_CALLBACK_DEF*/ FT_Error nsFreeTypeFaceRequester(FTC_FaceID, FT_Library,
88
FT_Pointer, FT_Face*);
89
static FT_Error nsFreeType2__DummyFunc();
91
static nsHashtable* gFreeTypeFaces = nsnull;
93
static NS_DEFINE_CID(kCharSetManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
96
// Define the FreeType2 functions we resolve at run time.
97
// see the comment near nsFreeType2::DoneFace() for more info
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},
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},
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},
129
nsTTFontEncoderInfo FEI_Adobe_Symbol_Encoding = {
130
"Adobe-Symbol-Encoding", TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, nsnull
132
nsTTFontEncoderInfo FEI_x_ttf_cmr = {
133
"x-ttf-cmr", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
135
nsTTFontEncoderInfo FEI_x_ttf_cmmi = {
136
"x-ttf-cmmi", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
138
nsTTFontEncoderInfo FEI_x_ttf_cmsy = {
139
"x-ttf-cmsy", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
141
nsTTFontEncoderInfo FEI_x_ttf_cmex = {
142
"x-ttf-cmex", TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, nsnull
144
nsTTFontEncoderInfo FEI_x_mathematica1 = {
145
"x-mathematica1", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
147
nsTTFontEncoderInfo FEI_x_mathematica2 = {
148
"x-mathematica2", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
150
nsTTFontEncoderInfo FEI_x_mathematica3 = {
151
"x-mathematica3", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
153
nsTTFontEncoderInfo FEI_x_mathematica4 = {
154
"x-mathematica4", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
156
nsTTFontEncoderInfo FEI_x_mathematica5 = {
157
"x-mathematica5", TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, nsnull
159
nsTTFontEncoderInfo FEI_x_mtextra = {
160
"x-mtextra", TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, nsnull
162
nsTTFontEncoderInfo FEI_windows_1252 = {
163
"windows-1252", TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, nsnull
167
///////////////////////////////////////////////////////////////////////
169
// class nsFreeType2 data/functions
171
///////////////////////////////////////////////////////////////////////
173
NS_IMPL_ISUPPORTS1(nsFreeType2, nsIFreeType2)
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.
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.
187
nsFreeType2::DoneFace(FT_Face face)
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;
195
nsFreeType2::DoneFreeType(FT_Library library)
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;
203
nsFreeType2::DoneGlyph(FT_Glyph glyph)
205
// call the FreeType2 function via the function pointer
206
nsFT_Done_Glyph(glyph);
211
nsFreeType2::GetCharIndex(FT_Face face, FT_ULong charcode, FT_UInt *index)
213
// call the FreeType2 function via the function pointer
214
*index = nsFT_Get_Char_Index(face, charcode);
219
nsFreeType2::GetGlyph(FT_GlyphSlot slot, FT_Glyph *glyph)
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;
227
nsFreeType2::GetSfntTable(FT_Face face, FT_Sfnt_Tag tag, void** table)
229
// call the FreeType2 function via the function pointer
230
*table = nsFT_Get_Sfnt_Table(face, tag);
235
nsFreeType2::GlyphGetCBox(FT_Glyph glyph, FT_UInt mode, FT_BBox *bbox)
237
// call the FreeType2 function via the function pointer
238
nsFT_Glyph_Get_CBox(glyph, mode, bbox);
243
nsFreeType2::InitFreeType(FT_Library *library)
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;
251
nsFreeType2::LoadGlyph(FT_Face face, FT_UInt glyph, FT_Int flags)
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;
259
nsFreeType2::NewFace(FT_Library library, const char *path,
260
FT_Long face_index, FT_Face *face)
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;
268
nsFreeType2::OutlineDecompose(FT_Outline *outline,
269
const FT_Outline_Funcs *funcs, void *user)
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;
277
nsFreeType2::SetCharmap(FT_Face face, FT_CharMap charmap)
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;
285
nsFreeType2::ImageCacheLookup(FTC_Image_Cache cache, FTC_Image_Desc *desc,
286
FT_UInt glyphID, FT_Glyph *glyph)
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;
294
nsFreeType2::ManagerLookupSize(FTC_Manager manager, FTC_Font font,
295
FT_Face *face, FT_Size *size)
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;
303
nsFreeType2::ManagerDone(FTC_Manager manager)
305
// call the FreeType2 function via the function pointer
306
nsFTC_Manager_Done(manager);
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)
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;
323
nsFreeType2::ImageCacheNew(FTC_Manager manager, FTC_Image_Cache *cache)
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;
332
nsFreeType2::GlyphTransform(FT_Glyph glyph, FT_Matrix * matrix, FT_Vector * delta)
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;
340
nsFreeType2::GetKerning(FT_Face face, FT_UInt left_glyph, FT_UInt right_glyph,
341
FT_UInt kern_mode, FT_Vector *akerning)
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;
350
nsFreeType2::GlyphCopy(FT_Glyph source, FT_Glyph *target)
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;
358
nsFreeType2::GlyphToBitmap(FT_Glyph *the_glyph, FT_Render_Mode render_mode,
359
FT_Vector * origin, FT_Bool destroy)
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;
369
nsFreeType2::GetFirstChar(FT_Face face, FT_UInt *gindex, FT_ULong *charcode)
371
// call the FreeType2 function via the function pointer
372
*charcode = nsFT_Get_First_Char(face, gindex);
377
nsFreeType2::GetNextChar(FT_Face face, FT_ULong charcode, FT_UInt *gindex, FT_ULong *ncharcode)
379
// call the FreeType2 function via the function pointer
380
*ncharcode = nsFT_Get_Next_Char(face, charcode, gindex);
385
nsFreeType2::SupportsExtFunc(PRBool *res)
392
nsFreeType2::GetImageCache(FTC_Image_Cache *aCache)
394
*aCache = mImageCache;
399
nsFreeType2::GetFTCacheManager(FTC_Manager *aManager)
401
*aManager = mFTCacheManager;
406
nsFreeType2::GetLibrary(FT_Library *aLibrary)
408
*aLibrary = mFreeTypeLibrary;
413
nsFreeType2::ClearFunctions()
417
for (p=FtFuncs; p->FuncName; p++) {
418
*((PRFuncPtr*)((char*)ptr+p->FuncOffset)) =
419
(PRFuncPtr)&nsFreeType2__DummyFunc;
424
nsFreeType2::ClearGlobals()
427
mFreeTypeLibrary = nsnull;
428
mFTCacheManager = nsnull;
429
mImageCache = nsnull;
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
436
nsFreeType2__DummyFunc()
438
NS_ERROR("nsFreeType2__DummyFunc should never be called");
442
nsFreeType2::~nsFreeType2()
448
nsFreeType2::FreeGlobals()
450
if (mFreeType2SharedLibraryName) {
451
free(mFreeType2SharedLibraryName);
452
mFreeType2SharedLibraryName = nsnull;
454
if (gFreeTypeFaces) {
455
gFreeTypeFaces->Reset(nsFreeTypeFace::FreeFace, nsnull);
456
delete gFreeTypeFaces;
457
gFreeTypeFaces = nsnull;
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;
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;
471
if (sRange1CharSetNames)
472
delete sRange1CharSetNames;
473
if (sRange2CharSetNames)
474
delete sRange2CharSetNames;
476
delete sFontFamilies;
478
NS_IF_RELEASE(sCharSetManager);
480
// release any encoders that were created
482
for (i=0; gFontFamilyEncoderInfo[i].mFamilyName; i++) {
483
nsTTFontFamilyEncoderInfo *ffei = &gFontFamilyEncoderInfo[i];
484
nsTTFontEncoderInfo *fei = ffei->mEncodingInfo;
485
NS_IF_RELEASE(fei->mConverter);
496
// set all the globals to default values
499
nsulCodePageRangeCharSetName *crn = nsnull;
500
nsTTFontFamilyEncoderInfo *ff = gFontFamilyEncoderInfo;
501
nsCOMPtr<nsIPref> mPref = do_GetService(NS_PREF_CONTRACTID);
505
return NS_ERROR_FAILURE;
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));
516
rv = mPref->GetCharPref("font.freetype2.shared-library",
517
&mFreeType2SharedLibraryName);
519
enable_freetype2 = PR_FALSE;
521
"mFreeType2SharedLibraryName missing, FreeType2 disabled"));
522
mFreeType2SharedLibraryName = nsnull;
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));
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));
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));
547
rv = mPref->GetCharPref("font.scale.tt_bitmap.dark_text.gain",
549
if (NS_SUCCEEDED(rv)) {
550
gAATTDarkTextGain = atof(str.get());
551
FREETYPE_PRINTF(("gAATTDarkTextGain = %g", gAATTDarkTextGain));
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));
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));
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;
582
if (!InitLibrary()) {
584
return NS_ERROR_OUT_OF_MEMORY;
586
gFreeTypeFaces = new nsHashtable();
587
if (!gFreeTypeFaces) {
589
return NS_ERROR_OUT_OF_MEMORY;
592
sRange1CharSetNames = new nsHashtable();
593
if (!sRange1CharSetNames) {
595
return NS_ERROR_OUT_OF_MEMORY;
597
crn = ulCodePageRange1CharSetNames;
598
while (crn->charsetName) {
600
sprintf(buf, "0x%08lx", crn->bit);
601
nsCStringKey key(buf);
602
sRange1CharSetNames->Put(&key, (void*)crn->charsetName);
606
sRange2CharSetNames = new nsHashtable();
607
if (!sRange2CharSetNames) {
609
return NS_ERROR_OUT_OF_MEMORY;
611
crn = ulCodePageRange2CharSetNames;
612
while (crn->charsetName) {
614
sprintf(buf, "0x%08lx", crn->bit);
615
nsCStringKey key(buf);
616
sRange2CharSetNames->Put(&key, (void*)crn->charsetName);
620
sFontFamilies = new nsHashtable();
621
if (!sFontFamilies) {
623
return NS_ERROR_OUT_OF_MEMORY;
625
while (ff->mFamilyName) {
626
nsCAutoString name(ff->mFamilyName);
628
nsCStringKey key(name);
629
sFontFamilies->Put(&key, (void*)ff);
637
nsFreeType2::InitLibrary()
641
// do not yet support MathML
642
// goto cleanup_and_return;
645
if (!mEnableFreeType2)
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())
653
// use "this->" to make sure it is obivious we are calling the member func
654
nsresult rv = this->InitFreeType(&mFreeTypeLibrary);
656
FREETYPE_PRINTF(("\n\n*********\nFreeType initialization error = %d",
658
mFreeTypeLibrary = nsnull;
659
goto cleanup_and_return;
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");
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");
671
goto cleanup_and_return;
675
// clean everything up but note that init was called
681
nsFreeType2::LoadSharedLib()
683
NS_ASSERTION(mSharedLib==nsnull, "library already loaded");
685
if (!mFreeType2SharedLibraryName)
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
691
NS_WARNING("freetype library not found");
695
// checking FT_Get_First_Char() and FT_Get_Next_Char() for fast lookup methods
696
gHasExtFunc = PR_TRUE;
700
for (p=FtFuncs; p->FuncName; p++) {
701
func = PR_FindFunctionSymbol(mSharedLib, p->FuncName);
703
if (p->Required == PR_TRUE) {
704
NS_WARNING("nsFreeType2::LoadSharedLib Error");
708
// should not return, but disable fast lookup methods
709
gHasExtFunc = PR_FALSE;
712
*((PRFuncPtr*)((char*)ptr+p->FuncOffset)) = func;
720
nsFreeType2::UnloadSharedLib()
723
PR_UnloadLibrary(mSharedLib);
728
nsFreeType2::GetRange1CharSetName(unsigned long aBit)
731
sprintf(buf, "0x%08lx", aBit);
732
nsCStringKey key(buf);
733
const char *charsetName = (const char *)sRange1CharSetNames->Get(&key);
738
nsFreeType2::GetRange2CharSetName(unsigned long aBit)
741
sprintf(buf, "0x%08lx", aBit);
742
nsCStringKey key(buf);
743
const char *charsetName = (const char *)sRange2CharSetNames->Get(&key);
747
nsTTFontFamilyEncoderInfo*
748
nsFreeType2::GetCustomEncoderInfo(const char * aFamilyName)
753
nsTTFontFamilyEncoderInfo *ffei;
754
nsCAutoString name(aFamilyName);
756
nsCStringKey key(name);
757
ffei = (nsTTFontFamilyEncoderInfo*)sFontFamilies->Get(&key);
761
// init the converter
762
if (!ffei->mEncodingInfo->mConverter) {
763
nsTTFontEncoderInfo *fei = ffei->mEncodingInfo;
765
// build the converter
767
nsICharsetConverterManager* charSetManager = GetCharSetManager();
771
res = charSetManager->GetUnicodeEncoderRaw(fei->mConverterName, &fei->mConverter);
772
if (NS_FAILED(res)) {
779
nsICharsetConverterManager*
780
nsFreeType2::GetCharSetManager()
782
if (!sCharSetManager) {
784
// get the sCharSetManager
786
nsServiceManager::GetService(kCharSetManagerCID,
787
NS_GET_IID(nsICharsetConverterManager),
788
(nsISupports**) &sCharSetManager);
789
NS_ASSERTION(sCharSetManager,"failed to create the charset manager");
791
return sCharSetManager;
795
nsFreeType2::GetCCMap(nsFontCatalogEntry *aFce)
797
nsCompressedCharMap ccmapObj;
798
if (aFce->mFlags & FCE_FLAGS_SURROGATE) {
801
ccmapObj.SetChars(aFce->mCCMap);
802
return ccmapObj.NewCCMap();
805
///////////////////////////////////////////////////////////////////////
807
// class nsFreeTypeFace data/functions
809
///////////////////////////////////////////////////////////////////////
810
NS_IMPL_ISUPPORTS1(nsFreeTypeFace, nsITrueTypeFontCatalogEntry)
812
nsFreeTypeFace::nsFreeTypeFace()
816
nsresult nsFreeTypeFace::Init(nsFontCatalogEntry *aFce)
818
NS_ASSERTION(aFce, "init of nsFreeTypeFace needs nsFontCatalogEntry");
822
mFce = new nsFontCatalogEntry;
823
NS_ASSERTION(mFce, "memory error while creating nsFontCatalogEntry");
825
return NS_ERROR_OUT_OF_MEMORY;
831
nsFreeTypeFace::~nsFreeTypeFace()
837
NS_IMETHODIMP nsFreeTypeFace::GetFontCatalogType(
838
PRUint16 *aFontCatalogType)
840
*aFontCatalogType = FONT_CATALOG_TRUETYPE;
844
/* readonly attribute ACString fileName; */
845
NS_IMETHODIMP nsFreeTypeFace::GetFileName(nsACString & aFileName)
847
aFileName.Assign(mFce->mFontFileName);
851
/* readonly attribute ACString familyName; */
852
NS_IMETHODIMP nsFreeTypeFace::GetFamilyName(nsACString & aFamilyName)
854
aFamilyName.Assign(mFce->mFamilyName);
858
/* readonly attribute ACString styleName; */
859
NS_IMETHODIMP nsFreeTypeFace::GetStyleName(nsACString & aStyleName)
861
aStyleName.Assign(mFce->mStyleName);
865
/* readonly attribute ACString vendorID; */
866
NS_IMETHODIMP nsFreeTypeFace::GetVendorID(nsACString & aVendorID)
868
aVendorID.Assign(mFce->mVendorID);
872
/* readonly attribute short faceIndex; */
873
NS_IMETHODIMP nsFreeTypeFace::GetFaceIndex(PRInt16 *aFaceIndex)
875
*aFaceIndex = mFce->mFaceIndex;
879
/* readonly attribute short numFaces; */
880
NS_IMETHODIMP nsFreeTypeFace::GetNumFaces(PRInt16 *aNumFaces)
882
*aNumFaces = mFce->mNumFaces;
886
/* readonly attribute short numEmbeddedBitmaps; */
887
NS_IMETHODIMP nsFreeTypeFace::GetNumEmbeddedBitmaps(
888
PRInt16 *aNumEmbeddedBitmaps)
890
*aNumEmbeddedBitmaps = mFce->mNumEmbeddedBitmaps;
894
/* readonly attribute long numGlyphs; */
895
NS_IMETHODIMP nsFreeTypeFace::GetNumGlyphs(PRInt32 *aNumGlyphs)
897
*aNumGlyphs = mFce->mNumGlyphs;
901
/* readonly attribute long numUsableGlyphs; */
902
NS_IMETHODIMP nsFreeTypeFace::GetNumUsableGlyphs(
903
PRInt32 *aNumUsableGlyphs)
905
*aNumUsableGlyphs = mFce->mNumUsableGlyphs;
909
/* readonly attribute unsigned short weight; */
910
NS_IMETHODIMP nsFreeTypeFace::GetWeight(PRUint16 *aWeight)
912
*aWeight = mFce->mWeight;
916
/* readonly attribute unsigned short width; */
917
NS_IMETHODIMP nsFreeTypeFace::GetWidth(PRUint16 *aWidth)
919
*aWidth = mFce->mWidth;
923
/* readonly attribute unsigned long flags; */
924
NS_IMETHODIMP nsFreeTypeFace::GetFlags(PRUint32 *aFlags)
926
*aFlags = mFce->mFlags;
930
/* readonly attribute long long faceFlags; */
931
NS_IMETHODIMP nsFreeTypeFace::GetFaceFlags(PRInt64 *aFaceFlags)
933
*aFaceFlags = mFce->mFaceFlags;
937
/* readonly attribute long long styleFlags; */
938
NS_IMETHODIMP nsFreeTypeFace::GetStyleFlags(PRInt64 *aStyleFlags)
940
*aStyleFlags = mFce->mStyleFlags;
944
/* readonly attribute unsigned long codePageRange1; */
945
NS_IMETHODIMP nsFreeTypeFace::GetCodePageRange1(
946
PRUint32 *aCodePageRange1)
948
*aCodePageRange1 = mFce->mCodePageRange1;
952
/* readonly attribute unsigned long codePageRange2; */
953
NS_IMETHODIMP nsFreeTypeFace::GetCodePageRange2(
954
PRUint32 *aCodePageRange2)
956
*aCodePageRange2 = mFce->mCodePageRange2;
960
/* readonly attribute long long time; */
961
NS_IMETHODIMP nsFreeTypeFace::GetFileModTime(PRInt64 *aTime)
963
*aTime = mFce->mMTime;
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)
972
*ccMaps = nsFreeType2::GetCCMap(mFce);
973
*size = CCMAP_SIZE(*ccMaps);
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)
982
*heights = mFce->mEmbeddedBitmapHeights;
983
*size = mFce->mNumEmbeddedBitmaps;
988
nsFreeTypeFace::FreeFace(nsHashKey* aKey, void* aData, void* aClosure)
990
nsFreeTypeFace *face = (nsFreeTypeFace*) aData;
997
nsFreeTypeFace::GetCCMap()
1000
mCCMap = nsFreeType2::GetCCMap(mFce);
1005
///////////////////////////////////////////////////////////////////////
1007
// miscellaneous routines in alphabetic order
1009
///////////////////////////////////////////////////////////////////////
1013
nsFreeTypeFaceRequester(FTC_FaceID face_id, FT_Library lib,
1014
FT_Pointer request_data, FT_Face* aFace)
1016
nsFreeTypeFace *faceID = (nsFreeTypeFace *)face_id;
1017
FT_Error fterror = 0;
1018
nsFreeType2 *ft2 = (nsFreeType2 *)request_data;
1021
rv = ft2->NewFace(lib, faceID->GetFilename(), faceID->GetFaceIndex(), aFace);
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);
1032
platform_id = ffei->mEncodingInfo->mCmapPlatformID;
1033
encoding_id = ffei->mEncodingInfo->mCmapEncoding;
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);
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.
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);
1069
nsFreeTypeGetFaceID(nsFontCatalogEntry *aFce)
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);
1080
face = new nsFreeTypeFace;
1081
NS_ASSERTION(face, "memory error while creating nsFreeTypeFace");
1085
nsresult rv = face->Init(aFce);
1086
if (NS_FAILED(rv)) {
1090
gFreeTypeFaces->Put(&key, face);
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, },
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" },
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" },