2 * Copyright (C) 2016 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef CSSFontFaceSet_h
27 #define CSSFontFaceSet_h
29 #include "CSSFontFace.h"
30 #include <wtf/HashMap.h>
31 #include <wtf/Vector.h>
32 #include <wtf/text/StringHash.h>
36 class CSSPrimitiveValue;
39 class CSSFontFaceSetClient {
41 virtual ~CSSFontFaceSetClient() { }
42 virtual void faceFinished(CSSFontFace&, CSSFontFace::Status) { };
43 virtual void fontModified() { };
44 virtual void startedLoading() { };
45 virtual void completedLoading() { };
48 class CSSFontFaceSet final : public RefCounted<CSSFontFaceSet>, public CSSFontFace::Client {
50 static Ref<CSSFontFaceSet> create()
52 return adoptRef(*new CSSFontFaceSet());
56 void addClient(CSSFontFaceSetClient&);
57 void removeClient(CSSFontFaceSetClient&);
59 bool hasFace(const CSSFontFace&) const;
60 size_t faceCount() const { return m_faces.size(); }
61 void add(CSSFontFace&);
62 void remove(const CSSFontFace&);
64 CSSFontFace& operator[](size_t i);
66 bool check(const String& font, const String& text, ExceptionCode&);
68 CSSSegmentedFontFace* getFontFace(FontTraitsMask, const AtomicString& family);
74 Status status() const { return m_status; }
76 Vector<std::reference_wrapper<CSSFontFace>> matchingFaces(const String& font, const String& text, ExceptionCode&);
78 // CSSFontFace::Client needs to be able to be held in a RefPtr.
79 void ref() override { RefCounted<CSSFontFaceSet>::ref(); }
80 void deref() override { RefCounted<CSSFontFaceSet>::deref(); }
85 void removeFromFacesLookupTable(const CSSFontFace&, const CSSValueList& familiesToSearchFor);
86 void addToFacesLookupTable(CSSFontFace&);
88 void incrementActiveCount();
89 void decrementActiveCount();
91 void fontStateChanged(CSSFontFace&, CSSFontFace::Status oldState, CSSFontFace::Status newState) override;
92 void fontPropertyChanged(CSSFontFace&, CSSValueList* oldFamilies = nullptr) override;
94 void registerLocalFontFacesForFamily(const String&);
96 static String familyNameFromPrimitive(const CSSPrimitiveValue&);
98 // m_faces should hold all the same fonts as the ones inside inside m_facesLookupTable.
99 Vector<Ref<CSSFontFace>> m_faces; // We should investigate moving m_faces to FontFaceSet and making it reference FontFaces. This may clean up the font loading design.
100 HashMap<String, Vector<Ref<CSSFontFace>>, ASCIICaseInsensitiveHash> m_facesLookupTable;
101 HashMap<String, Vector<Ref<CSSFontFace>>, ASCIICaseInsensitiveHash> m_locallyInstalledFacesLookupTable;
102 HashMap<String, HashMap<unsigned, RefPtr<CSSSegmentedFontFace>>, ASCIICaseInsensitiveHash> m_cache;
103 size_t m_facesPartitionIndex { 0 }; // All entries in m_faces before this index are CSS-connected.
104 Status m_status { Status::Loaded };
105 HashSet<CSSFontFaceSetClient*> m_clients;
106 unsigned m_activeCount { 0 };