printer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. return env;
  133. }
  134. void printerCloseWindow(struct printerEnv env)
  135. {
  136. XCloseDisplay(env.d); // window is automatically destroyed
  137. }
  138. XFontStruct* getFont(struct printerEnv env, enum t_type flags)
  139. {
  140. if(flags & T_ITALIC && flags & T_BOLD)
  141. return env.fontinfo_bi;
  142. if(flags & T_ITALIC)
  143. return env.fontinfo_i;
  144. if(flags & T_BOLD)
  145. return env.fontinfo_b;
  146. return env.fontinfo;
  147. }
  148. // return the width of the text drawn
  149. // look for i and b html tags
  150. // bug when there is several i (or several v) nested
  151. // set in rflags the flags at the end of the text
  152. int drawText(struct printerEnv env, char *text, int size,
  153. int x, int y, enum t_type flags, enum t_type *rflags, int draw)
  154. {
  155. char *i = strnstr(text, "<i>", size),
  156. *b = strnstr(text, "<b>", size),
  157. *begin = NULL, *end = NULL;
  158. int wopentag = 0, wclosetag = 0,
  159. width;
  160. enum t_type nflags = flags;
  161. *rflags = flags;
  162. if(i != NULL && (b == NULL || i < b)) // i is the first
  163. {
  164. wopentag = 3;
  165. begin = i;
  166. end = strnstr(i, "</i>", size - (i - text));
  167. nflags |= T_ITALIC;
  168. if(end != NULL)
  169. wclosetag = 4;
  170. else
  171. *rflags |= T_ITALIC;
  172. }
  173. else if(b != NULL) // b is the first
  174. {
  175. wopentag = 3;
  176. begin = b;
  177. end = strnstr(b, "</b>", size - (b - text));
  178. nflags |= T_BOLD;
  179. if(end != NULL)
  180. wclosetag = 4;
  181. else
  182. *rflags |= T_BOLD;
  183. }
  184. else // no opening tag
  185. {
  186. if(flags & T_ITALIC) // we look for it
  187. i = strnstr(text, "</i>", size);
  188. if(flags & T_BOLD)
  189. b = strnstr(text, "</b>", size);
  190. if(i != NULL && (b == NULL || i < b))
  191. {
  192. wclosetag = 4;
  193. begin = text;
  194. end = i;
  195. nflags |= T_ITALIC;
  196. *rflags &= ~T_ITALIC;
  197. }
  198. else if(b != NULL)
  199. {
  200. wclosetag = 4;
  201. begin = text;
  202. end = b;
  203. nflags |= T_BOLD;
  204. *rflags &= ~T_BOLD;
  205. }
  206. }
  207. enum t_type dummy;
  208. if(begin != NULL && end != NULL)
  209. {
  210. // before
  211. width = drawText(env, text, begin - text, x, y, flags, &dummy, draw);
  212. // middle
  213. width += drawText(env, begin + wopentag, end - begin - wopentag,
  214. x + width, y, nflags, &dummy, draw);
  215. // after
  216. width += drawText(env, end + wclosetag, size - (end + wclosetag - begin),
  217. x + width, y, flags, rflags, draw);
  218. }
  219. else if(begin != NULL)
  220. {
  221. // before
  222. width = drawText(env, text, begin - text, x, y, flags, &dummy, draw);
  223. // middle
  224. width += drawText(env, begin + wopentag, size - (begin + wopentag - text),
  225. x + width, y, nflags, &dummy, draw);
  226. *rflags |= dummy;
  227. }
  228. else
  229. {
  230. int font_direction, font_ascent, font_descent;
  231. XCharStruct text_structure;
  232. XTextExtents(getFont(env, flags), text, size,
  233. &font_direction, &font_ascent, &font_descent,
  234. &text_structure);
  235. width = text_structure.width;
  236. if(draw)
  237. {
  238. XSetFont(env.d, env.gc, getFont(env, flags)->fid);
  239. XDrawString(env.d, env.w, env.gc, x, y, text, size);
  240. }
  241. }
  242. return width;
  243. }
  244. // return a the max width
  245. int printLines(struct printerEnv env, char *text, int y, enum t_type flags,
  246. int draw)
  247. {
  248. char *next;
  249. int size;
  250. int width = 0;
  251. next = strchr(text, '\n');
  252. if(next == NULL)
  253. {
  254. size = strlen(text);
  255. next = text + size;
  256. }
  257. else
  258. {
  259. size = next-text;
  260. if(*(next-1) == '\r')
  261. size--; // hide this character
  262. next++; // forget \n
  263. }
  264. // compute the size of the text
  265. enum t_type dummy;
  266. int tmp = drawText(env, text, size, 0, 0, flags, &dummy, 0);
  267. // print line
  268. if(draw)
  269. drawText(env, text, size, (env.width - tmp)/2, y, flags, &flags, 1);
  270. else
  271. flags = dummy;
  272. // print next lines
  273. if(*next != '\0')
  274. width = printLines(env, next, y + env.maxascent + env.maxdescent
  275. + env.gap, flags, draw);
  276. if(tmp > width)
  277. return tmp;
  278. return width;
  279. }
  280. void printerShow(struct printerEnv *env, char* text, enum t_type font)
  281. {
  282. int nlines = 1;
  283. char *p = text;
  284. // remap the window
  285. XMapWindow(env->d, env->w);
  286. while((p = strchr(p, '\n')) != NULL)
  287. {
  288. p++;
  289. if(*p == '\0')
  290. break;
  291. nlines++;
  292. }
  293. // width and height
  294. env->width = printLines(*env, text, 0, font, 0) + 2 * env->padding;
  295. env->height = nlines * (env->maxascent + env->maxdescent + env->gap)
  296. - env->gap + 2 * env->padding;
  297. XClearWindow(env->d, env->w);
  298. XMoveResizeWindow(env->d, env->w, (env->root_width - env->width)/2,
  299. env->root_height - env->margin_bottom - env->height, env->width,
  300. env->height);
  301. // frame
  302. XSetForeground(env->d, env->gc, env->color_background);
  303. XFillRectangle(env->d, env->w, env->gc, 0, 0, env->width, env->height);
  304. XSetForeground(env->d, env->gc, env->color_text);
  305. // text
  306. printLines(*env, text, env->padding + env->maxascent, font, 1);
  307. XFlush(env->d);
  308. }
  309. void printerClean(struct printerEnv env)
  310. {
  311. XClearWindow(env.d, env.w);
  312. XUnmapWindow(env.d, env.w);
  313. XFlush(env.d);
  314. }