printer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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(char *font, char *font_i, char *font_b,
  57. 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. env.root_width = RootAttr.width;
  73. env.root_height = RootAttr.height;
  74. // set window attributes
  75. XSetWindowAttributes attr;
  76. attr.colormap = XCreateColormap(env.d, RootWindow(env.d, env.s),
  77. vinfo.visual, AllocNone);
  78. attr.border_pixel = 0;
  79. attr.background_pixel = 0;
  80. attr.override_redirect = 1; // no window manager border
  81. // set colors
  82. env.color_text = XWhitePixel(env.d, env.s);
  83. XColor tmp;
  84. XParseColor(env.d, attr.colormap, "#222222", &tmp);
  85. XAllocColor(env.d, attr.colormap, &tmp);
  86. env.color_background = tmp.pixel;
  87. //unsigned short r = 34, g = 34, b = 34, a = 192; // TODO where is the doc ?
  88. //env.color_background = a*256*256*256 + r*256*256 + g*256 + b;
  89. // create the window
  90. env.width = 1;
  91. env.height = 1;
  92. env.w = XCreateWindow(env.d, RootWindow(env.d, env.s), 0, 0, env.width,
  93. env.height, 0, vinfo.depth, InputOutput, vinfo.visual,
  94. CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, &attr);
  95. // setting fonts
  96. char *fontname = (font == NULL) ?
  97. "-bitstream-bitstream charter-medium-r-normal--*-280-100-100-p-*-iso8859-1"
  98. : font;
  99. env.fontinfo = XLoadQueryFont(env.d, fontname);
  100. if(!env.fontinfo)
  101. {
  102. fprintf (stderr, "unable to load font %s: using fixed\n", fontname);
  103. env.fontinfo = XLoadQueryFont(env.d, "fixed");
  104. }
  105. env.maxascent = env.fontinfo->ascent;
  106. env.maxdescent = env.fontinfo->descent;
  107. loadFont(&env, &env.fontinfo_i, (font_i == NULL) ?
  108. "-bitstream-bitstream charter-medium-i-normal--*-280-100-100-p-*-iso8859-1"
  109. : font_i);
  110. loadFont(&env, &env.fontinfo_b, (font_b == NULL) ?
  111. "-bitstream-bitstream charter-bold-r-normal--*-280-100-100-p-*-iso8859-1"
  112. : font_b);
  113. loadFont(&env, &env.fontinfo_bi, (font_bi == NULL) ?
  114. "-bitstream-bitstream charter-bold-i-normal--*-280-100-100-p-*-iso8859-1"
  115. : font_bi);
  116. // create GC
  117. XGCValues gr_values;
  118. gr_values.font = env.fontinfo->fid;
  119. gr_values.foreground = XWhitePixel(env.d, env.s);
  120. gr_values.background = XBlackPixel(env.d, env.s);
  121. env.gc = XCreateGC(env.d, env.w, GCFont | GCForeground, &gr_values);
  122. // event to be listened
  123. //XSelectInput(env.d, env.w, ExposureMask | KeyPressMask);
  124. /*
  125. XEvent e;
  126. while(XCheckWindowEvent(env.d, env.w, KeyPressMask, &e))
  127. {
  128. if(e.type == KeyPress && XLookupKeysym(&(e.xkey), 0) == XK_space)
  129. printf("PAUSE\n");
  130. }
  131. */
  132. // display the window
  133. XMapWindow(env.d, env.w);
  134. XFlush(env.d);
  135. return env;
  136. }
  137. void printerCloseWindow(struct printerEnv env)
  138. {
  139. XCloseDisplay(env.d); // window is automatically destroyed
  140. }
  141. XFontStruct* getFont(struct printerEnv env, enum t_type flags)
  142. {
  143. if(flags & T_ITALIC && flags & T_BOLD)
  144. return env.fontinfo_bi;
  145. if(flags & T_ITALIC)
  146. return env.fontinfo_i;
  147. if(flags & T_BOLD)
  148. return env.fontinfo_b;
  149. return env.fontinfo;
  150. }
  151. // return the width of the text drawn
  152. // look for i and b html tags
  153. // bug when there is several i (or several v) nested
  154. // set in rflags the flags at the end of the text
  155. int drawText(struct printerEnv env, char *text, int size,
  156. int x, int y, enum t_type flags, enum t_type *rflags, int draw)
  157. {
  158. char *i = strnstr(text, "<i>", size),
  159. *b = strnstr(text, "<b>", size),
  160. *begin = NULL, *end = NULL;
  161. int wopentag = 0, wclosetag = 0,
  162. width;
  163. enum t_type nflags = flags;
  164. *rflags = flags;
  165. if(i != NULL && (b == NULL || i < b)) // i is the first
  166. {
  167. wopentag = 3;
  168. begin = i;
  169. end = strnstr(i, "</i>", size - (i - text));
  170. nflags |= T_ITALIC;
  171. if(end != NULL)
  172. wclosetag = 4;
  173. else
  174. *rflags |= T_ITALIC;
  175. }
  176. else if(b != NULL) // b is the first
  177. {
  178. wopentag = 3;
  179. begin = b;
  180. end = strnstr(b, "</b>", size - (b - text));
  181. nflags |= T_BOLD;
  182. if(end != NULL)
  183. wclosetag = 4;
  184. else
  185. *rflags |= T_BOLD;
  186. }
  187. else // no opening tag
  188. {
  189. if(flags & T_ITALIC) // we look for it
  190. i = strnstr(text, "</i>", size);
  191. if(flags & T_BOLD)
  192. b = strnstr(text, "</b>", size);
  193. if(i != NULL && (b == NULL || i < b))
  194. {
  195. wclosetag = 4;
  196. begin = text;
  197. end = i;
  198. nflags |= T_ITALIC;
  199. *rflags &= ~T_ITALIC;
  200. }
  201. else if(b != NULL)
  202. {
  203. wclosetag = 4;
  204. begin = text;
  205. end = b;
  206. nflags |= T_BOLD;
  207. *rflags &= ~T_BOLD;
  208. }
  209. }
  210. enum t_type dummy;
  211. if(begin != NULL && end != NULL)
  212. {
  213. // before
  214. width = drawText(env, text, begin - text, x, y, flags, &dummy, draw);
  215. // middle
  216. width += drawText(env, begin + wopentag, end - begin - wopentag,
  217. x + width, y, nflags, &dummy, draw);
  218. // after
  219. width += drawText(env, end + wclosetag, size - (end + wclosetag - begin),
  220. x + width, y, flags, rflags, draw);
  221. }
  222. else if(begin != NULL)
  223. {
  224. // before
  225. width = drawText(env, text, begin - text, x, y, flags, &dummy, draw);
  226. // middle
  227. width += drawText(env, begin + wopentag, size - (begin + wopentag - text),
  228. x + width, y, nflags, &dummy, draw);
  229. *rflags |= dummy;
  230. }
  231. else
  232. {
  233. int font_direction, font_ascent, font_descent;
  234. XCharStruct text_structure;
  235. XTextExtents(getFont(env, flags), text, size,
  236. &font_direction, &font_ascent, &font_descent,
  237. &text_structure);
  238. width = text_structure.width;
  239. if(draw)
  240. {
  241. XSetFont(env.d, env.gc, getFont(env, flags)->fid);
  242. XDrawString(env.d, env.w, env.gc, x, y, text, size);
  243. }
  244. }
  245. return width;
  246. }
  247. // return a the max width
  248. int printLines(struct printerEnv env, char *text, int y, enum t_type flags,
  249. int draw)
  250. {
  251. char *next;
  252. int size;
  253. int width = 0;
  254. next = strchr(text, '\n');
  255. if(next == NULL)
  256. {
  257. size = strlen(text);
  258. next = text + size;
  259. }
  260. else
  261. {
  262. size = next-text;
  263. if(*(next-1) == '\r')
  264. size--; // hide this character
  265. next++; // forget \n
  266. }
  267. // compute the size of the text
  268. enum t_type dummy;
  269. int tmp = drawText(env, text, size, 0, 0, flags, &dummy, 0);
  270. // print line
  271. if(draw)
  272. drawText(env, text, size, (env.width - tmp)/2, y, flags, &flags, 1);
  273. else
  274. flags = dummy;
  275. // print next lines
  276. if(*next != '\0')
  277. width = printLines(env, next, y + env.maxascent + env.maxdescent
  278. + env.gap, flags, draw);
  279. if(tmp > width)
  280. return tmp;
  281. return width;
  282. }
  283. void printerShow(struct printerEnv *env, char* text, enum t_type font)
  284. {
  285. int nlines = 1;
  286. char *p = text;
  287. while((p = strchr(p, '\n')) != NULL)
  288. {
  289. p++;
  290. if(*p == '\0')
  291. break;
  292. nlines++;
  293. }
  294. // width and height
  295. env->width = printLines(*env, text, 0, font, 0) + 2 * env->padding;
  296. env->height = nlines * (env->maxascent + env->maxdescent + env->gap)
  297. - env->gap + 2 * env->padding;
  298. XClearWindow(env->d, env->w);
  299. XMoveResizeWindow(env->d, env->w, (env->root_width - env->width)/2,
  300. env->root_height - env->margin_bottom - env->height, env->width,
  301. env->height);
  302. // frame
  303. XSetForeground(env->d, env->gc, env->color_background);
  304. XFillRectangle(env->d, env->w, env->gc, 0, 0, env->width, env->height);
  305. XSetForeground(env->d, env->gc, env->color_text);
  306. // text
  307. printLines(*env, text, env->padding + env->maxascent, font, 1);
  308. XFlush(env->d);
  309. }
  310. void printerClean(struct printerEnv env)
  311. {
  312. XClearWindow(env.d, env.w);
  313. XFlush(env.d);
  314. }