Code:
#######################
# Script by: BEHEMOTH #
#############################################################################
# Script : Bitmap Fonts #
# Version: 2.0 #
# #
# Version 2.0 Features: #
# --------------------- #
# - Bitmap Fonts hue can be altered #
# #
# Version 1.0 Features: #
# --------------------- #
# - Fonts can now be displayed through bitmaps (images) #
# - Bitmap Fonts can be resized #
# - You can change back and fourth through bitmap fonts and normal fonts #
# anytime. #
# #
# Description: #
# ------------ #
# Gives the ability to have text displayed by an image. #
# #
# Script Installation #
# ------------------- #
# Copy the contents of this file into a new script and place it above the #
# script named "Main" #
# #
# Default Font Script Commands: #
# ----------------------------- #
# - Font.default_text_type (set to 0 for bitmap fonts, #
# set to 1 for normal fonts) #
# #
# - Font.default_bitmap_name (set to a string containing the filename #
# of the image) #
# #
# - Font.default_bitmap_hue (set to a numeric value from 0 to 360) #
# #
# - Font.default_bitmap_size (set to 100 to display font at normal size, #
# lower values shrink the text, #
# higher values enlarge the text) #
# #
# Font Script Commands(for individual font objects): #
# -------------------------------------------------- #
# - myFont.text_type (set to 0 for bitmap fonts, #
# set to 1 for normal fonts) #
# #
# - myFont.bitmap_name (set to a string containing the filename #
# of the image) #
# #
# - myFont.bitmap_hue (set to a numeric value from 0 to 360) #
# #
# - myFont.bitmap_size (set to 100 to display font at normal size, #
# lower values shrink the text, #
# higher values enlarge the text) #
# #
# Notes: #
# ------ #
# - Place your font images inside the Pictures directory. #
# - You can set the initial default font values a few lines down, after #
# the end of all these comments. #
# #
# Font Image Setup: #
# ---------------- #
# 1) Place a pixel with a different color than the images transparent #
# color in the very top left of the image. This is done so the script #
# knows what color is being used for the fonts character positioning. #
# #
# 2) After each letter in the image place another pixel at the top of the #
# image to tell the script that the letter's bitmap is done and the #
# next characters image starts here. #
# #
# 3) The top of each letter should be on the 3rd pixel down, leaving a #
# space of 2 pixels on the top of the entire image. The first letter #
# should be placed 1 pixel to right from the left of the image. #
# #
# 4) The character order in the font is as follows: #
# #
# ~ ! @ # $ % ^ & * ( ) _ + ` 0 1 2 3 4 5 6 7 8 9 - = { } | [ ] \ : #
# #
# " ; ' < > ? , . / A B C D E F G H I J K L M N O P Q R S T U V W X #
# #
# Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z #
# #
# The last letter in the image is a space, and so typically you should #
# leave about 6 or 7 pixels blank at the end. Or however big you want #
# a space to be. #
# #
# 5) *Optional and Recommended* Letters such as g j y q p #
# usually go below the baseline of other text characters. So to define #
# the baseline in the image: Place a pixel with the same color as the #
# pixel in the top left of your image, on the 1st pixel on the left of #
# the image. Place it appropriately on the y-axis according to your #
# fonts characters. #
# #
# Please view the sample font image if you're having trouble understanding #
# how to create a correct font image. #
# #
#############################################################################
class Font
attr_accessor :text_type
attr_accessor :bitmap_name
attr_accessor :bitmap_hue
attr_reader :bitmap_size
#**************************************************************************
#Edit Initial Default Font Values HERE!!!
#**************************************************************************
# set to 0 for bitmap fonts, set to 1 for normal fonts
@@default_text_type = 0
# set to a string containing the filename of the fonts image
@@default_bitmap_name = "alien_font.png"
# set to a numeric value to display font at a different hue.
# Values range from (Normal)0..360(Max hue).
@@default_bitmap_hue = 0
# set to 100 to display font at normal size,
# lower values shrink the text, higher values enlarge the text
@@default_bitmap_size = 100.0
#**************************************************************************
#End of Editing
#**************************************************************************
alias :font_init :initialize if ($aliased_font==nil)
$aliased_font = true
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(name=Font.default_name, size=Font.default_size)
@text_type = @@default_text_type
@bitmap_name = @@default_bitmap_name
@bitmap_hue = @@default_bitmap_hue
@bitmap_size = @@default_bitmap_size
font_init(name, size)
end
#--------------------------------------------------------------------------
# * Set Bitmap Size
#--------------------------------------------------------------------------
def bitmap_size=(bitmap_size)
@bitmap_size = bitmap_size.to_f
end
#--------------------------------------------------------------------------
# * Set Default Bitmap Name
#--------------------------------------------------------------------------
def Font.default_bitmap_name=(default_bitmap_name)
@@default_bitmap_name=default_bitmap_name
end
#--------------------------------------------------------------------------
# * Get Default Bitmap Name
#--------------------------------------------------------------------------
def Font.default_bitmap_name
return @@default_bitmap_name
end
#--------------------------------------------------------------------------
# * Set Default Bitmap Hue
#--------------------------------------------------------------------------
def Font.default_bitmap_hue=(default_bitmap_hue)
@@default_bitmap_hue=default_bitmap_hue
end
#--------------------------------------------------------------------------
# * Get Default Bitmap Hue
#--------------------------------------------------------------------------
def Font.default_bitmap_hue
return @@default_bitmap_hue
end
#--------------------------------------------------------------------------
# * Set Default Bitmap Size
#--------------------------------------------------------------------------
def Font.default_bitmap_size=(default_bitmap_size)
@@default_bitmap_size=default_bitmap_size.to_f
end
#--------------------------------------------------------------------------
# * Get Default Bitmap Size
#--------------------------------------------------------------------------
def Font.default_bitmap_size
return @@default_bitmap_size
end
#--------------------------------------------------------------------------
# * Set Default Text Type
#--------------------------------------------------------------------------
def Font.default_text_type=(default_text_type)
@@default_text_type=default_text_type
end
#--------------------------------------------------------------------------
# * Get Default Text Type
#--------------------------------------------------------------------------
def Font.default_text_type
return @@default_text_type
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Font_Bitmap
attr_reader :name
attr_reader :x
attr_reader :width
attr_reader :height
attr_reader :max_char_width #maximum width of a character
attr_reader :border_height #the bottom line of the text
attr_reader :fBitmap #The Fonts Bitmap
MAX_CHARS = 100
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(name)
@name = name
@fBitmap = RPG::Cache.picture(name)
@x = Table.new(MAX_CHARS) #0-95 - X-Position
@width = Table.new(MAX_CHARS) #0-95 - Width
@height = @fBitmap.height - 2
@border_height = 0
@max_char_width = 0
rebuild
end
#--------------------------------------------------------------------------
# * Creates x and width values from name.
#--------------------------------------------------------------------------
def rebuild
gColor = @fBitmap.get_pixel(0, 0)
char_index = 1
@x[1] = 1
max_width = 0
for i in 1...@fBitmap.width
color = @fBitmap.get_pixel(i, 0)
if color == gColor
if char_index >= MAX_CHARS - 1 #has too many pixels(letters)
break
end
char_index+=1
@x[char_index] = i
@width[char_index-1] = i - @x[char_index-1]
if max_width < @width[char_index-1]
max_width = @width[char_index-1]
end
end
end
@width[char_index] = [@fBitmap.width - @x[char_index], 0].max
if max_width < @width[char_index]
max_width = @width[char_index]
end
@max_char_width = max_width
#Get border height
@border_height = @fBitmap.height - 2
for i in 2...@fBitmap.height
color = @fBitmap.get_pixel(0, i)
if color == gColor
@border_height = i - 2
break
end
end
end
#--------------------------------------------------------------------------
# * Checks if name is the same
#--------------------------------------------------------------------------
def is_name?(name)
return @name == name
end
#--------------------------------------------------------------------------
end
#==============================================================================
module ExtraFont
module_function
@bitmap_text_pos = []
#--------------------------------------------------------------------------
# * Get bitmap text pos array
#--------------------------------------------------------------------------
def get_bitmap_text_pos(bitmap_name)
index = get_bitmap_name_index(bitmap_name)
if index == nil
return nil
else
return @bitmap_text_pos[index]
end
end
#--------------------------------------------------------------------------
# * Add bitmap text pos array
#--------------------------------------------------------------------------
def add_bitmap_text_pos(bitmap_name)
i = get_bitmap_name_index(bitmap_name)
if i == nil
@bitmap_text_pos.push(Font_Bitmap.new(bitmap_name))
end
end
#--------------------------------------------------------------------------
# * Gets the index value from a font's bitmap position name
# name : Font Bitmap Name
#--------------------------------------------------------------------------
def get_bitmap_name_index(name)
for i in 0...@bitmap_text_pos.size
if @bitmap_text_pos[i].is_name?(name)
return i
end
end
return nil
end
#--------------------------------------------------------------------------
# * Gets a Rect of where the character is within the bitmap
# char : The text character
# bitmap_name : Font Bitmap Name
#--------------------------------------------------------------------------
def get_char_rect(char, bitmap_name)
index = get_bitmap_name_index(bitmap_name)
if index == nil
return Rect.new(0,0,0,0)
end
char_value = get_char_num(char)
x = @bitmap_text_pos[index].x[char_value]
w = @bitmap_text_pos[index].width[char_value]
h = @bitmap_text_pos[index].height
return Rect.new(x, 2, w, h)
end
#--------------------------------------------------------------------------
# * Gets a Char Value From a Bitmap Font
# char : The text character
#--------------------------------------------------------------------------
def get_char_num(char)
value = 0
case char
when '~'
value = 1
when '!'
value = 2
when '@'
value = 3
when '#'
value = 4
when '$'
value = 5
when '%'
value = 6
when '^'
value = 7
when '&'
value = 8
when '*'
value = 9
when '('
value = 10
when ')'
value = 11
when '_'
value = 12
when '+'
value = 13
when '`'
value = 14
when '0'
value = 15
when '1'
value = 16
when '2'
value = 17
when '3'
value = 18
when '4'
value = 19
when '5'
value = 20
when '6'
value = 21
when '7'
value = 22
when '8'
value = 23
when '9'
value = 24
when '-'
value = 25
when '='
value = 26
when 'Ä'
value = 27
when 'ä'
value = 28
when 'Ö'
value = 29
when 'ö'
value = 30
when 'Ü'
value = 31
when '§'
value = 32
when ':'
value = 33
when '"'
value = 34
when ';'
value = 35
when '2"'
value = 36
when '<'
value = 37
when '>'
value = 38
when '?'
value = 39
when ','
value = 40
when '.'
value = 41
when '/'
value = 42
when 'A'
value = 43
when 'B'
value = 44
when 'C'
value = 45
when 'D'
value = 46
when 'E'
value = 47
when 'F'
value = 48
when 'G'
value = 49
when 'H'
value = 50
when 'I'
value = 51
when 'J'
value = 52
when 'K'
value = 53
when 'L'
value = 54
when 'M'
value = 55
when 'N'
value = 56
when 'O'
value = 57
when 'P'
value = 58
when 'Q'
value = 59
when 'R'
value = 60
when 'S'
value = 61
when 'T'
value = 62
when 'U'
value = 63
when 'V'
value = 64
when 'W'
value = 65
when 'X'
value = 66
when 'Y'
value = 67
when 'Z'
value = 68
when 'a'
value = 69
when 'b'
value = 70
when 'c'
value = 71
when 'd'
value = 72
when 'e'
value = 73
when 'f'
value = 74
when 'g'
value = 75
when 'h'
value = 76
when 'i'
value = 77
when 'j'
value = 78
when 'k'
value = 79
when 'l'
value = 80
when 'm'
value = 81
when 'n'
value = 82
when 'o'
value = 83
when 'p'
value = 84
when 'q'
value = 85
when 'r'
value = 86
when 's'
value = 87
when 't'
value = 88
when 'u'
value = 89
when 'v'
value = 90
when 'w'
value = 91
when 'x'
value = 92
when 'y'
value = 93
when 'z'
value = 94
when ' '
value = 95
when 'ä'
value = 96
end
return value
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Bitmap
if ($aliased_bitmap==nil)
alias normal_draw_text draw_text
alias normal_text_size text_size
end
$aliased_bitmap = true
#--------------------------------------------------------------------------
# * Draw Text (Overload Method)
# arg1 : X-Position, Rect , X-Position
# arg2 : Y-Position, Text , Y-Position
# arg3 : Width , [Align], Width
# arg4 : Height , , Height
# arg5 : Text , , Text
# arg6 : [Align] , , [Align]
#--------------------------------------------------------------------------
def draw_text(arg1, arg2, arg3=0, arg4=0, arg5="", arg6=0)
if arg1.is_a?(Rect)
if self.font.text_type == 0
draw_bitmap_text(arg1.x, arg1.y, arg1.width, arg1.height, arg2, arg3)
else
normal_draw_text(arg1, arg2, arg3)
end
else
if self.font.text_type == 0
draw_bitmap_text(arg1, arg2, arg3, arg4, arg5, arg6)
else
normal_draw_text(arg1, arg2, arg3, arg4, arg5, arg6)
end
end
end
#--------------------------------------------------------------------------
# * Gets the box (Rect) used when drawing
# a string str with the draw_text. (Overload Method)
# str : String to get rect from
#--------------------------------------------------------------------------
def text_size(str)
if self.font.text_type == 0
ExtraFont.add_bitmap_text_pos(self.font.bitmap_name)
bitmap_pos = ExtraFont.get_bitmap_text_pos(self.font.bitmap_name)
width = 0
font_size = self.font.bitmap_size / 100
height = bitmap_pos.height * font_size
for i in 0...str.size
char = str[i, 1]
src_rect = ExtraFont.get_char_rect(char, self.font.bitmap_name)
dw = src_rect.width * font_size
width += dw + 1
end
if width > 0
width -= 1
end
return Rect.new(0, 0, width, height)
else
normal_text_size(str)
end
end
#--------------------------------------------------------------------------
# * Draw Text With Font From Bitmap File
# filename : Filename of Font Bitmap
# x : draw spot x-coordinate
# y : draw spot y-coordinate
# width : draw width
# height : draw height
# text : draw text
# align : text alignment (0=left, 1=center, 2=right)
#--------------------------------------------------------------------------
def draw_bitmap_text(x, y, width, height, text, align=0)
ExtraFont.add_bitmap_text_pos(self.font.bitmap_name)
bitmap_pos = ExtraFont.get_bitmap_text_pos(self.font.bitmap_name)
src_bitmap = bitmap_pos.fBitmap.clone
drawx = 0
font_size = self.font.bitmap_size / 100
font_hue = self.font.bitmap_hue
dh = bitmap_pos.height * font_size
b_height = bitmap_pos.border_height * font_size
max_dw = bitmap_pos.max_char_width * font_size
dum_width = [max_dw * text.size, 32].max
dum_height = [dh, 32].max
dummy = Bitmap.new(dum_width, dum_height)
src_bitmap.hue_change(font_hue) if font_hue != 0
#Go through each char in string and draw the letter from the bitmap
for i in 0...text.size
char = text[i, 1]
src_rect = ExtraFont.get_char_rect(char, self.font.bitmap_name)
w = src_rect.width
h = src_rect.height
dw = w * font_size
dest_rect = Rect.new(drawx, 0, dw, dh)
dummy.stretch_blt(dest_rect, src_bitmap, src_rect)
drawx += dw
end
#If went beyond the width then strech it to the smaller width.
if drawx - 1 >= width
dest_rect = Rect.new(x, y+(height/2)-(b_height/2), width, dummy.height)
src_rect = Rect.new(0, 0, drawx, dum_height)
self.stretch_blt(dest_rect, dummy, src_rect)
else
posy = y+(height/2)-(b_height/2)
src_rect = Rect.new(0, 0, drawx, dum_height)
if align == 0 #left
posx = x
elsif align == 1 #center
posx = x + (width - src_rect.width) / 2
else #right
posx = x + (width - src_rect.width)
end
self.blt(posx, posy, dummy, src_rect)
end
dummy.dispose
end
#--------------------------------------------------------------------------
end
#==============================================================================
Demolink: