Преглед изворни кода

Draw always with size parameters, rename GetScaledSize, fix button glyph

Johann ELSASS пре 5 година
родитељ
комит
dd855a941f
3 измењених фајлова са 53 додато и 71 уклоњено
  1. 27 49
      bgrasvgimagelist.pas
  2. 20 22
      bgrasvgtheme.pas
  3. 6 0
      bgratheme.pas

+ 27 - 49
bgrasvgimagelist.pas

@@ -41,7 +41,7 @@ type
     procedure Remove(AIndex: integer);
     procedure Exchange(AIndex1, AIndex2: integer);
     procedure Replace(AIndex: integer; ASVG: string);
-    function GetSize(ATargetDPI: integer): TSize;
+    function GetScaledSize(ATargetDPI: integer): TSize;
     // Get TBGRABitmap with custom width and height
     function GetBGRABitmap(AIndex: integer; AWidth, AHeight: integer): TBGRABitmap; overload;
     function GetBGRABitmap(AIndex: integer; AWidth, AHeight: integer;
@@ -50,15 +50,6 @@ type
     function GetBitmap(AIndex: integer; AWidth, AHeight: integer): TBitmap; overload;
     function GetBitmap(AIndex: integer; AWidth, AHeight: integer;
       AUseSVGAlignment: boolean): TBitmap; overload;
-    // Draw image with default width and height scaled to the DPI of the control.
-    procedure Draw(AIndex: integer; AControl: TControl; ACanvas: TCanvas; ALeft, ATop: integer); overload;
-    procedure Draw(AIndex: integer; AControl: TControl; ACanvas: TCanvas; ALeft, ATop: integer;
-      AUseSVGAlignment: boolean; AOpacity: byte = 255); overload;
-    // Draw image with default width and height scaled to a custom DPI in LCL coordinates.
-    procedure Draw(AIndex: integer; AControl: TControl; ATargetDPI: integer;
-      ACanvas: TCanvas; ALeft, ATop: integer); overload;
-    procedure Draw(AIndex: integer; AControl: TControl; ATargetDPI: integer; ACanvas: TCanvas;
-      ALeft, ATop: integer; AUseSVGAlignment: boolean; AOpacity: byte = 255); overload;
     // Draw image with custom width and height. The Width and
     // Height property are in LCL coordinates.
     procedure Draw(AIndex: integer; AControl: TControl; ACanvas: TCanvas;
@@ -74,6 +65,10 @@ type
     procedure Draw(AIndex: integer; ACanvasScale: single; ACanvas: TCanvas;
       ALeft, ATop, AWidth, AHeight: integer; AUseSVGAlignment: boolean;
       AOpacity: byte = 255); overload;
+    // Draw on the target BGRABitmap with specified Width and Height.
+    procedure Draw(AIndex: integer; ABitmap: TBGRABitmap; const ARectF: TRectF); overload;
+    procedure Draw(AIndex: integer; ABitmap: TBGRABitmap; const ARectF: TRectF;
+      AUseSVGAlignment: boolean); overload;
 
     // Generate bitmaps for an image list
     procedure PopulateImageList(const AImageList: TImageList; AWidths: array of integer);
@@ -229,7 +224,7 @@ begin
   Result := FItems.Count;
 end;
 
-function TBGRASVGImageList.GetSize(ATargetDPI: integer): TSize;
+function TBGRASVGImageList.GetScaledSize(ATargetDPI: integer): TSize;
 begin
   result.cx := MulDiv(Width, ATargetDPI, ReferenceDPI);
   result.cy := MulDiv(Height, ATargetDPI, ReferenceDPI);
@@ -278,38 +273,6 @@ begin
   ms.Free;
 end;
 
-procedure TBGRASVGImageList.Draw(AIndex: integer; AControl: TControl;
-  ACanvas: TCanvas; ALeft, ATop: integer);
-begin
-  Draw(AIndex, AControl, ACanvas, ALeft, ATop, UseSVGAlignment);
-end;
-
-procedure TBGRASVGImageList.Draw(AIndex: integer; AControl: TControl; ACanvas: TCanvas;
-  ALeft, ATop: integer; AUseSVGAlignment: boolean; AOpacity: byte);
-var
-  parentForm: TCustomForm;
-  targetDPI: Integer;
-begin
-  parentForm := GetParentForm(AControl);
-  if parentForm <> nil then targetDPI := parentForm.PixelsPerInch
-  else targetDPI := Screen.PixelsPerInch;
-  Draw(AIndex, AControl, targetDPI, ACanvas, ALeft, ATop, AUseSVGAlignment, AOpacity);
-end;
-
-procedure TBGRASVGImageList.Draw(AIndex: integer; AControl: TControl;
-  ATargetDPI: integer; ACanvas: TCanvas; ALeft, ATop: integer);
-begin
-  Draw(AIndex, AControl, ATargetDPI, ACanvas, ALeft, ATop, UseSVGAlignment);
-end;
-
-procedure TBGRASVGImageList.Draw(AIndex: integer; AControl: TControl;
-  ATargetDPI: integer; ACanvas: TCanvas; ALeft, ATop: integer;
-  AUseSVGAlignment: boolean; AOpacity: byte);
-begin
-  with GetSize(ATargetDPI) do
-    Draw(AIndex, AControl, ACanvas, ALeft, ATop, cx, cy, AUseSVGAlignment, AOpacity);
-end;
-
 procedure TBGRASVGImageList.Draw(AIndex: integer; AControl: TControl;
   ACanvas: TCanvas; ALeft, ATop, AWidth, AHeight: integer);
 begin
@@ -333,20 +296,35 @@ procedure TBGRASVGImageList.Draw(AIndex: integer; ACanvasScale: single; ACanvas:
   ALeft, ATop, AWidth, AHeight: integer; AUseSVGAlignment: boolean; AOpacity: byte);
 var
   bmp: TBGRABitmap;
-  svg: TBGRASVG;
 begin
   if (AWidth = 0) or (AHeight = 0) or (ACanvasScale = 0) then
     Exit;
   bmp := TBGRABitmap.Create(round(AWidth * ACanvasScale), round(AHeight * ACanvasScale));
-  svg := TBGRASVG.CreateFromString(FItems[AIndex].Text);
   try
-    if AUseSVGAlignment then
-      svg.StretchDraw(bmp.Canvas2D, 0, 0, bmp.Width, bmp.Height, true)
-      else svg.StretchDraw(bmp.Canvas2D, HorizontalAlignment, VerticalAlignment, 0, 0, bmp.Width, bmp.Height);
+    Draw(AIndex, bmp, rectF(0, 0, bmp.Width, bmp.Height), AUseSVGAlignment);
     bmp.ApplyGlobalOpacity(AOpacity);
     bmp.Draw(ACanvas, RectWithSize(ALeft, ATop, AWidth, AHeight), False);
   finally
     bmp.Free;
+  end;
+end;
+
+procedure TBGRASVGImageList.Draw(AIndex: integer; ABitmap: TBGRABitmap; const ARectF: TRectF);
+begin
+  Draw(AIndex, ABitmap, ARectF, UseSVGAlignment);
+end;
+
+procedure TBGRASVGImageList.Draw(AIndex: integer; ABitmap: TBGRABitmap; const ARectF: TRectF;
+  AUseSVGAlignment: boolean);
+var
+  svg: TBGRASVG;
+begin
+  svg := TBGRASVG.CreateFromString(FItems[AIndex].Text);
+  try
+    if AUseSVGAlignment then
+      svg.StretchDraw(ABitmap.Canvas2D, ARectF, true)
+      else svg.StretchDraw(ABitmap.Canvas2D, HorizontalAlignment, VerticalAlignment, ARectF);
+  finally
     svg.Free;
   end;
 end;
@@ -361,7 +339,7 @@ begin
   AImageList.Height := MulDiv(AWidths[0], Height, Width);
   AImageList.Scaled := True;
   AImageList.RegisterResolutions(AWidths);
-  SetLength(arr, Length(AWidths));
+  SetLength({%H-}arr, Length(AWidths));
   for j := 0 to Count - 1 do
   begin
     for i := 0 to Length(arr) - 1 do

+ 20 - 22
bgrasvgtheme.pas

@@ -598,13 +598,12 @@ procedure TBGRASVGTheme.DrawButton(Caption: string;
   ASurface: TBGRAThemeSurface; AImageIndex: Integer;
   AImageList: TBGRASVGImageList);
 var
-  Style: TTextStyle;
-  svg, glyph: TBGRASVG;
-  r: TRect;
+  svg: TBGRASVG;
   svgCode: String;
-  gw: integer;
-  tw: integer;
-  x: integer;
+  gs: TSize;
+  gw, tw, gx: integer;
+  style: TTextStyle;
+  r: TRect;
   drawText: boolean = True;
 begin
   with ASurface do
@@ -628,23 +627,24 @@ begin
           svg := TBGRASVG.CreateFromString(FButtonNormal.Text);
     end;
     SliceScalingDraw(svg, FButtonSliceScalingLeft, FButtonSliceScalingTop,
-      FButtonSliceScalingRight, FButtonSliceScalingBottom, ASurface.Bitmap,
+      FButtonSliceScalingRight, FButtonSliceScalingBottom, Bitmap,
       BitmapDPI);
     svg.Free;
     gw := 0;
     tw := DestCanvas.TextWidth(Caption);
     if Assigned(AImageList) and (AImageIndex > -1) and (AImageIndex < AImageList.Count) then
     begin
-      gw := ScaleForCanvas(AImageList.Width) + ScaleForCanvas(8);
-      x := (Bitmap.Width - tw - ScaleForCanvas(AImageList.Width)) div 2;
-      if (x < ScaleForCanvas(8)) then
+      gs := AImageList.GetScaledSize(BitmapDPI);
+      gw := gs.cx + ScaleForBitmap(8);
+      gx := (Bitmap.Width - ScaleForBitmap(tw, DestCanvasDPI) - gw) div 2;
+      if (gx < ScaleForBitmap(8)) then
       begin
-        x := (Bitmap.Width - ScaleForCanvas(AImageList.Width)) div 2;
+        gx := (Bitmap.Width - gs.Width) div 2;
         drawText := False;
       end;
-      glyph := TBGRASVG.CreateFromString(AImageList.SVGString[AImageIndex]);
-      glyph.StretchDraw(Bitmap.Canvas2D, x, (Bitmap.Height - ScaleForCanvas(AImageList.Height)) div 2, ScaleForCanvas(AImageList.Width), ScaleForCanvas(AImageList.Height), True);
-      glyph.Free;
+      AImageList.Draw(AImageIndex, Bitmap,
+        RectWithSizeF(gx, (Bitmap.Height - gs.cy) div 2, gs.cx, gs.cy));
+      gw := ScaleForCanvas(gw, BitmapDPI);
     end;
     ColorizeSurface(ASurface, State);
     DrawBitmap;
@@ -665,14 +665,12 @@ begin
 
     if (Caption <> '') and drawText then
     begin
-      Style.Alignment := taCenter;
-      Style.Layout := tlCenter;
-      Style.Wordbreak := True;
-      Style.SystemFont := False;
-      Style.Clipping := True;
-      Style.Opaque := False;
-      ARect.Left := gw;
-      DestCanvas.TextRect(ARect, 0, 0, Caption, Style);
+      fillchar(style, sizeof(style), 0);
+      style.Alignment := taCenter;
+      style.Layout := tlCenter;
+      style.Wordbreak := True;
+      inc(ARect.Left, gw);
+      DestCanvas.TextRect(ARect, 0, 0, Caption, style);
     end;
   end;
 end;

+ 6 - 0
bgratheme.pas

@@ -32,6 +32,7 @@ type
     procedure DiscardBitmap;
     procedure BitmapColorOverlay(AColor: TBGRAPixel; AOperation: TBlendOperation = boTransparent); overload;
     function ScaleForCanvas(AValue: integer; AFromDPI: integer = 96): integer;
+    function ScaleForBitmap(AValue: integer; AFromDPI: integer = 96): integer;
     property DestCanvas: TCanvas read FDestCanvas;
     property DestCanvasDPI: integer read FLclDPI;
     property Bitmap: TBGRABitmap read GetBitmap;
@@ -187,6 +188,11 @@ begin
   result := MulDiv(AValue, DestCanvasDPI, AFromDPI);
 end;
 
+function TBGRAThemeSurface.ScaleForBitmap(AValue: integer; AFromDPI: integer): integer;
+begin
+  result := MulDiv(AValue, BitmapDPI, AFromDPI);
+end;
+
 { TBGRATheme }
 
 function TBGRATheme.GetThemedControl(AIndex: integer): TBGRAThemeControl;