You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Dylan Browne <db...@mango-solutions.com> on 2008/02/11 14:34:11 UTC

Fonts available to ImageTranscoder

Hi all,

 

I have a question regarding the fonts that are available to the
ImageTranscoder classes.

 

Would it be correct to assume that the list of fonts returned from the
following snippet of code correctly and completely identify the fonts
that are available to a, for example, PNGTranscoder.

 

      private void listFonts() {

            // get the local graphics environment

            GraphicsEnvironment graphicsEvn =
GraphicsEnvironment.getLocalGraphicsEnvironment();

            // get all the available fonts

            String availFonts[] =
graphicsEvn.getAvailableFontFamilyNames();

            System.out.println("The following fonts are available for
use:");

            for (int i = 0; i < availFonts.length; i++) {

                  System.out.println(availFonts[i]);

            }

      }

 

I have encountered a situation in which occasionally a PNG derived from
SVG is skewing the alignment of the text (it seems to ignore the
basline-shift attribute). An examination of the fonts available using
the above code indicates that my chosen font, Arial, is not available.
The actual font incorporated in the PNG is definitely not Arial, and
seems to be some basic system font.

 

The initial SVG itself is rendered fine in the browser, but I assume
that the fonts there are being retrieved from the client. The PNG
transcode is carried out on the server, where the above code seems to
suggest that Arial is not available. Could this be the cause of the
problem?

 

Thanks very much in advance for any advice,

 

Dylan 

 


RE: Fonts available to ImageTranscoder

Posted by Dylan Browne <db...@mango-solutions.com>.
Thanks for the reply.

In the meantime I have installed the fonts, listFonts() does indeed
perform as required :o), and still see the same issue.

I have a test case which generates 500 PNGs from an SVG file.

The font (Arial) is now present in all exports, skewed or un-skewed.

If the SVG file does not incorporate any baseline-shift attributes
applied to text elements, then the PNGs are rendered correctly 500
times.

If the SVG file incorporates any baseline-shift attributes applied to
text elements, then the PNGs are rendered correctly for a number of
times and then the remainder of the 500 incorporate skewed text (IE the
baseline-shift attribute is not applied in the PNG). The number of
successful exports varies, (but may be related to the size of the
initial SVG, not sure). It's typically around 5 to 15.

All other attributes are recognized and correct, only baseline-shift
seems to cause this. Only text elements are affected.

This is not currently reproducible on my laptop (Windows XP). It occurs
only in a SunOS environment (I am unable to test if it is consistent in
differing installations of Sun OS).

I include my central PNG export method here, if anyone cares to look.
Extremely puzzled by this!

We are now implementing a work-around by ditching the baseline-shift
attribute and applying the value of baseline-shift to the Y of the text
element.

Thanks as ever,

Dylan.

	/**
	 * Method to write an SVG input file into a given image type
	 * 
	 * @param width The width
	 * @param height The height
	 * @param inputFileName The input filename
	 * @param outputFileName The output filename. The output will be
the current working directory
	 */
	public synchronized void svgFileToImage(Float width, Float
height, String inputFileName, String outputFileName) {
		OutputStream ostream = null;
		TranscoderOutput output = null;
		InputStream fis = null;
		ByteArrayOutputStream imageOutputStream = null;
		FileOutputStream fos = null;
		String mimeType = ("image/" + imageType).toLowerCase();

		Long currentDate;
		Long lastDate;
		Long duration;

		// Create the transcoder
		ImageTranscoder t = null;
		if (mimeType.equalsIgnoreCase("image/jpeg")) {
			t = new JPEGTranscoder();
			t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(1.0));
		} else if (mimeType.equalsIgnoreCase("image/png")) {
			t = new PNGTranscoder();
		} else {
			System.out.println("ERROR: A request to stream
an image was recieved with an unsupported mime type: " + mimeType);
		}

		// Set the width and height
		t.addTranscodingHint(ImageTranscoder.KEY_WIDTH, width);
		t.addTranscodingHint(ImageTranscoder.KEY_HEIGHT,
height);
		// Set the background to white
	
t.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);

		byte[] imageBytes = null;
		try {
			// Open the svg file on the hard disk
			fis = new FileInputStream(new
File(this.currentDirectory + RESOURCE_ROOT + INPUT_ROOT +
inputFileName));
			lastDate = new Date().getTime();
			TranscoderInput input = new
TranscoderInput(fis);
			ostream = new ByteArrayOutputStream();
			output = new TranscoderOutput(ostream);
			t.transcode(input, output);
			imageOutputStream = ((ByteArrayOutputStream)
output.getOutputStream());
			imageBytes = imageOutputStream.toByteArray();
			currentDate = new Date().getTime();
			duration = currentDate - lastDate;
			System.out.println("Transcoder duration: " +
duration + " ms");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				ostream.flush();
				ostream.close();
				imageOutputStream.flush();
				imageOutputStream.close();
			} catch (Exception e) {
				System.out.println("Error in transcoder
stream because " + e.getMessage());
			}
		}

		try {
			// Write to file
			File outputImageFile = new
File(this.currentDirectory + RESOURCE_ROOT + OUTPUT_ROOT +
outputFileName + "." + imageType);
			fos = new FileOutputStream(outputImageFile);
			fos.write(imageBytes);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.flush();
				fos.close();
			} catch (Exception e) {
				System.out.println("Error in closing
stream when writing out image because " + e.getMessage());
			}
		}
	}

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Fonts available to ImageTranscoder

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
If your server runs a Unix it could very well be that there's no Arial
font available.

If I'm not wrong, yes, your listFonts() method should show the fonts
available to Batik. Except that it doesn't list any SVG fonts embedded
in the document.

On 11.02.2008 14:34:11 Dylan Browne wrote:
> Hi all,
> 
>  
> 
> I have a question regarding the fonts that are available to the
> ImageTranscoder classes.
> 
>  
> 
> Would it be correct to assume that the list of fonts returned from the
> following snippet of code correctly and completely identify the fonts
> that are available to a, for example, PNGTranscoder.
> 
>  
> 
>       private void listFonts() {
> 
>             // get the local graphics environment
> 
>             GraphicsEnvironment graphicsEvn =
> GraphicsEnvironment.getLocalGraphicsEnvironment();
> 
>             // get all the available fonts
> 
>             String availFonts[] =
> graphicsEvn.getAvailableFontFamilyNames();
> 
>             System.out.println("The following fonts are available for
> use:");
> 
>             for (int i = 0; i < availFonts.length; i++) {
> 
>                   System.out.println(availFonts[i]);
> 
>             }
> 
>       }
> 
>  
> 
> I have encountered a situation in which occasionally a PNG derived from
> SVG is skewing the alignment of the text (it seems to ignore the
> basline-shift attribute). An examination of the fonts available using
> the above code indicates that my chosen font, Arial, is not available.
> The actual font incorporated in the PNG is definitely not Arial, and
> seems to be some basic system font.
> 
>  
> 
> The initial SVG itself is rendered fine in the browser, but I assume
> that the fonts there are being retrieved from the client. The PNG
> transcode is carried out on the server, where the above code seems to
> suggest that Arial is not available. Could this be the cause of the
> problem?
> 
>  
> 
> Thanks very much in advance for any advice,
> 
>  
> 
> Dylan 
> 
>  
> 




Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org