Skip to content

Commit

Permalink
Create function for text bounding box
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitish-bot committed Jan 2, 2025
1 parent 54a1807 commit 984dee1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
14 changes: 14 additions & 0 deletions editor/src/messages/tool/common_functionality/utility_functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
use crate::messages::prelude::*;
use crate::messages::tool::common_functionality::graph_modification_utils::get_text;

use graphene_core::renderer::Quad;
use graphene_core::text::{load_face, FontCache};
use graphene_std::vector::PointId;

use glam::DVec2;
Expand Down Expand Up @@ -28,3 +31,14 @@ pub fn should_extend(document: &DocumentMessageHandler, goal: DVec2, tolerance:

best
}

pub fn text_bounding_box(layer: LayerNodeIdentifier, document: &DocumentMessageHandler, font_cache: &FontCache) -> Quad {
let (text, font, typesetting) = get_text(layer, &document.network_interface).expect("Text layer should have text when interacting with the Text tool in `interact()`");

let buzz_face = font_cache.get(font).map(|data| load_face(data));
let far = graphene_core::text::bounding_box(text, buzz_face, typesetting);
let quad = Quad::from_box([DVec2::ZERO, far]);
let transformed_quad = document.metadata().transform_to_viewport(layer) * quad;

transformed_quad
}
14 changes: 4 additions & 10 deletions editor/src/messages/tool/tool_messages/select_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use crate::messages::portfolio::document::utility_types::document_metadata::Laye
use crate::messages::portfolio::document::utility_types::misc::{AlignAggregate, AlignAxis, FlipAxis};
use crate::messages::portfolio::document::utility_types::network_interface::{FlowType, NodeNetworkInterface, NodeTemplate};
use crate::messages::portfolio::document::utility_types::transformation::Selected;
use crate::messages::tool::common_functionality::graph_modification_utils::{get_text, is_layer_fed_by_node_of_name};
use crate::messages::tool::common_functionality::graph_modification_utils::is_layer_fed_by_node_of_name;
use crate::messages::tool::common_functionality::pivot::Pivot;
use crate::messages::tool::common_functionality::snapping::{self, SnapCandidatePoint, SnapData, SnapManager};
use crate::messages::tool::common_functionality::transformation_cage::*;
use crate::messages::tool::common_functionality::utility_functions::text_bounding_box;
use crate::messages::tool::common_functionality::{auto_panning::AutoPanning, measure};

use graph_craft::document::NodeId;
use graphene_core::renderer::Quad;
use graphene_core::text::load_face;
use graphene_std::renderer::Rect;
use graphene_std::vector::misc::BooleanOperation;

Expand Down Expand Up @@ -425,14 +425,8 @@ impl Fsm for SelectToolFsmState {
overlay_context.outline(document.metadata().layer_outline(layer), document.metadata().transform_to_viewport(layer));

if is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text") {
let (text, font, typesetting) = get_text(layer, &document.network_interface).expect("Text layer should have text when interacting with the Text tool in `interact()`");

let buzz_face = font_cache.get(font).map(|data| load_face(data));
let far = graphene_core::text::bounding_box(text, buzz_face, typesetting);
let quad = Quad::from_box([DVec2::ZERO, far]);
let transformed_quad = document.metadata().transform_to_viewport(layer) * quad;

overlay_context.dashed_quad(transformed_quad, None, Some(7.), Some(5.));
let quad = text_bounding_box(layer, document, font_cache);
overlay_context.dashed_quad(quad, None, Some(7.), Some(5.));
}
}

Expand Down
25 changes: 7 additions & 18 deletions editor/src/messages/tool/tool_messages/text_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::messages::portfolio::document::utility_types::network_interface::Inpu
use crate::messages::tool::common_functionality::color_selector::{ToolColorOptions, ToolColorType};
use crate::messages::tool::common_functionality::graph_modification_utils::{self, is_layer_fed_by_node_of_name};
use crate::messages::tool::common_functionality::snapping::SnapData;
use crate::messages::tool::common_functionality::utility_functions::text_bounding_box;
use crate::messages::tool::common_functionality::{auto_panning::AutoPanning, resize::Resize};

use graph_craft::document::value::TaggedValue;
Expand Down Expand Up @@ -379,17 +380,10 @@ impl TextToolData {
.all_layers()
.filter(|&layer| is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text"))
.find(|&layer| {
let (text, font, typesetting) =
graph_modification_utils::get_text(layer, &document.network_interface).expect("Text layer should have text when interacting with the Text tool in `interact()`");

let buzz_face = font_cache.get(font).map(|data| load_face(data));
let far = graphene_core::text::bounding_box(text, buzz_face, typesetting);
let quad = Quad::from_box([DVec2::ZERO, far]);
let transformed_quad = document.metadata().transform_to_viewport(layer) * quad;

let quad = text_bounding_box(layer, document, font_cache);
let mouse = DVec2::new(input.mouse.position.x, input.mouse.position.y);

transformed_quad.contains(mouse)
quad.contains(mouse)
})
}
}
Expand Down Expand Up @@ -462,15 +456,10 @@ impl Fsm for TextToolFsmState {
overlay_context.quad(quad, Some(&("#".to_string() + &fill_color)));
} else {
for layer in document.network_interface.selected_nodes(&[]).unwrap().selected_layers(document.metadata()) {
let Some((text, font, typesetting)) = graph_modification_utils::get_text(layer, &document.network_interface) else {
continue;
};
let buzz_face = font_cache.get(font).map(|data| load_face(data));

let far = graphene_core::text::bounding_box(text, buzz_face, typesetting);
let quad = Quad::from_box([DVec2::ZERO, far]);
let multiplied = document.metadata().transform_to_viewport(layer) * quad;
overlay_context.quad(multiplied, None);
if is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text") {
let quad = text_bounding_box(layer, document, font_cache);
overlay_context.quad(quad, None);
}
}
}
tool_data.resize.snap_manager.draw_overlays(SnapData::new(document, input), &mut overlay_context);
Expand Down

0 comments on commit 984dee1

Please sign in to comment.