PageRenderTime 37ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sun/font/FreetypeFontScaler.java

https://gitlab.com/borneywpf/openjdk-7-src
Java | 262 lines | 195 code | 29 blank | 38 comment | 27 complexity | b484576ad31edd78e0f357d6247f74a6 MD5 | raw file
  1. /*
  2. * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package sun.font;
  26. import java.awt.geom.GeneralPath;
  27. import java.awt.geom.Point2D;
  28. import java.awt.geom.Rectangle2D;
  29. import java.lang.ref.WeakReference;
  30. /* This is Freetype based implementation of FontScaler.
  31. *
  32. * Note that in case of runtime error it is expected that
  33. * native code will release all native resources and
  34. * call invalidateScaler() (that will throw FontScalerException).
  35. *
  36. * Note that callee is responsible for releasing native scaler context.
  37. */
  38. class FreetypeFontScaler extends FontScaler {
  39. /* constants aligned with native code */
  40. private static final int TRUETYPE_FONT = 1;
  41. private static final int TYPE1_FONT = 2;
  42. static {
  43. /* At the moment fontmanager library depends on freetype library
  44. and therefore no need to load it explicitly here */
  45. FontManagerNativeLibrary.load();
  46. initIDs(FreetypeFontScaler.class);
  47. }
  48. private static native void initIDs(Class FFS);
  49. private void invalidateScaler() throws FontScalerException {
  50. nativeScaler = 0;
  51. font = null;
  52. throw new FontScalerException();
  53. }
  54. public FreetypeFontScaler(Font2D font, int indexInCollection,
  55. boolean supportsCJK, int filesize) {
  56. int fonttype = TRUETYPE_FONT;
  57. if (font instanceof Type1Font) {
  58. fonttype = TYPE1_FONT;
  59. }
  60. nativeScaler = initNativeScaler(font,
  61. fonttype,
  62. indexInCollection,
  63. supportsCJK,
  64. filesize);
  65. this.font = new WeakReference(font);
  66. }
  67. synchronized StrikeMetrics getFontMetrics(long pScalerContext)
  68. throws FontScalerException {
  69. if (nativeScaler != 0L) {
  70. return getFontMetricsNative(font.get(),
  71. pScalerContext,
  72. nativeScaler);
  73. }
  74. return FontScaler.getNullScaler().getFontMetrics(0L);
  75. }
  76. synchronized float getGlyphAdvance(long pScalerContext, int glyphCode)
  77. throws FontScalerException {
  78. if (nativeScaler != 0L) {
  79. return getGlyphAdvanceNative(font.get(),
  80. pScalerContext,
  81. nativeScaler,
  82. glyphCode);
  83. }
  84. return FontScaler.getNullScaler().
  85. getGlyphAdvance(0L, glyphCode);
  86. }
  87. synchronized void getGlyphMetrics(long pScalerContext,
  88. int glyphCode, Point2D.Float metrics)
  89. throws FontScalerException {
  90. if (nativeScaler != 0L) {
  91. getGlyphMetricsNative(font.get(),
  92. pScalerContext,
  93. nativeScaler,
  94. glyphCode,
  95. metrics);
  96. return;
  97. }
  98. FontScaler.getNullScaler().
  99. getGlyphMetrics(0L, glyphCode, metrics);
  100. }
  101. synchronized long getGlyphImage(long pScalerContext, int glyphCode)
  102. throws FontScalerException {
  103. if (nativeScaler != 0L) {
  104. return getGlyphImageNative(font.get(),
  105. pScalerContext,
  106. nativeScaler,
  107. glyphCode);
  108. }
  109. return FontScaler.getNullScaler().
  110. getGlyphImage(0L, glyphCode);
  111. }
  112. synchronized Rectangle2D.Float getGlyphOutlineBounds(
  113. long pScalerContext, int glyphCode)
  114. throws FontScalerException {
  115. if (nativeScaler != 0L) {
  116. return getGlyphOutlineBoundsNative(font.get(),
  117. pScalerContext,
  118. nativeScaler,
  119. glyphCode);
  120. }
  121. return FontScaler.getNullScaler().
  122. getGlyphOutlineBounds(0L,glyphCode);
  123. }
  124. synchronized GeneralPath getGlyphOutline(
  125. long pScalerContext, int glyphCode, float x, float y)
  126. throws FontScalerException {
  127. if (nativeScaler != 0L) {
  128. return getGlyphOutlineNative(font.get(),
  129. pScalerContext,
  130. nativeScaler,
  131. glyphCode,
  132. x, y);
  133. }
  134. return FontScaler.getNullScaler().
  135. getGlyphOutline(0L, glyphCode, x,y);
  136. }
  137. synchronized GeneralPath getGlyphVectorOutline(
  138. long pScalerContext, int[] glyphs, int numGlyphs,
  139. float x, float y) throws FontScalerException {
  140. if (nativeScaler != 0L) {
  141. return getGlyphVectorOutlineNative(font.get(),
  142. pScalerContext,
  143. nativeScaler,
  144. glyphs,
  145. numGlyphs,
  146. x, y);
  147. }
  148. return FontScaler
  149. .getNullScaler().getGlyphVectorOutline(0L, glyphs, numGlyphs, x, y);
  150. }
  151. synchronized long getLayoutTableCache() throws FontScalerException {
  152. return getLayoutTableCacheNative(nativeScaler);
  153. }
  154. public synchronized void dispose() {
  155. if (nativeScaler != 0L) {
  156. disposeNativeScaler(font.get(), nativeScaler);
  157. nativeScaler = 0L;
  158. }
  159. }
  160. synchronized int getNumGlyphs() throws FontScalerException {
  161. if (nativeScaler != 0L) {
  162. return getNumGlyphsNative(nativeScaler);
  163. }
  164. return FontScaler.getNullScaler().getNumGlyphs();
  165. }
  166. synchronized int getMissingGlyphCode() throws FontScalerException {
  167. if (nativeScaler != 0L) {
  168. return getMissingGlyphCodeNative(nativeScaler);
  169. }
  170. return FontScaler.getNullScaler().getMissingGlyphCode();
  171. }
  172. synchronized int getGlyphCode(char charCode) throws FontScalerException {
  173. if (nativeScaler != 0L) {
  174. return getGlyphCodeNative(font.get(), nativeScaler, charCode);
  175. }
  176. return FontScaler.getNullScaler().getGlyphCode(charCode);
  177. }
  178. synchronized Point2D.Float getGlyphPoint(long pScalerContext,
  179. int glyphCode, int ptNumber)
  180. throws FontScalerException {
  181. if (nativeScaler != 0L) {
  182. return getGlyphPointNative(font.get(), pScalerContext,
  183. nativeScaler, glyphCode, ptNumber);
  184. }
  185. return FontScaler.getNullScaler().getGlyphPoint(
  186. pScalerContext, glyphCode, ptNumber);
  187. }
  188. synchronized long getUnitsPerEm() {
  189. return getUnitsPerEMNative(nativeScaler);
  190. }
  191. long createScalerContext(double[] matrix,
  192. int aa, int fm, float boldness, float italic,
  193. boolean disableHinting) {
  194. if (nativeScaler != 0L) {
  195. return createScalerContextNative(nativeScaler, matrix,
  196. aa, fm, boldness, italic);
  197. }
  198. return NullFontScaler.getNullScalerContext();
  199. }
  200. //Note: native methods can throw RuntimeException if processing fails
  201. private native long initNativeScaler(Font2D font, int type,
  202. int indexInCollection, boolean supportsCJK, int filesize);
  203. private native StrikeMetrics getFontMetricsNative(Font2D font,
  204. long pScalerContext, long pScaler);
  205. private native float getGlyphAdvanceNative(Font2D font,
  206. long pScalerContext, long pScaler, int glyphCode);
  207. private native void getGlyphMetricsNative(Font2D font,
  208. long pScalerContext, long pScaler,
  209. int glyphCode, Point2D.Float metrics);
  210. private native long getGlyphImageNative(Font2D font,
  211. long pScalerContext, long pScaler, int glyphCode);
  212. private native Rectangle2D.Float getGlyphOutlineBoundsNative(Font2D font,
  213. long pScalerContext, long pScaler, int glyphCode);
  214. private native GeneralPath getGlyphOutlineNative(Font2D font,
  215. long pScalerContext, long pScaler,
  216. int glyphCode, float x, float y);
  217. private native GeneralPath getGlyphVectorOutlineNative(Font2D font,
  218. long pScalerContext, long pScaler,
  219. int[] glyphs, int numGlyphs, float x, float y);
  220. native Point2D.Float getGlyphPointNative(Font2D font,
  221. long pScalerContext, long pScaler, int glyphCode, int ptNumber);
  222. private native long getLayoutTableCacheNative(long pScaler);
  223. private native void disposeNativeScaler(Font2D font2D, long pScaler);
  224. private native int getGlyphCodeNative(Font2D font, long pScaler, char charCode);
  225. private native int getNumGlyphsNative(long pScaler);
  226. private native int getMissingGlyphCodeNative(long pScaler);
  227. private native long getUnitsPerEMNative(long pScaler);
  228. native long createScalerContextNative(long pScaler, double[] matrix,
  229. int aa, int fm, float boldness, float italic);
  230. /* Freetype scaler context does not contain any pointers that
  231. has to be invalidated if native scaler is bad */
  232. void invalidateScalerContext(long pScalerContext) {}
  233. }