FontEffectOutline.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "FontEffectOutline.h"
  2. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  3. namespace Rml {
  4. FontEffectOutline::FontEffectOutline()
  5. {
  6. width = 0;
  7. SetLayer(Layer::Back);
  8. }
  9. FontEffectOutline::~FontEffectOutline() {}
  10. bool FontEffectOutline::HasUniqueTexture() const
  11. {
  12. return true;
  13. }
  14. bool FontEffectOutline::Initialise(int _width)
  15. {
  16. if (_width <= 0)
  17. return false;
  18. width = _width;
  19. filter.Initialise(width, FilterOperation::Dilation);
  20. for (int x = -width; x <= width; ++x)
  21. {
  22. for (int y = -width; y <= width; ++y)
  23. {
  24. float weight = 1;
  25. float distance = Math::SquareRoot(float(x * x + y * y));
  26. if (distance > width)
  27. {
  28. weight = (width + 1) - distance;
  29. weight = Math::Max(weight, 0.0f);
  30. }
  31. filter[x + width][y + width] = weight;
  32. }
  33. }
  34. return true;
  35. }
  36. bool FontEffectOutline::GetGlyphMetrics(Vector2i& origin, Vector2i& dimensions, const FontGlyph& /*glyph*/) const
  37. {
  38. if (dimensions.x * dimensions.y > 0)
  39. {
  40. origin.x -= width;
  41. origin.y -= width;
  42. dimensions.x += 2 * width;
  43. dimensions.y += 2 * width;
  44. return true;
  45. }
  46. return false;
  47. }
  48. void FontEffectOutline::GenerateGlyphTexture(byte* destination_data, const Vector2i destination_dimensions, int destination_stride,
  49. const FontGlyph& glyph) const
  50. {
  51. filter.Run(destination_data, destination_dimensions, destination_stride, ColorFormat::RGBA8, glyph.bitmap_data, glyph.bitmap_dimensions,
  52. Vector2i(width), glyph.color_format);
  53. FillColorValuesFromAlpha(destination_data, destination_dimensions, destination_stride);
  54. }
  55. FontEffectOutlineInstancer::FontEffectOutlineInstancer() : id_width(PropertyId::Invalid), id_color(PropertyId::Invalid)
  56. {
  57. id_width = RegisterProperty("width", "1px", true).AddParser("length").GetId();
  58. id_color = RegisterProperty("color", "white", false).AddParser("color").GetId();
  59. RegisterShorthand("font-effect", "width, color", ShorthandType::FallThrough);
  60. }
  61. FontEffectOutlineInstancer::~FontEffectOutlineInstancer() {}
  62. SharedPtr<FontEffect> FontEffectOutlineInstancer::InstanceFontEffect(const String& /*name*/, const PropertyDictionary& properties)
  63. {
  64. float width = properties.GetProperty(id_width)->Get<float>();
  65. Colourb color = properties.GetProperty(id_color)->Get<Colourb>();
  66. auto font_effect = MakeShared<FontEffectOutline>();
  67. if (font_effect->Initialise(int(width)))
  68. {
  69. font_effect->SetColour(color);
  70. return font_effect;
  71. }
  72. return nullptr;
  73. }
  74. } // namespace Rml