Home | History | Annotate | Download | only in fonts
      1 /*
      2  * Copyright (C) 2003, 2006, 2008, 2011 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Holger Hans Peter Freyther
      4  * Copyright (C) 2014 Google Inc. All rights reserved.
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Library General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Library General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Library General Public License
     17  * along with this library; see the file COPYING.LIB.  If not, write to
     18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  * Boston, MA 02110-1301, USA.
     20  *
     21  */
     22 
     23 #ifndef WidthIterator_h
     24 #define WidthIterator_h
     25 
     26 #include "platform/PlatformExport.h"
     27 #include "platform/fonts/SVGGlyph.h"
     28 #include "platform/text/TextRun.h"
     29 #include "wtf/HashSet.h"
     30 #include "wtf/unicode/Unicode.h"
     31 #include "wtf/Vector.h"
     32 
     33 namespace blink {
     34 
     35 class Font;
     36 class GlyphBuffer;
     37 class SimpleFontData;
     38 class TextRun;
     39 struct GlyphData;
     40 
     41 struct PLATFORM_EXPORT WidthIterator {
     42     WTF_MAKE_FAST_ALLOCATED;
     43 public:
     44     WidthIterator(const Font*, const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, bool accountForGlyphBounds = false, bool forTextEmphasis = false);
     45 
     46     unsigned advance(int to, GlyphBuffer* = 0);
     47     bool advanceOneCharacter(float& width);
     48 
     49     float maxGlyphBoundingBoxY() const { ASSERT(m_accountForGlyphBounds); return m_maxGlyphBoundingBoxY; }
     50     float minGlyphBoundingBoxY() const { ASSERT(m_accountForGlyphBounds); return m_minGlyphBoundingBoxY; }
     51     float firstGlyphOverflow() const { ASSERT(m_accountForGlyphBounds); return m_firstGlyphOverflow; }
     52     float lastGlyphOverflow() const { ASSERT(m_accountForGlyphBounds); return m_lastGlyphOverflow; }
     53 
     54     const TextRun& run() const { return m_run; }
     55     float runWidthSoFar() const { return m_runWidthSoFar; }
     56 
     57 #if ENABLE(SVG_FONTS)
     58     Vector<SVGGlyph::ArabicForm>& arabicForms() { return m_arabicForms; }
     59 #endif
     60 
     61     const Font* m_font;
     62 
     63     const TextRun& m_run;
     64 
     65     unsigned m_currentCharacter;
     66     float m_runWidthSoFar;
     67     float m_expansion;
     68     float m_expansionPerOpportunity;
     69     bool m_isAfterExpansion;
     70 
     71 #if ENABLE(SVG_FONTS)
     72     Vector<SVGGlyph::ArabicForm> m_arabicForms;
     73 #endif
     74 
     75 private:
     76     struct CharacterData {
     77         UChar32 character;
     78         unsigned clusterLength;
     79         int characterOffset;
     80     };
     81 
     82     GlyphData glyphDataForCharacter(CharacterData&, bool normalizeSpace = false);
     83     float characterWidth(UChar32, const GlyphData&) const;
     84     void cacheFallbackFont(const SimpleFontData*, const SimpleFontData* primaryFont);
     85     float adjustSpacing(float, const CharacterData&, const SimpleFontData&, GlyphBuffer*);
     86     void updateGlyphBounds(const GlyphData&, float width, bool firstCharacter);
     87 
     88     template <typename TextIterator>
     89     unsigned advanceInternal(TextIterator&, GlyphBuffer*);
     90 
     91     HashSet<const SimpleFontData*>* m_fallbackFonts;
     92     float m_maxGlyphBoundingBoxY;
     93     float m_minGlyphBoundingBoxY;
     94     float m_firstGlyphOverflow;
     95     float m_lastGlyphOverflow;
     96 
     97     bool m_accountForGlyphBounds : 1;
     98     bool m_forTextEmphasis : 1;
     99 };
    100 
    101 } // namespace blink
    102 
    103 #endif
    104