From ea421f483db0bf007cbeda0c8022144d1d2a13f9 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 8 Mar 2017 15:24:00 +0000 Subject: [PATCH] fpspreadsheet: Fix mis-positioning of images read from ods files in WorksheetGrid git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5801 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/common/fpsopendocument.pas | 2 +- .../source/common/fpspreadsheet.pas | 27 +++++++++++++++++ .../source/visual/fpspreadsheetgrid.pas | 30 +++++++++---------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/components/fpspreadsheet/source/common/fpsopendocument.pas b/components/fpspreadsheet/source/common/fpsopendocument.pas index cf4bd5bcc..84f4aae1b 100644 --- a/components/fpspreadsheet/source/common/fpsopendocument.pas +++ b/components/fpspreadsheet/source/common/fpsopendocument.pas @@ -3897,7 +3897,7 @@ begin begin idx := FWorkbook.FindEmbeddedObj(ExtractFileName(href)); FWorksheet.CalcImageCell(idx, x, y, w, h, r, c, dr, dc, sx, sy); - FWorksheet.WriteImage(r, c, idx, dr, dc, sx, sy); + FWorksheet.WriteImage(r, c, idx, dc, dr, sx, sy); // order of dc and dr is correct! end; childShapeNode := childShapeNode.NextSibling; end; diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index 614aa8356..bc3a24547 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -544,6 +544,7 @@ type out x, y, AWidth, AHeight: Double); function GetImage(AIndex: Integer): TsImage; function GetImageCount: Integer; + function GetPointerToImage(AIndex: Integer): PsImage; procedure RemoveAllImages; procedure RemoveImage(AIndex: Integer); function WriteImage(ARow, ACol: Cardinal; AFileName: String; @@ -3764,6 +3765,11 @@ begin Result := img^; end; +function TsWorksheet.GetPointerToImage(AIndex: Integer): PsImage; +begin + Result := PsImage(FImages[AIndex]); +end; + {@@ ---------------------------------------------------------------------------- Returns the count of images that are embedded into this sheet. -------------------------------------------------------------------------------} @@ -3781,6 +3787,15 @@ var begin ACol := 0; sum := 0; + colW := GetColWidth(0, FWorkbook.Units); + while (sum + colW < x) do begin + sum := sum + colW; + inc(ACol); + colW := GetColWidth(ACol, FWorkbook.Units); + end; + AColOffs := x - sum; + { + sum := 0; repeat colW := GetColWidth(ACol, FWorkbook.Units);; sum := sum + colW; @@ -3789,7 +3804,18 @@ begin sum := sum - colW; AColOffs := x - sum; dec(ACol); + } + ARow := 0; + sum := 0; + rowH := CalcRowHeight(0); + while (sum + rowH < y) do begin + sum := sum + rowH; + inc(ARow); + rowH := CalcRowHeight(ARow); + end; + ARowOffs := y - sum; + { ARow := 0; sum := 0; repeat @@ -3800,6 +3826,7 @@ begin sum := sum - rowH; ARowOffs := y - sum; dec(ARow); + } embObj := FWorkbook.GetEmbeddedObj(AIndex); AScaleX := AWidth / embObj.ImageWidth; diff --git a/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas b/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas index 3e5225932..40f015644 100644 --- a/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas +++ b/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas @@ -2180,7 +2180,7 @@ procedure TsCustomWorksheetGrid.DrawImages; var i: Integer; - img: TsImage; + img: PsImage; obj: TsEmbeddedObj; clipArea, imgRect, R: TRect; w, h: Integer; @@ -2192,34 +2192,34 @@ begin ColRowToOffset(false, false, HeaderCount, clipArea.Top, tmp); for i := 0 to Worksheet.GetImageCount-1 do begin - img := Worksheet.GetImage(i); - obj := Workbook.GetEmbeddedObj(img.Index); + img := Worksheet.GetPointerToImage(i); + obj := Workbook.GetEmbeddedObj(img^.Index); - w := ToPixels(obj.ImageWidth * img.ScaleX); - h := ToPixels(obj.ImageHeight * img.ScaleY); + w := ToPixels(obj.ImageWidth * img^.ScaleX); + h := ToPixels(obj.ImageHeight * img^.ScaleY); - imgRect := CellRect(img.Col + HeaderCount, img.Row + HeaderCount); + imgRect := CellRect(img^.Col + HeaderCount, img^.Row + HeaderCount); imgRect.Right := imgRect.Left + w; imgRect.Bottom := imgRect.Top + h; - OffsetRect(imgRect, ToPixels(img.OffsetX), ToPixels(img.OffsetY)); + OffsetRect(imgRect, ToPixels(img^.OffsetX), ToPixels(img^.OffsetY)); if not IntersectRect(R, clipArea, imgRect) then continue; - if img.Bitmap = nil then begin + if img^.Bitmap = nil then begin // Load image into bitmap and scale to required size - img.Bitmap := TBitmap.Create; - TBitmap(img.Bitmap).SetSize(w, h); - TBitmap(img.Bitmap).PixelFormat := pf32Bit; - TBitmap(img.Bitmap).Transparent := true; + img^.Bitmap := TBitmap.Create; + TBitmap(img^.Bitmap).SetSize(w, h); + TBitmap(img^.Bitmap).PixelFormat := pf32Bit; + TBitmap(img^.Bitmap).Transparent := true; pic := TPicture.Create; try obj.Stream.Position := 0; pic.LoadFromStream(obj.Stream); if pic.Bitmap <> nil then - TBitmap(img.Bitmap).Canvas.StretchDraw(Rect(0, 0, w, h), pic.Bitmap) + TBitmap(img^.Bitmap).Canvas.StretchDraw(Rect(0, 0, w, h), pic.Bitmap) else if pic.Graphic <> nil then - TBitmap(img.Bitmap).Canvas.StretchDraw(Rect(0, 0, w, h), pic.Graphic); + TBitmap(img^.Bitmap).Canvas.StretchDraw(Rect(0, 0, w, h), pic.Graphic); finally pic.Free; end; @@ -2229,7 +2229,7 @@ begin Canvas.SaveHandleState; try InterSectClipRect(Canvas.Handle, R.Left, R.Top, R.Right, R.Bottom); - Canvas.Draw(imgRect.Left, imgRect.Top, TBitmap(img.Bitmap)); + Canvas.Draw(imgRect.Left, imgRect.Top, TBitmap(img^.Bitmap)); finally Canvas.RestoreHandleState; end;