Browse Source

small optimization.

SakiKawasaki 5 years ago
parent
commit
088de97db4
6 changed files with 76 additions and 67 deletions
  1. 2 1
      README.md
  2. 1 1
      badgemaker.nimble
  3. 46 65
      badgemaker/badgemaker.nim
  4. BIN
      tests/test11.exe
  5. 11 0
      tests/test11.nim
  6. 16 0
      tests/test11.svg

+ 2 - 1
README.md

@@ -31,4 +31,5 @@ The Nim badgemaker tool.
 [![test7](https://github.com/Ethosa/badgemaker/blob/master/tests/test7.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test7.svg)  
 [![test8](https://github.com/Ethosa/badgemaker/blob/master/tests/test8.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test8.svg)  
 [![test9](https://github.com/Ethosa/badgemaker/blob/master/tests/test9.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test9.svg)  
-[![test10](https://github.com/Ethosa/badgemaker/blob/master/tests/test10.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test10.svg)
+[![test10](https://github.com/Ethosa/badgemaker/blob/master/tests/test10.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test10.svg)  
+[![test11](https://github.com/Ethosa/badgemaker/blob/master/tests/test11.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test11.svg)

+ 1 - 1
badgemaker.nimble

@@ -1,7 +1,7 @@
 [Package]
 name = "badgemaker"
 author = "Ethosa"
-version = "0.0.3"
+version = "0.0.4"
 description = "The Nim badgemaker tool."
 license = "AGPLv3"
 srcDir = "badgemaker"

+ 46 - 65
badgemaker/badgemaker.nim

@@ -50,122 +50,103 @@ proc setIcon*(badge: BadgeRef, image_path, color: string) =
   badge.image_color = color
 
 proc `$`*(badge: BadgeRef): string =
-  var tree = newXMLTree(
-    "svg", [], {
-    "xmlns": "http://www.w3.org/2000/svg",
-    "xmlns:xlink": "http://www.w3.org/1999/xlink",
-    "width": $badge.width,
-    "height": $badge.height
-  }.toXMLAttributes)
-
-  var gradient = newXMLTree(
-      "linearGradient", [],
-      {"id": "gradient",
-       "x2": "0", "y2": "100%"
+  let
+    # start variables
+    image_width = if badge.image_path != "": badge.height else: 0
+    labell = len(badge.label)
+    valuel = len(badge.value)
+    labelw = labell*(badge.font_size - 3).int + labell + image_width
+    valuew = valuel*(badge.font_size - 3).int + valuel + image_width
+    radius = if "square" in badge.style: "0" else: "4"
+    dif =
+      if labelw > valuew:
+        labelw - valuew + radius.parseInt
+      else:
+        labelw - badge.font_size + radius.parseInt
+    stop_opacity = if "plastic" notin badge.style: "0" else: ".1"
+
+  var
+    # trees
+    tree = newXMLTree(
+      "svg", [], {
+      "xmlns": "http://www.w3.org/2000/svg",
+      "xmlns:xlink": "http://www.w3.org/1999/xlink",
+      "width": $badge.width, "height": $badge.height
+    }.toXMLAttributes)
+    main = newXMLTree("g", [], {"mask": "url(#gradient)"}.toXMLAttributes)
+    text = newXMLTree("g", [], {
+      "font-family": badge.font, "font-size": $badge.font_size, "fill": badge.label_color
       }.toXMLAttributes)
+    gradient = newXMLTree(
+      "linearGradient", [],
+      {"id": "gradient", "x2": "0", "y2": "100%"}.toXMLAttributes)
+
   gradient.add newXMLTree("stop", [], {
-    "offset": "0", "stop-color": "#bbb",
-    "stop-opacity": if "plastic" notin badge.style:
-      "0"
-    else:
-      ".1"
+    "offset": "0", "stop-color": "#bbb", "stop-opacity": stop_opacity
     }.toXMLAttributes)
   gradient.add newXMLTree("stop", [], {
-    "offset": "1",
-    "stop-opacity": if "plastic" notin badge.style:
-      "0"
-    else:
-      ".1"
+    "offset": "1", "stop-opacity": stop_opacity
     }.toXMLAttributes)
 
-  var
-    main = newXMLTree("g", [], {"mask": "url(#gradient)"}.toXMLAttributes)
-    image_width =
-      if badge.image_path != "":
-        badge.height
-      else:
-        0
-    labelw = len(badge.label)*(badge.font_size - 3).int + len(badge.label) + image_width
-    valuew = len(badge.value)*(badge.font_size - 3).int + len(badge.value) + image_width
-    dif =
-      if labelw > valuew:
-        labelw - valuew
-      else:
-        labelw - badge.font_size
-    radius = if "square" in badge.style: "0" else: "4"
-
-  dif += radius.parseInt
-
   main.add newXMLTree(
     "rect", [], {
       "x": "0", "y": "0", "width": $labelw, "height": $badge.height,
       "rx": radius,
       "ry": radius,
       "style": "fill:" & badge.label_color
-    }.toXMLAttributes
-  )
+    }.toXMLAttributes)
   main.add newXMLTree(
     "rect", [], {
       "x": $dif, "y": "0",
       "width": $((badge.width - (badge.font_size/2).int) - (dif)),
       "height": $badge.height,
       "rx": "0", "ry": "0", "style": "fill:" & badge.value_color
-    }.toXMLAttributes
-  )
+    }.toXMLAttributes)
   main.add newXMLTree(
     "rect", [], {
       "x": $(badge.width - badge.font_size), "y": "0",
       "width": $badge.font_size, "height": $badge.height,
-      "rx": radius,
-      "ry": radius,
+      "rx": radius, "ry": radius,
       "style": "fill:" & badge.value_color
-    }.toXMLAttributes
-  )
+    }.toXMLAttributes)
   main.add newXMLTree(
     "path", [], {
       "fill": "url(#gradient)",
        "d": "M0 0h" & $badge.width & "v" & $badge.height & "H0z"
-    }.toXMLAttributes
-  )
-
-  var text = newXMLTree("g", [], {
-    "font-family": badge.font, "font-size": $badge.font_size, "fill": badge.label_color
     }.toXMLAttributes)
 
   text.add newXMLTree(
     "text", [], {
       "x": $(image_width + 2 + parseInt(radius)),
       "y": $(badge.height/2 + (badge.font_size/2) - 1.0), "fill": badge.label_text_color
-    }.toXMLAttributes
-  )
-  text[0].add newText badge.label
-
+    }.toXMLAttributes)
   text.add newXMLTree(
     "text", [], {
       "x": $(dif + 2),
       "y": $(badge.height/2 + (badge.font_size/2) - 1.0), "fill": badge.value_text_color
-    }.toXMLAttributes
-  )
+    }.toXMLAttributes)
+
+  text[0].add newText badge.label
   text[1].add newText badge.value
 
   tree.add gradient
   tree.add main
   tree.add text
   if badge.image_path != "":
-    var img = newFileStream(badge.image_path, fmRead)
-    var image = img.readAll
+    var
+      img = newFileStream(badge.image_path, fmRead)
+      image = img.readAll
     img.close
     tree.add newXMLTree(
       "image", [], {
         "xlink:href": "data:image/png;base64," & encode image,
         "width": $badge.height, "height": $badge.height,
-        "x": radius, "y": "0",
-        "fill": badge.image_color
-      }.toXMLAttributes
-    )
+        "x": radius, "y": "0", "fill": badge.image_color
+      }.toXMLAttributes)
   return $tree
 
 proc write*(badge: BadgeRef, filename: string) =
+  ## Writes SVG image in file.
   var strm = newFileStream(filename, fmWrite)
   strm.write $badge
   strm.close

BIN
tests/test11.exe


+ 11 - 0
tests/test11.nim

@@ -0,0 +1,11 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(
+  label="Open source", value="❤",
+  label_color="#77dd77", value_color="#212121",
+  label_text_color="#212121", value_text_color="#dd7777",
+  style="plastic", width=100
+)
+
+badge.write "test11.svg"

+ 16 - 0
tests/test11.svg

@@ -0,0 +1,16 @@
+<svg width="100" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="20">
+  <linearGradient x2="0" id="gradient" y2="100%">
+    <stop stop-color="#bbb" stop-opacity=".1" offset="0" />
+    <stop stop-opacity=".1" offset="1" />
+  </linearGradient>
+  <g mask="url(#gradient)">
+    <rect style="fill:#77dd77" width="110" y="0" x="0" rx="4" height="20" ry="4" />
+    <rect style="fill:#212121" width="10" y="0" x="84" rx="0" height="20" ry="0" />
+    <rect style="fill:#212121" width="12" y="0" x="88" rx="4" height="20" ry="4" />
+    <path fill="url(#gradient)" d="M0 0h100v20H0z" />
+  </g>
+  <g font-size="12" fill="#77dd77" font-family="DejaVu Sans,Verdana,Geneva,sans-serif">
+    <text y="15.0" x="6" fill="#212121">Open source</text>
+    <text y="15.0" x="86" fill="#dd7777">❤</text>
+  </g>
+</svg>