Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Font.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*!
  2. * \file src/system/Font.cpp
  3. * \brief Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Log.h"
  9. #include "utils/strings.h"
  10. #include "system/Window.h"
  11. #include "system/FontImGui.h"
  12. #include "system/FontTRLE.h"
  13. #include "system/FontTTF.h"
  14. #include "system/Font.h"
  15. bool Font::isInit = false;
  16. std::string Font::fontName;
  17. void Font::shutdown() {
  18. FontTRLE::shutdown();
  19. FontTTF::shutdown();
  20. }
  21. int Font::initialize(std::string font) {
  22. fontName = font;
  23. if (stringEndsWith(fontName, ".pc")) {
  24. return FontTRLE::initialize(fontName);
  25. } else if (stringEndsWith(fontName, ".ttf")) {
  26. return FontTTF::initialize(fontName);
  27. }
  28. if (font != "") {
  29. getLog() << "Unknown font file format: " << font << Log::endl;
  30. return -1;
  31. } else {
  32. return 0;
  33. }
  34. }
  35. unsigned int Font::widthText(float scale, std::string s) {
  36. if (stringEndsWith(fontName, ".pc")) {
  37. return FontTRLE::widthText(scale, s);
  38. } else if (stringEndsWith(fontName, ".ttf")) {
  39. return FontTTF::widthText(scale, s);
  40. } else {
  41. return FontImGui::widthText(scale, s);
  42. }
  43. }
  44. unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
  45. if (stringEndsWith(fontName, ".pc")) {
  46. return FontTRLE::heightText(scale, maxWidth, s);
  47. } else if (stringEndsWith(fontName, ".ttf")) {
  48. return FontTTF::heightText(scale, maxWidth, s);
  49. } else {
  50. return FontImGui::heightText(scale, maxWidth, s);
  51. }
  52. }
  53. void Font::drawText(unsigned int x, unsigned int y, float scale,
  54. const unsigned char color[4], std::string s) {
  55. if (stringEndsWith(fontName, ".pc")) {
  56. FontTRLE::drawText(x, y, scale, color, s);
  57. } else if (stringEndsWith(fontName, ".ttf")) {
  58. FontTTF::drawText(x, y, scale, color, s);
  59. } else {
  60. FontImGui::drawText(x, y, scale, color, s);
  61. }
  62. }
  63. void Font::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  64. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  65. if (stringEndsWith(fontName, ".pc")) {
  66. FontTRLE::drawTextWrapped(x, y, scale, color, maxWidth, s);
  67. } else if (stringEndsWith(fontName, ".ttf")) {
  68. FontTTF::drawTextWrapped(x, y, scale, color, maxWidth, s);
  69. } else {
  70. FontImGui::drawTextWrapped(x, y, scale, color, maxWidth, s);
  71. }
  72. }
  73. void Font::drawTextCentered(unsigned int x, unsigned int y, float scale,
  74. const unsigned char color[4], unsigned int width, std::string s) {
  75. drawText(x + ((width / 2) - (widthText(scale, s) / 2)), y, scale, color, s);
  76. }