Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright 2009, The Android Open Source Project
      3  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      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 copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "FontCache.h"
     29 #include "FontPlatformData.h"
     30 #include "Font.h"
     31 #include "NotImplemented.h"
     32 #include "SimpleFontData.h"
     33 #include "SkPaint.h"
     34 #include "SkTypeface.h"
     35 #include "SkUtils.h"
     36 
     37 namespace WebCore {
     38 
     39 void FontCache::platformInit()
     40 {
     41 }
     42 
     43 const SimpleFontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
     44 {
     45     // since all of our fonts logically map to the fallback, we can always claim
     46     // that each font supports all characters.
     47     return font.primaryFont();
     48 }
     49 
     50 SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font)
     51 {
     52     return 0;
     53 }
     54 
     55 SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& font)
     56 {
     57     static AtomicString str("sans-serif");
     58     return getCachedFontData(font, str);
     59 }
     60 
     61 static char* AtomicStringToUTF8String(const AtomicString& utf16)
     62 {
     63     SkASSERT(sizeof(uint16_t) == sizeof(utf16.characters()[0]));
     64     const uint16_t* uni = (uint16_t*)utf16.characters();
     65 
     66     size_t bytes = SkUTF16_ToUTF8(uni, utf16.length(), NULL);
     67     char*  utf8 = (char*)sk_malloc_throw(bytes + 1);
     68 
     69     (void)SkUTF16_ToUTF8(uni, utf16.length(), utf8);
     70     utf8[bytes] = 0;
     71     return utf8;
     72 }
     73 
     74 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
     75 {
     76     char*       storage = 0;
     77     const char* name = 0;
     78 
     79     if (family.length() == 0) {
     80         static const struct {
     81             FontDescription::GenericFamilyType  mType;
     82             const char*                         mName;
     83         } gNames[] = {
     84             { FontDescription::SerifFamily,     "serif" },
     85             { FontDescription::SansSerifFamily, "sans-serif" },
     86             { FontDescription::MonospaceFamily, "monospace" },
     87             { FontDescription::CursiveFamily,   "cursive" },
     88             { FontDescription::FantasyFamily,   "fantasy" }
     89         };
     90 
     91         FontDescription::GenericFamilyType type = fontDescription.genericFamily();
     92         for (unsigned i = 0; i < SK_ARRAY_COUNT(gNames); i++)
     93         {
     94             if (type == gNames[i].mType)
     95             {
     96                 name = gNames[i].mName;
     97                 break;
     98             }
     99         }
    100         // if we fall out of the loop, its ok for name to still be 0
    101     }
    102     else {    // convert the name to utf8
    103         storage = AtomicStringToUTF8String(family);
    104         name = storage;
    105     }
    106 
    107     int style = SkTypeface::kNormal;
    108     if (fontDescription.weight() >= FontWeightBold)
    109         style |= SkTypeface::kBold;
    110     if (fontDescription.italic())
    111         style |= SkTypeface::kItalic;
    112 
    113     SkTypeface* tf = SkTypeface::CreateFromName(name, (SkTypeface::Style)style);
    114 
    115     FontPlatformData* result = new FontPlatformData(tf,
    116                                                     fontDescription.computedSize(),
    117                                                     (style & SkTypeface::kBold) && !tf->isBold(),
    118                                                     (style & SkTypeface::kItalic) && !tf->isItalic());
    119     tf->unref();
    120     sk_free(storage);
    121     return result;
    122 }
    123 
    124     // new as of SVN change 36269, Sept 8, 2008
    125 void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
    126 {
    127     // Don't understand this yet, but it seems safe to leave unimplemented
    128 }
    129 
    130 }
    131