Skip to content

Commit

Permalink
Set font-family via CSS style, not via JS
Browse files Browse the repository at this point in the history
Also, make font-family option available only in Settings
  • Loading branch information
di72nn committed Nov 30, 2015
1 parent 048935c commit 7af9c60
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 76 deletions.
3 changes: 0 additions & 3 deletions app/src/main/assets/base.js

This file was deleted.

4 changes: 4 additions & 0 deletions app/src/main/assets/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ body {
background: #000;
}

.serif-font {
font-family: serif;
}

/* ==========================================================================
1 = Style Guide
========================================================================== */
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ body {
font-weight: 600;
}

.serif-font {
font-family: serif;
}

/* ==========================================================================
1 = Style Guide
========================================================================== */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public class ReadArticleActivity extends BaseActionBarActivity {
private Settings settings;

private int fontSize = 100;
private boolean serifFont;

public void onCreate(Bundle savedInstanceState) {
Themes.applyTheme(this);
Expand Down Expand Up @@ -132,14 +131,14 @@ public void onCreate(Bundle savedInstanceState) {
}

fontSize = settings.getInt(Settings.FONT_SIZE, fontSize);
serifFont = settings.getBoolean(Settings.SERIF_FONT, serifFont);
boolean serifFont = settings.getBoolean(Settings.SERIF_FONT, false);

if(fontSize < 5) fontSize = 100; // TODO: remove: temp hack for compatibility

List<String> additionalClasses = new ArrayList<>(1);
if(highContrast) additionalClasses.add("high-contrast");
if(serifFont) additionalClasses.add("serif-font");

// TODO: remove?
String classAttr;
if(!additionalClasses.isEmpty()) {
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -345,22 +344,14 @@ private void loadingFinished() {
private boolean applyDisplaySettings() {
prepareToRestorePosition(false);

boolean result = false;
boolean changed = false;

if(fontSize != 100) {
result = true;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
setFontSizeNew(webViewContent, fontSize);
} else {
setFontSizeOld(webViewContent, fontSize);
}
}
if(serifFont) {
result = true;
setSerifFont(webViewContent, serifFont);
changed = true;
setFontSize(webViewContent, fontSize);
}

return result;
return changed;
}

private boolean openUrl(final String url) {
Expand Down Expand Up @@ -522,9 +513,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.menuDecreaseFontSize:
changeFontSize(false);
return true;
case R.id.menuSwitchFontFamily:
changeFontFamily();
return true;
default:
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -661,70 +649,31 @@ private void changeFontSize(boolean increase) {

int step = 5;
fontSize += step * (increase ? 1 : -1);
if(!increase && fontSize < 5) fontSize = 5;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
setFontSizeNew(webViewContent, fontSize);
} else {
setFontSizeOld(webViewContent, fontSize);
}
setFontSize(webViewContent, fontSize);

settings.setInt(Settings.FONT_SIZE, fontSize);

restorePositionAfterUpdate();
}

private void changeFontFamily() {
setFontFamily(!serifFont);
}

private void setFontFamily(boolean serif) {
prepareToRestorePosition(true);

serifFont = serif;

setSerifFont(webViewContent, serifFont);

settings.setBoolean(Settings.SERIF_FONT, serifFont);

restorePositionAfterUpdate();
private void setFontSize(WebView view, int size) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
setFontSizeNew(view, size);
} else {
setFontSizeOld(view, size);
}
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setFontSizeNew(WebView view, int size) {
webViewContent.getSettings().setTextZoom(size);
view.getSettings().setTextZoom(size);
}

@TargetApi(Build.VERSION_CODES.FROYO)
private void setFontSizeOld(WebView view, int size) {
webViewContent.getSettings().setDefaultFontSize(size);
}

private static void setSerifFont(WebView view, boolean flag) {
callJavaScript(view, "setSerifFontFamily", flag);
}

private static void callJavaScript(WebView webView, String methodName, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append("try{").append(methodName).append("(");
boolean first = true;
for(Object param: params) {
if(first) first = false;
else sb.append(", ");

if(param instanceof String) {
sb.append("'").append(param).append("'");
} else {
sb.append(param);
}
}
sb.append(")}catch(error){console.error(error.message);}");
String call = sb.toString();
Log.v(TAG, "callJavaScript: call=" + call);

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript(call, null);
} else {
webView.loadUrl("javascript:" + call);
}
view.getSettings().setDefaultFontSize(size);
}

private Drawable getIcon(int id, Resources.Theme theme) {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/res/menu/option_article.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@
<item
android:id="@+id/menuDecreaseFontSize"
android:title="@string/menu_readArticle_decreaseFontSize" />
<item
android:id="@+id/menuSwitchFontFamily"
android:title="@string/menu_readArticle_switchFontFamily" />
</menu>
3 changes: 1 addition & 2 deletions app/src/main/res/raw/webview_htmlbase.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
<meta charset="utf-8">
<link rel="stylesheet" href="%1$s.css" media="all" id="main-theme">
<link rel="stylesheet" href="ratatouille.css" media="all" id="extra-theme">
<script src="base.js"></script>
</head>
<div id="main">
<body id="html-base-body" %2$s>
<body %2$s>
<div id="content" class="w600p center">
<div id="article">
<header class="mbm">
Expand Down

0 comments on commit 7af9c60

Please sign in to comment.