How to get width of entire table-cell's text, w/ mixed fonts

General TRichView support forum. Please post your questions here
Post Reply
DXS
Posts: 44
Joined: Fri Sep 16, 2005 10:31 am
Location: Cape Town, South Africa

How to get width of entire table-cell's text, w/ mixed fonts

Post by DXS »

Hello :)

I am trying to 'manually' adjust a cell that is rotated 90 degrees. I assume that I need to set BestHeight to the required value - that works except for when I have fonts of mixed sizes - so, if above is correct method of adjusting cell height, how should I go about getting the total text length/width (given a mixture of font sizes in the same cell?

thank you,
Andy
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

A manual calculation may be complicated, so may be the best solution is format the document and then get sizes.

In formatted documents, you can use Cell properties DocumentWidth and DocumentHeight.
Note that for cells rotated by 90 or -90 degrees, DocumentWidth returns a vertical size, DocumentHeight returns a horizontal size.

To measure sizes, set a small BestHeight and include rvpaoNoWrap in Options of all paragraphs in the cell.
DXS
Posts: 44
Joined: Fri Sep 16, 2005 10:31 am
Location: Cape Town, South Africa

Post by DXS »

Thank you for your help - I do have a followup:

I want the size of the column (which contains 90/270 degree rotated text) to match the current size of that cells contents. I have it so the width (height) grows and shrinks with the text length (according to its font). When I increase the font's size, the cell expands, however, then I decrease the font's size, the cell's size remains the same. I've tried setting the BestWidth:=0 or 10 (small values as suggested?), then setting the ...BestWidth := ...DocumentWidth, but I can't seem to get the cell to shrink back.

Can you show me a/the preferred way to do this?

Thank you,
Andy
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Here is my test. It assumes you have RichViewEdit1 linked with RVStyle1.

Code: Select all

function GetNoWrapPara(RVStyle: TRVStyle): Integer;
var
  ParaStyle: TParaInfo;
begin
  ParaStyle := TParaInfo.Create(nil);
  ParaStyle.Options := [rvpaoNoWrap];
  Result := RVStyle.FindParaStyle(ParaStyle);
  ParaStyle.Free;
end;

function GetTextStyle(RVStyle: TRVStyle; Size: Integer): Integer;
var
  TextStyle: TFontInfo;
begin
  TextStyle := TFontInfo.Create(nil);
  TextStyle.Assign(RVStyle.TextStyles[0]);
  TextStyle.Size := Size;
  Result := RVStyle.FindTextStyle(TextStyle);
  TextStyle.Free;
end;

procedure TForm3.ToolButton63Click(Sender: TObject);
var
  Table: TRVTableItemInfo;
  ParaNo, StyleNo: Integer;

  procedure MakeAutoSize;
  begin
    Table.Cells[0,0].BestHeight := RVStyle1.PixelsToUnits(Table.Cells[0,0].DocumentWidth);
    Table.Cells[0,1].BestHeight := RVStyle1.PixelsToUnits(Table.Cells[0,1].DocumentWidth);
    Table.Cells[0,0].BestWidth := RVStyle1.PixelsToUnits(Table.Cells[0,0].DocumentHeight);
    Table.Cells[0,1].BestWidth := RVStyle1.PixelsToUnits(Table.Cells[0,1].DocumentHeight);
  end;

  procedure MakeSmallSize;
  begin
    Table.Cells[0,0].BestHeight := 1;
    Table.Cells[0,1].BestHeight := 1;
    Table.Cells[0,0].BestWidth := 1;
    Table.Cells[0,1].BestWidth := 1;
  end;

begin
  ParaNo := GetNoWrapPara(RVStyle1);
  StyleNo := GetTextStyle(RVStyle1, 20);
  Table := TRVTableItemInfo.CreateEx(1, 2, RichViewEdit1.RVData);
  Table.CellBorderWidth := RVStyle1.PixelsToUnits(1);
  Table.BorderWidth := RVStyle1.PixelsToUnits(1);
  Table.CellPadding := RVStyle1.PixelsToUnits(20);
  Table.Cells[0,0].Rotation := rvrot90;
  Table.Cells[0,0].Clear;
  Table.Cells[0,0].AddNL('Hello world!!!', StyleNo, ParaNo);
  Table.Cells[0,1].Rotation := rvrot90;
  Table.Cells[0,1].Clear;
  Table.Cells[0,1].AddNL('Good bye!', StyleNo, ParaNo);
  Table.Cells[0,1].AddNL('Good bye!', StyleNo, ParaNo);
  MakeSmallSize;
  if not RichViewEdit1.InsertItem('', Table) then
    exit;
  MakeAutoSize;
  RichViewEdit1.Reformat;
  Application.MessageBox('Making text smaller', 'Table Test');
  MakeSmallSize;
  StyleNo := GetTextStyle(RVStyle1, 10);
  Table.Cells[0, 0].GetItem(0).StyleNo := StyleNo;
  Table.Cells[0, 1].GetItem(0).StyleNo := StyleNo;
  Table.Cells[0, 1].GetItem(1).StyleNo := StyleNo;
  RichViewEdit1.Reformat;
  MakeAutoSize;
  RichViewEdit1.Reformat;
  Application.MessageBox('Making text larger', 'Table Test');
  MakeSmallSize;
  StyleNo := GetTextStyle(RVStyle1, 30);
  Table.Cells[0, 0].GetItem(0).StyleNo := StyleNo;
  Table.Cells[0, 1].GetItem(0).StyleNo := StyleNo;
  Table.Cells[0, 1].GetItem(1).StyleNo := StyleNo;
  RichViewEdit1.Reformat;
  MakeAutoSize;
  RichViewEdit1.Reformat;
end;
For simplicity, this code uses Reformat to reformat the document. A more efficient way to reformat is using BeginItemModify and EndItemModify.

Update: I added calls to PixelsToUnits, so this code works when sizes are measured in twips as well.
DXS
Posts: 44
Joined: Fri Sep 16, 2005 10:31 am
Location: Cape Town, South Africa

Post by DXS »

Hi Sergey,

That worked, thanks for the help :)

-Andy
Post Reply