int calculate_drawing_range(GtLineBreakerCaptions *lbc, GtDrawingRange *rng, GtBlock* block, GtError *err) { double textwidth = 0.0; GtDrawingRange drange; gt_assert(block && lbc); drange = gt_coords_calc_generic_range(gt_block_get_range(block), gt_layout_get_range(lbc->layout)); drange.start *= lbc->width-2*lbc->margins; drange.end *= lbc->width-2*lbc->margins; if (gt_block_get_caption(block)) { textwidth = gt_text_width_calculator_get_text_width( gt_layout_get_twc(lbc->layout), gt_str_get(gt_block_get_caption(block)), err); if (gt_double_smaller_double(textwidth, 0)) return -1; if (gt_double_smaller_double(gt_drawing_range_length(drange), textwidth)) drange.end = drange.start + textwidth; } rng->start = drange.start; rng->end = drange.end; return 0; }
int gt_block_compare(const GtBlock *block1, const GtBlock *block2, GT_UNUSED void *data) { GtRange range_a, range_b; int ret = 0; gt_assert(block1 && block2); range_a = gt_block_get_range(block1), range_b = gt_block_get_range(block2); ret = gt_range_compare(&range_a, &range_b); if (ret == 0 && block1 != block2) { GtStr *caption1, *caption2; caption1 = gt_block_get_caption(block1); caption2 = gt_block_get_caption(block2); /* blocks do not necessarily have captions. If both have a caption, we compare them. If only one block has a caption, this block comes first. */ if (caption1 && caption2) ret = strcmp(gt_str_get(caption1), gt_str_get(caption2)); else if (caption1) ret = -1; else if (caption2) ret = 1; } return ret; }
int gt_block_unit_test(GtError *err) { GtRange r1, r2, r_temp, b_range; GtStrand s; GtGenomeNode *gn1, *gn2; GtElement *e1, *e2; double height; GtBlock *b; GtStr *seqid, *caption1, *caption2; int had_err = 0; GtStyle *sty; GtError *testerr; gt_error_check(err); seqid = gt_str_new_cstr("seqid"); caption1 = gt_str_new_cstr("foo"); caption2 = gt_str_new_cstr("bar"); testerr = gt_error_new(); r1.start = 10UL; r1.end = 50UL; r2.start = 40UL; r2.end = 50UL; gn1 = gt_feature_node_new(seqid, gt_ft_gene, r1.start, r1.end, GT_STRAND_FORWARD); gn2 = gt_feature_node_new(seqid, gt_ft_exon, r2.start, r2.end, GT_STRAND_FORWARD); e1 = gt_element_new((GtFeatureNode*) gn1); e2 = gt_element_new((GtFeatureNode*) gn2); b = gt_block_new(); /* test gt_block_insert_elements */ gt_ensure((0UL == gt_block_get_size(b))); gt_block_insert_element(b, (GtFeatureNode*) gn1); gt_ensure((1UL == gt_block_get_size(b))); gt_block_insert_element(b, (GtFeatureNode*) gn2); gt_ensure((2UL == gt_block_get_size(b))); /* test gt_block_set_range & gt_block_get_range */ r_temp = gt_range_join(&r1, &r2); gt_block_set_range(b, r_temp); b_range = gt_block_get_range(b); gt_ensure((0 == gt_range_compare(&b_range, &r_temp))); gt_ensure((1 == gt_range_compare(&r2, &r_temp))); /* tests gt_block_set_caption & gt_block_get_caption */ gt_block_set_caption(b, caption1); gt_ensure((0 == gt_str_cmp(gt_block_get_caption(b), caption1))); gt_ensure((0 != gt_str_cmp(gt_block_get_caption(b), caption2))); /* tests gt_block_set_strand & gt_block_get_range */ s = gt_block_get_strand(b); gt_ensure((GT_STRAND_UNKNOWN == s)); gt_block_set_strand(b, GT_STRAND_FORWARD); s = gt_block_get_strand(b); gt_ensure((GT_STRAND_FORWARD == s)); /* test gt_block_get_max_height() */ sty = gt_style_new(err); gt_ensure(gt_block_get_max_height(b, &height, sty, err) == 0); gt_ensure(!gt_error_is_set(testerr)); gt_ensure(height == BAR_HEIGHT_DEFAULT); gt_style_set_num(sty, "exon", "bar_height", 42); gt_ensure(gt_block_get_max_height(b, &height, sty, err) == 0); gt_ensure(!gt_error_is_set(testerr)); gt_ensure(height == 42); gt_style_set_num(sty, "gene", "bar_height", 23); gt_ensure(gt_block_get_max_height(b, &height, sty, err) == 0); gt_ensure(!gt_error_is_set(testerr)); gt_ensure(height == 42); gt_style_unset(sty, "exon", "bar_height"); gt_ensure(gt_block_get_max_height(b, &height, sty, err) == 0); gt_ensure(!gt_error_is_set(testerr)); gt_ensure(height == 23); gt_str_delete(caption2); gt_str_delete(seqid); gt_element_delete(e1); gt_element_delete(e2); gt_block_delete(b); gt_style_delete(sty); gt_error_delete(testerr); gt_genome_node_delete(gn1); gt_genome_node_delete(gn2); return had_err; }
int gt_canvas_cairo_visit_block(GtCanvas *canvas, GtBlock *block, GtError *err) { int had_err = 0, arrow_status = ARROW_NONE; GtRange block_range; GtDrawingRange draw_range; GtColor grey, fillcolor, strokecolor; double block_start, block_width, bar_height = BAR_HEIGHT_DEFAULT, min_len_block = MIN_LEN_BLOCK_DEFAULT, arrow_width = ARROW_WIDTH_DEFAULT, stroke_width = STROKE_WIDTH_DEFAULT; const char *caption, *btype; GtStrand strand; double maxbarheight; btype = gt_block_get_type(block); gt_assert(canvas && block); grey.red = grey.green = grey.blue = DEFAULT_GREY_TONE; grey.alpha = 0.5; strand = gt_block_get_strand(block); block_range = gt_block_get_range(block); if (gt_block_get_max_height(block, &maxbarheight, canvas->pvt->sty, err)) { return -1; } if (gt_style_get_num(canvas->pvt->sty, "format", "bar_height", &bar_height, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_style_get_num(canvas->pvt->sty, "format", "min_len_block", &min_len_block,NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_style_get_num(canvas->pvt->sty, "format", "arrow_width", &arrow_width, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_style_get_num(canvas->pvt->sty, "format", "stroke_width", &stroke_width, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_style_get_color_with_track(canvas->pvt->sty, btype, "stroke", &strokecolor, gt_block_get_top_level_feature(block), gt_track_get_title(canvas->pvt->current_track), err) == GT_STYLE_QUERY_ERROR) { return -1; } if (strand == GT_STRAND_REVERSE || strand == GT_STRAND_BOTH) arrow_status = ARROW_LEFT; if (strand == GT_STRAND_FORWARD || strand == GT_STRAND_BOTH) arrow_status = (arrow_status == ARROW_LEFT ? ARROW_BOTH : ARROW_RIGHT); /* calculate scaled drawing coordinates for this canvas */ draw_range = gt_coords_calc_generic_range(block_range, canvas->pvt->viewrange); draw_range.start *= canvas->pvt->width-2*canvas->pvt->margins; draw_range.end *= canvas->pvt->width-2*canvas->pvt->margins; block_start = draw_range.start + canvas->pvt->margins; block_width = draw_range.end - draw_range.start; /* draw block caption */ if (gt_block_caption_is_visible(block)) { caption = gt_str_get(gt_block_get_caption(block)); if (caption) { double theight = gt_graphics_get_text_height(canvas->pvt->g), captionspace = CAPTION_BAR_SPACE_DEFAULT; if (gt_style_get_num(canvas->pvt->sty, "format", "block_caption_space", &captionspace, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_style_get_num(canvas->pvt->sty, "format", "block_caption_font_size", &theight, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } gt_graphics_set_font(canvas->pvt->g, "Sans", SLANT_NORMAL, WEIGHT_NORMAL, theight); gt_graphics_draw_text_clip(canvas->pvt->g, block_start, canvas->pvt->y - bar_height/2 - captionspace, caption); } } /* do not draw further details in very small blocks */ if (!gt_block_has_only_one_fullsize_element(block) && gt_double_smaller_double(block_width, min_len_block)) { /* optimise drawing for very narrow blocks, if <= 1px/pt width, draw as simple lines */ if (canvas->pvt->bt && draw_range.end-draw_range.start <= 1.1) { if (!gt_bittab_bit_is_set(canvas->pvt->bt, (unsigned long) draw_range.start)) { gt_graphics_draw_vertical_line(canvas->pvt->g, block_start, canvas->pvt->y - bar_height/2, strokecolor, bar_height, stroke_width); gt_bittab_set_bit(canvas->pvt->bt, (unsigned long) draw_range.start); } } else { if (gt_style_get_color_with_track(canvas->pvt->sty, btype, "fill", &fillcolor, gt_block_get_top_level_feature(block), gt_track_get_title(canvas->pvt->current_track), err) == GT_STYLE_QUERY_ERROR) { return -1; } gt_graphics_draw_box(canvas->pvt->g, block_start, canvas->pvt->y - bar_height/2, block_width, bar_height, fillcolor, arrow_status, arrow_width, stroke_width, strokecolor, true); /* draw arrowheads at clipped margins */ if (draw_range.clip == CLIPPED_LEFT || draw_range.clip == CLIPPED_BOTH) gt_graphics_draw_arrowhead(canvas->pvt->g, canvas->pvt->margins - 10, canvas->pvt->y - 4, grey, ARROW_LEFT); if (draw_range.clip == CLIPPED_RIGHT || draw_range.clip == CLIPPED_BOTH) gt_graphics_draw_arrowhead(canvas->pvt->g, canvas->pvt->width-canvas->pvt->margins + 10, canvas->pvt->y - 4, grey, ARROW_RIGHT); } /* register coordinates in GtImageInfo object if available */ if (canvas->pvt->ii) { GtRecMap *rm = gt_rec_map_new(block_start, canvas->pvt->y - bar_height/2, block_start + block_width, canvas->pvt->y + bar_height/2, (GtFeatureNode*) /* XXX */ gt_block_get_top_level_feature(block)); gt_image_info_add_rec_map(canvas->pvt->ii, rm); gt_rec_map_set_omitted_children(rm, true); } /* signal break */ return 1; } /* get stroke color */ if (gt_style_get_color(canvas->pvt->sty, "format", "default_stroke_color", &strokecolor, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } /* draw parent block boundaries */ gt_graphics_draw_dashes(canvas->pvt->g, block_start, canvas->pvt->y - bar_height/2, block_width, bar_height, ARROW_NONE, arrow_width, stroke_width, strokecolor); return had_err; }