2009-08-16 06:52:12 -07:00
|
|
|
/* -*- Mode: ObjC; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: BSD
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2009 Mozilla Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com>
|
|
|
|
* Masayuki Nakano <masayuki@d-toybox.com>
|
|
|
|
* John Daggett <jdaggett@mozilla.com>
|
|
|
|
* Jonathan Kew <jfkthame@gmail.com>
|
2009-10-12 16:43:43 -07:00
|
|
|
*
|
2009-08-16 06:52:12 -07:00
|
|
|
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-01-21 08:44:33 -08:00
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG /* Allow logging in the release build */
|
|
|
|
#endif
|
|
|
|
#include "prlog.h"
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
|
|
|
|
#import <AppKit/AppKit.h>
|
|
|
|
|
|
|
|
#include "gfxPlatformMac.h"
|
|
|
|
#include "gfxMacPlatformFontList.h"
|
2010-03-10 04:46:41 -08:00
|
|
|
#include "gfxMacFont.h"
|
2009-08-16 06:52:12 -07:00
|
|
|
#include "gfxUserFontSet.h"
|
|
|
|
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2012-03-08 18:05:47 -08:00
|
|
|
#include "nsCharTraits.h"
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-07-26 23:42:53 -07:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2011-07-26 23:42:53 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2010-03-09 08:37:56 -08:00
|
|
|
class nsAutoreleasePool {
|
|
|
|
public:
|
|
|
|
nsAutoreleasePool()
|
|
|
|
{
|
|
|
|
mLocalPool = [[NSAutoreleasePool alloc] init];
|
|
|
|
}
|
|
|
|
~nsAutoreleasePool()
|
|
|
|
{
|
|
|
|
[mLocalPool release];
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
NSAutoreleasePool *mLocalPool;
|
|
|
|
};
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// font info loader constants
|
|
|
|
static const PRUint32 kDelayBeforeLoadingCmaps = 8 * 1000; // 8secs
|
|
|
|
static const PRUint32 kIntervalBetweenLoadingCmaps = 150; // 150ms
|
|
|
|
static const PRUint32 kNumFontsPerSlice = 10; // read in info 10 fonts at a time
|
|
|
|
|
|
|
|
// indexes into the NSArray objects that the Cocoa font manager returns
|
|
|
|
// as the available members of a family
|
|
|
|
#define INDEX_FONT_POSTSCRIPT_NAME 0
|
|
|
|
#define INDEX_FONT_FACE_NAME 1
|
|
|
|
#define INDEX_FONT_WEIGHT 2
|
|
|
|
#define INDEX_FONT_TRAITS 3
|
|
|
|
|
|
|
|
static const int kAppleMaxWeight = 14;
|
|
|
|
static const int kAppleExtraLightWeight = 3;
|
|
|
|
static const int kAppleUltraLightWeight = 2;
|
|
|
|
|
|
|
|
static const int gAppleWeightToCSSWeight[] = {
|
|
|
|
0,
|
2009-10-12 16:43:43 -07:00
|
|
|
1, // 1.
|
2009-08-16 06:52:12 -07:00
|
|
|
1, // 2. W1, ultralight
|
|
|
|
2, // 3. W2, extralight
|
|
|
|
3, // 4. W3, light
|
|
|
|
4, // 5. W4, semilight
|
|
|
|
5, // 6. W5, medium
|
|
|
|
6, // 7.
|
|
|
|
6, // 8. W6, semibold
|
|
|
|
7, // 9. W7, bold
|
|
|
|
8, // 10. W8, extrabold
|
|
|
|
8, // 11.
|
|
|
|
9, // 12. W9, ultrabold
|
|
|
|
9, // 13
|
|
|
|
9 // 14
|
|
|
|
};
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
// cache Cocoa's "shared font manager" for performance
|
|
|
|
static NSFontManager *sFontManager;
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
static void GetStringForNSString(const NSString *aSrc, nsAString& aDist)
|
|
|
|
{
|
|
|
|
aDist.SetLength([aSrc length]);
|
|
|
|
[aSrc getCharacters:aDist.BeginWriting()];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString* GetNSStringForString(const nsAString& aSrc)
|
|
|
|
{
|
|
|
|
return [NSString stringWithCharacters:aSrc.BeginReading()
|
|
|
|
length:aSrc.Length()];
|
|
|
|
}
|
|
|
|
|
2009-10-07 07:13:40 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
|
2011-01-21 08:44:33 -08:00
|
|
|
#define LOG_FONTLIST(args) PR_LOG(gfxPlatform::GetLog(eGfxLog_fontlist), \
|
|
|
|
PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_FONTLIST_ENABLED() PR_LOG_TEST( \
|
|
|
|
gfxPlatform::GetLog(eGfxLog_fontlist), \
|
|
|
|
PR_LOG_DEBUG)
|
2012-03-08 18:05:14 -08:00
|
|
|
#define LOG_CMAPDATA_ENABLED() PR_LOG_TEST( \
|
|
|
|
gfxPlatform::GetLog(eGfxLog_cmapdata), \
|
|
|
|
PR_LOG_DEBUG)
|
2011-01-21 08:44:33 -08:00
|
|
|
|
|
|
|
#endif // PR_LOGGING
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
/* MacOSFontEntry - abstract superclass for ATS and CG font entries */
|
2009-08-16 06:52:12 -07:00
|
|
|
#pragma mark-
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
MacOSFontEntry::MacOSFontEntry(const nsAString& aPostscriptName,
|
2009-08-16 06:52:12 -07:00
|
|
|
PRInt32 aWeight,
|
|
|
|
gfxFontFamily *aFamily,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsStandardFace)
|
2009-08-16 06:52:12 -07:00
|
|
|
: gfxFontEntry(aPostscriptName, aFamily, aIsStandardFace),
|
2011-06-24 10:55:27 -07:00
|
|
|
mFontRef(NULL),
|
2011-10-17 07:59:28 -07:00
|
|
|
mFontRefInitialized(false),
|
|
|
|
mRequiresAAT(false),
|
|
|
|
mIsCFF(false),
|
|
|
|
mIsCFFInitialized(false)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
|
|
|
mWeight = aWeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ATSUI requires AAT-enabled fonts to render complex scripts correctly.
|
|
|
|
// For now, simple clear out the cmap codepoints for fonts that have
|
|
|
|
// codepoints for complex scripts. (Bug 361986)
|
|
|
|
// Core Text is similar, but can render Arabic using OpenType fonts as well.
|
|
|
|
|
|
|
|
enum eComplexScript {
|
|
|
|
eComplexScriptArabic,
|
|
|
|
eComplexScriptIndic,
|
|
|
|
eComplexScriptTibetan
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ScriptRange {
|
|
|
|
eComplexScript script;
|
|
|
|
PRUint32 rangeStart;
|
|
|
|
PRUint32 rangeEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ScriptRange gScriptsThatRequireShaping[] = {
|
2010-11-20 09:49:12 -08:00
|
|
|
{ eComplexScriptArabic, 0x0600, 0x077F }, // Basic Arabic, Syriac, Arabic Supplement
|
2009-08-16 06:52:12 -07:00
|
|
|
{ eComplexScriptIndic, 0x0900, 0x0D7F }, // Indic scripts - Devanagari, Bengali, ..., Malayalam
|
|
|
|
{ eComplexScriptTibetan, 0x0F00, 0x0FFF } // Tibetan
|
|
|
|
// Thai seems to be "renderable" without AAT morphing tables
|
|
|
|
// xxx - Lao, Khmer?
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MacOSFontEntry::ReadCMAP()
|
|
|
|
{
|
|
|
|
// attempt this once, if errors occur leave a blank cmap
|
2011-06-24 10:55:27 -07:00
|
|
|
if (mCmapInitialized) {
|
2009-08-16 06:52:12 -07:00
|
|
|
return NS_OK;
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mCmapInitialized = true;
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
PRUint32 kCMAP = TRUETYPE_TAG('c','m','a','p');
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2011-01-06 20:45:10 -08:00
|
|
|
AutoFallibleTArray<PRUint8,16384> cmap;
|
2011-06-24 10:55:27 -07:00
|
|
|
if (GetFontTable(kCMAP, cmap) != NS_OK) {
|
2009-08-16 06:52:12 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool unicodeFont, symbolFont; // currently ignored
|
2009-12-10 12:18:14 -08:00
|
|
|
nsresult rv = gfxFontUtils::ReadCMAP(cmap.Elements(), cmap.Length(),
|
2010-07-22 02:25:21 -07:00
|
|
|
mCharacterMap, mUVSOffset,
|
|
|
|
unicodeFont, symbolFont);
|
2009-09-17 17:45:29 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCharacterMap.reset();
|
|
|
|
return rv;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mHasCmapTable = true;
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
CGFontRef fontRef = GetFontRef();
|
|
|
|
if (!fontRef) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2010-12-06 02:41:29 -08:00
|
|
|
// for layout support, check for the presence of mort/morx and/or
|
|
|
|
// opentype layout tables
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasAATLayout = HasFontTable(TRUETYPE_TAG('m','o','r','x')) ||
|
2011-06-24 10:55:27 -07:00
|
|
|
HasFontTable(TRUETYPE_TAG('m','o','r','t'));
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasGSUB = HasFontTable(TRUETYPE_TAG('G','S','U','B'));
|
|
|
|
bool hasGPOS = HasFontTable(TRUETYPE_TAG('G','P','O','S'));
|
2010-07-22 02:25:21 -07:00
|
|
|
|
2010-12-06 02:41:29 -08:00
|
|
|
if (hasAATLayout && !(hasGSUB || hasGPOS)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mRequiresAAT = true; // prefer CoreText if font has no OTL tables
|
2010-07-22 02:25:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 numScripts =
|
|
|
|
sizeof(gScriptsThatRequireShaping) / sizeof(ScriptRange);
|
|
|
|
|
|
|
|
for (PRUint32 s = 0; s < numScripts; s++) {
|
2009-08-16 06:52:12 -07:00
|
|
|
eComplexScript whichScript = gScriptsThatRequireShaping[s].script;
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// check to see if the cmap includes complex script codepoints
|
|
|
|
if (mCharacterMap.TestRange(gScriptsThatRequireShaping[s].rangeStart,
|
|
|
|
gScriptsThatRequireShaping[s].rangeEnd)) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool omitRange = true;
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2010-07-22 02:25:21 -07:00
|
|
|
if (hasAATLayout) {
|
2011-10-17 07:59:28 -07:00
|
|
|
omitRange = false;
|
2010-12-06 02:41:29 -08:00
|
|
|
// prefer CoreText for Apple's complex-script fonts,
|
|
|
|
// even if they also have some OpenType tables
|
|
|
|
// (e.g. Geeza Pro Bold on 10.6; see bug 614903)
|
2011-10-17 07:59:28 -07:00
|
|
|
mRequiresAAT = true;
|
2010-07-22 02:25:21 -07:00
|
|
|
} else if (whichScript == eComplexScriptArabic) {
|
|
|
|
// special-case for Arabic:
|
2010-02-26 06:20:38 -08:00
|
|
|
// even if there's no morph table, CoreText can shape Arabic
|
2010-11-20 09:49:12 -08:00
|
|
|
// using OpenType layout; or if it's a downloaded font,
|
|
|
|
// assume the site knows what it's doing (as harfbuzz will
|
2010-12-06 02:41:29 -08:00
|
|
|
// be able to shape even though the font itself lacks tables
|
|
|
|
// stripped during sanitization).
|
|
|
|
// We check for GSUB here, as GPOS alone would not be ok
|
|
|
|
// for Arabic shaping.
|
|
|
|
if (hasGSUB || (mIsUserFont && !mIsLocalUserFont)) {
|
2010-07-22 02:25:21 -07:00
|
|
|
// TODO: to be really thorough, we could check that the
|
|
|
|
// GSUB table actually supports the 'arab' script tag.
|
2011-10-17 07:59:28 -07:00
|
|
|
omitRange = false;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (omitRange) {
|
|
|
|
mCharacterMap.ClearRange(gScriptsThatRequireShaping[s].rangeStart,
|
|
|
|
gScriptsThatRequireShaping[s].rangeEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 08:44:33 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
LOG_FONTLIST(("(fontlist-cmap) name: %s, size: %d\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mName).get(),
|
|
|
|
mCharacterMap.GetSize()));
|
2012-03-08 18:05:14 -08:00
|
|
|
if (LOG_CMAPDATA_ENABLED()) {
|
|
|
|
char prefix[256];
|
|
|
|
sprintf(prefix, "(cmapdata) name: %.220s",
|
|
|
|
NS_ConvertUTF16toUTF8(mName).get());
|
|
|
|
mCharacterMap.Dump(prefix, eGfxLog_cmapdata);
|
|
|
|
}
|
2011-01-21 08:44:33 -08:00
|
|
|
#endif
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
gfxFont*
|
2011-09-28 23:19:26 -07:00
|
|
|
MacOSFontEntry::CreateFontInstance(const gfxFontStyle *aFontStyle, bool aNeedsBold)
|
2011-06-24 10:55:27 -07:00
|
|
|
{
|
|
|
|
return new gfxMacFont(this, aFontStyle, aNeedsBold);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-06-24 10:55:27 -07:00
|
|
|
MacOSFontEntry::IsCFF()
|
|
|
|
{
|
|
|
|
if (!mIsCFFInitialized) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsCFFInitialized = true;
|
2011-06-24 10:55:27 -07:00
|
|
|
mIsCFF = HasFontTable(TRUETYPE_TAG('C','F','F',' '));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mIsCFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ATSFontEntry - used on Mac OS X 10.5.x */
|
|
|
|
#pragma mark-
|
|
|
|
|
|
|
|
ATSFontEntry::ATSFontEntry(const nsAString& aPostscriptName,
|
|
|
|
PRInt32 aWeight,
|
|
|
|
gfxFontFamily *aFamily,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsStandardFace)
|
2011-06-24 10:55:27 -07:00
|
|
|
: MacOSFontEntry(aPostscriptName, aWeight, aFamily, aIsStandardFace),
|
|
|
|
mATSFontRef(kInvalidFont),
|
2011-10-17 07:59:28 -07:00
|
|
|
mATSFontRefInitialized(false)
|
2011-06-24 10:55:27 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ATSFontEntry::ATSFontEntry(const nsAString& aPostscriptName,
|
|
|
|
ATSFontRef aFontRef,
|
|
|
|
PRUint16 aWeight, PRUint16 aStretch,
|
|
|
|
PRUint32 aItalicStyle,
|
|
|
|
gfxUserFontData *aUserFontData,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsLocal)
|
2011-10-17 07:59:28 -07:00
|
|
|
: MacOSFontEntry(aPostscriptName, aWeight, nsnull, false)
|
2011-06-24 10:55:27 -07:00
|
|
|
{
|
|
|
|
mATSFontRef = aFontRef;
|
2011-10-17 07:59:28 -07:00
|
|
|
mATSFontRefInitialized = true;
|
2011-06-24 10:55:27 -07:00
|
|
|
|
|
|
|
mWeight = aWeight;
|
|
|
|
mStretch = aStretch;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFixedPitch = false; // xxx - do we need this for downloaded fonts?
|
2012-03-15 02:04:46 -07:00
|
|
|
mItalic = (aItalicStyle & (NS_FONT_STYLE_ITALIC | NS_FONT_STYLE_OBLIQUE)) != 0;
|
2011-06-24 10:55:27 -07:00
|
|
|
mUserFontData = aUserFontData;
|
|
|
|
mIsUserFont = (aUserFontData != nsnull) || aIsLocal;
|
|
|
|
mIsLocalUserFont = aIsLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
ATSFontRef
|
|
|
|
ATSFontEntry::GetATSFontRef()
|
|
|
|
{
|
|
|
|
if (!mATSFontRefInitialized) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mATSFontRefInitialized = true;
|
2011-06-24 10:55:27 -07:00
|
|
|
NSString *psname = GetNSStringForString(mName);
|
|
|
|
mATSFontRef = ::ATSFontFindFromPostScriptName(CFStringRef(psname),
|
|
|
|
kATSOptionFlagsDefault);
|
|
|
|
}
|
|
|
|
return mATSFontRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGFontRef
|
|
|
|
ATSFontEntry::GetFontRef()
|
|
|
|
{
|
|
|
|
if (mFontRefInitialized) {
|
|
|
|
return mFontRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetATSFontRef will initialize mATSFontRef
|
|
|
|
if (GetATSFontRef() == kInvalidFont) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFontRef = ::CGFontCreateWithPlatformFont(&mATSFontRef);
|
2011-10-17 07:59:28 -07:00
|
|
|
mFontRefInitialized = true;
|
2011-06-24 10:55:27 -07:00
|
|
|
|
|
|
|
return mFontRef;
|
|
|
|
}
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
nsresult
|
2011-06-24 10:55:27 -07:00
|
|
|
ATSFontEntry::GetFontTable(PRUint32 aTableTag, FallibleTArray<PRUint8>& aBuffer)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
2010-03-09 08:37:56 -08:00
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
ATSFontRef fontRef = GetATSFontRef();
|
|
|
|
if (fontRef == kInvalidFont) {
|
2009-08-16 06:52:12 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-06-11 12:14:38 -07:00
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
ByteCount dataLength;
|
|
|
|
OSStatus status = ::ATSFontGetTable(fontRef, aTableTag, 0, 0, 0, &dataLength);
|
2010-06-11 12:14:38 -07:00
|
|
|
if (status != noErr) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-12-09 14:32:29 -08:00
|
|
|
if (!aBuffer.SetLength(dataLength)) {
|
2009-08-16 06:52:12 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2010-06-11 12:14:38 -07:00
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
PRUint8 *dataPtr = aBuffer.Elements();
|
|
|
|
|
|
|
|
status = ::ATSFontGetTable(fontRef, aTableTag, 0, dataLength, dataPtr, &dataLength);
|
|
|
|
NS_ENSURE_TRUE(status == noErr, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-06-24 10:55:27 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-06-24 10:55:27 -07:00
|
|
|
ATSFontEntry::HasFontTable(PRUint32 aTableTag)
|
|
|
|
{
|
|
|
|
ATSFontRef fontRef = GetATSFontRef();
|
|
|
|
ByteCount size;
|
|
|
|
return fontRef != kInvalidFont &&
|
|
|
|
(::ATSFontGetTable(fontRef, aTableTag, 0, 0, 0, &size) == noErr);
|
|
|
|
}
|
2009-09-28 03:25:22 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
/* CGFontEntry - used on Mac OS X 10.6+ */
|
|
|
|
#pragma mark-
|
|
|
|
|
|
|
|
CGFontEntry::CGFontEntry(const nsAString& aPostscriptName,
|
|
|
|
PRInt32 aWeight,
|
|
|
|
gfxFontFamily *aFamily,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsStandardFace)
|
2011-06-24 10:55:27 -07:00
|
|
|
: MacOSFontEntry(aPostscriptName, aWeight, aFamily, aIsStandardFace)
|
2009-10-07 08:26:58 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
CGFontEntry::CGFontEntry(const nsAString& aPostscriptName,
|
|
|
|
CGFontRef aFontRef,
|
|
|
|
PRUint16 aWeight, PRUint16 aStretch,
|
|
|
|
PRUint32 aItalicStyle,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsUserFont, bool aIsLocal)
|
2011-10-17 07:59:28 -07:00
|
|
|
: MacOSFontEntry(aPostscriptName, aWeight, nsnull, false)
|
2010-10-07 00:59:16 -07:00
|
|
|
{
|
2011-06-24 10:55:27 -07:00
|
|
|
mFontRef = aFontRef;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFontRefInitialized = true;
|
2011-06-24 10:55:27 -07:00
|
|
|
::CFRetain(mFontRef);
|
|
|
|
|
|
|
|
mWeight = aWeight;
|
|
|
|
mStretch = aStretch;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFixedPitch = false; // xxx - do we need this for downloaded fonts?
|
2012-03-15 02:04:46 -07:00
|
|
|
mItalic = (aItalicStyle & (NS_FONT_STYLE_ITALIC | NS_FONT_STYLE_OBLIQUE)) != 0;
|
2011-06-24 10:55:27 -07:00
|
|
|
mIsUserFont = aIsUserFont;
|
|
|
|
mIsLocalUserFont = aIsLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGFontRef
|
|
|
|
CGFontEntry::GetFontRef()
|
|
|
|
{
|
|
|
|
if (!mFontRefInitialized) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mFontRefInitialized = true;
|
2011-06-24 10:55:27 -07:00
|
|
|
NSString *psname = GetNSStringForString(mName);
|
|
|
|
mFontRef = ::CGFontCreateWithFontName(CFStringRef(psname));
|
2010-10-07 00:59:16 -07:00
|
|
|
}
|
2011-06-24 10:55:27 -07:00
|
|
|
return mFontRef;
|
|
|
|
}
|
2010-10-07 00:59:16 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
nsresult
|
|
|
|
CGFontEntry::GetFontTable(PRUint32 aTableTag, FallibleTArray<PRUint8>& aBuffer)
|
|
|
|
{
|
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
|
|
|
CGFontRef fontRef = GetFontRef();
|
|
|
|
if (!fontRef) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFDataRef tableData = ::CGFontCopyTableForTag(fontRef, aTableTag);
|
|
|
|
if (!tableData) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rval = NS_OK;
|
|
|
|
CFIndex dataLength = ::CFDataGetLength(tableData);
|
|
|
|
if (aBuffer.AppendElements(dataLength)) {
|
|
|
|
::CFDataGetBytes(tableData, ::CFRangeMake(0, dataLength),
|
|
|
|
aBuffer.Elements());
|
|
|
|
} else {
|
|
|
|
rval = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
::CFRelease(tableData);
|
|
|
|
|
|
|
|
return rval;
|
2010-10-07 00:59:16 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-06-24 10:55:27 -07:00
|
|
|
CGFontEntry::HasFontTable(PRUint32 aTableTag)
|
|
|
|
{
|
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
|
|
|
CGFontRef fontRef = GetFontRef();
|
|
|
|
if (!fontRef) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CFDataRef tableData = ::CGFontCopyTableForTag(fontRef, aTableTag);
|
|
|
|
if (!tableData) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
::CFRelease(tableData);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
2009-09-28 03:25:22 -07:00
|
|
|
|
|
|
|
/* gfxMacFontFamily */
|
|
|
|
#pragma mark-
|
|
|
|
|
|
|
|
class gfxMacFontFamily : public gfxFontFamily
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
gfxMacFontFamily(nsAString& aName) :
|
|
|
|
gfxFontFamily(aName)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~gfxMacFontFamily() {}
|
|
|
|
|
|
|
|
virtual void LocalizedName(nsAString& aLocalizedName);
|
2009-10-12 16:43:43 -07:00
|
|
|
|
|
|
|
virtual void FindStyleVariations();
|
2009-09-28 03:25:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxMacFontFamily::LocalizedName(nsAString& aLocalizedName)
|
|
|
|
{
|
2010-03-09 08:37:56 -08:00
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2009-09-28 03:25:22 -07:00
|
|
|
if (!HasOtherFamilyNames()) {
|
|
|
|
aLocalizedName = mName;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *family = GetNSStringForString(mName);
|
2009-10-12 16:43:43 -07:00
|
|
|
NSString *localized = [sFontManager
|
2009-09-28 03:25:22 -07:00
|
|
|
localizedNameForFamily:family
|
|
|
|
face:nil];
|
|
|
|
|
|
|
|
if (localized) {
|
|
|
|
GetStringForNSString(localized, aLocalizedName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed to get localized name, just use the canonical one
|
|
|
|
aLocalizedName = mName;
|
|
|
|
}
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
void
|
|
|
|
gfxMacFontFamily::FindStyleVariations()
|
|
|
|
{
|
|
|
|
if (mHasStyles)
|
|
|
|
return;
|
|
|
|
|
2010-03-09 08:37:56 -08:00
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
NSString *family = GetNSStringForString(mName);
|
|
|
|
|
|
|
|
// create a font entry for each face
|
|
|
|
NSArray *fontfaces = [sFontManager
|
|
|
|
availableMembersOfFontFamily:family]; // returns an array of [psname, style name, weight, traits] elements, goofy api
|
|
|
|
int faceCount = [fontfaces count];
|
|
|
|
int faceIndex;
|
|
|
|
|
|
|
|
// Bug 420981 - under 10.5, UltraLight and Light have the same weight value
|
2011-09-28 23:19:26 -07:00
|
|
|
bool needToCheckLightFaces =
|
2009-10-12 16:43:43 -07:00
|
|
|
(gfxPlatformMac::GetPlatform()->OSXVersion() >= MAC_OS_X_VERSION_10_5_HEX);
|
|
|
|
|
|
|
|
for (faceIndex = 0; faceIndex < faceCount; faceIndex++) {
|
|
|
|
NSArray *face = [fontfaces objectAtIndex:faceIndex];
|
|
|
|
NSString *psname = [face objectAtIndex:INDEX_FONT_POSTSCRIPT_NAME];
|
|
|
|
PRInt32 appKitWeight = [[face objectAtIndex:INDEX_FONT_WEIGHT] unsignedIntValue];
|
|
|
|
PRUint32 macTraits = [[face objectAtIndex:INDEX_FONT_TRAITS] unsignedIntValue];
|
|
|
|
NSString *facename = [face objectAtIndex:INDEX_FONT_FACE_NAME];
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isStandardFace = false;
|
2009-10-12 16:43:43 -07:00
|
|
|
|
|
|
|
if (needToCheckLightFaces && appKitWeight == kAppleExtraLightWeight) {
|
|
|
|
// if the facename contains UltraLight, set the weight to the ultralight weight value
|
|
|
|
NSRange range = [facename rangeOfString:@"ultralight" options:NSCaseInsensitiveSearch];
|
|
|
|
if (range.location != NSNotFound) {
|
|
|
|
appKitWeight = kAppleUltraLightWeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 cssWeight = gfxMacPlatformFontList::AppleWeightToCSSWeight(appKitWeight) * 100;
|
|
|
|
|
|
|
|
// make a nsString
|
|
|
|
nsAutoString postscriptFontName;
|
|
|
|
GetStringForNSString(psname, postscriptFontName);
|
|
|
|
|
|
|
|
if ([facename isEqualToString:@"Regular"] ||
|
|
|
|
[facename isEqualToString:@"Bold"] ||
|
|
|
|
[facename isEqualToString:@"Italic"] ||
|
|
|
|
[facename isEqualToString:@"Oblique"] ||
|
|
|
|
[facename isEqualToString:@"Bold Italic"] ||
|
|
|
|
[facename isEqualToString:@"Bold Oblique"])
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
isStandardFace = true;
|
2009-10-12 16:43:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a font entry
|
2011-06-24 10:55:27 -07:00
|
|
|
MacOSFontEntry *fontEntry;
|
|
|
|
if (gfxMacPlatformFontList::UseATSFontEntry()) {
|
|
|
|
fontEntry = new ATSFontEntry(postscriptFontName,
|
|
|
|
cssWeight, this, isStandardFace);
|
|
|
|
} else {
|
|
|
|
fontEntry = new CGFontEntry(postscriptFontName,
|
|
|
|
cssWeight, this, isStandardFace);
|
|
|
|
}
|
|
|
|
if (!fontEntry) {
|
|
|
|
break;
|
|
|
|
}
|
2009-10-12 16:43:43 -07:00
|
|
|
|
|
|
|
// set additional properties based on the traits reported by Cocoa
|
|
|
|
if (macTraits & (NSCondensedFontMask | NSNarrowFontMask | NSCompressedFontMask)) {
|
|
|
|
fontEntry->mStretch = NS_FONT_STRETCH_CONDENSED;
|
|
|
|
} else if (macTraits & NSExpandedFontMask) {
|
|
|
|
fontEntry->mStretch = NS_FONT_STRETCH_EXPANDED;
|
|
|
|
}
|
2010-11-16 00:34:38 -08:00
|
|
|
// Cocoa fails to set the Italic traits bit for HelveticaLightItalic,
|
|
|
|
// at least (see bug 611855), so check for style name endings as well
|
|
|
|
if ((macTraits & NSItalicFontMask) ||
|
|
|
|
[facename hasSuffix:@"Italic"] ||
|
|
|
|
[facename hasSuffix:@"Oblique"])
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
fontEntry->mItalic = true;
|
2009-10-12 16:43:43 -07:00
|
|
|
}
|
|
|
|
if (macTraits & NSFixedPitchFontMask) {
|
2011-10-17 07:59:28 -07:00
|
|
|
fontEntry->mFixedPitch = true;
|
2009-10-12 16:43:43 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 07:13:40 -07:00
|
|
|
#ifdef PR_LOGGING
|
2011-01-21 08:44:33 -08:00
|
|
|
if (LOG_FONTLIST_ENABLED()) {
|
|
|
|
LOG_FONTLIST(("(fontlist) added (%s) to family (%s)"
|
2009-10-07 07:13:40 -07:00
|
|
|
" with style: %s weight: %d stretch: %d"
|
|
|
|
" (apple-weight: %d macTraits: %8.8x)",
|
|
|
|
NS_ConvertUTF16toUTF8(fontEntry->Name()).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(Name()).get(),
|
|
|
|
fontEntry->IsItalic() ? "italic" : "normal",
|
|
|
|
cssWeight, fontEntry->Stretch(),
|
|
|
|
appKitWeight, macTraits));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
// insert into font entry array of family
|
|
|
|
AddFontEntry(fontEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
SortAvailableFonts();
|
2011-10-17 07:59:28 -07:00
|
|
|
SetHasStyles(true);
|
2009-10-12 16:43:43 -07:00
|
|
|
|
|
|
|
if (mIsBadUnderlineFamily) {
|
|
|
|
SetBadUnderlineFonts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-28 03:25:22 -07:00
|
|
|
|
|
|
|
/* gfxSingleFaceMacFontFamily */
|
|
|
|
#pragma mark-
|
|
|
|
|
|
|
|
class gfxSingleFaceMacFontFamily : public gfxFontFamily
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
gfxSingleFaceMacFontFamily(nsAString& aName) :
|
|
|
|
gfxFontFamily(aName)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~gfxSingleFaceMacFontFamily() {}
|
|
|
|
|
|
|
|
virtual void LocalizedName(nsAString& aLocalizedName);
|
|
|
|
|
2010-01-28 17:41:25 -08:00
|
|
|
virtual void ReadOtherFamilyNames(gfxPlatformFontList *aPlatformFontList);
|
2009-09-28 03:25:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxSingleFaceMacFontFamily::LocalizedName(nsAString& aLocalizedName)
|
|
|
|
{
|
2010-03-09 08:37:56 -08:00
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2009-09-28 03:25:22 -07:00
|
|
|
if (!HasOtherFamilyNames()) {
|
|
|
|
aLocalizedName = mName;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[0];
|
|
|
|
NSFont *font = [NSFont fontWithName:GetNSStringForString(fe->Name())
|
|
|
|
size:0.0];
|
|
|
|
if (font) {
|
|
|
|
NSString *localized = [font displayName];
|
|
|
|
if (localized) {
|
|
|
|
GetStringForNSString(localized, aLocalizedName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed to get localized name, just use the canonical one
|
|
|
|
aLocalizedName = mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-28 17:41:25 -08:00
|
|
|
gfxSingleFaceMacFontFamily::ReadOtherFamilyNames(gfxPlatformFontList *aPlatformFontList)
|
2009-09-28 03:25:22 -07:00
|
|
|
{
|
2009-10-12 16:43:43 -07:00
|
|
|
if (mOtherFamilyNamesInitialized)
|
2009-09-28 03:25:22 -07:00
|
|
|
return;
|
|
|
|
|
2010-01-28 17:41:25 -08:00
|
|
|
gfxFontEntry *fe = mAvailableFonts[0];
|
|
|
|
if (!fe)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const PRUint32 kNAME = TRUETYPE_TAG('n','a','m','e');
|
2011-01-06 20:45:10 -08:00
|
|
|
AutoFallibleTArray<PRUint8,8192> buffer;
|
2010-01-28 17:41:25 -08:00
|
|
|
|
|
|
|
if (fe->GetFontTable(kNAME, buffer) != NS_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mHasOtherFamilyNames = ReadOtherFamilyNamesForFace(aPlatformFontList,
|
|
|
|
buffer,
|
2011-10-17 07:59:28 -07:00
|
|
|
true);
|
|
|
|
mOtherFamilyNamesInitialized = true;
|
2009-09-28 03:25:22 -07:00
|
|
|
}
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
/* gfxMacPlatformFontList */
|
|
|
|
#pragma mark-
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
gfxMacPlatformFontList::gfxMacPlatformFontList() :
|
2012-03-08 18:05:47 -08:00
|
|
|
gfxPlatformFontList(false), mATSGeneration(PRUint32(kATSGenerationInitial)),
|
|
|
|
mDefaultFont(nsnull)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
|
|
|
::ATSFontNotificationSubscribe(ATSNotification,
|
|
|
|
kATSFontNotifyOptionDefault,
|
|
|
|
(void*)this, nsnull);
|
|
|
|
|
|
|
|
// this should always be available (though we won't actually fail if it's missing,
|
|
|
|
// we'll just end up doing a search and then caching the new result instead)
|
|
|
|
mReplacementCharFallbackFamily = NS_LITERAL_STRING("Lucida Grande");
|
2009-10-12 16:43:43 -07:00
|
|
|
|
|
|
|
// cache this in a static variable so that MacOSFontFamily objects
|
|
|
|
// don't have to repeatedly look it up
|
|
|
|
sFontManager = [NSFontManager sharedFontManager];
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
2012-03-08 18:05:47 -08:00
|
|
|
gfxMacPlatformFontList::~gfxMacPlatformFontList()
|
|
|
|
{
|
|
|
|
if (mDefaultFont) {
|
|
|
|
::CFRelease(mDefaultFont);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 03:02:27 -08:00
|
|
|
nsresult
|
2009-08-16 06:52:12 -07:00
|
|
|
gfxMacPlatformFontList::InitFontList()
|
|
|
|
{
|
2010-03-09 08:37:56 -08:00
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
ATSGeneration currentGeneration = ::ATSGetGeneration();
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// need to ignore notifications after adding each font
|
|
|
|
if (mATSGeneration == currentGeneration)
|
2010-11-08 03:02:27 -08:00
|
|
|
return NS_OK;
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-07-26 23:42:53 -07:00
|
|
|
Telemetry::AutoTimer<Telemetry::MAC_INITFONTLIST_TOTAL> timer;
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
mATSGeneration = currentGeneration;
|
2011-01-21 08:44:33 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
LOG_FONTLIST(("(fontlist) updating to generation: %d", mATSGeneration));
|
|
|
|
#endif
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2010-01-28 17:41:25 -08:00
|
|
|
// reset font lists
|
|
|
|
gfxPlatformFontList::InitFontList();
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// iterate over available families
|
2009-10-12 16:43:43 -07:00
|
|
|
NSEnumerator *families = [[sFontManager availableFontFamilies]
|
|
|
|
objectEnumerator]; // returns "canonical", non-localized family name
|
|
|
|
|
|
|
|
nsAutoString availableFamilyName;
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
NSString *availableFamily = nil;
|
|
|
|
while ((availableFamily = [families nextObject])) {
|
|
|
|
|
|
|
|
// make a nsString
|
|
|
|
GetStringForNSString(availableFamily, availableFamilyName);
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// create a family entry
|
2009-09-28 03:25:22 -07:00
|
|
|
gfxFontFamily *familyEntry = new gfxMacFontFamily(availableFamilyName);
|
2009-08-16 06:52:12 -07:00
|
|
|
if (!familyEntry) break;
|
|
|
|
|
|
|
|
// add the family entry to the hash table
|
|
|
|
ToLowerCase(availableFamilyName);
|
|
|
|
mFontFamilies.Put(availableFamilyName, familyEntry);
|
2010-01-27 22:56:16 -08:00
|
|
|
|
|
|
|
// check the bad underline blacklist
|
2010-02-11 04:00:07 -08:00
|
|
|
if (mBadUnderlineFamilyNames.Contains(availableFamilyName))
|
2010-01-27 22:56:16 -08:00
|
|
|
familyEntry->SetBadUnderlineFamily();
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
InitSingleFaceList();
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
// to avoid full search of font name tables, seed the other names table with localized names from
|
2009-08-16 06:52:12 -07:00
|
|
|
// some of the prefs fonts which are accessed via their localized names. changes in the pref fonts will only cause
|
|
|
|
// a font lookup miss earlier. this is a simple optimization, it's not required for correctness
|
|
|
|
PreloadNamesList();
|
|
|
|
|
|
|
|
// start the delayed cmap loader
|
2009-10-12 16:43:43 -07:00
|
|
|
StartLoader(kDelayBeforeLoadingCmaps, kIntervalBetweenLoadingCmaps);
|
2010-11-08 03:02:27 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxMacPlatformFontList::InitSingleFaceList()
|
|
|
|
{
|
|
|
|
nsAutoTArray<nsString, 10> singleFaceFonts;
|
|
|
|
gfxFontUtils::GetPrefsFontList("font.single-face-list", singleFaceFonts);
|
|
|
|
|
|
|
|
PRUint32 numFonts = singleFaceFonts.Length();
|
|
|
|
for (PRUint32 i = 0; i < numFonts; i++) {
|
2011-01-21 08:44:33 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
LOG_FONTLIST(("(fontlist-singleface) face name: %s\n",
|
|
|
|
NS_ConvertUTF16toUTF8(singleFaceFonts[i]).get()));
|
|
|
|
#endif
|
2009-08-16 06:52:12 -07:00
|
|
|
gfxFontEntry *fontEntry = LookupLocalFont(nsnull, singleFaceFonts[i]);
|
|
|
|
if (fontEntry) {
|
|
|
|
nsAutoString familyName, key;
|
2009-09-28 17:38:59 -07:00
|
|
|
familyName = singleFaceFonts[i];
|
2009-08-16 06:52:12 -07:00
|
|
|
GenerateFontListKey(familyName, key);
|
2011-01-21 08:44:33 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
LOG_FONTLIST(("(fontlist-singleface) family name: %s, key: %s\n",
|
|
|
|
NS_ConvertUTF16toUTF8(familyName).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(key).get()));
|
|
|
|
#endif
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
// add only if doesn't exist already
|
2011-09-28 23:19:26 -07:00
|
|
|
bool found;
|
2009-08-16 06:52:12 -07:00
|
|
|
gfxFontFamily *familyEntry;
|
|
|
|
if (!(familyEntry = mFontFamilies.GetWeak(key, &found))) {
|
2009-09-28 03:25:22 -07:00
|
|
|
familyEntry = new gfxSingleFaceMacFontFamily(familyName);
|
2009-08-16 06:52:12 -07:00
|
|
|
familyEntry->AddFontEntry(fontEntry);
|
2011-10-17 07:59:28 -07:00
|
|
|
familyEntry->SetHasStyles(true);
|
2009-08-16 06:52:12 -07:00
|
|
|
mFontFamilies.Put(key, familyEntry);
|
|
|
|
fontEntry->mFamily = familyEntry;
|
2011-01-21 08:44:33 -08:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
LOG_FONTLIST(("(fontlist-singleface) added new family\n",
|
|
|
|
NS_ConvertUTF16toUTF8(familyName).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(key).get()));
|
|
|
|
#endif
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-08-16 06:52:12 -07:00
|
|
|
gfxMacPlatformFontList::GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName)
|
|
|
|
{
|
|
|
|
gfxFontFamily *family = FindFamily(aFontName);
|
|
|
|
if (family) {
|
2009-09-28 03:25:22 -07:00
|
|
|
family->LocalizedName(aFamilyName);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gecko 1.8 used Quickdraw font api's which produce a slightly different set of "family"
|
|
|
|
// names. Try to resolve based on these names, in case this is stored in an old profile
|
|
|
|
// 1.8: "Futura", "Futura Condensed" ==> 1.9: "Futura"
|
|
|
|
|
|
|
|
// convert the name to a Pascal-style Str255 to try as Quickdraw name
|
|
|
|
Str255 qdname;
|
|
|
|
NS_ConvertUTF16toUTF8 utf8name(aFontName);
|
2011-06-25 07:06:02 -07:00
|
|
|
qdname[0] = NS_MAX<size_t>(255, strlen(utf8name.get()));
|
2009-08-16 06:52:12 -07:00
|
|
|
memcpy(&qdname[1], utf8name.get(), qdname[0]);
|
|
|
|
|
|
|
|
// look up the Quickdraw name
|
|
|
|
ATSFontFamilyRef atsFamily = ::ATSFontFamilyFindFromQuickDrawName(qdname);
|
|
|
|
if (atsFamily == (ATSFontFamilyRef)kInvalidFontFamily) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we found a family, get its ATS name
|
|
|
|
CFStringRef cfName;
|
|
|
|
OSStatus status = ::ATSFontFamilyGetName(atsFamily, kATSOptionFlagsDefault, &cfName);
|
|
|
|
if (status != noErr) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// then use this to locate the family entry and retrieve its localized name
|
|
|
|
nsAutoString familyName;
|
|
|
|
GetStringForNSString((const NSString*)cfName, familyName);
|
|
|
|
::CFRelease(cfName);
|
|
|
|
|
|
|
|
family = FindFamily(familyName);
|
|
|
|
if (family) {
|
2009-09-28 03:25:22 -07:00
|
|
|
family->LocalizedName(aFamilyName);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxMacPlatformFontList::ATSNotification(ATSFontNotificationInfoRef aInfo,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
// xxx - should be carefully pruning the list of fonts, not rebuilding it from scratch
|
|
|
|
gfxMacPlatformFontList *qfc = (gfxMacPlatformFontList*)aUserArg;
|
|
|
|
qfc->UpdateFontList();
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:05:47 -08:00
|
|
|
gfxFontEntry*
|
|
|
|
gfxMacPlatformFontList::GlobalFontFallback(const PRUint32 aCh,
|
|
|
|
PRInt32 aRunScript,
|
|
|
|
const gfxFontStyle* aMatchStyle,
|
|
|
|
PRUint32& aCmapCount)
|
|
|
|
{
|
|
|
|
bool useCmaps = gfxPlatform::GetPlatform()->UseCmapsDuringSystemFallback();
|
|
|
|
|
|
|
|
if (useCmaps) {
|
|
|
|
return gfxPlatformFontList::GlobalFontFallback(aCh,
|
|
|
|
aRunScript,
|
|
|
|
aMatchStyle,
|
|
|
|
aCmapCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFStringRef str;
|
|
|
|
UniChar ch[2];
|
|
|
|
CFIndex len = 1;
|
|
|
|
|
|
|
|
if (IS_IN_BMP(aCh)) {
|
|
|
|
ch[0] = aCh;
|
|
|
|
str = ::CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, ch, 1,
|
|
|
|
kCFAllocatorNull);
|
|
|
|
} else {
|
|
|
|
ch[0] = H_SURROGATE(aCh);
|
|
|
|
ch[1] = L_SURROGATE(aCh);
|
|
|
|
str = ::CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, ch, 2,
|
|
|
|
kCFAllocatorNull);
|
|
|
|
if (!str) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use CoreText to find the fallback family
|
|
|
|
|
|
|
|
gfxFontEntry *fontEntry = nsnull;
|
|
|
|
CTFontRef fallback;
|
|
|
|
bool cantUseFallbackFont = false;
|
|
|
|
|
|
|
|
if (!mDefaultFont) {
|
|
|
|
mDefaultFont = ::CTFontCreateWithName(CFSTR("Lucida Grande"), 12.f,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
fallback = ::CTFontCreateForString(mDefaultFont, str,
|
|
|
|
::CFRangeMake(0, len));
|
|
|
|
|
|
|
|
if (fallback) {
|
|
|
|
CFStringRef familyName = ::CTFontCopyFamilyName(fallback);
|
|
|
|
::CFRelease(fallback);
|
|
|
|
|
|
|
|
if (familyName &&
|
|
|
|
::CFStringCompare(familyName, CFSTR("LastResort"),
|
|
|
|
kCFCompareCaseInsensitive) != kCFCompareEqualTo)
|
|
|
|
{
|
|
|
|
nsAutoTArray<UniChar, 1024> buffer;
|
|
|
|
CFIndex len = ::CFStringGetLength(familyName);
|
|
|
|
buffer.SetLength(len+1);
|
|
|
|
::CFStringGetCharacters(familyName, ::CFRangeMake(0, len),
|
|
|
|
buffer.Elements());
|
|
|
|
buffer[len] = 0;
|
|
|
|
nsDependentString family(buffer.Elements(), len);
|
|
|
|
|
|
|
|
bool needsBold; // ignored in the system fallback case
|
|
|
|
|
|
|
|
fontEntry = FindFontForFamily(family, aMatchStyle, needsBold);
|
|
|
|
if (fontEntry && !fontEntry->TestCharacterMap(aCh)) {
|
|
|
|
fontEntry = nsnull;
|
|
|
|
cantUseFallbackFont = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (familyName) {
|
|
|
|
::CFRelease(familyName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cantUseFallbackFont) {
|
|
|
|
Telemetry::Accumulate(Telemetry::BAD_FALLBACK_FONT, cantUseFallbackFont);
|
|
|
|
}
|
|
|
|
|
|
|
|
::CFRelease(str);
|
|
|
|
|
|
|
|
return fontEntry;
|
|
|
|
}
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
gfxFontEntry*
|
2011-09-28 23:19:26 -07:00
|
|
|
gfxMacPlatformFontList::GetDefaultFont(const gfxFontStyle* aStyle, bool& aNeedsBold)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
2010-03-09 08:37:56 -08:00
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
NSString *defaultFamily = [[NSFont userFontOfSize:aStyle->size] familyName];
|
|
|
|
nsAutoString familyName;
|
|
|
|
|
|
|
|
GetStringForNSString(defaultFamily, familyName);
|
|
|
|
return FindFontForFamily(familyName, aStyle, aNeedsBold);
|
|
|
|
}
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
PRInt32
|
2009-08-16 06:52:12 -07:00
|
|
|
gfxMacPlatformFontList::AppleWeightToCSSWeight(PRInt32 aAppleWeight)
|
|
|
|
{
|
|
|
|
if (aAppleWeight < 1)
|
|
|
|
aAppleWeight = 1;
|
|
|
|
else if (aAppleWeight > kAppleMaxWeight)
|
|
|
|
aAppleWeight = kAppleMaxWeight;
|
|
|
|
return gAppleWeightToCSSWeight[aAppleWeight];
|
|
|
|
}
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
gfxFontEntry*
|
2009-08-16 06:52:12 -07:00
|
|
|
gfxMacPlatformFontList::LookupLocalFont(const gfxProxyFontEntry *aProxyEntry,
|
2009-10-07 07:13:40 -07:00
|
|
|
const nsAString& aFontName)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
2010-03-09 08:37:56 -08:00
|
|
|
nsAutoreleasePool localPool;
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
NSString *faceName = GetNSStringForString(aFontName);
|
2011-06-24 10:55:27 -07:00
|
|
|
MacOSFontEntry *newFontEntry;
|
|
|
|
|
|
|
|
if (UseATSFontEntry()) {
|
|
|
|
// first lookup a single face based on postscript name
|
|
|
|
ATSFontRef fontRef = ::ATSFontFindFromPostScriptName(CFStringRef(faceName),
|
|
|
|
kATSOptionFlagsDefault);
|
|
|
|
|
|
|
|
// if not found, lookup using full font name
|
|
|
|
if (fontRef == kInvalidFont) {
|
|
|
|
fontRef = ::ATSFontFindFromName(CFStringRef(faceName),
|
|
|
|
kATSOptionFlagsDefault);
|
|
|
|
if (fontRef == kInvalidFont) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aProxyEntry) {
|
|
|
|
PRUint16 w = aProxyEntry->mWeight;
|
|
|
|
NS_ASSERTION(w >= 100 && w <= 900, "bogus font weight value!");
|
|
|
|
|
|
|
|
newFontEntry =
|
|
|
|
new ATSFontEntry(aFontName, fontRef,
|
|
|
|
w, aProxyEntry->mStretch,
|
|
|
|
aProxyEntry->mItalic ?
|
2012-03-15 02:04:46 -07:00
|
|
|
NS_FONT_STYLE_ITALIC : NS_FONT_STYLE_NORMAL,
|
2011-10-17 07:59:28 -07:00
|
|
|
nsnull, true);
|
2011-06-24 10:55:27 -07:00
|
|
|
} else {
|
|
|
|
newFontEntry =
|
|
|
|
new ATSFontEntry(aFontName, fontRef,
|
2012-03-15 02:04:46 -07:00
|
|
|
400, 0, NS_FONT_STYLE_NORMAL, nsnull, false);
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// lookup face based on postscript or full name
|
|
|
|
CGFontRef fontRef = ::CGFontCreateWithFontName(CFStringRef(faceName));
|
|
|
|
if (!fontRef) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aProxyEntry) {
|
|
|
|
PRUint16 w = aProxyEntry->mWeight;
|
|
|
|
NS_ASSERTION(w >= 100 && w <= 900, "bogus font weight value!");
|
|
|
|
|
|
|
|
newFontEntry =
|
|
|
|
new CGFontEntry(aFontName, fontRef,
|
|
|
|
w, aProxyEntry->mStretch,
|
|
|
|
aProxyEntry->mItalic ?
|
2012-03-15 02:04:46 -07:00
|
|
|
NS_FONT_STYLE_ITALIC : NS_FONT_STYLE_NORMAL,
|
2011-10-17 07:59:28 -07:00
|
|
|
true, true);
|
2011-06-24 10:55:27 -07:00
|
|
|
} else {
|
|
|
|
newFontEntry =
|
|
|
|
new CGFontEntry(aFontName, fontRef,
|
2012-03-15 02:04:46 -07:00
|
|
|
400, 0, NS_FONT_STYLE_NORMAL,
|
2011-10-17 07:59:28 -07:00
|
|
|
false, false);
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
|
|
|
::CFRelease(fontRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newFontEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontEntry*
|
|
|
|
gfxMacPlatformFontList::MakePlatformFont(const gfxProxyFontEntry *aProxyEntry,
|
|
|
|
const PRUint8 *aFontData,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
return UseATSFontEntry()
|
|
|
|
? MakePlatformFontATS(aProxyEntry, aFontData, aLength)
|
|
|
|
: MakePlatformFontCG(aProxyEntry, aFontData, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReleaseData(void *info, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
NS_Free((void*)data);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontEntry*
|
|
|
|
gfxMacPlatformFontList::MakePlatformFontCG(const gfxProxyFontEntry *aProxyEntry,
|
|
|
|
const PRUint8 *aFontData,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aFontData, "MakePlatformFont called with null data");
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
PRUint16 w = aProxyEntry->mWeight;
|
|
|
|
NS_ASSERTION(w >= 100 && w <= 900, "bogus font weight value!");
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
// create the font entry
|
|
|
|
nsAutoString uniqueName;
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
nsresult rv = gfxFontUtils::MakeUniqueUserFontName(uniqueName);
|
|
|
|
if (NS_FAILED(rv)) {
|
2009-08-16 06:52:12 -07:00
|
|
|
return nsnull;
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
CGDataProviderRef provider =
|
|
|
|
::CGDataProviderCreateWithData(nsnull, aFontData, aLength,
|
|
|
|
&ReleaseData);
|
|
|
|
CGFontRef fontRef = ::CGFontCreateWithDataProvider(provider);
|
|
|
|
::CGDataProviderRelease(provider);
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
if (!fontRef) {
|
|
|
|
return nsnull;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
nsAutoPtr<CGFontEntry>
|
|
|
|
newFontEntry(new CGFontEntry(uniqueName, fontRef, w,
|
|
|
|
aProxyEntry->mStretch,
|
|
|
|
aProxyEntry->mItalic ?
|
2012-03-15 02:04:46 -07:00
|
|
|
NS_FONT_STYLE_ITALIC : NS_FONT_STYLE_NORMAL,
|
2011-10-17 07:59:28 -07:00
|
|
|
true, false));
|
2011-12-19 02:36:05 -08:00
|
|
|
::CFRelease(fontRef);
|
2011-06-24 10:55:27 -07:00
|
|
|
|
|
|
|
// if succeeded and font cmap is good, return the new font
|
|
|
|
if (newFontEntry->mIsValid && NS_SUCCEEDED(newFontEntry->ReadCMAP())) {
|
|
|
|
return newFontEntry.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if something is funky about this font, delete immediately
|
|
|
|
#if DEBUG
|
|
|
|
char warnBuf[1024];
|
|
|
|
sprintf(warnBuf, "downloaded font not loaded properly, removed face for (%s)",
|
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->Name()).get());
|
|
|
|
NS_WARNING(warnBuf);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return nsnull;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// grumble, another non-publised Apple API dependency (found in Webkit code)
|
|
|
|
// activated with this value, font will not be found via system lookup routines
|
|
|
|
// it can only be used via the created ATSFontRef
|
|
|
|
// needed to prevent one doc from finding a font used in a separate doc
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kPrivateATSFontContextPrivate = 3
|
|
|
|
};
|
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
class ATSUserFontData : public gfxUserFontData {
|
2009-08-16 06:52:12 -07:00
|
|
|
public:
|
2011-06-24 10:55:27 -07:00
|
|
|
ATSUserFontData(ATSFontContainerRef aContainerRef)
|
2009-08-16 06:52:12 -07:00
|
|
|
: mContainerRef(aContainerRef)
|
|
|
|
{ }
|
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
virtual ~ATSUserFontData()
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
|
|
|
// deactivate font
|
2011-06-24 10:55:27 -07:00
|
|
|
if (mContainerRef) {
|
2009-08-16 06:52:12 -07:00
|
|
|
::ATSFontDeactivate(mContainerRef, NULL, kATSOptionFlagsDefault);
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ATSFontContainerRef mContainerRef;
|
|
|
|
};
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
gfxFontEntry*
|
2011-06-24 10:55:27 -07:00
|
|
|
gfxMacPlatformFontList::MakePlatformFontATS(const gfxProxyFontEntry *aProxyEntry,
|
|
|
|
const PRUint8 *aFontData,
|
|
|
|
PRUint32 aLength)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
|
|
|
OSStatus err;
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2009-08-30 06:55:24 -07:00
|
|
|
NS_ASSERTION(aFontData, "MakePlatformFont called with null data");
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2009-10-07 07:13:40 -07:00
|
|
|
// MakePlatformFont is responsible for deleting the font data with NS_Free
|
|
|
|
// so we set up a stack object to ensure it is freed even if we take an
|
|
|
|
// early exit
|
|
|
|
struct FontDataDeleter {
|
|
|
|
FontDataDeleter(const PRUint8 *aFontData)
|
|
|
|
: mFontData(aFontData) { }
|
|
|
|
~FontDataDeleter() { NS_Free((void*)mFontData); }
|
|
|
|
const PRUint8 *mFontData;
|
|
|
|
};
|
|
|
|
FontDataDeleter autoDelete(aFontData);
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
ATSFontRef fontRef;
|
|
|
|
ATSFontContainerRef containerRef;
|
|
|
|
|
|
|
|
// we get occasional failures when multiple fonts are activated in quick succession
|
|
|
|
// if the ATS font cache is damaged; to work around this, we can retry the activation
|
|
|
|
const PRUint32 kMaxRetries = 3;
|
|
|
|
PRUint32 retryCount = 0;
|
|
|
|
while (retryCount++ < kMaxRetries) {
|
2009-08-30 06:55:24 -07:00
|
|
|
err = ::ATSFontActivateFromMemory(const_cast<PRUint8*>(aFontData), aLength,
|
2009-08-16 06:52:12 -07:00
|
|
|
kPrivateATSFontContextPrivate,
|
|
|
|
kATSFontFormatUnspecified,
|
2009-10-12 16:43:43 -07:00
|
|
|
NULL,
|
|
|
|
kATSOptionFlagsDoNotNotify,
|
2009-08-16 06:52:12 -07:00
|
|
|
&containerRef);
|
|
|
|
mATSGeneration = ::ATSGetGeneration();
|
|
|
|
|
|
|
|
if (err != noErr) {
|
|
|
|
#if DEBUG
|
|
|
|
char warnBuf[1024];
|
|
|
|
sprintf(warnBuf, "downloaded font error, ATSFontActivateFromMemory err: %d for (%s)",
|
|
|
|
PRInt32(err),
|
2009-10-07 07:13:40 -07:00
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->Name()).get());
|
2009-08-16 06:52:12 -07:00
|
|
|
NS_WARNING(warnBuf);
|
2009-10-12 16:43:43 -07:00
|
|
|
#endif
|
2009-08-16 06:52:12 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignoring containers with multiple fonts, use the first face only for now
|
2009-10-12 16:43:43 -07:00
|
|
|
err = ::ATSFontFindFromContainer(containerRef, kATSOptionFlagsDefault, 1,
|
2009-08-16 06:52:12 -07:00
|
|
|
&fontRef, NULL);
|
|
|
|
if (err != noErr) {
|
|
|
|
#if DEBUG
|
|
|
|
char warnBuf[1024];
|
|
|
|
sprintf(warnBuf, "downloaded font error, ATSFontFindFromContainer err: %d for (%s)",
|
|
|
|
PRInt32(err),
|
2009-10-07 07:13:40 -07:00
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->Name()).get());
|
2009-08-16 06:52:12 -07:00
|
|
|
NS_WARNING(warnBuf);
|
2009-10-12 16:43:43 -07:00
|
|
|
#endif
|
2009-08-16 06:52:12 -07:00
|
|
|
::ATSFontDeactivate(containerRef, NULL, kATSOptionFlagsDefault);
|
|
|
|
return nsnull;
|
|
|
|
}
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// now lookup the Postscript name; this may fail if the font cache is bad
|
|
|
|
OSStatus err;
|
|
|
|
NSString *psname = NULL;
|
|
|
|
err = ::ATSFontGetPostScriptName(fontRef, kATSOptionFlagsDefault, (CFStringRef*) (&psname));
|
|
|
|
if (err == noErr) {
|
|
|
|
[psname release];
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
char warnBuf[1024];
|
|
|
|
sprintf(warnBuf, "ATSFontGetPostScriptName err = %d for (%s), retries = %d", (PRInt32)err,
|
2009-10-07 07:13:40 -07:00
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->Name()).get(), retryCount);
|
2009-08-16 06:52:12 -07:00
|
|
|
NS_WARNING(warnBuf);
|
|
|
|
#endif
|
|
|
|
::ATSFontDeactivate(containerRef, NULL, kATSOptionFlagsDefault);
|
|
|
|
// retry the activation a couple of times if this fails
|
|
|
|
// (may be a transient failure due to ATS font cache issues)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 w = aProxyEntry->mWeight;
|
|
|
|
NS_ASSERTION(w >= 100 && w <= 900, "bogus font weight value!");
|
|
|
|
|
|
|
|
nsAutoString uniqueName;
|
|
|
|
nsresult rv = gfxFontUtils::MakeUniqueUserFontName(uniqueName);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
2009-10-12 16:43:43 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
// font entry will own this
|
|
|
|
ATSUserFontData *userFontData = new ATSUserFontData(containerRef);
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2011-06-24 10:55:27 -07:00
|
|
|
ATSFontEntry *newFontEntry =
|
|
|
|
new ATSFontEntry(uniqueName,
|
|
|
|
fontRef,
|
|
|
|
w, aProxyEntry->mStretch,
|
|
|
|
aProxyEntry->mItalic ?
|
2012-03-15 02:04:46 -07:00
|
|
|
NS_FONT_STYLE_ITALIC : NS_FONT_STYLE_NORMAL,
|
2011-10-17 07:59:28 -07:00
|
|
|
userFontData, false);
|
2009-08-16 06:52:12 -07:00
|
|
|
|
2009-09-17 17:45:29 -07:00
|
|
|
// if succeeded and font cmap is good, return the new font
|
2011-06-24 10:55:27 -07:00
|
|
|
if (newFontEntry->mIsValid && NS_SUCCEEDED(newFontEntry->ReadCMAP())) {
|
2009-08-16 06:52:12 -07:00
|
|
|
return newFontEntry;
|
2011-06-24 10:55:27 -07:00
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
// if something is funky about this font, delete immediately
|
|
|
|
#if DEBUG
|
|
|
|
char warnBuf[1024];
|
2009-10-12 16:43:43 -07:00
|
|
|
sprintf(warnBuf, "downloaded font not loaded properly, removed face for (%s)",
|
2009-10-07 07:13:40 -07:00
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->Name()).get());
|
2009-08-16 06:52:12 -07:00
|
|
|
NS_WARNING(warnBuf);
|
2009-10-12 16:43:43 -07:00
|
|
|
#endif
|
2009-08-16 06:52:12 -07:00
|
|
|
delete newFontEntry;
|
|
|
|
|
|
|
|
// We don't retry from here; the ATS font cache issue would have caused failure earlier
|
|
|
|
// so if we get here, there's something else bad going on within our font data structures.
|
|
|
|
// Currently, there should be no way to reach here, as fontentry creation cannot fail
|
|
|
|
// except by memory allocation failure.
|
|
|
|
NS_WARNING("invalid font entry for a newly activated font");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we get here, the activation failed (even with possible retries); can't use this font
|
|
|
|
return nsnull;
|
|
|
|
}
|