PageRenderTime 33ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/coot-0.6.2/ccp4mg-utils/pygl/x11_font.cc

#
C++ | 323 lines | 233 code | 59 blank | 31 comment | 43 complexity | 5cae02ecf450892f5c2f06f9734deeae MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. pygl/x11_font.cc: CCP4MG Molecular Graphics Program
  3. Copyright (C) 2001-2008 University of York, CCLRC
  4. This library is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License
  6. version 3, modified in accordance with the provisions of the
  7. license to address the requirements of UK law.
  8. You should have received a copy of the modified GNU Lesser General
  9. Public License along with this library. If not, copies may be
  10. downloaded from http://www.ccp4.ac.uk/ccp4license.php
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. */
  16. #ifdef USE_GLX
  17. #include "font_info.h"
  18. #include "x11_font.h"
  19. #include <mgutil.h>
  20. #include <iostream>
  21. #include <vector>
  22. #include <string.h>
  23. void FreeX11FontPath(char **paths){
  24. XFreeFontPath(paths);
  25. }
  26. char **GetX11FontPath(int *npaths){
  27. Display *dpy = XOpenDisplay(0);
  28. if(!dpy) return 0;
  29. char **fp = XGetFontPath(dpy,npaths);
  30. XCloseDisplay(dpy);
  31. return fp;
  32. }
  33. int LoadAllX11Fonts(){
  34. int nfonts;
  35. Display *dpy = XOpenDisplay(0);
  36. if(!dpy) return -1;
  37. char **cfonts = XListFonts(dpy, "*--0-0-*iso8859-*", -1, &nfonts);
  38. if(nfonts==0||!cfonts){
  39. cfonts = XListFonts(dpy, "*--10-*-*iso8859-*", -1, &nfonts);
  40. }
  41. if(nfonts==0||!cfonts){
  42. cfonts = XListFonts(dpy, "*--11-*-*iso8859-*", -1, &nfonts);
  43. }
  44. if(nfonts==0||!cfonts){
  45. cfonts = XListFonts(dpy, "*--12-*-*iso8859-*", -1, &nfonts);
  46. }
  47. if(nfonts==0||!cfonts){
  48. cfonts = XListFonts(dpy, "*iso8859-*", -1, &nfonts);
  49. }
  50. MGFontInfo finfo;
  51. for(int i=0;i<nfonts;i++){
  52. std::vector<std::string> split_name = ParseX11Name(cfonts[i]);
  53. if(std::string(cfonts[i]).find(std::string("omegaserif"))==std::string::npos){
  54. if(FontCache::isFamilyCached(split_name[1])==-1){
  55. //std::cout << "Loading " << cfonts[i] << "\n"; std::cout.flush();
  56. finfo = LoadXFont(dpy,cfonts[i]);
  57. if(finfo.isValid()){
  58. //std::cout << "Adding " << cfonts[i] << "\n"; std::cout.flush();
  59. FontCache::AddFont(finfo);
  60. }
  61. }
  62. }
  63. }
  64. if(cfonts) XFreeFontNames(cfonts);
  65. XCloseDisplay(dpy);
  66. return 0;
  67. }
  68. void FillBitmap (Display *dpy, Window win, GC gc,
  69. unsigned int width, unsigned int height,
  70. int x0, int y0, char c, unsigned char *bitmap)
  71. {
  72. XImage *image;
  73. unsigned int x, y;
  74. Pixmap pixmap;
  75. pixmap = XCreatePixmap (dpy, win, 8*width, height, 1);
  76. XSetForeground(dpy, gc, 0);
  77. XFillRectangle (dpy, pixmap, gc, 0, 0, 8*width, height);
  78. XSetForeground(dpy, gc, 1);
  79. XDrawString (dpy, pixmap, gc, x0, y0, &c, 1); // No unicode ??
  80. image = XGetImage (dpy, pixmap, 0, 0, 8*width, height, 1, XYPixmap);
  81. /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
  82. for (y = 0; y < height; y++)
  83. for (x = 0; x < 8*width; x++)
  84. if (XGetPixel (image, x, y))
  85. bitmap[width*(height - y - 1) + x/8] |= (1 << (7 - (x % 8)));
  86. XFreePixmap (dpy, pixmap);
  87. XDestroyImage (image);
  88. }
  89. MGFontInfo LoadXFont(Display *dpy, const char *name_in){
  90. //std::cout << "LoadXFont: " << name_in << "\n";
  91. if(!dpy) return MGFontInfo();
  92. MGFontInfo finfo;
  93. std::string name = std::string(name_in);
  94. GC gc;
  95. XGCValues values;
  96. unsigned long valuemask;
  97. XFontStruct *fs;
  98. fs = XLoadQueryFont(dpy, name.c_str());
  99. if(!fs) {
  100. //std::cout << "XLoadQueryFont failed\n";
  101. std::vector<std::string> split_name = ParseX11Name(name);
  102. if(split_name[1]==""||split_name[2]==""||split_name[3]==""||split_name[4]=="")
  103. return finfo;
  104. if(split_name[0]=="")
  105. split_name[0]="*";
  106. size_t t = split_name[4].find('*');
  107. if(t!=std::string::npos)
  108. split_name[4] = split_name[4].substr(0,t);
  109. std::string newname = std::string("-") + split_name[0] + std::string("-") + split_name[1] + std::string("-") + split_name[2] + std::string("-") + split_name[3] + std::string("-*--") + split_name[4] + std::string("-*-*-*-*-*-iso8859-*");
  110. //std::cout << "newname: " << newname << "\n";
  111. fs = XLoadQueryFont(dpy, newname.c_str());
  112. name = newname;
  113. if(!fs)
  114. return finfo;
  115. }
  116. unsigned int first = fs->min_char_or_byte2;
  117. unsigned int last = fs->max_char_or_byte2;
  118. std::vector <unsigned char *> bm;
  119. std::vector <size_t> widths;
  120. std::vector <size_t> heights;
  121. std::vector <float> lbearings;
  122. std::vector <float> descents;
  123. std::vector <float> dxs;
  124. std::vector <float> dys;
  125. std::string size_str = ParseX11Name(name)[4];
  126. if(size_str=="0"){
  127. for(unsigned int c=first; c<last+1; c++){
  128. unsigned char *bmtmp = new unsigned char[0];
  129. bm.push_back(bmtmp);
  130. widths.push_back(0);
  131. heights.push_back(0);
  132. lbearings.push_back(0.0);
  133. descents.push_back(0.0);
  134. dxs.push_back(0.0);
  135. dys.push_back(0.0);
  136. }
  137. //std::cout << "Returning empty stuff\n"; std::cout.flush();
  138. finfo = MGFontInfo(ParseX11Name(name),bm,widths,heights,lbearings,descents,dxs,dys,first,last);
  139. XFreeFontInfo( 0, fs, 0 );
  140. return finfo;
  141. }
  142. unsigned int max_width, max_height, max_bm_width, max_bm_height;
  143. /* Allocate a bitmap that can fit all characters. */
  144. max_width = fs->max_bounds.rbearing - fs->min_bounds.lbearing;
  145. max_height = fs->max_bounds.ascent + fs->max_bounds.descent;
  146. max_bm_width = (max_width + 7) / 8;
  147. max_bm_height = max_height;
  148. values.foreground = BlackPixel (dpy, DefaultScreen (dpy));
  149. values.background = WhitePixel (dpy, DefaultScreen (dpy));
  150. values.font = fs->fid; // THIS IS IMPORTANT BIT, it decides what XDrawString does !!!
  151. valuemask = GCForeground | GCBackground | GCFont;
  152. gc = XCreateGC (dpy, XCreatePixmap (dpy, RootWindow(dpy,DefaultScreen(dpy)), 10, 10, 1), valuemask, &values);
  153. unsigned int bm_width, bm_height;
  154. size_t width, height;
  155. float dx, dy;
  156. XCharStruct *ch;
  157. int x, y;
  158. for(unsigned int c=first; c<last+1; c++){
  159. if (fs->per_char && (c >= fs->min_char_or_byte2) && (c <= fs->max_char_or_byte2))
  160. ch = &fs->per_char[c-fs->min_char_or_byte2];
  161. else
  162. ch = &fs->max_bounds;
  163. width = ch->rbearing - ch->lbearing;
  164. height = ch->ascent + ch->descent;
  165. dx = ch->width;
  166. dy = 0;
  167. /* X11's starting point. */
  168. x = - ch->lbearing;
  169. y = ch->ascent;
  170. /* Round the width to a multiple of eight. We will use this also
  171. for the pixmap for capturing the X11 font. This is slightly
  172. inefficient, but it makes the OpenGL part real easy. */
  173. bm_width = (width + 7) / 8;
  174. bm_height = height;
  175. bm.push_back(new unsigned char[max_bm_width * max_bm_height]);
  176. widths.push_back(width);
  177. heights.push_back(height);
  178. lbearings.push_back(ch->lbearing);
  179. descents.push_back(ch->descent);
  180. dxs.push_back(dx);
  181. dys.push_back(dy);
  182. if ((c >= fs->min_char_or_byte2) && (c <= fs->max_char_or_byte2)
  183. && (bm_width > 0) && (bm_height > 0))
  184. {
  185. memset(bm.back(), '\0', bm_width * bm_height);
  186. FillBitmap (dpy, RootWindow(dpy,DefaultScreen(dpy)),
  187. gc, bm_width, bm_height, x, y, c, bm.back());
  188. }
  189. }
  190. finfo = MGFontInfo(ParseX11Name(name),bm,widths,heights,lbearings,descents,dxs,dys,first,last);
  191. XFreeFontInfo( 0, fs, 0 );
  192. XFreeGC (dpy, gc);
  193. //std::cout << "XLoadFont gives: " << finfo.fn_size << " " << finfo.family << " " << finfo.weight << " " << finfo.slant << " " << finfo.bm.size() << " " << finfo.widths.size() << " " << finfo.heights.size() << " " << finfo.lbearings.size() << " " << finfo.descents.size() << " " << finfo.dxs.size() << " " << finfo.dys.size() << "\n";
  194. return finfo;
  195. }
  196. MGFontInfo LoadXFont(Display *dpy, const std::string &foundry, const std::string &family, const std::string &weight, const std::string &slant, const std::string &encoding, int size){
  197. std::string name = std::string("-") + foundry + std::string("-") + family + std::string("-") + weight + std::string("-") + slant + std::string("-*--") + IntToString(size) + std::string("*-") + encoding;
  198. return LoadXFont(dpy,name.c_str());
  199. }
  200. MGFontInfo LoadXFont(const std::string &foundry, const std::string &family, const std::string &weight, const std::string &slant, const std::string &encoding, int size){
  201. std::string name = std::string("-") + foundry + std::string("-") + family + std::string("-") + weight + std::string("-") + slant + std::string("-*--") + IntToString(size) + std::string("*-") + encoding;
  202. return LoadXFont(name.c_str());
  203. }
  204. MGFontInfo LoadXFont(const char *name){
  205. Display *dpy = XOpenDisplay(0);
  206. if(!dpy) return MGFontInfo();
  207. MGFontInfo finfo = LoadXFont(dpy,name);
  208. XCloseDisplay(dpy);
  209. return finfo;
  210. }
  211. int FindNextX11FontSize(const MGFontInfo &finfo, int step){
  212. if(!finfo.isValid()) return 0;
  213. Display *dpy = XOpenDisplay(0);
  214. if(!dpy) return 0;
  215. int nfonts;
  216. std::string name;
  217. int this_size;
  218. int isize;
  219. int i;
  220. int nstep = 0;
  221. if((finfo.fn_size+step)>0)
  222. return finfo.Size();
  223. this_size = finfo.Size();
  224. isize = this_size;
  225. std::string newname = std::string("-") + finfo.foundry + std::string("-") + finfo.family + std::string("-") + finfo.weight + std::string("-") + finfo.slant + std::string("-normal--") + std::string("0-*-*-*-*-*-iso8859-*");
  226. XFontStruct *fs = XLoadQueryFont(dpy, newname.c_str());
  227. if(fs){
  228. XFreeFontInfo( 0, fs, 0 );
  229. return isize+step;
  230. }
  231. if(step<0){
  232. for(i=isize-1;i>0;i--){
  233. this_size = i;
  234. name = std::string("*") + finfo.family + std::string("*") + finfo.weight + std::string("*-") + finfo.slant + std::string("-*--") + IntToString(this_size) + std::string("-*");
  235. XListFonts(dpy, name.c_str(), 100, &nfonts);
  236. if(nfonts>0)
  237. nstep--;
  238. if(nstep==step){
  239. XCloseDisplay(dpy);
  240. return this_size;
  241. }
  242. }
  243. }
  244. if(step>0){
  245. for(i=isize+1;i<100;i++){
  246. this_size = i;
  247. name = std::string("*") + finfo.family + std::string("*") + finfo.weight + std::string("*-") + finfo.slant + std::string("-*--") + IntToString(this_size) + std::
  248. string("-*");
  249. XListFonts(dpy, name.c_str(), 100, &nfonts);
  250. if(nfonts>0)
  251. nstep++;
  252. if(nstep==step){
  253. XCloseDisplay(dpy);
  254. return this_size;
  255. }
  256. }
  257. }
  258. XCloseDisplay(dpy);
  259. return finfo.Size();
  260. }
  261. #endif