Home | History | Annotate | Download | only in gfx
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef UI_GFX_RENDER_TEXT_MAC_H_
      6 #define UI_GFX_RENDER_TEXT_MAC_H_
      7 
      8 #include <ApplicationServices/ApplicationServices.h>
      9 
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/mac/scoped_cftyperef.h"
     14 #include "ui/gfx/render_text.h"
     15 
     16 namespace gfx {
     17 
     18 // RenderTextMac is the Mac implementation of RenderText that uses CoreText for
     19 // layout and Skia for drawing.
     20 //
     21 // Note: The current implementation only supports drawing and sizing the text,
     22 //       but not text selection or cursor movement.
     23 class RenderTextMac : public RenderText {
     24  public:
     25   RenderTextMac();
     26   virtual ~RenderTextMac();
     27 
     28   // Overridden from RenderText:
     29   virtual Size GetStringSize() OVERRIDE;
     30   virtual SizeF GetStringSizeF() OVERRIDE;
     31   virtual SelectionModel FindCursorPosition(const Point& point) OVERRIDE;
     32   virtual std::vector<FontSpan> GetFontSpansForTesting() OVERRIDE;
     33 
     34  protected:
     35   // Overridden from RenderText:
     36   virtual int GetLayoutTextBaseline() OVERRIDE;
     37   virtual SelectionModel AdjacentCharSelectionModel(
     38       const SelectionModel& selection,
     39       VisualCursorDirection direction) OVERRIDE;
     40   virtual SelectionModel AdjacentWordSelectionModel(
     41       const SelectionModel& selection,
     42       VisualCursorDirection direction) OVERRIDE;
     43   virtual Range GetGlyphBounds(size_t index) OVERRIDE;
     44   virtual std::vector<Rect> GetSubstringBounds(const Range& range) OVERRIDE;
     45   virtual size_t TextIndexToLayoutIndex(size_t index) const OVERRIDE;
     46   virtual size_t LayoutIndexToTextIndex(size_t index) const OVERRIDE;
     47   virtual bool IsValidCursorIndex(size_t index) OVERRIDE;
     48   virtual void ResetLayout() OVERRIDE;
     49   virtual void EnsureLayout() OVERRIDE;
     50   virtual void DrawVisualText(Canvas* canvas) OVERRIDE;
     51 
     52  private:
     53   struct TextRun {
     54     CTRunRef ct_run;
     55     SkPoint origin;
     56     std::vector<uint16> glyphs;
     57     std::vector<SkPoint> glyph_positions;
     58     SkScalar width;
     59     std::string font_name;
     60     int font_style;
     61     SkScalar text_size;
     62     SkColor foreground;
     63     bool underline;
     64     bool strike;
     65     bool diagonal_strike;
     66 
     67     TextRun();
     68     ~TextRun();
     69   };
     70 
     71   // Applies RenderText styles to |attr_string| with the given |ct_font|.
     72   void ApplyStyles(CFMutableAttributedStringRef attr_string, CTFontRef ct_font);
     73 
     74   // Updates |runs_| based on |line_| and sets |runs_valid_| to true.
     75   void ComputeRuns();
     76 
     77   // The Core Text line of text. Created by |EnsureLayout()|.
     78   base::ScopedCFTypeRef<CTLineRef> line_;
     79 
     80   // Array to hold CFAttributedString attributes that allows Core Text to hold
     81   // weak references to them without leaking.
     82   base::ScopedCFTypeRef<CFMutableArrayRef> attributes_;
     83 
     84   // Visual dimensions of the text. Computed by |EnsureLayout()|.
     85   SizeF string_size_;
     86 
     87   // Common baseline for this line of text. Computed by |EnsureLayout()|.
     88   SkScalar common_baseline_;
     89 
     90   // Visual text runs. Only valid if |runs_valid_| is true. Computed by
     91   // |ComputeRuns()|.
     92   std::vector<TextRun> runs_;
     93 
     94   // Indicates that |runs_| are valid, set by |ComputeRuns()|.
     95   bool runs_valid_;
     96 
     97   DISALLOW_COPY_AND_ASSIGN(RenderTextMac);
     98 };
     99 
    100 }  // namespace gfx
    101 
    102 #endif  // UI_GFX_RENDER_TEXT_MAC_H_
    103