pentaho-reporting icon indicating copy to clipboard operation
pentaho-reporting copied to clipboard

When rendering html richtext containing style and fontsize specified in "px" then this text is displayed too big.

Open mstejska opened this issue 9 months ago • 0 comments

There is html fragment of richtext:

<span style=\"font-size:13px\">13px high text here.</span>

It should be rendered as text with height 10pt, but because of bug in org.pentaho.reporting.engine.classic.core.layout.richtext.HtmlRichTextConverter.HtmlRichTextConverter and its function parseLength() it is rendered with font tall 13 inches.

I suspect that it is because of copy&paste error. See the code:

      if ( "pt".equals( unit ) ) {
        return new Float( nval );
      }
      if ( "in".equals( unit ) ) {
        return new Float( nval * 72 );
      }
      if ( "px".equals( unit ) ) {
        return new Float( nval * 72 );
      }
      if ( "pc".equals( unit ) ) {
        return new Float( nval * 12 );
      }

The size in 'pt' is correct however size in 'px' is wrong. correct code for 'px' should be:

      if ( "px".equals( unit ) ) {
    	 return Float.valueOf((float) (nval * 0.75)); // 1pt is 1.33px
      }

Here is also junit to test correct behavior

package org.pentaho.reporting.engine.classic.core.layout.richtext;

import org.junit.Test;
import org.pentaho.reporting.engine.classic.core.Band;
import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot;
import org.pentaho.reporting.engine.classic.core.Element;
import org.pentaho.reporting.engine.classic.core.ReportElement;
import org.pentaho.reporting.engine.classic.core.layout.style.SimpleStyleSheet;
import org.pentaho.reporting.engine.classic.core.style.BandDefaultStyleSheet;
import org.pentaho.reporting.engine.classic.core.style.TextStyleKeys;

import junit.framework.TestCase;

public class HtmlRichTextConverterTest extends TestCase {

	@Test
	public void testConvertPxToPt() {
		ClassicEngineBoot.getInstance().start();
		testConversionPxToPt(
		 	"<span style=\"font-size:13px\">13px high text here.</span>",
			"13px high text here.",
			10
		);
		testConversionPxToPt(
			"<span style=\"font-size:26px\">this 26px text has 20pt size.</span>",
			"this 26px text has 20pt size.",
			20
		);
	}

	private void testConversionPxToPt(String aHtml, String aExpectedPlainText, double aExpectedFontSize) {
		HtmlRichTextConverter converter = new HtmlRichTextConverter();
		ReportElement source = new Element();
		source.setComputedStyle(new SimpleStyleSheet(BandDefaultStyleSheet.getBandDefaultStyle()));
		Object result = converter.convert(source, aHtml);
		assertTrue(result instanceof Band);
		Band band = (Band) result;
		Element content = (Element) band.getChildElementsByName("content")[0];
		Object elementAttributeValue = content.getAttribute("http://reporting.pentaho.org/namespaces/engine/attributes/core", "value");
		assertEquals(aExpectedPlainText, elementAttributeValue);
		double fontSize = content.getStyle().getDoubleStyleProperty(TextStyleKeys.FONTSIZE,0.0);
		assertEquals(aExpectedFontSize, fontSize);
	}
}

Could you please fix it and then inform me?

Best Regards

Martin Stejskal

[email protected]

mstejska avatar May 24 '24 08:05 mstejska