lib/thinreports/generator/pdf/document/graphics/text.rb in thinreports-0.7.7 vs lib/thinreports/generator/pdf/document/graphics/text.rb in thinreports-0.8.0

- old
+ new

@@ -1,10 +1,10 @@ # coding: utf-8 -module ThinReports +module Thinreports module Generator - + module PDF::Graphics # @param [String] content # @param [Numeric, String] x # @param [Numeric, String] y # @param [Numeric, String] w @@ -22,103 +22,102 @@ # @option attrs [Boolean] :single (false) # @option attrs [:trancate, :shrink_to_fit, :expand] :overflow (:trancate) # @option attrs [:none, :break_word] :word_wrap (:none) def text_box(content, x, y, w, h, attrs = {}) w, h = s2f(w, h) - box_attrs = text_box_attrs(x, y, w, h, :single => attrs.delete(:single), - :overflow => attrs[:overflow]) + box_attrs = text_box_attrs(x, y, w, h, single: attrs.delete(:single), + overflow: attrs[:overflow]) # Do not break by word unless :word_wrap is :break_word content = text_without_line_wrap(content) if attrs[:word_wrap] == :none - + with_text_styles(attrs) do |built_attrs, font_styles| - pdf.formatted_text_box([{:text => content, - :styles => font_styles}], - built_attrs.merge(box_attrs)) + pdf.formatted_text_box([{ text: content, styles: font_styles }], + built_attrs.merge(box_attrs)) end rescue Prawn::Errors::CannotFit => e # Nothing to do. - # + # # When the area is too small compared # with the content and the style of the text. # (See prawn/core/text/formatted/line_wrap.rb#L185) end - + # @see #text_box def text(content, x, y, w, h, attrs = {}) # Set the :overflow property to :shirink_to_fit. - text_box(content, x, y, w, h, {:overflow => :shrink_to_fit}.merge(attrs)) + text_box(content, x, y, w, h, { overflow: :shirink_to_fit }.merge(attrs)) end - + private - + # @param x (see #text_box) # @param y (see #text_box) # @param w (see #text_box) # @param h (see #text_box) # @param [Hash] states # @option states [Boolean] :single # @option states [Symbold] :overflow # @return [Hash] def text_box_attrs(x, y, w, h, states = {}) - attrs = {:at => pos(x, y), - :width => s2f(w)} + attrs = {at: pos(x, y), + width: s2f(w)} if states[:single] - states[:overflow] != :expand ? attrs.merge(:single_line => true) : attrs + states[:overflow] != :expand ? attrs.merge(single_line: true) : attrs else - attrs.merge(:height => s2f(h)) + attrs.merge(height: s2f(h)) end end - + # @param attrs (see #text) # @yield [built_attrs, font_styles] # @yieldparam [Hash] built_attrs The finalized attributes. # @yieldparam [Array] font_styles The finalized styles. def with_text_styles(attrs, &block) # When no color is given, do not draw. return unless attrs.key?(:color) && attrs[:color] != 'none' - + save_graphics_state - - fontinfo = {:name => attrs.delete(:font).to_s, - :color => parse_color(attrs.delete(:color)), - :size => s2f(attrs.delete(:size))} - + + fontinfo = {name: attrs.delete(:font).to_s, + color: parse_color(attrs.delete(:color)), + size: s2f(attrs.delete(:size))} + # Add the specified value to :leading option. if line_height = attrs.delete(:line_height) attrs[:leading] = text_line_leading(s2f(line_height), - :name => fontinfo[:name], - :size => fontinfo[:size]) + name: fontinfo[:name], + size: fontinfo[:size]) end - + # Set the :character_spacing option. if space = attrs.delete(:letter_spacing) attrs[:character_spacing] = s2f(space) end - + # Or... with_font_styles(attrs, fontinfo, &block) with_font_styles(attrs, fontinfo) do |modified_attrs, styles| block.call(modified_attrs, styles) end - + restore_graphics_state end - + # @param [Numeric] line_height # @param [Hash] font # @option font [String] :name Name of font. # @option font [Numeric] :size Size of font. # @return [Numeric] def text_line_leading(line_height, font) - line_height - pdf.font(font[:name], :size => font[:size]).height + line_height - pdf.font(font[:name], size: font[:size]).height end - + # @param [String] content # @return [String] def text_without_line_wrap(content) content.gsub(/ /, Prawn::Text::NBSP) end - + # @param [Hash] attrs # @param [Hash] font # @option font [String] :color # @option font [Numeric] :size # @option font [String] :name @@ -130,31 +129,31 @@ if styles = attrs.delete(:styles) manual, styles = styles.partition do |style| [:bold, :italic].include?(style) && !font_has_style?(font[:name], style) end end - + # Emulate bold style. if manual && manual.include?(:bold) pdf.stroke_color(font[:color]) pdf.line_width(font[:size] * 0.025) - + # Change rendering mode to :fill_stroke. attrs[:mode] = :fill_stroke end - + # Emulate italic style. if manual && manual.include?(:italic) # FIXME # pdf.transformation_matrix(1, 0, 0.26, 1, 0, 0) end - - pdf.font(font[:name], :size => font[:size]) do + + pdf.font(font[:name], size: font[:size]) do pdf.fill_color(font[:color]) block.call(attrs, styles || []) end - end - + end + end - + end end