1 /*
2 * Copyright (C) 2007 Apple Computer, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21 #include "config.h"
22 #include "FontCustomPlatformData.h"
23
24 #include <ApplicationServices/ApplicationServices.h>
25 #include "SharedBuffer.h"
26 #include "FontPlatformData.h"
27 #include "OpenTypeSanitizer.h"
28
29 namespace WebCore {
30
~FontCustomPlatformData()31 FontCustomPlatformData::~FontCustomPlatformData()
32 {
33 if (m_atsContainer)
34 ATSFontDeactivate(m_atsContainer, NULL, kATSOptionFlagsDefault);
35 CGFontRelease(m_cgFont);
36 }
37
fontPlatformData(int size,bool bold,bool italic,FontRenderingMode)38 FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontRenderingMode)
39 {
40 return FontPlatformData(m_cgFont, (ATSUFontID)m_atsFont, size, bold, italic);
41 }
42
createFontCustomPlatformData(SharedBuffer * buffer)43 FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
44 {
45 ASSERT_ARG(buffer, buffer);
46
47 #if ENABLE(OPENTYPE_SANITIZER)
48 OpenTypeSanitizer sanitizer(buffer);
49 RefPtr<SharedBuffer> transcodeBuffer = sanitizer.sanitize();
50 if (!transcodeBuffer)
51 return 0; // validation failed.
52 buffer = transcodeBuffer.get();
53 #endif
54
55 ATSFontContainerRef containerRef = 0;
56 ATSFontRef fontRef = 0;
57
58 RetainPtr<CGFontRef> cgFontRef;
59
60 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
61 RetainPtr<CFDataRef> bufferData(AdoptCF, buffer->createCFData());
62 RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(bufferData.get()));
63
64 cgFontRef.adoptCF(CGFontCreateWithDataProvider(dataProvider.get()));
65 if (!cgFontRef)
66 return 0;
67 #else
68 // Use ATS to activate the font.
69
70 // The value "3" means that the font is private and can't be seen by anyone else.
71 ATSFontActivateFromMemory((void*)buffer->data(), buffer->size(), 3, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault, &containerRef);
72 if (!containerRef)
73 return 0;
74 ItemCount fontCount;
75 ATSFontFindFromContainer(containerRef, kATSOptionFlagsDefault, 0, NULL, &fontCount);
76
77 // We just support the first font in the list.
78 if (fontCount == 0) {
79 ATSFontDeactivate(containerRef, NULL, kATSOptionFlagsDefault);
80 return 0;
81 }
82
83 ATSFontFindFromContainer(containerRef, kATSOptionFlagsDefault, 1, &fontRef, NULL);
84 if (!fontRef) {
85 ATSFontDeactivate(containerRef, NULL, kATSOptionFlagsDefault);
86 return 0;
87 }
88
89 cgFontRef.adoptCF(CGFontCreateWithPlatformFont(&fontRef));
90 #ifndef BUILDING_ON_TIGER
91 // Workaround for <rdar://problem/5675504>.
92 if (cgFontRef && !CGFontGetNumberOfGlyphs(cgFontRef.get()))
93 cgFontRef = 0;
94 #endif
95 if (!cgFontRef) {
96 ATSFontDeactivate(containerRef, NULL, kATSOptionFlagsDefault);
97 return 0;
98 }
99 #endif // !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
100
101 return new FontCustomPlatformData(containerRef, fontRef, cgFontRef.releaseRef());
102 }
103
104 }
105