Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (c) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      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
     13  * distribution.
     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.
     17  *
     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.
     29  */
     30 
     31 #ifndef ComplexTextControllerLinux_h
     32 #define ComplexTextControllerLinux_h
     33 
     34 #include "HarfbuzzSkia.h"
     35 #include "SkScalar.h"
     36 #include "TextRun.h"
     37 
     38 #include <unicode/uchar.h>
     39 #include <wtf/OwnArrayPtr.h>
     40 #include <wtf/OwnPtr.h>
     41 
     42 namespace WebCore {
     43 
     44 class Font;
     45 class FontPlatformData;
     46 class SimpleFontData;
     47 
     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
     53 // a single script.
     54 //
     55 // Iteration is always in logical (aka reading) order.  For RTL text that means
     56 // the rightmost part of the text will be first.
     57 //
     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 {
     62 public:
     63     ComplexTextController(const TextRun&, unsigned, const Font*);
     64     ~ComplexTextController();
     65 
     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.
     70     void setPadding(int);
     71     void reset(unsigned offset);
     72     // Advance to the next script run, returning false when the end of the
     73     // TextRun has been reached.
     74     bool nextScriptRun();
     75     float widthOfFullRun();
     76 
     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; }
     80 
     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; }
     86 
     87     // Set the x offset for the next script run. This affects the values in
     88     // |xPositions|
     89     bool rtl() const { return m_run.rtl(); }
     90     const uint16_t* glyphs() const { return m_glyphs16; }
     91 
     92     // Return the length of the array returned by |glyphs|
     93     const unsigned length() const { return m_item.num_glyphs; }
     94 
     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
     97     // run.
     98     const SkScalar* xPositions() const { return m_xPositions; }
     99 
    100     // Get the advances (widths) for each glyph.
    101     const HB_Fixed* advances() const { return m_item.advances; }
    102 
    103     // Return the width (in px) of the current script run.
    104     const unsigned width() const { return m_pixelWidth; }
    105 
    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
    110     // generated for it.
    111     const unsigned short* logClusters() const { return m_item.log_clusters; }
    112 
    113     // return the number of code points in the current script run
    114     const unsigned numCodePoints() const { return m_item.item.length; }
    115 
    116     // Return the current pixel position of the controller.
    117     const unsigned offsetX() const { return m_offsetX; }
    118 
    119     const FontPlatformData* fontPlatformDataForScriptRun() { return reinterpret_cast<FontPlatformData*>(m_item.font->userData); }
    120 
    121 private:
    122     void setupFontForScriptRun();
    123     HB_FontRec* allocHarfbuzzFont();
    124     void deleteGlyphArrays();
    125     void createGlyphArrays(int);
    126     void resetGlyphArrays();
    127     void shapeGlyphs();
    128     void setGlyphXPositions(bool);
    129 
    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);
    132 
    133     // This matches the logic in RenderBlock::findNextLineBreak
    134     static bool isCodepointSpace(HB_UChar16 c) { return c == ' ' || c == '\t'; }
    135 
    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.
    145 
    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.
    158 };
    159 
    160 } // namespace WebCore
    161 
    162 #endif // ComplexTextControllerLinux_h
    163