Skip to content

Commit

Permalink
Add support for dictd dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
baskerville committed Dec 19, 2019
1 parent df8d4c6 commit 1130bff
Show file tree
Hide file tree
Showing 39 changed files with 1,762 additions and 98 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
/libs
/bin
/Settings.toml
/user.css
/css/*-user.css
/hyphenation-patterns
/dictionaries
/src/wrapper/*.so
/src/wrapper/*.dylib
/dist
Expand Down
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ paragraph-breaker = "0.4.3"
rand_xorshift = "0.2.0"
xi-unicode = "0.2.0"
septem = "1.0.1"
byteorder = "1.3.2"
flate2 = "1.0.12"
levenshtein = "1.0.4"

[dependencies.getopts]
version = "0.2.21"
Expand Down
11 changes: 11 additions & 0 deletions css/dictionary.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.dictname {
margin-top: 1.5em;
text-align: center;
font-feature-settings: "smcp" "c2sc" "onum";
letter-spacing: 0.07em;
}

.headword {
margin-top: 1.0em;
font-weight: bold;
}
1 change: 1 addition & 0 deletions dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cp -R scripts dist
cp -R icons dist
cp -R fonts dist
cp -R css dist
find dist/css -name '*-user.css' -delete
cp target/arm-unknown-linux-gnueabihf/release/plato dist/

patchelf --remove-rpath dist/libs/*
Expand Down
2 changes: 1 addition & 1 deletion doc/GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ The default library path is `/mnt/onboard` on devices without an external SD car
library-path = "LIBRARY_PATH"
```

If there's a `user.css` in same directory as the program's binary, it will be used for all the reflowable formats.
The default ePUB stylesheet, `css/epub.css`, can be overriden via `css/epub-user.css`.
14 changes: 14 additions & 0 deletions doc/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ The *CMB* (combine) key can be used to enter special characters, e.g.: `CMB o e`

A tap and hold on the delete or motion keys will act on words instead of characters.

# Applications

Applications can be launched from the *Applications* submenu of the main menu.

You can go back to the previous view by tapping the top-left *back arrow*.

## Dictionary

*Dictionary* can be launched from the *Reader* view by tapping and holding a word or by making a text selection and tapping *Define* in the selection menu.

Dictionaries will be searched recursively in the `dictionaries` directory. The supported format is *dictd*: `.dict.dz` (or `.dict`) and `.index`. The dictionary definitions can be styled by creating a stylesheet at `css/dictionary-user.css`. The definitions that aren't formatted with XML are wrapped inside a *pre* tag. The font size and margin width can be changed in the `[dictionary]` section of `Settings.toml`.

You can select the search target by tapping the label in the bottom bar. You can set the input languages of a dictionary by tapping and holding the target's label. You can then provide a comma-separated list of IETF language tags (e.g.: *en, en-US, en-GB*).

# Annex

## Combination Sequences
Expand Down
2 changes: 1 addition & 1 deletion doc/NAVIGATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ To name a page, hold the current page indicator and select the *Name* entry. A p

Once a page is named, you can jump to any page above it in the same category. For example if you've defined page 15 as *vi*, by entering *'ix* (or *"ix*), in the *Go to page* input field, you'll jump to page 18.

You can also select a page name in the book's text and jump to it by taping *Go To* in the selection menu. This can be particularly useful within a book's index.
You can also select a page name in the book's text and jump to it by tapping *Go To* in the selection menu. This can be particularly useful within a book's index.

## Overriding the TOC

Expand Down
1 change: 0 additions & 1 deletion doc/TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- Dictionary.
- ePUB renderer: RTL.
- Metadata view.
- Complex/fuzzy search queries?
Expand Down
44 changes: 36 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{self, Receiver, Sender};
use std::process::Command;
use std::collections::VecDeque;
use std::collections::{BTreeMap, VecDeque};
use std::time::{Duration, Instant};
use failure::{Error, ResultExt};
use fnv::FnvHashMap;
use chrono::Local;
use glob::glob;
use crate::dictionary::{Dictionary, load_dictionary_from_file};
use crate::framebuffer::{Framebuffer, KoboFramebuffer, Display, UpdateMode};
use crate::view::{View, Event, EntryId, EntryKind, ViewId, AppId};
use crate::view::{View, Event, EntryId, EntryKind, ViewId, AppCmd};
use crate::view::{render, render_region, render_no_wait, render_no_wait_region, handle_event, expose};
use crate::view::common::{locate, locate_by_id, transfer_notifications, overlapping_rectangle};
use crate::view::frontlight::FrontlightWindow;
use crate::view::menu::{Menu, MenuKind};
use crate::view::sketch::Sketch;
use crate::view::dictionary::Dictionary as DictionaryApp;
use crate::view::calculator::Calculator;
use crate::view::sketch::Sketch;
use crate::input::{DeviceEvent, PowerSource, ButtonCode, ButtonStatus};
use crate::input::{raw_events, device_events, usb_events, display_rotate_event};
use crate::gesture::{GestureEvent, gesture_events};
Expand Down Expand Up @@ -49,6 +52,7 @@ pub struct Context {
pub metadata: Metadata,
pub filename: PathBuf,
pub fonts: Fonts,
pub dictionaries: BTreeMap<String, Dictionary>,
pub frontlight: Box<dyn Frontlight>,
pub battery: Box<dyn Battery>,
pub lightsensor: Box<dyn LightSensor>,
Expand All @@ -67,10 +71,31 @@ impl Context {
let dims = fb.dims();
let rotation = CURRENT_DEVICE.transformed_rotation(fb.rotation());
Context { fb, display: Display { dims, rotation },
settings, metadata, filename, fonts, battery,
settings, metadata, filename, fonts, dictionaries: BTreeMap::new(), battery,
frontlight, lightsensor, notification_index: 0, kb_rect: Rectangle::default(),
plugged: false, covered: false, shared: false, online: false }
}

pub fn load_dictionaries(&mut self) {
if let Ok(entries) = glob("dictionaries/**/*.index") {
for entry in entries.into_iter().filter_map(|e| e.ok()) {
let index_path = entry;
let mut content_path = index_path.clone();
content_path.set_extension("dict.dz");
if !content_path.exists() {
content_path.set_extension("");
}
if let Ok(mut dict) = load_dictionary_from_file(&content_path, &index_path) {
let name = dict.short_name().ok().unwrap_or_else(|| {
index_path.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default()
});
self.dictionaries.insert(name, dict);
}
}
}
}
}

struct Task {
Expand Down Expand Up @@ -746,15 +771,16 @@ pub fn run() -> Result<(), Error> {
});
view = next_view;
},
Event::Select(EntryId::Launch(app_id)) => {
Event::Select(EntryId::Launch(app_cmd)) => {
view.children_mut().retain(|child| !child.is::<Menu>());
let monochrome = context.fb.monochrome();
let mut next_view: Box<dyn View> = match app_id {
AppId::Sketch => {
let mut next_view: Box<dyn View> = match app_cmd {
AppCmd::Sketch => {
context.fb.set_monochrome(true);
Box::new(Sketch::new(context.fb.rect(), &tx, &mut context))
},
AppId::Calculator => Box::new(Calculator::new(context.fb.rect(), &tx, &mut context)?),
AppCmd::Calculator => Box::new(Calculator::new(context.fb.rect(), &tx, &mut context)?),
AppCmd::Dictionary { ref query, ref language } => Box::new(DictionaryApp::new(context.fb.rect(), query, language, &tx, &mut context)),
};
transfer_notifications(view.as_mut(), next_view.as_mut(), &mut context);
history.push(HistoryItem {
Expand All @@ -779,6 +805,8 @@ pub fn run() -> Result<(), Error> {
}
}
view.handle_event(&Event::Reseed, &tx, &mut bus, &mut context);
} else {
break;
}
},
Event::TogglePresetMenu(rect, index) => {
Expand Down

0 comments on commit 1130bff

Please sign in to comment.