Class: HexaPDF::Layout::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/layout/style.rb

Overview

A Style is a container for properties that describe the appearance of text or graphics.

Each property except #font has a default value, so only the desired properties need to be changed.

Each property has three associated methods:

property_name

Getter method.

property_name(*args) and property_name=

Setter method.

property_name?

Tester method to see if a value has been set or if the default value has already been used.

Defined Under Namespace

Classes: Border, Layers, LineSpacing, LinkLayer, Quad

Constant Summary collapse

UNSET =

:nodoc:

::Object.new

Instance Method Summary collapse

Constructor Details

#initialize(**properties) ⇒ Style

Creates a new Style object.

The properties hash may be used to set the initial values of properties by using keys equivalent to the property names.

Example:

Style.new(font_size: 15, align: :center, valign: center)


508
509
510
511
# File 'lib/hexapdf/layout/style.rb', line 508

def initialize(**properties)
  update(properties)
  @scaled_item_widths = {}
end

Instance Method Details

#calculated_font_sizeObject

The calculated font size, taking superscript and subscript into account.



923
924
925
# File 'lib/hexapdf/layout/style.rb', line 923

def calculated_font_size
  (superscript || subscript ? 0.583 : 1) * font_size
end

#calculated_strikeout_positionObject

Returns the correct offset from the baseline for the strikeout line.



940
941
942
943
944
# File 'lib/hexapdf/layout/style.rb', line 940

def calculated_strikeout_position
  calculated_text_rise +
    calculated_font_size / 1000.0 * font.wrapped_font.strikeout_position *
    font.scaling_factor - calculated_strikeout_thickness / 2.0
end

#calculated_strikeout_thicknessObject

Returns the correct thickness for the strikeout line.



947
948
949
# File 'lib/hexapdf/layout/style.rb', line 947

def calculated_strikeout_thickness
  calculated_font_size / 1000.0 * font.wrapped_font.strikeout_thickness * font.scaling_factor
end

#calculated_text_riseObject

The calculated text rise, taking superscript and subscript into account.



912
913
914
915
916
917
918
919
920
# File 'lib/hexapdf/layout/style.rb', line 912

def calculated_text_rise
  if superscript
    text_rise + font_size * 0.33
  elsif subscript
    text_rise - font_size * 0.20
  else
    text_rise
  end
end

#calculated_underline_positionObject

Returns the correct offset from the baseline for the underline.



928
929
930
931
932
# File 'lib/hexapdf/layout/style.rb', line 928

def calculated_underline_position
  calculated_text_rise +
    calculated_font_size / 1000.0 * font.wrapped_font.underline_position *
    font.scaling_factor - calculated_underline_thickness / 2.0
end

#calculated_underline_thicknessObject

Returns the correct thickness for the underline.



935
936
937
# File 'lib/hexapdf/layout/style.rb', line 935

def calculated_underline_thickness
  calculated_font_size / 1000.0 * font.wrapped_font.underline_thickness * font.scaling_factor
end

#clear_cacheObject

Clears all cached values.

This method needs to be called if the following style properties are changed and values were already cached: font, font_size, character_spacing, word_spacing, horizontal_scaling, ascender, descender.



1013
1014
1015
1016
1017
1018
# File 'lib/hexapdf/layout/style.rb', line 1013

def clear_cache
  @scaled_font_size = @scaled_character_spacing = @scaled_word_spacing = nil
  @scaled_horizontal_scaling = @scaled_font_ascender = @scaled_font_descender = nil
  @scaled_y_min = @scaled_y_max = nil
  @scaled_item_widths.clear
end

#nameObject

:method: text_line_wrapping_algorithm :call-seq:

text_line_wrapping_algorithm(algorithm = nil) {|items, width_block| block }

The line wrapping algorithm that should be used, defaults to TextLayouter::SimpleLineWrapping.

When setting the algorithm, either an object that responds to #call or a block can be used. See TextLayouter::SimpleLineWrapping#call for the needed method signature.



819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/hexapdf/layout/style.rb', line 819

[
  [:font, "raise HexaPDF::Error, 'No font set'"],
  [:font_size, 10],
  [:character_spacing, 0],
  [:word_spacing, 0],
  [:horizontal_scaling, 100],
  [:text_rise, 0],
  [:font_features, {}],
  [:text_rendering_mode, :fill],
  [:subscript, false, "value; superscript(false) if superscript"],
  [:superscript, false, "value; subscript(false) if subscript"],
  [:underline, false],
  [:strikeout, false],
  [:fill_color, "default_color"],
  [:fill_alpha, 1],
  [:stroke_color, "default_color"],
  [:stroke_alpha, 1],
  [:stroke_width, 1],
  [:stroke_cap_style, :butt],
  [:stroke_join_style, :miter],
  [:stroke_miter_limit, 10.0],
  [:stroke_dash_pattern, "Content::LineDashPattern.new",
   "Content::LineDashPattern.normalize(value, phase)", ", phase = 0"],
  [:align, :left],
  [:valign, :top],
  [:text_indent, 0],
  [:line_spacing, "LineSpacing.new(:single)",
   "LineSpacing.new(value, value: extra_arg)", ", extra_arg = nil"],
  [:background_color, nil],
  [:padding, "Quad.new(0)", "Quad.new(value)"],
  [:margin, "Quad.new(0)", "Quad.new(value)"],
  [:border, "Border.new", "Border.new(value)"],
  [:overlays, "Layers.new", "Layers.new(value)"],
  [:underlays, "Layers.new", "Layers.new(value)"],
  [:position, :default],
  [:position_hint, nil],
].each do |name, default, setter = "value", extra_args = ""|
  default = default.inspect unless default.kind_of?(String)
  module_eval(<<-EOF, __FILE__, __LINE__ + 1)
    def #{name}(value = UNSET#{extra_args})
      value == UNSET ? (@#{name} ||= #{default}) : (@#{name} = #{setter}; self)
    end
    def #{name}?
      defined?(@#{name})
    end
  EOF
  alias_method("#{name}=", name)
end

#scaled_character_spacingObject

The character spacing scaled appropriately.



957
958
959
# File 'lib/hexapdf/layout/style.rb', line 957

def scaled_character_spacing
  @scaled_character_spacing ||= character_spacing * scaled_horizontal_scaling
end

#scaled_font_ascenderObject

The ascender of the font scaled appropriately.



972
973
974
# File 'lib/hexapdf/layout/style.rb', line 972

def scaled_font_ascender
  @scaled_font_ascender ||= font.wrapped_font.ascender * font.scaling_factor * font_size / 1000
end

#scaled_font_descenderObject

The descender of the font scaled appropriately.



977
978
979
# File 'lib/hexapdf/layout/style.rb', line 977

def scaled_font_descender
  @scaled_font_descender ||= font.wrapped_font.descender * font.scaling_factor * font_size / 1000
end

#scaled_font_sizeObject

The font size scaled appropriately.



952
953
954
# File 'lib/hexapdf/layout/style.rb', line 952

def scaled_font_size
  @scaled_font_size ||= calculated_font_size / 1000.0 * scaled_horizontal_scaling
end

#scaled_horizontal_scalingObject

The horizontal scaling scaled appropriately.



967
968
969
# File 'lib/hexapdf/layout/style.rb', line 967

def scaled_horizontal_scaling
  @scaled_horizontal_scaling ||= horizontal_scaling / 100.0
end

#scaled_item_width(item) ⇒ Object

Returns the width of the item scaled appropriately (by taking font size, characters spacing, word spacing and horizontal scaling into account).

The item may be a (singleton) glyph object or an integer/float, i.e. items that can appear inside a TextFragment.



996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/hexapdf/layout/style.rb', line 996

def scaled_item_width(item)
  @scaled_item_widths[item.object_id] ||=
    begin
      if item.kind_of?(Numeric)
        -item * scaled_font_size
      else
        item.width * scaled_font_size + scaled_character_spacing +
          (item.apply_word_spacing? ? scaled_word_spacing : 0)
      end
    end
end

#scaled_word_spacingObject

The word spacing scaled appropriately.



962
963
964
# File 'lib/hexapdf/layout/style.rb', line 962

def scaled_word_spacing
  @scaled_word_spacing ||= word_spacing * scaled_horizontal_scaling
end

#scaled_y_maxObject

The maximum y-coordinate, calculated using the scaled descender of the font.



987
988
989
# File 'lib/hexapdf/layout/style.rb', line 987

def scaled_y_max
  @scaled_y_max ||= scaled_font_ascender + calculated_text_rise
end

#scaled_y_minObject

The minimum y-coordinate, calculated using the scaled descender of the font.



982
983
984
# File 'lib/hexapdf/layout/style.rb', line 982

def scaled_y_min
  @scaled_y_min ||= scaled_font_descender + calculated_text_rise
end

#update(**properties) ⇒ Object

:call-seq:

style.update(**properties)    -> style

Updates the style’s properties using the key-value pairs specified by the properties hash.



517
518
519
520
# File 'lib/hexapdf/layout/style.rb', line 517

def update(**properties)
  properties.each {|key, value| send(key, value) }
  self
end