[ruby-gnome2-doc-cvs] [Ruby-GNOME2 Project Website] update - tut-gtk2-dancr-rbcatut

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2013年 3月 29日 (金) 22:00:35 JST


-------------------------
REMOTE_ADDR = 70.49.48.128
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-dancr-rbcatut
-------------------------
@@ -614,34 +614,94 @@
 
     The ((<cairo_mask()|URL:http://www.cairographics.org/manual/cairo-cairo-t.html#cairo-mask>)) and ((<cairo_mask_surface()|URL:http://www.cairographics.org/manual/cairo-cairo-t.html#cairo-mask-surface>)) operations allow transfer according to the transparency/opacity of a second source pattern or surface. Where the pattern or surface is opaque, the current source is transferred to the destination. Where the pattern or surface is transparent, nothing is transferred.
 
-    :Ruby Cairo Library Fails To Support Gradients
-        (12.3.1.2.5.A1){{br}}
+    :Ruby Gtk-Cairo Library Fails To Support Gradient Patterns
+    (12.3.1.2.5.A1){{br}}
+    However, as mentioned in section 12.3.0.2 ((<Running Cairo Code From Non-Gtk Ruby Environment|tut-gtk2-dancr-rbcatut#Running Cairo Code From Non-Gtk Ruby Environment>)), we may go around this restriction, if we resort to pure Cairo library. Note, that you have to((*require 'cairo'*)) explicitly, unless you use our 'hiki2-gtk-w-cairo.rb' module which unlike the((*'gtk2'*)) requires it for you. Pay attention to how we create a non-GUI cairo context, how the size of the image is identical to our scale. Also, since we are not using the Gtk, at the end we have to make sure our 'art' work can be viewed - we save our cairo drawing surface into a file on the disk.
 
-        {{br}}{{image_right("1203-p08-mask-in-C.png")}}
-         # Your code goes between the two dashed lines:
-         # -- your code - start ----------------------------------------- -s-
+     Following is the complete example program using Cairo library from a non-Gtk environment: 
+
+    {{br}}{{image_right("1203-p08-mask-in-C.png")}}
+     #!/usr/bin/env ruby
+     require 'cairo'
+
+     # Create cairo context for image surface
+     surface = Cairo::ImageSurface.new(120, 120)
+     cr = Cairo::Context.new(surface)
+     cr.scale(120, 120)
 
-         linearg = Cairo.pattern_create_linear(0, 0, 1, 1)
-         linearg.add_color_stop_rgb(0, 0, 0.3, 0.8)
-         linearg.add_color_stop_rgb(1, 0, 0.8, 0.3)
+     # Set the gradient patterns
+     linearg = Cairo::LinearPattern.new(0, 0, 1, 1)
+     linearg.add_color_stop_rgb(0,    0, 0.3, 0.8)
+     linearg.add_color_stop_rgb(1,    0, 0.8, 0.3)
+     radialg = Cairo::RadialPattern.new(0.5, 0.5, 0.25, 0.5, 0.5, 0.75)
+     radialg.add_color_stop_rgba(0,   0, 0,   0,    1)
+     radialg.add_color_stop_rgba(0.5, 0, 0,   0,    0)
      
-         radialg = Cairo.pattern_create_radial(0.5, 0.5, 0.25, 0.5, 0.5, 0.75)
-         radialg.add_color_stop_rgba(0, 0, 0, 0, 1)
-         radialg.add_color_stop_rgba(0.5, 0, 0, 0, 0)
+     cr.set_source(linearg)
+     cr.mask(radialg)
      
+     cr.target.write_to_png("mask-cairo-no-gtk.png")
+     puts "DEBUG: cr.target.class=#{cr.target.class}"	#=> Cairo::ImageSurface
+
+
+    {{br}}Let's look at a Gtk version of the same program.
+
+    As mentioned at the beginning of this paragraph, if you use our 'hiki2-gtk-w-cairo.rb' module it includes the needed ((*require 'cairo'*)) declarative, hence, you should now have access to otherwise unsupported Cairo functionality even from your Gtk programs. Following is the above program, run from within the Ruby Gtk main-loop, indeed, I had to change it just a little bit, so we can learn few new things. Most importantly, you have to be aware of the scale. Here, we are now using the default scale ((*(cr.scale(1,1)*)), which requires that we use pixel, rather than relative coordinates (see:  section 12.3.1.2.1.A1 ((<Scale|tut-gtk2-dancr-rbcatut#Scale>)).)
+
+    {{br}}{{image_right("1203-p08-mask-in-gtkCairo.png")}}
+     #!/usr/bin/env ruby
+     $: << '~/work/HikiLib'
+     require 'hiki2-gtk-w-cairo.rb'
+     include HikiGtk
+     class MaskDemo < CairoWindow
+     
+       def draw(cr, da)
+         #width, height = da.window.size
+         # ------------------- clone: drawing surface creation ---- -s-
+         # Frame what would be the drawing area, if image_surface_create 
+         # were available in Ruby:
+         # cr.scale(120, 120)	# NOTE: Using default scale: scale(1,1)
+         cr.line_width = 1
+         cr.set_source_rgb(1, 0, 0)
+         cr.rectangle(0, 0, 100, 100)
+         cr.stroke
+         # ------------------- clone: drawing surface creation ---- -e-
+     
+         # Your code goes between the two dashed lines:
+         # -- your code - start ----------------------------------------- -s-
+         # Draw a shape to be effected by the gradient patterns below 
+         cr.line_width = 2
+         cr.set_source_rgb(0, 0.5, 0)
+         cr.rectangle(15, 15, 70, 70)
+         cr.stroke
+         # Set the gradient patterns
+         linearg = Cairo::LinearPattern.new(0, 0, 100, 100)
+         linearg.add_color_stop_rgb(0,    0, 0.3, 0.8)
+         linearg.add_color_stop_rgb(1,    0, 0.8, 0.3)
+         radialg = Cairo::RadialPattern.new(50,   50,  25,   50,  50,  75)
+         radialg.add_color_stop_rgba(0,   0, 0,   0,    1)
+         radialg.add_color_stop_rgba(0.5, 0, 0,   0,    0)
          cr.set_source(linearg)
          cr.mask(radialg)
          # -- your code - end ------------------------------------------- -e-
+       end
+     end # // EndOf class MaskDemo
+     
+     window = MaskDemo.new("Mask Tut. Example")
+     window.show_all
+     Gtk.main
+{{br}}
+{{br}}
+
+
+
+
 
-        The above code in Ruby Cairo library is completely unsupported. While "Cairo.pattern_create_linear" and "Cairo.pattern_create_radial", nor "Cairo::Context#add_color_stop_rgb/rgba" and "Cairo::Context#add_color_stop_rgb/rgba" do not exist at all, "Cairo::Context#set_source" and "Cairo::Context#mask" are not adequately documented, most likely because their parameters also are nowhere explained nor mentioned except in error messages. Namely, the line "cr.set_source(linearg)" would produce the following error message: ((*"`set_source': not a cairo pattern"*)), and the line "cr.mask(radialg)" blows up with: ((*"`mask': not a cairo surface".*))
 
-        {{br}}However, it is possible to use pure Ruby Cairo Library by requiring Cairo module (((*require 'cairo'*))), and create the missing methods. We will look at that shortly.
 
 
 
 
-{{br}}
-{{br}}
 
 == Drawing with Cairo
 (12.3.2){{br}}




ruby-gnome2-cvs メーリングリストの案内
Back to archive index