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.
31 #ifndef HarfBuzzShaper_h
32 #define HarfBuzzShaper_h
34 #include "FloatPoint.h"
35 #include "GlyphBuffer.h"
39 #include <wtf/HashSet.h>
40 #include <wtf/Vector.h>
41 #include <wtf/unicode/CharacterNames.h>
48 class HarfBuzzShaper {
51 DoNotNormalizeMirrorChars,
55 HarfBuzzShaper(const FontCascade*, const TextRun&);
56 virtual ~HarfBuzzShaper();
58 bool shape(GlyphBuffer* = 0);
59 FloatPoint adjustStartPoint(const FloatPoint&);
60 float totalWidth() { return m_totalWidth; }
61 int offsetForPosition(float targetX);
62 FloatRect selectionRect(const FloatPoint&, int height, unsigned from, unsigned to);
67 HarfBuzzRun(const Font*, unsigned startIndex, unsigned numCharacters, TextDirection, hb_script_t);
69 void applyShapeResult(hb_buffer_t*);
70 void setGlyphAndPositions(unsigned index, uint16_t glyphId, float advance, float offsetX, float offsetY);
71 void setWidth(float width) { m_width = width; }
73 unsigned characterIndexForXPosition(float targetX);
74 float xPositionForOffset(unsigned offset);
76 const Font* fontData() { return m_fontData; }
77 unsigned startIndex() const { return m_startIndex; }
78 unsigned numCharacters() const { return m_numCharacters; }
79 unsigned numGlyphs() const { return m_numGlyphs; }
80 uint16_t* glyphs() { return &m_glyphs[0]; }
81 float* advances() { return &m_advances[0]; }
82 FloatPoint* offsets() { return &m_offsets[0]; }
83 uint16_t* glyphToCharacterIndexes() { return &m_glyphToCharacterIndexes[0]; }
84 float width() { return m_width; }
85 bool rtl() { return m_direction == RTL; }
86 hb_script_t script() { return m_script; }
89 const Font* m_fontData;
90 unsigned m_startIndex;
91 unsigned m_numCharacters;
93 TextDirection m_direction;
95 Vector<uint16_t, 256> m_glyphs;
96 Vector<float, 256> m_advances;
97 Vector<uint16_t, 256> m_glyphToCharacterIndexes;
98 Vector<FloatPoint, 256> m_offsets;
102 void setNormalizedBuffer(NormalizeMode = DoNotNormalizeMirrorChars);
104 bool isWordEnd(unsigned);
105 int determineWordBreakSpacing();
106 // setPadding sets a number of pixels to be distributed across the TextRun.
107 // WebKit uses this to justify text.
108 void setPadding(int);
110 // In complex text word-spacing affects each line-break, space (U+0020) and non-breaking space (U+00A0).
111 static bool isCodepointSpace(UChar c) { return c == ' ' || c == noBreakSpace || c == '\n'; }
113 void setFontFeatures();
115 bool collectHarfBuzzRuns();
116 bool shapeHarfBuzzRuns(bool shouldSetDirection);
117 bool fillGlyphBuffer(GlyphBuffer*);
118 void fillGlyphBufferFromHarfBuzzRun(GlyphBuffer*, HarfBuzzRun*, FloatPoint& firstOffsetOfNextRun);
119 void setGlyphPositionsForHarfBuzzRun(HarfBuzzRun*, hb_buffer_t*);
121 GlyphBufferAdvance createGlyphBufferAdvance(float, float);
123 const FontCascade* m_font;
124 std::unique_ptr<UChar[]> m_normalizedBuffer;
125 unsigned m_normalizedBufferLength;
126 const TextRun& m_run;
128 int m_wordSpacingAdjustment; // Delta adjustment (pixels) for each word break.
129 float m_padding; // Pixels to be distributed over the line at word breaks.
130 float m_padPerWordBreak; // Pixels to be added to each word break.
131 float m_padError; // m_padPerWordBreak might have a fractional component. Since we only add a whole number of padding pixels at each word break we accumulate error. This is the number of pixels that we are behind so far.
132 int m_letterSpacing; // Pixels to be added after each glyph.
134 Vector<hb_feature_t, 4> m_features;
135 Vector<std::unique_ptr<HarfBuzzRun>, 16> m_harfBuzzRuns;
137 FloatPoint m_startOffset;
142 } // namespace WebCore
144 #endif // HarfBuzzShaper_h