RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
[XP] HTML-Rendering-Engine

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 83
Here I now present the HTML-Rendering-Enging by Caesar!

Since I keep using it in some of my scripts and I think of it as quite useful I decided to repost it here seperately.
Hope some other can use it.

Spoiler for:
Code: [Select]
#///////////////////////HTML-Rendering-Engine/////////////////////////////////
#~~~~~~~~~~~~~~~~by Caesar~~~~~~~~~~~~~~~~~~~
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
class Bitmap
def draw_shadow_text(x, y, width, height, str, align=0)
color = font.color.dup
font.color = Color.new(192, 192, 192, 156)
draw_text(x+2, y+2, width, height, str, align)
font.color = color
draw_text(x, y, width, height, str, align)
end
#----------------
def draw_html(x, y, width, height, str)
# remember string and font variables
str = str.dup
color = font.color.dup
bold = font.bold
italic = font.italic
size = font.size
name = font.name.dup
#::::::::::
shadow = false
underlined = false
opacity = 255
str.gsub!(/<if=([0-9]+)>(.+?)<else>(.+?)<\/if>/) {$game_switches[$1.to_i] ? $2 : $3}
str.gsub!(/<f=([0-9]+)>(.+?)<\/f>/) {$game_switches[$1.to_i] ? $2 : ""}
str.gsub!(/<var=([0-9]+)>/) {$game_variables[$1.to_i].to_s}
str.gsub!(/<eval={(.+?)}>/) {eval $1}
str.gsub!(/<style=([A-Za-z0-9_-]+)>(.+?)<\/style>/) {
STYLES.has_key?($1) ? STYLES[$1].sub("|", $2) : ""
} if defined?(STYLES)
str.gsub!(/<br>/) {"\n"}
str.gsub!(/\\\\/) {"\00"}
str.gsub!(/<b>/) {"\01"}
str.gsub!(/<\/b>/) {"\02"}
str.gsub!(/<i>/) {"\03"}
str.gsub!(/<\/i>/) {"\04"}
str.gsub!(/<color=(#?[0-9a-z_]+)>/) {"\05[#{$1}]"}
str.gsub!(/<\/color>/) {"\06"}
str.gsub!(/<shadow>/) {"\16"}
str.gsub!(/<\/shadow>/) {"\17"}
str.gsub!(/<small>/) {"\20"}
str.gsub!(/<\/small>/) {"\21"}
str.gsub!(/<big>/) {"\23"}
str.gsub!(/<\/big>/) {"\21"}
str.gsub!(/<size=([0-9]+)>/) {"\24[#{$1}]"}
str.gsub!(/<\/size>/) {"\21"}
str.gsub!(/<font=([A-Za-z0-9\s]+)>/) {"\25[#{$1}]"}
str.gsub!(/<\/font>/) {"\26"}
str.gsub!(/<u>/) {"\27"}
str.gsub!(/<\/u>/) {"\30"}
str.gsub!(/<icon=([_A-Za-z0-9-]+)>/) {"\11[#{$1}]"}
str.gsub!(/<image=([_A-Za-z0-9-]+)>/) {"\31[#{$1}]"}
str.gsub!(/<down=([0-9]+)>/) {"\22[#{$1}]"}
str.gsub!(/<space=([0-9]+)>/) {"\100[#{$1}]"}
str.gsub!(/<line>/) {"\07"}
ix = 0
iy = 0
while ((c = str.slice!(/./m)) != nil)
if c == "\00" # \\
c = "\\"
end
if c == "\01" # <b>
font.bold = true
end
if c == "\02" #</b>
font.bold = bold
end
if c == "\03" # <i>
font.italic = true
end
if c == "\04" # </i>
font.italic = false
end
if c == "\05" # <color=xxx>
str.sub!(/\[(#?[0-9a-z_]+)\]/, "")
if $1[0] == 35
col = Color.decode($1)
elsif $1.to_i != 0
col = Window_Base.text_color($1.to_i)
else
col = Color.get($1)
end
font.color = col
end
if c == "\06" # </color>
font.color = color
end
if c == "\16" # <shadow>
shadow = true
end
if c == "\17" # </shadow>
shadow = false
end
if c == "\20" # <small>
font.size -= 5 if font.size > 10
end
if c == "\21" # </small> </big> </size>
font.size = size
end
if c == "\23" # <big>
font.size += 5 if font.size < 92
end
if c == "\24" # <size=xx>
str.sub!(/\[([0-9]+)\]/, "")
newsize = $1.to_i
font.size = newsize if newsize > 5 and newsize < 97
end
if c == "\25" # <font=xxx>
str.sub!(/\[([A-Za-z0-9\s]+)\]/, "")
font.name = $1 if Font.exist?($1)
end
if c == "\26" # </font>
font.name = name
end
if c == "\27" # <u>
underlined = true
end
if c == "\30" # </u>
underlined = false
end
if c == "\11" #<icon=xxx>
str.sub!(/\[([_A-Za-z0-9-]+)\]/, "")
icon = RPG::Cache.icon($1)
blt(ix + 8, iy + LINE_HEIGHT/2 - 12, icon, Rect.new(0, 0, 24, 24))
ix += 24
end
if c == "\31" # <image=xxx>
str.sub!(/\[([_A-Za-z0-9-]+)\]/, "")
image = RPG::Cache.picture($1)
iy += LINE_HEIGHT
blt((width-image.rect.width)/2, iy, image, image.rect)
iy += image.rect.height
ix = 0
end
if c == "\22" # <down=xxx>
str.sub!(/\[([0-9]+)\]/, "")
iy += $1.to_i
ix = 0
end
if c == "\100" # <space=xxx>
str.sub!(/\[([0-9]+)\]/, "")
ix += $1.to_i
c = ""
end
if c == "\07" # <line>
iy += LINE_HEIGHT + 3
fill_rect(16, iy, width-32, 2, font.color)
fill_rect(16, iy, width-32, 2, Color.new(192, 192, 192, 156)) if shadow
iy += 5
ix = 0
end
if c == "\n"
iy += LINE_HEIGHT
ix = 0
end
#:::::::::
if shadow
draw_shadow_text(x+ix+4, y+iy, 40, font.size, c)
else
draw_text(x+ix+4, y+iy, 40, font.size, c)
end
w = text_size(c).width
if underlined
fill_rect(x+ix+4, y+iy+text_size("T").height+3, w, 2, font.color)
end
ix += w
end
#::::::::::
#reset font variables
font.color = color
font.bold = bold
font.italic = italic
font.size = size
font.name = name
#return height of the bitmap
return iy + LINE_HEIGHT
end
end
#==============
class Color
def Color.get(s)
eval "Color.#{s}" rescue Color.white
end
#------------
def Color.decode(hex)
return Color.decode(hex[1..hex.length]) if hex[0] == 35
hex.downcase!
red = hex[0..1].hex
green = hex[2..3].hex
blue = hex[4..5].hex
alpha = hex.length == 8 ? hex[6..7].hex : 255
return Color.new(red, green, blue, alpha)
end
#------------
def Color.normal_color
return Color.new(255, 255, 255, 255)
end
#-----------
def Color.disabled_color
return Color.new(255, 255, 255, 128)
end
#-----------
def Color.system_color
return Color.new(192, 224, 255, 255)
end
#-----------
def Color.crisis_color
return Color.new(255, 255, 64, 255)
end
#-----------
def Color.knockout_color
return Color.new(255, 64, 0)
end
#------------
def Color.white(alpha=255)
return Color.new(255, 255, 255, alpha)
end
#-----------
def Color.black(alpha=255)
return Color.new(0, 0, 0, alpha)
end
#----------
def Color.red(alpha=255)
return Color.new(255, 0, 0, alpha)
end
#----------
def Color.green(alpha=255)
return Color.new(0, 255, 0, alpha)
end
#---------
def Color.blue(alpha=255)
return Color.new(0, 0, 255, alpha)
end
#----------
def Color.yellow(alpha=255)
return Color.new(255, 255, 0, alpha)
end
#----------
def Color.cyan(alpha=255)
return Color.new(0, 255, 255, alpha)
end
#----------
def Color.magenta(alpha=255)
return Color.new(255, 255, 0, alpha)
end
#----------
def Color.light_gray(alpha=255)
return Color.new(192, 192, 192, alpha)
end
#-----------
def Color.gray(alpha=255)
return Color.new(128, 128, 128, alpha)
end
#-----------
def Color.dark_gray(alpha=255)
return Color.new(64, 64, 64, alpha)
end
#-----------
def Color.pink(alpha=255)
return Color.new(255, 175, 175, alpha)
end
#-----------
def Color.orange(alpha=255)
return Color.new(255, 200, 0, alpha)
end
end
#=====================
class Window_Base < Window
# redefine text colors for static context
def self.text_color(n)
case n
when 0
return Color.new(255, 255, 255, 255)
when 1
return Color.new(128, 128, 255, 255)
when 2
return Color.new(255, 128, 128, 255)
when 3
return Color.new(128, 255, 128, 255)
when 4
return Color.new(128, 255, 255, 255)
when 5
return Color.new(255, 128, 255, 255)
when 6
return Color.new(255, 255, 128, 255)
when 7
return Color.new(192, 192, 192, 255)
else
return Color.white
end
end
end

It may be used instead of the normal draw_text within each script. Just type draw_html with the same arguments.
Description of how to use it and which command are implemented is in the PDF attached here

Have fun!
Tidloc.
« Last Edit: July 25, 2010, 02:30:19 PM by tidloc »
[...]And they feared him.
He understands that cruelty arises from opportunity![...]