dyi_rails 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,208 @@
1
+ = DYI for Rails: A Library to Use DYI on Rails.
2
+
3
+ "DYI for Rails" is a library for use DYI on Rails. "DYI for Rails"
4
+ provides some helpers and module for drawing a image of DYI.
5
+
6
+ == Installing
7
+
8
+ To install "DYI for Rails", use the following command.
9
+
10
+ $ gem install dyi-rails
11
+
12
+ (Add sudo if you're installing under a POSIX system as root)
13
+
14
+ == How to Use
15
+
16
+ === Configuration
17
+
18
+ You add the following expression to <tt>RAILS_ROOT/config/environment.rb</tt>
19
+ so that you can use this library on your Rails application.
20
+
21
+ # in RAILS_ROOT/config/environment.rb
22
+
23
+ # Specify gems that this application depends on and have them installed with rake gems:install
24
+ # config.gem "bj"
25
+ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
26
+ # config.gem "sqlite3-ruby", :lib => "sqlite3"
27
+ # config.gem "aws-s3", :lib => "aws/s3"
28
+ config.gem "dyi" # add gem of DYI
29
+ config.gem "dyi_rails" # add gem of DYI for Rails
30
+
31
+ === Usage in View
32
+
33
+ You add the following expression to <tt>RAILS_ROOT/app/controllers/application_controller.rb</tt>
34
+ so that you can use {DyiRails::DyiHelper} on your Rails application.
35
+
36
+ # in RAILS_ROOT/app/controllers/application_controller.rb
37
+
38
+ class ApplicationController < ActionController::Base
39
+ helper :all # include all helpers, all the time
40
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
41
+
42
+ helper DyiRails::DyiHelper # add this expression, then you can use helper-methods provided by DyiHelper
43
+
44
+ # Scrub sensitive parameters from your log
45
+ # filter_parameter_logging :password
46
+ end
47
+
48
+ You create the image object using DYI as follows:
49
+
50
+ # in RAILS_ROOT/app/controllers/test_controller.rb
51
+
52
+ class TestController < ApplicationController
53
+ def index
54
+ # Creates image using DYI
55
+ @canvas = DYI::Canvas.new(200, 150)
56
+ brush = DYI::Drawing::Brush.blue_brush
57
+ brush.draw_rectangle(@canvas, [20, 20], 160, 110)
58
+ end
59
+ end
60
+
61
+ You can use helper-methods of {DyiRails::DyiHelper} in any view file, using inline
62
+ SVG in HTML5.
63
+
64
+ # in RAILS_ROOT/app/views/test/index.html.erb
65
+
66
+ <!DOCTYPE html>
67
+ <html lang="en">
68
+ <head>
69
+ <meta charset="utf-8" />
70
+ <title>Useage of DYI for Rails</title>
71
+ </head>
72
+ <body>
73
+ <div>
74
+ <h1>SVG Image created by DYI</h1>
75
+ <%= dyi_inline_image_tag @canvas, :alt => 'SVG Image' %>
76
+ </div>
77
+ </body>
78
+ </html>
79
+
80
+ In this sample, the output is as follows:
81
+
82
+ <!DOCTYPE html>
83
+ <html lang="en">
84
+ <head>
85
+ <title>Useage of DYI for Rails</title>
86
+ </head>
87
+ <body>
88
+ <div>
89
+ <h1>SVG Image created by DYI</h1>
90
+ <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
91
+ width="200" height="150" viewBox="0 0 200 150"
92
+ preserveAspectRatio="none">
93
+ <desc>SVG Image</desc>
94
+ <rect x="20" y="20" width="160" height="110" fill="#0000FF"/>
95
+ </svg>
96
+ </div>
97
+ </body>
98
+ </html>
99
+
100
+ In XML (e.g. XHTML, XSL-FO, etc...), you can use XML namespace.
101
+
102
+ # in RAILS_ROOT/app/views/test/index.html.erb
103
+ #
104
+ # NOTE: You must use "application/xhtml+xml" as content-type,
105
+ # when using XHTML include XML namespace.
106
+ # See http://www.w3.org/TR/xhtml-media-types/
107
+
108
+ <?xml version="1.0" encoding="utf-8"?>
109
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
110
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
111
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
112
+ <head>
113
+ <title>Useage of DYI for Rails</title>
114
+ </head>
115
+ <body>
116
+ <div>
117
+ <h1>SVG Image created by DYI</h1>
118
+ <%= dyi_inline_image_tag @canvas, :alt => 'SVG Image', :namespace => 'svg' %>
119
+ </div>
120
+ </body>
121
+ </html>
122
+
123
+ In this sample, the output is as follows:
124
+
125
+ <?xml version="1.0" encoding="utf-8"?>
126
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
127
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
128
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
129
+ <head>
130
+ <title>Useage of DYI for Rails</title>
131
+ </head>
132
+ <body>
133
+ <div>
134
+ <h1>SVG Image created by DYI</h1>
135
+ <svg:svg xmlns:svg="http://www.w3.org/2000/svg" version="1.1"
136
+ width="200" height="150" viewBox="0 0 200 150"
137
+ preserveAspectRatio="none">
138
+ <svg:desc>SVG Image</svg:desc>
139
+ <svg:rect x="20" y="20" width="160" height="110" fill="#0000FF"/>
140
+ </svg:svg>
141
+ </div>
142
+ </body>
143
+ </html>
144
+
145
+ For more information, see the document of {DyiRails::DyiHelper}.
146
+
147
+ === Usage in Controller
148
+
149
+ A method to send a DYI image to the client is defined in {DyiRails::Streaming}.
150
+ You can use the method to include {DyiRails::Streaming}, as follows:
151
+
152
+ # in RAILS_ROOT/app/controllers/test_controller.rb
153
+
154
+ class TestController < ApplicationController
155
+ include DyiRails::Streaming
156
+
157
+ def index
158
+ # Creates image using DYI
159
+ canvas = DYI::Canvas.new(200, 150)
160
+ brush = DYI::Drawing::Brush.blue_brush
161
+ brush.draw_rectangle(canvas, [20, 20], 160, 110)
162
+
163
+ # Sends image to the client
164
+ send_dyi_image(canvas)
165
+ end
166
+ end
167
+
168
+ In this sample, the response body is as follows:
169
+
170
+ <?xml version="1.0" encoding="UTF-8"?>
171
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
172
+ "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
173
+ <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
174
+ width="200" height="150" viewBox="0 0 200 150"
175
+ preserveAspectRatio="none">
176
+ <rect x="20" y="20" width="160" height="110" fill="#0000FF"/>
177
+ </svg>
178
+
179
+ You can also use graphics formats other than SVG (e.g. PNG, EPS, etc...).
180
+ See examples of {DyiRails::Streaming#send_dyi_image}.
181
+
182
+ == License
183
+
184
+ Copyright (c) 2012 Sound-F Co., Ltd. All rights reserved.
185
+
186
+ "DYI for Rails" is free software: you can redistribute it and/or modify
187
+ it under the terms of the GNU General Public License as published by
188
+ the Free Software Foundation, either version 3 of the License, or
189
+ (at your option) any later version.
190
+
191
+ "DYI for Rails" is distributed in the hope that it will be useful,
192
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
193
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
194
+ GNU General Public License for more details.
195
+
196
+ You should have received a copy of the GNU General Public License
197
+ along with "DYI for Rails". If not, see <http://www.gnu.org/licenses/>.
198
+
199
+ == Support
200
+
201
+ We support "DYI for Rails" using SourceForge.net. URL of "DYI for Rails"
202
+ Project is "http://sourceforge.net/projects/dyi-rails/". This page has
203
+ a tracker and forums.
204
+
205
+ We are also preparing the Japanese mailing list. More infomation
206
+ about the mailing list is indicated to
207
+ <http://open-dyi.org/contents.html#community> in Japanese,
208
+ including how to join it.
data/lib/dyi_rails.rb ADDED
@@ -0,0 +1,71 @@
1
+ # -*- encoding: UTF-8 -*-
2
+
3
+ # Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
4
+ #
5
+ # Author:: Mamoru Yuo
6
+ #
7
+ # This file is part of "DYI for Rails".
8
+ #
9
+ # "DYI for Rails" is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # "DYI for Rails" is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with "DYI for Rails". If not, see <http://www.gnu.org/licenses/>.
21
+
22
+ # Root namespace of "DYI for Rails".
23
+ # @since 0.0.0
24
+ module DyiRails
25
+
26
+ # "DYI for Rails" program version
27
+ VERSION = '0.0.0'
28
+
29
+ # URL of "DYI for Rails" Project
30
+ URL = 'http://sourceforge.net/projects/dyi-rails/'
31
+
32
+ # The correspondence of the image format to the mime-type
33
+ MIME_TYPE = {:svg => 'image/svg+xml',
34
+ :png => 'image/png',
35
+ :eps => 'application/postscript',
36
+ :xaml => 'application/xaml+xml'}
37
+
38
+ class << self
39
+
40
+ # Registers new correspondence of the image format to mime-type. When the
41
+ # image format has already registered, mime-type is overriden.
42
+ # @param [Symbol, String] format image format
43
+ # @param [String] mime_type mime-type which the image format corresponds to
44
+ def register_mime_type(format, mime_type)
45
+ MIME_TYPE[format.to_sym] = mime_type.to_s
46
+ end
47
+
48
+ # Returns mime-type which the given image format corresponds to.
49
+ # @param [Symbol, String] format image format
50
+ # @return [String] mime-type which the image format corresponds to
51
+ # @raise [ArgumentError] unknown format is given
52
+ def mime_type(format)
53
+ format = format.to_sym
54
+ unless MIME_TYPE.has_key?(format)
55
+ raise ArgumentError, "`#{options[:format]}' is unknown format"
56
+ end
57
+ return MIME_TYPE[format]
58
+ end
59
+ end
60
+ end
61
+
62
+ require 'dyi'
63
+
64
+ %w(
65
+
66
+ dyi_rails/dyi_helper.rb
67
+ dyi_rails/streaming.rb
68
+
69
+ ).each do |file_name|
70
+ require File.join(File.dirname(__FILE__), file_name)
71
+ end
@@ -0,0 +1,249 @@
1
+ # -*- encoding: UTF-8 -*-
2
+
3
+ # Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
4
+ #
5
+ # Author:: Mamoru Yuo
6
+ #
7
+ # This file is part of "DYI for Rails".
8
+ #
9
+ # "DYI for Rails" is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # "DYI for Rails" is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with "DYI for Rails". If not, see <http://www.gnu.org/licenses/>.
21
+
22
+
23
+ module DyiRails
24
+
25
+ # Provides a set of methods for making image tags using DYI.
26
+ module DyiHelper
27
+
28
+ # Returns a inline HTML element. The HTML element does not have a URI
29
+ # reference, and the element contains the image data itself.
30
+ # @param [DYI::Canvas, DYI::Chart::Base] canvas a canvas that hold the image
31
+ # @option options [String] :id id of the HTML element. If the canvas has
32
+ # _id_ attribute, this option is ignored.
33
+ # @option options [String] :class CSS class name of the HTML element
34
+ # @option options [String] :alt equivalent content for those who cannot
35
+ # process images or who have image loading disabled. If the canvas has
36
+ # _description_ attribute, this option is ignored. Default to <tt>'dyi
37
+ # image'</tt>
38
+ # @option options [String] :title title of the image. If the canvas has
39
+ # _title_ attribute, this option is ignored
40
+ # @option options [Symbol, String] :format format of the image. Default to
41
+ # +:svg+
42
+ # @option options [String] :namespace XML namespace when XML format (e.g.
43
+ # SVG) is specified at +:format+ option. If nothing is specified,
44
+ # XML namespace is not used
45
+ # @return [String] HTML element contains image data
46
+ # @example
47
+ # dyi_inline_image_tag(@canvas, :alt => 'my image')
48
+ # # => <svg width="200" height="150" version="1.1" viewBox="0 0 200 150"
49
+ # # xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none">
50
+ # # <description>my image</description>
51
+ # # ...
52
+ # # </svg>
53
+ #
54
+ # dyi_inline_image_tag(@canvas, :alt => 'my image', :namespace => 'svg')
55
+ # # => <svg:svg width="200" height="150" version="1.1" viewBox="0 0 200 150"
56
+ # # xmlns:svg="http://www.w3.org/2000/svg" preserveAspectRatio="none">
57
+ # # <svg:description>my image</svg:description>
58
+ # # ...
59
+ # # </svg:svg>
60
+ #
61
+ # dyi_inline_image_tag(@canvas, :alt => 'my image', :format => :png)
62
+ # # => <img width="200" height="150" alt="my image" src="data:image/png;base64,
63
+ # # ...(encoded PNG image)..." />
64
+ def dyi_inline_image_tag(canvas, options={})
65
+ case (format = (options[:format] || :svg).to_sym)
66
+ when :svg, :xaml #, :vml
67
+ # output inline XML
68
+ alt = canvas.description || options[:alt] || 'dyi image'
69
+
70
+ canvas.id = options[:id] if options[:id] && !canvas.inner_id
71
+ canvas.add_css_class(options[:class]) if options[:class]
72
+ canvas.description = alt unless canvas.description
73
+ canvas.title = options[:title] if options[:title] && !canvas.title
74
+ canvas.string(format, :inline_mode => true, :namespace => options[:namespace])
75
+ when :png
76
+ # output <img> tag with base64 encoding
77
+ element_id = canvas.inner_id || options[:id]
78
+ class_name = options[:class]
79
+ alt = canvas.description || options[:alt] || 'dyi image'
80
+ title = canvas.title || options[:title]
81
+ mime_type = DyiRails.mime_type(format)
82
+
83
+ tag_parts = ['<img']
84
+ tag_parts << " id=\"#{element_id}\"" if element_id
85
+ tag_parts << ' src="data:'
86
+ tag_parts << mime_type
87
+ tag_parts << ";base64,\n"
88
+ tag_parts << [canvas.string(format)].pack('m')[0..-2]
89
+ tag_parts << '"'
90
+ tag_parts << " type=\"#{mime_type}\""
91
+ tag_parts << " class=\"#{class_name}\"" if class_name
92
+ tag_parts << " width=\"#{canvas.real_width}\""
93
+ tag_parts << " height=\"#{canvas.real_height}\""
94
+ tag_parts << " alt=\"#{alt}\""
95
+ tag_parts << " title=\"#{title}\"" if title
96
+ tag_parts << " />"
97
+ tag_parts.join
98
+ else
99
+ # output <object> tag with base64 encoding
100
+ element_id = canvas.inner_id || options[:id]
101
+ class_name = options[:class]
102
+ alt = canvas.description || options[:alt] || 'dyi image'
103
+ title = canvas.title || options[:title]
104
+ mime_type = DyiRails.mime_type(format)
105
+
106
+ tag_parts = ['<object']
107
+ tag_parts << " id=\"#{element_id}\"" if element_id
108
+ tag_parts << " data=\"data:"
109
+ tag_parts << mime_type
110
+ tag_parts << ";base64,\n"
111
+ tag_parts << [canvas.string(format)].pack('m')[0..-2]
112
+ tag_parts << "\""
113
+ tag_parts << " type=\"#{mime_type}\""
114
+ tag_parts << " class=\"#{class_name}\"" if class_name
115
+ tag_parts << " width=\"#{canvas.real_width}\""
116
+ tag_parts << " height=\"#{canvas.real_height}\""
117
+ tag_parts << " title=\"#{title}\"" if title
118
+ tag_parts << ">"
119
+ tag_parts << alt
120
+ tag_parts << "</object>"
121
+ tag_parts.join
122
+ end
123
+ end
124
+
125
+ # Returns a HTML +_img_+ element.
126
+ # @option options [String] :id id of the HTML element
127
+ # @option options [String] :class CSS class name of the HTML element
128
+ # @option options [String] :alt equivalent content for those who cannot
129
+ # process images or who have image loading disabled. Default to 'dyi image'
130
+ # @option options [String] :title title of the image
131
+ # @option options [Integer] :width width of the image
132
+ # @option options [Integer] :height height of the image
133
+ # @option options [Symbol, String] :format format of the image. Default to
134
+ # +:svg+
135
+ # @option options [String] :controller controller of Rails' application
136
+ # that process the image. Default to 'images'
137
+ # @option options [String] :action action of Rails' application that process
138
+ # the image. Default to 'dyi'
139
+ # @option options [String] :model_id id as a parameter passed to the Rails'
140
+ # application that process the image. Default to 'dyi'
141
+ # @option options [String] other-options other options is passed to +url_for+
142
+ # method. See examples
143
+ # @return [String] HTML +_img_+ element contains URI reference to the image
144
+ # @example
145
+ # dyi_image_tag(:format => :png, :width => 200, :height => 150)
146
+ # # => <img width="200" height="150" alt="dyi image" src="/images/dyi/dyi.png" />
147
+ #
148
+ # dyi_image_tag(:format => :png, :width => 200, :height => 150, :id => 'emb',
149
+ # :controller => 'teams', :action => 'emblem', :model_id => '1')
150
+ # # => <img id="emb" width="200" height="150" alt="dyi image" src="/teams/emblem/1.png" />
151
+ #
152
+ # dyi_image_tag(:format => :png, :width => 200, :height => 150, :id => 'emb',
153
+ # :controller => 'teams', :action => 'emblem', :model_id => '1',
154
+ # :color => 'red', :type => 'simple')
155
+ # # => <img id="emb" width="200" height="150" alt="dyi image"
156
+ # # src="/teams/emblem/1.png?color=red&amp;type=simple" />
157
+ def dyi_image_tag(options={})
158
+ opt = options.clone
159
+ element_id = opt.delete(:id)
160
+ class_name = opt.delete(:class)
161
+ alt = opt.delete(:alt) || 'dyi image'
162
+ title = opt.delete(:title)
163
+ width = opt.delete(:width)
164
+ height = opt.delete(:height)
165
+
166
+ opt[:controller] = 'images' unless opt[:controller]
167
+ opt[:action] = 'dyi' unless opt[:action]
168
+ opt[:id] = opt.delete(:model_id) || 'dyi'
169
+ opt[:format] = 'svg' unless opt[:format]
170
+
171
+ tag_parts = ['<img']
172
+ tag_parts << " id=\"#{element_id}\"" if element_id
173
+ tag_parts << " src=\"#{url_for(opt)}\""
174
+ tag_parts << " class=\"#{class_name}\"" if class_name
175
+ tag_parts << " width=\"#{width}\"" if width
176
+ tag_parts << " height=\"#{height}\"" if height
177
+ tag_parts << " alt=\"#{alt}\""
178
+ tag_parts << " title=\"#{title}\"" if title
179
+ tag_parts << " />"
180
+ tag_parts.join
181
+ end
182
+
183
+ # Returns a HTML +_object_+ element.
184
+ # @option options [String] :id id of the HTML element
185
+ # @option options [String] :class CSS class name of the HTML element
186
+ # @option options [String] :alt equivalent content for those who cannot
187
+ # process images or who have image loading disabled. Default to 'dyi image'
188
+ # @option options [String] :title title of the image
189
+ # @option options [Integer] :width width of the image
190
+ # @option options [Integer] :height height of the image
191
+ # @option options [Symbol, String] :format format of the image. Default to
192
+ # +:svg+
193
+ # @option options [String] :controller controller of Rails' application
194
+ # that process the image. Default to 'images'
195
+ # @option options [String] :action action of Rails' application that process
196
+ # the image. Default to <tt>'dyi'</tt>
197
+ # @option options [String] :model_id id as a parameter passed to the Rails'
198
+ # application that process the image. Default to <tt>'dyi'</tt>
199
+ # @option options [String] other-options other options is passed to +url_for+
200
+ # method. See examples
201
+ # @return [String] HTML +_object_+ element contains URI reference to the
202
+ # image
203
+ # @example
204
+ # dyi_object_tag(:width => 200, :height => 150)
205
+ # # => <object width="200" height="150" type="image/svg+xml"
206
+ # # data="/images/dyi/dyi.svg">dyi image</object>
207
+ #
208
+ # dyi_object_tag(:format => :png, :width => 200, :height => 150,
209
+ # :id => 'emb', :alt => 'an emblem of the team',
210
+ # :controller => 'teams', :action => 'emblem', :model_id => '1')
211
+ # # => <object id="emb" width="200" height="150" type="image/svg+xml"
212
+ # # data="/teams/emblem/1.png">an emblem of the team</object>
213
+ #
214
+ # dyi_object_tag(:format => :png, :width => 200, :height => 150,
215
+ # :id => 'emb', :alt => 'an emblem of the team',
216
+ # :controller => 'teams', :action => 'emblem', :model_id => '1',
217
+ # :color => 'red', :type => 'simple')
218
+ # # => <object id="emb" width="200" height="150" type="image/svg+xml"
219
+ # # data="/teams/emblem/1.png">an emblem of the team</object>
220
+ def dyi_object_tag(options={})
221
+ opt = options.clone
222
+ element_id = opt.delete(:id)
223
+ class_name = opt.delete(:class)
224
+ alt = opt.delete(:alt) || 'dyi image'
225
+ title = opt.delete(:title)
226
+ width = opt.delete(:width)
227
+ height = opt.delete(:height)
228
+ mime_type = DyiRails.mime_type(opt[:format] || :svg)
229
+
230
+ opt[:controller] = 'images' unless opt[:controller]
231
+ opt[:action] = 'dyi' unless opt[:action]
232
+ opt[:id] = opt.delete(:model_id) || 'dyi'
233
+ opt[:format] = 'svg' unless opt[:format]
234
+
235
+ tag_parts = ['<object']
236
+ tag_parts << " id=\"#{element_id}\"" if element_id
237
+ tag_parts << " data=\"#{url_for(opt)}\""
238
+ tag_parts << " type=\"#{mime_type}\""
239
+ tag_parts << " class=\"#{class_name}\"" if class_name
240
+ tag_parts << " width=\"#{width}\"" if width
241
+ tag_parts << " height=\"#{height}\"" if height
242
+ tag_parts << " title=\"#{title}\"" if title
243
+ tag_parts << ">"
244
+ tag_parts << alt
245
+ tag_parts << "</object>"
246
+ tag_parts.join
247
+ end
248
+ end
249
+ end