mirror of
https://github.com/storytold/designcraft.git
synced 2026-10-28 12:16:53 -06:00
Merge pull request #1736 from storytold/sonar/s3776-a4
refactor: reduce cognitive complexity in epub table and pdf frame text (Sonar S3776)
This commit is contained in:
+15
-10
@@ -983,6 +983,20 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
out
|
||||
}
|
||||
|
||||
/// One table cell with its row and column spans, as `th` or `td`.
|
||||
fn table_cell(&mut self, text: &Story, cell_tag: &str, (rs, cs): (usize, usize), depth: usize, out: &mut String) {
|
||||
let _ = write!(out, "<{cell_tag}");
|
||||
if rs > 1 {
|
||||
let _ = write!(out, " rowspan=\"{rs}\"");
|
||||
}
|
||||
if cs > 1 {
|
||||
let _ = write!(out, " colspan=\"{cs}\"");
|
||||
}
|
||||
out.push('>');
|
||||
self.paragraphs(text, depth, "", out);
|
||||
let _ = writeln!(out, "</{cell_tag}>");
|
||||
}
|
||||
|
||||
/// A table: header rows as `thead`, footer rows as `tfoot`, merged cells as row and column
|
||||
/// spans (kept within their row group).
|
||||
fn table(&mut self, t: &Table, depth: usize, out: &mut String) {
|
||||
@@ -1016,16 +1030,7 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = write!(out, "<{cell_tag}");
|
||||
if rs > 1 {
|
||||
let _ = write!(out, " rowspan=\"{rs}\"");
|
||||
}
|
||||
if cs > 1 {
|
||||
let _ = write!(out, " colspan=\"{cs}\"");
|
||||
}
|
||||
out.push('>');
|
||||
self.paragraphs(&cell.text, depth, "", out);
|
||||
let _ = writeln!(out, "</{cell_tag}>");
|
||||
self.table_cell(&cell.text, cell_tag, (rs, cs), depth, out);
|
||||
}
|
||||
out.push_str("</tr>\n");
|
||||
}
|
||||
|
||||
+29
-24
@@ -233,6 +233,34 @@ impl Exporter<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// One line's glyphs as tagged runs; a glyph that is turned or slanted gets a transform of its own.
|
||||
fn line_glyphs(&mut self, s: &mut Surface, cs: &ComposedStory, ft: &FrameText, l: &designcraft_compose::Line, story: &str) {
|
||||
let gs = &l.glyphs;
|
||||
let mut i = 0;
|
||||
while i < gs.len() {
|
||||
let g = &gs[i];
|
||||
if !g.visible || g.sx <= 0.0 || g.sy <= 0.0 {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// Vertical type: an upright glyph turns back a quarter about its em box centre.
|
||||
// Shatai slants each glyph about its own em box centre first.
|
||||
let turn = if ft.vertical { g.vertical_xf(l.baseline) } else { None };
|
||||
let shatai = cs.styles.get(g.style as usize).and_then(|st| st.shatai_xf(g, l.baseline));
|
||||
if turn.is_some() || shatai.is_some() {
|
||||
let m = turn.unwrap_or(Affine::IDENTITY) * shatai.unwrap_or(Affine::IDENTITY);
|
||||
s.push_transform(&crate::export::tf(m));
|
||||
self.tagged_run(s, cs, &gs[i..i + 1], l.baseline, story);
|
||||
s.pop();
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let j = run_end(ft, gs, i);
|
||||
self.tagged_run(s, cs, &gs[i..j], l.baseline, story);
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn frame_text(&mut self, s: &mut Surface, cs: &ComposedStory, ft: &FrameText, story: &str) {
|
||||
// Paragraph shading and rules under the text.
|
||||
self.as_artifact(s, |me, s| {
|
||||
@@ -260,30 +288,7 @@ impl Exporter<'_> {
|
||||
});
|
||||
}
|
||||
for l in &ft.lines {
|
||||
let gs = &l.glyphs;
|
||||
let mut i = 0;
|
||||
while i < gs.len() {
|
||||
let g = &gs[i];
|
||||
if !g.visible || g.sx <= 0.0 || g.sy <= 0.0 {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// Vertical type: an upright glyph turns back a quarter about its em box centre.
|
||||
// Shatai slants each glyph about its own em box centre first.
|
||||
let turn = if ft.vertical { g.vertical_xf(l.baseline) } else { None };
|
||||
let shatai = cs.styles.get(g.style as usize).and_then(|st| st.shatai_xf(g, l.baseline));
|
||||
if turn.is_some() || shatai.is_some() {
|
||||
let m = turn.unwrap_or(Affine::IDENTITY) * shatai.unwrap_or(Affine::IDENTITY);
|
||||
s.push_transform(&crate::export::tf(m));
|
||||
self.tagged_run(s, cs, &gs[i..i + 1], l.baseline, story);
|
||||
s.pop();
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let j = run_end(ft, gs, i);
|
||||
self.tagged_run(s, cs, &gs[i..j], l.baseline, story);
|
||||
i = j;
|
||||
}
|
||||
self.line_glyphs(s, cs, ft, l, story);
|
||||
}
|
||||
self.as_artifact(s, |me, s| {
|
||||
for (rule, r) in text_rules(cs, ft, false) {
|
||||
|
||||
Reference in New Issue
Block a user