From 3c11c8c7d0a323cc02e56fee4266a512aaafd8ef Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Tue, 3 May 2022 11:46:11 +0000 Subject: [PATCH] LazMapViewer: Extend fulldemo to show how a map can be printed. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8273 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/fulldemo/MapViewer_Demo.lpi | 9 +- .../examples/fulldemo/MapViewer_Demo.lpr | 2 +- .../lazmapviewer/examples/fulldemo/main.lfm | 20 +++++ .../lazmapviewer/examples/fulldemo/main.pas | 84 ++++++++++++++++++- 4 files changed, 110 insertions(+), 5 deletions(-) diff --git a/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpi b/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpi index 503b9d767..398c1e6c2 100644 --- a/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpi +++ b/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpi @@ -25,13 +25,16 @@ - + - + - + + + + diff --git a/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpr b/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpr index 773f46c9b..81fbb649d 100644 --- a/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpr +++ b/components/lazmapviewer/examples/fulldemo/MapViewer_Demo.lpr @@ -5,7 +5,7 @@ program MapViewer_Demo; uses {$IFDEF UNIX}cthreads,{$ENDIF} Interfaces, // this includes the LCL widgetset - Forms, Main, gpslistform, globals, gpsptform + Forms, printer4lazarus, Main, gpslistform, globals, gpsptform { you can add units after this }; {$R *.res} diff --git a/components/lazmapviewer/examples/fulldemo/main.lfm b/components/lazmapviewer/examples/fulldemo/main.lfm index 382a0e2dd..2040f581c 100644 --- a/components/lazmapviewer/examples/fulldemo/main.lfm +++ b/components/lazmapviewer/examples/fulldemo/main.lfm @@ -536,6 +536,22 @@ object MainForm: TMainForm OnClick = BtnLoadGPXFileClick TabOrder = 8 end + object BtnPrintMap: TButton + AnchorSideLeft.Control = BtnSaveToFile + AnchorSideTop.Control = BtnSaveToFile + AnchorSideTop.Side = asrBottom + AnchorSideRight.Control = BtnSaveToFile + AnchorSideRight.Side = asrBottom + Left = 6 + Height = 25 + Top = 550 + Width = 110 + Anchors = [akTop, akLeft, akRight] + BorderSpacing.Top = 8 + Caption = 'Print...' + OnClick = BtnPrintMapClick + TabOrder = 9 + end end object PgConfig: TTabSheet Caption = 'Config' @@ -1255,4 +1271,8 @@ object MainForm: TMainForm 69754D32C924FBBF68FF0B9EFE66B6 } end + object PrintDialog1: TPrintDialog + Left = 552 + Top = 567 + end end diff --git a/components/lazmapviewer/examples/fulldemo/main.pas b/components/lazmapviewer/examples/fulldemo/main.pas index 39607626f..f201d2b42 100644 --- a/components/lazmapviewer/examples/fulldemo/main.pas +++ b/components/lazmapviewer/examples/fulldemo/main.pas @@ -6,7 +6,7 @@ interface uses Classes, SysUtils, Types, Forms, Controls, Graphics, Dialogs, - ExtCtrls, StdCtrls, ComCtrls, Buttons, IntfGraphics, ColorBox, + ExtCtrls, StdCtrls, ComCtrls, Buttons, IntfGraphics, ColorBox, PrintersDlgs, mvGeoNames, mvMapViewer, mvTypes, mvGpsObj, mvDrawingEngine; type @@ -21,6 +21,7 @@ type BtnSaveToFile: TButton; BtnLoadGPXFile: TButton; BtnPOITextFont: TButton; + BtnPrintMap: TButton; CbDoubleBuffer: TCheckBox; CbFoundLocations: TComboBox; CbLocations: TComboBox; @@ -63,10 +64,12 @@ type PgData: TTabSheet; PgConfig: TTabSheet; POIImages: TImageList; + PrintDialog1: TPrintDialog; rgPOIMode: TRadioGroup; ZoomTrackBar: TTrackBar; procedure BtnGoToClick(Sender: TObject); procedure BtnLoadGPXFileClick(Sender: TObject); + procedure BtnPrintMapClick(Sender: TObject); procedure BtnSearchClick(Sender: TObject); procedure BtnGPSPointsClick(Sender: TObject); procedure BtnSaveToFileClick(Sender: TObject); @@ -122,6 +125,7 @@ implementation uses LCLType, IniFiles, Math, FPCanvas, FPImage, GraphType, + Printers, OSPrinters, mvEngine, mvGPX, globals, gpsPtForm, gpslistform; @@ -233,6 +237,84 @@ begin end; end; +procedure TMainForm.BtnPrintMapClick(Sender: TObject); +const + INCH = 25.4; // 1" = 25.4 mm + HOR_MARGIN = 10.0; // mm + VERT_MARGIN = 10.0; // mm + + function px2mm(px, ppi: Integer): Double; + begin + Result := px / ppi * INCH; + end; + + function mm2px(mm: Double; ppi: Integer): Integer; + begin + Result := round(mm / INCH * ppi); + end; + +var + bmp: TBitmap; + PrintedRect: TRect; + aspectRatio_map: Double; + aspectRatio_page: Double; + mapWidth_mm: Double; + mapHeight_mm: Double; + pageWidth_mm: Double; + pageHeight_mm: Double; + printedWidth_mm: Double; + printedHeight_mm: Double; + factor: Double; +begin + if PrintDialog1.Execute then + begin + Printer.Orientation := poPortrait; + + // Scale map such that it fits into the width of the printer paper with + // a margin at all sides + mapWidth_mm := px2mm(MapView.Width, ScreenInfo.PixelsPerInchX); + mapHeight_mm := px2mm(MapView.Height, ScreenInfo.PixelsPerInchY); + pageWidth_mm := px2mm(Printer.PageWidth, Printer.XDPI) - 2*HOR_MARGIN; + pageHeight_mm := px2mm(Printer.PageHeight, Printer.YDPI) - 2*VERT_MARGIN; + aspectRatio_map := mapHeight_mm / mapWidth_mm; + aspectRatio_page := pageHeight_mm / pageWidth_mm; + if aspectRatio_Map > aspectRatio_page then + begin + factor := pageHeight_mm / mapHeight_mm; + printedWidth_mm := mapWidth_mm * factor; + printedHeight_mm := pageHeight_mm; + end else + begin + factor := pageWidth_mm / mapWidth_mm; + printedWidth_mm := pageWidth_mm; + printedHeight_mm := mapHeight_mm * factor; + end; + // Calculate size of the output rectangle (on paper). + PrintedRect := Rect(0, 0, mm2px(printedWidth_mm, Printer.XDPI), mm2px(printedHeight_mm, Printer.YDPI)); + // Center output rectangle horizontally on page + OffsetRect(PrintedRect, mm2px(HOR_MARGIN + (pageWidth_mm-printedWidth_mm) / 2 , Printer.XDPI), mm2px(VERT_MARGIN, Printer.YDPI)); + + // Begin printing + Printer.BeginDoc; + try + // Catch the displayed images as a bitmap. + bmp := TBitmap.Create; + try + bmp.SetSize(MapView.Width, MapView.Height); + MapView.DrawingEngine.PaintToCanvas(bmp.Canvas); + + // Stretch-draw the map image to the output rectangle on the printer. + Printer.Canvas.StretchDraw(PrintedRect, bmp); + finally + bmp.Free; + end; + + finally + Printer.EndDoc; + end; + end; +end; + procedure TMainForm.BtnSaveToFileClick(Sender: TObject); begin MapView.SaveToFile(TPortableNetworkGraphic, 'mapview.png');