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

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