From 171979a693502bc9d0b22fbac7cd465c65e7a5f3 Mon Sep 17 00:00:00 2001 From: Karryanna Date: Wed, 10 Jun 2015 11:23:06 +0200 Subject: [PATCH] Labelling: Support for rotated labels This forced some changes in segment initialization which are not nice and might deserve some rethinking in the future. --- labeller.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/labeller.c b/labeller.c index da3bb52..a747552 100644 --- a/labeller.c +++ b/labeller.c @@ -281,14 +281,45 @@ void make_bitmap_point(struct variant *v, struct sym_point *sp) void make_bitmap_label(struct variant *v, struct sym_text *text) { - v->width = ceil(text->tw); - v->height = ceil(text->th); + int tw = ceil(text->tw); + int th = ceil(text->th); + + double rotate_rad = text->rotate / (-180 / M_PI); + + // Initially, ll = [0; 0], lr = [tw, 0], ul = [0, th], ur = [tw, th] + // They could be declared before but should not be initialized in code + // as reassigning x-coordinate affects computation of y-cordinate + int llx = 0; + int lly = 0; + + int lrx = tw * cos(rotate_rad); + int lry = tw * sin(rotate_rad); + + int ulx = th * sin(rotate_rad); + int uly = th * cos(rotate_rad); + + int urx = tw * cos(rotate_rad) + th * sin(rotate_rad); + int ury = tw * sin(rotate_rad) + th * cos(rotate_rad); + + int min_x = min4(llx, lrx, ulx, urx); + int min_y = min4(lly, lry, uly, ury); + int max_x = max4(llx, lrx, ulx, urx); + int max_y = max4(lly, lry, uly, ury); + + v->width = max_x - min_x + 1; + v->height = max_y - min_y + 1; v->bitmap = malloc(v->width * v->height * sizeof(bool)); - for (int i=0; iheight; i++) - for (int j=0; jwidth; j++) + memset(v->bitmap, 0, v->width * v->height * sizeof(bool)); + + for (int i=0; ibitmap[i*v->width + j] = 1; + int nx = j*cos(rotate_rad) + i*sin(rotate_rad); + int ny = j*sin(rotate_rad) + i*cos(rotate_rad); + v->bitmap[(ny-min_y)*v->width + (nx-min_x)] = 1; } + } } void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex) @@ -715,8 +746,11 @@ struct request_segment *make_new_segment(struct request_section *rls, struct sym rs->request.type = REQUEST_SEGMENT; GARY_INIT(rs->request.variants, 0); - struct variant *v = GARY_PUSH(rs->request.variants); - make_bitmap(v, sym); + if (sym) + { + struct variant *v = GARY_PUSH(rs->request.variants); + make_bitmap(v, sym); + } return rs; } @@ -825,7 +859,7 @@ void make_segments(void) cut_edge(e, max2(conf_max_section_length - cur_length, 2)); } - rs = make_new_segment(rls, e->label); + rs = make_new_segment(rls, NULL); rs->label = malloc(sizeof(struct sym_text)); *((struct sym_text *) rs->label) = *((struct sym_text *) e->label); @@ -835,6 +869,11 @@ void make_segments(void) rs->y2 = e->n2->o->y; rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1); + ((struct sym_text *) rs->label)->rotate = atan(rs->slope) * (-180 / M_PI); + struct variant *v = GARY_PUSH(rs->request.variants); + printf("Request %d\n", rs->request.ind); + make_bitmap(v, rs->label); + rs->zindex = e->zindex; cur_length += e->length; -- 2.39.2