japanese-bookkeeping-svg 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c43657e693b2aaac4e582951f79a2fbfba17a12
4
- data.tar.gz: ee57cdc74902eae050f8e17c81c0849383cd4473
3
+ metadata.gz: 57ad3fae124e79aa17980b3e240320d237ee3957
4
+ data.tar.gz: 2d558545c1c2ae08281cba5c745289b6cc588d11
5
5
  SHA512:
6
- metadata.gz: 6fcd45ddddde80a059aa62b2cce20f9c37a858fcf2f529e386b0b53261df0fd0f1c81359ebd78cdee78aa0dcd9cd492a04e53051f104de3043f9862d0b9238dc
7
- data.tar.gz: cba1e8454b67643459862c6d4ba06d0151293604717e5500ffec1db8966ba237664d3f154a05fadc47dbc7a8159c50148f2c4bb962deeb255be23e71b001c9b1
6
+ metadata.gz: 259783e88b31a6cc487a0fc2048f709c56d697ecb75f93716a299d9560d9104aba2f2ea22240feef686552d72f5a3dba645300caf542622efa3a16a74a3e400a
7
+ data.tar.gz: 83081bd1e2c1f0a8100fa8e84908ae81330da8a4bbf753a582cad88c6ad2edc88726972424b8fcd1a65d9be0eb35a055f191e98fa78888524e9e5d9901bd1825
@@ -0,0 +1,10 @@
1
+ == 2016-06-26 version 0.0.2
2
+
3
+ * Improve appearances
4
+ * Add Batik usage on README.md
5
+
6
+
7
+ == 2016-06-25 version 0.0.1
8
+
9
+ * First release
10
+
data/README.md CHANGED
@@ -1,10 +1,16 @@
1
1
  # japanese-bookkeeping-svg
2
2
 
3
+ [![Build Status](https://travis-ci.org/gunyarakun/japanese-bookkeeping-svg.svg?branch=master)](https://travis-ci.org/gunyarakun/japanese-bookkeeping-svg)
4
+ [![Gem Version](https://badge.fury.io/rb/japanese-bookkeeping-svg.svg)](https://badge.fury.io/rb/japanese-bookkeeping-svg)
5
+ ![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)
6
+
3
7
  Generate SVG files for Japanese style bookkeeping diagrams.
4
8
 
9
+ :warning: This SVG uses `textLength` attritube which is not be supported by some SVG viewer. Use [Firefox](https://www.mozilla.org/en-US/firefox/products/), [Batik](https://xmlgraphics.apache.org/batik/), etc.
10
+
5
11
  ## Usage
6
12
 
7
- ```
13
+ ```bash
8
14
  gem install japanese-bookkeeping-svg
9
15
  ```
10
16
 
@@ -47,6 +53,23 @@ File.open('t-accounts.svg', 'w') do |file|
47
53
  end
48
54
  ```
49
55
 
56
+ ## Conversion to PNG with Batik
57
+
58
+ Make `user.css`.
59
+
60
+ ```css
61
+ text {
62
+ font-size: 16px;
63
+ font-family: "ヒラギノ明朝 ProN W6", "HiraMinProN-W6";
64
+ }
65
+ ```
66
+
67
+ Execute.
68
+
69
+ ```bash
70
+ java -jar batik-1.8/batik-rasterizer-1.8.jar -w 2400 -cssUser user.css out.svg
71
+ ```
72
+
50
73
  ## License
51
74
 
52
75
  ``japanese-bookkeeping-svg`` is distributed under the terms of the MIT license (see LICENSE.txt).
@@ -1,7 +1,16 @@
1
+ # rubocop:disable Style/FileName
1
2
  require_relative 'japanese_bookkeeping_svg/svg_generator'
2
3
  require_relative 'japanese_bookkeeping_svg/gem_version'
3
4
 
4
5
  module JapaneseBookkeepingSVG
6
+ WIDTH = 500
7
+ CENTER_X = WIDTH / 2
8
+ LEFT_MARGIN = 15
9
+ RIGHT_MARGIN = 10
10
+ ACCOUNT_WIDTH = 110
11
+ AMOUNT_X = LEFT_MARGIN + ACCOUNT_WIDTH + RIGHT_MARGIN
12
+ AMOUNT_WIDTH = 105
13
+
5
14
  @@line_height = SVGGenerator.convert_unit('1em') # rubocop:disable Style/ClassVars
6
15
 
7
16
  def self.line_height
@@ -14,18 +23,22 @@ module JapaneseBookkeepingSVG
14
23
  }
15
24
  end
16
25
 
17
- def self.account_value(svg, x, y, journals, with_brackets) # rubocop:disable Metrics/MethodLength
26
+ def self.account_value(svg, x, y, journals, with_brackets) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
18
27
  key_style = {
19
- 'textLength' => 125,
20
- # 'xml:space' => 'preserve'
28
+ 'textLength' => ACCOUNT_WIDTH
21
29
  }
22
30
  sum_value = 0
23
31
  journals.map do |key, value|
32
+ c_x = x
24
33
  y += line_height
25
- svg.text(x, y, '(') if with_brackets
26
- svg.text(x + 15, y, key.to_s, key_style)
27
- svg.text(x + 150, y, ')') if with_brackets
28
- svg.text(x + 290, y, delimited_number(value).to_s, value_style)
34
+ key_style['lengthAdjust'] = key.size > 8 ? 'spacingAndGlyphs' : 'spacing'
35
+ svg.text(c_x, y, '(') if with_brackets
36
+ c_x += LEFT_MARGIN
37
+ svg.text(c_x, y, key.to_s, key_style)
38
+ c_x += ACCOUNT_WIDTH + RIGHT_MARGIN
39
+ svg.text(c_x, y, ')') if with_brackets
40
+ c_x += AMOUNT_WIDTH
41
+ svg.text(c_x, y, delimited_number(value).to_s, value_style)
29
42
  sum_value += value
30
43
  end
31
44
 
@@ -36,16 +49,17 @@ module JapaneseBookkeepingSVG
36
49
  # TODO: check amount
37
50
  SVGGenerator.new do
38
51
  JapaneseBookkeepingSVG.account_value(self, 0, 0, debits, true)
39
- JapaneseBookkeepingSVG.account_value(self, 300, 0, credits, true)
52
+ JapaneseBookkeepingSVG.account_value(self, CENTER_X + RIGHT_MARGIN, 0, credits, true)
40
53
  end
41
54
  end
42
55
 
43
56
  def self.t_accounts(account, debits, credits) # rubocop:disable Metrics/LineLength,Metrics/MethodLength,Metrics/AbcSize
44
57
  # TODO: calc amount
45
58
  line_height = SVGGenerator.convert_unit('1em')
59
+ that = self
46
60
  SVGGenerator.new do
47
61
  account_style = {
48
- 'textLength' => 145,
62
+ 'textLength' => ACCOUNT_WIDTH,
49
63
  'text-anchor' => 'middle'
50
64
  }
51
65
  line_style = {
@@ -53,42 +67,43 @@ module JapaneseBookkeepingSVG
53
67
  'stroke-width' => 1
54
68
  }
55
69
 
56
- text(300, line_height, account, account_style)
70
+ account_style['lengthAdjust'] = account.size > 8 ? 'spacingAndGlyphs' : 'spacing'
71
+ text(CENTER_X, line_height, account, account_style)
57
72
  t_start_y = line_height + 8
58
- line(0, t_start_y, 600, t_start_y, line_style)
73
+ line(0, t_start_y, WIDTH, t_start_y, line_style)
59
74
 
60
75
  y = t_start_y + 5
61
- debits_y, sum_debits = JapaneseBookkeepingSVG.account_value(self, 0, y, debits, false)
62
- credits_y, sum_credits = JapaneseBookkeepingSVG.account_value(self, 300, y, credits, false)
76
+ debits_y, sum_debits = that.account_value(self, 0, y, debits, false)
77
+ credits_y, sum_credits = that.account_value(self, CENTER_X, y, credits, false)
63
78
 
64
79
  if debits_y < credits_y
65
80
  y = credits_y + 8
66
- line(15, y, 140, debits_y + 8, line_style)
67
- line(15, y, 300, y, line_style)
68
- line(460, y, 600, y, line_style)
81
+ line(LEFT_MARGIN, y, AMOUNT_X, debits_y + 8, line_style)
82
+ line(LEFT_MARGIN, y, CENTER_X, y, line_style)
83
+ line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)
69
84
  elsif debits_y > credits_y
70
85
  y = debits_y + 8
71
- line(160, y, 300, y, line_style)
72
- line(315, y, 440, credits_y + 8, line_style)
73
- line(315, y, 600, y, line_style)
86
+ line(AMOUNT_X, y, CENTER_X, y, line_style)
87
+ line(CENTER_X + LEFT_MARGIN, y, CENTER_X + AMOUNT_X, credits_y + 8, line_style)
88
+ line(CENTER_X + LEFT_MARGIN, y, WIDTH, y, line_style)
74
89
  else # eq
75
90
  y = debits_y + 8
76
- line(160, y, 300, y, line_style)
77
- line(460, y, 600, y, line_style)
91
+ line(AMOUNT_X, y, CENTER_X, y, line_style)
92
+ line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)
78
93
  end
79
94
 
80
95
  y += line_height + 3
81
- text(290, y, JapaneseBookkeepingSVG.delimited_number(sum_debits).to_s, JapaneseBookkeepingSVG.value_style)
82
- text(590, y, JapaneseBookkeepingSVG.delimited_number(sum_credits).to_s, JapaneseBookkeepingSVG.value_style)
96
+ text(CENTER_X - RIGHT_MARGIN, y, that.delimited_number(sum_debits).to_s, that.value_style)
97
+ text(WIDTH - RIGHT_MARGIN, y, that.delimited_number(sum_credits).to_s, that.value_style)
83
98
 
84
99
  y += 8
85
- line(160, y, 300, y, line_style)
86
- line(460, y, 600, y, line_style)
100
+ line(AMOUNT_X, y, CENTER_X, y, line_style)
101
+ line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)
87
102
  y += 3
88
- line(160, y, 300, y, line_style)
89
- line(460, y, 600, y, line_style)
103
+ line(AMOUNT_X, y, CENTER_X, y, line_style)
104
+ line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)
90
105
 
91
- line(300, t_start_y, 300, y, line_style)
106
+ line(CENTER_X, t_start_y, CENTER_X, y, line_style)
92
107
  end
93
108
  end
94
109
 
@@ -1,3 +1,3 @@
1
1
  module JapaneseBookkeepingSVG
2
- VERSION = '0.0.1'.freeze unless defined? JapaneseBookkeepingSVG::Version
2
+ VERSION = '0.0.2'.freeze unless defined? JapaneseBookkeepingSVG::Version
3
3
  end
@@ -1,5 +1,5 @@
1
1
  module JapaneseBookkeepingSVG
2
- class SVGGenerator
2
+ class SVGGenerator # rubocop:disable Metrics/ClassLength
3
3
  def initialize(&block)
4
4
  @output = []
5
5
  @max_width = 0
@@ -32,11 +32,12 @@ module JapaneseBookkeepingSVG
32
32
  update_max_width_and_height(x2, y2)
33
33
  end
34
34
 
35
- def text(x, y, text, style = {}) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
35
+ def text(x, y, text, style = {}) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
36
36
  s = style.clone
37
- max_width = self.class.convert_unit(s['textLength'])
38
- attrs = ['font-family', 'font-size', 'text-anchor', 'textLength', 'xml:space'].map do |attr|
37
+ attrs = {}
38
+ attrs_str = ['font-family', 'font-size', 'text-anchor', 'textLength', 'lengthAdjust', 'xml:space'].map do |attr|
39
39
  if s.key?(attr)
40
+ attrs[attr] = s[attr]
40
41
  %( #{attr}="#{s.delete attr}")
41
42
  else
42
43
  ''
@@ -44,7 +45,7 @@ module JapaneseBookkeepingSVG
44
45
  end.join('')
45
46
 
46
47
  text_lines = []
47
- text_lines << %(<text x="#{x}" y="#{y}"#{attrs}#{style_attr(s)}>)
48
+ text_lines << %(<text x="#{x}" y="#{y}"#{attrs_str}#{style_attr(s)}>)
48
49
  max_width_em = 0
49
50
  height_em = 0
50
51
  dy = 0
@@ -58,8 +59,15 @@ module JapaneseBookkeepingSVG
58
59
  text_lines << '</text>'
59
60
  @output << text_lines.join('')
60
61
 
61
- max_width ||= self.class.convert_unit("#{max_width_em}em")
62
- max_x = x + max_width
62
+ max_width = self.class.convert_unit(attrs['textLength'] || "#{max_width_em}em")
63
+ case attrs['text-anchor']
64
+ when nil, 'start'
65
+ max_x = x + max_width
66
+ when 'middle'
67
+ max_x = x + max_width / 2
68
+ when 'end'
69
+ max_x = x
70
+ end
63
71
  max_y = y + self.class.convert_unit("#{height_em}em")
64
72
  update_max_width_and_height(max_x, max_y)
65
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: japanese-bookkeeping-svg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tasuku SUENAGA a.k.a. gunyarakun
@@ -92,6 +92,7 @@ files:
92
92
  - ".rubocop.yml"
93
93
  - ".rubocop_todo.yml"
94
94
  - ".travis.yml"
95
+ - ChangeLog
95
96
  - Gemfile
96
97
  - LICENSE.txt
97
98
  - README.md