home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWTxtShp.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  13.3 KB  |  466 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWTxtShp.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWTXTSHP_H
  13. #include "FWTxtShp.h"
  14. #endif
  15.  
  16. #ifndef SLRENDER_H
  17. #include "SLRender.h"
  18. #endif
  19.  
  20. #ifndef FWCHARIT_H
  21. #include "FWCharIt.h"
  22. #endif
  23.  
  24. // ----- Foundation Includes -----
  25.  
  26. #ifndef FWSTREAM_H
  27. #include "FWStream.h"
  28. #endif
  29.  
  30. // ----- OpenDoc Includes -----
  31.  
  32. #ifndef _TRANSFORM_
  33. #include <Trnsform.xh>
  34. #endif
  35.  
  36. //========================================================================================
  37. // File scope definitions
  38. //========================================================================================
  39.  
  40. #ifdef FW_BUILD_MAC
  41. #pragma segment FWGraphics_BaseTextShape
  42. #endif
  43.  
  44. //========================================================================================
  45. //    class FW_CTextShape
  46. //========================================================================================
  47.  
  48. FW_DEFINE_AUTO(FW_CTextShape)
  49. FW_DEFINE_CLASS_M1(FW_CTextShape, FW_CBaseTextShape)
  50.  
  51. // This class is archivable, but we provide the archiving implementation in a separate
  52. // translation unit in order to enable deadstripping of the archiving-related code
  53. // in parts that do not use archiving with this class.
  54.  
  55. //----------------------------------------------------------------------------------------
  56. //    FW_CTextShape::FW_CTextShape
  57. //----------------------------------------------------------------------------------------
  58.  
  59. FW_CTextShape::FW_CTextShape(const FW_CTextShape& other) :
  60.     FW_CBaseTextShape(other),
  61.     fPosition(other.fPosition),
  62.     fTextAlignment(other.fTextAlignment)
  63. {
  64.     FW_END_CONSTRUCTOR
  65. }
  66.  
  67. //----------------------------------------------------------------------------------------
  68. //    FW_CTextShape::FW_CTextShape
  69. //----------------------------------------------------------------------------------------
  70.  
  71. FW_CTextShape::FW_CTextShape(const FW_CString& string, 
  72.                             FW_Fixed xPos,
  73.                             FW_Fixed yPos,
  74.                             const FW_CFont& font,
  75.                             FW_TextAlignment textAlignment,
  76.                             const FW_CInk& ink) :
  77.     FW_CBaseTextShape(string, ink, font),
  78.     fPosition(xPos, yPos),
  79.     fTextAlignment(textAlignment)
  80. {
  81.     FW_END_CONSTRUCTOR
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------
  85. //    FW_CTextShape::FW_CTextShape
  86. //----------------------------------------------------------------------------------------
  87.  
  88. FW_CTextShape::FW_CTextShape() :
  89.     FW_CBaseTextShape(),
  90.     fTextAlignment(kDefaultAlignment)
  91.         // fPosition is set to 0's by the default constructor
  92. {
  93.     FW_END_CONSTRUCTOR
  94. }
  95.  
  96. //----------------------------------------------------------------------------------------
  97. //    FW_CTextShape::FW_CTextShape
  98. //----------------------------------------------------------------------------------------
  99.  
  100. FW_CTextShape::FW_CTextShape(FW_CTextReader& textReader, 
  101.                             FW_Fixed xPos, FW_Fixed yPos,
  102.                             const FW_CFont& font,
  103.                             FW_TextAlignment textAlignment,
  104.                             const FW_CInk& ink) :
  105.     FW_CBaseTextShape(textReader, ink, font),
  106.     fPosition(xPos, yPos),
  107.     fTextAlignment(textAlignment)
  108. {
  109.     FW_END_CONSTRUCTOR
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. //    FW_CTextShape::FW_CTextShape
  114. //----------------------------------------------------------------------------------------
  115.  
  116. FW_CTextShape::FW_CTextShape(FW_CReadableStream& stream) :
  117.     FW_CBaseTextShape(stream)
  118. {
  119.     stream >> fPosition;
  120.     stream >> fTextAlignment;
  121.  
  122.     FW_END_CONSTRUCTOR
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. //    FW_CTextShape::~FW_CTextShape
  127. //----------------------------------------------------------------------------------------
  128.  
  129. FW_CTextShape::~FW_CTextShape()
  130. {
  131.     FW_START_DESTRUCTOR
  132. }
  133.  
  134. //----------------------------------------------------------------------------------------
  135. //    FW_CTextShape::operator=
  136. //----------------------------------------------------------------------------------------
  137.  
  138. FW_CTextShape& FW_CTextShape::operator=(const FW_CTextShape& other)
  139. {
  140.     if (this != &other)
  141.     {
  142.         FW_CBaseTextShape::operator=(other);
  143.         
  144.         fPosition = other.fPosition;
  145.         fTextAlignment = other.fTextAlignment;
  146.     }
  147.     
  148.     return *this;
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. //    FW_CTextShape::Render
  153. //----------------------------------------------------------------------------------------
  154.  
  155. void FW_CTextShape::Render(FW_CGraphicContext& gc) const
  156. {    
  157.     FW_PrivRenderTextString(gc.GetEnvironment(),
  158.         gc,
  159.         fString,
  160.         fPosition,
  161.         fTextAlignment,
  162.         GetRenderVerb(),
  163.         fInk,
  164.         fFont);
  165.     FW_FailOnEvError(gc.GetEnvironment());
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. //    FW_CTextShape::RenderText
  170. //----------------------------------------------------------------------------------------
  171.  
  172. void FW_CTextShape::RenderText(FW_CGraphicContext& gc,
  173.                                   FW_CTextReader& textReader,
  174.                                   const FW_CPoint& position,
  175.                                   const FW_CFont& font,
  176.                                  FW_TextAlignment textAlignment,
  177.                                   const FW_CInk& ink)
  178. {    
  179.     FW_PrivRenderTextReader(gc.GetEnvironment(),
  180.         gc,
  181.         textReader,
  182.         position,
  183.         textAlignment,
  184.         FW_kFill,
  185.         ink,
  186.         font);
  187.     FW_FailOnEvError(gc.GetEnvironment());
  188. }
  189.  
  190. //----------------------------------------------------------------------------------------
  191. //    FW_CTextShape::RenderString
  192. //----------------------------------------------------------------------------------------
  193.  
  194. void FW_CTextShape::RenderText(FW_CGraphicContext& gc,
  195.                                   const FW_CString& string,
  196.                                   const FW_CPoint& position,
  197.                                   const FW_CFont& font,
  198.                                  FW_TextAlignment textAlignment,
  199.                                   const FW_CInk& ink)
  200. {    
  201.     FW_PrivRenderTextString(gc.GetEnvironment(),
  202.         gc,
  203.         string,
  204.         position,
  205.         textAlignment,
  206.         FW_kFill,
  207.         ink,
  208.         font);
  209.     FW_FailOnEvError(gc.GetEnvironment());
  210. }
  211.  
  212. //----------------------------------------------------------------------------------------
  213. //    FW_CTextShape::Transform
  214. //----------------------------------------------------------------------------------------
  215.  
  216. void FW_CTextShape::Transform(Environment* ev, ODTransform* odTransform)
  217. {
  218.     fPosition.Transform(ev, odTransform);
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. //    FW_CTextShape::InverseTransform
  223. //----------------------------------------------------------------------------------------
  224.  
  225. void FW_CTextShape::InverseTransform(Environment* ev, ODTransform* odTransform)
  226. {
  227.     fPosition.InverseTransform(ev, odTransform);
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. //    FW_CTextShape::Inset
  232. //----------------------------------------------------------------------------------------
  233.  
  234. void FW_CTextShape::Inset(FW_Fixed x, FW_Fixed y)
  235. {
  236. FW_UNUSED(x);
  237. FW_UNUSED(y);
  238.  
  239.     // Nothing to do
  240. }
  241.  
  242. //----------------------------------------------------------------------------------------
  243. //    FW_CTextShape::Copy
  244. //----------------------------------------------------------------------------------------
  245.  
  246. FW_CShape* FW_CTextShape::Copy() const
  247. {
  248.     return FW_NEW(FW_CTextShape, (*this));
  249. }
  250.  
  251. //----------------------------------------------------------------------------------------
  252. //    FW_CTextShape::MoveShape
  253. //----------------------------------------------------------------------------------------
  254.  
  255. void FW_CTextShape::MoveShape(FW_Fixed deltaX, FW_Fixed deltaY)
  256. {
  257.     fPosition.x += deltaX;
  258.     fPosition.y += deltaY;
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. //    FW_CTextShape::MoveShapeTo
  263. //----------------------------------------------------------------------------------------
  264.  
  265. void FW_CTextShape::MoveShapeTo(FW_Fixed x, FW_Fixed y)
  266. {
  267.     fPosition.x = x;
  268.     fPosition.y = y;
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. //    FW_CTextShape::CalcExtent
  273. //----------------------------------------------------------------------------------------
  274.  
  275. FW_CPoint FW_CTextShape::CalcExtent(FW_CGraphicContext& gc) const
  276. {
  277.     FW_CPoint textExtent;
  278.     FW_PrivCalcTextExtentString(gc.GetEnvironment(),
  279.         gc,
  280.         fString, 
  281.         fFont,
  282.         textExtent);
  283.     FW_FailOnEvError(gc.GetEnvironment());
  284.     return textExtent;
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. //    FW_CTextShape::CalcTextExtent
  289. //----------------------------------------------------------------------------------------
  290.  
  291. FW_CPoint FW_CTextShape::CalcTextExtent(FW_CGraphicContext& gc,
  292.                                       FW_CTextReader& textReader,
  293.                                     const FW_CFont& font)
  294. {
  295.     FW_CPoint textExtent;
  296.     FW_PrivCalcTextExtentReader(gc.GetEnvironment(),
  297.         gc,
  298.         textReader, 
  299.         font,
  300.         textExtent);
  301.     FW_FailOnEvError(gc.GetEnvironment());
  302.     return textExtent;
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    FW_CTextShape::CalcTextExtent
  307. //----------------------------------------------------------------------------------------
  308.  
  309. FW_CPoint FW_CTextShape::CalcTextExtent(FW_CGraphicContext& gc,
  310.                                       const FW_CString& string,
  311.                                       const FW_CFont& font)
  312. {
  313.     FW_CPoint textExtent;
  314.     FW_PrivCalcTextExtentString(gc.GetEnvironment(),
  315.         gc,
  316.         string, 
  317.         font,
  318.         textExtent);
  319.     FW_FailOnEvError(gc.GetEnvironment());
  320.     return textExtent;
  321. }
  322.  
  323. //----------------------------------------------------------------------------------------
  324. //    FW_CTextShape::GetBounds
  325. //----------------------------------------------------------------------------------------
  326.  
  327. void FW_CTextShape::GetBounds(FW_CGraphicContext& gc, FW_CRect& rect) const
  328. {
  329.     FW_CPlatformRect plfmRect;
  330.     
  331.     FW_CPlatformPoint position = gc.LogicalToDevice(fPosition);    
  332.     FW_CPoint textExtent;
  333.     FW_PrivCalcTextExtentString(gc.GetEnvironment(), gc, fString, fFont, textExtent);
  334.     FW_FailOnEvError(gc.GetEnvironment());
  335.     
  336.     FW_CPlatformPoint plfmExtent = gc.LogicalToDevice(textExtent);
  337.     
  338.     switch (fTextAlignment & FW_kPrivTextAlignHorzAlignMask)
  339.     {
  340.         case FW_kTextAlignLeft:
  341.             plfmRect.left = position.X();
  342.             plfmRect.right = plfmRect.left + plfmExtent.X();
  343.             break;
  344.             
  345.         case FW_kTextAlignRight:
  346.             plfmRect.right = position.X();
  347.             plfmRect.left = plfmRect.right - plfmExtent.X();
  348.             break;
  349.             
  350.         case FW_kTextAlignHCenter:
  351.             plfmRect.left = position.X() - (plfmExtent.X() / 2);
  352.             plfmRect.right = plfmRect.left + plfmExtent.X();
  353.             break;
  354.     }
  355.  
  356.     FW_CFontMetrics fontMetrics;
  357.     switch (fTextAlignment & FW_kPrivTextAlignVertAlignMask)
  358.     {
  359.         case FW_kTextAlignTop:
  360.             plfmRect.top = position.Y();
  361.             plfmRect.bottom = plfmRect.top + plfmExtent.Y();
  362.             break;
  363.             
  364.         case FW_kTextAlignBottom:
  365.             plfmRect.bottom = position.Y();
  366.             plfmRect.top = plfmRect.bottom - plfmExtent.Y();
  367.             break;
  368.             
  369.         case FW_kTextAlignVCenter:
  370.             fFont.GetFontMetrics(gc.GetGraphicDevice(), fontMetrics);        
  371.             plfmRect.top = position.Y() - (fontMetrics.GetFontHeight() / 2);
  372.             plfmRect.bottom = plfmRect.top + fontMetrics.GetFontHeight();        
  373.             break;
  374.             
  375.         case FW_kTextAlignBaseLine:
  376.             fFont.GetFontMetrics(gc.GetGraphicDevice(), fontMetrics);        
  377.             plfmRect.top = position.Y() - fontMetrics.GetAscent();
  378.             plfmRect.bottom = plfmRect.top + fontMetrics.GetFontHeight();        
  379.             break;
  380.     }
  381.         
  382.     rect = gc.DeviceToLogical(plfmRect);
  383. }
  384.  
  385. //----------------------------------------------------------------------------------------
  386. //    FW_CTextShape::GetAnchorPoint
  387. //----------------------------------------------------------------------------------------
  388.  
  389. FW_CPoint FW_CTextShape::GetAnchorPoint() const
  390. {
  391.     return fPosition;
  392. }
  393.  
  394. //----------------------------------------------------------------------------------------
  395. //    FW_CTextShape::Flatten
  396. //----------------------------------------------------------------------------------------
  397.  
  398. void FW_CTextShape::Flatten(FW_CWritableStream& stream) const
  399. {
  400.     FW_CBaseTextShape::Flatten(stream);
  401.     
  402.     stream << fPosition;
  403.     stream << fTextAlignment;
  404. }
  405.  
  406. //----------------------------------------------------------------------------------------
  407. //    FW_CTextShape::GetBaseLine
  408. //----------------------------------------------------------------------------------------
  409.  
  410. void FW_CTextShape::GetBaseLine(FW_CGraphicContext& gc,
  411.                                 FW_CPoint& start, FW_CPoint& end) const
  412. {    
  413.     FW_CPlatformPoint pt1, pt2;
  414.  
  415.     FW_CPlatformPoint plfmPos = gc.LogicalToDevice(fPosition);
  416.     
  417.     FW_CFontMetrics fontMetrics;
  418.     fFont.GetFontMetrics(gc.GetGraphicDevice(), fontMetrics);        
  419.  
  420.     switch (fTextAlignment & FW_kPrivTextAlignVertAlignMask)
  421.     {
  422.         case FW_kTextAlignTop:
  423.             plfmPos.Y() += fontMetrics.GetAscent();
  424.             break;
  425.             
  426.         case FW_kTextAlignBottom:
  427.             plfmPos.Y() -= fontMetrics.GetDescent();
  428.             break;
  429.             
  430.         case FW_kTextAlignVCenter:
  431.             plfmPos.Y() += fontMetrics.GetAscent() - (fontMetrics.GetFontHeight() / 2);
  432.             break;
  433.             
  434.         case FW_kTextAlignBaseLine:
  435.             break;
  436.     }
  437.  
  438.     FW_CPoint textExtent;
  439.     FW_PrivCalcTextExtentString(gc.GetEnvironment(), gc, fString, fFont, textExtent);
  440.     FW_FailOnEvError(gc.GetEnvironment());
  441.         
  442.     FW_CPlatformPoint plfmExtent = gc.LogicalToDevice(textExtent);
  443.     
  444.     switch (fTextAlignment & FW_kPrivTextAlignHorzAlignMask)
  445.     {
  446.         case FW_kTextAlignLeft:
  447.             pt1 = plfmPos;
  448.             pt2.Set(pt1.X() + plfmExtent.X(), pt1.Y());
  449.             break;
  450.             
  451.         case FW_kTextAlignRight:
  452.             pt2 = plfmPos;
  453.             pt1.Set(pt2.X() - plfmExtent.X(), pt2.Y());
  454.             break;
  455.             
  456.         case FW_kTextAlignHCenter:
  457.             pt1.Set(plfmPos.X() - (plfmExtent.X() / 2), plfmPos.Y());
  458.             pt2.Set(pt1.X() + plfmExtent.X(), plfmPos.Y());
  459.             break;
  460.     }
  461.  
  462.     start = gc.DeviceToLogical(pt1);
  463.     end = gc.DeviceToLogical(pt2);
  464. }
  465.  
  466.