Advertisement
neonblack

Mono-Spaced Bitmap Text v0.5

Oct 19th, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.07 KB | None | 0 0
  1. Font.default_name = "Typeset"
  2.  
  3. class Color ## Do not edit this module.
  4.   def s_ary; [self.red, self.green, self.blue]; end
  5.   def l_ary; [self.red, self.green, self.blue, self.alpha]; end
  6.  
  7.   def same?(color)
  8.     return false unless color.is_a?(Color)
  9.     self.s_ary == color.s_ary
  10.   end
  11.  
  12.   def match?(color)
  13.     return false unless color.is_a?(Color)
  14.     self.l_ary == color.l_ary
  15.   end
  16.  
  17.   def trans?
  18.     return self.alpha <= 0
  19.   end
  20. end
  21.  
  22.  
  23. module Typeset
  24.   ## Names of the bitmap fonts.  A font name must be set to one of these for it
  25.   ## to display as a bitmap.
  26.   Names = ["Typeset", "Typebat"]
  27.  
  28.   ## All the letters in the bitmap file.
  29.   Letters = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
  30.  
  31.   ## The colours to replace with the new colour.  Alpha is not included.
  32.   ## Inner replaces font.color while Outer replaces font.out_color.
  33.   Inner = Color.new(255, 255, 255)
  34.   Outer = Color.new(0, 0, 0)
  35.  
  36.   ## Rows in the bitmap images stacked top to bottom.
  37.   Rows = 8
  38.  
  39.   ## Choose to preload graphics.  If set to true all 32 normal window colours
  40.   ## will pre-load before the title screen.  In this case you can use a loading
  41.   ## screen image from the Graphics/System folder.  If you do not want to use
  42.   ## a loading screen, set LoadingScreen = nil.
  43.   Preload = false
  44.   LoadingScreen = "LoadingScreen"
  45.  
  46. ##-----------------------------------------------------------------------------
  47.  
  48.   @@colors = {}
  49.  
  50.   def self.get_default_colors
  51.     show_loading_screen if LoadingScreen
  52.     for name in Names
  53.       32.times do |i|
  54.         color = windowskin.get_pixel(64 + (i % 8) * 8, 96 + (i / 8) * 8)
  55.         add_color(name, color, Font.default_out_color,
  56.                   Font.default_outline ? 255 : 0)
  57.       end
  58.     end
  59.     fade_loading_screen if @loading_sprite
  60.   end
  61.  
  62.   def self.show_loading_screen
  63.     @loading_sprite = Sprite.new
  64.     Graphics.fadeout(0)
  65.     @loading_sprite.bitmap = Cache.system(LoadingScreen)
  66.     Graphics.fadein(30)
  67.   end
  68.  
  69.   def self.fade_loading_screen
  70.     Graphics.fadeout(30)
  71.     @loading_sprite.dispose
  72.   end
  73.  
  74.   def self.first_color
  75.     key = @@colors.key(@@colors.values[0])
  76.     if @@colors[key].disposed?
  77.       get_default_colors
  78.     end
  79.     return @@colors.values[0]
  80.   end
  81.  
  82.   def self.check_color(name, color, out = Color.new(0,0,0), outline = 255)
  83.     c1 = color.is_a?(Color) ? color.s_ary : color
  84.     c2 = out.is_a?(Color)   ? out.s_ary   : out
  85.     key = [name, c1, c2, outline]
  86.     @@colors.include?(key) && !@@colors[key].disposed?
  87.   end
  88.  
  89.   def self.add_color(name, color, out = Color.new(0,0,0), outline = 255)
  90.     return if check_color(name, color, out, outline)
  91.     c1 = color.is_a?(Color) ? color.s_ary : color
  92.     c2 = out.is_a?(Color)   ? out.s_ary   : out
  93.     source = Cache.system(name) rescue return
  94.     bitmap = Bitmap.new(source.width, source.height)
  95.     bitmap.blt(0, 0, source, source.rect)
  96.     bitmap.height.times do |y|
  97.       sc = sw = nil
  98.       bitmap.width.times do |x|
  99.         pixel = bitmap.get_pixel(x, y)
  100.         if sc
  101.           if sc.match?(pixel)
  102.             sw += 1
  103.             next
  104.           else
  105.             if sc.same?(Inner)
  106.               bitmap.fill_rect(x-sw, y, sw, 1, Color.new(*c1, sc.alpha))
  107.             elsif sc.same?(Outer)
  108.               al = sc.alpha * outline / 255
  109.               bitmap.fill_rect(x-sw, y, sw, 1, Color.new(*c2, al))
  110.             end
  111.             sc = nil
  112.           end
  113.         elsif pixel.trans?
  114.           next
  115.         end
  116.         if pixel.same?(Inner) || pixel.same?(Outer)
  117.           sw = 1
  118.           sc = pixel
  119.         end
  120.       end
  121.       x = bitmap.width
  122.       if sc && sc.same?(Inner)
  123.         bitmap.fill_rect(x-sw, y, sw, 1, Color.new(*c1, sc.alpha))
  124.       elsif sc && sc.same?(Outer)
  125.         al = sc.alpha * outline / 255 if sc
  126.         bitmap.fill_rect(x-sw, y, sw, 1, Color.new(*c2, al))
  127.       end
  128.     end
  129.     @@colors[[name, c1, c2, outline]] = bitmap
  130.   end
  131.  
  132.   def self.windowskin
  133.     Cache.system("Window")
  134.   end
  135.  
  136.   def self.get_typeset(name, color, out = Color.new(0,0,0), outline = 255)
  137.     add_color(name, color, out, outline)
  138.     color_by_key(name, color.s_ary, out.s_ary, outline)
  139.   end
  140.  
  141.   def self.default_size(key = nil)
  142.     if key
  143.       color_by_key(key[0], key[1].s_ary, key[2].s_ary, key[3]).height / Rows
  144.     else
  145.       @default_size ||= first_color.height / Rows
  146.       @default_size
  147.     end
  148.   end
  149.  
  150.   def self.default_width(key = nil)
  151.     if key
  152.       color_by_key(key[0], key[1].s_ary, key[2].s_ary, key[3]).width / 12
  153.     else
  154.       first_color.width / 12
  155.     end
  156.   end
  157.  
  158.   def self.get_letterbox(size, key = nil)
  159.     fs = default_size(key)
  160.     fw = (default_width(key) * (size.to_f / fs)).round
  161.     fh = (fs * (size.to_f / fs)).round
  162.     [fw, fh]
  163.   end
  164.  
  165.   def self.get_pos(c)
  166.     Letters.index(c) || 0
  167.   end
  168.  
  169.   def self.color_by_key(name, c1, c2, out)
  170.     c1 = c1.is_a?(Color) ? c1.s_ary : c1
  171.     c2 = c2.is_a?(Color) ? c2.s_ary : c2
  172.     add_color(name, c1, c2, out) unless check_color(name, c1, c2, out)
  173.     @@colors[[name, c1, c2, out]]
  174.   end
  175.  
  176.   get_default_colors if Preload
  177. end
  178.  
  179.  
  180. class Bitmap
  181.   alias :cp_typeset_draw_text :draw_text
  182.   def draw_text(*args)
  183.     name = self.font.name
  184.     name = name.first if name.is_a?(Array)
  185.     return cp_typeset_draw_text(*args) unless Typeset::Names.include?(name)
  186.     case args.size
  187.     when 2, 3
  188.       rect = args[0].clone
  189.       text = args[1].to_s
  190.       algn = args[2] || 0
  191.     when 5, 6
  192.       rect = Rect.new(*args[0..3])
  193.       text = args[4].to_s
  194.       algn = args[5] || 0
  195.     else
  196.       return cp_typeset_draw_text(*args)
  197.     end
  198.     key = [name, self.font.color, self.font.out_color, self.font.outline ? 255 : 0]
  199.     typeset = Typeset.get_typeset(*key)
  200.     letterbox = Typeset.get_letterbox(self.font.size, key)
  201.     basicbox =  Typeset.get_letterbox(Typeset.default_size(key), key)
  202.     bitmap = Bitmap.new([letterbox[0] * text.size, 1].max, [letterbox[1], 1].max)
  203.     text.split(//).each_with_index do |c,i|
  204.       n = Typeset.get_pos(c)
  205.       rect2 = Rect.new((n % 12) * basicbox[0], (n / 12) * basicbox[1], *basicbox)
  206.       rect3 = Rect.new(i * letterbox[0], 0, *letterbox)
  207.       bitmap.stretch_blt(rect3, typeset, rect2)
  208.     end
  209.     ypos = rect.y + (rect.height - bitmap.height) / 2
  210.     if rect.width < bitmap.width
  211.       rect4 = Rect.new(rect.x, ypos, rect.width, bitmap.height)
  212.       self.stretch_blt(rect4, bitmap, bitmap.rect, self.font.color.alpha)
  213.     else
  214.       xpos = rect.x + (rect.width - bitmap.width) / 2 * algn
  215.       self.blt(xpos, ypos, bitmap, bitmap.rect, self.font.color.alpha)
  216.     end
  217.   end
  218.  
  219.   alias :cp_typeset_text_size :text_size
  220.   def text_size(string)
  221.     string = string.to_s
  222.     name = self.font.name
  223.     name = name.first if name.is_a?(Array)
  224.     return cp_typeset_text_size(string) unless Typeset::Names.include?(name)
  225.     key = [name, self.font.color, self.font.out_color, self.font.outline ? 255 : 0]
  226.     letterbox = Typeset.get_letterbox(self.font.size, key)
  227.     Rect.new(0, 0, letterbox[0] * string.size, letterbox[1])
  228.   end
  229. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement