/* jslint node: true */
'use strict';

var _ = require('lodash');

_.noConflict();

function typeName(bold, italics) {
	var type = 'normal';
	if (bold && italics) {
		type = 'bolditalics';
	} else if (bold) {
		type = 'bold';
	} else if (italics) {
		type = 'italics';
	}
	return type;
}

function FontProvider(fontDescriptors, pdfDoc) {
	this.fonts = {};
	this.pdfDoc = pdfDoc;
	this.fontCache = {};

	for (var font in fontDescriptors) {
		if (fontDescriptors.hasOwnProperty(font)) {
			var fontDef = fontDescriptors[font];

			this.fonts[font] = {
				normal: fontDef.normal,
				bold: fontDef.bold,
				italics: fontDef.italics,
				bolditalics: fontDef.bolditalics
			};
		}
	}
}

FontProvider.prototype.provideFont = function (familyName, bold, italics) {
	var type = typeName(bold, italics);
	if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
		throw new Error('Font \'' + familyName + '\' in style \'' + type + '\' is not defined in the font section of the document definition.');
	}

	this.fontCache[familyName] = this.fontCache[familyName] || {};

	if (!this.fontCache[familyName][type]) {
		this.fontCache[familyName][type] = this.pdfDoc.font(this.fonts[familyName][type])._font;
	}

	return this.fontCache[familyName][type];
};

module.exports = FontProvider;

@@ -1102,24 +1102,115 @@
 e%5D%5Btype%5D) %7B%0A
+%09%09var def = this.fonts%5BfamilyName%5D%5Btype%5D;%0A%09%09if (!Array.isArray(def)) %7B%0A%09%09%09def = %5Bdef%5D;%0A%09%09%7D%0A
 %09%09this.fontC
@@ -1254,37 +1254,31 @@
 font
-(this.fonts%5BfamilyName%5D%5Btype%5D
+.apply(this.pdfDoc, def
 )._f

Add support for TrueType Collection (.ttc)