Skip to content

Commit

Permalink
Font/Pango Text: add extra stroke to text outline to compensate for a…
Browse files Browse the repository at this point in the history
…lpha weirdness

Also added the outline to the hash.
  • Loading branch information
Vultraz committed Mar 13, 2018
1 parent 8285a2c commit d9b0a1c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/font/text.cpp
Expand Up @@ -653,6 +653,9 @@ void pango_text::render(PangoLayout& layout, const PangoRectangle& rect, const s
}
}

// Add a path to the cairo context tracing the current text.
pango_cairo_layout_path(cr.get(), &layout);

//
// TODO: the outline may be slightly cut off around certain text if it renders too
// close to the surface's edge. That causes the outline to extend just slightly
Expand All @@ -663,17 +666,15 @@ void pango_text::render(PangoLayout& layout, const PangoRectangle& rect, const s
// -- vultraz, 2018-03-07
//
if(add_outline_) {
// Add a path to the cairo context tracing the current text.
pango_cairo_layout_path(cr.get(), &layout);

// Set color for background outline (black).
cairo_set_source_rgba(cr.get(), 0.0, 0.0, 0.0, 1.0);

cairo_set_line_join(cr.get(), CAIRO_LINE_JOIN_ROUND);
cairo_set_line_width(cr.get(), 3.0); // Adjust as necessary

// Stroke path to draw outline.
cairo_stroke(cr.get());
// Stroke path to draw outline. Don't delete the path.
cairo_stroke_preserve(cr.get());
}

// Set main text color.
Expand All @@ -684,10 +685,13 @@ void pango_text::render(PangoLayout& layout, const PangoRectangle& rect, const s
foreground_color_.a / 255.0
);

pango_cairo_show_layout(cr.get(), &layout);
// Fill text path. This is a hack to work around bug #1744 (bad alpha blending when rendering the
// output surface). Instead of calling pango_cairo_show_layout twice, we fill the layout path here
// It greatly improves the look of text, but it shouldn't really be necessary and probably messes
// with certain OS settings like disabling AA.
cairo_fill(cr.get());

// HACK: 'draw' text a second time in order to get desired output when copying w/ alpha blending.
// See bug #1744 for more info.
// Necessary for pango markup to be properly rendered.
pango_cairo_show_layout(cr.get(), &layout);
}

Expand Down Expand Up @@ -920,6 +924,7 @@ size_t hash<font::pango_text>::operator()(const font::pango_text& t) const
hash_combine(hash, t.maximum_height_);
hash_combine(hash, t.alignment_);
hash_combine(hash, t.ellipse_mode_);
hash_combine(hash, t.add_outline_);

return hash;
}
Expand Down
1 change: 0 additions & 1 deletion src/font/text.hpp
Expand Up @@ -266,7 +266,6 @@ class pango_text
/** The SDL surface to render upon used as a cache. */
mutable surface surface_;


/** The text to draw (stored as UTF-8). */
std::string text_;

Expand Down

0 comments on commit d9b0a1c

Please sign in to comment.