Example #1
0
string Bubble::drawTextLine(int x0, int y0, string text, int maxwidth, float rel_height, bool centered) const {
    string prefix = "", oldprefix = "";
    int height, width = 0;
    while (width < maxwidth && oldprefix != text) {
	oldprefix = prefix;
	// Find the next word boundary
	size_t len = prefix.length();
	size_t word_idx = text.find(" ", len+1);

	if (word_idx != string::npos) {
	    prefix = text.substr(0, word_idx);
	} else {
	    prefix = text;
	}
	imlib_get_text_size(prefix.c_str(), &width, &height);
    }

    imlib_get_text_size(oldprefix.c_str(), &width, &height);
    int xpos = x0 + 2;
    if (centered) xpos += (maxwidth-width)/2;
    int ypos = y0 - height/2 - static_cast<int>(round(1.1*rel_height*(height/2.0)));
    imlib_text_draw(xpos, ypos, replace_all(oldprefix, "~", " ").c_str());
    //imlib_image_draw_line(x0, y0, x0+maxwidth, y0, 0);
    if (oldprefix == text) return "";
    else                   return text.substr(oldprefix.length()+1);
}
Example #2
0
static void draw_text(Imlib_Font font, uint align, int ox, int width, 
		int offx, int offy, const char *text, struct color *c)
{
	if (!font)
		return;

	imlib_context_set_image(bb);
	imlib_context_set_font(font);
	imlib_context_set_color(c->r, c->g, c->b, 255);
	int texth, textw, oy;
	imlib_get_text_size(text, &textw, &texth);
	oy = (theme->height - texth) / 2;
	switch (align) {
	case ALIGN_LEFT:
		break;
	case ALIGN_CENTER:
		ox += (width - textw) / 2;
		break;
	case ALIGN_RIGHT:
		ox = width - textw;
		break;
	}

	ox += offx;
	oy += offy;
	
	imlib_text_draw(ox, oy, text);
}
Example #3
0
void BubbleEllipse::writeImage() const {
    int fontsize = static_cast<int>(font->size);//todo
    bgcolor->use();
    imlib_image_fill_ellipse(centerx, centery, radiusx-1, radiusy-1);

    // Wähle die Höhe der 1. Zeile so, dass das 1. Wort gerade hineinpasst.
    int width, height;
    unsigned first_idx     = text.find(' ');
    string first_word = (first_idx != string::npos) ? text.substr(0,first_idx) : text;
    font->use();
    imlib_get_text_size(first_word.c_str(), &width, &height);
    height = -radiusy;
    do {
	height++;
	//printf("%s of width %d does not fit into width %d, increase h to %d\n",
	//first_word.c_str(), width, ellipseWidth(radiusx, radiusy, abs(height)), height);
    } while (ellipseWidth(radiusx, radiusy, abs(height)) < width && height <= 0);
    height += fontsize/3;

    // Schreibe den Text nacheinander in die Zeilen.
    string rest  = text;
    while(rest.length() > 0) {
	if (height > radiusy-2) {
	    std::cerr<< "Error: text does not fit, aborting: "<< text <<".\n";
	    break;
	}
	width = ellipseWidth(radiusx, radiusy, abs(height));
	rest = drawTextLine(centerx-width/2, centery+height, rest, width, height/(float)radiusx);
	height += fontsize*1.7;
    }
}
Example #4
0
File: tag.c Project: decasm/sxiv
void tag_init(tag_t *tag, win_t *win) {
	palette_t * p_counter;
	int text_w;
	char * text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	if ( tag->palette == NULL )
		tag->palette = load_palettes("tag.palette.yml");

	tag->win = win;
	tag->tagging_on = true;
	tag->palette_count = 1;
	tag->current_palette = -1;

	p_counter = tag->palette;
	while (p_counter->next != NULL) {
		tag->palette_count++;
		p_counter = p_counter->next;
	}

	set_font();
	set_label_normal();

	/* determine height of text given font size */
	imlib_get_text_size(text, &text_w, &text_height);
	button_height = text_height + pad_height;
}
Example #5
0
File: tag.c Project: decasm/sxiv
void render_text(char * text, int button_x, int button_y, int * button_w, int * button_h) {
	Imlib_Image buffer;
	int text_w, text_h;

	/* query the size it will be */
	imlib_get_text_size(text, &text_w, &text_h); 
	*button_w = text_w + pad_width;
	*button_h = text_height + pad_height;

	/* create our buffer image for rendering this update */
	buffer = imlib_create_image(*button_w, *button_h);
	if ( buffer == NULL ) {
		fprintf(stderr, "Null buffer First\n");
		return;
	}
	imlib_context_set_image(buffer);

	imlib_context_set_color(tag_color.red,tag_color.green,tag_color.blue,tag_color.alpha);
	imlib_image_fill_rectangle(0,0, *button_w, *button_h);

	imlib_context_set_color(255, 255, 255, 255);
	imlib_text_draw(pad_width/2, pad_height/2, text); 

	imlib_render_image_on_drawable(button_x, button_y);

	imlib_free_image();
}
Example #6
0
static void get_text_dimensions(Imlib_Font font, const char *text, int *w, int *h)
{
	if (!font) {
		if (w) *w = 0;
		if (h) *h = 0;
		return;
	}
		
	imlib_context_set_font(font);
	imlib_get_text_size(text, w, h);
}
Example #7
0
void
Rterm_draw_screen_string_imlib2(struct R_termscreen *screen, int row, int column, int len)
{
    R_text_t *text;
    R_textflags_t *textflags;
    int bufferrow;
    int bgcolor, fgcolor;
    int textw, texth;
    struct imlib2color *color;
    Imlib_Image *textimg;

    if (screen == NULL) {

	return;
    }
    text = screen->textbuf.data[screen->viewrow + row] + column;
    textflags = screen->textbuf.renddata[screen->viewrow + row] + column;
    memcpy(screen->stringbuffer, text, len);
    screen->stringbuffer[len] = '\0';
    fprintf(stderr, "string: %s\n", screen->stringbuffer);
    fgcolor = RTERM_CHAR_FG_COLOR(*textflags);
    if (fgcolor) {
	color = &screen->im2colors[fgcolor];
    } else {
	color = &screen->im2colors[RTERM_SCREEN_FOREGROUND];
    }
#if 0
    fprintf(stderr, "red == %d, green == %d, blue == %d, alpha == %d\n",
	    color->red, color->green, color->blue, color->alpha);
#endif
    imlib_context_set_font(screen->im2font);
    imlib_context_set_color(color->red, color->green, color->blue,
			    color->alpha);
    imlib_context_set_drawable(screen->buf);
    imlib_get_text_size(screen->stringbuffer,
                        &textw, &texth);
    textimg = imlib_create_image(textw, texth);
    imlib_context_set_image(textimg);
    imlib_image_clear();
    imlib_image_set_has_alpha(1);
    imlib_context_set_blend(0);
    imlib_context_set_anti_alias(0);
    imlib_text_draw(0,
                    0,
		    screen->stringbuffer);
    imlib_render_image_on_drawable(Rterm_screen_column_x(screen, column),
                                   Rterm_screen_row_y(screen, column));
    imlib_free_image();

    return;
}
Example #8
0
void
gib_imlib_get_text_size(Imlib_Font fn, char *text, gib_style * s, int *w,
                        int *h, Imlib_Text_Direction dir)
{

   imlib_context_set_font(fn);
   imlib_context_set_direction(dir);
   imlib_get_text_size(text, w, h);
   if (s)
   {
      gib_style_bit *b;
      int max_x_off = 0, min_x_off = 0, max_y_off = 0, min_y_off = 0;
      gib_list *l;

      l = s->bits;
      while (l)
      {
         b = (gib_style_bit *) l->data;
         if (b)
         {
            if (b->x_offset > max_x_off)
               max_x_off = b->x_offset;
            else if (b->x_offset < min_x_off)
               min_x_off = b->x_offset;
            if (b->y_offset > max_y_off)
               max_y_off = b->y_offset;
            else if (b->y_offset < min_y_off)
               min_y_off = b->y_offset;
         }
         l = l->next;
      }
      if (h)
      {
         *h += max_y_off;
         *h -= min_y_off;
      }
      if (w)
      {
         *w += max_x_off;
         *w -= min_x_off;
      }
   }
}
Example #9
0
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;
}
Example #10
0
void
menu_draw_string_imlib2(Rwindow_t *window)
{
    int textw, texth;
    unsigned int winw, winh;
    unsigned int dummyui;
    int dummyi;
    size_t len;
    char *str;
    Window dummywin;
    Imlib_Image textimg;
    char blend;

    if (window->im2title) {
        XGetGeometry(window->app->display, window->id, &dummywin,
                     &dummyi, &dummyi, &winw, &winh, &dummyui, &dummyui);
        imlib_context_set_image(window->im2title);
        imlib_context_set_drawable(window->id);
        blend = imlib_context_get_blend();
        imlib_context_set_blend(1);
        imlib_render_image_on_drawable(window->menu->lefttextoffset,
                                       (winh - window->im2titleh) / 2);
        if (window->menuitem) {
            textimg = NULL;
            if (window->menuitem->type == MENUITEM_TOGGLE) {
                if (!window->menuitem->im2true) {
                    imlib_context_set_font(window->im2font);
                    imlib_get_text_size(window->menuitem->truetext,
                                        &textw, &texth);
                    textimg = imlib_create_image(textw, texth);
                    if (textimg) {
                        imlib_context_set_image(textimg);
                        imlib_image_set_has_alpha(1);
                        imlib_image_clear();
                        imlib_context_set_color(204, 181, 59, 255);
                        imlib_text_draw(0, 0, window->menuitem->truetext);
                        window->menuitem->im2true = textimg;
                        window->menuitem->im2truew = textw;
                        window->menuitem->im2trueh = texth;
                    }
                }
                if (!window->menuitem->im2false) {
                    imlib_context_set_font(window->im2font);
                    imlib_get_text_size(window->menuitem->falsetext,
                                        &textw, &texth);
                    textimg = imlib_create_image(textw, texth);
                    if (textimg) {
                        imlib_context_set_image(textimg);
                        imlib_image_set_has_alpha(1);
                        imlib_image_clear();
                        imlib_context_set_color(204, 181, 59, 255);
                        imlib_text_draw(0, 0, window->menuitem->falsetext);
                        window->menuitem->im2false = textimg;
                        window->menuitem->im2falsew = textw;
                        window->menuitem->im2falseh = texth;
                    }
                }
                if (*((unsigned long *)window->menuitem->option.valueptr)
                    & window->menuitem->option.flag) {
                    str = window->menuitem->truetext;
                    textimg = window->menuitem->im2true;
                    textw = window->menuitem->im2truew;
                    texth = window->menuitem->im2trueh;
                } else {
                    str = window->menuitem->falsetext;
                    textimg = window->menuitem->im2false;
                    textw = window->menuitem->im2falsew;
                    texth = window->menuitem->im2falseh;
                }
            } else if (window->menuitem->righttext) {
                if (!window->menuitem->im2right) {
                    imlib_context_set_font(window->im2font);
                    imlib_get_text_size(window->menuitem->righttext,
                                        &textw, &texth);
                    textimg = imlib_create_image(textw, texth);
                    if (textimg) {
                        imlib_context_set_image(textimg);
                        imlib_image_set_has_alpha(1);
                        imlib_image_clear();
                        imlib_context_set_color(204, 181, 59, 255);
                        imlib_text_draw(0, 0, window->menuitem->righttext);
                        window->menuitem->im2right = textimg;
                        window->menuitem->im2rightw = textw;
                        window->menuitem->im2righth = texth;
                    }
                }
                str = window->menuitem->righttext;
                textimg = window->menuitem->im2right;
                textw = window->menuitem->im2rightw;
                texth = window->menuitem->im2righth;
            }
            if (textimg) {
                imlib_context_set_image(textimg);
                imlib_context_set_drawable(window->id);
                imlib_render_image_on_drawable(winw - window->menu->righttextoffset - textw,
                                               (winh - texth) / 2);
            } else {
#if 0
                len = strlen(str);
                if (len) {
                    XDrawString(window->app->display, window->id, window->gc,
                                winw - window->menu->righttextoffset
                                - len * FONT_WIDTH(window->fontinfo),
                                (winh + FONT_HEIGHT(window->fontinfo)) / 2,
                                str, len);
                }
#endif
            }
        }
        imlib_context_set_blend(blend);
    } else {
        menu_draw_string(window);
    }

    return;
}