2 * Copyright (c) 2010 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 ComplexTextControllerLinux_h
32 #define ComplexTextControllerLinux_h
34 #include "HarfbuzzSkia.h"
40 #include "harfbuzz-shaper.h"
43 #include <unicode/uchar.h>
44 #include <wtf/OwnArrayPtr.h>
45 #include <wtf/OwnPtr.h>
50 class FontPlatformData;
53 // ComplexTextController walks a TextRun and presents each script run in sequence. A
54 // TextRun is a sequence of code-points with the same embedding level (i.e. they
55 // are all left-to-right or right-to-left). A script run is a subsequence where
56 // all the characters have the same script (e.g. Arabic, Thai etc). Shaping is
57 // only ever done with script runs since the shapers only know how to deal with
60 // Iteration is always in logical (aka reading) order. For RTL text that means
61 // the rightmost part of the text will be first.
63 // Once you have setup the object, call |nextScriptRun| to get the first script
64 // run. This will return false when the iteration is complete. At any time you
65 // can call |reset| to start over again.
66 class ComplexTextController {
68 ComplexTextController(const TextRun&, int startingX, int startingY, unsigned wordSpacing, unsigned letterSpacing, unsigned padding, const Font*);
69 ~ComplexTextController();
71 bool isWordBreak(unsigned);
72 int determineWordBreakSpacing(unsigned);
73 // setPadding sets a number of pixels to be distributed across the TextRun.
74 // WebKit uses this to justify text.
76 void reset(int offset);
77 // Advance to the next script run, returning false when the end of the
78 // TextRun has been reached.
80 float widthOfFullRun();
82 // setWordSpacingAdjustment sets a delta (in pixels) which is applied at
83 // each word break in the TextRun.
84 void setWordSpacingAdjustment(int wordSpacingAdjustment) { m_wordSpacingAdjustment = wordSpacingAdjustment; }
86 // setLetterSpacingAdjustment sets an additional number of pixels that is
87 // added to the advance after each output cluster. This matches the behaviour
88 // of WidthIterator::advance.
89 void setLetterSpacingAdjustment(int letterSpacingAdjustment) { m_letterSpacing = letterSpacingAdjustment; }
90 int letterSpacing() const { return m_letterSpacing; }
93 bool rtl() const { return m_run.rtl(); }
94 const uint16_t* glyphs() const { return m_glyphs16; }
96 // Return the length of the array returned by |glyphs|
97 const unsigned length() const { return m_item.num_glyphs; }
99 // Return the offset for each of the glyphs. Note that this is translated
100 // by the current x offset and that the x offset is updated for each script
102 const SkPoint* positions() const { return m_positions; }
104 // return the number of code points in the current script run
105 const unsigned numCodePoints() const { return m_item.item.length; }
107 // Return the current pixel position of the controller.
108 const unsigned offsetX() const { return m_offsetX; }
110 const FontPlatformData* fontPlatformDataForScriptRun() { return reinterpret_cast<FontPlatformData*>(m_item.font->userData); }
112 int offsetForPosition(int);
113 FloatRect selectionRect(const FloatPoint&, int height, int from , int to);
116 // Return the width (in px) of the current script run.
117 unsigned width() const { return m_pixelWidth; }
119 void setupFontForScriptRun();
120 void deleteGlyphArrays();
121 void createGlyphArrays(int);
122 void resetGlyphArrays();
124 void setGlyphPositions(bool);
126 static void normalizeSpacesAndMirrorChars(const UChar* source, bool rtl, UChar* destination, int length);
127 static const TextRun& getNormalizedTextRun(const TextRun& originalRun, OwnPtr<TextRun>& normalizedRun, OwnArrayPtr<UChar>& normalizedBuffer);
129 // This matches the logic in RenderBlock::findNextLineBreak
130 static bool isCodepointSpace(HB_UChar16 c) { return c == ' ' || c == '\t'; }
132 int glyphIndexForXPositionInScriptRun(int targetX) const;
134 const Font* const m_font;
135 const SimpleFontData* m_currentFontData;
136 HB_ShaperItem m_item;
137 uint16_t* m_glyphs16; // A vector of 16-bit glyph ids.
138 SkPoint* m_positions; // A vector of positions for each glyph.
139 ssize_t m_indexOfNextScriptRun; // Indexes the script run in |m_run|.
140 int m_offsetX; // Offset in pixels to the start of the next script run.
141 int m_startingY; // The Y starting point of the script run.
142 unsigned m_pixelWidth; // Width (in px) of the current script run.
143 unsigned m_glyphsArrayCapacity; // Current size of all the Harfbuzz arrays.
145 OwnPtr<TextRun> m_normalizedRun;
146 OwnArrayPtr<UChar> m_normalizedBuffer; // A buffer for normalized run.
147 const TextRun& m_run;
148 int m_wordSpacingAdjustment; // delta adjustment (pixels) for each word break.
149 float m_padding; // pixels to be distributed over the line at word breaks.
150 float m_padPerWordBreak; // pixels to be added to each word break.
151 float m_padError; // |m_padPerWordBreak| might have a fractional component.
152 // Since we only add a whole number of padding pixels at
153 // each word break we accumulate error. This is the
154 // number of pixels that we are behind so far.
155 int m_letterSpacing; // pixels to be added after each glyph.
156 String m_smallCapsString; // substring of m_run converted to small caps.
159 } // namespace WebCore
161 #endif // ComplexTextControllerLinux_h