Skip to content

Commit

Permalink
[MNT-23960] Added options (pdfFont, pdfFontSize) and NotoSans fonts t…
Browse files Browse the repository at this point in the history
…o textToPdf transformer
  • Loading branch information
tiagosalvado10 committed Nov 14, 2023
1 parent 410f042 commit 8240521
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.tools.TextToPDF;
import org.slf4j.Logger;
Expand All @@ -48,12 +50,15 @@
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import static org.alfresco.transform.common.RequestParamMap.PAGE_LIMIT;
import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING;
import static org.alfresco.transform.common.RequestParamMap.PDF_FONT;
import static org.alfresco.transform.common.RequestParamMap.PDF_FONT_SIZE;

/**
* <p>
Expand Down Expand Up @@ -81,6 +86,11 @@ public class TextToPdfContentTransformer implements CustomTransformerFileAdaptor

private final PagedTextToPDF transformer;

protected static final String NOTOSANS_REGULAR = "NotoSans-Regular";
protected static final String NOTOSANS_BOLD = "NotoSans-Bold";
protected static final String NOTOSANS_ITALIC = "NotoSans-Italic";
protected static final String NOTOSANS_BOLD_ITALIC = "NotoSans-BoldItalic";

public TextToPdfContentTransformer()
{
transformer = new PagedTextToPDF();
Expand All @@ -90,7 +100,7 @@ public void setStandardFont(String fontName)
{
try
{
transformer.setFont(PagedTextToPDF.getStandardFont(fontName));
transformer.setFont(fontName);
}
catch (Throwable e)
{
Expand Down Expand Up @@ -130,6 +140,20 @@ public void transform(final String sourceMimetype, final String targetMimetype,
{
pageLimit = parseInt(stringPageLimit, PAGE_LIMIT);
}
String pdfFont = transformOptions.get(PDF_FONT);
String pdfFontSize = transformOptions.get(PDF_FONT_SIZE);
Integer fontSize = null;
if (pdfFontSize != null)
{
try
{
fontSize = parseInt(pdfFontSize, PDF_FONT_SIZE);
}
catch (Exception e)
{
fontSize = 10;
}
}

PDDocument pdf = null;
try (InputStream is = new FileInputStream(sourceFile);
Expand All @@ -138,7 +162,7 @@ public void transform(final String sourceMimetype, final String targetMimetype,
{
//TransformationOptionLimits limits = getLimits(reader, writer, options);
//TransformationOptionPair pageLimits = limits.getPagesPair();
pdf = transformer.createPDFFromText(ir, pageLimit);
pdf = transformer.createPDFFromText(ir, pageLimit, pdfFont, fontSize);
pdf.save(os);
}
finally
Expand Down Expand Up @@ -231,22 +255,32 @@ static PDType1Font getStandardFont(String name)
}
//duplicating until here

private String fontName = null;
private boolean fontChanged = false;

// The following code is based on the code in TextToPDF with the addition of
// checks for page limits.
// The calling code must close the PDDocument once finished with it.
public PDDocument createPDFFromText(Reader text, int pageLimit)
public PDDocument createPDFFromText(Reader text, int pageLimit, String pdfFontName, Integer pdfFontSize)
throws IOException
{
PDDocument doc = null;
int pageCount = 0;
try
{
doc = new PDDocument();

final PDFont font = getFont(doc, pdfFontName);
final int fontSize = pdfFontSize != null ? pdfFontSize : getFontSize();

logger.debug("Going to use font " + font.getName() + " with size " + fontSize);

final int margin = 40;
float height = getFont().getFontDescriptor().getFontBoundingBox().getHeight() / 1000;
float height = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000;

//calculate font height and increase by 5 percent.
height = height * getFontSize() * 1.05f;
doc = new PDDocument();
height = height * fontSize * 1.05f;

BufferedReader data = (text instanceof BufferedReader) ? (BufferedReader) text : new BufferedReader(text);
String nextLine;
PDPage page = new PDPage();
Expand Down Expand Up @@ -280,8 +314,8 @@ public PDDocument createPDFFromText(Reader text, int pageLimit)
{
String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex];
lengthIfUsingNextWord =
(getFont().getStringWidth(
lineWithNextWord) / 1000) * getFontSize();
(font.getStringWidth(
lineWithNextWord) / 1000) * fontSize;
}
}
while (lineIndex < lineWords.length &&
Expand All @@ -304,7 +338,7 @@ public PDDocument createPDFFromText(Reader text, int pageLimit)
contentStream.close();
}
contentStream = new PDPageContentStream(doc, page);
contentStream.setFont(getFont(), getFontSize());
contentStream.setFont(font, fontSize);
contentStream.beginText();
y = page.getMediaBox().getHeight() - margin + height;
contentStream.moveTextPositionByAmount(margin, y);
Expand Down Expand Up @@ -344,6 +378,74 @@ public PDDocument createPDFFromText(Reader text, int pageLimit)
}
return doc;
}

public void setFont(String aFontName)
{
PDType1Font font = PagedTextToPDF.getStandardFont(aFontName);

if (font != null)
{
super.setFont(font);
this.fontChanged = true;
}
else
{
this.fontChanged = false;
}

this.fontName = aFontName;
}

private PDFont getFont(PDDocument doc, String name)
{
PDFont font = null;

if (name == null && !fontChanged)
{
name = fontName != null ? fontName : NOTOSANS_REGULAR;
}

try
{
if (name != null)
{
String location = "fonts" + System.getProperty("file.separator") + name + ".ttf";
ClassLoader loader = TextToPdfContentTransformer.class.getClassLoader();
File fontFile = null;

if (null != loader)
{
URL resource = loader.getResource(location);
if (resource != null)
{
String file = resource.getFile();
if (file != null && !file.isEmpty())
{
fontFile = new File(file);
}
}
}

if (null != fontFile)
{
PDDocument documentMock = new PDDocument();
font = PDType0Font.load(documentMock, fontFile);
}
}
}
catch (IOException e)
{
String msg = "Error loading font " + name + " :" + e.getMessage();
logger.error(msg, e);
}

if (font == null)
{
font = getFont();
}

return font;
}
}

private int parseInt(String s, String paramName)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
93 changes: 93 additions & 0 deletions engines/misc/src/main/resources/licenses/3rd-party/OFL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright 2015-2021 Google LLC. All Rights Reserved.

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.
4 changes: 3 additions & 1 deletion engines/misc/src/main/resources/misc_engine_config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"transformOptions": {
"textToPdfOptions": [
{"value": {"name": "pageLimit"}}
{"value": {"name": "pageLimit"}},
{"value": {"name": "pdfFont"}},
{"value": {"name": "pdfFontSize"}}
],
"stringOptions": [
{"value": {"name": "targetEncoding"}}
Expand Down
Loading

0 comments on commit 8240521

Please sign in to comment.