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.

Defined Under Namespace

Classes: LineSpacing

Constant Summary collapse

UNSET =

:nodoc:

::Object.new

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Style

Creates a new Style object.

The options 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)


125
126
127
128
# File 'lib/hexapdf/layout/style.rb', line 125

def initialize(**options)
  options.each {|key, value| send(key, value)}
  @scaled_item_widths = {}
end

Instance Method Details

#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.



356
357
358
359
360
# File 'lib/hexapdf/layout/style.rb', line 356

def clear_cache
  @scaled_font_size = @scaled_character_spacing = @scaled_word_spacing = nil
  @scaled_horizontal_scaling = @ascender = @descender = nil
  @scaled_item_widths.clear
end

#line_spacing(type = UNSET, value = nil) ⇒ Object Also known as: line_spacing=

:call-seq:

line_spacing(type = nil, value = nil)

The spacing between consecutive lines, defaults to type = :single.

See: LineSpacing



259
260
261
262
263
264
265
266
# File 'lib/hexapdf/layout/style.rb', line 259

def line_spacing(type = UNSET, value = nil)
  if type == UNSET
    @line_spacing ||= LineSpacing.new(:single)
  else
    @line_spacing = LineSpacing.new(type, value: value)
    self
  end
end

#nameObject

:method: text_indent :call-seq:

text_indent(amount = nil)

The indentation to be used for the first line of a sequence of text lines, defaults to 0.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/hexapdf/layout/style.rb', line 232

[
  [:font, "raise HexaPDF::Error, 'No font set'"],
  [:font_size, 10],
  [:character_spacing, 0],
  [:word_spacing, 0],
  [:horizontal_scaling, 100],
  [:text_rise, 0],
  [:font_features, {}],
  [:align, :left],
  [:valign, :top],
  [:text_indent, 0],
].each do |name, default|
  default = default.inspect unless default.kind_of?(String)
  module_eval(<<-EOF, __FILE__, __LINE__)
    def #{name}(value = UNSET)
      value == UNSET ? (@#{name} ||= #{default}) : (@#{name} = value; self)
    end
  EOF
  alias_method("#{name}=", name)
end

#scaled_character_spacingObject

The character spacing scaled appropriately.



310
311
312
# File 'lib/hexapdf/layout/style.rb', line 310

def scaled_character_spacing
  @scaled_character_spacing ||= character_spacing * scaled_horizontal_scaling
end

#scaled_font_ascenderObject

The ascender of the font scaled appropriately.



325
326
327
# File 'lib/hexapdf/layout/style.rb', line 325

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

#scaled_font_descenderObject

The descender of the font scaled appropriately.



330
331
332
# File 'lib/hexapdf/layout/style.rb', line 330

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

#scaled_font_sizeObject

The font size scaled appropriately.



305
306
307
# File 'lib/hexapdf/layout/style.rb', line 305

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

#scaled_horizontal_scalingObject

The horizontal scaling scaled appropriately.



320
321
322
# File 'lib/hexapdf/layout/style.rb', line 320

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.



339
340
341
342
343
344
345
346
347
348
349
# File 'lib/hexapdf/layout/style.rb', line 339

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.



315
316
317
# File 'lib/hexapdf/layout/style.rb', line 315

def scaled_word_spacing
  @scaled_word_spacing ||= word_spacing * scaled_horizontal_scaling
end

#text_line_wrapping_algorithm(algorithm = UNSET, &block) ⇒ Object Also known as: text_line_wrapping_algorithm=

:call-seq:

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

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

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



294
295
296
297
298
299
300
301
# File 'lib/hexapdf/layout/style.rb', line 294

def text_line_wrapping_algorithm(algorithm = UNSET, &block)
  if algorithm == UNSET && !block
    @text_line_wrapping_algorithm ||= TextBox::SimpleLineWrapping
  else
    @text_line_wrapping_algorithm = (algorithm != UNSET ? algorithm : block)
    self
  end
end

#text_segmentation_algorithm(algorithm = UNSET, &block) ⇒ Object Also known as: text_segmentation_algorithm=

:call-seq:

text_segmentation_algorithm(algorithm = nil) {|items| block }

The algorithm to use for text segmentation purposes, defaults to TextBox::SimpleTextSegmentation.

When setting the algorithm, either an object that responds to #call(items) or a block can be used.



277
278
279
280
281
282
283
284
# File 'lib/hexapdf/layout/style.rb', line 277

def text_segmentation_algorithm(algorithm = UNSET, &block)
  if algorithm == UNSET && !block
    @text_segmentation_algorithm ||= TextBox::SimpleTextSegmentation
  else
    @text_segmentation_algorithm = (algorithm != UNSET ? algorithm : block)
    self
  end
end