Skip to content

Commit

Permalink
Add another test case for DrawTextSafe
Browse files Browse the repository at this point in the history
Now is even more restrictive on how far negative text can start drawing.

Also updates nearly all DrawText invocations to use the safe method.
  • Loading branch information
atruskie committed Apr 30, 2020
1 parent cc768a2 commit acbc0d1
Show file tree
Hide file tree
Showing 28 changed files with 93 additions and 70 deletions.
6 changes: 4 additions & 2 deletions src/Acoustics.Shared/ImageSharp/ImageSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ public static void DrawTextSafe(this IImageProcessingContext context, string tex
{
foreach (var characterBound in characterBounds)
{
// magic value found empirically, does not seem to trigger bug when first char less than offset equal to char size
if (characterBound.Bounds.X > -(font.Size - 2))
// magic value found empirically, does not seem to trigger bug when first char less
// than offset equal to char size
var magicLimit = 0 - ((int)font.Size / 2);
if (characterBound.Bounds.X > magicLimit)
{
// width of chars don't take into account kerning (spacing between chars).
// so we use the Left of the first value that is within our sfety margin instead
Expand Down
4 changes: 2 additions & 2 deletions src/AnalysisPrograms/ConcatenateIndexFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ public static void ConcatenateRibbonImages(DirectoryInfo[] dataDirs, string patt
image.Mutate(canvas =>
{
var str = $"{i + 1}";
canvas.DrawText(str, stringFont, Color.White, new PointF(3, 3));
canvas.DrawTextSafe(str, stringFont, Color.White, new PointF(3, 3));
});

imageList.Add(image);
Expand All @@ -679,7 +679,7 @@ public static void ConcatenateRibbonImages(DirectoryInfo[] dataDirs, string patt

// add title bar
var titleBmp = new Image<Rgb24>(finalComposite.Width, 30);
titleBmp.Mutate(canvas => { canvas.DrawText(title, stringFont, Color.White, new PointF(30, 3)); });
titleBmp.Mutate(canvas => { canvas.DrawTextSafe(title, stringFont, Color.White, new PointF(30, 3)); });

// add title plus spacer
spacer = new Image<Rgb24>(finalComposite.Width, 3);
Expand Down
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/Draw/RibbonPlots/RibbonPlot.Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void Operation(IImageProcessingContext context)
var y = Padding + ((Padding + ribbonHeight) * b);

// draw label
context.DrawText(textGraphics, dateLabel, scaledFont, textColor, new Point(HorizontalPadding, y + (ribbonHeight / 2)));
context.DrawTextSafe(textGraphics, dateLabel, scaledFont, textColor, new Point(HorizontalPadding, y + (ribbonHeight / 2)));

// draw void
var @void = new RectangularPolygon(ribbonLeft, y, estimatedWidth, ribbonHeight);
Expand Down
6 changes: 3 additions & 3 deletions src/AnalysisPrograms/DrawEasyImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ public static void Execute(Arguments arguments)
string month = monthNames[thisday.Month - 1];
if (thisday.Month == 1) // January
{
g.DrawText(thisday.Year.ToString(), stringFont, Color.White, new PointF(0, nextRow + trackHeight + 1)); //draw time
g.DrawText(month, stringFont, Color.White, new PointF(1, nextRow + trackHeight + 11)); //draw time
g.DrawTextSafe(thisday.Year.ToString(), stringFont, Color.White, new PointF(0, nextRow + trackHeight + 1)); //draw time
g.DrawTextSafe(month, stringFont, Color.White, new PointF(1, nextRow + trackHeight + 11)); //draw time
}
else
{
g.DrawText(month, stringFont, Color.White, new PointF(1, nextRow + trackHeight + 1)); //draw time
g.DrawTextSafe(month, stringFont, Color.White, new PointF(1, nextRow + trackHeight + 1)); //draw time
}

currentRow += 2;
Expand Down
4 changes: 2 additions & 2 deletions src/AnalysisPrograms/DrawLongDurationSpectrograms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static Image<Rgb24> DrawGrayScaleSpectrograms(Arguments arguments, string
label.Mutate(g1 =>
{
g1.Clear(Color.Gray);
g1.DrawText(key, stringFont, Color.Black, new PointF(4, 30));
g1.DrawTextSafe(key, stringFont, Color.Black, new PointF(4, 30));
g1.DrawLine(new Pen(Color.Black, 1), 0, 0, width, 0); //draw upper boundary
g1.DrawLine(new Pen(Color.Black, 1), 0, 1, width, 1); //draw upper boundary
});
Expand Down Expand Up @@ -418,7 +418,7 @@ public static Image DrawRidgeSpectrograms(DirectoryInfo inputDirectory, FileInfo
g2.Clear(Color.White);
}

g2.DrawText(key, stringFont, color[labelIndex], new PointF(0, labelYvalue));
g2.DrawTextSafe(key, stringFont, color[labelIndex], new PointF(0, labelYvalue));
});
labelYvalue += 10;

Expand Down
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/Recognizers/Base/MultiRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static Image<Rgb24> GenerateScoreTrackImage(string name, double[] scores,
}
}

g2.DrawText(name, stringFont, brush, new PointF(1, 1));
g2.DrawTextSafe(name, stringFont, brush, new PointF(1, 1));
g2.DrawRectangle(new Pen(Color.Gray, 1), 0, 0, imageWidth - 1, trackHeight - 1);
});
return trackImage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ public static Image<Rgb24> GetWaveformImage(double[] minValues, double[] maxValu
canvas.DrawLine(pen2, imageWidth / 2, 0, imageWidth / 2, imageHeight);
var stringFont = Drawing.Arial9;
var brush = Color.LightGray;
canvas.DrawText("+1.0", stringFont, brush, new PointF((imageWidth / 2) + 2, 10.0f));
canvas.DrawText("-1.0", stringFont, brush, new PointF((imageWidth / 2) + 2, imageHeight - 20.0f));
canvas.DrawTextSafe("+1.0", stringFont, brush, new PointF((imageWidth / 2) + 2, 10.0f));
canvas.DrawTextSafe("-1.0", stringFont, brush, new PointF((imageWidth / 2) + 2, imageHeight - 20.0f));
});

return image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public override AnalysisResult2 Analyze<T>(AnalysisSettings analysisSettings, Se
label.Mutate(g1 =>
{
g1.Clear(Color.Gray);
g1.DrawText(labelText, stringFont, Color.Black, new PointF(4, 30));
g1.DrawTextSafe(labelText, stringFont, Color.Black, new PointF(4, 30));
g1.DrawLine(new Pen(Color.Black, 1), 0, 0, width, 0); //draw upper boundary
g1.DrawLine(new Pen(Color.Black, 1), 0, 1, width, 1); //draw upper boundary
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void DrawNormalisedIndexMatrices(DirectoryInfo dir, string baseNam
var header = Drawing.NewImage(image.Width, 20, Color.LightGray);
header.Mutate(g =>
{
g.DrawText(key, Drawing.Tahoma9, Color.Black, new PointF(4, 4));
g.DrawTextSafe(key, Drawing.Tahoma9, Color.Black, new PointF(4, 4));
list.Add(ImageTools.CombineImagesVertically(header, image));
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/DSP/FrequencyScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public static void DrawFrequencyLinesOnImage(Image<Rgb24> bmp, int[,] gridLineLo

if (y > 1)
{
g.DrawText($"{hertzValue}", Drawing.Tahoma8, txtColour, new PointF(1, y));
g.DrawTextSafe($"{hertzValue}", Drawing.Tahoma8, txtColour, new PointF(1, y));
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/AudioAnalysisTools/FrommoltProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static void ConcatenateDays()
gapImage.Mutate(g5 =>
{
g5.Clear(Color.Gray);
g5.DrawText("Day", stringFont, brush, new PointF(2, 5));
g5.DrawText("missing", stringFont, brush, new PointF(2, 35));
g5.DrawTextSafe("Day", stringFont, brush, new PointF(2, 5));
g5.DrawTextSafe("missing", stringFont, brush, new PointF(2, 35));
});

list.Add(gapImage);
Expand All @@ -110,7 +110,7 @@ public static void ConcatenateDays()
labelImage1.Mutate(g1 =>
{
g1.Clear(Color.Black);
g1.DrawText(fileSuffix, stringFont, brush, new PointF(2, 2));
g1.DrawTextSafe(fileSuffix, stringFont, brush, new PointF(2, 2));
});

//labelImage1.Save(Path.Combine(imageDirectory.FullName, suffix1));
Expand Down Expand Up @@ -163,7 +163,7 @@ public static Image<Rgb24> ConcatenateFourChannelImages(FileInfo[] imageFiles, D
concatImage.Mutate(g =>
{
string chn = $"ch{channel + 1}";
g.DrawText(chn, stringFont, brush, new PointF(2, 40));
g.DrawTextSafe(chn, stringFont, brush, new PointF(2, 40));
});

fourChannelList.Add(concatImage);
Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/Indices/GapsAndJoins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public Image<Rgb24> DrawErrorPatch(int height, bool textInVerticalOrientation)
}
else
{
g.DrawText(" " + this.GapDescription, font, Color.Black, new PointF(2, fontVerticalPosition));
g.DrawTextSafe(" " + this.GapDescription, font, Color.Black, new PointF(2, fontVerticalPosition));
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/AudioAnalysisTools/Indices/IndexDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public static Image<Rgb24> DrawHighAmplitudeClippingTrack(double[] array1, doubl
bmp.Mutate(g =>
{
var font = Drawing.Arial9;
g.DrawText("Clipping", font, Color.DarkRed, new PointF(5, 1));
g.DrawText(" & High Amplitude", font, Color.DarkBlue, new PointF(50, 1));
g.DrawTextSafe("Clipping", font, Color.DarkRed, new PointF(5, 1));
g.DrawTextSafe(" & High Amplitude", font, Color.DarkBlue, new PointF(50, 1));
});

return bmp;
Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/Indices/IndexProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public Image<Rgb24> GetPlotImage(double[] array, Color backgroundColour, List<Ga
bmp.Mutate(g =>
{
var font = Drawing.Arial9;
g.DrawText(annotation, font, Color.Black, new PointF(dataLength, 5));
g.DrawTextSafe(annotation, font, Color.Black, new PointF(dataLength, 5));
});

// now add in image patches for possible erroneous segments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static Image<Rgb24> FrameSliceOf3DSpectrogram_DayOfYear(Image<Rgb24> bmp1

DateTime theDate = new DateTime(year, 1, 1).AddDays(dayOfYear - 1);
string dateString = $"{year} {DataTools.MonthNames[theDate.Month - 1]} {theDate.Day:d2}";
g.DrawText(dateString, stringFont, Color.Wheat, new PointF(10, 3));
g.DrawTextSafe(dateString, stringFont, Color.Wheat, new PointF(10, 3));
});

TimeSpan xAxisPixelDuration = TimeSpan.FromSeconds(60);
Expand Down Expand Up @@ -369,7 +369,7 @@ public static Image<Rgb24> FrameSliceOf3DSpectrogram_ConstantFreq(Image<Rgb24> b
var pen = new Pen(Color.White, 1);
var stringFont = Drawing.Arial12;
var str = $"Freq = {herzValue} Hz";
bmp1.Mutate(g => { g.DrawText(str, stringFont, Color.Wheat, new PointF(10, 7)); });
bmp1.Mutate(g => { g.DrawTextSafe(str, stringFont, Color.Wheat, new PointF(10, 7)); });
var xAxisPixelDuration = TimeSpan.FromSeconds(60);
var startOffset = TimeSpan.Zero;
double secondsDuration = xAxisPixelDuration.TotalSeconds * bmp1.Width;
Expand Down Expand Up @@ -422,8 +422,8 @@ public static Image<Rgb24> DrawFreqScale_vertical(int yoffset, int trackHeight,
Pen grayPen = new Pen(Color.DarkGray, 1);
Pen whitePen = new Pen(Color.White, 1);
Font stringFont = Drawing.Arial9;
g.DrawText("Herz", stringFont, Color.Yellow, new PointF(xoffset + 2, 6)); //draw label
g.DrawText("Scale", stringFont, Color.Yellow, new PointF(xoffset, 19)); //draw label
g.DrawTextSafe("Herz", stringFont, Color.Yellow, new PointF(xoffset + 2, 6)); //draw label
g.DrawTextSafe("Scale", stringFont, Color.Yellow, new PointF(xoffset, 19)); //draw label
g.DrawLine(whitePen, xoffset, yoffset, trackWidth, yoffset);
g.DrawLine(whitePen, xoffset, yoffset - 1, trackWidth, yoffset - 1);

Expand All @@ -438,14 +438,14 @@ public static Image<Rgb24> DrawFreqScale_vertical(int yoffset, int trackHeight,
Pen yellowPen = new Pen(Color.Yellow, 1);
g.DrawLine(yellowPen, xoffset, ymark, trackWidth, ymark);
g.DrawLine(yellowPen, xoffset, ymark - 1, trackWidth, ymark - 1);
g.DrawText(herzValue.ToString(), stringFont, Color.White,
g.DrawTextSafe(herzValue.ToString(), stringFont, Color.White,
new PointF(xoffset + 1, ymark - 14)); //draw time

// g.DrawLine(whitePen, 0, daysInYear + offset, trackWidth, daysInYear + offset);
// g.DrawLine(whitePen, 0, offset, trackWidth, offset); //draw lower boundary
// g.DrawLine(whitePen, duration, 0, duration, trackHeight - 1);//draw right end boundary

// g.DrawText(title, stringFont, Color.White, new PointF(duration + 4, 3));
// g.DrawTextSafe(title, stringFont, Color.White, new PointF(duration + 4, 3));
});

return bmp;
Expand All @@ -461,7 +461,7 @@ public static Image<Rgb24> FrameSliceOf3DSpectrogram_ConstantMin(Image<Rgb24> bm

TimeSpan time = TimeSpan.FromMinutes(minuteOfDay);
string str = $"Time = {time.Hours}h:{time.Minutes}m";
bmp1.Mutate(g => { g.DrawText(str, stringFont, Color.Wheat, new PointF(10, 7)); });
bmp1.Mutate(g => { g.DrawTextSafe(str, stringFont, Color.Wheat, new PointF(10, 7)); });

int binCount = 512;
if (imageHeight <= 256)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public static void ExtractSOMClusters1()
gr.DrawLine(blackPen, opColumn - 1, imageHt - trackheight, opColumn - 1, imageHt);

int location = opColumn - ((opColumn - clusterStartColumn) / 2);
gr.DrawText(clusterLabel[sortID], stringFont, Color.Black, new PointF(location - 10, imageHt - 19));
gr.DrawTextSafe(clusterLabel[sortID], stringFont, Color.Black, new PointF(location - 10, imageHt - 19));
});
}

Expand Down Expand Up @@ -474,7 +474,7 @@ public static void ExtractSOMClusters2()
clusterIDImage.Mutate(g2 => { g2.Clear(Color.Black); });
gr.DrawImage(clusterIDImage, new Point(clusterStartColumn, imageHt - 19), 1);
int location = opColumnNumber - ((opColumnNumber - clusterStartColumn) / 2);
gr.DrawText(clusterLabel[sortID], stringFont, Color.White, new PointF(location - 10, imageHt - 19));
gr.DrawTextSafe(clusterLabel[sortID], stringFont, Color.White, new PointF(location - 10, imageHt - 19));
}
}

Expand All @@ -497,7 +497,7 @@ public static Image DrawTitleBarOfClusterSpectrogram(string title, int width)
SizeF stringSize = new SizeF();

int X = 4;
g.DrawText(title, stringFont, Color.Wheat, new PointF(X, 3));
g.DrawTextSafe(title, stringFont, Color.Wheat, new PointF(X, 3));

stringSize = g.MeasureString(title, stringFont);
X += stringSize.ToSize().Width + 70;
Expand All @@ -507,7 +507,7 @@ public static Image DrawTitleBarOfClusterSpectrogram(string title, int width)
int X2 = width - stringSize.ToSize().Width - 2;
if (X2 > X)
{
g.DrawText(text, stringFont, Color.Wheat, new PointF(X2, 3));
g.DrawTextSafe(text, stringFont, Color.Wheat, new PointF(X2, 3));
}

g.DrawLine(new Pen(Color.Gray, 1), 0, 0, width, 0); //draw upper boundary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,20 @@ public static Image<Rgb24> DrawTitleBarOfEuclidianDistanceSpectrogram(
{
string text = string.Format("EUCLIDEAN DISTANCE SPECTROGRAM (scale:hours x kHz)");
int X = 4;
g.DrawText(text, stringFont, Color.Wheat, new PointF(X, 3));
g.DrawTextSafe(text, stringFont, Color.Wheat, new PointF(X, 3));

stringSize = g.MeasureString(text, stringFont);
X += stringSize.ToSize().Width + 70;
text = name1 + " +99.9%conf";
g.DrawText(text, stringFont, Color.Wheat, new PointF(X, 3));
g.DrawTextSafe(text, stringFont, Color.Wheat, new PointF(X, 3));

stringSize = g.MeasureString(text, stringFont);
X += stringSize.ToSize().Width + 1;
g.DrawImage(colorChart, new Point(X, 1), 1);

X += colorChart.Width;
text = "-99.9%conf " + name2;
g.DrawText(text, stringFont, Color.Wheat, new PointF(X, 3));
g.DrawTextSafe(text, stringFont, Color.Wheat, new PointF(X, 3));
stringSize = g.MeasureString(text, stringFont);
X += stringSize.ToSize().Width + 1; // distance to end of string

Expand All @@ -456,7 +456,7 @@ public static Image<Rgb24> DrawTitleBarOfEuclidianDistanceSpectrogram(
int x2 = width - stringSize.ToSize().Width - 2;
if (x2 > X)
{
g.DrawText(text, stringFont, Color.Wheat, new PointF(x2, 3));
g.DrawTextSafe(text, stringFont, Color.Wheat, new PointF(x2, 3));
}

g.DrawLine(new Pen(Color.Gray, 1), 0, 0, width, 0); // draw upper boundary
Expand Down
Loading

0 comments on commit acbc0d1

Please sign in to comment.