libpdfviewer: update to PdfViewer 16

This commit is contained in:
Matéo Duparc 2023-02-01 22:52:07 +01:00
commit 6e8fdb56a5
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A
11 changed files with 63 additions and 35 deletions

View File

@ -10,11 +10,11 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: true
- name: Set up JDK 18
- name: Set up JDK 19
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 18
java-version: 19
cache: gradle
- name: Build with Gradle
run: ./gradlew build --no-daemon

View File

@ -76,5 +76,5 @@ android {
dependencies {
implementation("androidx.appcompat:appcompat:1.5.1")
implementation("com.google.android.material:material:1.6.1")
implementation("com.google.android.material:material:1.7.0")
}

@ -1 +1 @@
Subproject commit cc3d3bf299ae11f8f72ae8d64cbf19b340f9a996
Subproject commit 5f07d5a4159bb99eee2f6143d1297f03b45bba58

View File

@ -1,20 +1,36 @@
html, body {
height: 100%;
}
body, canvas {
padding: 0;
margin: 0;
}
canvas {
margin: auto;
display: block;
body {
background-color: #c0c0c0;
}
#container {
width: 100%;
height: 100%;
display: grid;
place-items: center;
}
#container canvas, #container .textLayer {
/* overlay child elements on top of each other */
grid-row-start: 1;
grid-column-start: 1;
}
canvas, .textLayer {
display: inline-block;
position: relative;
}
.textLayer {
position: absolute;
text-align: initial;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: hidden;
opacity: 0.2;
line-height: 1;

View File

@ -7,8 +7,10 @@
<script src="pdf.js"></script>
</head>
<body>
<div id="container">
<canvas id="content"></canvas>
<div id="text" class="textLayer"></div>
</div>
<script src="viewer.js"></script>
</body>
</html>

View File

@ -73,7 +73,7 @@ function renderPage(pageNumber, zoom, prerender, prerenderTrigger=0) {
for (let i = 0; i < cache.length; i++) {
const cached = cache[i];
if (cached.pageNumber === pageNumber && cached.zoomRatio === newZoomRatio &&
cache.orientationDegrees === orientationDegrees) {
cached.orientationDegrees === orientationDegrees) {
if (useRender) {
cache.splice(i, 1);
cache.push(cached);

View File

@ -216,6 +216,7 @@ public class PdfViewer implements LoaderManager.LoaderCallbacks<List<CharSequenc
settings.setAllowFileAccess(false);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setJavaScriptEnabled(true);
settings.setMinimumFontSize(1);
CookieManager.getInstance().setAcceptCookie(false);
@ -460,12 +461,12 @@ public class PdfViewer implements LoaderManager.LoaderCallbacks<List<CharSequenc
}
private void showSystemUi() {
ViewKt.showSystemUi(binding.getRoot());
ViewKt.showSystemUi(binding.getRoot(), activity.getWindow());
activity.getSupportActionBar().show();
}
private void hideSystemUi() {
ViewKt.hideSystemUi(binding.getRoot());
ViewKt.hideSystemUi(binding.getRoot(), activity.getWindow());
activity.getSupportActionBar().hide();
}
@ -481,12 +482,12 @@ public class PdfViewer implements LoaderManager.LoaderCallbacks<List<CharSequenc
mToast.show();
}
public void onCreateOptionMenu(Menu menu) {
public void onCreateOptionMenu(@NonNull Menu menu) {
MenuInflater inflater = activity.getMenuInflater();
inflater.inflate(R.menu.pdf_viewer, menu);
}
public boolean onPrepareOptionsMenu(Menu menu) {
public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
final int[] ids = {R.id.action_jump_to_page, R.id.action_next, R.id.action_previous,
R.id.action_first, R.id.action_last, R.id.action_rotate_clockwise,
R.id.action_rotate_counterclockwise, R.id.action_view_document_properties};

View File

@ -25,6 +25,14 @@ public class Utils {
return format.format(kb / 1000) + " MB (" + fileSize + " Bytes)";
}
private static int parseIntSafely(String field) throws ParseException {
try {
return Integer.parseInt(field);
} catch (NumberFormatException e) {
throw new ParseException("Error while parsing int", -1);
}
}
// Parse date as per PDF spec (complies with PDF v1.4 to v1.7)
public static String parseDate(String date) throws ParseException {
int position = 0;
@ -45,7 +53,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid year", position);
}
int year = Integer.parseInt(field);
int year = parseIntSafely(field);
if (year > currentYear) {
year = currentYear;
}
@ -66,7 +74,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid month", position);
}
month = Integer.parseInt(field) - 1;
month = parseIntSafely(field) - 1;
if (month > 11) {
throw new ParseException("Invalid month", position);
}
@ -77,7 +85,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid day", position);
}
day = Integer.parseInt(field);
day = parseIntSafely(field);
if (day > 31) {
throw new ParseException("Invalid day", position);
}
@ -88,7 +96,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid hours", position);
}
hours = Integer.parseInt(field);
hours = parseIntSafely(field);
if (hours > 23) {
throw new ParseException("Invalid hours", position);
}
@ -99,7 +107,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid minutes", position);
}
minutes = Integer.parseInt(field);
minutes = parseIntSafely(field);
if (minutes > 59) {
throw new ParseException("Invalid minutes", position);
}
@ -110,7 +118,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid seconds", position);
}
seconds = Integer.parseInt(field);
seconds = parseIntSafely(field);
if (seconds > 59) {
throw new ParseException("Invalid seconds", position);
}
@ -134,7 +142,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid UTC offset hours", position);
}
offsetHours = Integer.parseInt(field);
offsetHours = parseIntSafely(field);
final int offsetHoursMinutes = offsetHours * 100 + offsetMinutes;
// Validate UTC offset (UTC-12:00 to UTC+14:00)
@ -157,7 +165,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid UTC offset minutes", position);
}
offsetMinutes = Integer.parseInt(field);
offsetMinutes = parseIntSafely(field);
if (offsetMinutes > 59) {
throw new ParseException("Invalid UTC offset minutes", position);
}

View File

@ -1,19 +1,20 @@
package app.grapheneos.pdfviewer.ktx
import android.view.View
import androidx.core.view.ViewCompat
import android.view.Window
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
private val systemBars = WindowInsetsCompat.Type.statusBars()
fun View.hideSystemUi() {
val controller = ViewCompat.getWindowInsetsController(this) ?: return
fun View.hideSystemUi(window: Window) {
val controller = WindowCompat.getInsetsController(window, this)
controller.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller.hide(systemBars)
}
fun View.showSystemUi() {
ViewCompat.getWindowInsetsController(this)?.show(systemBars)
fun View.showSystemUi(window: Window) {
WindowCompat.getInsetsController(window, this).show(systemBars)
}

View File

@ -98,7 +98,7 @@ public class DocumentPropertiesLoader extends AsyncTaskLoader<List<CharSequence>
final SpannableStringBuilder property = new SpannableStringBuilder(name).append(":\n");
final String value = json != null ? json.optString(specName, "-") : specName;
if (specName.endsWith("Date")) {
if (specName != null && specName.endsWith("Date")) {
final Context context = getContext();
try {
property.append(value.equals("-") ? value : Utils.parseDate(value));

View File

@ -10,8 +10,8 @@ buildscript {
}
}
dependencies {
classpath("com.android.tools.build:gradle:7.3.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20")
classpath("com.android.tools.build:gradle:7.3.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.22")
}
}