x11.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 5k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. #include <stdio.h>
  2. #include <X11/Xlib.h>
  3. #include <X11/Xutil.h>
  4. #include <X11/Xos.h>
  5. #include <X11/Xatom.h>
  6. #include <X11/keysym.h>
  7. #include <X11/cursorfont.h>
  8. #include "www.h"
  9. extern Display *display;
  10. extern Colormap colormap;
  11. extern double lens_factor;
  12. extern XFontStruct *Fonts[FONTS];  /* array of fonts */
  13. extern char *FontsName[FONTS];
  14. extern int LineSpacing[FONTS];
  15. extern int BaseLine[FONTS];
  16. extern int StrikeLine[FONTS];
  17. extern int LineThickness[FONTS];
  18. extern int debug;
  19. long ParseColor(const char *s)
  20. {
  21.     XColor xc;
  22.     if (XParseColor(display, colormap, s, &xc))
  23. return (((xc.red >> 8) << 16) | ((xc.green >> 8) << 8) | (xc.blue >> 8));
  24.     return 0; /* i.e. black */
  25. }
  26. int myLoadFont(char *name)
  27. {
  28.     int direction_hint, font_ascent, font_descent;
  29.     XCharStruct overall;
  30.     XFontStruct *font = NULL;
  31.     static int fix = 0;
  32.     /* janet 24/07/95: not used:    int use_fix = -1; */
  33.     int i;
  34.     char *test = "Testing";
  35.     
  36.     for(i=0; i < fix; i++) {
  37. if (strcmp(name, FontsName[i]) == 0) {
  38.     if (FONT_TRACE)
  39. fprintf(stderr,"font: %s found in cache, fix = %dn",name, i);
  40.          return i;
  41.     }
  42.     /* a font with the same name has not been loaded, so try it.. */
  43.     font = XLoadQueryFont(display, name);
  44.     
  45.     if (font) {
  46. FontsName[fix] = strdup(name);
  47. Fonts[fix] = font;
  48. XTextExtents(font, test, strlen(test),
  49.      &direction_hint,
  50.      &font_ascent,
  51.      &font_descent,
  52.      &overall);
  53. LineSpacing[fix] = SPACING(font);
  54. BaseLine[fix] = BASELINE(font);
  55. StrikeLine[fix] = STRIKELINE(font);
  56. LineThickness[fix] = LINETHICKNESS(font);
  57. fix++;
  58. /* fprintf(stderr,"FIX %dn",fix);*/
  59. if (FONT_TRACE)
  60.     fprintf(stderr,"font %s: xloadqueryfonts successfuln",name);
  61. return (fix - 1);
  62.     }
  63.     
  64.     if (FONT_TRACE)
  65. fprintf(stderr,"font: mloadFont failed %sn",name);
  66.     return (-1);
  67. }
  68. int GetFont(HTList *family_l, long px, long weight, long style, BOOL small_caps)
  69. {
  70.     int fix = -1;
  71.     char name[200];
  72.     char *weight_str, *slant_str, *condensed_str, *serif_str, *rgstry;
  73.     char *family;
  74.     /*    int i; */
  75.     HTList *l;
  76.     px = (int)((double) px * lens_factor);
  77.     /* if a point size is requested, let's compute the pixel size to
  78.        avoid using the dpi in the request */
  79.     switch (weight) {
  80.       case FONT_WEIGHT_LIGHT:
  81. weight_str = "light";
  82. break;
  83.       case FONT_WEIGHT_MEDIUM:
  84. weight_str = "medium";
  85. break;
  86.       case FONT_WEIGHT_DEMIBOLD:
  87. weight_str = "demibold";
  88. break;
  89.       case FONT_WEIGHT_BOLD:
  90. weight_str = "bold";
  91. break;
  92.       case FONT_WEIGHT_BLACK:
  93. weight_str = "black";
  94. break;
  95.       default:
  96. weight_str = "medium";
  97. break;
  98.     }
  99.     switch (style) {
  100.       case FONT_STYLE_ROMAN:
  101. slant_str = "r";
  102. break;
  103.       case FONT_STYLE_ITALIC:
  104.       case FONT_STYLE_OBLIQUE:
  105. slant_str = "i";
  106. break;
  107.       default:
  108. slant_str = "r";
  109. break;
  110.     }
  111.     serif_str = "*";
  112.     condensed_str = "normal";
  113.     l = family_l;
  114.     while((fix < 0) && (family = (char *)HTList_nextObject(l))) {
  115. if (strcasecmp(family,"symbol") == 0)
  116.     rgstry = "*-*";
  117. else
  118.     rgstry = "iso8859-1";
  119. sprintf(name,"-*-%s-%s-%s-%s-%s-%d-*-*-*-*-*-%s", family, weight_str, slant_str, condensed_str, serif_str, px, rgstry);
  120. fix = myLoadFont(name);
  121. if ((fix < 0) && ((style == FONT_STYLE_ITALIC) || (style == FONT_STYLE_OBLIQUE))){
  122.     slant_str = "o";
  123.     sprintf(name,"-*-%s-%s-%s-%s-%s-%d-*-*-*-*-*-%s", family, weight_str, slant_str, condensed_str, serif_str, px, rgstry);
  124.     fix = myLoadFont(name);
  125. }
  126.     }
  127.     if (FONT_TRACE)
  128. fprintf(stderr,"font: %s %sn", name,((fix >= 0) ? "succeded" : "failed, trying again"));
  129. #if 0
  130.     if ((fix < 0) && (style == FONT_STYLE_ITALIC)) {
  131. slant_str = "o";
  132. l = family_l;
  133. while((fix <= 0) && (family = (char *)HTList_nextObject(l))) {
  134.     if (strcasecmp(family,"symbol") == 0)
  135. rgstry = "*-*";
  136.     else
  137. rgstry = "iso8859-1";
  138.     sprintf(name,"-*-%s-%s-%s-%s-%s-%d-*-*-*-*-*-%s", family, weight_str, slant_str, condensed_str, serif_str, px, rgstry);
  139.     fix = myLoadFont(name);
  140. }
  141. if (FONT_TRACE)
  142.     fprintf(stderr,"font: %s %sn", name,((fix >= 0) ? "succeded" : "failed, trying again"));
  143.     }
  144. #endif
  145.     rgstry = "iso8859-1";
  146.     if (fix < 0) {
  147. if (FONT_TRACE)
  148.     fprintf(stderr,"font: resorting to 9x15n");
  149. strcpy(name,"9x15");
  150. fix = myLoadFont(name);
  151. /* fprintf(stderr,"font: %s %sn", name,((fix >= 0) ? "succeded" : "failed, trying again"));*/
  152.     }
  153.     if (fix < 0) {
  154. if (FONT_TRACE)
  155.     fprintf(stderr,"font: resorting to 9x15n");
  156. strcpy(name,"fixed");
  157. fix = myLoadFont(name);
  158. /* fprintf(stderr,"font: %s %sn", name,((fix >= 0) ? "succeded" : "failed, trying again"));*/
  159.     }
  160.     if (fix > 255) {
  161. fprintf(stderr,"GetFont: WARNING! more that 255 fonts loaded!n");
  162.         Exit(0);
  163.     }
  164.     return fix;
  165. }
  166. /*
  167.    how to get from mm to px:
  168.     1 inch = 25.4 mm = 72pt
  169.     pix / mm ~~ 4 (100dpi)
  170.                 3 (75 dpi)
  171.     14 pt / 2.83 (pt/mm)
  172.     14 pt * mm/pt * pix/mm = pix
  173. */