OpenCPN Partial API docs
svg_utils.h
1 /******************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: SVG Utility functions
5  * Author: David Register
6  *
7  ***************************************************************************
8  * Copyright (C) 2018 by David S. Register *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24  ***************************************************************************
25  *
26  *
27  */
28 
29 #ifndef OPENCPN_SVG_UTILS_H
30 #define OPENCPN_SVG_UTILS_H
31 
32 #include <wx/wxprec.h>
33 
34 #ifndef WX_PRECOMP
35 #include <wx/wx.h>
36 #endif // precompiled headers
37 
38 #include <algorithm>
39 #include <string>
40 #include <sstream>
41 #include <mutex>
42 #include <unordered_map>
43 
44 #define SVG_IN_TO_PT 72
45 #define SVG_IN_TO_PX 96
46 #define SVG_PT_TO_IN 1 / 72
47 #define SVG_PX_TO_IN 1 / 96
48 #define SVG_PT_TO_PX 96 / 72
49 #define SVG_MM_TO_PX 3.7795275591
50 #define SVG_PX_TO_MM 0.2645833333
51 #define SVG_MM_TO_PT 2.8346456693
52 #define SVG_PT_TO_MM 0.3527777778
53 #define SVG_CM_TO_PX 37.795275591
54 #define SVG_CM_TO_PT 28.346456693
55 #define SVG_MM_TO_IN 25.4
56 
57 // Load SVG file and return it's bitmap representation of requested size
58 // In case file can't be loaded an empty bitmap of desired size or a provided
59 // default bitmap is returned
60 wxBitmap LoadSVG(const wxString filename, const unsigned int width,
61  const unsigned int height, wxBitmap* default_bitmap = NULL,
62  bool use_cache = true);
63 
64 // Return the size of the SVG document in standard 96 DPI pixels
65 // https://developer.mozilla.org/en-US/docs/Web/SVG/Content_type#length
66 // Percentage, em and ex make no sense without being able to relate them
67 // to something, so we just return the numerical value as we do for px
68 bool SVGDocumentPixelSize(const wxString filename, unsigned int& width,
69  unsigned int& height);
70 
71 // Recalculate the length in standard 96 DPI pixels to actual display pixels
72 unsigned int SVGPixelsToDisplay(unsigned int svg_px);
73 
74 // Manage memory and disk cache for rendered bitmaps
76 private:
78  wxString cache_directory;
79  std::mutex sync;
80  std::unordered_map<std::string, wxBitmap> items;
81 
82 public:
83  static SVGBitmapCache& GetInstance() {
84  static SVGBitmapCache instance;
85  return instance;
86  }
87 
88  void Clear(bool disk = true) {
89  items.clear(); /*TODO: clear the disk cache as well*/
90  };
91 
92  std::string MakeKey(wxString file_path, const int width, const int height);
93 
94  bool HasKey(const wxString key);
95 
96  wxBitmap Get(const wxString key);
97 
98  void Add(const wxString, const wxBitmap bmp);
99 
100 public:
101  SVGBitmapCache(SVGBitmapCache const&) = delete;
102  void operator=(SVGBitmapCache const&) = delete;
103 };
104 
105 #endif // OPENCPN_SVG_UTILS_H