+#include<cairo/cairo.h>
#include"ses_core.h"
+static int _simplify_draw(ScfEfunction* f, uint32_t bx, uint32_t by, uint32_t bw, uint32_t bh)
+{
+ ScfEcomponent* B;
+ ScfEcomponent* c;
+ ScfEline* el;
+ ScfEpin* p;
+ ScfEpin* pb;
+ ScfEpin* pc;
+ ScfEpin* pe;
+ ScfLine* l;
+
+ cairo_surface_t* surface;
+ cairo_t* cr;
+
+ surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, bx + bw, by + bh);
+ cr = cairo_create (surface);
+
+ cairo_set_line_width(cr, 2);
+ cairo_set_source_rgb(cr, 1, 1, 1);
+ cairo_rectangle (cr, 0, 0, bx + bw, by + bh);
+ cairo_fill(cr);
+ cairo_stroke(cr);
+
+ size_t i;
+ size_t j;
+ size_t k;
+
+ B = f->components[0];
+
+ for (j = 0; j < f->n_elines; j++) {
+ el = f->elines[j];
+
+ if (el->v == B->pins[SCF_EDA_Battery_POS]->v)
+ cairo_set_source_rgb(cr, 1, 0, 0);
+
+ else if (el->v == B->pins[SCF_EDA_Battery_NEG]->v)
+ cairo_set_source_rgb(cr, 0, 0, 1);
+
+ else if (SCF_EDA_PIN_IN & el->flags)
+ cairo_set_source_rgb(cr, 0, 1, 0);
+
+ else if (SCF_EDA_PIN_OUT & el->flags)
+ cairo_set_source_rgb(cr, 1, 0, 1);
+ else
+ cairo_set_source_rgb(cr, 1, 0.5, 0.1);
+
+
+ for (k = 0; k < el->n_lines; k++) {
+ l = el->lines[k];
+
+ cairo_move_to(cr, l->x0, l->y0);
+ cairo_line_to(cr, l->x1, l->y1);
+ }
+
+ uint8_t text[64];
+ snprintf(text, sizeof(text) - 1, "%ld", el->id);
+
+ cairo_select_font_face(cr, "Georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
+ cairo_set_font_size (cr, 24);
+ cairo_move_to (cr, l->x0 - 20, l->y0 + 5);
+ cairo_show_text (cr, text);
+
+ cairo_stroke(cr);
+ }
+
+ for (j = 0; j < f->n_components; j++) {
+ c = f->components[j];
+
+ if ((SCF_EDA_Diode == c->type || SCF_EDA_Transistor == c->type)
+ && SCF_EDA_Status_OFF == c->status)
+ continue;
+
+ uint8_t text[64];
+ snprintf(text, sizeof(text) - 1, "%ld", c->id);
+
+ cairo_set_source_rgb (cr, 0, 0, 0);
+ cairo_select_font_face(cr, "Georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
+ cairo_set_font_size (cr, 20);
+ cairo_move_to (cr, c->x + 5, c->y - 5);
+ cairo_show_text (cr, text);
+ cairo_stroke(cr);
+
+ cairo_set_source_rgb(cr, 0.6, 0.6, 0.0);
+
+ int dx0;
+ int dy0;
+
+ int dx1;
+ int dy1;
+ int dx2;
+ int dy2;
+
+ int dx3;
+ int dy3;
+ int dx4;
+ int dy4;
+
+ switch (c->type) {
+
+ case SCF_EDA_Battery:
+ p = c->pins[SCF_EDA_Battery_POS];
+
+ if (p->y < c->y) {
+ cairo_move_to(cr, c->x - 8, c->y - 5);
+ cairo_line_to(cr, c->x + 8, c->y - 5);
+
+ cairo_move_to(cr, c->x, c->y - 5);
+ cairo_line_to(cr, p->x, p->y);
+
+ cairo_move_to(cr, c->x - 5, c->y + 5);
+ cairo_line_to(cr, c->x + 5, c->y + 5);
+
+ p = c->pins[SCF_EDA_Battery_NEG];
+ cairo_move_to(cr, c->x, c->y + 5);
+ cairo_line_to(cr, p->x, p->y);
+
+ } else {
+ cairo_move_to(cr, c->x - 8, c->y + 5);
+ cairo_line_to(cr, c->x + 8, c->y + 5);
+
+ cairo_move_to(cr, c->x, c->y + 5);
+ cairo_line_to(cr, p->x, p->y);
+
+ cairo_move_to(cr, c->x - 5, c->y - 5);
+ cairo_line_to(cr, c->x + 5, c->y - 5);
+
+ p = c->pins[SCF_EDA_Battery_NEG];
+ cairo_move_to(cr, c->x, c->y - 5);
+ cairo_line_to(cr, p->x, p->y);
+ }
+
+ break;
+
+ case SCF_EDA_Resistor:
+ p = c->pins[0];
+
+ vertical(&dx0, &dy0, c->x - p->x, c->y - p->y, 3);
+ forward (&dx1, &dy1, c->x - p->x, c->y - p->y, 6);
+
+ cairo_move_to (cr, p->x, p->y);
+ cairo_line_to (cr, c->x - dx1, c->y - dy1);
+
+ cairo_rel_move_to(cr, dx0, dy0);
+ cairo_rel_line_to(cr, -dx0 * 2, -dy0 * 2);
+ cairo_rel_line_to(cr, dx1 * 2, dy1 * 2);
+ cairo_rel_line_to(cr, dx0 * 2, dy0 * 2);
+ cairo_rel_line_to(cr, -dx1 * 2, -dy1 * 2);
+
+ p = c->pins[1];
+ cairo_move_to (cr, p->x, p->y);
+ cairo_line_to (cr, c->x + dx1, c->y + dy1);
+ break;
+
+ case SCF_EDA_Diode:
+ p = c->pins[SCF_EDA_Diode_POS];
+
+ vertical(&dx0, &dy0, c->x - p->x, c->y - p->y, 6);
+ forward (&dx1, &dy1, c->x - p->x, c->y - p->y, 6);
+
+ cairo_move_to(cr, p->x, p->y);
+ cairo_line_to(cr, c->x - dx1, c->y - dy1);
+
+ cairo_rel_move_to(cr, dx0, dy0);
+ cairo_rel_line_to(cr, -dx0 * 2, -dy0 * 2);
+ cairo_line_to (cr, c->x + dx1, c->y + dy1);
+ cairo_line_to (cr, c->x + dx0 - dx1, c->y + dy0 - dy1);
+
+ p = c->pins[SCF_EDA_Diode_NEG];
+
+ cairo_move_to (cr, p->x, p->y);
+ cairo_line_to (cr, c->x + dx1, c->y + dy1);
+ cairo_rel_move_to(cr, dx0, dy0);
+ cairo_rel_line_to(cr, -dx0 * 2, -dy0 * 2);
+ break;
+
+ case SCF_EDA_Transistor:
+ pb = c->pins[SCF_EDA_Transistor_B];
+ pc = c->pins[SCF_EDA_Transistor_C];
+ pe = c->pins[SCF_EDA_Transistor_E];
+
+ vertical(&dx0, &dy0, c->x - pb->x, c->y - pb->y, 8);
+ forward (&dx3, &dy3, c->x - pb->x, c->y - pb->y, 8);
+
+ cairo_arc(cr, c->x - dx3 / 2, c->y - dy3 / 2, 12, 0, 2 * M_PI);
+
+ cairo_move_to (cr, pb->x, pb->y);
+ cairo_line_to (cr, c->x - dx3, c->y - dy3);
+ cairo_rel_move_to(cr, dx0, dy0);
+ cairo_rel_line_to(cr, -dx0 * 2, -dy0 * 2);
+
+ cairo_move_to(cr, c->x - dx3, c->y - dy3);
+ cairo_line_to(cr, c->x + dx3 + dx0, c->y + dy3 + dy0);
+ cairo_line_to(cr, pe->x, pe->y);
+
+ vertical(&dx1, &dy1, dx3 * 2 + dx0, dy3 * 2 + dy0, 3);
+ forward (&dx4, &dy4, dx3 * 2 + dx0, dy3 * 2 + dy0, 8);
+
+ cairo_move_to(cr, c->x - dx3 + dx4 + dx1, c->y - dy3 + dy4 + dy1);
+ cairo_line_to(cr, c->x + dx3 + dx0, c->y + dy3 + dy0);
+ cairo_line_to(cr, c->x - dx3 + dx4 - dx1, c->y - dy3 + dy4 - dy1);
+
+ cairo_move_to(cr, c->x - dx3, c->y - dy3);
+ cairo_line_to(cr, c->x + dx3 - dx0, c->y + dy3 - dy0);
+ cairo_line_to(cr, pc->x, pc->y);
+ break;
+ default:
+ break;
+ };
+ cairo_stroke(cr);
+ }
+
+ cairo_surface_write_to_png(surface, "./simplify.png");
+
+ cairo_destroy(cr);
+ cairo_surface_destroy(surface);
+ return 0;
+}
+
+static int _simplify_handler(ScfEfunction* f, int64_t ns, int64_t count)
+{
+ ScfEcomponent* c;
+ ScfEcomponent* B;
+ ScfEline* el;
+ ScfEpin* pb;
+ ScfEpin* pc;
+ ScfEpin* pe;
+
+ size_t i;
+ size_t j;
+ size_t k;
+
+ _simplify_draw(f, f->x, f->y, f->w, f->h);
+
+ return 0;
+}
-ses_step_t ses_step_simplify =
+ses_step_t ses_step_simplify =
{
- .name = "simplify",
+ .name = "simplify",
+ .handler = _simplify_handler,
};