in_memory cairo_ctx for better font rendering when no X available

This commit is contained in:
stefan schippers 2024-03-12 11:48:18 +01:00
parent de4f44fd40
commit 9b8015b437
4 changed files with 37 additions and 42 deletions

View File

@ -3371,21 +3371,31 @@ void new_polygon(int what, double mousex_snap, double mousey_snap)
}
}
#if HAS_CAIRO==1
static int temporary_x_connection = 0;
#endif
void close_temporary_x_connection(void)
/* try to create a cairo context so we get better font metric calculation (text bbox)
* what = 1: create
* what = 0 : clear */
void create_memory_cairo_ctx(int what)
{
#if HAS_CAIRO==1
if(temporary_x_connection) {
cairo_destroy(xctx->cairo_ctx);
cairo_surface_destroy(xctx->cairo_sfc);
XDestroyWindow(display, xctx->window);
XCloseDisplay(display);
display = NULL;
temporary_x_connection = 0;
}
#endif
#if HAS_CAIRO==1
static int created = 0;
enum { w = 100, h = 64, bpp = 4 };
static unsigned char data[w * h * bpp];
if(!created && what && !xctx->cairo_ctx) {
xctx->cairo_sfc = cairo_image_surface_create_for_data(data,
CAIRO_FORMAT_RGB24, w, h, bpp * w);
xctx->cairo_ctx = cairo_create(xctx->cairo_sfc);
if(xctx->cairo_ctx) created = 1;
}
if(created && !what && xctx->cairo_ctx ) {
cairo_destroy(xctx->cairo_ctx);
cairo_surface_destroy(xctx->cairo_sfc);
xctx->cairo_ctx = NULL;
xctx->cairo_sfc = NULL;
created = 0;
}
#endif
}
#if HAS_CAIRO==1
@ -3400,28 +3410,9 @@ int text_bbox(const char *str, double xscale, double yscale,
cairo_font_extents_t fext;
double ww, hh, maxw;
/* try to create a cairo context so we get better font metric calculation (text bbox) */
if(!display) {
int screen;
unsigned long white;
Visual *visual;
if((display = XOpenDisplay(NULL))) {
screen = DefaultScreen(display);
visual = DefaultVisual(display, screen);
white = WhitePixel(display, screen);
xctx->window = XCreateSimpleWindow(display,
DefaultRootWindow(display), 0, 0, 1, 1, CopyFromParent, white, white);
xctx->cairo_sfc = cairo_xlib_surface_create(display, xctx->window, visual, 1, 1);
xctx->cairo_ctx = cairo_create(xctx->cairo_sfc);
temporary_x_connection = 1;
}
}
/* if XOpenDisplay() failed display will be still NULL so we will go with
* text_bbox_nocairo() */
/* will not match exactly font metrics when doing ps/svg output , but better than nothing */
if(!has_x && !display) return text_bbox_nocairo(str, xscale, yscale, rot, flip, hcenter, vcenter, x1, y1,
/* if no cairo_ctx is available use text_bbox_nocairo().
* will not match exactly font metrics when doing ps/svg output, but better than nothing */
if(!has_x && !xctx->cairo_ctx) return text_bbox_nocairo(str, xscale, yscale, rot, flip, hcenter, vcenter, x1, y1,
rx1, ry1, rx2, ry2, cairo_lines, cairo_longest_line);
size = xscale*52.*cairo_font_scale;

View File

@ -958,11 +958,6 @@ void svg_draw(void)
tclsetboolvar("draw_grid", old_grid);
my_free(_ALLOC_ID_, &svg_colors);
my_free(_ALLOC_ID_, &unused_layer);
/* if xschem started with no X connection, text_bbox() will
* try to open an X connection and create a cairo surface
* that will be used to return more precise font metrics for
* text bbox calculation. so we close the temporary connection here. */
close_temporary_x_connection();
Tcl_SetResult(interp,"",TCL_STATIC);
}

View File

@ -925,6 +925,8 @@ static void xwin_exit(void)
for(i = 0; i < cadlayers; ++i) Tk_FreePixmap(display, pixmap[i]);
#endif
my_free(_ALLOC_ID_, &pixmap);
} else { /* no X */
create_memory_cairo_ctx(0); /* clear in-memory created cairo_ctx if any */
}
dbg(1, "xwin_exit(): clearing drawing data structures\n");
@ -2047,6 +2049,10 @@ static void resetcairo(int create, int clear, int force_or_resize)
cairo_surface_destroy(xctx->cairo_save_sfc);
cairo_destroy(xctx->cairo_ctx);
cairo_surface_destroy(xctx->cairo_sfc);
xctx->cairo_save_ctx = NULL;
xctx->cairo_ctx = NULL;
xctx->cairo_save_sfc = NULL;
xctx->cairo_sfc = NULL;
}
if(create && force_or_resize) {
cairo_font_options_t *options;
@ -2719,6 +2725,9 @@ int Tcl_AppInit(Tcl_Interp *inter)
set_snap(0); /* set default value specified in xschemrc as 'snap' else CADSNAP */
set_grid(0); /* set default value specified in xschemrc as 'grid' else CADGRID */
} /* if(has_x) */
else { /* no X */
if(!xctx->cairo_ctx) create_memory_cairo_ctx(1); /* in-memory cairo_ctx for text_bbox when no X is used */
}
dbg(1, "Tcl_AppInit(): done X init\n");
/* pass to tcl values of Alt, Shift, COntrol key masks so bind Alt-KeyPress events will work for windows */

View File

@ -1330,7 +1330,7 @@ extern int text_bbox(const char * str,double xscale, double yscale,
short rot, short flip, int hcenter, int vcenter,
double x1,double y1, double *rx1, double *ry1,
double *rx2, double *ry2, int *cairo_lines, double *longest_line);
extern void close_temporary_x_connection(void);
extern void create_memory_cairo_ctx(int what);
extern int get_color(int value);
extern void incr_hilight_color(void);
extern void get_inst_pin_coord(int i, int j, double *x, double *y);