From 5a772e24b79d16e153e369810f188a952d436c0a Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Sat, 10 Jul 2021 18:52:51 -0700 Subject: [PATCH] Cleaned up set_font_size() in NEDsim. Leaving the inefficient version since we only call it twice. --- hacks/NEDsim/NEDsim.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/hacks/NEDsim/NEDsim.c b/hacks/NEDsim/NEDsim.c index e4d66e2..67fcb32 100644 --- a/hacks/NEDsim/NEDsim.c +++ b/hacks/NEDsim/NEDsim.c @@ -209,8 +209,9 @@ set_color(struct NEDsim * nedsim, struct color_rgb * color) XSetForeground(nedsim->dpy, nedsim->gc, temp.pixel); } -// TODO: Make this a lot faster. -// Set font size to fill 'size' cells vertically, minus space for border and padding. +// Set font size to fill 'size' cells vertically, minus space for border and +// padding. There is probably a better way to do this. The current method is +// horribly slow. Call this function as few times as possible. static void set_font_size(struct NEDsim * nedsim, int size) { @@ -220,9 +221,11 @@ set_font_size(struct NEDsim * nedsim, int size) const char * font_size_prefix = "-*-helvetica-*-r-*-*-"; const char * font_size_suffix = "-*-*-*-*-*-*-*"; - size_t buffer_size = strlen(font_size_prefix) + strlen(font_size_suffix) + 100; // '100' since nobody needs font with size in 'points' greater than 100 decimal digits long. + // In the buffer, we reserve 100 DIGITS for the font size integer. + size_t buffer_size = strlen(font_size_prefix) + strlen(font_size_suffix) + 100; char * font_full_name = malloc(buffer_size); - int font_size_in_points = 2; // Start with a 2 pt font and work our way up. + // Start with a 2pt font and work our way up. + int font_size_in_points = 2; while (1) { // Load the font. -- 2.20.1