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 "FontFaceCreationParams.h"
     35 #include "wtf/HashMap.h"
     36 #include "wtf/HashTableDeletedValueType.h"
     37 #include "wtf/text/AtomicStringHash.h"
     38 #include "wtf/text/StringHash.h"
     39 
     40 namespace blink {
     41 
     42 // Multiplying the floating point size by 100 gives two decimal point
     43 // precision which should be sufficient.
     44 static const unsigned s_fontSizePrecisionMultiplier = 100;
     45 
     46 struct FontCacheKey {
     47     WTF_MAKE_FAST_ALLOCATED;
     48 public:
     49     FontCacheKey()
     50         : m_creationParams()
     51         , m_fontSize(0)
     52         , m_options(0) { }
     53     FontCacheKey(FontFaceCreationParams creationParams, float fontSize, unsigned options)
     54         : m_creationParams(creationParams)
     55         , m_fontSize(fontSize * s_fontSizePrecisionMultiplier)
     56         , m_options(options) { }
     57     FontCacheKey(WTF::HashTableDeletedValueType)
     58         : m_fontSize(hashTableDeletedSize()) { }
     59 
     60     unsigned hash() const
     61     {
     62         unsigned hashCodes[3] = {
     63             m_creationParams.hash(),
     64             m_fontSize,
     65             m_options
     66         };
     67         return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
     68     }
     69 
     70     bool operator==(const FontCacheKey& other) const
     71     {
     72         return m_creationParams == other.m_creationParams
     73             && m_fontSize == other.m_fontSize
     74             && m_options == other.m_options;
     75     }
     76 
     77     bool isHashTableDeletedValue() const
     78     {
     79         return m_fontSize == hashTableDeletedSize();
     80     }
     81 
     82     static unsigned precisionMultiplier()
     83     {
     84         return s_fontSizePrecisionMultiplier;
     85     }
     86 
     87 private:
     88     static unsigned hashTableDeletedSize()
     89     {
     90         return 0xFFFFFFFFU;
     91     }
     92 
     93     FontFaceCreationParams m_creationParams;
     94     unsigned m_fontSize;
     95     unsigned m_options;
     96 };
     97 
     98 struct FontCacheKeyHash {
     99     static unsigned hash(const FontCacheKey& key)
    100     {
    101         return key.hash();
    102     }
    103 
    104     static bool equal(const FontCacheKey& a, const FontCacheKey& b)
    105     {
    106         return a == b;
    107     }
    108 
    109     static const bool safeToCompareToEmptyOrDeleted = true;
    110 };
    111 
    112 struct FontCacheKeyTraits : WTF::SimpleClassHashTraits<FontCacheKey> { };
    113 
    114 } // namespace blink
    115 
    116 #endif // FontCacheKey_h
    117