Fixed on GIT Font.TextHeight

Rich

Well-Known Member
CX Code Contributor
3rd Party Module Dev
3rd Party Tool Dev
Joined
Sep 9, 2017
While looking at my font editor I noticed the following in graphics.cxs
Cerberus X:
    Method TextHeight:Float( text:String )
        Return _height
    End
text:string looks redundant or it not actually returning the max height (including offsets) for the individual letters
 

magic

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Mar 5, 2018
I wonder why in javascript, its easy to get width of individual alphabet but not for height.
A lot of people do a work around by returning the height of a letter 'M'. Its like an average height !@!$#
 

SimonVD

Member
Joined
Dec 22, 2019
Cerberus V2020-05-29 throws an error:

"Unable to find overload for TextHeight(String)"

Is there a way to calculate text height for a given font?

TextHeight Error:
Import mojo

Class MyApp Extends App
  
    Method OnRender()
        Cls()

        DrawText("Height: " + TextHeight("Testing"), DeviceWidth()/2, DeviceHeight()/2)
    End
End

Function Main()
    New MyApp
End
 

dawlane

Well-Known Member
CX Code Contributor
Joined
Jun 21, 2017
@SimonVD : TextHeight( "testing") doesn't work like that. It only works with a font object and is a bit pointless passing a string of text as the font glyph's would all be the same height. Not sure what that function parameter was going to be used for, but it doesn't actually do anything.
font example:
Import mojo

Class MyApp Extends App
    Field myFont1:Font
    Field myFont2:Font
    
    Method OnCreate()
        myFont1 = Font.Load( "myfont1.png", 32, 96, True )
        myFont2 = Font.Load( "mojo_font.png", 32, 96, True )   
      End
      
    Method OnUpDate()
    End
    
    Method OnRender()
        Cls()
        SetFont(myFont1)
        DrawText("Font 1 height: "+myFont1.TextHeight("Testing"), 0, 0)
        SetFont(myFont2)
        DrawText("Font 2 height: "+myFont2.TextHeight(""), 0, myFont1.TextHeight("") )
    End
End

Function Main()
    New MyApp
End
See https://www.cerberus-x.com/cxDocs/Modules_mojo.graphics_Font.html
 

Attachments

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
Replace the TextHeight method of the Font class with this code, that should do the trick.

Cerberus X:
    Method TextHeight:Float( text:String = "" )
        If text = ""
            Return _height
        Else
            Local h:=0.0
            For Local char:=Eachin text
                Local glyph:=GetGlyph( char )
                If Not glyph Continue
                If h < glyph.height Then h = glyph.height
            Next   
            Return h
        Endif
    End
 
Top Bottom