PrintingServicesUnix.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. if ((printer == null) || (printer == String.Empty)) {
  48. return;
  49. }
  50. ptr = cupsGetPPD (printer);
  51. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  52. ppd_handle = ppdOpenFile (ppd_filename);
  53. //Console.WriteLine ("File: {0}", ppd_filename);
  54. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  55. settings.landscape_angle = ppd.landscape;
  56. settings.supports_color = (ppd.color_device == 0) ? false : true;
  57. ppdClose (ppd_handle);
  58. }
  59. internal override void LoadPrinterResolutions (string printer, PrinterSettings settings)
  60. {
  61. settings.PrinterResolutions.Clear ();
  62. LoadDefaultResolutions (settings.PrinterResolutions);
  63. }
  64. internal override void LoadPrinterPaperSizes (string printer, PrinterSettings settings)
  65. {
  66. IntPtr ptr, ppd_handle;
  67. string ppd_filename, real_name;
  68. PPD_FILE ppd;
  69. PPD_SIZE size;
  70. PaperSize ps;
  71. PaperKind kind = PaperKind.Custom;
  72. settings.PaperSizes.Clear ();
  73. ptr = cupsGetPPD (printer);
  74. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  75. ppd_handle = ppdOpenFile (ppd_filename);
  76. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  77. ptr = ppd.sizes;
  78. float w, h;
  79. for (int i = 0; i < ppd.num_sizes; i++) {
  80. size = (PPD_SIZE) Marshal.PtrToStructure (ptr, typeof (PPD_SIZE));
  81. real_name = GetPaperSizeName (ppd, size.name);
  82. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (size));
  83. w = size.width * 100 / 72;
  84. h = size.length * 100 / 72;
  85. ps = new PaperSize (real_name, (int) w, (int) h);
  86. // TODO: Convert from name to paper kind enum
  87. ps.SetKind (kind);
  88. settings.PaperSizes.Add (ps);
  89. }
  90. ppdClose (ppd_handle);
  91. }
  92. internal override bool StartPage (GraphicsPrinter gr)
  93. {
  94. return true;
  95. }
  96. internal override bool EndPage (GraphicsPrinter gr)
  97. {
  98. GdipGetPostScriptSavePage (gr.Hdc);
  99. return true;
  100. }
  101. internal override bool EndDoc (GraphicsPrinter gr)
  102. {
  103. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  104. gr.Graphics.Dispose (); // Dispose object to force surface finish
  105. cupsPrintFile (doc.settings.PrinterName, doc.filename, doc.title, 0, IntPtr.Zero);
  106. doc_info.Remove (gr.Hdc);
  107. //TODO: Delete temporary file created
  108. return true;
  109. }
  110. internal override bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file)
  111. {
  112. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  113. doc.title = doc_name;
  114. return true;
  115. }
  116. internal override IntPtr CreateGraphicsContext (PrinterSettings settings)
  117. {
  118. IntPtr graphics = IntPtr.Zero;
  119. StringBuilder name = new StringBuilder (1024);
  120. int length = name.Capacity;
  121. cupsTempFile (name, length);
  122. GdipGetPostScriptGraphicsContext (name.ToString(),
  123. settings.DefaultPageSettings.PaperSize.Width / 100 * 72,
  124. settings.DefaultPageSettings.PaperSize.Height / 100 * 72,
  125. // Harcoded dpy's
  126. 300, 300, ref graphics);
  127. DOCINFO doc = new DOCINFO ();
  128. doc.filename = name.ToString();
  129. doc.settings = settings;
  130. doc_info.Add (graphics, doc);
  131. return graphics;
  132. }
  133. // Properties
  134. internal override PrinterSettings.StringCollection InstalledPrinters {
  135. get {
  136. int n_printers;
  137. IntPtr printers = IntPtr.Zero, ptr_printers, ptr_printer;
  138. string str;
  139. PrinterSettings.StringCollection col = new PrinterSettings.StringCollection (new string[] {});
  140. /* FIXME: call is deprecated */
  141. n_printers = cupsGetPrinters (ref printers);
  142. ptr_printers = printers;
  143. for (int i = 0; i < n_printers; i++) {
  144. ptr_printer = (IntPtr) Marshal.ReadInt32 (ptr_printers);
  145. str = Marshal.PtrToStringAnsi (ptr_printer);
  146. Marshal.FreeHGlobal (ptr_printer);
  147. ptr_printers = new IntPtr (ptr_printers.ToInt64 () + 4);
  148. col.Add (str);
  149. }
  150. Marshal.FreeHGlobal (printers);
  151. return col;
  152. }
  153. }
  154. internal override string DefaultPrinter {
  155. get {
  156. IntPtr str;
  157. str = cupsGetDefault ();
  158. return Marshal.PtrToStringAnsi (str);
  159. }
  160. }
  161. // Private functions
  162. private string GetPaperSizeName (PPD_FILE ppd, string name)
  163. {
  164. string rslt = name;
  165. PPD_GROUP group;
  166. PPD_OPTION option;
  167. PPD_CHOICE choice;
  168. IntPtr ptr, ptr_opt, ptr_choice;
  169. ptr = ppd.groups;
  170. for (int i = 0; i < ppd.num_groups; i++) {
  171. group = (PPD_GROUP) Marshal.PtrToStructure (ptr, typeof (PPD_GROUP));
  172. //Console.WriteLine ("Size text:{0} name:{1} opts {2}", group.text, group.name, group.num_options);
  173. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (group));
  174. ptr_opt = group.options;
  175. for (int n = 0; n < group.num_options; n++) {
  176. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  177. ptr_opt = new IntPtr (ptr_opt.ToInt64 () + Marshal.SizeOf (option));
  178. //Console.WriteLine (" key:{0} def:{1} text: {2}", option.keyword, option.defchoice, option.text);
  179. if (!option.keyword.Equals ("PageSize"))
  180. continue;
  181. ptr_choice = option.choices;
  182. for (int c = 0; c < option.num_choices; c++) {
  183. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  184. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  185. //Console.WriteLine (" choice:{0} - text: {1}", choice.choice, choice.text);
  186. if (name.Equals (choice.choice)) {
  187. rslt = choice.text;
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. return rslt;
  194. }
  195. //
  196. // DllImports
  197. //
  198. [DllImport("libcups", CharSet=CharSet.Ansi)]
  199. static extern int cupsGetPrinters (ref IntPtr printers);
  200. [DllImport("libcups", CharSet=CharSet.Ansi)]
  201. static extern IntPtr cupsTempFile (StringBuilder sb, int len);
  202. [DllImport("libcups", CharSet=CharSet.Ansi)]
  203. static extern IntPtr cupsGetDefault ();
  204. [DllImport("libcups", CharSet=CharSet.Ansi)]
  205. static extern int cupsPrintFile (string printer, string filename, string title, int num_options, IntPtr options);
  206. [DllImport("libcups", CharSet=CharSet.Ansi)]
  207. static extern IntPtr cupsGetPPD (string printer);
  208. [DllImport("libcups", CharSet=CharSet.Ansi)]
  209. static extern IntPtr ppdOpenFile (string filename);
  210. [DllImport("libcups")]
  211. static extern void ppdClose (IntPtr ppd);
  212. [DllImport("libgdiplus", CharSet=CharSet.Ansi)]
  213. static extern int GdipGetPostScriptGraphicsContext (string filename, int with, int height, double dpix, double dpiy, ref IntPtr graphics);
  214. [DllImport("libgdiplus")]
  215. static extern int GdipGetPostScriptSavePage (IntPtr graphics);
  216. //Struct
  217. public struct DOCINFO
  218. {
  219. public PrinterSettings settings;
  220. public string title;
  221. public string filename;
  222. }
  223. public struct PPD_SIZE
  224. {
  225. public int marked;
  226. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  227. public string name;
  228. public float width;
  229. public float length;
  230. public float left;
  231. public float bottom;
  232. public float right;
  233. public float top;
  234. }
  235. public struct PPD_GROUP
  236. {
  237. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)]
  238. public string text;
  239. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  240. public string name;
  241. public int num_options;
  242. public IntPtr options;
  243. public int num_subgroups;
  244. public IntPtr subgrups;
  245. }
  246. public struct PPD_OPTION
  247. {
  248. public byte conflicted;
  249. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  250. public string keyword;
  251. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  252. public string defchoice;
  253. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  254. public string text;
  255. public int ui;
  256. public int section;
  257. public float order;
  258. public int num_choices;
  259. public IntPtr choices;
  260. }
  261. public struct PPD_CHOICE
  262. {
  263. public byte marked;
  264. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  265. public string choice;
  266. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  267. public string text;
  268. public IntPtr code;
  269. public IntPtr option;
  270. }
  271. public struct PPD_FILE
  272. {
  273. public int language_level;
  274. public int color_device;
  275. public int variable_sizes;
  276. public int accurate_screens;
  277. public int contone_only;
  278. public int landscape;
  279. public int model_number;
  280. public int manual_copies;
  281. public int throughput;
  282. public int colorspace;
  283. public IntPtr patches;
  284. public int num_emulations;
  285. public IntPtr emulations;
  286. public IntPtr jcl_begin;
  287. public IntPtr jcl_ps;
  288. public IntPtr jcl_end;
  289. public IntPtr lang_encoding;
  290. public IntPtr lang_version;
  291. public IntPtr modelname;
  292. public IntPtr ttrasterizer;
  293. public IntPtr manufacturer;
  294. public IntPtr product;
  295. public IntPtr nickname;
  296. public IntPtr shortnickname;
  297. public int num_groups;
  298. public IntPtr groups;
  299. public int num_sizes;
  300. public IntPtr sizes;
  301. /* There is more data after this that we are not using*/
  302. }
  303. }
  304. }