Esempio n. 1
0
def add_print_page(ctx, mmap, href, well_bounds_pt, points_FG, hm2pt_ratio, layout, text, mark, fuzzy, indexees):
    """
    """
    print "Adding print page:", href

    well_xmin_pt, well_ymin_pt, well_xmax_pt, well_ymax_pt = well_bounds_pt
    well_width_pt, well_height_pt = well_xmax_pt - well_xmin_pt, well_ymax_pt - well_ymin_pt
    well_aspect_ratio = well_width_pt / well_height_pt

    #
    # Offset drawing area to top-left of map area
    #
    ctx.translate(well_xmin_pt, well_ymin_pt)

    #
    # Build up map area
    #
    img = get_mmap_image(mmap)

    if layout == "half-page" and well_aspect_ratio > 1:
        map_width_pt, map_height_pt = well_width_pt / 2, well_height_pt
        add_page_text(ctx, text, map_width_pt + 24, 24, map_width_pt - 48, map_height_pt - 48)

    elif layout == "half-page" and well_aspect_ratio < 1:
        map_width_pt, map_height_pt = well_width_pt, well_height_pt / 2
        add_page_text(ctx, text, 32, map_height_pt + 16, map_width_pt - 64, map_height_pt - 32)

    else:
        map_width_pt, map_height_pt = well_width_pt, well_height_pt

    place_image(ctx, img, 0, 0, map_width_pt, map_height_pt)

    #
    # Draw a dot if need be
    #
    if fuzzy is not None:
        loc = Location(fuzzy[1], fuzzy[0])
        pt = mmap.locationPoint(loc)

        x = map_width_pt * float(pt.x) / mmap.dimensions.x
        y = map_height_pt * float(pt.y) / mmap.dimensions.y

        draw_circle(ctx, x, y, 20)
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(2)
        ctx.set_dash([2, 6])
        ctx.stroke()

    #
    # X marks the spot, if needed
    #
    if mark is not None:
        loc = Location(mark[1], mark[0])
        pt = mmap.locationPoint(loc)

        x = map_width_pt * float(pt.x) / mmap.dimensions.x
        y = map_height_pt * float(pt.y) / mmap.dimensions.y

        draw_cross(ctx, x, y, 8, 6)
        ctx.set_source_rgb(1, 1, 1)
        ctx.fill()

        draw_cross(ctx, x, y, 8, 4)
        ctx.set_source_rgb(0, 0, 0)
        ctx.fill()

    #
    # Perhaps some boxes?
    #
    page_numbers = []

    for page in indexees:
        north, west, south, east = page["bounds"]

        ul = mmap.locationPoint(Location(north, west))
        lr = mmap.locationPoint(Location(south, east))

        x1 = map_width_pt * float(ul.x) / mmap.dimensions.x
        x2 = map_width_pt * float(lr.x) / mmap.dimensions.x
        y1 = map_height_pt * float(ul.y) / mmap.dimensions.y
        y2 = map_height_pt * float(lr.y) / mmap.dimensions.y

        draw_box(ctx, x1, y1, x2 - x1, y2 - y1)
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(1)
        ctx.set_dash([])
        ctx.stroke()

        page_numbers.append((x1, y1, x2, y2, page["number"]))

    #
    # Calculate positions of registration points
    #
    ctx.save()

    ctx.translate(well_width_pt, well_height_pt)
    ctx.scale(1 / hm2pt_ratio, 1 / hm2pt_ratio)

    reg_points = (point_A, point_B, point_C, point_D, point_E) + points_FG

    device_points = [ctx.user_to_device(pt.x, pt.y) for pt in reg_points]

    ctx.restore()

    #
    # Draw QR code area
    #
    ctx.save()

    ctx.translate(well_width_pt, well_height_pt)

    draw_box(ctx, 0, 0, -90, -90)
    ctx.set_source_rgb(1, 1, 1)
    ctx.fill()

    place_image(ctx, get_qrcode_image(href), -83, -83, 83, 83)

    ctx.restore()

    #
    # Draw registration points
    #
    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)

        draw_circle(ctx, x, y, 0.12 * ptpin)
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(0.5)
        ctx.set_dash([1.5, 3])
        ctx.stroke()

    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)

        draw_circle(ctx, x, y, 0.12 * ptpin)
        ctx.set_source_rgb(1, 1, 1)
        ctx.fill()

    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)

        draw_circle(ctx, x, y, 0.06 * ptpin)
        ctx.set_source_rgb(0, 0, 0)
        ctx.fill()

    #
    # Draw top-left icon
    #
    icon = pathjoin(dirname(__file__), "images/logo.png")
    img = ImageSurface.create_from_png(icon)
    place_image(ctx, img, 0, -36, 129.1, 36)

    try:
        font_file = realpath("fonts/Helvetica.ttf")

        if font_file not in cached_fonts:
            cached_fonts[font_file] = create_cairo_font_face_for_file(font_file)

        font = cached_fonts[font_file]
    except:
        # no text for us.
        pass
    else:
        ctx.set_font_face(font)
        ctx.set_font_size(12)

        line = href
        text_width = ctx.text_extents(line)[2]

        ctx.move_to(well_width_pt - text_width, -6)
        ctx.show_text(line)

        add_scale_bar(ctx, mmap, map_height_pt)

        ctx.set_font_face(font)
        ctx.set_font_size(18)

        for (x1, y1, x2, y2, number) in page_numbers:
            number_w, number_h = ctx.text_extents(number)[2:4]
            offset_x, offset_y = (x1 + x2 - number_w) / 2, (y1 + y2 + number_h) / 2

            draw_box(ctx, offset_x - 4, offset_y - number_h - 4, number_w + 8, number_h + 8)
            ctx.set_source_rgb(1, 1, 1)
            ctx.fill()

            ctx.set_source_rgb(0, 0, 0)
            ctx.move_to(offset_x, offset_y)
            ctx.show_text(number)

    ctx.show_page()
Esempio n. 2
0
def add_scale_bar(ctx, mmap, map_height_pt):
    """
    """
    size, distance, units = get_map_scale(mmap, map_height_pt)

    ctx.save()
    ctx.translate(60, map_height_pt - 20)

    draw_box(ctx, -50, -10, size + 10 + 45, 20)
    ctx.set_source_rgb(1, 1, 1)
    ctx.fill()

    #
    # true north
    #

    draw_circle(ctx, -40, 0, 6.7)
    ctx.set_source_rgb(0, 0, 0)
    ctx.fill()

    ctx.move_to(-40, -6.7)
    ctx.line_to(-38.75, -2.2)
    ctx.line_to(-41.25, -2.2)
    ctx.line_to(-40, -6.5)
    ctx.set_source_rgb(1, 1, 1)
    ctx.fill()

    ctx.set_source_rgb(0, 0, 0)
    ctx.set_font_size(5.8)

    ctx.move_to(-25.1, -2.1)
    ctx.show_text("TRUE")
    ctx.move_to(-27.6, 4.9)
    ctx.show_text("NORTH")

    #
    # scale bar
    #

    ctx.move_to(0, 2)
    ctx.line_to(0, 5)
    ctx.line_to(size, 5)
    ctx.line_to(size, 2)

    ctx.set_source_rgb(0, 0, 0)
    ctx.set_line_width(0.5)
    ctx.set_dash([])
    ctx.stroke()

    ctx.set_font_size(9)
    zero_width = ctx.text_extents("0")[2]
    distance_width = ctx.text_extents(distance)[2]

    ctx.move_to(0, 0)
    ctx.show_text("0")

    ctx.move_to(size - distance_width, 0)
    ctx.show_text(distance)

    ctx.set_font_size(7)
    units_width = ctx.text_extents(units)[2]

    ctx.move_to(zero_width + (size - zero_width - distance_width) / 2 - units_width / 2, 0)
    ctx.show_text(units.upper())

    ctx.restore()
Esempio n. 3
0
def add_print_page(ctx, mmap, href, well_bounds_pt, points_FG, hm2pt_ratio, layout, text, mark, fuzzy, indexees):
    """
    """
    print 'Adding print page:', href

    well_xmin_pt, well_ymin_pt, well_xmax_pt, well_ymax_pt = well_bounds_pt
    well_width_pt, well_height_pt = well_xmax_pt - well_xmin_pt, well_ymax_pt - well_ymin_pt
    well_aspect_ratio = well_width_pt / well_height_pt

    #
    # Offset drawing area to top-left of map area
    #
    ctx.translate(well_xmin_pt, well_ymin_pt)

    #
    # Build up map area
    #
    img = get_mmap_image(mmap)

    if layout == 'half-page' and well_aspect_ratio > 1:
        map_width_pt, map_height_pt = well_width_pt/2, well_height_pt
        add_page_text(ctx, text, map_width_pt + 24, 24, map_width_pt - 48, map_height_pt - 48)

    elif layout == 'half-page' and well_aspect_ratio < 1:
        map_width_pt, map_height_pt = well_width_pt, well_height_pt/2
        add_page_text(ctx, text, 32, map_height_pt + 16, map_width_pt - 64, map_height_pt - 32)

    else:
        map_width_pt, map_height_pt = well_width_pt, well_height_pt

    place_image(ctx, img, 0, 0, map_width_pt, map_height_pt)

    #
    # Draw a dot if need be
    #
    if fuzzy is not None:
        loc = Location(fuzzy[1], fuzzy[0])
        pt = mmap.locationPoint(loc)

        x = map_width_pt * float(pt.x) / mmap.dimensions.x
        y = map_height_pt * float(pt.y) / mmap.dimensions.y

        draw_circle(ctx, x, y, 20)
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(2)
        ctx.set_dash([2, 6])
        ctx.stroke()

    #
    # X marks the spot, if needed
    #
    if mark is not None:
        loc = Location(mark[1], mark[0])
        pt = mmap.locationPoint(loc)

        x = map_width_pt * float(pt.x) / mmap.dimensions.x
        y = map_height_pt * float(pt.y) / mmap.dimensions.y

        draw_cross(ctx, x, y, 8, 6)
        ctx.set_source_rgb(1, 1, 1)
        ctx.fill()

        draw_cross(ctx, x, y, 8, 4)
        ctx.set_source_rgb(0, 0, 0)
        ctx.fill()

    #
    # Perhaps some boxes?
    #
    page_numbers = []

    for page in indexees:
        north, west, south, east = page['bounds']

        ul = mmap.locationPoint(Location(north, west))
        lr = mmap.locationPoint(Location(south, east))

        x1 = map_width_pt * float(ul.x) / mmap.dimensions.x
        x2 = map_width_pt * float(lr.x) / mmap.dimensions.x
        y1 = map_height_pt * float(ul.y) / mmap.dimensions.y
        y2 = map_height_pt * float(lr.y) / mmap.dimensions.y

        draw_box(ctx, x1, y1, x2-x1, y2-y1)
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(1)
        ctx.set_dash([])
        ctx.stroke()

        page_numbers.append((x1, y1, x2, y2, page['number']))

    #
    # Calculate positions of registration points
    #
    ctx.save()

    ctx.translate(well_width_pt, well_height_pt)
    ctx.scale(1/hm2pt_ratio, 1/hm2pt_ratio)

    reg_points = (point_A, point_B, point_C, point_D, point_E) + points_FG

    device_points = [ctx.user_to_device(pt.x, pt.y) for pt in reg_points]

    ctx.restore()

    #
    # Draw QR code area
    #
    ctx.save()

    ctx.translate(well_width_pt, well_height_pt)

    draw_box(ctx, 0, 0, -90, -90)
    ctx.set_source_rgb(1, 1, 1)
    ctx.fill()

    place_image(ctx, get_qrcode_image(href), -83, -83, 83, 83)

    ctx.restore()

    #
    # Draw registration points
    #
    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)

        draw_circle(ctx, x, y, .12 * ptpin)
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(.5)
        ctx.set_dash([1.5, 3])
        ctx.stroke()

    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)

        draw_circle(ctx, x, y, .12 * ptpin)
        ctx.set_source_rgb(1, 1, 1)
        ctx.fill()

    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)

        draw_circle(ctx, x, y, .06 * ptpin)
        ctx.set_source_rgb(0, 0, 0)
        ctx.fill()

    #
    # Draw top-left icon
    #
    icon = pathjoin(dirname(__file__), 'images/logo.png')
    img = ImageSurface.create_from_png(icon)
    place_image(ctx, img, 0, -36, 129.1, 36)

    try:
        font_file = realpath('fonts/LiberationSans-Regular.ttf')

        if font_file not in cached_fonts:
            cached_fonts[font_file] = create_cairo_font_face_for_file(font_file)

        font = cached_fonts[font_file]
    except:
        # no text for us.
        pass
    else:
        ctx.set_font_face(font)
        ctx.set_font_size(12)

        line = href
        text_width = ctx.text_extents(line)[2]

        ctx.move_to(well_width_pt - text_width, -6)
        ctx.show_text(line)

        add_scale_bar(ctx, mmap, map_height_pt)

        ctx.set_font_face(font)
        ctx.set_font_size(18)

        for (x1, y1, x2, y2, number) in page_numbers:
            number_w, number_h = ctx.text_extents(number)[2:4]
            offset_x, offset_y = (x1 + x2 - number_w) / 2, (y1 + y2 + number_h) / 2

            draw_box(ctx, offset_x - 4, offset_y - number_h - 4, number_w + 8, number_h + 8)
            ctx.set_source_rgb(1, 1, 1)
            ctx.fill()

            ctx.set_source_rgb(0, 0, 0)
            ctx.move_to(offset_x, offset_y)
            ctx.show_text(number)

    ctx.show_page()
Esempio n. 4
0
def add_print_page(ctx, mmap, href, well_bounds_pt, points_FG, hm2pt_ratio):
    """
    """
    print 'Adding print page:', href
    
    well_xmin_pt, well_ymin_pt, well_xmax_pt, well_ymax_pt = well_bounds_pt
    well_width_pt, well_height_pt = well_xmax_pt - well_xmin_pt, well_ymax_pt - well_ymin_pt
    
    #
    # Offset drawing area to top-left of map area
    #
    ctx.translate(well_xmin_pt, well_ymin_pt)
    
    #
    # Build up map area
    #
    draw_box(ctx, 0, 0, well_width_pt, well_height_pt)
    ctx.set_source_rgb(.9, .9, .9)
    ctx.fill()
    
    img = get_mmap_image(mmap)
    place_image(ctx, img, 0, 0, well_width_pt, well_height_pt)
    
    #
    # Calculate positions of registration points
    #
    ctx.save()
    
    ctx.translate(well_width_pt, well_height_pt)
    ctx.scale(1/hm2pt_ratio, 1/hm2pt_ratio)
    
    reg_points = (point_A, point_B, point_C, point_D, point_E) + points_FG
    
    device_points = [ctx.user_to_device(pt.x, pt.y) for pt in reg_points]
    
    ctx.restore()
    
    #
    # Draw QR code area
    #
    ctx.save()
    
    ctx.translate(well_width_pt, well_height_pt)
    
    draw_box(ctx, 0, 0, -90, -90)
    ctx.set_source_rgb(1, 1, 1)
    ctx.fill()
    
    place_image(ctx, get_qrcode_image(href), -83, -83, 83, 83)
    
    ctx.restore()
    
    #
    # Draw registration points
    #
    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)
    
        draw_circle(ctx, x, y, .12 * ptpin)
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(.5)
        ctx.set_dash([1.5, 3])
        ctx.stroke()

    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)
    
        draw_circle(ctx, x, y, .12 * ptpin)
        ctx.set_source_rgb(1, 1, 1)
        ctx.fill()

    for (x, y) in device_points:
        x, y = ctx.device_to_user(x, y)
    
        draw_circle(ctx, x, y, .06 * ptpin)
        ctx.set_source_rgb(0, 0, 0)
        ctx.fill()
    
    #
    # Draw top-left icon
    #
    icon = pathjoin(dirname(__file__), '../site/lib/print/icon.png')
    img = ImageSurface.create_from_png(icon)
    place_image(ctx, img, 0, -29.13, 19.2, 25.6)
    
    try:
        font = create_cairo_font_face_for_file('fonts/LiberationSans-Bold.ttf')
    except:
        # no text for us.
        pass
    else:
        # draw some text.
        ctx.set_font_face(font)
        ctx.set_font_size(24)
        ctx.move_to(0 + 19.2 + 8, -29.13 + 25.6 - 1)
        ctx.show_text('Walking Papers')
    
    try:
        font = create_cairo_font_face_for_file('fonts/LiberationSans-Regular.ttf')
    except:
        # no text for us.
        pass
    else:
        ctx.set_font_face(font)
        ctx.set_font_size(8)
        
        lines = ['OSM data ©2011 CC-BY-SA Openstreetmap.org contributors.',
                 'Help improve OpenStreetMap by drawing on this map, then visit',
                 href or '']
        
        text_width = max([ctx.text_extents(line)[2] for line in lines])
        
        ctx.move_to(well_width_pt - text_width, -25)
        ctx.show_text(lines[0])

        ctx.move_to(well_width_pt - text_width, -15)
        ctx.show_text(lines[1])

        ctx.move_to(well_width_pt - text_width, -5)
        ctx.show_text(lines[2])
    
    ctx.show_page()
Esempio n. 5
0
def add_scale_bar(ctx, mmap, map_height_pt):
    """
    """
    size, distance, units = get_map_scale(mmap, map_height_pt)

    ctx.save()
    ctx.translate(60, map_height_pt - 20)

    draw_box(ctx, -50, -10, size + 10 + 45, 20)
    ctx.set_source_rgb(1, 1, 1)
    ctx.fill()

    #
    # true north
    #

    draw_circle(ctx, -40, 0, 6.7)
    ctx.set_source_rgb(0, 0, 0)
    ctx.fill()

    ctx.move_to(-40, -6.7)
    ctx.line_to(-38.75, -2.2)
    ctx.line_to(-41.25, -2.2)
    ctx.line_to(-40, -6.5)
    ctx.set_source_rgb(1, 1, 1)
    ctx.fill()

    ctx.set_source_rgb(0, 0, 0)
    ctx.set_font_size(5.8)

    ctx.move_to(-25.1, -2.1)
    ctx.show_text('TRUE')
    ctx.move_to(-27.6, 4.9)
    ctx.show_text('NORTH')

    #
    # scale bar
    #

    ctx.move_to(0, 2)
    ctx.line_to(0, 5)
    ctx.line_to(size, 5)
    ctx.line_to(size, 2)

    ctx.set_source_rgb(0, 0, 0)
    ctx.set_line_width(.5)
    ctx.set_dash([])
    ctx.stroke()

    ctx.set_font_size(9)
    zero_width = ctx.text_extents('0')[2]
    distance_width = ctx.text_extents(distance)[2]

    ctx.move_to(0, 0)
    ctx.show_text('0')

    ctx.move_to(size - distance_width, 0)
    ctx.show_text(distance)

    ctx.set_font_size(7)
    units_width = ctx.text_extents(units)[2]

    ctx.move_to(zero_width + (size - zero_width - distance_width)/2 - units_width/2, 0)
    ctx.show_text(units.upper())

    ctx.restore()