1 /* 2 * Copyright (c) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "FontCache.h" 33 34 #include "AtomicString.h" 35 #include "ChromiumBridge.h" 36 #include "CString.h" 37 #include "Font.h" 38 #include "FontDescription.h" 39 #include "FontPlatformData.h" 40 #include "Logging.h" 41 #include "NotImplemented.h" 42 #include "SimpleFontData.h" 43 44 #include "SkPaint.h" 45 #include "SkTypeface.h" 46 #include "SkUtils.h" 47 48 #include <unicode/utf16.h> 49 #include <wtf/Assertions.h> 50 51 namespace WebCore { 52 53 void FontCache::platformInit() 54 { 55 } 56 57 const SimpleFontData* FontCache::getFontDataForCharacters(const Font& font, 58 const UChar* characters, 59 int length) 60 { 61 String family = ChromiumBridge::getFontFamilyForCharacters(characters, length); 62 if (family.isEmpty()) 63 return 0; 64 65 AtomicString atomicFamily(family); 66 return getCachedFontData(getCachedFontPlatformData(font.fontDescription(), atomicFamily, false)); 67 } 68 69 SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font) 70 { 71 return 0; 72 } 73 74 SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& description) 75 { 76 static const AtomicString sansStr("Sans"); 77 static const AtomicString serifStr("Serif"); 78 static const AtomicString monospaceStr("Monospace"); 79 80 FontPlatformData* fontPlatformData = 0; 81 switch (description.genericFamily()) { 82 case FontDescription::SerifFamily: 83 fontPlatformData = getCachedFontPlatformData(description, serifStr); 84 break; 85 case FontDescription::MonospaceFamily: 86 fontPlatformData = getCachedFontPlatformData(description, monospaceStr); 87 break; 88 case FontDescription::SansSerifFamily: 89 default: 90 fontPlatformData = getCachedFontPlatformData(description, sansStr); 91 break; 92 } 93 94 ASSERT(fontPlatformData); 95 return getCachedFontData(fontPlatformData); 96 } 97 98 void FontCache::getTraitsInFamily(const AtomicString& familyName, 99 Vector<unsigned>& traitsMasks) 100 { 101 notImplemented(); 102 } 103 104 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, 105 const AtomicString& family) 106 { 107 const char* name = 0; 108 CString s; 109 110 if (family.length() == 0) { 111 static const struct { 112 FontDescription::GenericFamilyType mType; 113 const char* mName; 114 } fontDescriptions[] = { 115 { FontDescription::SerifFamily, "serif" }, 116 { FontDescription::SansSerifFamily, "sans-serif" }, 117 { FontDescription::MonospaceFamily, "monospace" }, 118 { FontDescription::CursiveFamily, "cursive" }, 119 { FontDescription::FantasyFamily, "fantasy" } 120 }; 121 122 FontDescription::GenericFamilyType type = fontDescription.genericFamily(); 123 for (unsigned i = 0; i < SK_ARRAY_COUNT(fontDescriptions); i++) { 124 if (type == fontDescriptions[i].mType) { 125 name = fontDescriptions[i].mName; 126 break; 127 } 128 } 129 // if we fall out of the loop, it's ok for name to still be 0 130 } 131 else { // convert the name to utf8 132 s = family.string().utf8(); 133 name = s.data(); 134 } 135 136 int style = SkTypeface::kNormal; 137 if (fontDescription.weight() >= FontWeightBold) 138 style |= SkTypeface::kBold; 139 if (fontDescription.italic()) 140 style |= SkTypeface::kItalic; 141 142 SkTypeface* tf = SkTypeface::CreateFromName(name, static_cast<SkTypeface::Style>(style)); 143 if (!tf) 144 return 0; 145 146 FontPlatformData* result = 147 new FontPlatformData(tf, 148 fontDescription.computedSize(), 149 (style & SkTypeface::kBold) && !tf->isBold(), 150 (style & SkTypeface::kItalic) && !tf->isItalic()); 151 tf->unref(); 152 return result; 153 } 154 155 } // namespace WebCore 156