unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Aurélien Aptel" <aurelien.aptel@gmail.com>
To: "Stephen J. Turnbull" <stephen@xemacs.org>
Cc: Eli Zaretskii <eliz@gnu.org>,
	cyd@gnu.org, sdl.web@gmail.com, emacs-devel@gnu.org
Subject: Re: Does face support underline other than a straight line?
Date: Wed, 25 Jan 2012 18:32:52 +0100	[thread overview]
Message-ID: <CA+5B0FMB5OLYKL35gm2x0T8W3MkGNxdX4i78RRAcb8J76R+6_A@mail.gmail.com> (raw)
In-Reply-To: <871uqo3zge.fsf@uwakimon.sk.tsukuba.ac.jp>

[-- Attachment #1: Type: text/plain, Size: 812 bytes --]

On Wed, Jan 25, 2012 at 10:26 AM, Stephen J. Turnbull
<stephen@xemacs.org> wrote:
> In XEmacs you can specify a color or a pixmap as background (bitmaps
> are promoted to pixmaps, but with -- until about a week ago -- less
> than satisfactory results).  So you could specify a background pixmap
> (eg, as an XPM) as
> [...]
> and it will be tiled appropriately over the extent of the face, and
> the text will be printed over that in the foreground color.  Sort of
> like poor man's image layers.

I think it would be simpler to do directly in C. I'm new to this but
it should be simple to add a new field "underwave_p" in struct face (
with underline_p, overline_p) in dispextern.h:1590 and handle it (for
X11) near xterm.c:2756.

See POC program attached for drawing underwaved text in X11.

[-- Attachment #2: underwave.c --]
[-- Type: text/x-csrc, Size: 3681 bytes --]

/* 
   X11 underwaved text example by <aurelien.aptel@gmail.com>
   Usage: gcc -lX11 underwave.c && ./a.out 
   Press ESC to quit, any other key to change the strings
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>

#define RAND(min,max) (rand()%((max)-(min))+(min))

#define FONT_NAME    "-*-fixed-*-*-*-*-13-*-*-*-*-*-iso8859-*"
#define WAVE_COLOR   "red"
#define WAVE_HEIGHT  2
#define WAVE_THICK   1

Window win;
Display* dpy;
GC gc;
int screen;
int width, height;
XFontStruct* font_info;
char* font_name = FONT_NAME;
int font_height;
int seed = 42;
unsigned long undercolor, black;

void init (int w, int h)
{
    XColor color;
    XColorMap cmap;
    unsigned int line_width = WAVE_THICK;
    int line_style = LineSolid;
    int cap_style = CapButt;
    int join_style = JoinBevel;

    width = w, height = h;
    
    dpy = XOpenDisplay(NULL);

    if(!dpy)
        puts("err: XOpenDisplay"), exit(1);

    screen = XDefaultScreen(dpy);
    win = XCreateSimpleWindow(dpy, RootWindow(dpy, screen),
                              100, 100, width, height, 0,
                              BlackPixel(dpy, screen),
                              WhitePixel(dpy, screen));

    cmap = XDefaultColormap(dpy, screen);
    black = BlackPixel(dpy, screen);
    XAllocNamedColor(dpy, cmap, WAVE_COLOR, &color, &color);
    undercolor = color.pixel;
    
    gc = XCreateGC(dpy, win, 0, NULL);
    XSetForeground(dpy, gc, BlackPixel(dpy, screen));
    XSetBackground(dpy, gc, WhitePixel(dpy, screen));
    XSetLineAttributes(dpy, gc, line_width, line_style, cap_style, join_style);

    font_info = XLoadQueryFont(dpy, font_name);
    XSetFont(dpy, gc, font_info->fid);
    font_height = font_info->ascent + font_info->descent;
}

void draw_wave (int x0, int y0, int w)
{
    int dx = WAVE_HEIGHT;
    int dy = dx/2;
    int x1, y1, x2, y2, i, times = w/dx;

    for(i = 0; i < times; i++) {
        x1 = x0 + i*dx;
        y1 = y0 + (i%2 ? 1 : -1) * dy;
        x2 = x0 + (i+1)*dx;
        y2 = y0 + ((i+1)%2 ? 1 : -1) * dy;
        XDrawLine(dpy, win, gc, x1, y1, x2, y2);
    }
}

void draw_underwave_string (char* str, int y)
{
    int len = strlen(str);
    int w = XTextWidth(font_info, str, len);
    int x = (width-w)/2;

    XSetForeground(dpy, gc, black);
    XDrawString(dpy, win, gc, x, y+font_info->ascent, str, len);
    XSetForeground(dpy, gc, undercolor);
    draw_wave(x, y+font_height, w);
}

void draw (void)
{
    char buf[256];
    int i, j, y, n = 20;

    srand(seed);
    XClearWindow(dpy, win);

    for(i = 1; i <= n; i++) {
        y = (height-font_height*1.5*n)/2+font_height*1.5*i;

        /* random ascii string */
        memset(buf, 0, sizeof buf);
        for(j = 0; j < i; j++)
            buf[j] = RAND(32, 126);
        buf[j] = 0;

        draw_underwave_string(buf, y);
    }
}

int main (void)
{
    int done = 0;
    XEvent e;
    int mask = ExposureMask | KeyPressMask | StructureNotifyMask;

    init(300, 300);

    XSelectInput(dpy, win, mask);
    XMapWindow(dpy, win);

    while (!done) {
        XNextEvent(dpy, &e);
        switch (e.type) {
        case Expose:
                draw();
            break;
        case ConfigureNotify:
            width = e.xconfigure.width;
            height = e.xconfigure.height;
            draw();
            break;
        case KeyPress:
            if(XLookupKeysym(&e.xkey, 0) == XK_Escape)
                done = 1;
            else {
                seed = time(NULL)+rand();
                draw();
            }
            break;
        }
    }

    XFreeGC(dpy, gc);
    XCloseDisplay(dpy);

    return 0;
}

  reply	other threads:[~2012-01-25 17:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-24 10:00 Does face support underline other than a straight line? Leo
2012-01-25  6:03 ` Chong Yidong
2012-01-25  6:46   ` Eli Zaretskii
2012-01-25  6:49   ` Stephen J. Turnbull
2012-01-25  7:13     ` Eli Zaretskii
2012-01-25  9:26       ` Stephen J. Turnbull
2012-01-25 17:32         ` Aurélien Aptel [this message]
2012-01-25 17:36           ` Aurélien Aptel
2012-01-25 20:05             ` Aurélien Aptel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+5B0FMB5OLYKL35gm2x0T8W3MkGNxdX4i78RRAcb8J76R+6_A@mail.gmail.com \
    --to=aurelien.aptel@gmail.com \
    --cc=cyd@gnu.org \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=sdl.web@gmail.com \
    --cc=stephen@xemacs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).