PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/batik-1.7/sources/org/apache/batik/bridge/TextUtilities.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 319 lines | 198 code | 25 blank | 96 comment | 32 complexity | 94dd43664e45d5625bd202392b3113fe MD5 | raw file
  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.batik.bridge;
  16. import java.awt.font.TextAttribute;
  17. import java.util.ArrayList;
  18. import java.util.StringTokenizer;
  19. import org.apache.batik.css.engine.SVGCSSEngine;
  20. import org.apache.batik.css.engine.value.Value;
  21. import org.apache.batik.gvt.TextNode;
  22. import org.apache.batik.util.CSSConstants;
  23. import org.w3c.dom.Element;
  24. import org.w3c.dom.Node;
  25. import org.w3c.dom.css.CSSPrimitiveValue;
  26. /**
  27. * A collection of utility method for text.
  28. *
  29. * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
  30. * @author <a href="mailto:bill.haneman@ireland.sun.com">Bill Haneman</a>
  31. * @version $Id: TextUtilities.java 501922 2007-01-31 17:47:47Z dvholten $
  32. */
  33. public abstract class TextUtilities implements CSSConstants, ErrorConstants {
  34. /**
  35. * Returns the content of the given element.
  36. */
  37. public static String getElementContent(Element e) {
  38. StringBuffer result = new StringBuffer();
  39. for (Node n = e.getFirstChild();
  40. n != null;
  41. n = n.getNextSibling()) {
  42. switch (n.getNodeType()) {
  43. case Node.ELEMENT_NODE:
  44. result.append(getElementContent((Element)n));
  45. break;
  46. case Node.CDATA_SECTION_NODE:
  47. case Node.TEXT_NODE:
  48. result.append(n.getNodeValue());
  49. }
  50. }
  51. return result.toString();
  52. }
  53. /**
  54. * Returns the float list that represents a set of horizontal
  55. * values or percentage.
  56. *
  57. * @param element the element that defines the specified coordinates
  58. * @param attrName the name of the attribute (used by error handling)
  59. * @param valueStr the delimited string containing values of the coordinate
  60. * @param ctx the bridge context
  61. */
  62. public static
  63. ArrayList svgHorizontalCoordinateArrayToUserSpace(Element element,
  64. String attrName,
  65. String valueStr,
  66. BridgeContext ctx) {
  67. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, element);
  68. ArrayList values = new ArrayList();
  69. StringTokenizer st = new StringTokenizer(valueStr, ", ", false);
  70. while (st.hasMoreTokens()) {
  71. values.add
  72. (new Float(UnitProcessor.svgHorizontalCoordinateToUserSpace
  73. (st.nextToken(), attrName, uctx)));
  74. }
  75. return values;
  76. }
  77. /**
  78. * Returns the float list that represents a set of values or percentage.
  79. *
  80. *
  81. * @param element the element that defines the specified coordinates
  82. * @param attrName the name of the attribute (used by error handling)
  83. * @param valueStr the delimited string containing values of the coordinate
  84. * @param ctx the bridge context
  85. */
  86. public static
  87. ArrayList svgVerticalCoordinateArrayToUserSpace(Element element,
  88. String attrName,
  89. String valueStr,
  90. BridgeContext ctx) {
  91. UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, element);
  92. ArrayList values = new ArrayList();
  93. StringTokenizer st = new StringTokenizer(valueStr, ", ", false);
  94. while (st.hasMoreTokens()) {
  95. values.add
  96. (new Float(UnitProcessor.svgVerticalCoordinateToUserSpace
  97. (st.nextToken(), attrName, uctx)));
  98. }
  99. return values;
  100. }
  101. public static ArrayList svgRotateArrayToFloats(Element element,
  102. String attrName,
  103. String valueStr,
  104. BridgeContext ctx) {
  105. StringTokenizer st = new StringTokenizer(valueStr, ", ", false);
  106. ArrayList values = new ArrayList();
  107. String s;
  108. while (st.hasMoreTokens()) {
  109. try {
  110. s = st.nextToken();
  111. values.add
  112. (new Float(Math.toRadians
  113. (SVGUtilities.convertSVGNumber(s))));
  114. } catch (NumberFormatException nfEx ) {
  115. throw new BridgeException
  116. (ctx, element, nfEx, ERR_ATTRIBUTE_VALUE_MALFORMED,
  117. new Object [] {attrName, valueStr});
  118. }
  119. }
  120. return values;
  121. }
  122. /**
  123. * Converts the font-size CSS value to a float value.
  124. * @param e the element
  125. */
  126. public static Float convertFontSize(Element e) {
  127. Value v = CSSUtilities.getComputedStyle
  128. (e, SVGCSSEngine.FONT_SIZE_INDEX);
  129. return new Float(v.getFloatValue());
  130. }
  131. /**
  132. * Converts the font-style CSS value to a float value.
  133. * @param e the element
  134. */
  135. public static Float convertFontStyle(Element e) {
  136. Value v = CSSUtilities.getComputedStyle
  137. (e, SVGCSSEngine.FONT_STYLE_INDEX);
  138. switch (v.getStringValue().charAt(0)) {
  139. case 'n':
  140. return TextAttribute.POSTURE_REGULAR;
  141. default:
  142. return TextAttribute.POSTURE_OBLIQUE;
  143. }
  144. }
  145. /**
  146. * Converts the font-stretch CSS value to a float value.
  147. * @param e the element
  148. */
  149. public static Float convertFontStretch(Element e) {
  150. Value v = CSSUtilities.getComputedStyle
  151. (e, SVGCSSEngine.FONT_STRETCH_INDEX);
  152. String s = v.getStringValue();
  153. switch (s.charAt(0)) {
  154. case 'u':
  155. if (s.charAt(6) == 'c') {
  156. return TextAttribute.WIDTH_CONDENSED;
  157. } else {
  158. return TextAttribute.WIDTH_EXTENDED;
  159. }
  160. case 'e':
  161. if (s.charAt(6) == 'c') {
  162. return TextAttribute.WIDTH_CONDENSED;
  163. } else {
  164. if (s.length() == 8) {
  165. return TextAttribute.WIDTH_SEMI_EXTENDED;
  166. } else {
  167. return TextAttribute.WIDTH_EXTENDED;
  168. }
  169. }
  170. case 's':
  171. if (s.charAt(6) == 'c') {
  172. return TextAttribute.WIDTH_SEMI_CONDENSED;
  173. } else {
  174. return TextAttribute.WIDTH_SEMI_EXTENDED;
  175. }
  176. default:
  177. return TextAttribute.WIDTH_REGULAR;
  178. }
  179. }
  180. /**
  181. * Converts the font-weight CSS value to a float value.
  182. * @param e the element
  183. */
  184. public static Float convertFontWeight(Element e) {
  185. Value v = CSSUtilities.getComputedStyle
  186. (e, SVGCSSEngine.FONT_WEIGHT_INDEX);
  187. float f = v.getFloatValue();
  188. switch ((int)f) {
  189. case 100:
  190. return TextAttribute.WEIGHT_EXTRA_LIGHT;
  191. case 200:
  192. return TextAttribute.WEIGHT_LIGHT;
  193. case 300:
  194. return TextAttribute.WEIGHT_DEMILIGHT;
  195. case 400:
  196. return TextAttribute.WEIGHT_REGULAR;
  197. case 500:
  198. return TextAttribute.WEIGHT_SEMIBOLD;
  199. default:
  200. return TextAttribute.WEIGHT_BOLD;
  201. /* Would like to do this but the JDK 1.3 & 1.4
  202. seems to drop back to 'REGULAR' instead of 'BOLD'
  203. if there is not a match.
  204. case 700:
  205. return TextAttribute.WEIGHT_HEAVY;
  206. case 800:
  207. return TextAttribute.WEIGHT_EXTRABOLD;
  208. case 900:
  209. return TextAttribute.WEIGHT_ULTRABOLD;
  210. */
  211. }
  212. }
  213. /**
  214. * Converts the text-anchor CSS value to a TextNode.Anchor.
  215. * @param e the element
  216. */
  217. public static TextNode.Anchor convertTextAnchor(Element e) {
  218. Value v = CSSUtilities.getComputedStyle
  219. (e, SVGCSSEngine.TEXT_ANCHOR_INDEX);
  220. switch (v.getStringValue().charAt(0)) {
  221. case 's':
  222. return TextNode.Anchor.START;
  223. case 'm':
  224. return TextNode.Anchor.MIDDLE;
  225. default:
  226. return TextNode.Anchor.END;
  227. }
  228. }
  229. /**
  230. * Converts a baseline-shift CSS value to a value usable as a text
  231. * attribute, or null.
  232. * @param e the element
  233. */
  234. public static Object convertBaselineShift(Element e) {
  235. Value v = CSSUtilities.getComputedStyle
  236. (e, SVGCSSEngine.BASELINE_SHIFT_INDEX);
  237. if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
  238. String s = v.getStringValue();
  239. switch (s.charAt(2)) {
  240. case 'p': //suPerscript
  241. return TextAttribute.SUPERSCRIPT_SUPER;
  242. case 'b': //suBscript
  243. return TextAttribute.SUPERSCRIPT_SUB;
  244. default:
  245. return null;
  246. }
  247. } else {
  248. return new Float(v.getFloatValue());
  249. }
  250. }
  251. /**
  252. * Converts a kerning CSS value to a value usable as a text
  253. * attribute, or null.
  254. * @param e the element
  255. */
  256. public static Float convertKerning(Element e) {
  257. Value v = CSSUtilities.getComputedStyle
  258. (e, SVGCSSEngine.KERNING_INDEX);
  259. if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
  260. return null;
  261. }
  262. return new Float(v.getFloatValue());
  263. }
  264. /**
  265. * Converts a letter-spacing CSS value to a value usable as a text
  266. * attribute, or null.
  267. * @param e the element
  268. */
  269. public static Float convertLetterSpacing(Element e) {
  270. Value v = CSSUtilities.getComputedStyle
  271. (e, SVGCSSEngine.LETTER_SPACING_INDEX);
  272. if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
  273. return null;
  274. }
  275. return new Float(v.getFloatValue());
  276. }
  277. /**
  278. * Converts a word-spacing CSS value to a value usable as a text
  279. * attribute, or null.
  280. * @param e the element
  281. */
  282. public static Float convertWordSpacing(Element e) {
  283. Value v = CSSUtilities.getComputedStyle
  284. (e, SVGCSSEngine.WORD_SPACING_INDEX);
  285. if (v.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
  286. return null;
  287. }
  288. return new Float(v.getFloatValue());
  289. }
  290. }