Upgrade the OpenGL wrapper for KernelShark v2.0
cover Upgrade the OpenGL wrapper for KernelShark v2.0
This patch-sets contains one major improvement to the OpenGL wrapping interface used by KernelShark, that is the capability to draw text to the OpenGL canvas. In the same patch-sets we do few other independent small improvements and optimizations.
Steven,
Please review carefully the first patch. Check the new code in src/libkshark-plot.c, but also think if the addition of the public domain header file stb_truetype.h can bring some licensing issues. Also, is the reference to the original project good enough?
Yordan Karadzhov (VMware) (4):
kernel-shark: Add support for drawing text
kernel-shark: Make GLUT optional dependency
kernel-shark: Add ksplot_draw_polyline()
kernel-shark: Optimize ksplot_draw_polygon()
CMakeLists.txt | 6 +- Documentation/doxygen/dox_config | 1 + build/deff.h.cmake | 3 + src/CMakeLists.txt | 8 +- src/libkshark-plot.c | 305 +- src/libkshark-plot.h | 73 + src/stb_truetype.h | 5011 ++++++++++++++++++++++++++++++ 7 files changed, 5366 insertions(+), 41 deletions(-) create mode 100644 src/stb_truetype.h
Signature · 1 line
2.25.1
PATCH 1/4 kernel-shark: Add support for drawing text
OpenGL doesn't provide support for text rendering. Here we first re-enable the compilation of the OpenGL wrapper and add the 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
--- CMakeLists.txt | 6 +- Documentation/doxygen/dox_config | 1 + src/CMakeLists.txt | 4 +- src/libkshark-plot.c | 244 +- src/libkshark-plot.h | 57 + src/stb_truetype.h | 5011 ++++++++++++++++++++++++++++++ 6 files changed, 5312 insertions(+), 11 deletions(-) create mode 100644 src/stb_truetype.h
CMakeLists.txt +3 -3
@@ -26,9 +26,9 @@ include(${KS_DIR}/build/FindJSONC.cmake) find_package(Doxygen) -# set(OpenGL_GL_PREFERENCE LEGACY) -# find_package(OpenGL) -# find_package(GLUT) +set(OpenGL_GL_PREFERENCE LEGACY) +find_package(OpenGL) +find_package(GLUT) # find_package(Qt5Widgets 5.7.1) # find_package(Qt5Network)
Documentation/doxygen/dox_config +1 -0
@@ -14,3 +14,4 @@ SORT_MEMBER_DOCS = NO STRICT_PROTO_MATCHING = YES DOT_MULTI_TARGETS = YES PROJECT_LOGO = ../../icons/KS_logo_stacked.svg +EXCLUDE = ../../src/stb_truetype.h
src/CMakeLists.txt +2 -2
@@ -22,8 +22,8 @@ install(TARGETS kshark LIBRARY DESTINATION ${_LIBDIR}/${KS_APP_NAME}) if (OPENGL_FOUND AND GLUT_FOUND) message(STATUS "libkshark-plot") - add_library(kshark-plot SHARED libkshark-plot.c - KsPlotTools.cpp) + add_library(kshark-plot SHARED libkshark-plot.c) +# KsPlotTools.cpp) target_link_libraries(kshark-plot kshark ${GLUT_LIBRARY}
src/libkshark-plot.c +238 -6
267 lines not on this page · raw message
src/libkshark-plot.h +57 -0
69 lines not on this page · raw message
src/stb_truetype.h new file +5011 -0
2,638 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
Re: [PATCH 1/4] kernel-shark: Add support for drawing text
Quoted 131 lines · On Fri, 20 Nov 2020 11:50:28 +0200 "Yordan Karadzhov (VMware)"…
OpenGL doesn't provide support for text rendering. Here we first re-enable the compilation of the OpenGL wrapper and add the 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]>--- CMakeLists.txt | 6 +- Documentation/doxygen/dox_config | 1 + src/CMakeLists.txt | 4 +- src/libkshark-plot.c | 244 +- src/libkshark-plot.h | 57 + src/stb_truetype.h | 5011 ++++++++++++++++++++++++++++++ 6 files changed, 5312 insertions(+), 11 deletions(-) create mode 100644 src/stb_truetype.hCMakeLists.txt +3
-3@@ -26,9 +26,9 @@ include(${KS_DIR}/build/FindJSONC.cmake) find_package(Doxygen) -# set(OpenGL_GL_PREFERENCE LEGACY) -# find_package(OpenGL) -# find_package(GLUT) +set(OpenGL_GL_PREFERENCE LEGACY) +find_package(OpenGL) +find_package(GLUT) # find_package(Qt5Widgets 5.7.1) # find_package(Qt5Network)Documentation/doxygen/dox_config +1
-0@@ -14,3 +14,4 @@ SORT_MEMBER_DOCS = NO STRICT_PROTO_MATCHING = YES DOT_MULTI_TARGETS = YES PROJECT_LOGO = ../../icons/KS_logo_stacked.svg +EXCLUDE = ../../src/stb_truetype.hsrc/CMakeLists.txt +2
-2@@ -22,8 +22,8 @@ install(TARGETS kshark LIBRARY DESTINATION ${_LIBDIR}/${KS_APP_NAME}) if (OPENGL_FOUND AND GLUT_FOUND) message(STATUS "libkshark-plot") - add_library(kshark-plot SHARED libkshark-plot.c - KsPlotTools.cpp) + add_library(kshark-plot SHARED libkshark-plot.c) +# KsPlotTools.cpp) target_link_libraries(kshark-plot kshark ${GLUT_LIBRARY}src/libkshark-plot.c +36
-6@@ -1,13 +1,22 @@/* SPDX-License-Identifier: LGPL-2.1 */ /* -* Copyright (C) 2018 VMware Inc, Yordan Karadzhov <[email protected]> +* Copyright (C) 2018 VMware Inc, Yordan Karadzhov (VMware) <[email protected]> */ -/** -* @file libkshark-plot.c -* @brief Basic tools for OpenGL plotting. -*/ +/** +* @file libkshark-plot.c +* @brief Basic tools for OpenGL plotting. +*/ + +// C +#ifndef _GNU_SOURCE +/** Use GNU C Library. */ +#define _GNU_SOURCE +#endif // _GNU_SOURCE + +#include <string.h> +#include <stdio.h> // OpenGL #include <GL/freeglut.h> @@ -16,6 +25,17 @@// KernelShark #include "libkshark-plot.h" +/* +* STB TrueType (single-file public domain library) +* https://github.com/nothings/stb +*/ +/** Generate implementation. */ +#define STB_TRUETYPE_IMPLEMENTATION + +/** Make the implementation private. */ +#define STBTT_STATIC +#include "stb_truetype.h" +/** * @brief Create an empty scene for drawing. * @@ -66,7 +86,7 @@ void ksplot_init_opengl(int dpr)glEnable (GL_POLYGON_SMOOTH );glLineWidth (1.5 * dpr);glPointSize (2.5 * dpr); -glClearColor (1 ,1 ,1 ,1 ); +glClearColor (1 ,1 ,1 ,1 );// White }/** @@ -210,3 +230,215 @@ void ksplot_draw_polygon_contour(const struct ksplot_point *points, col, size); } + +/** +* @brief Find a TrueType font file. +* +* @param font_family: The family name of the font. +* @param font_name: The name of the font file without the extention. +* +* @returns A string containing the absolute path to the TrueType font file +* on success, or NULL on failure. The user is responsible for freeing +* the string.
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 252
Quoted 10 lines
src/libkshark-plot.c +10
-0+*/ +char *ksplot_find_font_file (const char *font_family,const char *font_name) +{ +char buffer[1024 ], *end; +char *cmd =NULL ; +int ret; +FILE *f; + + ret =asprintf (&cmd,"fc-list \'%s\' |grep %s.ttf" , font_family, + font_name);
This is sorta a hack, but for now we can keep it and improve it later, as it is wrapped in this function.
Perhaps add a comment /* FIXME: Do this a bit more properly */
Quoted 43 lines
src/libkshark-plot.c +43
-0+if (ret <=0 ) + goto fail; + + f =popen (cmd,"r" ); +free (cmd); +if (f ==NULL ) + goto fail; + + end =fgets (buffer,sizeof (buffer), f); +pclose (f); +if (!end) + goto fail; + + end =strchr (buffer,':' ); +if (!end) + goto fail; + +return strndup (buffer, end - buffer); + + fail: +fprintf (stderr,"Failed to find font file.\n" ); +return NULL ; +} + +/** The size of the bitmap matrix used to load the font. */ +#define KS_FONT_BITMAP_SIZE 1024 + +/** +* @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 ]; +int ascent, descent, line_gap, lsb; +ssize_t buff_size, ret; +unsigned char *buffer; +stbtt_fontinfo info; +FILE *font_file; +float scale;
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 305
Quoted 10 lines
src/libkshark-plot.c +10
-0+ + 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);
I would use stat(2) to find the file size.
Quoted 66 lines
src/libkshark-plot.c +66
-0+fseek (font_file,0 ,SEEK_SET ); + + buffer =malloc (buff_size); +if (!buffer) { +fprintf (stderr,"Failed to allocat memory for font!\n" ); + goto close_file; + } + + ret =fread (buffer, buff_size,1 , font_file); +fclose (font_file); + +if (ret ==0 ) { +fprintf (stderr,"Failed to read from font file!\n" ); + goto free_buffer; + } + +if (!stbtt_InitFont (&info, buffer,0 )) { +fprintf (stderr,"Failed to init font!\n" ); + goto free_buffer; + } + +/* Get the font's metrics. */ + scale =stbtt_ScaleForMappingEmToPixels (&info, size); +stbtt_GetFontVMetrics (&info, &ascent, &descent, &line_gap); +if (line_gap ==0 ) + line_gap = - descent; +/* +* Calculate the dimensions of the font. Note that the descent has +* a negative value. +*/ + font->height = (- descent + ascent + line_gap) * scale; + font->base = (- descent + line_gap /2 ) * scale; + font->size = size; + +/* +* The width of the 'z' character will be considered as an average +* character width. +*/ +stbtt_GetCodepointHMetrics (&info,'z' , &font->char_width, &lsb); + font->char_width *= scale; + + 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 free_buffer; + } + +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,
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 381
Quoted 10 lines
src/libkshark-plot.c +10
-0+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 ); +
I don't really understand any of the above, so I'm assuming you know what it is doing ;-)
Quoted 48 lines
src/libkshark-plot.c +48
-0+return true; + + close_file: +fclose (font_file); + + free_buffer: +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 ); + +/* Set the color of the text. */ +if (col) +glColor3ub (col->red, col->green, col->blue); +else +glColor3ub (0 ,0 ,0 );// Black + +glBindTexture (GL_TEXTURE_2D , font->texture_id); +glBegin (GL_QUADS ); +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);
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 439
Quoted 10 lines
src/libkshark-plot.c +10
-0+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);
How is the x and y moved to the next location?
src/stb_truetype.h
Quoted 85 lines
src/libkshark-plot.c +5
-0+ } + +glEnd (); +glDisable (GL_TEXTURE_2D ); +}src/libkshark-plot.h +57
-0@@ -12,6 +12,15 @@ #ifndef _LIB_KSHARK_PLOT_H #define _LIB_KSHARK_PLOT_H +// C +#include <stdbool.h> + +/* + * STB TrueType (single-file public domain library) + * https://github.com/nothings/stb + */ +#include "stb_truetype.h" + #ifdef __cplusplus extern "C" { #endif@@ -62,6 +71,54 @@ 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 ]; + +/** The height of a text line. */ +int height; + +/** The vertical position of the font's baseline. */ +int base; + +/** The size of the font. */ +int size; + +/** +* The width of the 'z' character. To be use as an average character +* width. +*/ +int char_width; +}; + +/** Check if the texture of the font is loaded. */ +static inline bool ksplot_font_is_loaded (struct ksplot_font *f) +{ +return f->texture_id >1 ; +} + +char *ksplot_find_font_file (const char *font_family,const char *font_name); + +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 src/stb_truetype.h new file +0
-0
I'm assuming the rest is what you took from public domain?
Quoted 35 lines
src/stb_truetype.h new file +34
-0@@ -0,0 +1,5011 @@ +// stb_truetype.h - v1.24 - public domain +// authored from 2009-2020 by Sean Barrett / RAD Game Tools +// +// ======================================================================= +// +// NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES +// +// This library does no range checking of the offsets found in the file, +// meaning an attacker can use it to read arbitrary memory. +// +// ======================================================================= +// +// This library processes TrueType files: +// parse files +// extract glyph metrics +// extract glyph shapes +// render glyphs to one-channel bitmaps with antialiasing (box filter) +// render glyphs to one-channel SDF bitmaps (signed-distance field/function) +// +// Todo: +// non-MS cmaps +// crashproof on bad data +// hinting? (no longer patented) +// cleartype-style AA? +// optimize: use simple memory allocator for intermediates +// optimize: build edge-list directly from curves +// optimize: rasterize directly from curves? +// +// ADDITIONAL CONTRIBUTORS +// +// Mikko Mononen: compound shape support, more cmap formats +// Tor Andersson: kerning, subpixel rendering +// Dougall Johnson: OpenType / Type 2 font handling +// Daniel Ribeiro Maciel: basic GPOS-based kerning
[..]
Re: [PATCH 1/4] kernel-shark: Add support for drawing text
Quoted 148 lines · Steven Rostedt
Quoted 131 lines · On Fri, 20 Nov 2020 11:50:28 +0200 "Yordan Karadzhov (VMware)"…
OpenGL doesn't provide support for text rendering. Here we first re-enable the compilation of the OpenGL wrapper and add the 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]>--- CMakeLists.txt | 6 +- Documentation/doxygen/dox_config | 1 + src/CMakeLists.txt | 4 +- src/libkshark-plot.c | 244 +- src/libkshark-plot.h | 57 + src/stb_truetype.h | 5011 ++++++++++++++++++++++++++++++ 6 files changed, 5312 insertions(+), 11 deletions(-)create mode 100644
src/stb_truetype.hCMakeLists.txt +3
-3@@ -26,9 +26,9 @@ include(${KS_DIR}/build/FindJSONC.cmake) find_package(Doxygen) -# set(OpenGL_GL_PREFERENCE LEGACY) -# find_package(OpenGL) -# find_package(GLUT) +set(OpenGL_GL_PREFERENCE LEGACY) +find_package(OpenGL) +find_package(GLUT) # find_package(Qt5Widgets 5.7.1) # find_package(Qt5Network)Documentation/doxygen/dox_config +1
-0@@ -14,3 +14,4 @@ SORT_MEMBER_DOCS = NO STRICT_PROTO_MATCHING = YES DOT_MULTI_TARGETS = YES PROJECT_LOGO = ../../icons/KS_logo_stacked.svg +EXCLUDE = ../../src/stb_truetype.hsrc/CMakeLists.txt +2
-2@@ -22,8 +22,8 @@ install(TARGETS kshark LIBRARY DESTINATION ${_LIBDIR}/${KS_APP_NAME}) if (OPENGL_FOUND AND GLUT_FOUND) message(STATUS "libkshark-plot") - add_library(kshark-plot SHARED libkshark-plot.c - KsPlotTools.cpp) + add_library(kshark-plot SHARED libkshark-plot.c) +# KsPlotTools.cpp) target_link_libraries(kshark-plot kshark ${GLUT_LIBRARY}src/libkshark-plot.c +36
-6@@ -1,13 +1,22 @@/* SPDX-License-Identifier: LGPL-2.1 */ /* -* Copyright (C) 2018 VMware Inc, Yordan Karadzhov <[email protected]> +* Copyright (C) 2018 VMware Inc, Yordan Karadzhov (VMware) <[email protected]> */ -/** -* @file libkshark-plot.c -* @brief Basic tools for OpenGL plotting. -*/ +/** +* @file libkshark-plot.c +* @brief Basic tools for OpenGL plotting. +*/ + +// C +#ifndef _GNU_SOURCE +/** Use GNU C Library. */ +#define _GNU_SOURCE +#endif // _GNU_SOURCE + +#include <string.h> +#include <stdio.h> // OpenGL #include <GL/freeglut.h> @@ -16,6 +25,17 @@// KernelShark #include "libkshark-plot.h" +/* +* STB TrueType (single-file public domain library) +* https://github.com/nothings/stb +*/ +/** Generate implementation. */ +#define STB_TRUETYPE_IMPLEMENTATION + +/** Make the implementation private. */ +#define STBTT_STATIC +#include "stb_truetype.h" +/** * @brief Create an empty scene for drawing. * @@ -66,7 +86,7 @@ void ksplot_init_opengl(int dpr)glEnable (GL_POLYGON_SMOOTH );glLineWidth (1.5 * dpr);glPointSize (2.5 * dpr); -glClearColor (1 ,1 ,1 ,1 ); +glClearColor (1 ,1 ,1 ,1 );// White }/** @@ -210,3 +230,215 @@ void ksplot_draw_polygon_contour(const struct ksplot_point *points, col, size); } + +/** +* @brief Find a TrueType font file. +* +* @param font_family: The family name of the font. +* @param font_name: The name of the font file without the extention. +* +* @returns A string containing the absolute path to the TrueType font file +* on success, or NULL on failure. The user is responsible for freeing +* the string. src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 252
Quoted 10 lines
src/libkshark-plot.c +10
-0+*/ +char *ksplot_find_font_file (const char *font_family,const char *font_name) +{ +char buffer[1024 ], *end; +char *cmd =NULL ; +int ret; +FILE *f; + + ret =asprintf (&cmd,"fc-list \'%s\' |grep %s.ttf" , font_family, + font_name);This is sorta a hack, but for now we can keep it and improve it later, as it is wrapped in this function.
Quoted 1 line
Perhaps add a comment /* FIXME: Do this a bit more properly */
OK, I will add the comment.
Quoted 49 lines
Quoted 49 lines
src/libkshark-plot.c +47
-0+if (ret <=0 ) + goto fail; + + f =popen (cmd,"r" ); +free (cmd); +if (f ==NULL ) + goto fail; + + end =fgets (buffer,sizeof (buffer), f); +pclose (f); +if (!end) + goto fail; + + end =strchr (buffer,':' ); +if (!end) + goto fail; + +return strndup (buffer, end - buffer); + + fail: +fprintf (stderr,"Failed to find font file.\n" ); +return NULL ; +} + +/** The size of the bitmap matrix used to load the font. */ +#define KS_FONT_BITMAP_SIZE 1024 + +/** +* @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 ]; +int ascent, descent, line_gap, lsb; +ssize_t buff_size, ret; +unsigned char *buffer; +stbtt_fontinfo info; +FILE *font_file; +float scale; + + font_file =fopen (file,"rb" ); +if (!font_file) { +fprintf (stderr,"Failed to open font file!\n" );
Quoted 8 lines
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 305
Quoted 6 lines
src/libkshark-plot.c +6
-0+return false; + } + +/* Get the size of the file. */ +fseek (font_file,0 ,SEEK_END ); + buff_size =ftell (font_file);I would use stat(2) to find the file size.
I will fix this, thanks!
Quoted 72 lines
Quoted 72 lines
src/libkshark-plot.c +71
-0+fseek (font_file,0 ,SEEK_SET ); + + buffer =malloc (buff_size); +if (!buffer) { +fprintf (stderr,"Failed to allocat memory for font!\n" ); + goto close_file; + } + + ret =fread (buffer, buff_size,1 , font_file); +fclose (font_file); + +if (ret ==0 ) { +fprintf (stderr,"Failed to read from font file!\n" ); + goto free_buffer; + } + +if (!stbtt_InitFont (&info, buffer,0 )) { +fprintf (stderr,"Failed to init font!\n" ); + goto free_buffer; + } + +/* Get the font's metrics. */ + scale =stbtt_ScaleForMappingEmToPixels (&info, size); +stbtt_GetFontVMetrics (&info, &ascent, &descent, &line_gap); +if (line_gap ==0 ) + line_gap = - descent; +/* +* Calculate the dimensions of the font. Note that the descent has +* a negative value. +*/ + font->height = (- descent + ascent + line_gap) * scale; + font->base = (- descent + line_gap /2 ) * scale; + font->size = size; + +/* +* The width of the 'z' character will be considered as an average +* character width. +*/ +stbtt_GetCodepointHMetrics (&info,'z' , &font->char_width, &lsb); + font->char_width *= scale; + + 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 free_buffer; + } + +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); +
Quoted 8 lines
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 381
Quoted 5 lines
src/libkshark-plot.c +5
-0+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 ); +I don't really understand any of the above, so I'm assuming you know what it is doing ;-)
;-)
Quoted 53 lines
Quoted 53 lines
src/libkshark-plot.c +52
-0+return true; + + close_file: +fclose (font_file); + + free_buffer: +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 ); + +/* Set the color of the text. */ +if (col) +glColor3ub (col->red, col->green, col->blue); +else +glColor3ub (0 ,0 ,0 );// Black + +glBindTexture (GL_TEXTURE_2D , font->texture_id); +glBegin (GL_QUADS ); +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);
Quoted 8 lines
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 439
Quoted 6 lines
src/libkshark-plot.c +6
-0+ +glTexCoord2f (quad.s1, quad.t0); +glVertex2f (quad.x1, quad.y0); + +glTexCoord2f (quad.s0, quad.t0); +glVertex2f (quad.x0, quad.y0);How is the x and y moved to the next location?
Actually the "y" does not change at all here, because for the moment we can't draw multi-line text. "x" gets incremented inside stbtt_GetBakedQuad(). The horizontal advance of the character is used to calculate the new position.
Thanks a lot!
Yordan
Quoted 127 lines
src/stb_truetype.h
Quoted 86 lines
src/libkshark-plot.c +5
-0+ } + +glEnd (); +glDisable (GL_TEXTURE_2D ); +}src/libkshark-plot.h +57
-0@@ -12,6 +12,15 @@ #ifndef _LIB_KSHARK_PLOT_H #define _LIB_KSHARK_PLOT_H +// C +#include <stdbool.h> + +/* + * STB TrueType (single-file public domain library) + * https://github.com/nothings/stb + */ +#include "stb_truetype.h" + #ifdef __cplusplus extern "C" { #endif@@ -62,6 +71,54 @@ 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 ]; + +/** The height of a text line. */ +int height; + +/** The vertical position of the font's baseline. */ +int base; + +/** The size of the font. */ +int size; + +/** +* The width of the 'z' character. To be use as an average character +* width. +*/ +int char_width; +}; + +/** Check if the texture of the font is loaded. */ +static inline bool ksplot_font_is_loaded (struct ksplot_font *f) +{ +return f->texture_id >1 ; +} + +char *ksplot_find_font_file (const char *font_family,const char *font_name); + +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 src/stb_truetype.h new file +0
-0I'm assuming the rest is what you took from public domain?
Quoted 35 lines
src/stb_truetype.h new file +34
-0@@ -0,0 +1,5011 @@ +// stb_truetype.h - v1.24 - public domain +// authored from 2009-2020 by Sean Barrett / RAD Game Tools +// +// ======================================================================= +// +// NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES +// +// This library does no range checking of the offsets found in the file, +// meaning an attacker can use it to read arbitrary memory. +// +// ======================================================================= +// +// This library processes TrueType files: +// parse files +// extract glyph metrics +// extract glyph shapes +// render glyphs to one-channel bitmaps with antialiasing (box filter) +// render glyphs to one-channel SDF bitmaps (signed-distance field/function) +// +// Todo: +// non-MS cmaps +// crashproof on bad data +// hinting? (no longer patented) +// cleartype-style AA? +// optimize: use simple memory allocator for intermediates +// optimize: build edge-list directly from curves +// optimize: rasterize directly from curves? +// +// ADDITIONAL CONTRIBUTORS +// +// Mikko Mononen: compound shape support, more cmap formats +// Tor Andersson: kerning, subpixel rendering +// Dougall Johnson: OpenType / Type 2 font handling +// Daniel Ribeiro Maciel: basic GPOS-based kerning [..]
Re: [PATCH 1/4] kernel-shark: Add support for drawing text
Quoted 11 lines · Yordan Karadzhov (VMware)
src/stb_truetype.h
Quoted 9 lines
#ifdef
__cplusplus
}
#endifsrc/stb_truetype.h new file +0
-0I'm assuming the rest is what you took from public domain?
Yes, the rest is the public domain source code.
Thanks!
Y.
Quoted 11 lines
Quoted 11 lines
src/stb_truetype.h new file +4
-0@@ -0,0 +1,5011 @@ +// stb_truetype.h - v1.24 - public domain +// authored from 2009-2020 by Sean Barrett / RAD Game Tools +// +// =======================================================================
src/stb_truetype.h new file +4
-0+// +// NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES +// +// This library does no range
Re: [PATCH 1/4] kernel-shark: Add support for drawing text
In reply to Yordan Karadzhov (VMware)Quoted 22 lines · On Wed, 9 Dec 2020 15:30:38 +0200 "Yordan Karadzhov (VMware)"…
Quoted 13 lines
Quoted 13 lines
src/libkshark-plot.c +13
-0+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);Quoted 8 lines
src/libkshark-plot.c void ksplot_draw_polygon_contour(const struct ksplot_point *points, · line 439
Quoted 6 lines
src/libkshark-plot.c +6
-0+ +glTexCoord2f (quad.s1, quad.t0); +glVertex2f (quad.x1, quad.y0); + +glTexCoord2f (quad.s0, quad.t0); +glVertex2f (quad.x0, quad.y0);How is the x and y moved to the next location?
Quoted 4 lines
Actually the "y" does not change at all here, because for the moment we can't draw multi-line text. "x" gets incremented inside
stbtt_GetBakedQuad(). The horizontal advance of the character is used to calculate the new position.
A comment next to that should be stated.
/* x is incremented here to a new position */ stbtt_GetBakedQuad(....)
-- Steve
PATCH 2/4 kernel-shark: Make GLUT optional dependency
In reply to Yordan Karadzhov (VMware)GLUT is not needed in order to build the KernelShark GUI. Currently it is being used only by the plotting example, so it makes more sense to removed it from the list of compulsory third party packages. The patch optimizes the way the OpenGL and GLUT headers are included and removes some duplicated code.
--- build/deff.h.cmake | 3 +++ src/CMakeLists.txt | 4 ++-- src/libkshark-plot.c | 20 +++++++------------- src/libkshark-plot.h | 11 +++++++++++ 4 files changed, 23 insertions(+), 15 deletions(-)
build/deff.h.cmake +3 -0
@@ -23,6 +23,9 @@ /** "pkexec" executable. */ #cmakedefine DO_AS_ROOT "@DO_AS_ROOT@" +/** GLUT has been found. */ +#cmakedefine GLUT_FOUND + /** Semicolon-separated list of plugin names. */ #define KS_BUILTIN_PLUGINS "@PLUGINS@"
src/CMakeLists.txt +2 -2
@@ -19,7 +19,7 @@ set_target_properties(kshark PROPERTIES SUFFIX ".so.${KS_VERSION_STRING}") install(TARGETS kshark LIBRARY DESTINATION ${_LIBDIR}/${KS_APP_NAME}) -if (OPENGL_FOUND AND GLUT_FOUND) +if (OPENGL_FOUND) message(STATUS "libkshark-plot") add_library(kshark-plot SHARED libkshark-plot.c)
@@ -33,7 +33,7 @@ if (OPENGL_FOUND AND GLUT_FOUND) install(TARGETS kshark-plot LIBRARY DESTINATION ${_LIBDIR}/${KS_APP_NAME}) -endif (OPENGL_FOUND AND GLUT_FOUND) +endif (OPENGL_FOUND) if (Qt5Widgets_FOUND AND Qt5Network_FOUND)
src/libkshark-plot.c +7 -13
@@ -18,10 +18,6 @@#include <string.h> #include <stdio.h> -// OpenGL -#include <GL/freeglut.h> -#include <GL/gl.h> -// KernelShark #include "libkshark-plot.h"
@@ -36,6 +32,10 @@#define STBTT_STATIC #include "stb_truetype.h" +#ifdef GLUT_FOUND + +#include <GL/freeglut.h> +/** * @brief Create an empty scene for drawing. *
@@ -60,17 +60,11 @@ void ksplot_make_scene(int width, int height)/* Open the screen window. */ glutCreateWindow ("KernelShark Plot" ); -/* -* Set the origin of the coordinate system to be the top left corner. -* The "Y" coordinate is inverted. -*/ -gluOrtho2D (0 , width, height,0 ); -glViewport (0 ,0 , width, height); - -glMatrixMode (GL_PROJECTION ); -glLoadIdentity (); + ksplot_resize_opengl(width, height); } +#endif // GLUT_FOUND +/** * @brief Initialize OpenGL. *
src/libkshark-plot.h +11 -0
@@ -15,6 +15,10 @@// C #include <stdbool.h> +// OpenGL +#include <GL/gl.h> +#include <GL/glu.h> +/* * STB TrueType (single-file public domain library) * https://github.com/nothings/stb
@@ -25,6 +29,9 @@ extern "C" { #endif +// KernelShark +#include "KsCmakeDef.hpp" + /** Structure defining a RGB color. */ struct ksplot_color { /** The Red component of the color. */
@@ -46,8 +53,12 @@ struct ksplot_point {int y; }; +#ifdef GLUT_FOUND +void ksplot_make_scene (int width,int height); +#endif // GLUT_FOUND +void ksplot_init_opengl (int dpr);void ksplot_resize_opengl (int width,int height);
Signature · 1 line
2.25.1
PATCH 3/4 kernel-shark: Add ksplot_draw_polyline()
In reply to Yordan Karadzhov (VMware)The method draws continuous line between an ordered array of points (poly-line).
--- src/libkshark-plot.c | 32 ++++++++++++++++++++++++++------ src/libkshark-plot.h | 5 +++++ 2 files changed, 31 insertions(+), 6 deletions(-)
src/libkshark-plot.c +26 -6
@@ -150,6 +150,30 @@ void ksplot_draw_line(const struct ksplot_point *a,glEnd (); } +/** +* @brief Draw the a polyline. +* +* @param points: Input location for the array of points defining the polyline. +* @param n_points: The size of the array of points. +* @param col: The color of the polyline. +* @param size: The size of the polyline. +*/ +void ksplot_draw_polyline (const struct ksplot_point *points, +size_t n_points, +const struct ksplot_color *col, +float size) +{ +if (!points || !n_points || !col || size <.5f ) +return ; + +/* Loop over the points of the polygon and draw connecting lines. */ +for (size_t i =1 ; i < n_points; ++i) +ksplot_draw_line (&points[i -1 ], + &points[i], + col, + size); +} +/** * @brief Draw a polygon. *
@@ -211,12 +235,8 @@ void ksplot_draw_polygon_contour(const struct ksplot_point *points,if (!points || !n_points || !col || size <.5f )return ; -/* Loop over the points of the polygon and draw connecting lines. */ -for (size_t i =1 ; i < n_points; ++i) -ksplot_draw_line (&points[i -1 ], - &points[i], - col, - size); +/* Loop over the points of the polygon and draw a polyline. */ +ksplot_draw_polyline (points, n_points, col, size);/* Close the contour. */ ksplot_draw_line(&points[0],
src/libkshark-plot.h +5 -0
@@ -72,6 +72,11 @@ void ksplot_draw_line(const struct ksplot_point *a,const struct ksplot_color *col, float size); +void ksplot_draw_polyline (const struct ksplot_point *points, +size_t n_points, +const struct ksplot_color *col, +float size); + void ksplot_draw_polygon(const struct ksplot_point *points, size_t n_points, const struct ksplot_color *col,
Signature · 1 line
2.25.1
PATCH 4/4 kernel-shark: Optimize ksplot_draw_polygon()
In reply to Yordan Karadzhov (VMware)Using a point inside the polygon to start the Triangle Fan looked more intuitive to me when I first implemented this method. I guess this was because when you use an internal point to start, the resulting set of triangles looks like fan indeed. However, as pointed out by Slavomir Kaslev, the Triangle Fan can be started from any corner of the polygon.
--- src/libkshark-plot.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
src/libkshark-plot.c +1 -10
@@ -200,18 +200,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.25.1