1/*
2 * Copyright 2007, Axel D��rfler, axeld@pinc-software.de. All rights reserved.
3 * Copyright 2004, Fran��ois Revol. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
6
7
8#include <getopt.h>
9#include <stdio.h>
10
11#include <Application.h>
12#include <Font.h>
13#include <String.h>
14
15
16static struct option const kLongOptions[] = {
17	{"styles", no_argument, 0, 's'},
18	{"long", no_argument, 0, 'l'},
19	{"tuned", no_argument, 0, 't'},
20	{"help", no_argument, 0, 'h'},
21	{NULL}
22};
23
24extern const char *__progname;
25static const char *sProgramName = __progname;
26
27
28void
29usage(void)
30{
31	printf("%s [-s] [-l]\n", sProgramName);
32	printf("lists currently installed font families.\n");
33	printf("\t-s  --styles  list styles for each family\n");
34	printf("\t-l  --long    long listing with more info (spacing, encoding,\n"
35		"\t\t\theight (ascent/descent/leading), ...)\n");
36	printf("\t-t  --tuned   display tuned fonts\n");
37#ifndef __HAIKU__
38	printf("\t-u            update font families\n");
39#endif
40}
41
42
43int
44main(int argc, char **argv)
45{
46	// parse command line parameters
47
48	bool displayStyles = false;
49	bool displayLong = false;
50	bool displayTuned = false;
51#ifndef __HAIKU__
52	bool updateFamilies = false;
53#endif
54
55	int c;
56	while ((c = getopt_long(argc, argv, "sltuh", kLongOptions, NULL)) != -1) {
57		switch (c) {
58			case 0:
59				break;
60			case 'h':
61				usage();
62				return 0;
63			default:
64				usage();
65				return 1;
66
67			case 't':
68				displayTuned = true;
69			case 'l':
70				displayLong = true;
71			case 's':
72				displayStyles = true;
73				break;
74#ifndef __HAIKU__
75			case 'u':
76				updateFamilies = true;
77				break;
78#endif
79		}
80	}
81
82	BApplication app("application/x-vnd.Haiku-listfont");
83
84#ifndef __HAIKU__
85	if (updateFamilies) {
86		bool changed = update_font_families(true);
87		printf("font families %s.\n", changed ? "changed" : "did not change");
88		return 0;
89	}
90#endif
91
92	int32 familyCount = count_font_families();
93
94	if (displayLong) {
95		printf("name/style                            face  spc. enc. "
96			"height (a, d, l)  flags\n\n");
97	}
98
99	for (int32 f = 0; f < familyCount; f++) {
100		font_family family;
101		if (get_font_family(f, &family) < B_OK)
102			continue;
103		if (!displayStyles) {
104			printf("%s\n", family);
105			continue;
106		}
107
108		int32 styleCount = count_font_styles(family);
109
110		for (int32 s = 0; s < styleCount; s++) {
111			font_style style;
112			uint16 face;
113			uint32 flags;
114			if (get_font_style(family, s, &style, &face, &flags) < B_OK)
115				continue;
116
117			if (!displayLong) {
118				printf("%s/%s\n", family, style);
119				continue;
120			}
121
122			BString fontName;
123			fontName << family << "/" << style;
124			printf("%-37s", fontName.String());
125
126			BFont font;
127			font.SetFamilyAndStyle(family, style);
128			printf(" 0x%02x  %-4d %-4d", face, font.Spacing(), font.Encoding());
129
130			font_height fh;
131			font.GetHeight(&fh);
132			printf(" %5.2f, %4.2f, %4.2f ", fh.ascent, fh.descent, fh.leading);
133			if ((flags & B_IS_FIXED) != 0)
134				printf("fixed");
135
136			if ((flags & B_HAS_TUNED_FONT) != 0) {
137				if (displayTuned)
138					printf("\n    ");
139				else if ((flags & B_IS_FIXED) != 0)
140					printf(", ");
141
142				int32 tunedCount = font.CountTuned();
143				printf("%" B_PRId32 " tuned", tunedCount);
144
145				if (displayTuned) {
146					printf(":");
147					for (int32 i = 0; i < tunedCount; i++) {
148						tuned_font_info info;
149						font.GetTunedInfo(i, &info);
150						printf("\n\t(size %4.1f, "
151							"shear %5.3f, "
152							"rot. %5.3f, "
153							"flags 0x%" B_PRIx32 ", "
154							"face 0x%x)",
155								info.size,
156								info.shear,
157								info.rotation,
158								info.flags,
159								info.face);
160					}
161				}
162			}
163			putchar('\n');
164		}
165	}
166	return 0;
167}
168