p_text.cpp

Go to the documentation of this file.
00001 /*
00002  * p_text.cpp
00003  *
00004  * Copyright (C) 2007  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Implementation of a text primitive.
00032  * (p_ stands for Primitive)
00033  */
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "vgfx.h"             // all primitives should include this header first
00037 
00038 #include "drawer.h"
00039 
00040 #include "util/parsing.h"
00041 
00042 
00043 namespace vgfx {
00044 
00045 ////////////////////////////////////////////////////////////////////////////////
00046 //
00047 //      static helper methods
00048 //
00049 ////////////////////////////////////////////////////////////////////////////////
00050 
00051 class Text : public Primitive {
00052 public:
00053         ~Text(void) throw() { }
00054 
00055         // public class methods ------------------------------------------------
00056         void initialize(IN const init_text_t& init);
00057 
00058         // vgfx::Primitive class interface methods -----------------------------
00059         const char * getType(void) const throw() { return "text"; }
00060         void persist(IO std::ostream& stream) const;
00061         void recalcBoundingRect(IN const char * tag_path,
00062                                 IN Drawer * drawer,
00063                                 IN const xform_2d_t& T);
00064         bool getBoundingRect(OUT rect_t& r) const throw();
00065         void draw(IN Drawer * drawer,
00066                                 IN const rect_t& r_cm,
00067                                 IN const xform_2d_t& T);
00068 
00069 private:
00070         std::string     m_text;
00071         rect_t          m_rect;         // bounding rect
00072 };
00073 
00074 
00075 
00076 ////////////////////////////////////////////////////////////////////////////////
00077 //
00078 //      Text -- public class methods
00079 //
00080 ////////////////////////////////////////////////////////////////////////////////
00081 
00082 void
00083 Text::initialize
00084 (
00085 IN const init_text_t& init
00086 )
00087 {
00088         this->setID(init.id.c_str());
00089         m_text = init.text;
00090 
00091 //      DPRINTF("  New text primitive: '%s'", m_text.c_str());
00092 }
00093 
00094 
00095 
00096 ////////////////////////////////////////////////////////////////////////////////
00097 //
00098 //      Text -- vgfx::Primitive class interface methods
00099 //
00100 ////////////////////////////////////////////////////////////////////////////////
00101 
00102 void
00103 Text::draw
00104 (
00105 IN Drawer * drawer,
00106 IN const rect_t& r_cm,
00107 IN const xform_2d_t& T
00108 )
00109 {
00110         ASSERT(drawer, "null drawer");
00111 
00112         drawer->setTransform(T);
00113         drawer->drawText(m_rect, m_text.c_str());
00114 }
00115 
00116 
00117 
00118 void
00119 Text::persist
00120 (
00121 OUT std::ostream& out
00122 )
00123 const
00124 {
00125         out << "text id " << this->getID() << " text \"" << m_text << "\"";
00126 }
00127 
00128 
00129 
00130 void
00131 Text::recalcBoundingRect
00132 (
00133 IN const char * tag_path,
00134 IN Drawer * drawer,
00135 IN const xform_2d_t& T
00136 )
00137 {
00138         ASSERT(drawer, "null drawer");
00139 
00140         // get bounding rect from drawer (it knows text details)
00141         drawer->setTransform(T);
00142         drawer->getBoundingRect(m_text.c_str(), m_rect);
00143         m_rect.expand(0.05);    // pen width etc
00144 }
00145 
00146 
00147 
00148 bool
00149 Text::getBoundingRect
00150 (
00151 OUT rect_t& r
00152 )
00153 const throw()
00154 {
00155         r = m_rect;
00156         return true;
00157 }
00158 
00159 
00160 
00161 ////////////////////////////////////////////////////////////////////////////////
00162 //
00163 //      public API
00164 //
00165 ////////////////////////////////////////////////////////////////////////////////
00166 
00167 void
00168 init_text_from_data
00169 (
00170 IN const dictionary_t& data,
00171 OUT init_text_t& init
00172 )
00173 {
00174         init.id = getRequiredValue(data, "id");
00175         init.text = getRequiredValue(data, "text");
00176 }
00177 
00178 
00179 
00180 smart_ptr<Primitive>
00181 create_text
00182 (
00183 IN const init_text_t& init
00184 )
00185 {
00186         smart_ptr<Text> local = new Text;
00187         ASSERT(local, "out of memory");
00188 
00189         local->initialize(init);
00190 
00191         return local;
00192 }
00193 
00194 
00195 };      // vgfx namespace
00196