2016-08-22 10:03:53 +03:00
|
|
|
/*
|
|
|
|
* kicad/pl-render.c - RenderKiCad page layout
|
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
2016-08-22 11:38:19 +03:00
|
|
|
#define _GNU_SOURCE /* for asprintf */
|
2016-08-22 10:03:53 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2016-08-22 11:38:19 +03:00
|
|
|
#include <stdio.h>
|
2016-08-22 10:03:53 +03:00
|
|
|
#include <string.h>
|
2016-08-22 11:57:27 +03:00
|
|
|
#include <ctype.h>
|
2016-08-22 10:03:53 +03:00
|
|
|
|
|
|
|
#include "misc/util.h"
|
|
|
|
#include "misc/diag.h"
|
|
|
|
#include "gfx/style.h"
|
2016-08-22 10:40:40 +03:00
|
|
|
#include "gfx/text.h"
|
2016-08-22 10:03:53 +03:00
|
|
|
#include "gfx/gfx.h"
|
2016-08-22 11:38:19 +03:00
|
|
|
#include "kicad/sch.h"
|
2016-08-22 10:03:53 +03:00
|
|
|
#include "kicad/pl-common.h"
|
|
|
|
#include "kicad/pl.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Eeschema works in mil
|
|
|
|
* Page layouts are in mm
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static int mil(float mm)
|
|
|
|
{
|
|
|
|
return mm / 25.4 * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 10:40:40 +03:00
|
|
|
static int coord(int v, int d, int o, int e)
|
2016-08-22 10:03:53 +03:00
|
|
|
{
|
2016-08-22 10:40:40 +03:00
|
|
|
return d >= 0 ? o + v : e - v;
|
2016-08-22 10:03:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 11:38:19 +03:00
|
|
|
static char *expand(const struct pl_ctx *pl, const char *s,
|
|
|
|
const struct sheet *sheets, const struct sheet *sheet)
|
|
|
|
{
|
|
|
|
const struct sheet *sch;
|
|
|
|
char *res = NULL;
|
|
|
|
const char *p;
|
|
|
|
unsigned size = 0;
|
|
|
|
unsigned len;
|
|
|
|
char *x;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
p = strchr(s, '%');
|
|
|
|
if (!p)
|
|
|
|
break;
|
|
|
|
switch (p[1]) {
|
|
|
|
case '%':
|
|
|
|
x = "%";
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
x = "%C"; // comment #n
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
x = "%D"; // date
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
x = "%F"; // file name
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
x = "%K"; // KiCad version
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
n = 0;
|
|
|
|
for (sch = sheets; sch; sch = sch->next)
|
|
|
|
n++;
|
|
|
|
if (asprintf(&x, "%u", n)) {}
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
x = "%P"; // sheet path
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
x = "%R"; // revision
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
n = 1;
|
|
|
|
for (sch = sheets; sch != sheet;
|
|
|
|
sch = sch->next)
|
|
|
|
n++;
|
|
|
|
if (asprintf(&x, "%u", n)) {}
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
x = (char *) sheet->title;
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
x = "%Y"; // company name
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
x = "%Z"; // paper format
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
x = "???";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len = strlen(x);
|
2016-08-23 03:00:10 +03:00
|
|
|
res = realloc_size(res, size + p - s + len);
|
2016-08-22 11:38:19 +03:00
|
|
|
memcpy(res + size, s, p - s);
|
|
|
|
size += p - s;
|
|
|
|
s = p + 2;
|
|
|
|
memcpy(res + size, x, len);
|
|
|
|
size += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(s);
|
2016-08-23 03:00:10 +03:00
|
|
|
res = realloc_size(res, size + len + 1);
|
2016-08-22 11:38:19 +03:00
|
|
|
memcpy(res + size, s, len + 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 11:57:27 +03:00
|
|
|
static char *increment(char *s, int inc, const char *range)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
unsigned len = strlen(s);
|
|
|
|
int base, n;
|
|
|
|
|
2016-08-23 03:00:10 +03:00
|
|
|
t = realloc_size(s, len + 2);
|
2016-08-22 11:57:27 +03:00
|
|
|
t[len + 1] = 0;
|
|
|
|
|
|
|
|
base = range[1] - range[0] + 1;
|
|
|
|
n = t[len - 1] - range[0] + inc;
|
|
|
|
t[len - 1] = n / base + range[0];
|
|
|
|
t[len] = n % base + range[0];
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 11:00:38 +03:00
|
|
|
static void render_text(const struct pl_ctx *pl, const struct pl_obj *obj,
|
2016-08-22 11:57:27 +03:00
|
|
|
int x, int y, int inc,
|
|
|
|
const struct sheet *sheets, const struct sheet *sheet)
|
2016-08-22 10:52:18 +03:00
|
|
|
{
|
2016-08-22 11:57:27 +03:00
|
|
|
char *s = expand(pl, obj->s, sheets, sheet);
|
2016-08-22 10:52:18 +03:00
|
|
|
struct text txt = {
|
2016-08-22 11:00:38 +03:00
|
|
|
.size = mil(obj->ey ? obj->ey : pl->ty),
|
2016-08-22 10:52:18 +03:00
|
|
|
.x = x,
|
|
|
|
.y = y,
|
|
|
|
.rot = 0,
|
|
|
|
.hor = obj->hor,
|
|
|
|
.vert = obj->vert,
|
|
|
|
};
|
|
|
|
|
2016-08-22 11:57:27 +03:00
|
|
|
if (inc && *s) {
|
|
|
|
char *end = strchr(s, 0) - 1;
|
|
|
|
const char *range = NULL;
|
|
|
|
|
|
|
|
if (isdigit(*end))
|
|
|
|
range = "09";
|
|
|
|
else if (isupper(*end))
|
|
|
|
range = "AZ";
|
|
|
|
else if (islower(*end))
|
|
|
|
range = "az";
|
|
|
|
if (range) {
|
|
|
|
if (*end + inc <= range[1])
|
|
|
|
*end += inc;
|
|
|
|
else
|
|
|
|
s = increment(s, inc, range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txt.s = s;
|
2016-08-22 10:52:18 +03:00
|
|
|
text_fig(&txt, COLOR_COMP_DWG, LAYER_COMP_DWG);
|
2016-08-22 11:57:27 +03:00
|
|
|
free(s);
|
2016-08-22 10:52:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 10:03:53 +03:00
|
|
|
static void render_obj(const struct pl_ctx *pl, const struct pl_obj *obj,
|
2016-08-22 11:57:27 +03:00
|
|
|
unsigned inc,
|
2016-08-22 11:38:19 +03:00
|
|
|
const struct sheet *sheets, const struct sheet *sheet)
|
2016-08-22 10:03:53 +03:00
|
|
|
{
|
2016-08-22 11:38:19 +03:00
|
|
|
int w = sheet->w;
|
|
|
|
int h = sheet->h;
|
2016-08-22 10:03:53 +03:00
|
|
|
int xo = mil(pl->l);
|
|
|
|
int yo = mil(pl->r);
|
|
|
|
int xe = w - mil(pl->t);
|
|
|
|
int ye = h - mil(pl->b);
|
2016-08-22 11:57:27 +03:00
|
|
|
int x = mil(obj->x + inc * obj->incrx);
|
|
|
|
int y = mil(obj->y + inc * obj->incry);
|
|
|
|
int ex = mil(obj->ex + inc * obj->incrx);
|
|
|
|
int ey = mil(obj->ey + inc * obj->incry);
|
2016-08-22 10:03:53 +03:00
|
|
|
int ww = xe - xo;
|
|
|
|
int hh = ye - yo;
|
|
|
|
|
|
|
|
if (x < 0 || y < 0 || ex < 0 || ey < 0)
|
|
|
|
return;
|
|
|
|
if (x > ww || y > hh || ex > ww || ey > hh)
|
|
|
|
return;
|
|
|
|
|
2016-08-22 10:40:40 +03:00
|
|
|
x = coord(x, obj->dx, xo, xe);
|
|
|
|
y = coord(y, obj->dy, yo, ye);
|
|
|
|
ex = coord(ex, obj->edx, xo, xe);
|
|
|
|
ey = coord(ey, obj->edy, yo, ye);
|
|
|
|
|
2016-08-22 10:03:53 +03:00
|
|
|
switch (obj->type) {
|
|
|
|
case pl_obj_rect:
|
2016-08-22 10:40:40 +03:00
|
|
|
gfx_rect(x, y, ex, ey,
|
2016-08-22 10:03:53 +03:00
|
|
|
COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG);
|
|
|
|
break;
|
|
|
|
case pl_obj_line: {
|
2016-08-22 10:40:40 +03:00
|
|
|
int vx[] = { x, ex };
|
|
|
|
int vy[] = { y, ey };
|
2016-08-22 10:03:53 +03:00
|
|
|
|
|
|
|
gfx_poly(2, vx, vy,
|
|
|
|
COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG);
|
|
|
|
}
|
|
|
|
break;
|
2016-08-22 10:40:40 +03:00
|
|
|
case pl_obj_text:
|
2016-08-22 11:57:27 +03:00
|
|
|
render_text(pl, obj, x, y, inc, sheets, sheet);
|
2016-08-22 10:40:40 +03:00
|
|
|
break;
|
2016-08-22 10:03:53 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 11:38:19 +03:00
|
|
|
void pl_render(struct pl_ctx *pl, const struct sheet *sheets,
|
|
|
|
const struct sheet *sheet)
|
2016-08-22 10:03:53 +03:00
|
|
|
{
|
|
|
|
const struct pl_obj *obj;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (obj = pl->objs; obj; obj = obj->next)
|
|
|
|
for (i = 0; i != obj->repeat; i++)
|
2016-08-22 11:38:19 +03:00
|
|
|
render_obj(pl, obj, i, sheets, sheet);
|
2016-08-22 10:03:53 +03:00
|
|
|
}
|