Skip to content

Commit

Permalink
Adjust the x1 offset of the bounds to make sure a font is rendered in…
Browse files Browse the repository at this point in the history
…side the canvas (#4278).
  • Loading branch information
dlemstra committed Oct 2, 2021
1 parent 94721c6 commit ed6f7d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
38 changes: 31 additions & 7 deletions coders/caption.c
Expand Up @@ -91,6 +91,21 @@
% o exception: return any errors or warnings in this structure.
%
*/
static inline void AdjustTypeMetricBounds(TypeMetric *metrics)
{
if (metrics->bounds.x1 < 0.0)
{
double
new_x1;

new_x1=-metrics->bounds.x1;
metrics->width+=new_x1+new_x1;
metrics->bounds.x1=new_x1;
}
else
metrics->bounds.x1=0.0;
}

static Image *ReadCAPTIONImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
Expand Down Expand Up @@ -174,27 +189,31 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
{
text=AcquireString(caption);
i=FormatMagickCaption(image,draw_info,split,&metrics,&text,exception);
AdjustTypeMetricBounds(&metrics);
(void) CloneString(&draw_info->text,text);
text=DestroyString(text);
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
-metrics.bounds.x1,metrics.ascent);
metrics.bounds.x1,metrics.ascent);
if (draw_info->gravity == UndefinedGravity)
(void) CloneString(&draw_info->geometry,geometry);
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
AdjustTypeMetricBounds(&metrics);
image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
}
if (image->rows == 0)
{
split=MagickTrue;
text=AcquireString(caption);
i=FormatMagickCaption(image,draw_info,split,&metrics,&text,exception);
AdjustTypeMetricBounds(&metrics);
(void) CloneString(&draw_info->text,text);
text=DestroyString(text);
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
-metrics.bounds.x1,metrics.ascent);
metrics.bounds.x1,metrics.ascent);
if (draw_info->gravity == UndefinedGravity)
(void) CloneString(&draw_info->geometry,geometry);
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
AdjustTypeMetricBounds(&metrics);
image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
draw_info->interline_spacing+draw_info->stroke_width)+0.5);
}
Expand Down Expand Up @@ -240,15 +259,17 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
text=AcquireString(caption);
i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
exception);
AdjustTypeMetricBounds(&metrics);
(void) CloneString(&draw_info->text,text);
text=DestroyString(text);
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
-metrics.bounds.x1,metrics.ascent);
metrics.bounds.x1,metrics.ascent);
if (draw_info->gravity == UndefinedGravity)
(void) CloneString(&draw_info->geometry,geometry);
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
if (status == MagickFalse)
break;
AdjustTypeMetricBounds(&metrics);
width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
height=(size_t) floor(metrics.height+draw_info->interline_spacing+
draw_info->stroke_width+0.5);
Expand All @@ -270,15 +291,17 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
text=AcquireString(caption);
i=FormatMagickCaption(image,draw_info,split,&metrics,&text,
exception);
AdjustTypeMetricBounds(&metrics);
(void) CloneString(&draw_info->text,text);
text=DestroyString(text);
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
-metrics.bounds.x1,metrics.ascent);
metrics.bounds.x1,metrics.ascent);
if (draw_info->gravity == UndefinedGravity)
(void) CloneString(&draw_info->geometry,geometry);
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
if (status == MagickFalse)
break;
AdjustTypeMetricBounds(&metrics);
width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
height=(size_t) floor(metrics.height+draw_info->interline_spacing+
draw_info->stroke_width+0.5);
Expand All @@ -302,11 +325,12 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
Draw caption.
*/
i=FormatMagickCaption(image,draw_info,split,&metrics,&caption,exception);
AdjustTypeMetricBounds(&metrics);
(void) CloneString(&draw_info->text,caption);
caption=DestroyString(caption);
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",MagickMax(
draw_info->direction == RightToLeftDirection ? (double) image->columns-
metrics.bounds.x2 : -metrics.bounds.x1,0.0),(draw_info->gravity ==
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
(draw_info->direction == RightToLeftDirection ? (double) image->columns-
metrics.bounds.x2 : metrics.bounds.x1),(draw_info->gravity ==
UndefinedGravity ? MagickMax(metrics.ascent,metrics.bounds.y2) : 0.0));
(void) CloneString(&draw_info->geometry,geometry);
status=AnnotateImage(image,draw_info,exception);
Expand Down
27 changes: 23 additions & 4 deletions coders/label.c
Expand Up @@ -87,6 +87,21 @@
% o exception: return any errors or warnings in this structure.
%
*/
static inline void AdjustTypeMetricBounds(TypeMetric *metrics)
{
if (metrics->bounds.x1 < 0.0)
{
double
new_x1;

new_x1=-metrics->bounds.x1;
metrics->width+=new_x1+new_x1;
metrics->bounds.x1=new_x1;
}
else
metrics->bounds.x1=0.0;
}

static Image *ReadLABELImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
Expand Down Expand Up @@ -145,6 +160,7 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
draw_info->text=ConstantString(label);
(void) memset(&metrics,0,sizeof(metrics));
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
AdjustTypeMetricBounds(&metrics);
if ((image->columns == 0) && (image->rows == 0))
{
image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
Expand All @@ -168,12 +184,13 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
for (n=0; n < 32; n++, draw_info->pointsize*=2.0)
{
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
-metrics.bounds.x1,metrics.ascent);
metrics.bounds.x1,metrics.ascent);
if (draw_info->gravity == UndefinedGravity)
(void) CloneString(&draw_info->geometry,geometry);
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
if (status == MagickFalse)
break;
AdjustTypeMetricBounds(&metrics);
width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
if ((image->columns != 0) && (image->rows != 0))
Expand All @@ -198,12 +215,13 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
{
draw_info->pointsize=(low+high)/2.0;
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
-metrics.bounds.x1,metrics.ascent);
metrics.bounds.x1,metrics.ascent);
if (draw_info->gravity == UndefinedGravity)
(void) CloneString(&draw_info->geometry,geometry);
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
if (status == MagickFalse)
break;
AdjustTypeMetricBounds(&metrics);
width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
if ((image->columns != 0) && (image->rows != 0))
Expand All @@ -224,6 +242,7 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
{
draw_info->pointsize=floor((low+high)/2.0-0.5);
status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
AdjustTypeMetricBounds(&metrics);
}
}
label=DestroyString(label);
Expand Down Expand Up @@ -260,8 +279,8 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
*/
(void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
(draw_info->direction == RightToLeftDirection ? (double) image->columns-
metrics.bounds.x2 : 0.0),(draw_info->gravity == UndefinedGravity ?
MagickMax(metrics.ascent,metrics.bounds.y2) : 0.0));
metrics.bounds.x2 : metrics.bounds.x1),(draw_info->gravity == UndefinedGravity ?
metrics.bounds.y2 : 0.0));
(void) CloneString(&draw_info->geometry,geometry);
status=AnnotateImage(image,draw_info,exception);
if (image_info->pointsize == 0.0)
Expand Down

0 comments on commit ed6f7d0

Please sign in to comment.