Announcement

Collapse
No announcement yet.

Text centering - custom fonts

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Text centering - custom fonts


    Hello everyone,

    I would like to know which is the best way (or the way) to center a text using custom fonts.

    For printing custom fonts I followed the way described in Application note - 4D-AN-P3001-ViSi-Displaying-Third-Party-Fonts.



    var hstrings; //handle to access uSD strings
    var hFont1,hFont2; //handles to access uSD fonts

    hFont1:= file_LoadImageControl("Test.da1","Test.gc1",1); //Open handle to access uSD fonts
    hFont2:= file_LoadImageControl("Test.da2","Test.gc2",1); //Open handle to access uSD fonts
    hstrings:= file_Open("IRemo.txf",'r'); //Open handle to access uSD strings

    txt_FGcolour(WHITE) ; //text color
    txt_BGcolour(BLACK) ; //text background

    gfx_MoveTo(20,20); //printing position

    txt_FontID(hFont1); //first font type
    print("I am Font1\n"); //print with font1

    txt_FontID(hFont2); //second font type
    print("I am Font2\n"); //print with font2

    Have a nice day!
    Chris



  • #2


    wid := strwidth(str_Ptr(string)) ;
    gfx_MoveTo((fieldwidth-wid)/2,0) ;
    print([STR] string) ;

    Or something like that
    Mark

    Comment


    • #3


      Thanks for your (as always) quick replay. I'll try it as soon as I can.

      Have a nice weekend!
      Chris

      Comment


      • #4


        Hi!
        The code is not working fine every time - I'll explain below:

        This one works fine (see how the variables are defined):

        var textCh1,textCh2,textCh3,textCh4,textCh5,textCh6,textCh7,textCh8; //var definition


        func main()

        textCh1:= "Channel 1";
        textCh2:= "Channel 2";
        textCh3:= "Channel 3";
        textCh4:= "Channel 4";
        textCh5:= "Channel 5";
        textCh6:= "Channel 6";
        textCh7:= "Channel 7";
        textCh8:= "Channel 8";

        hFont1:= file_LoadImageControl("Test.da1","Test.gc1",1); //Open handle to access uSD fonts
        hstrings:= file_Open("Test.txf",'r'); //Open handle to access uSD strings


        repeat

        // do something

        forever

        endfunc

        //Function called for printing centered text
        func PrintText(var text,var yPos)//print centered text
        var wid;
        txt_FGcolour(WHITE); //text color
        txt_BGcolour(BLACK); //text background
        txt_FontID(hFont1); //append custom Font
        wid := strwidth(str_Ptr(text));
        gfx_MoveTo((480-wid)/2,yPos);
        print([STR] text);
        endfunc





        This one doesn't work fine (sometimes text is at the rigt side, other times at left side or centered - depending to the position in code of variables definition):



        var textCh1[10],textCh2[10],textCh3[10],textCh4[10],textCh5[10],textCh6[10],textCh7[10],textCh8[10];//var definition


        func main()

        to (textCh1);
        putstr ("Channel 1");
        to (textCh2);
        putstr ("Channel 2");
        to (textCh3);
        putstr ("Channel 3");
        to (textCh4);
        putstr ("Channel 4");
        to (textCh5);
        putstr ("Channel 5");
        to (textCh6);
        putstr ("Channel 6");
        to (textCh7);
        putstr ("Channel 7");
        to (textCh8);
        putstr ("Channel 8");


        hFont1:= file_LoadImageControl("Test.da1","Test.gc1",1); //Open handle to access uSD fonts
        hstrings:= file_Open("Test.txf",'r'); //Open handle to access uSD strings


        repeat

        // do something

        forever

        endfunc

        //Function called for printing centered text
        func PrintText(var text,var yPos)//print centered text
        var wid;
        txt_FGcolour(WHITE); //text color
        txt_BGcolour(BLACK); //text background
        txt_FontID(hFont1); //append custom Font
        wid := strwidth(str_Ptr(text));
        gfx_MoveTo((480-wid)/2,yPos);
        print([STR] text);
        endfunc

        The second example is what I really need since the data that I have to put into textCh buffers is loaded from uSD card with file_GetS() function which needs a sized buffer in order to get data from file.

        It's an odd behavior for I don't have a clue and I need help.

        Best regards,
        Chris

        Comment


        • #5


          How are you calling PrintText in the second example?
          Mark

          Comment


          • #6


            Hi Mark,

            After receiving data from Com Port, I have a 'Processing data' routine with following code (just a part of it):

            // Process ComPort Rx data
            func ProcessRxData(var rData)
            var ptr;
            if (rData != 0)
            ptr:=str_Ptr(RxDatabuffer); //rise pointer for Rx buffer

            //Some more code here

            //Check Channels
            if (str_Find(&ptr,"C")) //If string starts with C
            if (str_Match(&ptr,"C1")) //Ch1
            currentCh:=1;
            else if (str_Match(&ptr,"C2")) //Ch2
            currentCh:=2;
            else if (str_Match(&ptr,"C3")) //Ch3
            currentCh:=3;
            else if (str_Match(&ptr,"C4")) //Ch4
            currentCh:=4;
            else if (str_Match(&ptr,"C5")) //Ch5
            currentCh:=5;
            else if (str_Match(&ptr,"C6")) //Ch6
            currentCh:=6;
            else if (str_Match(&ptr,"C7")) //Ch7
            currentCh:=7;
            else if (str_Match(&ptr,"C8")) //Ch8
            currentCh:=8;
            endif
            PrintChannel(currentCh);
            endif

            //Some more code here

            endif

            endfunc


            PrintChannel function looks like that:


            func PrintChannel(var chNumber)
            if (chNumber == 1) //Ch1
            PrintText(textCh1);
            else if (chNumber == 2) //Ch2
            PrintText(textCh2);
            else if (chNumber == 3) //Ch3
            PrintText(textCh3);
            else if (chNumber == 4) //Ch4
            PrintText(textCh4);
            else if (chNumber == 5) //Ch5
            PrintText(textCh5);
            else if (chNumber == 6) //Ch6
            PrintText(textCh6);
            else if (chNumber == 7) //Ch7
            PrintText(textCh7);
            else if (chNumber == 8) //Ch8
            PrintText(textCh8);
            endif
            endfunc
            And, of course the print Text Function:



            func PrintText(var text)
            var wid;
            gfx_RectangleFilled(0,14,480,54,BLACK); //erase previous data
            txt_FGcolour(WHITE); //text color
            txt_BGcolour(BLACK); //text background
            txt_FontID(hFont1); //custom font
            wid := strwidth(str_Ptr(text)); //string width
            gfx_MoveTo((480-wid)/2,14); //centering calculation
            print([STR] text);
            endfunc
            I used all these because textCh1 variables are subject to change during program execution, also they will by saved on uSD card and loaded at start-up and PrintChannel function will be called from different sections of program, not only when some data is received from Com Port.

            Thanks!
            Chris

            Comment


            • #7


              Sorry it took me so long to realise this (and it was my untested code that displayed the problem in the first place ) .... strwidth takes a word pointer, not a string (byte) pointer...

              so
              wid := strwidth(str_Ptr(text)); //string width

              should be
              wid := strwidth(text); //string width
              Mark

              Comment


              • #8


                Thanks Mark!

                I'm more familiar with Assembly than C or other mid or high level programming languages. After your comment it's clear where is the problem and if I had a bit more knoledge of C, I would probably find myself what was wrong.

                Any way I am glad that is somebody out there offering great support for us that we're struggling to learn.

                Regards,
                Chris

                Comment

                Working...
                X