Skip to content

Commit

Permalink
vertical alignment for images
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Jordan committed Aug 13, 2023
1 parent d9a60cd commit 7c8cb47
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
12 changes: 12 additions & 0 deletions htdocs/js/ImageView/imageview.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
cursor: pointer;
}

.image-view-elt.top {
vertical-align: text-top;
}

.image-view-elt.middle {
vertical-align: middle;
}

.image-view-elt.bottom {
vertical-align: baseline;
}

.image-view-dialog.modal {
padding: 0 !important;
}
Expand Down
13 changes: 7 additions & 6 deletions macros/core/PGML.pl
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ sub NOOP {
terminator => qr/!\]/,
terminateMethod => 'terminateGetString',
cancelNL => 1,
options => [ "source", "width", "height" ]
options => [ "source", "width", "height", "image_options" ]
},
"[<" => {
type => 'link',
Expand Down Expand Up @@ -1362,11 +1362,12 @@ sub Text {

sub Image {
my ($self, $item) = @_;
my $text = $item->{text};
my $source = $item->{source};
my $width = $item->{width} || '';
my $height = $item->{height} || '';
return (main::image($source, alt => $text, width => $width, height => $height));
my $text = $item->{text};
my $source = $item->{source};
my $width = $item->{width} || '';
my $height = $item->{height} || '';
my $image_options = $item->{image_options} || {};
return (main::image($source, alt => $text, width => $width, height => $height, %$image_options));
}

######################################################################
Expand Down
21 changes: 18 additions & 3 deletions macros/core/PGbasicmacros.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2940,7 +2940,7 @@ =head2 Macros for displaying images

Usage:

image($image, width => 200, height => 200, tex_size => 800, alt => 'alt text', extra_html_tags => 'style="border:solid black 1pt"');
image($image, width => 200, height => 200, tex_size => 800, valign => 'middle', alt => 'alt text', extra_html_tags => 'style="border:solid black 1pt"');

where C<$image> can be a local file path, URL, WWPlot object, PGlateximage object,
PGtikz object, or parser::GraphTool object.
Expand All @@ -2957,6 +2957,9 @@ =head2 Macros for displaying images
HTML version with a 600 pixel wide reading area. If C<width> is missing and C<height>
is declared, we presume this is a wide image and then C<tex_size> defaults to 800.

C<valign> can be 'top', 'middle', or 'bottom'. This aligns the image relative to
the surrounding line of text.

image([$image1,$image2], width => 200, height => 200, tex_size => 800, alt => ['alt text 1','alt text 2'], extra_html_tags => 'style="border:solid black 1pt"');
image([$image1,$image2], width => 200, height => 200, tex_size => 800, alt => 'common alt text', extra_html_tags => 'style="border:solid black 1pt"');

Expand All @@ -2976,6 +2979,7 @@ sub image {
width => '',
height => '',
tex_size => '',
valign => 'middle',
# default value for alt is undef, since an empty string is the explicit indicator of a decorative image
alt => undef,
extra_html_tags => '',
Expand Down Expand Up @@ -3004,6 +3008,9 @@ sub image {
my $width_ratio = $tex_size * (.001);
my @image_list = ();
my @alt_list = ();
my $valign = 'middle';
$valign = 'top' if ($out_options{valign} eq 'top');
$valign = 'bottom' if ($out_options{valign} eq 'bottom');

# if width and/or height are explicit, create string for attribute to be used in HTML, LaTeX2HTML
my $width_attrib = ($width) ? qq{ width="$width"} : '';
Expand Down Expand Up @@ -3046,7 +3053,15 @@ sub image {
# We're going to create PDF files with our TeX (using pdflatex), so
# alias should have given us the path to a PNG image.
if ($imagePath) {
$out = "\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}\n";
if ($valign eq 'top') {
$out =
"\\raisebox{-\\height + \\fontcharht\\font`I}{\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}}\n";
} elsif ($valign eq 'bottom') {
$out = "\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}\n";
} else {
$out =
"\\raisebox{-0.5\\height + 0.5\\fontcharht\\font`I}{\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}}\n";
}
} else {
$out = "";
}
Expand All @@ -3067,7 +3082,7 @@ sub image {
my $altattrib = '';
if (defined $alt_list[0]) { $altattrib = 'alt="' . encode_pg_and_html(shift @alt_list) . '"' }
$out =
qq!<IMG SRC="$imageURL" class="image-view-elt" tabindex="0" role="button"$width_attrib$height_attrib $out_options{extra_html_tags} $altattrib>!;
qq!<IMG SRC="$imageURL" class="image-view-elt $valign" tabindex="0" role="button"$width_attrib$height_attrib $out_options{extra_html_tags} $altattrib>!;
} elsif ($displayMode eq 'PTX') {
my $ptxwidth = ($width ? int($width / 6) : 80);
if (defined $alt) {
Expand Down
2 changes: 1 addition & 1 deletion t/tikz_test/tikz_image.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ok my $img = image($drawing), 'img tag is generated';
like $img, qr!
^<IMG\s
SRC="/pg_files/tmp/images/([a-z0-9_-]*)\.svg"\s
class="image-view-elt"\s
class="image-view-elt\smiddle"\s
tabindex="0"\s
role="button"\s
width="200"\s*
Expand Down

0 comments on commit 7c8cb47

Please sign in to comment.