<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Sorting contours]]></title><description><![CDATA[<p dir="auto">Dear Frederik, Dear Robofont users,</p>
<p dir="auto">I need to order some contours inside a glyph.</p>
<p dir="auto">This sorting operation is not related to interpolation, so the <code>glyph.autoContourOrder()</code> method doesn’t come in help. Let’s say that I want to sort contours according to length, at the moment this would be my procedure:</p>
<ul>
<li>collect the contours in a list</li>
<li>perform the length measurement</li>
<li>sort the list according to the results</li>
<li>clear the “original” contours</li>
<li>draw the sorted contours in the emptied glyph according to the order of the list that stores them</li>
</ul>
<p dir="auto">This procedure works, but I was wondering if maybe there’s a method to achieve this goal without deleting the contours, just sorting in place. Like changing the “index” attribute to contour object. I tried to access this attribute, but this is the reply that I receive: <code>AttributeError: 'RobofabWrapperContour' object has no attribute 'index'</code></p>
<p dir="auto">Any tips?</p>
<p dir="auto">Appendix to the problem: after this sorting operation, I need to automatically select one of this contours. I tried the <code>select(state=True)</code> RPoint method or to change to <code>selected</code> RContour attribute, but they do not seem to work. Any advice to solve this?</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.robofont.com/topic/377/sorting-contours</link><generator>RSS for Node</generator><lastBuildDate>Tue, 19 May 2026 00:43:09 GMT</lastBuildDate><atom:link href="https://forum.robofont.com/topic/377.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 01 May 2015 05:56:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Sorting contours on Wed, 03 Jan 2018 17:27:09 GMT]]></title><description><![CDATA[<p dir="auto">Dear Frederik, Dear Robofont users,</p>
<p dir="auto">I need to order some contours inside a glyph.</p>
<p dir="auto">This sorting operation is not related to interpolation, so the <code>glyph.autoContourOrder()</code> method doesn’t come in help. Let’s say that I want to sort contours according to length, at the moment this would be my procedure:</p>
<ul>
<li>collect the contours in a list</li>
<li>perform the length measurement</li>
<li>sort the list according to the results</li>
<li>clear the “original” contours</li>
<li>draw the sorted contours in the emptied glyph according to the order of the list that stores them</li>
</ul>
<p dir="auto">This procedure works, but I was wondering if maybe there’s a method to achieve this goal without deleting the contours, just sorting in place. Like changing the “index” attribute to contour object. I tried to access this attribute, but this is the reply that I receive: <code>AttributeError: 'RobofabWrapperContour' object has no attribute 'index'</code></p>
<p dir="auto">Any tips?</p>
<p dir="auto">Appendix to the problem: after this sorting operation, I need to automatically select one of this contours. I tried the <code>select(state=True)</code> RPoint method or to change to <code>selected</code> RContour attribute, but they do not seem to work. Any advice to solve this?</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.robofont.com/post/376</link><guid isPermaLink="true">https://forum.robofont.com/post/376</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Wed, 03 Jan 2018 17:27:09 GMT</pubDate></item><item><title><![CDATA[Reply to Sorting contours on Wed, 03 Jan 2018 17:28:12 GMT]]></title><description><![CDATA[<p dir="auto">the best thing to do is to draw the glyph into a pen store and process the data and draw it back into the glyph</p>
<p dir="auto">this should work :)</p>
<p dir="auto">enjoy</p>
<pre><code class="language-python">from robofab.pens.pointPen import AbstractPointPen

class SortingPen(AbstractPointPen):
    
    def __init__(self):
        self._contours = []
        
    def beginPath(self):
        self._contours.append([])
                       
    def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
        self._contours[-1].append((pt, segmentType, smooth, name, kwargs))
    
    def endPath(self):
        pass

    def addComponent(self, baseGlyph, transformation):
        pass   
            
    def drawPoints(self, pen):
        # sort by the len of oncurves and offcurvers for each contour
        sortable = []
        for contour in self._contours:
            oncurves = 0
            offcurves = 0
            for pt, segmentType, smooth, name, kwargs in contour:
                if segmentType is None:
                    offcurves += 1
                else:
                    oncurves += 1
            sortable.append((oncurves, offcurves, contour))
        sortable.sort()

        for oncurvers, offcurvers, contour in sortable:
            pen.beginPath()
            for pt, segmentType, smooth, name, kwargs in contour:
                pen.addPoint(pt, 
                            smooth=smooth, 
                            segmentType=segmentType, 
                            name=name, 
                            **kwargs)
            pen.endPath()

# get the current glyph
g = CurrentGlyph()
# get the pen
sorting = SortingPen()
# draw into that pen
g.drawPoints(sorting)
# clear the contours
g.clearContours()
# draw back into the glyph
sorting.drawPoints(g.getPointPen())
</code></pre>
]]></description><link>https://forum.robofont.com/post/1359</link><guid isPermaLink="true">https://forum.robofont.com/post/1359</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Wed, 03 Jan 2018 17:28:12 GMT</pubDate></item><item><title><![CDATA[Reply to Sorting contours on Sun, 03 May 2015 06:36:15 GMT]]></title><description><![CDATA[<p dir="auto">Thanks, I'll try today.<br />
And what about the selection problem?<br />
Any advice?</p>
<p dir="auto">What I found in the robofab documentation, doesn't seem to work in Robofont.<br />
Or maybe I misunderstood the intended use of those methods/attributes.</p>
]]></description><link>https://forum.robofont.com/post/1360</link><guid isPermaLink="true">https://forum.robofont.com/post/1360</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Sun, 03 May 2015 06:36:15 GMT</pubDate></item><item><title><![CDATA[Reply to Sorting contours on Wed, 03 Jan 2018 17:28:55 GMT]]></title><description><![CDATA[<p dir="auto">use:</p>
<pre><code class="language-python">g = CurrentGlyph()

contour = g[1]
contour.selected = True
g.update()
</code></pre>
]]></description><link>https://forum.robofont.com/post/1362</link><guid isPermaLink="true">https://forum.robofont.com/post/1362</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Wed, 03 Jan 2018 17:28:55 GMT</pubDate></item><item><title><![CDATA[Reply to Sorting contours on Mon, 04 May 2015 07:52:28 GMT]]></title><description><![CDATA[<p dir="auto">Perfect, it works.<br />
Thanks!</p>
]]></description><link>https://forum.robofont.com/post/1363</link><guid isPermaLink="true">https://forum.robofont.com/post/1363</guid><dc:creator><![CDATA[[[global:former_user]]]]></dc:creator><pubDate>Mon, 04 May 2015 07:52:28 GMT</pubDate></item></channel></rss>