Empirical
Font.h
Go to the documentation of this file.
1 
11 #ifndef EMP_FONT_H
12 #define EMP_FONT_H
13 
14 #include <string>
15 #include <sstream>
16 
17 #include "color_map.h"
18 #include "Style.h"
19 
20 namespace emp {
21 namespace web {
22 
24  class Font {
25  protected:
26  std::string family;
27  int size;
28  std::string color;
29  std::string line_color;
30 
31  bool is_bold;
32  bool is_italic;
33  bool is_smallcaps;
35  bool is_overlined;
37  bool is_wavy_line;
38 
39  public:
40  Font(const Font &) = default;
41  Font(Font &&) = default;
42  Font(const std::string & _family="Helvetica", int _size=15,
43  const std::string & _color="black", bool _bold=false, bool _italic=false)
44  : family(_family), size(_size), color(_color)
45  , is_bold(_bold), is_italic(_italic), is_smallcaps(false)
46  , is_underlined(false), is_overlined(false), is_linethrough(false), is_wavy_line(false) { ; }
47  Font(int _size, const std::string & _color="black", bool _bold=false, bool _italic=false)
48  : Font("Helvetica", _size, _color, _bold, _italic) { ; }
49  ~Font() { ; }
50 
51  Font & operator=(const Font &) = default;
52  Font & operator=(Font &&) = default;
53 
54  const std::string & GetFamily() const { return family; }
55  int GetSize() const { return size; }
56  const std::string & GetColor() const { return color; }
57  const std::string & GetLineColor() const { return line_color; }
58  bool IsBold() const { return is_bold; }
59  bool IsItalic() const { return is_italic; }
60  bool IsSmallcaps() const { return is_smallcaps; }
61  bool IsUnderlined() const { return is_underlined; }
62  bool IsOverlined() const { return is_overlined; }
63  bool IsStrikethrough() const { return is_linethrough; }
64  bool IsWavyLine() const { return is_wavy_line; }
65  bool HasLine() const { return is_underlined || is_overlined || is_linethrough; }
66 
67  Font & SetFamily(const std::string & _family) { family = _family; return *this; }
68  Font & SetSize(int _size) { size = _size; return *this; }
69  Font & SetColor(const std::string & _color) { color = _color; return *this; }
70  Font & SetLineColor(const std::string & _color) { line_color = _color; return *this; }
71  Font & SetBold(bool _in=true) { is_bold = _in; return *this; }
72  Font & SetItalic(bool _in=true) { is_italic = _in; return *this; }
73  Font & SetSmallcaps(bool _in=true) { is_smallcaps = _in; return *this; }
74  Font & SetUnderlined(bool _in = true) { is_underlined = _in; return *this; }
75  Font & SetOverlined(bool _in = true) { is_overlined = _in; return *this; }
76  Font & SetStrikethrough(bool _in = true) { is_linethrough = _in; return *this; }
77  Font & SetWavyLine(bool _in = true) { is_wavy_line = _in; return *this; }
78 
80  void ConfigStyle(Style & style) const {
81  style.Set("color", color);
82  style.Set("font-family", family);
83  style.Set("font-size", to_string(size,"px"));
84  if (is_bold) style.Set("font-weight", "bold");
85  if (is_italic) style.Set("font-style", "italic");
86  if (is_smallcaps) style.Set("font-variant", "small-caps");
87  if (HasLine()) {
88  std::string decoration("");
89  if (is_underlined) decoration += " underline";
90  if (is_overlined) decoration += " overline";
91  if (is_linethrough) decoration += " line-through";
92  if (line_color != "") { decoration += " "; decoration += line_color; }
93  if (is_wavy_line) decoration += " wavy";
94  style.Set("text-decoration", decoration);
95  }
96  }
97 
98  Style AsStyle() const {
99  Style style;
100  ConfigStyle(style);
101  return style;
102  }
103 
104  std::string GetHTMLStart() {
105  std::stringstream ss;
106  ss << "<span style=\"color:" << color
107  << "; font-family:" << family
108  << "; font-size:" << size;
109  if (is_bold) ss << "; font-weight:bold";
110  if (is_italic) ss << "; font-style:italic";
111  if (is_smallcaps) ss << "; font-variant:small-caps";
112  if (HasLine()) {
113  ss << "; text-decoration:";
114  if (is_underlined) ss << " underline";
115  if (is_overlined) ss << " overline";
116  if (is_linethrough) ss << " line-through";
117  if (line_color != "") ss << " " << line_color;
118  if (is_wavy_line) ss << " wavy";
119  }
120  ss << "\">";
121  return ss.str();
122  }
123  std::string GetHTMLEnd() { return "</span>"; }
124 
125  bool operator==(const Font & _in) const {
126  return (family == _in.family)
127  && (size == _in.size)
128  && (color == _in.color)
129  && (line_color == _in.line_color)
130  && (is_bold == _in.is_bold)
131  && (is_italic == _in.is_italic)
132  && (is_smallcaps == _in.is_smallcaps)
133  && (is_underlined == _in.is_underlined)
134  && (is_overlined == _in.is_overlined)
135  && (is_linethrough == _in.is_linethrough)
136  && (is_wavy_line == _in.is_wavy_line)
137  ;
138  }
139  bool operator!=(const Font & _in) const { return !operator==(_in); }
140  };
141 
142 }
143 }
144 
145 #endif
void ConfigStyle(Style &style) const
Take a Style object an fill it out based on this font information.
Definition: Font.h:80
Font(const std::string &_family="Helvetica", int _size=15, const std::string &_color="black", bool _bold=false, bool _italic=false)
Definition: Font.h:42
Font & SetSize(int _size)
Definition: Font.h:68
Font & SetWavyLine(bool _in=true)
Definition: Font.h:77
bool is_underlined
Should this text be underlined?
Definition: Font.h:34
const std::string & GetFamily() const
Definition: Font.h:54
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
Font(int _size, const std::string &_color="black", bool _bold=false, bool _italic=false)
Definition: Font.h:47
bool is_smallcaps
Should this test be in small caps?
Definition: Font.h:33
Font & SetItalic(bool _in=true)
Definition: Font.h:72
Definition: Style.h:29
Font & SetOverlined(bool _in=true)
Definition: Font.h:75
int GetSize() const
Definition: Font.h:55
Style & Set(const std::string &s, SET_TYPE v)
Record that setting "s" is set to value "v" (converted to string) and return this object...
Definition: Style.h:50
const std::string & GetColor() const
Definition: Font.h:56
Font(const Font &)=default
bool operator!=(const Font &_in) const
Definition: Font.h:139
bool is_linethrough
Should this text have a line through it?
Definition: Font.h:36
Font & SetColor(const std::string &_color)
Definition: Font.h:69
std::string GetHTMLStart()
Definition: Font.h:104
bool IsSmallcaps() const
Definition: Font.h:60
bool IsStrikethrough() const
Definition: Font.h:63
std::string GetHTMLEnd()
Definition: Font.h:123
Tools to dynamically build (and cache) color maps.
bool is_italic
Is this font itaic?
Definition: Font.h:32
~Font()
Definition: Font.h:49
Font & SetSmallcaps(bool _in=true)
Definition: Font.h:73
bool IsWavyLine() const
Definition: Font.h:64
Font & SetLineColor(const std::string &_color)
Definition: Font.h:70
Font & SetBold(bool _in=true)
Definition: Font.h:71
Font & SetStrikethrough(bool _in=true)
Definition: Font.h:76
bool IsUnderlined() const
Definition: Font.h:61
std::string family
Font family to use.
Definition: Font.h:26
bool is_bold
Is this font bold?
Definition: Font.h:31
std::string line_color
Color of lines through the text (underline, linethrough, etc.)
Definition: Font.h:29
bool HasLine() const
Definition: Font.h:65
If we are in emscripten, make sure to include the header.
Definition: array.h:37
bool is_overlined
Should this text have a line above it?
Definition: Font.h:35
Style AsStyle() const
Definition: Font.h:98
Font & SetUnderlined(bool _in=true)
Definition: Font.h:74
const std::string & GetLineColor() const
Definition: Font.h:57
bool operator==(const Font &_in) const
Definition: Font.h:125
bool IsOverlined() const
Definition: Font.h:62
int size
Font size (in px) to use.
Definition: Font.h:27
Font & operator=(const Font &)=default
bool is_wavy_line
Should lines be made wavy?
Definition: Font.h:37
std::string color
Font color.
Definition: Font.h:28
Font & SetFamily(const std::string &_family)
Definition: Font.h:67
Maintain information about an HTML font.
Definition: Font.h:24
A CSS class for tracking font style, etc.
bool IsBold() const
Definition: Font.h:58
bool IsItalic() const
Definition: Font.h:59