[Groonga-commit] ranguba/chupa-text at e9a6383 [master] Support screenshot size change

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Jul 6 15:06:07 JST 2017


Kouhei Sutou	2017-07-06 15:06:07 +0900 (Thu, 06 Jul 2017)

  New Revision: e9a6383bb0d7d32f23618bf49e5fd77e501d6150
  https://github.com/ranguba/chupa-text/commit/e9a6383bb0d7d32f23618bf49e5fd77e501d6150

  Message:
    Support screenshot size change

  Modified files:
    lib/chupa-text/command/chupa-text.rb
    lib/chupa-text/data.rb
    lib/chupa-text/decomposers/csv.rb
    test/command/test-chupa-text.rb

  Modified: lib/chupa-text/command/chupa-text.rb (+18 -0)
===================================================================
--- lib/chupa-text/command/chupa-text.rb    2017-07-06 14:13:36 +0900 (fc766f7)
+++ lib/chupa-text/command/chupa-text.rb    2017-07-06 15:06:07 +0900 (984660b)
@@ -28,12 +28,24 @@ module ChupaText
 
       AVAILABLE_FORMATS = [:json, :text]
 
+      SIZE = /\A\d+x\d+\z/o
+      OptionParser.accept(SIZE, SIZE) do |value|
+        if value
+           begin
+             value.split("x").collect {|number| Integer(number)}
+           rescue ArgumentError
+             raise OptionParser::InvalidArgument, value
+           end
+        end
+      end
+
       def initialize
         @input = nil
         @configuration = Configuration.load_default
         @enable_gems = true
         @format = :json
         @need_screenshot = true
+        @expected_screenshot_size = [200, 200]
       end
 
       def run(*arguments)
@@ -107,6 +119,11 @@ module ChupaText
                   "(default: #{@need_screenshot})") do |boolean|
           @need_screenshot = boolean
         end
+        parser.on("--expected-screenshot-size=WIDTHxHEIGHT", SIZE,
+                  "Expected screenshot size.",
+                  "(default: #{@expected_screenshot_size.join("x")})") do |size|
+          @expected_screenshot_size = size
+        end
 
         parser.separator("")
         parser.separator("Log related options:")
@@ -172,6 +189,7 @@ module ChupaText
           data = InputData.new(input)
         end
         data.need_screenshot = @need_screenshot
+        data.expected_screenshot_size = @expected_screenshot_size
         data
       end
 

  Modified: lib/chupa-text/data.rb (+5 -0)
===================================================================
--- lib/chupa-text/data.rb    2017-07-06 14:13:36 +0900 (ce269d4)
+++ lib/chupa-text/data.rb    2017-07-06 15:06:07 +0900 (702549c)
@@ -60,6 +60,9 @@ module ChupaText
     # @return [Bool] the specified value
     attr_writer :need_screenshot
 
+    # @return [Array<Integer, Integer>] the expected screenshot size.
+    attr_accessor :expected_screenshot_size
+
     def initialize(options={})
       @uri = nil
       @body = nil
@@ -70,6 +73,7 @@ module ChupaText
       @source = nil
       @screenshot = nil
       @need_screenshot = true
+      @expected_screenshot_size = [200, 200]
       @options = options || {}
       source_data = @options[:source_data]
       if source_data
@@ -100,6 +104,7 @@ module ChupaText
         self["source-mime-types"].unshift(data.mime_type)
       end
       self.need_screenshot = data.need_screenshot?
+      self.expected_screenshot_size = data.expected_screenshot_size
     end
 
     # @param [String, URI, nil] uri The URI for the data. If `uri` is

  Modified: lib/chupa-text/decomposers/csv.rb (+11 -8)
===================================================================
--- lib/chupa-text/decomposers/csv.rb    2017-07-06 14:13:36 +0900 (d37acfc)
+++ lib/chupa-text/decomposers/csv.rb    2017-07-06 15:06:07 +0900 (07ebba5)
@@ -45,31 +45,34 @@ module ChupaText
 
         text_data = TextData.new(text, :source_data => data)
         if data.need_screenshot?
-          text_data.screenshot = create_screenshot(text)
+          text_data.screenshot = create_screenshot(data, text)
         end
 
         yield(text_data)
       end
 
       private
-      def create_screenshot(text)
+      def create_screenshot(data, text)
+        width, height = data.expected_screenshot_size
+        max_n_lines = 10
+        font_size = height / max_n_lines
         target_text = ""
         text.each_line.with_index do |line, i|
+          break if i == max_n_lines
           target_text << line
-          break if i == 4
         end
         mime_type = "image/svg+xml"
         data = <<-SVG
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <svg
   xmlns="http://www.w3.org/2000/svg"
-  width="100"
-  height="100"
-  viewBox="0 0 100 100">
+  width="#{width}"
+  height="#{height}"
+  viewBox="0 0 #{width} #{height}">
   <text
     x="0"
-    y="10"
-    style="font-size: 10px; white-space: pre-wrap;"
+    y="#{font_size}"
+    style="font-size: #{font_size}px; white-space: pre-wrap;"
     xml:space="preserve">#{CGI.escapeHTML(target_text)}</text>
 </svg>
         SVG

  Modified: test/command/test-chupa-text.rb (+5 -5)
===================================================================
--- test/command/test-chupa-text.rb    2017-07-06 14:13:36 +0900 (4afd7a2)
+++ test/command/test-chupa-text.rb    2017-07-06 15:06:07 +0900 (b99d868)
@@ -209,13 +209,13 @@ class TestCommandChupaText < Test::Unit::TestCase
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <svg
   xmlns="http://www.w3.org/2000/svg"
-  width="100"
-  height="100"
-  viewBox="0 0 100 100">
+  width="200"
+  height="200"
+  viewBox="0 0 200 200">
   <text
     x="0"
-    y="10"
-    style="font-size: 10px; white-space: pre-wrap;"
+    y="20"
+    style="font-size: 20px; white-space: pre-wrap;"
     xml:space="preserve">1 2 3
 4 5 6
 7 8 9
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index