pdf/plugin_impl/render_result_impl.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: render_result_impl.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-pdf.
00007  *
00008  * uds-plugin-pdf is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-pdf is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include "render_result_impl.h"
00028 #include "render_settings_impl.h"
00029 #include "collection_impl.h"
00030 
00031 #include "pdf_renderer.h"
00032 #include "pdf_doc_controller.h"
00033 #include "pdf_page.h"
00034 
00035 namespace pdf
00036 {
00037 
00038 utils::ObjectTable<PluginRenderResultImpl> PluginRenderResultImpl::g_instances_table;
00039 
00040 PluginRenderResultImpl::PluginRenderResultImpl(const unsigned int page_num, const unsigned int id)
00041 : page(0)
00042 , page_number(page_num)
00043 , ref_id(id)
00044 , discard(false)
00045 {
00046     // IPluginUnknown
00047     query_interface = query_interface_impl;
00048     release = release_impl;
00049 
00050     // IPluginRenderResult
00051     get_bitmap_attributes = get_bitmap_attributes_impl;
00052     get_anchor_from_coordinates = get_anchor_from_coordinates_impl;
00053     get_bounding_rectangles_from_range = get_bounding_rectangles_from_range_impl;
00054     get_rendered_range = get_rendered_range_impl;
00055 
00056     // IPluginRenderSettings, no method yet.
00057 
00058     // IPluginZoom
00059     set_zoom_factor = set_zoom_factor_impl;
00060     get_zoom_factor = get_zoom_factor_impl;
00061 
00062     // IPluginRotation
00063     set_rotation = set_rotation_impl;
00064     get_rotation = get_rotation_impl;
00065 
00066     // Initialize all supported interfaces. In render result, 
00067     // the render settings, zoom and rotation are read only.
00068     g_instances_table.add_interface<IPluginUnknown>(this);
00069     g_instances_table.add_interface<IPluginRenderResult>(this);
00070     g_instances_table.add_interface<IPluginRenderSettings>(this);
00071     g_instances_table.add_interface<IPluginZoom>(this);
00072     g_instances_table.add_interface<IPluginRotation>(this);
00073 
00074 }
00075 
00076 void PluginRenderResultImpl::set_page(PagePtr ptr)
00077 {
00078     if (ptr != 0)
00079     {
00080         page = ptr;
00081         page_number = static_cast<unsigned int>(page->get_page_num());
00082         ref_id = page->get_ref_id();
00083         page->lock();
00084     }
00085 }
00086 
00087 PluginRenderResultImpl::~PluginRenderResultImpl(void)
00088 {
00089     // unlock the pdf page but not delete it
00090     // the page would be deleted by cache manager
00091     if (page != 0)
00092     {
00093         page->unlock();
00094     }
00095 
00096     g_instances_table.remove(this);
00097 }
00098 
00099 PluginStatus
00100 PluginRenderResultImpl::query_interface_impl(IPluginUnknown    *thiz,
00101                                              const UDSString   *id, 
00102                                              void              **ptr )
00103 {
00104     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00105     if (g_instances_table.query_interface(instance, id->get_buffer(id), ptr))
00106     {
00107         return PLUGIN_OK;
00108     }
00109     return PLUGIN_FAIL;
00110 }
00111 
00112 int 
00113 PluginRenderResultImpl::release_impl(IPluginUnknown  *thiz )
00114 {
00115     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00116     instance->release_signal.safe_broadcast(instance);
00117     return 0;
00118 }
00119 
00120 PluginStatus 
00121 PluginRenderResultImpl::get_bitmap_attributes_impl(IPluginUnknown         *thiz, 
00122                                                    PluginBitmapAttributes *attributes)
00123 {
00124     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00125 
00126     if (instance->page == 0)
00127     {
00128         return PLUGIN_FAIL;
00129     }
00130 
00131     attributes->width = instance->page->get_bitmap_width();
00132     attributes->height = instance->page->get_bitmap_height();
00133     attributes->row_stride = instance->page->get_bitmap_row_stride();
00134     attributes->data = instance->page->get_bitmap_data();
00135 
00136     return PLUGIN_OK;
00137 }
00138 
00139 PluginStatus 
00140 PluginRenderResultImpl::get_anchor_from_coordinates_impl(IPluginUnknown *thiz,
00141                                                  const int      x,
00142                                                  const int      y,
00143                                                  UDSString      *anchor )
00144 {
00145 
00146     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00147     
00148     if (instance->page == 0)
00149     {
00150         return PLUGIN_FAIL;
00151     }
00152 
00153     double ux, uy;
00154 
00155     instance->page->coordinates_dev_to_user(x, y, &ux, &uy);
00156 
00157     PDFAnchor param;
00158     instance->page->get_anchor_param_from_coordinates(ux, uy, param);
00159     //param.file_name = instance->page->get_doc_controller()->name();
00160     anchor->assign(anchor, param.get_string().c_str());
00161 
00162     return PLUGIN_OK;
00163 }
00164 
00165 IPluginUnknown* 
00166 PluginRenderResultImpl::get_bounding_rectangles_from_range_impl(IPluginUnknown      *thiz,
00167                                                                 const PluginRange   *range)
00168 {
00169     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00170 
00171     if (instance->page == 0)
00172     {
00173         return 0;
00174     }
00175 
00176     PDFRectangles *rectangles = new PDFRectangles;
00177 
00178     // construct a start/end string
00179     string start(range->start_anchor->get_buffer(range->start_anchor));
00180     string end(range->end_anchor->get_buffer(range->end_anchor));
00181     if (instance->page->get_bounding_rectangles(start, end, *rectangles))
00182     {
00183         PluginCollectionImpl *collection = new PluginCollectionImpl;
00184 
00185         collection->set_data(rectangles);
00186 
00187         return static_cast<IPluginUnknown *>(collection);
00188     }
00189 
00190     return 0;
00191 }
00192 
00193 float 
00194 PluginRenderResultImpl::get_zoom_factor_impl(IPluginUnknown* thiz )
00195 {
00196     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00197 
00198     if (instance->page == 0)
00199     {
00200         return static_cast<float>(PLUGIN_ZOOM_INVALID);
00201     }
00202 
00203     return static_cast<float>(instance->page->get_render_attr().get_zoom_setting());
00204 }
00205 
00206 PluginRotationDegree 
00207 PluginRenderResultImpl::get_rotation_impl(IPluginUnknown  *thiz )
00208 {
00209     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00210 
00211     if (instance->page == 0)
00212     {
00213         return Clockwise_Degrees_0;
00214     }
00215     
00216     return static_cast<PluginRotationDegree>(
00217         instance->page->get_render_attr().get_rotate());
00218 }
00219 
00220 PluginStatus
00221 PluginRenderResultImpl::get_rendered_range_impl(IPluginUnknown  *thiz,
00222                                                 PluginRange     *range)
00223 {
00224     // return the entire page now
00225     // TODO. return the accurate range in the future
00226     PluginRenderResultImpl *instance = g_instances_table.get_object(thiz);
00227 
00228     if (instance->page == 0)
00229     {
00230         return PLUGIN_FAIL;
00231     }
00232 
00233     int page_num = instance->page->get_page_num();
00234 
00235     PDFAnchor start_anchor;
00236     start_anchor.page_num = page_num;
00237     //start_anchor.file_name = instance->page->get_doc_controller()->name();
00238 
00239     PDFAnchor end_anchor;
00240     //end_anchor.file_name = instance->page->get_doc_controller()->name();
00241     end_anchor.set_end_anchor();
00242 
00243     range->start_anchor->assign( range->start_anchor, start_anchor.get_string().c_str() );
00244     range->end_anchor->assign( range->end_anchor, end_anchor.get_string().c_str() );
00245 
00246     return PLUGIN_OK;
00247 }
00248 
00249 // Not support
00250 PluginStatus
00251 PluginRenderResultImpl::set_zoom_factor_impl(IPluginUnknown* thiz,
00252                                              const float zoom_factor )
00253 {
00254     return PLUGIN_FAIL;
00255 }
00256 
00257 PluginStatus 
00258 PluginRenderResultImpl::set_rotation_impl(IPluginUnknown               *thiz,
00259                                           const PluginRotationDegree   rotation )
00260 {
00261     return PLUGIN_FAIL;
00262 }
00263 
00264 } // namespace pdf
00265 
Generated by  doxygen 1.6.2-20100208