Skip to content

Commit

Permalink
For #28 - Let user specify custom text transformers.
Browse files Browse the repository at this point in the history
Text transformers are currently toUpper, toTitle and toLower.
  • Loading branch information
danfickle committed Jun 29, 2016
1 parent f138331 commit f1597aa
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 93 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ CHANGELOG

head - 0.0.1-RC5-SNAPSHOT
========
+ BREAKING CHANGE: Changed bi-directional method names in builder to be more consistent.
+ [Add method to builder to specify custom text transformers](https://github.com/danfickle/openhtmltopdf/issues/28)
+ [Add method to builder to specify a custom line breaker](https://github.com/danfickle/openhtmltopdf/issues/25) Thanks @Magotchi

0.0.1-RC4
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.openhtmltopdf.extend;

/**
* Transforms text, such as making it title case.
*/
public interface FSTextTransformer {
public String transform(String in);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.openhtmltopdf.css.value.FontSpecification;
import com.openhtmltopdf.extend.FSCanvas;
import com.openhtmltopdf.extend.FSTextBreaker;
import com.openhtmltopdf.extend.FSTextTransformer;
import com.openhtmltopdf.extend.FontContext;
import com.openhtmltopdf.extend.FontResolver;
import com.openhtmltopdf.extend.NamespaceHandler;
Expand Down Expand Up @@ -130,6 +131,10 @@ public class SharedContext {

private String replacementText = "#";
private FSTextBreaker lineBreaker = new UrlAwareLineBreakIterator(BreakIterator.getLineInstance(Locale.US));

private FSTextTransformer _unicodeToLowerTransformer = new TextUtil.DefaultToLowerTransformer(Locale.US);
private FSTextTransformer _unicodeToUpperTransformer = new TextUtil.DefaultToUpperTransformer(Locale.US);
private FSTextTransformer _unicodeToTitleTransformer = new TextUtil.DefaultToTitleTransformer();

public SharedContext() {
}
Expand Down Expand Up @@ -661,6 +666,30 @@ public void registerWithThread() {
public void removeFromThread() {
ThreadCtx.get().setSharedContext(null);
}

public FSTextTransformer getUnicodeToLowerTransformer() {
return this._unicodeToLowerTransformer;
}

public FSTextTransformer getUnicodeToUpperTransformer() {
return this._unicodeToUpperTransformer;
}

public FSTextTransformer getUnicodeToTitleTransformer() {
return this._unicodeToTitleTransformer;
}

public void setUnicodeToLowerTransformer(FSTextTransformer tr) {
this._unicodeToLowerTransformer = tr;
}

public void setUnicodeToUpperTransformer(FSTextTransformer tr) {
this._unicodeToUpperTransformer = tr;
}

public void setUnicodeToTitleTransformer(FSTextTransformer tr) {
this._unicodeToTitleTransformer = tr;
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
*/
package com.openhtmltopdf.layout;

import java.util.Locale;

import com.openhtmltopdf.css.constants.CSSName;
import com.openhtmltopdf.css.constants.IdentValue;
import com.openhtmltopdf.css.style.CalculatedStyle;
import com.openhtmltopdf.extend.FSTextTransformer;
import com.openhtmltopdf.util.ThreadCtx;
import com.openhtmltopdf.util.Uu;


Expand All @@ -31,28 +35,80 @@
* @author empty
*/
public class TextUtil {
public static class DefaultToUpperTransformer implements FSTextTransformer {
private final Locale lc;

public DefaultToUpperTransformer(Locale lc) {
this.lc = lc;
}

@Override
public String transform(String in) {
return in.toUpperCase(lc);
}
}

public static class DefaultToLowerTransformer implements FSTextTransformer {
private final Locale lc;

public DefaultToLowerTransformer(Locale lc) {
this.lc = lc;
}

@Override
public String transform(String in) {
return in.toLowerCase(lc);
}
}

/**
* A best effort implementation of title casing. Use the implementation in the rtl-support
* module for better results.
*/
public static class DefaultToTitleTransformer implements FSTextTransformer {
public DefaultToTitleTransformer() { }

@Override
public String transform(String in) {
StringBuilder out = new StringBuilder(in.length());
boolean makeTitle = true;

for (int i = 0; i < in.length(); ) {
int cp = in.codePointAt(i);

if (Character.isLetter(cp) && makeTitle) {
out.appendCodePoint(Character.toTitleCase(cp));
makeTitle = false;
} else if (Character.isWhitespace(cp) || Character.isSpaceChar(cp)) {
out.appendCodePoint(cp);
makeTitle = true;
} else {
out.appendCodePoint(cp);
}

i += Character.charCount(cp);
}

return out.toString();
}
}

/**
* Description of the Method
*
* @param text PARAM
* @param style
* @return Returns
*/
public static String transformText( String text, CalculatedStyle style ) {
IdentValue transform = style.getIdent( CSSName.TEXT_TRANSFORM );
SharedContext ctx = ThreadCtx.get().sharedContext();

if ( transform == IdentValue.LOWERCASE ) {
text = text.toLowerCase();
text = ctx.getUnicodeToLowerTransformer().transform(text);
}
if ( transform == IdentValue.UPPERCASE ) {
text = text.toUpperCase();
text = ctx.getUnicodeToUpperTransformer().transform(text);
}
if ( transform == IdentValue.CAPITALIZE ) {
text = capitalizeWords( text );
text = ctx.getUnicodeToTitleTransformer().transform(text);
}
IdentValue fontVariant = style.getIdent( CSSName.FONT_VARIANT );
if ( fontVariant == IdentValue.SMALL_CAPS ) {
text = text.toUpperCase();
text = ctx.getUnicodeToUpperTransformer().transform(text);
}
return text;
}
Expand Down Expand Up @@ -125,49 +181,6 @@ public static boolean isFirstLetterSeparatorChar( char c ) {
return false;
}
}


/**
* Description of the Method
*
* @param text PARAM
* @return Returns
*/
private static String capitalizeWords( String text ) {
//Uu.p("start = -"+text+"-");
if ( text.length() == 0 ) {
return text;
}

StringBuffer sb = new StringBuffer();
//Uu.p("text = -" + text + "-");

// do first letter
//Uu.p("first = " + text.substring(0,1));
boolean cap = true;
for ( int i = 0; i < text.length(); i++ ) {
String ch = text.substring( i, i + 1 );
//Uu.p("ch = " + ch + " cap = " + cap);


if ( cap ) {
sb.append( ch.toUpperCase() );
} else {
sb.append( ch );
}
cap = false;
if ( ch.equals( " " ) ) {
cap = true;
}
}

//Uu.p("final = -"+sb.toString()+"-");
if ( sb.toString().length() != text.length() ) {
Uu.p( "error! to strings arent the same length = -" + sb.toString() + "-" + text + "-" );
}
return sb.toString();
}

}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ public void exportToPdfBox( String path )
os = new FileOutputStream(path);
try {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
builder.useUnicodeBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
builder.defaultTextDirection(TextDirection.LTR);
builder.useBidiReorderer(new ICUBidiReorderer());
builder.useUnicodeBidiReorderer(new ICUBidiReorderer());
builder.withUri(manager.getBaseURL());
builder.toStream(os);
builder.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static void runTestCase(String testCaseFile) throws Exception {

try {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
builder.useBidiReorderer(new ICUBidiReorderer());
builder.useUnicodeBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
builder.useUnicodeBidiReorderer(new ICUBidiReorderer());
builder.defaultTextDirection(TextDirection.LTR);
builder.withHtmlContent(html, TestcaseRunner.class.getResource("/testcases/").toString());
builder.toStream(outputStream);
Expand Down
Loading

0 comments on commit f1597aa

Please sign in to comment.