|
4 | 4 | import android.content.Context; |
5 | 5 | import android.net.Uri; |
6 | 6 | import android.util.AttributeSet; |
| 7 | +import android.util.Log; |
7 | 8 | import android.util.Pair; |
8 | 9 | import android.view.Choreographer; |
9 | 10 | import android.view.View; |
|
13 | 14 | import androidx.annotation.Nullable; |
14 | 15 | import androidx.fragment.app.FragmentManager; |
15 | 16 |
|
| 17 | +import com.facebook.react.bridge.ReadableArray; |
16 | 18 | import com.facebook.react.bridge.ReadableMap; |
17 | 19 | import com.facebook.react.common.MapBuilder; |
18 | 20 | import com.facebook.react.uimanager.events.EventDispatcher; |
| 21 | +import com.pspdfkit.PSPDFKit; |
19 | 22 | import com.pspdfkit.annotations.Annotation; |
| 23 | +import com.pspdfkit.annotations.AnnotationType; |
| 24 | +import com.pspdfkit.annotations.configuration.FreeTextAnnotationConfiguration; |
20 | 25 | import com.pspdfkit.configuration.activity.PdfActivityConfiguration; |
21 | 26 | import com.pspdfkit.document.PdfDocument; |
22 | 27 | import com.pspdfkit.document.PdfDocumentLoader; |
|
42 | 47 | import com.pspdfkit.ui.PdfFragment; |
43 | 48 | import com.pspdfkit.ui.PdfUiFragment; |
44 | 49 | import com.pspdfkit.ui.PdfUiFragmentBuilder; |
| 50 | +import com.pspdfkit.ui.fonts.Font; |
| 51 | +import com.pspdfkit.ui.fonts.FontManager; |
45 | 52 | import com.pspdfkit.ui.search.PdfSearchView; |
46 | 53 | import com.pspdfkit.ui.search.PdfSearchViewInline; |
47 | | -import com.pspdfkit.ui.toolbar.MainToolbar; |
| 54 | +import com.pspdfkit.ui.special_mode.controller.AnnotationTool; |
48 | 55 | import com.pspdfkit.ui.toolbar.grouping.MenuItemGroupingRule; |
49 | 56 |
|
50 | 57 | import org.json.JSONArray; |
@@ -84,6 +91,7 @@ public class PdfView extends FrameLayout { |
84 | 91 |
|
85 | 92 | /** Key to use when setting the id argument of PdfFragments created by this PdfView. */ |
86 | 93 | private static final String ARG_ROOT_ID = "root_id"; |
| 94 | + private static final String TAG = "PdfView"; |
87 | 95 |
|
88 | 96 | private FragmentManager fragmentManager; |
89 | 97 | private EventDispatcher eventDispatcher; |
@@ -116,6 +124,18 @@ public class PdfView extends FrameLayout { |
116 | 124 | /** We keep track if the inline search view is shown since we don't want to add a second navigation button while it is shown. */ |
117 | 125 | private boolean isSearchViewShown = false; |
118 | 126 |
|
| 127 | + /** Disposable keeping track of our subscription to update the annotation configuration on each emitted PdfFragment. */ |
| 128 | + @Nullable |
| 129 | + private Disposable updateAnnotationConfigurationDisposable; |
| 130 | + |
| 131 | + /** The currently configured array of available font names for free text annotations. */ |
| 132 | + @Nullable |
| 133 | + private ReadableArray availableFontNames; |
| 134 | + |
| 135 | + /** The currently configured default font name for free text annotations. */ |
| 136 | + @Nullable |
| 137 | + private String selectedFontName; |
| 138 | + |
119 | 139 | public PdfView(@NonNull Context context) { |
120 | 140 | super(context); |
121 | 141 | init(); |
@@ -233,6 +253,63 @@ public void setMenuItemGroupingRule(@NonNull MenuItemGroupingRule groupingRule) |
233 | 253 | pdfViewModeController.setMenuItemGroupingRule(groupingRule); |
234 | 254 | } |
235 | 255 |
|
| 256 | + public void setAvailableFontNames(@Nullable final ReadableArray availableFontNames) { |
| 257 | + this.availableFontNames = availableFontNames; |
| 258 | + updateAnnotationConfiguration(); |
| 259 | + } |
| 260 | + |
| 261 | + public void setSelectedFontName(@Nullable final String selectedFontName) { |
| 262 | + this.selectedFontName = selectedFontName; |
| 263 | + updateAnnotationConfiguration(); |
| 264 | + } |
| 265 | + |
| 266 | + private void updateAnnotationConfiguration() { |
| 267 | + if (updateAnnotationConfigurationDisposable != null) { |
| 268 | + updateAnnotationConfigurationDisposable.dispose(); |
| 269 | + } |
| 270 | + |
| 271 | + // First we create the new FreeTextAnnotationConfiguration. |
| 272 | + FreeTextAnnotationConfiguration.Builder builder = FreeTextAnnotationConfiguration.builder(getContext()); |
| 273 | + FontManager systemFontManager = PSPDFKit.getSystemFontManager(); |
| 274 | + if (availableFontNames != null) { |
| 275 | + // Custom list of available fonts is set. |
| 276 | + final ArrayList<Font> availableFonts = new ArrayList<>(); |
| 277 | + for (int i = 0; i < availableFontNames.size(); i++) { |
| 278 | + final String fontName = availableFontNames.getString(i); |
| 279 | + final Font font = systemFontManager.getFontByName(fontName); |
| 280 | + if (font != null) { |
| 281 | + availableFonts.add(font); |
| 282 | + } else { |
| 283 | + Log.w(TAG, String.format("Failed to add font %s to list of available fonts since it wasn't found in the list of system fonts.", fontName)); |
| 284 | + } |
| 285 | + } |
| 286 | + builder.setAvailableFonts(availableFonts); |
| 287 | + } |
| 288 | + |
| 289 | + if (selectedFontName != null) { |
| 290 | + final Font defaultFont = systemFontManager.getFontByName(selectedFontName); |
| 291 | + if (defaultFont != null) { |
| 292 | + builder.setDefaultFont(defaultFont); |
| 293 | + } else { |
| 294 | + Log.w(TAG, String.format("Failed to set default font to %s since it wasn't found in the list of system fonts.", selectedFontName)); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + final FreeTextAnnotationConfiguration configuration = builder.build(); |
| 299 | + // We want to set this on the current PdfFragment and all future ones. |
| 300 | + // We use the observable emitting PdfFragments for this purpose. |
| 301 | + updateAnnotationConfigurationDisposable = getPdfFragment() |
| 302 | + .observeOn(AndroidSchedulers.mainThread()) |
| 303 | + .subscribe(pdfFragment -> { |
| 304 | + pdfFragment.getAnnotationConfiguration().put( |
| 305 | + AnnotationTool.FREETEXT,configuration); |
| 306 | + pdfFragment.getAnnotationConfiguration().put( |
| 307 | + AnnotationType.FREETEXT,configuration); |
| 308 | + pdfFragment.getAnnotationConfiguration().put( |
| 309 | + AnnotationTool.FREETEXT_CALLOUT,configuration); |
| 310 | + }); |
| 311 | + } |
| 312 | + |
236 | 313 | public void setShowNavigationButtonInToolbar(final boolean showNavigationButtonInToolbar) { |
237 | 314 | isNavigationButtonShown = showNavigationButtonInToolbar; |
238 | 315 | pendingFragmentActions.add(getCurrentPdfUiFragment() |
|
0 commit comments