Languages

Menu
Sites
Language
Unable to set font of snprintf
Hey fellow programmers. I'm fairly new to Tizen and I programmed my first watchface. I am stuck on one thing though: I have quite a lot of snprintf that I'm using for time, battery, etc., but I'm unable to change the font of these. Changing font size, style, alignment and similar works, but I can't change the font itself :( Could you help me out with this please? I unfortunately can't upload my project (only .txt allowed), you can find the project and a screenshot of what I'm trying to make it look like following this link: https://goo.gl/RGx4h6 The font I'd love to use can be found in res/font/ I am also encoutering a bug in which coming out of ambient mode I can only see the background, time, battery and date for a second (and then time_tick most likely kicks in, redrawing the screen entirely). You can find a short video of this behaviour in the link listed above as well. Maybe you have an idea on this as well? I don't mind this bug too much, but it'd be nice to have it gone. Thanks in advance and Kind regards, Jakob P.s. Yes, I know most of this code is garbage, but I just wasn't sure how to do a lot of things in Tizen (like switch cases and similar things) :/
View Selected Answer

Responses

8 Replies
K Johnson

Instead of snprintf, you may try changing text font of an Evas object(e.g. label). In that case, please try following the solution provided in below link.

https://developer.tizen.org/ko/forums/native-application-development/custom-font-family-tizen-native-app-using-.ttf?langredirect=1

Jakob Dobberow

Hi K Johnson,

Thank you for your reply. Please excuse my late answer as I was away on a business trip.
I tried this solution before and now again, but was unfortunately unsuccessful :(
I checked the font and resource paths with the get command, for me it is: "/opt/usr/globalapps/org.example.pokemon/res/font/" so I defined FONT as that.
you can find a code snippet on how I create the time label and how I apply the text to itdown below. Unfortunately nothing happens, neither the font size, nor the font itself is applied :/

If you have any idea why this is just not working out, I'd be very happy :)

Thanks in advance and kind regards,
Jakob

Code:

#include <tizen.h>
#include <device/battery.h>
#include <fontconfig/fontconfig.h>
#include "pokemon.h"
 
typedef struct appdata {
Evas_Object *win;
Evas_Object *conform;
Evas_Object *time;
Evas_Object *bg;
Evas_Object *enemy_pkmn;
Evas_Object *my_pkmn;
Evas_Object *enemy_pkmn_lv;
Evas_Object *my_pkmn_lv;
Evas_Object *date;
Evas_Object *battery;
} appdata_s;
 
#define FONT "/opt/usr/globalapps/org.example.pokemon/res/font/"



within update_watch:
snprintf(watch_text, TEXT_BUF_SIZE, "<align=center>%02d:%02d</align>", hour24, minute);
evas_object_text_font_set(ad->time, FONT"PokemonGB.ttf", 60);
elm_object_text_set(ad->time, watch_text);


within create_base_gui:
//Conformant
ad->conform = elm_conformant_add(ad->win);
evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_win_resize_object_add(ad->win, ad->conform);
evas_object_show(ad->conform);
 
//Background
ad->bg = elm_bg_add(ad->win);
evas_object_move(ad->bg, 0, 0);
evas_object_resize(ad->bg, 360, 360);
evas_object_show(ad->bg);
 
//Time Label
ad->time = elm_label_add(ad->bg);
evas_object_resize(ad->time, width, height);
evas_object_move(ad->time, 0, 250);
evas_object_show(ad->time);
Mark as answer
K Johnson

You may try using below code. It workes for me.

ad->label = elm_label_add(ad->conform);
evas_object_resize(ad->label, 360 / 3, 360 / 18);
evas_object_move(ad->label, 360 / 3 , 360 / 3 + 40);
elm_object_text_set(ad->label,"<font=/opt/usr/globalapps/org.example.basicuicustomfont/res/font/PokemonGB.ttf:style=Regular font_size=20>Inital Text</font>");
ad->str = makestring("changed");
evas_object_smart_callback_add(ad->label, "clicked", _clicked, ad);
evas_object_show(ad->label);

write makestring() and _clicked callback as below:

makestring():

char * makestring(char *str)
{
    char *finalString, *prefix, *suffix;
	finalString = (char*) malloc(1000*sizeof(char*));
	prefix = "<font=/opt/usr/globalapps/org.example.basicuicustomfont/res/font/PokemonGB.ttf:style=Regular font_size=20>";
	suffix = "</font>";
	snprintf(finalString,1000,"%s%s%s",prefix,str, suffix);
	return finalString;
}

_clicked:

static void _clicked(void *user_data, Evas_Object *obj, void *event_info)
{
    appdata_s *ad = user_data;
    elm_object_text_set(ad->label, ad->str);

}

 

Jakob Dobberow

Hi K Johnson,

THANK YOU SO MUCH(!!!)
The important and award winning tip was that when I used snprintf(<font= there needs to be the whole path of the custom font here
You made a very elegant solution by making a function that returns a font changed string, in my simple project setting the font in the actual snprintf while changing the text works just fine.

So to reiterate for anyone searching for this kind of thing:

In the create_base_gui I create the label:
 
//Time Label
ad->time = elm_label_add(ad->bg);
evas_object_resize(ad->time, width, height);
evas_object_move(ad->time, 0, 265);
evas_object_show(ad->time);
 
Int the tick function:
snprintf(watch_text, TEXT_BUF_SIZE, "<align=center><font=/opt/usr/globalapps/org.example.pokemon/res/font/PokemonGB.ttf:style=Regular font_size=42>%02d:%02d</font></align>", hour24, minute);

You can use 
resource_path = app_get_resource_path();
and put out the resource_path variable on a label to see the exact path of your project.

Kind regards and thank you again,
Jakob
K Johnson

That's great! However, there's another way in which you don't need to define the whole path inside the markup. Do you need that?

Jakob Dobberow

Jeah, sure that'd definitely be cool. I think it'd also be helpful if other people are searching for this kind of topic as the only other good resource you can find on the topic is the other forum post you listed.

K Johnson

In that case, you have to append your required font path to the list of font paths used by the application. To do that, you've to write below line of codes inside app_create function before calling create_base_gui() function.

char filepath[1000] = {0,};
char *resource_path = NULL;
resource_path = app_get_resource_path();
snprintf(filepath,1000,"%s%s",resource_path,"font/");
evas_font_path_global_append(filepath);

After that use the font as simple as that, write below code.

/... required code to create a label .../
elm_object_text_set(ad->label, "<font=PokemonGB:style=Regular font_size=20>Initial text</font>");
/... reuired code to make the label visible .../

 

Jakob Dobberow

Ahhh, That's quite a bit more elegant, yes.
I changed it slightly however in which I just need this one font path statically, so I didn't go through the hoops with the extra variable and just inserted

    evas_font_path_global_append("/opt/usr/globalapps/org.example.pokemon/res/font/");

into the create_base_gui() which works just as well and saves me a couple lines of code :)
Thank you so much for the solution and the added bonus of showing a much more elegant way of doing it A+++

Kind regards,
Jakob