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 - uses: actions/checkout@v3
with: with:
submodules: true submodules: true
- name: Set up JDK 18 - name: Set up JDK 19
uses: actions/setup-java@v3 uses: actions/setup-java@v3
with: with:
distribution: 'temurin' distribution: 'temurin'
java-version: 18 java-version: 19
cache: gradle cache: gradle
- name: Build with Gradle - name: Build with Gradle
run: ./gradlew build --no-daemon run: ./gradlew build --no-daemon

View File

@ -76,5 +76,5 @@ android {
dependencies { dependencies {
implementation("androidx.appcompat:appcompat:1.5.1") 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 { body, canvas {
padding: 0; padding: 0;
margin: 0; margin: 0;
} }
canvas { body {
margin: auto; background-color: #c0c0c0;
display: block; }
#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 { .textLayer {
position: absolute;
text-align: initial; text-align: initial;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: hidden; overflow: hidden;
opacity: 0.2; opacity: 0.2;
line-height: 1; line-height: 1;

View File

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

View File

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

View File

@ -216,6 +216,7 @@ public class PdfViewer implements LoaderManager.LoaderCallbacks<List<CharSequenc
settings.setAllowFileAccess(false); settings.setAllowFileAccess(false);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setJavaScriptEnabled(true); settings.setJavaScriptEnabled(true);
settings.setMinimumFontSize(1);
CookieManager.getInstance().setAcceptCookie(false); CookieManager.getInstance().setAcceptCookie(false);
@ -460,12 +461,12 @@ public class PdfViewer implements LoaderManager.LoaderCallbacks<List<CharSequenc
} }
private void showSystemUi() { private void showSystemUi() {
ViewKt.showSystemUi(binding.getRoot()); ViewKt.showSystemUi(binding.getRoot(), activity.getWindow());
activity.getSupportActionBar().show(); activity.getSupportActionBar().show();
} }
private void hideSystemUi() { private void hideSystemUi() {
ViewKt.hideSystemUi(binding.getRoot()); ViewKt.hideSystemUi(binding.getRoot(), activity.getWindow());
activity.getSupportActionBar().hide(); activity.getSupportActionBar().hide();
} }
@ -481,12 +482,12 @@ public class PdfViewer implements LoaderManager.LoaderCallbacks<List<CharSequenc
mToast.show(); mToast.show();
} }
public void onCreateOptionMenu(Menu menu) { public void onCreateOptionMenu(@NonNull Menu menu) {
MenuInflater inflater = activity.getMenuInflater(); MenuInflater inflater = activity.getMenuInflater();
inflater.inflate(R.menu.pdf_viewer, menu); 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, 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_first, R.id.action_last, R.id.action_rotate_clockwise,
R.id.action_rotate_counterclockwise, R.id.action_view_document_properties}; 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)"; 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) // Parse date as per PDF spec (complies with PDF v1.4 to v1.7)
public static String parseDate(String date) throws ParseException { public static String parseDate(String date) throws ParseException {
int position = 0; int position = 0;
@ -45,7 +53,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid year", position); throw new ParseException("Invalid year", position);
} }
int year = Integer.parseInt(field); int year = parseIntSafely(field);
if (year > currentYear) { if (year > currentYear) {
year = currentYear; year = currentYear;
} }
@ -66,7 +74,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid month", position); throw new ParseException("Invalid month", position);
} }
month = Integer.parseInt(field) - 1; month = parseIntSafely(field) - 1;
if (month > 11) { if (month > 11) {
throw new ParseException("Invalid month", position); throw new ParseException("Invalid month", position);
} }
@ -77,7 +85,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid day", position); throw new ParseException("Invalid day", position);
} }
day = Integer.parseInt(field); day = parseIntSafely(field);
if (day > 31) { if (day > 31) {
throw new ParseException("Invalid day", position); throw new ParseException("Invalid day", position);
} }
@ -88,7 +96,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid hours", position); throw new ParseException("Invalid hours", position);
} }
hours = Integer.parseInt(field); hours = parseIntSafely(field);
if (hours > 23) { if (hours > 23) {
throw new ParseException("Invalid hours", position); throw new ParseException("Invalid hours", position);
} }
@ -99,7 +107,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid minutes", position); throw new ParseException("Invalid minutes", position);
} }
minutes = Integer.parseInt(field); minutes = parseIntSafely(field);
if (minutes > 59) { if (minutes > 59) {
throw new ParseException("Invalid minutes", position); throw new ParseException("Invalid minutes", position);
} }
@ -110,7 +118,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid seconds", position); throw new ParseException("Invalid seconds", position);
} }
seconds = Integer.parseInt(field); seconds = parseIntSafely(field);
if (seconds > 59) { if (seconds > 59) {
throw new ParseException("Invalid seconds", position); throw new ParseException("Invalid seconds", position);
} }
@ -134,7 +142,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid UTC offset hours", position); throw new ParseException("Invalid UTC offset hours", position);
} }
offsetHours = Integer.parseInt(field); offsetHours = parseIntSafely(field);
final int offsetHoursMinutes = offsetHours * 100 + offsetMinutes; final int offsetHoursMinutes = offsetHours * 100 + offsetMinutes;
// Validate UTC offset (UTC-12:00 to UTC+14:00) // Validate UTC offset (UTC-12:00 to UTC+14:00)
@ -157,7 +165,7 @@ public class Utils {
if (!TextUtils.isDigitsOnly(field)) { if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid UTC offset minutes", position); throw new ParseException("Invalid UTC offset minutes", position);
} }
offsetMinutes = Integer.parseInt(field); offsetMinutes = parseIntSafely(field);
if (offsetMinutes > 59) { if (offsetMinutes > 59) {
throw new ParseException("Invalid UTC offset minutes", position); throw new ParseException("Invalid UTC offset minutes", position);
} }

View File

@ -1,19 +1,20 @@
package app.grapheneos.pdfviewer.ktx package app.grapheneos.pdfviewer.ktx
import android.view.View 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.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat import androidx.core.view.WindowInsetsControllerCompat
private val systemBars = WindowInsetsCompat.Type.statusBars() private val systemBars = WindowInsetsCompat.Type.statusBars()
fun View.hideSystemUi() { fun View.hideSystemUi(window: Window) {
val controller = ViewCompat.getWindowInsetsController(this) ?: return val controller = WindowCompat.getInsetsController(window, this)
controller.systemBarsBehavior = controller.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller.hide(systemBars) controller.hide(systemBars)
} }
fun View.showSystemUi() { fun View.showSystemUi(window: Window) {
ViewCompat.getWindowInsetsController(this)?.show(systemBars) 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 SpannableStringBuilder property = new SpannableStringBuilder(name).append(":\n");
final String value = json != null ? json.optString(specName, "-") : specName; final String value = json != null ? json.optString(specName, "-") : specName;
if (specName.endsWith("Date")) { if (specName != null && specName.endsWith("Date")) {
final Context context = getContext(); final Context context = getContext();
try { try {
property.append(value.equals("-") ? value : Utils.parseDate(value)); property.append(value.equals("-") ? value : Utils.parseDate(value));

View File

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