lib/svgshield.rb in svgshield-0.1.2 vs lib/svgshield.rb in svgshield-0.1.3
- old
+ new
@@ -6,34 +6,63 @@
attr_reader :shield
def initialize(subject, status, color = '#aaa')
color = Color::CSS[color] if color[0] == '#'
- @shield = Rasem::SVGImage.new(width: 90, height: 20) do
- linearGradient('a', x2: 0, y2: '100%') do
+ # subject_width = 37
+ # status_width = 53
+ full_width = 86
+ full_height = 20
+
+ @shield = Rasem::SVGImage.new(width: full_width, height: full_height) do
+ linearGradient('b', x2: 0, y2: '100%') do
stop 0, '#bbb', '.1'
stop 1, nil, '.1'
end
- rect nil, nil, '90', '20', '3', fill: '#555', stroke_width: 0
- rect '37', nil, '53', '20', '3', fill: color, stroke_width: 0
- path fill: color, d: 'M37 0h4v20h-4z'
- rect nil, nil, '90', '20', '3', fill: 'url(#a)', stroke_width: 0
+ clipPath(id: 'a') do
+ rect nil, nil, full_width, full_height, rx: 3, stroke_width: 0, fill: '#fff'
+ end
+ group 'clip-path' => 'url(#a)' do
+ path fill: '#555', d: "M0 0h37v#{full_height}H0z"
+ path fill: color, d: "M37 0h49v#{full_height}H37z"
+ path fill: 'url(#b)' d: "M0 0h#{full_width}v#{full_height}H0z"
+ end
+
group(
fill: '#fff',
'text-anchor' => 'middle',
'font-family' => 'DejaVu Sans,Verdana,Geneva,sans-serif',
'font-size' => 11
) do
- text(19.5, 15, fill: '#010101', 'fill-opacity' => '.3') { raw subject }
- text(19.5, 14, fill: '#fff') { raw subject }
- text(62.5, 15, fill: '#010101', 'fill-opacity' => '.3') { raw status }
- text(62.5, 14, fill: '#fff') { raw status }
+ text(18.5, 15, fill: '#010101', 'fill-opacity' => '.3') { raw subject }
+ text(18.5, 14, fill: '#fff') { raw subject }
+ text(60.5, 15, fill: '#010101', 'fill-opacity' => '.3') { raw status }
+ text(60.5, 14, fill: '#fff') { raw status }
end
end
end
def to_s
shield.to_s
+ end
+
+ private
+
+ def string_width(string)
+ # Output is (theoretically, anyway) in pixels
+
+ string.chars.inject(0) { |sum, char| sum + char_width(char) }.round 0
+ end
+
+ def char_width(char)
+ {
+ 'i' => 8.0,
+ 'j' => 9.0,
+ 'l' => 6.0,
+ 'm' => 11.0,
+ 'w' => 11.0,
+ '1' => 8.5
+ }[char] || 9.0
end
end