Home | History | Annotate | Download | only in fonts
      1 /*
      2  * Copyright (c) 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 #ifndef FontFaceCreationParams_h
     32 #define FontFaceCreationParams_h
     33 
     34 #include "wtf/Assertions.h"
     35 #include "wtf/StringHasher.h"
     36 #include "wtf/text/AtomicString.h"
     37 #include "wtf/text/StringHash.h"
     38 
     39 namespace blink {
     40 
     41 enum FontFaceCreationType {
     42     CreateFontByFamily,
     43     CreateFontByFciIdAndTtcIndex
     44 };
     45 
     46 class FontFaceCreationParams {
     47     FontFaceCreationType m_creationType;
     48     AtomicString m_family;
     49     CString m_filename;
     50     int m_fontconfigInterfaceId;
     51     int m_ttcIndex;
     52 
     53 public:
     54     FontFaceCreationParams()
     55         : m_creationType(CreateFontByFamily), m_family(AtomicString()), m_filename(CString()), m_fontconfigInterfaceId(0), m_ttcIndex(0)
     56     {
     57     }
     58 
     59     explicit FontFaceCreationParams(AtomicString family)
     60         : m_creationType(CreateFontByFamily), m_family(family), m_filename(CString()), m_fontconfigInterfaceId(0), m_ttcIndex(0)
     61     {
     62 #if OS(WIN) && ENABLE(OPENTYPE_VERTICAL)
     63     // Leading "@" in the font name enables Windows vertical flow flag for the font.
     64     // Because we do vertical flow by ourselves, we don't want to use the Windows feature.
     65     // IE disregards "@" regardless of the orientation, so we follow the behavior and
     66     // normalize the family name.
     67     m_family = (m_family.isEmpty() || m_family[0] != '@') ? m_family : AtomicString(m_family.impl()->substring(1));
     68 #endif
     69     }
     70 
     71     FontFaceCreationParams(CString filename, int fontconfigInterfaceId, int ttcIndex = 0)
     72         : m_creationType(CreateFontByFciIdAndTtcIndex), m_filename(filename), m_fontconfigInterfaceId(fontconfigInterfaceId), m_ttcIndex(ttcIndex)
     73     {
     74     }
     75 
     76     FontFaceCreationType creationType() const { return m_creationType; }
     77     AtomicString family() const
     78     {
     79         ASSERT(m_creationType == CreateFontByFamily);
     80         return m_family;
     81     }
     82     CString filename() const
     83     {
     84         ASSERT(m_creationType == CreateFontByFciIdAndTtcIndex);
     85         return m_filename;
     86     }
     87     int fontconfigInterfaceId() const
     88     {
     89         ASSERT(m_creationType == CreateFontByFciIdAndTtcIndex);
     90         return m_fontconfigInterfaceId;
     91     }
     92     int ttcIndex() const
     93     {
     94         ASSERT(m_creationType == CreateFontByFciIdAndTtcIndex);
     95         return m_ttcIndex;
     96     }
     97 
     98     unsigned hash() const
     99     {
    100         if (m_creationType == CreateFontByFciIdAndTtcIndex) {
    101             StringHasher hasher;
    102             // Hashing the filename and ints in this way is sensitive to character encoding
    103             // and endianness. However, since the hash is not transferred over a network
    104             // or permanently stored and only used for the runtime of Chromium,
    105             // this is not a concern.
    106             hasher.addCharacters(reinterpret_cast<const LChar*>(m_filename.data()), m_filename.length());
    107             hasher.addCharacters(reinterpret_cast<const LChar*>(&m_ttcIndex), sizeof(m_ttcIndex));
    108             hasher.addCharacters(reinterpret_cast<const LChar*>(&m_fontconfigInterfaceId), sizeof(m_fontconfigInterfaceId));
    109             return hasher.hash();
    110         }
    111         return CaseFoldingHash::hash(m_family.isEmpty() ? "" : m_family);
    112     }
    113 
    114     bool operator==(const FontFaceCreationParams& other) const
    115     {
    116         return m_creationType == other.m_creationType
    117             && equalIgnoringCase(m_family, other.m_family)
    118             && m_filename == other.m_filename
    119             && m_fontconfigInterfaceId == other.m_fontconfigInterfaceId
    120             && m_ttcIndex == other.m_ttcIndex;
    121     }
    122 
    123 };
    124 
    125 } // namespace blink
    126 
    127 #endif // FontFaceCreationParams_h
    128