On Mon, Jul 1, 2019 at 3:42 PM Yordan Karadzhov (VMware) <y.karadz@xxxxxxxxx> wrote: > > OpenGL doesn't provide support for text rendering. Here we add capability > of printing text to the OpenGL scene by using the single-file public > domain library "stb_truetype": > https://github.com/nothings/stb/blob/master/stb_truetype.h > > Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@xxxxxxxxx> > --- > kernel-shark/src/libkshark-plot.c | 153 +- > kernel-shark/src/libkshark-plot.h | 35 + > kernel-shark/src/stb_truetype.h | 4882 +++++++++++++++++++++++++++++ > 3 files changed, 5067 insertions(+), 3 deletions(-) > create mode 100644 kernel-shark/src/stb_truetype.h > > diff --git a/kernel-shark/src/libkshark-plot.c b/kernel-shark/src/libkshark-plot.c > index cdd0ef8..36982e3 100644 > --- a/kernel-shark/src/libkshark-plot.c > +++ b/kernel-shark/src/libkshark-plot.c > @@ -9,13 +9,21 @@ > * @brief Basic tools for OpenGL plotting. > */ [...] > > > +bool ksplot_init_font(struct ksplot_font *font, float size, const char *file) > +{ > + unsigned char bitmap[KS_FONT_BITMAP_SIZE * KS_FONT_BITMAP_SIZE]; > + ssize_t buff_size, ret; > + unsigned char *buffer; > + stbtt_fontinfo info; > + FILE *font_file; > + > + font_file = fopen(file, "rb"); > + if (!font_file) { > + fprintf(stderr, "Failed to open font file!\n"); > + return false; > + } > + > + /* Get the size of the file. */ > + fseek(font_file, 0, SEEK_END); > + buff_size = ftell(font_file); > + fseek(font_file, 0, SEEK_SET); > + > + buffer = malloc(buff_size); > + if (!buffer) { font_file doesn't get fclose()d in this case. > > + fprintf(stderr, "Failed to allocat memory for font!\n"); > + goto fail; > + } > + > + ret = fread(buffer, buff_size, 1, font_file); > + fclose(font_file); > + > + if (ret == 0) { > + fprintf(stderr, "Failed to read from font file!\n"); > + goto fail; > + } > + > + if (!stbtt_InitFont(&info, buffer, 0)) { > + fprintf(stderr, "Failed to init font!\n"); > + goto fail; > + } > + > + ret = stbtt_BakeFontBitmap(buffer, > + 0, > + size, > + bitmap, > + KS_FONT_BITMAP_SIZE, > + KS_FONT_BITMAP_SIZE, > + KS_SPACE_CHAR, > + KS_N_CHAR, > + font->cdata); > + > + if (ret <= 0) { > + fprintf(stderr, "Failed to bake font bitmap!\n"); > + goto fail; > + } > + > + free(buffer); > + > + glGenTextures(1, &font->texture_id); > + glBindTexture(GL_TEXTURE_2D, font->texture_id); > + > + glTexImage2D(GL_TEXTURE_2D, > + 0, > + GL_ALPHA, > + KS_FONT_BITMAP_SIZE, > + KS_FONT_BITMAP_SIZE, > + 0, > + GL_ALPHA, > + GL_UNSIGNED_BYTE, > + bitmap); > + > + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); > + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); > + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); > + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); > + > + return true; > + > + fail: > + free(buffer); > + return false; > +} [...] > diff --git a/kernel-shark/src/libkshark-plot.h b/kernel-shark/src/libkshark-plot.h > index 9a4dbc0..617dca0 100644 > --- a/kernel-shark/src/libkshark-plot.h > +++ b/kernel-shark/src/libkshark-plot.h > @@ -12,6 +12,16 @@ > #ifndef _LIB_KSHARK_PLOT_H > #define _LIB_KSHARK_PLOT_H > > +// OpenGL > +#include <GL/freeglut.h> Freeglut is used only in ksplot_make_scene() and is not a necessary dependency for programs that use Qt for window and OpenGL context management. Can we drop freeglut as a dependency for libkshark-plot and move ksplot_make_scene() to where its only use is (in examples/dataplot.cpp)? The rest of the patch looks great. Thank you, -- Slavi