int i = 0, index = -1; while (i < PDF_FONTS.length && index < 0) { if (PDF_FONTS[i].equals(fontName)) index = i; else i++; } if (index < 0) { index = 0; } writeLine(mPDFGeometryWriter, "/F" + index + " " + mCoordinateDecimal.format(fontSize) + " Tf");
	public void setFontAttribute(String fontName, double fontSize, double fontRotation, double outlineWidth)		throws IOException, MapyrusException	{		if (mOutputType == POSTSCRIPT_GEOMETRY)		{			if (mEncodeAsISOLatin1.contains(fontName))			{				/*				 * Re-encode font from StandardEncoding to ISOLatin1Encoding				 * before it is used.				 */				writeLine(mWriter, isoLatinEncode(fontName));				mEncodeAsISOLatin1.remove(fontName);			}			/*			 * Set font and size for labelling.			 */			writeLine(mWriter, "/" + fontName + " " +				fontSize + " " +				fontRotation + " " +				outlineWidth + " font");			mNeededFontResources.add(fontName);		}		else if (mOutputType == PDF)		{			int i = 0, index = -1;			while (i < PDF_FONTS.length && index < 0)			{				if (PDF_FONTS[i].equals(fontName))					index = i;				else					i++;			}			if (index < 0)			{				index = 0;			}			writeLine(mPDFGeometryWriter, "/F" + index + " " +				mCoordinateDecimal.format(fontSize) + " Tf");		}		else		{			/*			 * Split font name into font and style.			 */			int style = Font.PLAIN;			if (fontName.endsWith("-Bold"))			{				style = Font.BOLD;				fontName = fontName.substring(0, fontName.length() - 5);			}			else if (fontName.endsWith("-Italic"))			{				style = Font.ITALIC;				fontName = fontName.substring(0, fontName.length() - 7);			}			else if (fontName.endsWith("-BoldItalic"))			{				style = Font.BOLD|Font.ITALIC;				fontName = fontName.substring(0, fontName.length() - 11);			}			/*			 * Continually opening and deriving fonts is probably expensive.			 * Check that new font is actually different to current font			 * before defining it.			 */			Font currentFont = mGraphics2D.getFont();			float newSize = (float)fontSize;			float currentSize = currentFont.getSize2D();			String currentFontName = currentFont.getName();			if (newSize != currentSize ||				style != currentFont.getStyle() ||				(!fontName.equals(currentFontName)) ||				fontRotation != mFontRotation)			{				/*				 * We need a base font that is not rotated for calculating				 * string widths for justifying text.				 * Get base font from cache, or create it if we don't find it there.				 */				mBaseFont = mFontCache.get(fontName, style, newSize, 0);				if (mBaseFont == null)				{					/*					 * If this is a font for which user provided a TTF file then					 * use that, else expect the operating system to be able to					 * open the font.					 */					TrueTypeFont ttf = (TrueTypeFont)mTTFFonts.get(fontName);					if (ttf != null)						mBaseFont = ttf.getFont().deriveFont(style, newSize);					else						mBaseFont = new Font(fontName, style, (int)newSize).deriveFont(newSize);					mFontCache.put(fontName, style, newSize, 0, mBaseFont);				}				/*				 * The real font used for labelling must be mirrored in Y axis				 * (to reverse the transform we use on Graphics2D objects) and				 * rotated to the angle the user wants.				 *				 * Look it up in cache too.				 */				Font font = mFontCache.get(fontName, style, -newSize, fontRotation);				if (font == null)				{					AffineTransform fontTransform;					fontTransform = AffineTransform.getRotateInstance(fontRotation);					fontTransform.scale(1, -1);					font = mBaseFont.deriveFont(fontTransform);					mFontCache.put(fontName, style, -newSize, fontRotation, font);				}				mGraphics2D.setFont(font);			}		}		/*		 * Font rotation and outlining not easily held in a Graphics2D		 * object so keep track of it's current value ourselves.		 */		mFontRotation = fontRotation;		mFontOutlineWidth = outlineWidth;	}