Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Stream for reading Qt Resources (font) - failed to create the font face  (Read 3908 times)

0 Members and 1 Guest are viewing this topic.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Hi,

Here's my code:

qresourcestream.hpp
#ifndef QRESOURCESTREAM_H
#define QRESOURCESTREAM_H

#include <SFML/System.hpp>
#include <QFile>

namespace qsf
{
    class QResourceStream
        : public sf::InputStream
    {
    public:
        QResourceStream();
        ~QResourceStream();

        bool open(const char* path);

        virtual sf::Int64 read(void* data, sf::Int64 size);
        virtual sf::Int64 seek(sf::Int64 position);
        virtual sf::Int64 tell();
        virtual sf::Int64 getSize();

    private:
        void * dat;
        sf::Int64 pos;
        sf::Int64 size;
    };
}
#endif // QRESOURCESTREAM_H
 

qresourcestream.cpp
#include "qresourcestream.hpp"

#include <iostream>
namespace qsf
{
    QResourceStream::QResourceStream()
    {
        this->pos = 0;
        this->size = 0;
    }
    QResourceStream::~QResourceStream()
    {
        if(size != 0)
            free(dat);
    }

    bool QResourceStream::open(const char* path)
    {
        QFile file(path);
        if(!file.open(QIODevice::ReadOnly))
            return false;

        this->size = file.size();
        dat = malloc(file.size() + 1);

        file.read((char*) dat, file.size() + 1);
        file.close();

        return true;
    }

    sf::Int64 QResourceStream::read(void *data, sf::Int64 size)
    {
        sf::Int64 i;
        for(i = 0; i <= size && i + pos < this->size; i++, pos++)
            ((char*)data)[i] = ((char*)dat)[i + pos];

        return i;
    }
    sf::Int64 QResourceStream::seek(sf::Int64 position)
    {
        pos = position;
        return tell();
    }
    sf::Int64 QResourceStream::tell()
    {
        return pos;
    }
    sf::Int64 QResourceStream::getSize()
    {
        return this->size;
    }
}
 

While loading font i get that error:
Failed to load font from stream (failed to create the font face)

That's how I use my stream:
qsf::QResourceStream fontStream;
fontStream.open("://BitFont.ttf");
bitFont.loadFromStream(fontStream);
 

I have no idea what's wrong, but it's propably one of those virtual functions.

Btw that code works ok:
QFile fontFile("://BitFont.ttf");
fontFile.open(QIODevice::ReadOnly);
fontDat = malloc(fontFile.size() + 1);
fontFile.read((char*) fontDat, fontFile.size() + 1);

bitFont.loadFromMemory(fontDat, fontFile.size() + 1);
 

Any ideas?

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Wow, it was so stupid mistake.  >:(
I was working on that code too late & tried to solve 1 problem in 2 ways... at the same time.

sf::Int64 QResourceStream::read(void *data, sf::Int64 size)
{
    sf::Int64 i;
    for(i = 0; i <= size && i + pos < this->size; i++, pos++)
        ((char*)data)[i] = ((char*)dat)[pos];

    return i;
}
 

Now everything works purrfect.  ::)

 

anything