int do_scalebar(void) { double scale_size; double length, width; double x, x1, x2, y1, y2, y3; int seg, i, j, lab; int margin; char num[50]; /* get scale size */ scale_size = METERS_TO_INCHES * distance(PS.w.east, PS.w.west) / scale(PS.scaletext); /* convert scale size to map inches */ length = (sb.length / scale_size) * G_database_units_to_meters_factor() * METERS_TO_INCHES; /* if(sb.units == SB_UNITS_AUTO) { do nothing } */ if(sb.units == SB_UNITS_METERS) length /= G_database_units_to_meters_factor(); else if(sb.units == SB_UNITS_KM) length *= KILOMETERS_TO_METERS / G_database_units_to_meters_factor(); else if(sb.units == SB_UNITS_FEET) length *= FEET_TO_METERS / G_database_units_to_meters_factor(); else if(sb.units == SB_UNITS_MILES) length *= MILES_TO_METERS / G_database_units_to_meters_factor(); else if(sb.units == SB_UNITS_NMILES) length *= NAUT_MILES_TO_METERS / G_database_units_to_meters_factor(); width = sb.height; seg = sb.segment; j = 0; lab = 0; margin = (int)(0.2 * (double)sb.fontsize + 0.5); if (margin < 2) margin = 2; fprintf(PS.fp, "/mg %d def\n", margin); x = sb.x - (length / 2.); set_font_size(sb.fontsize); set_line_width(sb.width); if (strcmp(sb.type, "f") == 0) { /* draw fancy scale bar */ for (i = 0; i < seg; i++) { /* draw a filled rectangle */ x1 = 72.0 * (x + (length / seg) * i) + 0.5; y1 = 72.0 * (PS.page_height - sb.y); x2 = 72.0 * (x + (length / seg) * (i + 1)) + 0.5; y2 = (72.0 * (PS.page_height - sb.y)) + (width * 72.0); /* Alternate black and white */ if (j == 0) { fprintf(PS.fp, "0.0 0.0 0.0 C\n"); j = 1; } else { fprintf(PS.fp, "1.0 1.0 1.0 C\n"); j = 0; } fprintf(PS.fp, "%.1f %.1f %.1f %.1f B\n", x1, y1, x2, y2); /* set outline to black */ fprintf(PS.fp, "F 0.0 0.0 0.0 C\n"); fprintf(PS.fp, "D\n"); lab++; /* do text */ if (i == 0 || lab == sb.numbers) { sprintf(num, "%s", nice_number((sb.length / sb.segment) * i)); text_box_path(x1, y2 + margin, CENTER, LOWER, num, sb.fontsize, 0); if (sb.bgcolor) { /* TODO: take bg color, not just [white|none] */ set_rgb_color(WHITE); fprintf(PS.fp, "F "); } set_rgb_color(BLACK); fprintf(PS.fp, "TIB\n"); lab = 0; } if ((lab > 0 && i == seg - 1) || (sb.numbers == 1 && i == seg - 1)) { /* special case for last label */ sprintf(num, "%s", nice_number(sb.length)); text_box_path(x2, y2 + margin, CENTER, LOWER, num, sb.fontsize, 0); if (sb.bgcolor) { set_rgb_color(WHITE); fprintf(PS.fp, "F "); } set_rgb_color(BLACK); fprintf(PS.fp, "TIB\n"); } } } else { /* draw simple scalebar */ x1 = 72.0 * x + 0.5; y1 = (72.0 * (PS.page_height - sb.y)) + (width * 72.0); x2 = 72.0 * x + 0.5; y2 = 72.0 * (PS.page_height - sb.y); fprintf(PS.fp, "%.1f %.1f %.1f %.1f L D\n", x1, y1, x2, y2); /* draw label */ text_box_path(x1, y1 + margin, CENTER, LOWER, "0", sb.fontsize, 0); if (sb.bgcolor) { set_rgb_color(WHITE); fprintf(PS.fp, "F "); } set_rgb_color(BLACK); fprintf(PS.fp, "TIB\n"); x1 = 72.0 * x + 0.5; y1 = 72.0 * (PS.page_height - sb.y); x2 = 72.0 * (x + length) + 0.5; y2 = 72.0 * (PS.page_height - sb.y); fprintf(PS.fp, "%.1f %.1f %.1f %.1f L D\n", x1, y1, x2, y2); x1 = 72.0 * (x + length) + 0.5; y2 = (72.0 * (PS.page_height - sb.y)) + (width * 72.0); x2 = 72.0 * (x + length) + 0.5; y1 = 72.0 * (PS.page_height - sb.y); fprintf(PS.fp, "%.1f %.1f %.1f %.1f L D\n", x1, y1, x2, y2); /* draw label */ sprintf(num, "%s", nice_number(sb.length)); text_box_path(x1, y2 + margin, CENTER, LOWER, num, sb.fontsize, 0); if (sb.bgcolor) { set_rgb_color(WHITE); fprintf(PS.fp, "F "); } set_rgb_color(BLACK); fprintf(PS.fp, "TIB\n"); for (i = 1; i < seg; i++) { x1 = 72.0 * (x + (length / seg) * i) + 0.5; y1 = 72.0 * (PS.page_height - sb.y); x2 = 72.0 * (x + (length / seg) * i) + 0.5; y2 = (72.0 * (PS.page_height - sb.y)) + (width / 2. * 72.0); y3 = (72.0 * (PS.page_height - sb.y)) + (width * 72.0); fprintf(PS.fp, "%.1f %.1f %.1f %.1f L D\n", x1, y1, x2, y2); lab++; /* do text */ if (lab == sb.numbers) { sprintf(num, "%s", nice_number((sb.length / sb.segment) * i)); text_box_path(x1, y3 + margin, CENTER, LOWER, num, sb.fontsize, 0); if (sb.bgcolor) { set_rgb_color(WHITE); fprintf(PS.fp, "F "); } set_rgb_color(BLACK); fprintf(PS.fp, "TIB\n"); lab = 0; } } } /* draw units label */ if (sb.units == SB_UNITS_AUTO) strcpy(num, G_database_unit_name(TRUE)); else if(sb.units == SB_UNITS_METERS) strcpy(num, "meters"); else if(sb.units == SB_UNITS_KM) strcpy(num, "kilometers"); else if(sb.units == SB_UNITS_FEET) strcpy(num, "feet"); else if(sb.units == SB_UNITS_MILES) strcpy(num, "miles"); else if(sb.units == SB_UNITS_NMILES) strcpy(num, "nautical miles"); text_box_path(72.0 * (x + length/2), 72.0 * (PS.page_height - (sb.y + 0.075)), CENTER, UPPER, num, sb.fontsize, 0); if (sb.bgcolor) { set_rgb_color(WHITE); fprintf(PS.fp, "F "); } set_rgb_color(BLACK); fprintf(PS.fp, "TIB\n"); return 0; }
int do_grid_numbers(void) { double g; char num_text[50]; int grid, vy, vx, hy = 0, hx = 0; int first, len, x, y, last_bottom, last_right; int rounded_grid, margin; if (PS.grid <= 0 || PS.grid_numbers <= 0) return 1; grid = PS.grid * PS.grid_numbers; /* round grid to multiple of 10 */ rounded_grid = 1; if (PS.w.proj != PROJECTION_LL) { sprintf(num_text, "%d", PS.grid); len = strlen(num_text); while (len-- && num_text[len] == '0') rounded_grid *= 10; if (rounded_grid == 10) rounded_grid = 1; } /* initialize */ set_font_name(PS.grid_font); set_font_size(PS.grid_fontsize); set_ps_color(&PS.grid_numbers_color); first = 1; /* horizontal grid numbers * these numbers only appear on the left edge of the first panel. * center the numbers on each grid line. * suppress number if it falls off the map or would overlay the previous * label */ margin = (int)(0.2 * (double)PS.grid_fontsize + 0.5); if (margin < 2) margin = 2; fprintf(PS.fp, "/mg %d def\n", margin); g = floor(PS.w.north / grid) * grid; last_bottom = (int)PS.map_top; first = 1; /* x = XCONV(PS.w.west); */ for (; g > PS.w.south; g -= grid) { /*y = YCONV(g); */ G_plot_where_xy(PS.w.west, g, &vx, &vy); x = (double)vx / 10.; y = (double)vy / 10.; if (y + PS.grid_fontsize > last_bottom) continue; if (y - PS.grid_fontsize < (int)PS.map_bot) continue; sprintf(num_text, "%s", format_northing(g, rounded_grid)); text_box_path(x, y, LEFT, CENTER, num_text, PS.grid_fontsize, 0); set_rgb_color(WHITE); fprintf(PS.fp, "F "); set_ps_color(&PS.grid_numbers_color); fprintf(PS.fp, "TIB\n"); last_bottom = y - PS.grid_fontsize; if (first) { first = 0; hy = y + (int)(0.5 * (double)PS.grid_fontsize + 0.5) + margin; hx = x + 0.7 * strlen(num_text) * PS.grid_fontsize + 2 * margin; } } /* vertical grid numbers * center the numbers on each grid line. * suppress number if it falls of the map or would overlay the previous * label */ g = floor(PS.w.west / grid) * grid; last_right = (int)PS.map_left; /* y = YCONV(PS.w.north); */ for (; g < PS.w.east; g += grid) { /* x = XCONV(g); */ G_plot_where_xy(g, PS.w.north, &vx, &vy); x = (double)vx / 10.; y = (double)vy / 10.; if (x - PS.grid_fontsize < last_right) continue; if (x + PS.grid_fontsize > (int)PS.map_right) continue; sprintf(num_text, "%s", format_easting(g, rounded_grid)); vy = y - 0.7 * strlen(num_text) * PS.grid_fontsize - 2 * margin; vx = x - (int)(0.5 * (double)PS.grid_fontsize + 0.5) - margin; if (vx < hx && vy < hy) continue; fprintf(PS.fp, "ZB (%s) PB 90 rotate\n", num_text); fprintf(PS.fp, "%d br sub bl add mg add\n", y); fprintf(PS.fp, "%d bt bb sub D2 add mg sub neg TR TB\n", x); set_rgb_color(WHITE); fprintf(PS.fp, "F "); set_ps_color(&PS.grid_numbers_color); fprintf(PS.fp, "TIB\n"); last_right = x + PS.grid_fontsize; } return 0; }