wxMaxima
Loading...
Searching...
No Matches
FontAttribs.h
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2020 Kuba Ober <kuba@mareimbrium.org>
4//
5// Everyone is permitted to copy and distribute verbatim copies
6// of this licence document, but changing it is not allowed.
7//
8// WXWINDOWS LIBRARY LICENCE
9// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
10//
11// This library is free software; you can redistribute it and/or modify it
12// under the terms of the GNU Library General Public Licence as published by
13// the Free Software Foundation; either version 2 of the Licence, or (at your
14// option) any later version.
15//
16// This library is distributed in the hope that it will be useful, but WITHOUT
17// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
19// Licence for more details.
20//
21// You should have received a copy of the GNU Library General Public Licence
22// along with this software, usually in a file named COPYING.LIB. If not,
23// write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
24// Floor, Boston, MA 02110-1301 USA.
25//
26// EXCEPTION NOTICE
27//
28// 1. As a special exception, the copyright holders of this library give
29// permission for additional uses of the text contained in this release of the
30// library as licenced under the wxWindows Library Licence, applying either
31// version 3.1 of the Licence, or (at your option) any later version of the
32// Licence as published by the copyright holders of version 3.1 of the Licence
33// document.
34//
35// 2. The exception is that you may use, copy, link, modify and distribute
36// under your own terms, binary object code versions of works based on the
37// Library.
38//
39// 3. If you copy code from files distributed under the terms of the GNU
40// General Public Licence or the GNU Library General Public Licence into a
41// copy of this library, as this licence permits, the exception does not apply
42// to the code that you add in this way. To avoid misleading anyone as to the
43// status of such modified files, you must delete this exception notice from
44// such code and/or adjust the licensing conditions notice accordingly.
45//
46// 4. If you write modifications of your own for this library, it is your
47// choice whether to permit this exception to apply to your modifications. If
48// you do not wish that, you must delete the exception notice from such code
49// and/or adjust the licensing conditions notice accordingly.
50//
51// SPDX-License-Identifier: wxWindows
52
53#ifndef WXMAXIMA_FONTATTRIBS_H
54#define WXMAXIMA_FONTATTRIBS_H
55
56#include "EnumWrapper.h"
57#include "stx/clamp.hpp"
58#include <wx/font.h>
59#include <wx/version.h>
60#include <algorithm>
61#include <cstdint>
62#include <functional>
63#include <limits>
64
69
97class AFontSize final
98{
99 constexpr static float Size_Unit = 0.05f;
100
101public:
102 using value_type = int16_t;
103
104 constexpr static float Minimum_Size = 4.0f;
105 constexpr static float Maximum_Size = std::numeric_limits<value_type>::max() * Size_Unit;
106
107 constexpr AFontSize() = default;
108 constexpr explicit AFontSize(float size) : m_uSize(ToUSize(size)) {}
109 constexpr AFontSize(AFontSize minimum, double size) :
110 m_uSize(std::max(minimum.m_uSize, ToUSize(static_cast<float>(size)))) {}
111 constexpr AFontSize(AFontSize minimum, AFontSize size) : m_uSize(std::max(minimum.m_uSize, size.m_uSize)) {}
112 constexpr AFontSize(const AFontSize &o) = default;
113
114 constexpr void Set(float size) { m_uSize = ToUSize(size); }
115 constexpr void Clear() { m_uSize = {}; }
116 // Old cppcheck bugs:
117 // cppcheck-suppress operatorEq
118 constexpr AFontSize &operator=(const AFontSize &o) = default;
119
120 constexpr bool operator==(AFontSize o) const { return m_uSize == o.m_uSize; }
121 constexpr bool operator!=(AFontSize o) const { return m_uSize != o.m_uSize; }
122 constexpr bool operator<(AFontSize o) const { return m_uSize < o.m_uSize; }
123 constexpr bool operator>(AFontSize o) const { return m_uSize > o.m_uSize; }
124 constexpr float Get() const { return IsValid() ? m_uSize * Size_Unit : Minimum_Size; }
125 constexpr long GetAsLong() const { return long(Get() + 0.5f); }
126 constexpr auto GetForWX() const;
127 constexpr bool IsNull() const { return !IsValid(); }
128 constexpr bool IsValid() const { return m_uSize > 0; }
129 constexpr bool IsMinimal() const { return m_uSize == ToUSize(Minimum_Size); }
130
131 struct Equals {
132 bool operator()(AFontSize l, AFontSize r) const { return l == r; }
133 };
134
135private:
136 friend bool EqualToWithin(AFontSize, AFontSize, float);
137 value_type m_uSize = {};
138 constexpr static value_type ToUSize(float size);
139};
140
141constexpr double operator*(double factor, AFontSize size) { return factor * size.Get(); }
142constexpr double operator*(AFontSize size, double factor) { return size.Get() * factor; }
143constexpr double operator/(double dividend, AFontSize size) { return dividend / size.Get(); }
144constexpr double operator/(AFontSize size, double divisor) { return size.Get() / divisor; }
145constexpr double operator+(double offset, AFontSize size) { return offset + size.Get(); }
146constexpr double operator+(AFontSize size, double offset) { return size.Get() + offset; }
147constexpr double operator-(double offset, AFontSize size) { return offset - size.Get(); }
148constexpr double operator-(AFontSize size, double offset) { return size.Get() - offset; }
149constexpr AFontSize operator-=(AFontSize &size, double factor)
150{ return size.Set(static_cast<float>(size - factor)), size; }
151
152constexpr AFontSize::value_type AFontSize::ToUSize(float size)
153{
154 return AFontSize::value_type(stx::clamp(size, Minimum_Size, Maximum_Size) / Size_Unit + 0.5f);
155}
156
158constexpr auto AFontSize::GetForWX() const
159{
160#if wxCHECK_VERSION(3, 1, 2)
161 return Get();
162#else
163 return GetAsLong();
164#endif
165}
166
168bool EqualToWithin(AFontSize left, AFontSize right, float limit);
169
170#endif
A Type-Safe Fixed-Point Font Size.
Definition: FontAttribs.h:98
constexpr auto GetForWX() const
Get the numerical value suitable for passing to wxFont/wxFontInfo.
Definition: FontAttribs.h:158
friend bool EqualToWithin(AFontSize, AFontSize, float)
Whether the difference between to font sizes is below a provided limit value.
Definition: FontAttribs.cpp:64
Definition: FontAttribs.h:131