TextDocumentLayout: Line info and refactoring
* Added LineIndexForOffset() * Extracted _ParagraphLayoutIndexForOffset()
This commit is contained in:
@@ -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
|
void
|
||||||
TextDocumentLayout::GetTextBounds(int32 textOffset, float& x1, float& y1,
|
TextDocumentLayout::GetTextBounds(int32 textOffset, float& x1, float& y1,
|
||||||
float& x2, float& y2)
|
float& x2, float& y2)
|
||||||
{
|
{
|
||||||
_ValidateLayout();
|
int32 index = _ParagraphLayoutIndexForOffset(textOffset);
|
||||||
|
if (index >= 0) {
|
||||||
int32 paragraphs = fParagraphLayouts.CountItems();
|
const ParagraphLayoutInfo& info = fParagraphLayouts.ItemAtFast(index);
|
||||||
for (int32 i = 0; i < paragraphs; i++) {
|
info.layout->GetTextBounds(textOffset, x1, y1, x2, y2);
|
||||||
const ParagraphLayoutInfo& layout = fParagraphLayouts.ItemAtFast(i);
|
y1 += info.y;
|
||||||
|
y2 += info.y;
|
||||||
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;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,3 +197,26 @@ TextDocumentLayout::_Layout()
|
|||||||
y += info.layout->Height() + style.SpacingBottom();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ public:
|
|||||||
void Draw(BView* view, const BPoint& offset,
|
void Draw(BView* view, const BPoint& offset,
|
||||||
const BRect& updateRect);
|
const BRect& updateRect);
|
||||||
|
|
||||||
|
int32 LineIndexForOffset(int32 textOffset);
|
||||||
|
|
||||||
void GetTextBounds(int32 textOffset,
|
void GetTextBounds(int32 textOffset,
|
||||||
float& x1, float& y1,
|
float& x1, float& y1,
|
||||||
@@ -99,6 +100,9 @@ private:
|
|||||||
void _DrawLayout(BView* view,
|
void _DrawLayout(BView* view,
|
||||||
const ParagraphLayoutInfo& layout) const;
|
const ParagraphLayoutInfo& layout) const;
|
||||||
|
|
||||||
|
int32 _ParagraphLayoutIndexForOffset(
|
||||||
|
int32& textOffset);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float fWidth;
|
float fWidth;
|
||||||
bool fLayoutValid;
|
bool fLayoutValid;
|
||||||
|
|||||||
Reference in New Issue
Block a user