]> git.neil.brown.name Git - edlib.git/commitdiff
renderline: always cache the measured size of the image
authorNeilBrown <neil@brown.name>
Tue, 25 Apr 2023 06:09:41 +0000 (16:09 +1000)
committerNeilBrown <neil@brown.name>
Fri, 28 Apr 2023 22:54:20 +0000 (08:54 +1000)
Knowing the size of the image can be generally useful so keep track of
it.

Signed-off-by: NeilBrown <neil@brown.name>
lib-renderline.c

index d74b33f50471c45ea80792a683c855ba6bd465c7..0f85b4036d6f7b75d71d26257a5cf9839c0bb33a 100644 (file)
@@ -418,13 +418,12 @@ static void render_image(struct pane *p safe, struct pane *focus safe,
 {
        char *fname = NULL;
        short width, height;
-       char *size = attr_find(p->attrs, "cached-size");
+       char *ssize = attr_find(p->attrs, "cached-size");
+       struct xy size= {-1, -1};
+
+       width = p->parent->w/2;
+       height = p->parent->h/2;
 
-       if (!size || sscanf(size, "%hdx%hd", &width, &height) != 2) {
-               size = NULL;
-               width = p->parent->w/2;
-               height = p->parent->h/2;
-       }
        while (*line == '<')
                line += 1;
 
@@ -433,22 +432,33 @@ static void render_image(struct pane *p safe, struct pane *focus safe,
 
                if (strncmp(line, "image:", 6) == 0) {
                        fname = strndup(line+6, len-6);
+                       if (!ssize ||
+                           sscanf(ssize, "%hdx%hd", &size.x, &size.y) != 2) {
+                               struct call_return cr =
+                                       home_call_ret(all, focus,
+                                                     "Draw:image-size",
+                                                     p, 0, NULL, fname);
+                               if (cr.x > 0 && cr.y > 0) {
+                                       size.x = cr.x;
+                                       size.y = cr.y;
+                                       asprintf(&ssize, "%hdx%hd",
+                                                cr.x, cr.y);
+                                       attr_set_str(&p->attrs,
+                                                    "cached-size", ssize);
+                               }
+                       }
                } else if (strncmp(line, "width:", 6) == 0) {
                        width = atoi(line + 6);
                        width = width * scale / 1000;
                } else if (strncmp(line, "height:", 7) == 0) {
                        height = atoi(line + 7);
                        height = height * scale / 1000;
-               } else if (!size && fname && strncmp(line, "noupscale", 9) == 0) {
-                       struct call_return cr =
-                               home_call_ret(all, focus, "Draw:image-size",
-                                             p, 0, NULL, fname);
-                       if (cr.x > 0 && cr.x < p->parent->w)
-                               width = cr.x;
-                       if (cr.y > 0 && cr.y < p->parent->h)
-                               height = cr.y;
-                       asprintf(&size, "%hdx%hd", width, height);
-                       attr_set_str(&p->attrs, "cached-size", size);
+               } else if (strncmp(line, "noupscle", 9) == 0 &&
+                          fname && size.x > 0) {
+                       if (size.x < p->parent->w)
+                               width = size.x;
+                       if (size.y < p->parent->h)
+                               height = size.y;
                }
                line += len;
                line += strspn(line, ",");