class DYI::Drawing::PenBase
A factory base class for createing a shape in the image.
PenBase
object holds a {Painting} object and a {Font} object. Using these object, PenBase
object creates instances of concrete subclass of {Shape::Base}; a created instance has a painting attribute and a font attribute that PenBase
object holds.
This class has same attributes as {Painting}, these attributs access {Painting} object that PenBase
object holds. @abstract @see Painting
@see Brush
@see Pen
@since 0.0.0
Constants
- DROP_SHADOW_OPTIONS
@private
Attributes
@private
Public Class Methods
@option options [Font, Hash] :font the value of attribute {#font font} @option (see Painting#initialize)
# File lib/dyi/drawing/pen.rb, line 51 def initialize(options={}) @attributes = {} @painting = Painting.new options.each do |key, value| if key.to_sym == :font self.font = value elsif Painting::IMPLEMENT_ATTRIBUTES.include?(key) @painting.__send__("#{key}=", value) else @attributes[key] = value end end end
Public Instance Methods
Draws a circle to specify the center point and the radius. @param [Element] canvas the element which the circle is drawn on @param [Coordinate] center_point the center point of the circle @param [Length] radius the radius of the circle @option (see draw_line
) @return [Shape::Circle] a drawn circle
# File lib/dyi/drawing/pen.rb, line 339 def draw_circle(canvas, center_point, radius, options={}) Shape::Circle.create_on_center_radius(center_point, radius, merge_option(options)).draw_on(canvas) end
Draws a free-form line or curve, and closes path finally. See methods of {Shape::Path} for commands to draw the path. @param (see draw_path
) @option (see draw_path
) @yield (see draw_path
) @yieldparam (see draw_path
) @return (see draw_path
) @see (see draw_path
)
# File lib/dyi/drawing/pen.rb, line 327 def draw_closed_path(canvas, point, options={}, &block) path = draw_path(canvas, point, options, &block) path.close_path unless path.close? path end
Draws an ellipse to specify the center point and the radius. @param [Element] canvas the element which the ellipse is drawn on @param [Coordinate] center_point the center point of the ellipse @param [Length] radius_x the x-axis radius of the ellipse @param [Length] radius_y the y-axis radius of the ellipse @option (see draw_line
) @return [Shape::Ellipse] a drawn ellipse
# File lib/dyi/drawing/pen.rb, line 350 def draw_ellipse(canvas, center_point, radius_x, radius_y, options={}) Shape::Ellipse.create_on_center_radius(center_point, radius_x, radius_y, merge_option(options)).draw_on(canvas) end
Draws an image. @param [Element] canvas the element which the image is drawn on @param [Coordinate] left_top the left-top corner point of the image @param [Length] width the width of the image @param [Length] height the height of the image @param [String] file_path the path of the image file @option (see draw_line
) @option options [String] :content_type the content-type of the image @return [Shape::Image] a drawn image @since 1.0.0
# File lib/dyi/drawing/pen.rb, line 364 def draw_image(canvas, left_top_point, width, height, file_path, options={}) Shape::Image.new(left_top_point, width, height, file_path, merge_option(options)).draw_on(canvas) end
Draws a line to specify the start and end points. @param [Element] canvas the element which the line is drawn on @param [Coordinate] start_point the start point of the line @param [Coordinate] end_point the start point of the line @option options [String] :id the ID of the drawn shape @option options [String] :anchor_href the location of a reference of a
source anchor for the link of the drawn shape
@option options [String] :anchor_target the relevant presentation
context when the link is activated
@option options [String] :css_class the CSS class attribute of the drawn
shape
@return [Shape::Line] a drawn line
# File lib/dyi/drawing/pen.rb, line 161 def draw_line(canvas, start_point, end_point, options={}) Shape::Line.create_on_start_end(start_point, end_point, merge_option(options)).draw_on(canvas) end
Draws a line to specify the start points and the direction. @param [Element] canvas the element which the line is drawn on @param [Coordinate] start_point the start point of the line @param [Length] direction_x the x-direction of the end point @param [Length] direction_y the y-direction of the end point @option (see draw_line
) @return (see draw_line
)
# File lib/dyi/drawing/pen.rb, line 174 def draw_line_on_direction(canvas, start_point, direction_x, direction_y, options={}) Shape::Line.create_on_direction(start_point, direction_x, direction_y, merge_option(options)).draw_on(canvas) end
Draws free-form lines or curves. See methods of {Shape::Path} for commands to draw the path. @param [Element] canvas the element which the path is drawn on @param [Coordinate] point the start point of the path @option (see draw_line
) @yield [path] a block which the path is drawn in @yieldparam [Shape::Path] path the created path object @return [Shape::Path] a drawn path @see Shape::Path
# File lib/dyi/drawing/pen.rb, line 313 def draw_path(canvas, point, options={}, &block) path = Shape::Path.new(point, merge_option(options)).draw_on(canvas) yield path path end
Draws a polygon. @return [Shape::Polygon] a drawn polygon @overload draw_polygon
(canvas, point, options = {})
Draws a polygon. Second and subsequent vertices are specified in the block. @param [Element] canvas the element which the polygon is drawn on @param [Coordinate] point the first vertix of the polygon @option options [String] :id the ID of the drawn shape @option options [String] :anchor_href the location of a reference of a source anchor for the link of the drawn shape @option options [String] :anchor_target the relevant presentation context when the link is activated @option options [String] :css_class the CSS class attribute of the drawn shape @yield [polygon] a block which the polygon is drawn in @yieldparam [Shape::Polygon] polygon the created polygon object
@overload draw_polygon
(canvas, points, options = {})
Draws a polygon to specify in the vertices. @param [Element] canvas the element which the polygon is drawn on @param [Array<Coordinate>] points the array of the vertices @option options [String] :id the ID of the drawn shape @option options [String] :anchor_href the location of a reference of a source anchor for the link of the drawn shape @option options [String] :anchor_target the relevant presentation context when the link is activated @option options [String] :css_class the CSS class attribute of the drawn shape @since 1.1.0
@example
canvas = DYI::Canvas.new(100,100) brush = DYI::Drawing::Brush.red_brush brush.draw_polygon(canvas, [20, 20]) {|polygon| polygon.line_to([20, 80]) polygon.line_to([80, 80]) } # the following is the same processing # brush.draw_polygon(canvas, [[20, 20], [20, 80], [80, 80]])
# File lib/dyi/drawing/pen.rb, line 263 def draw_polygon(canvas, points, options={}) if block_given? polygon = Shape::Polygon.new(points, merge_option(options)) yield polygon else polygon = Shape::Polygon.new(points.first, merge_option(options)) polygon.line_to(*points[1..-1]) end polygon.draw_on(canvas) end
Draws a polyline. @return [Shape::Polyline] a drawn polyline @overload draw_polyline
(canvas, point, options = {})
Draws a polyline. Second and subsequent making-up points are specified in the block. @param [Element] canvas the element which the polyline is drawn on @param [Coordinate] point the start point of the polyline @option options [String] :id the ID of the drawn shape @option options [String] :anchor_href the location of a reference of a source anchor for the link of the drawn shape @option options [String] :anchor_target the relevant presentation context when the link is activated @option options [String] :css_class the CSS class attribute of the drawn shape @yield [polyline] a block which the polyline is drawn in @yieldparam [Shape::Polyline] polyline the created polyline object
@overload draw_polyline
(canvas, points, options = {})
Draws a polyline to specify in the making-up points. @param [Element] canvas the element which the polyline is drawn on @param [Array<Coordinate>] points the array of the making-up points @option options [String] :id the ID of the drawn shape @option options [String] :anchor_href the location of a reference of a source anchor for the link of the drawn shape @option options [String] :anchor_target the relevant presentation context when the link is activated @option options [String] :css_class the CSS class attribute of the drawn shape @since 1.1.0
@example
canvas = DYI::Canvas.new(100,100) pen = DYI::Drawing::Pen.black_pen pen.draw_polyline(canvas, [20, 20]) {|polyline| polyline.line_to([20, 80]) polyline.line_to([80, 80]) } # the following is the same processing # pen.draw_polyline(canvas, [[20, 20], [20, 80], [80, 80]])
# File lib/dyi/drawing/pen.rb, line 215 def draw_polyline(canvas, points, options={}) if block_given? polyline = Shape::Polyline.new(points, merge_option(options)) yield polyline else polyline = Shape::Polyline.new(points.first, merge_option(options)) polyline.line_to(*points[1..-1]) end polyline.draw_on(canvas) end
Draws a rectangle to specify the left-top points, the width and the height. @param [Element] canvas the element which the rectangle is drawn on @param [Coordinate] left_top the left-top corner point of the rectangle @param [Length] width the width of the rectangle @param [Length] height the height of the rectangle @option (see draw_line
) @option options [Length] :rx for rounded rectangles, the x-axis radius
of the ellipse used to round off the corners of the rectangle
@option options [Length] :ry for rounded rectangles, the y-axis radius
of the ellipse used to round off the corners of the rectangle
@return [Shape::Rectangle] a drawn rectangle
# File lib/dyi/drawing/pen.rb, line 286 def draw_rectangle(canvas, left_top_point, width, height, options={}) Shape::Rectangle.create_on_width_height(left_top_point, width, height, merge_option(options)).draw_on(canvas) end
Draws a rectangle to specify the top, right, botton and left cooridinate. @param [Element] canvas the element which the rectangle is drawn on @param [Length] top the y-coordinate of the top of the rectangle @param [Length] right the x-coordinate of the right of the rectangle @param [Length] bottom the y-coordinate of the bottom of the rectangle @param [Length] left the x-coordinate of the left of the rectangle @option (see draw_rectangle
) @return (see draw_rectangle
)
# File lib/dyi/drawing/pen.rb, line 300 def draw_rectangle_on_corner(canvas, top, right, bottom, left, options={}) Shape::Rectangle.create_on_corner(top, right, bottom, left, merge_option(options)).draw_on(canvas) end
Draws a circular sector. @param [Element] canvas the element which the sector is drawn on @param [Coordinate] center_point the center point of the sector @param [Length] radius_x the x-axis radius of the sector @param [Length] radius_y the y-axis radius of the sector @param [Number] start_angle the starting-radius angle of the sector from
x-direction in degrees
@param [Number] center_angle the center angle of the sector from the
starting-radius in degrees
@option (see draw_line
) @option options [#to_f] :inner_radius if you want to make a concentric
hole with sector, the ratio of the hole's radius to the sector radius
@return [Shape::Path] a drawn sector @raise [ArgumentError] :inner_radius option is out of range
# File lib/dyi/drawing/pen.rb, line 395 def draw_sector(canvas, center_point, radius_x, radius_y, start_angle, center_angle, options={}) start_angle = (center_angle > 0 ? start_angle : (start_angle + center_angle)) % 360 center_angle = center_angle.abs options = merge_option(options) inner_radius = options.delete(:inner_radius).to_f center_point = Coordinate.new(center_point) radius_x = Length.new(radius_x).abs radius_y = Length.new(radius_y).abs large_arc = (center_angle.abs > 180) if inner_radius >= 1 || 0 > inner_radius raise ArgumentError, "inner_radius option is out of range: #{inner_radius}" end if 360 <= center_angle if inner_radius == 0.0 draw_ellipse(canvas, center_point, radius_x, radius_y, options) else draw_toroid(canvas, center_point, radius_x, radius_y, inner_radius, options) end else arc_start_pt = Coordinate.new( radius_x * DYI::Util.cos(start_angle), radius_y * DYI::Util.sin(start_angle)) + center_point arc_end_pt = Coordinate.new( radius_x * DYI::Util.cos(start_angle + center_angle), radius_y * DYI::Util.sin(start_angle + center_angle)) + center_point draw_sector_internal(canvas, center_point, radius_x, radius_y, inner_radius, arc_start_pt, arc_end_pt, start_angle, center_angle, options) end end
Draws a text. @param [Element] canvas the element which the text is drawn on @param [Coordinate] point the point of the base-line of the
text. See _:text_anchor_ option
@param [String] text the drawn text string @option (see draw_line
) @option options [String] :text_anchor the way of aligning a string of
text relative to _point_ argument. specifies one of the following vlaues: <tt>"start"</tt>, <tt>"middle"</tt>, <tt>"end"</tt>
@option options [Length] :text_length the length of the displayed text @option options [String] :length_adjust the way of adjustments to make the
rendered length of the text match _text_length_ option. specifies one of the following vlaues: <tt>"spacing"</tt>, <tt>"spacingAndGlyphs"</tt>
@option options [String] :text_decoration the decorations that are
added to the text. specifies a string in a comma-separated combination of the following vlaues: <tt>"underline"</tt>, <tt>"overline"</tt>, <tt>"line-through"</tt>, <tt>"blink"</tt>
@option options [String] :writing_mode the inline-progression-direction
for a text. specifies one of the following vlaues: <tt>"lr-tb"</tt>, <tt>"rl-tb"</tt>, <tt>"tb-rl"</tt>, <tt>"lr"</tt>, <tt>"rl"</tt>, <tt>"tb"</tt>
@option options [Boolean] :show_border whether the border is shown @option options [Length] :border_rx the x-axis radius of the ellipse
used to round off the corners of the rectangle when the rounded border is shown
@option options [Length] :vertical_padding the interval of vertical
border line and text area
@option options [Length] :horizontal_padding the interval of horizontal
border line and text area
@option options [Length] :border_ry the y-axis radius of the ellipse
used to round off the corners of the rectangle when the rounded border is shown
@option options [Color, write_as] :background_color the interior
painting of the border-line when the border is shown
@option options [Color, write_as] :border_color the painting along the
border-line when the border is shown
@option options [Length] :border_width the width of border-line when the
border is shown
@return [Shape::Text] a drawn text
# File lib/dyi/drawing/pen.rb, line 504 def draw_text(canvas, point, text, options={}) Shape::Text.new(point, text, merge_option(options)).draw_on(canvas) end
Draws a toroid. @param [Element] canvas the element which the toroid is drawn on @param [Coordinate] center_point the center point of the toroid @param [Length] radius_x the x-axis radius of the toroid @param [Length] radius_y the y-axis radius of the toroid @param [#to_f] inner_radius the ratio of inner radius to the sector
radius
@option (see draw_line
) @return [Shape::Path] a drawn toroid @raise [ArgumentError] inner_radius is out of range @since 1.1.0
# File lib/dyi/drawing/pen.rb, line 440 def draw_toroid(canvas, center_point, radius_x, radius_y, inner_radius, options={}) if inner_radius >= 1 || 0 > inner_radius raise ArgumentError, "inner_radius is out of range: #{inner_radius}" end radius_x, radius_y = Length.new(radius_x).abs, Length.new(radius_y).abs center_point = Coordinate.new(center_point) arc_start_pt = center_point + [radius_x, 0] arc_opposite_pt = center_point - [radius_x, 0] inner_arc_start_pt = center_point + [radius_x * inner_radius, 0] inner_arc_opposite_pt = center_point - [radius_x * inner_radius, 0] draw_closed_path(canvas, arc_start_pt, options) {|path| path.arc_to(arc_opposite_pt, radius_x, radius_y, 0, true) path.arc_to(arc_start_pt, radius_x, radius_y, 0, true) path.close_path path.move_to(inner_arc_start_pt) path.arc_to(inner_arc_opposite_pt, radius_x * inner_radius, radius_y * inner_radius, 0, true, false) path.arc_to(inner_arc_start_pt, radius_x * inner_radius, radius_y * inner_radius, 0, true, false) } end
@private @todo
# File lib/dyi/drawing/pen.rb, line 143 def drop_shadow=(options) DROP_SHADOW_OPTIONS.each do |key| @drop_shadow[key] = options[key.to_sym] if options[key.to_sym] end end
Adds a reference to an image. @param [Element] canvas the element which the reference is added on @param [Coordinate] left_top the left-top corner point of the image @param [Length] width the width of the image @param [Length] height the height of the image @param [String] image_uri the reference URI to the image file @option (see draw_line
) @return [Shape::ImageReference] a drawn reference to the image @since 1.0.0
# File lib/dyi/drawing/pen.rb, line 377 def import_image(canvas, left_top_point, width, height, image_uri, options={}) Shape::ImageReference.new(left_top_point, width, height, image_uri, merge_option(options)).draw_on(canvas) end
Private Instance Methods
@since 1.1.0
# File lib/dyi/drawing/pen.rb, line 515 def draw_sector_internal(canvas, center_point, radius_x, radius_y, inner_radius, arc_start_pt, arc_end_pt, start_angle, center_angle, merged_options) draw_closed_path(canvas, arc_start_pt, merged_options) {|path| path.arc_to(arc_end_pt, radius_x, radius_y, 0, (180 < center_angle)) if inner_radius == 0 path.line_to(center_point) if center_angle != 180 else inner_arc_start_pt = center_point * (1 - inner_radius) + arc_end_pt * inner_radius inner_arc_end_pt = center_point * (1 - inner_radius) + arc_start_pt * inner_radius path.line_to(inner_arc_start_pt) path.arc_to(inner_arc_end_pt, radius_x * inner_radius, radius_y * inner_radius, 0, (180 < center_angle), false) end } end
# File lib/dyi/drawing/pen.rb, line 510 def merge_option(options) {:painting=>@painting, :font=>@font}.merge(options) end