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 "Font.h" 35 #include "FontDescription.h" 36 #include "FontPlatformData.h" 37 #include "Logging.h" 38 #include "NotImplemented.h" 39 #include "PlatformBridge.h" 40 #include "SimpleFontData.h" 41 42 #include "SkPaint.h" 43 #include "SkTypeface.h" 44 #include "SkUtils.h" 45 46 #include <unicode/locid.h> 47 #include <wtf/Assertions.h> 48 #include <wtf/text/AtomicString.h> 49 #include <wtf/text/CString.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 icu::Locale locale = icu::Locale::getDefault(); 62 String family = PlatformBridge::getFontFamilyForCharacters(characters, length, locale.getLanguage()); 63 if (family.isEmpty()) 64 return 0; 65 66 AtomicString atomicFamily(family); 67 return getCachedFontData(getCachedFontPlatformData(font.fontDescription(), atomicFamily, false)); 68 } 69 70 SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font) 71 { 72 return 0; 73 } 74 75 SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& description) 76 { 77 static const AtomicString sansStr("Sans"); 78 static const AtomicString serifStr("Serif"); 79 static const AtomicString monospaceStr("Monospace"); 80 81 FontPlatformData* fontPlatformData = 0; 82 switch (description.genericFamily()) { 83 case FontDescription::SerifFamily: 84 fontPlatformData = getCachedFontPlatformData(description, serifStr); 85 break; 86 case FontDescription::MonospaceFamily: 87 fontPlatformData = getCachedFontPlatformData(description, monospaceStr); 88 break; 89 case FontDescription::SansSerifFamily: 90 default: 91 fontPlatformData = getCachedFontPlatformData(description, sansStr); 92 break; 93 } 94 95 ASSERT(fontPlatformData); 96 return getCachedFontData(fontPlatformData); 97 } 98 99 void FontCache::getTraitsInFamily(const AtomicString& familyName, 100 Vector<unsigned>& traitsMasks) 101 { 102 notImplemented(); 103 } 104 105 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, 106 const AtomicString& family) 107 { 108 const char* name = 0; 109 CString s; 110 111 // If we're creating a fallback font (e.g. "-webkit-monospace"), convert the name into 112 // the fallback name (like "monospace") that fontconfig understands. 113 if (!family.length() || family.startsWith("-webkit-")) { 114 static const struct { 115 FontDescription::GenericFamilyType mType; 116 const char* mName; 117 } fontDescriptions[] = { 118 { FontDescription::SerifFamily, "serif" }, 119 { FontDescription::SansSerifFamily, "sans-serif" }, 120 { FontDescription::MonospaceFamily, "monospace" }, 121 { FontDescription::CursiveFamily, "cursive" }, 122 { FontDescription::FantasyFamily, "fantasy" } 123 }; 124 125 FontDescription::GenericFamilyType type = fontDescription.genericFamily(); 126 for (unsigned i = 0; i < SK_ARRAY_COUNT(fontDescriptions); i++) { 127 if (type == fontDescriptions[i].mType) { 128 name = fontDescriptions[i].mName; 129 break; 130 } 131 } 132 if (!name) 133 name = ""; 134 } else { 135 // convert the name to utf8 136 s = family.string().utf8(); 137 name = s.data(); 138 } 139 140 int style = SkTypeface::kNormal; 141 if (fontDescription.weight() >= FontWeightBold) 142 style |= SkTypeface::kBold; 143 if (fontDescription.italic()) 144 style |= SkTypeface::kItalic; 145 146 SkTypeface* tf = SkTypeface::CreateFromName(name, static_cast<SkTypeface::Style>(style)); 147 if (!tf) 148 return 0; 149 150 FontPlatformData* result = 151 new FontPlatformData(tf, 152 name, 153 fontDescription.computedSize(), 154 (style & SkTypeface::kBold) && !tf->isBold(), 155 (style & SkTypeface::kItalic) && !tf->isItalic(), 156 fontDescription.orientation(), 157 fontDescription.textOrientation()); 158 tf->unref(); 159 return result; 160 } 161 162 } // namespace WebCore 163