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 <wx/font.h>
58#include <wx/version.h>
59#include <algorithm>
60#include <cstdint>
61#include <functional>
62#include <limits>
63
68
96class AFontSize final
97{
98 constexpr static float Size_Unit = 0.05f;
99
100public:
101 using value_type = int16_t;
102
103 constexpr static float Minimum_Size = 4.0f;
104 constexpr static float Maximum_Size = std::numeric_limits<value_type>::max() * Size_Unit;
105
106 constexpr AFontSize() = default;
107 constexpr explicit AFontSize(float size) : m_uSize(ToUSize(size)) {}
108 constexpr AFontSize(AFontSize minimum, double size) :
109 m_uSize(std::max(minimum.m_uSize, ToUSize(static_cast<float>(size)))) {}
110 constexpr AFontSize(AFontSize minimum, AFontSize size) : m_uSize(std::max(minimum.m_uSize, size.m_uSize)) {}
111 constexpr AFontSize(const AFontSize &o) = default;
112
113 constexpr void Set(float size) { m_uSize = ToUSize(size); }
114 constexpr void Clear() { m_uSize = {}; }
115 // Old cppcheck bugs:
116 // cppcheck-suppress operatorEq
117 constexpr AFontSize &operator=(const AFontSize &o) = default;
118
119 constexpr auto operator<=>(const AFontSize& o) const = default;
120 constexpr float Get() const { return IsValid() ? m_uSize * Size_Unit : Minimum_Size; }
121 constexpr long GetAsLong() const { return long(Get() + 0.5f); }
122 constexpr auto GetForWX() const;
123 constexpr bool IsNull() const { return !IsValid(); }
124 constexpr bool IsValid() const { return m_uSize > 0; }
125 constexpr bool IsMinimal() const { return m_uSize == ToUSize(Minimum_Size); }
126
127 struct Equals {
128 bool operator()(AFontSize l, AFontSize r) const { return l == r; }
129 };
130
131private:
132 friend bool EqualToWithin(AFontSize, AFontSize, float);
133 value_type m_uSize = {};
134 constexpr static value_type ToUSize(float size);
135};
136
137constexpr double operator*(double factor, AFontSize size) { return factor * size.Get(); }
138constexpr double operator*(AFontSize size, double factor) { return size.Get() * factor; }
139constexpr double operator/(double dividend, AFontSize size) { return dividend / size.Get(); }
140constexpr double operator/(AFontSize size, double divisor) { return size.Get() / divisor; }
141constexpr double operator+(double offset, AFontSize size) { return offset + size.Get(); }
142constexpr double operator+(AFontSize size, double offset) { return size.Get() + offset; }
143constexpr double operator-(double offset, AFontSize size) { return offset - size.Get(); }
144constexpr double operator-(AFontSize size, double offset) { return size.Get() - offset; }
145constexpr AFontSize operator-=(AFontSize &size, double factor)
146{ return size.Set(static_cast<float>(size - factor)), size; }
147
148constexpr AFontSize::value_type AFontSize::ToUSize(float size)
149{
150 return AFontSize::value_type(std::clamp(size, Minimum_Size, Maximum_Size) / Size_Unit + 0.5f);
151}
152
154constexpr auto AFontSize::GetForWX() const
155{
156 return GetAsLong();
157}
158
160bool EqualToWithin(AFontSize left, AFontSize right, float limit);
161
162#endif
A Type-Safe Fixed-Point Font Size.
Definition: FontAttribs.h:97
constexpr auto GetForWX() const
Get the numerical value suitable for passing to wxFont/wxFontInfo.
Definition: FontAttribs.h:154
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:127