📄 simplefontdatamac.mm
字号:
/* * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved. * Copyright (C) 2006 Alexey Proskuryakov * * 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. */#import "config.h"#import "SimpleFontData.h"#import "BlockExceptions.h"#import "Color.h"#import "FloatRect.h"#import "Font.h"#import "FontCache.h"#import "FontDescription.h"#import "SharedBuffer.h"#import "WebCoreSystemInterface.h"#import <ApplicationServices/ApplicationServices.h>#import <float.h>#import <unicode/uchar.h>#import <wtf/Assertions.h>#import <wtf/StdLibExtras.h>#import <wtf/RetainPtr.h>@interface NSFont (WebAppKitSecretAPI)- (BOOL)_isFakeFixedPitch;@endnamespace WebCore { const float smallCapsFontSizeMultiplier = 0.7f;const float contextDPI = 72.0f;static inline float scaleEmToUnits(float x, unsigned unitsPerEm) { return x * (contextDPI / (contextDPI * unitsPerEm)); }static bool initFontData(SimpleFontData* fontData){ if (!fontData->m_font.cgFont()) return false;#ifdef BUILDING_ON_TIGER ATSUStyle fontStyle; if (ATSUCreateStyle(&fontStyle) != noErr) return false; ATSUFontID fontId = fontData->m_font.m_atsuFontID; if (!fontId) { ATSUDisposeStyle(fontStyle); return false; } ATSUAttributeTag tag = kATSUFontTag; ByteCount size = sizeof(ATSUFontID); ATSUFontID *valueArray[1] = {&fontId}; OSStatus status = ATSUSetAttributes(fontStyle, 1, &tag, &size, (void* const*)valueArray); if (status != noErr) { ATSUDisposeStyle(fontStyle); return false; } if (wkGetATSStyleGroup(fontStyle, &fontData->m_styleGroup) != noErr) { ATSUDisposeStyle(fontStyle); return false; } ATSUDisposeStyle(fontStyle);#endif return true;}static NSString *webFallbackFontFamily(void){ DEFINE_STATIC_LOCAL(RetainPtr<NSString>, webFallbackFontFamily, ([[NSFont systemFontOfSize:16.0f] familyName])); return webFallbackFontFamily.get();}#if !ERROR_DISABLED#ifdef __LP64__static NSString* pathFromFont(NSFont*){ // FMGetATSFontRefFromFont is not available in 64-bit. As pathFromFont is only used for debugging // purposes, returning nil is acceptable. return nil;}#elsestatic NSString* pathFromFont(NSFont *font){#ifndef BUILDING_ON_TIGER ATSFontRef atsFont = FMGetATSFontRefFromFont(CTFontGetPlatformFont(toCTFontRef(font), 0));#else ATSFontRef atsFont = FMGetATSFontRefFromFont(wkGetNSFontATSUFontId(font));#endif FSRef fileRef;#ifndef BUILDING_ON_TIGER OSStatus status = ATSFontGetFileReference(atsFont, &fileRef); if (status != noErr) return nil;#else FSSpec oFile; OSStatus status = ATSFontGetFileSpecification(atsFont, &oFile); if (status != noErr) return nil; status = FSpMakeFSRef(&oFile, &fileRef); if (status != noErr) return nil;#endif UInt8 filePathBuffer[PATH_MAX]; status = FSRefMakePath(&fileRef, filePathBuffer, PATH_MAX); if (status == noErr) return [NSString stringWithUTF8String:(const char*)filePathBuffer]; return nil;}#endif // __LP64__#endif // !ERROR_DISABLEDvoid SimpleFontData::platformInit(){#ifdef BUILDING_ON_TIGER m_styleGroup = 0;#endif#if USE(ATSUI) m_ATSUStyleInitialized = false; m_ATSUMirrors = false; m_checkedShapesArabic = false; m_shapesArabic = false;#endif m_syntheticBoldOffset = m_font.m_syntheticBold ? 1.0f : 0.f; bool failedSetup = false; if (!initFontData(this)) { // Ack! Something very bad happened, like a corrupt font. // Try looking for an alternate 'base' font for this renderer. // Special case hack to use "Times New Roman" in place of "Times". // "Times RO" is a common font whose family name is "Times". // It overrides the normal "Times" family font. // It also appears to have a corrupt regular variant. NSString *fallbackFontFamily; if ([[m_font.font() familyName] isEqual:@"Times"]) fallbackFontFamily = @"Times New Roman"; else fallbackFontFamily = webFallbackFontFamily(); // Try setting up the alternate font. // This is a last ditch effort to use a substitute font when something has gone wrong.#if !ERROR_DISABLED RetainPtr<NSFont> initialFont = m_font.font();#endif if (m_font.font()) m_font.setFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toFamily:fallbackFontFamily]); else m_font.setFont([NSFont fontWithName:fallbackFontFamily size:m_font.size()]);#if !ERROR_DISABLED NSString *filePath = pathFromFont(initialFont.get()); if (!filePath) filePath = @"not known";#endif if (!initFontData(this)) { if ([fallbackFontFamily isEqual:@"Times New Roman"]) { // OK, couldn't setup Times New Roman as an alternate to Times, fallback // on the system font. If this fails we have no alternative left. m_font.setFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toFamily:webFallbackFontFamily()]); if (!initFontData(this)) { // We tried, Times, Times New Roman, and the system font. No joy. We have to give up. LOG_ERROR("unable to initialize with font %@ at %@", initialFont.get(), filePath); failedSetup = true; } } else { // We tried the requested font and the system font. No joy. We have to give up. LOG_ERROR("unable to initialize with font %@ at %@", initialFont.get(), filePath); failedSetup = true; } } // Report the problem. LOG_ERROR("Corrupt font detected, using %@ in place of %@ located at \"%@\".", [m_font.font() familyName], [initialFont.get() familyName], filePath); } // If all else fails, try to set up using the system font. // This is probably because Times and Times New Roman are both unavailable. if (failedSetup) { m_font.setFont([NSFont systemFontOfSize:[m_font.font() pointSize]]); LOG_ERROR("failed to set up font, using system font %s", m_font.font()); initFontData(this); } int iAscent; int iDescent; int iLineGap;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -