2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
5 #ifndef PARAGRAPH_LAYOUT_H
6 #define PARAGRAPH_LAYOUT_H
9 #include <Referenceable.h>
12 #include "Paragraph.h"
13 #include "CharacterStyle.h"
30 GlyphInfo(uint32 charCode
, float x
, float width
, int32 lineIndex
)
39 GlyphInfo(const GlyphInfo
& other
)
41 charCode(other
.charCode
),
44 lineIndex(other
.lineIndex
)
48 GlyphInfo
& operator=(const GlyphInfo
& other
)
50 charCode
= other
.charCode
;
53 lineIndex
= other
.lineIndex
;
57 bool operator==(const GlyphInfo
& other
) const
59 return charCode
== other
.charCode
61 && width
== other
.width
62 && lineIndex
== other
.lineIndex
;
65 bool operator!=(const GlyphInfo
& other
) const
67 return !(*this == other
);
80 typedef List
<GlyphInfo
, false> GlyphInfoList
;
92 extraGlyphSpacing(0.0f
),
93 extraWhiteSpacing(0.0f
),
98 LineInfo(int32 textOffset
, float y
, float height
, float maxAscent
,
101 textOffset(textOffset
),
104 maxAscent(maxAscent
),
105 maxDescent(maxDescent
),
106 extraGlyphSpacing(0.0f
),
107 extraWhiteSpacing(0.0f
),
112 LineInfo(const LineInfo
& other
)
114 textOffset(other
.textOffset
),
116 height(other
.height
),
117 maxAscent(other
.maxAscent
),
118 maxDescent(other
.maxDescent
),
119 extraGlyphSpacing(other
.extraGlyphSpacing
),
120 extraWhiteSpacing(other
.extraWhiteSpacing
),
121 layoutedSpans(other
.layoutedSpans
)
125 LineInfo
& operator=(const LineInfo
& other
)
127 textOffset
= other
.textOffset
;
129 height
= other
.height
;
130 maxAscent
= other
.maxAscent
;
131 maxDescent
= other
.maxDescent
;
132 extraGlyphSpacing
= other
.extraGlyphSpacing
;
133 extraWhiteSpacing
= other
.extraWhiteSpacing
;
134 layoutedSpans
= other
.layoutedSpans
;
138 bool operator==(const LineInfo
& other
) const
140 return textOffset
== other
.textOffset
142 && height
== other
.height
143 && maxAscent
== other
.maxAscent
144 && maxDescent
== other
.maxDescent
145 && extraGlyphSpacing
== other
.extraGlyphSpacing
146 && extraWhiteSpacing
== other
.extraWhiteSpacing
147 && layoutedSpans
== other
.layoutedSpans
;
150 bool operator!=(const LineInfo
& other
) const
152 return !(*this == other
);
164 float extraGlyphSpacing
;
165 float extraWhiteSpacing
;
167 TextSpanList layoutedSpans
;
171 typedef List
<LineInfo
, false> LineInfoList
;
174 class ParagraphLayout
: public BReferenceable
{
177 ParagraphLayout(const Paragraph
& paragraph
);
178 ParagraphLayout(const ParagraphLayout
& other
);
179 virtual ~ParagraphLayout();
181 void SetParagraph(const Paragraph
& paragraph
);
182 const ParagraphStyle
& Style() const
183 { return fParagraphStyle
; }
185 void SetWidth(float width
);
190 void Draw(BView
* view
, const BPoint
& offset
);
192 int32
CountGlyphs() const;
195 int32
LineIndexForOffset(int32 textOffset
);
196 int32
FirstOffsetOnLine(int32 lineIndex
);
197 int32
LastOffsetOnLine(int32 lineIndex
);
199 void GetLineBounds(int32 lineIndex
,
200 float& x1
, float& y1
,
201 float& x2
, float& y2
);
203 void GetTextBounds(int32 textOffset
,
204 float& x1
, float& y1
,
205 float& x2
, float& y2
);
207 int32
TextOffsetAt(float x
, float y
,
208 bool& rightOfCenter
);
213 void _ValidateLayout();
215 void _ApplyAlignment();
217 bool _AppendGlyphInfos(const TextSpan
& span
);
218 bool _AppendGlyphInfo(uint32 charCode
,
220 const CharacterStyle
& style
);
222 bool _FinalizeLine(int lineStart
, int lineEnd
,
223 int lineIndex
, float y
, float& lineHeight
);
225 void _IncludeStyleInLine(LineInfo
& line
,
226 const CharacterStyle
& style
);
228 void _DrawLine(BView
* view
, const BPoint
& offset
,
229 const LineInfo
& line
) const;
230 void _DrawSpan(BView
* view
, BPoint offset
,
231 const TextSpan
& span
,
232 int32 textOffset
) const;
234 void _GetEmptyLayoutBounds(float& x1
, float& y1
,
235 float& x2
, float& y2
) const;
238 TextSpanList fTextSpans
;
239 ParagraphStyle fParagraphStyle
;
244 GlyphInfoList fGlyphInfos
;
245 LineInfoList fLineInfos
;
249 typedef BReference
<ParagraphLayout
> ParagraphLayoutRef
;
252 #endif // PARAGRAPH_LAYOUT_H