Usar una fuente True Type donde sea

1964 vistas

1. Recuperamos un InputStream sobr el fichero TTF



java



- desde un servlet, el fichero TTF está en el directorio /fonts/:



java
  1. ttf = getServletContext().getResourceAsStream("fonts/ARIALN.TTF");



- desde el sustema de ficheros:



java
  1. ttf = new FileInputStream("/aaa/bbb/ccc/ARIALN.TTF");



2. Creamos la fuente:



java
  1. Font arialNarrow;
  2. try {
  3.   arialNarrow = Font.createFont(Font.TRUETYPE_FONT, ttf);
  4. }
  5. catch (Exception e) {
  6.   arialNarrow = new Font("Arial", Font.PLAIN, 1);
  7. }
  8. finally {
  9.   try {
  10.       ttf.close();
  11.   }
  12.   catch (Exception ignore) {}
  13. }



3. La fuente obtenida es de tamaño 1. Ahora usaremos deriveFont para crear deribados. Por ejemplo, obtenemos arial narrow negrita con:



java
  1. Font arialNarrowBold = arialNarrow.deriveFont(Font.BOLD);