|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Nwutobo Samuel Ugochukwu <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#ifndef LUNASVG_H |
| 24 | +#define LUNASVG_H |
| 25 | + |
| 26 | +#include <memory> |
| 27 | +#include <string> |
| 28 | +#include <cstdint> |
| 29 | + |
| 30 | +#if defined(_MSC_VER) && defined(LUNASVG_SHARED) |
| 31 | +#ifdef LUNASVG_EXPORT |
| 32 | +#define LUNASVG_API __declspec(dllexport) |
| 33 | +#else |
| 34 | +#define LUNASVG_API __declspec(dllimport) |
| 35 | +#endif |
| 36 | +#else |
| 37 | +#define LUNASVG_API |
| 38 | +#endif |
| 39 | + |
| 40 | +namespace lunasvg { |
| 41 | + |
| 42 | +class Rect; |
| 43 | +class Matrix; |
| 44 | + |
| 45 | +class LUNASVG_API Box { |
| 46 | +public: |
| 47 | + Box() = default; |
| 48 | + Box(double x, double y, double w, double h); |
| 49 | + Box(const Rect& rect); |
| 50 | + |
| 51 | + Box& transform(const Matrix& matrix); |
| 52 | + Box transformed(const Matrix& matrix) const; |
| 53 | + |
| 54 | +public: |
| 55 | + double x{0}; |
| 56 | + double y{0}; |
| 57 | + double w{0}; |
| 58 | + double h{0}; |
| 59 | +}; |
| 60 | + |
| 61 | +class Transform; |
| 62 | + |
| 63 | +class LUNASVG_API Matrix { |
| 64 | +public: |
| 65 | + Matrix() = default; |
| 66 | + Matrix(double a, double b, double c, double d, double e, double f); |
| 67 | + Matrix(const Transform& transform); |
| 68 | + |
| 69 | + Matrix& rotate(double angle); |
| 70 | + Matrix& rotate(double angle, double cx, double cy); |
| 71 | + Matrix& scale(double sx, double sy); |
| 72 | + Matrix& shear(double shx, double shy); |
| 73 | + Matrix& translate(double tx, double ty); |
| 74 | + Matrix& transform(double a, double b, double c, double d, double e, double f); |
| 75 | + Matrix& identity(); |
| 76 | + Matrix& invert(); |
| 77 | + |
| 78 | + Matrix& operator*=(const Matrix& matrix); |
| 79 | + Matrix& premultiply(const Matrix& matrix); |
| 80 | + Matrix& postmultiply(const Matrix& matrix); |
| 81 | + |
| 82 | + Matrix inverted() const; |
| 83 | + Matrix operator*(const Matrix& matrix) const; |
| 84 | + |
| 85 | + static Matrix rotated(double angle); |
| 86 | + static Matrix rotated(double angle, double cx, double cy); |
| 87 | + static Matrix scaled(double sx, double sy); |
| 88 | + static Matrix sheared(double shx, double shy); |
| 89 | + static Matrix translated(double tx, double ty); |
| 90 | + |
| 91 | +public: |
| 92 | + double a{1}; |
| 93 | + double b{0}; |
| 94 | + double c{0}; |
| 95 | + double d{1}; |
| 96 | + double e{0}; |
| 97 | + double f{0}; |
| 98 | +}; |
| 99 | + |
| 100 | +class LUNASVG_API Bitmap { |
| 101 | +public: |
| 102 | + /** |
| 103 | + * @note Bitmap format is ARGB32 Premultiplied. |
| 104 | + */ |
| 105 | + Bitmap(); |
| 106 | + Bitmap(std::uint8_t* data, std::uint32_t width, std::uint32_t height, std::uint32_t stride); |
| 107 | + Bitmap(std::uint32_t width, std::uint32_t height); |
| 108 | + |
| 109 | + void reset(std::uint8_t* data, std::uint32_t width, std::uint32_t height, std::uint32_t stride); |
| 110 | + void reset(std::uint32_t width, std::uint32_t height); |
| 111 | + |
| 112 | + std::uint8_t* data() const; |
| 113 | + std::uint32_t width() const; |
| 114 | + std::uint32_t height() const; |
| 115 | + std::uint32_t stride() const; |
| 116 | + |
| 117 | + void clear(std::uint32_t color); |
| 118 | + void convert(int ri, int gi, int bi, int ai, bool unpremultiply); |
| 119 | + void convertToRGBA() { convert(0, 1, 2, 3, true); } |
| 120 | + |
| 121 | + bool valid() const { return !!m_impl; } |
| 122 | + |
| 123 | +private: |
| 124 | + struct Impl; |
| 125 | + std::shared_ptr<Impl> m_impl; |
| 126 | +}; |
| 127 | + |
| 128 | +class LayoutSymbol; |
| 129 | + |
| 130 | +class LUNASVG_API Document { |
| 131 | +public: |
| 132 | + /** |
| 133 | + * @brief Creates a document from a file |
| 134 | + * @param filename - file to load |
| 135 | + * @return pointer to document on success, otherwise nullptr |
| 136 | + */ |
| 137 | + static std::unique_ptr<Document> loadFromFile(const std::string& filename); |
| 138 | + |
| 139 | + /** |
| 140 | + * @brief Creates a document from a string |
| 141 | + * @param string - string to load |
| 142 | + * @return pointer to document on success, otherwise nullptr |
| 143 | + */ |
| 144 | + static std::unique_ptr<Document> loadFromData(const std::string& string); |
| 145 | + |
| 146 | + /** |
| 147 | + * @brief Creates a document from a string data and size |
| 148 | + * @param data - string data to load |
| 149 | + * @param size - size of the data to load, in bytes |
| 150 | + * @return pointer to document on success, otherwise nullptr |
| 151 | + */ |
| 152 | + static std::unique_ptr<Document> loadFromData(const char* data, std::size_t size); |
| 153 | + |
| 154 | + /** |
| 155 | + * @brief Creates a document from a null terminated string data |
| 156 | + * @param data - null terminated string data to load |
| 157 | + * @return pointer to document on success, otherwise nullptr |
| 158 | + */ |
| 159 | + static std::unique_ptr<Document> loadFromData(const char* data); |
| 160 | + |
| 161 | + /** |
| 162 | + * @brief Sets the current transformation matrix of the document |
| 163 | + * @param matrix - current transformation matrix |
| 164 | + */ |
| 165 | + void setMatrix(const Matrix& matrix); |
| 166 | + |
| 167 | + /** |
| 168 | + * @brief Returns the current transformation matrix of the document |
| 169 | + * @return the current transformation matrix |
| 170 | + */ |
| 171 | + Matrix matrix() const; |
| 172 | + |
| 173 | + /** |
| 174 | + * @brief Returns the smallest rectangle in which the document fits |
| 175 | + * @return the smallest rectangle in which the document fits |
| 176 | + */ |
| 177 | + Box box() const; |
| 178 | + |
| 179 | + /** |
| 180 | + * @brief Returns width of the document |
| 181 | + * @return the width of the document in pixels |
| 182 | + */ |
| 183 | + double width() const; |
| 184 | + |
| 185 | + /** |
| 186 | + * @brief Returns the height of the document |
| 187 | + * @return the height of the document in pixels |
| 188 | + */ |
| 189 | + double height() const; |
| 190 | + |
| 191 | + /** |
| 192 | + * @brief Renders the document to a bitmap |
| 193 | + * @param matrix - the current transformation matrix |
| 194 | + * @param bitmap - target image on which the content will be drawn |
| 195 | + */ |
| 196 | + void render(Bitmap bitmap, const Matrix& matrix = Matrix{}) const; |
| 197 | + |
| 198 | + /** |
| 199 | + * @brief Renders the document to a bitmap |
| 200 | + * @param width - maximum width, in pixels |
| 201 | + * @param height - maximum height, in pixels |
| 202 | + * @param backgroundColor - background color in 0xRRGGBBAA format |
| 203 | + * @return the raster representation of the document |
| 204 | + */ |
| 205 | + Bitmap renderToBitmap(std::uint32_t width = 0, std::uint32_t height = 0, std::uint32_t backgroundColor = 0x00000000) const; |
| 206 | + |
| 207 | + Document(Document&&); |
| 208 | + ~Document(); |
| 209 | + |
| 210 | +private: |
| 211 | + Document(); |
| 212 | + |
| 213 | + std::unique_ptr<LayoutSymbol> root; |
| 214 | +}; |
| 215 | + |
| 216 | +} //namespace lunasvg |
| 217 | + |
| 218 | +#endif // LUNASVG_H |
0 commit comments