PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/batik-1.7/sources/org/apache/batik/gvt/font/MultiGlyphVector.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 405 lines | 241 code | 46 blank | 118 comment | 48 complexity | 8da66fe7544345732b26dc4fdf64b4ad 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.gvt.font;
  16. import java.awt.Graphics2D;
  17. import java.awt.Shape;
  18. import java.awt.font.FontRenderContext;
  19. import java.awt.font.GlyphJustificationInfo;
  20. import java.awt.geom.AffineTransform;
  21. import java.awt.geom.GeneralPath;
  22. import java.awt.geom.Point2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.text.AttributedCharacterIterator;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import org.apache.batik.gvt.text.AttributedCharacterSpanIterator;
  28. /**
  29. *
  30. * @version $Id: MultiGlyphVector.java 501495 2007-01-30 18:00:36Z dvholten $
  31. */
  32. public class MultiGlyphVector implements GVTGlyphVector {
  33. GVTGlyphVector [] gvs;
  34. int [] nGlyphs;
  35. int [] off;
  36. int nGlyph;
  37. public MultiGlyphVector(List gvs) {
  38. int nSlots = gvs.size();
  39. this.gvs = new GVTGlyphVector[ nSlots ];
  40. this.nGlyphs = new int[ nSlots ];
  41. this.off = new int[ nSlots ];
  42. Iterator iter = gvs.iterator();
  43. int i=0;
  44. while (iter.hasNext()) {
  45. off[i] = nGlyph;
  46. GVTGlyphVector gv = (GVTGlyphVector)iter.next();
  47. this.gvs[i] = gv;
  48. nGlyphs[i] = gv.getNumGlyphs();
  49. nGlyph += nGlyphs[i];
  50. i++;
  51. }
  52. nGlyphs[i-1]++;
  53. }
  54. /**
  55. * Returns the number of glyphs in this GlyphVector.
  56. */
  57. public int getNumGlyphs() {
  58. return nGlyph;
  59. }
  60. int getGVIdx(int glyphIdx) {
  61. if (glyphIdx > nGlyph) return -1;
  62. if (glyphIdx == nGlyph) return gvs.length-1;
  63. for (int i=0; i<nGlyphs.length; i++)
  64. if (glyphIdx-off[i] < nGlyphs[i]) return i;
  65. return -1;
  66. }
  67. /**
  68. * Returns the Font associated with this GlyphVector.
  69. */
  70. public GVTFont getFont() {
  71. throw new IllegalArgumentException("Can't be correctly Implemented");
  72. }
  73. /**
  74. * Returns the FontRenderContext associated with this GlyphVector.
  75. */
  76. public FontRenderContext getFontRenderContext() {
  77. return gvs[0].getFontRenderContext();
  78. }
  79. /**
  80. * Returns the glyphcode of the specified glyph.
  81. */
  82. public int getGlyphCode(int glyphIndex) {
  83. int idx = getGVIdx(glyphIndex);
  84. return gvs[idx].getGlyphCode(glyphIndex-off[idx]);
  85. }
  86. /**
  87. * Returns the justification information for the glyph at the specified
  88. * index into this GlyphVector.
  89. */
  90. public GlyphJustificationInfo getGlyphJustificationInfo(int glyphIndex) {
  91. int idx = getGVIdx(glyphIndex);
  92. return gvs[idx].getGlyphJustificationInfo(glyphIndex-off[idx]);
  93. }
  94. /**
  95. * Returns the logical bounds of the specified glyph within this
  96. * GlyphVector.
  97. */
  98. public Shape getGlyphLogicalBounds(int glyphIndex) {
  99. int idx = getGVIdx(glyphIndex);
  100. return gvs[idx].getGlyphLogicalBounds(glyphIndex-off[idx]);
  101. }
  102. /**
  103. * Returns the metrics of the glyph at the specified index into this
  104. * GlyphVector.
  105. */
  106. public GVTGlyphMetrics getGlyphMetrics(int glyphIndex) {
  107. int idx = getGVIdx(glyphIndex);
  108. return gvs[idx].getGlyphMetrics(glyphIndex-off[idx]);
  109. }
  110. /**
  111. * Returns a Shape whose interior corresponds to the visual representation
  112. * of the specified glyph within this GlyphVector.
  113. */
  114. public Shape getGlyphOutline(int glyphIndex) {
  115. int idx = getGVIdx(glyphIndex);
  116. return gvs[idx].getGlyphOutline(glyphIndex-off[idx]);
  117. }
  118. /**
  119. * Returns the bounding box of the specified glyph, considering only the
  120. * glyph's metrics (ascent, descent, advance) rather than the actual glyph
  121. * shape.
  122. */
  123. public Rectangle2D getGlyphCellBounds(int glyphIndex) {
  124. return getGlyphLogicalBounds(glyphIndex).getBounds2D();
  125. }
  126. /**
  127. * Returns the position of the specified glyph within this GlyphVector.
  128. */
  129. public Point2D getGlyphPosition(int glyphIndex) {
  130. int idx = getGVIdx(glyphIndex);
  131. return gvs[idx].getGlyphPosition(glyphIndex-off[idx]);
  132. }
  133. /**
  134. * Gets the transform of the specified glyph within this GlyphVector.
  135. */
  136. public AffineTransform getGlyphTransform(int glyphIndex) {
  137. int idx = getGVIdx(glyphIndex);
  138. return gvs[idx].getGlyphTransform(glyphIndex-off[idx]);
  139. }
  140. /**
  141. * Returns the visual bounds of the specified glyph within the GlyphVector.
  142. */
  143. public Shape getGlyphVisualBounds(int glyphIndex) {
  144. int idx = getGVIdx(glyphIndex);
  145. return gvs[idx].getGlyphVisualBounds(glyphIndex-off[idx]);
  146. }
  147. /**
  148. * Sets the position of the specified glyph within this GlyphVector.
  149. */
  150. public void setGlyphPosition(int glyphIndex, Point2D newPos) {
  151. int idx = getGVIdx(glyphIndex);
  152. // System.out.println("setting: " + idx + " - " + (glyphIndex-off[idx]) +
  153. // " -> " + newPos);
  154. gvs[idx].setGlyphPosition(glyphIndex-off[idx], newPos);
  155. }
  156. /**
  157. * Sets the transform of the specified glyph within this GlyphVector.
  158. */
  159. public void setGlyphTransform(int glyphIndex, AffineTransform newTX) {
  160. int idx = getGVIdx(glyphIndex);
  161. gvs[idx].setGlyphTransform(glyphIndex-off[idx], newTX);
  162. }
  163. /**
  164. * Tells the glyph vector whether or not to draw the specified glyph.
  165. */
  166. public void setGlyphVisible(int glyphIndex, boolean visible) {
  167. int idx = getGVIdx(glyphIndex);
  168. gvs[idx].setGlyphVisible(glyphIndex-off[idx], visible);
  169. }
  170. /**
  171. * Returns true if specified glyph will be drawn.
  172. */
  173. public boolean isGlyphVisible(int glyphIndex) {
  174. int idx = getGVIdx(glyphIndex);
  175. return gvs[idx].isGlyphVisible(glyphIndex-off[idx]);
  176. }
  177. /**
  178. * Returns an array of glyphcodes for the specified glyphs.
  179. */
  180. public int[] getGlyphCodes(int beginGlyphIndex, int numEntries,
  181. int[] codeReturn) {
  182. int [] ret = codeReturn;
  183. if (ret == null)
  184. ret = new int[numEntries];
  185. int [] tmp = null;
  186. int gvIdx = getGVIdx(beginGlyphIndex);
  187. int gi = beginGlyphIndex-off[gvIdx];
  188. int i=0;
  189. GVTGlyphVector gv;
  190. while (numEntries != 0) {
  191. int len = numEntries;
  192. if (gi+len > nGlyphs[gvIdx])
  193. len = nGlyphs[gvIdx]-gi;
  194. gv = gvs[gvIdx];
  195. if (i == 0) {
  196. gv.getGlyphCodes(gi, len, ret);
  197. } else {
  198. if ((tmp == null) || (tmp.length < len))
  199. tmp = new int[len];
  200. gv.getGlyphCodes(gi, len, tmp);
  201. System.arraycopy( tmp, 0, ret, i, len );
  202. }
  203. gi=0;
  204. gvIdx++;
  205. numEntries -= len;
  206. i+=len;
  207. }
  208. return ret;
  209. }
  210. /**
  211. * Returns an array of glyph positions for the specified glyphs
  212. */
  213. public float[] getGlyphPositions(int beginGlyphIndex,
  214. int numEntries,
  215. float[] positionReturn) {
  216. float [] ret = positionReturn;
  217. if (ret == null)
  218. ret = new float[numEntries*2];
  219. float [] tmp = null;
  220. int gvIdx = getGVIdx(beginGlyphIndex);
  221. int gi = beginGlyphIndex-off[gvIdx];
  222. int i=0;
  223. GVTGlyphVector gv;
  224. while (numEntries != 0) {
  225. int len = numEntries;
  226. if (gi+len > nGlyphs[gvIdx])
  227. len = nGlyphs[gvIdx]-gi;
  228. gv = gvs[gvIdx];
  229. if (i == 0) {
  230. gv.getGlyphPositions(gi, len, ret);
  231. } else {
  232. if ((tmp == null) || (tmp.length < len*2))
  233. tmp = new float[len*2];
  234. gv.getGlyphPositions(gi, len, tmp);
  235. System.arraycopy( tmp, 0, ret, i, len * 2 );
  236. }
  237. gi=0;
  238. gvIdx++;
  239. numEntries -= len;
  240. i+=len*2;
  241. }
  242. return ret;
  243. }
  244. /**
  245. * Returns the logical bounds of this GlyphVector.
  246. */
  247. public Rectangle2D getLogicalBounds() {
  248. Rectangle2D ret = null;
  249. for (int idx=0; idx<gvs.length; idx++) {
  250. Rectangle2D b = gvs[idx].getLogicalBounds();
  251. if (ret == null) ret = b;
  252. //else ret = ret.createUnion(b);
  253. else ret.add( b ); // same as union
  254. }
  255. return ret;
  256. }
  257. /**
  258. * Returns a Shape whose interior corresponds to the visual representation
  259. * of this GlyphVector.
  260. */
  261. public Shape getOutline() {
  262. GeneralPath ret = null;
  263. for (int idx=0; idx<gvs.length; idx++) {
  264. Shape s = gvs[idx].getOutline();
  265. if (ret == null) ret = new GeneralPath(s);
  266. else ret.append(s, false);
  267. }
  268. return ret;
  269. }
  270. /**
  271. * Returns a Shape whose interior corresponds to the visual representation
  272. * of this GlyphVector, offset to x, y.
  273. */
  274. public Shape getOutline(float x, float y) {
  275. Shape outline = getOutline();
  276. AffineTransform tr = AffineTransform.getTranslateInstance(x,y);
  277. outline = tr.createTransformedShape(outline);
  278. return outline;
  279. }
  280. /**
  281. * Returns the bounds of this GlyphVector. This includes
  282. * stroking effects.
  283. */
  284. public Rectangle2D getBounds2D(AttributedCharacterIterator aci) {
  285. Rectangle2D ret = null;
  286. int begin = aci.getBeginIndex();
  287. for (int idx=0; idx<gvs.length; idx++) {
  288. GVTGlyphVector gv = gvs[idx];
  289. int end = gv.getCharacterCount(0, gv.getNumGlyphs())+1;
  290. Rectangle2D b = gvs[idx].getBounds2D
  291. (new AttributedCharacterSpanIterator(aci, begin, end));
  292. if (ret == null) ret = b;
  293. //else ret = ret.createUnion(b);
  294. else ret.add(b);
  295. begin = end;
  296. }
  297. return ret;
  298. }
  299. /**
  300. * Returns the geometric bounds of this GlyphVector. The geometric
  301. * bounds is the bounds of the geometry of the glyph vector,
  302. * disregarding stroking.
  303. */
  304. public Rectangle2D getGeometricBounds() {
  305. Rectangle2D ret = null;
  306. for (int idx=0; idx<gvs.length; idx++) {
  307. Rectangle2D b = gvs[idx].getGeometricBounds();
  308. if (ret == null) ret = b;
  309. //else ret = ret.createUnion(b);
  310. else ret.add(b);
  311. }
  312. return ret;
  313. }
  314. public void performDefaultLayout() {
  315. for (int idx=0; idx<gvs.length; idx++) {
  316. gvs[idx].performDefaultLayout();
  317. }
  318. }
  319. /**
  320. * Returns the number of chars represented by the glyphs within the
  321. * specified range.
  322. *
  323. * @param startGlyphIndex The index of the first glyph in the range.
  324. * @param endGlyphIndex The index of the last glyph in the range.
  325. * @return The number of chars.
  326. */
  327. public int getCharacterCount(int startGlyphIndex, int endGlyphIndex) {
  328. int idx1 = getGVIdx(startGlyphIndex);
  329. int idx2 = getGVIdx(endGlyphIndex);
  330. int ret=0;
  331. for (int idx=idx1; idx<=idx2; idx++) {
  332. int gi1 = startGlyphIndex-off[idx];
  333. int gi2 = endGlyphIndex-off[idx];
  334. if (gi2 >= nGlyphs[idx]) {
  335. gi2 = nGlyphs[idx]-1;
  336. }
  337. ret += gvs[idx].getCharacterCount(gi1, gi2);
  338. startGlyphIndex += (gi2-gi1+1);
  339. }
  340. return ret;
  341. }
  342. /**
  343. * Draws the glyph vector.
  344. */
  345. public void draw(Graphics2D g2d,
  346. AttributedCharacterIterator aci) {
  347. int begin = aci.getBeginIndex();
  348. for (int idx=0; idx<gvs.length; idx++) {
  349. GVTGlyphVector gv = gvs[idx];
  350. int end = gv.getCharacterCount(0, gv.getNumGlyphs())+1;
  351. gv.draw(g2d, new AttributedCharacterSpanIterator(aci, begin, end));
  352. begin = end;
  353. }
  354. }
  355. }