From a4b7981d56e3cb22e2a1c18e40906e88fc331da5 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 17 May 2016 16:45:08 +0200 Subject: html: Add SVG image support. --- source/html/html-layout.c | 23 ++++++++++++++++------- source/svg/svg-doc.c | 23 ++++++++++++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) (limited to 'source') diff --git a/source/html/html-layout.c b/source/html/html-layout.c index a720a9f8..cef2acd3 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -1,4 +1,5 @@ #include "mupdf/html.h" +#include "mupdf/svg.h" #include "hb.h" #include "hb-ft.h" @@ -395,7 +396,13 @@ static fz_image *load_html_image(fz_context *ctx, fz_archive *zip, const char *b fz_try(ctx) { buf = fz_read_archive_entry(ctx, zip, path); - img = fz_new_image_from_buffer(ctx, buf); + if (strstr(path, ".svg")) + { + fz_write_buffer_byte(ctx, buf, 0); + img = fz_new_image_from_svg(ctx, buf); + } + else + img = fz_new_image_from_buffer(ctx, buf); } fz_always(ctx) fz_drop_buffer(ctx, buf); @@ -728,15 +735,17 @@ static void generate_boxes(fz_context *ctx, fz_xml *node, fz_html *top, static void measure_image(fz_context *ctx, fz_html_flow *node, float max_w, float max_h) { float xs = 1, ys = 1, s = 1; + float image_w = node->content.image->w * 72 / node->content.image->xres; + float image_h = node->content.image->h * 72 / node->content.image->yres; node->x = 0; node->y = 0; - if (node->content.image->w > max_w) - xs = max_w / node->content.image->w; - if (node->content.image->h > max_h) - ys = max_h / node->content.image->h; + if (image_w > max_w) + xs = max_w / image_w; + if (image_h > max_h) + ys = max_h / image_h; s = fz_min(xs, ys); - node->w = node->content.image->w * s; - node->h = node->content.image->h * s; + node->w = image_w * s; + node->h = image_h * s; } typedef struct string_walker diff --git a/source/svg/svg-doc.c b/source/svg/svg-doc.c index 9b7aa670..25f00e62 100644 --- a/source/svg/svg-doc.c +++ b/source/svg/svg-doc.c @@ -150,14 +150,18 @@ svg_recognize(fz_context *ctx, const char *magic) } fz_display_list * -fz_new_display_list_from_svg(fz_context *ctx, fz_buffer *buf) +fz_new_display_list_from_svg(fz_context *ctx, fz_buffer *buf, float *w, float *h) { fz_document *doc; fz_display_list *list; doc = svg_open_document_with_buffer(ctx, buf); fz_try(ctx) + { list = fz_new_display_list_from_page_number(ctx, doc, 0); + *w = ((svg_document*)doc)->width; + *h = ((svg_document*)doc)->height; + } fz_always(ctx) fz_drop_document(ctx, doc); fz_catch(ctx) @@ -166,6 +170,23 @@ fz_new_display_list_from_svg(fz_context *ctx, fz_buffer *buf) return list; } +fz_image * +fz_new_image_from_svg(fz_context *ctx, fz_buffer *buf) +{ + fz_display_list *list; + fz_image *image; + float w, h; + + list = fz_new_display_list_from_svg(ctx, buf, &w, &h); + fz_try(ctx) + image = fz_new_image_from_display_list(ctx, w, h, list); + fz_always(ctx) + fz_drop_display_list(ctx, list); + fz_catch(ctx) + fz_rethrow(ctx); + return image; +} + fz_document_handler svg_document_handler = { &svg_recognize, -- cgit v1.2.3