Browse Source

v0.0.1 was added

Ethosa 5 years ago
parent
commit
daf37d5b32
14 changed files with 262 additions and 2 deletions
  1. 2 2
      README.md
  2. 10 0
      badgemaker.nimble
  3. 129 0
      badgemaker/badgemaker.nim
  4. 1 0
      tests/nim.cfg
  5. 8 0
      tests/test1.nim
  6. 16 0
      tests/test1.svg
  7. 8 0
      tests/test2.nim
  8. 16 0
      tests/test2.svg
  9. 8 0
      tests/test3.nim
  10. 16 0
      tests/test3.svg
  11. 8 0
      tests/test4.nim
  12. 16 0
      tests/test4.svg
  13. 8 0
      tests/test5.nim
  14. 16 0
      tests/test5.svg

+ 2 - 2
README.md

@@ -1,2 +1,2 @@
-# badgemaker
-The Nim badgemaker tool.
+# Badgemaker
+The Nim badgemaker tool.

+ 10 - 0
badgemaker.nimble

@@ -0,0 +1,10 @@
+[Package]
+name = "badgemaker"
+author = "Ethosa"
+version = "0.0.1"
+description = "The Nim badgemaker tool."
+license = "AGPLv3"
+srcDir = "badgemaker"
+
+[Deps]
+Requires: "nim >= 1.0.0"

+ 129 - 0
badgemaker/badgemaker.nim

@@ -0,0 +1,129 @@
+# author: Ethosa
+import streams
+import xmltree
+import strutils
+
+
+type
+  BadgeRef* = ref object
+    style: string  ## egg "flat", "square" or "plastic"
+    label: string  ## left text.
+    value: string  ## right text.
+    label_text_color: string  ## left text color
+    value_text_color: string  ## right text color
+    label_color: string  ## left color.
+    value_color: string  ## right color.
+    font: string
+    width, height: int
+
+
+proc newBadge*(label="", value="", style="flat", label_color="#212121",
+               value_color="#e0e0e0", label_text_color="white",
+               value_text_color="black", width=120, height=20): BadgeRef =
+  BadgeRef(label: label, value: value, style: style,
+           label_text_color: label_text_color, value_text_color: value_text_color,
+           label_color: label_color, value_color: value_color,
+           font: "DejaVu Sans,Verdana,Geneva,sans-serif",
+           width: width, height: height)
+
+proc setFont*(badge: BadgeRef, font: string) =
+  badge.font = font
+
+proc write*(badge: BadgeRef, filename: string) =
+  var tree = newXMLTree(
+    "svg", [], {
+    "xmlns": "http://www.w3.org/2000/svg",
+    "width": $badge.width,
+    "height": $badge.height
+  }.toXMLAttributes)
+
+  var 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"
+    }.toXMLAttributes)
+  gradient.add newXMLTree("stop", [], {
+    "offset": "1",
+    "stop-opacity": if "plastic" notin badge.style:
+      "0"
+    else:
+      ".1"
+    }.toXMLAttributes)
+
+  var
+    main = newXMLTree("g", [], {"mask": "url(#gradient)"}.toXMLAttributes)
+    labelw = len(badge.label)*9 + len(badge.label)
+    valuew = len(badge.value)*9 + len(badge.value)
+    dif =
+      if labelw > valuew:
+        labelw - valuew
+      else:
+        labelw - 12
+    radius = if "square" in badge.style: "0" else: "4"
+  echo dif, " ", labelw, " ", valuew
+
+  main.add newXMLTree(
+    "rect", [], {
+      "x": "0", "y": "0", "width": $labelw, "height": $badge.height,
+      "rx": radius,
+      "ry": radius,
+      "style": "fill:" & badge.label_color
+    }.toXMLAttributes
+  )
+  main.add newXMLTree(
+    "rect", [], {
+      "x": $dif, "y": "0",
+      "width": $((badge.width - 6) - (dif)),
+      "height": $badge.height,
+      "rx": "0", "ry": "0", "style": "fill:" & badge.value_color
+    }.toXMLAttributes
+  )
+  main.add newXMLTree(
+    "rect", [], {
+      "x": $(badge.width - 12), "y": "0",
+      "width": "12", "height": $badge.height,
+      "rx": radius,
+      "ry": radius,
+      "style": "fill:" & badge.value_color
+    }.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": "12", "fill": badge.label_color
+    }.toXMLAttributes)
+
+  text.add newXMLTree(
+    "text", [], {
+      "x": "2", "y": $(badge.height/2 + 5), "fill": badge.label_text_color
+    }.toXMLAttributes
+  )
+  text[0].add newText badge.label
+
+  text.add newXMLTree(
+    "text", [], {
+      "x": $(dif + 2),
+      "y": $(badge.height/2 + 5), "fill": badge.value_text_color
+    }.toXMLAttributes
+  )
+  text[1].add newText badge.value
+
+  tree.add gradient
+  tree.add main
+  tree.add text
+  
+  var strm = newFileStream(filename, fmWrite)
+  strm.write $tree
+  strm.close

+ 1 - 0
tests/nim.cfg

@@ -0,0 +1 @@
+--path:"../badgemaker"

+ 8 - 0
tests/test1.nim

@@ -0,0 +1,8 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(label="helo blin", value="._.", style="plastic",
+                     label_color="#212121", value_color="fuchsia",
+                     label_text_color="#e0e0e0")
+
+badge.write "test1.svg"

+ 16 - 0
tests/test1.svg

@@ -0,0 +1,16 @@
+<svg width="120" 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:#212121" width="90" y="0" x="0" rx="4" height="20" ry="4" />
+    <rect style="fill:fuchsia" width="54" y="0" x="60" rx="0" height="20" ry="0" />
+    <rect style="fill:fuchsia" width="12" y="0" x="108" rx="4" height="20" ry="4" />
+    <path fill="url(#gradient)" d="M0 0h120v20H0z" />
+  </g>
+  <g font-size="12" fill="#212121" font-family="DejaVu Sans,Verdana,Geneva,sans-serif">
+    <text y="15.0" x="2" fill="#e0e0e0">helo blin</text>
+    <text y="15.0" x="62" fill="black">._.</text>
+  </g>
+</svg>

+ 8 - 0
tests/test2.nim

@@ -0,0 +1,8 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(label="Hello", value="biggest world", style="plastic",
+                     label_color="#282A36", value_color="#f1fa8c",
+                     label_text_color="#e0e0e0", width=130)
+
+badge.write "test2.svg"

+ 16 - 0
tests/test2.svg

@@ -0,0 +1,16 @@
+<svg width="130" 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:#282A36" width="50" y="0" x="0" rx="4" height="20" ry="4" />
+    <rect style="fill:#f1fa8c" width="86" y="0" x="38" rx="0" height="20" ry="0" />
+    <rect style="fill:#f1fa8c" width="12" y="0" x="118" rx="4" height="20" ry="4" />
+    <path fill="url(#gradient)" d="M0 0h130v20H0z" />
+  </g>
+  <g font-size="12" fill="#282A36" font-family="DejaVu Sans,Verdana,Geneva,sans-serif">
+    <text y="15.0" x="2" fill="#e0e0e0">Hello</text>
+    <text y="15.0" x="40" fill="black">biggest world</text>
+  </g>
+</svg>

+ 8 - 0
tests/test3.nim

@@ -0,0 +1,8 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(label="Hello", value="world", style="flat",
+                     label_color="#282A36", value_color="#f1fa8c",
+                     label_text_color="#e0e0e0")
+
+badge.write "test3.svg"

+ 16 - 0
tests/test3.svg

@@ -0,0 +1,16 @@
+<svg width="120" xmlns="http://www.w3.org/2000/svg" height="20">
+  <linearGradient x2="0" id="gradient" y2="100%">
+    <stop stop-color="#bbb" stop-opacity="0" offset="0" />
+    <stop stop-opacity="0" offset="1" />
+  </linearGradient>
+  <g mask="url(#gradient)">
+    <rect style="fill:#282A36" width="50" y="0" x="0" rx="4" height="20" ry="4" />
+    <rect style="fill:#f1fa8c" width="76" y="0" x="38" rx="0" height="20" ry="0" />
+    <rect style="fill:#f1fa8c" width="12" y="0" x="108" rx="4" height="20" ry="4" />
+    <path fill="url(#gradient)" d="M0 0h120v20H0z" />
+  </g>
+  <g font-size="12" fill="#282A36" font-family="DejaVu Sans,Verdana,Geneva,sans-serif">
+    <text y="15.0" x="2" fill="#e0e0e0">Hello</text>
+    <text y="15.0" x="40" fill="black">world</text>
+  </g>
+</svg>

+ 8 - 0
tests/test4.nim

@@ -0,0 +1,8 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(label="Hello", value="world", style="plastic square",
+                     label_color="#282A36", value_color="fuchsia",
+                     label_text_color="#e0e0e0")
+
+badge.write "test4.svg"

+ 16 - 0
tests/test4.svg

@@ -0,0 +1,16 @@
+<svg width="120" 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:#282A36" width="50" y="0" x="0" rx="0" height="20" ry="0" />
+    <rect style="fill:fuchsia" width="76" y="0" x="38" rx="0" height="20" ry="0" />
+    <rect style="fill:fuchsia" width="12" y="0" x="108" rx="0" height="20" ry="0" />
+    <path fill="url(#gradient)" d="M0 0h120v20H0z" />
+  </g>
+  <g font-size="12" fill="#282A36" font-family="DejaVu Sans,Verdana,Geneva,sans-serif">
+    <text y="15.0" x="2" fill="#e0e0e0">Hello</text>
+    <text y="15.0" x="40" fill="black">world</text>
+  </g>
+</svg>

+ 8 - 0
tests/test5.nim

@@ -0,0 +1,8 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(label="Hello", value="world", style="flat square",
+                     label_color="#282A36", value_color="fuchsia",
+                     label_text_color="#e0e0e0")
+
+badge.write "test5.svg"

+ 16 - 0
tests/test5.svg

@@ -0,0 +1,16 @@
+<svg width="120" xmlns="http://www.w3.org/2000/svg" height="20">
+  <linearGradient x2="0" id="gradient" y2="100%">
+    <stop stop-color="#bbb" stop-opacity="0" offset="0" />
+    <stop stop-opacity="0" offset="1" />
+  </linearGradient>
+  <g mask="url(#gradient)">
+    <rect style="fill:#282A36" width="50" y="0" x="0" rx="0" height="20" ry="0" />
+    <rect style="fill:fuchsia" width="76" y="0" x="38" rx="0" height="20" ry="0" />
+    <rect style="fill:fuchsia" width="12" y="0" x="108" rx="0" height="20" ry="0" />
+    <path fill="url(#gradient)" d="M0 0h120v20H0z" />
+  </g>
+  <g font-size="12" fill="#282A36" font-family="DejaVu Sans,Verdana,Geneva,sans-serif">
+    <text y="15.0" x="2" fill="#e0e0e0">Hello</text>
+    <text y="15.0" x="40" fill="black">world</text>
+  </g>
+</svg>