Forum > Beginners

[Resolved] split a text with accent

(1/3) > >>

Nono Reading Activity:
Hello
i want to split a text into two text before writing it into a pdf


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---        Ligne1 := leftStr(Texte, Ceil(Length(Texte) * prorata5));Ligne2 := Trim(RightStr(Texte, Length(Texte) - Length(Ligne1))); PdfPage.WriteText(Min, Y+1, Ligne1);PdfPage.WriteText(Min, Y-1, '  '+Ligne2);     
i get that result. the "é" is split into #$C3 and #$A9


--- Quote ---Texte @$000000000913E648 = $000000000913E648^: '¤Jambières d’acier|Protection : 2|Encombrement : 3|Emplacement : Jambe|Bonus : Impénétrable,Points faibles,Peu discret,Peu discret'
Ligne1 @$000000000816EF88 = $000000000816EF88^: '¤Jambières d’acier|Protection : 2|Encombrement : 3|Emplacement : Jambe|Bonus : Impén'#$C3
Ligne2 @$00000000081D1BA8 = $00000000081D1BA8^: #$A9'trable,Points faibles,Peu discret,Peu discret'

--- End quote ---

what should i change to avoid it ?

jamie:
I can offer you some suggestions.

You can assign your string to a Unicode32 type which is fixed size per char. From that, you can properly index it.

Or you can use the LazUtf8 unit which has Utf8Copy, ...Pos, etc.

wp:
You do not specify the criteria how the input string should be split. But in your sample I see that the input string contains several parts separated by the '|' character. Therfore I guess: Do you want to split at the '|'?

In this case: simply use the string helper s.Split('|') where s is the input string and the result is a TStringArray (array of string) (or use the function SplitString which does the same (https://www.freepascal.org/docs-html/rtl/strutils/splitstring.html).

Here as an example (the input string is in the Caption of Label1, and the split parts are displayed in a Memo):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.BitBtn1Click(Sender: TObject);var  sa: TStringArray;  i: Integer;begin  sa := String(Label1.Caption).Split('|');   // Cast the Caption to string to get access to the StringHelper. Or use: sa := SplitString(Label1.Caption, '|'), in unit StrUtils).  Memo1.Lines.Clear;  for i := 0 to High(sa) do    Memo1.Lines.Add(sa[i]);end;
Of course, your question mentions two parts - but my solution results in five... Maybe you want to split the string at a given fraction of its length? (What is prorata5? The fraction of the entire length where the split should be made?)

In this case you must work with UTF8Length which counts the "characters" (rather than Length which counts the bytes) and UTF8Copy rather than Copy. These are in unit LazUTF8 which, BTW, contains also a UTF8LeftStr and a UTF8RightStr.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  LazUTF8; procedure TForm1.Button1Click(Sender: TObject);const  prorata5 = 0.64;var  s1, s2: STring;  p: Integer;begin  p := round(UTF8Length(Label1.Caption) * prorata5);  s1 := UTF8Copy(Label1.Caption, 1, p);  s2 := UTF8Copy(Label1.Caption, p+1, MaxInt);  Memo1.Lines.Add(s1);  Memo1.Lines.Add(s2); end;
I hope that you see in this answer that you always must precisely describe the issue. Otherwise we can only wildly guess what you mean.

Nono Reading Activity:
sorry, now i put the full code to explain my problem
i want to split my line into two if i can't write everything on the same line (if changing the font size doesn't resolve it)
Should i convert every string to Unicode32 or UTF8 when working with PDF ?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function TailleTexte(Texte: String): TPDFFloat;  var    PosTemp:      TPDFFloat;    lFC:          TFPFontCacheItem;  Begin    lFC     := gTTFontCache.Find(PdfFamilyName, PdfIsBold, PdfIsItalic);    PosTemp := PDFTomm(lFC.TextWidth(Texte, PdfFontTaille));    Result  := PosTemp;  end; Procedure PdfEcrit(PdfPage: TPDFPage; Min: Single; Max: Single; Y: Single; Texte: String; MinPolice: Integer);  var    TailleTemp:   Integer = 0;    Prorata:      Single;    DeuxLignes:   Boolean = false;    Ligne1:       String;    Ligne2:       String;    Prorata5:     Single;    PosTemp:      TPDFFloat;   BEgin    PosTemp := TailleTexte(Texte);    if PosTemp > (Max - Min) then      begin        Prorata    := (Max - Min) / (PosTemp);        Tailletemp := Ceil(PdfFontTaille * Prorata);        if Tailletemp = PdfFontTaille then          Tailletemp := Tailletemp - 1;         if TailleTemp < MinPolice then          begin            Prorata5   := TailleTemp / MinPolice;            TailleTemp := MinPolice;            DeuxLignes := true;          end;        if TailleTemp <> PdfFontTaille then          PdfPage.SetFont(PdfFontEnCours, Tailletemp);      end;     if DeuxLignes = true then      begin        Ligne1 := leftStr(Texte, Ceil(Length(Texte) * prorata5));        Ligne2 := Trim(RightStr(Texte, Length(Texte) - Length(Ligne1)));        Ligne1 := Trim(Ligne1);        PdfPage.WriteText(Min, Y+1, Ligne1);        PdfPage.WriteText(Min, Y-1, '  '+Ligne2);      end    else      PdfPage.WriteText(Min, Y, Texte);    if TailleTemp <> PdfFontTaille then      PdfPage.SetFont(PdfFontEnCours, PdfFontTaille);  end;

jamie:
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtextw

Maybe this is of some interest to you ?

The Canvas has also the same abilities.

With this, you can calculate the box needed to contain a text.

I believe in the LCLintf unit would be the ported equal function.


Navigation

[0] Message Index

[#] Next page

Go to full version