-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprintSVGpoly.m
49 lines (41 loc) · 1.16 KB
/
printSVGpoly.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function printSVGpoly(svgData,bitmap,fileID)
%Takes data formatted by vectorizeLineSmart and prints it to an SVG using
%polylines
%
%EXAMPLE:
% printSVGpoly(svgDataIntermediate, exLarge, 'output.svg');
%
%INPUT:
% svgData - the svgDataIntermediate variable output by vectorizeLineSmart
% bitmap - the bitmap image, used to extract image size
% fileID - the id of the file to write to
%
%
%Add file extension if not present
if 4 <= length(fileID)
if ~isequal(fileID(end-3:end),'.svg')
fileID = [fileID, '.svg'];
end
else
fileID = [fileID, '.svg'];
end
svgData = padarray(svgData,[0,2],0,'post');
%create and overwrite file
fileID = fopen(fileID,'w');
%header
fprintf(fileID, '<svg viewBox="0 0 %u %u" xmlns="http://www.w3.org/2000/svg">\n', size(bitmap,2),size(bitmap,1));
%format svg polylines
for i=1:size(svgData,1)
counter=1;
fprintf(fileID, '<polyline points="');
while svgData(i,counter) ~= 0
fprintf(fileID,'%u,%u ' ,svgData(i,counter), svgData(i,counter+1));
counter = counter+2;
end
fprintf(fileID, '" fill="none" stroke="black" />\n');
end
%footer
fprintf(fileID, '</svg>');
%save file
fclose(fileID);
end