' ' This is an SVG image loader plugin for the CANVAS context. It uses the NANOSVG libraries. PvE - MIT License 2015. ' ' Updated to use textures instead of the deprecated glDrawPixels - October 2016 - PvE - MIT License 2016. ' ' Web address: https://github.com/memononen/nanosvg ' ' NANOSVG_IMAGE_FILE(file$, xpos, ypos, flag) ' file$: name of file to load ' xpos: x position of the centre of the picture on the canvas ' ypos: y position of the centre of the pictureon the canvas ' flag: 0=no rasterize (setup color with INK), 1=rasterize ' ' NANOSVG_IMAGE_PTR(txt$, xpos, ypos, flag) ' file$: string with SVG definition ' xpos: x position of the centre of the picture on the canvas ' ypos: y position of the centre of the pictureon the canvas ' flag: 0=no rasterize (setup color with INK), 1=rasterize ' ' NANOSVG_IMAGE_INFO(file$) ---> print information on console about picture ' file$: name of file ' ' NANOSVG_IMAGE_WIDTH(file$) ' file$: name of file ' ' NANOSVG_IMAGE_HEIGHT(file$) ' file$: name of file ' '----------------------------------------------------------------------------------------------------------------------------------- ' ' The next lines needs a correct location to the 'nanosvg.h' and 'nanosvgrast.h' file. PRAGMA INCLUDE nanosvg-master/src/nanosvg.h : ' Include the nanosvg file loader header file PRAGMA INCLUDE nanosvg-master/src/nanosvgrast.h : ' Include the nanosvg rasterizer header file '----------------------------------------------------------------------------------------------------------------------------------- PRAGMA OPTIONS -DNANOSVG_IMPLEMENTATION -DNANOSVGRAST_IMPLEMENTATION -DNANOSVG_ALL_COLOR_KEYWORDS : ' Enable graphical functions PROTO nsvgDelete, nsvgRasterize, nsvgDeleteRasterizer : ' Make these functions known to BaCon parser '----------------------------------------------------------------------------------------------------------------------------------- SUB NANOSVG_IMAGE(NSVGimage* im, xpos, ypos, flag) LOCAL rast TYPE NSVGrasterizer* LOCAL shape TYPE NSVGshape* LOCAL path TYPE NSVGpath* LOCAL data TYPE unsigned char* LOCAL w, h, i, texture TYPE int LOCAL p TYPE float* w = im->width : ' Must convert size to integers h = im->height IF flag = TRUE THEN rast = nsvgCreateRasterizer() : ' Needed to fill in colors data = MEMORY(w*h*4) : ' Allocate memory for our bytes nsvgRasterize(rast, im, 0, 0, 1, data, w, h, w*4) : ' Colorize the picture CALL Draw_Prepare : ' Setup 2D canvas with (0,0) top left SELECT CANVAS.flipping CASE 0 glPixelZoom(-1.0, -1.0) : ' Horizontal flip CASE 1 glPixelZoom(1.0, 1.0) : ' Vertical flip CASE 2 glPixelZoom(-1.0, 1.0) : ' Both flip DEFAULT glPixelZoom(1.0, -1.0) : ' No flip ENDSELECT glPixelStorei(GL_UNPACK_ROW_LENGTH, w) : ' Set the row length of the data array glGenTextures(1, &texture) glBindTexture(GL_TEXTURE_2D, texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data) glColor4ub(255,255,255,255) : ' Prevent color mixup with the texture glEnable(GL_TEXTURE_2D) glBegin(GL_QUADS) glTexCoord2f(0.0f, 0.0f) : glVertex2f(xpos-w/2, ypos-h/2) glTexCoord2f(0.0f, 1.0f) : glVertex2f(xpos-w/2, ypos+h/2-1) glTexCoord2f(1.0f, 1.0f) : glVertex2f(xpos+w/2, ypos+h/2-1) glTexCoord2f(1.0f, 0.0f) : glVertex2f(xpos+w/2, ypos-h/2) glEnd() glDisable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, 0) glDeleteTextures(1, &texture) nsvgDeleteRasterizer(rast) : ' Delete rasterizer from memory FREE data : ' Release bytes glPixelStorei(GL_UNPACK_ROW_LENGTH, 0) : ' Restore the row length ELSE CALL Draw_Prepare : ' Setup 2D canvas with (0,0) top left shape = im->shapes WHILE shape <> NULL path = shape->paths WHILE path <> NULL FOR i = 0 TO (path->npts-1)-1 STEP 3 p = &path->pts[i*2] CBEZIER(p[0]+xpos-w/2, p[1]+ypos-h/2, p[2]+xpos-w/2, p[3]+ypos-h/2, p[4]+xpos-w/2, p[5]+ypos-h/2, p[6]+xpos-w/2, p[7]+ypos-h/2) NEXT i path = path->next WEND shape = shape->next WEND ENDIF ENDSUB SUB NANOSVG_IMAGE_FILE(file$, xpos, ypos, flag) LOCAL image TYPE NSVGimage* image = nsvgParseFromFile(file$, "px", 96.0f) : ' Parse the SVG file using pixel format and 96DPI CALL NANOSVG_IMAGE(image, xpos, ypos, flag) nsvgDelete(image) : ' Delete SVG image from memory ENDSUB FUNCTION NANOSVG_IMAGE_WIDTH(file$) LOCAL image TYPE NSVGimage* LOCAL width image = nsvgParseFromFile(file$, "px", 96.0f) : ' Parse the SVG file using pixel format and 96DPI width = image->width nsvgDelete(image) : ' Delete SVG image from memory RETURN width ENDFUNCTION FUNCTION NANOSVG_IMAGE_HEIGHT(file$) LOCAL image TYPE NSVGimage* LOCAL height image = nsvgParseFromFile(file$, "px", 96.0f) : ' Parse the SVG file using pixel format and 96DPI height = image->height nsvgDelete(image) : ' Delete SVG image from memory RETURN height ENDFUNCTION SUB NANOSVG_IMAGE_INFO(file$) LOCAL image TYPE NSVGimage* image = nsvgParseFromFile(file$, "px", 96.0f) : ' Parse the SVG file using pixel format and 96DPI PRINT "Filename: ", file$ PRINT "Dimensions: ", image->width, "x", image->height nsvgDelete(image) : ' Delete SVG image from memory ENDSUB SUB NANOSVG_IMAGE_PTR(txt$, xpos, ypos, flag) LOCAL image TYPE NSVGimage* image = nsvgParse(txt$, "px", 96.0f) : ' Parse the SVG string using pixel format and 96DPI CALL NANOSVG_IMAGE(image, xpos, ypos, flag) nsvgDelete(image) : ' Delete SVG image from memory ENDSUB