-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#161 Improved sizing and tests for MathML elements.
Now, always keep the correct aspect ratio.
- Loading branch information
Showing
7 changed files
with
314 additions
and
134 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...ltopdf-core/src/main/java/com/openhtmltopdf/simple/extend/ReplacedElementScaleHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+20 KB
openhtmltopdf-examples/src/main/resources/visualtest/expected/replaced-sizing-mathml.pdf
Binary file not shown.
206 changes: 206 additions & 0 deletions
206
openhtmltopdf-examples/src/main/resources/visualtest/html/replaced-sizing-mathml.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.