void zwm_decor_init(void) { int i; XGlyphInfo info; cmap = DefaultColormap(dpy, scr); gc = XCreateGC(dpy, root, 0, NULL); memset(icons,0,64); xfont = XftFontOpenName (dpy, scr, "DejaVu Sans-9:bold"); if(!xfont)xfont = XftFontOpenXlfd(dpy, scr, config.font); ifont = XftFontOpenName (dpy, scr, "DejaVu Sans-16:bold"); if(!ifont)ifont = XftFontOpenXlfd(dpy, scr, config.icons); XftColorAllocValue(dpy, DefaultVisual(dpy, scr), cmap, &xcolor.color, &xcolor); config.xcolor_nborder = alloc_color(config.normal_border_color); config.xcolor_fborder = alloc_color(config.focus_border_color); config.xcolor_nbg = alloc_color(config.normal_bg_color); config.xcolor_fbg = alloc_color(config.focus_bg_color); config.xcolor_flbg = alloc_color(config.float_bg_color); config.xcolor_nshadow = alloc_color(config.normal_shadow_color); config.xcolor_fshadow = alloc_color(config.focus_shadow_color); config.xcolor_ntitle = alloc_color(config.normal_title_color); config.xcolor_ftitle = alloc_color(config.focus_title_color); config.xcolor_root = alloc_color(config.root_bg_color); get_status(icons, 64); XftTextExtentsUtf8(dpy, xfont, (FcChar8*)" [000] ", 7, &info); char_width = info.width; XftTextExtentsUtf8(dpy, xfont, (FcChar8*)icons, strlen(icons), &info); date_width = info.width; icons[0] = 0; for(i=0; i<32 && config.buttons[i].func; i++){ strcat(icons, config.buttons[i].c); config.button_count = i+1; } if(config.button_count){ XftTextExtentsUtf8(dpy, ifont, (FcChar8*)icons, strlen(icons), &info); config.button_width = info.width / config.button_count; config.icon_y = info.height + (config.title_height - info.height)/2 + 1; } config.title_y = config.title_height - xfont->descent - 2; }
/*----------------------------------------------------------------------------*/ void VKLoadXResource() { VKLocaleInit(); VKLoadPalette(); if( !vk_font_name ) vk_font_name = strdup(screen_width<=800 ? VK_FONT_SMALL : VK_FONT_LARGE); if( !vk_font_name ) error("Error: not enough memory!"); #ifdef USE_XFT vk_font = XftFontOpenName(display, 0, vk_font_name); if( !vk_font ) vk_font = XftFontOpenXlfd(display, 0, vk_font_name); #else vk_font = XLoadQueryFont(display, vk_font_name); if( !vk_font ) vk_font = XLoadQueryFont(display, screen_width<=800 ? VK_FONT_SMALL : VK_FONT_LARGE); VKFontSetInit(); #endif if( !vk_font ) error("Cannot load font %s\n", vk_font_name); vk_hand = XCreateFontCursor(display, XC_hand2); vk_cursor = XCreateFontCursor(display, XC_left_ptr); vk_attrib.override_redirect = True; #ifdef USE_XFT vk_text_height = vk_font->height; vk_text_ascent = vk_font->ascent; #else { XFontSetExtents *fse = XExtentsOfFontSet(vk_fontset); vk_text_height = fse->max_logical_extent.height + 4; vk_text_ascent = vk_font->ascent + 3; } #endif }
void setup_font(struct font *font, char *fontstr) { if(!(font->xfont = XftFontOpenName(dpy, screen, fontstr))) { err("WARNING: Could not setup font: '%s'. Falling back to 'fixed'\n", fontstr); if(!(font->xfont = XftFontOpenXlfd(dpy, screen, "fixed"))) die("ERROR: Could not setup font\n"); } font->h = font->xfont->ascent + font->xfont->descent; }
WMFont* WMCreateFont(WMScreen *scrPtr, char *fontName) { WMFont *font; Display *display = scrPtr->display; char *fname, *ptr; /* This is for back-compat (to allow reading of old xlfd descriptions) */ if (fontName[0]=='-' && (ptr = strchr(fontName, ','))) { // warn for deprecation fname = wmalloc(ptr - fontName + 1); strncpy(fname, fontName, ptr - fontName); fname[ptr - fontName] = 0; } else { fname = wstrdup(fontName); } font = WMHashGet(scrPtr->fontCache, fname); if (font) { WMRetainFont(font); wfree(fname); return font; } font = wmalloc(sizeof(WMFont)); memset(font, 0, sizeof(WMFont)); font->screen = scrPtr; // remove printf("WMCreateFont: %s\n", fname); if (fname[0] == '-') { // Backward compat thing. Remove in a later version font->font = XftFontOpenXlfd(display, scrPtr->screen, fname); } else { font->font = XftFontOpenName(display, scrPtr->screen, fname); } if (!font->font) { wfree(font); wfree(fname); return NULL; } font->height = font->font->ascent+font->font->descent; font->y = font->font->ascent; font->refCount = 1; font->name = fname; assert(WMHashInsert(scrPtr->fontCache, font->name, font)==NULL); return font; }
void setfont(const char *fontstr) { #ifndef DZEN_XFT char *def, **missing; int i, n; missing = NULL; if(dzen.font.set) XFreeFontSet(dzen.dpy, dzen.font.set); dzen.font.set = XCreateFontSet(dzen.dpy, fontstr, &missing, &n, &def); if(missing) XFreeStringList(missing); if(dzen.font.set) { XFontSetExtents *font_extents; XFontStruct **xfonts; char **font_names; dzen.font.ascent = dzen.font.descent = 0; font_extents = XExtentsOfFontSet(dzen.font.set); n = XFontsOfFontSet(dzen.font.set, &xfonts, &font_names); for(i = 0, dzen.font.ascent = 0, dzen.font.descent = 0; i < n; i++) { if(dzen.font.ascent < (*xfonts)->ascent) dzen.font.ascent = (*xfonts)->ascent; if(dzen.font.descent < (*xfonts)->descent) dzen.font.descent = (*xfonts)->descent; xfonts++; } } else { if(dzen.font.xfont) XFreeFont(dzen.dpy, dzen.font.xfont); dzen.font.xfont = NULL; if(!(dzen.font.xfont = XLoadQueryFont(dzen.dpy, fontstr))) eprint("dzen: error, cannot load font: '%s'\n", fontstr); dzen.font.ascent = dzen.font.xfont->ascent; dzen.font.descent = dzen.font.xfont->descent; } dzen.font.height = dzen.font.ascent + dzen.font.descent; #else if(dzen.font.xftfont) XftFontClose(dzen.dpy, dzen.font.xftfont); dzen.font.xftfont = XftFontOpenXlfd(dzen.dpy, dzen.screen, fontstr); if(!dzen.font.xftfont) dzen.font.xftfont = XftFontOpenName(dzen.dpy, dzen.screen, fontstr); if(!dzen.font.xftfont) eprint("error, cannot load font: '%s'\n", fontstr); dzen.font.extents = malloc(sizeof(XGlyphInfo)); XftTextExtentsUtf8(dzen.dpy, dzen.font.xftfont, (unsigned const char *) fontstr, strlen(fontstr), dzen.font.extents); dzen.font.height = dzen.font.xftfont->ascent + dzen.font.xftfont->descent; dzen.font.width = (dzen.font.extents->width)/strlen(fontstr); #endif }
/* helper function to correctly open Xft/core fonts by name #### Can't we use FcParseName here? #### Is this done so often that the logic needs to be hard-coded in C? Daniel Pittman sez: Older code tried to enforce that an XLFD font was not scaled, while this version just doesn't care. I think that is a better behavior, since if someone really wants a scaled font we should oblige them. Stephen sez: This whole function was ill-conceived, and I'm not sure it succeeds at any of the things it attempts to do. First, we should be using fontconfig directly. I'm not sure what Xft (or fontconfig) will try to do if passed an XLFD. As for scaled fonts, both options are equally bad. The problem is that the X server will often scale bitmap fonts willy-nilly; it's worth trying to avoid that, but I can't say whether that's worth overriding somebody who knows what they're doing. In any case, I think we should find out what Xft (fontconfig?) is able and willing to do with XLFDs, and probably move the logic to LISP. */ XftFont * xft_open_font_by_name (Display *dpy, char *name) { XftFont *res = NULL; /* if (!NILP (Fxft_xlfd_font_name_p (make_string (name, strlen (name))))) */ /* #### this is bogus but ... */ int count = 0; char *pos = name; /* extra parens shut up gcc */ while ((pos = strchr (pos, '-'))) { count++; pos++; } /* #### hard-coding DefaultScreen is evil! */ if (count == 14 /* fully-qualified XLFD */ || (count < 14 /* heuristic for wildcarded XLFD */ && count >= 5 && strchr (name, '*'))) res = XftFontOpenXlfd (dpy, DefaultScreen (dpy), name); else res = XftFontOpenName (dpy, DefaultScreen (dpy), name); /* Try for a generic monospace font #### Why? Menus don't need to line up in columns! */ if (!res) res = XftFontOpenName (dpy, DefaultScreen (dpy), "monospace"); /* Try for anything we can get */ if (!res) res = XftFontOpenName (dpy, DefaultScreen (dpy), ""); if (!res) { /* #### This is Just So Wrong ... ! */ /* sorry folks ... */ fprintf (stderr, "Unable to find any usable XFT font, even the defaults!\n"); abort (); return 0; } return res; }
static int _xft_Load(TextState * ts, const char *name) { XftFont *font; FontCtxXft *fdc; if (name[0] == '-') font = XftFontOpenXlfd(disp, Dpy.screen, name); else font = XftFontOpenName(disp, Dpy.screen, name); if (!font) return -1; #if 0 /* Debug */ { FT_Face ftf = XftLockFace(font); if (ftf == NULL) return -1; Eprintf("Font %s family_name=%s style_name=%s\n", name, ftf->family_name, ftf->style_name); XftUnlockFace(font); } #endif fdc = EMALLOC(FontCtxXft, 1); if (!fdc) return -1; fdc->font = font; ts->fdc = fdc; ts->need_utf8 = 1; ts->type = FONT_TYPE_XFT; ts->ops = &FontOps_xft; return 0; }
JBoolean JXFontManager::GetFontID ( const JCharacter* xFontStr, JFontID* fontID ) const { const JSize count = itsFontList->GetElementCount(); for (JIndex i=1; i<=count; i++) { const FontInfo info = itsFontList->GetElement(i); if (*(info.name) == xFontStr && info.size == 0) { *fontID = i; return kJTrue; } } // falling through means we need to create a new entry FontInfo info; #if ENABLE_TRUE_TYPE XftFont* xft = XftFontOpenXlfd(*itsDisplay, itsDisplay->GetScreen(), xFontStr); if (xft != NULL) { info.xfont.type = kTrueType; info.xfont.xftrue = xft; } else #endif { XFontStruct* xfs = XLoadQueryFont(*itsDisplay, xFontStr); if (xfs != NULL) { info.xfont.type = kStdType; info.xfont.xfstd = xfs; } else { *fontID = 0; return kJFalse; } } info.name = new JString(xFontStr); assert( info.name != NULL ); info.size = 0; info.exact = kJTrue; info.monoWidth = IsMonospace(info.xfont); itsFontList->AppendElement(info); *fontID = itsFontList->GetElementCount(); return kJTrue; }
XftFont * load_xft_font_retry (Display *dpy, int screen, const char *xlfd) { return XftFontOpenXlfd (dpy, screen, xlfd); }
DEFont *de_load_font(const char *fontname) { DEFont *fnt; const char *default_fontname=de_default_fontname(); #ifdef HAVE_X11_XFT XftFont *font=NULL; #endif #ifdef HAVE_X11_BMF XFontSet fontset=NULL; XFontStruct *fontstruct=NULL; #endif assert(fontname!=NULL); /* There shouldn't be that many fonts... */ for(fnt=fonts; fnt!=NULL; fnt=fnt->next){ if(strcmp(fnt->pattern, fontname)==0){ fnt->refcount++; return fnt; } } #ifdef HAVE_X11_XFT LOG(DEBUG, FONT, "Loading font %s via XFT", fontname); if(strncmp(fontname, "xft:", 4)==0){ font=XftFontOpenName(ioncore_g.dpy, DefaultScreen(ioncore_g.dpy), fontname+4); }else{ #ifdef HAVE_X11_BMF goto bitmap_font; #else font=XftFontOpenXlfd(ioncore_g.dpy, DefaultScreen(ioncore_g.dpy), fontname); #endif } if(font==NULL){ if(strcmp(fontname, default_fontname)!=0){ warn(TR("Could not load font \"%s\", trying \"%s\""), fontname, default_fontname); fnt=de_load_font(default_fontname); if(fnt==NULL) LOG(WARN, FONT, TR("Failed to load fallback font.")); return fnt; } return NULL; }else{ FcPatternPrint(font->pattern); } #endif /* HAVE_X11_XFT */ #ifdef HAVE_X11_BMF #ifdef HAVE_X11_XFT bitmap_font: #endif if(ioncore_g.use_mb && !(ioncore_g.enc_utf8 && iso10646_font(fontname))){ LOG(DEBUG, FONT, "Loading fontset %s", fontname); fontset=de_create_font_set(fontname); if(fontset!=NULL){ if(XContextDependentDrawing(fontset)){ warn(TR("Fontset for font pattern '%s' implements context " "dependent drawing, which is unsupported. Expect " "clutter."), fontname); } } }else{ LOG(DEBUG, FONT, "Loading fontstruct %s", fontname); fontstruct=XLoadQueryFont(ioncore_g.dpy, fontname); } if(fontstruct==NULL && fontset==NULL){ if(strcmp(fontname, default_fontname)!=0){ DEFont *fnt; LOG(WARN, FONT, TR("Could not load font \"%s\", trying \"%s\""), fontname, default_fontname); fnt=de_load_font(default_fontname); if(fnt==NULL) LOG(WARN, FONT, TR("Failed to load fallback font.")); return fnt; } return NULL; } #endif /* HAVE_X11_BMF */ fnt=ALLOC(DEFont); if(fnt==NULL) return NULL; #ifdef HAVE_X11_XFT fnt->font=font; #endif #ifdef HAVE_X11_BMF fnt->fontset=fontset; fnt->fontstruct=fontstruct; #endif fnt->pattern=scopy(fontname); fnt->next=NULL; fnt->prev=NULL; fnt->refcount=1; LINK_ITEM(fonts, fnt, next, prev); return fnt; }
int main(int argc, char *argv[]) { XEvent e; Atom type; XClassHint *h; XSetWindowAttributes wa; unsigned int desktop; struct pollfd pfd[1]; int nfds; char *fontstr = FONT; int running = 1; d = XOpenDisplay(NULL); if (d == NULL) { fprintf(stderr, "Cannot open display\n"); exit(1); } s = DefaultScreen(d); wa.override_redirect = 1; wa.background_pixmap = ParentRelative; wa.event_mask = ExposureMask | ButtonReleaseMask | ButtonPressMask; w = XCreateWindow(d, RootWindow(d, s), 0, BARPOS, BARWIDTH, BARHEIGHT, 0, DefaultDepth(d, s), CopyFromParent, DefaultVisual(d, s), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); h = XAllocClassHint(); h->res_name = "status"; h->res_class = "status"; XSetClassHint(d, w, h); XFree(h); XStoreName(d, w, "status"); type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", False); XChangeProperty(d, w, XInternAtom(d, "_NET_WM_WINDOW_TYPE", False), XInternAtom(d, "ATOM", False), 32, PropModeReplace, (unsigned char *)&type, 1); type = XInternAtom(d, "_NET_WM_STATE_ABOVE", False); XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", False), XInternAtom(d, "ATOM", False), 32, PropModeReplace, (unsigned char *)&type, 1); type = XInternAtom(d, "_NET_WM_STATE_STICKY", False); XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", False), XInternAtom(d, "ATOM", False), 32, PropModeAppend, (unsigned char *)&type, 1); desktop = 0xffffffff; XChangeProperty(d, w, XInternAtom(d, "_NET_WM_DESKTOP", False), XInternAtom(d, "CARDINAL", False), 32, PropModeReplace, (unsigned char *)&desktop, 1); xftd = XftDrawCreate(d, w, DefaultVisual(d, s), DefaultColormap(d, s)); XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s), "white", &white); XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s), "black", &black); XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s), "red", &red); XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s), "green", &green); xftfont = XftFontOpenXlfd(d, s, fontstr); if (!xftfont) xftfont = XftFontOpenName(d, s, fontstr); if (!xftfont) exit(1); XSelectInput(d, w, ExposureMask | ButtonPressMask | ButtonReleaseMask | Button1MotionMask); XSelectInput(d, RootWindow(d, s), PropertyChangeMask); XMapWindow(d, w); XFlush(d); pfd[0].fd = ConnectionNumber(d); pfd[0].events = POLLIN; while (running) { nfds = poll(pfd, 1, 1000); if (nfds == -1 || (pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL))) break; if (nfds == 0) { redraw(); XFlush(d); continue; } while (XPending(d)) { XNextEvent(d, &e); if (e.type == PropertyNotify && e.xproperty.window == RootWindow(d, s) && e.xproperty.atom == XInternAtom(d, "_NET_CURRENT_DESKTOP", True)) { redraw(); } if (e.type == Expose) { XftDrawRect(xftd, &black, 0, 0, BARWIDTH, BARHEIGHT); redraw(); } if (e.type == ButtonPress) { /*running = 0; break;*/ redraw(); } } } XftColorFree(d, DefaultVisual(d, s), DefaultColormap(d, s), &white); XftColorFree(d, DefaultVisual(d, s), DefaultColormap(d, s), &black); XftFontClose(d, xftfont); XftDrawDestroy(xftd); XDestroyWindow(d, w); XCloseDisplay(d); return 0; }
static void * noseguy_init (Display *d, Window w) { struct state *st = (struct state *) calloc (1, sizeof(*st)); unsigned long fg, bg, text_fg, text_bg; XWindowAttributes xgwa; Colormap cmap; char *fontname; XGCValues gcvalues; st->dpy = d; st->window = w; st->first_time = 1; fontname = get_string_resource (st->dpy, "font", "Font"); XGetWindowAttributes (st->dpy, st->window, &xgwa); st->Width = xgwa.width + 2; st->Height = xgwa.height + 2; cmap = xgwa.colormap; st->tc = textclient_open (st->dpy); { int w = 40; int h = 15; textclient_reshape (st->tc, w, h, w, h, /* Passing MAXLINES isn't actually necessary */ 0); } init_images(st); st->xftfont = XftFontOpenXlfd (st->dpy, screen_number (xgwa.screen), fontname); XftColorAllocName (st->dpy, xgwa.visual, xgwa.colormap, get_string_resource (st->dpy, "textForeground", "Foreground"), &st->xftcolor); st->xftdraw = XftDrawCreate (st->dpy, st->window, xgwa.visual, xgwa.colormap); fg = get_pixel_resource (st->dpy, cmap, "foreground", "Foreground"); bg = get_pixel_resource (st->dpy, cmap, "background", "Background"); text_fg = get_pixel_resource (st->dpy, cmap, "textForeground", "Foreground"); text_bg = get_pixel_resource (st->dpy, cmap, "textBackground", "Background"); /* notice when unspecified */ if (! get_string_resource (st->dpy, "textForeground", "Foreground")) text_fg = bg; if (! get_string_resource (st->dpy, "textBackground", "Background")) text_bg = fg; gcvalues.foreground = fg; gcvalues.background = bg; st->fg_gc = XCreateGC (st->dpy, st->window, GCForeground|GCBackground, &gcvalues); gcvalues.foreground = bg; gcvalues.background = fg; st->bg_gc = XCreateGC (st->dpy, st->window, GCForeground|GCBackground, &gcvalues); gcvalues.foreground = text_fg; gcvalues.background = text_bg; st->text_fg_gc = XCreateGC (st->dpy, st->window, GCForeground|GCBackground, &gcvalues); gcvalues.foreground = text_bg; gcvalues.background = text_fg; st->text_bg_gc = XCreateGC (st->dpy, st->window, GCForeground|GCBackground, &gcvalues); st->x = st->Width / 2; st->y = st->Height / 2; st->state = IS_MOVING; st->next_fn = move; st->walk_up = 1; return st; }