lib/rabbit/element/image.rb in rabbit-3.0.1 vs lib/rabbit/element/image.rb in rabbit-3.0.2

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (C) 2004-2020 Sutou Kouhei <kou@cozmixng.org> +# Copyright (C) 2004-2021 Sutou Kouhei <kou@cozmixng.org> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. @@ -27,25 +27,31 @@ alias element_draw draw include ImageManipulable alias image_draw draw alias draw element_draw - def initialize(filename, props) - super(filename, props) + def initialize(filename, props, canvas: nil) + super(filename, props, canvas: canvas) setup_draw_parameters resize(properties.get_size("width", @filename), properties.get_size("height", @filename)) end def draw_element(canvas, x, y, w, h, simulation) - draw_image(canvas, x, y, w, h, simulation) + result = draw_image(canvas, x, y, w, h, simulation) + draw_properties(canvas, x, y, w, h) unless simulation + result end def caption self["caption"] end + def caption_font_size + properties.get_float("caption-font-size") + end + def text caption.to_s end def to_rd @@ -111,14 +117,16 @@ def compile(canvas, x, y, w, h) super adjust_size(canvas, @x, @y, @w, @h) end + alias_method :width_without_padding, :width def width super + @padding_left + @padding_right end + alias_method :height_without_padding, :height def height super + @padding_top + @padding_bottom end def as_large_as_possible? @@ -188,9 +196,67 @@ else iw = relative_width&.resolve(base_w) ih = relative_height&.resolve(base_h) end resize(iw, ih) + end + + def draw_properties(canvas, base_x, base_y, base_w, base_h) + properties.draws.each do |type, *args| + case type + when "line" + if args.last.is_a?(Hash) + params = args.pop + else + params = nil + end + points = args.each_slice(2).collect do |x, y| + x = (x * width_without_padding) + base_x + y = (y * height_without_padding) + base_y + [x, y] + end + params = normalize_params(params) + color = params.delete(:color) || "black" + canvas.draw_lines(points, color, params) + when "rectangle" + filled, x, y, w, h, params = args + x = (x * width_without_padding) + base_x + y = (y * height_without_padding) + base_y + w = (w * width_without_padding) + h = (h * height_without_padding) + params = normalize_params(params) + color = params.delete(:color) || "black" + canvas.draw_rectangle(filled, x, y, w, h, color, params) + when "text" + text, x, y, params = args + params = normalize_params(params) + layout = canvas.make_layout(markup_text(text, params)) + x = (x * width_without_padding) + base_x + y = (y * height_without_padding) + base_y + color = params.delete(:color) || "black" + canvas.draw_layout(layout, x, y, color, params) + end + end + end + + def normalize_params(params) + return {} if params.nil? + normalized_params = {} + params.each do |key, value| + normalized_params[key.to_sym] = value + end + normalized_params + end + + def markup_text(text, props) + props.each do |name, value| + formatter_name = Utils.to_class_name(name.to_s) + next unless Format.const_defined?(formatter_name) + formatter = Format.const_get(formatter_name).new(value) + next unless formatter.text_formatter? + text = formatter.format(text) + end + text end end end end