[XP] Memory Font Loader

Started by ForeverZer0, January 08, 2014, 08:46:25 am

Previous topic - Next topic

ForeverZer0

January 08, 2014, 08:46:25 am Last Edit: December 17, 2014, 07:35:32 am by ForeverZer0
Memory Font Loader
Authors: ForeverZer0
Version: 1.1
Type: Game Utility
Key Term: Game Utility



Introduction

Small script that allows for loading and using fonts into memory at run-time without the need for installing them on the operating system. The advantage of this script over others, such as Wachunga's font-install script, is that it does not require a administrator privileges to use the fonts, nor a restart of the game after running.



Features


  • Automatically load all fonts found in user-defined folder

  • Supports TrueType (*.ttf) and OpenType(*.otf) fonts

  • Does not require Administrator privileges

  • Does not require a game restart

  • Plug & play




Screenshots

None.


Demo

None.


Script

Spoiler: ShowHide

#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Memory Font Loader
# Author: ForeverZer0
# Version: 1.1
# Date: 12.17.2014
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#                             VERSION HISTORY
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# v.1.0   1.8.2014
#   - Original Release
# v.1.1   12.17.2014
#   - Improved compatibility on different systems by adding "AddFontResource"
#     as backup call if "AddFontResourceEx" fails
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#
# Introduction:
#
#   Small script that allows for loading and using fonts into memory at run-time
#   without the need for installing them on the operating system. The advantage
#   of this script over others, such as Wachunga's font-install script, is that
#   it does not require a administrator privileges to use the fonts, nor a
#   restart of the game after running.
#
# Features:
#
#   - Automatically load all fonts found in user-defined folder
#   - Supports TrueType (*.ttf) and OpenType(*.otf) fonts
#   - Does not require Administrator privileges
#   - Does not require a game restart
#
# Instructions:
#   
#   - Place script anywhere above main
#   - Some optional configuration below with explanation
#   - Use the script call "Font.add(PATH_TO_FILE)" to add a font.
#   - If using auto-load, create a folder for the fonts ("Fonts" by default),
#     and place all font files within it. Loading will be done automatically.
#
# Compatibility:
#
#   - No known compatibility issues with any other script.
#
# Credits/Thanks:
#
#   - ForeverZer0, for the script
#   - Duds, for pointing out the "AddFontResource" fix
#
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

#===============================================================================
# ** Font
#-------------------------------------------------------------------------------
# The font class. Font is a property of the Bitmap class.
#===============================================================================
class Font
 
  #-----------------------------------------------------------------------------
  #                            CONFIGURATION
  #-----------------------------------------------------------------------------
 
  # This value (true/false) indicates if all fonts in the specified fonts folder
  # will be automatically loaded when the game is started.
  AUTO_LOAD = true
   
  # Set this to the name of the folder, relative to the game directory, where
  # the font files are contained. This folder will be searched for font files
  # when using the auto-load feature
  FONT_FOLDER = 'Fonts'
 
  #-----------------------------------------------------------------------------
  #                           END CONFIGURATION
  #-----------------------------------------------------------------------------
 
  #-----------------------------------------------------------------------------
  # * Constants
  #-----------------------------------------------------------------------------
  WM_FONTCHANGE = 0x001D
  HWND_BROADCAST = 0xFFFF
  AddFontResource = Win32API.new('gdi32', 'AddFontResource', ['P'], 'L')
  AddFontResourceEx = Win32API.new('gdi32', 'AddFontResourceEx', 'PLL', 'L')
  SendMessage = Win32API.new('user32', 'SendMessage', 'LLLL', 'L')
  SendNotifyMessage = Win32API.new('user32', 'SendNotifyMessage', 'LLLL', 'L')
  #-----------------------------------------------------------------------------
  # * Class Variables
  #-----------------------------------------------------------------------------
  @@memory_fonts = []
  #-----------------------------------------------------------------------------
  # * Load font(s) into memory
  #     filename  : Full path to font file. Accepts multiple filenames.
  #-----------------------------------------------------------------------------
  def self.add(*filenames)
    filenames.each {|f|
      next if @@memory_fonts.include?(f)
      if AddFontResourceEx.call(f, 16, 0) > 0 || AddFontResource.call(f) > 0
        @@memory_fonts.push(f)
      end
    }
    SendNotifyMessage.call(HWND_BROADCAST, WM_FONTCHANGE, 0, 0)
    SendMessage.call(HWND_BROADCAST, WM_FONTCHANGE, 0, 0)
  end
  #-----------------------------------------------------------------------------
  # * Automatically seach and load all font files found in the font folder.
  #-----------------------------------------------------------------------------
  def self.auto_load
    dir = FONT_FOLDER == '' ? Dir.pwd : File.join(Dir.pwd, FONT_FOLDER)
    return unless File.directory?(dir)
    files = Dir.glob(File.join(dir, '*.{ttf,otf}'))
    self.add(*files)
  end
end

# Load fonts if using auto-load
if Font::AUTO_LOAD
  Font.auto_load
end



Instructions

Place script anywhere above main. Some optional configuration and instructions within the script.


Compatibility

No known compatibility issues, should have been solved with version 1.1.  


Credits and Thanks


  • ForeverZer0, for the script

  • Duds, for pointing out the "AddFontResource" fix




Author's Notes

Please feel free to ask for support, post bugs, or anything else. Enjoy!
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

PhoenixFire

   I currently have absolutely no use for this, but now that this exists, I may just change the fonts in my game so that I can use this  :^_^':
Quote from: Subsonic_Noise on July 01, 2011, 02:42:19 amNext off, how to create a first person shooter using microsoft excel.

Quote from: Zeriab on September 09, 2011, 02:58:58 pm<Remember when computers had turbo buttons?

Zexion

... Words cannot describe how much I love you right now :P
Nice job F0! I'm sure that there will be tons of use of this script!

Jamal XVI

So i tested and it didn't work.I've just pasted the script and put a code 'Font.default_name = "Font Name"', but it won't work,
What should i do?
Thanks,
Jamal XVI

ForeverZer0

Make sure the name of the font is correct. The name of the font and the filename are not always the same.

Try with this, I have confirmed that this name is the same. Simply use the name "Slant".
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Jamal XVI

The same problem.Something must be wrong.
Look at this code:
module GTBS
  #=============================================================#
  #                       ENGINE SETTINGS                       #
  #=============================================================#
  def self.font
    return "slant"#Fonts::Names[0]
  end
end

and the main
begin
  #intalar_fonts
  $data_configuracoes = Configuracoes.new
  # Prepare for transition
  GMRK::API.disable_sys_key(1)
  Graphics.freeze
  # Make scene object (title screen)
  $scene = Scene_Perfis.new
  Font.default_name = GTBS::font  #############################################Here#################################
  Font.default_size = 22
  Font.default_bold = true
  # Call main method as long as $scene is effective
  while $scene != nil
    $scene.main
  end
  # Fade out
  Graphics.transition(20)
rescue Errno::ENOENT
  # Supplement Errno::ENOENT exception
  # If unable to open file, display message and end
  filename = $!.message.sub("No such file or directory - ", "")
  print("Unable to find file #{filename}.")
end

i wondering why it didn't work.Hope you can help me   :)
Thanks,
Jamal XVI

ForeverZer0

Does it work in a new project, as in a blank new project, without any added scripts?
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

KK20


Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Jamal XVI

Quote from: ForeverZer0 on January 21, 2014, 12:58:34 pm
Does it work in a new project, as in a blank new project, without any added scripts?

A new project main
#==============================================================================
# Main
#------------------------------------------------------------------------------
# Após o final de cada definição de classe o processo principal
# do jogo é iniciado. Esta é a classe principal do jogo.
#==============================================================================

begin
 
 # É definida aqui a fonte usada nas janelas do jogo
 # Você pode usar aqui qualquer fonte disponível em seu computador
 # Porém é recomendável usar fontes padrão do Windows
 # Por exemplo: Arial, Lucida Console, Tahoma, Verdana...
 
 $defaultfonttype = $fontface = $fontname = Font.default_name = "Slant"
 # É definido aqui o tamanho da fonte usada nas janelas de jogo
 # O tamanho padrão é 25. Porém, você pode usar o tamanho que você
 # achar melhor para seu sistema e fonte
 
 $defaultfontsize = $fontsize = Font.default_size = 22
 
 # É preparada uma transição de tela
 
 Graphics.freeze
 
 # Aqui é chamada a tela título do jogo. O script padrão
 # de título é 'Scene_Title'. Você pode mudá-lo, mas isso não é
 # recomendável
 
 $scene = Scene_Title.new
 
 # É definida a limitação efetiva da variável $scene.
 # Se esta é nula, é chamado o método principal
 
 while $scene != nil
   $scene.main
 end
 
 # A transição de tela é executada
 
 Graphics.transition(20)
rescue Errno::ENOENT
 
 # Aqui, definimos a mensagem padrão para Errno::ENOENT
 # Quando não é possível abrir um arquivo, a mensagem é exibida
 
 filename = $!.message.sub("Arquivo não encontrado - ", "")
 print("O Arquivo #{filename} não foi encontrado.")
end

Results: ShowHide
So i put a print in the method self.auto_load and the result was:

so i started the game and...

I can't imagine what problem making this, but it wont work.
Thanks,
Jamal XVI

ForeverZer0

Strange.
I will have to see if maybe there is some issue with WinXP, or perhaps something else I am missing.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Jamal XVI

So i got the problem.
The font name "slant.ttf" has to be the name name of the font "Slant" as the same in RGGS,but the font name is case sensitive to.
Then the file name must be "Slant.ttf" and in RGSS the font name must be "Slant"
Thanks,
Jamal XVI

Zexion

Here's a tip, doesn't always work but will for the most part. In your system fonts folder, double click the font in question and it should say the fonts real name inside. A lot of font files are named one thing but are actually something else. A file like "sans bold" could actually be called "sanserif bold" and rgss won't pick it up.

PrinceEndymion88

I've added the Joystix Monospace.ttf font into Fonts folder and into UMS script I've edited
@font = "Arial"
into
@font = "Joystix Monospace"
but I can't see the font when I play the game... where I'm wrong?

Zexion

I think it might be related to the post I made above you. The font is named Joystix Monospace.ttf  but the real font probably has a different name like joystix mono.
Double click it and read where it says Font Name:

PrinceEndymion88

I had already checked out, the real name is Joystix Monospace.

Nome tipo carattere = Font Name (in italian  :haha:)

Dust

Doesn't work for me too, sadly :/ I tried it with the Slant-font aswell, even renaming it and trying other fonts, but the font won't be loaded. I'm using Windows 8.1 by the way.

ForeverZer0

I think it does have something to do with the new "upgrade" that is Windows 8.
I wrote the script under Windows 7, and now that I have remade a switch back to Windows 8, it is not working for me either.  AddFontResourceEx is executing correctly, but using SendMessage with the broadcast is failing, returning a generic error code of "1", which doesn't explain much other than "I did not work".

I will take a look into it soon when I get a chance. One more reason Windows 8 sucks.
Just out of curiosity, are you running the game in compatibility mode or anything? That shouldn't be a problem, but may help track down the cause of the error.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

orochii

I don't care if this script works (well, actually I do, but...). I love this kind of scripts where I don't get a shit of how does it works. I can imagine, and I could probably, like, write it later from scratch (it's pretty short anyway). Because the idea is pretty simple (though I still don't understand why the SendMessage command is used, something about a protocol?). But as in "thought that it could be done this way", nope. Not without a lot of research, and there is where the beautifulness of this code is. As in, how the hell did you came up with this solution xD.

This is wizardry!

Blizzard

Quote from: orochii on May 24, 2014, 06:23:23 pm
(though I still don't understand why the SendMessage command is used, something about a protocol?).


You need to let the running apps in the OS know "hey, dudes, there's a new kid font on the block". SendMessage() does exactly that.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.