% This file parses a Hershey font file and converts it to xy format.
% The output data for the character specified is in the array 'data'

% Written by Shad Roundy
% Date:  25/9/03

clear all;

% Load file
[fileName,path]=uigetfile('*.*','Choose input file for parameters.');
result = importdata(strcat(path,fileName), '\n');


% Prompt user for character number
charNum = input('Enter the character number you want:\n');

% Parse each line and convert to numeric data
foundFlag = 0;
lineNum = 1;
counter = 1;
for i = 1:length(result)
    tmpChar = char(result(i));
    tmpTok = str2double(strtok(tmpChar));
    if tmpTok == charNum
        lineNum = i;
        foundFlag = 1;
    end
end

if foundFlag == 0
    'You entered a character number that does not exist.'
    break;
end
    
tmpChar = char(result(lineNum));
    
% Break up each line into space delimited tokens
firstToken = 1;
while ~isempty(tmpChar)
    [token,tmpChar] = strtok(tmpChar,[' ','1','2','3','4','5','6','7','8','9','0']);
        
     % First token has an extra leading character
     if firstToken == 1
         offset = 2;
         firstToken = 0;
     else
         offset = 1;
     end
        
     % Move to the starting point in air.
     k = 1;
     data(counter,1) = double(token(offset + k) - double('R')); 
     data(counter,2) = double(token(offset + k + 1) - double('R'));
     data(counter,3) = 1;
     counter = counter + 1;
        
     while k + offset < length(token)
         data(counter,1) = double(token(offset + k) - double('R')); 
         data(counter,2) = double(token(offset + k + 1) - double('R'));          
         data(counter,3) = 0;
         counter = counter + 1;
         k = k + 2;
     end
        
     %Raise up pen at end of token and move to next token in air
     data(counter,1) = data(counter - 1, 1); 
     data(counter,2) = data(counter - 1, 2);
     data(counter,3) = 1;
     counter = counter + 1;
end


figure(1)
plot3(data(:,1),data(:,2),data(:,3));
xlabel('x');
ylabel('y');
zlabel('z');

