示例#1
1
文件: tkImgSVGnano.c 项目: tcltk/tk
static int
StringMatchSVG(
    Tcl_Obj *dataObj,
    Tcl_Obj *formatObj,
    int *widthPtr, int *heightPtr,
    Tcl_Interp *interp)
{
    int length;
    const char *data;
    RastOpts ropts;
    NSVGimage *nsvgImage;

    CleanCache(interp);
    data = Tcl_GetStringFromObj(dataObj, &length);
    nsvgImage = ParseSVGWithOptions(interp, data, length, formatObj, &ropts);
    if (nsvgImage != NULL) {
	*widthPtr = (int) ceil(nsvgImage->width * ropts.scale);
	*heightPtr = (int) ceil(nsvgImage->height * ropts.scale);
        if ((*widthPtr <= 0) || (*heightPtr <= 0)) {
            nsvgDelete(nsvgImage);
            return 0;
        }
	if (!CacheSVG(interp, dataObj, formatObj, nsvgImage, &ropts)) {
	    nsvgDelete(nsvgImage);
	}
	return 1;
    }
    return 0;
}
示例#2
0
文件: svg.c 项目: Ever-Never/libaroma
LIBAROMA_CANVASP libaroma_svg_ex(
    LIBAROMA_STREAMP stream,
    byte freeStream,
    byte use_px) {

  LIBAROMA_CANVASP cv = NULL;
  if (!stream) {
    return NULL;
  }
  
  char * data = libaroma_stream_to_string(stream,0);
  if (data){
    NSVGimage *image = NULL;
    if (!use_px){
      image=nsvgParse(data, "dp", ((float) libaroma_fb()->dpi));
    }
    else{
      image=nsvgParse(data, "px", ((float) libaroma_fb()->dpi));
    }
    free(data);
    if (image == NULL) {
  		ALOGW("libaroma_svg: Could not open SVG image.");
  		goto exit;
  	}
  	
  	
  	NSVGrasterizer *rast =nsvgCreateRasterizer();
  	if (rast == NULL) {
  		printf("libaroma_svg: Could not init rasterizer.");
  		nsvgDelete(image);
  		goto exit;
  	}
  	if (!use_px){
  	  cv = libaroma_canvas_ex(libaroma_dp(image->width),libaroma_dp(image->height),1);
  	}
  	else{
  	  cv = libaroma_canvas_ex(image->width,image->height,1);
  	}
  	libaroma_canvas_setcolor(cv,0,0);
  	nsvgRasterize(rast,image,0,0,1,cv);
  	nsvgDelete(image);
  	nsvgDeleteRasterizer(rast);
  }
  
exit:
  if (freeStream) {
    libaroma_stream_close(stream);
  }
  return cv;
}
示例#3
0
/* Load a SVG type image from an SDL datasource */
SDL_Surface *IMG_LoadSVG_RW(SDL_RWops *src)
{
    char *data;
    struct NSVGimage *image;
    struct NSVGrasterizer *rasterizer;
    SDL_Surface *surface = NULL;
    float scale = 1.0f;

    data = (char *)SDL_LoadFile_RW(src, NULL, SDL_FALSE);
    if ( !data ) {
        return NULL;
    }

    /* For now just use default units of pixels at 96 DPI */
    image = nsvgParse(data, "px", 96.0f);
    SDL_free(data);
    if ( !image ) {
        IMG_SetError("Couldn't parse SVG image");
        return NULL;
    }

    rasterizer = nsvgCreateRasterizer();
    if ( !rasterizer ) {
        IMG_SetError("Couldn't create SVG rasterizer");
        nsvgDelete( image );
        return NULL;
    }

    surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
                                   (int)(image->width * scale),
                                   (int)(image->height * scale),
                                   32,
                                   0x000000FF,
                                   0x0000FF00,
                                   0x00FF0000,
                                   0xFF000000);
    if ( !surface ) {
        nsvgDeleteRasterizer( rasterizer );
        nsvgDelete( image );
        return NULL;
    }

    nsvgRasterize(rasterizer, image, 0.0f, 0.0f, scale, (unsigned char *)surface->pixels, surface->w, surface->h, surface->pitch);
    nsvgDeleteRasterizer( rasterizer );
    nsvgDelete( image );

    return surface;
}
示例#4
0
Error ImageLoaderSVG::_create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale, bool upsample, bool convert_colors) {
	NSVGimage *svg_image;
	PoolVector<uint8_t>::Read src_r = p_data->read();
	svg_image = nsvgParse((char *)src_r.ptr(), "px", 96);
	if (svg_image == NULL) {
		ERR_PRINT("SVG Corrupted");
		return ERR_FILE_CORRUPT;
	}
	if (convert_colors)
		_convert_colors(svg_image);

	float upscale = upsample ? 2.0 : 1.0;

	int w = (int)(svg_image->width * p_scale * upscale);
	int h = (int)(svg_image->height * p_scale * upscale);

	PoolVector<uint8_t> dst_image;
	dst_image.resize(w * h * 4);

	PoolVector<uint8_t>::Write dw = dst_image.write();

	rasterizer.rasterize(svg_image, 0, 0, p_scale * upscale, (unsigned char *)dw.ptr(), w, h, w * 4);

	dw = PoolVector<uint8_t>::Write();
	p_image->create(w, h, false, Image::FORMAT_RGBA8, dst_image);
	if (upsample)
		p_image->shrink_x2();

	nsvgDelete(svg_image);

	return OK;
}
示例#5
0
文件: export.c 项目: mehdidc/nanosvg
int main(int argc, char **argv){
	NSVGimage* image;
        int dpi;
        if(argc <= 1){
            printf("Usage : filename.svg dpi=96\n");
            return 0;
        }
        if(argc <= 2){
            dpi = 96;
        }
        else{
            dpi = atoi(argv[2]);
        }
	image = nsvgParseFromFile(argv[1], "px", dpi );

	printf("size: %f x %f\n", image->width, image->height);
	// Use...
        NSVGshape *shape;
        NSVGpath *path;
        int i;
	for (shape = image->shapes; shape != NULL; shape = shape->next) {
		for (path = shape->paths; path != NULL; path = path->next) {
			for (i = 0; i < path->npts-1; i += 3) {
				float* p = &path->pts[i*2];
                                printf("%.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f\n", p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
				//drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);

			}
		}
	}
	// Delete
	nsvgDelete(image);
}
示例#6
0
文件: tkImgSVGnano.c 项目: tcltk/tk
static void
FreeCache(ClientData clientData, Tcl_Interp *interp)
{
    NSVGcache *cachePtr = clientData;

    Tcl_DStringFree(&cachePtr->formatString);
    if (cachePtr->nsvgImage != NULL) {
        nsvgDelete(cachePtr->nsvgImage);
    }
    ckfree(cachePtr);
}
示例#7
0
文件: tkImgSVGnano.c 项目: tcltk/tk
static void
CleanCache(Tcl_Interp *interp)
{
    NSVGcache *cachePtr = GetCachePtr(interp);

    if (cachePtr != NULL) {
        cachePtr->dataOrChan = NULL;
        Tcl_DStringSetLength(&cachePtr->formatString, 0);
	if (cachePtr->nsvgImage != NULL) {
	    nsvgDelete(cachePtr->nsvgImage);
	    cachePtr->nsvgImage = NULL;
	}
    }
}
示例#8
0
    VectorTexture::VectorTexture(std::string const * pGraphic, Filtering filtering, Wrap wrap)
    {
        // Parse graphics
        char* str = static_cast<char*>(malloc(sizeof(char) * pGraphic->size() + 1));
        strcpy(str, pGraphic->data());
        NSVGimage* svg = nsvgParse(str, "px", SVG_DPI);
        free(str);

        // Rasterize it and create OpenGL texture
        rasterizeGraphics(svg, filtering, wrap, "");

        // Delete graphics
        nsvgDelete(svg);
    }
示例#9
0
文件: tkImgSVGnano.c 项目: tcltk/tk
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;
    RastOpts ropts;
    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, &ropts);
    Tcl_DecrRefCount(dataObj);
    if (nsvgImage != NULL) {
	*widthPtr = (int) ceil(nsvgImage->width * ropts.scale);
	*heightPtr = (int) ceil(nsvgImage->height * ropts.scale);
        if ((*widthPtr <= 0) || (*heightPtr <= 0)) {
            nsvgDelete(nsvgImage);
            return 0;
        }
	if (!CacheSVG(interp, chan, formatObj, nsvgImage, &ropts)) {
	    nsvgDelete(nsvgImage);
	}
	return 1;
    }
    return 0;
}
示例#10
0
文件: PiGui.cpp 项目: shawnco/pioneer
ImTextureID PiGui::RenderSVG(std::string svgFilename, int width, int height) {
	Output("nanosvg: %s %dx%d\n", svgFilename.c_str(), width, height);
	NSVGimage *image = NULL;
	NSVGrasterizer *rast = NULL;
	unsigned char* img = NULL;
	int w, h;
	// size of each icon
	//	int size = 64;
	// 16 columns
	//	int W = 16*size;
	int W = width;
	// 16 rows
	//	int H = 16*size;
	int H = height;
	img = static_cast<unsigned char*>(malloc(W*H*4));
	memset(img, 0, W * H * 4);
	std::string filename = svgFilename; // FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "icons"), "icons.svg");
	image = nsvgParseFromFile(filename.c_str(), "px", 96.0f);
	if (image == NULL) {
		Error("Could not open SVG image.\n");
	}
	w = static_cast<int>(image->width);
	h = static_cast<int>(image->height);

	rast = nsvgCreateRasterizer();
	if (rast == NULL) {
		Error("Could not init rasterizer.\n");
	}

	if (img == NULL) {
		Error("Could not alloc image buffer.\n");
	}
	{
		float scale = double(W)/w;
		float tx = 0;
		float ty = 0;
		nsvgRasterize(rast, image, tx, ty, scale, img, W, H, W*4);
	}
	nsvgDeleteRasterizer(rast);
	nsvgDelete(image);
	return makeTexture(img, W, H);
}
示例#11
0
int main()
{
	GLFWwindow* window;
	const GLFWvidmode* mode;

	if (!glfwInit())
		return -1;

	mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    window = glfwCreateWindow(mode->width - 40, mode->height - 80, "Nano SVG", NULL, NULL);
	if (!window)
	{
		printf("Could not open window\n");
		glfwTerminate();
		return -1;
	}

	glfwSetFramebufferSizeCallback(window, resizecb);
	glfwMakeContextCurrent(window);
	glEnable(GL_POINT_SMOOTH);
	glEnable(GL_LINE_SMOOTH);


	g_image = nsvgParseFromFile("../example/nano.svg", "px", 96.0f);
	if (g_image == NULL) {
		printf("Could not open SVG image.\n");
		glfwTerminate();
		return -1;
	}

	while (!glfwWindowShouldClose(window))
	{
		drawframe(window);
		glfwPollEvents();
	}

	nsvgDelete(g_image);

	glfwTerminate();
	return 0;
}
示例#12
0
int main()
{
	struct NSVGimage *image = NULL;
	struct NSVGrasterizer *rast = NULL;
	unsigned char* img = NULL;
	int w, h;
	const char* filename = "../example/23.svg";

	printf("parsing %s\n", filename);
	image = nsvgParseFromFile(filename, "px", 96.0f);
	if (image == NULL) {
		printf("Could not open SVG image.\n");
		goto error;
	}
	w = image->width;
	h = image->height;

	rast = nsvgCreateRasterizer();
	if (rast == NULL) {
		printf("Could not init rasterizer.\n");
		goto error;
	}

	img = malloc(w*h*4);
	if (img == NULL) {
		printf("Could not alloc image buffer.\n");
		goto error;
	}

	printf("rasterizing image %d x %d\n", w, h);
	nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);

	printf("writing svg.png\n");
 	stbi_write_png("svg.png", w, h, 4, img, w*4);

error:
	nsvgDeleteRasterizer(rast);
	nsvgDelete(image);

	return 0;
}
示例#13
0
    VectorTexture::VectorTexture(std::string filepath, Filtering filtering, Wrap wrap) : Texture()
    {
        // Check file format
        if (!checkFileNameExtension(filepath, "svg"))
        {
            throwError(OperationNotifier::Operation::IMAGE_LOADING, "Graphics file not found or wrong format", filepath);
        }

        // Parse file
        NSVGimage* svg = nsvgParseFromFile(buildPath(filepath).c_str(), "px", SVG_DPI);

        // Check whether file found and parsed
        if (svg == NULL)
        {
            throwError(OperationNotifier::Operation::IMAGE_LOADING, "Graphics file not found or error while parsing", filepath);
        }

        // Rasterize it and create OpenGL texture
        rasterizeGraphics(svg, filtering, wrap, filepath);

        // Delete graphics
        nsvgDelete(svg);
    }
示例#14
0
文件: vtests.cpp 项目: cpizano/Plex
plx::ComPtr<ID2D1PathGeometry> RealizeSVG(
    const char* file, const plx::DPI& dpi, plx::ComPtr<ID2D1Factory2> dd_factory) {

  auto image = nsvgParseFromFile(file, "px", static_cast<float>(dpi.get_dpi_x()));
  if (!image)
    return nullptr;

  plx::ComPtr<ID2D1PathGeometry> geom;
  auto hr = dd_factory->CreatePathGeometry(geom.GetAddressOf());
  if (hr != S_OK)
    throw plx::ComException(__LINE__, hr);

  plx::ComPtr<ID2D1GeometrySink> sink;
  hr = geom->Open(sink.GetAddressOf());
  if (hr != S_OK)
    throw plx::ComException(__LINE__, hr);

  for (auto shape = image->shapes; shape != nullptr; shape = shape->next) {
    for (auto path = shape->paths; path != nullptr; path = path->next) {
      sink->BeginFigure(
          D2D1::Point2F(path->pts[0], path->pts[1]), D2D1_FIGURE_BEGIN_FILLED);
      for (auto i = 0; i < path->npts - 1; i += 3) {
        float* p = &path->pts[i*2];
        sink->AddBezier(
            D2D1::BezierSegment(D2D1::Point2F(p[2], p[3]),
                                D2D1::Point2F(p[4], p[5]),
                                D2D1::Point2F(p[6], p[7])));
      }
      sink->EndFigure(
          (path->closed == 1) ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN);
    }
  }

  sink->Close();
  nsvgDelete(image);
  return geom;
}
示例#15
0
文件: tkImgSVGnano.c 项目: tcltk/tk
static int
RasterizeSVG(
    Tcl_Interp *interp,
    Tk_PhotoHandle imageHandle,
    NSVGimage *nsvgImage,
    int destX, int destY,
    int width, int height,
    int srcX, int srcY,
    RastOpts *ropts)
{
    int w, h, c;
    NSVGrasterizer *rast;
    unsigned char *imgData;
    Tk_PhotoImageBlock svgblock;

    w = (int) ceil(nsvgImage->width * ropts->scale);
    h = (int) ceil(nsvgImage->height * ropts->scale);
    rast = nsvgCreateRasterizer();
    if (rast == NULL) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj("cannot initialize rasterizer", -1));
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "RASTERIZER_ERROR",
		NULL);
	goto cleanAST;
    }
    imgData = attemptckalloc(w * h *4);
    if (imgData == NULL) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj("cannot alloc image buffer", -1));
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "OUT_OF_MEMORY", NULL);
	goto cleanRAST;
    }
    nsvgRasterize(rast, nsvgImage, (float) ropts->x, (float) ropts->y,
	    (float) ropts->scale, 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;
}