height of paragraph at position

General TRichView support forum. Please post your questions here
Post Reply
blsstdok
Posts: 25
Joined: Tue Jun 07, 2011 2:36 pm

height of paragraph at position

Post by blsstdok »

What is the best way to get the pixel height of a paragraph at any given point in the document? I assume this would be two steps -- 1. Get the paragraph and 2. find it's height.

Thanks.
Sergey Tkachenko
Site Admin
Posts: 17572
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

It's possible by calculating positions of all items, but may be a more simple solution possible.
What do you want to implement?
blsstdok
Posts: 25
Joined: Tue Jun 07, 2011 2:36 pm

Post by blsstdok »

It's complicated, but basically I want to make subtle adjustments to the indentation, reformat, and test to be sure I haven't changed the vertical size of the paragraph due to wrapping. I also need to know the horizontal size of each line in the paragraph.
Sergey Tkachenko
Site Admin
Posts: 17572
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

This calculation requires using "drawing items" which are undocumented.

The example is shown below. It calculates the height of the current paragraph (the paragraph containing the caret).
If you have the specified position in the document - (RVData, ItemNo), you can use them instead of (rve.RVData, rve.CurItemNo).

Code: Select all

uses DLines;

var FirstItemNo, LastItemNo: Integer;
    i, FirstDItemNo, LastDItemNo: Integer;
    rve: TCustomRichViewEdit;
    Top, Bottom: Integer;
    DrawItem: TRVDrawLineInfo;
    ParaStyle: TParaInfo;
begin
  rve := RichViewEdit1.TopLevelEditor;
  rve.RVData.ExpandToPara(rve.CurItemNo, rve.CurItemNo, FirstItemNo, LastItemNo);
  rve.RVData.Item2FirstDrawItem(FirstItemNo, FirstDItemNo);
  rve.RVData.Item2LastDrawItem(LastItemNo, LastDItemNo);
  DrawItem := rve.RVData.DrawItems[FirstDItemNo];
  Top := DrawItem.Top-DrawItem.ExtraSpaceAbove;
  Bottom := DrawItem.Top+DrawItem.Height+DrawItem.ExtraSpaceBelow;
  for i := FirstDItemNo+1 to LastDItemNo do begin
    DrawItem := rve.RVData.DrawItems[i];
    if Top>DrawItem.Top-DrawItem.ExtraSpaceAbove then
      Top := DrawItem.Top-DrawItem.ExtraSpaceAbove;
    if Bottom<DrawItem.Top+DrawItem.Height+DrawItem.ExtraSpaceBelow then
      Bottom := DrawItem.Top+DrawItem.Height+DrawItem.ExtraSpaceBelow;
  end;
  ParaStyle := rve.Style.ParaStyles[rve.GetItemPara(FirstItemNo)];
  dec(Top, ParaStyle.SpaceBefore);
  inc(Bottom, ParaStyle.SpaceAfter);

  Caption := 'Current paragraph height: '+IntToStr(Bottom-Top);
end;
A document must be formatted when you call this procedure. This procedure ignores left- and right-aligned objects.
Post Reply