Thread: saving font

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    saving font

    I have an application that uses fonts, user can change the look of fonts and
    their color. I want to save those font settings in a file, so each time user runs
    my program the same fonts appear...
    I have a LOGFONT struct full of data that needs to be saved in a file, and loaded.
    Heres how i do it:
    Code:
    // General
    using namespace std;
    #include <fstream>
    LOGFONT font;
    ...........
    ...........
    // Loading
    ifstream fin;
    fin.open("config.txt");
    fin>>font.lfFaceName;
    fin>>font.lfCharSet;
    fin>>font.lfClipPrecision;
    fin>>font.lfEscapement;
    fin>>font.lfOrientation;
    fin>>font.lfHeight;
    fin>>font.lfItalic;
    fin>>font.lfStrikeOut;
    fin>>font.lfUnderline;
    fin>>font.lfOutPrecision;
    fin>>font.lfPitchAndFamily;
    fin>>font.lfQuality;
    fin>>font.lfWeight;
    fin>>font.lfWidth;
    ...........
    ...........
    // Saving
    ofstream fout;
    fout.open("config.txt");
    fout<<font.lfFaceName<<endl;
    fout<<font.lfCharSet<<endl;
    fout<<font.lfClipPrecision<<endl;
    fout<<font.lfEscapement<<endl;
    fout<<font.lfOrientation<<endl;
    fout<<font.lfHeight<<endl;
    fout<<font.lfItalic<<endl;
    fout<<font.lfStrikeOut<<endl;
    fout<<font.lfUnderline<<endl;
    fout<<font.lfOutPrecision<<endl;
    fout<<font.lfPitchAndFamily<<endl;
    fout<<font.lfQuality<<endl;
    fout<<font.lfWeight<<endl;
    fout<<font.lfWidth<<endl;
    And this doesn't work as always... Well sometimes it does, but mostly the
    structure is filled with very wierd data.
    Why doesn't this work? How should it work?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Doesn't anyone have an idea how to save font?
    Any way of doing it would be cool... just that i can save it somehow.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Make sure you check that the lfFaceName member of the LOGFONT struct does not exceed 32 characters, including the NULL terminator as described for that struct in msdn. You should check before you save and when you load the LOGFONT to and from file.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    The registry, maybe?

    Have you looked into registry programming. That's usually where programs store a font. Or maybe you could write the 'LOGFONT' object to a pre-created file (such as 'data/font.dat') and reading it when your program starts. And writing the font to the file when it changes. ...

    Just some suggestions. =)

  5. #5
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    I have noted that font's with short names are working. This seems to be the problem.
    Should i then add this line
    Code:
    font.lfFaceName[31] = NULL;
    before saving the font? ( Tryed and not working )
    I'm using the CreateFontIndirect(&font); function to create font from my LOGFONT structure.
    How can i shorten this lfFaceName anyway, and is it going to work shortened?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Use the constant provided since it's defined anyway:
    Code:
    font.lfFaceName[LF_FACESIZE-1]='\0';
    >>How can i shorten this lfFaceName<<

    You're not looking to shorten lfFaceName, you're looking to make sure that you don't exceed its array bounds by reading something into it from file that may be longer than LF_FACESIZE characters.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    ok, now it all works.
    thanks.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. problem with my font manager
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 03-07-2006, 08:03 AM
  3. Font rendering in Direct3D
    By VirtualAce in forum Game Programming
    Replies: 3
    Last Post: 10-17-2005, 09:56 AM
  4. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  5. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM