Sha256: fa59a86cbbb86035eac7c6723edab70a977308e6d426ec7b11b2c4ac10201761

Contents?: true

Size: 1.76 KB

Versions: 10

Compression:

Stored size: 1.76 KB

Contents

require 'berkeley_library/util/ods/xml/element_node'

module BerkeleyLibrary
  module Util
    module ODS
      module XML
        module Style
          class TextProperties < ElementNode

            FONT_WEIGHT_ATTRS = %w[font-weight font-weight-asian font-weight-complex].freeze
            COLOR_RE = /^#[[:xdigit:]]{6}$/.freeze

            attr_reader :color, :font_name, :language, :country, :font_weight

            # rubocop:disable Metrics/ParameterLists, Style/KeywordParametersOrder
            def initialize(color: nil, font_name: nil, font_weight: nil, language: 'en', country: 'US', doc:)
              super(:style, 'text-properties', doc: doc)
              @color = ensure_color(color)
              @font_name = font_name
              @language = language
              @country = country
              @font_weight = font_weight
              set_default_attributes!
            end
            # rubocop:enable Metrics/ParameterLists, Style/KeywordParametersOrder

            private

            def set_default_attributes!
              set_attribute('font-name', font_name) if font_name
              set_attribute(:fo, 'language', language) if language
              set_attribute(:fo, 'country', country) if country
              set_attribute(:fo, 'color', color) if color
              set_font_weight_attributes!
            end

            def set_font_weight_attributes!
              FONT_WEIGHT_ATTRS.each { |attr| set_attribute(:fo, attr, font_weight) } if font_weight
            end

            def ensure_color(color)
              return unless color
              raise ArgumentError, "Not a valid hex color: #{color.inspect}" unless color =~ COLOR_RE

              color.downcase
            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
berkeley_library-tind-0.7.2 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.7.1 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.7.0 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.6.0 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.5.1 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.5.0 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.4.3 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.4.2 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.4.1 lib/berkeley_library/util/ods/xml/style/text_properties.rb
berkeley_library-tind-0.4.0 lib/berkeley_library/util/ods/xml/style/text_properties.rb