src/egonomy/models.py
changeset 74 5a3d8a3eb34d
parent 69 412ab5e76c65
child 89 da5504ff262e
--- a/src/egonomy/models.py	Tue Feb 26 17:52:44 2013 +0100
+++ b/src/egonomy/models.py	Wed Feb 27 18:42:19 2013 +0100
@@ -82,4 +82,70 @@
     title = models.CharField(max_length=2048, blank=True, null=True)
     description = models.TextField(blank=True, null=True)
     tags = models.TextField(blank=True, null=True)
+    
+    
+    def get_viewbox_info(self):
+        if not self.coordinates:
+            return None,None,None,None
+        # Now we split the coordinates to get the path points's max and min x and y
+        # A typical path is M0.1995 0.1574L0.3718 0.0131L0.7731 0.0597L0.5126 0.2915Z
+        points_str = self.coordinates.strip("M").strip("Z").split("L")
+        points_x = []
+        points_y = []
+        for p in points_str:
+            xy = p.split(" ")
+            points_x.append(float(xy[0]))
+            points_y.append(float(xy[1]))
+        # At this point, values are floats like 19.95, 15.74...
+        min_x = min(points_x)
+        max_x = max(points_x)
+        min_y = min(points_y)
+        max_y = max(points_y)
+        # At this point, values are floats like 19, 15...
+        # Now we build the viewbox, which is min_x min_y (max_x-min_x) (max_y-min_y) with number like 0.19 0.15...
+        # We use floor and +2 for the viewbox to be a bit larger than the strict fragment
+        vb_x = min_x
+        vb_y = min_y
+        vb_w = max_x - min_x
+        vb_h = max_y - min_y
+        return vb_x, vb_y, vb_w, vb_h
+    
+    # This property returns a ratio between width and height
+    @property
+    def ratio(self):
+        if not self.coordinates:
+            return 0
+        _, _, vb_w, vb_h = self.get_viewbox_info()
+        return vb_w/vb_h
+    
+    # This property returns a viewbox used in the swg xml and enbling to see the fragment only
+    @property
+    def viewbox(self):
+        if not self.coordinates:
+            return "0 0 1 1"
+        vb_x, vb_y, vb_w, vb_h = self.get_viewbox_info()
+        vb = str(vb_x) + " " + str(vb_y) + " " + str(vb_w) + " " + str(vb_h)
+        return vb
+    
+    # This property returns a square viewbox used in the swg xml and enbling to see the fragment only
+    @property
+    def viewbox_square(self):
+        if not self.coordinates:
+            return "0 0 1 1"
+        vb_x, vb_y, vb_w, vb_h = self.get_viewbox_info()
+        img_info = self.image.info
+        img_ratio = float(img_info.width) / float(img_info.height)
+        if vb_w > vb_h:
+            # If fragment w > h, we center the fragment on y ...
+            vb_y = max(0, vb_y - (((vb_w * img_ratio) - vb_h) / 2))
+            # ... and resize the viewbox's h with image's ratio
+            vb_h = vb_w * img_ratio
+        else:
+            # If fragment w > h, we center the fragment on x ...
+            vb_x = max(0, vb_x - (((vb_h / img_ratio) - vb_w) / 2))
+            # ... and we resize the viewbox's w with image's ratio
+            vb_w = vb_h / img_ratio
+        vb = str(vb_x) + " " + str(vb_y) + " " + str(vb_w) + " " + str(vb_h)
+        
+        return vb
     
\ No newline at end of file