OpenCPN Partial API docs
Loading...
Searching...
No Matches
printout_chart.cpp
1/***************************************************************************
2 * Copyright (C) 2010 by David S. Register *
3 * Copyright (C) 2025 by NoCodeHummel *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 **************************************************************************/
20
21#include "chcanv.h"
22#include "glChartCanvas.h"
23#include "ocpn_frame.h"
24#include "printout_chart.h"
25
26extern bool g_bopengl;
27extern MyFrame* gFrame;
28
29class ChartCanvas;
30ChartCanvas* GetFocusCanvas();
31
32void ChartPrintout::DrawPage(wxDC* dc, int page) {
33 // Get the Size of the Chart Canvas
34 int sx, sy;
35 gFrame->GetFocusCanvas()->GetClientSize(&sx, &sy); // of the canvas
36
37 float max_x = sx;
38 float max_y = sy;
39
40 // Let's have at least some device units margin
41 float margin_x = 50;
42 float margin_y = 50;
43
44 // Add the margin to the graphic size
45 max_x += (2 * margin_x);
46 max_y += (2 * margin_y);
47
48 // Get the size of the DC in pixels
49 int w, h;
50 dc->GetSize(&w, &h);
51
52 // Calculate a suitable scaling factor
53 float scale_x = (float)(w / max_x);
54 float scale_y = (float)(h / max_y);
55
56 // Use x or y scaling factor, whichever fits on the DC
57 float actual_scale = wxMin(scale_x, scale_y);
58
59 // Calculate the position on the DC for centring the graphic
60 float pos_x = (float)((w - (max_x * actual_scale)) / 2.0);
61 float pos_y = (float)((h - (max_y * actual_scale)) / 2.0);
62
63 pos_x = wxMax(pos_x, margin_x);
64 pos_y = wxMax(pos_y, margin_y);
65
66 // Set the scale and origin
67 dc->SetUserScale(actual_scale, actual_scale);
68 dc->SetDeviceOrigin((long)pos_x, (long)pos_y);
69
70 // Get the latest bitmap as rendered by the ChartCanvas
71
72 if (g_bopengl) {
73#ifdef ocpnUSE_GL
74 if (m_gl_bmp.IsOk()) {
75 wxMemoryDC mdc;
76 mdc.SelectObject(m_gl_bmp);
77 dc->Blit(0, 0, m_gl_bmp.GetWidth(), m_gl_bmp.GetHeight(), &mdc, 0, 0);
78 mdc.SelectObject(wxNullBitmap);
79 }
80#endif
81 } else {
82 // And Blit/scale it onto the Printer DC
83 wxMemoryDC mdc;
84 mdc.SelectObject(*(gFrame->GetFocusCanvas()->pscratch_bm));
85
86 dc->Blit(0, 0, gFrame->GetFocusCanvas()->pscratch_bm->GetWidth(),
87 gFrame->GetFocusCanvas()->pscratch_bm->GetHeight(), &mdc, 0, 0);
88
89 mdc.SelectObject(wxNullBitmap);
90 }
91}
92
94 if (g_bopengl) {
95#ifdef ocpnUSE_GL
96 int gsx = gFrame->GetFocusCanvas()->GetglCanvas()->GetSize().x;
97 int gsy = gFrame->GetFocusCanvas()->GetglCanvas()->GetSize().y;
98
99 unsigned char* buffer = (unsigned char*)malloc(gsx * gsy * 4);
100 glReadPixels(0, 0, gsx, gsy, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
101
102 unsigned char* e = (unsigned char*)malloc(gsx * gsy * 3);
103
104 if (buffer && e) {
105 for (int p = 0; p < gsx * gsy; p++) {
106 e[3 * p + 0] = buffer[4 * p + 0];
107 e[3 * p + 1] = buffer[4 * p + 1];
108 e[3 * p + 2] = buffer[4 * p + 2];
109 }
110 }
111 free(buffer);
112
113 wxImage image(gsx, gsy);
114 image.SetData(e);
115 wxImage mir_imag = image.Mirror(false);
116 m_gl_bmp = wxBitmap(mir_imag);
117#endif
118 }
119}
ChartCanvas - Main chart display and interaction component.
Definition chcanv.h:151
void GenerateGLbmp()
In OperGL mode, make the bitmap capture of the screen before the print method starts as to be sure th...
Main application frame.
Definition ocpn_frame.h:136