Skip to content

Commit

Permalink
#161 Improved sizing and tests for MathML elements.
Browse files Browse the repository at this point in the history
Now, always keep the correct aspect ratio.
  • Loading branch information
danfickle committed Jun 27, 2019
1 parent d9cdd6a commit 3b02928
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.openhtmltopdf.simple.extend;

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;

public class ReplacedElementScaleHelper {
/**
* Creates a scale <code>AffineTransform</code> to scale a given replaced element to the desired size.
* @param dotsPerPixel
* @param contentBounds the desired size
* @param width the intrinsic width
* @param height the intrinsic height
* @return AffineTransform or null if not available.
*/
public static AffineTransform createScaleTransform(double dotsPerPixel, Rectangle contentBounds, float width, float height) {
int intrinsicWidth = (int) width;
int intrinsicHeight = (int) height;

int desiredWidth = (int) (contentBounds.width / dotsPerPixel);
int desiredHeight = (int) (contentBounds.height / dotsPerPixel);

AffineTransform scale = null;

if (width == 0 || height == 0) {
// Do nothing...
}
else if (desiredWidth > intrinsicWidth &&
desiredHeight > intrinsicHeight) {

double rw = (double) desiredWidth / width;
double rh = (double) desiredHeight / height;

double factor = Math.min(rw, rh);
scale = AffineTransform.getScaleInstance(factor, factor);
} else if (desiredWidth < intrinsicWidth &&
desiredHeight < intrinsicHeight) {
double rw = (double) desiredWidth / width;
double rh = (double) desiredHeight / height;

double factor = Math.max(rw, rh);
scale = AffineTransform.getScaleInstance(factor, factor);
}

return scale;
}

public static AffineTransform inverseOrNull(AffineTransform in) {
if (in == null) {
return null;
}
try {
return in.createInverse();
} catch (NoninvertibleTransformException e) {
return null;
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<!DOCTYPE html PUBLIC
"-//OPENHTMLTOPDF//MATH XHTML Character Entities With MathML 1.0//EN"
"">
<html>
<head>
<style>
@font-face {
src: url(fonts/Karla-Bold.ttf);
font-family: 'MyFont';
font-weight: normal;
}
@page {
size: 500px 1500px;
margin: 0;
}
body {
margin: 0;
}
math {
font-family: 'MyFont';
display: block;
border: 2px solid red;
margin: 2px;
padding: 4px;
}
</style>
</head>
<body>
<!-- width only -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="width: 100px;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- height only -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="height: 100px;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- min-width -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="width: 100px; min-width: 200px;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- min-height -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="height: 100px; min-height: 50px;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- max-width -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="width: 400px; max-width: 70px;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- max-height -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="height: 400px; max-height: 80px;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- border-box -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="padding: 50px; width: 220px; box-sizing: border-box;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- content-box -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="padding: 50px; width: 200px; box-sizing: content-box;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- width and height -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="width: 100px; height: 100px;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- border-box with different padding values -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="padding: 0 10px 20px 40px; width: 220px; box-sizing: border-box;">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

<!-- Intrinsic size -->
<math xmlns="http://www.w3.org/1998/Math/MathML" style="">
<mrow>
<msup>
<mfenced>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
</mfenced>
<mn>2</mn>
</msup>
</mrow>
</math>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Ignore;
import org.junit.Test;

import com.openhtmltopdf.mathmlsupport.MathMLDrawer;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;
import com.openhtmltopdf.visualtest.VisualTester;
Expand Down Expand Up @@ -763,6 +764,16 @@ public void testReplacedSizingSvgNonCss() throws IOException {
assertTrue(vt.runTest("replaced-sizing-svg-non-css", WITH_SVG));
}

/**
* Tests all the CSS sizing properties for MathML elements.
*/
@Test
public void testReplacedSizingMathMl() throws IOException {
assertTrue(vt.runTest("replaced-sizing-mathml", (builder) -> {
builder.useMathMLDrawer(new MathMLDrawer());
}));
}

// TODO:
// + Elements that appear just on generated overflow pages.
// + content property (page counters, etc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public SVGImage buildSVGImage(Element mathMlElement, Box box, CssContext c, doub
double cssMaxHeight = CalculatedStyle.getCSSMaxHeight(c, box);
List<String> fontList = Arrays.asList(fonts);

MathMLImage img = new MathMLImage(mathMlElement, cssWidth, cssHeight, cssMaxWidth, cssMaxHeight, dotsPerPixel, fontList);
MathMLImage img = new MathMLImage(mathMlElement, box, cssWidth, cssHeight, cssMaxWidth, cssMaxHeight, dotsPerPixel, fontList);

return img;
}
Expand Down
Loading

0 comments on commit 3b02928

Please sign in to comment.