IMG_svg.c
author Ozkan Sezer <sezeroz@gmail.com>
Thu, 18 Oct 2018 11:57:19 +0300
changeset 612 ca95d0e31aec
parent 584 8fee51506499
child 638 e3e9d7430674
permissions -rw-r--r--
use less ancient versions of autofoo scripts
slouken@524
     1
/*
slouken@524
     2
  SDL_image:  An example image loading library for use with SDL
slouken@575
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@524
     4
slouken@524
     5
  This software is provided 'as-is', without any express or implied
slouken@524
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@524
     7
  arising from the use of this software.
slouken@524
     8
slouken@524
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@524
    10
  including commercial applications, and to alter it and redistribute it
slouken@524
    11
  freely, subject to the following restrictions:
slouken@524
    12
slouken@524
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@524
    14
     claim that you wrote the original software. If you use this software
slouken@524
    15
     in a product, an acknowledgment in the product documentation would be
slouken@524
    16
     appreciated but is not required.
slouken@524
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@524
    18
     misrepresented as being the original software.
slouken@524
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@524
    20
*/
slouken@524
    21
slouken@524
    22
/* This is an SVG image file loading framework, based on Nano SVG:
slouken@524
    23
 * https://github.com/memononen/nanosvg
slouken@524
    24
 */
slouken@524
    25
slouken@524
    26
#include "SDL_image.h"
slouken@524
    27
slouken@524
    28
#ifdef LOAD_SVG
slouken@524
    29
slouken@524
    30
/* Replace C runtime functions with SDL C runtime functions for building on Windows */
slouken@545
    31
#define acosf   SDL_acosf
slouken@545
    32
#define atan2f  SDL_atan2f
slouken@545
    33
#define cosf    SDL_cosf
slouken@573
    34
#define ceilf   SDL_ceilf
slouken@566
    35
#define fabs    SDL_fabs
slouken@565
    36
#define fabsf   SDL_fabsf
slouken@573
    37
#define floorf  SDL_floorf
slouken@545
    38
#define fmodf   SDL_fmodf
slouken@545
    39
#define free    SDL_free
slouken@545
    40
#define malloc  SDL_malloc
slouken@545
    41
#undef memcpy
slouken@545
    42
#define memcpy  SDL_memcpy
slouken@545
    43
#undef memset
slouken@545
    44
#define memset  SDL_memset
slouken@545
    45
#define pow     SDL_pow
slouken@545
    46
#define qsort   SDL_qsort
slouken@545
    47
#define realloc SDL_realloc
slouken@545
    48
#define sinf    SDL_sinf
slouken@545
    49
#define sqrt    SDL_sqrt
slouken@545
    50
#define sqrtf   SDL_sqrtf
slouken@545
    51
#define sscanf  SDL_sscanf
sezeroz@584
    52
#undef strchr
slouken@545
    53
#define strchr  SDL_strchr
sezeroz@584
    54
#undef strcmp
slouken@545
    55
#define strcmp  SDL_strcmp
sezeroz@584
    56
#undef strncmp
slouken@545
    57
#define strncmp SDL_strncmp
slouken@545
    58
#undef strncpy
slouken@545
    59
#define strncpy SDL_strlcpy
slouken@545
    60
#define strlen  SDL_strlen
slouken@545
    61
#define strstr  SDL_strstr
slouken@545
    62
#define strtol  SDL_strtol
slouken@537
    63
#define strtoll SDL_strtoll
slouken@545
    64
#define tanf    SDL_tanf
slouken@528
    65
#ifndef FLT_MAX
slouken@528
    66
#define FLT_MAX     3.402823466e+38F
slouken@528
    67
#endif
slouken@545
    68
#undef HAVE_STDIO_H
slouken@524
    69
slouken@524
    70
#define NANOSVG_IMPLEMENTATION
slouken@524
    71
#include "nanosvg.h"
slouken@524
    72
#define NANOSVGRAST_IMPLEMENTATION
slouken@524
    73
#include "nanosvgrast.h"
slouken@524
    74
slouken@524
    75
/* See if an image is contained in a data source */
slouken@524
    76
int IMG_isSVG(SDL_RWops *src)
slouken@524
    77
{
slouken@524
    78
    Sint64 start;
slouken@524
    79
    int is_SVG;
slouken@524
    80
    char magic[4096];
slouken@524
    81
    size_t magic_len;
slouken@524
    82
slouken@524
    83
    if ( !src )
slouken@524
    84
        return 0;
slouken@524
    85
    start = SDL_RWtell(src);
slouken@524
    86
    is_SVG = 0;
slouken@524
    87
    magic_len = SDL_RWread(src, magic, 1, sizeof(magic) - 1);
slouken@524
    88
    magic[magic_len] = '\0';
slouken@524
    89
    if ( SDL_strstr(magic, "<svg") ) {
slouken@524
    90
        is_SVG = 1;
slouken@524
    91
    }
slouken@524
    92
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@524
    93
    return(is_SVG);
slouken@524
    94
}
slouken@524
    95
slouken@524
    96
/* Load a SVG type image from an SDL datasource */
slouken@524
    97
SDL_Surface *IMG_LoadSVG_RW(SDL_RWops *src)
slouken@524
    98
{
slouken@524
    99
    char *data;
slouken@524
   100
    struct NSVGimage *image;
slouken@524
   101
    struct NSVGrasterizer *rasterizer;
slouken@524
   102
    SDL_Surface *surface = NULL;
slouken@526
   103
    float scale = 1.0f;
slouken@524
   104
slouken@524
   105
    data = (char *)SDL_LoadFile_RW(src, NULL, SDL_FALSE);
slouken@524
   106
    if ( !data ) {
slouken@524
   107
        return NULL;
slouken@524
   108
    }
slouken@524
   109
slouken@524
   110
    /* For now just use default units of pixels at 96 DPI */
slouken@524
   111
    image = nsvgParse(data, "px", 96.0f);
slouken@524
   112
    SDL_free(data);
slouken@524
   113
    if ( !image ) {
slouken@524
   114
        IMG_SetError("Couldn't parse SVG image");
slouken@524
   115
        return NULL;
slouken@524
   116
    }
slouken@524
   117
slouken@524
   118
    rasterizer = nsvgCreateRasterizer();
slouken@524
   119
    if ( !rasterizer ) {
slouken@524
   120
        IMG_SetError("Couldn't create SVG rasterizer");
slouken@524
   121
        nsvgDelete( image );
slouken@524
   122
        return NULL;
slouken@524
   123
    }
slouken@524
   124
slouken@524
   125
    surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@524
   126
                                   (int)(image->width * scale),
slouken@524
   127
                                   (int)(image->height * scale),
slouken@524
   128
                                   32,
slouken@524
   129
                                   0x000000FF,
slouken@524
   130
                                   0x0000FF00,
slouken@524
   131
                                   0x00FF0000,
slouken@524
   132
                                   0xFF000000);
slouken@524
   133
    if ( !surface ) {
slouken@524
   134
        nsvgDeleteRasterizer( rasterizer );
slouken@524
   135
        nsvgDelete( image );
slouken@524
   136
        return NULL;
slouken@524
   137
    }
slouken@524
   138
slouken@524
   139
    nsvgRasterize(rasterizer, image, 0.0f, 0.0f, scale, (unsigned char *)surface->pixels, surface->w, surface->h, surface->pitch);
slouken@524
   140
    nsvgDeleteRasterizer( rasterizer );
slouken@524
   141
    nsvgDelete( image );
slouken@524
   142
slouken@524
   143
    return surface;
slouken@524
   144
}
slouken@524
   145
slouken@524
   146
#else
slouken@524
   147
slouken@524
   148
/* See if an image is contained in a data source */
slouken@524
   149
int IMG_isSVG(SDL_RWops *src)
slouken@524
   150
{
slouken@524
   151
    return(0);
slouken@524
   152
}
slouken@524
   153
slouken@524
   154
/* Load a SVG type image from an SDL datasource */
slouken@524
   155
SDL_Surface *IMG_LoadSVG_RW(SDL_RWops *src)
slouken@524
   156
{
slouken@524
   157
    return(NULL);
slouken@524
   158
}
slouken@524
   159
slouken@524
   160
#endif /* LOAD_SVG */