Commit 98bb8853 authored by twanvl's avatar twanvl

Text drawing now uses an AColor instead of a normal Color, so transparent text is possible.

parent acdca5da
......@@ -15,7 +15,7 @@ A reference to a normal [[type:font]] for drawing text.
| @style@ [[type:scriptable]] font style @"normal"@ Style of the font, one of @"normal"@ or @"italic"@. This can be changed locally by [[type:tagged string|tags]].
| @underline@ [[type:scriptable]] [[type:boolean]] @false@ Should the font be underlined?
| @color@ [[type:scriptable]] [[type:color]] @rgb(0,0,0)@ What color should text be drawn in?
| @shadow color@ [[type:scriptable]] [[type:color]] @rgb(0,0,0)@ Color for a shadow below the text.
| @shadow color@ [[type:scriptable]] [[type:color]] @"transparent"@ Color for a shadow below the text.
| @shadow displacement x@ [[type:double]] @0@ Relative position of the shadow in pixels. A shadow is only drawn if the displacement is nonzero.
| @shadow displacement y@ [[type:double]] @0@ ^^^
| @shadow blur@ [[type:double]] @0@ How much should the shadow be blurred?
......
......@@ -17,6 +17,7 @@ Font::Font()
, underline(false)
, scale_down_to(100000)
, max_stretch(1.0)
, color(AColor(0,0,0))
, shadow_displacement(0,0)
, shadow_blur(0)
, separator_color(128,128,128)
......
......@@ -12,6 +12,7 @@
#include <util/prec.hpp>
#include <util/real_point.hpp>
#include <script/scriptable.hpp>
#include <gfx/color.hpp>
DECLARE_POINTER_TYPE(Font);
......@@ -40,8 +41,8 @@ class Font : public IntrusivePtrBase<Font> {
Scriptable<bool> underline; ///< Underlined?
double scale_down_to; ///< Smallest size to scale down to
double max_stretch; ///< How much should the font be stretched before scaling down?
Scriptable<Color> color; ///< Color to use
Scriptable<Color> shadow_color; ///< Color for shadow
Scriptable<AColor> color; ///< Color to use
Scriptable<AColor> shadow_color; ///< Color for shadow
RealSize shadow_displacement; ///< Position of the shadow
double shadow_blur; ///< Blur radius of the shadow
Color separator_color; ///< Color for <sep> text
......
......@@ -52,7 +52,7 @@ void sharp_resample_and_clip(const Image& img_in, Image& img_out, wxRect rect, i
* rect = rectangle to draw in (a rectangle somewhere around pos)
* stretch = amount to stretch in the direction of the text after drawing
*/
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, int angle, const String& text, int blur_radius = 0, int repeat = 1);
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, int angle, AColor color, const String& text, int blur_radius = 0, int repeat = 1);
// scaling factor to use when drawing resampled text
extern const int text_scaling;
......
......@@ -165,7 +165,9 @@ void blur_image_alpha(Image& img) {
// Draw text by first drawing it using a larger font and then downsampling it
// optionally rotated by an angle
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, int angle, const String& text, int blur_radius, int repeat) {
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, int angle, AColor color, const String& text, int blur_radius, int repeat) {
// transparent text can be ignored
if (color.alpha == 0) return;
// enlarge slightly; some fonts are larger then the GetTextExtent tells us (especially italic fonts)
int w = static_cast<int>(rect.width) + 3 + 2 * blur_radius, h = static_cast<int>(rect.height) + 1 + 2 * blur_radius;
// determine sub-pixel position
......@@ -188,8 +190,12 @@ void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, dou
if (!sideways(angle)) w = int(w * stretch); // GCC makes annoying conversion warnings if *= is used here.
else h = int(h * stretch);
Image img_small(w, h, false);
fill_image(img_small, dc.GetTextForeground());
fill_image(img_small, color);
downsample_to_alpha(buffer, img_small);
// multiply alpha
if (color.alpha != 255) {
set_alpha(img_small, color.alpha / 255.);
}
// blur
for (int i = 0 ; i < blur_radius ; ++i) {
blur_image_alpha(img_small);
......
......@@ -199,7 +199,12 @@ RotatedDC::RotatedDC(DC& dc, const Rotation& rotation, RenderQuality quality)
// ----------------------------------------------------------------------------- : RotatedDC : Drawing
void RotatedDC::DrawText (const String& text, const RealPoint& pos, int blur_radius, int boldness, double stretch_) {
DrawText(text, pos, dc.GetTextForeground(), blur_radius, boldness, stretch_);
}
void RotatedDC::DrawText (const String& text, const RealPoint& pos, AColor color, int blur_radius, int boldness, double stretch_) {
if (text.empty()) return;
if (color.alpha == 0) return;
if (quality >= QUALITY_AA) {
RealRect r(pos, GetTextExtent(text));
RealRect r_ext = trRectToBB(r);
......@@ -212,27 +217,25 @@ void RotatedDC::DrawText (const String& text, const RealPoint& pos, int blur_ra
r_ext.x = r_ext2.x;
r_ext.y = r_ext2.y;
}
draw_resampled_text(dc, pos2, r_ext, stretch_ * getStretch(), angle, text, blur_radius, boldness);
draw_resampled_text(dc, pos2, r_ext, stretch_ * getStretch(), angle, color, text, blur_radius, boldness);
} else if (quality >= QUALITY_SUB_PIXEL) {
RealPoint p_ext = tr(pos)*text_scaling;
double usx,usy;
dc.GetUserScale(&usx, &usy);
dc.SetUserScale(usx/text_scaling, usy/text_scaling);
dc.SetTextForeground(color);
dc.DrawRotatedText(text, (int) p_ext.x, (int) p_ext.y, angle);
dc.SetUserScale(usx, usy);
} else {
RealPoint p_ext = tr(pos);
dc.SetTextForeground(color);
dc.DrawRotatedText(text, (int) p_ext.x, (int) p_ext.y, angle);
}
}
void RotatedDC::DrawTextWithShadow(const String& text, const Font& font, const RealPoint& pos, double scale, double stretch) {
if (font.hasShadow()) {
SetTextForeground(font.shadow_color);
DrawText(text, pos + font.shadow_displacement * scale, font.shadow_blur * scale, 1, stretch);
}
SetTextForeground(font.color);
DrawText(text, pos, 0, 1, stretch);
DrawText(text, pos + font.shadow_displacement * scale, font.shadow_color, font.shadow_blur * scale, 1, stretch);
DrawText(text, pos, font.color, 0, 1, stretch);
}
void RotatedDC::DrawBitmap(const Bitmap& bitmap, const RealPoint& pos) {
......
......@@ -164,7 +164,8 @@ class RotatedDC : public Rotation {
// --------------------------------------------------- : Drawing
/// Draw text
void DrawText (const String& text, const RealPoint& pos, int blur_radius = 0, int boldness = 1, double stretch = 1.0);
void DrawText (const String& text, const RealPoint& pos, int blur_radius = 0, int boldness = 1, double stretch = 1.0);
void DrawText (const String& text, const RealPoint& pos, AColor color, int blur_radius = 0, int boldness = 1, double stretch = 1.0);
/// Draw text with the shadow and color settings of the given font
void DrawTextWithShadow(const String& text, const Font& font, const RealPoint& pos, double scale = 1.0, double stretch = 1.0);
/// Draw abitmap, it must already be zoomed!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment