static EFont * Efont_load(const char *file, int size) { static char ttfont_path_set = 0; char s[4096]; EFont *f; if (!ttfont_path_set) { EFonts_Init(); ttfont_path_set = 1; } Esnprintf(s, sizeof(s), "%s/%d", file, size); f = imlib_load_font(s); return f; }
void set_font() { Imlib_Font font; /* set the font cache to 512Kb - again to avoid re-loading */ imlib_set_font_cache_size(512 * 1024); /* add the ./ttfonts dir to our font path - you'll want a notepad.ttf */ /* in that dir for the text to display */ imlib_add_path_to_font_path("/usr/share/fonts/TTF"); /* draw text - centered with the current mouse x, y */ font = imlib_load_font("times/10"); if (font) { /* set the current font */ imlib_context_set_font(font); imlib_context_set_anti_alias(1); } else { fprintf(stderr, "Font not found: %s\n", "times/8"); } }
int main(int argc, char **argv) { int w, h, tw, th; Imlib_Image im_bg = NULL; XEvent ev; KeySym keysym; static char kbuf[20]; Imlib_Font font; Imlib_Color_Range range; const char *display_name = getenv("DISPLAY"); /** * First tests to determine which rendering task to perform */ if (display_name == NULL) display_name = ":0"; disp = XOpenDisplay(display_name); if (disp == NULL) { fprintf(stderr, "Can't open display %s\n", display_name); return 1; } vis = DefaultVisual(disp, DefaultScreen(disp)); depth = DefaultDepth(disp, DefaultScreen(disp)); cm = DefaultColormap(disp, DefaultScreen(disp)); win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 100, 100, 0, 0, 0); XSelectInput(disp, win, ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | PointerMotionMask | ExposureMask | KeyPressMask); XMapWindow(disp, win); /** * Start rendering */ imlib_set_font_cache_size(512 * 1024); imlib_add_path_to_font_path(PACKAGE_DATA_DIR"/data/fonts"); imlib_context_set_display(disp); imlib_context_set_visual(vis); imlib_context_set_colormap(cm); imlib_context_set_drawable(win); imlib_context_set_blend(0); imlib_context_set_color_modifier(NULL); imlib_context_set_blend(0); im_bg = imlib_create_image(600, 400); imlib_context_set_image(im_bg); w = imlib_image_get_width(); h = imlib_image_get_height(); imlib_context_set_color(128, 128, 255, 255); imlib_image_fill_rectangle(0, 0, w, h); XResizeWindow(disp, win, w, h); XSync(disp, False); while (1) { do { XNextEvent(disp, &ev); switch (ev.type) { case ButtonRelease: exit(0); break; case KeyPress: XLookupString(&ev.xkey, (char *)kbuf, sizeof(kbuf), &keysym, NULL); switch (*kbuf) { case 'q': exit(0); default: break; } break; default: break; } } while (XPending(disp)); imlib_context_set_image(im_bg); imlib_context_set_color(128, 128, 255, 255); imlib_image_fill_rectangle(0, 0, w, h); imlib_context_set_color(0, 0, 0, 255); imlib_image_draw_rectangle(20, 20, 560, 140); imlib_image_draw_rectangle(20, 220, 560, 140); font = imlib_load_font("notepad/15"); if (font) { char text[4096]; imlib_context_set_font(font); imlib_context_set_color(0, 0, 0, 255); sprintf(text, "RGBA range, 2 points, from red to magenta"); imlib_get_text_size(text, &tw, &th); imlib_text_draw(300 - tw / 2, 180 - th / 2, text); sprintf(text, "HSVA range, 2 points, from red to magenta"); imlib_get_text_size(text, &tw, &th); imlib_text_draw(300 - tw / 2, 380 - th / 2, text); imlib_free_font(); } /* Draw rectangle w/ RGBA gradient */ range = imlib_create_color_range(); imlib_context_set_color_range(range); imlib_context_set_color(255, 0, 0, 255); imlib_add_color_to_color_range(0); imlib_context_set_color(255, 0, 255, 255); imlib_add_color_to_color_range(20); imlib_image_fill_color_range_rectangle(21, 21, 558, 138, -90.0); imlib_free_color_range(); /* Draw rectangle w/ HSVA gradient */ range = imlib_create_color_range(); imlib_context_set_color_range(range); imlib_context_set_color_hsva(0, 1, 1, 255); imlib_add_color_to_color_range(0); imlib_context_set_color_hsva(300, 1, 1, 255); imlib_add_color_to_color_range(20); imlib_image_fill_hsva_color_range_rectangle(21, 221, 558, 138, -90.0); imlib_free_color_range(); imlib_render_image_on_drawable(0, 0); } return 0; }
/* TODO s/bit/lot */ void init_thumbnail_mode(void) { /* moved to thumbnail_data: Imlib_Image im_main; Imlib_Image bg_im = NULL; Imlib_Font fn = NULL; Imlib_Font title_fn = NULL; int w = 800, h = 600; int bg_w = 0, bg_h = 0; int text_area_w, text_area_h; int max_column_w = 0; */ Imlib_Image im_temp; int ww = 0, hh = 0, www, hhh, xxx, yyy; int x = 0, y = 0; winwidget winwid = NULL; Imlib_Image im_thumb = NULL; unsigned char trans_bg = 0; int title_area_h = 0; int tw = 0, th = 0; int fw_name, fw_size, fw_dim, fh; int thumbnailcount = 0; feh_file *file = NULL; gib_list *l, *last = NULL; int lines; int index_image_width, index_image_height; int x_offset_name = 0, x_offset_dim = 0, x_offset_size = 0; char *s; unsigned int thumb_counter = 0; /* initialize thumbnail mode data */ td.im_main = NULL; td.im_bg = NULL; td.font_main = NULL; td.font_title = NULL; td.w = 640; td.h = 480; td.bg_w = 0; td.bg_h = 0; td.thumb_tot_h = 0; td.text_area_w = 0; td.text_area_h = 0; td.vertical = 0; td.max_column_w = 0; mode = "thumbnail"; if (opt.font) td.font_main = gib_imlib_load_font(opt.font); if (!td.font_main) td.font_main = gib_imlib_load_font(DEFAULT_FONT); if (opt.title_font) { int fh, fw; td.font_title = gib_imlib_load_font(opt.title_font); gib_imlib_get_text_size(td.font_title, "W", NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT); title_area_h = fh + 4; } else td.font_title = imlib_load_font(DEFAULT_FONT_TITLE); if ((!td.font_main) || (!td.font_title)) eprintf("Error loading fonts"); /* Work out how tall the font is */ gib_imlib_get_text_size(td.font_main, "W", NULL, &tw, &th, IMLIB_TEXT_TO_RIGHT); /* For now, allow room for the right number of lines with small gaps */ td.text_area_h = ((th + 2) * (opt.index_show_name + opt.index_show_size + opt.index_show_dim)) + 5; /* This includes the text area for index data */ td.thumb_tot_h = opt.thumb_h + td.text_area_h; /* Use bg image dimensions for default size */ if (opt.bg && opt.bg_file) { if (!strcmp(opt.bg_file, "trans")) trans_bg = 1; else { D(3, ("Time to apply a background to blend onto\n")); if (feh_load_image_char(&td.im_bg, opt.bg_file) != 0) { td.bg_w = gib_imlib_image_get_width(td.im_bg); td.bg_h = gib_imlib_image_get_height(td.im_bg); } } } /* figure out geometry for the main window and entries */ feh_thumbnail_calculate_geometry(); index_image_width = td.w; index_image_height = td.h + title_area_h; td.im_main = imlib_create_image(index_image_width, index_image_height); gib_imlib_image_set_has_alpha(td.im_main, 1); if (!td.im_main) eprintf("Imlib error creating index image, are you low on RAM?"); if (td.im_bg) gib_imlib_blend_image_onto_image(td.im_main, td.im_bg, gib_imlib_image_has_alpha (td.im_bg), 0, 0, td.bg_w, td.bg_h, 0, 0, td.w, td.h, 1, 0, 0); else if (trans_bg) { gib_imlib_image_fill_rectangle(td.im_main, 0, 0, td.w, td.h + title_area_h, 0, 0, 0, 0); gib_imlib_image_set_has_alpha(td.im_main, 1); } else { /* Colour the background */ gib_imlib_image_fill_rectangle(td.im_main, 0, 0, td.w, td.h + title_area_h, 0, 0, 0, 255); } /* Create title now */ if (!opt.title) s = estrdup(PACKAGE " [thumbnail mode]"); else s = estrdup(feh_printf(opt.title, NULL)); if (opt.display) { winwid = winwidget_create_from_image(td.im_main, s, WIN_TYPE_THUMBNAIL); winwidget_show(winwid); } /* make sure we have an ~/.thumbnails/normal directory for storing permanent thumbnails */ td.cache_thumbnails = opt.cache_thumbnails; if (td.cache_thumbnails) { if (opt.thumb_w > opt.thumb_h) td.cache_dim = opt.thumb_w; else td.cache_dim = opt.thumb_h; if (td.cache_dim > 256) { /* No caching as specified by standard. Sort of. */ td.cache_thumbnails = 0; } else if (td.cache_dim > 128) { td.cache_dim = 256; td.cache_dir = estrdup("large"); } else { td.cache_dim = 128; td.cache_dir = estrdup("normal"); } feh_thumbnail_setup_thumbnail_dir(); } for (l = filelist; l; l = l->next) { file = FEH_FILE(l->data); if (last) { filelist = feh_file_remove_from_list(filelist, last); last = NULL; } D(4, ("About to load image %s\n", file->filename)); /* if (feh_load_image(&im_temp, file) != 0) */ if (feh_thumbnail_get_thumbnail(&im_temp, file) != 0) { if (opt.verbose) feh_display_status('.'); D(4, ("Successfully loaded %s\n", file->filename)); www = opt.thumb_w; hhh = opt.thumb_h; ww = gib_imlib_image_get_width(im_temp); hh = gib_imlib_image_get_height(im_temp); thumbnailcount++; if (gib_imlib_image_has_alpha(im_temp)) imlib_context_set_blend(1); else imlib_context_set_blend(0); if (opt.aspect) { double ratio = 0.0; /* Keep the aspect ratio for the thumbnail */ ratio = ((double) ww / hh) / ((double) www / hhh); if (ratio > 1.0) hhh = opt.thumb_h / ratio; else if (ratio != 1.0) www = opt.thumb_w * ratio; } if ((!opt.stretch) && ((www > ww) || (hhh > hh))) { /* Don't make the image larger unless stretch is specified */ www = ww; hhh = hh; } im_thumb = gib_imlib_create_cropped_scaled_image(im_temp, 0, 0, ww, hh, www, hhh, 1); gib_imlib_free_image_and_decache(im_temp); if (opt.alpha) { DATA8 atab[256]; D(3, ("Applying alpha options\n")); gib_imlib_image_set_has_alpha(im_thumb, 1); memset(atab, opt.alpha_level, sizeof(atab)); gib_imlib_apply_color_modifier_to_rectangle (im_thumb, 0, 0, www, hhh, NULL, NULL, NULL, atab); } td.text_area_w = opt.thumb_w; /* Now draw on the info text */ if (opt.index_show_name) { gib_imlib_get_text_size(td.font_main, file->name, NULL, &fw_name, &fh, IMLIB_TEXT_TO_RIGHT); if (fw_name > td.text_area_w) td.text_area_w = fw_name; } if (opt.index_show_dim) { gib_imlib_get_text_size(td.font_main, create_index_dimension_string(ww, hh), NULL, &fw_dim, &fh, IMLIB_TEXT_TO_RIGHT); if (fw_dim > td.text_area_w) td.text_area_w = fw_dim; } if (opt.index_show_size) { gib_imlib_get_text_size(td.font_main, create_index_size_string(file->filename), NULL, &fw_size, &fh, IMLIB_TEXT_TO_RIGHT); if (fw_size > td.text_area_w) td.text_area_w = fw_size; } if (td.text_area_w > opt.thumb_w) td.text_area_w += 5; /* offsets for centering text */ x_offset_name = (td.text_area_w - fw_name) / 2; x_offset_dim = (td.text_area_w - fw_dim) / 2; x_offset_size = (td.text_area_w - fw_size) / 2; if (td.vertical) { if (td.text_area_w > td.max_column_w) td.max_column_w = td.text_area_w; if (y > td.h - td.thumb_tot_h) { y = 0; x += td.max_column_w; td.max_column_w = 0; } if (x > td.w - td.text_area_w) break; } else { if (x > td.w - td.text_area_w) { x = 0; y += td.thumb_tot_h; } if (y > td.h - td.thumb_tot_h) break; } /* center image relative to the text below it (if any) */ xxx = x + ((td.text_area_w - www) / 2); yyy = y; if (opt.aspect) yyy += (opt.thumb_h - hhh) / 2; /* Draw now */ gib_imlib_blend_image_onto_image(td.im_main, im_thumb, gib_imlib_image_has_alpha (im_thumb), 0, 0, www, hhh, xxx, yyy, www, hhh, 1, gib_imlib_image_has_alpha(im_thumb), 0); thumbnails = gib_list_add_front(thumbnails, feh_thumbnail_new(file, xxx, yyy, www, hhh)); gib_imlib_free_image_and_decache(im_thumb); lines = 0; if (opt.index_show_name) gib_imlib_text_draw(td.im_main, td.font_main, NULL, x + x_offset_name, y + opt.thumb_h + (lines++ * (th + 2)) + 2, file->name, IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255); if (opt.index_show_dim) gib_imlib_text_draw(td.im_main, td.font_main, NULL, x + x_offset_dim, y + opt.thumb_h + (lines++ * (th + 2)) + 2, create_index_dimension_string(ww, hh), IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255); if (opt.index_show_size) gib_imlib_text_draw(td.im_main, td.font_main, NULL, x + x_offset_size, y + opt.thumb_h + (lines++ * (th + 2)) + 2, create_index_size_string(file->filename), IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255); if (td.vertical) y += td.thumb_tot_h; else x += td.text_area_w; } else { if (opt.verbose) feh_display_status('x'); last = l; } if (opt.display) { /* thumb_counter is unsigned, so no need to catch overflows */ if (++thumb_counter == opt.thumb_redraw) { winwidget_render_image(winwid, 0, 0); thumb_counter = 0; } if (!feh_main_iteration(0)) exit(0); } } if (thumb_counter != 0) winwidget_render_image(winwid, 0, 0); if (opt.verbose) fprintf(stdout, "\n"); if (opt.title_font) { int fw, fh, fx, fy; char *s; s = create_index_title_string(thumbnailcount, td.w, td.h); gib_imlib_get_text_size(td.font_title, s, NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT); fx = (index_image_width - fw) >> 1; fy = index_image_height - fh - 2; gib_imlib_text_draw(td.im_main, td.font_title, NULL, fx, fy, s, IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255); }
int Configure(void **ctxp, int argc, char *argv[]) { int c; ContextInfo *ci; char *font = "LucidaSansDemiBold/16"; char *fp = getenv("FONTPATH"); char *color = 0; FILE *f; *ctxp = av_mallocz(sizeof(ContextInfo)); ci = (ContextInfo *) *ctxp; optind = 0; if (fp) imlib_add_path_to_font_path(fp); while ((c = getopt(argc, argv, "c:f:F:t:x:y:")) > 0) { switch (c) { case 'c': color = optarg; break; case 'F': font = optarg; break; case 't': ci->text = av_strdup(optarg); break; case 'f': ci->file = av_strdup(optarg); break; case 'x': ci->x = atoi(optarg); break; case 'y': ci->y = atoi(optarg); break; case '?': fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]); return -1; } } ci->fn = imlib_load_font(font); if (!ci->fn) { fprintf(stderr, "Failed to load font '%s'\n", font); return -1; } imlib_context_set_font(ci->fn); imlib_context_set_direction(IMLIB_TEXT_TO_RIGHT); if (color) { char buff[256]; int done = 0; f = fopen("/usr/lib/X11/rgb.txt", "r"); if (!f) { fprintf(stderr, "Failed to find rgb.txt\n"); return -1; } while (fgets(buff, sizeof(buff), f)) { int r, g, b; char colname[80]; if (sscanf(buff, "%d %d %d %64s", &r, &g, &b, colname) == 4 && strcasecmp(colname, color) == 0) { ci->r = r; ci->g = g; ci->b = b; /* fprintf(stderr, "%s -> %d,%d,%d\n", colname, r, g, b); */ done = 1; break; } } fclose(f); if (!done) { fprintf(stderr, "Unable to find color '%s' in rgb.txt\n", color); return -1; } } imlib_context_set_color(ci->r, ci->g, ci->b, 255); return 0; }