2 * Copyright (c) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "platform/fonts/harfbuzz/HarfBuzzFace.h"
34 #include "platform/fonts/FontPlatformData.h"
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');
43 // Though we have FontCache class, which provides the cache mechanism for
44 // WebKit's font objects, we also need additional caching layer for HarfBuzz
45 // to reduce the memory consumption because hb_face_t should be associated with
46 // underling font data (e.g. CTFontRef, FTFace).
48 class FaceCacheEntry : public RefCounted<FaceCacheEntry> {
50 static PassRefPtr<FaceCacheEntry> create(hb_face_t* face)
53 return adoptRef(new FaceCacheEntry(face));
57 hb_face_destroy(m_face);
60 hb_face_t* face() { return m_face; }
61 HashMap<uint32_t, uint16_t>* glyphCache() { return &m_glyphCache; }
64 explicit FaceCacheEntry(hb_face_t* face)
69 HashMap<uint32_t, uint16_t> m_glyphCache;
72 typedef HashMap<uint64_t, RefPtr<FaceCacheEntry>, WTF::IntHash<uint64_t>, WTF::UnsignedWithZeroKeyHashTraits<uint64_t> > HarfBuzzFaceCache;
74 static HarfBuzzFaceCache* harfBuzzFaceCache()
76 DEFINE_STATIC_LOCAL(HarfBuzzFaceCache, s_harfBuzzFaceCache, ());
77 return &s_harfBuzzFaceCache;
80 HarfBuzzFace::HarfBuzzFace(FontPlatformData* platformData, uint64_t uniqueID)
81 : m_platformData(platformData)
82 , m_uniqueID(uniqueID)
83 , m_scriptForVerticalText(HB_SCRIPT_INVALID)
85 HarfBuzzFaceCache::AddResult result = harfBuzzFaceCache()->add(m_uniqueID, 0);
86 if (result.isNewEntry)
87 result.iterator->value = FaceCacheEntry::create(createFace());
88 result.iterator->value->ref();
89 m_face = result.iterator->value->face();
90 m_glyphCacheForFaceCacheEntry = result.iterator->value->glyphCache();
93 HarfBuzzFace::~HarfBuzzFace()
95 HarfBuzzFaceCache::iterator result = harfBuzzFaceCache()->find(m_uniqueID);
96 ASSERT_WITH_SECURITY_IMPLICATION(result != harfBuzzFaceCache()->end());
97 ASSERT(result.get()->value->refCount() > 1);
98 result.get()->value->deref();
99 if (result.get()->value->refCount() == 1)
100 harfBuzzFaceCache()->remove(m_uniqueID);
103 static hb_script_t findScriptForVerticalGlyphSubstitution(hb_face_t* face)
105 static const unsigned maxCount = 32;
107 unsigned scriptCount = maxCount;
108 hb_tag_t scriptTags[maxCount];
109 hb_ot_layout_table_get_script_tags(face, HB_OT_TAG_GSUB, 0, &scriptCount, scriptTags);
110 for (unsigned scriptIndex = 0; scriptIndex < scriptCount; ++scriptIndex) {
111 unsigned languageCount = maxCount;
112 hb_tag_t languageTags[maxCount];
113 hb_ot_layout_script_get_language_tags(face, HB_OT_TAG_GSUB, scriptIndex, 0, &languageCount, languageTags);
114 for (unsigned languageIndex = 0; languageIndex < languageCount; ++languageIndex) {
115 unsigned featureIndex;
116 if (hb_ot_layout_language_find_feature(face, HB_OT_TAG_GSUB, scriptIndex, languageIndex, HarfBuzzFace::vertTag, &featureIndex)
117 || hb_ot_layout_language_find_feature(face, HB_OT_TAG_GSUB, scriptIndex, languageIndex, HarfBuzzFace::vrt2Tag, &featureIndex))
118 return hb_ot_tag_to_script(scriptTags[scriptIndex]);
121 return HB_SCRIPT_INVALID;
124 void HarfBuzzFace::setScriptForVerticalGlyphSubstitution(hb_buffer_t* buffer)
126 if (m_scriptForVerticalText == HB_SCRIPT_INVALID)
127 m_scriptForVerticalText = findScriptForVerticalGlyphSubstitution(m_face);
128 hb_buffer_set_script(buffer, m_scriptForVerticalText);
131 } // namespace WebCore