Home | History | Annotate | Download | only in fonts
      1 /*
      2  * Copyright (C) 2013 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
      6  * are met:
      7  *
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer
     12  *    in the documentation and/or other materials provided with the
     13  *    distribution.
     14  * 3. Neither the name of Google Inc. nor the names of its contributors
     15  *    may be used to endorse or promote products derived from this
     16  *    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 #ifndef FontCacheKey_h
     32 #define FontCacheKey_h
     33 
     34 #include "wtf/HashMap.h"
     35 #include "wtf/HashTableDeletedValueType.h"
     36 #include "wtf/text/AtomicStringHash.h"
     37 #include "wtf/text/StringHash.h"
     38 
     39 namespace WebCore {
     40 
     41 // Multiplying the floating point size by 100 gives two decimal point
     42 // precision which should be sufficient.
     43 static const unsigned s_fontSizePrecisionMultiplier = 100;
     44 
     45 struct FontCacheKey {
     46     WTF_MAKE_FAST_ALLOCATED;
     47 public:
     48     FontCacheKey()
     49         : m_familyName()
     50         , m_fontSize(0)
     51         , m_options(0) { }
     52     FontCacheKey(AtomicString familyName, float fontSize, unsigned options)
     53         : m_familyName(familyName)
     54         , m_fontSize(fontSize * s_fontSizePrecisionMultiplier)
     55         , m_options(options) { }
     56     FontCacheKey(WTF::HashTableDeletedValueType)
     57         : m_fontSize(hashTableDeletedSize()) { }
     58 
     59     unsigned hash() const
     60     {
     61         unsigned hashCodes[3] = {
     62             CaseFoldingHash::hash(m_familyName),
     63             m_fontSize,
     64             m_options
     65         };
     66         return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
     67     }
     68 
     69     bool operator==(const FontCacheKey& other) const
     70     {
     71         return equalIgnoringCase(m_familyName, other.m_familyName)
     72             && m_fontSize == other.m_fontSize
     73             && m_options == other.m_options;
     74     }
     75 
     76     bool isHashTableDeletedValue() const
     77     {
     78         return m_fontSize == hashTableDeletedSize();
     79     }
     80 
     81     static unsigned precisionMultiplier()
     82     {
     83         return s_fontSizePrecisionMultiplier;
     84     }
     85 
     86 private:
     87     static unsigned hashTableDeletedSize()
     88     {
     89         return 0xFFFFFFFFU;
     90     }
     91 
     92     AtomicString m_familyName;
     93     unsigned m_fontSize;
     94     unsigned m_options;
     95 };
     96 
     97 struct FontCacheKeyHash {
     98     static unsigned hash(const FontCacheKey& key)
     99     {
    100         return key.hash();
    101     }
    102 
    103     static bool equal(const FontCacheKey& a, const FontCacheKey& b)
    104     {
    105         return a == b;
    106     }
    107 
    108     static const bool safeToCompareToEmptyOrDeleted = true;
    109 };
    110 
    111 struct FontCacheKeyTraits : WTF::SimpleClassHashTraits<FontCacheKey> { };
    112 
    113 }
    114 
    115 #endif // FontCacheKey_h
    116