Skip to content

Commit 71a6233

Browse files
committed
Handle RGB color wrongly being passed as byte values
1 parent dee18a1 commit 71a6233

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

UglyToad.PdfPig.Rendering.Skia/Helpers/SkiaExtensions.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,38 @@ public static SKColor ToSKColor(this IColor pdfColor, double alpha)
237237
if (pdfColor is not null)
238238
{
239239
var (r, g, b) = pdfColor.ToRGBValues();
240-
return new SKColor(Convert.ToByte(r * 255), Convert.ToByte(g * 255), Convert.ToByte(b * 255),
240+
241+
if (r >= 0 && r <= 1 && g >= 0 && g <= 1 && b >= 0 && b <= 1)
242+
{
243+
// This is the expected case
244+
return new SKColor(
245+
Convert.ToByte(r * 255),
246+
Convert.ToByte(g * 255),
247+
Convert.ToByte(b * 255),
248+
Convert.ToByte(alpha * 255));
249+
}
250+
251+
// Should never happen, but see GHOSTSCRIPT-686749-1.pdf
252+
return new SKColor(
253+
ConvertToByte(r),
254+
ConvertToByte(g),
255+
ConvertToByte(b),
241256
Convert.ToByte(alpha * 255));
242257
}
243258

244259
return SKColors.Black.WithAlpha(Convert.ToByte(alpha * 255));
245260
}
246261

262+
private static byte ConvertToByte(double v)
263+
{
264+
return v switch
265+
{
266+
>= byte.MaxValue => byte.MaxValue,
267+
<= byte.MinValue => byte.MinValue,
268+
_ => (byte)v
269+
};
270+
}
271+
247272
public static SKMatrix ToSkMatrix(this TransformationMatrix transformationMatrix)
248273
{
249274
return new SKMatrix((float)transformationMatrix.A, (float)transformationMatrix.C,

UglyToad.PdfPig.Rendering.Skia/SkiaStreamProcessor.Glyph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public override void RenderGlyph(IFont font,
4545
return;
4646
}
4747

48-
var strokingColor = currentState.CurrentStrokingColor!;
49-
var nonStrokingColor = currentState.CurrentNonStrokingColor!;
48+
var strokingColor = currentState.CurrentStrokingColor;
49+
var nonStrokingColor = currentState.CurrentNonStrokingColor;
5050

5151
if (_fontCache.TryGetPath(font, code, out SKPath path))
5252
{

0 commit comments

Comments
 (0)