[XP] Memory Font Loader

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

Previous topic - Next topic

Dust

Quote from: ForeverZer0 on May 24, 2014, 06:39:32 am
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.


It would be reaaally great if you could get this to work c: And no, I'm not running the Game.exe or RPG Maker itself in compatibility mode

Cremno

May 25, 2014, 11:22:37 am #21 Last Edit: May 25, 2014, 12:27:40 pm by Cremno
I don't know exactly why it doesn't work, but it's neither the fault of the (unnecessary) SendMessage call nor Windows 8. Does it work for you or someone else? It doesn't even work for me under the good old Windows XP SP3.

By the way, it works if you write your own Game.exe.

ForeverZer0

Yes, SendMessage should most definitely be used.

http://msdn.microsoft.com/en-us/library/aa911393.aspx

Quote from: MSDNRemarks

An application that adds or removes fonts from the system (for example, by using the AddFontResource or RemoveFontResource function) should send this message to all top-level windows.
To send the WM_FONTCHANGE message to all top-level windows, an application can call the SendMessage function with the hwnd parameter set to HWND_BROADCAST.


I am hardly going to rewrite Game.exe just to load a font from memory, that is ludicrous.

I only think that it could be the fault of Win 8 due to the fact that it worked perfectly for me before making a switch from Win7 to Win8, though it is obviously unconfirmed at this point.
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.

Cremno

May 25, 2014, 01:48:26 pm #23 Last Edit: May 25, 2014, 01:51:01 pm by Cremno
AddFontResource != AddFontResourceEx (especially with FR_PRIVATE)

I think RGSS1 has an internal font list which is initialized at start and that's why calling AddFont* functions don't work afterwards.

Heretic86

Methinks time for Blizz to step in...

I cant get this to work either.  Renaming fonts, case sensitive, spaces, putting ".ttf" in the name, and this box is XP-SP3.  I monkeyed with the scripts, but even after its there, using Font.exist?(name) I get nada.  AddFontResourceEx.call returns 1 so it says the font is loaded into memory.  Fantastic idea, and already implemented into VXA apparently, just can not get it to work.  Only thing I could find that was relevant was this:

https://github.com/Ancurio/mkxp

Way over my head...
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

Blizzard

Try AddFontResourceExA(), that's probably the reason. Using just AddFontResourceExW() will cause problems because of wide-chars.
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.

Heretic86

Still no luck.  Ex, ExA, ExW, no Ex, my brain just keeps coming up short.
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

ForeverZer0

I have yet to find a pattern to what works definitely and what does not. I do believe it has SOMETHING to do with the OS, but I cannot even say that for sure.

@heretic:
Just for a test, run the game as admin and see if has something to do with permissions or not. I have had success and not being an admin, put it could possibly be a combination of OS and UAC.
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.

Heretic86

October 11, 2014, 04:45:57 am #28 Last Edit: October 11, 2014, 05:16:07 am by Heretic86
Still doesnt work as Administrator either.

However, I do use a program for my VPN called PIA (Private Internet Access) and when I set it up and my firewall complained about it, it is also Ruby based, so not sure if that could somehow cause a conflict.  It may not sound like it might, but since RMXP uses system globals (like fonts), I wonder if the two might conflict?  I did exit the VPN program, and still didnt get any results.  Perhaps Adobe Flash?  I messed with some of the geek config settings, namely "DisableDeviceFontEnumeration", and think Flash shouldnt cause a conflict, but perhaps Font Enumeration?  Some sort of other Windows permission?  AVG?  All disabled for testing, and no luck on anything I've tried.  Shortcuts are not set to run in compatability mode, the advanced text services are not turned off.  Just related things, but dont seem to have any impact...
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

Duds

November 30, 2014, 07:13:57 pm #29 Last Edit: December 01, 2014, 09:38:14 am by Duds
Hi guys, this is my first post here. :haha:
I was very interested in this script so I gave a try on fixing it.
It works with AddFontResource (without Ex).


Spoiler: ShowHide


#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Memory Font Loader
# Author: ForeverZer0
# Version: 1.0
# Date: 1.8.2014
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#                             VERSION HISTORY
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# v.1.0   1.8.2014
#   - Original Release
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#
# 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
#
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

#===============================================================================
# ** 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

 AddFontResourceEx = Win32API.new('gdi32', 'AddFontResource', ['P'], 'L')
 SendMessage = Win32API.new('user32', 'SendMessage', ['L'] * 4, '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) > 0
       @@memory_fonts.push(f)
     end
   }
   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
 # Load fonts if using auto-load
 auto_load if AUTO_LOAD
end


Heretic86

+Rep

Changing AddFontResourceEx (ExA or ExW also) to just AddFontResource works perfectly!  At least on XP SP3, so Im not sure how that will affect Windows 7 / 8 / 8.1 but feedback on the proposed fix would be appreciated by many...
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

ForeverZer0

* Updates script to version 1.1 *

I added the fix that Duds pointed out to the script as a backup if the original method fails. If you were having troubles getting the script to work before, it may be worth your while to check again.

Thank you, Duds.  8)
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

I think that I can offer some more insight here after experimenting with this a bit.
The problem seems to be indeed that RMXP keeps a list of available fonts that it creates on load. AddFontResourceEx() can't work, because the fonts are uninstalled again right after it closes. AddFontResource() will work, because it temporarily installs the fonts until the next reboot. So yeah, I don't think this problem can be solved.
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.

Blizzard

*BUMP*

I just had an idea that I shared with Heretic:

Quote from: le meThere MIGHT be one last possible solution. You could reinstall the fonts every time the game runs and copy the file into the fonts directory regardless if it's already there. In that case you would just have to change the code a bit to avoid reporting missing fonts to users.
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.

F117Landers

I'm rather new to this, so perhaps someone can help:

what is "PATH_TO_FILE"? Is that to add a script from outside the Fonts folder into the game? or is that to actually set the font once it is loaded?

KK20

If you're not using AUTO_LOAD, you'll have to use that script call to add your fonts. Otherwise, just throw all your fonts into whatever Font folder you package your game with; you won't have to worry about making any script calls.

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!

F117Landers

July 11, 2016, 07:46:14 am #36 Last Edit: July 11, 2016, 08:05:13 am by F117Landers
Thanks KK20. I am using auto load, but the fonts weren't loading (Edit: using Font.default_name = "King's Gambit") and I had checked the font size, name (within the ttf), and capitalization already.

ThallionDarkshine

So I seem to have found a bug with this on Windows 8.1. After the call to
SendMessage
, the game freezes. Have you tried with just
SendNotifyMessage
? From what I can tell in the docs,
SendMessage
blocks until all windows process the notification, whereas
SendNotifyMessage
returns immediately... I tried removing the
SendMessage
call, and the game appears to run fine. Is there a reason you're making both calls?

maathieu

Hi,

I have been asked by a friend developing an RPGMaker XP game to look into this script in order to get it to load a user-defined font at runtime. Reading the forum here, I can't really figure out why it seems to work for some people, and not for others.

I am doing the testing on Wine simulating Windows XP. Ruby properly calls AddFontResource, SendMessage, and SendNotifyMessage; but after that, the font is not made available to the game. I have read that RPGMaker XP maintains an internal "list of fonts", could it be possible that said list is loaded before the script for some verions of RPGMaker XP, and after it for others? I use version 1.03 here.

If the "list" hypothesis is right, is there any way to access the internal list and manually add the font name in it?

Cheers,

Maathieu

Blizzard

I think you're correct about the internal list IIRC. But that's exactly what AddFontResource, SendMessage and SendNotifyMessage are supposed to handle and change. So I'm not sure what's wrong. I assume it's some internal bug in XP. XP's been around since, well, Windows XP and the tighter permission restrictions have only been added in Windows Vista and later and I wouldn't be surprised if permissions are what's causing the issue in the first place. So it's likely that this is an issue that would require an update to XP to reliably fix. But the original developers don't maintain it anymore so there's not much hope that this will be fixed.

You may want to consider using XPA. Ever since I converted my project to XPA, I haven't had any font issues anymore. The VXA engine handles fonts so much better than previous versions of RPG Maker.
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.