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"
38 #include <unicode/uchar.h>
39 #include <wtf/OwnArrayPtr.h>
40 #include <wtf/OwnPtr.h>
45 class FontPlatformData;
48 // ComplexTextController walks a TextRun and presents each script run in sequence. A
49 // TextRun is a sequence of code-points with the same embedding level (i.e. they
50 // are all left-to-right or right-to-left). A script run is a subsequence where
51 // all the characters have the same script (e.g. Arabic, Thai etc). Shaping is
52 // only ever done with script runs since the shapers only know how to deal with
55 // Iteration is always in logical (aka reading) order. For RTL text that means
56 // the rightmost part of the text will be first.
58 // Once you have setup the object, call |nextScriptRun| to get the first script
59 // run. This will return false when the iteration is complete. At any time you
60 // can call |reset| to start over again.
61 class ComplexTextController {
63 ComplexTextController(const TextRun&, unsigned, const Font*);
64 ~ComplexTextController();
66 bool isWordBreak(unsigned);
67 int determineWordBreakSpacing(unsigned);
68 // setPadding sets a number of pixels to be distributed across the TextRun.
69 // WebKit uses this to justify text.
71 void reset(unsigned offset);
72 // Advance to the next script run, returning false when the end of the
73 // TextRun has been reached.
75 float widthOfFullRun();
77 // setWordSpacingAdjustment sets a delta (in pixels) which is applied at
78 // each word break in the TextRun.
79 void setWordSpacingAdjustment(int wordSpacingAdjustment) { m_wordSpacingAdjustment = wordSpacingAdjustment; }
81 // setLetterSpacingAdjustment sets an additional number of pixels that is
82 // added to the advance after each output cluster. This matches the behaviour
83 // of WidthIterator::advance.
84 void setLetterSpacingAdjustment(int letterSpacingAdjustment) { m_letterSpacing = letterSpacingAdjustment; }
85 int letterSpacing() const { return m_letterSpacing; }
87 // Set the x offset for the next script run. This affects the values in
89 bool rtl() const { return m_run.rtl(); }
90 const uint16_t* glyphs() const { return m_glyphs16; }
92 // Return the length of the array returned by |glyphs|
93 const unsigned length() const { return m_item.num_glyphs; }
95 // Return the x offset for each of the glyphs. Note that this is translated
96 // by the current x offset and that the x offset is updated for each script
98 const SkScalar* xPositions() const { return m_xPositions; }
100 // Get the advances (widths) for each glyph.
101 const HB_Fixed* advances() const { return m_item.advances; }
103 // Return the width (in px) of the current script run.
104 const unsigned width() const { return m_pixelWidth; }
106 // Return the cluster log for the current script run. For example:
107 // script run: f i a n c é (fi gets ligatured)
108 // log clutrs: 0 0 1 2 3 4
109 // So, for each input code point, the log tells you which output glyph was
111 const unsigned short* logClusters() const { return m_item.log_clusters; }
113 // return the number of code points in the current script run
114 const unsigned numCodePoints() const { return m_item.item.length; }
116 // Return the current pixel position of the controller.
117 const unsigned offsetX() const { return m_offsetX; }
119 const FontPlatformData* fontPlatformDataForScriptRun() { return reinterpret_cast<FontPlatformData*>(m_item.font->userData); }
122 void setupFontForScriptRun();
123 HB_FontRec* allocHarfbuzzFont();
124 void deleteGlyphArrays();
125 void createGlyphArrays(int);
126 void resetGlyphArrays();
128 void setGlyphXPositions(bool);
130 static void normalizeSpacesAndMirrorChars(const UChar* source, bool rtl, UChar* destination, int length);
131 static const TextRun& getNormalizedTextRun(const TextRun& originalRun, OwnPtr<TextRun>& normalizedRun, OwnArrayPtr<UChar>& normalizedBuffer);
133 // This matches the logic in RenderBlock::findNextLineBreak
134 static bool isCodepointSpace(HB_UChar16 c) { return c == ' ' || c == '\t'; }
136 const Font* const m_font;
137 const SimpleFontData* m_currentFontData;
138 HB_ShaperItem m_item;
139 uint16_t* m_glyphs16; // A vector of 16-bit glyph ids.
140 SkScalar* m_xPositions; // A vector of x positions for each glyph.
141 ssize_t m_indexOfNextScriptRun; // Indexes the script run in |m_run|.
142 unsigned m_offsetX; // Offset in pixels to the start of the next script run.
143 unsigned m_pixelWidth; // Width (in px) of the current script run.
144 unsigned m_glyphsArrayCapacity; // Current size of all the Harfbuzz arrays.
146 OwnPtr<TextRun> m_normalizedRun;
147 OwnArrayPtr<UChar> m_normalizedBuffer; // A buffer for normalized run.
148 const TextRun& m_run;
149 int m_wordSpacingAdjustment; // delta adjustment (pixels) for each word break.
150 float m_padding; // pixels to be distributed over the line at word breaks.
151 float m_padPerWordBreak; // pixels to be added to each word break.
152 float m_padError; // |m_padPerWordBreak| might have a fractional component.
153 // Since we only add a whole number of padding pixels at
154 // each word break we accumulate error. This is the
155 // number of pixels that we are behind so far.
156 int m_letterSpacing; // pixels to be added after each glyph.
157 String m_smallCapsString; // substring of m_run converted to small caps.
160 } // namespace WebCore
162 #endif // ComplexTextControllerLinux_h