Displaying Text with SDL_ttf without font file hard code - c

I have code in C LANGUAGE to display text on screen using SDL_ttf library in linux.
TTF_Font *font;
font = TTF_OpenFont("FreeSans.ttf", 20);
But in this I don't want to give file name as hard coded or file path. so when I execute this programme on other Linux machine it should run fine. I don't want to keep always this font file with my executable.
Is there any way to write portable code for TTF_OpenFont ?
or i can use something else that can solve my problem ?

To avoid hardcoding fonts you need to integrate SFL_TTF with fontconfig.
Or use fontconfig through a higher-level framework like pango http://sdlpango.sourceforge.net/

Related

GTK 4 and applications icons : how to include an application icon in a portable way?

I have difficulties to understand the icon system in GTK 4 (I work in C language).
In GTK 2 and 3 that was easy to instruct the OS about the icon to use for displaying the apps.
In GTK 4 the set_icon functions have been removed which let us with the theming system.
I understand that, by default, gtk want us to follow the Freedesktop Icon Theme Specification and so to put the icons during the installation in directories like /usr/local/share/icons/hicolor/ and setting it in the application via function like gtk_window_set_default_icon_name or gtk_window_set_icon_name. But I didn't really manage to make this system work.
Moreover, it remains obscure to me what happens on other systems that are not gnome-based like Windows (or even KDE desktop)...
So, well, I have a few questions that stem from theses previous points :
How the system work on other OS or DE that do not follow the Freedesktop Icon Theme Specification ?
Is this possible to have a very short working example that illustrates how to use, in GTK4, a new application icons that, for example, was just copied in /usr/local/share/icons/hicolor/
And my real question for my use case : is this still possible, by one way or another, to include applications icons in the binary or in the binary directory to have a simple portable application which do not need installations and work on every system ?
(edit : edited to include nielsdg precision)
Ok, I have found a (partial) answer to my second question and third question, sort of.
In fact, it's pretty simple, but I made a few mistakes.
You simply have to have a directory with this structure exactly (I made a mistake in the structure) :
my_ressource_directory/hicolor/apps/48x48/my-icon.png
my_ressource_directory/hicolor/apps/256x256/my-icon.png
my_ressource_directory/hicolor/apps/512x512/my-icon.png
Only the 48x48 size is necessary, there are a lot of other sizes which seems to be : 8x8 16x16 22x22 24x24 32x32 36x36 42x42 44x44 48x48 64x64 72x72 96x96 128x128 150x150 192x192 256x256 310x310 512x512 and "scalable".
I don't know in which context the system use all of them and I don't know if personalised size is possible.
I equally don't know in which context the svg icons in scalable can be used by the system...
In the code, then, you simply have to include a thing like that :
icon_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
gtk_icon_theme_add_search_path(icon_theme,path_to_my_ressource_directory);
if(gtk_icon_theme_has_icon(icon_theme,"my-icon")!=1)
{
// manage error
}
gtk_window_set_default_icon_name("my-icon"); // default icon for all windows if nothing is set explicitly if I understand well.
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Your app");
gtk_window_set_icon_name(GTK_WINDOW (window),"my-icon"); // set explicitly the icon for the min window
And tadaaa; it works :) .
Please also notice that some post seems to say that the call to gtk-update-icon-cache is important to make the system works. This command creates a "icon-theme.cache" file in the directory that is used by GTK to accelerate the opening/processing of icons.
In practice I can't say if this is efficient or really used by GTK but after checking, even without the icon-theme.cache it works. So the call of this command seems not to be mandatory.
That said, it cost nothing to call the command in the Makefile. The exact command is :
gtk-update-icon-cache -f -t my_ressource_directory/
EDIT
The solution is incomplete because the resulting icon is fuzzy in certain context. See this question : Why GTK4 seems to use only 48x48 icons for displaying minimized application in all context?
And the answer, to have the full solution.
Works fine for me (under FedoraCore 35). Needed to have custom icon shown when running exampleapp1 (A trivial application) of GTK 4 doc. https://docs.gtk.org/gtk4/getting_started.html

Use Font for S7735R LCD using ESP32

I'm working on ESP32. With ESP32 MCU interface S7735 LCD and I'm use ESP_IDF as an IDE.
I use " https://github.com/loboris/ESP32_TFT_library " for TFT_LCD and there are given fonts I use.I'm tried given font and its working fine and if you want to use your custom font then simply put font file in spiffs/fonts folder and flash into the ESP32.
Above this steps I follow but not reflect any font style if I flash custom font in .ttf . I also try to convert .ttf to .fon font but .fon format is for Window/Dos. and for LCD I need a .fon (binary).
I did not get which font extension I use for this given particular file. Kindly suggest compatible font extension and link also where I will be download easily.
static const char *file_fonts[3] = {"/spiffs/fonts/DotMatrix_M.fon", "/spiffs/fonts/Ubuntu.fon", "/spiffs/fonts/Grotesk24x48.fon"};
from main/tft_demo.c there are given above line and that font use from given path.
I need a solution for font file which is compatible with my driver.
Kindly suggest me flow for new custom font use.
The font format is proprietary and simple bit-maps. The file name extension .fon is generic and tells you nothing about the actual format.
The /tools folder of the library you linked includes a tool for converting TTF fonts to bitmaps generated as C code data arrays that you simply link to your project to add fonts.
From the read-me file:
Program to convert any ttf font to c source file that can be includes
in ESP32 tft library.
This is a windows program, but can be used under Linux with wine:
Usage:
ttf2c_vc2003.exe <point-size> <input-file> <output-file> [<start_char> <end_char>]
or, under Linux:
wine ./ttf2c_vc2003.exe <point-size> <input-file> <output-file> [<start_char> <end_char>]
There is more in the read-me file - a couple of manual edits that need to be made. The folder includes examples for Vera and DejaVu Sans.

Allegro load_bitmap not working

i'm trying to load bitmap like this:
BITMAP *image = load_bitmap("picture.bmp", NULL);
when I test it:
if (image == NULL)
printf("No image loaded\n");
it prints No image loaded so load_bitmap doesn't work ... i have also tried absolute path but still nothing.
Im using Ubuntu and allegro 4.2
Some suggestions?
Did you try placing the image on the same location as the executable? After that is solved check this things also if still getting the error:
Is really a *.bmp file? A file of a different type can not be converted by just renaming it.
Is the file you are trying to read actually called like that? Check for spelling both in code and in the file explorer.
Does the program run correctly if executed from the file explorer or command-line but not from the IDE? If that is the case, then you should change the configuration of the workspace or project you are currently using so that the execution directory is the same as the one where the image file is located.
If all else fails then try following the steps of the tutorial again, perhaps you made something wrong. By the way, if this is your first C++ project I recommend you that instead go to more basic stuff and stick to the command-line for a while until you get the hang of the facilities the language and its libraries have to offer.

Image output in C

Quick question, is there a way to show an image(ex. bmp) from file using C? It's not in graphics.h apparently, and I can't use Allegro because it does not support Borland(or so I've read). I need to use the very old compiler for a school project. I would like to ask if anyone had any experience of doing this using other libraries? If yes, which library was it? Thanks a lot.
I hope you have visual (windows) borland like Borland C++ builder 3++ or turbo C++ not the MS DOS one. in that case it is quite easy because you can use bitmap which is part of VCL so no additional include is needed.
here you can find some hints on rendering under borland
now how to visualize picture from file to your window:
// this will create and load your bitmap
Graphics::TBitmap *bmp=new Graphics::TBitmap;
bmp->LoadFromFile("image.bmp");
bmp->HandleType=bmDIB;
bmp->PixelFormat=pf32bit;
// on paint you can draw your image to form,paintbox,another bitmap or whatever...
Form1->Canvas->Draw(0,0,bmp); // also you can use stretch draw or copy rectangle GDI functions
// before exiting delete the bmp
delete bmp;
[Notes]
You can also save image by bmp->SaveToFile("out.bmp"); In case you need jpg then add:
#include <jpeg.hpp>
TJPEGImage *jpg=new TJPEGImage;
jpg->LoadFromFile("image.jpg");
bmp->Assign(jpg);
delete jpg;
this will load jpg to your bmp also you can save jpg as well in the same way. Beware older Borlands has a bug in TJPEGImage and will crash if the jpg resolution is too big**

How to avoid having to reference an external XML file?

I am writing an app in C and using Glade3 to make the GUI (and GTK builder).
Glade 3 doesn't allow C code generation anymore (which I understand the reasons for), but now all applications need to have the XML file hanging around the compiled app like an annoying .dll file. Does anyone have any knowledge of project to create C code for it, or how to embed the XML file in the code itself? I imagine this could be done, but it would be a pain I bet.
What you probably want is:
gtk_builder_new_from_string(const gchar *string, gssize length);
https://developer.gnome.org/gtk3/3.10/GtkBuilder.html#gtk-builder-new-from-string
Just stringify the glade3 xml file (in GtkBuilder format) and include it as a string.
It is still possible to use glade-2.12 to generate gtk2 code and then port it to gtk3; however if you are going to do that it may be worth it to simply modify that version of glade to output the gtk3 syntax directly.

Resources