Skip to content

Commit

Permalink
#180 Implements text and transforms within page margins.
Browse files Browse the repository at this point in the history
This commit also includes a small (16KB) font for testing purposes. It was chosen for its small size and OFL license (also committed). The bold variant was chosen so that the tester can easily see where it is being used.

Page margin transforms are specifically tested in this commit.
  • Loading branch information
danfickle committed Oct 15, 2018
1 parent 6156eed commit b545b18
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,6 @@ public void paintBackground(RenderingContext c, int additionalClearance, short m

private MarginAreaContainer currentMarginAreaContainer;
public void paintMarginAreas(RenderingContext c, int additionalClearance, short mode) {
SimplePainter painter = c.getOutputDevice().isFastRenderer() ? new SimplePainter() : null;

for (int i = 0; i < MARGIN_AREA_DEFS.length; i++) {
MarginAreaContainer container = _marginAreas[i];

Expand All @@ -406,6 +404,7 @@ public void paintMarginAreas(RenderingContext c, int additionalClearance, short

c.getOutputDevice().translate(p.x, p.y);
if (c.getOutputDevice().isFastRenderer()) {
SimplePainter painter = new SimplePainter(p.x, p.y);
painter.paintLayer(c, table.getLayer());
} else {
table.getLayer().paint(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,47 @@ public static AffineTransform createPageCoordinatesTranform(RenderingContext c,
return start;
}

public static AffineTransform createPageMarginCoordinatesTransform(RenderingContext c, Box box, PageBox page, int xTranslate, int yTranslate) {
FSDerivedValue transforms = box.getStyle().valueByName(CSSName.TRANSFORM);

float relOriginX = box.getStyle().getFloatPropertyProportionalWidth(CSSName.FS_TRANSFORM_ORIGIN_X,
box.getWidth(), c);
float relOriginY = box.getStyle().getFloatPropertyProportionalHeight(CSSName.FS_TRANSFORM_ORIGIN_Y,
box.getHeight(), c);

float flipFactor = c.getOutputDevice().isPDF() ? -1 : 1;

float absTranslateX = relOriginX + box.getAbsX();
float absTranslateY = relOriginY + box.getAbsY();

float pageTranslateX;
float pageTranslateY;

if (c.getOutputDevice().isPDF()) {
// The transform point is the lower left of the page (PDF coordinate system).
pageTranslateX = absTranslateX + xTranslate;
float topDownPageTranslateY = (absTranslateY + yTranslate);
pageTranslateY = (page.getHeight(c) - topDownPageTranslateY);
} else { // PAGE_TOP
// The transform point is the upper left of the page.
pageTranslateX = absTranslateX + xTranslate;
pageTranslateY = (absTranslateY) + yTranslate;
}

AffineTransform translateToOrigin = AffineTransform.getTranslateInstance(pageTranslateX, pageTranslateY);
AffineTransform translateBackFromOrigin = AffineTransform.getTranslateInstance(-pageTranslateX,
-pageTranslateY);

List<PropertyValue> transformList = (List<PropertyValue>) ((ListValue) transforms).getValues();

AffineTransform result = new AffineTransform();
result.concatenate(translateToOrigin);
applyTransformFunctions(flipFactor, transformList, result, box, c);
result.concatenate(translateBackFromOrigin);

return result;
}

/**
* Creates an absolute transform in document coordinates. This is typically used to figure out what pages the box will
* fall on. The <code>_parentCtm</code> may be null in case the parent layer uses the identity tranform. If it is not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;

import com.openhtmltopdf.layout.CollapsedBorderSide;
import com.openhtmltopdf.layout.InlinePaintable;
import com.openhtmltopdf.layout.Layer;
import com.openhtmltopdf.newtable.TableCellBox;
import com.openhtmltopdf.render.BlockBox;
Expand All @@ -18,6 +19,14 @@
import com.openhtmltopdf.render.displaylist.TransformCreator;

public class SimplePainter {
private final int xTranslate;
private final int yTranslate;

public SimplePainter(int xtrans, int ytrans) {
this.xTranslate = xtrans;
this.yTranslate = ytrans;
}

private void debugOnly(String msg, Object arg) {
//System.out.println(msg + " : " + arg);
}
Expand All @@ -31,8 +40,7 @@ public void paintLayer(RenderingContext c, Layer layer) {
}

if (layer.hasLocalTransform()) {
// TODO: This is not correct for page margins!
AffineTransform transform = TransformCreator.createPageCoordinatesTranform(c, master, c.getPage());
AffineTransform transform = TransformCreator.createPageMarginCoordinatesTransform(c, master, c.getPage(), xTranslate, yTranslate);
c.getOutputDevice().pushTransformLayer(transform);
}

Expand Down Expand Up @@ -131,7 +139,22 @@ private void paintListMarkers(RenderingContext c, List<DisplayListItem> listItem
}

private void paintInlineContent(RenderingContext c, List<DisplayListItem> inlines) {
// TODO Auto-generated method stub
for (DisplayListItem dli : inlines) {
if (dli instanceof OperatorClip) {
OperatorClip clip = (OperatorClip) dli;
clip(c, clip);
} else if (dli instanceof OperatorSetClip) {
OperatorSetClip setClip = (OperatorSetClip) dli;
setClip(c, setClip);
} else if (dli instanceof BlockBox) {
// Inline blocks need to be painted as a layer.
BlockBox bb = (BlockBox) dli;
paintAsLayer(c, bb);
} else {
InlinePaintable paintable = (InlinePaintable) dli;
paintable.paintInline(c);
}
}

}

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright (c) 2011-2012, Jonathan Pinhorn (jonpinhorn.typedesign@gmail.com), with Reserved Font Names 'Karla'

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<html>
<head>
<style>
@font-face {
src: url(fonts/Karla-Bold.ttf);
font-family: 'TestFont';
}
@page {
size: 200px 200px;
margin: 80px;

@left-middle {
font-family: 'TestFont';
content: "->";
font-size: 35px;
transform: rotate(-90deg);
}
@right-middle {
font-family: 'TestFont';
content: "->";
font-size: 35px;
transform: rotate(90deg);
}

@bottom-center {
font-family: 'TestFont';
content: "->";
font-size: 35px;
transform: rotate(-45deg);
}
@top-center {
font-family: 'TestFont';
content: "->";
font-size: 35px;
transform: rotate(45deg);
}
}
</style>
</head>
<body>
<div style="width: 100%; height: 20px; background-color: transparent; page-break-after: always;"></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ public void testPageMarginsSimple() throws IOException {
assertTrue(vt.runTest("page-margins-simple"));
}

/**
* Tests a simple transform in top-center, bottom-center, left-middle, right-middle.
*/
@Test
public void testPageMarginsSimpleTransform() throws IOException {
assertTrue(vt.runTest("page-margins-simple-transform"));
}


// TODO:
// + Inline layers.
// + Replaced elements.
Expand Down

0 comments on commit b545b18

Please sign in to comment.