bpp-core3  3.0.0
FontManager.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_GRAPHICS_FONT_FONTMANAGER_H
6 #define BPP_GRAPHICS_FONT_FONTMANAGER_H
7 
8 
9 #include "Font.h"
10 
11 // From the STL:
12 #include <vector>
13 
14 #include "../../Text/TextTools.h"
15 #include "../../Exceptions.h"
16 
17 namespace bpp
18 {
24 template<class CodeType>
26 {
27 public:
29  virtual ~FontManager() {}
30 
31 public:
36  virtual CodeType getCode(const Font& font) const = 0;
37 
43  virtual const Font& getFont(CodeType& code) const = 0;
44 
48  virtual std::vector<CodeType> getCodes() const = 0;
49 
53  virtual std::vector<Font> getFonts() const = 0;
54 
58  virtual size_t getNumberOfFonts() const = 0;
59 };
60 
61 
62 template<class CodeType>
64  public virtual FontManager<CodeType>
65 {
66 private:
67  std::vector<Font> fonts_;
68  std::vector<CodeType> codes_;
69 
70 public:
71  AbstractFontManager() : fonts_(), codes_() {}
72 
73 public:
74  CodeType getCode(const Font& font) const
75  {
76  for (unsigned int i = 0; i < fonts_.size(); i++)
77  {
78  if (fonts_[i] == font)
79  {
80  return codes_[i];
81  }
82  }
83  throw Exception("AbstractFontManager::getCode. Unknown font: " + font.toString());
84  }
85 
86  const Font& getFont(int& code) const
87  {
88  for (unsigned int i = 0; i < codes_.size(); i++)
89  {
90  if (codes_[i] == code)
91  {
92  return fonts_[i];
93  }
94  }
95  throw Exception("AbstractFontManager::getColor. No font associated to this code: " + TextTools::toString(code));
96  }
97  std::vector<CodeType> getCodes() const { return codes_; }
98  std::vector<Font> getFonts() const { return fonts_; }
99  size_t getNumberOfFonts() const { return fonts_.size(); }
100 
101 protected:
102  void registerFont_(const Font& font, int code)
103  {
104  codes_.push_back(code);
105  fonts_.push_back(font);
106  }
107 };
108 } // end of namespace.
109 #endif // BPP_GRAPHICS_FONT_FONTMANAGER_H
std::vector< Font > getFonts() const
Definition: FontManager.h:98
const Font & getFont(int &code) const
Definition: FontManager.h:86
Data structure for fonts.
Definition: Font.h:21
void registerFont_(const Font &font, int code)
Definition: FontManager.h:102
virtual const Font & getFont(CodeType &code) const =0
virtual std::vector< CodeType > getCodes() const =0
std::string toString() const
Definition: Font.h:122
size_t getNumberOfFonts() const
Definition: FontManager.h:99
virtual size_t getNumberOfFonts() const =0
virtual ~FontManager()
Definition: FontManager.h:29
std::vector< CodeType > codes_
Definition: FontManager.h:68
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
virtual std::vector< Font > getFonts() const =0
CodeType getCode(const Font &font) const
Definition: FontManager.h:74
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
Associate special fonts to a code.
Definition: FontManager.h:25
virtual CodeType getCode(const Font &font) const =0
std::vector< CodeType > getCodes() const
Definition: FontManager.h:97
std::vector< Font > fonts_
Definition: FontManager.h:67