uses CRVData;
function GetCharCount(RVData: TCustomRVData): Integer;
var i,r,c: Integer;
table: TRVTableItemInfo;
begin
Result := 0;
for i := 0 to RVData.Items.Count-1 do
if RVData.GetItemStyle(i)>=0 then begin // this is a text item
inc(Result, RVData.GetItemTextLength(i))
else if RVData.GetItemStyle(i)=rvsTab then
inc(Result)
else if RVData.GetItemStyle(i)=rvsTable then begin
table := TRVTableItemInfo(RVData.GetItem(i));
for r := 0 to table.Rows.Count-1 do
for c := 0 to table.Rows[r].Count-1 do
if table.Cells[r,c]<>nil then
inc(Result, GetCharCount(table.Cells[r,c].GetRVData));
end;
end;
TWordCounter = class(TRVWordEnumerator)
private
FCounter: Integer;
protected
function ProcessWord: Boolean; override;
public
function GetWordCount(rve: TCustomRichViewEdit): Integer;
end;
function TWordCounter.ProcessWord: Boolean;
begin
inc(FCounter);
Result := True;
end;
function TWordCounter.GetWordCount(rve: TCustomRichViewEdit): Integer;
begin
FCounter := 0;
Run(rve, rvesFromStart);
Result := FCounter;
end;
(this function treats word written with two different fonts as two words)
uses CRVData;
function GetCharCount(RVData: TCustomRVData): Integer;
var i,r,c: Integer;
table: TRVTableItemInfo;
begin
Result := 0;
for i := 0 to RVData.Items.Count-1 do
if RVData.GetItemStyle(i)>=0 then begin // this is a text item
inc(Result, RVData.ItemLength(i))
else if RVData.GetItemStyle(i)=rvsTable then begin
table := TRVTableItemInfo(RVData.GetItem(i));
for r := 0 to table.Rows.Count-1 do
for c := 0 to table.Rows[r].Count-1 do
if table.Cells[r,c]<>nil then
inc(Result, GetCharCount(table.Cells[r,c].GetRVData));
end;
end;
This pair of functions counts the number of words and characters in a TRichViewEdit control. The WordCount function is just a generic routine to return word count from any text. The second is fired when the text changes. It loops through the text items in the RVE and passes each to WordCount and totals up the results.
// Function to provide word count from memo or text field
function WordCount(t: String): LongInt;
var
ws: Boolean;
wc: Integer;
i: Integer;
begin
ws := True; // In whitespace
wc := 0;
for i := 0 to Length(t) - 1 do
begin
if t[i] in [' ', #9, #10, #13] then
ws := True
else if ws then
begin
ws := False;
Inc(wc);
end;
end;
Result := wc;
end;
// Change in text - count words and characters.
procedure Tform1.RVEChange(Sender: TObject);
var
wc, cc: LongInt;
I: Integer;
begin
cc := 0;
wc := 0;
with RVE do
begin
for I := 0 to RVData.Items.Count - 1 do
if RVData.GetItemStyle(I) >= 0 then
begin
inc (cc, RVData.GetItemTextLength(I));
inc (wc, WordCount(RVData.GetItemTextA(I)));
end;
end;
lbCount.Caption := IntToStr(cc) + ' characters, ' + IntToStr(wc) + ' words.';
end;
Updated 2017-02-15 for compatibility with TRichView 16.13. For older versions of TRichView, use ItemLength instead of GetItemTextLength
uses RVWordEnum, RVTable, CRVFData;
type
TSelWordCounter = class(TRVWordEnumerator)
private
FCounter: Integer;
FSelRVData: TCustomRVFormattedData;
FSelStartItemNo, FSelStartOffs, FSelEndItemNo, FSelEndOffs: Integer;
protected
function ProcessWord: Boolean; override;
public
function GetWordCount(rve: TCustomRichViewEdit): Integer;
end;
function TSelWordCounter.ProcessWord: Boolean;
begin
Result :=
not
(
(FRVData=FSelRVData) and
((FItemNo>FSelEndItemNo) or ((FItemNo=FSelEndItemNo) and (FStartOffs>=FSelEndOffs)))
);
if Result then
inc(FCounter);
end;
function TSelWordCounter.GetWordCount(rve: TCustomRichViewEdit): Integer;
var table: TRVTableItemInfo;
r, c: Integer;
begin
Result := 0;
FCounter := 0;
rve := rve.TopLevelEditor;
if rve.RVData.PartialSelectedItem<>nil then begin
// multicell selection
FSelRVData := nil;
table := rve.RVData.PartialSelectedItem as TRVTableItemInfo;
for r := 0 to table.RowCount-1 do
for c := 0 to table.ColCount-1 do
if (table.Cells[r,c]<>nil) and table.IsCellSelected(r, c) then
RunRVData(rve, table.Cells[r,c], 0, table.Cells[r,c].GetOffsBeforeItem(0));
end
else begin
if not rve.SelectionExists then
exit;
// normal selection
FSelRVData := rve.RVData;
FSelRVData.GetSelectionBoundsEx(FSelStartItemNo, FSelStartOffs,
FSelEndItemNo, FSelEndOffs, True);
RunRVData(rve, rve.RVData, FSelStartItemNo, FSelStartOffs);
end;
Result := FCounter;
end;
[DCC Warning] Unit3.pas(777): W1050 WideChar reduced to byte char in set expressions. Consider using 'CharInSet' function in 'SysUtils' unit.
can you fix this function
function WordCount(t: String): LongInt;
var
ws: Boolean;
wc: Integer;
i: Integer;
begin
ws := True; // In whitespace
wc := 0;
for i := 0 to Length(t) - 1 do
begin
if t in [' ', #9, #10, #13] then
ws := True
else if ws then
begin
ws := False;
Inc(wc);
end;
end;
Result := wc;
end;
It is possible to count lines and paragraphs too, and if yes, do you think that it will be too time consuming when working with large documents (compared to char and word count)?
I need something similar to Word's word count. I've made a test and it seems to work like this:
* Lines - they depend on word wrapping and each table cell is counted as a line, if the cell is empty
* Paragraphs - it seems that only items, containing text are counted as paragraphs (empty table cells are not)
Anyway, absolute accuracy is not necessary, speed is priority here. People using a word count function need accuracy in the word and character counts more.
uses CRVData, RVTable;
function GetParagraphCount(RVData: TCustomRVData): Integer;
var i,r,c: Integer;
table: TRVTableItemInfo;
begin
Result := 0;
for i := 0 to RVData.ItemCount-1 do
begin
if RVData.IsParaStart(i) then
inc(Result);
if RVData.GetItem(i) is TRVTableItemInfo 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, GetParagraphCount(table.Cells[r,c].GetRVData));
end;
end;
end;
Use: Count := GetParagraphCount(RichViewEdit1.RVData);
In the show-special-characters mode, you can see pilcrow characters at the end of paragraphs. GetParagraphCount returns the count of these characters. Note that they are displayed in places where MS Word does not display them:
- at the end of table cells
- at the end of tables.
Last edited by Sergey Tkachenko on Thu Mar 11, 2010 7:24 pm, edited 1 time in total.
uses CRVFData, RVTable, DLines;
function GetLineCount(RVData: TCustomRVFormattedData): Integer;
var i,r,c: Integer;
table: TRVTableItemInfo;
begin
Result := 0;
for i := 0 to RVData.DrawItems.Count-1 do
begin
if RVData.DrawItems[i].FromNewLine then
inc(Result);
if RVData.GetItem(RVData.DrawItems[i].ItemNo) is TRVTableItemInfo 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)));
end;
end;
end;