Class: DYI::Formatter::EpsFormatter

Inherits:
Base
  • Object
show all
Defined in:
lib/dyi/formatter/eps_formatter.rb,
lib/ironruby.rb

Overview

Since:

  • 0.0.0

Instance Method Summary collapse

Methods inherited from Base

#initialize, #save, #string, #write_image

Constructor Details

This class inherits a constructor from DYI::Formatter::Base

Instance Method Details

#base_transformObject

Since:

  • 0.0.0



59
60
61
62
63
64
# File 'lib/dyi/formatter/eps_formatter.rb', line 59

def base_transform
  pt_ratio = 1.quo(Length.unit_ratio('pt'))
  <<-EOS
[#{pt_ratio} 0 0 -#{pt_ratio} 0 #{@canvas.real_height * pt_ratio}] concat
  EOS
end

Since:

  • 0.0.0



53
54
55
56
57
# File 'lib/dyi/formatter/eps_formatter.rb', line 53

def footer_comment
  <<-EOS
%%EOF
  EOS
end

#header_commentObject

Since:

  • 0.0.0



30
31
32
33
34
35
36
37
38
# File 'lib/dyi/formatter/eps_formatter.rb', line 30

def header_comment
  <<-EOS
%!PS-Adobe-3.0 EPSF-3.0
%%Creator: DYI #{DYI::VERSION} (#{DYI::URL})
%%CreationDate: #{Time.now.strftime('%a %b %d %H:%M:%S %Y')}
%%BoundingBox: 0 0 #{@canvas.real_width.to_f('pt').ceil}  #{@canvas.real_height.to_f('pt').ceil}
%%EndComments
  EOS
end

#prologObject

Since:

  • 0.0.0



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dyi/formatter/eps_formatter.rb', line 40

def prolog
  <<-EOS
%%BeginProlog
/px { } bind def
/pt { #{Length.unit_ratio('pt')} mul } bind def
/cm { #{Length.unit_ratio('cm')} mul } bind def
/mm { #{Length.unit_ratio('mm')} mul } bind def
/in { #{Length.unit_ratio('in')} mul } bind def
/pc { #{Length.unit_ratio('pc')} mul } bind def
%%EndProlog
  EOS
end

#puts(io = $>) ⇒ Object

Since:

  • 0.0.0



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dyi/formatter/eps_formatter.rb', line 66

def puts(io=$>)
  StringFormat.set_default_formats(:color=>['%.3f %.3f %.3f', true], :length=>'0.### U', :coordinate=>'x y') {
    if @canvas.root_element?
      io << header_comment
      io << prolog
      io << base_transform
    end
    @canvas.write_as(self, io)
    if @canvas.root_element?
      io << footer_comment
    end
  }
end

#write_canvas(canvas, io) ⇒ Object

Since:

  • 0.0.0



80
81
82
83
84
# File 'lib/dyi/formatter/eps_formatter.rb', line 80

def write_canvas(canvas, io)
  canvas.child_elements.each do |element|
    element.write_as(self, io)
  end
end

#write_circle(shape, io) ⇒ Object

Since:

  • 0.0.0



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dyi/formatter/eps_formatter.rb', line 105

def write_circle(shape, io)
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    puts_line(io, 'newpath')
    puts_line(io, shape.center.x + shape.radius, shape.center.y, 'moveto')
    puts_line(io, shape.center.x, shape.center.y, shape.radius, 0, 360, 'arc')
    if block_given?
      yield
    else
      fill_current_path(io, shape)
      stroke_current_path(io, shape)
    end
  }
end

#write_ellipse(shape, io) ⇒ Object

Since:

  • 0.0.0



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dyi/formatter/eps_formatter.rb', line 121

def write_ellipse(shape, io)
  ratio = shape.radius_x.to_f / shape.radius_y.to_f
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    puts_line(io, 1, 1.0 / ratio, 'scale')
    puts_line(io, 'newpath')
    puts_line(io, shape.center.x + shape.radius_x, shape.center.y * ratio, 'moveto')
    puts_line(io, shape.center.x, shape.center.y * ratio, shape.radius_x, 0, 360, 'arc')
    if block_given?
      yield
    else
      fill_current_path(io, shape)
      stroke_current_path(io, shape)
    end
  }
end

#write_group(shape, io) ⇒ Object

Since:

  • 0.0.0



237
238
239
240
241
242
243
244
245
# File 'lib/dyi/formatter/eps_formatter.rb', line 237

def write_group(shape, io)
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    shape.child_elements.each do |element|
      element.write_as(self, io)
    end
  } unless shape.child_elements.empty?
end

#write_line(shape, io) ⇒ Object

Since:

  • 0.0.0



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dyi/formatter/eps_formatter.rb', line 139

def write_line(shape, io)
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    puts_line(io, 'newpath')
    write_lines(io, [shape.start_point, shape.end_point])
    if block_given?
      yield
    else
      stroke_current_path(io, shape)
    end
  }
end

#write_path(shape, io) ⇒ Object

Since:

  • 0.0.0



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
# File 'lib/dyi/formatter/eps_formatter.rb', line 184

def write_path(shape, io)
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    puts_line(io, 'newpath')
    shape.compatible_path_data.each do |cmd|
      case cmd
      when Shape::Path::MoveCommand
        puts_line(io, cmd.point.x, cmd.point.y, cmd.relative? ? 'rmoveto' : 'moveto')
      when Shape::Path::CloseCommand
        puts_line(io, 'closepath')
      when Shape::Path::LineCommand
        puts_line(io, cmd.point.x, cmd.point.y, cmd.relative? ? 'rlineto' : 'lineto')
      when Shape::Path::CurveCommand
        puts_line(io,
                  cmd.control_point1.x, cmd.control_point1.y,
                  cmd.control_point2.x, cmd.control_point2.y,
                  cmd.point.x, cmd.point.y,
                  cmd.relative? ? 'rcurveto' : 'curveto')
      else
        raise TypeError, "unknown command: #{cmd.class}"
      end
    end
    if block_given?
      yield
    else
      fill_current_path(io, shape)
      stroke_current_path(io, shape)
    end
  }
end

#write_polygon(shape, io) ⇒ Object

Since:

  • 0.0.0



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/dyi/formatter/eps_formatter.rb', line 168

def write_polygon(shape, io)
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    puts_line(io, 'newpath')
    write_lines(io, shape.points)
    puts_line(io, 'closepath')
    if block_given?
      yield
    else
      fill_current_path(io, shape)
      stroke_current_path(io, shape)
    end
  }
end

#write_polyline(shape, io) ⇒ Object

Since:

  • 0.0.0



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dyi/formatter/eps_formatter.rb', line 153

def write_polyline(shape, io)
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    puts_line(io, 'newpath')
    write_lines(io, shape.points)
    if block_given?
      yield
    else
      fill_current_path(io, shape)
      stroke_current_path(io, shape)
    end
  }
end

#write_rectangle(shape, io) ⇒ Object

Since:

  • 0.0.0



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dyi/formatter/eps_formatter.rb', line 86

def write_rectangle(shape, io)
  command_block(io) {
    transform_path(io, shape)
    clip_path(io, shape)
    puts_line(io, 'newpath')
    puts_line(io, shape.left, shape.top, 'moveto')
    puts_line(io, shape.width, 0, 'rlineto')
    puts_line(io, 0, shape.height, 'rlineto')
    puts_line(io, -shape.width, 0, 'rlineto')
    puts_line(io, 'closepath')
    if block_given?
      yield
    else
      fill_current_path(io, shape)
      stroke_current_path(io, shape)
    end
  }
end

#write_text(shape, io) ⇒ Object

Since:

  • 0.0.0



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
# File 'lib/ironruby.rb', line 298

def write_text(shape, io)
  command_block(io) {
    puts_line(io, '/GothicBBB-Medium-RKSJ-H findfont', shape.font.draw_size, 'scalefont setfont')
    text = String.new(
        System::Text::Encoding.convert(
          System::Text::Encoding.get_encoding('UTF-8'),
          System::Text::Encoding.get_encoding('Shift_JIS'),
          shape.formated_text
        )
      ).unpack('H*').first
#          text = NKF.nkf('-s -W', shape.formated_text).unpack('H*').first
    case shape.attributes[:text_anchor]
      when 'middle' then dx = "<#{text}> stringwidth pop -0.5 mul"
      when 'end' then dx = "<#{text}> stringwidth pop -1 mul"
      else dx = "0"
    end
    case shape.attributes[:alignment_baseline]
      when 'top' then y = shape.point.y - shape.font_height * 0.85
      when 'middle' then y = shape.point.y - shape.font_height * 0.35
      when 'bottom' then y = shape.point.y + shape.font_height * 0.15
      else y = shape.point.y
    end
    puts_line(io, "[ 1 0 0 -1 #{dx}", shape.point.y * 2, '] concat')
    puts_line(io, shape.point.x, y, 'moveto')
    puts_line(io, "<#{text}>", 'show')
  }
end