repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / haikudepot / textview / ParagraphLayout.h
blob4cc46df80c58a3e81c29ba4b741067f5447e6724
1 /*
2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5 #ifndef PARAGRAPH_LAYOUT_H
6 #define PARAGRAPH_LAYOUT_H
8 #include <Font.h>
9 #include <Referenceable.h>
10 #include <String.h>
12 #include "Paragraph.h"
13 #include "CharacterStyle.h"
16 class BView;
19 class GlyphInfo {
20 public:
21 GlyphInfo()
23 charCode(0),
24 x(0.0f),
25 width(0.0f),
26 lineIndex(0)
30 GlyphInfo(uint32 charCode, float x, float width, int32 lineIndex)
32 charCode(charCode),
33 x(x),
34 width(width),
35 lineIndex(lineIndex)
39 GlyphInfo(const GlyphInfo& other)
41 charCode(other.charCode),
42 x(other.x),
43 width(other.width),
44 lineIndex(other.lineIndex)
48 GlyphInfo& operator=(const GlyphInfo& other)
50 charCode = other.charCode;
51 x = other.x;
52 width = other.width;
53 lineIndex = other.lineIndex;
54 return *this;
57 bool operator==(const GlyphInfo& other) const
59 return charCode == other.charCode
60 && x == other.x
61 && width == other.width
62 && lineIndex == other.lineIndex;
65 bool operator!=(const GlyphInfo& other) const
67 return !(*this == other);
70 public:
71 uint32 charCode;
73 float x;
74 float width;
76 int32 lineIndex;
80 typedef List<GlyphInfo, false> GlyphInfoList;
83 class LineInfo {
84 public:
85 LineInfo()
87 textOffset(0),
88 y(0.0f),
89 height(0.0f),
90 maxAscent(0.0f),
91 maxDescent(0.0f),
92 extraGlyphSpacing(0.0f),
93 extraWhiteSpacing(0.0f),
94 layoutedSpans()
98 LineInfo(int32 textOffset, float y, float height, float maxAscent,
99 float maxDescent)
101 textOffset(textOffset),
102 y(y),
103 height(height),
104 maxAscent(maxAscent),
105 maxDescent(maxDescent),
106 extraGlyphSpacing(0.0f),
107 extraWhiteSpacing(0.0f),
108 layoutedSpans()
112 LineInfo(const LineInfo& other)
114 textOffset(other.textOffset),
115 y(other.y),
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;
128 y = other.y;
129 height = other.height;
130 maxAscent = other.maxAscent;
131 maxDescent = other.maxDescent;
132 extraGlyphSpacing = other.extraGlyphSpacing;
133 extraWhiteSpacing = other.extraWhiteSpacing;
134 layoutedSpans = other.layoutedSpans;
135 return *this;
138 bool operator==(const LineInfo& other) const
140 return textOffset == other.textOffset
141 && y == other.y
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);
155 public:
156 int32 textOffset;
158 float y;
159 float height;
161 float maxAscent;
162 float maxDescent;
164 float extraGlyphSpacing;
165 float extraWhiteSpacing;
167 TextSpanList layoutedSpans;
171 typedef List<LineInfo, false> LineInfoList;
174 class ParagraphLayout : public BReferenceable {
175 public:
176 ParagraphLayout();
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);
186 float Width() const
187 { return fWidth; }
189 float Height();
190 void Draw(BView* view, const BPoint& offset);
192 int32 CountGlyphs() const;
193 int32 CountLines();
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);
210 private:
211 void _Init();
213 void _ValidateLayout();
214 void _Layout();
215 void _ApplyAlignment();
217 bool _AppendGlyphInfos(const TextSpan& span);
218 bool _AppendGlyphInfo(uint32 charCode,
219 float advanceX,
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;
237 private:
238 TextSpanList fTextSpans;
239 ParagraphStyle fParagraphStyle;
241 float fWidth;
242 bool fLayoutValid;
244 GlyphInfoList fGlyphInfos;
245 LineInfoList fLineInfos;
249 typedef BReference<ParagraphLayout> ParagraphLayoutRef;
252 #endif // PARAGRAPH_LAYOUT_H