1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-26 16:32:53 +02:00

sch2fig/text.c (guess_width): move width guessing to the graphics back-ends

This commit is contained in:
Werner Almesberger 2016-07-31 03:28:08 -03:00
parent 6293c1e833
commit 7615675485
5 changed files with 59 additions and 26 deletions

View File

@ -53,6 +53,12 @@ static inline int cx(int x)
} }
static inline int xc(int x)
{
return x * 5;
}
static inline int cy(int y) static inline int cy(int y)
{ {
return y / 5; return y / 5;
@ -142,6 +148,9 @@ void cr_arc(void *ctx, int x, int y, int r, int sa, int ea,
} }
#define TEXT_STRETCH 1.3
void cr_text(void *ctx, int x, int y, const char *s, unsigned size, void cr_text(void *ctx, int x, int y, const char *s, unsigned size,
enum text_align align, int rot, unsigned color, unsigned layer) enum text_align align, int rot, unsigned color, unsigned layer)
{ {
@ -149,7 +158,7 @@ void cr_text(void *ctx, int x, int y, const char *s, unsigned size,
cairo_text_extents_t ext; cairo_text_extents_t ext;
cairo_matrix_t m; cairo_matrix_t m;
cairo_set_font_size(cc->cr, cx(size) * 1.3); cairo_set_font_size(cc->cr, cx(size) * TEXT_STRETCH);
cairo_text_extents(cc->cr, s, &ext); cairo_text_extents(cc->cr, s, &ext);
set_color(cc->cr, color); set_color(cc->cr, color);
@ -177,6 +186,17 @@ void cr_text(void *ctx, int x, int y, const char *s, unsigned size,
} }
unsigned cr_text_width(void *ctx, const char *s, unsigned size)
{
struct cairo_ctx *cc = ctx;
cairo_text_extents_t ext;
cairo_set_font_size(cc->cr, cx(size) * TEXT_STRETCH);
cairo_text_extents(cc->cr, s, &ext);
return xc(ext.width);
}
/* ----- Initializatio and termination ------------------------------------- */ /* ----- Initializatio and termination ------------------------------------- */
@ -213,10 +233,11 @@ void cr_end(void *ctx)
const struct gfx_ops cairo_ops = { const struct gfx_ops cairo_ops = {
// .line = cr_line, @@@ later // .line = cr_line, @@@ later
.poly = cr_poly, .poly = cr_poly,
.circ = cr_circ, .circ = cr_circ,
.arc = cr_arc, .arc = cr_arc,
.text = cr_text, .text = cr_text,
.init = cr_init, .text_width = cr_text_width,
.end = cr_end, .init = cr_init,
.end = cr_end,
}; };

View File

@ -14,6 +14,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <math.h> #include <math.h>
#include <assert.h> #include <assert.h>
@ -161,6 +162,16 @@ void fig_text(void *ctx, int x, int y, const char *s, unsigned size,
} }
unsigned fig_text_width(void *ctx, const char *s, unsigned size)
{
/*
* Note that we stretch the text size, so the ratio is larger than
* expressed here.
*/
return strlen(s) * size * 1.0;
}
/* ----- FIG file header --------------------------------------------------- */ /* ----- FIG file header --------------------------------------------------- */
@ -232,11 +243,12 @@ void *fig_init(const char *template, int n_vars, const char **vars)
const struct gfx_ops fig_ops = { const struct gfx_ops fig_ops = {
.line = fig_line, .line = fig_line,
.rect = fig_rect, .rect = fig_rect,
.poly = fig_poly, .poly = fig_poly,
.circ = fig_circ, .circ = fig_circ,
.arc = fig_arc, .arc = fig_arc,
.text = fig_text, .text = fig_text,
.init = fig_init, .text_width = fig_text_width,
.init = fig_init,
}; };

View File

@ -77,6 +77,12 @@ void gfx_text(int x, int y, const char *s, unsigned size,
} }
unsigned gfx_text_width(const char *s, unsigned size)
{
return gfx_ops->text_width(gfx_ctx, s, size);
}
void gfx_init(const struct gfx_ops *ops, void gfx_init(const struct gfx_ops *ops,
const char *template, int n_vars, const char **vars) const char *template, int n_vars, const char **vars)
{ {

View File

@ -30,6 +30,7 @@ struct gfx_ops {
int color, int fill_color, unsigned layer); int color, int fill_color, unsigned layer);
void (*text)(void *ctx, int x, int y, const char *s, unsigned size, void (*text)(void *ctx, int x, int y, const char *s, unsigned size,
enum text_align align, int rot, unsigned color, unsigned layer); enum text_align align, int rot, unsigned color, unsigned layer);
unsigned (*text_width)(void *ctx, const char *s, unsigned size);
void *(*init)(const char *template, int n_vars, const char **vars); void *(*init)(const char *template, int n_vars, const char **vars);
void (*end)(void *ctx); void (*end)(void *ctx);
}; };
@ -47,6 +48,7 @@ void gfx_arc(int x, int y, int r, int sa, int ea,
int color, int fill_color, unsigned layer); int color, int fill_color, unsigned layer);
void gfx_text(int x, int y, const char *s, unsigned size, void gfx_text(int x, int y, const char *s, unsigned size,
enum text_align align, int rot, unsigned color, unsigned layer); enum text_align align, int rot, unsigned color, unsigned layer);
unsigned gfx_text_width(const char *s, unsigned size);
/* inititalization and termination */ /* inititalization and termination */

View File

@ -115,22 +115,14 @@ void text_fig(const struct text *txt, int color, unsigned layer)
} }
static unsigned guess_width(const struct text *txt)
{
/*
* Note that fig.c stretches the text size, so the ratio is larger than
* expressed here.
*/
return strlen(txt->s) * txt->size * 1.0;
}
void text_rel(const struct text *txt, enum text_align xr, enum text_align yr, void text_rel(const struct text *txt, enum text_align xr, enum text_align yr,
int dx, int dy, int *res_x, int *res_y) int dx, int dy, int *res_x, int *res_y)
{ {
dx -= align(guess_width(txt), txt->hor); int width = gfx_text_width(txt->s, txt->size);
dx -= align(width, txt->hor);
dy += align(txt->size, txt->vert); dy += align(txt->size, txt->vert);
dx += align(guess_width(txt), xr); dx += align(width, xr);
dy -= align(txt->size, yr); dy -= align(txt->size, yr);
if (res_x) if (res_x)
*res_x = txt->x + rx(dx, dy, txt->rot); *res_x = txt->x + rx(dx, dy, txt->rot);