Class: JapaneseBookkeepingSVG::SVGGenerator
- Inherits:
-
Object
- Object
- JapaneseBookkeepingSVG::SVGGenerator
- Defined in:
- lib/japanese_bookkeeping_svg/svg_generator.rb
Overview
rubocop:disable Metrics/ClassLength
Class Method Summary collapse
-
.convert_unit(length) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength.
Instance Method Summary collapse
- #close(width = @max_width, height = @max_height) ⇒ Object
-
#initialize(&block) ⇒ SVGGenerator
constructor
A new instance of SVGGenerator.
- #line(x1, y1, x2, y2, style = {}) ⇒ Object
-
#text(x, y, text, style = {}) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity.
- #to_s ⇒ Object
Constructor Details
#initialize(&block) ⇒ SVGGenerator
Returns a new instance of SVGGenerator.
3 4 5 6 7 8 9 10 11 |
# File 'lib/japanese_bookkeeping_svg/svg_generator.rb', line 3 def initialize(&block) @output = [] @max_width = 0 @max_height = 0 return unless block instance_exec(&block) close end |
Class Method Details
.convert_unit(length) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/japanese_bookkeeping_svg/svg_generator.rb', line 75 def self.convert_unit(length) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength return nil if length.nil? m = /\A(\d+)(em|ex|px|in|cm|mm|pt|pc)?\z/.match(length.to_s) raise ArgumentError, "Unknown length #{length}" unless m v = m[1].to_i return v if m[2].nil? case m[2].intern when :em, :ex v * 16 when :px v when :in v * 96 when :cm (v * 96) / 2.54 when :mm (v * 96) / 25.4 when :pt (v * 4).fdiv(3) when :pc v * 16 end end |
Instance Method Details
#close(width = @max_width, height = @max_height) ⇒ Object
13 14 15 16 |
# File 'lib/japanese_bookkeeping_svg/svg_generator.rb', line 13 def close(width = @max_width, height = @max_height) unshift_header(width, height) end |
#line(x1, y1, x2, y2, style = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/japanese_bookkeeping_svg/svg_generator.rb', line 22 def line(x1, y1, x2, y2, style = {}) s = style.clone attrs = ['stroke', 'stroke-width'].map do |attr| if s.key?(attr) %( #{attr}="#{s.delete attr}") else '' end end.join('') @output << %(<line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}"#{attrs}#{style_attr(s)} />) update_max_width_and_height(x2, y2) end |
#text(x, y, text, style = {}) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/japanese_bookkeeping_svg/svg_generator.rb', line 35 def text(x, y, text, style = {}) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity s = style.clone attrs = {} attrs_str = ['font-family', 'font-size', 'text-anchor', 'textLength', 'lengthAdjust', 'xml:space'].map do |attr| if s.key?(attr) attrs[attr] = s[attr] %( #{attr}="#{s.delete attr}") else '' end end.join('') text_lines = [] text_lines << %(<text x="#{x}" y="#{y}"#{attrs_str}#{style_attr(s)}>) max_width_em = 0 height_em = 0 dy = 0 text.each_line do |line| line.chomp! text_lines << %(<tspan x="#{x}" dy="#{dy}em">#{line}</tspan>) dy = 1 max_width_em = line.size if line.size > max_width_em height_em += 1 end text_lines << '</text>' @output << text_lines.join('') max_width = self.class.convert_unit(attrs['textLength'] || "#{max_width_em}em") case attrs['text-anchor'] when nil, 'start' max_x = x + max_width when 'middle' max_x = x + max_width / 2 when 'end' max_x = x end max_y = y + self.class.convert_unit("#{height_em}em") update_max_width_and_height(max_x, max_y) end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/japanese_bookkeeping_svg/svg_generator.rb', line 18 def to_s @output.join("\n") end |