Adjust font size to fit memo in fixed-sized box

I have an application (Delphi 10.2, FR VCL6, MySQL) where I am going to need to create a report where the data must fit into preprinted field areas of a paper form. Fields cannot grow or shrink as in a normal report. Getting all the data fields located in a data band is tedious but doable (each report record is a new page), but there is one issue I am having a problem with, and am seeking guidance: when the data for a field exceeds the size of its pre-printed area on the form, at the field's default font size.

I would like all the data to, for example, print with a Font Size of 12. And >95% of the time, all the data in the fields fit. But it is possible for the data to exceed the allotted space. Since I cannot grow the field size, I instead want to decrease the font size, in this instance only, until the data does fit (i.e., font size = 10 or 9). The client does not want the font for this field to always be smaller than the rest of the form, so I am trying to figure out how to know that the size width of the printed data and adjust the font size accordingly. Preferably I would do this within the field's BeforePrint event, but I could also do the check within the Delphi calling routine, if necessary, and pass information as report variables (this particular report will only print a single master data record at time and the field of interest is at the master level).

Thanks for any help / guidance.

Rick Brodzinsky

Comments

  • edited 12:52PM
    Figured it out by reading through the source for the MemoView components, and very simple

    Uses Memo2 as an example:

    procedure Memo2OnBeforePrint(Sender: TfrxComponet);
    begin
    Memo2.Font.Size := 12; {this sets the field to the default font size}
    while Memo2.CalcWidth > Memo2.width do Memo2.Font.Size := Memo2.Font.Size-1;
    end;

    The trick, of course, is the CalcWidth function in the component. It figures out the width needed to fit the contents of the memo's text. With this function there, don't have to attempt using the text rectangle or canvas directly.

    Rick Brodzinsky

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.