izzi
SVG SUBSET C++ API
Loading...
Searching...
No Matches
a60-svg.h
Go to the documentation of this file.
1// svg API -*- mode: C++ -*-
2
3// Copyright (C) 2019-2025 Benjamin De Kosnik <b.dekosnik@gmail.com>
4
5// This file is part of the alpha60-MiL SVG library. This library is
6// free software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the Free
8// Software Foundation; either version 3, or (at your option) any
9// later version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
16#ifndef MiL_SVG_H
17#define MiL_SVG_H 1
18
19#include <cstddef>
20#include <cmath>
21#include <algorithm>
22#include <array>
23#include <tuple>
24#include <string>
25#include <vector>
26#include <set>
27#include <unordered_map>
28#include <sstream>
29
30/**
31 * Scalable Vector Graphics (SVG) namespace
32 */
33namespace svg {
34
35/// Base string types.
36using std::string;
37using strings = std::vector<string>;
39using vvstrings = std::vector<strings>;
40
41// Utility function, like regex_replace.
42inline void
43string_replace(std::string& target, const std::string& match,
44 const std::string& replace)
45{
46 size_t pos = 0;
47 while((pos = target.find(match, pos)) != std::string::npos)
48 {
49 target.replace(pos, match.length(), replace);
50 pos += replace.length();
51 }
52}
53
54
55/// Base integer type: positive and negative, signed integral value.
56using ushort = unsigned short;
57using uint = unsigned int;
58using ulong = unsigned long;
59using ssize_type = int;
60
61/// Base floating point type.
62using space_type = double;
63
64
65/// Scale value from min to max on range (nfloor, nceil).
66double
68 const ssize_type max,
69 const ssize_type nfloor, const ssize_type nceil)
70{
71 double rmultp(nceil - nfloor);
72 double valnum(value - min);
73 double valdenom(max - min);
74 double weightn = (rmultp * (valnum / valdenom)) + nfloor;
75 return weightn;
76}
77
78
79/// Resolution of output display device.
80double&
82{
83 static double dpi(96.0);
84 return dpi;
85}
86
87
88/// Conversion between point size to pixels given dpi density.
89double
90pt_to_px(const uint i = 1)
91{
92 // 1pt is equal to exactly 1/72th of an inch.
93 // On a 72dpi output device (display), this is 1 (aka 72 * 1/72).
94 // On a 90dpi output device (display), this is 1.25 (aka 90 * 1/72).
95 // On a 96dpi output device (display), this is 1.33 (aka 96 * 1/72).
96 // constexpr double dpimult = (get_dpi() / 72);
97
98 // Or 18 pt -> 13.5 px -> 1.33
99 // Or 30 pt -> 22.5 px -> 1.33
100 constexpr double dpimult = 1.33;
101 return std::lround(i * dpimult);
102}
103
104
105/// Approximate pixel height of type of point size @sz.
106constexpr double
108{ return 0.58 * sz; }
109
110
111/// Approximate pixel height of type of point size @sz.
112constexpr double
114{ return 0.94 * sz; }
115
116
117/**
118 * SVG Constants
119 */
120namespace constants {
121
122/// Formatting character constants.
123constexpr char space(' ');
124constexpr char quote('"');
125constexpr char hyphen('-');
126constexpr char comma(',');
127constexpr char tab('\t');
128constexpr char newline('\n');
129
130
131/**
132 Numeric constants.
133 pi = double(22)/double(7);
134 pi = 3.14159265358979323846
135*/
136constexpr double pi(3.14159265358979323846);
137
138} // namespace constants
139
140/// Inject nested namepace constants into svg namespace with alias k.
141namespace k = constants;
142
143} // namespace svg
144
145
146#include "a60-svg-color.h" // color, color_qi, color_qf
148#include "a60-svg-color-band.h"
149
150#include "izzi-points.h" // 2d-points, range, distance
151#include "a60-svg-base-types.h" // area, style, filter, transform, typography
152#include "a60-svg-constants.h"
153#include "a60-svg-elements.h"
155#include "a60-svg-render-state.h"
158#include "a60-svg-sequences.h"
159
160#endif
constexpr double pi(3.14159265358979323846)
constexpr char quote('"')
constexpr char comma(',')
constexpr char newline('\n')
constexpr char space(' ')
Formatting character constants.
constexpr char tab('\t')
constexpr char hyphen('-')
unsigned short ushort
Base integer type: positive and negative, signed integral value.
Definition a60-svg.h:56
double scale_value_on_range(const ssize_type value, const ssize_type min, const ssize_type max, const ssize_type nfloor, const ssize_type nceil)
Scale value from min to max on range (nfloor, nceil).
Definition a60-svg.h:67
int ssize_type
Definition a60-svg.h:59
void string_replace(std::string &target, const std::string &match, const std::string &replace)
Definition a60-svg.h:43
double pt_to_px(const uint i=1)
Conversion between point size to pixels given dpi density.
Definition a60-svg.h:90
double space_type
Base floating point type.
Definition a60-svg.h:62
unsigned long ulong
Definition a60-svg.h:58
std::vector< strings > vvstrings
Definition a60-svg.h:39
constexpr double char_width_to_px(const uint sz)
Approximate pixel height of type of point size @sz.
Definition a60-svg.h:107
strings vstrings
Definition a60-svg.h:38
constexpr double char_height_to_px(const uint sz)
Approximate pixel height of type of point size @sz.
Definition a60-svg.h:113
double & get_dpi()
Resolution of output display device.
Definition a60-svg.h:81
unsigned int uint
Definition a60-svg.h:57
std::vector< string > strings
Definition a60-svg.h:37