Font.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #ifndef Font_H
  2. #define Font_H
  3. #include <functional>
  4. #include "Critical.h"
  5. #include "Point.h"
  6. #include "ReferenceCounter.h"
  7. namespace Framework
  8. {
  9. class Image; //! Image.h
  10. class Text; //! Text.h
  11. class Character; //! from this file
  12. class Alphabet; //! from this file
  13. class Font; //! from this file
  14. //! Stores the alpha values of a character of a specific font.
  15. //! The other color values are determined by the font color. Hence only
  16. //! the alpha values.
  17. class Character : public virtual ReferenceCounter
  18. {
  19. private:
  20. Point size;
  21. unsigned char* alpha;
  22. int fontSize;
  23. public:
  24. //! Creates a new empty object
  25. DLLEXPORT Character();
  26. //! Deletes the object
  27. DLLEXPORT ~Character();
  28. //! Creates a new character with a specific size
  29. //! \param size The size of the character in pixels
  30. DLLEXPORT void NewCharacter(Point& size);
  31. //! Sets the alpha value of a specific pixel
  32. //! \param pos The position of the pixel
  33. //! \param alpha The value of the pixel
  34. DLLEXPORT void setPixel(Point& pos, unsigned char alpha);
  35. //! Sets the alpha value of a specific pixel
  36. //! \param x The x position of the pixel
  37. //! \param y The y position of the pixel
  38. //! \param alpha The value of the pixel
  39. DLLEXPORT void setPixel(int x, int y, unsigned char alpha);
  40. //! Sets the alpha value of a specific pixel
  41. //! \param i The index of the pixel. Calculated as x + y *
  42. //! width and ranges from 0 to width * height - 1 \param alpha The value
  43. //! of the pixel
  44. DLLEXPORT void setPixel(int i, unsigned char alpha);
  45. //! Sets the font size this character belongs to
  46. //! \param sg The font size of the character.
  47. DLLEXPORT void setFontSize(int sg);
  48. //! Returns the font size this character belongs to
  49. DLLEXPORT int getFontSize() const;
  50. //! Returns the alpha values of the character as an array where the
  51. //! values are stored row by row
  52. DLLEXPORT unsigned char* getBuff() const;
  53. //! Returns the unscaled size of the character in pixels.
  54. DLLEXPORT const Point& getSize() const;
  55. //! Returns the width of the character in pixels
  56. DLLEXPORT int getWidth() const;
  57. //! Returns the height of the character in pixels
  58. DLLEXPORT int getHeight() const;
  59. };
  60. //! Stores all characters of the same font size.
  61. //! Used by the Font class
  62. class Alphabet : public virtual ReferenceCounter
  63. {
  64. private:
  65. Character** zeichen;
  66. int fontSize;
  67. public:
  68. //! Creates an empty object
  69. DLLEXPORT Alphabet();
  70. //! Deletes the object
  71. DLLEXPORT ~Alphabet();
  72. //! Deletes all stored characters
  73. DLLEXPORT void NeuAlphabet();
  74. //! Adds a character to the alphabet.
  75. //! If the added character already exists, it will be overwritten
  76. //! \param i The ASCII code of the character to add
  77. //! \param buchstabe The character to add
  78. DLLEXPORT void setCharacter(unsigned char i, Character* buchstabe);
  79. //! Sets the font size of the alphabet and its stored characters
  80. //! \param gr The font size of the alphabet
  81. DLLEXPORT void setFontSize(int gr);
  82. //! Returns the stored character for a specific ASCII code
  83. //! \param i The ASCII code of the character \return A pointer to
  84. //! the character with increased reference counter
  85. DLLEXPORT Character* getCharacter(unsigned char i) const;
  86. //! Returns the stored character for a specific ASCII code
  87. //! \param i The ASCII code of the character \return A pointer to
  88. //! the character without increased reference counter
  89. DLLEXPORT Character* zCharacter(unsigned char i) const;
  90. //! Checks whether a character exists for a specific ASCII code
  91. //! \param b The ASCII code to check
  92. //! \return (true) if a character was found for the code. (false)
  93. //! otherwise
  94. DLLEXPORT bool hasCharacter(unsigned char b) const;
  95. //! Returns the font size whose characters are stored in this alphabet
  96. DLLEXPORT int getFontSize() const;
  97. };
  98. //! Stores a list of alphabets with different font sizes.
  99. //! Used by the Font class to store all characters sorted by font size.
  100. class AlphabetArray
  101. {
  102. private:
  103. Alphabet* alphabets[256];
  104. public:
  105. //! Creates a new list
  106. DLLEXPORT AlphabetArray();
  107. DLLEXPORT ~AlphabetArray();
  108. //! Adds an alphabet to the list.
  109. //! If an alphabet with the same font size already exists,
  110. //! the alphabet is not added \param alphabet The alphabet
  111. //! to add \return (true) if the alphabet was added. (false) otherwise.
  112. DLLEXPORT bool addAlphabet(Alphabet* alphabet);
  113. //! Removes an alphabet of a specific font size from the list
  114. //! \param sg The font size of the alphabet to remove
  115. //! \return (true) if an alphabet was removed. (false) otherwise
  116. DLLEXPORT bool removeAlphabet(unsigned char sg);
  117. //! Returns a specific alphabet with increased reference counter
  118. //! \param sg The font size whose alphabet is sought
  119. //! \return (0) if no matching alphabet was found
  120. DLLEXPORT Alphabet* getAlphabet(unsigned char sg) const;
  121. //! Returns a specific alphabet without increased reference counter
  122. //! \param sg The font size whose alphabet is sought
  123. //! \return (0) if no matching alphabet was found
  124. DLLEXPORT Alphabet* zAlphabet(unsigned char sg) const;
  125. };
  126. //! Stores all characters of a font in different font sizes
  127. class Font : public virtual ReferenceCounter
  128. {
  129. private:
  130. unsigned char alphabetCount;
  131. AlphabetArray* alphabet;
  132. public:
  133. //! Creates an empty font
  134. DLLEXPORT Font();
  135. //! Deletes the object
  136. DLLEXPORT ~Font();
  137. //! Adds an alphabet to the font. If an alphabet of the same font
  138. //! size already exists, the alphabet is not added
  139. //! \param alphabet The alphabet to add
  140. //! \return (true) if the alphabet was added. (false) otherwise
  141. DLLEXPORT bool addAlphabet(Alphabet* alphabet);
  142. //! Removes a specific alphabet from the font
  143. //! \param sg The font size whose alphabet should be removed
  144. DLLEXPORT void removeAlphabet(unsigned char sg);
  145. //! Returns a specific alphabet with increased reference counter
  146. //! \param sg The font size whose alphabet is sought
  147. //! \return (0) if no matching alphabet was found
  148. DLLEXPORT Alphabet* getAlphabet(unsigned char sg) const;
  149. //! Returns a specific alphabet without increased reference counter
  150. //! \param sg The font size whose alphabet is sought
  151. //! \return (0) if no matching alphabet was found
  152. DLLEXPORT Alphabet* zAlphabet(unsigned char sg) const;
  153. //! Returns how many alphabets (and thus font sizes) the font contains
  154. DLLEXPORT unsigned char getAlphabetCount() const;
  155. };
  156. class TextRenderer : public virtual ReferenceCounter
  157. {
  158. protected:
  159. Font* s;
  160. int fontSize;
  161. int lineSpacing;
  162. int charSpacing;
  163. int charWidths[256];
  164. int charHeights[256];
  165. public:
  166. DLLEXPORT TextRenderer();
  167. DLLEXPORT TextRenderer(Font* font);
  168. DLLEXPORT virtual ~TextRenderer();
  169. DLLEXPORT void setFontZ(Font* font);
  170. DLLEXPORT Font* getFont();
  171. DLLEXPORT Font* zFont();
  172. //! Sets the font size to draw with. The font automatically
  173. //! selects the appropriate alphabet for drawing \param sg The font size
  174. DLLEXPORT void setFontSize(int sg);
  175. //! Sets the line spacing to use for drawing
  176. //! \param za The line spacing to the bottom of the line above in pixels
  177. DLLEXPORT void setLineSpacing(int za);
  178. //! Sets the character spacing to use for drawing
  179. //! \param za The character spacing in pixels
  180. DLLEXPORT void setCharSpacing(int za);
  181. //! Inserts line breaks into the text so it is fully displayed
  182. //! at a given width \param zText The text to insert line breaks into
  183. //! \param maxWidth The width in pixels at which the text should be
  184. //! fully displayed
  185. DLLEXPORT virtual void formatText(Text* zText, int maxWidth);
  186. //! Draws a specific text with cursor and coloring onto an image.
  187. //! Use (setPosition) and (setDrawFontGroesse) to change position
  188. //! and size \param x x position of the first character
  189. //! \param y y position of the first character
  190. //! \param txt The text to draw
  191. //! \param zRObj The image to draw on
  192. //! \param cpos The position of the cursor in the text
  193. //! \param cf The color of the cursor
  194. //! \param fbeg The position of the character in the text where coloring
  195. //! should begin. The text is colored from there to the cursor position
  196. //! \param ff The background color of the colored text
  197. //! \param f A function called for each character that returns its color
  198. DLLEXPORT virtual void renderText(int x,
  199. int y,
  200. const char* txt,
  201. Image& zRObj,
  202. std::function<int(int, int, int)> f,
  203. int cpos = -1,
  204. int cf = 0,
  205. int fbeg = -1,
  206. int ff = 0);
  207. //! Draws a specific text with cursor and coloring onto an image.
  208. //! Use (setPosition) and (setDrawFontGroesse) to change position
  209. //! and size \param x x position of the first character
  210. //! \param y y position of the first character
  211. //! \param txt The text to draw
  212. //! \param zRObj The image to draw on
  213. //! \param cpos The position of the cursor in the text
  214. //! \param cf The color of the cursor
  215. //! \param fbeg The position of the character in the text where coloring
  216. //! should begin. The text is colored from there to the cursor position
  217. //! \param ff The background color of the colored text
  218. //! \param f The color in which the text should be drawn
  219. DLLEXPORT virtual void renderText(int x,
  220. int y,
  221. const char* txt,
  222. Image& zRObj,
  223. int f,
  224. int cpos = -1,
  225. int cf = 0,
  226. int fbeg = -1,
  227. int ff = 0);
  228. //! Draws a specific character with coloring onto an image.
  229. //! Use (setPosition) and (setDrawFontGroesse) to change position
  230. //! and size \param x x position of the first character
  231. //! \param y y position of the first character
  232. //! \param txt The text to draw
  233. //! \param zRObj The image to draw on
  234. //! \param color The color in which the text should be drawn
  235. //! \param underlined 1 if the text should be underlined
  236. //! \param selected 1 if the character should be colored
  237. //! \param selectedBackgroundColor The background color of the
  238. //! colored text
  239. DLLEXPORT virtual void renderChar(int& x,
  240. int y,
  241. char c,
  242. Image& zRObj,
  243. int color,
  244. bool underlined = 0,
  245. bool selected = 0,
  246. int selectedBackgroundColor = 0);
  247. //! Returns the font size used for drawing
  248. DLLEXPORT int getFontSize() const;
  249. //! Determines how many pixels are needed to fully display a specific
  250. //! text
  251. //! \param txt The text whose width in pixels should be determined
  252. DLLEXPORT virtual int getTextWidth(const char* txt) const;
  253. //! Determines how many pixels are needed to fully display a specific
  254. //! text
  255. //! \param txt The text whose height in pixels should be determined
  256. DLLEXPORT virtual int getTextHeight(const char* txt) const;
  257. //! Determines how many pixels are needed to fully display a specific
  258. //! character \param c The character whose width in pixels should be
  259. //! determined
  260. DLLEXPORT virtual int getCharWidth(const char c) const;
  261. //! Determines the maximum pixels needed to fully display a character
  262. DLLEXPORT virtual int getMaxCharWidth() const;
  263. //! Determines how many pixels are needed to fully display a specific
  264. //! character \param c The character whose height in pixels should be
  265. //! determined
  266. DLLEXPORT virtual int getCharHeight(const char c) const;
  267. //! Returns the line spacing in pixels to the bottom of the line above
  268. DLLEXPORT int getLineSpacing() const;
  269. //! Returns the character spacing in pixels on the x axis
  270. DLLEXPORT int getCharSpacing() const;
  271. //! Returns the scaled height needed by a drawn line in pixels
  272. DLLEXPORT int getRowHeight() const;
  273. //! Determines the character in the text that the mouse points to
  274. //! \param zTxt The text the mouse points to
  275. //! \param mouseX The X position of the mouse in pixels relative to the
  276. //! position of the first character \param mouseY The Y position of the
  277. //! mouse in pixels relative to the position of the first character
  278. DLLEXPORT virtual int textPos(
  279. const char* txt, int mouseX, int mouseY) const;
  280. };
  281. class EngravedTextRenderer : public TextRenderer
  282. {
  283. public:
  284. DLLEXPORT EngravedTextRenderer();
  285. DLLEXPORT EngravedTextRenderer(Font* font);
  286. DLLEXPORT virtual ~EngravedTextRenderer();
  287. //! Draws a specific character with coloring onto an image.
  288. //! Use (setPosition) and (setDrawFontGroesse) to change position
  289. //! and size \param x x position of the first character
  290. //! \param y y position of the first character
  291. //! \param txt The text to draw
  292. //! \param zRObj The image to draw on
  293. //! \param color The color in which the text should be drawn
  294. //! \param underlined 1 if the text should be underlined
  295. //! \param selected 1 if the character should be colored
  296. //! \param selectedBackgroundColor The background color of the
  297. //! colored text
  298. DLLEXPORT virtual void renderChar(int& x,
  299. int y,
  300. char c,
  301. Image& zRObj,
  302. int color,
  303. bool underlined = 0,
  304. bool selected = 0,
  305. int selectedBackgroundColor = 0) override;
  306. //! Determines how many pixels are needed to fully display a specific
  307. //! character \param c The character whose width in pixels should be
  308. //! determined
  309. DLLEXPORT virtual int getCharWidth(const char c) const override;
  310. //! Determines how many pixels are needed to fully display a specific
  311. //! character \param c The character whose height in pixels should be
  312. //! determined
  313. DLLEXPORT virtual int getCharHeight(const char c) const override;
  314. };
  315. class ItalicTextRenderer : public TextRenderer
  316. {
  317. public:
  318. DLLEXPORT ItalicTextRenderer();
  319. DLLEXPORT ItalicTextRenderer(Font* font);
  320. DLLEXPORT virtual ~ItalicTextRenderer();
  321. //! Draws a specific character with coloring onto an image.
  322. //! Use (setPosition) and (setDrawFontGroesse) to change position
  323. //! and size \param x x position of the first character
  324. //! \param y y position of the first character
  325. //! \param txt The text to draw
  326. //! \param zRObj The image to draw on
  327. //! \param color The color in which the text should be drawn
  328. //! \param underlined 1 if the text should be underlined
  329. //! \param selected 1 if the character should be colored
  330. //! \param selectedBackgroundColor The background color of the
  331. //! colored text
  332. DLLEXPORT virtual void renderChar(int& x,
  333. int y,
  334. char c,
  335. Image& zRObj,
  336. int color,
  337. bool underlined = 0,
  338. bool selected = 0,
  339. int selectedBackgroundColor = 0) override;
  340. //! Determines how many pixels are needed to fully display a specific
  341. //! character \param c The character whose width in pixels should be
  342. //! determined
  343. DLLEXPORT virtual int getCharWidth(const char c) const override;
  344. };
  345. } // namespace Framework
  346. #endif