/vendor/gems/prawn-core-0.8.4/lib/prawn/graphics/transparency.rb

https://github.com/darrikmazey/finance · Ruby · 99 lines · 39 code · 13 blank · 47 comment · 2 complexity · c50b0da43bf23f58fb1e2a7dbd846674 MD5 · raw file

  1. # encoding: utf-8
  2. #
  3. # transparency.rb : Implements transparency
  4. #
  5. # Copyright October 2009, Daniel Nelson. All Rights Reserved.
  6. #
  7. # This is free software. Please see the LICENSE and COPYING files for details.
  8. #
  9. module Prawn
  10. module Graphics
  11. # The Prawn::Transparency module is used to place transparent
  12. # content on the page. It has the capacity for separate
  13. # transparency values for stroked content and all other content.
  14. #
  15. # Example:
  16. # # both the fill and stroke will be at 50% opacity
  17. # pdf.transparent(0.5) do
  18. # pdf.text("hello world")
  19. # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
  20. # end
  21. #
  22. # # the fill will be at 50% opacity, but the stroke will
  23. # # be at 75% opacity
  24. # pdf.transparent(0.5, 0.75) do
  25. # pdf.text("hello world")
  26. # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
  27. # end
  28. #
  29. module Transparency
  30. # Sets the <tt>opacity</tt> and <tt>stroke_opacity</tt> for all
  31. # the content within the <tt>block</tt>
  32. # If <tt>stroke_opacity</tt> is not provided, then it takes on
  33. # the same value as <tt>opacity</tt>
  34. #
  35. # Valid ranges for both paramters are 0.0 to 1.0
  36. #
  37. # Example:
  38. # # both the fill and stroke will be at 50% opacity
  39. # pdf.transparent(0.5) do
  40. # pdf.text("hello world")
  41. # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
  42. # end
  43. #
  44. # # the fill will be at 50% opacity, but the stroke will
  45. # # be at 75% opacity
  46. # pdf.transparent(0.5, 0.75) do
  47. # pdf.text("hello world")
  48. # pdf.fill_and_stroke_circle_at([x, y], :radius => 25)
  49. # end
  50. #
  51. def transparent(opacity, stroke_opacity=opacity, &block)
  52. min_version(1.4)
  53. opacity = [[opacity, 0.0].max, 1.0].min
  54. stroke_opacity = [[stroke_opacity, 0.0].max, 1.0].min
  55. save_graphics_state
  56. add_content "/#{opacity_dictionary_name(opacity, stroke_opacity)} gs"
  57. yield if block_given?
  58. restore_graphics_state
  59. end
  60. private
  61. def opacity_dictionary_registry
  62. @opacity_dictionary_registry ||= {}
  63. end
  64. def next_opacity_dictionary_id
  65. opacity_dictionary_registry.length + 1
  66. end
  67. def opacity_dictionary_name(opacity, stroke_opacity)
  68. key = "#{opacity}_#{stroke_opacity}"
  69. if opacity_dictionary_registry[key]
  70. dictionary = opacity_dictionary_registry[key][:obj]
  71. dictionary_name = opacity_dictionary_registry[key][:name]
  72. else
  73. dictionary = ref!(:Type => :ExtGState,
  74. :CA => stroke_opacity,
  75. :ca => opacity
  76. )
  77. dictionary_name = "Tr#{next_opacity_dictionary_id}"
  78. opacity_dictionary_registry[key] = { :name => dictionary_name,
  79. :obj => dictionary }
  80. end
  81. page.ext_gstates.merge!(dictionary_name => dictionary)
  82. dictionary_name
  83. end
  84. end
  85. end
  86. end