tbx  0.7.3
font.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010-2015 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TBX_FONT_H
26 #define TBX_FONT_H
27 
28 #include <string>
29 #include "colour.h"
30 #include "bbox.h"
31 #include "size.h"
32 
33 namespace tbx
34 {
38  struct FontDetails
39  {
44  int font_age;
46  };
47 
52  struct FullFontDetails : public FontDetails
53  {
54  std::string identifier;
55  };
56 
60  class Font
61  {
62  public:
63  Font();
64  Font(const std::string &font_name, int width, int height = -1);
65  Font(const Font &other);
66  virtual ~Font();
67 
73  int handle() const {return _font_ref->handle;}
79  bool is_valid() const {return (_font_ref->handle != 0);}
80  void set_colours(Colour fore, Colour back, int colourOffset = 14);
81  void read_details(FontDetails &details) const;
82  void read_details(FullFontDetails &details) const;
83  std::string identifier() const;
84  Size size() const;
85  int height() const;
86  int width() const;
87  Size size_os() const;
88  int height_os() const;
89  int width_os() const;
90 
91  // Operations
92  bool find(const std::string &font_name, int width, int height = -1);
93  bool desktop_font();
94 
98  enum PaintFlags {FPF_NONE = 0, FPF_JUSTIFY=1, FPF_RUBOUT= 2, FPF_OSUNITS=16};
99  void paint(int x, int y, const std::string &text, int flags = Font::FPF_NONE) const;
100  void paint(int x, int y, const char *text, int length = -1, int flags = Font::FPF_NONE) const;
101 
102  int string_width_mp(const std::string &text) const;
103  int string_width_mp(const char *text, int length = -1) const;
104  int string_width_os(const std::string &text) const;
105  int string_width_os(const char *text, int length = -1) const;
106  int string_height_os(const std::string &text, int max_width = -1) const;
107  int string_height_os(const char *text, int length = -1, int max_width = -1) const;
108  tbx::Size string_size_os(const std::string &text, int max_width = -1) const;
109  tbx::Size string_size_os(const char *text, int length = -1, int max_width = -1) const;
110  tbx::BBox string_bounds_os(const std::string &text, int max_width = -1) const;
111  tbx::BBox string_bounds_os(const char *text, int length = -1, int max_width = -1) const;
112 
113  void get_char_bounds_mp(BBox &bounds, char c);
114  void get_char_bounds_os(BBox &bounds, char c);
115 
116  int find_split_os(const char *text, int length, int width, int split_char =-1);
117  int find_index_xy_os(const char *text, int length, int x, int y);
118 
119  BBox bounding_box() const;
120  void get_bounding_box(BBox &bounds) const;
121 
122  // Operators
123  Font &operator=(const Font &other);
124  bool operator==(const Font &other);
125  bool operator!=(const Font &other);
126 
127  private:
128  class FontRef
129  {
130  int ref_count;
131  public:
132  FontRef(int handle);
133 
134  void add_ref() {ref_count++;}
135  void release();
136 
137  int handle;
138  protected:
139  ~FontRef();
140  } *_font_ref;
141 
142  static FontRef *s_invalid_font_ref;
143  static FontRef *s_desktop_font_ref;
144  };
145 
146  // Conversion functions for points
147  // Notes: assumes 180 os units per inch - also rounds down
148  // 1 points is 1/72 of an inch
149 
159  inline int os_to_points(int os) {return (os<<1) / 5;}
169  inline int points_to_os(int points) {return (5 * points) >> 1;}
170 
180  inline int os_to_points_16th(int os) {return (os<<5) / 5;}
190  inline int points_16th_to_os(int point16th) {return (5 * point16th) >> 5;}
191 
201  inline int os_to_millipoints(int os) {return 400 * os;}
211  inline int millipoints_to_os(int millipoint) {return millipoint / 400;}
212 
217  class WimpFont
218  {
219  public:
220  void set_colours(Colour foreground, Colour background);
221 
227  int height_os() const {return 32;}
228  int string_width_os(const std::string &text, int num_chars = 0);
229 
233  enum PaintFlags {WFPF_NONE, WFPF_VCENTRE = (1<<30), WFPF_RJUSTIFY = (1<<31)};
234  void paint(int x, int y, const std::string &text, int flags = WimpFont::WFPF_NONE) const;
235  void paint(int x, int y, const char *text, int length = -1, int flags = WimpFont::WFPF_NONE) const;
236  };
237 
241  namespace fpcs
242  {
249  inline std::string move_x(int move)
250  {
251  std::string t("\x09xxx");
252  t[1] = move & 0xFF;
253  t[2] = (move & 0xFF00) >> 8;
254  t[3] = (move & 0xFF0000) >> 16;
255  return t;
256  }
257 
263  inline std::string move_x_os(int move) {return move_x(tbx::os_to_millipoints(move));}
264 
271  inline std::string move_y(int move)
272  {
273  std::string t("\x0bxxx");
274  t[1] = move & 0xFF;
275  t[2] = (move & 0xFF00) >> 8;
276  t[3] = (move & 0xFF0000) >> 16;
277  return t;
278  }
279 
285  inline std::string move_y_os(int move) {return move_y(tbx::os_to_millipoints(move));}
286 
295  inline std::string colour(tbx::Colour fore, tbx::Colour back, int offset = 14)
296  {
297  std::string t("\x13rgbRGBo");
298  t[1] = char(back.red());
299  t[2] = char(back.green());
300  t[3] = char(back.blue());
301  t[4] = char(fore.red());
302  t[5] = char(fore.green());
303  t[6] = char(fore.blue());
304  t[7] = char(offset);
305  return t;
306  }
307 
314  inline std::string underline(int base, int thickness)
315  {
316  std::string t("0x19bt");
317  t[1] = char(base);
318  t[2] = char(thickness);
319  return t;
320  }
321 
328  inline std::string font(const tbx::Font &fnt)
329  {
330  std::string t("0x1af");
331  t[1] = char(fnt.handle());
332  return t;
333  }
334  }
335 };
336 
337 #endif
bool is_valid() const
Check if this object is assigned to an outline font on the system.
Definition: font.h:79
std::string move_x_os(int move)
Move position horizontally in OS units.
Definition: font.h:263
Class to represent a two-dimensional size.
Definition: size.h:34
int os_to_points(int os)
Convert from OS units to points.
Definition: font.h:159
Size size() const
Get the size of the font.
Definition: font.cc:771
void get_bounding_box(BBox &bounds) const
Get the bounding box (in os coordinates) of the font.
Definition: font.cc:908
void blue(int b)
Change the blue component of the colour.
Definition: colour.h:90
int width_os() const
Get the width of the font.
Definition: font.cc:874
int os_to_millipoints(int os)
Convert from OS units to millipoints.
Definition: font.h:201
Size size_os() const
Get the size of the font.
Definition: font.cc:832
Class to represent a two dimensional bounding box.
Definition: bbox.h:37
bool operator==(const Font &other)
Check if this object refers to the same OS font as another object.
Definition: font.cc:624
int height() const
Get the height of the font.
Definition: font.cc:794
int height_os() const
Return height of the font.
Definition: font.h:227
bool find(const std::string &font_name, int width, int height=-1)
Find the specified font and attach it to this object.
Definition: font.cc:102
std::string colour(tbx::Colour fore, tbx::Colour back, int offset=14)
Set foreground and background font colours.
Definition: font.h:295
int y_resolution
y resolution in dots per inch
Definition: font.h:43
void get_char_bounds_os(BBox &bounds, char c)
Get bounding box for a character in OS Units.
Definition: font.cc:668
std::string identifier() const
Return full font identifier string for this font.
Definition: font.cc:747
std::string font(const tbx::Font &fnt)
Change font in the string.
Definition: font.h:328
int os_to_points_16th(int os)
Convert from OS units to 16th of a point.
Definition: font.h:180
std::string move_y_os(int move)
Move position vertically in OS units.
Definition: font.h:285
int string_height_os(const std::string &text, int max_width=-1) const
Get height of string in os units.
Definition: font.cc:329
int handle() const
Get RISC OS font handle.
Definition: font.h:73
void set_colours(Colour foreground, Colour background)
Set the foreground and background colour for the WIMP font.
Definition: font.cc:955
BBox bounding_box() const
Get the bounding box (in os coordinates) of the font.
Definition: font.cc:894
int usage_count
usage count of font
Definition: font.h:45
int points_16th_to_os(int point16th)
Convert from 16ths of a point to OS units.
Definition: font.h:190
int y_point_size
y point size in 16th of a point
Definition: font.h:41
int x_point_size
x point size in 16th of a point
Definition: font.h:40
int height_os() const
Get the height of the font.
Definition: font.cc:855
bool desktop_font()
Sets font to current desktop font.
Definition: font.cc:134
tbx::Size string_size_os(const std::string &text, int max_width=-1) const
Get size of string in os units.
Definition: font.cc:366
Font()
Construct an uninitialised font.
Definition: font.cc:47
void read_details(FontDetails &details) const
Read the details of the font.
Definition: font.cc:689
Structure used to return details of a font.
Definition: font.h:38
int string_width_os(const std::string &text) const
Get the width of a string.
Definition: font.cc:255
Font & operator=(const Font &other)
Assign a font to the same OS font as another Font object.
Definition: font.cc:609
void paint(int x, int y, const std::string &text, int flags=WimpFont::WFPF_NONE) const
Paint the wimp font at the given screen coordinates.
Definition: font.cc:993
tbx::BBox string_bounds_os(const std::string &text, int max_width=-1) const
Get bounding box of a string in os units.
Definition: font.cc:441
void red(int r)
Change the red component of the colour.
Definition: colour.h:78
virtual ~Font()
Destructor release the OS Font if no other font objects are using it.
Definition: font.cc:86
void paint(int x, int y, const std::string &text, int flags=Font::FPF_NONE) const
Paint the font to the screen.
Definition: font.cc:160
PaintFlags
Flags used for painting the WimpFont.
Definition: font.h:233
std::string move_x(int move)
Move position horizontally.
Definition: font.h:249
int points_to_os(int points)
Convert from points to OS units.
Definition: font.h:169
void get_char_bounds_mp(BBox &bounds, char c)
Get bounding box for a character in millipoints.
Definition: font.cc:646
int find_split_os(const char *text, int length, int width, int split_char=-1)
Find index where to split a string to fit in the given width.
Definition: font.cc:520
std::string move_y(int move)
Move position vertically.
Definition: font.h:271
void green(int g)
Change the green component of the colour.
Definition: colour.h:84
int width() const
Get the width of the font.
Definition: font.cc:813
PaintFlags
Flags to control font painting operation.
Definition: font.h:98
int string_width_os(const std::string &text, int num_chars=0)
Get the width of the text in OS units.
Definition: font.cc:968
Class to represent a RGB colour.
Definition: colour.h:43
int find_index_xy_os(const char *text, int length, int x, int y)
Find index in a string of the given co-ordinate, rounded to the nearest caret position.
Definition: font.cc:558
Class to handle painting and measuring text using an outline font.
Definition: font.h:60
int font_age
age of font
Definition: font.h:44
std::string underline(int base, int thickness)
Turn on or off underlining.
Definition: font.h:314
int x_resolution
x resolution in dots per inch
Definition: font.h:42
Class to display and measure text using the current desktop font.
Definition: font.h:217
int string_width_mp(const std::string &text) const
Get the width of a string.
Definition: font.cc:210
Structure used to return full details of the font which also includes the font identifier.
Definition: font.h:52
int millipoints_to_os(int millipoint)
Convert from millipoints to OS units.
Definition: font.h:211
void set_colours(Colour fore, Colour back, int colourOffset=14)
Set the colours used to paint this font.
Definition: font.cc:591
std::string identifier
Full font identifier string.
Definition: font.h:54
bool operator!=(const Font &other)
Check if this object does not refer to the same OS font as another object.
Definition: font.cc:635