Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

ADD font face selection #145

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@font-face {
font-family: 'Noto Serif';
src: url('Noto-Serif-italic.woff') format('woff');
font-weight: normal;
font-style: italic;
}

@font-face {
font-family: 'Noto Serif';
src: url('Noto-Serif-700.woff') format('woff');
font-weight: bold;
font-style: normal;
}

@font-face {
font-family: 'Noto Serif';
src: url('Noto-Serif-700italic.woff') format('woff');
font-weight: bold;
font-style: italic;
}

@font-face {
font-family: 'Noto Serif';
src: url('Noto-Serif-regular.woff') format('woff');
font-weight: normal;
font-style: normal;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@font-face {
font-family: 'Open Sans';
src: url('Open-Sans-italic.woff') format('woff');
font-weight: normal;
font-style: italic;
}

@font-face {
font-family: 'Open Sans';
src: url('Open-Sans-700.woff') format('woff');
font-weight: bold;
font-style: normal;
}

@font-face {
font-family: 'Open Sans';
src: url('Open-Sans-700italic.woff') format('woff');
font-weight: bold;
font-style: italic;
}

@font-face {
font-family: 'Open Sans';
src: url('Open-Sans-regular.woff') format('woff');
font-weight: normal;
font-style: normal;
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@font-face {
font-family: 'OpenDyslexic';
src:
local('OpenDyslexic'),
local('OpenDyslexic Regular'),
local('OpenDyslexic-Regular'),
url('OpenDyslexic-Regular.eot') format('embedded-opentype'),
url('OpenDyslexic-Regular.woff') format('woff'),
url('OpenDyslexic-Regular.ttf') format('truetype'),
url('OpenDyslexic-Regular.svg') format('svg');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'OpenDyslexic';
src:
local('OpenDyslexic Italic'),
local('OpenDyslexic-Italic'),
url('OpenDyslexic-Italic.eot') format('embedded-opentype'),
url('OpenDyslexic-Italic.woff') format('woff'),
url('OpenDyslexic-Italic.ttf') format('truetype'),
url('OpenDyslexic-Italic.svg') format('svg');
font-weight: normal;
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'OpenDyslexic';
src:
local('OpenDyslexic Bold'),
local('OpenDyslexic-Bold'),
url('OpenDyslexic-Bold.eot') format('embedded-opentype'),
url('OpenDyslexic-Bold.woff') format('woff'),
url('OpenDyslexic-Bold.ttf') format('truetype'),
url('OpenDyslexic-Bold.svg') format('svg');
font-weight: normal;
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'OpenDyslexic';
src:
local('OpenDyslexic Bold Italic'),
local('OpenDyslexic-BoldItalic'),
url('OpenDyslexic-BoldItalic.eot') format('embedded-opentype'),
url('OpenDyslexic-BoldItalic.woff') format('woff'),
url('OpenDyslexic-BoldItalic.ttf') format('truetype'),
url('OpenDyslexic-BoldItalic.svg') format('svg');
font-weight: normal;
font-weight: bold;
font-style: italic;
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,16 @@ require(['readium_shared_js/views/reader_view'], function (ReaderView)
};
}();

var fontsArray = [];
if (typeof readiumFontFaces != "undefined") { // Injected by WebViewActivity
fontsArray = readiumFontFaces;
}

var opts = {
needsFixedLayoutScalerWorkAround: true,
el: "#viewport",
annotationCSSUrl: '/readium_Annotations.css' //prefix + '/css/annotations.css'
annotationCSSUrl: '/readium_Annotations.css', //prefix + '/css/annotations.css'
fonts: fontsArray
};

ReadiumSDK.on(ReadiumSDK.Events.PLUGINS_LOADED, function(reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script src="builds/single-bundle/readium-shared-js_all.js" type="text/javascript"></script>

<script src="epubReadingSystem.js" type="text/javascript"></script>
<script src="font-faces/fonts.js" type="text/javascript"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the font-faces/fonts.js file missing from this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<script src="host_app_feedback.js" type="text/javascript"></script>

<link rel="stylesheet" type="text/css" href="builds/css/sdk.css" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,49 @@

package org.readium.sdk.android.launcher;

import org.readium.sdk.android.launcher.model.ViewerSettings;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;

import org.readium.sdk.android.launcher.model.Font;
import org.readium.sdk.android.launcher.model.ViewerSettings;

import java.util.ArrayList;
import java.util.List;

/**
* This dialog displays the viewer settings to the user.
* The model is represented by the class {@link ViewerSettings}
*
*/
public class ViewerSettingsDialog extends DialogFragment {

/**
* Interface to notify the listener when a viewer settings have been changed.
*/
public interface OnViewerSettingsChange {
public void onViewerSettingsChange(ViewerSettings settings);
}

protected static final String TAG = "ViewerSettingsDialog";

private OnViewerSettingsChange mListener;
protected static final String TAG = "ViewerSettingsDialog";
private final List<Font> mFontFaces;
private OnViewerSettingsChange mListener;
private ViewerSettings mOriginalSettings;

private ViewerSettings mOriginalSettings;

public ViewerSettingsDialog(OnViewerSettingsChange listener, ViewerSettings originalSettings) {
mListener = listener;
mOriginalSettings = originalSettings;
}
public ViewerSettingsDialog(OnViewerSettingsChange listener, ViewerSettings originalSettings, List<Font> fontFaces) {
mListener = listener;
mOriginalSettings = originalSettings;
mFontFaces = fontFaces;
}

@Override
public Dialog onCreateDialog(
Expand Down Expand Up @@ -102,6 +108,18 @@ public Dialog onCreateDialog(
break;
}

final Spinner fontSpinner = (Spinner) dialogView.findViewById(R.id.spinner);
List<String> fontDisplayName = new ArrayList<String>();
fontDisplayName.add("Default");
for (Font font : mFontFaces) {
fontDisplayName.add(font.getDisplayName());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, fontDisplayName);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fontSpinner.setAdapter(dataAdapter);
fontSpinner.setSelection(mOriginalSettings.getFontSelection());

final EditText fontSizeText = (EditText) dialogView.findViewById(R.id.fontSize);
fontSizeText.setText("" + mOriginalSettings.getFontSize());

Expand All @@ -115,6 +133,7 @@ public Dialog onCreateDialog(
@Override
public void onClick(DialogInterface dialog, int id) {
if (mListener != null) {
int fontSelection = fontSpinner.getSelectedItemPosition();
int fontSize = parseString(fontSizeText.getText().toString(), 100);
int columnGap = parseString(columnGapText.getText().toString(), 20);

Expand Down Expand Up @@ -144,7 +163,7 @@ public void onClick(DialogInterface dialog, int id) {
break;
}

ViewerSettings settings = new ViewerSettings(syntheticSpreadMode, scrollMode, fontSize, columnGap);
ViewerSettings settings = new ViewerSettings(syntheticSpreadMode, scrollMode, fontSelection, fontSize, columnGap);
mListener.onViewerSettingsChange(settings);
}
dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.readium.sdk.android.Container;
import org.readium.sdk.android.ManifestItem;
import org.readium.sdk.android.Package;
import org.readium.sdk.android.SpineItem;
import org.readium.sdk.android.launcher.model.BookmarkDatabase;
import org.readium.sdk.android.launcher.model.Font;
import org.readium.sdk.android.launcher.model.OpenPageRequest;
import org.readium.sdk.android.launcher.model.Page;
import org.readium.sdk.android.launcher.model.PaginationInfo;
Expand Down Expand Up @@ -207,6 +210,7 @@ public byte[] handle(byte[] data, String mime, String uriPath,
private Package mPackage;
private OpenPageRequest mOpenPageRequestData;
private TextView mPageInfo;
private ArrayList<Font> mFontArray;
private ViewerSettings mViewerSettings;
private ReadiumJSApi mReadiumJSApi;
private EpubServer mServer;
Expand Down Expand Up @@ -269,11 +273,18 @@ protected void onCreate(Bundle savedInstanceState) {
mPackage, quiet, dataPreProcessor);
mServer.startServer();

// Load the page skeleton
mWebview.loadUrl(READER_SKELETON);
mViewerSettings = new ViewerSettings(
ViewerSettings.SyntheticSpreadMode.AUTO,
ViewerSettings.ScrollMode.AUTO, 100, 20);
// Load the page skeleton
mWebview.loadUrl(READER_SKELETON);

ArrayList<Font> fontList = new ArrayList<>();
fontList.add(new Font("Open Dyslexic", "OpenDyslexic", "font-faces/OpenDyslexic/OpenDyslexic.css"));
fontList.add(new Font("Open Sans", "Open Sans", "font-faces/Open-Sans/Open-Sans.css"));
fontList.add(new Font("Noto Serif", "Noto Serif", "font-faces/Noto-Serif/Noto-Serif.css"));
mFontArray = fontList;

mViewerSettings = new ViewerSettings(
ViewerSettings.SyntheticSpreadMode.AUTO,
ViewerSettings.ScrollMode.AUTO, 0, 100, 20);

mReadiumJSApi = new ReadiumJSApi(new ReadiumJSApi.JSLoader() {
@Override
Expand Down Expand Up @@ -364,7 +375,7 @@ public void onClick(View v) {
private void showSettings() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
DialogFragment dialog = new ViewerSettingsDialog(this, mViewerSettings);
DialogFragment dialog = new ViewerSettingsDialog(this, mViewerSettings, mFontArray);
dialog.show(fm, "dialog");
fragmentTransaction.commit();
}
Expand Down Expand Up @@ -507,10 +518,35 @@ public WebResourceResponse shouldInterceptRequest(WebView view,
if (!quiet)
Log.d(TAG, "READER ASSET ...");

// reader.html
if (mime.equals("application/xhtml+xml")) {
mime = "text/html";
}
// Font injection
if (url.matches(ASSET_PREFIX + "\\/?font-faces\\/fonts.js")) {
if (!quiet)
Log.d(TAG, "readiumFontFaces inject ...");

if (mFontArray != null && mFontArray.size() > 0) {
try {
JSONArray jsonArray = new JSONArray();
for (Font fontFace : mFontArray) {
jsonArray.put(fontFace.toJSON());
}

String readiumFontFaces = "var readiumFontFaces =" + jsonArray.toString() + ";";

return new WebResourceResponse("text/javascript", UTF_8, new ByteArrayInputStream(readiumFontFaces.getBytes()));
} catch (JSONException e) {
return new WebResourceResponse(null, UTF_8,
new ByteArrayInputStream("".getBytes()));
}
} else {
return new WebResourceResponse(null, UTF_8,
new ByteArrayInputStream("".getBytes()));
}
}

// reader.html
if (mime.equals("application/xhtml+xml")) {
mime = "text/html";
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Tabulations vs. space characters! :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies, I chose the wrong option on Android Studio

InputStream is = null;
try {
Expand Down Expand Up @@ -538,12 +574,12 @@ public WebResourceResponse shouldInterceptRequest(WebView view,
// iframe(s)

evaluateJavascript(INJECT_EPUB_RSO_SCRIPT_2);

return new WebResourceResponse("text/javascript", UTF_8,
new ByteArrayInputStream(
"(function(){})()".getBytes()));
}

if (cleanedUrl.matches("\\/?readium_MathJax.js")) {
if (!quiet)
Log.d(TAG, "MathJax.js inject ...");
Expand Down Expand Up @@ -580,6 +616,25 @@ public WebResourceResponse shouldInterceptRequest(WebView view,
return new WebResourceResponse("text/css", UTF_8, is);
}

if (cleanedUrl.matches(".*\\/?font-faces\\/.*")) {
if (!quiet)
Log.d(TAG, "font-face inject ...");

String assetPath = cleanedUrl.substring(cleanedUrl.lastIndexOf("font-faces/"));

InputStream is = null;
try {
is = getAssets().open(assetPath);
} catch (IOException e) {

Log.e(TAG, "font-face asset fail!");

return super.shouldInterceptRequest(view, url);
}

return new WebResourceResponse(null, UTF_8, is);
}


ManifestItem item = mPackage.getManifestItem(cleanedUrl);
String contentType = item != null ? item.getMediaType() : null;
Expand Down
Loading