示例#1
0
文件: text.c 项目: ashaw/simple-tiles
// Before placing a new label we need to see if the label overlaps over
// previously placed labels. This algorithm will be refactored a bit to try
// NE SE SW NW placements in the future.
void
try_and_insert_placement(simplet_lithograph_t *litho, PangoLayout *layout, double x, double y){
  int width, height;
  // Find the computed width and height of a layout in image pixels
  pango_layout_get_pixel_size(layout, &width, &height);
  simplet_bounds_t *bounds = simplet_bounds_new();
  if(!bounds) return;
  // Create a bounds to test for intersection
  simplet_bounds_extend(bounds, floor(x - width / 2), floor(y - height / 2));
  simplet_bounds_extend(bounds, floor(x + width / 2), floor(y + height / 2));

  // Iterate through the list of already placed labels and check for overlaps.
  simplet_listiter_t *iter = simplet_get_list_iter(litho->placements);
  placement_t *placement;
  while((placement = (placement_t *) simplet_list_next(iter))){
    if(simplet_bounds_intersects(placement->bounds, bounds)){
      simplet_bounds_free(bounds);
      g_object_unref(layout);
      simplet_list_iter_free(iter);
      return;
    }
  }

  // If we get here we can create and insert a new placement.
  placement_t *plc = placement_new(layout, bounds);
  if(!plc) {
    simplet_bounds_free(bounds);
    g_object_unref(layout);
    return;
  }

  simplet_list_push(litho->placements, (void *)plc);
}
示例#2
0
// Create a bounds from the Convex Hull of an OGR Geometry.
simplet_bounds_t*
simplet_bounds_from_ogr(OGRGeometryH geom){
  OGRGeometryH hull;

  // Grab the Convex Hull
  if(!(hull = OGR_G_ConvexHull(geom)))
    return NULL;

  // Create the bounds.
  simplet_bounds_t *bounds;
  if(!(bounds = simplet_bounds_new())){
    OGR_G_DestroyGeometry(hull);
    return NULL;
  }

  // Extend the bounds by adding the points from the Convex Hull.
  double x, y;
  for(int i = 0; i < OGR_G_GetGeometryCount(hull); i++){
    OGRGeometryH subgeom = OGR_G_GetGeometryRef(hull, i);
    if(subgeom == NULL)
      continue;
    for(int j = 0; j < OGR_G_GetPointCount(subgeom); j++){
      OGR_G_GetPoint(subgeom, j, &x, &y, NULL);
      simplet_bounds_extend(bounds, x, y);
    }
  }

  OGR_G_DestroyGeometry(hull);
  return bounds;
}