Magick++ - How to load a font faster?

81 Views Asked by At

In Magick++, I create a gif with about 100 frames, on each frames I draw 16 different text but for every text it take 10ms to draw (it increase if I choose a font heavier, for 10ms it weighs 22ko but take 20ms when a font weighs 59ko). It result taking 30sec to draw a gif of 5sec (20 frames/sec) while drawing other images on each frames take less than 0ms.

The text can be long of 50 character or 1, it takes the same time, so each time a text is draw, it need to load the font from the file, is there no way to register the font to optimize the time ?

Currently a solution is to draw A-Z, a-z and 0-9 in image and to use them for the text, it's faster but it's a bit stupid.

My code: The this->texts is a list of drawable with my font in the first place of the list for each frames like this this->texts = {Magick::DrawableFont("../assets/font/pixelmix.ttf")};.

void Gif::drawText(const std::string &text, double x, double y, double fontSize, bool centered, const Magick::Color &color, bool shadow) {
    this->texts.emplace_back(Magick::DrawablePointSize(fontSize));
    this->texts.emplace_back(Magick::DrawableGravity(centered ? MagickLib::CenterGravity : MagickLib::ForgetGravity));

    if (centered) {
        x -= this->width / 2.0;
        y -= this->height / 2.0;
    } else {
        y += fontSize / 2.0;
    }

    if (shadow) {
    this->texts.emplace_back(Magick::DrawableFillColor(Magick::Color("black")));
    this->texts.emplace_back(Magick::DrawableText(x - 1, y + 1, text));
    }

    this->texts.emplace_back(Magick::DrawableFillColor(color));
    this->texts.emplace_back(Magick::DrawableText(x, y, text));
}

All text is draw when adding the frame to the final gif:

    if (this->texts.size() > 1) {
        TimeDifference td;
        this->currentImage.draw(this->texts);
        std::cout << "draw took " << td.getDifferenceWithLast() << "ms" << std::endl;
    }
0

There are 0 best solutions below