#=================================================  ==============================================
#   Text Cache & Custom Font Junk Symbol Fix
#   Авторы: Mithran & Lone Wolf
#   Версия: 1.0
#   Тип: Исправление багов
#   Описание:
#   Исправляет кривое отображение текста и заменяет "квадратики" на пробелы.
#   Особенности:
#   Исправляет кривое отображение текст
#   Кэширует текстовые данные, улучшая производительность
#=================================================  ==============================================
class Bitmap
  TEXT_TOP_BUFFER = 2
  TEXT_SIDE_BUFFER = 8 # buffer in pixels to draw text away from 
  # the edge of the bitmap, to prevent certain characters from being cut off
  SIMPLE_FIX = false # just adds the two pixels to prevent unnecessary squeeze
  MAX_TEXT_DRAW_WIDTH = 640 # tests have shown the draw fails at around 640px
  # if nil, no max width
  NO_FIX = false # completely disables the fix, for testing comparison
  
  
  alias draw_text_vxa draw_text
  def draw_text(*args)
    return draw_text_vxa(*args) if NO_FIX
    if args[0].is_a?(Rect)
      rect = args[0]
      x, y, width, height = rect.x, rect.y, rect.width, rect.height
      text = args[1].to_s.clone || ""
      align = args[2] || 0
    else
      x, y, width, height = *args[0..3]
      text = args[4].to_s.clone || ""
      align = args[5] || 0
    end
    text_rect = self.text_size(text)
    text_width = text_rect.width
    text_height = text_rect.height
    squeeze = text_width > width
    if SIMPLE_FIX or (squeeze and (MAX_TEXT_DRAW_WIDTH and width <= MAX_TEXT_DRAW_WIDTH))
      x -= align
      # shift one pixels to the left if centering 
      # two if right right justified
      # to offset the extra width given
      return draw_text_vxa(x, y, width + 2, height, text, align)
    else
      # TextCache.canvas(font) # passing the font slows this down extremly, changed it to later
      fontkey = self.font.to_a
      case align
      when 1; x += (width - text_width) / 2
      when 2; x += width - text_width
      end
      y += (height - text_height) / 2 # horizontal center
      buf = -TEXT_SIDE_BUFFER
      text.each_char { |char|
        letter = TextCache.letters(fontkey, char)
        draw_text_vxa(x + buf, y, letter.rect.width + 2, letter.height, char) if SIMPLE_FIX
        # swap with original method for debugging and simple fix
        self.blt(x + buf, y, letter, letter.rect) unless SIMPLE_FIX
        buf += letter.rect.width - TEXT_SIDE_BUFFER * 2
      }      
    end
  end
end
module TextCache
  BUFFER_DRAW = 300 # for drawing characters, to make sure there is enough room
  
  def self.canvas(font = nil)
    @canvas = Bitmap.new(32, 32) if @canvas.nil? || @canvas.disposed?
    #@canvas.font = font if font and font != @canvas.font
    @canvas
  end
  
  def self.letters(font, char)
    @cache ||= {}
    key = font + [char]
    if include?(key)
      return @cache[key]
    elsif char.empty?
      return empty_bitmap
    else
      return new_letter(font, char)
    end
  end
  
  def self.empty_bitmap # not used, added for completness in case the cache is accessed directly
    @cache[:empty] = Bitmap.new(32, 32) unless include?(:empty)
    @cache[:empty]
  end 
  
  def self.new_letter(fontary, char)
    font = create_font(fontary)
    # get the font
    canvas.font = font
    rect = canvas.text_size(char * 3) 
    # get size of character between two other characters (for better kerning)
    b = Bitmap.new((rect.width / 3) + Bitmap::TEXT_SIDE_BUFFER * 2, rect.height)
    # create bitmap just big enough for one character
    b.font = font
    # get the font
    b.draw_text_vxa(rect.x - b.text_size(" ").width + Bitmap::TEXT_SIDE_BUFFER, rect.y - Bitmap::TEXT_TOP_BUFFER, BUFFER_DRAW, rect.height + Bitmap::TEXT_TOP_BUFFER * 2, " #{char} ", 0)
    # draw blank spaces before and after character, fix for cutting off the 
    # first pixel using draw_text
    key = fontary + [char]
    @cache[key] = b    
  end
  
  def self.create_font(fontary)
    font = Font.new(*fontary[0..1])
    font.bold = fontary[2]
    font.italic = fontary[3]
    font.outline = fontary[4]
    font.shadow = fontary[5]
    font.color.set(*fontary[6..9])
    font.out_color.set(*fontary[10..13])
    font
  end
  
  def self.include?(key)
    @cache[key] && !@cache[key].disposed?
  end
  def self.clear
    @cache ||= {}
    @cache.clear
    GC.start
  end
  
end
class Window_Base
  alias 

rocess_normal_character_vxa 

rocess_normal_character
  def process_normal_character(c, pos)
    return unless c >= ' '
    process_normal_character_vxa(c, pos)
  end
end
class Font
  # font's instance variables are not reflective, so this has to be defined explicitly
  def to_a
    [name, size, bold, italic, outline, shadow, color.red, color.green, color.blue, color.alpha, out_color.red, out_color.green, out_color.blue, out_color.alpha]
  end
  
end
			
		
 
	
Социальные закладки