From 7934ac9e3da16cdfa3e6ed481acaa3bdbc6dbfed Mon Sep 17 00:00:00 2001 From: Michael Vrhel Date: Tue, 4 Jun 2013 11:55:51 -0700 Subject: Change in doc_document.c to account for oxps type. Also error catching when file fails to open. Removal of null checks for object freeing in interface to mupdf. --- winrt/mupdf_cpp/MainPage.xaml.cpp | 37 +++++++---- winrt/mupdf_cpp/Package.appxmanifest | 1 + winrt/mupdfwinrt/muctx.cpp | 123 ++++++++++++----------------------- winrt/mupdfwinrt/muctx.h | 15 +++-- winrt/mupdfwinrt/mudocument.cpp | 14 ++-- winrt/mupdfwinrt/mudocument.h | 2 +- winrt/mupdfwinrt/mupdfwinrt.vcxproj | 2 + 7 files changed, 88 insertions(+), 106 deletions(-) (limited to 'winrt') diff --git a/winrt/mupdf_cpp/MainPage.xaml.cpp b/winrt/mupdf_cpp/MainPage.xaml.cpp index dded888e..f6d74dda 100644 --- a/winrt/mupdf_cpp/MainPage.xaml.cpp +++ b/winrt/mupdf_cpp/MainPage.xaml.cpp @@ -172,6 +172,7 @@ void mupdf_cpp::MainPage::Picker(Platform::Object^ sender, Windows::UI::Xaml::Ro openPicker->FileTypeFilter->Append(".pdf"); openPicker->FileTypeFilter->Append(".xps"); openPicker->FileTypeFilter->Append(".cbz"); + openPicker->FileTypeFilter->Append(".oxps"); create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) { @@ -554,34 +555,42 @@ void mupdf_cpp::MainPage::OpenDocument(StorageFile^ file) /* Open document and when open, push on */ auto open_task = create_task(mu_doc->OpenFileAsync(file)); - open_task.then([this]() -> bool + open_task.then([this](int code) -> int { assert(IsMainThread()); + if (code != S_ISOK) + { + return code; + } /* We need to check if password is required */ if (mu_doc->RequiresPassword()) { xaml_PasswordStack->Visibility = Windows::UI::Xaml::Visibility::Visible; - return false; + return E_NEEDPASSWORD; } else { xaml_PasswordStack->Visibility = Windows::UI::Xaml::Visibility::Collapsed; - return true; + return S_ISOK; } - }).then([this](bool password_ok)->bool + }).then([this](int code)->int { assert(IsMainThread()); - if (!password_ok) - return password_ok; - else - { + if (code == S_ISOK) InitialRender(); - return password_ok; - } - }, task_continuation_context::use_current()).then([this](bool password_ok) + return code; + }, task_continuation_context::use_current()).then([this](int code) { - if (password_ok) + if (code == S_ISOK) RenderThumbs(); + else + { + if (code != E_NEEDPASSWORD) + { + NotifyUser("Sorry, an issue was encountered in opening file", + StatusMessage); + } + } }, task_continuation_context::use_current()); } @@ -1328,6 +1337,8 @@ void mupdf_cpp::MainPage::ScrollChanged(Platform::Object^ sender, Windows::UI::Xaml::FrameworkElement^ FindVisualChildByName(DependencyObject^ obj, String^ name) { FrameworkElement^ ret; + if (obj == nullptr) return nullptr; + int numChildren = VisualTreeHelper::GetChildrenCount(obj); for (int i = 0; i < numChildren; i++) @@ -1348,6 +1359,8 @@ Windows::UI::Xaml::FrameworkElement^ FindVisualChildByName(DependencyObject^ obj /* Zoom in and out for keyboard only case. */ void MainPage::OnKeyDown(KeyRoutedEventArgs^ e) { + if (!m_init_done) return; + ScrollViewer^ scrollviewer; FlipViewItem^ item = safe_cast (m_curr_flipView->ItemContainerGenerator->ContainerFromIndex(m_currpage)); diff --git a/winrt/mupdf_cpp/Package.appxmanifest b/winrt/mupdf_cpp/Package.appxmanifest index fa810175..f4315785 100644 --- a/winrt/mupdf_cpp/Package.appxmanifest +++ b/winrt/mupdf_cpp/Package.appxmanifest @@ -36,6 +36,7 @@ .pdf .xps .cbz + .oxps diff --git a/winrt/mupdfwinrt/muctx.cpp b/winrt/mupdfwinrt/muctx.cpp index 17a62535..2643de24 100644 --- a/winrt/mupdfwinrt/muctx.cpp +++ b/winrt/mupdfwinrt/muctx.cpp @@ -72,9 +72,9 @@ static void win_seek_file(fz_stream *stm, int offset, int whence) static void win_close_file(fz_context *ctx, void *state) { - DataReader^ dataReader = reinterpret_cast (state); - - delete dataReader; + win_stream_struct *win_stream = reinterpret_cast (state); + IRandomAccessStream^ stream = win_stream->stream; + delete stream; } /* mutext functions see mupdf readme for details */ @@ -102,11 +102,10 @@ void muctx::CleanUp(void) this->mu_ctx = NULL; this->mu_doc = NULL; this->mu_outline = NULL; - this->mu_stream = NULL; } /* Set up the context, mutex and cookie */ -HRESULT muctx::InitializeContext() +status_t muctx::InitializeContext() { int i; @@ -121,11 +120,11 @@ HRESULT muctx::InitializeContext() this->mu_ctx = fz_new_context(NULL, &mu_locks, FZ_STORE_DEFAULT); if (this->mu_ctx == NULL) { - return E_OUTOFMEMORY; + return E_OUTOFMEM; } else { - return S_OK; + return S_ISOK; } } @@ -135,7 +134,6 @@ muctx::muctx(void) this->mu_ctx = NULL; this->mu_doc = NULL; this->mu_outline = NULL; - this->mu_stream = NULL; } /* Destructor */ @@ -151,24 +149,30 @@ muctx::~muctx(void) this->mu_ctx = NULL; this->mu_doc = NULL; this->mu_outline = NULL; - this->mu_stream = NULL; } /* Set up the stream access */ -HRESULT muctx::InitializeStream(IRandomAccessStream^ readStream, char *ext) +status_t muctx::InitializeStream(IRandomAccessStream^ readStream, char *ext) { win_stream.stream = readStream; - this->mu_stream = fz_new_stream(mu_ctx, 0, win_read_file, win_close_file); - this->mu_stream->seek = win_seek_file; - this->mu_stream->state = reinterpret_cast (&win_stream); + fz_stream *mu_stream = fz_new_stream(mu_ctx, 0, win_read_file, win_close_file); + mu_stream->seek = win_seek_file; + mu_stream->state = reinterpret_cast (&win_stream); /* Now lets see if we can open the file */ - mu_doc = fz_open_document_with_stream(mu_ctx, ext, this->mu_stream); - - if (mu_doc == NULL) - return E_FAIL; - else - return S_OK; + fz_try(mu_ctx) + { + mu_doc = fz_open_document_with_stream(mu_ctx, ext, mu_stream); + } + fz_always(mu_ctx) + { + fz_close(mu_stream); + } + fz_catch(mu_ctx) + { + return E_FAILURE; + } + return S_ISOK; } /* Return the documents page count */ @@ -263,10 +267,7 @@ int muctx::GetContents(sh_vector_content contents_vec) } fz_always(ctx_clone) { - if (root != NULL) - { - fz_free_outline(ctx_clone, root); - } + fz_free_outline(ctx_clone, root); } fz_catch(ctx_clone) { @@ -315,22 +316,10 @@ int muctx::GetTextSearch(int page_num, char* needle, sh_vector_text texts_vec) } fz_always(ctx_clone) { - if (page != NULL) - { - fz_free_page(mu_doc, page); - } - if (dev != NULL) - { - fz_free_device(dev); - } - if (sheet != NULL) - { - fz_free_text_sheet(ctx_clone, sheet); - } - if (text != NULL) - { - fz_free_text_page(ctx_clone, text); - } + fz_free_page(mu_doc, page); + fz_free_device(dev); + fz_free_text_sheet(ctx_clone, sheet); + fz_free_text_page(ctx_clone, text); } fz_catch(ctx_clone) { @@ -403,14 +392,8 @@ int muctx::GetLinks(int page_num, sh_vector_link links_vec) } fz_always(ctx_clone) { - if (page != NULL) - { - fz_free_page(mu_doc, page); - } - if (links != NULL) - { - fz_drop_link(ctx_clone, links); - } + fz_free_page(mu_doc, page); + fz_drop_link(ctx_clone, links); } fz_catch(ctx_clone) { @@ -422,7 +405,7 @@ int muctx::GetLinks(int page_num, sh_vector_link links_vec) } /* Render page_num to size width by height into bmp_data buffer */ -HRESULT muctx::RenderPage(int page_num, int width, int height, +status_t muctx::RenderPage(int page_num, int width, int height, unsigned char *bmp_data) { fz_device *dev = NULL; @@ -454,27 +437,18 @@ HRESULT muctx::RenderPage(int page_num, int width, int height, } fz_always(ctx_clone) { - if (dev != NULL) - { - fz_free_device(dev); - } - if (pix != NULL) - { - fz_drop_pixmap(ctx_clone, pix); - } - if (page != NULL) - { - fz_free_page(mu_doc, page); - } + fz_free_device(dev); + fz_drop_pixmap(ctx_clone, pix); + fz_free_page(mu_doc, page); } fz_catch(ctx_clone) { fz_free_context(ctx_clone); - return E_FAIL; + return E_FAILURE; } fz_free_context(ctx_clone); - return S_OK; + return S_ISOK; } bool muctx::RequiresPassword(void) @@ -522,26 +496,11 @@ String^ muctx::GetHTML(int page_num) } fz_always(ctx_clone) { - if (dev != NULL) - { - fz_free_device(dev); - } - if (page != NULL) - { - fz_free_page(mu_doc, page); - } - if (sheet != NULL) - { - fz_free_text_sheet(ctx_clone, sheet); - } - if (text != NULL) - { - fz_free_text_page(ctx_clone, text); - } - if (buf != NULL) - { - fz_drop_buffer(ctx_clone, buf); - } + fz_free_device(dev); + fz_free_page(mu_doc, page); + fz_free_text_sheet(ctx_clone, sheet); + fz_free_text_page(ctx_clone, text); + fz_drop_buffer(ctx_clone, buf); } fz_catch(ctx_clone) { diff --git a/winrt/mupdfwinrt/muctx.h b/winrt/mupdfwinrt/muctx.h index 2175884a..d35718d9 100644 --- a/winrt/mupdfwinrt/muctx.h +++ b/winrt/mupdfwinrt/muctx.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "utils.h" @@ -15,6 +14,13 @@ extern "C" { #include "mupdf.h" } +typedef enum { + S_ISOK = 0, + E_FAILURE = 1, + E_OUTOFMEM = 2, + E_NEEDPASSWORD +} status_t; + #define MAX_SEARCH 500 using namespace Platform; /* For String */ @@ -76,7 +82,6 @@ private: fz_document *mu_doc; fz_outline *mu_outline; fz_rect mu_hit_bbox[MAX_SEARCH]; - fz_stream *mu_stream; void FlattenOutline(fz_outline *outline, int level, sh_vector_content contents_vec); @@ -84,10 +89,10 @@ public: muctx(void); ~muctx(void); void CleanUp(void); - HRESULT InitializeStream(IRandomAccessStream^ readStream, char *ext); + status_t InitializeStream(IRandomAccessStream^ readStream, char *ext); int GetPageCount(); - HRESULT InitializeContext(); - HRESULT RenderPage(int page_num, int width, int height, unsigned char *bmp_data); + status_t InitializeContext(); + status_t RenderPage(int page_num, int width, int height, unsigned char *bmp_data); Point MeasurePage(int page_num); Point MeasurePage(fz_page *page); int GetLinks(int page_num, sh_vector_link links_vec); diff --git a/winrt/mupdfwinrt/mudocument.cpp b/winrt/mupdfwinrt/mudocument.cpp index e05ee846..fb227412 100644 --- a/winrt/mupdfwinrt/mudocument.cpp +++ b/winrt/mupdfwinrt/mudocument.cpp @@ -46,7 +46,7 @@ Point mudocument::GetPageSize(int page_num) return this->mu_object.MeasurePage(page_num); } -Windows::Foundation::IAsyncAction^ mudocument::OpenFileAsync(StorageFile^ file) +Windows::Foundation::IAsyncOperation^ mudocument::OpenFileAsync(StorageFile^ file) { return create_async([this, file]() { @@ -69,13 +69,15 @@ Windows::Foundation::IAsyncAction^ mudocument::OpenFileAsync(StorageFile^ file) if (size <= MAXUINT32) { - HRESULT code = this->mu_object.InitializeStream(readStream, ext); - return; + status_t code = this->mu_object.InitializeStream(readStream, ext); + if (code != S_ISOK) + delete readStream; + return (int) code; } else { delete readStream; - return; + return (int) E_FAILURE; } } catch(COMException^ ex) { @@ -178,8 +180,8 @@ Windows::Foundation::IAsyncOperation^ std::lock_guard lock(mutex_lock); /* Get raster bitmap stream */ - HRESULT code = mu_object.RenderPage(page_num, width, height, &(bmp_data[0])); - if (code != S_OK) + status_t code = mu_object.RenderPage(page_num, width, height, &(bmp_data[0])); + if (code != S_ISOK) { throw ref new FailureException("Page Rendering Failed"); } diff --git a/winrt/mupdfwinrt/mudocument.h b/winrt/mupdfwinrt/mudocument.h index 9f4911f5..f452f489 100644 --- a/winrt/mupdfwinrt/mudocument.h +++ b/winrt/mupdfwinrt/mudocument.h @@ -33,7 +33,7 @@ namespace mupdfwinrt public: mudocument(); void CleanUp(); - Windows::Foundation::IAsyncAction^ OpenFileAsync(StorageFile^ file); + Windows::Foundation::IAsyncOperation^ OpenFileAsync(StorageFile^ file); int GetNumPages(void); Point GetPageSize(int page_num); Windows::Foundation::IAsyncOperation^ diff --git a/winrt/mupdfwinrt/mupdfwinrt.vcxproj b/winrt/mupdfwinrt/mupdfwinrt.vcxproj index 891c3a15..322f0d5f 100644 --- a/winrt/mupdfwinrt/mupdfwinrt.vcxproj +++ b/winrt/mupdfwinrt/mupdfwinrt.vcxproj @@ -152,6 +152,7 @@ $(IntDir)pch.pch $(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories) /bigobj %(AdditionalOptions) + ../../xps/;../../pdf/;../../fitz/;%(AdditionalIncludeDirectories) Console @@ -182,6 +183,7 @@ $(IntDir)pch.pch $(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories) /bigobj %(AdditionalOptions) + ../../xps/;../../pdf/;../../fitz/;%(AdditionalIncludeDirectories) Console -- cgit v1.2.3