fantasdic r384 - in trunk: . lib/fantasdic/sources lib/fantasdic/ui



Author: mblondel
Date: Thu Jan  8 12:56:13 2009
New Revision: 384
URL: http://svn.gnome.org/viewvc/fantasdic?rev=384&view=rev

Log:
    * lib/fantasdic/ui/result_text_view.rb: Support for image tags.
    * lib/fantasdic/sources/epwing_dictionary.rb: Support for images and gaiji.



Modified:
   trunk/ChangeLog
   trunk/lib/fantasdic/sources/epwing_dictionary.rb
   trunk/lib/fantasdic/ui/result_text_view.rb

Modified: trunk/lib/fantasdic/sources/epwing_dictionary.rb
==============================================================================
--- trunk/lib/fantasdic/sources/epwing_dictionary.rb	(original)
+++ trunk/lib/fantasdic/sources/epwing_dictionary.rb	Thu Jan  8 12:56:13 2009
@@ -17,6 +17,7 @@
 
 begin
     require "eb"
+    require "base64"
 rescue LoadError
 end
 
@@ -187,6 +188,15 @@
         end
     end
 
+    def get_narrow_font_size(height)
+        # height can be 16, 24, 30, 48
+        [height / 2, height, EB.const_get("FONT_#{height.to_s}")]
+    end
+
+    def get_wide_font_size(height)
+        [height, height, EB.const_get("FONT_#{height.to_s}")]
+    end
+
     def create_hookset
         h = EB::Hookset.new
 
@@ -195,11 +205,21 @@
         end
 
         h.register(EB::HOOK_WIDE_FONT) do |eb, argv|
-            "(?)"
+            code = argv[0]
+            w, h, fontcode = get_wide_font_size(16)
+            eb.fontcode = fontcode
+            raw = eb.get_widefont(code).to_bmp
+            b64 = Base64.encode64(raw).gsub("\n", "")            
+            "[img b64=\"#{b64}\" /]"
         end
 
         h.register(EB::HOOK_NARROW_FONT) do |eb, argv|
-            "(?)"
+            code = argv[0]
+            w, h, fontcode = get_narrow_font_size(16)
+            eb.fontcode = fontcode
+            raw = eb.get_narrowfont(code).to_bmp
+            b64 = Base64.encode64(raw).gsub("\n", "")  
+            "[img b64=\"#{b64}\" /]"
         end
 
         h.register(EB::HOOK_BEGIN_EMPHASIS) do |eb, argv|
@@ -262,13 +282,18 @@
             ""
         end
 
-        h.register(EB::HOOK_BEGIN_IN_COLOR_BMP) do |eb, argv|
-            ""
-        end if EB.const_defined?(:HOOK_BEGIN_IN_COLOR_BMP)
+        img_hook = Proc.new do |eb, argv|
+            eb.read_colorgraphic(EB::Position.new(argv[2], argv[3])) do |raw|
+                b64 = Base64.encode64(raw).gsub("\n", "")  
+                "[img b64=\"#{b64}\" /]"
+            end
+        end
 
-        h.register(EB::HOOK_BEGIN_IN_COLOR_JPEG) do |eb2,argv|
-            ""
-        end if EB.const_defined?(:HOOK_BEGIN_IN_COLOR_JPEG)
+        h.register(EB::HOOK_BEGIN_IN_COLOR_BMP, &img_hook) \
+            if EB.const_defined?(:HOOK_BEGIN_IN_COLOR_BMP)
+
+        h.register(EB::HOOK_BEGIN_IN_COLOR_JPEG, &img_hook) \
+            if EB.const_defined?(:HOOK_BEGIN_IN_COLOR_JPEG)
 
         h.register(EB::HOOK_BEGIN_WAVE) do |eb, argv|
             "(wave file ignored)"

Modified: trunk/lib/fantasdic/ui/result_text_view.rb
==============================================================================
--- trunk/lib/fantasdic/ui/result_text_view.rb	(original)
+++ trunk/lib/fantasdic/ui/result_text_view.rb	Thu Jan  8 12:56:13 2009
@@ -15,6 +15,8 @@
 #Âwith this program; if not, write to the Free Software Foundation, Inc.,
 #Â51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
+require "base64"
+
 module Fantasdic
 
 module UI
@@ -179,6 +181,21 @@
             insert(@iter, word, "link")
         end
 
+        def insert_img_src(src)
+            pixbuf = Gdk::Pixbuf.new(src)
+            insert(@iter, pixbuf)
+        end
+
+        def insert_img_b64(b64)
+            raw = Base64.decode64(b64)
+
+            loader = Gdk::PixbufLoader.new
+            loader.write(raw)
+            loader.close
+ 
+            insert(@iter, loader.pixbuf)
+        end
+
         def insert_definitions(definitions)
             @definitions = definitions
             last_db = ""
@@ -193,7 +210,67 @@
                 else
                     insert_header("\n__________\n")
                 end
-                insert_with_links(d.body.strip)
+                insert_all(d.body.strip)
+            end
+        end
+
+        # Insert with support for:
+        # - pango markup (pseudo html) 
+        # - images: [img src="..." /] or [img b64="..." /]
+        # - links: {reference}
+
+        LINK_REGEXP = /\{([\w\s\-]+)\}/
+        IMG_SRC_REGEXP = /\[img src="([^\"]+)" \/\]/
+        IMG_B64_REGEXP = /\[img b64="([a-zA-Z0-9\+\/\=]+)" \/\]/
+        
+        def insert_all(str)
+            link_pos, link_val = [LINK_REGEXP =~ str, $1]
+            img_src_pos, img_src_val = [IMG_SRC_REGEXP =~ str, $1]
+            img_b64_pos, img_b64_val = [IMG_B64_REGEXP =~ str, $1]
+
+            arr = [[link_pos, link_val, :link],
+                   [img_src_pos, img_src_val, :imgsrc],
+                   [img_b64_pos, img_b64_val, :imgb64]]
+
+            arr.sort! do |a, b|
+                # sort numbers in ascending order and give priority to
+                # numbers over nil
+                a = a[0]
+                b = b[0]
+                if not a and not b
+                    0
+                elsif not a and b
+                    1
+                elsif a and not b
+                    -1
+                else
+                    a <=> b
+                end  
+            end
+
+            if not link_pos and not img_src_pos and not img_b64_pos
+                insert_text(str)
+            else
+                pos, val, type = arr.first
+
+                # start_text [pattern] following_text
+
+                # start_text
+                insert_text(str.slice(0..pos-1)) unless pos == 0
+
+                case type
+                    when :link
+                        insert_link(val)
+                        length = val.length + 2
+                    when :imgsrc
+                        insert_img_src(val)
+                        length = val.length + 14
+                    when :imgb64
+                        insert_img_b64(val)
+                        length = val.length + 14
+                end
+                following_text = str.slice(pos+length..-1)
+                insert_all(following_text) if following_text
             end
         end
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]