This tutorial explains how I managed to add a non-standard temporary font to an RPG Maker XP game at runtime without using the aging Win32API library approach.
This was achieved by using a PowerShell script to load the Font directly into the currently open windows session.
Meaning it temporarily installs a font that only lasts while the RPG Maker XP game session is running and is then discarded in a similar way to how the newer versions of RPG Maker work. This method uses a simple 'Custom Font Loaded' Module added to the Script Editor, which is then called at the beginning of the 'Main' Class script when the XP game runs.
I have had success using this method but provide no guarantees and it is supplied as is.
Steps Taken:
1) Make two subdirectories directly under your XP game directory.
One called: Font
One called: PowerShell
2) Copy your required .ttf font file into the Font directory.
3) Make a text file called LoadFont.ps1 in the PowerShell directory.
Then paste the contents of the PowerShell script below into that file and save it.
Code:
param( [Parameter(Mandatory=$true)] [string]$FontPath ) # --- LOAD FONT --- function Load-TemporaryFont { # Check the file exists if (-not (Test-Path -Path $FontPath -PathType Leaf)) { Write-Error "ERROR: Font file not found at: $FontPath" exit 1 # Exit with error code 1 } try { $AbsoluteFontPath = (Resolve-Path $FontPath).Path # Setup the Windows function... Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; public class FontLoader { [DllImport("gdi32.dll", CharSet = CharSet.Auto)] public static extern IntPtr AddFontResource(string lpszFilename); [DllImport("gdi32.dll", CharSet = CharSet.Auto)] public static extern int RemoveFontResource(string lpszFilename); [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff); public static readonly int WM_FONTCHANGE = 0x001d; } "@ # Load the font into the session $resourceHandle = [FontLoader]::AddFontResource($AbsoluteFontPath) if ($resourceHandle -ne 0) { # Broadcast the change to all windows so the game can detect it [FontLoader]::SendMessage([FontLoader]::HWND_BROADCAST, [FontLoader]::WM_FONTCHANGE, 0, 0) Write-Output "SUCCESS: Font successfully loaded for session: $AbsoluteFontPath" exit 0 # Exit successfully } else { Write-Error "ERROR: AddFontResource failed for unknown reason (File might be corrupted or in use)." exit 2 # Exit with error code 2 } } catch { # Catch any errors Write-Error "FATAL ERROR during font loading: $($_.Exception.Message)" exit 3 # Exit with error code 3 } } # Run the function Load-TemporaryFont
4) Open your RPG Maker XP game in the Game Maker editor application and go to the Tools --> Script Editor option (Or press the F11 key)
Once in the script editor scroll down to the end of the list where you see the 'Main' script.
5) Next we need to add the 'Custom Font Loader' module.
Make a new script just above the existing 'Main' script.
You can right-click on a script name and select insert to make a new script.
This will have a blank name field, use the 'Name' text box underneath the scripts list to rename this blank entry to 'Custom Font Loader'
Next paste the contents of the module into the newly created script.
Ruby:
#============================================================================== # ** Scene_FontLoaderPS #------------------------------------------------------------------------------ # This module executes the Font Load PowerShell script. #============================================================================== module FontLoaderPS def self.load_font(font_filename) font_path = File.expand_path("Font/#{font_filename}") ps_script = "start /b powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File \"PowerShell\\LoadFont.ps1\" -FontPath \"#{font_path}\"" #This needs to be called asynchronously or RMXP gets confused when the script closes. system(ps_script) end end
6) Finally you need to edit the 'Main' script and add the commands to call the PowerShell script loader and setup the custom font file at the beginning of this script.
Add these lines directly under the 'begin' keyword, E.g.
Ruby:
#============================================================================== # ** Main #------------------------------------------------------------------------------ # After defining each class, actual processing begins here. #============================================================================== begin #Set the Font FontLoaderPS.load_font("{filename}.ttf") #wait to mitigate the race condition... This may need to be changed for your game, sleep(0.3) $defaultfonttype = $fontface = $fontname = Font.default_name = "{Font Name}" $defaultfontsize = $fontsize = Font.default_size = 18
The
{filename}.ttf needs to be the name of the Windows Font file as it appears in the Fonts directory.
The
{Font Name} needs to be the actual proper name of the font, which can be different from the file name.
So remember these placeholders need to be replaced by the real names.
E.g.
FontLoaderPS.load_font("Mordred.ttf")
$defaultfonttype = $fontface = $fontname = Font.default_name = "Mordred"
If setup properly this should load the font into the current session when the game first runs.
If it works for you this approach is much more straightforward, simple and modular than the old Ruby scripting method.
And since the font was only loaded temporarily using PowerShell's AddFontResource, it will automatically be neatly removed when the RMXP game session is closed with no user involvement in setting up fonts.
P.s. I kept my Try Catch testing in the PowerShell script so you can see the testing I did. Technically you don't need that but it keeps it safe if the script can't run for whatever windows related reason there may be!
本贴来自国际rpgmaker官方论坛作者:Garryg处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:
https://forums.rpgmakerweb.com/threads/automatically-running-a-temporary-font-with-rmxp-using-powershell.181324/