2
0

PrintingServicesUnix.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Author:
  24. //
  25. // Jordi Mas i Hernandez, [email protected]
  26. //
  27. using System.Runtime.InteropServices;
  28. using System.Collections;
  29. using System.Drawing.Printing;
  30. using System.ComponentModel;
  31. using System.Drawing.Imaging;
  32. using System.Text;
  33. namespace System.Drawing.Printing
  34. {
  35. internal class PrintingServicesUnix : PrintingServices
  36. {
  37. private Hashtable doc_info = new Hashtable ();
  38. internal PrintingServicesUnix ()
  39. {
  40. }
  41. // Methods
  42. internal override void LoadPrinterSettings (string printer, PrinterSettings settings)
  43. {
  44. IntPtr ptr, ppd_handle;
  45. string ppd_filename;
  46. PPD_FILE ppd;
  47. ptr = cupsGetPPD (printer);
  48. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  49. ppd_handle = ppdOpenFile (ppd_filename);
  50. //Console.WriteLine ("File: {0}", ppd_filename);
  51. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  52. settings.landscape_angle = ppd.landscape;
  53. settings.supports_color = (ppd.color_device == 0) ? false : true;
  54. ppdClose (ppd_handle);
  55. }
  56. internal override void LoadPrinterResolutions (string printer, PrinterSettings settings)
  57. {
  58. settings.PrinterResolutions.Clear ();
  59. LoadDefaultResolutions (settings.PrinterResolutions);
  60. }
  61. internal override void LoadPrinterPaperSizes (string printer, PrinterSettings settings)
  62. {
  63. IntPtr ptr, ppd_handle;
  64. string ppd_filename, real_name;
  65. PPD_FILE ppd;
  66. PPD_SIZE size;
  67. PaperSize ps;
  68. PaperKind kind = PaperKind.Custom;
  69. settings.PaperSizes.Clear ();
  70. ptr = cupsGetPPD (printer);
  71. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  72. ppd_handle = ppdOpenFile (ppd_filename);
  73. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  74. ptr = ppd.sizes;
  75. float w, h;
  76. for (int i = 0; i < ppd.num_sizes; i++) {
  77. size = (PPD_SIZE) Marshal.PtrToStructure (ptr, typeof (PPD_SIZE));
  78. real_name = GetPaperSizeName (ppd, size.name);
  79. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (size));
  80. w = size.width * 100 / 72;
  81. h = size.length * 100 / 72;
  82. ps = new PaperSize (real_name, (int) w, (int) h);
  83. // TODO: Convert from name to paper kind enum
  84. ps.SetKind (kind);
  85. settings.PaperSizes.Add (ps);
  86. }
  87. ppdClose (ppd_handle);
  88. }
  89. internal override bool StartPage (GraphicsPrinter gr)
  90. {
  91. return true;
  92. }
  93. internal override bool EndPage (GraphicsPrinter gr)
  94. {
  95. GdipGetPostScriptSavePage (gr.Hdc);
  96. return true;
  97. }
  98. internal override bool EndDoc (GraphicsPrinter gr)
  99. {
  100. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  101. gr.Graphics.Dispose (); // Dispose object to force surface finish
  102. cupsPrintFile (doc.settings.PrinterName, doc.filename, doc.title, 0, IntPtr.Zero);
  103. doc_info.Remove (gr.Hdc);
  104. //TODO: Delete temporary file created
  105. return true;
  106. }
  107. internal override bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file)
  108. {
  109. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  110. doc.title = doc_name;
  111. return true;
  112. }
  113. internal override IntPtr CreateGraphicsContext (PrinterSettings settings)
  114. {
  115. IntPtr graphics = IntPtr.Zero;
  116. StringBuilder name = new StringBuilder (1024);
  117. int length = name.Capacity;
  118. cupsTempFile (name, length);
  119. //Console.WriteLine ("CreateGraphicsContext filename {0}", name.ToString());
  120. GdipGetPostScriptGraphicsContext (name.ToString(),
  121. settings.DefaultPageSettings.PaperSize.Width,
  122. settings.DefaultPageSettings.PaperSize.Height, ref graphics);
  123. DOCINFO doc = new DOCINFO ();
  124. doc.filename = name.ToString();
  125. doc.settings = settings;
  126. doc_info.Add (graphics, doc);
  127. return graphics;
  128. }
  129. // Properties
  130. internal override PrinterSettings.StringCollection InstalledPrinters {
  131. get {
  132. int n_printers;
  133. IntPtr printers = IntPtr.Zero, ptr_printers, ptr_printer;
  134. string str;
  135. PrinterSettings.StringCollection col = new PrinterSettings.StringCollection (new string[] {});
  136. n_printers = cupsGetPrinters (ref printers);
  137. ptr_printers = printers;
  138. for (int i = 0; i < n_printers; i++) {
  139. ptr_printer = (IntPtr) Marshal.ReadInt32 (ptr_printers);
  140. str = Marshal.PtrToStringAnsi (ptr_printer);
  141. ptr_printers = new IntPtr (ptr_printers.ToInt64 () + 4);
  142. col.Add (str);
  143. }
  144. Marshal.FreeHGlobal (printers);
  145. return col;
  146. }
  147. }
  148. internal override string DefaultPrinter {
  149. get {
  150. IntPtr str;
  151. str = cupsGetDefault ();
  152. return Marshal.PtrToStringAnsi (str);
  153. }
  154. }
  155. // Private functions
  156. private string GetPaperSizeName (PPD_FILE ppd, string name)
  157. {
  158. string rslt = name;
  159. PPD_GROUP group;
  160. PPD_OPTION option;
  161. PPD_CHOICE choice;
  162. IntPtr ptr, ptr_opt, ptr_choice;
  163. ptr = ppd.groups;
  164. for (int i = 0; i < ppd.num_groups; i++) {
  165. group = (PPD_GROUP) Marshal.PtrToStructure (ptr, typeof (PPD_GROUP));
  166. //Console.WriteLine ("Size text:{0} name:{1} opts {2}", group.text, group.name, group.num_options);
  167. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (group));
  168. ptr_opt = group.options;
  169. for (int n = 0; n < group.num_options; n++) {
  170. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  171. ptr_opt = new IntPtr (ptr_opt.ToInt64 () + Marshal.SizeOf (option));
  172. //Console.WriteLine (" key:{0} def:{1} text: {2}", option.keyword, option.defchoice, option.text);
  173. if (!option.keyword.Equals ("PageSize"))
  174. continue;
  175. ptr_choice = option.choices;
  176. for (int c = 0; c < option.num_choices; c++) {
  177. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  178. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  179. //Console.WriteLine (" choice:{0} - text: {1}", choice.choice, choice.text);
  180. if (name.Equals (choice.choice)) {
  181. rslt = choice.text;
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. return rslt;
  188. }
  189. //
  190. // DllImports
  191. //
  192. [DllImport("libcups", CharSet=CharSet.Ansi)]
  193. static extern int cupsGetPrinters (ref IntPtr printers);
  194. [DllImport("libcups", CharSet=CharSet.Ansi)]
  195. static extern IntPtr cupsTempFile (StringBuilder sb, int len);
  196. [DllImport("libcups", CharSet=CharSet.Ansi)]
  197. static extern IntPtr cupsGetDefault ();
  198. [DllImport("libcups", CharSet=CharSet.Ansi)]
  199. static extern int cupsPrintFile (string printer, string filename, string title, int num_options, IntPtr options);
  200. [DllImport("libcups", CharSet=CharSet.Ansi)]
  201. static extern IntPtr cupsGetPPD (string printer);
  202. [DllImport("libcups", CharSet=CharSet.Ansi)]
  203. static extern IntPtr ppdOpenFile (string filename);
  204. [DllImport("libcups")]
  205. static extern void ppdClose (IntPtr ppd);
  206. [DllImport("libgdiplus", CharSet=CharSet.Ansi)]
  207. static extern int GdipGetPostScriptGraphicsContext (string filename, int with, int height, ref IntPtr graphics);
  208. [DllImport("libgdiplus")]
  209. static extern int GdipGetPostScriptSavePage (IntPtr graphics);
  210. //Struct
  211. public struct DOCINFO
  212. {
  213. public PrinterSettings settings;
  214. public string title;
  215. public string filename;
  216. }
  217. public struct PPD_SIZE
  218. {
  219. public int marked;
  220. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  221. public string name;
  222. public float width;
  223. public float length;
  224. public float left;
  225. public float bottom;
  226. public float right;
  227. public float top;
  228. }
  229. public struct PPD_GROUP
  230. {
  231. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)]
  232. public string text;
  233. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  234. public string name;
  235. public int num_options;
  236. public IntPtr options;
  237. public int num_subgroups;
  238. public IntPtr subgrups;
  239. }
  240. public struct PPD_OPTION
  241. {
  242. public byte conflicted;
  243. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  244. public string keyword;
  245. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  246. public string defchoice;
  247. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  248. public string text;
  249. public int ui;
  250. public int section;
  251. public float order;
  252. public int num_choices;
  253. public IntPtr choices;
  254. }
  255. public struct PPD_CHOICE
  256. {
  257. public byte marked;
  258. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  259. public string choice;
  260. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  261. public string text;
  262. public IntPtr code;
  263. public IntPtr option;
  264. }
  265. public struct PPD_FILE
  266. {
  267. public int language_level;
  268. public int color_device;
  269. public int variable_sizes;
  270. public int accurate_screens;
  271. public int contone_only;
  272. public int landscape;
  273. public int model_number;
  274. public int manual_copies;
  275. public int throughput;
  276. public int colorspace;
  277. public IntPtr patches;
  278. public int num_emulations;
  279. public IntPtr emulations;
  280. public IntPtr jcl_begin;
  281. public IntPtr jcl_ps;
  282. public IntPtr jcl_end;
  283. public IntPtr lang_encoding;
  284. public IntPtr lang_version;
  285. public IntPtr modelname;
  286. public IntPtr ttrasterizer;
  287. public IntPtr manufacturer;
  288. public IntPtr product;
  289. public IntPtr nickname;
  290. public IntPtr shortnickname;
  291. public int num_groups;
  292. public IntPtr groups;
  293. public int num_sizes;
  294. public IntPtr sizes;
  295. /* There is more data after this that we are not using*/
  296. }
  297. }
  298. }