Skip to content

Clip text when using clip_path in AGG #10811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,50 @@ def test_titles():
ax.set_yticks([])


@image_comparison(baseline_images=['text_clip'],
extensions=['png'])
def test_text_clip():
plt.figure(figsize=(8, 8))
ax = plt.gca()

poly = matplotlib.patches.Polygon([[1, 0], [0, 1], [-1, 0], [0, -1]],
facecolor="#ddffdd",
edgecolor="#00ff00",
linewidth=2,
alpha=0.5)

ax.add_patch(poly)

# add a text into the axes with clip path which should not be displayed
txt_outside = matplotlib.text.Text(0.65,
0.85,
"outside 1",
clip_on=True,
clip_path=poly)
ax.add_artist(txt_outside)
txt_outside.set_clip_path(poly)
# add a text into the axes with clip path which should not be displayed
a = ax.text(0.65, 0.75, "outside 2", clip_on=True, clip_path=poly)
a.set_clip_path(poly)
# should be (and is) displayed
txt_inside = matplotlib.text.Text(0.,
0.,
"inside :)",
clip_on=True,
clip_path=poly)
txt_inside.set_clip_path(poly)
ax.add_artist(txt_inside)

# works perfectly
scatter = plt.scatter(*np.transpose([[0.1, 0.1], [0.7, 0.7]]),
zorder=5,
clip_path=poly)

ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.add_artist(scatter)


@image_comparison(baseline_images=['text_alignment'])
def test_alignment():
plt.figure()
Expand Down
79 changes: 47 additions & 32 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,28 @@ class font_to_rgba
_gen->prepare();
}
};
class span_conv_alpha
{
public:
typedef agg::rgba8 color_type;

double m_alpha;

span_conv_alpha(double alpha) : m_alpha(alpha)
{
}

void prepare()
{
}
void generate(color_type *span, int x, int y, unsigned len) const
{
do {
span->a = (agg::int8u)((double)span->a * m_alpha);
++span;
} while (--len);
}
};
template <class ImageArray>
inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, int y, double angle)
{
Expand All @@ -740,11 +761,13 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in

theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
if (angle != 0.0) {
double alpha = gc.alpha;
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);
if (angle != 0.0 || has_clippath) {
agg::rendering_buffer srcbuf(
image.data(), (unsigned)image.dim(1),
(unsigned)image.dim(0), (unsigned)image.dim(1));
agg::pixfmt_gray8 pixf_img(srcbuf);
pixfmt pixf(srcbuf);

set_clipbox(gc.cliprect, theRasterizer);

Expand All @@ -764,17 +787,31 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
agg::trans_affine inv_mtx(mtx);
inv_mtx.invert();

agg::image_filter_lut filter;
filter.calculate(agg::image_filter_spline36());
interpolator_type interpolator(inv_mtx);
typedef agg::span_allocator<agg::rgba8> color_span_alloc_type;
typedef agg::image_accessor_clip<pixfmt> image_accessor_type;
typedef agg::span_interpolator_linear<> interpolator_type;
typedef agg::span_image_filter_rgba_nn<image_accessor_type, interpolator_type>
image_span_gen_type;
typedef agg::span_converter<image_span_gen_type, span_conv_alpha> span_conv;

color_span_alloc_type sa;
image_accessor_type ia(pixf_img, agg::gray8(0));
image_span_gen_type image_span_generator(ia, interpolator, filter);
span_gen_type output_span_generator(&image_span_generator, gc.color);
renderer_type ri(rendererBase, sa, output_span_generator);
image_accessor_type ia(pixf, agg::rgba8(0, 0, 0, 0));
interpolator_type interpolator(inv_mtx);
image_span_gen_type image_span_generator(ia, interpolator);
span_conv_alpha conv_alpha(alpha);
span_conv spans(image_span_generator, conv_alpha);

typedef agg::pixfmt_amask_adaptor<pixfmt, alpha_mask_type> pixfmt_amask_type;
typedef agg::renderer_base<pixfmt_amask_type> amask_ren_type;
typedef agg::renderer_scanline_aa<amask_ren_type, color_span_alloc_type, span_conv>
renderer_type_alpha;

pixfmt_amask_type pfa(pixFmt, alphaMask);
amask_ren_type r(pfa);
renderer_type_alpha ri(r, sa, spans);

theRasterizer.add_path(rect2);
agg::render_scanlines(theRasterizer, slineP8, ri);
agg::render_scanlines(theRasterizer, scanlineAlphaMask, ri);
} else {
agg::rect_i fig, text;

Expand All @@ -801,28 +838,6 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
}
}

class span_conv_alpha
{
public:
typedef agg::rgba8 color_type;

double m_alpha;

span_conv_alpha(double alpha) : m_alpha(alpha)
{
}

void prepare()
{
}
void generate(color_type *span, int x, int y, unsigned len) const
{
do {
span->a = (agg::int8u)((double)span->a * m_alpha);
++span;
} while (--len);
}
};

template <class ImageArray>
inline void RendererAgg::draw_image(GCAgg &gc,
Expand Down