Class: Magick::RVG::Utility::GraphicContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rvg/misc.rb

Overview

class TextAttributes

Constant Summary collapse

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}
FONT_STYLE =
{: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}
ANCHOR_TO_ALIGN =
{:start  => Magick::LeftAlign,
:middle => Magick::CenterAlign,
:end    => Magick::RightAlign}
TEXT_DECORATION =
{:none         => Magick::NoDecoration,
:underline    => Magick::UnderlineDecoration,
:overline     => Magick::OverlineDecoration,
:line_through => Magick::LineThroughDecoration}
TEXT_STRATEGIES =
{'lr-tb'=>LRTextStrategy, 'lr'=>LRTextStrategy,
'rt-tb'=>RLTextStrategy, 'rl'=>RLTextStrategy,
'tb-rl'=>TBTextStrategy, 'tb'=>TBTextStrategy}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphicContext

Returns a new instance of GraphicContext.



536
537
538
539
540
541
542
# File 'lib/rvg/misc.rb', line 536

def initialize()
    @gc = Magick::Draw.new
    @shadow = Array.new
    @shadow << Magick::Draw.new
    @text_attrs = TextAttributes.new
    init_matrix()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methID, *args, &block) ⇒ Object



544
545
546
# File 'lib/rvg/misc.rb', line 544

def method_missing(methID, *args, &block)
    @gc.__send__(methID, *args, &block)
end

Instance Attribute Details

#gcObject (readonly)

Returns the value of attribute gc.



534
535
536
# File 'lib/rvg/misc.rb', line 534

def gc
  @gc
end

#text_attrsObject (readonly)

Returns the value of attribute text_attrs.



534
535
536
# File 'lib/rvg/misc.rb', line 534

def text_attrs
  @text_attrs
end

Class Method Details

.degrees_to_radians(deg) ⇒ Object



508
509
510
# File 'lib/rvg/misc.rb', line 508

def GraphicContext.degrees_to_radians(deg)
    Math::PI * (deg % 360.0) / 180.0
end

Instance Method Details

#affine(sx, rx, ry, sy, tx, ty) ⇒ Object



548
549
550
551
552
553
# File 'lib/rvg/misc.rb', line 548

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)
    @text_attrs.set_affine(sx, rx, ry, sy, tx, ty)
    nil
end

#baseline_shift(value) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
# File 'lib/rvg/misc.rb', line 555

def baseline_shift(value)
    @text_attrs.baseline_shift = case value
        when 'baseline', 'sub', 'super'
            value.intern
        when /[-+]?\d+%/, Numeric
            value
        else
            :baseline
        end
    nil
end

#font(name) ⇒ Object



567
568
569
570
571
# File 'lib/rvg/misc.rb', line 567

def font(name)
    @gc.font(name)
    @shadow[-1].font = name
    nil
end

#font_family(name) ⇒ Object



573
574
575
576
577
# File 'lib/rvg/misc.rb', line 573

def font_family(name)
    @gc.font_family(name)
    @shadow[-1].font_family = name
    nil
end

#font_size(points) ⇒ Object



579
580
581
582
583
# File 'lib/rvg/misc.rb', line 579

def font_size(points)
    @gc.font_size(points)
    @shadow[-1].pointsize = points
    nil
end

#font_stretch(stretch) ⇒ Object



585
586
587
588
589
590
# File 'lib/rvg/misc.rb', line 585

def font_stretch(stretch)
    stretch = FONT_STRETCH.fetch(stretch.intern, Magick::NormalStretch)
    @gc.font_stretch(stretch)
    @shadow[-1].font_stretch = stretch
    nil
end

#font_style(style) ⇒ Object



592
593
594
595
596
597
# File 'lib/rvg/misc.rb', line 592

def font_style(style)
    style = FONT_STYLE.fetch(style.intern, Magick::NormalStyle)
    @gc.font_style(style)
    @shadow[-1].font_style = style
    nil
end

#font_weight(weight) ⇒ Object



599
600
601
602
603
604
605
# File 'lib/rvg/misc.rb', line 599

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}
    @gc.font_weight(weight)
    @shadow[-1].font_weight = weight
    nil
end

#glyph_orientation_horizontal(deg) ⇒ Object



607
608
609
610
611
# File 'lib/rvg/misc.rb', line 607

def glyph_orientation_horizontal(deg)
    deg = Magick::RVG.convert_one_to_float(deg)
    @text_attrs.glyph_orientation_horizontal = (deg % 360) / 90 * 90
    nil
end

#glyph_orientation_vertical(deg) ⇒ Object



613
614
615
616
617
# File 'lib/rvg/misc.rb', line 613

def glyph_orientation_vertical(deg)
    deg = Magick::RVG.convert_one_to_float(deg)
    @text_attrs.glyph_orientation_vertical = (deg % 360) / 90 * 90
    nil
end

#inspectObject



619
620
621
# File 'lib/rvg/misc.rb', line 619

def inspect()
    @gc.inspect
end

#letter_spacing(value) ⇒ Object



623
624
625
626
# File 'lib/rvg/misc.rb', line 623

def letter_spacing(value)
    @text_attrs.letter_spacing = Magick::RVG.convert_one_to_float(value)
    nil
end

#popObject



635
636
637
638
639
640
# File 'lib/rvg/misc.rb', line 635

def pop()
    @gc.pop
    @shadow.pop
    @text_attrs.pop
    nil
end

#pushObject



628
629
630
631
632
633
# File 'lib/rvg/misc.rb', line 628

def push()
    @gc.push
    @shadow.push(@shadow.last.dup)
    @text_attrs.push
    nil
end

#rotate(degrees) ⇒ Object



642
643
644
645
646
647
648
649
650
651
# File 'lib/rvg/misc.rb', line 642

def rotate(degrees)
    degrees = Magick::RVG.convert_one_to_float(degrees)
    @gc.rotate(degrees)
    @sx =  Math.cos(GraphicContext.degrees_to_radians(degrees))
    @rx =  Math.sin(GraphicContext.degrees_to_radians(degrees))
    @ry = -Math.sin(GraphicContext.degrees_to_radians(degrees))
    @sy =  Math.cos(GraphicContext.degrees_to_radians(degrees))
    concat_matrix()
    nil
end

#scale(sx, sy) ⇒ Object



653
654
655
656
657
658
659
# File 'lib/rvg/misc.rb', line 653

def scale(sx, sy)
    sx, sy = Magick::RVG.convert_to_float(sx, sy)
    @gc.scale(sx, sy)
    @sx, @sy = sx, sy
    concat_matrix()
    nil
end

#shadowObject



661
662
663
# File 'lib/rvg/misc.rb', line 661

def shadow()
    @shadow.last
end

#skewX(degrees) ⇒ Object



665
666
667
668
669
670
671
# File 'lib/rvg/misc.rb', line 665

def skewX(degrees)
    degrees = Magick::RVG.convert_one_to_float(degrees)
    @gc.skewX(degrees)
    @ry = Math.tan(GraphicContext.degrees_to_radians(degrees))
    concat_matrix()
    nil
end

#skewY(degrees) ⇒ Object



673
674
675
676
677
678
679
# File 'lib/rvg/misc.rb', line 673

def skewY(degrees)
    degrees = Magick::RVG.convert_one_to_float(degrees)
    @gc.skewY(degrees)
    @rx = Math.tan(GraphicContext.degrees_to_radians(degrees))
    concat_matrix()
    nil
end

#stroke_width(width) ⇒ Object



681
682
683
684
685
686
# File 'lib/rvg/misc.rb', line 681

def stroke_width(width)
    width = Magick::RVG.convert_one_to_float(width)
    @gc.stroke_width(width)
    @shadow[-1].stroke_width = width
    nil
end

#text(x, y, text) ⇒ Object



688
689
690
691
692
693
694
695
696
697
# File 'lib/rvg/misc.rb', line 688

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 text_renderer.render(x, y, text)
end

#text_anchor(anchor) ⇒ Object



699
700
701
702
703
704
705
706
707
# File 'lib/rvg/misc.rb', line 699

def text_anchor(anchor)
    anchor = anchor.intern
    anchor_enum = TEXT_ANCHOR.fetch(anchor, Magick::StartAnchor)
    @gc.text_anchor(anchor_enum)
    align = ANCHOR_TO_ALIGN.fetch(anchor, Magick::LeftAlign)
    @shadow[-1].align = align
    @text_attrs.text_anchor = anchor
    nil
end

#text_decoration(decoration) ⇒ Object



709
710
711
712
713
714
# File 'lib/rvg/misc.rb', line 709

def text_decoration(decoration)
    decoration = TEXT_DECORATION.fetch(decoration.intern, Magick::NoDecoration)
    @gc.decorate(decoration)
    @shadow[-1].decorate = decoration
    nil
end

#translate(tx, ty) ⇒ Object



716
717
718
719
720
721
722
# File 'lib/rvg/misc.rb', line 716

def translate(tx, ty)
    tx, ty = Magick::RVG.convert_to_float(tx, ty)
    @gc.translate(tx, ty)
    @tx, @ty = tx, ty
    concat_matrix()
    nil
end

#word_spacing(value) ⇒ Object



724
725
726
727
# File 'lib/rvg/misc.rb', line 724

def word_spacing(value)
    @text_attrs.word_spacing = Magick::RVG.convert_one_to_float(value)
    nil
end

#writing_mode(mode) ⇒ Object



729
730
731
732
# File 'lib/rvg/misc.rb', line 729

def writing_mode(mode)
    @text_attrs.writing_mode = mode
    nil
end