Loading

Paste #ppfjsacb5

  1. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
  2. index e162bac..837232f 100644
  3. --- a/src/CMakeLists.txt
  4. +++ b/src/CMakeLists.txt
  5. @@ -66,11 +66,10 @@ IF(SDL2_FOUND)
  6.     target_link_libraries(freerct ${SDL2_LIBRARY})
  7.  ENDIF()
  8.  
  9. -find_package(SDL2_ttf REQUIRED)
  10. -# Legacy variable names
  11. -IF(SDL2_TTF_FOUND)
  12. -   include_directories(${SDL2TTF_INCLUDE_DIR})
  13. -   target_link_libraries(freerct ${SDL2TTF_LIBRARY})
  14. +find_package(Freetype REQUIRED)
  15. +IF(FREETYPE_FOUND)
  16. +   include_directories(${FREETYPE_INCLUDE_DIRS})
  17. +   target_link_libraries(freerct ${FREETYPE_LIBRARIES})
  18.  ENDIF()
  19.  
  20.  # Translated messages are bad
  21. diff --git a/src/video.cpp b/src/video.cpp
  22. index 11141ad..c7f3f0f 100644
  23. --- a/src/video.cpp
  24. +++ b/src/video.cpp
  25. @@ -185,29 +185,17 @@ std::string VideoSystem::Initialize(const char *font_name, int font_size)
  26.         printf("Could not set window icon (%s)\n", SDL_GetError());
  27.     }
  28.  
  29. -   if (TTF_Init() != 0) {
  30. -       SDL_Quit();
  31. -       delete[] this->mem;
  32. -       std::string err = "TTF font initialization failed: ";
  33. -       err += TTF_GetError();
  34. -       return err;
  35. +   if (FT_Init_FreeType(&this->library) != 0) {
  36.     }
  37.  
  38. -   this->font = TTF_OpenFont(font_name, font_size);
  39. -   if (this->font == nullptr) {
  40. -       std::string err = "TTF Opening font \"";
  41. -       err += font_name;
  42. -       err += "\" size ";
  43. -       err += std::to_string(font_size);
  44. -       err += " failed: ";
  45. -       err += TTF_GetError();
  46. -       TTF_Quit();
  47. -       SDL_Quit();
  48. -       delete[] this->mem;
  49. -       return err;
  50. +   if (FT_New_Face(this->library, font_name, 0, &this->face) != 0) {
  51. +   }
  52. +
  53. +   /** @todo use actual screen dpi? */
  54. +   if (FT_Set_Char_Size(this->face, 0, font_size * 64, 0, 0) != 0) {
  55.     }
  56.  
  57. -   this->font_height = TTF_FontLineSkip(this->font);
  58. +   this->font_height = this->face->height / 64;
  59.     this->initialized = true;
  60.     this->dirty = true; // Ensure it gets painted.
  61.     this->missing_sprites = false;
  62. @@ -454,8 +442,8 @@ void VideoSystem::MainLoop()
  63.  void VideoSystem::Shutdown()
  64.  {
  65.     if (this->initialized) {
  66. -       TTF_CloseFont(this->font);
  67. -       TTF_Quit();
  68. +       FT_Done_Face(this->face);
  69. +       FT_Done_FreeType(this->library);
  70.         SDL_Quit();
  71.         delete[] this->mem;
  72.         this->initialized = false;
  73. @@ -856,9 +844,29 @@ void VideoSystem::BlitImages(int32 x_base, int32 y_base, const ImageData *spr, u
  74.   */
  75.  void VideoSystem::GetTextSize(const uint8 *text, int *width, int *height)
  76.  {
  77. -   if (TTF_SizeUTF8(this->font, (const char *)text, width, height) != 0) {
  78. -       *width = 0;
  79. -       *height = 0;
  80. +   *width = 0;
  81. +   *height = 0;
  82. +
  83. +   bool use_kerning = FT_HAS_KERNING(this->face);
  84. +   uint previous = 0;
  85. +   FT_GlyphSlot slot = this->face->glyph;
  86. +
  87. +   for (int n = 0; n < strlen((const char*)text); n++) {
  88. +       uint glyph = FT_Get_Char_Index(this->face, text[n]);
  89. +
  90. +       if (use_kerning && previous && glyph) {
  91. +           FT_Vector delta;
  92. +           FT_Get_Kerning(this->face, previous, glyph, FT_KERNING_DEFAULT, &delta);
  93. +
  94. +           *width += delta.x / 64;
  95. +       }
  96. +
  97. +       if (FT_Load_Glyph(this->face, glyph, FT_LOAD_RENDER) != 0) {
  98. +       }
  99. +       *width += slot->advance.x / 64;
  100. +       *height = std::max(*height, (int)slot->metrics.height / 64);
  101. +
  102. +       previous = glyph;
  103.     }
  104.  }
  105.  
  106. @@ -911,18 +919,37 @@ void VideoSystem::GetNumberRangeSize(int64 smallest, int64 biggest, int *width,
  107.   */
  108.  void VideoSystem::BlitText(const uint8 *text, uint32 colour, int xpos, int ypos, int width, Alignment align)
  109.  {
  110. -   SDL_Color col = {0, 0, 0}; // Font colour does not matter as only the bitmap is used.
  111. -   SDL_Surface *surf = TTF_RenderUTF8_Solid(this->font, (const char *)text, col);
  112. -   if (surf == nullptr) {
  113. -       fprintf(stderr, "Rendering text failed (%s)\n", TTF_GetError());
  114. -       return;
  115. -   }
  116. +   bool use_kerning = FT_HAS_KERNING(this->face);
  117. +   uint previous = 0;
  118. +   FT_GlyphSlot slot = this->face->glyph;
  119.  
  120. -   if (surf->format->BitsPerPixel != 8 || surf->format->BytesPerPixel != 1) {
  121. -       fprintf(stderr, "Rendering text failed (Wrong surface format)\n");
  122. -       return;
  123. +   for (int n = 0; n < strlen((const char*)tex); n++) {
  124. +       uint glyph = FT_Get_Char_Index(this->face, tex[n]);
  125. +
  126. +       if (use_kerning && previous && glyph) {
  127. +           FT_Vector delta;
  128. +           FT_Get_Kerning(this->face, previous, glyph, FT_KERNING_DEFAULT, &delta);
  129. +
  130. +       }
  131. +
  132. +       if (FT_Load_Glyph(this->face, glyph, FT_LOAD_RENDER) != 0) {
  133. +       }
  134. +
  135. +       for (int i = 0; i < slot->bitmap.rows; i++) {
  136. +           for (int j = 0; j < slot->bitmap.pitch; j++) {
  137. +               printf("%02x ", slot->bitmap.buffer[i * slot->bitmap.pitch + j]);
  138. +           }
  139. +           printf("\n");
  140. +       }
  141. +
  142. +       printf("\n");
  143. +
  144. +       previous = glyph;
  145.     }
  146.  
  147. +   SDL_Surface *surf = nullptr;
  148. +   return;
  149. +
  150.     int real_w = std::min(surf->w, width);
  151.     switch (align) {
  152.         case ALG_LEFT:
  153. diff --git a/src/video.h b/src/video.h
  154. index 6f435a9..f5bbb2b 100644
  155. --- a/src/video.h
  156. +++ b/src/video.h
  157. @@ -13,8 +13,11 @@
  158.  #define VIDEO_H
  159.  
  160.  #include <set>
  161. +
  162.  #include <SDL.h>
  163. -#include <SDL_ttf.h>
  164. +#include <ft2build.h>
  165. +#include FT_FREETYPE_H
  166. +
  167.  #include "geometry.h"
  168.  #include "palette.h"
  169.  
  170. @@ -160,7 +163,9 @@ private:
  171.     bool initialized; ///< Video system is initialized.
  172.     bool dirty;       ///< Video display needs being repainted.
  173.  
  174. -   TTF_Font *font;             ///< Opened text font.
  175. +   FT_Library library;
  176. +   FT_Face face;
  177. +
  178.     SDL_Window *window;         ///< %Window of the application.
  179.     SDL_Renderer *renderer;     ///< GPU renderer to the application window.
  180.     SDL_Texture *texture;       ///< GPU Texture storage of the application window.
  181.  

Comments