FontEffectGlow.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 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 "Memory.h"
  30. #include "../../Include/RmlUi/Core/PropertyDefinition.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. {
  41. }
  42. bool FontEffectGlow::HasUniqueTexture() const
  43. {
  44. return true;
  45. }
  46. bool FontEffectGlow::Initialise(int _width_outline, int _width_blur, Vector2i _offset)
  47. {
  48. if (_width_outline < 0 || _width_blur < 0)
  49. return false;
  50. width_outline = _width_outline;
  51. width_blur = _width_blur;
  52. combined_width = width_blur + width_outline;
  53. offset = _offset;
  54. // Outline filter.
  55. filter_outline.Initialise(width_outline, FilterOperation::Dilation);
  56. for (int x = -width_outline; x <= width_outline; ++x)
  57. {
  58. for (int y = -width_outline; y <= width_outline; ++y)
  59. {
  60. float weight = 1;
  61. float distance = Math::SquareRoot(float(x * x + y * y));
  62. if (distance > width_outline)
  63. {
  64. weight = (width_outline + 1) - distance;
  65. weight = Math::Max(weight, 0.0f);
  66. }
  67. filter_outline[x + width_outline][y + width_outline] = weight;
  68. }
  69. }
  70. // Gaussian blur filter
  71. const float std_dev = (width_blur == 0 ? 1.f : .4f * float(width_blur));
  72. const float two_variance = 2.f * std_dev * std_dev;
  73. const float gain = 1.f / Math::SquareRoot(Math::RMLUI_PI * two_variance);
  74. float sum_weight = 0.f;
  75. // We separate the blur filter into two passes, horizontal and vertical, for performance reasons.
  76. filter_blur_x.Initialise(Vector2i(width_blur, 0), FilterOperation::Sum);
  77. filter_blur_y.Initialise(Vector2i(0, width_blur), FilterOperation::Sum);
  78. for (int x = -width_blur; x <= width_blur; ++x)
  79. {
  80. float weight = gain * Math::Exp(-Math::SquareRoot(float(x * x) / two_variance));
  81. filter_blur_x[0][x + width_blur] = weight;
  82. filter_blur_y[x + width_blur][0] = weight;
  83. sum_weight += weight;
  84. }
  85. // Normalize the kernels
  86. for (int x = -width_blur; x <= width_blur; ++x)
  87. {
  88. filter_blur_x[0][x + width_blur] /= sum_weight;
  89. filter_blur_y[x + width_blur][0] /= sum_weight;
  90. }
  91. return true;
  92. }
  93. bool FontEffectGlow::GetGlyphMetrics(Vector2i& origin, Vector2i& dimensions, const FontGlyph& RMLUI_UNUSED_PARAMETER(glyph)) const
  94. {
  95. RMLUI_UNUSED(glyph);
  96. if (dimensions.x * dimensions.y > 0)
  97. {
  98. origin.x += offset.x - combined_width;
  99. origin.y += offset.y - combined_width;
  100. dimensions.x += 2 * combined_width;
  101. dimensions.y += 2 * combined_width;
  102. return true;
  103. }
  104. return false;
  105. }
  106. void FontEffectGlow::GenerateGlyphTexture(byte* destination_data, const Vector2i destination_dimensions, int destination_stride, const FontGlyph& glyph) const
  107. {
  108. const Vector2i buf_dimensions = destination_dimensions;
  109. const int buf_stride = buf_dimensions.x;
  110. const int buf_size = buf_dimensions.x * buf_dimensions.y;
  111. DynamicArray<byte, GlobalStackAllocator<byte>> outline_output(buf_size);
  112. DynamicArray<byte, GlobalStackAllocator<byte>> blur_x_output(buf_size);
  113. filter_outline.Run(outline_output.data(), buf_dimensions, buf_stride, ColorFormat::A8, glyph.bitmap_data, glyph.bitmap_dimensions,
  114. Vector2i(combined_width), glyph.color_format);
  115. filter_blur_x.Run(blur_x_output.data(), buf_dimensions, buf_stride, ColorFormat::A8, outline_output.data(), buf_dimensions, Vector2i(0),
  116. ColorFormat::A8);
  117. filter_blur_y.Run(destination_data, destination_dimensions, destination_stride, ColorFormat::RGBA8, blur_x_output.data(), buf_dimensions,
  118. Vector2i(0), ColorFormat::A8);
  119. }
  120. FontEffectGlowInstancer::FontEffectGlowInstancer() : 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. {
  131. }
  132. SharedPtr<FontEffect> FontEffectGlowInstancer::InstanceFontEffect(const String& RMLUI_UNUSED_PARAMETER(name), const PropertyDictionary& properties)
  133. {
  134. RMLUI_UNUSED(name);
  135. Vector2i offset;
  136. int width_outline = properties.GetProperty(id_width_outline)->Get< int >();
  137. int width_blur = properties.GetProperty(id_width_blur)->Get< int >();
  138. offset.x = properties.GetProperty(id_offset_x)->Get< int >();
  139. offset.y = properties.GetProperty(id_offset_y)->Get< int >();
  140. Colourb color = properties.GetProperty(id_color)->Get< Colourb >();
  141. if (width_blur < 0)
  142. width_blur = width_outline;
  143. auto font_effect = MakeShared<FontEffectGlow>();
  144. if (font_effect->Initialise(width_outline, width_blur, offset))
  145. {
  146. font_effect->SetColour(color);
  147. return font_effect;
  148. }
  149. return nullptr;
  150. }
  151. } // namespace Rml