LineLayout.jvm.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // System.Drawing.Test.LineLayout.jvm.cs
  3. //
  4. // Author:
  5. // Konstantin Triger <[email protected]>
  6. //
  7. // Copyright (C) 2005 Mainsoft Corporation, (http://www.mainsoft.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Drawing.Drawing2D;
  30. using font = java.awt.font;
  31. using text = java.text;
  32. using awt = java.awt;
  33. using geom = java.awt.geom;
  34. namespace System.Drawing.Text {
  35. internal sealed class LineLayout {
  36. #region Fields
  37. readonly font.TextLayout _layout;
  38. readonly float _accumulatedHeight;
  39. readonly TextLineIterator _lineIter;
  40. #endregion
  41. #region ctor
  42. internal LineLayout(font.TextLayout layout,
  43. TextLineIterator lineIter,
  44. float accumulatedHeight) {
  45. _layout = layout;
  46. _lineIter = lineIter;
  47. _accumulatedHeight = accumulatedHeight;
  48. }
  49. #endregion
  50. #region Properties
  51. internal float AccumulatedHeight {
  52. get { return _accumulatedHeight; }
  53. }
  54. internal float MeasureWidth {
  55. get {
  56. return Width + (_lineIter.Margin*2);
  57. }
  58. }
  59. internal int CharacterCount {
  60. get { return _layout.getCharacterCount(); }
  61. }
  62. internal float Ascent {
  63. get { return _layout.getAscent(); }
  64. }
  65. internal float Descent {
  66. get { return _layout.getDescent(); }
  67. }
  68. public float Leading {
  69. get { return _layout.getLeading(); }
  70. }
  71. internal float NativeY {
  72. get {
  73. if (_lineIter.Format.IsVertical) {
  74. float height = _lineIter.Height;
  75. if (float.IsPositiveInfinity(height))
  76. height = 0;
  77. switch (_lineIter.Format.Alignment) {
  78. case StringAlignment.Center:
  79. return (height - Width) / 2;
  80. case StringAlignment.Far:
  81. return height - _layout.getVisibleAdvance() - _lineIter.Margin;
  82. default:
  83. return _lineIter.Margin;
  84. }
  85. }
  86. else
  87. return AccumulatedHeight + Ascent;
  88. }
  89. }
  90. internal float NativeX {
  91. get {
  92. float width = _lineIter.Width;
  93. if (float.IsPositiveInfinity(width))
  94. width = 0;
  95. if (_lineIter.Format.IsVertical)
  96. return (_lineIter.Format.IsRightToLeft) ?
  97. width - AccumulatedHeight - Ascent :
  98. AccumulatedHeight + Leading + Descent;
  99. else {
  100. float xOffset;
  101. switch ( _lineIter.Format.Alignment) {
  102. case StringAlignment.Center:
  103. xOffset = (width - Width) / 2;
  104. break;
  105. case StringAlignment.Far:
  106. if (_lineIter.Format.IsRightToLeft)
  107. xOffset = _lineIter.Margin;
  108. else
  109. xOffset = width - _layout.getVisibleAdvance() - _lineIter.Margin;
  110. break;
  111. default:
  112. if (_lineIter.Format.IsRightToLeft)
  113. xOffset = width - _layout.getVisibleAdvance() - _lineIter.Margin;
  114. else
  115. xOffset = _lineIter.Margin;
  116. break;
  117. }
  118. return xOffset;
  119. }
  120. }
  121. }
  122. internal float Height {
  123. get {
  124. return Ascent + Descent + Leading;
  125. }
  126. }
  127. internal float Width {
  128. get {
  129. if (_lineIter.Format.MeasureTrailingSpaces)
  130. if (!(_lineIter.Format.IsRightToLeft ^
  131. (_lineIter.Format.Alignment == StringAlignment.Far)))
  132. return _layout.getAdvance();
  133. return _layout.getVisibleAdvance();
  134. }
  135. }
  136. #endregion
  137. #region Methods
  138. internal void Draw(awt.Graphics2D g2d, float x, float y) {
  139. if (_lineIter.Format.IsVertical)
  140. _layout.draw(g2d, y + NativeY, -(x + NativeX) );
  141. else
  142. _layout.draw(g2d, x + NativeX, y + NativeY );
  143. }
  144. internal awt.Shape GetOutline(float x, float y) {
  145. geom.AffineTransform t = (geom.AffineTransform) _lineIter.Transform.MemberwiseClone ();
  146. if (_lineIter.Format.IsVertical)
  147. t.translate(y + NativeY, -(x + NativeX));
  148. else
  149. t.translate(x + NativeX, y + NativeY);
  150. return _layout.getOutline(t);
  151. }
  152. #endregion
  153. }
  154. }