lib/rvg/misc.rb in rmagick-2.16.0 vs lib/rvg/misc.rb in rmagick-3.0.0
- old
+ new
@@ -12,43 +12,41 @@
unless copy
h[__id__] = copy = self.class.allocate
ivars = instance_variables
ivars.each do |ivar|
ivalue = instance_variable_get(ivar)
- cvalue = case
- when NilClass === ivalue, Symbol === ivalue, Float === ivalue,
- Fixnum === ivalue, FalseClass === ivalue, TrueClass === ivalue
- ivalue
- when ivalue.respond_to?(:deep_copy)
- ivalue.deep_copy(h)
- when ivalue.respond_to?(:dup)
- ivalue.dup
- else
- ivalue
- end
+ cvalue = if ivalue.is_a?(NilClass) || ivalue.is_a?(Symbol) || ivalue.is_a?(Float) || ivalue.is_a?(Fixnum) || ivalue.is_a?(FalseClass) || ivalue.is_a?(TrueClass)
+ ivalue
+ elsif ivalue.respond_to?(:deep_copy)
+ ivalue.deep_copy(h)
+ elsif ivalue.respond_to?(:dup)
+ ivalue.dup
+ else
+ ivalue
+ end
copy.instance_variable_set(ivar, cvalue)
end
copy.freeze if frozen?
end
copy
end
- end # module Duplicatable
+ end # module Duplicatable
# Convert an array of method arguments to Float objects. If any
# cannot be converted, raise ArgumentError and issue a message.
def self.fmsg(*args)
- "at least one argument cannot be converted to Float (got #{args.collect {|a| a.class}.join(', ')})"
+ "at least one argument cannot be converted to Float (got #{args.collect(&:class).join(', ')})"
end
def self.convert_to_float(*args)
allow_nil = false
if args.last == :allow_nil
allow_nil = true
args.pop
end
begin
- fargs = args.collect { |a| (allow_nil && a.nil?) ? a : Float(a) }
+ fargs = args.collect { |a| allow_nil && a.nil? ? a : Float(a) }
rescue ArgumentError, TypeError
raise ArgumentError, fmsg(*args)
end
fargs
end
@@ -72,20 +70,20 @@
@ctx = context
@ctx.shadow.affine = @ctx.text_attrs.affine
end
def enquote(text)
- if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text)
+ return text if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text)
+
+ if !text['\'']
+ text = '\'' + text + '\''
return text
- elsif !text['\'']
- text = '\''+text+'\''
- return text
elsif !text['"']
- text = '"'+text+'"'
+ text = '"' + text + '"'
return text
elsif !(text['{'] || text['}'])
- text = '{'+text+'}'
+ text = '{' + text + '}'
return text
end
# escape existing braces, surround with braces
text.gsub!(/[}]/) { |b| '\\' + b }
@@ -95,11 +93,11 @@
def glyph_metrics(glyph_orientation, glyph)
gm = @ctx.shadow.get_type_metrics('a' + glyph + 'a')
gm2 = @ctx.shadow.get_type_metrics('aa')
h = (gm.ascent - gm.descent + 0.5).to_i
w = gm.width - gm2.width
- if glyph_orientation == 0 || glyph_orientation == 180
+ if glyph_orientation.zero? || glyph_orientation == 180
[w, h]
else
[h, w]
end
end
@@ -125,59 +123,59 @@
[x_rel_coords, y_rel_coords]
end
def shift_baseline(glyph_orientation, glyph)
glyph_dimensions = @ctx.shadow.get_type_metrics(glyph)
- if glyph_orientation == 0 || glyph_orientation == 180
- x = glyph_dimensions.width
- else
- x = glyph_dimensions.ascent - glyph_dimensions.descent
- end
+ x = if glyph_orientation.zero? || glyph_orientation == 180
+ glyph_dimensions.width
+ else
+ glyph_dimensions.ascent - glyph_dimensions.descent
+ end
case @ctx.text_attrs.baseline_shift
- when :baseline
- x = 0
- when :sub
+ when :baseline
+ x = 0
+ when :sub
- when :super
- x = -x
- when /[-+]?(\d+)%/
- m = $1 == '-' ? -1.0 : 1.0
- x = (m * x * $1.to_f / 100.0)
- else
- x = -@ctx.text_attrs.baseline_shift
+ when :super
+ x = -x
+ when /[-+]?(\d+)%/
+ m = Regexp.last_match(1) == '-' ? -1.0 : 1.0
+ x = (m * x * Regexp.last_match(1).to_f / 100.0)
+ else
+ x = -@ctx.text_attrs.baseline_shift
end
x
end
def render_glyph(glyph_orientation, x, y, glyph)
- if glyph_orientation == 0
+ if glyph_orientation.zero?
@ctx.gc.text(x, y, enquote(glyph))
else
@ctx.gc.push
@ctx.gc.translate(x, y)
@ctx.gc.rotate(glyph_orientation)
@ctx.gc.translate(-x, -y)
@ctx.gc.text(x, y, enquote(glyph))
@ctx.gc.pop
end
end
- end # class TextStrategy
+ end # class TextStrategy
class LRTextStrategy < TextStrategy
def get_word_spacing
@word_space ||= glyph_metrics(@ctx.text_attrs.glyph_orientation_horizontal, ' ')[0]
[@word_space + @ctx.text_attrs.word_spacing, 0]
end
def get_letter_spacing(glyph)
gx, gy = glyph_metrics(@ctx.text_attrs.glyph_orientation_horizontal, glyph)
- [gx+@ctx.text_attrs.letter_spacing, gy]
+ [gx + @ctx.text_attrs.letter_spacing, gy]
end
def render(x, y, text)
x_rel_coords, y_rel_coords = text_rel_coords(text)
- dx = x_rel_coords.inject(0) {|sum, a| sum + a}
+ dx = x_rel_coords.inject(0) { |sum, a| sum + a }
dy = y_rel_coords.max
# We're handling the anchoring.
@ctx.gc.push
@ctx.gc.text_anchor(Magick::StartAnchor)
@@ -187,29 +185,25 @@
x -= dx / 2
end
# Align the first glyph
case @ctx.text_attrs.glyph_orientation_horizontal
- when 0
-
- when 90
- y -= dy
- when 180
- x += x_rel_coords.shift
- x_rel_coords << 0
- y -= dy
- when 270
- x += x_rel_coords[0]
+ when 90
+ y -= dy
+ when 180
+ x += x_rel_coords.shift
+ x_rel_coords << 0
+ y -= dy
+ when 270
+ x += x_rel_coords[0]
end
- y += shift_baseline(@ctx.text_attrs.glyph_orientation_horizontal, text[0,1])
+ y += shift_baseline(@ctx.text_attrs.glyph_orientation_horizontal, text[0, 1])
first_word = true
text.split(::Magick::RVG::WORD_SEP).each do |word|
- unless first_word
- x += x_rel_coords.shift
- end
+ x += x_rel_coords.shift unless first_word
first_word = false
word.split('').each do |glyph|
render_glyph(@ctx.text_attrs.glyph_orientation_horizontal, x, y, glyph)
x += x_rel_coords.shift
end
@@ -219,12 +213,12 @@
[dx, 0]
end
end # class LRTextStrategy
class RLTextStrategy < TextStrategy
- def render(x, y, text)
- fail NotImplementedError
+ def render(_x, _y, _text)
+ raise NotImplementedError
end
end # class RLTextStrategy
class TBTextStrategy < TextStrategy
def get_word_spacing
@@ -232,17 +226,17 @@
[0, @word_space + @ctx.text_attrs.word_spacing]
end
def get_letter_spacing(glyph)
gx, gy = glyph_metrics(@ctx.text_attrs.glyph_orientation_vertical, glyph)
- [gx, gy+@ctx.text_attrs.letter_spacing]
+ [gx, gy + @ctx.text_attrs.letter_spacing]
end
def render(x, y, text)
x_rel_coords, y_rel_coords = text_rel_coords(text)
dx = x_rel_coords.max
- dy = y_rel_coords.inject(0) {|sum, a| sum + a}
+ dy = y_rel_coords.inject(0) { |sum, a| sum + a }
# We're handling the anchoring.
@ctx.gc.push
@ctx.gc.text_anchor(Magick::StartAnchor)
if @ctx.text_attrs.text_anchor == :end
@@ -253,78 +247,76 @@
# Align the first glyph such that its center
# is aligned on x and its top is aligned on y.
case @ctx.text_attrs.glyph_orientation_vertical
- when 0
- x -= x_rel_coords.max / 2
- y += y_rel_coords[0]
- when 90
- x -= x_rel_coords.max / 2
- when 180
- x += x_rel_coords.max / 2
- when 270
- x += x_rel_coords.max / 2
- y += y_rel_coords.shift
- y_rel_coords << 0 # since we used an element we need to add a dummy
+ when 0
+ x -= x_rel_coords.max / 2
+ y += y_rel_coords[0]
+ when 90
+ x -= x_rel_coords.max / 2
+ when 180
+ x += x_rel_coords.max / 2
+ when 270
+ x += x_rel_coords.max / 2
+ y += y_rel_coords.shift
+ y_rel_coords << 0 # since we used an element we need to add a dummy
end
- x -= shift_baseline(@ctx.text_attrs.glyph_orientation_vertical, text[0,1])
+ x -= shift_baseline(@ctx.text_attrs.glyph_orientation_vertical, text[0, 1])
first_word = true
text.split(::Magick::RVG::WORD_SEP).each do |word|
unless first_word
y += y_rel_coords.shift
x_rel_coords.shift
end
first_word = false
word.split('').each do |glyph|
case @ctx.text_attrs.glyph_orientation_vertical.to_i
- when 0, 90, 270
- x_shift = (dx - x_rel_coords.shift) / 2
- when 180
- x_shift = -(dx - x_rel_coords.shift) / 2
+ when 0, 90, 270
+ x_shift = (dx - x_rel_coords.shift) / 2
+ when 180
+ x_shift = -(dx - x_rel_coords.shift) / 2
end
- render_glyph(@ctx.text_attrs.glyph_orientation_vertical, x+x_shift, y, glyph)
+ render_glyph(@ctx.text_attrs.glyph_orientation_vertical, x + x_shift, y, glyph)
y += y_rel_coords.shift
end
end
@ctx.gc.pop
[0, dy]
end
- end # class TBTextStrategy
+ end # class TBTextStrategy
# Handle "easy" text
class DefaultTextStrategy < TextStrategy
def render(x, y, text)
@ctx.gc.text(x, y, enquote(text))
tm = @ctx.shadow.get_type_metrics(text)
dx = case @ctx.text_attrs.text_anchor
- when :start
+ when :start
tm.width
- when :middle
+ when :middle
tm.width / 2
- when :end
+ when :end
0
- end
+ end
[dx, 0]
end
- end # class NormalTextStrategy
+ end # class NormalTextStrategy
end # class Utility
end # class RVG
end # module Magick
module Magick
class RVG
class Utility
class TextAttributes
- public
+ WRITING_MODE = %w[lr-tb lr rl-tb rl tb-rl tb]
- WRITING_MODE = %w{lr-tb lr rl-tb rl tb-rl tb}
-
def initialize
@affine = []
@affine << Magick::AffineMatrix.new(1, 0, 0, 1, 0, 0)
@baseline_shift = []
@baseline_shift << :baseline
@@ -417,12 +409,12 @@
@letter_spacing[-1] = value
end
def non_default?
@baseline_shift[-1] != :baseline || @letter_spacing[-1] != 0 ||
- @word_spacing[-1] != 0 || @writing_mode[-1][/\Alr/].nil? ||
- @glyph_orientation_horizontal[-1] != 0
+ @word_spacing[-1] != 0 || @writing_mode[-1][/\Alr/].nil? ||
+ @glyph_orientation_horizontal[-1] != 0
end
def word_spacing
@word_spacing[-1]
end
@@ -436,57 +428,58 @@
end
def writing_mode=(mode)
@writing_mode[-1] = WRITING_MODE.include?(mode) ? mode : 'lr-tb'
end
- end # class TextAttributes
+ end # class TextAttributes
class GraphicContext
FONT_STRETCH = {
- :normal => Magick::NormalStretch,
- :ultra_condensed => Magick::UltraCondensedStretch,
- :extra_condensed => Magick::ExtraCondensedStretch,
- :condensed => Magick::CondensedStretch,
- :semi_condensed => Magick::SemiCondensedStretch,
- :semi_expanded => Magick::SemiExpandedStretch,
- :expanded => Magick::ExpandedStretch,
- :extra_expanded => Magick::ExtraExpandedStretch,
- :ultra_expanded => Magick::UltraExpandedStretch
+ normal: Magick::NormalStretch,
+ ultra_condensed: Magick::UltraCondensedStretch,
+ extra_condensed: Magick::ExtraCondensedStretch,
+ condensed: Magick::CondensedStretch,
+ semi_condensed: Magick::SemiCondensedStretch,
+ semi_expanded: Magick::SemiExpandedStretch,
+ expanded: Magick::ExpandedStretch,
+ extra_expanded: Magick::ExtraExpandedStretch,
+ ultra_expanded: Magick::UltraExpandedStretch
}
FONT_STYLE = {
- :normal => Magick::NormalStyle,
- :italic => Magick::ItalicStyle,
- :oblique => Magick::ObliqueStyle
+ normal: Magick::NormalStyle,
+ italic: Magick::ItalicStyle,
+ oblique: Magick::ObliqueStyle
}
FONT_WEIGHT = {
'normal' => Magick::NormalWeight,
'bold' => Magick::BoldWeight,
'bolder' => Magick::BolderWeight,
'lighter' => Magick::LighterWeight
}
TEXT_ANCHOR = {
- :start => Magick::StartAnchor,
- :middle => Magick::MiddleAnchor,
- :end => Magick::EndAnchor
+ start: Magick::StartAnchor,
+ middle: Magick::MiddleAnchor,
+ end: Magick::EndAnchor
}
ANCHOR_TO_ALIGN = {
- :start => Magick::LeftAlign,
- :middle => Magick::CenterAlign,
- :end => Magick::RightAlign
+ start: Magick::LeftAlign,
+ middle: Magick::CenterAlign,
+ end: Magick::RightAlign
}
TEXT_DECORATION = {
- :none => Magick::NoDecoration,
- :underline => Magick::UnderlineDecoration,
- :overline => Magick::OverlineDecoration,
- :line_through => Magick::LineThroughDecoration}
+ none: Magick::NoDecoration,
+ underline: Magick::UnderlineDecoration,
+ overline: Magick::OverlineDecoration,
+ line_through: Magick::LineThroughDecoration
+ }
- TEXT_STRATEGIES = {
+ TEXT_STRATEGIES = {
'lr-tb' => LRTextStrategy, 'lr' => LRTextStrategy,
'rt-tb' => RLTextStrategy, 'rl' => RLTextStrategy,
'tb-rl' => TBTextStrategy, 'tb' => TBTextStrategy
}
@@ -524,12 +517,12 @@
@shadow << Magick::Draw.new
@text_attrs = TextAttributes.new
init_matrix
end
- def method_missing(methID, *args, &block)
- @gc.__send__(methID, *args, &block)
+ def method_missing(meth_id, *args, &block)
+ @gc.__send__(meth_id, *args, &block)
end
def affine(sx, rx, ry, sy, tx, ty)
sx, rx, ry, sy, tx, ty = Magick::RVG.convert_to_float(sx, rx, ry, sy, tx, ty)
@gc.affine(sx, rx, ry, sy, tx, ty)
@@ -537,17 +530,17 @@
nil
end
def baseline_shift(value)
@text_attrs.baseline_shift = case value
- when 'baseline', 'sub', 'super'
- value.intern
- when /[-+]?\d+%/, Numeric
- value
- else
- :baseline
- end
+ when 'baseline', 'sub', 'super'
+ value.intern
+ when /[-+]?\d+%/, Numeric
+ value
+ else
+ :baseline
+ end
nil
end
def font(name)
@gc.font(name)
@@ -581,11 +574,11 @@
nil
end
def font_weight(weight)
# If the arg is not in the hash use it directly. Handles numeric values.
- weight = FONT_WEIGHT.fetch(weight) {|key| key}
+ weight = FONT_WEIGHT.fetch(weight) { |key| key }
@gc.font_weight(weight)
@shadow[-1].font_weight = weight
nil
end
@@ -670,17 +663,18 @@
@shadow[-1].stroke_width = width
nil
end
def text(x, y, text)
- return if text.length == 0
- if @text_attrs.non_default?
- text_renderer = TEXT_STRATEGIES[@text_attrs.writing_mode].new(self)
- else
- text_renderer = DefaultTextStrategy.new(self)
- end
+ return if text.length.zero?
+ text_renderer = if @text_attrs.non_default?
+ TEXT_STRATEGIES[@text_attrs.writing_mode].new(self)
+ else
+ DefaultTextStrategy.new(self)
+ end
+
text_renderer.render(x, y, text)
end
def text_anchor(anchor)
anchor = anchor.intern
@@ -715,9 +709,9 @@
def writing_mode(mode)
@text_attrs.writing_mode = mode
nil
end
- end # class GraphicContext
+ end # class GraphicContext
end # class Utility
end # class RVG
end # module Magick