Tk Source Code

Check-in [c111a3ea]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

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

Overview
Comment:Remove wrong forcing of image size to 1x1 when it was read from the data or disk to be 0x0 (which indicates the image could not be parsed in that format). Handling for images 0x0 in size must be kept because Tk bases its error detection for the format on this feature.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tip-545-svg-options
Files: files | file ages | folders
SHA3-256: c111a3ea33248b29e2540e895e6753af2723d0e7dd2b0195787689bf94fac2aa
User & Date: fvogel 2019-06-12 22:23:54
Context
2019-06-14
16:46
Tests for svg file check-in: 8809f721 user: oehhar tags: tip-545-svg-options
2019-06-12
22:23
Remove wrong forcing of image size to 1x1 when it was read from the data or disk to be 0x0 (which indicates the image could not be parsed in that format). Handling for images 0x0 in size must be kept because Tk bases its error detection for the format on this feature. check-in: c111a3ea user: fvogel tags: tip-545-svg-options
2019-06-10
12:22
Fix code style a bit, and comments check-in: ebc9fe82 user: fvogel tags: tip-545-svg-options
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tkImgSVGnano.c.

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
...
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
...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
...
602
603
604
605
606
607
608
609

610
611
612

613
614
615
616
617
618
619
620
...
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
	/* in case of an error reading the file */
	Tcl_DecrRefCount(dataObj);
	return 0;
    }
    data = Tcl_GetStringFromObj(dataObj, &length);
    nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj, &ropts);
    Tcl_DecrRefCount(dataObj);
    if (nsvgImage == NULL) {
	return 0;
    }
    /*
     * Width and Height equal zero is an svgnano error case and must be errored
     * out. Valid png images give width and height 0 as result
     */
    if ((nsvgImage->width <= 0.0) || (nsvgImage->height <= 0.0)) {

	nsvgDelete(nsvgImage);
	return 0;
    }
    GetScaleFromParameters(
	nsvgImage,
	&ropts,
	widthPtr,
	heightPtr);
    if (!CacheSVG(interp, chan, formatObj, nsvgImage, &ropts)) {
	nsvgDelete(nsvgImage);
    }
    return 1;


}

/*
 *----------------------------------------------------------------------
 *
 * FileReadSVG --
 *
................................................................................
    const char *data;
    RastOpts ropts;
    NSVGimage *nsvgImage;

    CleanCache(interp);
    data = Tcl_GetStringFromObj(dataObj, &length);
    nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj, &ropts);
    if (nsvgImage == NULL) {
	return 0;
    }
    /*
     * Width and Height equal zero is an svgnano error case and must be errored
     * out. Valid png images give width and height 0 as result
     */
    if ((nsvgImage->width <= 0.0) || (nsvgImage->height <= 0.0)) {

	nsvgDelete(nsvgImage);
	return 0;
    }
    GetScaleFromParameters(nsvgImage, &ropts, widthPtr, heightPtr);
    if (!CacheSVG(interp, dataObj, formatObj, nsvgImage, &ropts)) {
	nsvgDelete(nsvgImage);
    }
    return 1;


}

/*
 *----------------------------------------------------------------------
 *
 * StringReadSVG --
 *
................................................................................
 *
 * GetScaleFromParameters --
 *
 *	Get the scale value from the already parsed parameters -scale,
 *	-scaletoheight and -scaletowidth.
 *
 *	The image width and height is also returned.
 *	Both are greater than or equal to 1.
 *
 * Results:
 *	The evaluated or configured scale value, or 0.0 on failure
 *
 * Side effects:
 *	heightPtr and widthPtr are set to height and width of the image.
 *
................................................................................
    NSVGimage *nsvgImage,
    RastOpts *ropts,
    int *widthPtr,
    int *heightPtr)
{
    double scale;
    int width, height;
    /*

     * nsvgImage->width  and nsvgImage->height are greater than 0.
     * Equal to 0 is an svgnano error case.
     */

    if (ropts->scaleToHeight > 0) {
	/*
	 * Fixed height
	 */
	height = ropts->scaleToHeight;
	scale = height / nsvgImage->height;
	width = (int) ceil(nsvgImage->width * scale);
    } else if (ropts->scaleToWidth > 0) {
................................................................................
	/*
	 * Scale factor
	 */
	scale = ropts->scale;
	width = (int) ceil(nsvgImage->width * scale);
	height = (int) ceil(nsvgImage->height * scale);
    }
    /*
     * Set width or height to minimum 1 pixel.
     * This is the minimum.
     * It is more sensible to scale to the minimum than output an
     * error on very small scales.
     */
    if (width <= 0) {
	width = 1;
    }
    if (height <= 0) {
	height = 1;
    }
    /*
     * Output the found values
     */
    *heightPtr = height;
    *widthPtr = width;
    return scale;
}

/*
 *----------------------------------------------------------------------






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







 







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







 







<







 







<
>
|
|
<
>
|







 







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







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
...
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
...
570
571
572
573
574
575
576

577
578
579
580
581
582
583
...
589
590
591
592
593
594
595

596
597
598

599
600
601
602
603
604
605
606
607
...
615
616
617
618
619
620
621








622






623
624
625
626
627
628
629
	/* in case of an error reading the file */
	Tcl_DecrRefCount(dataObj);
	return 0;
    }
    data = Tcl_GetStringFromObj(dataObj, &length);
    nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj, &ropts);
    Tcl_DecrRefCount(dataObj);
    if (nsvgImage != NULL) {
        GetScaleFromParameters(nsvgImage, &ropts, widthPtr, heightPtr);






        if ((*widthPtr <= 0.0) || (*heightPtr <= 0.0)) {
	    nsvgDelete(nsvgImage);
	    return 0;
        }





        if (!CacheSVG(interp, chan, formatObj, nsvgImage, &ropts)) {
	    nsvgDelete(nsvgImage);
        }
        return 1;
    }
    return 0;
}

/*
 *----------------------------------------------------------------------
 *
 * FileReadSVG --
 *
................................................................................
    const char *data;
    RastOpts ropts;
    NSVGimage *nsvgImage;

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






        if ((*widthPtr <= 0.0) || (*heightPtr <= 0.0)) {
	    nsvgDelete(nsvgImage);
	    return 0;
        }

        if (!CacheSVG(interp, dataObj, formatObj, nsvgImage, &ropts)) {
	    nsvgDelete(nsvgImage);
        }
        return 1;
    }
    return 0;
}

/*
 *----------------------------------------------------------------------
 *
 * StringReadSVG --
 *
................................................................................
 *
 * GetScaleFromParameters --
 *
 *	Get the scale value from the already parsed parameters -scale,
 *	-scaletoheight and -scaletowidth.
 *
 *	The image width and height is also returned.

 *
 * Results:
 *	The evaluated or configured scale value, or 0.0 on failure
 *
 * Side effects:
 *	heightPtr and widthPtr are set to height and width of the image.
 *
................................................................................
    NSVGimage *nsvgImage,
    RastOpts *ropts,
    int *widthPtr,
    int *heightPtr)
{
    double scale;
    int width, height;


    if ((nsvgImage->width == 0.0) || (nsvgImage->height == 0.0)) {
        width = height = 0;

        scale = 1.0;
    } else if (ropts->scaleToHeight > 0) {
	/*
	 * Fixed height
	 */
	height = ropts->scaleToHeight;
	scale = height / nsvgImage->height;
	width = (int) ceil(nsvgImage->width * scale);
    } else if (ropts->scaleToWidth > 0) {
................................................................................
	/*
	 * Scale factor
	 */
	scale = ropts->scale;
	width = (int) ceil(nsvgImage->width * scale);
	height = (int) ceil(nsvgImage->height * scale);
    }















    *heightPtr = height;
    *widthPtr = width;
    return scale;
}

/*
 *----------------------------------------------------------------------