FontEffectGlow.cpp 6.2 KB

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