Font.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. //! Adds an alphabet to the list.
  108. //! If an alphabet with the same font size already exists,
  109. //! the alphabet is not added \param alphabet The alphabet
  110. //! to add \return (true) if the alphabet was added. (false) otherwise.
  111. DLLEXPORT bool addAlphabet(Alphabet* alphabet);
  112. //! Removes an alphabet of a specific font size from the list
  113. //! \param sg The font size of the alphabet to remove
  114. //! \return (true) if an alphabet was removed. (false) otherwise
  115. DLLEXPORT bool removeAlphabet(unsigned char sg);
  116. //! Returns a specific alphabet with increased reference counter
  117. //! \param sg The font size whose alphabet is sought
  118. //! \return (0) if no matching alphabet was found
  119. DLLEXPORT Alphabet* getAlphabet(unsigned char sg) const;
  120. //! Returns a specific alphabet without increased reference counter
  121. //! \param sg The font size whose alphabet is sought
  122. //! \return (0) if no matching alphabet was found
  123. DLLEXPORT Alphabet* zAlphabet(unsigned char sg) const;
  124. };
  125. //! Stores all characters of a font in different font sizes
  126. class Font : public virtual ReferenceCounter
  127. {
  128. private:
  129. unsigned char alphabetCount;
  130. AlphabetArray* alphabet;
  131. public:
  132. //! Creates an empty font
  133. DLLEXPORT Font();
  134. //! Deletes the object
  135. DLLEXPORT ~Font();
  136. //! Adds an alphabet to the font. If an alphabet of the same font
  137. //! size already exists, the alphabet is not added
  138. //! \param alphabet The alphabet to add
  139. //! \return (true) if the alphabet was added. (false) otherwise
  140. DLLEXPORT bool addAlphabet(Alphabet* alphabet);
  141. //! Removes a specific alphabet from the font
  142. //! \param sg The font size whose alphabet should be removed
  143. DLLEXPORT void removeAlphabet(unsigned char sg);
  144. //! Returns a specific alphabet with increased reference counter
  145. //! \param sg The font size whose alphabet is sought
  146. //! \return (0) if no matching alphabet was found
  147. DLLEXPORT Alphabet* getAlphabet(unsigned char sg) const;
  148. //! Returns a specific alphabet without increased reference counter
  149. //! \param sg The font size whose alphabet is sought
  150. //! \return (0) if no matching alphabet was found
  151. DLLEXPORT Alphabet* zAlphabet(unsigned char sg) const;
  152. //! Returns how many alphabets (and thus font sizes) the font contains
  153. DLLEXPORT unsigned char getAlphabetCount() const;
  154. };
  155. class TextRenderer : public virtual ReferenceCounter
  156. {
  157. protected:
  158. Font* s;
  159. int fontSize;
  160. int zeilenAbstand;
  161. int zeichenAbstand;
  162. int charWidths[256];
  163. int charHeights[256];
  164. public:
  165. DLLEXPORT TextRenderer();
  166. DLLEXPORT TextRenderer(Font* font);
  167. DLLEXPORT virtual ~TextRenderer();
  168. DLLEXPORT void setFontZ(Font* font);
  169. DLLEXPORT Font* getFont();
  170. DLLEXPORT Font* zFont();
  171. //! Sets the font size to draw with. The font automatically
  172. //! selects the appropriate alphabet for drawing \param sg The font size
  173. DLLEXPORT void setFontSize(int sg);
  174. //! Sets the line spacing to use for drawing
  175. //! \param za The line spacing to the bottom of the line above in pixels
  176. DLLEXPORT void setLineSpacing(int za);
  177. //! Sets the character spacing to use for drawing
  178. //! \param za The character spacing in pixels
  179. DLLEXPORT void setCharSpacing(int za);
  180. //! Inserts line breaks into the text so it is fully displayed
  181. //! at a given width \param zText The text to insert line breaks into
  182. //! \param maxWidth The width in pixels at which the text should be
  183. //! fully displayed
  184. DLLEXPORT virtual void formatText(Text* zText, int maxWidth);
  185. //! Draws a specific text with cursor and coloring onto an image.
  186. //! Use (setPosition) and (setDrawFontGroesse) to change position
  187. //! and size \param x x position of the first character
  188. //! \param y y position of the first character
  189. //! \param txt The text to draw
  190. //! \param zRObj The image to draw on
  191. //! \param cpos The position of the cursor in the text
  192. //! \param cf The color of the cursor
  193. //! \param fbeg The position of the character in the text where coloring
  194. //! should begin. The text is colored from there to the cursor position
  195. //! \param ff The background color of the colored text
  196. //! \param f A function called for each character that returns its color
  197. DLLEXPORT virtual void renderText(int x,
  198. int y,
  199. const char* txt,
  200. Image& zRObj,
  201. std::function<int(int, int, int)> f,
  202. int cpos = -1,
  203. int cf = 0,
  204. int fbeg = -1,
  205. int ff = 0);
  206. //! Draws a specific text with cursor and coloring onto an image.
  207. //! Use (setPosition) and (setDrawFontGroesse) to change position
  208. //! and size \param x x position of the first character
  209. //! \param y y position of the first character
  210. //! \param txt The text to draw
  211. //! \param zRObj The image to draw on
  212. //! \param cpos The position of the cursor in the text
  213. //! \param cf The color of the cursor
  214. //! \param fbeg The position of the character in the text where coloring
  215. //! should begin. The text is colored from there to the cursor position
  216. //! \param ff The background color of the colored text
  217. //! \param f The color in which the text should be drawn
  218. DLLEXPORT virtual void renderText(int x,
  219. int y,
  220. const char* txt,
  221. Image& zRObj,
  222. int f,
  223. int cpos = -1,
  224. int cf = 0,
  225. int fbeg = -1,
  226. int ff = 0);
  227. //! Draws a specific character with coloring onto an image.
  228. //! Use (setPosition) and (setDrawFontGroesse) to change position
  229. //! and size \param x x position of the first character
  230. //! \param y y position of the first character
  231. //! \param txt The text to draw
  232. //! \param zRObj The image to draw on
  233. //! \param color The color in which the text should be drawn
  234. //! \param underlined 1 if the text should be underlined
  235. //! \param selected 1 if the character should be colored
  236. //! \param selectedBackgroundColor The background color of the
  237. //! colored text
  238. DLLEXPORT virtual void renderChar(int& x,
  239. int y,
  240. char c,
  241. Image& zRObj,
  242. int color,
  243. bool underlined = 0,
  244. bool selected = 0,
  245. int selectedBackgroundColor = 0);
  246. //! Returns the font size used for drawing
  247. DLLEXPORT int getFontSize() const;
  248. //! Determines how many pixels are needed to fully display a specific text
  249. //! \param txt The text whose width in pixels should be determined
  250. DLLEXPORT virtual int getTextWidth(const char* txt) const;
  251. //! Determines how many pixels are needed to fully display a specific text
  252. //! \param txt The text whose height in pixels should be determined
  253. DLLEXPORT virtual int getTextHeight(const char* txt) const;
  254. //! Determines how many pixels are needed to fully display a specific
  255. //! character \param c The character whose width in pixels should be
  256. //! determined
  257. DLLEXPORT virtual int getCharWidth(const char c) const;
  258. //! Determines the maximum pixels needed to fully display a character
  259. DLLEXPORT virtual int getMaxCharWidth() const;
  260. //! Determines how many pixels are needed to fully display a specific
  261. //! character \param c The character whose height in pixels should be
  262. //! determined
  263. DLLEXPORT virtual int getCharHeight(const char c) const;
  264. //! Returns the line spacing in pixels to the bottom of the line above
  265. DLLEXPORT int getLineSpacing() const;
  266. //! Returns the character spacing in pixels on the x axis
  267. DLLEXPORT int getCharSpacing() const;
  268. //! Returns the scaled height needed by a drawn line in pixels
  269. DLLEXPORT int getRowHeight() const;
  270. //! Determines the character in the text that the mouse points to
  271. //! \param zTxt The text the mouse points to
  272. //! \param mouseX The X position of the mouse in pixels relative to the
  273. //! position of the first character \param mouseY The Y position of the
  274. //! mouse in pixels relative to the position of the first character
  275. DLLEXPORT virtual int textPos(
  276. const char* txt, int mouseX, int mouseY) const;
  277. };
  278. class EngravedTextRenderer : public TextRenderer
  279. {
  280. public:
  281. DLLEXPORT EngravedTextRenderer();
  282. DLLEXPORT EngravedTextRenderer(Font* font);
  283. DLLEXPORT virtual ~EngravedTextRenderer();
  284. //! Draws a specific character with coloring onto an image.
  285. //! Use (setPosition) and (setDrawFontGroesse) to change position
  286. //! and size \param x x position of the first character
  287. //! \param y y position of the first character
  288. //! \param txt The text to draw
  289. //! \param zRObj The image to draw on
  290. //! \param color The color in which the text should be drawn
  291. //! \param underlined 1 if the text should be underlined
  292. //! \param selected 1 if the character should be colored
  293. //! \param selectedBackgroundColor The background color of the
  294. //! colored text
  295. DLLEXPORT virtual void renderChar(int& x,
  296. int y,
  297. char c,
  298. Image& zRObj,
  299. int color,
  300. bool underlined = 0,
  301. bool selected = 0,
  302. int selectedBackgroundColor = 0) override;
  303. //! Determines how many pixels are needed to fully display a specific
  304. //! character \param c The character whose width in pixels should be
  305. //! determined
  306. DLLEXPORT virtual int getCharWidth(const char c) const override;
  307. //! Determines how many pixels are needed to fully display a specific
  308. //! character \param c The character whose height in pixels should be
  309. //! determined
  310. DLLEXPORT virtual int getCharHeight(const char c) const override;
  311. };
  312. class ItalicTextRenderer : public TextRenderer
  313. {
  314. public:
  315. DLLEXPORT ItalicTextRenderer();
  316. DLLEXPORT ItalicTextRenderer(Font* font);
  317. DLLEXPORT virtual ~ItalicTextRenderer();
  318. //! Draws a specific character with coloring onto an image.
  319. //! Use (setPosition) and (setDrawFontGroesse) to change position
  320. //! and size \param x x position of the first character
  321. //! \param y y position of the first character
  322. //! \param txt The text to draw
  323. //! \param zRObj The image to draw on
  324. //! \param color The color in which the text should be drawn
  325. //! \param underlined 1 if the text should be underlined
  326. //! \param selected 1 if the character should be colored
  327. //! \param selectedBackgroundColor The background color of the
  328. //! colored text
  329. DLLEXPORT virtual void renderChar(int& x,
  330. int y,
  331. char c,
  332. Image& zRObj,
  333. int color,
  334. bool underlined = 0,
  335. bool selected = 0,
  336. int selectedBackgroundColor = 0) override;
  337. //! Determines how many pixels are needed to fully display a specific
  338. //! character \param c The character whose width in pixels should be
  339. //! determined
  340. DLLEXPORT virtual int getCharWidth(const char c) const override;
  341. };
  342. } // namespace Framework
  343. #endif