SVGTextContentElement.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/SVGTextContentElement.h"
  6. #include "nsISVGPoint.h"
  7. #include "SVGTextFrame.h"
  8. #include "mozilla/dom/SVGIRect.h"
  9. namespace mozilla {
  10. namespace dom {
  11. nsSVGEnumMapping SVGTextContentElement::sLengthAdjustMap[] = {
  12. { &nsGkAtoms::spacing, SVG_LENGTHADJUST_SPACING },
  13. { &nsGkAtoms::spacingAndGlyphs, SVG_LENGTHADJUST_SPACINGANDGLYPHS },
  14. { nullptr, 0 }
  15. };
  16. nsSVGElement::EnumInfo SVGTextContentElement::sEnumInfo[1] =
  17. {
  18. { &nsGkAtoms::lengthAdjust, sLengthAdjustMap, SVG_LENGTHADJUST_SPACING }
  19. };
  20. nsSVGElement::LengthInfo SVGTextContentElement::sLengthInfo[1] =
  21. {
  22. { &nsGkAtoms::textLength, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::XY }
  23. };
  24. SVGTextFrame*
  25. SVGTextContentElement::GetSVGTextFrame()
  26. {
  27. nsIFrame* frame = GetPrimaryFrame(Flush_Layout);
  28. while (frame) {
  29. SVGTextFrame* textFrame = do_QueryFrame(frame);
  30. if (textFrame) {
  31. return textFrame;
  32. }
  33. frame = frame->GetParent();
  34. }
  35. return nullptr;
  36. }
  37. already_AddRefed<SVGAnimatedLength>
  38. SVGTextContentElement::TextLength()
  39. {
  40. return LengthAttributes()[TEXTLENGTH].ToDOMAnimatedLength(this);
  41. }
  42. already_AddRefed<SVGAnimatedEnumeration>
  43. SVGTextContentElement::LengthAdjust()
  44. {
  45. return EnumAttributes()[LENGTHADJUST].ToDOMAnimatedEnum(this);
  46. }
  47. //----------------------------------------------------------------------
  48. int32_t
  49. SVGTextContentElement::GetNumberOfChars()
  50. {
  51. SVGTextFrame* textFrame = GetSVGTextFrame();
  52. return textFrame ? textFrame->GetNumberOfChars(this) : 0;
  53. }
  54. float
  55. SVGTextContentElement::GetComputedTextLength()
  56. {
  57. SVGTextFrame* textFrame = GetSVGTextFrame();
  58. return textFrame ? textFrame->GetComputedTextLength(this) : 0.0f;
  59. }
  60. void
  61. SVGTextContentElement::SelectSubString(uint32_t charnum, uint32_t nchars, ErrorResult& rv)
  62. {
  63. SVGTextFrame* textFrame = GetSVGTextFrame();
  64. if (!textFrame)
  65. return;
  66. rv = textFrame->SelectSubString(this, charnum, nchars);
  67. }
  68. float
  69. SVGTextContentElement::GetSubStringLength(uint32_t charnum, uint32_t nchars, ErrorResult& rv)
  70. {
  71. SVGTextFrame* textFrame = GetSVGTextFrame();
  72. if (!textFrame)
  73. return 0.0f;
  74. float length = 0.0f;
  75. rv = textFrame->GetSubStringLength(this, charnum, nchars, &length);
  76. return length;
  77. }
  78. already_AddRefed<nsISVGPoint>
  79. SVGTextContentElement::GetStartPositionOfChar(uint32_t charnum, ErrorResult& rv)
  80. {
  81. SVGTextFrame* textFrame = GetSVGTextFrame();
  82. if (!textFrame) {
  83. rv.Throw(NS_ERROR_FAILURE);
  84. return nullptr;
  85. }
  86. nsCOMPtr<nsISVGPoint> point;
  87. rv = textFrame->GetStartPositionOfChar(this, charnum, getter_AddRefs(point));
  88. return point.forget();
  89. }
  90. already_AddRefed<nsISVGPoint>
  91. SVGTextContentElement::GetEndPositionOfChar(uint32_t charnum, ErrorResult& rv)
  92. {
  93. SVGTextFrame* textFrame = GetSVGTextFrame();
  94. if (!textFrame) {
  95. rv.Throw(NS_ERROR_FAILURE);
  96. return nullptr;
  97. }
  98. nsCOMPtr<nsISVGPoint> point;
  99. rv = textFrame->GetEndPositionOfChar(this, charnum, getter_AddRefs(point));
  100. return point.forget();
  101. }
  102. already_AddRefed<SVGIRect>
  103. SVGTextContentElement::GetExtentOfChar(uint32_t charnum, ErrorResult& rv)
  104. {
  105. SVGTextFrame* textFrame = GetSVGTextFrame();
  106. if (!textFrame) {
  107. rv.Throw(NS_ERROR_FAILURE);
  108. return nullptr;
  109. }
  110. RefPtr<SVGIRect> rect;
  111. rv = textFrame->GetExtentOfChar(this, charnum, getter_AddRefs(rect));
  112. return rect.forget();
  113. }
  114. float
  115. SVGTextContentElement::GetRotationOfChar(uint32_t charnum, ErrorResult& rv)
  116. {
  117. SVGTextFrame* textFrame = GetSVGTextFrame();
  118. if (!textFrame) {
  119. rv.Throw(NS_ERROR_FAILURE);
  120. return 0.0f;
  121. }
  122. float rotation;
  123. rv = textFrame->GetRotationOfChar(this, charnum, &rotation);
  124. return rotation;
  125. }
  126. int32_t
  127. SVGTextContentElement::GetCharNumAtPosition(nsISVGPoint& aPoint)
  128. {
  129. SVGTextFrame* textFrame = GetSVGTextFrame();
  130. return textFrame ? textFrame->GetCharNumAtPosition(this, &aPoint) : -1;
  131. }
  132. } // namespace dom
  133. } // namespace mozilla