File: fontchooser.c

package info (click to toggle)
et 80b2-6
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,052 kB
  • ctags: 1,076
  • sloc: ansic: 10,311; tcl: 2,633; makefile: 180
file content (143 lines) | stat: -rw-r--r-- 4,743 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
** This program allows the user to view the various fonts
** available on the X server.
**
** Preprocess this file using "et2c" then link with "et.o".
*/
#include "tk.h"     /* This automatically loads Xlib.h */

void main(int argc, char **argv){
  Et_Init(&argc,argv);
		  ET_INSTALL_COMMANDS;
  ET_INCLUDE( fontchooser.tcl );
  Et_MainLoop();
}

/* This function parses up font names as follows:
**
**         Font Family               Font size
**    __________________________  ________________
**   /                          \/                \
**   -misc-fixed-medium-r-normal--10-100-75-75-c-60-iso8859-1
**                                |   |  \___/    | \_______/
**                                |   |    |      |     |
**                                |   |    |      |     `-- Always as shown
**                                |   |    |      |
**             The point size ----'   |    |      `--- 10x average width
**                                    |    |
**              This field ignored----'    `--- Resolution in dots per inch
**
**
** If $name is a font name (the first 6 fields of the X11 font name)
** then this procedure defines the global variable $Font($name), giving
** it as a value a list of available font sizes in accending order.
** Only fonts of a particular resolution are included.  By default, the
** resolution selected is 75dpi, but this can be changed by the
** argument to the command.
**
** This command also creates global variable FontCount which holds the
** number of entries in the Font() array.
*/
ET_PROC( FindFonts ){
  char **fontnames;    /* The names of all fonts in the selected resolution */
  int count;           /* Number of fonts */
  int i;               /* Loop counter */
  char pattern[400];   /* Buffer to hold a pattern used to select fonts. */

  if( argc==1 ){
    strcpy(pattern,"*-75-75-*-*-iso8859-1");
  }else if( argc==2 ){
    extern int atoi();
    int resolution = atoi(argv[1]);
    if( resolution<=0 ){
      sprintf(pattern,"*-*-*-*-*-iso8859-1");
    }else{
      sprintf(pattern,"*-%d-%d-*-*-iso8859-1",resolution,resolution);
    }
  }
  fontnames = XListFonts(Et_Display,pattern,1000,&count);
  ET(
    catch {unset Font}
    set FontCount 0
  );
  for(i=0; i<count; i++){
    int k;               /* Counts the '-' characters in the name */
    char *cp;            /* For scanning thru the name */
    char *nameStart;     /* First non-'-' character in the font name */
    int pointSize;       /* The point size for this font in pixels */

    cp = fontnames[i];
    k = 0;
    while( *cp=='-' ){
      cp++;
      k++;
    }
    nameStart = cp;
    while( *cp && k<7 ){
      if( *cp=='-' ){
        k++;
        if( k==6 ) *cp = 0;
      }
      cp++;
    }
    pointSize = atoi(cp);
    if( pointSize<=0 ) continue;  /* Ignore scalable fonts */
    ET( 
      if {![info exists {Font(%s(nameStart))}]} {
        set {Font(%s(nameStart))} {}
        incr FontCount
      }
      lappend {Font(%s(nameStart))} {%s(cp)}
    );
  }
  XFreeFontNames(fontnames);
  ET(
    foreach i [array names Font] {
      set Font($i) [lsort -command FontSizeCompare $Font($i)]
    }
  );
  return ET_OK;
}

/* This procedure is used to sort font size fields.  By "font size" we
** mean the tail of the font name, as follows:
**
**                      10-100-75-75-c-60-iso8859-1
**                      |   |  \___/    | \_______/
**                      |   |    |      |     `-- Always as shown
**                      |   |    |      |
**   The point size ----'   |    |      `--- 10x average width
**                          |    |
** 10x point size (ignored)-'    `--- Resolution in dots per inch (ignored)
**
** We want to sort the font sizes in accending order using the point
** size as the primary sort key and the average width as the secondary
** key.  This command is called from the "lsort" command with two
** arguments which are to font sizes to be compared.  We must return
** negative, zero, or positive if the first font size is less than,
** equal to, or greater than the second.
*/
ET_PROC( FontSizeCompare ){
  int leftHeight;
  int leftWidth;
  int rightHeight;
  int rightWidth;
  int result;

  if( argc!=3 ){
    interp->result = "Wrong # args";
    return ET_ERROR;
  }
  if( sscanf(argv[1],"%d-%*d-%*d-%*d-%*c-%d",&leftHeight,&leftWidth)!=2 ){
    interp->result = "First argument is not a font size";
    return ET_ERROR;
  }
  if( sscanf(argv[2],"%d-%*d-%*d-%*d-%*c-%d",&rightHeight,&rightWidth)!=2 ){
    interp->result = "Second argument is not a font size";
    return ET_ERROR;
  }
  result = leftHeight - rightHeight;
  if( result==0 ) result = leftWidth - rightWidth;
  sprintf(interp->result,"%d",result);
  return ET_OK;
}