Advertisement
sds-michael

Unreal Engine 4.8 Slate font outline hack - SlateTextRun.cpp

Jul 16th, 2015
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.41 KB | None | 0 0
  1. --- //depot/Projects/UnrealEngine/Main/Engine/Source/Runtime/Slate/Private/Framework/Text/SlateTextRun.cpp  2015-03-22 07:56:30.000000000 1100
  2. +++ C:\Local\Projects\UnrealEngine\Main\Engine\Source\Runtime\Slate\Private\Framework\Text\SlateTextRun.cpp 2015-03-22 07:56:30.000000000 1100
  3. @@ -6,6 +6,16 @@
  4.  
  5.  #include "SlateTextRun.h"
  6.  
  7. +FVector2D GetShadowOffsetAndOutlineThickness(const FVector2D& StyleShadowOffset, float& OutlineThicknessOut, uint32& OutlineDirectionsOut)
  8. +{
  9. +   FVector2D ShadowOffset(StyleShadowOffset);
  10. +   OutlineThicknessOut = FMath::RoundToInt(StyleShadowOffset.X * 0.001f);
  11. +   ShadowOffset.X -= OutlineThicknessOut * 1000.f;
  12. +   OutlineDirectionsOut = FMath::RoundToInt(StyleShadowOffset.Y * 0.001f);
  13. +   ShadowOffset.Y -= OutlineDirectionsOut * 1000.f;
  14. +   return ShadowOffset;
  15. +}
  16. +
  17.  TSharedRef< FSlateTextRun > FSlateTextRun::Create( const FRunInfo& InRunInfo, const TSharedRef< const FString >& InText, const FTextBlockStyle& Style )
  18.  {
  19.     return MakeShareable( new FSlateTextRun( InRunInfo, InText, Style ) );
  20. @@ -28,19 +38,29 @@
  21.  
  22.  int16 FSlateTextRun::GetBaseLine( float Scale ) const
  23.  {
  24. +   float OutlineThickness;
  25. +   uint32 OutlineDirections;
  26. +   FVector2D ShadowOffset = GetShadowOffsetAndOutlineThickness(Style.ShadowOffset, OutlineThickness, OutlineDirections);
  27.     const TSharedRef< FSlateFontMeasure > FontMeasure = FSlateApplication::Get().GetRenderer()->GetFontMeasureService();
  28. -   return FontMeasure->GetBaseline( Style.Font, Scale ) - FMath::Min(0.0f, Style.ShadowOffset.Y * Scale);
  29. +   return FontMeasure->GetBaseline( Style.Font, Scale ) - FMath::Min(0.0f, ShadowOffset.Y * Scale);
  30.  }
  31.  
  32.  int16 FSlateTextRun::GetMaxHeight( float Scale ) const
  33.  {
  34. +   float OutlineThickness;
  35. +   uint32 OutlineDirections;
  36. +   FVector2D ShadowOffset = GetShadowOffsetAndOutlineThickness(Style.ShadowOffset, OutlineThickness, OutlineDirections);
  37.     const TSharedRef< FSlateFontMeasure > FontMeasure = FSlateApplication::Get().GetRenderer()->GetFontMeasureService();
  38. -   return FontMeasure->GetMaxCharacterHeight( Style.Font, Scale ) + FMath::Abs(Style.ShadowOffset.Y * Scale);
  39. +   return FontMeasure->GetMaxCharacterHeight( Style.Font, Scale ) + FMath::Abs(ShadowOffset.Y * Scale);
  40.  }
  41.  
  42.  FVector2D FSlateTextRun::Measure( int32 BeginIndex, int32 EndIndex, float Scale ) const
  43.  {
  44. -   const FVector2D ShadowOffsetToApply((EndIndex == Range.EndIndex) ? FMath::Abs(Style.ShadowOffset.X * Scale) : 0.0f, FMath::Abs(Style.ShadowOffset.Y * Scale));
  45. +   float OutlineThickness;
  46. +   uint32 OutlineDirections;
  47. +   FVector2D ShadowOffset = GetShadowOffsetAndOutlineThickness(Style.ShadowOffset, OutlineThickness, OutlineDirections);
  48. +
  49. +   const FVector2D ShadowOffsetToApply((EndIndex == Range.EndIndex) ? FMath::Abs(ShadowOffset.X * Scale) : 0.0f, FMath::Abs(ShadowOffset.Y * Scale));
  50.  
  51.     if ( EndIndex - BeginIndex == 0 )
  52.     {
  53. @@ -73,7 +93,11 @@
  54.     const FSlateRect ClippingRect = AllottedGeometry.GetClippingRect().IntersectionWith(MyClippingRect);
  55.     const ESlateDrawEffect::Type DrawEffects = bParentEnabled ? ESlateDrawEffect::None : ESlateDrawEffect::DisabledEffect;
  56.  
  57. -   const bool ShouldDropShadow = Style.ShadowOffset.Size() > 0;
  58. +   float OutlineThickness;
  59. +   uint32 OutlineDirections;
  60. +   FVector2D ShadowOffset = GetShadowOffsetAndOutlineThickness(Style.ShadowOffset, OutlineThickness, OutlineDirections);
  61. +
  62. +   const bool ShouldDropShadow = ShadowOffset.Size() > 0;
  63.     const FVector2D BlockLocationOffset = Block->GetLocationOffset();
  64.     const FTextRange BlockRange = Block->GetTextRange();
  65.    
  66. @@ -82,14 +106,40 @@
  67.  
  68.     // A negative shadow offset should be applied as a positive offset to the text to avoid clipping issues
  69.     const FVector2D DrawShadowOffset(
  70. -       (Style.ShadowOffset.X > 0.0f) ? Style.ShadowOffset.X * AllottedGeometry.Scale : 0.0f,
  71. -       (Style.ShadowOffset.Y > 0.0f) ? Style.ShadowOffset.Y * AllottedGeometry.Scale : 0.0f
  72. +       (ShadowOffset.X > 0.0f) ? ShadowOffset.X * AllottedGeometry.Scale : 0.0f,
  73. +       (ShadowOffset.Y > 0.0f) ? ShadowOffset.Y * AllottedGeometry.Scale : 0.0f
  74.         );
  75.     const FVector2D DrawTextOffset(
  76. -       (Style.ShadowOffset.X < 0.0f) ? -Style.ShadowOffset.X * AllottedGeometry.Scale : 0.0f,
  77. -       (Style.ShadowOffset.Y < 0.0f) ? -Style.ShadowOffset.Y * AllottedGeometry.Scale : 0.0f
  78. +       (ShadowOffset.X < 0.0f) ? -ShadowOffset.X * AllottedGeometry.Scale : 0.0f,
  79. +       (ShadowOffset.Y < 0.0f) ? -ShadowOffset.Y * AllottedGeometry.Scale : 0.0f
  80.         );
  81.  
  82. +   // Draw the outline
  83. +   if (OutlineThickness > 0.f)
  84. +   {
  85. +       FSlateRect OutlineClippingRect(ClippingRect.Left - OutlineThickness, ClippingRect.Top - OutlineThickness, ClippingRect.Right + OutlineThickness, ClippingRect.Bottom + OutlineThickness);
  86. +       FVector2D OutlineOffset;
  87. +       float AngleStep = 360.f / (float)OutlineDirections;
  88. +       FVector2D OutlineDirection(0.f, OutlineThickness * AllottedGeometry.Scale);
  89. +       for (uint32 i = 0; i < OutlineDirections; ++i)
  90. +       {
  91. +           OutlineOffset = OutlineDirection.GetRotated(AngleStep * i);
  92. +           FSlateDrawElement::MakeText(
  93. +               OutDrawElements,
  94. +               ++LayerId,
  95. +               AllottedGeometry.ToPaintGeometry(TransformVector(InverseScale, Block->GetSize()), FSlateLayoutTransform(TransformPoint(InverseScale, Block->GetLocationOffset() + DrawTextOffset + OutlineOffset))),
  96. +               Text.Get(),
  97. +               BlockRange.BeginIndex,
  98. +               BlockRange.EndIndex,
  99. +               Style.Font,
  100. +               OutlineClippingRect,
  101. +               DrawEffects,
  102. +               InWidgetStyle.GetColorAndOpacityTint() * Style.ShadowColorAndOpacity
  103. +               );
  104. +
  105. +       }
  106. +   }
  107. +
  108.     // Draw the optional shadow
  109.     if ( ShouldDropShadow )
  110.     {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement