Class: HexaPDF::Content::GraphicsState

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/content/graphics_state.rb

Overview

A GraphicsState object holds all the graphic control parameters needed for correct operation when parsing or creating a content stream with a Processor object.

While a content stream is parsed/created, operations may use the current parameters or modify them.

The device-dependent graphics state parameters have not been implemented!

See: PDF1.7 s8.4.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphicsState

Initializes the graphics state parameters to their default values.



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/hexapdf/content/graphics_state.rb', line 571

def initialize
  @ctm = TransformationMatrix.new
  @stroke_color = @fill_color =
    GlobalConfiguration.constantize('color_space.map', :DeviceGray).new.default_color
  @line_width = 1.0
  @line_cap_style = LineCapStyle::BUTT_CAP
  @line_join_style = LineJoinStyle::MITER_JOIN
  @miter_limit = 10.0
  @line_dash_pattern = LineDashPattern.new
  @rendering_intent = RenderingIntent::RELATIVE_COLORIMETRIC
  @stroke_adjustment = false
  @blend_mode = :Normal
  @soft_mask = :None
  @stroke_alpha = @fill_alpha = 1.0
  @alpha_source = false

  @tm = nil
  @tlm = nil
  @character_spacing = 0
  @word_spacing = 0
  @horizontal_scaling = 100
  @leading = 0
  @font = nil
  @font_size = 0
  @text_rendering_mode = TextRenderingMode::FILL
  @text_rise = 0
  @text_knockout = true

  @scaled_character_spacing = 0
  @scaled_word_spacing = 0
  @scaled_font_size = 0
  @scaled_horizontal_scaling = 1

  @stack = []
end

Instance Attribute Details

#alpha_sourceObject

A boolean specifying whether the current soft mask and alpha parameters should be interpreted as shape values or opacity values.



481
482
483
# File 'lib/hexapdf/content/graphics_state.rb', line 481

def alpha_source
  @alpha_source
end

#blend_modeObject

The current blend mode for the transparent imaging model.



467
468
469
# File 'lib/hexapdf/content/graphics_state.rb', line 467

def blend_mode
  @blend_mode
end

#character_spacingObject

The character spacing in unscaled text units.

It specifies the additional spacing used for the horizontal or vertical displacement of glyphs.



497
498
499
# File 'lib/hexapdf/content/graphics_state.rb', line 497

def character_spacing
  @character_spacing
end

#ctmObject

The current transformation matrix.



436
437
438
# File 'lib/hexapdf/content/graphics_state.rb', line 436

def ctm
  @ctm
end

#fill_alphaObject

The alpha constant for non-stroking operations in the transparent imaging model.



477
478
479
# File 'lib/hexapdf/content/graphics_state.rb', line 477

def fill_alpha
  @fill_alpha
end

#fill_colorObject

The current color used for all other (i.e. non-stroking) painting operations.



442
443
444
# File 'lib/hexapdf/content/graphics_state.rb', line 442

def fill_color
  @fill_color
end

#fontObject

The font for the text.



515
516
517
# File 'lib/hexapdf/content/graphics_state.rb', line 515

def font
  @font
end

#font_sizeObject

The font size.



518
519
520
# File 'lib/hexapdf/content/graphics_state.rb', line 518

def font_size
  @font_size
end

#horizontal_scalingObject

The horizontal text scaling.

The value specifies the percentage of the normal width that should be used.



507
508
509
# File 'lib/hexapdf/content/graphics_state.rb', line 507

def horizontal_scaling
  @horizontal_scaling
end

#leadingObject

The leading in unscaled text units.

It specifies the distance between the baselines of adjacent lines of text.



512
513
514
# File 'lib/hexapdf/content/graphics_state.rb', line 512

def leading
  @leading
end

#line_cap_styleObject

The current line cap style (for the available values see LineCapStyle).



448
449
450
# File 'lib/hexapdf/content/graphics_state.rb', line 448

def line_cap_style
  @line_cap_style
end

#line_dash_patternObject

The line dash pattern (see LineDashPattern).



457
458
459
# File 'lib/hexapdf/content/graphics_state.rb', line 457

def line_dash_pattern
  @line_dash_pattern
end

#line_join_styleObject

The current line join style (for the available values see LineJoinStyle).



451
452
453
# File 'lib/hexapdf/content/graphics_state.rb', line 451

def line_join_style
  @line_join_style
end

#line_widthObject

The current line width in user space units.



445
446
447
# File 'lib/hexapdf/content/graphics_state.rb', line 445

def line_width
  @line_width
end

#miter_limitObject

The maximum line length of mitered line joins for stroked paths.



454
455
456
# File 'lib/hexapdf/content/graphics_state.rb', line 454

def miter_limit
  @miter_limit
end

#rendering_intentObject

The rendering intent (only used for CIE-based colors; for the available values see RenderingIntent).



461
462
463
# File 'lib/hexapdf/content/graphics_state.rb', line 461

def rendering_intent
  @rendering_intent
end

#scaled_character_spacingObject (readonly)

The scaled character spacing used in glyph displacement calculations.

This returns the character spacing multiplied by #scaled_horizontal_scaling.

See PDF1.7 s9.4.4



544
545
546
# File 'lib/hexapdf/content/graphics_state.rb', line 544

def scaled_character_spacing
  @scaled_character_spacing
end

#scaled_font_sizeObject (readonly)

The scaled font size used in glyph displacement calculations.

This returns the font size multiplied by the scaling factor from glyph space to text space (0.001 for all fonts except Type3 fonts or the scaling specified in /FontMatrix for Type3 fonts) and multiplied by #scaled_horizontal_scaling.

See PDF1.7 s9.4.4, HexaPDF::Type::FontType3



560
561
562
# File 'lib/hexapdf/content/graphics_state.rb', line 560

def scaled_font_size
  @scaled_font_size
end

#scaled_horizontal_scalingObject (readonly)

The scaled horizontal scaling used in glyph displacement calculations.

Since the horizontal scaling attribute is stored in percent of 100, this method returns the correct value for calculations.

See PDF1.7 s9.4.4



568
569
570
# File 'lib/hexapdf/content/graphics_state.rb', line 568

def scaled_horizontal_scaling
  @scaled_horizontal_scaling
end

#scaled_word_spacingObject (readonly)

The scaled word spacing used in glyph displacement calculations.

This returns the word spacing multiplied by #scaled_horizontal_scaling.

See PDF1.7 s9.4.4



551
552
553
# File 'lib/hexapdf/content/graphics_state.rb', line 551

def scaled_word_spacing
  @scaled_word_spacing
end

#soft_maskObject

The soft mask specifying the mask shape or mask opacity value to be used in the transparent imaging model.



471
472
473
# File 'lib/hexapdf/content/graphics_state.rb', line 471

def soft_mask
  @soft_mask
end

#stroke_adjustmentObject

The stroke adjustment for very small line width.



464
465
466
# File 'lib/hexapdf/content/graphics_state.rb', line 464

def stroke_adjustment
  @stroke_adjustment
end

#stroke_alphaObject

The alpha constant for stroking operations in the transparent imaging model.



474
475
476
# File 'lib/hexapdf/content/graphics_state.rb', line 474

def stroke_alpha
  @stroke_alpha
end

#stroke_colorObject

The current color used for stroking operations during painting.



439
440
441
# File 'lib/hexapdf/content/graphics_state.rb', line 439

def stroke_color
  @stroke_color
end

#text_knockoutObject

The text knockout, a boolean value.

It specifies whether each glyph should be treated as separate elementary object for the purpose of color compositing in the transparent imaging model (knockout = false) or if all glyphs together are treated as one elementary object (knockout = true).



537
538
539
# File 'lib/hexapdf/content/graphics_state.rb', line 537

def text_knockout
  @text_knockout
end

#text_rendering_modeObject

The text rendering mode.

It determines if and how the glyphs of a text should be shown (for all available values see TextRenderingMode).



524
525
526
# File 'lib/hexapdf/content/graphics_state.rb', line 524

def text_rendering_mode
  @text_rendering_mode
end

#text_riseObject

The text rise distance in unscaled text units.

It specifies the distance that the baseline should be moved up or down from its default location.



530
531
532
# File 'lib/hexapdf/content/graphics_state.rb', line 530

def text_rise
  @text_rise
end

#tlmObject

The text line matrix which captures the state of the text matrix at the beginning of a line.

As with the text matrix the text line matrix is non-nil only when inside a text object.



491
492
493
# File 'lib/hexapdf/content/graphics_state.rb', line 491

def tlm
  @tlm
end

#tmObject

The text matrix.

This attribute is non-nil only when inside a text object.



486
487
488
# File 'lib/hexapdf/content/graphics_state.rb', line 486

def tm
  @tm
end

#word_spacingObject

The word spacing in unscaled text units.

It works like the character spacing but is only applied to the ASCII space character.



502
503
504
# File 'lib/hexapdf/content/graphics_state.rb', line 502

def word_spacing
  @word_spacing
end

Instance Method Details

#fill_color_spaceObject

:nodoc:



662
663
664
# File 'lib/hexapdf/content/graphics_state.rb', line 662

def fill_color_space
  @fill_color.color_space
end

#fill_color_space=(color_space) ⇒ Object

:nodoc:



666
667
668
# File 'lib/hexapdf/content/graphics_state.rb', line 666

def fill_color_space=(color_space) #:nodoc:
  self.fill_color = color_space.default_color
end

#restoreObject

Restores the graphics state from the internal stack.

Raises an error if the stack is empty.



623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/hexapdf/content/graphics_state.rb', line 623

def restore
  if @stack.empty?
    raise HexaPDF::Error, "Can't restore graphics state because the stack is empty"
  end
  @ctm, @stroke_color, @fill_color,
    @line_width, @line_cap_style, @line_join_style, @miter_limit, @line_dash_pattern,
    @rendering_intent, @stroke_adjustment, @blend_mode,
    @soft_mask, @stroke_alpha, @fill_alpha, @alpha_source,
    @character_spacing, @word_spacing, @horizontal_scaling, @leading,
    @font, @font_size, @text_rendering_mode, @text_rise, @text_knockout,
    @scaled_character_spacing, @scaled_word_spacing, @scaled_font_size,
    @scaled_horizontal_scaling = @stack.pop
end

#saveObject

Saves the current graphics state on the internal stack.



608
609
610
611
612
613
614
615
616
617
618
# File 'lib/hexapdf/content/graphics_state.rb', line 608

def save
  @stack.push([@ctm, @stroke_color, @fill_color,
               @line_width, @line_cap_style, @line_join_style, @miter_limit,
               @line_dash_pattern, @rendering_intent, @stroke_adjustment, @blend_mode,
               @soft_mask, @stroke_alpha, @fill_alpha, @alpha_source,
               @character_spacing, @word_spacing, @horizontal_scaling, @leading,
               @font, @font_size, @text_rendering_mode, @text_rise, @text_knockout,
               @scaled_character_spacing, @scaled_word_spacing, @scaled_font_size,
               @scaled_horizontal_scaling])
  @ctm = @ctm.dup
end

#saved_states?Boolean

Returns true if the internal stack of saved graphic states contains entries.

Returns:

  • (Boolean)


638
639
640
# File 'lib/hexapdf/content/graphics_state.rb', line 638

def saved_states?
  !@stack.empty?
end

#stroke_color_spaceObject

:nodoc:



648
649
650
# File 'lib/hexapdf/content/graphics_state.rb', line 648

def stroke_color_space
  @stroke_color.color_space
end

#stroke_color_space=(color_space) ⇒ Object

:nodoc:



652
653
654
# File 'lib/hexapdf/content/graphics_state.rb', line 652

def stroke_color_space=(color_space) # :nodoc:
  self.stroke_color = color_space.default_color
end