Browse Source

added new tests, buf fixs.

SakiKawasaki 5 years ago
parent
commit
347308945b
9 changed files with 91 additions and 15 deletions
  1. 3 1
      README.md
  2. 1 1
      badgemaker.nimble
  3. 27 13
      badgemaker/badgemaker.nim
  4. BIN
      tests/test8.exe
  5. 15 0
      tests/test8.nim
  6. 15 0
      tests/test8.svg
  7. BIN
      tests/test9.exe
  8. 15 0
      tests/test9.nim
  9. 15 0
      tests/test9.svg

+ 3 - 1
README.md

@@ -9,4 +9,6 @@ The Nim badgemaker tool.
 [![test4](https://github.com/Ethosa/badgemaker/blob/master/tests/test4.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test4.svg)  
 [![test5](https://github.com/Ethosa/badgemaker/blob/master/tests/test5.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test5.svg)  
 [![test6](https://github.com/Ethosa/badgemaker/blob/master/tests/test6.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test6.svg)  
-[![test7](https://github.com/Ethosa/badgemaker/blob/master/tests/test7.svg)](https://github.com/Ethosa/badgemaker/blob/master/tests/test7.svg)
+[![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)

+ 1 - 1
badgemaker.nimble

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

+ 27 - 13
badgemaker/badgemaker.nim

@@ -15,6 +15,7 @@ type
     label_color: string  ## left color.
     value_color: string  ## right color.
     font: string
+    font_size: int
     width, height: int
     image_path: string
     image_color: string
@@ -23,23 +24,32 @@ type
 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 =
+  ## Creates a new Badge object.
   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, image_path: "", image_color: "")
+           width: width, height: height, image_path: "", image_color: "",
+           font_size: 12)
 
 proc setFont*(badge: BadgeRef, font: string) =
+  ## Sets badge font.
   badge.font = font
 
+proc setFontSize*(badge: BadgeRef, size: int) =
+  ## Sets badge font size.
+  badge.font_size = size
+
 proc setIcon*(badge: BadgeRef, image_path: string) =
+  ## Sets icon for badge.
   badge.image_path = image_path
 
 proc setIcon*(badge: BadgeRef, image_path, color: string) =
+  ## Sets icon with fill color.
   badge.image_path = image_path
   badge.image_color = color
 
-proc write*(badge: BadgeRef, filename: string) =
+proc `$`*(badge: BadgeRef): string =
   var tree = newXMLTree(
     "svg", [], {
     "xmlns": "http://www.w3.org/2000/svg",
@@ -75,15 +85,17 @@ proc write*(badge: BadgeRef, filename: string) =
         badge.height
       else:
         0
-    labelw = len(badge.label)*9 + len(badge.label) + image_width
-    valuew = len(badge.value)*9 + len(badge.value) + image_width
+    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 - 12
+        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,
@@ -95,15 +107,15 @@ proc write*(badge: BadgeRef, filename: string) =
   main.add newXMLTree(
     "rect", [], {
       "x": $dif, "y": "0",
-      "width": $((badge.width - 6) - (dif)),
+      "width": $((badge.width - (badge.font_size/2).int) - (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,
+      "x": $(badge.width - badge.font_size), "y": "0",
+      "width": $badge.font_size, "height": $badge.height,
       "rx": radius,
       "ry": radius,
       "style": "fill:" & badge.value_color
@@ -117,13 +129,13 @@ proc write*(badge: BadgeRef, filename: string) =
   )
 
   var text = newXMLTree("g", [], {
-    "font-family": badge.font, "font-size": "12", "fill": badge.label_color
+    "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 + 5), "fill": badge.label_text_color
+      "y": $(badge.height/2 + (badge.font_size/2) - 1.0), "fill": badge.label_text_color
     }.toXMLAttributes
   )
   text[0].add newText badge.label
@@ -131,7 +143,7 @@ proc write*(badge: BadgeRef, filename: string) =
   text.add newXMLTree(
     "text", [], {
       "x": $(dif + 2),
-      "y": $(badge.height/2 + 5), "fill": badge.value_text_color
+      "y": $(badge.height/2 + (badge.font_size/2) - 1.0), "fill": badge.value_text_color
     }.toXMLAttributes
   )
   text[1].add newText badge.value
@@ -151,7 +163,9 @@ proc write*(badge: BadgeRef, filename: string) =
         "fill": badge.image_color
       }.toXMLAttributes
     )
-  
+  return $tree
+
+proc write*(badge: BadgeRef, filename: string) =
   var strm = newFileStream(filename, fmWrite)
-  strm.write $tree
+  strm.write $badge
   strm.close

BIN
tests/test8.exe


+ 15 - 0
tests/test8.nim

@@ -0,0 +1,15 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(
+  label="Programming language:", value="Nim",
+  label_color="#282A36", value_color="fuchsia",
+  label_text_color="#e0e0e0", value_text_color="#333",
+  width=256, height=32, style="plastic"
+)
+
+badge.setFontSize 14
+
+badge.setIcon "C://Users/Admin/Desktop/nim.png"
+
+badge.write "test8.svg"

File diff suppressed because it is too large
+ 15 - 0
tests/test8.svg


BIN
tests/test9.exe


+ 15 - 0
tests/test9.nim

@@ -0,0 +1,15 @@
+# author: Ethosa
+import badgemaker
+
+var badge = newBadge(
+  label="Programming language:", value="Nim",
+  label_color="#282A36", value_color="fuchsia",
+  label_text_color="#e0e0e0", value_text_color="#333",
+  width=512, height=32, style="plastic"
+)
+
+badge.setFontSize 18
+
+badge.setIcon "C://Users/Admin/Desktop/nim.png"
+
+badge.write "test9.svg"

File diff suppressed because it is too large
+ 15 - 0
tests/test9.svg


Some files were not shown because too many files changed in this diff