x_retrieve.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains the Plotter-specific _retrieve_font method, which is
  16. called by the _pl_g_set_font() function, which in turn is invoked by the
  17. API functions alabel() and flabelwidth(). It is called when the
  18. font_name, font_size, and textangle fields of the current drawing state
  19. have been filled in. It retrieves the specified font, and fills in the
  20. font_type, typeface_index, font_index, font_is_iso8858, true_font_size,
  21. and font_ascent, and font_descent fields of the drawing state. */
  22. /* This version is for XDrawablePlotters (and XPlotters). It also fills in
  23. the x_font_struct and x_font_pixel_size fields of the drawing state,
  24. which are X-specific.
  25. This version operates by retrieving an 8-bit core X font from the X
  26. display, as follows. First, it looks at the x_label field of the
  27. drawing state, which may contain a "hint": a string passed down from a
  28. higher level, containing the characters that will eventually be rendered
  29. in the font. This is for efficiency. If possible, only a subset of the
  30. font will be retrieved (most X displays support this).
  31. Nearly all core X fonts have XLFD (X Logical Font Description) names.
  32. So we run through various possibilities: first we try to interpret the
  33. font_name parameter as the name of a PS font in libplot's hardcoded font
  34. database (e.g. "Times-Roman"), in which case the database will supply a
  35. corresponding base XLFD name (e.g., "times-medium-r-normal"); then we
  36. try to interpret font_name as a base XLFD name which is not in the
  37. hardcoded database (e.g. "charter-medium-r-normal"); then finally we try
  38. to interpret it as a non-XLFD font name (e.g., "fixed" or "9x15"). In
  39. each of these cases, we try to retrieve only a subset of the font (as
  40. specified by the x_label field, just mentioned); if that fails, an
  41. attempt is made to retrieve the entire font.
  42. Text strings, including rotated and affinely transformed text strings,
  43. will eventually be rendered in x_text.c, using the XAffText module in
  44. x_afftext.c. So here, all we do is retrieve a reasonable pixel size for
  45. the font, which will be scaled, etc., as needed. (Note that the choice
  46. we make for the pixel size does not depend on the `textangle' drawing
  47. parameter, which is the rotation angle of the text in the user frame;
  48. though the XAffText module will pay attention to that parameter.) The
  49. XAffText module will take care of scaling, so we set the true_font_size
  50. parameter to be the same as the user-specified font_size.
  51. Note: For speed, we maintain a linked-list cache of previously
  52. rasterized-and-retrieved fonts. The linked list is accessible via the
  53. x_font_list member of the XDrawablePlotter (or XPlotter). An
  54. ever-growing linked list is probably good enough if there aren't huge
  55. numbers of font or font size changes. It's deallocated when the Plotter
  56. is destroyed; see x_defplot.c. */
  57. #include "sys-defines.h"
  58. #include "extern.h"
  59. #include "g_her_metr.h"
  60. /* max length user-level font name we'll accept; this may be either an XLFD
  61. base name (a string with exactly three hyphens in it), or something else
  62. (an alias, a pre-XLFD font name, etc.). */
  63. #define MAX_USER_FONT_NAME_LENGTH 200
  64. /* length of buffer for constructing an X font name; must be large enough
  65. to handle XFLD template, user-level font name */
  66. #define MAX_FONT_NAME_LENGTH 255
  67. /* XLFD templates, with holes into which we can punch the base XLFD name (a
  68. string with exactly three hyphens in it) and the integer size in terms
  69. of pixels. We need a Latin-1 one and a generic one, since e.g. the
  70. Symbol font, which we support, has "adobe-fontspecific" as its last two
  71. fields. */
  72. static const char * const xlfd_template_latin_1 = "-*-%s-*-%d-*-*-*-*-*-iso8859-1";
  73. static const char * const xlfd_template_generic = "-*-%s-*-%d-*-*-*-*-*-*-*";
  74. /* length of buffer for constructing an X11R6-style list-of-charset-ranges
  75. string, e.g. "[32 48_57 65_90]" would represent the non-contiguous set
  76. of characters [ 0-9A-Z]. */
  77. #define MAX_CHARSET_SUBSET_LIST_LENGTH 767
  78. #define PL_NUM_XLFD_FIELDS 14
  79. #define XLFD_FIELD_FOUNDRY 0
  80. #define XLFD_FIELD_FAMILY 1
  81. #define XLFD_FIELD_WEIGHT 2
  82. #define XLFD_FIELD_SLANT 3
  83. #define XLFD_FIELD_SET_WIDTH 4
  84. #define XLFD_FIELD_ADDITIONAL_STYLE 5
  85. #define XLFD_FIELD_PIXELS 6
  86. #define XLFD_FIELD_POINTS 7
  87. #define XLFD_FIELD_HORIZONTAL_RESOLUTION 8
  88. #define XLFD_FIELD_VERTICAL_RESOLUTION 9
  89. #define XLFD_FIELD_SPACING 10
  90. #define XLFD_FIELD_AVERAGE_WIDTH 11
  91. #define XLFD_FIELD_CHARACTER_SET_MAJOR 12
  92. #define XLFD_FIELD_CHARACTER_SET_MINOR 13 /* in X11R6 may include char subset */
  93. /* forward references */
  94. static bool is_a_subset (unsigned char set1[32], unsigned char set2[32]);
  95. static char *xlfd_field (const char *name, int field);
  96. static double min_sing_val (double m[4]);
  97. static void print_bitvector (unsigned char v[32], char *s);
  98. static void set_font_dimensions (Display *dpy, plXFontRecord *fptr);
  99. static void string_to_bitvector (const unsigned char *s, unsigned char v[8]);
  100. static plXFontRecord *select_x_font (Display *dpy, plXFontRecord **x_fontlist_ptr, const char *name, const unsigned char *s, bool subsetting);
  101. /* _pl_x_retrieve_font() attempts to retrieve a core X font specified by a
  102. triple, namely {name, size, rotation}. The rotation parameter is
  103. ignored, since the XAffText module will be used to rotate or transform
  104. any rendered string (see x_text.c, and x_afftext.c for the module).
  105. Four possible font retrievals are attempted, in order.
  106. 1. `name' is taken to be an alias for an XLFD base name, as listed in
  107. libplot's hardcoded font database in g_fontdb.c. (Aliases for the 35
  108. standard font names appear there. E.g., name="times-roman" or
  109. "Times-Roman" is an alias for the three-hyphen XLFD base name
  110. "times-roman-r-normal".)
  111. 2. `name' is taken to be an XLFD base name, of the form
  112. foundry-family-weight-slant-width, with exactly four hyphens. E.g.,
  113. name="adobe-times-roman-r-normal" or
  114. name="bitstream-courier-medium-r-normal". NOT YET IMPLEMENTED.
  115. 3. `name' is taken to be an XLFD base name, of the form
  116. family-weight-slant-width, with exactly three hyphens. E.g.,
  117. name="grotesk-bold-r-normal", or "times-medium-r-normal".
  118. 4. `name' is taken to be a full font name, in which case `size' is
  119. ignored. E.g., name="fixed" or name="9x15" or
  120. name="-dec-terminal-bold-r-normal--14-140-75-75-c-80-iso8859-1". This
  121. option is mostly to support ancient core X fonts without proper XLFD
  122. names, such as "9x15".
  123. If a core X font is successfully retrieved (which will set the fields
  124. true_font_size, font_ascent, font_descent, and font_is_iso8859_1 of the
  125. drawing state, and the X-specific fields x_font_struct,
  126. x_font_pixel_size), then this function fills in the font_type field
  127. (PL_F_POSTSCRIPT [or PL_F_PCL] in case 1, PL_F_OTHER in cases 2, 3 and
  128. 4), and returns true. If a font is not successfully retrieved, this
  129. function returns false.
  130. The typeface_index and font_index fields are also filled in, in case 1.
  131. In the other cases (PL_F_OTHER) it's hardly worth it to fill them in, since
  132. switching to other fonts in the middle of a text string, except for a
  133. symbol font (`font #0') won't be supported. See g_cntrlify.c. */
  134. bool
  135. _pl_x_retrieve_font (S___(Plotter *_plotter))
  136. {
  137. const char *name, *true_name = ""; /* keep compiler happy */
  138. bool matched_builtin = false; /* font name matches name of a font in db? */
  139. bool success; /* font retrieved from cache or server? */
  140. const char *name_p;
  141. const char *x_name = NULL, *x_name_alt = NULL; /* from db */
  142. const char *x_name_alt2 = NULL, *x_name_alt3 = NULL; /* from db */
  143. int typeface_index = 0, font_index = 0; /* from db */
  144. int font_type = PL_F_POSTSCRIPT; /* from db */
  145. int i, hyphen_count;
  146. name = _plotter->drawstate->font_name;
  147. #ifdef DEBUG
  148. fprintf (stderr, "----------------------------------------------------------------------\n");
  149. fprintf (stderr, "_pl_x_retrieve_font(): name=\"%s\", size=%g, x_label=%s\n",
  150. name, _plotter->drawstate->font_size, _plotter->drawstate->x_label);
  151. #endif
  152. if (strlen (name) > MAX_USER_FONT_NAME_LENGTH) /* avoid buffer overflow */
  153. return false;
  154. if (_plotter->drawstate->font_size == 0.0)
  155. /* don't try to retrieve zero-size fonts */
  156. return false;
  157. /* Search null-terminated table of recognized PS fonts, in g_fontdb.c,
  158. for a name matching the passed name. We support either PS-style names
  159. (e.g. "Times-Roman") or shortened XLFD-style names
  160. (e.g. "times-medium-r-normal"). Alternative versions of latter are
  161. supported because some X servers use "zapfdingbats" instead of "itc
  162. zapf dingbats", etc. */
  163. i = -1;
  164. while (_pl_g_ps_font_info[++i].ps_name) /* array ends in NULL */
  165. {
  166. if ((strcasecmp (_pl_g_ps_font_info[i].ps_name, name) == 0)
  167. /* check alternative ps font name if any */
  168. || (_pl_g_ps_font_info[i].ps_name_alt
  169. && strcasecmp (_pl_g_ps_font_info[i].ps_name_alt, name) == 0)
  170. /* check 2nd alternative ps font name if any */
  171. || (_pl_g_ps_font_info[i].ps_name_alt2
  172. && strcasecmp (_pl_g_ps_font_info[i].ps_name_alt2, name) == 0)
  173. /* check X font name */
  174. || (strcasecmp (_pl_g_ps_font_info[i].x_name, name) == 0)
  175. /* check alternative X font name if any */
  176. || (_pl_g_ps_font_info[i].x_name_alt
  177. && strcasecmp (_pl_g_ps_font_info[i].x_name_alt, name) == 0)
  178. /* check 2nd alternative X font name if any */
  179. || (_pl_g_ps_font_info[i].x_name_alt2
  180. && strcasecmp (_pl_g_ps_font_info[i].x_name_alt2, name) == 0)
  181. /* check 3rd alternative X font name if any */
  182. || (_pl_g_ps_font_info[i].x_name_alt3
  183. && strcasecmp (_pl_g_ps_font_info[i].x_name_alt3, name) == 0))
  184. break;
  185. }
  186. if (_pl_g_ps_font_info[i].ps_name) /* matched name of a PS font in database */
  187. {
  188. matched_builtin = true;
  189. true_name = _pl_g_ps_font_info[i].ps_name;
  190. x_name = _pl_g_ps_font_info[i].x_name;
  191. x_name_alt = _pl_g_ps_font_info[i].x_name_alt;
  192. x_name_alt2 = _pl_g_ps_font_info[i].x_name_alt2;
  193. x_name_alt3 = _pl_g_ps_font_info[i].x_name_alt3;
  194. font_type = PL_F_POSTSCRIPT;
  195. typeface_index = _pl_g_ps_font_info[i].typeface_index;
  196. font_index = _pl_g_ps_font_info[i].font_index;
  197. }
  198. #ifdef USE_LJ_FONTS_IN_X
  199. if (matched_builtin == false) /* PS match failed, so try PCL fonts too */
  200. {
  201. i = -1;
  202. while (_pl_g_pcl_font_info[++i].ps_name) /* array ends in NULL */
  203. {
  204. if ((strcasecmp (_pl_g_pcl_font_info[i].ps_name, name) == 0)
  205. /* check alternative ps font name if any */
  206. || (_pl_g_pcl_font_info[i].ps_name_alt
  207. && strcasecmp (_pl_g_pcl_font_info[i].ps_name_alt, name) == 0)
  208. /* check X font name */
  209. || (strcasecmp (_pl_g_pcl_font_info[i].x_name, name) == 0))
  210. break;
  211. }
  212. if (_pl_g_pcl_font_info[i].ps_name) /* matched name of a PCL font in db */
  213. {
  214. matched_builtin = true;
  215. true_name = _pl_g_pcl_font_info[i].ps_name;
  216. x_name = _pl_g_pcl_font_info[i].x_name;
  217. x_name_alt = NULL;
  218. font_type = PL_F_PCL;
  219. typeface_index = _pl_g_pcl_font_info[i].typeface_index;
  220. font_index = _pl_g_pcl_font_info[i].font_index;
  221. }
  222. }
  223. #endif /* USE_LJ_FONTS_IN_X */
  224. #ifdef DEBUG
  225. fprintf (stderr, "Matched database font %s = %s = %s = %s\n",
  226. x_name, x_name_alt, x_name_alt2, x_name_alt3);
  227. #endif
  228. if (matched_builtin)
  229. {
  230. /* user passed the name of a PS or PCL font in libplot's database */
  231. success = _pl_x_select_xlfd_font_carefully (R___(_plotter) x_name,
  232. x_name_alt, x_name_alt2,
  233. x_name_alt3);
  234. if (success)
  235. /* RETRIEVAL TYPE #1: we've retrieved a core X font, the XLFD name
  236. of which was listed in libplot's hardcoded database; and have
  237. filled in X-specific fields */
  238. {
  239. free ((char *)_plotter->drawstate->true_font_name);
  240. _plotter->drawstate->true_font_name =
  241. (const char *)_pl_xmalloc (strlen (true_name) + 1);
  242. strcpy ((char *)_plotter->drawstate->true_font_name, true_name);
  243. _plotter->drawstate->font_type = font_type;
  244. _plotter->drawstate->typeface_index = typeface_index;
  245. _plotter->drawstate->font_index = font_index;
  246. #ifdef DEBUG
  247. fprintf (stderr, "_pl_x_retrieve_font(): retrieved \"%s\" as \"%s\", type=%d\n", name, _plotter->drawstate->true_font_name, _plotter->drawstate->font_type);
  248. fprintf (stderr, "_pl_x_retrieve_font(): font_size=%g, true_font_size=%g, font_ascent=%g, font_descent=%g, font_is_iso8859_1=%d, x_font_pixel_size=%d\n", _plotter->drawstate->font_size, _plotter->drawstate->true_font_size, _plotter->drawstate->font_ascent, _plotter->drawstate->font_descent, _plotter->drawstate->font_is_iso8859_1, _plotter->drawstate->x_font_pixel_size);
  249. #endif
  250. return true;
  251. }
  252. }
  253. /* User-specified font name didn't match either of the names of any PS
  254. [or PCL] font in libplot's database, so first handle the possibility
  255. that it's an XLFD base name for some other core X font
  256. (e.g. "charter-medium-r-normal"), with exactly three hyphens. */
  257. name_p = name;
  258. hyphen_count = 0;
  259. while (*name_p)
  260. hyphen_count += (*name_p++ == '-' ? 1 : 0);
  261. if (hyphen_count == 3)
  262. /* treat as base of an XLFD name */
  263. {
  264. success = _pl_x_select_xlfd_font_carefully (R___(_plotter) name,
  265. NULL, NULL, NULL);
  266. if (success)
  267. /* RETRIEVAL TYPE #3: we've retrieved a core X font, the base XLFD
  268. name of which was passed by the user, that isn't one of the
  269. fonts in libplot's hardcoded database; and have filled in
  270. X-specific fields */
  271. {
  272. free ((char *)_plotter->drawstate->true_font_name);
  273. _plotter->drawstate->true_font_name =
  274. (const char *)_pl_xmalloc (strlen (name) + 1);
  275. strcpy ((char *)_plotter->drawstate->true_font_name, name);
  276. _plotter->drawstate->font_type = PL_F_OTHER;
  277. /* these two fields are irrelevant because we don't support
  278. switching among fonts not in libplot's internal database */
  279. _plotter->drawstate->typeface_index = 0;
  280. _plotter->drawstate->font_index = 1;
  281. #ifdef DEBUG
  282. fprintf (stderr, "_pl_x_retrieve_font(): retrieved \"%s\" as \"%s\", type=%d\n", name, _plotter->drawstate->true_font_name, _plotter->drawstate->font_type);
  283. fprintf (stderr, "_pl_x_retrieve_font(): font_size=%g, true_font_size=%g, font_ascent=%g, font_descent=%g, font_is_iso8859_1=%d, x_font_pixel_size=%d\n", _plotter->drawstate->font_size, _plotter->drawstate->true_font_size, _plotter->drawstate->font_ascent, _plotter->drawstate->font_descent, _plotter->drawstate->font_is_iso8859_1, _plotter->drawstate->x_font_pixel_size);
  284. #endif
  285. return true;
  286. }
  287. }
  288. /* User-passed name didn't have exactly 3 hyphens, so try treating it as
  289. the full name of a core X font; ignore size. This a kludge, included
  290. partly to support pre-XLFD fonts, e.g. "9x15", and aliases for XLFD
  291. fonts, e.g. "fixed". Most of the latter are really pre-XLFD names. */
  292. {
  293. double det;
  294. det = _plotter->drawstate->transform.m[0] * _plotter->drawstate->transform.m[3]
  295. - _plotter->drawstate->transform.m[1] * _plotter->drawstate->transform.m[2];
  296. if (det == 0.0)
  297. /* singular user-space -> device-space map; bail */
  298. return false;
  299. /* Try to retrieve font from server or cache list, given its full
  300. name; and for the moment, ignore the preferred pixel size we just
  301. computed. 3rd argument `false' requests entire font. A GOOD IDEA? */
  302. success = _pl_x_select_font_carefully (R___(_plotter) name,
  303. _plotter->drawstate->x_label,
  304. false);
  305. if (success)
  306. /* RETRIEVAL TYPE #4: we've retrieved a core X font, the full name
  307. of which was passed by the user, that isn't one of the fonts in
  308. libplot's hardcoded database */
  309. {
  310. free ((char *)_plotter->drawstate->true_font_name);
  311. _plotter->drawstate->true_font_name =
  312. (const char *)_pl_xmalloc (strlen (name) + 1);
  313. strcpy ((char *)_plotter->drawstate->true_font_name, name);
  314. _plotter->drawstate->font_type = PL_F_OTHER;
  315. /* these two fields are irrelevant because we don't support
  316. switching among `other' fonts */
  317. _plotter->drawstate->typeface_index = 0;
  318. _plotter->drawstate->font_index = 1;
  319. if (_plotter->drawstate->x_font_pixel_size == 0) /* paranoia */
  320. return false;
  321. #ifdef DEBUG
  322. fprintf (stderr, "_pl_x_retrieve_font(): retrieved \"%s\" as \"%s\", type=%d\n", name, _plotter->drawstate->true_font_name, _plotter->drawstate->font_type);
  323. #endif
  324. return true;
  325. }
  326. }
  327. /* couldn't retrieve a matching X font, so declare failure; this will
  328. lead (at a higher level; see g_retrieve.c) either to the retrieval of
  329. a default substitute X font, or a builtin Hershey font */
  330. #ifdef DEBUG
  331. fprintf (stderr, "_pl_x_retrieve_font(): FAILURE, couldn't retrieve \"%s\"\n", name);
  332. #endif
  333. return false;
  334. }
  335. /* _pl_x_select_xlfd_font_carefully() is a helper function that
  336. x_retrieve_font() above uses. It constructs a full XLFD name of a core
  337. X11 font from each of several specified base XLFD names, such as
  338. "helvetica-medium-r-normal", and attempts to retrieve them in order,
  339. until a successful retrieval occurs. The inclusion of several
  340. alternatives is useful, since each of the built-in fonts in libplot's
  341. database (see g_fontdb.c) is associated with several possible base XLFD
  342. names. That's because there is no standardization of names across
  343. vendors, even for the commonly used `Adobe 35' fonts, i.e., libplot's
  344. supported `Postscript fonts'.
  345. For each alternative, a fontname ending in -iso8859-1 (indicating the
  346. ISO-Latin-1 encoding) is tried, and if that doesn't work, a fontname
  347. ending in -*-* (the encoding being left up to the server). The
  348. lower-level function _pl_x_select_font_carefully() does the retrieval.
  349. It is passed the `x_label' field of the drawing state, which is a hint
  350. indicating which characters are needed. A proper subset of the font
  351. will be retrieved, if possible, to save time. The proper subset is
  352. indicated to the server, as usual, by a suffix on the font name. E.g.,
  353. ....-iso8859-1[88 89] consists only of the letters `X' and `Y'
  354. (characters 88 and 89).
  355. This code, when requesting the retrieval, must place an integer pixel
  356. size in the XLFD name. The pixel size it chooses is based on (1) the
  357. font_size in user coordinates and (2) the transformation matrix, which
  358. takes user-space to device-space, i.e., to X11 pixel space.
  359. When any label is drawn, the retrieved font will in general be scaled,
  360. by XAffDrawString(); see the code in x_text.c. So this code cleverly
  361. chooses an integer pixel size which, if it weren't for rounding to the
  362. closest integer, wouldn't require any scaling of the glyph bitmaps at
  363. all, provided that the user-space -> device-space is uniform (not
  364. "anamorphic"), and doesn't involve a rotation. */
  365. bool
  366. _pl_x_select_xlfd_font_carefully (R___(Plotter *_plotter) const char *x_name, const char *x_name_alt, const char *x_name_alt2, const char *x_name_alt3)
  367. {
  368. char *x_name_buf; /* buffer for creating font name */
  369. bool success = false;
  370. int integer_font_size_in_pixels;
  371. double det, font_size_in_pixels;
  372. det = _plotter->drawstate->transform.m[0] * _plotter->drawstate->transform.m[3]
  373. - _plotter->drawstate->transform.m[1] * _plotter->drawstate->transform.m[2];
  374. if (det == 0.0)
  375. /* singular user-space -> device-space map; bail */
  376. return false;
  377. /* Compute preferred pixel size for the core X font: the user-space font
  378. size, multiplied by a measure of the size of the user-space to
  379. device-space transformation matrix. The "measure" we choose is the
  380. minimum of the matrix's two singular values. (There are other
  381. possible choices for this measure.) */
  382. font_size_in_pixels =
  383. min_sing_val (_plotter->drawstate->transform.m) *_plotter->drawstate->font_size;
  384. if (font_size_in_pixels == 0.0)
  385. /* preferred device-space font size, in terms of pixels, is zero; bail */
  386. return false;
  387. /* quantize to an integer pixel size: round downward */
  388. integer_font_size_in_pixels = (int)font_size_in_pixels;
  389. if (font_size_in_pixels == 0)
  390. /* integer device-space size, in terms of pixels, is zero; bail */
  391. return false;
  392. /* prepare buffer for font name assemblage */
  393. x_name_buf = (char *)_pl_xmalloc ((MAX_FONT_NAME_LENGTH + 1) * sizeof (char));
  394. /* try to retrieve font from server or cache list, after punching the
  395. pixel size into the appropriate XLFD fontname template */
  396. /* try Latin-1 fontname, i.e. fontname ending in -iso8859-1 */
  397. sprintf (x_name_buf, xlfd_template_latin_1, x_name, integer_font_size_in_pixels);
  398. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  399. _plotter->drawstate->x_label,
  400. true);
  401. #ifdef DEBUG
  402. fprintf (stderr, "_pl_x_select_xlfd_font_carefully(): retrieval begins with %s\n",
  403. x_name_buf);
  404. #endif
  405. if (success == false)
  406. /* try fontname ending in -*-* */
  407. {
  408. sprintf (x_name_buf, xlfd_template_generic,
  409. x_name, integer_font_size_in_pixels);
  410. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  411. _plotter->drawstate->x_label,
  412. true);
  413. }
  414. if (x_name_alt)
  415. /* alternative base XLFD name was supplied, so try it too */
  416. {
  417. if (success == false)
  418. /* try Latin-1 fontname, i.e. fontname ending in -iso8859-1 */
  419. {
  420. sprintf (x_name_buf, xlfd_template_latin_1,
  421. x_name_alt, integer_font_size_in_pixels);
  422. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  423. _plotter->drawstate->x_label,
  424. true);
  425. }
  426. if (success == false)
  427. /* try fontname ending in -*-* */
  428. {
  429. sprintf (x_name_buf, xlfd_template_generic,
  430. x_name_alt, integer_font_size_in_pixels);
  431. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  432. _plotter->drawstate->x_label,
  433. true);
  434. }
  435. }
  436. if (x_name_alt2)
  437. /* 2nd alternative base XLFD name was supplied, so try it too */
  438. {
  439. if (success == false)
  440. /* try Latin-1 fontname, i.e. fontname ending in -iso8859-1 */
  441. {
  442. sprintf (x_name_buf, xlfd_template_latin_1,
  443. x_name_alt2, integer_font_size_in_pixels);
  444. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  445. _plotter->drawstate->x_label,
  446. true);
  447. }
  448. if (success == false)
  449. /* try fontname ending in -*-* */
  450. {
  451. sprintf (x_name_buf, xlfd_template_generic,
  452. x_name_alt2, integer_font_size_in_pixels);
  453. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  454. _plotter->drawstate->x_label,
  455. true);
  456. }
  457. }
  458. if (x_name_alt3)
  459. /* 3rd alternative base XLFD name was supplied, so try it too */
  460. {
  461. if (success == false)
  462. /* try Latin-1 fontname, i.e. fontname ending in -iso8859-1 */
  463. {
  464. sprintf (x_name_buf, xlfd_template_latin_1,
  465. x_name_alt3, integer_font_size_in_pixels);
  466. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  467. _plotter->drawstate->x_label,
  468. true);
  469. }
  470. if (success == false)
  471. /* try fontname ending in -*-* */
  472. {
  473. sprintf (x_name_buf, xlfd_template_generic,
  474. x_name_alt3, integer_font_size_in_pixels);
  475. success = _pl_x_select_font_carefully (R___(_plotter) x_name_buf,
  476. _plotter->drawstate->x_label,
  477. true);
  478. }
  479. }
  480. if (success)
  481. /* A clever hack. Slightly alter the true_font_size field (and
  482. font_ascent/font_descent/font_cap_height for consistency), in a way
  483. that will permit any text string to be rendered in this font by
  484. XDrawString(), rather than by our bitmap-scaling code in
  485. x_afftext.c. Provided, that is, the text string isn't rotated, and
  486. the user-space -> device-space transformation is a uniform scaling
  487. that respects coordinate axes.
  488. The reason this works is that the rendering code in x_text.c calls
  489. our bitmap-scaling function XAffDrawString() with a certain
  490. transformation matrix a[] as an argument. Under the above
  491. circumstances, this matrix will turn out to be the identity matrix,
  492. due to the slight modification performed below. That being the
  493. case, XAffDrawString() will simply call XDrawString rather than
  494. performing any bitmap scaling. The result: in the above
  495. circumstances, which are very common, the glyphs in the text string
  496. won't undergo a slight scaling traceable to the integer quantization
  497. of font pixel size, and will look better. */
  498. {
  499. double factor = integer_font_size_in_pixels / font_size_in_pixels;
  500. /* scale by this factor, quite close to unity */
  501. _plotter->drawstate->true_font_size *= factor;
  502. _plotter->drawstate->font_ascent *= factor;
  503. _plotter->drawstate->font_descent *= factor;
  504. _plotter->drawstate->font_cap_height *= factor;
  505. }
  506. return success;
  507. }
  508. /* _pl_x_select_font_carefully() is a wrapper around select_x_font() below.
  509. It attempts to retrieve the font NAME from the X server or from the
  510. per-Plotter font cache of already-retrieved fonts. The character subset
  511. desired (if any) is specified by the string S, and whether an attempt
  512. should actually be made to retrieve a subset rather than the entire is
  513. specified by the parameter SUBSETTING. The return value indicates
  514. whether the retrieval succeeded.
  515. A NULL value for S means the font is being retrieved only to look at its
  516. metrics; in which case select_x_font() will attempt to retrieve the
  517. character 'X' only, to permit the cap-height metric to be determined;
  518. and the space character, for good measure. This is arranged in the
  519. low-level function string_to_bitvector() below; we _always_ include the
  520. characters 'X' and ' ' among those we ask the server to rasterize.
  521. The inclusion of 'X' incidentally works around a bug reported by Georgy
  522. Salnikov <sge@nmr.nioch.nsc.ru>, in XFree86-3.2. (We previously tried
  523. to retrieve not 'X', but rather the space character. But if an XLFD
  524. font name ends in *-*[32], i.e. font contains only a single space,
  525. XFree86 reports an error retrieving the font, and any executable linked
  526. with this code client will terminate.)
  527. If the font is successfully retrieved (either from the X server or from
  528. the cache), this function fills in most font-related fields of the
  529. drawing state. That includes the generic fields true_font_size,
  530. font_ascent, font_descent, font_cap_height, font_is_iso8859_1, and the
  531. X-specific fields x_font_struct and x_font_pixel_size.
  532. The true_font_size is simply set equal to font_size (which is whatever
  533. the user passed to libplot by invoking the fontname() function in the
  534. libplot API). That is because in principle, libplot's X driver can
  535. match exactly any user-specified font size, by scaling bitmaps. See
  536. x_text.c and x_afftext.c for the code that renders text strings. */
  537. bool
  538. _pl_x_select_font_carefully (R___(Plotter *_plotter) const char *name, const unsigned char *s, bool subsetting)
  539. {
  540. plXFontRecord *fptr;
  541. if (s == (unsigned char *)NULL)
  542. s = (unsigned char *)""; /* "" is effectively "X " */
  543. /* attempt to retrieve the specified (subset of the) font */
  544. fptr = select_x_font (_plotter->x_dpy, &(_plotter->x_fontlist),
  545. name, s, subsetting);
  546. #ifdef DEBUG
  547. fprintf (stderr, "_pl_x_select_font_carefully(): select_x_font() returns %p\n",
  548. fptr);
  549. #endif
  550. if (subsetting && (fptr == (plXFontRecord *)NULL))
  551. /* failure; so try to retrieve entire font instead of a subset,
  552. ignoring the passed hint string S (perhaps server doesn't support
  553. subsetting?) */
  554. fptr = select_x_font (_plotter->x_dpy, &(_plotter->x_fontlist),
  555. name, s, false);
  556. if (fptr == (plXFontRecord *)NULL)
  557. /* couldn't retrieve font from cache or from server */
  558. return false;
  559. else
  560. /* Success, so fill in fields of the drawing state from the returned
  561. font record. Most are generic rather than X-specific. Ascent,
  562. descent and cap_height are user-space quantities; so are scaled by
  563. the font_size, which is expressed in user-space units. */
  564. {
  565. if (fptr->x_font_pixel_size <= 0) /* paranoia */
  566. return false;
  567. #ifdef DEBUG
  568. fprintf (stderr, "fontname = %s, min_char_or_byte2 = %u, max_char_or_byte2 = %u, ascent = %d, descent = %d, default_char = %u\n", name, fptr->x_font_struct->min_char_or_byte2, fptr->x_font_struct->max_char_or_byte2, fptr->x_font_struct->ascent, fptr->x_font_struct->descent, fptr->x_font_struct->default_char);
  569. #endif
  570. /* set generic fields */
  571. _plotter->drawstate->true_font_size = _plotter->drawstate->font_size;
  572. _plotter->drawstate->font_ascent =
  573. ((fptr->x_font_struct->ascent * _plotter->drawstate->font_size)
  574. / fptr->x_font_pixel_size);
  575. _plotter->drawstate->font_descent =
  576. ((fptr->x_font_struct->descent * _plotter->drawstate->font_size)
  577. / fptr->x_font_pixel_size);
  578. _plotter->drawstate->font_cap_height =
  579. ((fptr->x_font_cap_height * _plotter->drawstate->font_size)
  580. / fptr->x_font_pixel_size);
  581. _plotter->drawstate->font_is_iso8859_1 = fptr->x_font_is_iso8859_1;
  582. /* set X-specific fields */
  583. _plotter->drawstate->x_font_struct = fptr->x_font_struct;
  584. _plotter->drawstate->x_font_pixel_size = fptr->x_font_pixel_size;
  585. return true;
  586. }
  587. }
  588. /* Attempt to retrieve a core X font, as a plXFontRecord. The font is
  589. specified by NAME, and a hint as to which characters will need to be
  590. rendered is passed as the non-null string S. This permits the retrieval
  591. of a proper subset of the font, if desired. The SUBSETTING parameter
  592. indicates whether the retrieval of an appropriate subset of the font
  593. should first be attempted, before retrieval of the entire font.
  594. The X_FONTLIST_PTR argument passes [by reference!] a pointer to a font
  595. cache, a linked list of plXFontRecords that will be searched. If the
  596. font isn't found in the cache but can be successfully retrieved from the
  597. X display server instead, a new record is added to the head of this
  598. list; and if it can't be, a null (invalid) record is added to the head
  599. of the list; in both cases, to speed up later retrieval attempts.
  600. Return value: a pointer to the font record, if a font was found in the
  601. cache or newly added to it; otherwise NULL. */
  602. static plXFontRecord *
  603. select_x_font (Display *dpy, plXFontRecord **x_fontlist_ptr, const char *name, const unsigned char *s, bool subsetting)
  604. {
  605. bool found = false;
  606. unsigned char bitvec[32];
  607. plXFontRecord *x_fontlist, *fptr;
  608. #ifdef DEBUG
  609. fprintf (stderr, "select_x_font (name=\"%s\", subset=\"%s\", subsetting=%d)\n",
  610. name, (const char *)s, subsetting);
  611. #endif
  612. if (subsetting)
  613. /* construct 256-bit vector specifying charset subset */
  614. string_to_bitvector (s, bitvec);
  615. /* get head of linked-list cache */
  616. x_fontlist = *x_fontlist_ptr;
  617. /* attempt to find font in cache */
  618. for (fptr = x_fontlist; fptr; fptr = fptr->next)
  619. {
  620. #ifdef DEBUG
  621. fprintf (stderr, "select_x_font(): cache entry: name=\"%s\", subset=%d\n",
  622. fptr->x_font_name, fptr->subset);
  623. #endif
  624. if (strcmp (name, fptr->x_font_name) == 0)
  625. {
  626. if ((subsetting && fptr->subset
  627. && is_a_subset (bitvec, fptr->subset_vector))
  628. || (subsetting && (fptr->subset == false))
  629. || (subsetting == false && fptr->subset == false))
  630. {
  631. found = true;
  632. break;
  633. }
  634. }
  635. }
  636. if (found)
  637. {
  638. if (fptr->x_font_struct)
  639. /* found record was a genuine one */
  640. {
  641. #ifdef DEBUG
  642. fprintf (stderr, "select_x_font(): font cache HIT on name=%s, s=\"%s\"\n", name, s);
  643. #endif
  644. return fptr;
  645. }
  646. else
  647. {
  648. #ifdef DEBUG
  649. fprintf (stderr, "select_x_font(): font cache HIT (fake) on name=\"%s\", s=\"%s\"\n", name, s);
  650. #endif
  651. /* invalid record: an earlier retrieval attempt must have failed */
  652. return (plXFontRecord *)NULL;
  653. }
  654. }
  655. #ifdef DEBUG
  656. fprintf (stderr, "select_x_font(): font cache miss on name=\"%s\", s=\"%s\"\n", name, s);
  657. #endif
  658. /* no record in cache, so try to retrieve font from X server */
  659. {
  660. char *tmpname, *tmpname_perm, *_charset_subset_list = NULL;
  661. int extra = 0;
  662. /* allocate space for new record, update pointer to cache to include it */
  663. fptr =
  664. (plXFontRecord *)_pl_xmalloc (sizeof (plXFontRecord));
  665. fptr->next = *x_fontlist_ptr;
  666. *x_fontlist_ptr = fptr;
  667. if (subsetting)
  668. {
  669. _charset_subset_list =
  670. (char *)_pl_xmalloc ((MAX_CHARSET_SUBSET_LIST_LENGTH + 1) * sizeof (char));
  671. print_bitvector (bitvec, _charset_subset_list);
  672. extra = strlen (_charset_subset_list);
  673. }
  674. tmpname_perm = (char *)_pl_xmalloc (1 + strlen (name));
  675. strcpy (tmpname_perm, name);
  676. tmpname = (char *)_pl_xmalloc (1 + strlen (name) + extra);
  677. strcpy (tmpname, name);
  678. if (subsetting)
  679. {
  680. /* append X11R6 list-of-ranges to name to be sent to server */
  681. strcat (tmpname, _charset_subset_list);
  682. free (_charset_subset_list);
  683. }
  684. #ifdef DEBUG
  685. fprintf (stderr, "select_x_font(): trying to invoke XLoadQueryFont on \"%s\", subsetting=%d\n", tmpname, subsetting);
  686. #endif
  687. /* attempt to retrieve font from server; return value from
  688. XLoadQueryFont() equalling NULL indicates failure */
  689. fptr->x_font_struct =
  690. XLoadQueryFont (dpy, tmpname);
  691. free (tmpname);
  692. /* whether or not there was success, fill in some add'l fields of record */
  693. fptr->x_font_name = tmpname_perm; /* don't include subset in stored name */
  694. fptr->subset = subsetting;
  695. if (subsetting)
  696. memcpy (fptr->subset_vector, bitvec, 32 * sizeof (unsigned char));
  697. /* handle a special case: retrieval from server succeeded, but the
  698. retrieved font wasn't an 8-bit font, so we can't use it */
  699. if (fptr->x_font_struct
  700. && (fptr->x_font_struct->min_byte1 != 0
  701. || fptr->x_font_struct->max_byte1 != 0))
  702. /* treat as if retrieval failed */
  703. {
  704. XFreeFont (dpy, fptr->x_font_struct);
  705. fptr->x_font_struct = (XFontStruct *)NULL;
  706. }
  707. if (fptr->x_font_struct)
  708. /* retrieval succeeded */
  709. {
  710. #ifdef DEBUG
  711. fprintf (stderr, "select_x_font(): loaded font \"%s\"\n", name);
  712. #endif
  713. /* fill in, as well, the x_font_pixel_size, x_font_cap_height,
  714. x_font_iso_8859_1 fields of the font record */
  715. set_font_dimensions (dpy, fptr);
  716. return fptr; /* X font selected */
  717. }
  718. else
  719. /* retrieval failed */
  720. {
  721. #ifdef DEBUG
  722. fprintf (stderr, "select_x_font(): failed to load font \"%s\"\n", name);
  723. #endif
  724. return (plXFontRecord *)NULL;
  725. }
  726. }
  727. }
  728. /* Complete the filling in of an plXFontRecord, by filling in the fields
  729. x_font_pixel_size, x_font_cap_height, and x_font_is_iso8859_1, on the
  730. basis of the x_font_struct field. Called by preceding function. */
  731. static void
  732. set_font_dimensions (Display *dpy, plXFontRecord *fptr)
  733. {
  734. unsigned long retval;
  735. char *name, *pixel_field;
  736. char *charset_major_field, *charset_minor_field;
  737. #ifdef DEBUG2
  738. {
  739. int i;
  740. for (i = 0; i < fptr->x_font_struct->n_properties; i++)
  741. fprintf (stderr, "\tproperty %s [atom %lu] is %ld\n",
  742. XGetAtomName(dpy, fptr->x_font_struct->properties[i].name),
  743. fptr->x_font_struct->properties[i].name,
  744. fptr->x_font_struct->properties[i].card32);
  745. }
  746. #endif
  747. if (XGetFontProperty (fptr->x_font_struct, XA_FONT, &retval))
  748. /* this font has a FONT property, as any well behaved font should */
  749. {
  750. /* Extract relevant fields from this property (i.e. from X server's
  751. idea of the font name). This will work if it's an XLFD name. */
  752. name = XGetAtomName (dpy, retval);
  753. #ifdef DEBUG
  754. fprintf (stderr, "set_font_dimensions(): FONT property is \"%s\"\n", name);
  755. #endif
  756. pixel_field = xlfd_field (name, XLFD_FIELD_PIXELS);
  757. charset_major_field = xlfd_field (name, XLFD_FIELD_CHARACTER_SET_MAJOR);
  758. charset_minor_field = xlfd_field (name, XLFD_FIELD_CHARACTER_SET_MINOR);
  759. XFree (name);
  760. /* determine whether font encoding is ISO-Latin-1 */
  761. if ((charset_major_field != NULL) && (charset_minor_field != NULL)
  762. && strcasecmp (charset_major_field, "iso8859") == 0
  763. && (charset_minor_field[0] == (char)'1'
  764. && (charset_minor_field[1] == (char)0 /* string terminator */
  765. || charset_minor_field[1] == (char)'[')))
  766. fptr->x_font_is_iso8859_1 = true;
  767. else
  768. fptr->x_font_is_iso8859_1 = false;
  769. if (charset_major_field)
  770. free (charset_major_field);
  771. if (charset_minor_field)
  772. free (charset_minor_field);
  773. if (pixel_field != NULL)
  774. /* font presumably has an XLFD name, since it has a pixel field */
  775. {
  776. /* extract x_font_pixel_size from the pixel field */
  777. unsigned int size;
  778. sscanf (pixel_field, "%u", &size);
  779. fptr->x_font_pixel_size = size;
  780. free (pixel_field);
  781. /* fill in the font_{cap_height} field; we get it from the ascent
  782. of the `X' character, if it exists */
  783. if ('X' >= fptr->x_font_struct->min_char_or_byte2
  784. && 'X' <= fptr->x_font_struct->max_char_or_byte2
  785. && fptr->x_font_struct->per_char)
  786. /* have `X' char in the font, and have per-char data */
  787. {
  788. int X = 'X' - fptr->x_font_struct->min_char_or_byte2;
  789. fptr->x_font_cap_height
  790. = fptr->x_font_struct->per_char[X].ascent;
  791. }
  792. else /* do our best */
  793. fptr->x_font_cap_height
  794. = fptr->x_font_struct->min_bounds.ascent;
  795. /* we've set all fields, so we can return */
  796. return;
  797. }
  798. #ifdef DEBUG2
  799. fprintf (stderr, "FONT property does not exist\n");
  800. #endif
  801. }
  802. else
  803. /* font doesn't have an XLFD name (so no pixel size field), or there's no
  804. FONT property at all (a bad situation) */
  805. {
  806. Atom pixel_size_atom, resolution_y_atom;
  807. unsigned long point_size, resolution_y;
  808. fptr->x_font_is_iso8859_1 = false; /* assumed (worst case) */
  809. pixel_size_atom = XInternAtom (dpy, "PIXEL_SIZE", (Bool)false);
  810. if (XGetFontProperty (fptr->x_font_struct, pixel_size_atom, &retval))
  811. /* there's a PIXEL_SIZE property, so use it to compute font size */
  812. {
  813. #ifdef DEBUG2
  814. fprintf (stderr, "PIXEL_SIZE property is \"%lu\"\n", retval);
  815. #endif
  816. fptr->x_font_pixel_size = retval;
  817. }
  818. else
  819. /* no PIXEL_SIZE, so try to compute pixel size from POINT_SIZE and
  820. RESOLUTION_Y properties */
  821. {
  822. #ifdef DEBUG2
  823. fprintf (stderr, "PIXEL_SIZE property does not exist\n");
  824. #endif
  825. resolution_y_atom = XInternAtom (dpy, "RESOLUTION_Y", (Bool)false);
  826. if (XGetFontProperty (fptr->x_font_struct, XA_POINT_SIZE, &point_size)
  827. && (XGetFontProperty (fptr->x_font_struct,
  828. resolution_y_atom, &resolution_y)))
  829. {
  830. #ifdef DEBUG2
  831. fprintf (stderr, "POINT_SIZE property is \"%lu\"\n",
  832. point_size);
  833. fprintf (stderr, "RESOLUTION_Y property is \"%lu\"\n",
  834. resolution_y);
  835. #endif
  836. fptr->x_font_pixel_size =
  837. IROUND(((double)point_size * (double)resolution_y / 722.7));
  838. }
  839. else
  840. /* we can't compute the font size legitimately, so estimate it
  841. from the XFontStruct (may not be reliable) */
  842. {
  843. #ifdef DEBUG2
  844. fprintf (stderr, "POINT_SIZE and/or RESOLUTION_Y properties do not exist\n");
  845. #endif
  846. fptr->x_font_pixel_size = fptr->x_font_struct->ascent + fptr->x_font_struct->descent;
  847. }
  848. }
  849. fptr->x_font_cap_height
  850. = fptr->x_font_struct->per_char['X' - fptr->x_font_struct->min_char_or_byte2].ascent;
  851. }
  852. }
  853. /* Extract a field from an XLFD name string, by number, and return it, via
  854. a call to malloc. If `name' doesn't appear to be an XLFD name, NULL is
  855. returned. */
  856. static char *
  857. xlfd_field(const char *name, int field)
  858. {
  859. const char *p;
  860. const char *fields[PL_NUM_XLFD_FIELDS];
  861. char *retstring;
  862. int len[PL_NUM_XLFD_FIELDS];
  863. int i, n, m;
  864. /* split into fields at hyphens */
  865. for (p = name, i = 0, n = 0, m = 0;
  866. *p && (i < PL_NUM_XLFD_FIELDS);
  867. p++, n++, m++)
  868. {
  869. if (*p == '-')
  870. {
  871. if (i > 0)
  872. len[i-1] = n;
  873. n = 0;
  874. fields[i++] = p;
  875. }
  876. }
  877. if (i < PL_NUM_XLFD_FIELDS)
  878. return NULL;
  879. len[PL_NUM_XLFD_FIELDS - 1] = strlen (name) - (m - 1); /* final field exhausts string */
  880. /* for len[] and fields[], each field includes initial hyphen */
  881. retstring = (char *)_pl_xmalloc (len[field] * sizeof(char));
  882. strncpy (retstring, fields[field] + 1,
  883. (unsigned int)(len[field] - 1)); /* skip initial - */
  884. retstring[len[field] - 1] = '\0';
  885. return retstring;
  886. }
  887. /* Prepare a bit vector (length 256 bits, i.e. 32 bytes) indicating which
  888. characters in the range 1..255 are used in string S. We always include
  889. the character 'X', even if it isn't present in the string. `X' is
  890. special because we can subsequently use it to retrieve the cap height.
  891. For backward compatibility (not necessary?) we also include the space
  892. character. */
  893. static void
  894. string_to_bitvector (const unsigned char *s, unsigned char v[32])
  895. {
  896. unsigned char c;
  897. unsigned int i, j;
  898. int k;
  899. for (k = 0; k < 32; k++)
  900. v[k] = 0;
  901. /* include the X character */
  902. c = 'X';
  903. i = c / 8;
  904. j = c % 8;
  905. v[i] |= (1 << j);
  906. /* include the space character too */
  907. c = ' ';
  908. i = c / 8;
  909. j = c % 8;
  910. v[i] |= (1 << j);
  911. /* include all characters in the passed string */
  912. while ((c = *s) != (unsigned char)'\0')
  913. {
  914. i = c / 8;
  915. j = c % 8;
  916. #ifdef DEBUG2
  917. fprintf (stderr, "saw char %d (i.e. %c), stored as %d,%d\n", c, c, i, j);
  918. #endif
  919. v[i] |= (1 << j);
  920. s++;
  921. }
  922. }
  923. /* This writes a bitvector as a string, in the form used in X11R6-style
  924. charset subsetting. Each range of chars may require the writing of up
  925. to 8 bytes, e.g. " 160_180". The list of ranges is contained within
  926. brackets. */
  927. static void
  928. print_bitvector (unsigned char v[32], char *s)
  929. {
  930. int i, num_ranges_output = 0, num_chars_output = 0;
  931. int start_of_range = 0;
  932. bool used;
  933. bool in_range = false;
  934. *s++ = '[';
  935. for (i = 0; i <= 256; i++)
  936. {
  937. if (i == 256)
  938. used = false;
  939. else
  940. used = (v[i / 8] & (1 << (i % 8))) ? true : false;
  941. #ifdef DEBUG2
  942. if (used)
  943. fprintf (stderr, "stored char %d (i.e. %c), from %d,%d\n", i, i, i/8, i%8);
  944. #endif
  945. if (used && in_range == false)
  946. /* begin new range */
  947. {
  948. start_of_range = i;
  949. in_range = true;
  950. }
  951. else if (used == false && in_range)
  952. /* end of range, so output the range */
  953. {
  954. int hundreds, tens, ones;
  955. bool hundreds_output;
  956. if (num_chars_output > MAX_CHARSET_SUBSET_LIST_LENGTH - 8)
  957. break; /* abort to avoid buffer overrun */
  958. if (num_ranges_output > 0)
  959. /* use space as separator */
  960. {
  961. *s++ = ' ';
  962. num_chars_output++;
  963. }
  964. #ifdef DEBUG2
  965. fprintf (stderr, "outputting character range %d..%d, i.e. %c..%c\n",
  966. start_of_range, i-1, start_of_range, i-1);
  967. #endif
  968. if (start_of_range < (i - 1))
  969. /* have a genuine range, start..(i-1), not a singleton */
  970. {
  971. /* output start of range, followed by underscore */
  972. hundreds = start_of_range / 100;
  973. tens = (start_of_range - hundreds * 100) / 10;
  974. ones = start_of_range % 10;
  975. hundreds_output = false;
  976. if (hundreds > 0)
  977. {
  978. *s++ = (char)'0' + hundreds;
  979. hundreds_output = true;
  980. num_chars_output++;
  981. }
  982. if (hundreds_output || tens > 0)
  983. {
  984. *s++ = (char)'0' + tens;
  985. num_chars_output++;
  986. }
  987. *s++ = (char)'0' + ones;
  988. num_chars_output++;
  989. *s++ = (char)'_';
  990. num_chars_output++;
  991. }
  992. /* output end of range, which is i-1 */
  993. hundreds = (i-1) / 100;
  994. tens = ((i-1) - hundreds * 100) / 10;
  995. ones = (i-1) % 10;
  996. hundreds_output = false;
  997. if (hundreds > 0)
  998. {
  999. *s++ = (char)'0' + hundreds;
  1000. hundreds_output = true;
  1001. num_chars_output++;
  1002. }
  1003. if (hundreds_output || tens > 0)
  1004. {
  1005. *s++ = (char)'0' + tens;
  1006. num_chars_output++;
  1007. }
  1008. *s++ = (char)'0' + ones;
  1009. num_chars_output++;
  1010. /* no longer in range */
  1011. in_range = false;
  1012. num_ranges_output++;
  1013. }
  1014. }
  1015. *s++ = ']';
  1016. /* add final null */
  1017. *s = '\0';
  1018. }
  1019. static bool
  1020. is_a_subset (unsigned char set1[32], unsigned char set2[32])
  1021. {
  1022. int i;
  1023. bool retval = true;
  1024. for (i = 0; i < 32; i++)
  1025. if (set1[i] & ~(set2[i]))
  1026. {
  1027. retval = false;
  1028. break;
  1029. }
  1030. return retval;
  1031. }
  1032. /* Compute the minimum of the two singular values of a 2x2 matrix. Used
  1033. for computing our favored pixel size, at which to retrieve a font; the
  1034. matrix is the user-space -> device-space transformation matrix. */
  1035. static double
  1036. min_sing_val (double m[4])
  1037. {
  1038. double mm[4], mprod[4];
  1039. double mag, max_mag = 0.0;
  1040. double trace, det, b2_4ac, min_sing_val_squared, min_sing_val;
  1041. int i;
  1042. /* scale the elements of m so that the largest has magnitude unity, to
  1043. reduce the chance of floating point roundoff error; this scaling will
  1044. be undone at the end */
  1045. for (i = 0; i < 4; i++)
  1046. {
  1047. mag = fabs (m[i]);
  1048. if (mag > max_mag)
  1049. max_mag = mag;
  1050. }
  1051. if (max_mag <= 0.0)
  1052. return 0.0;
  1053. for (i = 0; i < 4; i++)
  1054. mm[i] = m[i] / max_mag;
  1055. /* Compute M times the transpose of M. In the absence of floating-point
  1056. rounding error, this product matrix, which is symmetric, will be
  1057. "non-negative", i.e., its eigenvalues will be non-negative. The
  1058. singular values of M are (square roots of) its eigenvalues. */
  1059. mprod[0] = mm[0]*mm[0] + mm[1]*mm[1];
  1060. mprod[1] = mm[0]*mm[2] + mm[1]*mm[3];
  1061. mprod[2] = mm[2]*mm[0] + mm[3]*mm[1];
  1062. mprod[3] = mm[2]*mm[2] + mm[3]*mm[3];
  1063. trace = mprod[0] + mprod[3];
  1064. det = mprod[0] * mprod[3] - mprod[1] * mprod[2];
  1065. if (det < 0.0) /* rare rounding error problem */
  1066. return 0.0;
  1067. /* sing vals are (square roots of) solns of x^2 - trace * x + det = 0 */
  1068. b2_4ac = trace * trace - 4 * det;
  1069. if (b2_4ac < 0.0)
  1070. /* a common, innocuous rounding error problem */
  1071. b2_4ac = 0.0;
  1072. min_sing_val_squared = 0.5 * (trace - sqrt (b2_4ac));
  1073. if (min_sing_val_squared < 0.0) /* rare rounding error problem */
  1074. return 0.0;
  1075. min_sing_val = sqrt (min_sing_val_squared);
  1076. /* return minimum singular value, not forgetting to undo the useful
  1077. scaling with which we began */
  1078. return min_sing_val * max_mag;
  1079. }