Font Addons: Underline, Strikethrough, Outline & Shadow

Font Addons
Version: 1.5


Introduction

Allows you to have underlined, strikethroughed, outlined and shadowed text.

Features
  • Default font properties - You can set the game to have underlined and/or strikethroughed text, just like bold and italic
  • Easy to use. Operates just like bold and italic
  • Can now set the underline and/or strikethrough effect to be the text's width. By default it is the text's width
  • Outlined and shadowed text
  • Outline and shadow can have their own colours!
Screenshots

http://i12.photobucket.com/albums/a229/ ... rline2.png[/IMG]http://i12.photobucket.com/albums/a229/meismeofcourse/NewFontStrikethough2.png[/IMG]
http://i12.photobucket.com/albums/a229/ ... tline2.png[/IMG]http://i12.photobucket.com/albums/a229/meismeofcourse/NewFontShadow.png[/IMG]

Script

Content Hidden Spoiler
#============================================================================
#  New Font Addons
#----------------------------------------------------------------------------
#  Adds two new font properties: Underline and Strikethrough
#  Operates in just the same way as bold or italic.
#----------------------------------------------------------------------------
#  Yeyinde
#  12/15/06
#============================================================================


#============================================================================
#  Font Class
#----------------------------------------------------------------------------
#  Bitmap property; holds the flags for the font display modes.
#============================================================================

class Font
  #--------------------------------------------------------------------------
  # * Class Variable Decloration
  #--------------------------------------------------------------------------
  @@default_underline = false
  @@default_underline_full = false
  @@default_strikethrough = false
  @@default_strikethrough_full = false
  @@default_shadow = false
  @@default_shadow_color = Color.new(0, 0, 0, 100)
  @@default_outline = false
  @@default_outline_color = Color.new(0, 0, 0)
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :underline, :underline_full
  attr_accessor :strikethrough, :strikethrough_full
  attr_accessor :shadow_color, :outline_color
  attr_reader :shadow, :outline
  #--------------------------------------------------------------------------
  # * Object Aliasing
  #--------------------------------------------------------------------------
  if @yeyinde_font_alias.nil?
    alias yeyinde_font_int initialize
    @yeyinde_font_alias = true
  end
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(*args)
    yeyinde_font_int(*args)
    @underline = @@default_underline
    @underline_full = @@default_underline_full
    @strikethrough = @@default_strikethrough
    @strikethrough_full = @@default_strikethrough_full
    @shadow = @@default_shadow
    @shadow_color = @@default_shadow_color
    @outline = @@default_outline
    @outline_color = @@default_outline_color
  end
  #--------------------------------------------------------------------------
  # * default_underline=(bool)
  #     bool : set class variable (true/false)
  #--------------------------------------------------------------------------
  def self.default_underline=(bool)
    @@default_underline = bool
  end
  #--------------------------------------------------------------------------
  # * default_strikethrough=(bool)
  #     bool : set class variable (true/false)
  #--------------------------------------------------------------------------
  def self.default_strikethrough=(bool)
    @@default_strikethrough = bool
  end
  #--------------------------------------------------------------------------
  # * default_underline
  #--------------------------------------------------------------------------
  def self.default_underline
    return @@default_underline
  end
  #--------------------------------------------------------------------------
  # * default_strikethrough
  #--------------------------------------------------------------------------
  def self.default_strikethrough
    return @@default_strikethrough
  end
  #--------------------------------------------------------------------------
  # * default_underline_full=(bool)
  #     bool : set class variable (true/false)
  #--------------------------------------------------------------------------
  def self.default_underline_full=(bool)
    @@default_underline_full = bool
  end
  #--------------------------------------------------------------------------
  # * default_strikethrough_full=(bool)
  #     bool : set class variable (true/false)
  #--------------------------------------------------------------------------
  def self.default_strikethrough_full=(bool)
    @@default_strikethrough_full = bool
  end
  #--------------------------------------------------------------------------
  # * default_underline_full
  #--------------------------------------------------------------------------
  def self.default_underline_full
    return @@default_underline_full
  end
  #--------------------------------------------------------------------------
  # * default_strikethrough_full
  #--------------------------------------------------------------------------
  def self.default_strikethrough_full
    return @@default_strikethrough_full
  end
  #--------------------------------------------------------------------------
  # * default_shadow=(bool)
  #     bool : set class variable (true/false)
  #--------------------------------------------------------------------------
  def self.default_shadow=(bool)
    @@default_shadow = bool
    @@default_outline = false unless bool == false
  end
  #--------------------------------------------------------------------------
  # * default_shadow
  #--------------------------------------------------------------------------
  def self.default_shadow
    return @@default_shadow
  end
  #--------------------------------------------------------------------------
  # * default_shadow_color=(color)
  #     color : Color class (Color.new(r, g, b [, a]))
  #--------------------------------------------------------------------------
  def self.default_shadow_color=(color)
    raise(TypeError, 'Cannot use a non Color object for color.') unless color.is_a?(Color)
    @@default_shadow_color = color
  end
  #--------------------------------------------------------------------------
  # * default_shadow_color
  #--------------------------------------------------------------------------
  def self.default_shadow_color
    return @@default_shadow_color
  end
  #--------------------------------------------------------------------------
  # * default_outline=(bool)
  #     bool : set class variable (true/false)
  #--------------------------------------------------------------------------
  def self.default_outline=(bool)
    @@default_outline = bool
    @@default_shadow = false unless bool == false
  end
  #--------------------------------------------------------------------------
  # * default_shadow
  #--------------------------------------------------------------------------
  def self.default_outline
    return @@default_outline
  end
  #--------------------------------------------------------------------------
  # * default_shadow_color=(color)
  #     color : Color class (Color.new(r, g, b [, a])
  #--------------------------------------------------------------------------
  def self.default_outline_color=(color)
    raise(TypeError, 'Cannot use a non Color object for color.') unless color.is_a?(Color)
    @@default_outline_color = color
  end
  #--------------------------------------------------------------------------
  # * default_shadow_color
  #--------------------------------------------------------------------------
  def self.default_outline_color
    return @@default_outline_color
  end
  #--------------------------------------------------------------------------
  # * shadow=(bool)
  #     bool : set variable (true/false)
  #--------------------------------------------------------------------------
  def shadow=(bool)
    @shadow = bool
    @outline = false unless bool == false
  end
  #--------------------------------------------------------------------------
  # * outline=(bool)
  #     bool : set variable (true/false)
  #--------------------------------------------------------------------------
  def outline=(bool)
    @shadow = false unless bool == false
    @outline = bool
  end
end


#============================================================================
#  Bitmap Class
#----------------------------------------------------------------------------
#  Handles display and drawing data.  Needs a medium to be viewed.
#============================================================================

class Bitmap
  #--------------------------------------------------------------------------
  # * Object Aliasing
  #--------------------------------------------------------------------------
  if @yeyinde_us_alias.nil?
    alias yeyinde_us_draw_text draw_text
    @yeyinde_us_alias = true
  end
  #--------------------------------------------------------------------------
  # * draw_text (underline & strikethrough)
  #--------------------------------------------------------------------------
  def draw_text(*args)
    yeyinde_us_draw_text(*args)   
    if self.font.underline
      u_color = self.font.color.dup
      if args[0].is_a?(Rect)
        u_x = args[0].x
        u_y = args[0].y + args[0].height / 2 + self.font.size / 3
        if self.font.underline_full
          u_width = args[0].width
        else
          u_width = self.text_size(args[1]).width
          case args[2]
          when 1
            u_x += args[0].width / 2 - u_width / 2
          when 2
            u_x += args[0].width - u_width
          end
        end
      else
        u_x = args[0]
        u_y = args[1] + args[3] / 2 + self.font.size / 3
        if self.font.underline_full
          u_width = args[2]
        else
          u_width = self.text_size(args[4]).width
          case args[5]
          when 1
            u_x += args[2] / 2 - u_width / 2
          when 2
            u_x += args[2] - u_width
          end
        end
      end
      self.fill_rect(u_x, u_y, u_width, 1, u_color)
    end
    if self.font.strikethrough
      s_color = self.font.color.dup
      if args[0].is_a?(Rect)
        s_x = args[0].x
        s_y = args[0].y + args[0].height / 2
        if self.font.strikethrough_full
          s_width = args[0].width
        else
          s_width = self.text_size(args[1]).width
          case args[2]
          when 1
            s_x += args[0].width / 2 - s_width / 2
          when 2
            s_x += args[0].width - s_width
          end
        end
      else
        s_x = args[0]
        s_y = args[1] + args[3] / 2
        if self.font.strikethrough_full
          s_width = args[0].width
        else
          s_width = self.text_size(args[4]).width
          case args[5]
          when 1
            s_x += args[2] / 2 - s_width / 2
          when 2
            s_x += args[2] - s_width
          end
        end
      end
      self.fill_rect(s_x, s_y, s_width, 1, s_color)
    end
  end
  #--------------------------------------------------------------------------
  # * Object Aliasing
  #--------------------------------------------------------------------------
  if @yeyinde_so_alias.nil?
    alias yeyinde_so_draw_text draw_text
    @yeyinde_so_alias = true
  end
  #--------------------------------------------------------------------------
  # * draw_text (shadow & outline)
  #--------------------------------------------------------------------------
  def draw_text(*args)
    if self.font.shadow
      orig_color = self.font.color.dup
      self.font.color = self.font.shadow_color
      if args[0].is_a?(Rect)
        s_x = args[0].x + 2
        s_y = args[0].y + 2
        s_w = args[0].width
        s_h = args[0].height
        s_t = args[1]
        s_a = args[2]
      else
        s_x = args[0] + 2
        s_y = args[1] + 2
        s_w = args[2]
        s_h = args[3]
        s_t = args[4]
        s_a = args[5]
      end
      s_a = 0 if s_a.nil?
      self.yeyinde_so_draw_text(s_x, s_y, s_w, s_h, s_t, s_a)
      self.font.color = orig_color
    end
    if self.font.outline
      orig_color = self.font.color.dup
      self.font.color = self.font.outline_color
      if args[0].is_a?(Rect)
        o_x = args[0].x
        o_y = args[0].y
        o_w = args[0].width
        o_h = args[0].height
        o_t = args[1]
        o_a = args[2]
      else
        o_x = args[0]
        o_y = args[1]
        o_w = args[2]
        o_h = args[3]
        o_t = args[4]
        o_a = args[5]
      end
      o_a = 0 if o_a.nil?
      self.yeyinde_so_draw_text(o_x + 1, o_y, o_w, o_h, o_t, o_a)
      self.yeyinde_so_draw_text(o_x - 1, o_y, o_w, o_h, o_t, o_a)
      self.yeyinde_so_draw_text(o_x, o_y + 1, o_w, o_h, o_t, o_a)
      self.yeyinde_so_draw_text(o_x, o_y - 1, o_w, o_h, o_t, o_a)
      self.yeyinde_so_draw_text(o_x + 1, o_y + 1, o_w, o_h, o_t, o_a)
      self.yeyinde_so_draw_text(o_x + 1, o_y - 1, o_w, o_h, o_t, o_a)
      self.yeyinde_so_draw_text(o_x - 1, o_y - 1, o_w, o_h, o_t, o_a)
      self.yeyinde_so_draw_text(o_x - 1, o_y + 1, o_w, o_h, o_t, o_a)
      self.font.color = orig_color
    end
    self.yeyinde_so_draw_text(*args)
  end
end
Expand to see the code.


Instructions

Copy and paste into a new section above Main. Now you can set the new font properties in any bitmap you want!

FAQ

No questions asked

Compatibility

Untested, but should work with text outline scripts

Credits and Thanks

Thanks to Trickster for assisting me with *args and teaching my about hidden alias stacks.

Author's Notes

Credit "Underline/Strikethough/Outline/Shadow Text" to "Yeyinde"
Very Nice!
Here is something you need to remember about alias Yeyinde. When you alias a methods from a hidden class do something like this, to prevent a stack error when F12 is pressed, since they will get aliased again.

  if @new_stack.nil?
    alias new_title_name title_name
    alias new_title_bgm title_bgm
    alias new_start_map_id start_map_id
    alias new_start_x start_x
    alias new_start_y start_y
    alias new_party_members party_members
    @new_stack = true
  end
Expand to see the code.


Other than that good job :)
You learn something every day ;)

Updated to V. 1.1, which means NO ERRORS! ^_^
[QUOTE=Trickster;115618]Here is something you need to remember about alias Yeyinde. When you alias a methods from a hidden class do something like this, to prevent a stack error when F12 is pressed, since they will get aliased again.

  if @new_stack.nil?
    alias new_title_name title_name
    alias new_title_bgm title_bgm
    alias new_start_map_id start_map_id
    alias new_start_x start_x
    alias new_start_y start_y
    alias new_party_members party_members
    @new_stack = true
  end
Expand to see the code.


Other than that good job :)[/QUOTE]
Thanks for the useful info ;)

Oh, and the script is nice as well :P

Judging by your screenshots though, the underline/strikethrough goes the whole width of the available text area - would it be possible to set a variable that would allow it to only go as far as the end of the text? Having a choice is always nice :) (I really like the effect in the first shot though)
I did actually, in Me's alias thread, but when we backed up, it was removed.

I will remember to do so soon.
;o
You rock~nuff said...Damn useful! ^_^
[QUOTE=Myonosken;115643]

And Trickster, you might want to post that in another thread as a script snippet as many of us never understood how to fix the stack error.[/QUOTE]


[QUOTE=SephirothSpawn;115650]I did actually, in Me's alias thread, but when we backed up, it was removed.[/QUOTE]

I learned that one when Seph was aliasing Graphics#update, but it was a really hard bug to fix since many don't know what was going on when you pressed F12.

I am planning a advanced tutorial on alias (In special cases, It is possible to create the illusion of adding code to the middle of a method), but its still in the works maybe Seph could team up with me and help ;)

and yeah Yeyinde there should be an option to do the underline/strikethrough on the drawn text, I can now update my draw_formatted_text/draw_formatted_text_memory methods to include Strikethrough and Underlining with this :D
Dirtie wrote:
Judging by your screenshots though, the underline/strikethrough goes the whole width of the available text area - would it be possible to set a variable that would allow it to only go as far as the end of the text? Having a choice is always nice (I really like the effect in the first shot though)


Thank you for an idea. Now the script has been updated to do so.
You can set the underline length to the full draw_text width, or just the text width by changing the underline_full and strikethrough_full to false. You can also turn them off on all Font objects with the oh-so-standard Font.default_whatever = value.

EDIT: A small error has drawn upon the script. When the effects are set to text_width, they don't take into factor the align. I'll fix this when I get home.
[QUOTE=SephirothSpawn;115650]I did actually, in Me's alias thread, but when we backed up, it was removed.
I will remember to do so soon.[/QUOTE]

Omg.. that was removed? -_- ':|
Nevertheless, I will screate a new alias tut topic then :yes:

The script is nice :D

Edit: That tutorial was not deleted ^^
Thank you, everyone. ^_^

The script has now been updated. The underline and strikethrough lines, if the effect full is false, now work with alignment. I've also changed the y value for the underline. The underline now appears directly under the text's base line. This means that whatever text size you are using, the line will always be in the right spot.

Enjoy :)
Oh boy! I've updated the script once again for outline and shadowed text, all handled the same way. The outline and shadow colours are even handled the same way, too.

For a limited time (this weekend), I will be taking a few ideas to add. Don't make them too crazy; this is supposed to be easy to use ;)
You could try and improve your shadowing method slightly, allowing for full angles for the shadow. That could be kind of nice, also: Different fonts?
Oooh! I hope I can add this onto my other text script.
Bump and update. Fixed an error in my coding were Font.default_outline_color would not work.
First of all I <3 your script and I'm glad to be able to use it.

When I use your script, I use outline as the default text which works perfectly. But only when I use Wachunga's Multiple Windows script, I use a white windowskin with black font (the only place in the whole game that uses black font).

Is there a way to disable outlined text only when I use the message system? If you want, I can upload a demo.

Thanks in advance!
Well, you can set self.contents.font.outline = false in the message window. Simple, eh? :P
Thanks a lot! But...

In case I'm doing it wrong, I used Call script to put self.contents.font.outline = false but it brings up an error.

NoMethodError occured while running script.
undefined method 'contents' for #

Am I doing something wrong?
Not in a call script, in the message window class. Make sure it is below the declaration of contents.
Thank you! Works perfectly now!! =)
Hey Yey. I am using this for the outline and shadowed like i showed you in th enew battle screenshots. But it is lagging Text semi on the map, and lagging REALLY battle for Text boxes in the battle. Any suggestions or ideas for a fix?
The outline will cause some lag because it draw the text 9 times to get a good effect. I suggest only using the outline in sertain things. Never use it for in a window or sprite what will update often!
Well that sucks only reason why i got it was for the battles lol. But still a great script.
Nice work, most options lag game up but shadow works nice thanks ill credit you.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=10696