• Hello

    I'm working now with WPTools 7, the problem was in WPTools 5 also.

    I have to print tables, with different sum-rows and I optimize the place between the columns. Because of that I have since many years a own algorithm with word wrapping.

    All I need is a accurate text with.

    I have a cell or a paragraph, and the Font is set
    Now I want to know the textwith in twips.
    The only way I found was:

    Code
    mywidth := MulDiv(MyRichText.Canvas.TextWidth(s),1440,Screen.PixelsPerInch); //Twips;

    But than I have differences from 39% [/] to 20% [%] to my own measures. A normal C has 10% difference.

    I have tested with 40times a charater in a string, something like CCCC....CCCC (40ty C's).

    I cannot format the whole text, to caluclate one value.
    Is the anywhere a correct with, if I format only my paragraph/cell?

    Thanks for any help.

    Andreas

  • Zitat von wpsupport

    The paragraphs have an Array CharPos - that contains w elements which represent the width in Pixels measured in render resolution - Memo.CurrentXPixelsPerInch.

    I need the width before I put the text to the document.
    I can't format the whole document after each piece of text.

    Therefor I have a format Paragraph:

    Code
    cha := FWorkItem.rtf.AttrHelper;    cha.Clear;    cha.SetFontName('Arial');    cha.SetFontSize(8);    FWorkItem.formatPar := FWorkItem.rtf.ActiveText.AppendNewPar;    FWorkItem.formatPar.SetText('<undef>',cha.CharAttr);

    I use this to cut the text, in the place cw calculated before:

    Code
    FWorkItem.formatPar.setText(s);  w := 0; i:=1;  while (w<cw) and (i <= length(s)) do    begin    w := w + FWorkItem.formatPar.CharPos[i-1].width;  //(1)    i := i + 1;    end;  Result := Copy(s,1,i-1);

    After all I delete the Paragraph:

    Code
    FWorkItem.formatPar.DeleteParagraph;


    The problem is now: CharPos is empty LOW(CharPos) = 0, High(CharPos) = -1 and a GeneralProtectionFault in (1).

    Thanks for your help.

    Best Regards Andreas

    • Offizieller Beitrag

    To know the width before you put text into the document you can only use Canvas.TextWidth. WPTools is using a resolution of 600 by Standard, but using the FormatOptions you can select screen resolution, too.
    But I doubt the result will be a stable solution, just an estimation.

    You could work with 2 TWPRichText and use one to put in all your text and initialize (define CharPos) and format it. Then you can measure the text there and enter it to the other.

    Zitat

    I have to print tables, with different sum-rows and I optimize the place between the columns. Because of that I have since many years a own algorithm with word wrapping.


    You know that wptools supports footer rows? The footer rows can contain a text object which can be used to display the result of a calculation.

    Instead of deleting the paragraph I would delete the characters which do not fit, TParagraph has methods for this. Deleting a paragraph in a table could delete the cell.

  • Hello Julian,

    I changed my approach to use your format information. I work now with table and columns. I have set the columns that there is no line break, and everthing is disiplayed in one line.

    I believe I work in pts (1/1440 inch)

    But the results are crazy:

    First a picture:
    [Blockierte Grafik: http://www.autopoll.de/files/wptools.jpg]

    a=1014 is my internal size of the column, if I add all these values for the columns I have exakt the place between left and right margin. Therfore, that's ok.

    After showing the editor, I have for test purposal, a doubleclick event, than I search recursivly for the cell/paragraph and have a look.

    Code
    procedure TtgBaseWPReport.FindPar(par:TParagraph);...begin  while par <> nil do    begin    s := par.GetText();    b := par.TextAreaWidth; //Soll-Breite, der Zelle, Section oder Zeile    c := 0;    for i:=1 to length(s) do      c := c + par.CharPos[i-1].Width;    if c > b then      ...    FindPar(par.ChildPar);    par := par.NextPar;    end;end;

    b = 931 is the textwidth of the cell, ok a little bit smaller than a, because of margin, border or padding.

    but c = 542 can't be true. Because you can see in the picture, the text is to long.

    I have tried to work with a resolution of 600, your wrote in your last reply.

    Code
    c := c + Round(par.CharPos[i-1].Width * (1400/600))


    But then also c is to large.

    How can I calculate c?

    Thanks for your help.

    • Offizieller Beitrag

    I tested that so:

    c := 0;
    for i := 0 to WPRichText1.ActivePar.CharCount-1 do
    c := c + WPRichText1.ActivePar.CharPos[i].Width;
    ShowMessage( Format('%d pts', [ MulDiv(c , 1440, WPRichText1.Memo.CurrentXPixelsPerInch )] ));

    When the text is initialized this should work. I would continue with this with support mail - it does not fit into this forum.