Class: Prawn::SVG::Elements::TextComponent

Inherits:
DirectRenderBase show all
Defined in:
lib/prawn/svg/elements/text_component.rb

Constant Summary

Constants inherited from Base

Base::COMMA_WSP_REGEXP, Base::MissingAttributesError, Base::PAINT_TYPES, Base::SVG_NAMESPACE, Base::SkipElementError, Base::SkipElementQuietly

Constants included from Attributes::Stroke

Attributes::Stroke::CAP_STYLE_TRANSLATIONS, Attributes::Stroke::JOIN_STYLE_TRANSLATIONS

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #base_calls, #calls, #document, #parent_calls, #properties, #source, #state

Instance Method Summary collapse

Methods inherited from DirectRenderBase

#render

Methods inherited from Base

#name, #parse_and_apply, #process

Methods included from TransformParser

#parse_transform_attribute

Methods included from PDFMatrix

#load_matrix, #matrix_for_pdf, #rotation_matrix, #scale_matrix, #translation_matrix

Methods included from Attributes::Space

#parse_xml_space_attribute

Methods included from Attributes::Stroke

#parse_stroke_attributes_and_call

Methods included from Attributes::ClipPath

#parse_clip_path_attribute_and_call

Methods included from Attributes::Opacity

#parse_opacity_attributes_and_call

Methods included from Attributes::Transform

#parse_transform_attribute_and_call

Constructor Details

#initialize(document, source, _calls, state, parent_component = nil) ⇒ TextComponent

Returns a new instance of TextComponent.



7
8
9
10
11
12
13
14
# File 'lib/prawn/svg/elements/text_component.rb', line 7

def initialize(document, source, _calls, state, parent_component = nil)
  if parent_component.nil? && source.name != 'text'
    raise SkipElementError, 'attempted to <use> a component inside a text element, this is not supported'
  end

  super(document, source, [], state)
  @parent_component = parent_component
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/prawn/svg/elements/text_component.rb', line 3

def children
  @children
end

#dxObject (readonly)

Returns the value of attribute dx.



4
5
6
# File 'lib/prawn/svg/elements/text_component.rb', line 4

def dx
  @dx
end

#dyObject (readonly)

Returns the value of attribute dy.



4
5
6
# File 'lib/prawn/svg/elements/text_component.rb', line 4

def dy
  @dy
end

#fontObject (readonly)

Returns the value of attribute font.



5
6
7
# File 'lib/prawn/svg/elements/text_component.rb', line 5

def font
  @font
end

#length_adjustObject (readonly)

Returns the value of attribute length_adjust.



4
5
6
# File 'lib/prawn/svg/elements/text_component.rb', line 4

def length_adjust
  @length_adjust
end

#parent_componentObject (readonly)

Returns the value of attribute parent_component.



3
4
5
# File 'lib/prawn/svg/elements/text_component.rb', line 3

def parent_component
  @parent_component
end

#rotationObject (readonly)

Returns the value of attribute rotation.



4
5
6
# File 'lib/prawn/svg/elements/text_component.rb', line 4

def rotation
  @rotation
end

#text_lengthObject (readonly)

Returns the value of attribute text_length.



4
5
6
# File 'lib/prawn/svg/elements/text_component.rb', line 4

def text_length
  @text_length
end

#x_valuesObject (readonly)

Returns the value of attribute x_values.



4
5
6
# File 'lib/prawn/svg/elements/text_component.rb', line 4

def x_values
  @x_values
end

#y_valuesObject (readonly)

Returns the value of attribute y_values.



4
5
6
# File 'lib/prawn/svg/elements/text_component.rb', line 4

def y_values
  @y_values
end

Instance Method Details

#calculated_widthObject



97
98
99
# File 'lib/prawn/svg/elements/text_component.rb', line 97

def calculated_width
  children.reduce(0) { |total, child| total + child.calculated_width }
end

#current_length_adjust_is_scaling?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
# File 'lib/prawn/svg/elements/text_component.rb', line 101

def current_length_adjust_is_scaling?
  if @text_length
    @length_adjust == 'spacingAndGlyphs'
  elsif parent_component
    parent_component.current_length_adjust_is_scaling?
  else
    false
  end
end

#lay_out(prawn) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/prawn/svg/elements/text_component.rb', line 44

def lay_out(prawn)
  @children.each do |child|
    prawn.save_font do
      prawn.font(font.name, style: font.subfamily) if font
      child.lay_out(prawn)
    end
  end

  if @text_length
    flexible_width, fixed_width = total_flexible_and_fixed_width

    if flexible_width.positive?
      target_width = [@text_length - fixed_width, 0].max
      factor = target_width / flexible_width
      apply_factor_to_base_width(factor)
    end
  end
end

#letter_spacing_pixelsObject



111
112
113
114
115
116
117
# File 'lib/prawn/svg/elements/text_component.rb', line 111

def letter_spacing_pixels
  if computed_properties.letter_spacing == 'normal'
    nil
  else
    x_pixels(computed_properties.letter_spacing)
  end
end

#parseObject

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/prawn/svg/elements/text_component.rb', line 16

def parse
  raise SkipElementError, '<text> elements are not supported in clip paths' if state.inside_clip_path

  @x_values = parse_wsp('x').map { |n| x(n) }
  @y_values = parse_wsp('y').map { |n| y(n) }
  @dx = parse_wsp('dx').map { |n| x_pixels(n) }
  @dy = parse_wsp('dy').map { |n| y_pixels(n) }
  @rotation = parse_wsp('rotate').map(&:to_f)
  @text_length = normalize_length(attributes['textLength'])
  @length_adjust = attributes['lengthAdjust']

  @font = select_font

  @children = svg_text_children.flat_map do |child|
    if child.node_type == :text
      build_text_node(child)
    else
      case child.name
      when 'tspan', 'tref'
        build_child(child)
      else
        warnings << "Unknown tag '#{child.name}' inside text tag; ignoring"
        []
      end
    end
  end
end

#render_component(prawn, renderer, cursor, translate_x = nil) ⇒ Object

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/prawn/svg/elements/text_component.rb', line 63

def render_component(prawn, renderer, cursor, translate_x = nil)
  raise SkipElementQuietly if computed_properties.display == 'none'

  add_yield_call do
    prawn.translate(translate_x, 0) if translate_x

    size = computed_properties.numeric_font_size

    if computed_properties.dominant_baseline == 'middle'
      height = FontMetrics.x_height_in_points(prawn, size || prawn.font_size)
      y_offset = -height / 2.0
    end

    prawn.save_font do
      prawn.font(font.name, style: font.subfamily) if font

      children.each do |child|
        case child
        when Elements::TextNode
          child.render(prawn, size, cursor, y_offset)
        when self.class
          prawn.save_graphics_state
          child.render_component(prawn, renderer, cursor)
          prawn.restore_graphics_state
        else
          raise
        end
      end
    end
  end

  renderer.render_calls(prawn, base_calls)
end