TextDocumentLayout: Line info and refactoring

* Added LineIndexForOffset()
 * Extracted _ParagraphLayoutIndexForOffset()
This commit is contained in:
Stephan Aßmus 2013-12-07 00:15:08 +01:00
parent e3dc81cc9b
commit 7d66167e17
2 changed files with 52 additions and 15 deletions

View File

@ -105,25 +105,35 @@ TextDocumentLayout::Draw(BView* view, const BPoint& offset,
}
int32
TextDocumentLayout::LineIndexForOffset(int32 textOffset)
{
int32 index = _ParagraphLayoutIndexForOffset(textOffset);
if (index >= 0) {
int32 lineIndex = 0;
for (int32 i = 0; i < index; i++) {
lineIndex += fParagraphLayouts.ItemAtFast(index)
.layout->CountLines();
}
const ParagraphLayoutInfo& info = fParagraphLayouts.ItemAtFast(index);
return lineIndex + info.layout->LineIndexForOffset(textOffset);
}
return 0;
}
void
TextDocumentLayout::GetTextBounds(int32 textOffset, float& x1, float& y1,
float& x2, float& y2)
{
_ValidateLayout();
int32 paragraphs = fParagraphLayouts.CountItems();
for (int32 i = 0; i < paragraphs; i++) {
const ParagraphLayoutInfo& layout = fParagraphLayouts.ItemAtFast(i);
int32 length = layout.layout->CountGlyphs();
if (textOffset > length) {
textOffset -= length;
continue;
}
layout.layout->GetTextBounds(textOffset, x1, y1, x2, y2);
y1 += layout.y;
y2 += layout.y;
int32 index = _ParagraphLayoutIndexForOffset(textOffset);
if (index >= 0) {
const ParagraphLayoutInfo& info = fParagraphLayouts.ItemAtFast(index);
info.layout->GetTextBounds(textOffset, x1, y1, x2, y2);
y1 += info.y;
y2 += info.y;
return;
}
@ -187,3 +197,26 @@ TextDocumentLayout::_Layout()
y += info.layout->Height() + style.SpacingBottom();
}
}
int32
TextDocumentLayout::_ParagraphLayoutIndexForOffset(int32& textOffset)
{
_ValidateLayout();
int32 paragraphs = fParagraphLayouts.CountItems();
for (int32 i = 0; i < paragraphs; i++) {
const ParagraphLayoutInfo& info = fParagraphLayouts.ItemAtFast(i);
int32 length = info.layout->CountGlyphs();
if (textOffset > length) {
textOffset -= length;
continue;
}
return i;
}
return -1;
}

View File

@ -86,6 +86,7 @@ public:
void Draw(BView* view, const BPoint& offset,
const BRect& updateRect);
int32 LineIndexForOffset(int32 textOffset);
void GetTextBounds(int32 textOffset,
float& x1, float& y1,
@ -99,6 +100,9 @@ private:
void _DrawLayout(BView* view,
const ParagraphLayoutInfo& layout) const;
int32 _ParagraphLayoutIndexForOffset(
int32& textOffset);
private:
float fWidth;
bool fLayoutValid;