Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DrawTextFragment.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.Graphics2D;
  17. import java.awt.font.TextLayout;
  18. import java.text.AttributedCharacterIterator;
  19. import java.text.AttributedString;
  20. import java.text.CharacterIterator;
  21. public class DrawTextFragment implements Drawable {
  22. final TextLayout layout;
  23. final AttributedString str;
  24. double x, y;
  25. public DrawTextFragment(TextLayout layout, AttributedString str) {
  26. this.layout = layout;
  27. this.str = str;
  28. }
  29. public void setPosition(double x, double y) {
  30. // TODO: replace it, by applyTransform????
  31. this.x = x;
  32. this.y = y;
  33. }
  34. public void draw(Graphics2D graphics){
  35. if(str == null) {
  36. return;
  37. }
  38. double yBaseline = y + layout.getAscent();
  39. Integer textMode = (Integer)graphics.getRenderingHint(Drawable.TEXT_RENDERING_MODE);
  40. if(textMode != null && textMode == Drawable.TEXT_AS_SHAPES){
  41. layout.draw(graphics, (float)x, (float)yBaseline);
  42. } else {
  43. graphics.drawString(str.getIterator(), (float)x, (float)yBaseline );
  44. }
  45. }
  46. public void applyTransform(Graphics2D graphics) {
  47. }
  48. public void drawContent(Graphics2D graphics) {
  49. }
  50. public TextLayout getLayout() {
  51. return layout;
  52. }
  53. public AttributedString getAttributedString() {
  54. return str;
  55. }
  56. /**
  57. * @return full height of this text run which is sum of ascent, descent and leading
  58. */
  59. public float getHeight(){
  60. double h = layout.getAscent() + layout.getDescent();
  61. return (float)h;
  62. }
  63. /**
  64. * @return the leading height before/after a text line
  65. */
  66. public float getLeading() {
  67. // fix invalid leadings (leading == 0)
  68. double l = layout.getLeading();
  69. if (l == 0) {
  70. // see https://stackoverflow.com/questions/925147
  71. // we use a 115% value instead of the 120% proposed one, as this seems to be closer to LO/OO
  72. l = (layout.getAscent()+layout.getDescent())*0.15;
  73. }
  74. return (float)l;
  75. }
  76. /**
  77. *
  78. * @return width if this text run
  79. */
  80. public float getWidth(){
  81. return layout.getAdvance();
  82. }
  83. /**
  84. *
  85. * @return the string to be painted
  86. */
  87. public String getString(){
  88. if (str == null) return "";
  89. AttributedCharacterIterator it = str.getIterator();
  90. StringBuilder buf = new StringBuilder();
  91. for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
  92. buf.append(c);
  93. }
  94. return buf.toString();
  95. }
  96. @Override
  97. public String toString(){
  98. return "[" + getClass().getSimpleName() + "] " + getString();
  99. }
  100. }