printer.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. Copyright (C) 2014 Olivier Marty <olivier.marty.m at gmail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. #include "printer.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <X11/Xlib.h>
  19. #include <X11/Xutil.h>
  20. #include <X11/keysym.h>
  21. #include <string.h>
  22. char *strnstr(const char *s, const char *find, size_t slen)
  23. {
  24. char c, sc;
  25. size_t len;
  26. if ((c = *find++) != '\0') {
  27. len = strlen(find);
  28. do {
  29. do {
  30. if ((sc = *s++) == '\0' || slen-- < 1)
  31. return (NULL);
  32. } while (sc != c);
  33. if (len > slen)
  34. return (NULL);
  35. } while (strncmp(s, find, len) != 0);
  36. s--;
  37. }
  38. return ((char *)s);
  39. }
  40. void loadFont(struct printerEnv *env, XFontStruct **font, char* fontname)
  41. {
  42. *font = XLoadQueryFont(env->d, fontname);
  43. if(!*font)
  44. {
  45. fprintf (stderr, "unable to load font %s - using normal font\n", fontname);
  46. *font = env->fontinfo;
  47. }
  48. else
  49. {
  50. if((*font)->ascent > env->maxascent)
  51. env->maxascent = (*font)->ascent;
  52. if((*font)->descent > env->maxdescent)
  53. env->maxdescent = (*font)->descent;
  54. }
  55. }
  56. struct printerEnv printerOpenWindow(int margin_bottom, char *font, char *font_i,
  57. char *font_b, char *font_bi)
  58. {
  59. struct printerEnv env;
  60. // open connection
  61. env.d = XOpenDisplay(NULL);
  62. if (env.d == NULL) {
  63. fprintf(stderr, "Unable to open display\n");
  64. exit(1);
  65. }
  66. env.s = DefaultScreen(env.d);
  67. XVisualInfo vinfo;
  68. XMatchVisualInfo(env.d, env.s, 32, TrueColor, &vinfo);
  69. // size of the screen
  70. XWindowAttributes RootAttr;
  71. XGetWindowAttributes(env.d, RootWindow(env.d, env.s), &RootAttr);
  72. // set window attributes
  73. XSetWindowAttributes attr;
  74. attr.colormap = XCreateColormap(env.d, RootWindow(env.d, env.s),
  75. vinfo.visual, AllocNone);
  76. attr.border_pixel = 0;
  77. attr.background_pixel = 0;
  78. attr.override_redirect = 1; // no window manager border
  79. // set colors
  80. env.color_text = XWhitePixel(env.d, env.s);
  81. XColor tmp;
  82. XParseColor(env.d, attr.colormap, "#222222", &tmp);
  83. XAllocColor(env.d, attr.colormap, &tmp);
  84. env.color_background = tmp.pixel;
  85. //unsigned short r = 34, g = 34, b = 34, a = 192; // TODO where is the doc ?
  86. //env.color_background = a*256*256*256 + r*256*256 + g*256 + b;
  87. // create the window
  88. env.margin_bottom = margin_bottom;
  89. env.root_width = RootAttr.width;
  90. env.root_height = RootAttr.height;
  91. env.width = 1;
  92. env.height = 1;
  93. env.w = XCreateWindow(env.d, RootWindow(env.d, env.s), 0, 0, env.width,
  94. env.height, 0, vinfo.depth, InputOutput, vinfo.visual,
  95. CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, &attr);
  96. // setting fonts
  97. char *fontname = (font == NULL) ?
  98. "-bitstream-bitstream charter-medium-r-normal--*-280-100-100-p-*-iso8859-1"
  99. : font;
  100. env.fontinfo = XLoadQueryFont(env.d, fontname);
  101. if(!env.fontinfo)
  102. {
  103. fprintf (stderr, "unable to load font %s: using fixed\n", fontname);
  104. env.fontinfo = XLoadQueryFont(env.d, "fixed");
  105. }
  106. env.maxascent = env.fontinfo->ascent;
  107. env.maxdescent = env.fontinfo->descent;
  108. loadFont(&env, &env.fontinfo_i, (font_i == NULL) ?
  109. "-bitstream-bitstream charter-medium-i-normal--*-280-100-100-p-*-iso8859-1"
  110. : font_i);
  111. loadFont(&env, &env.fontinfo_b, (font_b == NULL) ?
  112. "-bitstream-bitstream charter-bold-r-normal--*-280-100-100-p-*-iso8859-1"
  113. : font_b);
  114. loadFont(&env, &env.fontinfo_bi, (font_bi == NULL) ?
  115. "-bitstream-bitstream charter-bold-i-normal--*-280-100-100-p-*-iso8859-1"
  116. : font_bi);
  117. // create GC
  118. XGCValues gr_values;
  119. gr_values.font = env.fontinfo->fid;
  120. gr_values.foreground = XWhitePixel(env.d, env.s);
  121. gr_values.background = XBlackPixel(env.d, env.s);
  122. env.gc = XCreateGC(env.d, env.w, GCFont | GCForeground, &gr_values);
  123. // event to be listened
  124. //XSelectInput(env.d, env.w, ExposureMask | KeyPressMask);
  125. /*
  126. XEvent e;
  127. while(XCheckWindowEvent(env.d, env.w, KeyPressMask, &e))
  128. {
  129. if(e.type == KeyPress && XLookupKeysym(&(e.xkey), 0) == XK_space)
  130. printf("PAUSE\n");
  131. }
  132. */
  133. // display the window
  134. XMapWindow(env.d, env.w);
  135. XFlush(env.d);
  136. return env;
  137. }
  138. void printerCloseWindow(struct printerEnv env)
  139. {
  140. XCloseDisplay(env.d); // window is automatically destroyed
  141. }
  142. XFontStruct* getFont(struct printerEnv env, enum t_type flags)
  143. {
  144. if(flags & T_ITALIC && flags & T_BOLD)
  145. return env.fontinfo_bi;
  146. if(flags & T_ITALIC)
  147. return env.fontinfo_i;
  148. if(flags & T_BOLD)
  149. return env.fontinfo_b;
  150. return env.fontinfo;
  151. }
  152. // return the width of the text drawn
  153. // look for i and b html tags
  154. // bug when there is several i (or several v) nested
  155. // set in rflags the flags at the end of the text
  156. int drawText(struct printerEnv env, char *text, int size,
  157. int x, int y, enum t_type flags, enum t_type *rflags, int draw)
  158. {
  159. char *i = strnstr(text, "<i>", size),
  160. *b = strnstr(text, "<b>", size),
  161. *begin = NULL, *end = NULL;
  162. int wopentag = 0, wclosetag = 0,
  163. width;
  164. enum t_type nflags = flags;
  165. *rflags = flags;
  166. if(i != NULL && (b == NULL || i < b)) // i is the first
  167. {
  168. wopentag = 3;
  169. begin = i;
  170. end = strnstr(i, "</i>", size - (i - text));
  171. nflags |= T_ITALIC;
  172. if(end != NULL)
  173. wclosetag = 4;
  174. else
  175. *rflags |= T_ITALIC;
  176. }
  177. else if(b != NULL) // b is the first
  178. {
  179. wopentag = 3;
  180. begin = b;
  181. end = strnstr(b, "</b>", size - (b - text));
  182. nflags |= T_BOLD;
  183. if(end != NULL)
  184. wclosetag = 4;
  185. else
  186. *rflags |= T_BOLD;
  187. }
  188. else // no opening tag
  189. {
  190. if(flags & T_ITALIC) // we look for it
  191. i = strnstr(text, "</i>", size);
  192. if(flags & T_BOLD)
  193. b = strnstr(text, "</b>", size);
  194. if(i != NULL && (b == NULL || i < b))
  195. {
  196. wclosetag = 4;
  197. begin = text;
  198. end = i;
  199. nflags |= T_ITALIC;
  200. *rflags &= ~T_ITALIC;
  201. }
  202. else if(b != NULL)
  203. {
  204. wclosetag = 4;
  205. begin = text;
  206. end = b;
  207. nflags |= T_BOLD;
  208. *rflags &= ~T_BOLD;
  209. }
  210. }
  211. enum t_type dummy;
  212. if(begin != NULL && end != NULL)
  213. {
  214. // before
  215. width = drawText(env, text, begin - text, x, y, flags, &dummy, draw);
  216. // middle
  217. width += drawText(env, begin + wopentag, end - begin - wopentag,
  218. x + width, y, nflags, &dummy, draw);
  219. // after
  220. width += drawText(env, end + wclosetag, size - (end + wclosetag - begin),
  221. x + width, y, flags, rflags, draw);
  222. }
  223. else if(begin != NULL)
  224. {
  225. // before
  226. width = drawText(env, text, begin - text, x, y, flags, &dummy, draw);
  227. // middle
  228. width += drawText(env, begin + wopentag, size - (begin + wopentag - text),
  229. x + width, y, nflags, &dummy, draw);
  230. *rflags |= dummy;
  231. }
  232. else
  233. {
  234. int font_direction, font_ascent, font_descent;
  235. XCharStruct text_structure;
  236. XTextExtents(getFont(env, flags), text, size,
  237. &font_direction, &font_ascent, &font_descent,
  238. &text_structure);
  239. width = text_structure.width;
  240. if(draw)
  241. {
  242. XSetFont(env.d, env.gc, getFont(env, flags)->fid);
  243. XDrawString(env.d, env.w, env.gc, x, y, text, size);
  244. }
  245. }
  246. return width;
  247. }
  248. // return a the max width
  249. int printLines(struct printerEnv env, char *text, int gap, int y,
  250. enum t_type flags, int draw)
  251. {
  252. char *next;
  253. int size;
  254. int width = 0;
  255. next = strchr(text, '\n');
  256. if(next == NULL)
  257. {
  258. size = strlen(text);
  259. next = text + size;
  260. }
  261. else
  262. {
  263. size = next-text;
  264. if(*(next-1) == '\r')
  265. size--; // hide this character
  266. next++; // forget \n
  267. }
  268. // compute the size of the text
  269. enum t_type dummy;
  270. int tmp = drawText(env, text, size, 0, 0, flags, &dummy, 0);
  271. // print line
  272. if(draw)
  273. drawText(env, text, size, (env.width - tmp)/2, y, flags, &flags, 1);
  274. else
  275. flags = dummy;
  276. // print next lines
  277. if(*next != '\0')
  278. width = printLines(env, next, gap, y + env.maxascent + env.maxdescent + gap,
  279. flags, draw);
  280. if(tmp > width)
  281. return tmp;
  282. return width;
  283. }
  284. void printerShow(struct printerEnv *env, char* text, enum t_type font)
  285. {
  286. int gap = 5, padding = 5;
  287. int nlines = 1;
  288. char *p = text;
  289. while((p = strchr(p, '\n')) != NULL)
  290. {
  291. p++;
  292. if(*p == '\0')
  293. break;
  294. nlines++;
  295. }
  296. // width and height
  297. env->width = printLines(*env, text, gap, 0, font, 0) + 2*padding;
  298. env->height = nlines * (env->maxascent + env->maxdescent + gap) - gap +
  299. 2*padding;
  300. XClearWindow(env->d, env->w);
  301. XMoveResizeWindow(env->d, env->w, (env->root_width - env->width)/2,
  302. env->root_height - env->margin_bottom - env->height, env->width,
  303. env->height);
  304. // frame
  305. XSetForeground(env->d, env->gc, env->color_background);
  306. XFillRectangle(env->d, env->w, env->gc, 0, 0, env->width, env->height);
  307. XSetForeground(env->d, env->gc, env->color_text);
  308. // text
  309. printLines(*env, text, gap, padding + env->maxascent, font, 1);
  310. XFlush(env->d);
  311. }
  312. void printerClean(struct printerEnv env)
  313. {
  314. XClearWindow(env.d, env.w);
  315. XFlush(env.d);
  316. }