Re: [RFC PATCH 2/3] kernel-shark: Add support for drawing text

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[...]

> +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;
> +       }

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);
}

> +
> +       glDisable(GL_TEXTURE_2D);
> +       glEnd();
> +}



[Index of Archives]     [Linux USB Development]     [Linux USB Development]     [Linux Audio Users]     [Yosemite Hiking]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux