Recent

Author Topic: Slow Canvas.TextRect (TsWorksheetGrid)  (Read 3482 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Slow Canvas.TextRect (TsWorksheetGrid)
« on: October 22, 2016, 12:18:45 am »
Hello guys, follow my function that draws on one of the TsWorksheetGrid cells with colored background and a lot of text. If the text is short the program is fast changing in the cell. If the text is long time to change from one cell to another it is long (up to 2 seconds). Can anyone tell me why?

Code: Pascal  [Select][+][-]
  1. procedure TAppuntamentiGiornalieri.InserisciTestoSplittato(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState; stepRow: integer; indice: integer; LarghezzaCella: integer; Shield: boolean);
  2. var
  3.   i, x, y: Integer;
  4.   txtStyle, appStyle : TTextStyle;
  5.   Testo: string;
  6. begin
  7.  
  8.   //Vado ad inserire il testo
  9.   with sender as TsWorksheetGrid do
  10.   begin
  11.  
  12.     appStyle := Canvas.TextStyle;
  13.     appStyle.Wordbreak:=true;
  14.     appStyle.SingleLine:=false;
  15.     appStyle.Alignment:=taLeftJustify;
  16.  
  17.     Canvas.Font.Name := 'Courier New';
  18.     Canvas.Font.Color := clBlack; //clSilver;
  19.     Canvas.Font.Style := [];
  20.     Canvas.Font.Height := 12;
  21.  
  22.     y:=aRect.Top;
  23.     if LarghezzaCella>20 then
  24.        x:=aRect.Left + 20 //il + 20 è lo spazio dal bordo sinistro perchè il testo deve iniziare dopo la barra colorata
  25.     else
  26.         x:=aRect.Left + 1;
  27.  
  28.     // queste due righe mi permettono di scrivere sul blocco verde senza togliermi gli angoli arrotondati
  29.     txtStyle := Canvas.TextStyle;
  30.     txtStyle.Opaque:=True;
  31.  
  32.     //Inserisco il testo
  33.     Testo:=Self.Dati[indice].TestoAssemblato;
  34.     if Shield=true then
  35.        Testo := TrasformaTesto(Testo);
  36.  
  37.     Canvas.TextRect(aRect, x, y, Self.GetTesto(indice, Shield), appStyle);
  38.  
  39.     //reimposto lo stile precedente all'inserimento del testo
  40.     Canvas.TextStyle := txtStyle;
  41.  
  42.   end;
  43. end;
  44.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: Slow Canvas.TextRect (TsWorksheetGrid)
« Reply #1 on: October 22, 2016, 08:25:04 am »
Maybe Self.GetTesto is slow?
Conscience is the debugger of the mind

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Slow Canvas.TextRect (TsWorksheetGrid)
« Reply #2 on: October 22, 2016, 10:09:59 am »
No! Also this is slow (direct string)

Canvas.TextRect(aRect, x, y, 'ciao come stai tante belle parole che non sono coerenti, ciao caio sempronio, servo solo a fare una stringa discretamente lunga...porca paletta', appStyle);
 

Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Slow Canvas.TextRect (TsWorksheetGrid)
« Reply #3 on: October 22, 2016, 10:23:15 am »
I understood what makes the slow Canvas.TextRect!

It 'option

appStyle.Wordbreak:=true;

In fact, if the comments performance improves. However, if one needs to go to the head? How can it be done?

//appStyle.Wordbreak:=true;
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Slow Canvas.TextRect (TsWorksheetGrid)
« Reply #4 on: October 22, 2016, 11:53:51 am »
What I don't understand is why you are working with TextStyle.Wordbreak and Fonts at all? All these are built into the WorksheetGrid, you just have to activate the options for the particular cell (see http://wiki.lazarus.freepascal.org/TsWorksheetGrid#Cell_formatting):

Code: Pascal  [Select][+][-]
  1.   WorksheetGrid.WordWrap[gridCol, gridRow] := true;
  2.   WorksheetGrid.CellFontName[gridCol, gridRow] := 'Courier'
  3.   WorksheetGrid.CellFontSize[gridCol, gridRow] := 12;
  4.   WorksheetGrid.CellFontColor[gridCol, gridRow] := clSilver;  

Could you condense the issue into a simple compilable project so that I can have a look?

 

TinyPortal © 2005-2018