File: to_file.rb

package info (click to toggle)
ruby-gnome2 0.15.0-1.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 7,704 kB
  • ctags: 8,558
  • sloc: ansic: 69,912; ruby: 19,511; makefile: 97; xml: 35; sql: 13
file content (326 lines) | stat: -rw-r--r-- 7,255 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
require "gnomeprint2"
require "gdk_pixbuf2" # for loading image.

class Renderer
  def initialize(filename)
    @job = Gnome::PrintJob.new
    @context = @job.context
    @config = @job.config
    @filename = filename
    init_setting
    draw
    @job.close
  end
  
  def print
    @job.print
  end
  
  private
  def init_setting
    init_printer
    @job.print_to_file(@filename)
    key = Gnome::PrintConfig::KEY_DOCUMENT_NAME
    @config[key] = "Sample gnome-print document"
  end

  def init_printer
    printers = Gnome::GPARoot.printers
    target_printer = find_printer(printers)
    
    if target_printer.nil?
      printer_names = printers.collect {|x| x.value}
      raise "Could not find available printer in #{printer_names.join(', ')}"
    end
    
    @config["Printer"] = target_printer.id
    if @config["Printer", :string] != target_printer.id
      raise "Could not set printer to #{target_printer.value}"
    end
  end
  
  def find_printer(printers)
    case File.extname(@filename)
    when /\.ps/i
      printers.find do |printer|
        /Postscript/i =~ printer.value
      end
    when /\.pdf/i
      printers.find do |printer|
        /PDF/i =~ printer.value
      end
    else
      nil
    end
  end
  
  def draw
    @context.begin_page("1") do
      draw_line
      draw_rectangle
      draw_arc
      draw_path
      draw_curve
      draw_text
      draw_pango
      draw_image
      draw_transformation
    end
  end

  def draw_line
    @context.save do
      @context.set_rgb_color(0, 0, 1)

      label("line", 120, 200)

      @context.save do
        @context.stroke do
          @context.move_to(100, 100)
          @context.line_to(170, 200)
          
          @context.set_dash([5, 3], 0)
          @context.line_width = 10
        end
      end
      
      @context.save do
        @context.stroke do
          @context.move_to(115, 90)
          @context.line_to(185, 190)
          
          @context.set_dash([5, 3], 2)
          @context.line_width = 10
        end
      end

      @context.save do
        @context.fill do
          @context.move_to(90, 100)
          @context.line_to(100, 150)
          @context.line_to(80, 190)
          @context.line_to(90, 100)
        end
      end
      
      @context.line_width = 20
      @context.line_cap = Art::PATH_STROKE_CAP_ROUND
      @context.line_stroked(100, 500, 170, 200)
    end
  end

  def draw_rectangle
    @context.save do
      @context.set_rgb_color(0, 0.5, 0.5)

      label("rectangle", 310, 300)
      
      @context.rect_filled(200, 300, 100, 100)
      
      @context.line_width = 10
      @context.line_join = Art::PATH_STROKE_JOIN_ROUND
      @context.rect_stroked(205, 190, 90, 90)
    end
  end

  def draw_arc
    @context.save do
      @context.set_rgb_color(0.5, 0.5, 0)

      label("arc", 70, 450)
      
      @context.stroke do
        @context.arc_to(50, 450, 10, 0, 90, true)
      end
      @context.save do
        @context.fill do
          @context.set_rgb_color(1, 0.4, 0.9)
          @context.opacity = 0.7
          @context.arc_to(40, 450, 10, 0, 359, false)
        end
      end
      @context.stroke do
        @context.arc_to(40, 450, 10, 0, 359, false)
      end
    end
  end

  def draw_path
    draw_bpath
    draw_vpath
  end

  def draw_bpath
    @context.save do
      @context.set_rgb_color(1, 0, 0)
      
      label("bpath", 500, 180)
      
      path = [
        [Art::MOVETO_OPEN, 580, 180],
        [Art::CURVETO, 630, 180, 350, 120, 420, 150],
        [Art::LINETO, 300, 50],
        [Art::END],
      ]
      bpath = Art::Bpath.new(path)
      @context.path(bpath)

      # append
      path = [
        [Art::LINETO, 500, 180],
        [Art::END],
      ]
      bpath = Art::Bpath.new(path)
      @context.path(bpath)
      
      @context.stroke
    end
  end

  def draw_vpath
    @context.save do
      @context.set_rgb_color(0, 1, 0)

      label("vpath", 400, 700)
      
      path = [
        [Art::MOVETO_OPEN, 400, 700],
        [Art::LINETO, 440, 780],
        [Art::END],
      ]
      vpath = Art::Vpath.new(path)
      @context.path(vpath)

      # append
      path = [
        [Art::LINETO, 500, 700],
        [Art::END],
      ]
      vpath = Art::Vpath.new(path)
      @context.path(vpath)
      
      @context.stroke
    end
  end
  
  def draw_curve
    @context.save do
      @context.set_rgb_color(0.5, 0, 0.5)
      
      label("curve", 500, 450)
      
      @context.stroke do
        @context.move_to(500, 300)
        @context.curve_to(470, 400, 470, 500, 520, 600)
      end
    end
  end
  
  def draw_text
    @context.move_to(100, 50)
    @context.show("abcde")
  end
  
  def draw_pango
    @context.move_to(350, 350)
    markupped_text = "<span color='red'>"
    markupped_text << "<span size='larger'>Pa</span>"
    markupped_text << "<span underline='double'>n</span>"
    markupped_text << "<span weight='bold' style='italic'>go</span>"
    markupped_text << "</span>"
    attrs, text = Pango.parse_markup(markupped_text)
    layout = @context.create_layout
    layout.text = text
    layout.set_attributes(attrs)
    @context.layout(layout)
  end

  def draw_image
    filename = Dir["**/*.png"].first
    pixbuf = Gdk::Pixbuf.new(filename)
    draw_image_by_convenience(pixbuf)
    draw_image_by_low_level_api(pixbuf)
  end

  def draw_image_by_convenience(pixbuf)
    @context.set_rgb_color(0.3, 0.1, 0.1)
    
    @context.save do
      label("image(c)", 150, 450)
      @context.translate(150, 450)
      @context.image(pixbuf)
    end

    @context.save do
      label("image(c) x2", 250, 450)
      @context.translate(250, 450)
      @context.image(pixbuf) do |context, _pixbuf|
        @context.scale(2, 2)
      end
    end
  end

  def draw_image_by_low_level_api(pixbuf)
    @context.set_rgb_color(0.1, 0.3, 0.1)
    
    args = [pixbuf.pixels, pixbuf.width, pixbuf.height, pixbuf.rowstride]

    @context.save do
      label("image(l)", 150, 650)
      @context.translate(150, 650)
      @context.scale(pixbuf.width, pixbuf.height)
      if pixbuf.has_alpha?
        @context.rgba_image(*args)
      else
        @context.rgb_image(*args)
      end
    end

    @context.save do
      label("image(l) x2", 250, 650)
      @context.translate(250, 650)
      @context.scale(pixbuf.width * 2, pixbuf.height * 2)
      if pixbuf.has_alpha?
        @context.rgba_image(*args)
      else
        @context.rgb_image(*args)
      end
    end
  end
  
  def draw_transformation
    draw_concat
  end

  def draw_concat
    @context.save do
      @context.set_rgb_color(0.7, 0.7, 0.9)
      label("concat", 100, 580)
      
      @context.set_rgb_color(0.9, 0.7, 0.7)
      
      label("before", 80, 600)
      @context.rect_stroked(100, 600, 10, 10)
      
      @context.set_rgb_color(0.7, 0.9, 0.7)
      label("after", 110, 630)
      @context.concat(Art::Affine.new([1.0, 0, 0, 1.0, 20, 30]))
      @context.rect_stroked(100, 600, 10, 10)
    end

  end
  
  def label(text, x, y)
    @context.save do
      @context.move_to(x, y)
      layout = @context.create_layout
      layout.text = text
      layout.context_changed
      @context.layout(layout)
    end
  end
  
end

Renderer.new("sample.ps").print
Renderer.new("sample.pdf").print