Add support for text rendering
cover Add support for text rendering
Implemented using the single-file public domain library "stb_truetype":
https://github.com/nothings/stb/blob/master/stb_truetype.h
PS: Patch 1/3 is a simple fix suggested by Slavomir which strictly speaking is independent from the other two patches.
Yordan Karadzhov (VMware) (3):
kernel-shark: Simplify the drawing of the Triangle Fan
kernel-shark: Add support for drawing text
kernel-shark: Add text rendering to the plotting example
kernel-shark/examples/dataplot.cpp | 8 + kernel-shark/src/libkshark-plot.c | 164 +- kernel-shark/src/libkshark-plot.h | 35 + kernel-shark/src/stb_truetype.h | 4882 ++++++++++++++++++++++++++++ 4 files changed, 5076 insertions(+), 13 deletions(-) create mode 100644 kernel-shark/src/stb_truetype.h
Signature · 1 line
2.20.1
RFC PATCH 1/3 kernel-shark: Simplify the drawing of the Triangle Fan
The central vertex of the Triangle Fan doesn't need to be an internal point. The first point of the polygon can be used instead.
--- kernel-shark/src/libkshark-plot.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
kernel-shark/src/libkshark-plot.c +1 -10
@@ -162,18 +162,9 @@ void ksplot_draw_polygon(const struct ksplot_point *points,return ; } -/* Obtain a point inside the surface of the polygon. */ -struct ksplot_point in_point; - in_point.x = (points[0 ].x + points[2 ].x) /2 ; - in_point.y = (points[0 ].y + points[2 ].y) /2 ; - -/* -* Draw a Triangle Fan using the internal point as a central -* vertex. -*/ +/* Draw a Triangle Fan. */ glBegin (GL_TRIANGLE_FAN );glColor3ub (col->red, col->green, col->blue); -glVertex2i (in_point.x, in_point.y);for (size_t i =0 ; i < n_points; ++i)glVertex2i (points[i].x, points[i].y);
Signature · 1 line
2.20.1
RFC PATCH 2/3 kernel-shark: Add support for drawing text
In reply to Yordan Karadzhov (VMware)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
--- 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
kernel-shark/src/libkshark-plot.c +150 -3
@@ -9,13 +9,21 @@* @brief Basic tools for OpenGL plotting. */ -// OpenGL -#include <GL/freeglut.h> -#include <GL/gl.h> +// C +#include <stdbool.h> +#include <stdio.h> // KernelShark #include "libkshark-plot.h" +/* +* STB TrueType (single-file public domain library) +* https://github.com/nothings/stb +*/ +#define STB_TRUETYPE_IMPLEMENTATION // Generate implementation. +#define STBTT_STATIC // Make the implementation private. +#include "stb_truetype.h" +/** * @brief Create an empty scene for drawing. *
@@ -201,3 +209,142 @@ void ksplot_draw_polygon_contour(const struct ksplot_point *points, col, size); } + +/** The size of the bitmap matrix use3d to load the font. */ +#define KS_FONT_BITMAP_SIZE 512 + +/** +* @brief Initialize a font. +* +* @param font: Output location for the font descriptor. +* @param size: The size of the font. +* @param file: Input location for the truetype font file. +*/ +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) { +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; +} + +/** +* @brief Print(draw) a text. +* +* @param font: Intput location for the font descriptor. +* @param col: The color of the polygon. +* @param x: Horizontal coordinate of the beginning of the text. +* @param y: Verticalal coordinate of the beginning of the text. +* @param text: The text to be drawn. +*/ +void ksplot_print_text (const struct ksplot_font *font, +const struct ksplot_color *col, +float x,float y, +const char *text) +{ +glEnable (GL_TEXTURE_2D ); +glColor3ub (col->red, col->green, col->blue);// Text color +glBindTexture (GL_TEXTURE_2D , font->texture_id); +glBegin (GL_QUADS ); +while (*text) { +if (*text >=KS_SPACE_CHAR && *text <=KS_TILDA_CHAR ) { +stbtt_aligned_quad quad; +stbtt_GetBakedQuad (font->cdata, +KS_FONT_BITMAP_SIZE , +KS_FONT_BITMAP_SIZE , + *text -KS_SPACE_CHAR , + &x, &y, + &quad, +1 ); + +glTexCoord2f (quad.s0, quad.t1); +glVertex2f (quad.x0, quad.y1); + +glTexCoord2f (quad.s1, quad.t1); +glVertex2f (quad.x1, quad.y1); + +glTexCoord2f (quad.s1, quad.t0); +glVertex2f (quad.x1, quad.y0); + +glTexCoord2f (quad.s0, quad.t0); +glVertex2f (quad.x0, quad.y0); + } + + ++text; + } + +glDisable (GL_TEXTURE_2D ); +glEnd (); +}
kernel-shark/src/libkshark-plot.h +35 -0
@@ -12,6 +12,16 @@ #ifndef _LIB_KSHARK_PLOT_H #define _LIB_KSHARK_PLOT_H +// OpenGL +#include <GL/freeglut.h> +#include <GL/gl.h> + +/* + * STB TrueType (single-file public domain library) + * https://github.com/nothings/stb + */ +#include "stb_truetype.h" + #ifdef __cplusplus extern "C" { #endif
@@ -62,6 +72,31 @@ void ksplot_draw_polygon_contour(const struct ksplot_point *points,const struct ksplot_color *col, float size); +/** The index of the "Space" character. */ +#define KS_SPACE_CHAR 32 + +/** The index of the "Tilda" character. */ +#define KS_TILDA_CHAR 126 + +/** Total number of characters supported for drawing. */ +#define KS_N_CHAR (KS_TILDA_CHAR - KS_SPACE_CHAR + 1) + +/** Structure defining a font. */ +struct ksplot_font { +/** Identifier of the font's texture. */ +GLuint texture_id; + +/** Font's texture baking data. */ +stbtt_bakedchar cdata[KS_N_CHAR ]; +}; + +bool ksplot_init_font (struct ksplot_font *font,float size,const char *file); + +void ksplot_print_text (const struct ksplot_font *font, +const struct ksplot_color *col, +float x,float y, +const char *text); + #ifdef __cplusplus }#endif
kernel-shark/src/stb_truetype.h new file +4882 -0
2,787 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.20.1
Part of the diff is not shown here. View the raw message.
Re: [RFC PATCH 2/3] kernel-shark: Add support for drawing text
Quoted 21 lines · Yordan Karadzhov (VMware)
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.hSigned-off-by Yordan Karadzhov (VMware) <[email protected]>--- 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.hkernel-shark/src/libkshark-plot.c +0
-0@@ -9,13 +9,21 @@* @brief Basic tools for OpenGL plotting. */
[...]
Quoted 13 lines
kernel-shark/src/libkshark-plot.c +11
-0+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" );
kernel-shark/src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 243
Quoted 10 lines
kernel-shark/src/libkshark-plot.c +10
-0+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.
Quoted 59 lines
kernel-shark/src/libkshark-plot.c +58
-0+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; +}
[...]
kernel-shark/src/libkshark-plot.h line 16
Quoted 10 lines
kernel-shark/src/libkshark-plot.h +2
-0@@ -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
Re: [RFC PATCH 2/3] kernel-shark: Add support for drawing text
In reply to Yordan Karadzhov (VMware)Quoted 15 lines · Yordan Karadzhov (VMware)
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.hSigned-off-by Yordan Karadzhov (VMware) <[email protected]>--- 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.hkernel-shark/src/libkshark-plot.c +0
-0
[...]
Quoted 25 lines
kernel-shark/src/libkshark-plot.c +25
-0+void ksplot_print_text (const struct ksplot_font *font, +const struct ksplot_color *col, +float x,float y, +const char *text) +{ +glEnable (GL_TEXTURE_2D ); +glColor3ub (col->red, col->green, col->blue);// Text color +glBindTexture (GL_TEXTURE_2D , font->texture_id); +glBegin (GL_QUADS ); +while (*text) { +if (*text >=KS_SPACE_CHAR && *text <=KS_TILDA_CHAR ) { +stbtt_aligned_quad quad; +stbtt_GetBakedQuad (font->cdata, +KS_FONT_BITMAP_SIZE , +KS_FONT_BITMAP_SIZE , + *text -KS_SPACE_CHAR , + &x, &y, + &quad, +1 ); + +glTexCoord2f (quad.s0, quad.t1); +glVertex2f (quad.x0, quad.y1); + +glTexCoord2f (quad.s1, quad.t1); +glVertex2f (quad.x1, quad.y1);
kernel-shark/src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 346
Quoted 10 lines
kernel-shark/src/libkshark-plot.c +10
-0+ +glTexCoord2f (quad.s1, quad.t0); +glVertex2f (quad.x1, quad.y0); + +glTexCoord2f (quad.s0, quad.t0); +glVertex2f (quad.x0, quad.y0); + } + + ++text; + }
Nit: Can we have the above branch inverted to make the indentation less deep? Maybe something like:
for (; *text; text++) {
if (*text < KS_SPACE_CHAR || *text > KS_TILDA_CHAR)
continue;
stbtt_aligned_quad quad;
stbtt_GetBakedQuad(font->cdata,
KS_FONT_BITMAP_SIZE,
KS_FONT_BITMAP_SIZE,
*text - KS_SPACE_CHAR,
&x, &y,
&quad,
1);
glTexCoord2f(quad.s0, quad.t1);
glVertex2f(quad.x0, quad.y1);
glTexCoord2f(quad.s1, quad.t1);
glVertex2f(quad.x1, quad.y1);
glTexCoord2f(quad.s1, quad.t0);
glVertex2f(quad.x1, quad.y0);
glTexCoord2f(quad.s0, quad.t0);
glVertex2f(quad.x0, quad.y0);
}Quoted 4 lines
kernel-shark/src/libkshark-plot.c +4
-0+ +glDisable (GL_TEXTURE_2D ); +glEnd (); +}
RFC PATCH 3/3 kernel-shark: Add text rendering to the plotting example
In reply to Yordan Karadzhov (VMware)run:
kernel-shark/bin/dplot -s
And if the font file is loaded successfully you will see "Hello Kernel!" rendered.
--- kernel-shark/examples/dataplot.cpp | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel-shark/examples/dataplot.cpp +8 -0
@@ -27,6 +27,9 @@ using namespace std; #define default_file (char*)"trace.dat" +struct ksplot_font font; +const char *font_file = "/usr/share/fonts/truetype/msttcorefonts/times.ttf"; + struct kshark_trace_histo histo; vector<KsPlot::Graph *> graphs;
@@ -72,6 +75,11 @@ void drawShapes() r.setFill(false); r.draw(); + if (ksplot_init_font(&font, 60, font_file)) { + struct ksplot_color col = {100, 255, 0}; + ksplot_print_text(&font, &col, 250, 70, "Hello Kernel!"); + } + glFlush(); }
Signature · 1 line
2.20.1