Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: Source/platform/fonts/GlyphBuffer.h

Issue 676523003: Offset-only GlyphBuffer (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: minor cleanup Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2009, 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2009, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2007-2008 Torch Mobile Inc. 3 * Copyright (C) 2007-2008 Torch Mobile Inc.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 13 matching lines...) Expand all
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 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 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 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. 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30 #ifndef GlyphBuffer_h 30 #ifndef GlyphBuffer_h
31 #define GlyphBuffer_h 31 #define GlyphBuffer_h
32 32
33 #include "platform/fonts/Glyph.h" 33 #include "platform/fonts/Glyph.h"
34 #include "platform/geometry/FloatSize.h" 34 #include "platform/geometry/FloatPoint.h"
35 #include "platform/heap/Heap.h" 35 #include "platform/heap/Heap.h"
36 #include "wtf/Vector.h" 36 #include "wtf/Vector.h"
37 37
38 namespace blink { 38 namespace blink {
39 39
40 class SimpleFontData; 40 class SimpleFontData;
41 41
42 class GlyphBuffer { 42 class GlyphBuffer {
43 STACK_ALLOCATED(); 43 STACK_ALLOCATED();
44 public: 44 public:
45 bool isEmpty() const { return m_fontData.isEmpty(); } 45 bool isEmpty() const { return m_fontData.isEmpty(); }
46 bool hasOffsets() const { return !m_offsets.isEmpty(); } 46 unsigned size() const
47 unsigned size() const { return m_fontData.size(); } 47 {
48 ASSERT(m_fontData.size() == m_glyphs.size());
49 ASSERT(m_fontData.size() == m_offsets.size() || 2 * m_fontData.size() == m_offsets.size());
50 return m_fontData.size();
51 }
48 52
49 const Glyph* glyphs(unsigned from) const { return m_glyphs.data() + from; } 53 bool hasVerticalOffsets() const
50 const float* advances(unsigned from) const { return m_advances.data() + from ; } 54 {
51 const FloatSize* offsets(unsigned from) const { return m_offsets.data() + fr om; } 55 return size() != m_offsets.size();
jbroman 2014/10/29 15:06:07 Any reason not to just use a bool to explicitly tr
f(malita) 2014/10/29 16:48:29 add() is pretty hot if I'm not mistaken, and optim
jbroman 2014/10/29 19:05:36 Hmm, okay. I suspect the expense is relatively min
56 }
57
58 const Glyph* glyphs(unsigned from) const
59 {
60 ASSERT(from < size());
61 return m_glyphs.data() + from;
62 }
63
64 const float* offsets(unsigned from) const
65 {
66 ASSERT(from < size());
67 return m_offsets.data() + (hasVerticalOffsets() ? from * 2 : from);
68 }
52 69
53 const SimpleFontData* fontDataAt(unsigned index) const 70 const SimpleFontData* fontDataAt(unsigned index) const
54 { 71 {
72 ASSERT(index < size());
jbroman 2014/10/29 15:06:07 Do these assertions slow down debug builds much?
f(malita) 2014/10/29 16:48:29 Nothing noticeable. A couple of local fast/text l
jbroman 2014/10/29 19:05:36 Acknowledged.
55 return m_fontData[index]; 73 return m_fontData[index];
56 } 74 }
57 75
58 Glyph glyphAt(unsigned index) const 76 Glyph glyphAt(unsigned index) const
59 { 77 {
78 ASSERT(index < size());
60 return m_glyphs[index]; 79 return m_glyphs[index];
61 } 80 }
62 81
63 float advanceAt(unsigned index) const 82 float xOffsetAt(unsigned index) const
64 { 83 {
65 return m_advances[index]; 84 ASSERT(index < size());
85 return hasVerticalOffsets() ? m_offsets[index * 2] : m_offsets[index];
86
66 } 87 }
67 88
68 void add(Glyph glyph, const SimpleFontData* font, float width) 89 float yOffsetAt(unsigned index) const
69 { 90 {
70 // should not mix offset/advance-only glyphs 91 ASSERT(index < size());
71 ASSERT(!hasOffsets()); 92 ASSERT(hasVerticalOffsets());
93 return m_offsets[index * 2 + 1];
94 }
95
96 void add(Glyph glyph, const SimpleFontData* font, float x)
97 {
98 // cannot mix x-only/xy offsets
99 ASSERT(!hasVerticalOffsets());
72 100
73 m_fontData.append(font); 101 m_fontData.append(font);
74 m_glyphs.append(glyph); 102 m_glyphs.append(glyph);
75 m_advances.append(width); 103 m_offsets.append(x);
76 } 104 }
77 105
78 void add(Glyph glyph, const SimpleFontData* font, const FloatSize& offset, f loat advance) 106 void add(Glyph glyph, const SimpleFontData* font, const FloatPoint& offset)
79 { 107 {
80 // should not mix offset/advance-only glyphs 108 // cannot mix x-only/xy offsets
81 ASSERT(size() == m_offsets.size()); 109 ASSERT(isEmpty() || hasVerticalOffsets());
82 110
83 m_fontData.append(font); 111 m_fontData.append(font);
84 m_glyphs.append(glyph); 112 m_glyphs.append(glyph);
85 m_offsets.append(offset); 113 m_offsets.append(offset.x());
86 m_advances.append(advance); 114 m_offsets.append(offset.y());
87 } 115 }
88 116
89 void reverse() 117 void reverse(float afterOffset, float totalWidth)
90 { 118 {
119 ASSERT(!hasVerticalOffsets());
120
91 m_fontData.reverse(); 121 m_fontData.reverse();
92 m_glyphs.reverse(); 122 m_glyphs.reverse();
93 m_advances.reverse();
94 }
95 123
96 void expandLastAdvance(float width) 124 // | .. [X0 X1 .. Xn] .. |
97 { 125 // ^ ^ ^
98 ASSERT(!isEmpty()); 126 // 0 afterOffset totalWidth
jbroman 2014/10/29 15:06:07 This diagram is helpful, but I could use some word
f(malita) 2014/10/29 16:48:29 Done.
99 float& lastAdvance = m_advances.last(); 127 ASSERT_WITH_SECURITY_IMPLICATION(!m_offsets.isEmpty());
jbroman 2014/10/29 15:06:07 Why is it illegal to reverse an empty glyph buffer
f(malita) 2014/10/29 16:48:29 Ah, good catch: I assumed we check for an empty bu
100 lastAdvance += width; 128 float x = totalWidth - m_offsets[0];
129 for (unsigned i = 0; i + 1 < m_offsets.size(); ++i) {
130 float advance = m_offsets[i + 1] - m_offsets[i];
131 ASSERT(advance >= 0);
132 x -= advance;
133 m_offsets[i] = x;
134 }
135 float lastAdvance = afterOffset - m_offsets.last();
136 ASSERT(lastAdvance >= 0);
137 m_offsets.last() = x - lastAdvance;
138
139 m_offsets.reverse();
101 } 140 }
102 141
103 protected: 142 protected:
104 Vector<const SimpleFontData*, 2048> m_fontData; 143 Vector<const SimpleFontData*, 2048> m_fontData;
105 Vector<Glyph, 2048> m_glyphs; 144 Vector<Glyph, 2048> m_glyphs;
106 Vector<float, 2048> m_advances; 145 Vector<float, 2048> m_offsets;
107 Vector<FloatSize, 1024> m_offsets;
108 }; 146 };
109 147
110 } // namespace blink 148 } // namespace blink
111 149
112 #endif 150 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698