Skip to content

Commit

Permalink
align text layer without rasterising
Browse files Browse the repository at this point in the history
  • Loading branch information
ndepaola committed Apr 10, 2022
1 parent 527fe55 commit 51d332f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
12 changes: 12 additions & 0 deletions scripts/helpers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ function compute_text_layer_dimensions(layer) {
return dimensions;
}

function compute_text_layer_bounds(layer) {
/**
* Return an object with the specified text layer's bounding box.
*/

var layer_copy = layer.duplicate(activeDocument, ElementPlacement.INSIDE);
layer_copy.rasterize(RasterizeType.TEXTCONTENTS);
var layer_bounds = layer_copy.bounds;
layer_copy.remove();
return layer_bounds;
}

function select_layer_pixels(layer) {
/**
* Select the bounding box of a given layer.
Expand Down
21 changes: 9 additions & 12 deletions scripts/text_layers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ function vertically_align_text(layer, reference_layer) {
* Rasterises a given text layer and centres it vertically with respect to the bounding box of a reference layer.
*/

layer.rasterize(RasterizeType.TEXTCONTENTS);
var layer_copy = layer.duplicate(activeDocument, ElementPlacement.INSIDE);
layer_copy.rasterize(RasterizeType.TEXTCONTENTS);
select_layer_pixels(reference_layer);
app.activeDocument.activeLayer = layer;
align_vertical(layer);
app.activeDocument.activeLayer = layer_copy;
align_vertical(layer_copy);

This comment has been minimized.

Copy link
@Investigamer

Investigamer Apr 11, 2022

Contributor

Looking great! Just a small correction, align_vertical doesn't need to pass the copy_layer instance since it uses the active layer :)

Also I'm curious if you tried running this with creature cards, for me this method causes a crash during the vertically_nudge_creature_text function (might not result in a crash for the javascript version?). I'm sure its an easy fix just wanted to know if you had run into that!

EDIT: For now for the vertically_nudge_creature_text func I just had the text layer duplicated, hid the original, and rasterized the copy to work off that one

clear_selection();
var layer_dimensions = compute_text_layer_bounds(layer);
var layer_copy_dimensions = compute_text_layer_bounds(layer_copy);
layer.translate(0, layer_copy_dimensions[1].as("px") - layer_dimensions[1].as("px"));
layer_copy.remove();
}

function vertically_nudge_creature_text(layer, reference_layer, top_reference_layer) {
Expand Down Expand Up @@ -276,16 +281,8 @@ var FormattedTextArea = Class({
// resize the text until it fits into the reference layer
scale_text_to_fit_reference(this.layer, this.reference_layer);

// rasterise and centre vertically
// centre vertically
vertically_align_text(this.layer, this.reference_layer);

if (this.is_centred) {
// ensure the layer is centred horizontally as well
select_layer_pixels(this.reference_layer);
app.activeDocument.activeLayer = this.layer;
align_horizontal();
clear_selection();
}
}
}
});
Expand Down

0 comments on commit 51d332f

Please sign in to comment.