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
Supporting different alphas for face and edge colours #1954
Changes from 5 commits
ae5c2f1
1e7399f
1aefc52
6cc8aaa
a7f7cc7
cd18645
71e9ad9
ba1f673
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,13 +110,13 @@ def set_width_height(self, width, height): | |
# font transform? | ||
|
||
|
||
def _fill_and_stroke (self, ctx, fill_c, alpha): | ||
def _fill_and_stroke (self, ctx, fill_c, alpha, alpha_overrides): | ||
if fill_c is not None: | ||
ctx.save() | ||
if len(fill_c) == 3: | ||
if len(fill_c) == 3 or alpha_overrides: | ||
ctx.set_source_rgba (fill_c[0], fill_c[1], fill_c[2], alpha) | ||
else: | ||
ctx.set_source_rgba (fill_c[0], fill_c[1], fill_c[2], alpha*fill_c[3]) | ||
ctx.set_source_rgba (fill_c[0], fill_c[1], fill_c[2], fill_c[3]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ouch. We don't have cairo tests to check this either (we need to think about testing the cairo backend more). Nice spot. |
||
ctx.fill_preserve() | ||
ctx.restore() | ||
ctx.stroke() | ||
|
@@ -150,7 +150,7 @@ def draw_path(self, gc, path, transform, rgbFace=None): | |
ctx.new_path() | ||
self.convert_path(ctx, path, transform) | ||
|
||
self._fill_and_stroke(ctx, rgbFace, gc.get_alpha()) | ||
self._fill_and_stroke(ctx, rgbFace, gc.get_alpha(), gc.get_forced_alpha()) | ||
|
||
def draw_image(self, gc, x, y, im): | ||
# bbox - not currently used | ||
|
@@ -316,7 +316,10 @@ def set_alpha(self, alpha): | |
GraphicsContextBase.set_alpha(self, alpha) | ||
_alpha = self.get_alpha() | ||
rgb = self._rgb | ||
self.ctx.set_source_rgba (rgb[0], rgb[1], rgb[2], _alpha) | ||
if self.get_forced_alpha(): | ||
self.ctx.set_source_rgba (rgb[0], rgb[1], rgb[2], _alpha) | ||
else: | ||
self.ctx.set_source_rgba (rgb[0], rgb[1], rgb[2], rgb[3]) | ||
|
||
|
||
#def set_antialiased(self, b): | ||
|
@@ -359,8 +362,8 @@ def set_dashes(self, offset, dashes): | |
self.renderer.points_to_pixels (np.asarray(dashes)), offset) | ||
|
||
|
||
def set_foreground(self, fg, isRGB=None): | ||
GraphicsContextBase.set_foreground(self, fg, isRGB) | ||
def set_foreground(self, fg, isRGBA=None): | ||
GraphicsContextBase.set_foreground(self, fg, isRGBA) | ||
if len(self._rgb) == 3: | ||
self.ctx.set_source_rgb(*self._rgb) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1064,7 +1064,7 @@ def alphaState(self, alpha): | |
self.nextAlphaState += 1 | ||
self.alphaStates[alpha] = \ | ||
(name, { 'Type': Name('ExtGState'), | ||
'CA': alpha, 'ca': alpha }) | ||
'CA': alpha[0], 'ca': alpha[1] }) | ||
return name | ||
|
||
def hatchPattern(self, hatch_style): | ||
|
@@ -1443,11 +1443,21 @@ def check_gc(self, gc, fillcolor=None): | |
orig_fill = gc._fillcolor | ||
gc._fillcolor = fillcolor | ||
|
||
orig_alphas = gc._effective_alphas | ||
|
||
if gc._forced_alpha: | ||
gc._effective_alphas = (gc._alpha, gc._alpha) | ||
elif fillcolor is None or len(fillcolor) < 4: | ||
gc._effective_alphas = (gc._rgb[3], 1.0) | ||
else: | ||
gc._effective_alphas = (gc._rgb[3], fillcolor[3]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your effective alphas approach here is, IMHO, something that could be done in the GC object. The GC could then just draw an RGBA of fill and an RGBA of line color _and the backend wouldn't need to know anything about forced_alpha. Do you know of a reason why that wouldn't be a viable approach? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess what I'm suggesting is that we updated the necessary signatures (to draw_path for example) such that we pass through an RGBA rather than an RGB and expect the backend to figure out the final face RGBA color. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That actually is how The issue is there are instances where the GC is applying an alpha property to things that have neither fill or draw colours -- e.g., images -- which is done by setting an alpha on the backend's underlying context. (The Mac OS X backend comes to mind as an example.) Supporting that means:
I believe for |
||
|
||
delta = self.gc.delta(gc) | ||
if delta: self.file.output(*delta) | ||
|
||
# Restore gc to avoid unwanted side effects | ||
gc._fillcolor = orig_fill | ||
gc._effective_alphas = orig_alphas | ||
|
||
def tex_font_mapping(self, texfont): | ||
if self.tex_font_map is None: | ||
|
@@ -2004,6 +2014,7 @@ class GraphicsContextPdf(GraphicsContextBase): | |
def __init__(self, file): | ||
GraphicsContextBase.__init__(self) | ||
self._fillcolor = (0.0, 0.0, 0.0) | ||
self._effective_alphas = (1.0, 1.0) | ||
self.file = file | ||
self.parent = None | ||
|
||
|
@@ -2072,8 +2083,8 @@ def dash_cmd(self, dashes): | |
offset = 0 | ||
return [list(dash), offset, Op.setdash] | ||
|
||
def alpha_cmd(self, alpha): | ||
name = self.file.alphaState(alpha) | ||
def alpha_cmd(self, alpha, forced, effective_alphas): | ||
name = self.file.alphaState(effective_alphas) | ||
return [name, Op.setgstate] | ||
|
||
def hatch_cmd(self, hatch): | ||
|
@@ -2138,7 +2149,7 @@ def clip_cmd(self, cliprect, clippath): | |
|
||
commands = ( | ||
(('_cliprect', '_clippath'), clip_cmd), # must come first since may pop | ||
(('_alpha',), alpha_cmd), | ||
(('_alpha', '_forced_alpha', '_effective_alphas'), alpha_cmd), | ||
(('_capstyle',), capstyle_cmd), | ||
(('_fillcolor',), fillcolor_cmd), | ||
(('_joinstyle',), joinstyle_cmd), | ||
|
@@ -2183,6 +2194,7 @@ def copy_properties(self, other): | |
""" | ||
GraphicsContextBase.copy_properties(self, other) | ||
self._fillcolor = other._fillcolor | ||
self._effective_alphas = other._effective_alphas | ||
|
||
def finalize(self): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,7 +289,7 @@ def _write_default_style(self): | |
writer = self.writer | ||
default_style = generate_css({ | ||
u'stroke-linejoin': u'round', | ||
u'stroke-linecap': u'square'}) | ||
u'stroke-linecap': u'butt'}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch -- indeed the "default" according to the SVG spec is butt. |
||
writer.start(u'defs') | ||
writer.start(u'style', type=u'text/css') | ||
writer.data(u'*{%s}\n' % default_style) | ||
|
@@ -386,15 +386,21 @@ def _get_style_dict(self, gc, rgbFace): | |
""" | ||
attrib = {} | ||
|
||
forced_alpha = gc.get_forced_alpha() | ||
|
||
if gc.get_hatch() is not None: | ||
attrib[u'fill'] = u"url(#%s)" % self._get_hatch(gc, rgbFace) | ||
if rgbFace is not None and len(rgbFace) == 4 and rgbFace[3] != 1.0 and not forced_alpha: | ||
attrib[u'fill-opacity'] = str(rgbFace[3]) | ||
else: | ||
if rgbFace is None: | ||
attrib[u'fill'] = u'none' | ||
elif tuple(rgbFace[:3]) != (0, 0, 0): | ||
attrib[u'fill'] = rgb2hex(rgbFace) | ||
if len(rgbFace) == 4 and rgbFace[3] != 1.0 and not forced_alpha: | ||
attrib[u'fill-opacity'] = str(rgbFace[3]) | ||
|
||
if gc.get_alpha() != 1.0: | ||
if forced_alpha and gc.get_alpha() != 1.0: | ||
attrib[u'opacity'] = str(gc.get_alpha()) | ||
|
||
offset, seq = gc.get_dashes() | ||
|
@@ -404,12 +410,15 @@ def _get_style_dict(self, gc, rgbFace): | |
|
||
linewidth = gc.get_linewidth() | ||
if linewidth: | ||
attrib[u'stroke'] = rgb2hex(gc.get_rgb()) | ||
rgb = gc.get_rgb() | ||
attrib[u'stroke'] = rgb2hex(rgb) | ||
if not forced_alpha and rgb[3] != 1.0: | ||
attrib[u'stroke-opacity'] = str(rgb[3]) | ||
if linewidth != 1.0: | ||
attrib[u'stroke-width'] = str(linewidth) | ||
if gc.get_joinstyle() != 'round': | ||
attrib[u'stroke-linejoin'] = gc.get_joinstyle() | ||
if gc.get_capstyle() != 'projecting': | ||
if gc.get_capstyle() != 'butt': | ||
attrib[u'stroke-linecap'] = _capstyle_d[gc.get_capstyle()] | ||
|
||
return attrib | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is subtle, but I think it will break the non forced alpha case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Should calling
set_alpha
turn on theforced_alpha
value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set_alpha
does turn on_force_alpha
-- in fact, it already did, I didn't need to change anything there. (But prior these changes, practically nothing actually paid attention to it.) Callinggc.set_alpha(None)
clears_forced_alpha
.This doesn't break the non-forced alpha case because the backends themselves, where appropriate, now pull the alpha value straight out of
_rgb[3]
in the non forced alpha case.