Skip to content

Commit

Permalink
Merge pull request #275 from Sharktheone/feat/render-tree-remove-dupl…
Browse files Browse the repository at this point in the history
…icate-code

Remove some duplicate code in render tree
  • Loading branch information
Kiyoshika authored Nov 21, 2023
2 parents de8be91 + 2dbc4b0 commit 5fa063f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 209 deletions.
215 changes: 45 additions & 170 deletions src/render_tree.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
pub mod properties;
pub mod text;
pub mod util;

use std::borrow::BorrowMut;
use std::{cell::RefCell, rc::Rc};

use crate::html5::node::NodeData;
use crate::html5::parser::document;
use crate::html5::parser::document::{Document, DocumentHandle};

use crate::html5::node::NodeData;
use crate::render_tree::{properties::Rectangle, text::TextNode};

pub mod properties;
pub mod text;
pub mod util;

/// The position of the render cursor used to determine where
/// to draw an object
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -103,51 +102,21 @@ impl RenderTree {
if let Some(current_node) = doc_read.get_node_by_id(current_node_id) {
match &current_node.data {
NodeData::Element(element) => {
match element.name.as_str() {
"h1" => {
let new_node =
Rc::new(RefCell::new(Node::new_heading1(&mut self.position)));
util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
"h2" => {
let new_node =
Rc::new(RefCell::new(Node::new_heading2(&mut self.position)));
util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
"h3" => {
let new_node =
Rc::new(RefCell::new(Node::new_heading3(&mut self.position)));
util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
"h4" => {
let new_node =
Rc::new(RefCell::new(Node::new_heading4(&mut self.position)));
util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
"h5" => {
let new_node =
Rc::new(RefCell::new(Node::new_heading5(&mut self.position)));
util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
"h6" => {
let new_node =
Rc::new(RefCell::new(Node::new_heading6(&mut self.position)));
util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
"p" => {
let new_node =
Rc::new(RefCell::new(Node::new_paragraph(&mut self.position)));
util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
_ => { /* add more here, for now ignore */ }
}
let new_node = match element.name.as_str() {
"h1" => Node::new_heading1,
"h2" => Node::new_heading2,
"h3" => Node::new_heading3,
"h4" => Node::new_heading4,
"h5" => Node::new_heading5,
"h6" => Node::new_heading6,
"p" => Node::new_paragraph,
_ => continue, /* add more here, for now ignore */
}(&mut self.position);

let new_node = Rc::new(RefCell::new(new_node));

util::add_text_node(&reference_element, &new_node);
reference_element = Rc::clone(&new_node);
}
NodeData::Text(text) => {
let mut mut_element_ref = reference_element.as_ref().borrow_mut();
Expand Down Expand Up @@ -195,18 +164,11 @@ impl Node {
}
}

// I took the margins/font sizes from Chrome dev tools.
// There are still some slight differences but it's very close

pub fn new_heading1(position: &mut Position) -> Self {
let margin = 10.72;
fn new_text(node: TextNode, margin: f64, position: &mut Position) -> Self {
position.offset_y(margin);

let new_heading = TextNode::new_heading1();
let font_size = new_heading.font_size;

let fs = node.font_size;
let new_node = Node {
node_type: NodeType::Text(new_heading),
node_type: NodeType::Text(node),
margin: Rectangle::with_values(margin, 0., 0., margin),
padding: Rectangle::new(),
parent: None,
Expand All @@ -215,148 +177,61 @@ impl Node {
position: Position::new_from_existing(position),
};

position.offset_y(font_size);
position.offset_y(margin);
position.offset_y(fs + margin);

new_node
}

pub fn new_heading2(position: &mut Position) -> Self {
let margin = 9.96;
position.offset_y(margin);
// I took the margins/font sizes from Chrome dev tools.
// There are still some slight differences but it's very close

let new_heading = TextNode::new_heading2();
let font_size = new_heading.font_size;
pub fn new_heading1(position: &mut Position) -> Self {
let margin = 10.72;
let heading = TextNode::new_heading1();

let new_node = Node {
node_type: NodeType::Text(new_heading),
margin: Rectangle::with_values(margin, 0., 0., margin),
padding: Rectangle::new(),
parent: None,
next_sibling: None,
children: Vec::new(),
position: Position::new_from_existing(position),
};
Node::new_text(heading, margin, position)
}

position.offset_y(font_size);
position.offset_y(margin);
pub fn new_heading2(position: &mut Position) -> Self {
let margin = 9.96;
let heading = TextNode::new_heading2();

new_node
Node::new_text(heading, margin, position)
}

pub fn new_heading3(position: &mut Position) -> Self {
let margin = 9.36;
position.offset_y(margin);

let new_heading = TextNode::new_heading3();
let font_size = new_heading.font_size;
let heading = TextNode::new_heading3();

let new_node = Node {
node_type: NodeType::Text(new_heading),
margin: Rectangle::with_values(margin, 0., 0., margin),
padding: Rectangle::new(),
parent: None,
next_sibling: None,
children: Vec::new(),
position: Position::new_from_existing(position),
};

position.offset_y(font_size);
position.offset_y(margin);

new_node
Node::new_text(heading, margin, position)
}

pub fn new_heading4(position: &mut Position) -> Self {
let margin = 10.64;
position.offset_y(margin);
let heading = TextNode::new_heading4();

let new_heading = TextNode::new_heading4();
let font_size = new_heading.font_size;

let new_node = Node {
node_type: NodeType::Text(new_heading),
margin: Rectangle::with_values(margin, 0., 0., margin),
padding: Rectangle::new(),
parent: None,
next_sibling: None,
children: Vec::new(),
position: Position::new_from_existing(position),
};

position.offset_y(font_size);
position.offset_y(margin);

new_node
Node::new_text(heading, margin, position)
}

pub fn new_heading5(position: &mut Position) -> Self {
let margin = 11.089;
position.offset_y(margin);
let heading = TextNode::new_heading5();

let new_heading = TextNode::new_heading5();
let font_size = new_heading.font_size;

let new_node = Node {
node_type: NodeType::Text(new_heading),
margin: Rectangle::with_values(margin, 0., 0., margin),
padding: Rectangle::new(),
parent: None,
next_sibling: None,
children: Vec::new(),
position: Position::new_from_existing(position),
};

position.offset_y(font_size);
position.offset_y(margin);

new_node
Node::new_text(heading, margin, position)
}

pub fn new_heading6(position: &mut Position) -> Self {
let margin = 12.489;
position.offset_y(margin);
let heading = TextNode::new_heading6();

let new_heading = TextNode::new_heading6();
let font_size = new_heading.font_size;

let new_node = Node {
node_type: NodeType::Text(new_heading),
margin: Rectangle::with_values(margin, 0., 0., margin),
padding: Rectangle::new(),
parent: None,
next_sibling: None,
children: Vec::new(),
position: Position::new_from_existing(position),
};

position.offset_y(font_size);
position.offset_y(margin);

new_node
Node::new_text(heading, margin, position)
}

pub fn new_paragraph(position: &mut Position) -> Self {
let margin = 8.;
position.offset_y(margin);
let paragraph = TextNode::new_paragraph();

let new_paragraph = TextNode::new_paragraph();
let font_size = new_paragraph.font_size;

let new_node = Node {
node_type: NodeType::Text(new_paragraph),
margin: Rectangle::with_values(margin, 0., 0., margin),
padding: Rectangle::new(),
parent: None,
next_sibling: None,
children: Vec::new(),
position: Position::new_from_existing(position),
};

position.offset_y(font_size);
position.offset_y(margin);

new_node
Node::new_text(paragraph, margin, position)
}

pub fn add_child(&mut self, child: &Rc<RefCell<Node>>) {
Expand Down
53 changes: 14 additions & 39 deletions src/render_tree/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,41 @@ impl TextNode {
/*
NOTE: I got the default font sizes from https://stackoverflow.com/a/70720104
*/
pub fn new_heading1() -> Self {

fn new(fs: f64, bold: bool) -> Self {
Self {
value: "".to_owned(),
font: DEFAULT_FONT.to_owned(),
font_size: 37.,
is_bold: true,
font_size: fs,
is_bold: bold,
}
}

pub fn new_heading1() -> Self {
TextNode::new(37., true)
}

pub fn new_heading2() -> Self {
Self {
value: "".to_owned(),
font: DEFAULT_FONT.to_owned(),
font_size: 27.5,
is_bold: true,
}
TextNode::new(27.5, true)
}

pub fn new_heading3() -> Self {
Self {
value: "".to_owned(),
font: DEFAULT_FONT.to_owned(),
font_size: 21.5,
is_bold: true,
}
TextNode::new(21.5, true)
}

pub fn new_heading4() -> Self {
Self {
value: "".to_owned(),
font: DEFAULT_FONT.to_owned(),
font_size: 18.5,
is_bold: true,
}
TextNode::new(18.5, true)
}

pub fn new_heading5() -> Self {
Self {
value: "".to_owned(),
font: DEFAULT_FONT.to_owned(),
font_size: 15.5,
is_bold: true,
}
TextNode::new(15.5, true)
}

pub fn new_heading6() -> Self {
Self {
value: "".to_owned(),
font: DEFAULT_FONT.to_owned(),
font_size: 12.,
is_bold: true,
}
TextNode::new(12., true)
}

pub fn new_paragraph() -> Self {
Self {
value: "".to_owned(),
font: DEFAULT_FONT.to_owned(),
font_size: 18.5,
is_bold: false,
}
TextNode::new(18.5, false)
}
}

0 comments on commit 5fa063f

Please sign in to comment.