Project1
标题: Paragraph Formatter无法令中文说明文字自动换行的问题。 [打印本页]
作者: tinytime 时间: 2011-12-4 00:59
标题: Paragraph Formatter无法令中文说明文字自动换行的问题。
继续是我拆的某英文游戏OTZ……
它依托RMXP开发,不过使用了似乎是RMVX基础的脚本Paragraph Formatter2.0。
英文状态时使用此脚本draw_paragraph时他会自动换行。但当我将道具说明文字改为中文之后换行就失效了。
代码如下:- #==============================================================================
- # Paragraph Formatter (VX)
- # Version: 2.0
- # Author: modern algebra (rmrk.net)
- # Date: September 10, 2009
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # Description:
- # The idea behind this script is to easily separate a long string into a
- # paragraph that fits in to the dimensions you specify. More than that, you
- # can also justify the paragraph
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # Instructions:
- # For ease of use of people who are not neccesarily interested in writing
- # their own algorithm, I have included a facade which you can use simply by
- # this code:
- #
- # bitmap.draw_paragraph (x, y, width, height, string)
- #
- # where x & y are the x & y coordinates on the specified bitmap, and width
- # and height are the maximum dimensions of the paragraph and string is the
- # text you want to display in paragraph form. You can easily change which
- # formatter or artist classes you want to use with the codes:
- #
- # bitmap.paragraph_formatter = Paragrapher::<formatter_name>
- # bitmap.paragraph_artist = Paragrapher::<artist_name
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # How it works:
- # The paragrapher expects two objects when initialized, a Formatter and an
- # Artist. The idea behind the formatter is that it is expected to take the
- # initial specifications and convert it to a Formatted Text object. Then, the
- # Artist class is expected to interpret the Formatted Text object and draw
- # the paragraph. For details on how each specific algorithm works, visit the
- # comments above and inside them. It is not necessary to use the default
- # Formatter, Artist, or Formatted Text objects.
- #==============================================================================
- #==============================================================================
- # ** Bitmap
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # Summary of Changes:
- # new attr_writer - paragraph_formatter, paragraph_artist
- # new methods - paragraph_formatter, paragraph_artist, draw_paragraph
- #==============================================================================
- class Bitmap
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Public Instance Variables
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- attr_writer :paragraph_formatter # The formatting class for Paragraphing
- attr_writer :paragraph_artist # The artist class for Paragraphing
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Get Formatter
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def paragraph_formatter
- @paragraph_formatter = $game_system.default_formatter if @paragraph_formatter.nil?
- return @paragraph_formatter.new
- end
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Get Artist
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def paragraph_artist
- @paragraph_artist = $game_system.default_artist if @paragraph_artist.nil?
- return @paragraph_artist.new
- end
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * The Facade, which uses default Formatter and Artist to draw the formatted text directly
- # to a bitmap, such as self.contents
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def draw_paragraph (x, y, max_width, max_height, string)
- bitmap = Bitmap.new (max_width, max_height)
- bitmap.font = self.font.dup
- pg = Paragrapher.new (paragraph_formatter, paragraph_artist)
- bitmap = pg.paragraph (string, bitmap)
- blt (x, y, bitmap, bitmap.rect)
- # Dispose of the proxy bitmap
- bitmap.dispose
- end
- end
- #==============================================================================
- # *** Paragrapher
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # Module containing the objects for the Paragrapher
- #==============================================================================
- module Paragrapher
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * New
- #``````````````````````````````````````````````````````````````````````````
- # Allows the 'Paragrapher.new' command outside of the module to be used
- # rather than having to use 'Paragrapher::Paragrapher.new'
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- class << self
- def new(*args, &block)
- return Paragrapher.new(*args, &block)
- end
- end
-
- #==========================================================================
- # ** Formatted_Text
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # Bundles together the result of a Formatter class
- #==========================================================================
- Formatted_Text = Struct.new (:lines, :blank_width, :bitmap)
-
- #==========================================================================
- # ** Paragrapher
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # This struct has accessible attributes and can easily paragraph objects.
- #==========================================================================
- class Paragrapher < Struct.new (:formatter, :artist)
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Paragraph
- # string : the string to be broken into lines
- # specifications : the other arguments required for the Formatter
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def paragraph(string, *specifications)
- f = formatter.format (string, *specifications)
- return artist.draw (f)
- end
- end
-
- #============================================================================
- # ** Formatter
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # This class converts a string into a formatted text object
- #============================================================================
-
- class Formatter
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Format
- # string : the string to be formatted
- # specifications : the desired width of the paragraph, or a bitmap
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def format (string, specifications)
- @string = string
- # Checks whether specifications is a bitmap or a number. It then sets
- # max_width and f.bitmap accordingly
- if specifications.is_a? (Bitmap)
- bitmap = specifications
- @max_width = specifications.width
- elsif specifications.is_a? (Numeric)
- @max_width = specifications
- bitmap = Bitmap.new (@max_width, 32)
- else
- # Error Catching: Incorrect Specifications
- f = format ('Specifications Error', Bitmap.new (200, 64))
- p 'Specifications Error: Please Pass Numeric or Bitmap'
- return f
- end
- # Initializes Formatted_Text object
- @format_text = Formatted_Text.new ([], [], bitmap)
- @line_break = 0
- @last_word = 0
- for i in [email protected]
- format_character (i)
- end
- # Adds the last line to f.lines
- @format_text.lines.push ( @string[@line_break, @string.size - @line_break].scan (/./) )
- # Since the last line is drawn normally, blank_width should be 0
- @format_text.blank_width.push (0)
- height = @format_text.lines.size*Window_Base::WLH
- @format_text.bitmap = Bitmap.new (@max_width, height) if specifications.is_a? (Numeric)
- # Returns the Formatted_Text object
- formatted_text = @format_text.dup
- @format_text = nil
- return formatted_text
- end
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Format Character
- # i : index of position in the string
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def format_character (i)
- character = @string[i, 1]
- # If at the end of a word
- if character == "\n" || character == " " || i == @string.size - 1
- i += 1 if i == @string.size - 1 # Account for possible overlap at end
- # If this word fits on the current line
- substring = @string[@line_break, i - @line_break]
- if @format_text.bitmap.text_size (substring).width > @max_width
- next_line (@last_word)
- end
- if character == "\n"
- next_line (i)
- @format_text.blank_width[-1] = 0
- end
- @last_word = i
- end
- end
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Proceed to Next Line
- # last_word : the index of the beginning of the previous word
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def next_line (last_word)
- line = @string[@line_break, last_word - @line_break]
- # Adds current line to f.lines
- @format_text.lines.push ( line.scan (/./) )
- # Calculates the blank space left to cover in the line
- line_blank = @max_width - @format_text.bitmap.text_size(line).width
- @format_text.blank_width.push (line_blank.to_f / (line.size.to_f - 1.0) )
- # Keeps track of the position in the array of each line
- @line_break = last_word + 1
- end
- end
-
- #============================================================================
- # ** Artist
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # Interprets a Formatted Text object and returns a bitmap of the paragraph
- #============================================================================
-
- class Artist
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Draw
- # f : Formatted Text Object
- # justify_text : boolean value on whether to justify text
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def draw (f, justify_text = true)
- # Calculates the necessary distance between lines
- line_distance = f.bitmap.height.to_f / f.lines.size.to_f
- line_distance = [f.bitmap.font.size + 4, line_distance].min
- # For all lines in the lines array
- for i in 0...f.lines.size
- blank_space = f.blank_width[i]
- position = 0
- # For all indices of the line array
- for j in 0...f.lines[i].size
- string = f.lines[i][j]
- tw = f.bitmap.text_size (string).width
- # Draws the string located at each index
- f.bitmap.draw_text (position, line_distance*i, tw, line_distance, string)
- # Keeps track of the position we are in in pixels
- position += tw
- position += blank_space if justify_text
- end
- end
- return f.bitmap
- end
- end
- end
- #========================================================================
- # ** Game_System
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # Summary of changes:
- # new instance variables - default_formatter, default_artist
- # aliased methods - initialize
- #========================================================================
- class Game_System
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Public Instance Variables
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- attr_accessor :default_formatter
- attr_accessor :default_artist
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # * Object Initialization
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- alias ma_paragraph_formatter_init initialize
- def initialize
- # Run original method
- ma_paragraph_formatter_init
- # Initialize original default format and artist classes
- @default_formatter = Paragrapher::Formatter
- @default_artist = Paragrapher::Artist
- end
- end
复制代码 看得我一头雾水不知道该改什么地方。
大概是要把def draw_paragraph (x, y, max_width, max_height, string)的定义哪里修正一下么?
另外被调用的时候的语句是这样的:- return if @last_index == self.index
- @last_index = self.index
- x = self.contents.width/2 + 32
- w = self.contents.width-x
- y = self.oy
- h = self.contents.height
- self.contents.fill_rect(Rect.new(x,oy,w,h), Color.new(0,0,0,0))
- return if self.index == -1
- @separator.visible = true
- text = self.item == nil ? "" : self.item.description
- self.contents.font.size = 16
- self.contents.draw_text(x,oy,w, 32, "Discribe:")
- self.contents.font.size = 13
- self.contents.draw_paragraph(x,32+oy, w, h, text)
复制代码