Font size

Monkey Forums/Monkey Programming/Font size

KawaCoder(Posted 2012) [#1]
How do we change font size in Monkey? I am using mojo font set.

Thank you.


smilertoo(Posted 2012) [#2]
Normally you'd generate a new font at the size you need.


Volker(Posted 2012) [#3]
The Monkey font system is just rudimental. Sooner or later you will have to choose another one. Search for Angelfont (example in bananas folder), Fontmachine (comes with BLIde) or the one Ignition comes with.
Then you use SetScale to draw the fonts bigger or smaller, which will
although work with the monkey font. Don't forget that SetScale
although transforms the position of the drawn text, so you need to recalc it.


smilertoo(Posted 2012) [#4]
I made myself a little font class but i've noticed its better to oversize them slightly and scale down, than to scale up.


KawaCoder(Posted 2012) [#5]
Thanks, guys. I will keep this in my Tips folder.


ziggy(Posted 2012) [#6]
Fontmachine comes with BLIde (if you already own it in case you come from BllitzMax world) but it also comes as part of the Jungle Ide license package for Monkey. More info here: http://www.jungleide.com/?page_id=515


smilertoo(Posted 2012) [#7]
Here's my little font class i made, it uses output from Spritefont2, just rename the Metrics.xml file to be the same as the font bitmaps name.
sorry about the indenting being removed by posting it here. *indenting put back thanks to muddy*

[monkeycode]
Import mojo
Import brl

'my font class
Class Rectangle
Field x:Int=0
Field y:Int=0
Field width:Int=1
Field height:Int=1
End

Class Cfont
Field name:String
Field fontImage:Image
Field characterPosition:Rectangle[256] 'location and size of each character on sprite sheet
Field spacing:Int=0
Field scaleX:Float=1, scaleY:Float=1
Field fontColorR:Int=255, fontColorG:Int=255, fontColorB:Int=255
Field shadow:Bool=false
Field shadowColorR:Int=128, shadowColorG:Int=0, shadowColorB:Int=0
Field shadowOffsetX:Int=4, shadowOffsetY:Int=4
Field shadowAlpha:Float=1.0

Field characterKEY:Int
Field characterX:Int
Field characterY:Int
Field characterWIDTH:Int
Field characterHEIGHT:Int

Method New()
Local count:Int

For count=0 To 255
characterPosition[count] = New Rectangle 'prepare rectangles to hold character sizing info
Next
End

Method loadFont:Int(_path:String, _ext:String="png")
fontImage = LoadImage(_path+"."+_ext)
If (stripXML(_path)) Then Return 1 'suffer this until i can get file writing working
Return 0
End

Method stripXML:int(_path:String, _ext:String="xml")
Local fileLines:StringList = New StringList
Local count:Int=0
Local file:String

file=LoadString (_path+"."+_ext) 'load in the xml file

If file 'if file loaded then start pulling it apart into lines
Local line:String [] = file.Split ("~n")

'strip file into lines
For Local thisline:String = Eachin line
fileLines.AddLast ( thisline.Trim () )
Next

'examine lines to remove xml formatting
For Local c:String = Eachin fileLines
If c.StartsWith("<character key") Then characterKEY=stripKey(c,16,2)
If c.StartsWith("<x") Then characterX=stripKey(c,3,4)
If c.StartsWith("<y") Then characterY=stripKey(c,3,4)
If c.StartsWith("<width") Then characterWIDTH=stripKey(c,7,8)
If c.StartsWith("<height") Then characterHEIGHT=stripKey(c,8,9)
If c.StartsWith("</character>") 'got all of data so build an entry
characterPosition[characterKEY].x = characterX
characterPosition[characterKEY].y = characterY
characterPosition[characterKEY].width = characterWIDTH
characterPosition[characterKEY].height = characterHEIGHT
endif
Next

Return 1
Else
Return 0
End If
End

Method stripKey:Int(key:String, front:Int=0, back:Int=0)
Local pad:String
Local len:Int
Local val:Int

pad=key[front..] 'strip off front of string
len=pad.Length()-back 'get length of what remains minus the back of string size
pad=pad[..len] 'strip off last back string length
val=Int(pad) 'convert to int

Return val
End

Method Spacing(s:Int)
spacing=s
End

Method setScale(x:Float=1.0, y:Float=1.0)
scaleX=x
scaleY=y
End

Method setShadow(s:Bool, a:Float=1.0, x:Int=4, y:Int=4)
shadow=s
shadowAlpha=a
shadowOffsetX=x
shadowOffsetY=y
End

Method setColor(r:Int, g:Int, b:Int)
fontColorR=r
fontColorG=g
fontColorB=b
End

Method setShadowColor(r:Int, g:Int, b:Int)
shadowColorR=r
shadowColorG=g
shadowColorB=b
End

Method Textsize:Int(text:String)
Local len=text.Length() 'get length of string to print
Local char:Int
Local x:Int=0

For Local count=0 To len-1 'loop through each character
char=text[count]
x=x+characterPosition[char].width 'move cursor to next position
x=x+spacing
Next
Return x
End

Method Drawfast(text:String, xpos:Int, ypos:Int, mode:Int=1)
Local len=text.Length() 'get length of string to print
Local char:Int
Local x:Int=xpos, y:Int=ypos

If mode=0 Then x=x-(Textsize(text)/2) 'centre align
If mode=-1 Then x=x-Textsize(text) 'right align
If mode=1 Then x=x 'left align

For Local count=0 To len-1 'loop through each character
char=text[count]

DrawImageRect(fontImage, x, y, characterPosition[char].x, characterPosition[char].y, characterPosition[char].width, characterPosition[char].height)

x=x+characterPosition[char].width 'move cursor to next position
x=x+spacing
Next

End

Method Draw(text:String, xpos:Int, ypos:Int, mode:Int=1)
Local len=text.Length() 'get length of string to print
Local char:Int
Local x:Int=xpos, y:Int=ypos, storeX:Int

If mode=0 Then x=x-((Textsize(text)/2)*scaleX) 'centre align
If mode=-1 Then x=x-(Textsize(text)* scaleX) 'right align
If mode=1 Then x=x 'left align
storeX=x 'remember x position

If (shadow=True) 'draw shadow text first if shadow is enabled
SetColor(shadowColorR, shadowColorG, shadowColorB)
If (shadowAlpha<>1.0) Then SetAlpha(shadowAlpha)

For Local count=0 To len-1 'loop through each character
char=text[count]

DrawImageRect(fontImage, x+shadowOffsetX, y+shadowOffsetY, characterPosition[char].x, characterPosition[char].y,
characterPosition[char].width, characterPosition[char].height, 0.0, scaleX, scaleY)

x=x+(characterPosition[char].width * scaleX) 'move cursor to next position
x=x+(spacing * scaleX)
Next
If (shadowAlpha<>1.0) Then SetAlpha(1.0)
endif

x=storeX 'restore the x position and draw normal text
SetColor(fontColorR, fontColorG, fontColorB)
For Local count=0 To len-1 'loop through each character
char=text[count]

DrawImageRect(fontImage, x, y, characterPosition[char].x, characterPosition[char].y, characterPosition[char].width, characterPosition[char].height,
0.0, scaleX, scaleY)

x=x+(characterPosition[char].width * scaleX) 'move cursor to next position
x=x+(spacing * scaleX)
Next

End
End

[/monkeycode]


muddy_shoes(Posted 2012) [#8]
If you wrap your code in code tags you can retain the formatting and even get highlighting.

[monkeycode]
Class MonkeyClass
Field monkeyField:Int = 0

Method MonkeyMethod:Void()
For Local i:Int = 0 Until 10
Print "Methodically Monkeying"
End
End
End
[/monkeycode]

The forum tags are here: http://www.monkeycoder.co.nz/Community/forum_codes.php


KawaCoder(Posted 2012) [#9]
Dear Monkey coders,

What I really need for my students to learn programming basics is the scalable font set in place of default monk font set. The monk fonts are white against black background. The fonts I want are white against transparent background so that students could use any colors for the console background (aka, canvas background) as well as different font sizes. See the example below.

I want to teach the students (who have ZERO programming skills) the programming fundamentals using the simplest pedagogy as possible. No OOP concepts will be introduced at the start.

Thank you.

PS I will start up a project at Google Code site for the package soon.

[monkeycode]
Strict

Import monkcon

Function Configure:Void()

' Put console properties here

TextColor(YELLOW_COLOR)
' TextSize(24) ' in pixels - not implemented
' BackgroundColor(GREEN_COLOR) ' not implemented
LeftMargin(40) ' in pixels
LineRate(20) ' lines per sec (animation)

End Function


Function Execute:Void()

' Put your script here

Console.Print "~nInterest Compound~nby Geo~n"

Local invest:Float = 1000 ' bucks at your own risk!

Console.Print "Year" + Pad("Balance", 10)

For Local year:Int = 1 To 30
invest *= 1.06 ' at interest rate of 6%
Local roundoff:Float = Int(invest * 100) / 100.0
Console.Print Pad(year, 4) + Pad("$" + roundoff, 10)
Next

End Function


Function Main:Int()
Configure() ' the console
Execute() ' your script
Console.Show() ' the results
Return 0
End Function
[/monkeycode]

The results



impixi(Posted 2012) [#10]
Presently, there's no *simple* cross-platform solution. But if you can make do with a HTML5-only solution that plays nice with Mojo, then you could use the HTML5 Canvas Text wrapper from the code archives. Save the 'canvastext.js' and 'canvastext.monkey' files in the same folder as your project's other monkey files, and call the functions as necessary.

You can change the font family, size, decoration, baseline and alignment via the provided functions. Colors, alpha, scaling and rotation are set by calling the usual Mojo functions. Use the DrawCanvasText function wherever you would normally use Mojo's DrawText function. The parameters are the same, except for xalign and yalign (they are redundant in this case). See the provided examples.

Again, this is a HTML5-only solution, so it may not be useful in your situation.


KawaCoder(Posted 2012) [#11]
@impixi

The wrapper for HTML5 you mentioned would be handy for HTML5 coders. However, I am writing a video tutorial for beginners using Monkey Demo Version as an educational tool. Once they understood the programming concepts fully, they may choose one of the other programming languages to further study.

Thank you.