Class: DYI::Formatter::EmfFormatter

Inherits:
Base
  • Object
show all
Defined in:
lib/dyi/formatter/emf_formatter.rb

Overview

Since:

  • 0.0.0

Instance Method Summary collapse

Methods inherited from Base

#initialize, #puts, #string, #write_image

Constructor Details

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

Instance Method Details

#save(file_name, options = {}) ⇒ Object

Since:

  • 0.0.0



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

def save(file_name, options={})
  form = System::Windows::Forms::Form.new
  tmp_g = form.create_graphics
  dc = tmp_g.get_hdc
  stream = System::IO::FileStream.new(file_name, System::IO::FileMode.create)
#        stream = System::IO::MemoryStream.new()
  metafile = System::Drawing::Imaging::Metafile.new(
      stream,
      dc,
      System::Drawing::Rectangle.new(0, 0, @canvas.width.to_f, @canvas.height.to_f),
      System::Drawing::Imaging::MetafileFrameUnit.pixel,
      System::Drawing::Imaging::EmfType.emf_plus_dual)
  tmp_g.release_hdc
  tmp_g.dispose

  graphics = System::Drawing::Graphics.from_image(metafile);

  @canvas.write_as(self, graphics)

#        fs = System::IO::FileStream.new('test.wmf', System::IO::FileMode.create)
#        fs.write(stream.to_array, 0, stream.length)

  graphics.dispose
  metafile.dispose
end

#write_canvas(canvas, graphics) ⇒ Object

Since:

  • 0.0.0



57
58
59
60
61
# File 'lib/dyi/formatter/emf_formatter.rb', line 57

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

#write_circle(shape, graphics) ⇒ Object

Since:

  • 0.0.0



75
76
77
# File 'lib/dyi/formatter/emf_formatter.rb', line 75

def write_circle(shape, graphics)
  write_ellipse(shape, graphics)
end

#write_ellipse(shape, graphics) ⇒ Object

Since:

  • 0.0.0



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dyi/formatter/emf_formatter.rb', line 79

def write_ellipse(shape, graphics)
  set_transform(shape, graphics) {
    painting = shape.painting
    if painting.fill
      graphics.fill_ellipse(painting.cls_brush(shape), shape.left.to_f, shape.top.to_f, shape.width.to_f, shape.height.to_f)
    end
    if painting.stroke && (painting.stroke_width != DYI::Length::ZERO)
      graphics.draw_ellipse(painting.cls_pen, shape.left.to_f, shape.top.to_f, shape.width.to_f, shape.height.to_f)
    end
  }
end

#write_group(shape, graphics) ⇒ Object

Since:

  • 0.0.0



200
201
202
203
204
205
206
# File 'lib/dyi/formatter/emf_formatter.rb', line 200

def write_group(shape, graphics)
  set_transform(shape, graphics) {
    shape.child_elements.each do |element|
      element.write_as(self, graphics)
    end
  }
end

#write_line(shape, graphics) ⇒ Object

Since:

  • 0.0.0



91
92
93
94
95
96
97
98
# File 'lib/dyi/formatter/emf_formatter.rb', line 91

def write_line(shape, graphics)
  set_transform(shape, graphics) {
    painting = shape.painting
    if painting.stroke && (painting.stroke_width != DYI::Length::ZERO)
      graphics.draw_line(painting.cls_pen, shape.start_point.to_cls_point, shape.end_point.to_cls_point)
    end
  }
end

#write_path(shape, graphics) ⇒ Object

Since:

  • 0.0.0



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

def write_path(shape, graphics)
  set_transform(shape, graphics) {
    painting = shape.painting
    path = System::Drawing::Drawing2D::GraphicsPath.new(painting.cls_fill_mode)
    path.start_figure
    shape.compatible_path_data.each do |cmd|
      case cmd
      when Shape::Path::MoveCommand
        # do nothing.
      when Shape::Path::CloseCommand
        path.close_figure
      when Shape::Path::LineCommand
        path.add_line(cmd.preceding_point.x.to_f, cmd.preceding_point.y.to_f,
                      cmd.last_point.x.to_f, cmd.last_point.y.to_f)
      when Shape::Path::CurveCommand
        pre_pt = cmd.preceding_point
        ctrl_pt1 = cmd.relative? ? pre_pt + cmd.control_point1 : cmd.control_point1
        ctrl_pt2 = cmd.relative? ? pre_pt + cmd.control_point2 : cmd.control_point2
        path.add_bezier(pre_pt.x.to_f, pre_pt.y.to_f,
                        ctrl_pt1.x.to_f, ctrl_pt1.y.to_f,
                        ctrl_pt2.x.to_f, ctrl_pt2.y.to_f,
                        cmd.last_point.x.to_f, cmd.last_point.y.to_f)
      else
        raise TypeError, "unknown command: #{cmd.class}"
      end
    end
    if painting.fill
      graphics.fill_path(painting.cls_brush(shape), path)
    end
    if painting.stroke && (painting.stroke_width != DYI::Length::ZERO)
      graphics.draw_path(painting.cls_pen, path)
    end
  }
end

#write_polygon(shape, graphics) ⇒ Object

Since:

  • 0.0.0



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dyi/formatter/emf_formatter.rb', line 114

def write_polygon(shape, graphics)
  set_transform(shape, graphics) {
    points = System::Array[System::Drawing::PointF].new(shape.points.size)
    shape.points.each_with_index do |point, i|
      points[i] = point.to_cls_point
    end

    painting = shape.painting
    if painting.fill
      graphics.fill_polygon(painting.cls_brush(shape), points, painting.cls_fill_mode)
    end
    if painting.stroke && (painting.stroke_width != DYI::Length::ZERO)
      graphics.draw_polygon(painting.cls_pen, points)
    end
  }
end

#write_polyline(shape, graphics) ⇒ Object

Since:

  • 0.0.0



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dyi/formatter/emf_formatter.rb', line 100

def write_polyline(shape, graphics)
  set_transform(shape, graphics) {
    points = System::Array[System::Drawing::PointF].new(shape.points.size)
    shape.points.each_with_index do |point, i|
      points[i] = point.to_cls_point
    end

    painting = shape.painting
    if painting.stroke && (painting.stroke_width != DYI::Length::ZERO)
      graphics.draw_lines(painting.cls_pen, points)
    end
  }
end

#write_rectangle(shape, graphics) ⇒ Object

Since:

  • 0.0.0



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dyi/formatter/emf_formatter.rb', line 63

def write_rectangle(shape, graphics)
  set_transform(shape, graphics) {
    painting = shape.painting
    if painting.fill
      graphics.fill_rectangle(painting.cls_brush(shape), shape.left.to_f, shape.top.to_f, shape.width.to_f, shape.height.to_f)
    end
    if painting.stroke && (painting.stroke_width != DYI::Length::ZERO)
      graphics.draw_rectangle(painting.cls_pen, shape.left.to_f, shape.top.to_f, shape.width.to_f, shape.height.to_f)
    end
  }
end

#write_text(shape, graphics) ⇒ Object

Since:

  • 0.0.0



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

def write_text(shape, graphics)
  set_transform(shape, graphics) {
#          font = Font.to_cls_font(shape.font)
#          brush = System::Drawing::SolidBrush.new(Color.black.to_cls_color)
    graphics.draw_string(shape.formated_text, shape.font.to_cls_font, shape.painting.cls_brush(shape), shape.point.to_cls_point, shape.string_format)
=begin
  attrs = {:x => shape.point.x, :y => shape.point.y}
  attrs.merge!(common_attributes(shape))
  attrs[:"text-decoration"] = shape.attributes[:text_decoration] if shape.attributes[:text_decoration]
#        attrs[:"alignment-baseline"] = shape.attributes[:alignment_baseline] if shape.attributes[:alignment_baseline]
  case shape.attributes[:alignment_baseline]
    when 'top' then attrs[:y] += shape.font_height * 0.85
    when 'middle' then attrs[:y] += shape.font_height * 0.35
    when 'bottom' then attrs[:y] -= shape.font_height * 0.15
  end
  attrs[:"text-anchor"] = shape.attributes[:text_anchor] if shape.attributes[:text_anchor]
  attrs[:"writing-mode"] = shape.attributes[:writing_mode] if shape.attributes[:writing_mode]
  attrs[:textLength] = shape.attributes[:textLength] if shape.attributes[:textLength]
  attrs[:lengthAdjust] = shape.attributes[:lengthAdjust] if shape.attributes[:lengthAdjust]
  text = shape.formated_text
  if text =~ /(\r\n|\n|\r)/
    create_node(io, 'text', attrs) {
      create_leaf_node(io, 'tspan', $`.strip, :x => shape.point.x)
      $'.each_line do |line|
        create_leaf_node(io, 'tspan', line.strip, :x => shape.point.x, :dy => shape.dy)
      end
    }
  else
    create_leaf_node(io, 'text', text, attrs)
  end
=end

  }
end