Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly deal with Furigana #360

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
77 changes: 76 additions & 1 deletion crates/core/src/document/html/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,48 @@ impl Engine {
inlines.push(InlineMaterial::LineBreak);
return;
},
"ruby" => {
// Ruby needs to be applied to the first text element only, with the content from the succeeding rt tag.
// So grab all text until rt, and attach its content to the first text child.
let mut text_datas = Vec::new();
for child in node.children() {
match child.data() {
NodeData::Element(ElementData { name, .. }) => {
if name == "rt" {
let mut ruby_content = "".to_owned();
for subchild in child.children() {
match subchild.data() {
NodeData::Text(TextData { text, .. }) => {
ruby_content.push_str(&decode_entities(text))
},
_ => {},
}
}
style.ruby = Some(ruby_content);

for TextData {text, offset} in &text_datas {
inlines.push(InlineMaterial::Text(TextMaterial {
offset: *offset,
text: decode_entities(text).into_owned(),
style: style.clone(),
}));
style.ruby = None;
}

text_datas.clear();
}
},
NodeData::Text(text_data) => {
text_datas.push(text_data.clone());
},
_ => {},
}
}
return;
},
"rt" => {
return;
},
_ => {},
}

Expand Down Expand Up @@ -811,7 +853,9 @@ impl Engine {
font.plan(" 0.", None, None)
};
let mut start_index = 0;
let mut ruby_start_index = 0;
for (end_index, _is_hardbreak) in LineBreakIterator::new(text) {
let text_len = text[start_index..end_index].chars().count();
for chunk in text[start_index..end_index].split_inclusive(char::is_whitespace) {
if let Some((i, c)) = chunk.char_indices().next_back() {
let j = i + if c.is_whitespace() { 0 } else { c.len_utf8() };
Expand All @@ -828,12 +872,16 @@ impl Engine {
};
plan.space_out(style.letter_spacing);

let ruby_chars = style.ruby.clone().map(|r| r.chars().count());
let ruby_chars_use = ruby_chars.map(|c| std::cmp::min(c.div_ceil(text_len), (c - ruby_start_index).div_ceil(text_len)));

items.push(ParagraphItem::Box {
width: plan.width,
data: ParagraphElement::Text(TextElement {
offset: local_offset,
language: style.language.clone(),
text: buf.to_string(),
ruby: style.ruby.clone().map(|r| r.chars().skip(ruby_start_index).take(ruby_chars_use.unwrap()).collect()),
plan,
font_features: style.font_features.clone(),
font_kind: style.font_kind,
Expand All @@ -846,6 +894,8 @@ impl Engine {
uri: style.uri.clone(),
}),
});

ruby_start_index += ruby_chars_use.unwrap_or(0);
}
if c.is_whitespace() {
if c == '\n' && parent_style.retain_whitespace {
Expand Down Expand Up @@ -1236,7 +1286,7 @@ impl Engine {

for i in last_index..index {
match items[i] {
ParagraphItem::Box { ref data, width } => {
ParagraphItem::Box { ref data, mut width } => {
match data {
ParagraphElement::Text(element) => {
let pt = pt!(position.x, position.y - element.vertical_align);
Expand Down Expand Up @@ -1267,6 +1317,30 @@ impl Engine {
font_size: element.font_size,
color: element.color,
}));
if let Some(ruby) = &element.ruby {
let ruby_plan = {
let font = self.fonts.as_mut().unwrap()
.get_mut(element.font_kind, element.font_style, element.font_weight);
font.set_size(element.font_size / 2, self.dpi);
font.plan(ruby.to_string(), None, style.font_features.as_deref())
};
if width < ruby_plan.width {
width = ruby_plan.width;
}
page.push(DrawCommand::ExtraText(TextCommand {
offset: element.offset + root_data.start_offset,
position: pt + pt!(0, -ascender),
rect,
text: ruby.to_string(),
plan: ruby_plan,
uri: element.uri.clone(),
font_kind: element.font_kind,
font_style: element.font_style,
font_weight: element.font_weight,
font_size: element.font_size / 2,
color: element.color,
}));
}
},
ParagraphElement::Image(element) => {
while let Some(offset) = markers.get(markers_index) {
Expand Down Expand Up @@ -1490,6 +1564,7 @@ impl Engine {
letter_spacing: element.letter_spacing,
color: element.color,
uri: element.uri.clone(),
ruby: element.ruby.clone(),
}),
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/document/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct StyleData {
pub vertical_align: i32,
pub list_style_type: Option<ListStyleType>,
pub uri: Option<String>,
pub ruby: Option<String>,
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -193,6 +194,7 @@ impl Default for StyleData {
vertical_align: 0,
list_style_type: None,
uri: None,
ruby: None,
}
}
}
Expand Down Expand Up @@ -326,6 +328,7 @@ pub struct TextElement {
pub offset: usize,
pub language: Option<String>,
pub text: String,
pub ruby: Option<String>,
pub plan: RenderPlan,
pub font_features: Option<Vec<String>>,
pub font_kind: FontKind,
Expand Down