PolyFontGlyphSheet.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include "polycode/core/PolyGlobals.h"
  3. #define generic GenericFreetypeLibrary
  4. #include "ft2build.h"
  5. #include FT_FREETYPE_H
  6. #undef generic
  7. #include "polycode/core/PolyString.h"
  8. #include "polycode/core/PolyVector2.h"
  9. #include <vector>
  10. #include <map>
  11. #include <set>
  12. namespace Polycode {
  13. class String;
  14. class Texture;
  15. class Image;
  16. class Font;
  17. struct FontTextureGlyph {
  18. Vector2 offset[4];
  19. Vector2 texCoord[4];
  20. Vector2 advance;
  21. };
  22. /** Wraps a sheet of rendered font glyphs on a Texture.
  23. * Use in combination with TextMesh to render text from minimal texture creation. */
  24. class _PolyExport FontGlyphSheet : public PolyBase {
  25. public:
  26. enum FontTextureGlyphMode {
  27. /** Regular anti-aliased font rendering. Colour is pure-white for clean custom tinting with an alpha channel. */
  28. ANTIALIAS,
  29. /** Using distance-from-edge calculation as described in the Valve paper.
  30. *
  31. * "Improved Alpha-Tested Magnification for Vector Textures and Special Effects"
  32. * http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf
  33. *
  34. * To make the most of this:
  35. * set renderer->alphaTestValue = 0.5
  36. * set sceneMesh->alphaTest = true
  37. * set sceneMesh->blendingMode = Renderer::BLEND_MODE_NONE;
  38. *
  39. * Or use a custom shader - alpha values of 0.5 indicate the boundary.
  40. * */
  41. ALPHA_TEST
  42. };
  43. FontGlyphSheet(Font* font, int size = 32, FontTextureGlyphMode mode = ANTIALIAS);
  44. virtual ~FontGlyphSheet();
  45. void setMode(FontTextureGlyphMode mode) { this->mode = mode; }
  46. /** Set height of font to be rendered in pixels. */
  47. void setSize(int size);
  48. /** Scans extraCharacters for anything that isn't currently in the rendered sheet and rebuilds the sheet if necessary. */
  49. void addGlyphs(String extraCharacters);
  50. /** Convenience method to build a sheet with all of the visible ASCII characters. */
  51. void buildVisibleAscii();
  52. /** Convenience method to build a sheet of glyphs with one of each of what is in characters. */
  53. void buildGlyphs(String characters);
  54. /** Creates the sheet given a set of characters. */
  55. void buildGlyphs(std::set<wchar_t> characters);
  56. /** Returns the currently rendered characters as a set. */
  57. std::set<wchar_t> getCharacters() const;
  58. /** Used by TextMesh to generate the vertices for the given text into the vertex array.
  59. @return the next index after that which was used */
  60. //int renderStringVertices(String text, std::vector<Vertex*>& vertices, int index = 0);
  61. Texture* getTexture() { return texture; }
  62. int tabWidth;
  63. protected:
  64. Font* font;
  65. FontTextureGlyphMode mode;
  66. Texture* texture;
  67. std::map<wchar_t,FontTextureGlyph> locations;
  68. int size;
  69. };
  70. }