Check-in [a67e4657ca]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:add upstream tksvg changes
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a67e4657caf2c5e51f053e1d93c7b3d316733473
User & Date: chw 2016-10-14 08:58:35
Context
2016-10-14
12:08
add tk upstream changes plus fixes in droid ttk theme check-in: 1af051c03d user: chw tags: trunk
08:58
add upstream tksvg changes check-in: a67e4657ca user: chw tags: trunk
2016-10-12
22:16
add tcl upstream changes check-in: 242f9b888c user: chw tags: trunk
Changes

jni/tcl/library/tzdata/America/Resolute became executable.

jni/tcl/tests/tcltest.test became executable.

Changes to jni/tksvg/generic/tkImgSVG.c.

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207






208
209



210
211
212





















213
214
215
216







217
218
219
220



221


222
223
224




























225
226



227
228

229
230
231



232


233
234
235



236






237
238
239
240
241
242
243
			    int width, int height, int srcX, int srcY);
static int		StringMatchSVG(Tcl_Obj *dataObj, Tcl_Obj *format,
			    int *widthPtr, int *heightPtr, Tcl_Interp *interp);
static int		StringReadSVG(Tcl_Interp *interp, Tcl_Obj *dataObj,
			    Tcl_Obj *format, Tk_PhotoHandle imageHandle,
			    int destX, int destY, int width, int height,
			    int srcX, int srcY);
static NSVGimage *	ParseSVGWithOptions(const char *input, size_t length,








			    Tcl_Obj *format);




Tk_PhotoImageFormat tkImgFmtSVG = {
    "svg",			/* name */
    FileMatchSVG,		/* fileMatchProc */
    StringMatchSVG,		/* stringMatchProc */
    FileReadSVG,		/* fileReadProc */
    StringReadSVG,		/* stringReadProc */
    NULL,			/* fileWriteProc */
    NULL,			/* stringWriteProc */
    NULL
};

static int
FileMatchSVG(Tcl_Channel chan, const char *fileName,
			    Tcl_Obj *format, int *widthPtr, int *heightPtr,
			    Tcl_Interp *interp)
{
    Tcl_Obj *dataObj = Tcl_NewObj();
    int result;

    if (Tcl_ReadChars(chan, dataObj, -1, 0) == -1) {
	/* in case of an error reading the file */
	Tcl_DecrRefCount(dataObj);
	return 0;
    }

    result = StringMatchSVG(dataObj, format, widthPtr, heightPtr, interp);
    
    Tcl_DecrRefCount(dataObj);
    
    return result;
}

static int
FileReadSVG(Tcl_Interp *interp, Tcl_Channel chan,
			    const char *fileName, Tcl_Obj *format,
			    Tk_PhotoHandle imageHandle, int destX, int destY,
			    int width, int height, int srcX, int srcY)
{
    Tcl_Obj *dataObj = Tcl_NewObj();
    int result;

    if (Tcl_ReadChars(chan, dataObj, -1, 0) == -1) {
	/* in case of an error reading the file */
	Tcl_DecrRefCount(dataObj);
	return 0;
    }









    result = StringReadSVG(interp, dataObj, format, imageHandle, 
	destX, destY, width, height, srcX, srcY);

    























    Tcl_DecrRefCount(dataObj);



    




    return result;
}





static int

StringMatchSVG(Tcl_Obj *dataObj, Tcl_Obj *format,

			    int *widthPtr, int *heightPtr, Tcl_Interp *interp)

{

    int length;
    const char *data = Tcl_GetStringFromObj(dataObj, &length);
    NSVGimage* nsvgimage = ParseSVGWithOptions(data, length, format);





    if (nsvgimage != NULL) {
	/* Stupid interface: after parsing the file we destroy the parse 
	 * The image is parsed again on StringReadSVG */
	*widthPtr = nsvgimage -> width;
	*heightPtr = nsvgimage -> height;

	nsvgDelete(nsvgimage);

	return 1;
    }
    return 0;
}

static int

StringReadSVG(Tcl_Interp *interp, Tcl_Obj *dataObj,


			    Tcl_Obj *format, Tk_PhotoHandle imageHandle,

			    int destX, int destY, int width, int height,
			    int srcX, int srcY)
{   




    int length, w, h, c;

    const char *data = Tcl_GetStringFromObj(dataObj, &length);










    NSVGimage* nsvgimage = ParseSVGWithOptions(data, length, format);
    NSVGrasterizer *rast;

    unsigned char *imgdata;


    





















































































    if (nsvgimage == NULL) {
	Tcl_SetResult(interp, "Cannot parse SVG image", TCL_STATIC);










	return TCL_ERROR;
    }









    





    w = nsvgimage -> width;
    h = nsvgimage -> height; 

    rast = nsvgCreateRasterizer();
   
    if (rast == NULL) {
	Tcl_SetResult(interp, "SVG: Could not initialize rasterizer", TCL_STATIC);


	goto cleanAST;
    }

    imgdata = ckalloc(w*h*4);

    if (imgdata == NULL) {
	Tcl_SetResult(interp, "Could not alloc image buffer", TCL_STATIC);

	goto cleanRAST;
    }

    nsvgRasterize(rast, nsvgimage, 0,0,1, imgdata, w, h, w*4);
    
    /* transfer the data to a photo block */
    Tk_PhotoImageBlock svgblock;
    svgblock.pixelPtr = imgdata;
    svgblock.width = w;
    svgblock.height = h;
    svgblock.pitch = w*4;
    svgblock.pixelSize = 4;
    for (c=0; c<=3; c++) {
	svgblock.offset[c]=c;
    }

    if (Tk_PhotoExpand(interp, imageHandle,
	    destX + width, destY + height) != TCL_OK) {
	goto cleanRAST;
    }

    if (Tk_PhotoPutBlock(interp, imageHandle, &svgblock, destX, destY,
		width, height, TK_PHOTO_COMPOSITE_SET) != TCL_OK) {
	goto cleanimg;
    }


    ckfree(imgdata);
    nsvgDeleteRasterizer(rast);
    nsvgDelete(nsvgimage);
    return TCL_OK;

cleanimg:
    ckfree(imgdata);
    
cleanRAST:
    nsvgDeleteRasterizer(rast);

cleanAST:
    nsvgDelete(nsvgimage);
    return TCL_ERROR;
}








static NSVGimage * ParseSVGWithOptions(const char *input, size_t length, Tcl_Obj *format)



{
    /* The parser destroys the original input string, therfore first duplicate */
    char *input_dup = ckalloc(length+1);





















    NSVGimage *nsvgi;

    memcpy(input_dup, input, length);
    input_dup[length]='\0';







    
    /* here we should read the options (dpi) from format TODO */
    nsvgi = nsvgParse(input_dup, "px", 96.0);
    ckfree(input_dup);



    


    return nsvgi;
}





























int Tksvg_Init(Tcl_Interp* interp)
{



    if (interp == 0) return TCL_ERROR;


    if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
	return TCL_ERROR;
    }






    if (Tk_InitStubs(interp, TCL_VERSION, 0) == NULL) {
	return TCL_ERROR;
    }










    Tk_CreatePhotoImageFormat(&tkImgFmtSVG);

    Tcl_PkgProvide(interp, PACKAGE_NAME, PACKAGE_VERSION);

    return TCL_OK;
}








|
>
>
>
>
>
>
>
>
|
>
>

<
|










|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<





>
>
>
>
>
>
>
>
|
<
<
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
|
|
|
>
>
|
>

>
|
>
|
>

<

|
<
>

>
>
>
|
<
<
|
|
>
|
>






>
|
>
>
|
>
|
|

>
>
>

<
>
|
>
>
>
>
>
>
>
>
>
>
|
<
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
|
|

>
>
>
>
>
>
>
>
|
>
>
>
>
>
|
<
>

<

|
>
>


<
<
>
|
|
>


<
|
<

<
|


|

|
|

<

|


<




<
<
|

|



|





|



>
>
>
>
>
>
|
<
>
>
>
|
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

<
<
>
>
>
>
>
>
>
|
<
<
<
>
>
>
|
>
>
|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
>
|
|
>



>
>
>
|
>
>



>
>
>
|
>
>
>
>
>
>

<

<


<
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
171
172
173


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

198
199
200
201
202
203
204
205
206
207
208
209
210

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

331
332

333
334
335
336
337
338


339
340
341
342
343
344

345

346

347
348
349
350
351
352
353
354

355
356
357
358

359
360
361
362


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

386
387
388
389


390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412


413
414
415
416
417
418
419
420



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

489

490
491

			    int width, int height, int srcX, int srcY);
static int		StringMatchSVG(Tcl_Obj *dataObj, Tcl_Obj *format,
			    int *widthPtr, int *heightPtr, Tcl_Interp *interp);
static int		StringReadSVG(Tcl_Interp *interp, Tcl_Obj *dataObj,
			    Tcl_Obj *format, Tk_PhotoHandle imageHandle,
			    int destX, int destY, int width, int height,
			    int srcX, int srcY);
static NSVGimage *	ParseSVGWithOptions(Tcl_Interp *interp,
			    const char *input, int length, Tcl_Obj *format);
static int		RasterizeSVG(Tcl_Interp *interp,
			    Tk_PhotoHandle imageHandle, NSVGimage *nsvgImage,
			    int destX, int destY, int width, int height,
			    int srcX, int srcY);
static int		CacheSVG(Tcl_Interp *interp, ClientData dataOrChan,
			    Tcl_Obj *formatObj, NSVGimage *nsvgImage);
static NSVGimage *	GetCachedSVG(Tcl_Interp *interp, ClientData dataOrChan,
			    Tcl_Obj *formatObj);
static void		CleanCache(Tcl_Interp *interp);
static void		FreeCache(ClientData clientData, Tcl_Interp *interp);


static Tk_PhotoImageFormat tkImgFmtSVG = {
    "svg",			/* name */
    FileMatchSVG,		/* fileMatchProc */
    StringMatchSVG,		/* stringMatchProc */
    FileReadSVG,		/* fileReadProc */
    StringReadSVG,		/* stringReadProc */
    NULL,			/* fileWriteProc */
    NULL,			/* stringWriteProc */
    NULL
};

/*
 * Per interp cache of last NSVGimage which was matched to
 * be immediately rasterized after the match. This helps to
 * eliminate double parsing of the SVG file/string.
 */

typedef struct {
    ClientData dataOrChan;
    Tcl_DString formatString;
    NSVGimage *nsvgImage;
} NSVGcache;

static int
FileMatchSVG(
    Tcl_Channel chan,
    const char *fileName,
    Tcl_Obj *formatObj,
    int *widthPtr, int *heightPtr,
    Tcl_Interp *interp)
{
    int length;
    Tcl_Obj *dataObj = Tcl_NewObj();
    const char *data;
    NSVGimage *nsvgImage;

    CleanCache(interp);




    if (Tcl_ReadChars(chan, dataObj, -1, 0) == -1) {
	/* in case of an error reading the file */
	Tcl_DecrRefCount(dataObj);
	return 0;
    }
    data = Tcl_GetStringFromObj(dataObj, &length);
    nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj);
    Tcl_DecrRefCount(dataObj);
    if (nsvgImage != NULL) {
	*widthPtr = nsvgImage->width;
	*heightPtr = nsvgImage->height;
	if (!CacheSVG(interp, chan, formatObj, nsvgImage)) {
	    nsvgDelete(nsvgImage);
	}


	return 1;
    }
    return 0;
}

static int
FileReadSVG(
    Tcl_Interp *interp,
    Tcl_Channel chan,
    const char *fileName,
    Tcl_Obj *formatObj,
    Tk_PhotoHandle imageHandle,
    int destX, int destY,
    int width, int height,
    int srcX, int srcY)
{
    int length;
    const char *data;
    NSVGimage *nsvgImage = GetCachedSVG(interp, chan, formatObj);

    if (nsvgImage == NULL) {
        Tcl_Obj *dataObj = Tcl_NewObj();

	if (Tcl_ReadChars(chan, dataObj, -1, 0) == -1) {
	    /* in case of an error reading the file */
	    Tcl_DecrRefCount(dataObj);
	    Tcl_SetResult(interp, "read error", TCL_STATIC);
	    Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "READ_ERROR", NULL);
	    return TCL_ERROR;
	}
        data = Tcl_GetStringFromObj(dataObj, &length);
	nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj);
	Tcl_DecrRefCount(dataObj);
	if (nsvgImage == NULL) {
	    return TCL_ERROR;
	}
    }
    return RasterizeSVG(interp, imageHandle, nsvgImage, destX, destY,
		width, height, srcX, srcY);
}

static int
StringMatchSVG(
    Tcl_Obj *dataObj,
    Tcl_Obj *formatObj,
    int *widthPtr, int *heightPtr,
    Tcl_Interp *interp)
{

    int length;
    const char *data;

    NSVGimage *nsvgImage;

    CleanCache(interp);
    data = Tcl_GetStringFromObj(dataObj, &length);
    nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj);
    if (nsvgImage != NULL) {


	*widthPtr = nsvgImage->width;
	*heightPtr = nsvgImage->height;
	if (!CacheSVG(interp, dataObj, formatObj, nsvgImage)) {
	    nsvgDelete(nsvgImage);
	}
	return 1;
    }
    return 0;
}

static int
StringReadSVG(
    Tcl_Interp *interp,
    Tcl_Obj *dataObj,
    Tcl_Obj *formatObj,
    Tk_PhotoHandle imageHandle,
    int destX, int destY,
    int width, int height,
    int srcX, int srcY)
{   
    int length;
    const char *data;
    NSVGimage *nsvgImage = GetCachedSVG(interp, dataObj, formatObj);


    if (nsvgImage == NULL) {
        data = Tcl_GetStringFromObj(dataObj, &length);
	nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj);
    }
    if (nsvgImage == NULL) {
	return TCL_ERROR;
    }
    return RasterizeSVG(interp, imageHandle, nsvgImage, destX, destY,
		width, height, srcX, srcY);
}

static NSVGimage *
ParseSVGWithOptions(

    Tcl_Interp *interp,
    const char *input,
    int length,
    Tcl_Obj *formatObj)
{
    Tcl_Obj **objv = NULL;
    int objc = 0;
    double dpi = 96.0;
    char unit[3], *p;
    char *inputCopy = NULL;
    NSVGimage *nsvgImage;
    static const char *const fmtOptions[] = {
        "-dpi", "-unit", NULL
    };
    enum fmtOptions {
        OPT_DPI, OPT_UNIT
    };

    /*
     * The parser destroys the original input string,
     * therefore first duplicate.
     */

    inputCopy = attemptckalloc(length+1);
    if (inputCopy == NULL) {
	Tcl_SetResult(interp, "cannot alloc data buffer", TCL_STATIC);
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "OUT_OF_MEMORY", NULL);
	goto error;
    }
    memcpy(inputCopy, input, length);
    inputCopy[length] = '\0';

    /*
     * Process elements of format specification as a list.
     */

    strcpy(unit, "px");
    if ((formatObj != NULL) &&
	    Tcl_ListObjGetElements(interp, formatObj, &objc, &objv) != TCL_OK) {
        goto error;
    }
    for (; objc > 0 ; objc--, objv++) {
	int optIndex;

	/*
	 * Ignore the "svg" part of the format specification.
	 */

	if (!strcasecmp(Tcl_GetString(objv[0]), "svg")) {
	    continue;
	}

	if (Tcl_GetIndexFromObjStruct(interp, objv[0], fmtOptions,
		sizeof(char *), "option", 0, &optIndex) == TCL_ERROR) {
	    goto error;
	}

	if (objc < 2) {
	    ckfree(inputCopy);
	    Tcl_WrongNumArgs(interp, 1, objv, "value");
	    goto error;
	}

	objc--;
	objv++;

	switch ((enum fmtOptions) optIndex) {
	case OPT_DPI:
	    if (Tcl_GetDoubleFromObj(interp, objv[0], &dpi) == TCL_ERROR) {
	        goto error;
	    }
	    if (dpi < 0.0) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(
			"-dpi value must be positive", -1));
		Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "BAD_DPI",
			NULL);
		goto error;
	    }
	    break;
	case OPT_UNIT:
	    p = Tcl_GetString(objv[0]);
	    if ((p != NULL) && (p[0])) {
	        strncpy(unit, p, 3);
		unit[2] = '\0';
	    }
	    break;
	}
    }

    nsvgImage = nsvgParse(inputCopy, unit, dpi);
    if (nsvgImage == NULL) {
        Tcl_SetResult(interp, "cannot parse SVG image", TCL_STATIC);
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "PARSE_ERROR", NULL);
	goto error;
    }
    ckfree(inputCopy);
    return nsvgImage;

error:
    if (inputCopy != NULL) {
        ckfree(inputCopy);
    }
    return NULL;
}

static int
RasterizeSVG(
    Tcl_Interp *interp,
    Tk_PhotoHandle imageHandle,
    NSVGimage *nsvgImage,
    int destX, int destY,
    int width, int height,
    int srcX, int srcY)
{   
    int w, h, c;
    NSVGrasterizer *rast;
    unsigned char *imgData;
    Tk_PhotoImageBlock svgblock;

    w = nsvgImage->width;

    h = nsvgImage->height; 
    rast = nsvgCreateRasterizer();

    if (rast == NULL) {
	Tcl_SetResult(interp, "cannot initialize rasterizer", TCL_STATIC);
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "RASTERIZER_ERROR",
		NULL);
	goto cleanAST;
    }


    imgData = attemptckalloc(w * h *4);
    if (imgData == NULL) {
	Tcl_SetResult(interp, "cannot alloc image buffer", TCL_STATIC);
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "OUT_OF_MEMORY", NULL);
	goto cleanRAST;
    }

    nsvgRasterize(rast, nsvgImage, 0, 0, 1, imgData, w, h, w * 4);

    /* transfer the data to a photo block */

    svgblock.pixelPtr = imgData;
    svgblock.width = w;
    svgblock.height = h;
    svgblock.pitch = w * 4;
    svgblock.pixelSize = 4;
    for (c = 0; c <= 3; c++) {
	svgblock.offset[c] = c;
    }

    if (Tk_PhotoExpand(interp, imageHandle,
		destX + width, destY + height) != TCL_OK) {
	goto cleanRAST;
    }

    if (Tk_PhotoPutBlock(interp, imageHandle, &svgblock, destX, destY,
		width, height, TK_PHOTO_COMPOSITE_SET) != TCL_OK) {
	goto cleanimg;
    }


    ckfree(imgData);
    nsvgDeleteRasterizer(rast);
    nsvgDelete(nsvgImage);
    return TCL_OK;

cleanimg:
    ckfree(imgData);
    
cleanRAST:
    nsvgDeleteRasterizer(rast);

cleanAST:
    nsvgDelete(nsvgImage);
    return TCL_ERROR;
}

static int
CacheSVG(
    Tcl_Interp *interp,
    ClientData dataOrChan,
    Tcl_Obj *formatObj,
    NSVGimage *nsvgImage)
{

    int length;
    const char *data;
    NSVGcache *cachePtr = Tcl_GetAssocData(interp, "tksvg", NULL);



    if (cachePtr != NULL) {
        cachePtr->dataOrChan = dataOrChan;
	if (formatObj != NULL) {
	    data = Tcl_GetStringFromObj(formatObj, &length);
	    Tcl_DStringAppend(&cachePtr->formatString, data, length);
	}
	cachePtr->nsvgImage = nsvgImage;
	return 1;
    }
    return 0;
}

static NSVGimage *
GetCachedSVG(
    Tcl_Interp *interp,
    ClientData dataOrChan,
    Tcl_Obj *formatObj)
{
    int length;
    const char *data;
    NSVGcache *cachePtr = Tcl_GetAssocData(interp, "tksvg", NULL);
    NSVGimage *nsvgImage = NULL;



    if ((cachePtr != NULL) && (cachePtr->nsvgImage != NULL) &&
	(cachePtr->dataOrChan == dataOrChan)) {
        if (formatObj != NULL) {
	    data = Tcl_GetStringFromObj(formatObj, &length);
	    if (strcmp(data, Tcl_DStringValue(&cachePtr->formatString)) == 0) {
	        nsvgImage = cachePtr->nsvgImage;
		cachePtr->nsvgImage = NULL;
	    }



	} else if (Tcl_DStringLength(&cachePtr->formatString) == 0) {
	    nsvgImage = cachePtr->nsvgImage;
	    cachePtr->nsvgImage = NULL;
	}
    }
    CleanCache(interp);
    return nsvgImage;
}

static void
CleanCache(Tcl_Interp *interp)
{
    NSVGcache *cachePtr = Tcl_GetAssocData(interp, "tksvg", NULL);

    if (cachePtr != NULL) {
        cachePtr->dataOrChan = NULL;
        Tcl_DStringSetLength(&cachePtr->formatString, 0);
	if (cachePtr->nsvgImage != NULL) {
	    nsvgDelete(cachePtr->nsvgImage);
	    cachePtr->nsvgImage = NULL;
	}
    }
}

static void
FreeCache(ClientData clientData, Tcl_Interp *interp)
{
    NSVGcache *cachePtr = clientData;

    Tcl_DStringFree(&cachePtr->formatString);
    if (cachePtr->nsvgImage != NULL) {
        nsvgDelete(cachePtr->nsvgImage);
    }
    ckfree(cachePtr);
}

int
Tksvg_Init(Tcl_Interp *interp)
{
    NSVGcache *cachePtr;

    if (interp == NULL) {
        return TCL_ERROR;
    }
#ifdef USE_TCL_STUBS
    if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
	return TCL_ERROR;
    }
#else
    if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 0) == NULL) {
	return TCL_ERROR;
    }
#endif
#ifdef USE_TK_STUBS
    if (Tk_InitStubs(interp, TCL_VERSION, 0) == NULL) {
	return TCL_ERROR;
    }
#else
    if (Tcl_PkgRequire(interp, "Tk", TK_VERSION, 0) == NULL) {
	return TCL_ERROR;
    }
#endif
    cachePtr = ckalloc(sizeof(NSVGcache));
    cachePtr->dataOrChan = NULL;
    Tcl_DStringInit(&cachePtr->formatString);
    cachePtr->nsvgImage = NULL;
    Tcl_SetAssocData(interp, "tksvg", FreeCache, cachePtr);
    Tk_CreatePhotoImageFormat(&tkImgFmtSVG);

    Tcl_PkgProvide(interp, PACKAGE_NAME, PACKAGE_VERSION);

    return TCL_OK;
}