Font cannot be loaded with Emscripten but with g++ - c++

I am trying to use FreeType the first time with OpenGL in C/C++ with this tutorial: https://learnopengl.com/In-Practice/Text-Rendering.
#include <iostream>
#include <ft2build.h>
#include FT_FREETYPE_H
FT_Library ft;
FT_Face face;
int main(int argc, char** argv)
{
FT_Init_FreeType(&ft);
if (FT_New_Face(ft, "/usr/share/fonts/truetype/malayalam/Uroob-Regular.ttf", 0, &face))
{
std::cout << "Font could not be loaded" << std::endl;
return -1;
}
}
Now when I try to compile this it works with g++ so far as there is no error output, but with em++ (Emscripten) the font cannot be loaded as the error output "Font could not be loaded" then indicates.
I do not need THIS kind of font "Uroob-Regular.ttf", any readable Latin font would do it for me. I am under Ubuntu.
I use the flag -s USE_FREETYPE=1 with Emscripten.

It seems like Emscripten cannot find files below the current directory, because when I try to compile it with --preload-file "/usr/share/fonts/truetype/malayalam/Uroob-Regular.ttf" to help it find the font it indicates: "Embedding "/usr/share/fonts/truetype/malayalam/Uroob-Regular.ttf" which is below the current directory "xxx". This is invalid since the current directory becomes the root that the generated code will see".
I don't know if this is a valid way to do so, but then I put the font file in the same directory as the cpp file. Then I used --preload-file "Uroob-Regular.ttf" and changed the corresponding code line to if (FT_New_Face(ft, "Uroob-Regular.ttf", 0, &face)) and it worked for both Emscripten and g++.

Related

How to fix 'The procedure entry point SDL_RWclose could not be located in the dynamic link library'

I'm trying to draw a png image to a window using the SDL_image extension, but it gives me an "Entry Point Not Found" error
I'm using SDL (2.0.9) and SDL_Image (2.0.5)
I've copied the following bin files to the executable directory
libjpeg-9.dll
libpng16-16.dll
libtiff-5.dll
libwebp-7.dll
SDL2.dll
SDL2_image.dll
zlib1.dll
main.cpp extract
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
int main( int argc, char* args[] )
{
SDL_Texture* test_tex;
SDL_Window* window = NULL;
SDL_Renderer* renderer;
if(renderer)
{
//Tested blank screen and it works
/*
SDL_RenderPresent(renderer);
SDL_Delay(2000);
*/
//Trying to use SDL_image and it fails
SDL_Surface *tmp_surface = IMG_Load("player.png");
test_tex = SDL_CreateTextureFromSurface(renderer,tmp_surface);
SDL_FreeSurface(tmp_surface);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
}
...
Complied like this
g++ test.cpp ^
-IC:\dev\SDL2-2.0.9\i686-w64-mingw32\include\SDL2 ^
-IC:\dev\SDL2_image-2.0.5\i686-w64-mingw32\include\SDL2 ^
-LC:\dev\SDL2-2.0.9\i686-w64-mingw32\lib ^
-LC:\dev\SDL2_image-2.0.5\i686-w64-mingw32\lib ^
-lmingw32 ^
-lSDL2main ^
-lSDL2 ^
-lSDL2_image ^
-o test
I've tested the window with a blank renderer and it's all ok, It fails when I add the call to IMG_Load
You need another SDL_Image version. Use SDL_Image (2.0.4) instead of (2.0.5).
You can get the older versions here:
https://www.libsdl.org/projects/SDL_image/release/?C=M;O=D
(That fixed the same problem for me)
The 2.0.9 32 bit SDL2.dll gave me issues with anything other than VC++. Fortunately the 2.0.10 version is available for testing which actually works for my Code::Blocks compiled tests: https://www.libsdl.org/tmp/download-2.0.php

FreeType bitmaps are zeroed after rendering a glyph

I've created a repository that has the most basic way to reproduce my issue. I use Cmake to build and it's set up to statically link freetype into my executable.
FreeType is a submodule, so you can clone it all at once with:
git clone --recurse-submodules https://github.com/jeffw387/freetype_minimum_test
or if you prefer just clone freetype from git into the externals directory.
I'm currently testing on Ubuntu.
I can initialize FreeType, create a face from the font, and load a glyph from a character code with the FT_LOAD_RENDER flag all without errors.
Then when I inspect the bitmap buffer, it's zero-initialized on every row. I used gdb from within VS Code in order to inspect the buffer, but before that I also tried copying out the data line by line.
The font I'm testing with is a free font I found online, but I was able to test it in Libre Office and it seems to work just fine.
Can anyone spot a problem with the code? If anyone is willing I'd love to know if this repo creates the same issue on their machine.
Here's my cpp file for easy viewing:
#include <ft2build.h>
#include FT_FREETYPE_H
#include <iostream>
#include <vector>
int main() {
FT_Library library{};
if (FT_Init_FreeType(&library)) {
std::cout << "Error initializing FreeType.";
}
FT_Face face{};
auto faceResult = FT_New_Face(library, "Anke.ttf", 0, &face);
if (faceResult) {
std::cout << "Error creating face.";
}
if ((face->face_flags & FT_FACE_FLAG_SCALABLE) != FT_FACE_FLAG_SCALABLE) {
std::cout << "Error: font is not scalable.";
}
if (FT_Set_Pixel_Sizes(face, 0, 50)) {
std::cout << "Error setting font pixel size.";
}
if (FT_Load_Char(face, 'P', FT_LOAD_RENDER)) {
std::cout << "Error loading or rendering glyph.";
}
auto bmp = face->glyph->bitmap;
return 0;
}
I've done some further testing and it seems like there was a problem with my freetype fork. I haven't gone to the trouble of figuring out what it was, but in any case now that I've tried it with a fresh clone from https://github.com/aseprite/freetype2 it works.

Font not loading

I spent the last 2 hours trying to figure out what is wrong with my code. Some solutions on Google/StackOverflow didn't change the problem.
Here's this simple code :
#include <SFML/Graphics.hpp>
#include <fstream>
int WinMain()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "BBS");
window.setFramerateLimit(60);
// redirect sfml stderr to this file
std::ofstream file("sfml-log.txt");
std::streambuf* previous = sf::err().rdbuf(file.rdbuf());
sf::err() << "ABCD" << std::endl;
sf::Font font;
// this line fail and make stderr crash
font.loadFromFile("E:\\arial.ttf");
sf::err() << "XYZ" << std::endl;
return 0;
}
The file "sfml-log.txt" is created but it contain everytime the same thing :
ABCD
Failed to load font "
For the context I launch the executable within the same directory as sfml DLLs.
I tried to modify the font path to "arial.ttf" and put it in the same directory, nothing changed.
Thanks in advance for your help.
Ok, I just solved it.
I was compiling with debug settings in VS and was linking with
sfml-graphics.lib sfml-window-s.lib sfml-window.lib sfml-system-s.lib sfml-system.lib
instead of
sfml-graphics-d.lib sfml-window-s-d.lib sfml-window-d.lib sfml-system-s-d.lib sfml-system-d.lib

OpenCV 3 : Program not running in Eclipse

I am trying to run a simple image show program in Eclipse CDT in MinGW built using cmake.
OpenCV Include Path : "E:\cv\opencv\eclipse\install\include"
OpenCV Library Path : "E:\cv\opencv\eclipse\lib" (has all libraries eg.libopencv_highgui310)
My Code is,
#include <iostream>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main(int argc, const char** argv) {
Mat img(500, 500, CV_8UC3, Scalar(100, 0, 0));
cout << "LOL!!!" << endl;
if (img.empty()) {
cout << "Error: Image cannot be loaded." << endl;
system("pause");
return -1;
}
namedWindow("Image Window", CV_WINDOW_AUTOSIZE);
imshow("Image Window", img);
if (waitKey(10) == 27) {
return -1;
}
destroyWindow("Image Window");
return 1;
}
When I build the code my console shows,
07:19:50 **** Incremental Build of configuration Release for project opencv_cpp ****
Info: Internal Builder is used for build
g++ "-IE:\\cv\\opencv\\eclipseBuild\\install\\include" -O3 -Wall -c -fmessage-length=0 -o "src\\faceDetect.o" "..\\src\\faceDetect.cpp"
g++ "-LE:\\cv\\opencv\\eclipseBuild\\lib" -o opencv_cpp.exe "src\\faceDetect.o" -llibopencv_highgui310 -llibopencv_core310 -llibopencv_imgproc310 -llibopencv_imgcodecs310 -llibopencv_objdetect310
07:19:56 Build Finished (took 5s.647ms)
When I run the program it just terminates and nothing happens. Even the print statement is not executed.
Here is the youtube link for the video of the problem,
https://youtu.be/kCrz_WPi_AI
Can someone help me with this?
Even I too faced this problem for longtime.
I researched through all the forum's , websites and all over google but i'm not able to find the answer.
But suddenly I thought of switching off the Windows Firewall and Windows Defender, And it magically works for me.. You too try it and let me know the result !
Happy coding :)

SDL 2.0 compiles, but failt to run

Im using the latest SDL 2.0 version on Xubuntu 64-bits. I installed through the provided install script on the source code.
Compiling works well, however when trying to open a font or image (regardless of its extension), it will always fail to open.
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main (int argc, char *argvp[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
cout << SDL_GetError() << endl;
}
if (TTF_Init() == -1)
{
std::cout << TTF_GetError() << std::endl;
return 2;
}
TTF_Font *font1 = NULL;
font1 = TTF_OpenFont("SourceSansPro-Regular.ttf", 20);
if (font1 == NULL)
{
std::cout << "ERROR OPENING FONT = " << TTF_GetError() << std::endl;
}
TTF_CloseFont(font1);
SDL_Quit();
return 0;
}
I compiled with
g++ -Wall fontTEST.cpp -o TEST -lSDL2 -lSDL_ttf (NOTE that SDL_ttf installs as such, not as SDL2_ttf)
And get the following error: Failed to load font: 0 Couldn't load font file
This happens with images as well. I've already tried with different fonts and images, apparently it works if I compile with SDL 1.2, just not with 2.0.
Also why does the provided install script installs the lib and include folders in /user/local/?
I moved them to /usr/ but the problem persists.
Remember the following:
On Unix, file paths are case-sensitive
As said in Xonar's comment, tilde '~' expansion is a shell feature, it does not work in C/C++ programs, you should use the real path instead.
The strace log says clearly that something is wrong with the path.
You should try the following:
Rename your font file to "font.ttf"
put it in /home/user/font.ttf
use "/home/user/font.ttf" as the path in your code.