PolyFontGlyphSheet.h 2.6 KB

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