Code covered by the BSD License  

Highlights from
matprint

image thumbnail
from matprint by Karel Lebeda
Prints (rasterizes) text directly to image matrix.

loadAndParseTFF(filename)
function [font] = loadAndParseTFF(filename)
% load and parse tff file

	if nargin == 0
		filename = 'default.tff';
	end
	fid = fopen(filename, 'r');

	if fid == -1
		error('File %s does not exist or is inaccessible!', filename);
	end

	ln = 0;

	hasQuestionMark = false;

	while true
		ln = ln + 1;
		line = fgetl(fid);
		if isnumeric(line)
			break
		end
		if isempty(line)
			continue
		end
		switch line(1)
			case {' ', '#', '%'}
				continue
			case 'N'
				if line(2) ~= ':'
					error('File %s contains unexpected character ''%c'' on line %d (expected '':'')!', filename, line(2), ln);
				end
				font.name = processText(line(3:end));
			case 'D'
				if line(2) ~= ':'
					error('File %s contains unexpected character ''%c'' on line %d (expected '':'')!', filename, line(2), ln);
				end
				font.desc = processText(line(3:end));
			case 'C'
				if line(2) ~= ':' || line(4) ~= ':'
					error('File %s contains unexpected character ''%c'' on line %d (expected '':'')!', filename, line(2), ln);
				end
				c = parseChar(line, filename, ln);
				font.char(c.char + 0) = c;
				if c.char == '?'
					hasQuestionMark = true;
				end
			case 'S'
				if line(2) ~= ':'
					error('File %s contains unexpected character ''%c'' on line %d (expected '':'')!', filename, line(2), ln);
				end
				semicolonPos = find(line == ';', 1);
				if isempty(semicolonPos)
					font.spacing = str2double(line(3:end));
				else
					font.spacing = str2double(line(3:semicolonPos-1));
				end
				if isnan(font.spacing)
					error('Invalid spacing specification on line %d of file %s!', ln, filename);
				end
			otherwise
					error('Line %d of file %s begins with unexpected character ''%c'' (expected N/D/C)!', ln, filename, line(1));
		end




	end


	if ~hasQuestionMark
		error('No record with question mark (mandatory) found in the font file %s!', filename);
	end

end


Contact us