OpenCPN Partial API docs
Loading...
Searching...
No Matches
printout_chart.cpp
Go to the documentation of this file.
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, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#include <wx/dc.h>
26#include <wx/utils.h>
27
28#include "model/config_vars.h"
29
30#include "chcanv.h"
31#include "gl_chart_canvas.h"
32#include "ocpn_frame.h"
33#include "printout_chart.h"
34
35void ChartPrintout::DrawPage(wxDC* dc, int page) {
36 // Get the Size of the Chart Canvas
37 int sx, sy;
38 gFrame->GetFocusCanvas()->GetClientSize(&sx, &sy); // of the canvas
39
40 float max_x = sx;
41 float max_y = sy;
42
43 // Let's have at least some device units margin
44 float margin_x = 50;
45 float margin_y = 50;
46
47 // Add the margin to the graphic size
48 max_x += (2 * margin_x);
49 max_y += (2 * margin_y);
50
51 // Get the size of the DC in pixels
52 int w, h;
53 dc->GetSize(&w, &h);
54
55 // Calculate a suitable scaling factor
56 float scale_x = (float)(w / max_x);
57 float scale_y = (float)(h / max_y);
58
59 // Use x or y scaling factor, whichever fits on the DC
60 float actual_scale = wxMin(scale_x, scale_y);
61
62 // Calculate the position on the DC for centring the graphic
63 float pos_x = (float)((w - (max_x * actual_scale)) / 2.0);
64 float pos_y = (float)((h - (max_y * actual_scale)) / 2.0);
65
66 pos_x = wxMax(pos_x, margin_x);
67 pos_y = wxMax(pos_y, margin_y);
68
69 // Set the scale and origin
70 dc->SetUserScale(actual_scale, actual_scale);
71 dc->SetDeviceOrigin((long)pos_x, (long)pos_y);
72
73 // Get the latest bitmap as rendered by the ChartCanvas
74
75 if (g_bopengl) {
76#ifdef ocpnUSE_GL
77 if (m_gl_bmp.IsOk()) {
78 wxMemoryDC mdc;
79 mdc.SelectObject(m_gl_bmp);
80 dc->Blit(0, 0, m_gl_bmp.GetWidth(), m_gl_bmp.GetHeight(), &mdc, 0, 0);
81 mdc.SelectObject(wxNullBitmap);
82 }
83#endif
84 } else {
85 // And Blit/scale it onto the Printer DC
86 wxMemoryDC mdc;
87 mdc.SelectObject(*(gFrame->GetFocusCanvas()->pscratch_bm));
88
89 dc->Blit(0, 0, gFrame->GetFocusCanvas()->pscratch_bm->GetWidth(),
90 gFrame->GetFocusCanvas()->pscratch_bm->GetHeight(), &mdc, 0, 0);
91
92 mdc.SelectObject(wxNullBitmap);
93 }
94}
95
97 if (g_bopengl) {
98#ifdef ocpnUSE_GL
99 int gsx = gFrame->GetFocusCanvas()->GetglCanvas()->GetSize().x;
100 int gsy = gFrame->GetFocusCanvas()->GetglCanvas()->GetSize().y;
101
102 unsigned char* buffer = (unsigned char*)malloc(gsx * gsy * 4);
103 glReadPixels(0, 0, gsx, gsy, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
104
105 unsigned char* e = (unsigned char*)malloc(gsx * gsy * 3);
106
107 if (buffer && e) {
108 for (int p = 0; p < gsx * gsy; p++) {
109 e[3 * p + 0] = buffer[4 * p + 0];
110 e[3 * p + 1] = buffer[4 * p + 1];
111 e[3 * p + 2] = buffer[4 * p + 2];
112 }
113 }
114 free(buffer);
115
116 wxImage image(gsx, gsy);
117 image.SetData(e);
118 wxImage mir_imag = image.Mirror(false);
119 m_gl_bmp = wxBitmap(mir_imag);
120#endif
121 }
122}
Generic Chart canvas base.
void GenerateGLbmp()
In OpenGL mode, make the bitmap capture of the screen before the print method starts as to be sure th...
Global variables stored in configuration file.
OpenGL chart rendering canvas.
OpenCPN top window.
Print chart canvas mix-in.