FontSystems
From IfsoGUIWiki
Contents |
Using a Custom Font System (bitmap fonts)
General
You should be able to use just about any custom bitmap font system with ifsoGUI. This is supported by having 3 overridable functions that you can assign your own functions for.
Step 1 – Setup the ifsoGUI parameters and functions
GUI.SetUseCustomFontSystem(True)
This tells the GUI you are using a custom font system to handle the text drawing.
GUI.SetCustomDrawText(YourDrawTextFunction)
Pass in your custom DrawText Function. It must be like this:
MyDrawTextFunction(strText:String, x:Float, y:Float, gadget:ifsoGUI_Base)
This function is called to draw the actual text.
- strText is the text to be drawn.
- x and y are where the text should be drawn.
- gadget is a reference to the calling gadget. You can use this to determine which gadget made the call, allowing you can to use different fonts/effects for different gadgets. If gadget is Null, then the text being drawn is the Tip Text and should be drawn accordingly.
GUI.SetCustomTextHeight(YourTextHeightFunction)
Pass in your custom TextHeight Function. It must be like this:
MyTextHeightFunction:Int(gadget:ifsoGUI_Base)
This function is called to determine the height of the font.
- gadget is a reference to the calling gadget. You can use this to determine which gadget made the call, allowing you to use different fonts/effects for different gadgets. If gadget is Null, then the is being made for the Tip Text.
GUI.SetCustomTextWidth(YourTextWidthFunction)
Pass in your custom TextWidth Function. It must be like this:
MyTextWidthFunction:Int(strText:String, gadget:ifsoGUI_Base)
This function is called to determine how wide the text will be when drawn.
- strText is the text to determine the width of.
- gadget is a reference to the calling gadget. You can use this to determine which gadget made the call, allowing you to use different fonts/effects for different gadgets. If gadget is Null, then the is being made for the Tip Text.
Step 2 – Create your custom functions
Here is an example I created using ziggy’s FontMachine mod.
Import blide.fontmachine
This is at the top of the source to import the module.
Global BMFont:TBitmapFont = New TBitmapFont
BMFont.Load(“small.fmf”)
These two lines create a TBitmapFont object and load the small.fmf font into it.
GUI.SetUseCustomFontSystem(True)
This line tells ifsoGUI that we are using a custom font system.
GUI.SetCustomDrawText(myDrawText)
GUI.SetCustomTextHeight(myTextHeight)
GUI.SetCustomTextWidth(myTextWidth)
These three lines set my custom functions to be called by the GUI. These functions must return the correct types, and have the correct parameters.
Function myDrawText(strText:String, x:Float, y:Float, gadget:ifsoGUI_Base) BMFont.DrawText(strText, x, y) End Function
This is the function that will draw the text. ifsoGUI automatically only sends text to you that will fit where it needs to be drawn. Since I am only using one font for the entire app, I just ignore the gadget parameter. If I were using more than one font, I could check the gadgets name (gadget.Name) and react accordingly.
Function myTextHeight:Int(gadget:ifsoGUI_Base) Return BMFont.GetFontHeight() End Function
This is the function that returns the height of the font. This is not a height of a specific character, this is a general height used by the entire font. If your custom font system does not return this, then a good way to overcome this deficiency is too ask for the height of a string containing the highest and lowest character (ie. SomeFontSystem.TextHeight(“Wy”) ).
Function myTextWidth:Int(strText:String, gadget:ifsoGUI_Base) If strText.Length = 0 Return 0 If strText.Length = 1 Return BMFont.GetCharOffset(Asc(strText)) Return BMFont.GetTxtWidth(strText) – (BMFont.GetTxtWidth(Chr(strText[strText.Length – 1])) – BMFont.GetCharOffset(strText[strText.Length – 1])) End Function
This is the function that returns the width of a string. In this case, I found that FontMachine did something kind of strange. It had an offset value that was used as the width of the last character in the string, rather than all of the individual widths added up. So I found if I drew only 1 character, I needed the Offset of it and not the width. And, if I drew more than one character, I needed to subtract the difference between the width and the offset of the final character from the width.
So, as you can see, you may have to do a little experimentation to get the font system working correctly, but it should not be too difficult. And, if you need assistance making one work, let me know.
Drawing Partial Characters
There is another feature. In the example I showed, Font Machine is not capable of drawing a partial character. So, when drawing text in a textbox that is wider than the text box, only whole characters would be drawn, you would not see half of the letter that is at the edge of the textbox. This is fine, but ifsoGUI is capable of drawing partial characters.
If your custom font system is capable of this, then you would do the following:
GUI.SetCanDrawPartialChars(True)
This tells ifsoGUI your system is capable of controlling the drawing based on a viewport of sorts.
By setting this to true, ifsoGUI would then send you text in the DrawText function that goes beyond the current viewport. You would need to read the viewport and deal with drawing the partial text.
The current viewport in ifsoGUI can be read in the ifsoGUI_VP singleton.
- ifsoGUI_VP.vpX – The X value of the top left corner.
- ifsoGUI_VP.vpY – The Y value of the top left corner.
- ifsoGUI_VP.vpW – The width of the viewport.
- ifsoGUI_VP.vpH – The height of the viewport.
You could then use these values to have your custom font system draw partial characters.
There is another way
There is one other way to handle partial characters, if your custom font system does not handle partial characters, but it is written in BlitzMax and you have access to the source, you may be able to use the viewport drawing functions built into ifsoGUI.
ifsoGUI has two drawing command that are similar to the DrawImage and DrawImageRect commands of BlitzMax, but they do not draw out of the current viewport, they draw partial images.
So, if you have the source to your custom font system, you can change the DrawImage functions to:
ifsoGUI_VP.DrawImageArea(image:TImage, x:Float, y:Float)
Then you could set GUI.SetCanDrawPartialChars(True) and draw partial characters.
(As usual, feel free to contact me if you need help with any of this. I will be glad to help you get your custom font system working with ifsoGUI.)
