Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,27 @@ static void Main(string[] args)
TestGetText();
TestMarkdownReader();
TestRecompressJBIG2();
TestIssue234();

return;
}

static void TestIssue234()
{
Console.WriteLine("\n=== TestIssue234 =======================");

var pix = new Pixmap("../../../TestDocuments/Image/boxedpage.jpg"); // 629x1000 image
var scaled = new Pixmap(pix, 943, 1500, null); // scale up
byte[] jpeg = scaled.ToBytes("jpg", 65);

using var doc = new Document();
Page page = doc.NewPage(0, 943, 1500);
page.InsertImage(page.Rect, stream: jpeg);
page.Dispose();
doc.Save("issue_234.pdf");
doc.Close();
}

static void TestRecompressJBIG2()
{
Console.WriteLine("\n=== TestJBIG2 =======================");
Expand Down
50 changes: 50 additions & 0 deletions MuPDF.NET.Test/UtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using NUnit.Framework;
using MuPDF.NET;

namespace MuPDF.NET.Test
{
public class UtilsTest
{
[Test]
public void FloatToString_NoScientificNotation()
{
Assert.That(Utils.FloatToString(1.5f), Is.EqualTo("1.5"));
Assert.That(Utils.FloatToString(0f), Is.EqualTo("0"));
Assert.That(Utils.FloatToString(-123.456f), Is.EqualTo("-123.456"));
Assert.That(Utils.FloatToString(1000000f), Is.EqualTo("1000000"));

// Values that would use scientific notation with default ToString
string small = Utils.FloatToString(0.0000123f);
Assert.That(small.Contains("E") || small.Contains("e"), Is.False, "Should not use scientific notation");
Assert.That(small.Contains("0.00001") || small.Contains("0.000012"), Is.True);
}

[Test]
public void FloatToString_InvariantCulture()
{
string result = Utils.FloatToString(1.5f);
Assert.That(result.Contains("."), Is.True);
Assert.That(result.Contains(","), Is.False);
}

[Test]
public void DoubleToString_NoScientificNotation()
{
Assert.That(Utils.DoubleToString(1.5), Is.EqualTo("1.5"));
Assert.That(Utils.DoubleToString(0), Is.EqualTo("0"));
Assert.That(Utils.DoubleToString(-123.456), Is.EqualTo("-123.456"));
Assert.That(Utils.DoubleToString(1000000), Is.EqualTo("1000000"));

string small = Utils.DoubleToString(1.23e-10);
Assert.That(small.Contains("E") || small.Contains("e"), Is.False, "Should not use scientific notation");
}

[Test]
public void DoubleToString_InvariantCulture()
{
string result = Utils.DoubleToString(1.5);
Assert.That(result.Contains("."), Is.True);
Assert.That(result.Contains(","), Is.False);
}
}
}
10 changes: 9 additions & 1 deletion MuPDF.NET/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,15 @@ public int InsertImage(
xobject.pdf_dict_puts(imgName, ref_);
FzBuffer nres = mupdf.mupdf.fz_new_buffer(50);
nres.fz_append_string(
string.Format(System.Globalization.CultureInfo.InvariantCulture, template, mat.a, mat.b, mat.c, mat.d, mat.e, mat.f, imgName)
string.Format(System.Globalization.CultureInfo.InvariantCulture,
template,
Utils.FloatToString(mat.a),
Utils.FloatToString(mat.b),
Utils.FloatToString(mat.c),
Utils.FloatToString(mat.d),
Utils.FloatToString(mat.e),
Utils.FloatToString(mat.f),
imgName)
);
Utils.InsertContents(pageDoc, page.obj(), nres, overlay);
}
Expand Down
12 changes: 6 additions & 6 deletions MuPDF.NET/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7913,19 +7913,19 @@ internal static void SetDotCultureForNumber()
}

/// <summary>
/// Converts a float to string with dot as decimal separator, regardless of culture
/// Converts a float to string with dot as decimal separator, without scientific notation.
/// </summary>
internal static string FloatToString(float value)
public static string FloatToString(float value)
{
return value.ToString(CultureInfo.InvariantCulture);
return value.ToString("0.#######", CultureInfo.InvariantCulture);
}

/// <summary>
/// Converts a double to string with dot as decimal separator, regardless of culture
/// Converts a double to string with dot as decimal separator, without scientific notation.
/// </summary>
internal static string DoubleToString(double value)
public static string DoubleToString(double value)
{
return value.ToString(CultureInfo.InvariantCulture);
return value.ToString("0.#################", CultureInfo.InvariantCulture);
}

/// <summary>
Expand Down