Recent

Author Topic: Clean up at TFreeTypeRenderableFont.SplitText  (Read 804 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 390
Clean up at TFreeTypeRenderableFont.SplitText
« on: August 10, 2023, 11:54:01 am »
components/freetype/easylazfreetype.pas has
Code: Pascal  [Select][+][-]
  1. procedure TFreeTypeRenderableFont.SplitText(var AText: string;
  2.   AMaxWidth: single; out ARemains: string);
  3. var
  4.   pstr: pchar;
  5.   left,charlen: integer;
  6.   totalWidth: single;
  7.   firstChar: boolean;
  8.   glyphWidth: single;
  9.   glyphCode: cardinal;
  10.  
  11.   procedure WordBreak(ADropCount: Integer = 0);
  12.   begin
  13.     ARemains:= copy(AText, length(AText) - left + 1 + ADropCount, left);
  14.     SetLength(AText, length(AText) - left);
  15.   end;
  16.  
  17. begin
  18.   ARemains := '';
  19.   if AText = '' then
  20.     exit;
  21.   totalWidth := 0;
  22.   pstr := @AText[1];
  23.   left := length(AText);
  24.   firstChar := true;
  25.   while left > 0 do
  26.   begin
  27.     if pstr[0] in [#13, #10] then
  28.     begin
  29.       if (left > 1)  and ([pstr[0], pstr[1]] = [#13, #10]) then
  30.         WordBreak(2)
  31.       else
  32.         WordBreak(1);
  33.       exit;
  34.     end;
  35.  
  36.     charlen := UTF8CodepointSize(pstr);
  37.     glyphCode := UTF8CodepointToUnicode(pstr, charlen);
  38.     inc(pstr,charlen);
  39.  
  40.     glyphWidth := CharWidthFromUnicode(glyphCode);
  41.     if glyphWidth <> 0 then
  42.     begin
  43.       totalWidth += glyphWidth;
  44.       if (totalWidth > AMaxWidth) and not firstChar then
  45.       begin
  46.         WordBreak;
  47.         if Assigned(FWordBreakHandler) then
  48.           FWordBreakHandler(AText,ARemains)
  49.         else
  50.           DefaultWordBreakHandler(AText,ARemains);
  51.         exit;
  52.       end;
  53.     end;
  54.  
  55.     dec(left,charlen);
  56.     firstChar := false;
  57.   end;
  58.   ARemains := ''; //no split
  59. end;
The first and the last lines of the procedure are: ARemains := ''; and ARemains := ''; //no split
Between these two assignments, each and every time ARemains is modified, it is followed by an exit, making the last assignment line useless.
The following patch removes it.
Code: Pascal  [Select][+][-]
  1. diff --git a/components/freetype/easylazfreetype.pas b/components/freetype/easylazfreetype.pas
  2. index 27f7151674..5ba7762125 100644
  3. --- a/components/freetype/easylazfreetype.pas
  4. +++ b/components/freetype/easylazfreetype.pas
  5. @@ -596,7 +596,6 @@ begin
  6.      dec(left,charlen);
  7.      firstChar := false;
  8.    end;
  9. -  ARemains := ''; //no split
  10.  end;
  11.  
  12.  procedure TFreeTypeRenderableFont.GetTextSize(AText: string; out w, h: single);

AlexTP

  • Hero Member
  • *****
  • Posts: 2292
    • UVviewsoft
Re: Clean up at TFreeTypeRenderableFont.SplitText
« Reply #1 on: August 10, 2023, 12:01:28 pm »
Too micro change (for my taste). No overhead here.

 

TinyPortal © 2005-2018