Class: DYI::Formatter::XamlFormatter

Inherits:
XmlFormatter show all
Defined in:
lib/dyi/formatter/xaml_formatter.rb

Overview

Since:

  • 0.0.0

Constant Summary collapse

FONT_STYLE =

Since:

  • 0.0.0

{'normal'=>'Normal','italic'=>'Italic'}
FONT_WEIGHT =

Since:

  • 0.0.0

{'normal'=>'Normal','bold'=>'Bold','100'=>'Thin','200'=>'ExtraLight','300'=>'Light','400'=>'Normal','500'=>'Medium','600'=>'SemiBold','700'=>'Bold','800'=>'ExtraBold','900'=>'Black'}
FONT_STRETCH =

Since:

  • 0.0.0

{'normal'=>'Normal','ultra-condensed'=>'UltraCondensed','extra-condensed'=>'ExtraCondensed','condensed'=>'Condensed','semi-condensed'=>'SemiCondensed','semi-expanded'=>'SemiExpanded','expanded'=>'Expanded','extra-expanded'=>'ExtraExpanded','ultra-expanded'=>'UltraExpanded'}
STROKE_LINE_CAP =

Since:

  • 0.0.0

{'butt'=>'Flat','round'=>'Round','square'=>'Square'}
STROKE_LINE_JOIN =

Since:

  • 0.0.0

{'miter'=>'Miter','round'=>'Round','bevel'=>'Bevel'}
TEXT_ANCHOR =

Since:

  • 0.0.0

{'start'=>'Left','middle'=>'Center','end'=>'Right'}
SPREAD_METHOD =

Since:

  • 0.0.0

{'pad'=>'Pad','reflect'=>'Reflect','repeat'=>'Repeat'}

Constants included from XmlChar

DYI::Formatter::XmlChar::ATTR_PREDEFINED, DYI::Formatter::XmlChar::CP1252, DYI::Formatter::XmlChar::PREDEFINED, DYI::Formatter::XmlChar::VALID

Instance Attribute Summary

Attributes inherited from XmlFormatter

#namespace

Instance Method Summary collapse

Methods inherited from XmlFormatter

#declaration, #generator_comment, #initialize, #inline_mode=, #inline_mode?, #stylesheet_instruction, #xml_instruction

Methods inherited from Base

#initialize, #save, #string, #write_image

Constructor Details

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

Instance Method Details

#puts(io = $>) ⇒ Object

Since:

  • 0.0.0



37
38
39
40
41
# File 'lib/dyi/formatter/xaml_formatter.rb', line 37

def puts(io=$>)
  StringFormat.set_default_formats(:coordinate => 'x,y') {
    super
  }
end

#write_canvas(canvas, io) ⇒ Object

Since:

  • 0.0.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dyi/formatter/xaml_formatter.rb', line 43

def write_canvas(canvas, io)
  create_node(io, 'UserControl',
      :xmlns => "http://schemas.microsoft.com/winfx/2006/xaml/presentation",
      :"xmlns:x" => "http://schemas.microsoft.com/winfx/2006/xaml",
#            :"xmlns:navigation" => "clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation",
#            :"xmlns:d" => "http://schemas.microsoft.com/expression/blend/2008",
#            :"xmlns:mc" => "http://schemas.openxmlformats.org/markup-compatibility/2006",
      :Width => canvas.width,
      :Height => canvas.height) {
    create_node(io, 'Canvas'){
      canvas.child_elements.each do |element|
        element.write_as(self, io)
      end
    }
  }
end

#write_circle(shape, io) ⇒ Object

Since:

  • 0.0.0



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dyi/formatter/xaml_formatter.rb', line 74

def write_circle(shape, io)
  attrs, attr_creator = common_attributes(shape, :shape)
  attrs.merge!(:"Canvas.Left"=>shape.center.x - shape.radius, :"Canvas.Top"=>shape.center.y - shape.radius, :Width=>shape.radius * 2, :Height=>shape.radius * 2)
  if attr_creator
    create_node(io, 'Ellipse', attrs) {
      attr_creator.call(io, 'Ellipse')
    }
  else
    create_leaf_node(io, 'Ellipse', attrs)
  end
end

#write_clip(shape, io) ⇒ Object

Since:

  • 0.0.0



221
222
223
# File 'lib/dyi/formatter/xaml_formatter.rb', line 221

def write_clip(shape, io)
  # TODO
end

#write_ellipse(shape, io) ⇒ Object

Since:

  • 0.0.0



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dyi/formatter/xaml_formatter.rb', line 86

def write_ellipse(shape, io)
  attrs, attr_creator = common_attributes(shape, :shape)
  attrs.merge!(:"Canvas.Left"=>shape.center.x - shape.radius_x, :"Canvas.Top"=>shape.center.y - shape.radius_y, :Width=>shape.radius_x * 2, :Height=>shape.radius_y * 2)
  if attr_creator
    create_node(io, 'Ellipse', attrs) {
      attr_creator.call(io, 'Ellipse')
    }
  else
    create_leaf_node(io, 'Ellipse', attrs)
  end
end

#write_gradient_stop(shape, io) ⇒ Object

Since:

  • 0.0.0



215
216
217
218
219
# File 'lib/dyi/formatter/xaml_formatter.rb', line 215

def write_gradient_stop(shape, io)
  attrs = {:Offset=>shape.offset}
  attrs[:Color] = shape.color.to_s16(shape.opacity) if shape.color
  create_leaf_node(io, 'GradientStop', attrs)
end

#write_group(shape, io) ⇒ Object

Since:

  • 0.0.0



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/dyi/formatter/xaml_formatter.rb', line 188

def write_group(shape, io)
  attrs, attr_creator = common_attributes(shape)
  create_node(io, 'Canvas', attrs) {
    attr_creator.call(io, 'Canvas') if attr_creator
    create_node(io, 'Canvas.RenderTransform') {
      create_transform_node(shape, io)
    } unless shape.transform.empty?
    shape.child_elements.each do |element|
      element.write_as(self, io)
    end
  }
end

#write_line(shape, io) ⇒ Object

Since:

  • 0.0.0



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dyi/formatter/xaml_formatter.rb', line 98

def write_line(shape, io)
  attrs, attr_creator = common_attributes(shape, :line)
  attrs.merge!(:X1 => shape.start_point.x, :Y1 => shape.start_point.y, :X2 => shape.end_point.x, :Y2 => shape.end_point.y)
  if attr_creator
    create_node(io, 'Line', attrs) {
      attr_creator.call(io, 'Line')
    }
  else
    create_leaf_node(io, 'Line', attrs)
  end
end

#write_linear_gradient(shape, io) ⇒ Object

Since:

  • 0.0.0



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/dyi/formatter/xaml_formatter.rb', line 201

def write_linear_gradient(shape, io)
  attr = {
    :StartPoint => "#{shape.start_point[0]},#{shape.start_point[1]}",
    :EndPoint => "#{shape.stop_point[0]},#{shape.stop_point[1]}"}
  if spread_method = SPREAD_METHOD[shape.spread_method]
    attr[:SpreadMethod] = spread_method
  end
  create_node(io, 'LinearGradientBrush', attr) {
    shape.child_elements.each do |element|
      element.write_as(self, io)
    end
  }
end

#write_path(shape, io) ⇒ Object

Since:

  • 0.0.0



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dyi/formatter/xaml_formatter.rb', line 134

def write_path(shape, io)
  attrs, attr_creator = common_attributes(shape, :line)
  attrs.merge!(:Data => shape.concise_path_data)
  if attr_creator
    create_node(io, 'Path', attrs) {
      attr_creator.call(io, 'Path')
    }
  else
    create_leaf_node(io, 'Path', attrs)
  end
end

#write_polygon(shape, io) ⇒ Object

Since:

  • 0.0.0



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dyi/formatter/xaml_formatter.rb', line 122

def write_polygon(shape, io)
  attrs, attr_creator = common_attributes(shape, :shape)
  attrs.merge!(:Points => shape.points.join(' '))
  if attr_creator
    create_node(io, 'Polygon', attrs) {
      attr_creator.call(io, 'Polygon')
    }
  else
    create_leaf_node(io, 'Polygon', attrs)
  end
end

#write_polyline(shape, io) ⇒ Object

Since:

  • 0.0.0



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dyi/formatter/xaml_formatter.rb', line 110

def write_polyline(shape, io)
  attrs, attr_creator = common_attributes(shape, :line)
  attrs.merge!(:Points => shape.points.join(' '))
  if attr_creator
    create_node(io, 'Polyline', attrs) {
      attr_creator.call(io, 'Polyline')
    }
  else
    create_leaf_node(io, 'Polyline', attrs)
  end
end

#write_rectangle(shape, io) ⇒ Object

Since:

  • 0.0.0



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dyi/formatter/xaml_formatter.rb', line 60

def write_rectangle(shape, io)
  attrs, attr_creator = common_attributes(shape, :shape)
  attrs.merge!(:"Canvas.Left"=>shape.left, :"Canvas.Top"=>shape.top, :Width=>shape.width, :Height=>shape.height)
  attrs[:RadiusX] = shape.attributes[:rx] if shape.attributes[:rx]
  attrs[:RadiusY] = shape.attributes[:ry] if shape.attributes[:ry]
  if attr_creator
    create_node(io, 'Rectangle', attrs) {
      attr_creator.call(io, 'Rectangle')
    }
  else
    create_leaf_node(io, 'Rectangle', attrs)
  end
end

#write_text(shape, io) ⇒ Object

Since:

  • 0.0.0



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

def write_text(shape, io)
  attrs, attr_creator = common_attributes(shape, :text)
  attrs.merge!(:"Canvas.Left" => shape.point.x, :"Canvas.Top" => shape.point.y)
#        attrs[:"text-decoration"] = shape.attributes[:text_decoration] if shape.attributes[:text_decoration]
  case shape.attributes[:alignment_baseline]
    when nil then attrs[:"Canvas.Top"] -= shape.font_height * 0.85
    when 'middle' then attrs[:"Canvas.Top"] -= shape.font_height * 0.5
    when 'bottom' then attrs[:"Canvas.Top"] -= shape.font_height
  end
  case text_anchor = TEXT_ANCHOR[shape.attributes[:text_anchor]]
  when 'Left'
    attrs[:TextAlignment] = text_anchor
  when 'Center'
    attrs[:Width] = @canvas.width
    attrs[:"Canvas.Left"] -= @canvas.width.quo(2)
    attrs[:TextAlignment] = text_anchor
  when 'Right'
    attrs[:Width] = @canvas.width
    attrs[:"Canvas.Left"] -= @canvas.width
    attrs[:TextAlignment] = text_anchor
  end
#        attrs[:"writing-mode"] = shape.attributes[:writing_mode] if shape.attributes[:writing_mode]
  text = shape.formated_text
  if text =~ /(\r\n|\n|\r)/
    attrs[:Text] = $`.strip
    create_node(io, 'TextBlock', attrs) {
      attr_creator.call(io, 'TextBlock') if attr_creator
      create_leaf_node(io, 'LineBreak')
      $'.each_line do |line|
        create_leaf_node(io, 'Run', line.strip)
      end
    }
  else
    attrs[:Text] = text
    if attr_creator
      create_node(io, 'TextBlock', attrs, &attr_creator)
    else
      create_leaf_node(io, 'TextBlock', attrs)
    end
  end
end