优化图片排版

pull/935/head
ag2s20150909 4 years ago
parent 296f839f7c
commit eee6570f32
  1. 2
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  2. 8
      epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java
  3. 16
      epublib/src/main/java/me/ag2s/epublib/util/StringUtil.java

@ -207,7 +207,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
//set css //set css
epubBook.resources.add( epubBook.resources.add(
Resource( Resource(
"body{background:white;margin:0;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(), "body,div{background:white;margin:0 auto;}padding:0;outline:none;width:100%;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{display:inline-block;width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(),
"css/style.css" "css/style.css"
) )
) )

@ -60,12 +60,18 @@ public class ResourceUtil {
private static String createHtml(String title, String txt) { private static String createHtml(String title, String txt) {
StringBuilder body = new StringBuilder(); StringBuilder body = new StringBuilder();
for (String s : txt.split("\\r?\\n")) { for (String s : txt.split("\\r?\\n")) {
s = s.trim(); s=StringUtil.FixTrim(s);
if (s.length() != 0) { if (s.length() != 0) {
if(s.contains("<img")){
//body.append("<div class=\"duokan-image-single img-note\">")
body.append(s);//.append("</div>");
}else {
body.append("<p>").append(s).append("</p>"); body.append("<p>").append(s).append("</p>");
} }
} }
}
String html = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; String html = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
html += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">"; html += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">";
html += "<head><title>" + title + "</title>" + html += "<head><title>" + title + "</title>" +

@ -3,6 +3,8 @@ package me.ag2s.epublib.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Various String utility functions. * Various String utility functions.
@ -271,4 +273,18 @@ public class StringUtil {
} }
return text.substring(cPos + 1); return text.substring(cPos + 1);
} }
public static String FixTrim(String s) {
if (s==null){
return "null";
}
Pattern r = Pattern.compile("^[\\s]{1,9}(.*?)[\\s]{1,9}$");
Matcher m = r.matcher(s);
if (m.find()) {
s= m.group(1);
}
//移除GBK中文全角空格
s = s.replace(" ", "");
return s;
}
} }

Loading…
Cancel
Save