Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Keavon committed Feb 6, 2025
1 parent a27807b commit ead74f4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,8 @@ pub fn get_text(layer: LayerNodeIdentifier, network_interface: &NodeNetworkInter
let Some(&TaggedValue::F64(font_size)) = inputs[3].as_value() else { return None };
let Some(&TaggedValue::F64(line_height_ratio)) = inputs[4].as_value() else { return None };
let Some(&TaggedValue::F64(character_spacing)) = inputs[5].as_value() else { return None };
let Some(&TaggedValue::OptionalF64(max_width)) = inputs[6].as_value() else {
return None;
};
let Some(&TaggedValue::OptionalF64(max_height)) = inputs[7].as_value() else {
return None;
};
let Some(&TaggedValue::OptionalF64(max_width)) = inputs[6].as_value() else { return None };
let Some(&TaggedValue::OptionalF64(max_height)) = inputs[7].as_value() else { return None };

let typesetting = TypesettingConfig {
font_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ where
best
}

/// Gives the bounding box of the text in the layer using max width and height from the typesetting config.
/// Calculates the bounding box of the layer's text, based on the settings for max width and height specified in the typesetting config.
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");

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 far = graphene_core::text::bounding_box(text, buzz_face.as_ref(), typesetting);

quad
Quad::from_box([DVec2::ZERO, far])
}
38 changes: 22 additions & 16 deletions editor/src/messages/tool/tool_messages/text_tool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::too_many_arguments)]

use super::tool_prelude::*;
use crate::consts::DRAG_THRESHOLD;
use crate::consts::{COLOR_OVERLAY_RED, DRAG_THRESHOLD};
use crate::messages::portfolio::document::graph_operation::utility_types::TransformIn;
use crate::messages::portfolio::document::overlays::utility_types::OverlayContext;
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
Expand Down Expand Up @@ -277,7 +277,7 @@ struct TextToolData {
bounding_box_manager: Option<BoundingBoxManager>,
pivot: Pivot,
snap_candidates: Vec<SnapCandidatePoint>,
// Handle multiple layers in the future
// TODO: Handle multiple layers in the future
layer_dragging: Option<LayerNodeIdentifier>,
}

Expand Down Expand Up @@ -409,11 +409,13 @@ impl TextToolData {

fn get_snap_candidates(&mut self, document: &DocumentMessageHandler, input: &InputPreprocessorMessageHandler, font_cache: &FontCache) {
self.snap_candidates.clear();
if let Some(layer) = self.layer_dragging {

if let Some(layer) = self.layer_dragging {
if (self.snap_candidates.len() as f64) < document.snapping_state.tolerance {
snapping::get_layer_snap_points(layer, &SnapData::new(document, input), &mut self.snap_candidates);
}
let quad = document.metadata().transform_to_document(layer) * text_bounding_box(layer, document, &font_cache);

let quad = document.metadata().transform_to_document(layer) * text_bounding_box(layer, document, font_cache);
snapping::get_bbox_points(quad, &mut self.snap_candidates, snapping::BBoxSnapValues::BOUNDING_BOX, document);
}
}
Expand Down Expand Up @@ -461,7 +463,7 @@ impl Fsm for TextToolFsmState {
});
if let Some(editing_text) = tool_data.editing_text.as_mut() {
let buzz_face = font_cache.get(&editing_text.font).map(|data| load_face(data));
let far = graphene_core::text::bounding_box(&tool_data.new_text, buzz_face.clone(), editing_text.typesetting);
let far = graphene_core::text::bounding_box(&tool_data.new_text, buzz_face.as_ref(), editing_text.typesetting);
if far.x != 0. && far.y != 0. {
let quad = Quad::from_box([DVec2::ZERO, far]);
let transformed_quad = document.metadata().transform_to_viewport(tool_data.layer) * quad;
Expand All @@ -487,8 +489,8 @@ impl Fsm for TextToolFsmState {
overlay_context.quad(quad, Some(&("#".to_string() + &fill_color)));
}

// TODO: implement for multiple layers
// currently we find the first layer
// TODO: Implement for multiple layers
// Currently we find the first layer
let layer = document
.network_interface
.selected_nodes(&[])
Expand All @@ -515,7 +517,7 @@ impl Fsm for TextToolFsmState {
if let Some((text, font, typesetting)) = graph_modification_utils::get_text(layer.unwrap(), &document.network_interface) {
let buzz_face = font_cache.get(font).map(|data| load_face(data));
if lines_clipping(text.as_str(), buzz_face, typesetting) {
overlay_context.line(transformed_quad.0[2], transformed_quad.0[3], Some(&"#FF0000".to_string()));
overlay_context.line(transformed_quad.0[2], transformed_quad.0[3], Some(COLOR_OVERLAY_RED));
}
}

Expand Down Expand Up @@ -572,19 +574,21 @@ impl Fsm for TextToolFsmState {
if selected.is_none() {
TextToolFsmState::Placing
} else if tool_data.pivot.is_over(input.mouse.position) {
// Pivot currently hardcoded to 0.5, 0.5 for text layers
// TODO: Pivot is currently hardcoded to 0.5, 0.5 for text layers

// responses.add(DocumentMessage::StartTransaction);

// tool_data.snap_manager.start_snap(document, input, document.bounding_boxes(), true, true);
// tool_data.snap_manager.add_all_document_handles(document, input, &[], &[], &[]);

TextToolFsmState::DraggingPivot

}
// Check if the user is dragging the bounds
} else if let Some(_selected_edges) = dragging_bounds {
else if let Some(_selected_edges) = dragging_bounds {
responses.add(DocumentMessage::StartTransaction);

tool_data.layer_dragging = selected.clone();
tool_data.layer_dragging = selected;

if let Some(bounds) = &mut tool_data.bounding_box_manager {
bounds.original_bound_transform = bounds.transform;
Expand All @@ -597,6 +601,7 @@ impl Fsm for TextToolFsmState {
// false
// }
// });

let selected = Vec::from([tool_data.layer_dragging.unwrap()]);

let mut selected = Selected::new(
Expand All @@ -611,14 +616,14 @@ impl Fsm for TextToolFsmState {
);
bounds.center_of_transformation = selected.mean_average_of_pivots();
}
tool_data.get_snap_candidates(document, input, &font_cache);
tool_data.get_snap_candidates(document, input, font_cache);

TextToolFsmState::ResizingBounds
} else {
TextToolFsmState::Placing
};

return state;
state
}
(TextToolFsmState::Ready, TextToolMessage::PointerMove { .. }) => {
// This ensures the cursor only changes if a layer is selected
Expand All @@ -633,12 +638,12 @@ impl Fsm for TextToolFsmState {
if layer.is_none() || cursor == MouseCursorIcon::Default {
cursor = MouseCursorIcon::Text;
}

// Dragging the pivot overrules the other operations
// if tool_data.pivot.is_over(input.mouse.position) {
// cursor = MouseCursorIcon::Move;
// }

responses.add(OverlaysMessage::Draw);
responses.add(FrontendMessage::UpdateMouseCursor { cursor });

Expand Down Expand Up @@ -708,7 +713,7 @@ impl Fsm for TextToolFsmState {
input_connector: InputConnector::node(node_id.unwrap(), 7),
input: NodeInput::value(TaggedValue::OptionalF64(Some(height)), false),
});
responses.add(GraphOperationMessage::TransformSet { layer: text_layer.unwrap(), transform: transform, transform_in: TransformIn::Viewport, skip_rerender: false });
responses.add(GraphOperationMessage::TransformSet { layer: text_layer.unwrap(), transform, transform_in: TransformIn::Viewport, skip_rerender: false });
responses.add(NodeGraphMessage::RunDocumentGraph);

// AutoPanning
Expand Down Expand Up @@ -864,6 +869,7 @@ impl Fsm for TextToolFsmState {
}

responses.add(FrontendMessage::TriggerTextCommit);

TextToolFsmState::Editing
}
(state, TextToolMessage::Abort) => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/panels/Document.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
if (displayEditableTextbox.text === "") textInput.textContent = "";
else textInput.textContent = `${displayEditableTextbox.text}\n`;
// Make it so maxHeight is a multiple of lineHeight
// Make it so `maxHeight` is a multiple of `lineHeight`
const lineHeight = displayEditableTextbox.lineHeightRatio * displayEditableTextbox.fontSize;
let height;
if (displayEditableTextbox.maxHeight) {
Expand Down
6 changes: 3 additions & 3 deletions node-graph/gcore/src/text/to_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ pub fn to_path(str: &str, buzz_face: Option<rustybuzz::Face>, typesetting: Types
builder.other_subpaths
}

pub fn bounding_box(str: &str, buzz_face: Option<rustybuzz::Face>, typesetting: TypesettingConfig) -> DVec2 {
pub fn bounding_box(str: &str, buzz_face: Option<&rustybuzz::Face>, typesetting: TypesettingConfig) -> DVec2 {
let buzz_face = match buzz_face {
Some(face) => face,
// Show blank layer if font has not loaded
None => return DVec2::ZERO,
};
let space_glyph = buzz_face.glyph_index(' ');

let (scale, line_height, mut buffer) = font_properties(&buzz_face, typesetting.font_size, typesetting.line_height_ratio);
let (scale, line_height, mut buffer) = font_properties(buzz_face, typesetting.font_size, typesetting.line_height_ratio);

let mut pos = DVec2::ZERO;
let mut bounds = DVec2::ZERO;
Expand All @@ -177,7 +177,7 @@ pub fn bounding_box(str: &str, buzz_face: Option<rustybuzz::Face>, typesetting:
for (index, word) in SplitWordsIncludingSpaces::new(line).enumerate() {
push_str(&mut buffer, word);

let glyph_buffer = rustybuzz::shape(&buzz_face, &[], buffer);
let glyph_buffer = rustybuzz::shape(buzz_face, &[], buffer);

// Don't wrap the first word
if index != 0 && wrap_word(typesetting.max_width, &glyph_buffer, scale, typesetting.character_spacing, pos.x, space_glyph) {
Expand Down

0 comments on commit ead74f4

Please sign in to comment.