[Examples] Count of characters and words
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Interesting question.
Characters, paragraphs - yes.
As for word enumeration, as I can see, it is safe too, with one possible problem. It uses TRVWordEnumerator class that was primarily created for spell checking. When called for TRichViewEdit, it prevents its form from closing by assigning its OnCloseQuery event and restoring it after its work.
If several word enumerators will work at the same time in threads, it may happen that the original OnCloseQuery will not be restored.
Characters, paragraphs - yes.
As for word enumeration, as I can see, it is safe too, with one possible problem. It uses TRVWordEnumerator class that was primarily created for spell checking. When called for TRichViewEdit, it prevents its form from closing by assigning its OnCloseQuery event and restoring it after its work.
If several word enumerators will work at the same time in threads, it may happen that the original OnCloseQuery will not be restored.
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Calculating paragraph counts in the way like MS Word: empty paragraphs are not taken into account.
Paragraphs are considered empty if they do not contain any of:
- non-empty text items (at least one non-space character);
- bullet or numbering (text);
- non-empty labelitem (label/number/footnote/endnote)
As you can see, paragraph containing only pictures, spaces and tabs are considered empty.
For tables, the count of paragraphs equals to sum of counts of paragraphs in all its cells. The table itself is not counted as a paragraph.
Use:
Paragraphs are considered empty if they do not contain any of:
- non-empty text items (at least one non-space character);
- bullet or numbering (text);
- non-empty labelitem (label/number/footnote/endnote)
As you can see, paragraph containing only pictures, spaces and tabs are considered empty.
For tables, the count of paragraphs equals to sum of counts of paragraphs in all its cells. The table itself is not counted as a paragraph.
Code: Select all
function GetParaCount(RVData: TCustomRVData): Integer;
var i, r, c, StyleNo: Integer;
table: TRVTableItemInfo;
EmptyPara: Boolean;
ListNo, ListLevel, StartFrom: Integer;
Reset: Boolean;
begin
Result := 0;
EmptyPara := True;
for i := 0 to RVData.ItemCount-1 do begin
if RVData.IsParaStart(i) then begin
if not EmptyPara then
inc(Result);
EmptyPara := True;
end;
StyleNo := RVData.GetItemStyle(i);
if StyleNo=rvsListMarker then begin
RVData.GetListMarkerInfo(i,ListNo, ListLevel, StartFrom, Reset);
if (ListNo>=0) and (ListNo<RVData.GetRVStyle.ListStyles.Count) and
(ListLevel>=0) and (ListLevel<RVData.GetRVStyle.ListStyles[ListNo].Levels.Count) and
RVData.GetRVStyle.ListStyles[ListNo].Levels[ListLevel].UsesFont then
EmptyPara := False
end
else if RVData.GetItem(i) is TRVLabelItemInfo then begin
if Trim(TRVLabelItemInfo(RVData.GetItem(i)).Text)<>'' then
end
else if StyleNo>=0 then begin
if Trim(RVData.GetItemText(i))<>'' then
EmptyPara := False;
end
else if StyleNo=rvsTable then begin
table := TRVTableItemInfo(RVData.GetItem(i));
for r := 0 to table.RowCount-1 do
for c := 0 to table.ColCount-1 do
if table.Cells[r,c]<>nil then
inc(Result, GetParaCount(table.Cells[r,c].GetRVData));
end;
end;
if not EmptyPara then
inc(Result);
end;
Code: Select all
lblParaCount.Caption := IntToStr(GetParaCount(RichViewEdit1.RVData));
Ok, everything works fine here, i can count words/characters/paragraphs but still have some quiestions:
1. When counting document lines globally (including tables) how can I check cursor position globally too? Because RVE.GetCurrentLineCol(line,col); returns only local line number.
2. Using GetLineCount(RVE.RVData); from your example inside RVE.OnCaretMove generates errors. How can I avoid this checking cursor position when it moves?
1. When counting document lines globally (including tables) how can I check cursor position globally too? Because RVE.GetCurrentLineCol(line,col); returns only local line number.
2. Using GetLineCount(RVE.RVData); from your example inside RVE.OnCaretMove generates errors. How can I avoid this checking cursor position when it moves?
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
I want to know global current line number.
How can I check if we got cursor in the current line in this routine:
How can I check if we got cursor in the current line in this routine:
Code: Select all
...
if RVData.GetItemStyle(RVData.DrawItems[i].ItemNo)=rvsTable then begin
table := TRVTableItemInfo(RVData.GetItem(RVData.DrawItems[i].ItemNo));
for r := 0 to table.RowCount-1 do
for c := 0 to table.ColCount-1 do
if table.Cells[r,c]<>nil then
inc(Result, GetLineCount(TCustomRVFormattedData(table.Cells[r,c].GetRVData)));
...
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Code: Select all
function GetCurrentLine(rve: TCustomRichViewEdit): Integer;
var
CurRVData: TCustomRVFormattedData;
CurDItemNo: Integer;
CurDOffs: Integer;
function DoGetCurrentLine(RVData: TCustomRVFormattedData; var LineNo: Integer): Boolean;
var i,r,c: Integer;
table: TRVTableItemInfo;
begin
Result := False;
for i := 0 to RVData.DrawItems.Count-1 do begin
if RVData.DrawItems[i].FromNewLine then
inc(LineNo);
Result := (RVData=CurRVData) and (i=CurDItemNo);
if Result then
exit;
if RVData.GetItemStyle(RVData.DrawItems[i].ItemNo)=rvsTable then begin
table := TRVTableItemInfo(RVData.GetItem(RVData.DrawItems[i].ItemNo));
for r := 0 to table.RowCount-1 do
for c := 0 to table.ColCount-1 do
if table.Cells[r,c]<>nil then begin
Result := DoGetCurrentLine(TCustomRVFormattedData(table.Cells[r,c].GetRVData), LineNo);
if Result then
exit;
end;
end;
end;
end;
begin
CurRVData := rve.TopLevelEditor.RVData;
CurRVData.Item2DrawItem(rve.TopLevelEditor.CurItemNo,
rve.TopLevelEditor.OffsetInCurItem, CurDItemNo, CurDOffs);
Result := 0;
DoGetCurrentLine(rve.RVData, Result);
end;
But the table may contain several lines and in your first example it counts them.
So I can have just one table with 100 lines and get 100 lines result in your line count example. And this is what I need. Just want to know cursor line number according all my 100 lines (now I'm getting 100 lines total and current line is some 3rd line in a table cell, but must include all previous lines)
So I can have just one table with 100 lines and get 100 lines result in your line count example. And this is what I need. Just want to know cursor line number according all my 100 lines (now I'm getting 100 lines total and current line is some 3rd line in a table cell, but must include all previous lines)
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact: