f03cace8ed9bed82b38cc3819d33bff28a5b183e
[WebKit-https.git] / Source / WebCore / platform / graphics / harfbuzz / HarfBuzzFace.cpp
1 /*
2  * Copyright (c) 2012 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 "HarfBuzzFace.h"
33
34 #include "FontPlatformData.h"
35 #include <hb-ot.h>
36 #include <hb.h>
37
38 namespace WebCore {
39
40 const hb_tag_t HarfBuzzFace::vertTag = HB_TAG('v', 'e', 'r', 't');
41 const hb_tag_t HarfBuzzFace::vrt2Tag = HB_TAG('v', 'r', 't', '2');
42 const hb_tag_t HarfBuzzFace::kernTag = HB_TAG('k', 'e', 'r', 'n');
43
44 // Though we have FontCache class, which provides the cache mechanism for
45 // WebKit's font objects, we also need additional caching layer for HarfBuzz
46 // to reduce the memory consumption because hb_face_t should be associated with
47 // underling font data (e.g. CTFontRef, FTFace).
48
49 class FaceCacheEntry : public RefCounted<FaceCacheEntry> {
50 public:
51     static PassRefPtr<FaceCacheEntry> create(hb_face_t* face)
52     {
53         ASSERT(face);
54         return adoptRef(new FaceCacheEntry(face));
55     }
56     ~FaceCacheEntry()
57     {
58         hb_face_destroy(m_face);
59     }
60
61     hb_face_t* face() { return m_face; }
62     HashMap<uint32_t, uint16_t>* glyphCache() { return &m_glyphCache; }
63
64 private:
65     explicit FaceCacheEntry(hb_face_t* face)
66         : m_face(face)
67     { }
68
69     hb_face_t* m_face;
70     HashMap<uint32_t, uint16_t> m_glyphCache;
71 };
72
73 typedef HashMap<uint64_t, RefPtr<FaceCacheEntry>, WTF::IntHash<uint64_t>, WTF::UnsignedWithZeroKeyHashTraits<uint64_t> > HarfBuzzFaceCache;
74
75 static HarfBuzzFaceCache* harfBuzzFaceCache()
76 {
77     DEPRECATED_DEFINE_STATIC_LOCAL(HarfBuzzFaceCache, s_harfBuzzFaceCache, ());
78     return &s_harfBuzzFaceCache;
79 }
80
81 HarfBuzzFace::HarfBuzzFace(FontPlatformData* platformData, uint64_t uniqueID)
82     : m_platformData(platformData)
83     , m_uniqueID(uniqueID)
84     , m_scriptForVerticalText(HB_SCRIPT_INVALID)
85 {
86     HarfBuzzFaceCache::AddResult result = harfBuzzFaceCache()->add(m_uniqueID, nullptr);
87     if (result.isNewEntry)
88         result.iterator->value = FaceCacheEntry::create(createFace());
89     result.iterator->value->ref();
90     m_face = result.iterator->value->face();
91     m_glyphCacheForFaceCacheEntry = result.iterator->value->glyphCache();
92 }
93
94 HarfBuzzFace::~HarfBuzzFace()
95 {
96     HarfBuzzFaceCache::iterator result = harfBuzzFaceCache()->find(m_uniqueID);
97     ASSERT(result != harfBuzzFaceCache()->end());
98     ASSERT(result.get()->value->refCount() > 1);
99     result.get()->value->deref();
100     if (result.get()->value->refCount() == 1)
101         harfBuzzFaceCache()->remove(m_uniqueID);
102 }
103
104 static hb_script_t findScriptForVerticalGlyphSubstitution(hb_face_t* face)
105 {
106     static const unsigned maxCount = 32;
107
108     unsigned scriptCount = maxCount;
109     hb_tag_t scriptTags[maxCount];
110     hb_ot_layout_table_get_script_tags(face, HB_OT_TAG_GSUB, 0, &scriptCount, scriptTags);
111     for (unsigned scriptIndex = 0; scriptIndex < scriptCount; ++scriptIndex) {
112         unsigned languageCount = maxCount;
113         hb_tag_t languageTags[maxCount];
114         hb_ot_layout_script_get_language_tags(face, HB_OT_TAG_GSUB, scriptIndex, 0, &languageCount, languageTags);
115         for (unsigned languageIndex = 0; languageIndex < languageCount; ++languageIndex) {
116             unsigned featureIndex;
117             if (hb_ot_layout_language_find_feature(face, HB_OT_TAG_GSUB, scriptIndex, languageIndex, HarfBuzzFace::vertTag, &featureIndex)
118                 || hb_ot_layout_language_find_feature(face, HB_OT_TAG_GSUB, scriptIndex, languageIndex, HarfBuzzFace::vrt2Tag, &featureIndex))
119                 return hb_ot_tag_to_script(scriptTags[scriptIndex]);
120         }
121     }
122     return HB_SCRIPT_INVALID;
123 }
124
125 void HarfBuzzFace::setScriptForVerticalGlyphSubstitution(hb_buffer_t* buffer)
126 {
127     if (m_scriptForVerticalText == HB_SCRIPT_INVALID)
128         m_scriptForVerticalText = findScriptForVerticalGlyphSubstitution(m_face);
129     hb_buffer_set_script(buffer, m_scriptForVerticalText);
130 }
131
132 } // namespace WebCore