1 | /* |
2 | * Copyright (C) 2006, 2007, 2008, 2013 Apple Inc. All rights reserved. |
3 | * Copyright (C) Research In Motion Limited 2011. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
15 | * its contributors may be used to endorse or promote products derived |
16 | * from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #ifndef GlyphPage_h |
31 | #define GlyphPage_h |
32 | |
33 | #include "Glyph.h" |
34 | #include <unicode/utypes.h> |
35 | #include <wtf/RefCounted.h> |
36 | #include <wtf/Ref.h> |
37 | |
38 | namespace WebCore { |
39 | |
40 | class Font; |
41 | |
42 | // Holds the glyph index and the corresponding Font information for a given |
43 | // character. |
44 | struct GlyphData { |
45 | GlyphData(Glyph glyph = 0, const Font* font = nullptr) |
46 | : glyph(glyph) |
47 | , font(font) |
48 | { |
49 | } |
50 | |
51 | bool isValid() const { return glyph || font; } |
52 | |
53 | Glyph glyph; |
54 | const Font* font; |
55 | }; |
56 | |
57 | // A GlyphPage contains a fixed-size set of GlyphData mappings for a contiguous |
58 | // range of characters in the Unicode code space. GlyphPages are indexed |
59 | // starting from 0 and incrementing for each "size" number of glyphs. |
60 | class GlyphPage : public RefCounted<GlyphPage> { |
61 | public: |
62 | static Ref<GlyphPage> create(const Font& font) |
63 | { |
64 | return adoptRef(*new GlyphPage(font)); |
65 | } |
66 | |
67 | ~GlyphPage() |
68 | { |
69 | --s_count; |
70 | } |
71 | |
72 | static unsigned count() { return s_count; } |
73 | |
74 | static const unsigned size = 16; |
75 | |
76 | static unsigned sizeForPageNumber(unsigned) { return 16; } |
77 | static unsigned indexForCodePoint(UChar32 c) { return c % size; } |
78 | static unsigned pageNumberForCodePoint(UChar32 c) { return c / size; } |
79 | static UChar32 startingCodePointInPageNumber(unsigned pageNumber) { return pageNumber * size; } |
80 | static bool pageNumberIsUsedForArabic(unsigned pageNumber) { return startingCodePointInPageNumber(pageNumber) >= 0x600 && startingCodePointInPageNumber(pageNumber) + sizeForPageNumber(pageNumber) < 0x700; } |
81 | |
82 | GlyphData glyphDataForCharacter(UChar32 c) const |
83 | { |
84 | return glyphDataForIndex(indexForCodePoint(c)); |
85 | } |
86 | |
87 | Glyph glyphForCharacter(UChar32 c) const |
88 | { |
89 | return glyphForIndex(indexForCodePoint(c)); |
90 | } |
91 | |
92 | GlyphData glyphDataForIndex(unsigned index) const |
93 | { |
94 | Glyph glyph = glyphForIndex(index); |
95 | return GlyphData(glyph, glyph ? &m_font : nullptr); |
96 | } |
97 | |
98 | Glyph glyphForIndex(unsigned index) const |
99 | { |
100 | ASSERT_WITH_SECURITY_IMPLICATION(index < size); |
101 | return m_glyphs[index]; |
102 | } |
103 | |
104 | // FIXME: Pages are immutable after initialization. This should be private. |
105 | void setGlyphForIndex(unsigned index, Glyph glyph) |
106 | { |
107 | ASSERT_WITH_SECURITY_IMPLICATION(index < size); |
108 | m_glyphs[index] = glyph; |
109 | } |
110 | |
111 | const Font& font() const |
112 | { |
113 | return m_font; |
114 | } |
115 | |
116 | // Implemented by the platform. |
117 | bool fill(UChar* characterBuffer, unsigned bufferLength); |
118 | |
119 | private: |
120 | explicit GlyphPage(const Font& font) |
121 | : m_font(font) |
122 | { |
123 | ++s_count; |
124 | } |
125 | |
126 | const Font& m_font; |
127 | Glyph m_glyphs[size] { }; |
128 | |
129 | WEBCORE_EXPORT static unsigned s_count; |
130 | }; |
131 | |
132 | } // namespace WebCore |
133 | |
134 | #endif // GlyphPage_h |
135 | |