aboutsummaryrefslogtreecommitdiff
path: root/src/svg.c
blob: 5536e9a35d600e4eb321e4fbbe341370546629fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * File: svg.c
 *
 * Copyright (C) 2024 Rodrigo Arias Mallo <rodarima@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */

#include <config.h>

#ifdef ENABLE_SVG

#include <stdlib.h> /* For abort() */

#include "msg.h"
#include "image.hh"
#include "cache.h"
#include "dicache.h"

#define NANOSVG_ALL_COLOR_KEYWORDS
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"

typedef struct {
   DilloImage *Image;           /* Image meta data */
   DilloUrl *url;               /* Primary Key for the dicache */
   int version;                 /* Secondary Key for the dicache */
   int bgcolor;                 /* Parent widget background color */
   int fgcolor;                 /* Parent widget foreground color */
} DilloSvg;

/*
 * Free up the resources for this image.
 */
static void Svg_free(DilloSvg *svg)
{
   _MSG("Svg_free: svg=%p\n", svg);

   dFree(svg);
}

/*
 * Finish the decoding process (and free the memory)
 */
static void Svg_close(DilloSvg *svg, CacheClient_t *Client)
{
   _MSG("Svg_close\n");
   /* Let dicache know decoding is over */
   a_Dicache_close(svg->url, svg->version, Client);
   Svg_free(svg);
}

/*
 * Receive and process new chunks of SVG image data
 */
static void Svg_write(DilloSvg *svg, void *Buf, uint_t BufSize)
{
   static NSVGrasterizer *rasterizer = NULL;

   if (!rasterizer)
      rasterizer = nsvgCreateRasterizer();

   if (Buf == NULL || BufSize <= 0)
      return;

   /* SVG image not finished yet */
   if (strstr(Buf, "</svg>") == NULL)
      return;

   /* Use foreground as the current color, but transform to
    * nanosvg color format (BGR). */
   unsigned fg_r = (svg->fgcolor >> 16) & 0xff;
   unsigned fg_g = (svg->fgcolor >>  8) & 0xff;
   unsigned fg_b = (svg->fgcolor >>  0) & 0xff;
   unsigned curcolor = NSVG_RGB(fg_r, fg_g, fg_b);

   /* NULL-terminate Buf */
   char *str = dStrndup(Buf, BufSize);
   NSVGimage *nimg = nsvgParse(str, "px", svg->Image->dpi, curcolor);
   dFree(str);

   if (nimg == NULL) {
      MSG_ERR("Svg_write: cannot parse SVG image\n");
      return;
   }

   /* The height and width values can be nonintegral */
   unsigned width = nimg->width;
   unsigned height = nimg->height;

   if (width == 0 || height == 0 || width > IMAGE_MAX_AREA / height) {
      MSG_WARN("Svg_write: suspicious image size request %u x %u\n", width, height);
      nsvgDelete(nimg);
      return;
   }

   DilloImgType type = DILLO_IMG_TYPE_RGB;
   unsigned stride = width * 4;

   a_Dicache_set_parms(svg->url, svg->version, svg->Image,
         width, height, type, 1 / 2.2);

   unsigned char *dest = dNew(unsigned char, height * stride);

   nsvgRasterizeXY(rasterizer, nimg, 0, 0, 1, 1, dest, width, height, stride);

   unsigned bg_blue  = (svg->bgcolor) & 0xFF;
   unsigned bg_green = (svg->bgcolor >> 8) & 0xFF;
   unsigned bg_red   = (svg->bgcolor >> 16) & 0xFF;

   unsigned char *line = dNew(unsigned char, width * 3);
   for (unsigned i = 0; i < height; i++) {
      for (unsigned j = 0; j < width; j++) {
         unsigned r = dest[i * stride + 4 * j];
         unsigned g = dest[i * stride + 4 * j + 1];
         unsigned b = dest[i * stride + 4 * j + 2];
         unsigned alpha = dest[i * stride + 4 * j + 3];

         line[3 * j + 0] = (r * alpha + (bg_red   * (0xFF - alpha))) / 0xFF;
         line[3 * j + 1] = (g * alpha + (bg_green * (0xFF - alpha))) / 0xFF;
         line[3 * j + 2] = (b * alpha + (bg_blue  * (0xFF - alpha))) / 0xFF;
      }
      a_Dicache_write(svg->url, svg->version, line, i);
   }
   dFree(dest);
   dFree(line);
   nsvgDelete(nimg);
}

void a_Svg_callback(int Op, void *data)
{
   if (Op == CA_Send) {
      CacheClient_t *Client = data;
      Svg_write(Client->CbData, Client->Buf, Client->BufSize);
   } else if (Op == CA_Close) {
      CacheClient_t *Client = data;
      Svg_close(Client->CbData, Client);
   } else if (Op == CA_Abort) {
      Svg_free(data);
   }
}

/*
 * Create the image state data that must be kept between calls
 */
void *a_Svg_new(DilloImage *Image, DilloUrl *url, int version)
{
   DilloSvg *svg = dNew0(DilloSvg, 1);
   _MSG("a_Svg_new: svg=%p\n", svg);

   svg->Image = Image;
   svg->url = url;
   svg->version = version;
   svg->bgcolor = Image->bg_color;
   svg->fgcolor = Image->fg_color;

   return svg;
}

#else /* ENABLE_SVG */

void *a_Svg_new() { return 0; }
void a_Svg_callback() { return; }

#endif /* ENABLE_SVG */