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 "printout_chart.h"
33#include "top_frame.h"
34
35void ChartPrintout::DrawPage(wxDC* dc, int page) {
36 // Get the Size of the Chart Canvas
37 int sx, sy;
38 top_frame::Get()->GetAbstractPrimaryCanvas()->GetClientSize(&sx, &sy);
39 // of the canvas
40
41 float max_x = sx;
42 float max_y = sy;
43
44 // Let's have at least some device units margin
45 float margin_x = 50;
46 float margin_y = 50;
47
48 // Add the margin to the graphic size
49 max_x += (2 * margin_x);
50 max_y += (2 * margin_y);
51
52 // Get the size of the DC in pixels
53 int w, h;
54 dc->GetSize(&w, &h);
55
56 // Calculate a suitable scaling factor
57 float scale_x = (float)(w / max_x);
58 float scale_y = (float)(h / max_y);
59
60 // Use x or y scaling factor, whichever fits on the DC
61 float actual_scale = wxMin(scale_x, scale_y);
62
63 // Calculate the position on the DC for centring the graphic
64 float pos_x = (float)((w - (max_x * actual_scale)) / 2.0);
65 float pos_y = (float)((h - (max_y * actual_scale)) / 2.0);
66
67 pos_x = wxMax(pos_x, margin_x);
68 pos_y = wxMax(pos_y, margin_y);
69
70 // Set the scale and origin
71 dc->SetUserScale(actual_scale, actual_scale);
72 dc->SetDeviceOrigin((long)pos_x, (long)pos_y);
73
74 // Get the latest bitmap as rendered by the ChartCanvas
75
76 if (g_bopengl) {
77#ifdef ocpnUSE_GL
78 if (m_gl_bmp.IsOk()) {
79 wxMemoryDC mdc;
80 mdc.SelectObject(m_gl_bmp);
81 dc->Blit(0, 0, m_gl_bmp.GetWidth(), m_gl_bmp.GetHeight(), &mdc, 0, 0);
82 mdc.SelectObject(wxNullBitmap);
83 }
84#endif
85 } else {
86 // And Blit/scale it onto the Printer DC
87 wxMemoryDC mdc;
88 wxBitmap* scratch_bitmap =
89 top_frame::Get()->GetAbstractPrimaryCanvas()->GetScratchBitmap();
90 mdc.SelectObject(*scratch_bitmap);
91 dc->Blit(0, 0, scratch_bitmap->GetWidth(), scratch_bitmap->GetHeight(),
92 &mdc, 0, 0);
93
94 mdc.SelectObject(wxNullBitmap);
95 }
96}
97
99 if (g_bopengl) {
100#ifdef ocpnUSE_GL
101 int gsx = top_frame::Get()->GetWxGlCanvas()->GetSize().x;
102 int gsy = top_frame::Get()->GetWxGlCanvas()->GetSize().y;
103
104 unsigned char* buffer = (unsigned char*)malloc(gsx * gsy * 4);
105 glReadPixels(0, 0, gsx, gsy, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
106
107 unsigned char* e = (unsigned char*)malloc(gsx * gsy * 3);
108
109 if (buffer && e) {
110 for (int p = 0; p < gsx * gsy; p++) {
111 e[3 * p + 0] = buffer[4 * p + 0];
112 e[3 * p + 1] = buffer[4 * p + 1];
113 e[3 * p + 2] = buffer[4 * p + 2];
114 }
115 }
116 free(buffer);
117
118 wxImage image(gsx, gsy);
119 image.SetData(e);
120 wxImage mir_imag = image.Mirror(false);
121 m_gl_bmp = wxBitmap(mir_imag);
122#endif
123 }
124}
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.
Print chart canvas mix-in.
Abstract gFrame/MyFrame interface.