FontEffectGlow.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "FontEffectGlow.h"
  29. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  30. #include "Memory.h"
  31. namespace Rml {
  32. FontEffectGlow::FontEffectGlow()
  33. {
  34. width_blur = 0;
  35. width_outline = 0;
  36. combined_width = 0;
  37. SetLayer(Layer::Back);
  38. }
  39. FontEffectGlow::~FontEffectGlow() {}
  40. bool FontEffectGlow::HasUniqueTexture() const
  41. {
  42. return true;
  43. }
  44. bool FontEffectGlow::Initialise(int _width_outline, int _width_blur, Vector2i _offset)
  45. {
  46. if (_width_outline < 0 || _width_blur < 0)
  47. return false;
  48. width_outline = _width_outline;
  49. width_blur = _width_blur;
  50. combined_width = width_blur + width_outline;
  51. offset = _offset;
  52. // Outline filter.
  53. filter_outline.Initialise(width_outline, FilterOperation::Dilation);
  54. for (int x = -width_outline; x <= width_outline; ++x)
  55. {
  56. for (int y = -width_outline; y <= width_outline; ++y)
  57. {
  58. float weight = 1;
  59. float distance = Math::SquareRoot(float(x * x + y * y));
  60. if (distance > width_outline)
  61. {
  62. weight = (width_outline + 1) - distance;
  63. weight = Math::Max(weight, 0.0f);
  64. }
  65. filter_outline[x + width_outline][y + width_outline] = weight;
  66. }
  67. }
  68. // Gaussian blur filter
  69. const float std_dev = (width_blur == 0 ? 1.f : .4f * float(width_blur));
  70. const float two_variance = 2.f * std_dev * std_dev;
  71. const float gain = 1.f / Math::SquareRoot(Math::RMLUI_PI * two_variance);
  72. float sum_weight = 0.f;
  73. // We separate the blur filter into two passes, horizontal and vertical, for performance reasons.
  74. filter_blur_x.Initialise(Vector2i(width_blur, 0), FilterOperation::Sum);
  75. filter_blur_y.Initialise(Vector2i(0, width_blur), FilterOperation::Sum);
  76. for (int x = -width_blur; x <= width_blur; ++x)
  77. {
  78. float weight = gain * Math::Exp(-Math::SquareRoot(float(x * x) / two_variance));
  79. filter_blur_x[0][x + width_blur] = weight;
  80. filter_blur_y[x + width_blur][0] = weight;
  81. sum_weight += weight;
  82. }
  83. // Normalize the kernels
  84. for (int x = -width_blur; x <= width_blur; ++x)
  85. {
  86. filter_blur_x[0][x + width_blur] /= sum_weight;
  87. filter_blur_y[x + width_blur][0] /= sum_weight;
  88. }
  89. return true;
  90. }
  91. bool FontEffectGlow::GetGlyphMetrics(Vector2i& origin, Vector2i& dimensions, const FontGlyph& /*glyph*/) const
  92. {
  93. if (dimensions.x * dimensions.y > 0)
  94. {
  95. origin.x += offset.x - combined_width;
  96. origin.y += offset.y - combined_width;
  97. dimensions.x += 2 * combined_width;
  98. dimensions.y += 2 * combined_width;
  99. return true;
  100. }
  101. return false;
  102. }
  103. void FontEffectGlow::GenerateGlyphTexture(byte* destination_data, const Vector2i destination_dimensions, int destination_stride,
  104. const FontGlyph& glyph) const
  105. {
  106. const Vector2i buf_dimensions = destination_dimensions;
  107. const int buf_stride = buf_dimensions.x;
  108. const int buf_size = buf_dimensions.x * buf_dimensions.y;
  109. DynamicArray<byte, GlobalStackAllocator<byte>> outline_output(buf_size);
  110. DynamicArray<byte, GlobalStackAllocator<byte>> blur_x_output(buf_size);
  111. filter_outline.Run(outline_output.data(), buf_dimensions, buf_stride, ColorFormat::A8, glyph.bitmap_data, glyph.bitmap_dimensions,
  112. Vector2i(combined_width), glyph.color_format);
  113. filter_blur_x.Run(blur_x_output.data(), buf_dimensions, buf_stride, ColorFormat::A8, outline_output.data(), buf_dimensions, Vector2i(0),
  114. ColorFormat::A8);
  115. filter_blur_y.Run(destination_data, destination_dimensions, destination_stride, ColorFormat::RGBA8, blur_x_output.data(), buf_dimensions,
  116. Vector2i(0), ColorFormat::A8);
  117. FillColorValuesFromAlpha(destination_data, destination_dimensions, destination_stride);
  118. }
  119. FontEffectGlowInstancer::FontEffectGlowInstancer() :
  120. id_width_outline(PropertyId::Invalid), id_width_blur(PropertyId::Invalid), id_color(PropertyId::Invalid)
  121. {
  122. id_width_outline = RegisterProperty("width-outline", "1px", true).AddParser("length").GetId();
  123. id_width_blur = RegisterProperty("width-blur", "-1px", true).AddParser("length").GetId();
  124. id_offset_x = RegisterProperty("offset-x", "0px", true).AddParser("length").GetId();
  125. id_offset_y = RegisterProperty("offset-y", "0px", true).AddParser("length").GetId();
  126. id_color = RegisterProperty("color", "white", false).AddParser("color").GetId();
  127. RegisterShorthand("font-effect", "width-outline, width-blur, offset-x, offset-y, color", ShorthandType::FallThrough);
  128. }
  129. FontEffectGlowInstancer::~FontEffectGlowInstancer() {}
  130. SharedPtr<FontEffect> FontEffectGlowInstancer::InstanceFontEffect(const String& /*name*/, const PropertyDictionary& properties)
  131. {
  132. Vector2i offset;
  133. int width_outline = properties.GetProperty(id_width_outline)->Get<int>();
  134. int width_blur = properties.GetProperty(id_width_blur)->Get<int>();
  135. offset.x = properties.GetProperty(id_offset_x)->Get<int>();
  136. offset.y = properties.GetProperty(id_offset_y)->Get<int>();
  137. Colourb color = properties.GetProperty(id_color)->Get<Colourb>();
  138. if (width_blur < 0)
  139. width_blur = width_outline;
  140. auto font_effect = MakeShared<FontEffectGlow>();
  141. if (font_effect->Initialise(width_outline, width_blur, offset))
  142. {
  143. font_effect->SetColour(color);
  144. return font_effect;
  145. }
  146. return nullptr;
  147. }
  148. } // namespace Rml