Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Text.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 
27 #include "cinder/Surface.h"
28 #include "cinder/Font.h"
29 #include "cinder/Vector.h"
30 
31 #include <vector>
32 #include <deque>
33 #include <string>
34 
35 // Core Text forward declarations
36 #if defined( CINDER_COCOA )
37 struct __CTFrame;
38 struct __CTLine;
39 #endif
40 
41 namespace cinder {
42 
43 class TextLayout {
44  public:
50  TextLayout();
51 
53  void clear( const Color &color );
55  void clear( const ColorA &color );
56 
58  void addLine( const std::string &line );
60  void addCenteredLine( const std::string &line );
62  void addRightLine( const std::string &line );
64  void append( const std::string &str );
65 
66  void setFont( const Font &font );
68  void setColor( const Color &color );
70  void setColor( const ColorA &color );
72  void setLeadingOffset( float leadingOffset );
73 
75  void setBorder( int horizontal, int vertical );
76 
78  Surface render( bool useAlpha = false, bool premultiplied = false );
79 
80  private:
81  ColorA mBackgroundColor;
82  Font mCurrentFont;
83  ColorA mCurrentColor;
84  float mCurrentLeadingOffset;
85  int mHorizontalBorder, mVerticalBorder;
86 
87  std::deque<std::shared_ptr<class Line> > mLines;
88 };
89 
90 class TextBox {
91  public:
92  typedef enum Alignment { LEFT, CENTER, RIGHT } Alignment;
93  enum { GROW = 0 };
94 
95  TextBox() : mAlign( LEFT ), mSize( GROW, GROW ), mFont( Font::getDefault() ), mInvalid( true ), mColor( 1, 1, 1, 1 ), mBackgroundColor( 0, 0, 0, 0 ), mPremultiplied( false ), mLigate( true ) {}
96 
97  TextBox& size( Vec2i sz ) { setSize( sz ); return *this; }
98  TextBox& size( int width, int height ) { setSize( Vec2i( width, height ) ); return *this; }
99  Vec2i getSize() const { return mSize; }
100  void setSize( Vec2i sz ) { mSize = sz; mInvalid = true; }
101 
102  TextBox& text( const std::string &t ) { setText( t ); return *this; }
103  const std::string& getText() const { return mText; }
104  void setText( const std::string &t ) { mText = t; mInvalid = true; }
105  void appendText( const std::string &t ) { mText += t; mInvalid = true; }
106 
107  TextBox& font( const Font &f ) { setFont( f ); return *this; }
108  const Font& getFont() const { return mFont; }
109  void setFont( const Font &f ) { mFont = f; mInvalid = true; }
110 
111  TextBox& alignment( Alignment align ) { setAlignment( align ); return *this; }
112  Alignment getAlignment() const { return mAlign; }
113  void setAlignment( Alignment align ) { mAlign = align; mInvalid = true; }
114 
115  TextBox& color( ColorA color ) { setColor( color ); return *this; }
116  ColorA getColor() const { return mColor; }
117  void setColor( ColorA color ) { mColor = color; mInvalid = true; }
118 
119  TextBox& backgroundColor( ColorA bgColor ) { setBackgroundColor( bgColor ); return *this; }
121  void setBackgroundColor( ColorA bgColor ) { mBackgroundColor = bgColor; }
122 
123  TextBox& premultiplied( bool premult = true ) { setPremultiplied( premult ); return *this; }
124  bool getPremultiplied() const { return mPremultiplied; }
125  void setPremultiplied( bool premult ) { mPremultiplied = premult; }
126 
127  TextBox& ligate( bool ligateText = true ) { setLigate( ligateText ); return *this; }
128  bool getLigate() const { return mLigate; }
129  void setLigate( bool ligateText ) { mLigate = ligateText; }
130 
131  Vec2f measure() const;
134  std::vector<std::pair<uint16_t,Vec2f> > measureGlyphs() const;
135 
137 
138  protected:
145  bool mLigate;
146  mutable bool mInvalid;
147 
149 #if defined( CINDER_COCOA )
150  void createLines() const;
151 
152  mutable std::vector<std::pair<std::shared_ptr<const __CTLine>,Vec2f> > mLines;
153 #elif defined( CINDER_MSW )
154  std::vector<std::string> calculateLineBreaks() const;
155  void calculate() const;
156 
157  mutable std::u16string mWideText;
158 #elif defined( CINDER_WINRT )
159  std::vector<std::string> calculateLineBreaks() const;
160 #endif
161 };
162 
167 #if defined( CINDER_COCOA_TOUCH )
168 Surface renderStringPow2( const std::string &str, const Font &font, const ColorA &color, Vec2i *actualSize, float *baselineOffset = 0 );
169 #else
170 Surface renderString( const std::string &str, const Font &font, const ColorA &color, float *baselineOffset = 0 );
171 #endif
172 
173 } // namespace cinder
void setSize(Vec2i sz)
Definition: Text.h:100
ColorA getBackgroundColor() const
Definition: Text.h:120
void setBackgroundColor(ColorA bgColor)
Definition: Text.h:121
TextBox & premultiplied(bool premult=true)
Definition: Text.h:123
void setPremultiplied(bool premult)
Definition: Text.h:125
void setColor(ColorA color)
Definition: Text.h:117
TextLayout()
This is an abstract line.
Definition: Text.cpp:304
ColorA mBackgroundColor
Definition: Text.h:143
bool getPremultiplied() const
Definition: Text.h:124
GLsizei const GLchar ** string
Definition: GLee.h:2427
Font mFont
Definition: Text.h:142
bool getLigate() const
Definition: Text.h:128
GLenum GLsizei width
Definition: GLee.h:969
Surface render(Vec2f offset=Vec2f::zero())
static Vec2< float > zero()
Definition: Vector.h:295
bool mPremultiplied
Definition: Text.h:144
void setLeadingOffset(float leadingOffset)
Sets an offset relative to the default leading (the vertical space between lines).
Definition: Text.cpp:366
Surface render(bool useAlpha=false, bool premultiplied=false)
Returns a Surface into which the TextLayout is rendered. If useAlpha the Surface will contain an alph...
Definition: Text.cpp:387
Alignment
Definition: Text.h:92
TextBox & font(const Font &f)
Definition: Text.h:107
const std::string & getText() const
Definition: Text.h:103
void addLine(const std::string &line)
Adds a left-justified line of text to the layout. Assumes UTF-8 encoding.
Definition: Text.cpp:326
TextBox & color(ColorA color)
Definition: Text.h:115
std::string mText
Definition: Text.h:141
Definition: Text.h:93
TextBox()
Definition: Text.h:95
GLenum GLsizei GLsizei height
Definition: GLee.h:1029
void setBorder(int horizontal, int vertical)
Adds a horizontal pixel border to the left and the right sides, and a vertical border to the top and ...
Definition: Text.cpp:371
Represents an instance of a font at a point size. Implicitly shared object.
Definition: Font.h:63
void clear(const Color &color)
Sets the background color for the TextLayout. Implicit opqaue alpha.
Definition: Text.cpp:316
ColorA mColor
Definition: Text.h:143
bool mInvalid
Definition: Text.h:146
TextBox & backgroundColor(ColorA bgColor)
Definition: Text.h:119
GLintptr offset
Definition: GLee.h:2095
void addRightLine(const std::string &line)
Adds a right-justified line of text to the layout. Assumes UTF-8 encoding.
Definition: Text.cpp:344
const Font & getFont() const
Definition: Text.h:108
ColorA getColor() const
Definition: Text.h:116
Surface renderString(const std::string &str, const Font &font, const ColorA &color, float *baselineOffset=0)
Renders a single string and returns it as a Surface. Optional baselineOffset pointer will receive how...
Definition: Text.h:92
Definition: Text.h:90
TextBox & alignment(Alignment align)
Definition: Text.h:111
TextBox & size(int width, int height)
Definition: Text.h:98
void addCenteredLine(const std::string &line)
Adds a centered line of text to the layout. Assumes UTF-8 encoding.
Definition: Text.cpp:335
Definition: Text.h:92
Vec2i getSize() const
Definition: Text.h:99
TextBox & text(const std::string &t)
Definition: Text.h:102
void setAlignment(Alignment align)
Definition: Text.h:113
bool mLigate
Definition: Text.h:145
void setText(const std::string &t)
Definition: Text.h:104
void setColor(const Color &color)
Sets the currently active color. Implicit opqaue alpha.
Definition: Text.cpp:377
void append(const std::string &str)
Appends string str to the current line. Assumes UTF-8 encoding.
Definition: Text.cpp:353
Vec2f measure() const
std::vector< std::pair< uint16_t, Vec2f > > measureGlyphs() const
Vec2f mCalculatedSize
Definition: Text.h:148
Alignment mAlign
Definition: Text.h:139
void setFont(const Font &font)
Definition: Text.cpp:361
Vec2i mSize
Definition: Text.h:140
Alignment getAlignment() const
Definition: Text.h:112
TextBox & ligate(bool ligateText=true)
Definition: Text.h:127
GLdouble GLdouble t
Definition: GLee.h:1426
GLclampf f
Definition: GLee.h:15307
void setFont(const Font &f)
Definition: Text.h:109
void setLigate(bool ligateText)
Definition: Text.h:129
Definition: Text.h:92
TextBox & size(Vec2i sz)
Definition: Text.h:97
Vec2< int > Vec2i
Definition: Vector.h:1313
Definition: Text.h:43
void appendText(const std::string &t)
Definition: Text.h:105
GLuint color
Definition: GLee.h:3198