! *viewer/fontfunctions.pvc - Miscellaneous Font Functions  Object
! By Gord Davey
! (c) Copyright 2004-2005 Sage Software Canada Ltd.
! Version Info:
! Source Code File: $RCSfile: fontfunctions.pvs,v $   Version: $Revision: 1.9 $
!      Last Edited: $Date: 2005/10/14 15:31:18 $   Author: $Author: gordd $
!
!
def class "*viewer/fontfunctions"

    property DPI

    property Version$ = mid(mid("$Revision: 1.9 $",12), 1, len(mid("$Revision: 1.9 $",12)) - 2)

! returns a list of sizes: IE:"-40=80:30,-38=88:32" and so on
! Comma separated List is:  Size = Columns : Rows
! Steps from StartSize down to 2 points, defaults to 40 point starting size
! if not given
    function GetSizesForPage$(FontName$, StartSize, Width, Length, Margins$)

        enter (FontName$), (StartSize), (Width), (Length), (Margins$)

        if DPI <= 0 \
            then DPI = 120

        if Length <=0 or Width <= 0 or FontName$ = "" \
            then return ""

        if StartSize = 0 \
            then StartSize = -40
        if StartSize > 0 \
            then StartSize *= -1

        Channel = hfn

        open object (Channel)"*BITMAP*" + \
            ";DPI=" + str(DPI) + \
            ";Length=" + str(Length) + \
            ";Width=" + str(Width) + \
            tbl(Margins$ > "", "", ";Margins="+Margins$)

            !";FORCE6X10=Yes"


        for x = StartSize to -2 step 1
            print (Channel)'font'(FontName$,x),'df',
            c = mxc(Channel) + 1
            l = mxl(Channel) + 1
            Result$ += str(x) + "=" + str(c) + ":" + str(l) + ","
        next x

        close(Channel)

        if Result$ > "" \
            then Result$ = Result$(1, len(Result$) - 1)

        return Result$
!
! returns string "Size,WidthInGU,HeightInGU"
    function GetFontSizeForTextToFitRectangle$(Text$, FontName$, FontSize, FontAttrib$, FontRotation, \
                                       Width, Length, DefaultFont$, DefaultDPI, DefaultLPI, DefaultCPI)

    enter (Text$), (FontName$), (FontSize), (FontAttrib$), (FontRotation), \
          (Width), (Length), (DefaultFont$), (DefaultDPI), (DefaultLPI), (DefaultCPI)

    if Text$ = "" or FontName$ = "" or Width = 0 or Length = 0 \
        then return ""

    if DefaultLPI <= 0 \
        then DefaultLPI = 6

    if DefaultCPI <= 0 \
        then DefaultCPI = 10

    if FontSize = 0 \
        then FontSize = -200

    if DPI <= 0 \
        then DPI = DefaultDPI

    if DPI <= 0 \
        then DPI = 120

    Channel = hfn
    open object (Channel)"*BITMAP*" + \
        ";Length=" + str(Length) + \
        ";Width=" + str(Width) + \
        ";FORCE6X10=Yes" + \
        ";DPI="+str(DPI)

    print (Channel, err=*proceed) 'font'(DefaultFont$),'df','lpi'(DefaultLPI),'cpi'(DefaultCPI),
    lines = mxl(Channel) + 1
    columns = mxc(Channel) + 1
    GUColumns = @x(columns, Channel)
    GULines = @y(lines, Channel)

    while 1
        UseFont$ = FontName$ + "," + str(FontSize) + "," + FontAttrib$ + "," + str(FontRotation)

        TextWidth = txw(Text$, Channel, fnt=UseFont$)
        TextHeight = txh(Text$, Channel, fnt=UseFont$)

        _obj'GetSizeOfRotatedRectangle(SizeInX, SizeInY, TextWidth, TextHeight, FontRotation)

        if SizeInX < GUColumns and SizeInY < GULines \
            then break

        FontSize += 2
    wend

    close(Channel)
    return str(FontSize) + "," + str(TextWidth) + "," + str(TextHeight)
!
!
    function local GetSizeOfRotatedRectangle(SizeInX, SizeInY, Width, Height, Rotation)

        enter SizeInX, SizeInY, (Width), (Height), (Angle)
        SizeInX = 0, SizeInY = 0
        Radian = 57.29577951
        Angle |= 360

        if Width = 0 or Height = 0 \
            then return 0

        ang = Angle | 90
        hyp1 = Width / 2
        adj1 = (cos(ang / Radian) * hyp1)
        opp1 = (sin(ang / Radian) * hyp1)

        hyp2 = Height
        adj2 = (cos(ang / Radian) * hyp2)
        opp2 = (sin(ang / Radian) * hyp2)

        switch Angle
            case 0, 180
                SizeInX = Width
                SizeInY = Height
                break

            case 90, 270
                SizeInX = Height
                SizeInY = Width
                break

            case < 90
                SizeInX = adj1 * 2 + opp2
                SizeInY = opp1 * 2 + adj2
                break

            case < 180
                SizeInX = opp1 * 2 + adj2
                SizeInY = adj1 * 2 + opp2
                break

            case < 270
                SizeInX = adj1 * 2 + opp2
                SizeInY = opp1 * 2 + adj2
                break

            case <360
                SizeInX = opp1 * 2 + adj2
                SizeInY = adj1 * 2 + opp2
                break
        esac
    return 0
!
!
function CreatePageAsBitmap(Width, Length, Margins$)

    enter (Width), (Length), (Margins$)

    if DPI <= 0 \
        then DPI = 120

    Channel = hfn
        open object (Channel)"*BITMAP*" + \
            ";DPI=" + str(DPI) + \
            ";Length=" + str(Length) + \
            ";Width=" + str(Width) + \
            tbl(Margins$ > "", "", ";Margins="+Margins$)

    return Channel
!
!
function PrintPageAsBitmap(Channel, Data$)
    enter (Channel), (Data$)
    print (Channel, err=*proceed) Data$,
    return 1
!
!
function GetMaximums$(Channel)
    enter (Channel)
    columns = mxc(Channel, err=*proceed) + 1
    rows = mxl(Channel, err=*proceed) + 1
    colgus = @x(columns, Channel)
    rowgus = @y(rows, Channel)
    colgu = colgus / columns
    rowgu = rowgus / rows
    return str(columns) + ":" + str(rows) + ":" + str(colgus) + ":" + str(rowgus) + ":" + str(colgu) + ":" + str(rowgu)
!
!
function DestroyPageAsBitmap(Channel)
    enter (Channel)
    close(Channel)
    return 0


fnend
