Skip to content

Commit 704476e

Browse files
committed
Added DPI handing for graph font size. DPI is detected on duc-gui X11 and Glfw, and can be configured on the cmd line with the --dpi option for duc-cgi and duc-graph. Closes #201
1 parent 4ffeb6d commit 704476e

File tree

10 files changed

+57
-7
lines changed

10 files changed

+57
-7
lines changed

doc/duc.1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ select database file to use [~/\.duc\.db]
283283
show number of files instead of file size
284284
.
285285
.TP
286+
\fB\-\-dpi=VAL\fR
287+
set destination resolution in DPI [96\.0]
288+
.
289+
.TP
286290
\fB\-f\fR, \fB\-\-format=VAL\fR
287291
select output format \fIpng|svg|pdf|html\fR [png]
288292
.
@@ -338,6 +342,10 @@ url of CSS style sheet to use instead of default CSS
338342
select database file to use [~/\.duc\.db]
339343
.
340344
.TP
345+
\fB\-\-dpi=VAL\fR
346+
set destination resolution in DPI [96\.0]
347+
.
348+
.TP
341349
\fB\-\-footer=VAL\fR
342350
select HTML file to include as footer
343351
.

doc/duc.1.html

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/duc.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ Options for command `duc graph [options] [PATH]`:
278278
* `--count`:
279279
show number of files instead of file size
280280

281+
* `--dpi=VAL`:
282+
set destination resolution in DPI [96.0]
283+
281284
* `-f`, `--format=VAL`:
282285
select output format <png|svg|pdf|html> [png]
283286

@@ -322,6 +325,9 @@ Options for command `duc cgi [options] [PATH]`:
322325
* `-d`, `--database=VAL`:
323326
select database file to use [~/.duc.db]
324327

328+
* `--dpi=VAL`:
329+
set destination resolution in DPI [96.0]
330+
325331
* `--footer=VAL`:
326332
select HTML file to include as footer
327333

src/duc/cmd-cgi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static int opt_levels = 4;
3737
static char *opt_palette = NULL;
3838
static bool opt_tooltip = false;
3939
static int opt_ring_gap = 4;
40+
static double opt_dpi = 96.0;
4041

4142
static struct param *param_list = NULL;
4243

@@ -526,6 +527,7 @@ static int cgi_main(duc *duc, int argc, char **argv)
526527

527528
duc_graph *graph = duc_graph_new_html(duc, stdout, 0);
528529
duc_graph_set_size(graph, opt_size, opt_size);
530+
duc_graph_set_dpi(graph, opt_dpi);
529531
duc_graph_set_max_level(graph, opt_levels);
530532
duc_graph_set_fuzz(graph, opt_fuzz);
531533
duc_graph_set_palette(graph, palette);
@@ -549,6 +551,7 @@ static struct ducrc_option options[] = {
549551
{ &opt_count, "count", 0, DUCRC_TYPE_BOOL, "show number of files instead of file size" },
550552
{ &opt_css_url, "css-url", 0, DUCRC_TYPE_STRING, "url of CSS style sheet to use instead of default CSS" },
551553
{ &opt_database, "database", 'd', DUCRC_TYPE_STRING, "select database file to use [~/.duc.db]" },
554+
{ &opt_dpi, "dpi", 0 , DUCRC_TYPE_DOUBLE, "set destination resolution in DPI [96.0]" },
552555
{ &opt_footer, "footer", 0, DUCRC_TYPE_STRING, "select HTML file to include as footer" },
553556
{ &opt_fuzz, "fuzz", 0, DUCRC_TYPE_DOUBLE, "use radius fuzz factor when drawing graph [0.7]" },
554557
{ &opt_gradient, "gradient", 0, DUCRC_TYPE_BOOL, "draw graph with color gradient" },

src/duc/cmd-graph.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static char *opt_palette = NULL;
2727
static enum duc_graph_palette palette = 0;
2828
static int opt_ring_gap = 4;
2929
static bool opt_gradient = false;
30+
static double opt_dpi = 96.0;
3031

3132
#ifdef ENABLE_CAIRO
3233
static char *opt_format = "png";
@@ -119,6 +120,7 @@ static int graph_main(duc *duc, int argc, char **argv)
119120
opt_apparent ? DUC_SIZE_TYPE_APPARENT : DUC_SIZE_TYPE_ACTUAL;
120121

121122
duc_graph_set_size(graph, opt_size, opt_size);
123+
duc_graph_set_dpi(graph, opt_dpi);
122124
duc_graph_set_fuzz(graph, opt_fuzz);
123125
duc_graph_set_max_level(graph, opt_levels);
124126
duc_graph_set_palette(graph, palette);
@@ -140,6 +142,7 @@ static struct ducrc_option options[] = {
140142
{ &opt_apparent, "apparent", 'a', DUCRC_TYPE_BOOL, "Show apparent instead of actual file size" },
141143
{ &opt_database, "database", 'd', DUCRC_TYPE_STRING, "select database file to use [~/.duc.db]" },
142144
{ &opt_count, "count", 0, DUCRC_TYPE_BOOL, "show number of files instead of file size" },
145+
{ &opt_dpi, "dpi", 0 , DUCRC_TYPE_DOUBLE, "set destination resolution in DPI [96.0]" },
143146
{ &opt_format, "format", 'f', DUCRC_TYPE_STRING,
144147
#ifdef ENABLE_CAIRO
145148
"select output format <png|svg|pdf|html> [png]" },

src/duc/cmd-gui.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,15 @@ int gui_main(duc *duc, int argc, char *argv[])
260260
cs = cairo_xlib_surface_create(dpy, win, DefaultVisual(dpy, 0), win_w, win_h);
261261
cr = cairo_create(cs);
262262

263-
264-
265263
graph = duc_graph_new_cairo(duc, cr);
266264

265+
int w_px = DisplayWidth(dpy, 0);
266+
int w_mm = DisplayWidthMM(dpy, 0);
267+
if(w_px && w_mm) {
268+
double dpi = 25.4 * w_px / w_mm;
269+
duc_graph_set_dpi(graph, dpi);
270+
}
271+
267272
do_gui(duc, graph, dir);
268273

269274
duc_dir_close(dir);

src/duc/cmd-guigl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ int guigl_main(duc *duc, int argc, char *argv[])
201201
sc2fb(window, &font_scale, NULL);
202202
graph = duc_graph_new_opengl(duc, font_scale);
203203

204+
GLFWmonitor* mon = glfwGetPrimaryMonitor();
205+
const GLFWvidmode* mode = glfwGetVideoMode(mon);
206+
if(mode) {
207+
int w_mm = 0, h_mm = 0;
208+
glfwGetMonitorPhysicalSize(mon, &w_mm, &h_mm);
209+
if(mode && mode->width && w_mm) {
210+
double dpi = 25.4 * mode->width / w_mm;
211+
printf("dpi %f\n", dpi);
212+
duc_graph_set_dpi(graph, dpi);
213+
}
214+
}
215+
204216
glfwSetKeyCallback(window, cb_keyboard);
205217
glfwSetFramebufferSizeCallback(window, cb_winsize);
206218
glfwSetMouseButtonCallback(window, cb_mouse_button);

src/libduc-graph/duc-graph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void duc_graph_free(duc_graph *g);
4141

4242
void duc_graph_set_max_level(duc_graph *g, int max_level);
4343
void duc_graph_set_size(duc_graph *g, int w, int h);
44+
void duc_graph_set_dpi(duc_graph *g, double dpi);
4445
void duc_graph_set_position(duc_graph *g, double x, double y);
4546
void duc_graph_set_tooltip(duc_graph *g, double x, double y);
4647
void duc_graph_set_palette(duc_graph *g, enum duc_graph_palette p);

src/libduc-graph/graph-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct duc_graph {
3131

3232
struct duc *duc;
3333
double size;
34+
double dpi;
35+
double font_scale;
3436
double cx, cy;
3537
double pos_x, pos_y;
3638
double width, height;

src/libduc-graph/graph.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ duc_graph *duc_graph_new(duc *duc)
3131
g->duc = duc;
3232
g->r_start = 100;
3333
g->fuzz = 0;
34+
duc_graph_set_dpi(g, 96);
3435
duc_graph_set_max_level(g, 3);
3536
duc_graph_set_size(g, 400, 400);
3637

@@ -65,6 +66,13 @@ void duc_graph_set_size(duc_graph *g, int w, int h)
6566
}
6667

6768

69+
void duc_graph_set_dpi(duc_graph *g, double dpi)
70+
{
71+
g->dpi = dpi;
72+
g->font_scale = dpi / 96.0;
73+
}
74+
75+
6876
void duc_graph_set_position(duc_graph *g, double pos_x, double pos_y)
6977
{
7078
g->pos_x = pos_x;
@@ -430,22 +438,22 @@ int duc_graph_draw(duc_graph *g, duc_dir *dir)
430438

431439
LL_FOREACH_SAFE(g->label_list, l, ln) {
432440
if(g->backend)
433-
g->backend->draw_text(g, (int)l->x, (int)l->y, FONT_SIZE_LABEL, l->text);
441+
g->backend->draw_text(g, (int)l->x, (int)l->y, FONT_SIZE_LABEL * g->font_scale, l->text);
434442
free(l->text);
435443
free(l);
436444
}
437445

438446
char *p = duc_dir_get_path(dir);
439447
if(g->backend)
440-
g->backend->draw_text(g, (int)g->cx, 10, FONT_SIZE_LABEL, p);
448+
g->backend->draw_text(g, (int)g->cx, 10, FONT_SIZE_LABEL * g->font_scale, p);
441449
free(p);
442450

443451
struct duc_size size;
444452
duc_dir_get_size(dir, &size);
445453
char siz[16];
446454
duc_human_size(&size, g->size_type, g->bytes, siz, sizeof siz);
447455
if(g->backend)
448-
g->backend->draw_text(g, (int)g->cx, (int)g->cy, FONT_SIZE_CENTER, siz);
456+
g->backend->draw_text(g, (int)g->cx, (int)g->cy, FONT_SIZE_CENTER * g->font_scale, siz);
449457

450458
if(g->tooltip_r < g->r_start) {
451459
gen_tooltip(g, &size, NULL, DUC_FILE_TYPE_DIR);

0 commit comments

Comments
 (0)