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 std::string_view;
38using strings = std::vector<string>;
40using vvstrings = std::vector<strings>;
41
42// Utility function, like regex_replace.
43inline void
44string_replace(std::string& target, const std::string& match,
45 const std::string& replace)
46{
47 size_t pos = 0;
48 while((pos = target.find(match, pos)) != std::string::npos)
49 {
50 target.replace(pos, match.length(), replace);
51 pos += replace.length();
52 }
53}
54
55
56/// Base integer type: positive and negative, signed integral value.
57using ushort = unsigned short;
58using uint = unsigned int;
59using ulong = unsigned long;
60using ssize_type = int;
61
62/// Base floating point type.
63using space_type = double;
64
65
66/// Scale value from min to max on range (nfloor, nceil).
67double
69 const ssize_type max,
70 const ssize_type nfloor, const ssize_type nceil)
71{
72 double rmultp(nceil - nfloor);
73 double valnum(value - min);
74 double valdenom(max - min);
75 double weightn = (rmultp * (valnum / valdenom)) + nfloor;
76 return weightn;
77}
78
79
80/// Resolution of output display device.
81double&
83{
84 static double dpi(96.0);
85 return dpi;
86}
87
88
89/// Conversion between point size to pixels given dpi density.
90double
91pt_to_px(const uint i = 1)
92{
93 // 1pt is equal to exactly 1/72th of an inch.
94 // On a 72dpi output device (display), this is 1 (aka 72 * 1/72).
95 // On a 90dpi output device (display), this is 1.25 (aka 90 * 1/72).
96 // On a 96dpi output device (display), this is 1.33 (aka 96 * 1/72).
97 // constexpr double dpimult = (get_dpi() / 72);
98
99 // Or 18 pt -> 13.5 px -> 1.33
100 // Or 30 pt -> 22.5 px -> 1.33
101 constexpr double dpimult = 1.33;
102 return std::lround(i * dpimult);
103}
104
105
106/// Approximate pixel height of type of point size @sz.
107constexpr double
109{ return 0.58 * sz; }
110
111
112/// Approximate pixel height of type of point size @sz.
113constexpr double
115{ return 0.94 * sz; }
116
117
118/**
119 * SVG Constants
120 */
121namespace constants {
122
123/// Formatting character constants.
124constexpr char space(' ');
125constexpr char quote('"');
126constexpr char hyphen('-');
127constexpr char comma(',');
128constexpr char tab('\t');
129constexpr char newline('\n');
130
131
132/**
133 Numeric constants.
134 pi = double(22)/double(7);
135 pi = 3.14159265358979323846
136*/
137constexpr double pi(3.14159265358979323846);
138
139} // namespace constants
140
141/// Inject nested namepace constants into svg namespace with alias k.
142namespace k = constants;
143
144} // namespace svg
145
146
147#include "a60-svg-color.h" // color, color_qi, color_qf
149#include "a60-svg-color-band.h"
150
151#include "izzi-points.h" // 2d-points, range, distance
152#include "a60-svg-base-types.h" // area, style, filter, transform, typography
153#include "a60-svg-constants.h"
154#include "a60-svg-elements.h"
156#include "a60-svg-render-state.h"
159#include "a60-svg-sequences.h"
160
161#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:57
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:68
int ssize_type
Definition a60-svg.h:60
void string_replace(std::string &target, const std::string &match, const std::string &replace)
Definition a60-svg.h:44
double pt_to_px(const uint i=1)
Conversion between point size to pixels given dpi density.
Definition a60-svg.h:91
double space_type
Base floating point type.
Definition a60-svg.h:63
unsigned long ulong
Definition a60-svg.h:59
std::vector< strings > vvstrings
Definition a60-svg.h:40
constexpr double char_width_to_px(const uint sz)
Approximate pixel height of type of point size @sz.
Definition a60-svg.h:108
strings vstrings
Definition a60-svg.h:39
constexpr double char_height_to_px(const uint sz)
Approximate pixel height of type of point size @sz.
Definition a60-svg.h:114
double & get_dpi()
Resolution of output display device.
Definition a60-svg.h:82
unsigned int uint
Definition a60-svg.h:58
std::vector< string > strings
Definition a60-svg.h:38