Skip to content

Commit

Permalink
Support for square line cap
Browse files Browse the repository at this point in the history
  • Loading branch information
fathat committed Mar 3, 2013
1 parent f478d51 commit 7ec61dc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 38 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ Features:
- [x] "use" element
- [x] stroke-dasharray
- [ ] stroke-linecap ("butt", "round", "square")
- [ ] stroke-linejoin
- [x] stroke-linejoin ("miter", "round", "bevel")
- [x] Draw lines as triangle strip (instead of triangle fan)


- [*] Patterns
- [ ] userSpaceOnUse
- [x] objectBoundingBox
Expand Down
35 changes: 28 additions & 7 deletions glsvg/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def __init__(self, startp, endp, w=0):
self.lower_join = None
self.connector = []

@property
def direction(self):
return (self.end-self.start).normalized()

@property
def upper_edge(self):
return LineSegment(self.start + self.up_normal,
Expand Down Expand Up @@ -189,7 +193,7 @@ def split_line_by_pattern(points, pattern):
return lines


def calc_polyline(points, w, join_type='miter', miter_limit=4, closed=False):
def calc_polyline(points, w, line_cap='butt', join_type='miter', miter_limit=4, closed=False):

miter_length = w * miter_limit
points = [vec2(p) for p in points]
Expand All @@ -204,6 +208,11 @@ def calc_polyline(points, w, join_type='miter', miter_limit=4, closed=False):
lines[0].upper_join = lines[0].upper_edge.start
lines[0].lower_join = lines[0].lower_edge.start

if line_cap == 'square':
ext = lines[0].direction * w * -0.5
lines[0].upper_join = lines[0].upper_join + ext
lines[0].lower_join = lines[0].lower_join + ext

for i in range(1, len(lines)):
ln, pln = lines[i], lines[i-1]
_process_joint(ln, pln, miter_length, join_type=='round')
Expand Down Expand Up @@ -238,15 +247,27 @@ def calc_polyline(points, w, join_type='miter', miter_limit=4, closed=False):
ll.lower_v.append(lower_join)

else:
ll.upper_v.append(ll.upper_join)
ll.upper_v.append(ll.upper_edge.end)
ll.lower_v.append(ll.lower_join)
ll.lower_v.append(ll.lower_edge.end)
if line_cap == 'butt':
ll.upper_v.append(ll.upper_join)
ll.upper_v.append(ll.upper_edge.end)
ll.lower_v.append(ll.lower_join)
ll.lower_v.append(ll.lower_edge.end)
elif line_cap == 'square':
ext = ll.direction * w*0.5
ll.upper_v.append(ll.upper_join)
ll.upper_v.append(ll.upper_edge.end + ext)
ll.lower_v.append(ll.lower_join)
ll.lower_v.append(ll.lower_edge.end + ext)
elif line_cap == 'round':
ll.upper_v.append(ll.upper_join)
ll.upper_v.append(ll.upper_edge.end)
ll.lower_v.append(ll.lower_join)
ll.lower_v.append(ll.lower_edge.end)

return lines


def draw_polyline(points, w, color, join_type='miter', miter_limit=4, closed=False, debug=False):
def draw_polyline(points, w, color, line_cap='butt', join_type='miter', miter_limit=4, closed=False, debug=False):
if len(points) == 0:
return

Expand All @@ -265,7 +286,7 @@ def draw_polyline(points, w, color, join_type='miter', miter_limit=4, closed=Fal
if points[0] == points[-1]:
closed = True

lines = calc_polyline(points, w, join_type, miter_limit, closed)
lines = calc_polyline(points, w, line_cap, join_type, miter_limit, closed)
swap = False
vertices = []

Expand Down
4 changes: 2 additions & 2 deletions glsvg/svg_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ def _render_stroke(self):
ls = lines.split_line_by_pattern(loop_plus, self.style.stroke_dasharray)

for l in ls:
lines.draw_polyline(l, stroke_width, color=strokes[0], join_type=self.style.stroke_linejoin, miter_limit=miter_limit)
lines.draw_polyline(l, stroke_width, color=strokes[0], line_cap=self.style.stroke_linecap, join_type=self.style.stroke_linejoin, miter_limit=miter_limit)

else:
lines.draw_polyline(loop_plus, stroke_width, color=strokes[0], join_type=self.style.stroke_linejoin, miter_limit=miter_limit)
lines.draw_polyline(loop_plus, stroke_width, color=strokes[0], line_cap=self.style.stroke_linecap, join_type=self.style.stroke_linejoin, miter_limit=miter_limit)

def _render_stroke_stencil(self):
if not self.outline:
Expand Down
3 changes: 3 additions & 0 deletions glsvg/svg_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def from_element(self, element):
self.stroke_opacity = float(element.get('stroke-opacity', 1))
self.stroke_linejoin = element.get('stroke-linejoin', 'miter')
self.stroke_miterlimit = float(element.get('stroke-miterlimit', 4))
self.stroke_linecap = element.get('stroke-linecap', 'butt')

dash_array = element.get('stroke-dasharray', None)
if dash_array:
Expand All @@ -69,6 +70,8 @@ def from_element(self, element):
self.stroke_dasharray = [float(x.strip()) for x in dash_array.split(',')]
if 'stroke-linejoin' in style_dict:
self.stroke_linejoin = style_dict['stroke-linejoin']
if 'stroke-linecap' in style_dict:
self.stroke_linecap = style_dict['stroke-linecap']
if 'opacity' in style_dict:
self.fill_opacity *= float(style_dict['opacity'])
self.stroke_opacity *= float(style_dict['opacity'])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = "glsvg",
packages = ["glsvg"],
version = "0.2",
version = "0.3",
description = "OpenGL SVG Renderer",
author = "Ian Overgard",
author_email = "ian.overgard@gmail.com",
Expand Down
34 changes: 9 additions & 25 deletions svgs/0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7ec61dc

Please sign in to comment.