Use bold for CSD titles

This commit is contained in:
Kovid Goyal 2021-04-01 12:18:48 +05:30
parent 55dc354e68
commit c5b3e43b6c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 15 additions and 20 deletions

View File

@ -77,9 +77,12 @@ cleanup(RenderCtx *ctx) {
void
set_main_face_family(FreeTypeRenderCtx ctx_, const char *family, bool bold, bool italic) {
RenderCtx *ctx = (RenderCtx*)ctx_;
if (family == main_face_family.name || (main_face_family.name && strcmp(family, main_face_family.name) == 0)) return;
if (
(family == main_face_family.name || (main_face_family.name && strcmp(family, main_face_family.name) == 0)) &&
main_face_family.bold == bold && main_face_family.italic == italic
) return;
cleanup(ctx);
main_face_family.name = strdup(family);
main_face_family.name = family ? strdup(family) : NULL;
main_face_family.bold = bold; main_face_family.italic = italic;
}
@ -104,18 +107,6 @@ load_font(FontConfigFace *info, Face *ans) {
return true;
}
static bool
ensure_state(RenderCtx *ctx) {
if (main_face.freetype && main_face.hb) return true;
if (!information_for_font_family(main_face_family.name, main_face_family.bold, main_face_family.italic, &main_face_information)) return false;
if (!load_font(&main_face_information, &main_face)) return false;
hb_buffer = hb_buffer_create();
if (!hb_buffer) { PyErr_NoMemory(); return false; }
return true;
}
static int
font_units_to_pixels_y(FT_Face face, int x) {
return (int)ceil((double)FT_MulFix(x, face->size->metrics.y_scale) / 64.0);
@ -375,9 +366,14 @@ end:
}
FreeTypeRenderCtx
create_freetype_render_context(void) {
create_freetype_render_context(const char *family, bool bold, bool italic) {
RenderCtx *ctx = calloc(1, sizeof(RenderCtx));
if (!ensure_state(ctx)) { free(ctx); return NULL; }
main_face_family.name = family ? strdup(family) : NULL;
main_face_family.bold = bold; main_face_family.italic = italic;
if (!information_for_font_family(main_face_family.name, main_face_family.bold, main_face_family.italic, &main_face_information)) return false;
if (!load_font(&main_face_information, &main_face)) return false;
hb_buffer = hb_buffer_create();
if (!hb_buffer) { PyErr_NoMemory(); return false; }
ctx->created = true;
return (FreeTypeRenderCtx)ctx;
}
@ -399,9 +395,8 @@ render_line(PyObject *self UNUSED, PyObject *args, PyObject *kw) {
PyObject *ans = PyBytes_FromStringAndSize(NULL, width * height * 4);
if (!ans) return NULL;
uint8_t *buffer = (u_int8_t*) PyBytes_AS_STRING(ans);
RenderCtx *ctx = (RenderCtx*)create_freetype_render_context();
RenderCtx *ctx = (RenderCtx*)create_freetype_render_context(family, bold, italic);
if (!ctx) return NULL;
if (family) set_main_face_family((FreeTypeRenderCtx)ctx, family, bold, italic);
if (!render_single_line((FreeTypeRenderCtx)ctx, text, 3 * height / 4, 0, 0xffffffff, buffer, width, height, x_offset, y_offset)) {
Py_CLEAR(ans);
if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError, "Unknown error while rendering text");

View File

@ -11,7 +11,7 @@
typedef struct {bool created;} *FreeTypeRenderCtx;
FreeTypeRenderCtx create_freetype_render_context(void);
FreeTypeRenderCtx create_freetype_render_context(const char *family, bool bold, bool italic);
void set_main_face_family(FreeTypeRenderCtx ctx, const char *family, bool bold, bool italic);
bool render_single_line(FreeTypeRenderCtx ctx, const char *text, unsigned sz_px, uint32_t fg, uint32_t bg, uint8_t *output_buf, size_t width, size_t height, float x_offset, float y_offset);
void release_freetype_render_context(FreeTypeRenderCtx ctx);

View File

@ -401,7 +401,7 @@ static bool
draw_text_callback(GLFWwindow *window, const char *text, uint32_t fg, uint32_t bg, uint8_t *output_buf, size_t width, size_t height, float x_offset, float y_offset) {
if (!set_callback_window(window)) return false;
if (!csd_title_render_ctx) {
csd_title_render_ctx = create_freetype_render_context();
csd_title_render_ctx = create_freetype_render_context(NULL, true, false);
if (!csd_title_render_ctx) {
if (PyErr_Occurred()) PyErr_Print();
return false;