PrintingServicesUnix.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. GdipGetPostScriptGraphicsContext (name.ToString(),
  120. settings.DefaultPageSettings.PaperSize.Width / 100 * 72,
  121. settings.DefaultPageSettings.PaperSize.Height / 100 * 72,
  122. // Harcoded dpy's
  123. 300, 300, ref graphics);
  124. DOCINFO doc = new DOCINFO ();
  125. doc.filename = name.ToString();
  126. doc.settings = settings;
  127. doc_info.Add (graphics, doc);
  128. return graphics;
  129. }
  130. // Properties
  131. internal override PrinterSettings.StringCollection InstalledPrinters {
  132. get {
  133. int n_printers;
  134. IntPtr printers = IntPtr.Zero, ptr_printers, ptr_printer;
  135. string str;
  136. PrinterSettings.StringCollection col = new PrinterSettings.StringCollection (new string[] {});
  137. n_printers = cupsGetPrinters (ref printers);
  138. ptr_printers = printers;
  139. for (int i = 0; i < n_printers; i++) {
  140. ptr_printer = (IntPtr) Marshal.ReadInt32 (ptr_printers);
  141. str = Marshal.PtrToStringAnsi (ptr_printer);
  142. ptr_printers = new IntPtr (ptr_printers.ToInt64 () + 4);
  143. col.Add (str);
  144. }
  145. Marshal.FreeHGlobal (printers);
  146. return col;
  147. }
  148. }
  149. internal override string DefaultPrinter {
  150. get {
  151. IntPtr str;
  152. str = cupsGetDefault ();
  153. return Marshal.PtrToStringAnsi (str);
  154. }
  155. }
  156. // Private functions
  157. private string GetPaperSizeName (PPD_FILE ppd, string name)
  158. {
  159. string rslt = name;
  160. PPD_GROUP group;
  161. PPD_OPTION option;
  162. PPD_CHOICE choice;
  163. IntPtr ptr, ptr_opt, ptr_choice;
  164. ptr = ppd.groups;
  165. for (int i = 0; i < ppd.num_groups; i++) {
  166. group = (PPD_GROUP) Marshal.PtrToStructure (ptr, typeof (PPD_GROUP));
  167. //Console.WriteLine ("Size text:{0} name:{1} opts {2}", group.text, group.name, group.num_options);
  168. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (group));
  169. ptr_opt = group.options;
  170. for (int n = 0; n < group.num_options; n++) {
  171. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  172. ptr_opt = new IntPtr (ptr_opt.ToInt64 () + Marshal.SizeOf (option));
  173. //Console.WriteLine (" key:{0} def:{1} text: {2}", option.keyword, option.defchoice, option.text);
  174. if (!option.keyword.Equals ("PageSize"))
  175. continue;
  176. ptr_choice = option.choices;
  177. for (int c = 0; c < option.num_choices; c++) {
  178. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  179. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  180. //Console.WriteLine (" choice:{0} - text: {1}", choice.choice, choice.text);
  181. if (name.Equals (choice.choice)) {
  182. rslt = choice.text;
  183. break;
  184. }
  185. }
  186. }
  187. }
  188. return rslt;
  189. }
  190. //
  191. // DllImports
  192. //
  193. [DllImport("libcups", CharSet=CharSet.Ansi)]
  194. static extern int cupsGetPrinters (ref IntPtr printers);
  195. [DllImport("libcups", CharSet=CharSet.Ansi)]
  196. static extern IntPtr cupsTempFile (StringBuilder sb, int len);
  197. [DllImport("libcups", CharSet=CharSet.Ansi)]
  198. static extern IntPtr cupsGetDefault ();
  199. [DllImport("libcups", CharSet=CharSet.Ansi)]
  200. static extern int cupsPrintFile (string printer, string filename, string title, int num_options, IntPtr options);
  201. [DllImport("libcups", CharSet=CharSet.Ansi)]
  202. static extern IntPtr cupsGetPPD (string printer);
  203. [DllImport("libcups", CharSet=CharSet.Ansi)]
  204. static extern IntPtr ppdOpenFile (string filename);
  205. [DllImport("libcups")]
  206. static extern void ppdClose (IntPtr ppd);
  207. [DllImport("libgdiplus", CharSet=CharSet.Ansi)]
  208. static extern int GdipGetPostScriptGraphicsContext (string filename, int with, int height, double dpix, double dpiy, ref IntPtr graphics);
  209. [DllImport("libgdiplus")]
  210. static extern int GdipGetPostScriptSavePage (IntPtr graphics);
  211. //Struct
  212. public struct DOCINFO
  213. {
  214. public PrinterSettings settings;
  215. public string title;
  216. public string filename;
  217. }
  218. public struct PPD_SIZE
  219. {
  220. public int marked;
  221. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  222. public string name;
  223. public float width;
  224. public float length;
  225. public float left;
  226. public float bottom;
  227. public float right;
  228. public float top;
  229. }
  230. public struct PPD_GROUP
  231. {
  232. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)]
  233. public string text;
  234. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  235. public string name;
  236. public int num_options;
  237. public IntPtr options;
  238. public int num_subgroups;
  239. public IntPtr subgrups;
  240. }
  241. public struct PPD_OPTION
  242. {
  243. public byte conflicted;
  244. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  245. public string keyword;
  246. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  247. public string defchoice;
  248. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  249. public string text;
  250. public int ui;
  251. public int section;
  252. public float order;
  253. public int num_choices;
  254. public IntPtr choices;
  255. }
  256. public struct PPD_CHOICE
  257. {
  258. public byte marked;
  259. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  260. public string choice;
  261. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  262. public string text;
  263. public IntPtr code;
  264. public IntPtr option;
  265. }
  266. public struct PPD_FILE
  267. {
  268. public int language_level;
  269. public int color_device;
  270. public int variable_sizes;
  271. public int accurate_screens;
  272. public int contone_only;
  273. public int landscape;
  274. public int model_number;
  275. public int manual_copies;
  276. public int throughput;
  277. public int colorspace;
  278. public IntPtr patches;
  279. public int num_emulations;
  280. public IntPtr emulations;
  281. public IntPtr jcl_begin;
  282. public IntPtr jcl_ps;
  283. public IntPtr jcl_end;
  284. public IntPtr lang_encoding;
  285. public IntPtr lang_version;
  286. public IntPtr modelname;
  287. public IntPtr ttrasterizer;
  288. public IntPtr manufacturer;
  289. public IntPtr product;
  290. public IntPtr nickname;
  291. public IntPtr shortnickname;
  292. public int num_groups;
  293. public IntPtr groups;
  294. public int num_sizes;
  295. public IntPtr sizes;
  296. /* There is more data after this that we are not using*/
  297. }
  298. }
  299. }