#include <assert.h>
#include <cairo.h>
#include <cairo-win32.h>
#include <windows.h>

#ifdef CAIRO_HAS_PDF_SURFACE
# include <cairo-pdf.h>
#endif
#ifdef CAIRO_HAS_PS_SURFACE
# include <cairo-ps.h>
#endif
#ifdef CAIRO_HAS_SVG_SURFACE
# include <cairo-svg.h>
#endif

void test(cairo_t* cairo, cairo_font_face_t* font, const char* name) {
    printf("trying %s\n", name);
    cairo_set_font_face(cairo, font);
    cairo_set_font_size(cairo, 12);
    cairo_move_to(cairo, 72, 72);
    cairo_show_text(cairo, "Hello World!");
    cairo_show_page(cairo);
    cairo_status_t status;
    if ( CAIRO_STATUS_SUCCESS != (status = cairo_status(cairo)) ) {
        printf("cairo_show_page() failed for %s with error %s\n", name, cairo_status_to_string(status));
    }
    cairo_destroy(cairo);
}

int main(int argc, char** argv) {
#ifdef CAIRO_HAS_PDF_SURFACE
    cairo_surface_t* pdf_surface_hfont = cairo_pdf_surface_create("hfont.pdf", 720, 720);
    cairo_t* pdf_cairo_hfont = cairo_create(pdf_surface_hfont);
    cairo_surface_destroy(pdf_surface_hfont);
    cairo_surface_t* pdf_surface_logfont = cairo_pdf_surface_create("logfont.pdf", 720, 720);
    cairo_t* pdf_cairo_logfont = cairo_create(pdf_surface_logfont);
    cairo_surface_destroy(pdf_surface_logfont);
#endif
#ifdef CAIRO_HAS_PS_SURFACE
    cairo_surface_t* ps_surface_hfont = cairo_ps_surface_create("hfont.ps", 720, 720);
    cairo_t* ps_cairo_hfont = cairo_create(ps_surface_hfont);
    cairo_surface_destroy(ps_surface_hfont);
    cairo_surface_t* ps_surface_logfont = cairo_ps_surface_create("logfont.ps", 720, 720);
    cairo_t* ps_cairo_logfont = cairo_create(ps_surface_logfont);
    cairo_surface_destroy(ps_surface_logfont);
#endif
#ifdef CAIRO_HAS_SVG_SURFACE
    cairo_surface_t* svg_surface_hfont = cairo_svg_surface_create("hfont.svg", 720, 720);
    cairo_t* svg_cairo_hfont = cairo_create(svg_surface_hfont);
    cairo_surface_destroy(svg_surface_hfont);
    cairo_surface_t* svg_surface_logfont = cairo_svg_surface_create("logfont.svg", 720, 720);
    cairo_t* svg_cairo_logfont = cairo_create(svg_surface_logfont);
    cairo_surface_destroy(svg_surface_logfont);
#endif

    LOGFONTW lf = {
        12,
        0,
        0,
        0,
        400,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        DEFAULT_CHARSET,
        OUT_DEFAULT_PRECIS,
        DEFAULT_QUALITY,
        DEFAULT_PITCH | FF_DONTCARE,
        L"Times New Roman" };
    HFONT hf = CreateFontIndirectW(&lf);
    cairo_font_face_t* hfont = cairo_win32_font_face_create_for_hfont(hf);
    assert(CAIRO_STATUS_SUCCESS == cairo_font_face_status(hfont));
    cairo_font_face_t* logfont = cairo_win32_font_face_create_for_logfontw(&lf);
    assert(CAIRO_STATUS_SUCCESS == cairo_font_face_status(logfont));

#ifdef CAIRO_HAS_PDF_SURFACE
    test(pdf_cairo_logfont, logfont, "pdf/logfont");
    test(pdf_cairo_hfont, hfont, "pdf/hfont");
#endif
#ifdef CAIRO_HAS_PS_SURFACE
    test(ps_cairo_logfont, logfont, "ps/logfont");
    test(ps_cairo_hfont, hfont, "ps/hfont");
#endif
#ifdef CAIRO_HAS_SVG_SURFACE
    test(svg_cairo_logfont, logfont, "svg/logfont");
    test(svg_cairo_hfont, hfont, "svg/hfont");
#endif

    cairo_font_face_destroy(hfont);
    cairo_font_face_destroy(logfont);
}
