comparison mupdf-source/scripts/mupdfwrap_test.cs @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 public class HelloWorld
2 {
3 public static void Main(string[] args)
4 {
5 System.Console.WriteLine("MuPDF C# test starting.");
6
7 // Check FZ_ENABLE_FB2.
8 System.Console.WriteLine("FZ_VERSION=" + mupdf.mupdf.FZ_VERSION);
9 System.Console.WriteLine("FZ_ENABLE_FB2=" + mupdf.mupdf.FZ_ENABLE_FB2);
10
11 // Check we can load a document.
12 mupdf.FzDocument document = new mupdf.FzDocument("zlib.3.pdf");
13 System.Console.WriteLine("document: " + document);
14 System.Console.WriteLine("num chapters: " + document.fz_count_chapters());
15 mupdf.FzPage page = document.fz_load_page(0);
16 mupdf.FzRect rect = page.fz_bound_page();
17 System.Console.WriteLine("rect: " + rect);
18 if ("" + rect != rect.to_string())
19 {
20 throw new System.Exception("rect ToString() is broken: '" + rect + "' != '" + rect.to_string() + "'");
21 }
22
23 // Test conversion to html using docx device.
24 var buffer = page.fz_new_buffer_from_page_with_format(
25 "docx",
26 "html",
27 new mupdf.FzMatrix(1, 0, 0, 1, 0, 0),
28 new mupdf.FzCookie()
29 );
30 var data = buffer.fz_buffer_extract();
31 var s = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
32 if (s.Length < 100) {
33 throw new System.Exception("HTML text is too short");
34 }
35 System.Console.WriteLine("s=" + s);
36
37 // Check that previous buffer.fz_buffer_extract() cleared the buffer.
38 data = buffer.fz_buffer_extract();
39 s = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
40 if (s.Length > 0) {
41 throw new System.Exception("Buffer was not cleared.");
42 }
43
44 // Check we can create pixmap from page.
45 var pixmap = page.fz_new_pixmap_from_page_contents(
46 new mupdf.FzMatrix(1, 0, 0, 1, 0, 0),
47 new mupdf.FzColorspace(mupdf.FzColorspace.Fixed.Fixed_RGB),
48 0 /*alpha*/
49 );
50
51 // Check returned tuple from bitmap.fz_bitmap_details().
52 var w = 100;
53 var h = 200;
54 var n = 4;
55 var xres = 300;
56 var yres = 300;
57 var bitmap = new mupdf.FzBitmap(w, h, n, xres, yres);
58 (var w2, var h2, var n2, var stride) = bitmap.fz_bitmap_details();
59 System.Console.WriteLine("bitmap.fz_bitmap_details() returned:"
60 + " " + w2 + " " + h2 + " " + n2 + " " + stride);
61 if (w2 != w || h2 != h) {
62 throw new System.Exception("Unexpected tuple values from bitmap.fz_bitmap_details().");
63 }
64
65 // Check we get exception from MuPDF. As of 2024-06-14 this exception
66 // does not contain the original MuPDF exception text, it just says
67 // "External component has thrown an exception."
68 //
69 // We only do this test if not running on Mono - Mono fails
70 // with:
71 //
72 // > terminate called after throwing an instance of
73 // > 'mupdf::FzErrorSystem'
74 //
75 if (System.Type.GetType("Mono.Runtime") == null)
76 {
77 int received_exception = 0;
78 try
79 {
80 mupdf.FzDocument document2 = new mupdf.FzDocument("does not exist");
81 System.Console.WriteLine("*** Error, did not get expected exception.");
82 }
83 catch (System.Exception e)
84 {
85 received_exception = 1;
86 System.Console.WriteLine("Received exception: " + e.Message);
87 }
88 if (received_exception != 1)
89 {
90 throw new System.Exception("Did not receive expected exception");
91 }
92 }
93 else
94 {
95 System.Console.WriteLine("Not checking handling of exceptions because running on Mono.");
96 }
97
98 // Check we can make MuPDF open filename containing 4-byte
99 // unicode character. This file will have been created by
100 // `scripts/wrap/__main__.py --test-csharp`.
101 byte[] text_utf8 =
102 {
103 0xf0,
104 0x90,
105 0x90,
106 0xb7,
107 };
108 string testfile2 = "zlib.3.pdf"
109 + System.Text.Encoding.UTF8.GetString(text_utf8)
110 + ".pdf";
111 System.Console.WriteLine("Opening testfile2: " + testfile2);
112 try
113 {
114 mupdf.FzDocument document2 = new mupdf.FzDocument(testfile2);
115 System.Console.WriteLine("new mupdf.FzDocument succeeded");
116 }
117 catch (System.Exception e)
118 {
119 System.Console.WriteLine("Exception: " + e.Message);
120 throw new System.Exception("Failed to open filename containing 4-byte unicode character");
121 }
122
123 System.Console.WriteLine("MuPDF C# test finished.");
124 }
125 }