Class: RubyDocx::Elements::Style

Inherits:
Element
  • Object
show all
Defined in:
lib/ruby_docx/elements/style.rb

Instance Attribute Summary

Attributes inherited from Element

#doc, #grid, #node, #style

Instance Method Summary collapse

Methods inherited from Element

#elements, #initialize, #inspect, #to_xml

Constructor Details

This class inherits a constructor from RubyDocx::Elements::Element

Instance Method Details

#alignObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_docx/elements/style.rb', line 26

def align
  ele = @node.xpath(".//w:jc").last

  if ele
    v = ele.attributes["val"].value.to_s
    if v == "start"
      "left"
    elsif v == "end"
      "right"
    elsif v == "center"
      "center"
    end
  else
    nil
  end
end

#bold?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ruby_docx/elements/style.rb', line 22

def bold?
  false
end

#colorObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_docx/elements/style.rb', line 55

def color
  ele = @node.xpath(".//w:color").last

  if ele
    v = ele.attributes["val"].value
    if v.to_s == "auto"
      nil
    else
      v
    end
  else
    nil
  end
end

#font_nameObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby_docx/elements/style.rb', line 70

def font_name
  ele = @node.xpath(".//w:rFonts").last

  if ele
    ascii = ele.attributes["ascii"]
    cs = ele.attributes["cs"]
    hAnsi = ele.attributes["hAnsi"]

    ascii.value
  else
    nil
  end
end

#font_sizeObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_docx/elements/style.rb', line 43

def font_size
  ele = @node.xpath(".//w:sz").last

  if ele
    v = ele.attributes["val"].value
    v.to_i/2
  else
    nil
  end

end

#fontsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby_docx/elements/style.rb', line 85

def fonts
  ele = @node.xpath(".//w:rFonts").last

  if ele
    ascii = ele.attributes["ascii"]
    cs = ele.attributes["cs"]
    hAnsi = ele.attributes["hAnsi"]

    [ascii.value, cs.value, hAnsi.value].uniq
  else
    nil
  end
end

#to_sObject

attr_reader :font_name, :font_size, :color, :text_align, :fonts



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_docx/elements/style.rb', line 5

def to_s
  arry = []
  if font_size
    arry << "font-size: #{font_size}pt"
  end

  if fonts
    arry << "font-family: #{fonts.join(",")}"
  end

  if color
    arry << "color: #{color}"
  end

  arry.join(";")
end