设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1306|回复: 1
打印 上一主题 下一主题

[已经过期] Paragraph Formatter无法令中文说明文字自动换行的问题。

[复制链接]

Lv1.梦旅人

梦石
0
星屑
50
在线时间
10 小时
注册时间
2008-8-23
帖子
20
跳转到指定楼层
1
发表于 2011-12-4 00:59:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
继续是我拆的某英文游戏OTZ……

它依托RMXP开发,不过使用了似乎是RMVX基础的脚本Paragraph Formatter2.0。

英文状态时使用此脚本draw_paragraph时他会自动换行。但当我将道具说明文字改为中文之后换行就失效了。

代码如下:
  1. #==============================================================================
  2. #  Paragraph Formatter (VX)
  3. #  Version: 2.0
  4. #  Author: modern algebra (rmrk.net)
  5. #  Date: September 10, 2009
  6. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. #  Description:
  8. #    The idea behind this script is to easily separate a long string into a
  9. #   paragraph that fits in to the dimensions you specify. More than that, you
  10. #   can also justify the paragraph
  11. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  12. #  Instructions:
  13. #    For ease of use of people who are not neccesarily interested in writing
  14. #   their own algorithm, I have included a facade which you can use simply by
  15. #   this code:
  16. #
  17. #      bitmap.draw_paragraph (x, y, width, height, string)
  18. #
  19. #   where x & y are the x & y coordinates on the specified bitmap, and width
  20. #   and height are the maximum dimensions of the paragraph and string is the
  21. #   text you want to display in paragraph form. You can easily change which
  22. #   formatter or artist classes you want to use with the codes:
  23. #
  24. #      bitmap.paragraph_formatter = Paragrapher::<formatter_name>
  25. #      bitmap.paragraph_artist = Paragrapher::<artist_name
  26. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  27. #  How it works:
  28. #    The paragrapher expects two objects when initialized, a Formatter and an
  29. #   Artist. The idea  behind the formatter is that it is expected to take the
  30. #   initial specifications and convert it to a Formatted Text object. Then, the
  31. #   Artist class is expected to interpret the Formatted Text object and draw
  32. #   the paragraph. For details on how each specific algorithm works,  visit the
  33. #   comments above and inside them. It is not necessary to use the default
  34. #   Formatter, Artist, or Formatted Text objects.
  35. #==============================================================================

  36. #==============================================================================
  37. # ** Bitmap
  38. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  39. #  Summary of Changes:
  40. #    new attr_writer - paragraph_formatter, paragraph_artist
  41. #    new methods - paragraph_formatter, paragraph_artist, draw_paragraph
  42. #==============================================================================

  43. class Bitmap
  44.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45.   # * Public Instance Variables
  46.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47.   attr_writer :paragraph_formatter # The formatting class for Paragraphing
  48.   attr_writer :paragraph_artist    # The artist class for Paragraphing
  49.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50.   # * Get Formatter
  51.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52.   def paragraph_formatter
  53.     @paragraph_formatter = $game_system.default_formatter if @paragraph_formatter.nil?
  54.     return @paragraph_formatter.new
  55.   end
  56.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  57.   # * Get Artist
  58.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59.   def paragraph_artist
  60.     @paragraph_artist = $game_system.default_artist if @paragraph_artist.nil?
  61.     return @paragraph_artist.new
  62.   end
  63.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64.   # * The Facade, which uses default Formatter and Artist to draw the formatted text directly
  65.   #  to a bitmap, such as self.contents
  66.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67.   def draw_paragraph (x, y, max_width, max_height, string)
  68.     bitmap = Bitmap.new (max_width, max_height)
  69.     bitmap.font = self.font.dup
  70.     pg = Paragrapher.new (paragraph_formatter, paragraph_artist)
  71.     bitmap = pg.paragraph (string, bitmap)
  72.     blt (x, y, bitmap, bitmap.rect)
  73.     # Dispose of the proxy bitmap
  74.     bitmap.dispose
  75.   end
  76. end

  77. #==============================================================================
  78. # *** Paragrapher
  79. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  80. #  Module containing the objects for the Paragrapher
  81. #==============================================================================

  82. module Paragrapher
  83.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84.   # * New
  85.   #``````````````````````````````````````````````````````````````````````````
  86.   #  Allows the 'Paragrapher.new' command outside of the module to be used
  87.   # rather than having to use 'Paragrapher::Paragrapher.new'
  88.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89.   class << self
  90.     def new(*args, &block)
  91.       return Paragrapher.new(*args, &block)
  92.     end
  93.   end
  94.   
  95.   #==========================================================================
  96.   # ** Formatted_Text
  97.   #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  98.   #  Bundles together the result of a Formatter class
  99.   #==========================================================================
  100.   Formatted_Text = Struct.new (:lines, :blank_width, :bitmap)
  101.   
  102.   #==========================================================================
  103.   # ** Paragrapher
  104.   #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  105.   #  This struct has accessible attributes and can easily paragraph objects.
  106.   #==========================================================================
  107.   class Paragrapher < Struct.new (:formatter, :artist)
  108.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  109.     # * Paragraph
  110.     #    string         : the string to be broken into lines
  111.     #    specifications : the other arguments required for the Formatter
  112.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  113.     def paragraph(string, *specifications)
  114.       f = formatter.format (string, *specifications)
  115.       return artist.draw (f)
  116.     end
  117.   end
  118.   
  119.   #============================================================================
  120.   # ** Formatter
  121.   #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  122.   #  This class converts a string into a formatted text object
  123.   #============================================================================
  124.   
  125.   class Formatter
  126.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127.     # * Format
  128.     #    string         : the string to be formatted
  129.     #    specifications : the desired width of the paragraph, or a bitmap
  130.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  131.     def format (string, specifications)
  132.       @string = string
  133.       # Checks whether specifications is a bitmap or a number. It then sets
  134.       # max_width and f.bitmap accordingly
  135.       if specifications.is_a? (Bitmap)
  136.         bitmap = specifications
  137.         @max_width = specifications.width
  138.       elsif specifications.is_a? (Numeric)
  139.         @max_width = specifications
  140.         bitmap = Bitmap.new (@max_width, 32)
  141.       else
  142.         # Error Catching: Incorrect Specifications
  143.         f = format ('Specifications Error', Bitmap.new (200, 64))
  144.         p 'Specifications Error: Please Pass Numeric or Bitmap'
  145.         return f
  146.       end
  147.       # Initializes Formatted_Text object
  148.       @format_text = Formatted_Text.new ([], [], bitmap)
  149.       @line_break = 0
  150.       @last_word = 0
  151.       for i in [email protected]
  152.         format_character (i)
  153.       end
  154.       # Adds the last line to f.lines
  155.       @format_text.lines.push ( @string[@line_break, @string.size - @line_break].scan (/./) )
  156.       # Since the last line is drawn normally, blank_width should be 0
  157.       @format_text.blank_width.push (0)
  158.       height = @format_text.lines.size*Window_Base::WLH
  159.       @format_text.bitmap = Bitmap.new (@max_width, height) if specifications.is_a? (Numeric)
  160.       # Returns the Formatted_Text object
  161.       formatted_text = @format_text.dup
  162.       @format_text = nil
  163.       return formatted_text
  164.     end
  165.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166.     # * Format Character
  167.     #    i : index of position in the string
  168.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169.     def format_character (i)
  170.       character = @string[i, 1]
  171.       # If at the end of a word
  172.       if character == "\n" || character == " " || i == @string.size - 1
  173.         i += 1 if i == @string.size - 1 # Account for possible overlap at end
  174.         # If this word fits on the current line
  175.         substring = @string[@line_break, i - @line_break]
  176.         if @format_text.bitmap.text_size (substring).width > @max_width
  177.           next_line (@last_word)
  178.         end
  179.         if character == "\n"
  180.           next_line (i)
  181.           @format_text.blank_width[-1] = 0
  182.         end
  183.         @last_word = i
  184.       end
  185.     end
  186.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  187.     # * Proceed to Next Line
  188.     #    last_word : the index of the beginning of the previous word
  189.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190.     def next_line (last_word)
  191.       line = @string[@line_break, last_word - @line_break]
  192.       # Adds current line to f.lines
  193.       @format_text.lines.push ( line.scan (/./) )
  194.       # Calculates the blank space left to cover in the line
  195.       line_blank = @max_width - @format_text.bitmap.text_size(line).width
  196.       @format_text.blank_width.push (line_blank.to_f / (line.size.to_f - 1.0) )
  197.       # Keeps track of the position in the array of each line
  198.       @line_break = last_word + 1
  199.     end
  200.   end

  201.   #============================================================================
  202.   # ** Artist
  203.   #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  204.   #  Interprets a Formatted Text object and returns a bitmap of the paragraph
  205.   #============================================================================
  206.   
  207.   class Artist
  208.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  209.     # * Draw
  210.     #    f            : Formatted Text Object
  211.     #    justify_text : boolean value on whether to justify text
  212.     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213.     def draw (f, justify_text = true)
  214.       # Calculates the necessary distance between lines
  215.       line_distance = f.bitmap.height.to_f / f.lines.size.to_f
  216.       line_distance = [f.bitmap.font.size + 4, line_distance].min
  217.       # For all lines in the lines array
  218.       for i in 0...f.lines.size
  219.         blank_space = f.blank_width[i]
  220.         position = 0
  221.         # For all indices of the line array
  222.         for j in 0...f.lines[i].size
  223.           string = f.lines[i][j]
  224.           tw = f.bitmap.text_size (string).width
  225.           # Draws the string located at each index
  226.           f.bitmap.draw_text (position, line_distance*i, tw, line_distance, string)
  227.           # Keeps track of the position we are in in pixels
  228.           position += tw
  229.           position += blank_space if justify_text
  230.         end
  231.       end
  232.       return f.bitmap
  233.     end
  234.   end
  235. end

  236. #========================================================================
  237. # ** Game_System
  238. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  239. #  Summary of changes:
  240. #     new instance variables - default_formatter, default_artist
  241. #     aliased methods - initialize
  242. #========================================================================

  243. class Game_System
  244.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  245.   # * Public Instance Variables
  246.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  247.   attr_accessor :default_formatter
  248.   attr_accessor :default_artist
  249.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  250.   # * Object Initialization
  251.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  252.   alias ma_paragraph_formatter_init initialize
  253.   def initialize
  254.     # Run original method
  255.     ma_paragraph_formatter_init
  256.     # Initialize original default format and artist classes
  257.     @default_formatter = Paragrapher::Formatter
  258.     @default_artist = Paragrapher::Artist
  259.   end
  260. end
复制代码
看得我一头雾水不知道该改什么地方。

大概是要把def draw_paragraph (x, y, max_width, max_height, string)的定义哪里修正一下么?


另外被调用的时候的语句是这样的:
  1.     return if @last_index == self.index
  2.     @last_index = self.index
  3.     x = self.contents.width/2 + 32
  4.     w = self.contents.width-x
  5.     y = self.oy
  6.     h = self.contents.height
  7.         self.contents.fill_rect(Rect.new(x,oy,w,h), Color.new(0,0,0,0))
  8.     return if self.index == -1
  9.     @separator.visible = true
  10.     text = self.item == nil ? "" : self.item.description
  11.     self.contents.font.size = 16
  12.     self.contents.draw_text(x,oy,w, 32, "Discribe:")
  13.     self.contents.font.size = 13
  14.     self.contents.draw_paragraph(x,32+oy, w, h, text)
复制代码

Lv2.观梦者

(?????)

梦石
0
星屑
723
在线时间
1327 小时
注册时间
2011-7-18
帖子
3184

贵宾

2
发表于 2011-12-4 01:14:39 | 只看该作者
英文转中文的时候要修正很多东西 = = b
最好能附个可以测试的工程。

可以参考下我汉化过来的这个脚本:
http://rpg.blue/thread-211137-1-1.html
(原版需要翻墙 - - )
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-11-5 18:30

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表