Draw Text
#1
Draw Text
Version: 1.0


Introduction
An Little addon for any bitmap that allows you to draw wrapped text or formatted text it automatically wraps to the next line if the text is too long to fit on the line. Also adds features (for formatted text) like color changing, italics, bold, new line, and tabbing.


Screenshots
I'll post them if requested


Script
add this in a new script above main

Code:
#C[RRBBGG] Changes the Color (RR,BB,GG)
      #C[N] Changes to color back to normal
      #I[] Changes the font to Italics
      #B[] Changes the font to Bold
      #T[] A Tab
      #N[] New Line
#==============================================================================
# ** Bitmap
#------------------------------------------------------------------------------
#  Additions add a draw wrap text method which draws word wrapped text
#  Text Automattically goes to the next line if the text can not fit on the line
#==============================================================================
class Bitmap
  def draw_wrap_text(x,y,width, height, text)
    array = text.split
    for i in array
      word = i + ' '
      word_width = text_size(word).width
      if x + word_width > width
        y += height
        x = 0
      end
      self.draw_text(x, y, word_width, height, word)
      x += word_width
    end
  end
  
  def text_wrap_size(x,y,width, height, text)
    array = text.split
    begin_y = y
    y += height
    for i in array
      word = i + ' '
      word_width = text_size(word).width
      if x + word_width > width
        y += height
        x = 0
      end
      x += word_width
    end
    return Rect.new(x, y, width, y-begin_y)
  end
  
  def draw_formatted_text(offset_x, offset_y, width, height, text)
    self.font.color = Color.new(255, 255, 255)
    text = text.dup
    text.gsub!(/[Cc]\[((([A-F]|[a-f]|[0-9])+)|N)\]/) {" \001[#{$1}] "}
    text.gsub!(/[Bb]\[\]/) {" \002 "}
    text.gsub!(/[Ii]\[\]/) {" \003 "}
    text.gsub!(/[Tt]\[\]/) {" \004 "}
    text.gsub!(/[Nn]\[\]/) {" \005 "}
    x = 0
    y = 0
    while ((c = text.slice!(/(\S+\s)|(\S+)/m)) != nil)
      if c.include?("\001")
        c.slice!("\001")
        # Change text color
        c.sub!(/\[((([A-F]|[a-f]|[0-9])+)|N)\]\s/, "")
        if $1 != nil and $1.size > 1
          r = $1.slice(0..1).to_i(16)
          g = $1.slice(2..3).to_i(16)
          b = $1.slice(4..5).to_i(16)
          color = Color.new(r,g,b)
        else
          color = Color.new(255, 255, 255)
        end
        self.font.color = color
        # go to next text
        next
      end
      if c.include?("\002")
        c.slice!("\002")
        self.font.bold = !self.font.bold
        c = ""
        # go to next text
        next
      end
      if c.include?("\003")
        c.slice!("\003")
        self.font.italic = !self.font.italic
        c = ""
        # go to next text
        next
      end
      if c.include?("\004")
        c.slice!("\004")
        x += text_size("    ").width
        c = ""
        next
      end
      if c.include?("\005")
        c.slice!("\005")
        y = x / width + 1
        x = y * width
        c = ""
        next
      end
      word_width = text_size(c).width
      if (x+word_width) % width < x % width
        y = x / width + 1
        x = y * width
      end
      pos_x = x % width + offset_x
      pos_y = x / width * height + offset_y
      self.draw_text(pos_x, pos_y, word_width, 32, c)
      x += word_width
    end
  end
      
  def get_formatted_size(off_x, off_y, width, height, text)
    text = text.dup
    text.gsub!(/[Cc]\[((([A-F]|[a-f]|[0-9])+)|N)\]/) {""}
    text.gsub!(/[Bb]\[\]/) {""}
    text.gsub!(/[Ii]\[\]/) {""}
    text.gsub!(/[Tt]\[\]/) {" \004 "}
    text.gsub!(/[Nn]\[\]/) {" \005 "}
    x = 0
    y = 0
    while ((c = text.slice!(/(\S+\s)|(\S+)/m)) != nil)
      if c.include?("\004")
        c.slice!("\004")
        x += text_size("    ").width
        c = ""
        next
      end
      if c.include?("\005")
        c.slice!("\005")
        y = x / width + 1
        x = y * width
        c = ""
        next
      end
      word_width = text_size(c).width
      if (x+word_width) % width < x % width
        y = x / width + 1
        x = y * width
      end
      x += word_width
      pos_x = x % width + off_x
      pos_y = x / width * height + off_y
    end
    return Rect.new(pos_x, pos_y, width, (pos_y-off_y+height).to_i)
  end
end

Instructions

to call this all you have to do is
self.contents.draw_formatted_text(x,y,width, height, text)
or
self.contents.draw_wrap_text(x,y,width,height,text)

you may also call get_wrap_size, and get_formatted size to get the rectangle the text is drawn on (corresponds to method text_size)


where offset_x is the x coordinate to start drawing text and offset_y is the y coordinate to start drawing, text is the text to draw

If you use C[RRGGBB] where RR GG and BB are hexadecimal color values inside the text then the color will change to that color. To change it back just put C[N]. If you want bold text then type B[] for Italics use I[]. If you want a newline just put N[] and for tab (4 spaces) use T[] you can easily change these flags by modifing the Regexp used in these lines

Code:
text.gsub!(/[Cc]\[((([A-F]|[a-f]|[0-9])+)|N)\]/) {" \001[#{$1}] "}
        text.gsub!(/[Bb]\[\]/) {" \002 "}
        text.gsub!(/[Ii]\[\]/) {" \003 "}
        text.gsub!(/[Tt]\[\]/) {" \004 "}
        text.gsub!(/[Nn]\[\]/) {" \005 "}


FAQ
Note: Permission granted by Trickster to post:
Quote:And if you post what you have now of my stuff then you don't have the latest versions. I'm too lazy/busy to post stuff.
As this is his material, it is deletable upon his request. Due to his current absense, no support is available. Please do not PM or eMail him for support.


Compatibility
Compatible with any script


Author's Notes
If you find any bugs let me know...


Terms and Conditions
Hey, I posted this publicly. You can use it. What do you expect? But if you do use it, I do expect you to spell my name correctly in your game. And yes you can use it in commercial games too.
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Text Scroll Script - Enhanced DerVVulfman 23 34,290 02-18-2021, 04:16 AM
Last Post: DerVVulfman
   Message Text from Files DerVVulfman 0 5,283 04-13-2016, 03:25 AM
Last Post: DerVVulfman
   Text Extraction Generator DerVVulfman 5 12,947 10-13-2014, 09:32 PM
Last Post: Narzew
   Text to RGSS DerVVulfman 10 25,612 05-04-2013, 04:34 AM
Last Post: DerVVulfman
   Damage Text Revamp 2.1 PK8 3 12,825 04-08-2012, 06:47 PM
Last Post: PK8
   Auto-Sizing Text Windows RPG Advocate 9 18,933 06-11-2011, 06:18 PM
Last Post: sagam12
   Another Scrolling Text Script kyonides 1 6,475 04-12-2010, 01:36 AM
Last Post: Shiroiyuki
   Damage Text Edit ParaDog 0 5,071 03-05-2008, 08:22 AM
Last Post: ParaDog
   Command Windows - Animated Text Selwyn 0 4,323 03-03-2008, 06:18 AM
Last Post: Selwyn
   One-Letter-at-a-Time Text Display Script Anonymous 0 5,030 03-03-2008, 05:41 AM
Last Post: Anonymous



Users browsing this thread: 1 Guest(s)