Skip to content

Instantly share code, notes, and snippets.

@fridgerator
Created March 27, 2019 16:39
Show Gist options
  • Save fridgerator/6943fd63f0cc7128fe27900a0dc5057c to your computer and use it in GitHub Desktop.
Save fridgerator/6943fd63f0cc7128fe27900a0dc5057c to your computer and use it in GitHub Desktop.
#define USE_SDL 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "lvgl/lvgl.h"
#include <pango/pangocairo.h>
#if USE_SDL
#include <SDL2/SDL.h>
#include "lv_drivers/display/monitor.h"
#else
#include "lv_drivers/display/fbdev.h"
#endif
static char * label_text = "Billy Bob 你会说英语吗";
static char * label_text2 = "不用谢 かわいい!";
static void hal_init(void);
void disp_init(void);
void build_container(lv_obj_t * parent);
int main (int argc, char ** argv)
{
lv_init();
hal_init();
disp_init();
while (1) {
lv_tick_inc(5);
lv_task_handler();
usleep(5 * 1000);
}
}
static void hal_init(void)
{
#if USE_SDL
monitor_init();
#else
fbdev_init();
#endif
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
#if USE_SDL
disp_drv.disp_flush = monitor_flush;
disp_drv.disp_fill = monitor_fill;
disp_drv.disp_map = monitor_map;
#else
disp_drv.disp_flush = fbdev_flush;
disp_drv.disp_fill = fbdev_fill;
disp_drv.disp_map = fbdev_map;
#endif
lv_disp_drv_register(&disp_drv);
}
void disp_init(void)
{
lv_obj_t *scr = lv_obj_create(NULL, NULL);
lv_scr_load(scr);
build_container(scr);
build_container2(scr);
}
cairo_t*
create_layout_context ()
{
cairo_surface_t *temp_surface;
cairo_t *context;
temp_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
context = cairo_create (temp_surface);
cairo_surface_destroy (temp_surface);
return context;
}
cairo_t*
create_cairo_context (int width,
int height,
int channels,
cairo_surface_t** surf,
unsigned char** buffer)
{
*buffer = calloc (channels * width * height, sizeof (unsigned char));
*surf = cairo_image_surface_create_for_data (*buffer,
CAIRO_FORMAT_ARGB32,
width,
height,
channels * width);
return cairo_create (*surf);
}
void build_container(lv_obj_t *parent)
{
lv_obj_t *main_container = lv_obj_create(parent, NULL);
lv_obj_set_size(main_container, 200, 100);
cairo_t *layout_context;
cairo_t *render_context;
cairo_surface_t *surface;
PangoFontDescription *desc;
PangoLayout *layout;
unsigned int *text_width = 200;
unsigned int *text_height = 100;
char * sd;
layout_context = create_layout_context ();
layout = pango_cairo_create_layout (layout_context);
pango_layout_set_text (layout, label_text, -1);
desc = pango_font_description_from_string("Noto Sans");
render_context = create_cairo_context(text_width, text_height, 4, &surface, &sd);
cairo_set_source_rgba (render_context, 0.333, 0.588, 0.847, 1);
cairo_paint(render_context);
cairo_set_source_rgba (render_context, 0.0, 0.0, 1.0, 1);
pango_cairo_show_layout(render_context, layout);
static lv_img_dsc_t thing;
thing.header.w = text_width;
thing.header.h = text_height;
thing.header.cf = LV_IMG_CF_TRUE_COLOR;
thing.header.always_zero = 0;
thing.data_size = sizeof(sd);
thing.data = sd;
lv_obj_t *img = lv_img_create(main_container, NULL);
lv_img_set_src(img, &thing);
// /* Clean up */
free(sd);
g_object_unref (layout);
cairo_destroy (layout_context);
cairo_destroy (render_context);
cairo_surface_destroy (surface);
}
void build_container2(lv_obj_t *parent)
{
lv_obj_t *main_container = lv_obj_create(parent, NULL);
lv_obj_set_size(main_container, 200, 100);
cairo_t *layout_context;
cairo_t *render_context;
cairo_surface_t *surface;
PangoFontDescription *desc;
PangoLayout *layout;
unsigned int *text_width = 200;
unsigned int *text_height = 100;
char * sd;
layout_context = create_layout_context ();
layout = pango_cairo_create_layout (layout_context);
pango_layout_set_text (layout, label_text2, -1);
desc = pango_font_description_from_string("Noto Sans");
render_context = create_cairo_context(text_width, text_height, 4, &surface, &sd);
cairo_set_source_rgba (render_context, 0.333, 0.588, 0.847, 1);
cairo_paint(render_context);
cairo_set_source_rgba (render_context, 0.0, 0.0, 1.0, 1);
pango_cairo_show_layout(render_context, layout);
static lv_img_dsc_t thing;
thing.header.w = text_width;
thing.header.h = text_height;
thing.header.cf = LV_IMG_CF_TRUE_COLOR;
thing.header.always_zero = 0;
thing.data_size = sizeof(sd);
thing.data = sd;
lv_obj_t *img = lv_img_create(main_container, NULL);
lv_img_set_src(img, &thing);
lv_obj_align(main_container, parent, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
// /* Clean up */
free(sd);
g_object_unref (layout);
cairo_destroy (layout_context);
cairo_destroy (render_context);
cairo_surface_destroy (surface);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment