c++linuxubuntufltk

FLTK - Why fl_width function returns -1 for the length of a char array on a Linux machine?


I am using the fl_width function to create widgets whose size depends on their label (e.g., some Fl_Box). In the documentation for this function one reads

FL_EXPORT double fl_width (const char *txt)

Returns the typographical width of a nul-terminated string using the current font face and size.

I encountered anyway some strange behaviors on my Linux machine when I run even a simple program as the following one.

#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>

#include <iostream>

int main(){

    Fl_Double_Window* W = new Fl_Double_Window(200,100,"Test");

    W->begin();

        // Creation of the char array
        const char*  s1 = "text";

        // Usage of the fl_width
        double I1  = fl_width(s1);

        // Print the result
        std::cout << I1 << std::endl;

        // A Fl_Box for comparinson
        Fl_Box* B1 = new Fl_Box(50,10,100,30,"New Box");
        B1 -> box(FL_UP_BOX);

        // Another box whose width depends on the length of its label: 
        Fl_Box* B2 = new Fl_Box(50,50,(int)I1,30,s1);
        B2 -> box(FL_UP_BOX);

    W->end();
    W->show();
    return  Fl::run();

}

In this case I get -1 as a result for the length of s1, as it is evident also from the second Fl_Box that actually does not appear.

Same result when I use a std::string with its c_str() method. Where did I get wrong? Have I have to include some other header files?

My setup is the following: Ubuntu 20.04.04, FLTK 1.3.8, gcc version 9.4.0.


On other projects on my MacBook Pro (MacOS 11) I did not have any problems.


Solution

  • One should invoke fl_font(Fl_Font face, Fl_Fontsize fsize) first.

    As user7860670 suggested in the comments to the question, one have to invoke fl_font(Fl_Font face, Fl_Fontsize fsize) for having a proper behaviour for the fl_width() function. This is also explained in the documentation of fl_font and in the (local) header file fl_draw.H.

    The example code below shows how to call this function and how setting different sizes impacts on the further computations.

    #include <FL/Fl.H>
    #include <FL/fl_draw.H>
    #include <FL/Fl_Double_Window.H>
    #include <FL/Fl_Box.H>
    
    #include <iostream>
    
    int main(){
    
        Fl_Double_Window* W = new Fl_Double_Window(200,150,"Test");
    
        W->begin();
    
            // Creation of the char array
            const char*  s1 = "text";
            fl_font(0,20);
            std::cout <<  "The initial size is set to " << fl_size() << std::endl;
    
            // Usage of the fl_width
            double I1  = fl_width(s1);
    
            // Print the result
            std::cout << "The length hence is " << I1 << std::endl;
    
            // A Fl_Box for comparinson
            Fl_Box* B1 = new Fl_Box(50,10,100,30,"New Box");
            B1 -> box(FL_UP_BOX);
            // Another box whose width depends on the length of its label: 
            Fl_Box* B2 = new Fl_Box(50,50,(int)I1,30,s1);
            B2 -> box(FL_UP_BOX);
    
            // Another Box which shows that actually fl_width() returns a number depending on the size of the font
            fl_font(0,40);
            std::cout <<  "The new size is set to " << fl_size() << std::endl;
            double I2  = fl_width(s1);
            std::cout << "The new length hence is " << I2 << std::endl;
            Fl_Box* B3 = new Fl_Box(50,90,(int)I2,30,s1);
            B3 -> box(FL_UP_BOX);
    
        W->end();
        W->show();
        return  Fl::run();
    
    }