text_view.h

Go to the documentation of this file.
00001 /*
00002  * File Name: text_view.h
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-plaintext.
00007  *
00008  * uds-plugin-plaintext 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-plaintext 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 #ifndef TEXT_VIEW_H
00028 #define TEXT_VIEW_H
00029 
00030 #include <string>
00031 #include <pango/pango.h>
00032 #include <pango/pangoft2.h>
00033 
00034 #include <ft2build.h>
00035 #include FT_FREETYPE_H
00036 #include FT_GLYPH_H
00037 
00038 #include "plugin_render_result.h"
00039 
00040 #include "thread.h"
00041 #include "signal_slot.h"
00042 #include "text_base_types.h"
00043 #include "text_model.h"
00044 #include "text_controller.h"
00045 
00046 namespace text
00047 {
00048 
00049 class PaginationTask;
00050 
00051 class TextView
00052 {
00053 public:
00054     /// @brief Constructors and destructors
00055     TextView(TextModel *m);
00056     ~TextView();
00057 
00058 public:
00059     /// @brief Initialize pango related stuff
00060     void initialize();
00061 
00062     /// @brief Release pango related stuff
00063     void deinitialize();
00064 
00065     void set_controller(TextController* c)
00066     {
00067         ctrl = c;
00068     }
00069 
00070     TextController* get_controller()
00071     {
00072         return ctrl;
00073     }
00074 
00075     /// @brief Pagination
00076     /// @param start_pos If page table is empty, then this parameter tells
00077     ///  the view from which position the pagination starts.
00078     /// @return True if pagination is complete. else false is returned.
00079     bool paginate(const Position& start_pos);
00080 
00081     /// @brief Reset the page table.
00082     void clear_pages()
00083     {
00084         pages.clear();
00085     }
00086 
00087     /// @brief Render a page start with start_pos.
00088     /// @return The position where render ends.
00089     Position render(unsigned char *bmp, const Position& start_pos);
00090 
00091     const TextModel* get_model() const
00092     {
00093         return model;
00094     }
00095 
00096     /// @brief Get font size
00097     int get_font_size() const
00098     {
00099         return font_size;
00100     }
00101 
00102     /// @brief Set font size
00103     bool set_font_size(int font_size);
00104 
00105     /// @brief Get font family
00106     const std::string& get_font_family() const
00107     {
00108         return font_family;
00109     }
00110 
00111     /// @brief Set font family
00112     void set_font_family(const std::string& font_family)
00113     {
00114         this->font_family = font_family;
00115     }
00116 
00117     /// @brief Get output display size
00118     void get_display_size(int& width, int& height)
00119     {
00120         width  = display_width;
00121         height = display_height;
00122     }
00123 
00124     /// @brief Set output display size
00125     void set_display_size(unsigned int width, unsigned int height);
00126 
00127     /// @brief Get dpi settings
00128     unsigned int get_DPI()
00129     {
00130         return dpi;
00131     }
00132 
00133     /// @brief Set dpi
00134     void set_DPI(unsigned int dpi)
00135     {
00136         this->dpi = dpi;
00137     }
00138 
00139     /// @brief Get color depth
00140     unsigned int get_color_depth()
00141     {
00142         return color_depth;        
00143     }
00144 
00145     /// @brief Set color depth
00146     void set_color_depth(unsigned int color_depth)
00147     {
00148         this->color_depth = color_depth;
00149     }
00150 
00151     int get_page_count()
00152     {
00153         if (!pages.empty() && pages.back().end == Position(0, 0))
00154         {
00155             return static_cast<int>(pages.size());
00156         }
00157         return 0;
00158     }
00159 
00160     /// @brief Check if re-pagination is needed given by anchor to be rendered.
00161     void check_page_table(const Position& rendering_pos);
00162 
00163     /// @brief Get start anchor of specified page.
00164     bool get_anchor_by_page(unsigned int page_index, Position& pos);
00165 
00166     /// @brief Get page index given by anchor.
00167     /// @return The page index of the specified anchor.
00168     ///  return 0 if page table is not ready.
00169     unsigned int get_page_index_by_anchor(const Position& anchor);
00170 
00171     /// @brief Get the start anchor of page which contains the specified anchor.
00172     Position get_page_anchor_by_anchor(const Position& anchor);
00173 
00174     /// @brief Get current page index.
00175     /// @return The index of page which has just been rendered.
00176     ///  return 1 if page table is not ready.
00177     unsigned int get_current_page_index();
00178 
00179     /// @brief Find view position given by document position (anchor).
00180     /// @param page_anchor Start anchor of current page.
00181     /// @param doc_pos Input anchor.
00182     /// @param view_pos Output view position (x,y).
00183     /// @return True if successfully mapped, otherwise false is returned.
00184     bool map_doc_pos_to_view_pos(const Position& page_anchor,
00185                                  const Position& doc_pos,
00186                                  ViewPosition&   view_pos,
00187                                  bool            trailing);
00188 
00189     /// @brief Find doc position (anchor) given by view position.
00190     /// @param page_anchor Start anchor of current page.
00191     /// @param view_pos Input view position.
00192     /// @param doc_pos Output anchor.
00193     /// @return True if successfully mapped, otherwise false is returned.
00194     bool map_view_pos_to_doc_pos(const Position&     page_anchor,
00195                                  const ViewPosition& view_pos,
00196                                  Position&           doc_pos);
00197 
00198     /// @brief Get bounding rectangels given by range.
00199     bool get_bounding_rectangles(const Position& page_anchor,
00200                                  std::vector<Rect>& bounding_rect,
00201                                  const Range& range);
00202 
00203     /// @brief Calculate start position of next page
00204     /// @param in_pos start position of current page
00205     /// @param out_pos start position of next page
00206     /// @return If no next page, false is returned, otherwise return true.
00207     bool calculate_next_page_pos(const Position& in_pos, Position& out_pos);
00208 
00209     /// @brief Calculate start position of previous page
00210     /// @param in_pos start position of current page
00211     /// @param out_pos start position of previous page
00212     /// @return If no previous page, false is returned, otherwise return true.
00213     bool calculate_prev_page_pos(const Position& in_pos, Position& out_pos);
00214 
00215     /// @brief Check if repagination is needed.
00216     bool need_repagination()
00217     {
00218         return is_repagination_needed;
00219     }
00220 
00221 public:
00222     /// @brief signals.
00223     utils::Signal<unsigned int>               pagination_start_signal;
00224     utils::Signal<unsigned int, unsigned int> pagination_end_signal;
00225     utils::Signal<unsigned int,
00226                   const Position&,
00227                   const Position&,
00228                   void  *> render_done_signal;
00229 
00230 private:
00231     /// @brief Initialize pango stuff
00232     void pango_context_init();
00233 
00234     /// @brief Finalize pango stuff
00235     void pango_context_final();
00236 
00237     /// @brief Calculate lines per page.
00238     void calculate_lines_per_page();
00239 
00240     /// @brief Create a layout with current settings, fill it with specified
00241     ///  string.
00242     PangoLayout* create_layout(const char *str, int len);
00243 
00244 private:
00245     /// @brief Text model.
00246     TextModel *model;
00247 
00248     /// @brief Text controller.
00249     TextController *ctrl;
00250 
00251     /// @brief Font family
00252     std::string font_family;
00253 
00254     /// @brief Font size.
00255     int font_size;
00256 
00257     /// @brief Output display width
00258     unsigned int display_width;
00259 
00260     /// @brief Output display height
00261     unsigned int display_height;
00262 
00263     /// @brief dpi
00264     unsigned int dpi;
00265 
00266     /// @brief Color depth
00267     unsigned int color_depth;
00268 
00269     /// @brief Line height
00270     unsigned int line_height;
00271 
00272     /// @brief Lines per page
00273     unsigned int lines_per_page;
00274 
00275     /// @brief Left margin
00276     unsigned int left_margin;
00277 
00278     /// @brief Right margin
00279     unsigned int right_margin;
00280 
00281     /// @brief Top margin
00282     unsigned int top_margin;
00283 
00284     /// @brief Bottom margin
00285     unsigned int bottom_margin;
00286 
00287     /// @brief Client width
00288     unsigned int client_width;
00289 
00290     /// @brief Client height
00291     unsigned int client_height;
00292 
00293     /// @brief The document position which is just being rendered.
00294     Position rendering_pos;
00295 
00296     /// @brief Page table definition 
00297     typedef SafeDeque<PageInfo> PageTable;
00298     PageTable pages;
00299 
00300     /// @brief Pango stuff
00301     PangoContext         *pango_context;
00302 
00303     /// @brief Mutex to ensure each time only 1 thread can access pango stuff.
00304     GMutex               *pango_mutex;
00305     unsigned int         font_hash_code;
00306 
00307     /// @brief A flag indicating re-pagination is needed.
00308     bool is_repagination_needed;
00309 
00310     /// @brief Number of blank lines in the first page.
00311     unsigned int blank_lines;
00312 };
00313 
00314 }; // namespace text
00315 
00316 #endif // TEXT_VIEW_H
00317 
00318 
Generated by  doxygen 1.6.2-20100208