You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DrawTextFragment.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.sl.draw;
  16. import java.awt.Color;
  17. import java.awt.Graphics2D;
  18. import java.awt.font.TextAttribute;
  19. import java.awt.font.TextLayout;
  20. import java.text.AttributedCharacterIterator;
  21. import java.text.AttributedString;
  22. import java.text.CharacterIterator;
  23. public class DrawTextFragment implements Drawable {
  24. final TextLayout layout;
  25. final AttributedString str;
  26. double x, y;
  27. public DrawTextFragment(TextLayout layout, AttributedString str) {
  28. this.layout = layout;
  29. this.str = str;
  30. }
  31. public void setPosition(double x, double y) {
  32. // TODO: replace it, by applyTransform????
  33. this.x = x;
  34. this.y = y;
  35. }
  36. public void draw(Graphics2D graphics){
  37. if(str == null) {
  38. return;
  39. }
  40. double yBaseline = y + layout.getAscent();
  41. Integer textMode = (Integer)graphics.getRenderingHint(Drawable.TEXT_RENDERING_MODE);
  42. if(textMode != null && textMode == Drawable.TEXT_AS_SHAPES){
  43. layout.draw(graphics, (float)x, (float)yBaseline);
  44. } else {
  45. try {
  46. graphics.drawString(str.getIterator(), (float) x, (float) yBaseline);
  47. } catch (ClassCastException e) {
  48. // workaround: batik issue, which expects only Color as forground color
  49. replaceForgroundPaintWithBlack(str);
  50. graphics.drawString(str.getIterator(), (float) x, (float) yBaseline);
  51. }
  52. }
  53. }
  54. private void replaceForgroundPaintWithBlack(AttributedString as) {
  55. AttributedCharacterIterator iter = as.getIterator(new TextAttribute[]{TextAttribute.FOREGROUND});
  56. for (char ch = iter.first();
  57. ch != CharacterIterator.DONE;
  58. ch = iter.next()) {
  59. as.addAttribute(TextAttribute.FOREGROUND, Color.BLACK, iter.getBeginIndex(), iter.getEndIndex());
  60. }
  61. }
  62. public void applyTransform(Graphics2D graphics) {
  63. }
  64. public void drawContent(Graphics2D graphics) {
  65. }
  66. public TextLayout getLayout() {
  67. return layout;
  68. }
  69. public AttributedString getAttributedString() {
  70. return str;
  71. }
  72. /**
  73. * @return full height of this text run which is sum of ascent, descent and leading
  74. */
  75. public float getHeight(){
  76. double h = layout.getAscent() + layout.getDescent();
  77. return (float)h;
  78. }
  79. /**
  80. * @return the leading height before/after a text line
  81. */
  82. public float getLeading() {
  83. // fix invalid leadings (leading == 0)
  84. double l = layout.getLeading();
  85. if (l == 0) {
  86. // we use a 115% value instead of the 120% proposed one, as this seems to be closer to LO/OO
  87. l = (layout.getAscent()+layout.getDescent())*0.15;
  88. }
  89. return (float)l;
  90. }
  91. /**
  92. *
  93. * @return width if this text run
  94. */
  95. public float getWidth(){
  96. return layout.getAdvance();
  97. }
  98. /**
  99. *
  100. * @return the string to be painted
  101. */
  102. public String getString(){
  103. if (str == null) return "";
  104. AttributedCharacterIterator it = str.getIterator();
  105. StringBuilder buf = new StringBuilder();
  106. for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
  107. buf.append(c);
  108. }
  109. return buf.toString();
  110. }
  111. @Override
  112. public String toString(){
  113. return "[" + getClass().getSimpleName() + "] " + getString();
  114. }
  115. }