💿🐜 Antkeeper source code https://antkeeper.com
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.

164 lines
5.0 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "type/freetype/typeface.hpp"
  20. #include <stdexcept>
  21. #include <string>
  22. namespace type {
  23. namespace freetype {
  24. typeface::typeface(FT_Library library, FT_Face face, unsigned char* buffer):
  25. library(library),
  26. face(face),
  27. buffer(buffer),
  28. height(-1.0f)
  29. {}
  30. typeface::~typeface()
  31. {
  32. FT_Done_Face(face);
  33. delete[] buffer;
  34. FT_Done_FreeType(library);
  35. }
  36. bool typeface::has_kerning() const
  37. {
  38. return FT_HAS_KERNING(face);
  39. }
  40. bool typeface::has_glyph(char32_t code) const
  41. {
  42. return FT_Get_Char_Index(face, static_cast<FT_ULong>(code)) != 0;
  43. }
  44. bool typeface::get_metrics(float height, font_metrics& metrics) const
  45. {
  46. // Set font size
  47. set_face_pixel_size(height);
  48. // Get font metrics
  49. metrics.ascent = face->size->metrics.ascender / 64.0f;
  50. metrics.descent = face->size->metrics.descender / 64.0f;
  51. metrics.linegap = face->size->metrics.height / 64.0f;
  52. metrics.linespace = metrics.ascent - metrics.descent + metrics.linegap;
  53. metrics.underline_position = FT_MulFix(face->underline_position, face->size->metrics.y_scale) / 64.0f;
  54. metrics.underline_thickness = FT_MulFix(face->underline_thickness, face->size->metrics.y_scale) / 64.0f;
  55. metrics.max_horizontal_advance = face->size->metrics.max_advance / 64.0f;
  56. metrics.max_vertical_advance = FT_MulFix(face->max_advance_height, face->size->metrics.y_scale) / 64.0f;
  57. return true;
  58. }
  59. bool typeface::get_metrics(float height, char32_t code, glyph_metrics& metrics) const
  60. {
  61. // Set font size
  62. set_face_pixel_size(height);
  63. // Get index of glyph
  64. FT_UInt glyph_index = FT_Get_Char_Index(face, static_cast<FT_ULong>(code));
  65. // Load glyph and render bitmap
  66. FT_Error error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
  67. if (error)
  68. {
  69. throw std::runtime_error("FreeType failed to load glyph (error code " + std::to_string(error) + ")");
  70. }
  71. // Get glyph metrics
  72. metrics.width = face->glyph->metrics.width / 64.0f;
  73. metrics.height = face->glyph->metrics.height / 64.0f;
  74. metrics.horizontal_bearing.x = face->glyph->metrics.horiBearingX / 64.0f;
  75. metrics.horizontal_bearing.y = face->glyph->metrics.horiBearingY / 64.0f;
  76. metrics.vertical_bearing.x = face->glyph->metrics.vertBearingX / 64.0f;
  77. metrics.vertical_bearing.y = face->glyph->metrics.vertBearingY / 64.0f;
  78. metrics.horizontal_advance = face->glyph->metrics.horiAdvance / 64.0f;
  79. metrics.vertical_advance = face->glyph->metrics.vertAdvance / 64.0f;
  80. return true;
  81. }
  82. bool typeface::get_bitmap(float height, char32_t code, image& bitmap) const
  83. {
  84. // Set font size
  85. set_face_pixel_size(height);
  86. // Get index of glyph
  87. FT_UInt glyph_index = FT_Get_Char_Index(face, static_cast<FT_ULong>(code));
  88. // Load glyph and render bitmap
  89. FT_Error error = FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_NORMAL));
  90. if (error)
  91. {
  92. throw std::runtime_error("FreeType failed to load glyph (error code " + std::to_string(error) + ")");
  93. }
  94. // Format and resize bitmap
  95. bitmap.resize(0, 0);
  96. bitmap.format(sizeof(unsigned char), 1);
  97. bitmap.resize(face->glyph->bitmap.width, face->glyph->bitmap.rows);
  98. // Copy glyph bitmap data in bitmap
  99. std::memcpy(bitmap.get_pixels(), face->glyph->bitmap.buffer, bitmap.get_size());
  100. return true;
  101. }
  102. bool typeface::get_kerning(float height, char32_t first, char32_t second, float2& offset) const
  103. {
  104. // Check if typeface has kerning information
  105. if (!has_kerning())
  106. return false;
  107. // Set font size
  108. set_face_pixel_size(height);
  109. // Get indices of the two glyphs
  110. FT_UInt first_index = FT_Get_Char_Index(face, static_cast<FT_ULong>(first));
  111. FT_UInt second_index = FT_Get_Char_Index(face, static_cast<FT_ULong>(second));
  112. // Get kerning vector
  113. FT_Vector kerning;
  114. FT_Error error = FT_Get_Kerning(face, first_index, second_index, FT_KERNING_DEFAULT, &kerning);
  115. if (error)
  116. {
  117. throw std::runtime_error("FreeType failed to get kerning vector (error code " + std::to_string(error) + ")");
  118. }
  119. offset = float2{static_cast<float>(kerning.x), static_cast<float>(kerning.y)} / 64.0f;
  120. return true;
  121. }
  122. void typeface::set_face_pixel_size(float height) const
  123. {
  124. if (this->height == height)
  125. return;
  126. FT_Error error = FT_Set_Pixel_Sizes(face, 0, static_cast<FT_UInt>(height));
  127. if (error)
  128. {
  129. throw std::runtime_error("FreeType failed to set face size (error code " + std::to_string(error) + ")");
  130. }
  131. this->height = height;
  132. }
  133. } // namespace freetype
  134. } // namespace type