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.

GlyphMapping.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable;
  22. import org.apache.fop.complexscripts.util.CharScript;
  23. import org.apache.fop.traits.MinOptMax;
  24. import org.apache.fop.util.CharUtilities;
  25. /**
  26. * Stores the mapping of a text fragment to glyphs, along with various information.
  27. */
  28. public class GlyphMapping {
  29. private static final Log LOG = LogFactory.getLog(GlyphMapping.class);
  30. /** Inclusive. */
  31. public final int startIndex;
  32. /** Exclusive. */
  33. public final int endIndex;
  34. private int wordCharLength;
  35. public final int wordSpaceCount;
  36. public int letterSpaceCount;
  37. public MinOptMax areaIPD;
  38. public final boolean isHyphenated;
  39. public final boolean isSpace;
  40. public boolean breakOppAfter;
  41. public final Font font;
  42. public final int level;
  43. public final int[][] gposAdjustments;
  44. public final String mapping;
  45. public GlyphMapping(int startIndex, int endIndex, int wordSpaceCount, int letterSpaceCount,
  46. MinOptMax areaIPD, boolean isHyphenated, boolean isSpace, boolean breakOppAfter,
  47. Font font, int level, int[][] gposAdjustments) {
  48. this(startIndex, endIndex, wordSpaceCount, letterSpaceCount, areaIPD, isHyphenated,
  49. isSpace, breakOppAfter, font, level, gposAdjustments, null);
  50. }
  51. public GlyphMapping(int startIndex, int endIndex, int wordSpaceCount, int letterSpaceCount,
  52. MinOptMax areaIPD, boolean isHyphenated, boolean isSpace, boolean breakOppAfter,
  53. Font font, int level, int[][] gposAdjustments, String mapping) {
  54. assert startIndex <= endIndex;
  55. this.startIndex = startIndex;
  56. this.endIndex = endIndex;
  57. this.wordCharLength = -1;
  58. this.wordSpaceCount = wordSpaceCount;
  59. this.letterSpaceCount = letterSpaceCount;
  60. this.areaIPD = areaIPD;
  61. this.isHyphenated = isHyphenated;
  62. this.isSpace = isSpace;
  63. this.breakOppAfter = breakOppAfter;
  64. this.font = font;
  65. this.level = level;
  66. this.gposAdjustments = gposAdjustments;
  67. this.mapping = mapping;
  68. }
  69. public static GlyphMapping doGlyphMapping(TextFragment text, int startIndex, int endIndex,
  70. Font font, MinOptMax letterSpaceIPD, MinOptMax[] letterSpaceAdjustArray,
  71. char precedingChar, char breakOpportunityChar, final boolean endsWithHyphen, int level) {
  72. GlyphMapping mapping;
  73. if (font.performsSubstitution() || font.performsPositioning()) {
  74. mapping = processWordMapping(text, startIndex, endIndex, font,
  75. breakOpportunityChar, endsWithHyphen, level);
  76. } else {
  77. mapping = processWordNoMapping(text, startIndex, endIndex, font,
  78. letterSpaceIPD, letterSpaceAdjustArray, precedingChar, breakOpportunityChar, endsWithHyphen,
  79. level);
  80. }
  81. return mapping;
  82. }
  83. private static GlyphMapping processWordMapping(TextFragment text, int startIndex,
  84. int endIndex, final Font font, final char breakOpportunityChar,
  85. final boolean endsWithHyphen, int level) {
  86. int e = endIndex; // end index of word in FOText character buffer
  87. int nLS = 0; // # of letter spaces
  88. String script = text.getScript();
  89. String language = text.getLanguage();
  90. if (LOG.isDebugEnabled()) {
  91. LOG.debug("PW: [" + startIndex + "," + endIndex + "]: {"
  92. + " +M"
  93. + ", level = " + level
  94. + " }");
  95. }
  96. // 1. extract unmapped character sequence
  97. CharSequence ics = text.subSequence(startIndex, e);
  98. // 2. if script is not specified (by FO property) or it is specified as 'auto',
  99. // then compute dominant script
  100. if ((script == null) || "auto".equals(script)) {
  101. script = CharScript.scriptTagFromCode(CharScript.dominantScript(ics));
  102. }
  103. if ((language == null) || "none".equals(language)) {
  104. language = "dflt";
  105. }
  106. // 3. perform mapping of chars to glyphs ... to glyphs ... to chars
  107. CharSequence mcs = font.performSubstitution(ics, script, language);
  108. // 4. compute glyph position adjustments on (substituted) characters
  109. int[][] gpa;
  110. if (font.performsPositioning()) {
  111. // handle GPOS adjustments
  112. gpa = font.performPositioning(mcs, script, language);
  113. } else if (font.hasKerning()) {
  114. // handle standard (non-GPOS) kerning adjustments
  115. gpa = getKerningAdjustments(mcs, font);
  116. } else {
  117. gpa = null;
  118. }
  119. // 5. reorder combining marks so that they precede (within the mapped char sequence) the
  120. // base to which they are applied; N.B. position adjustments (gpa) are reordered in place
  121. mcs = font.reorderCombiningMarks(mcs, gpa, script, language);
  122. // 6. compute word ipd based on final position adjustments
  123. MinOptMax ipd = MinOptMax.ZERO;
  124. for (int i = 0, n = mcs.length(); i < n; i++) {
  125. int c = mcs.charAt(i);
  126. // TODO !BMP
  127. int w = font.getCharWidth(c);
  128. if (w < 0) {
  129. w = 0;
  130. }
  131. if (gpa != null) {
  132. w += gpa[i][GlyphPositioningTable.Value.IDX_X_ADVANCE];
  133. }
  134. ipd = ipd.plus(w);
  135. }
  136. // [TBD] - handle letter spacing
  137. return new GlyphMapping(startIndex, e, 0, nLS, ipd, endsWithHyphen, false,
  138. breakOpportunityChar != 0, font, level, gpa,
  139. CharUtilities.isSameSequence(mcs, ics) ? null : mcs.toString());
  140. }
  141. /**
  142. * Given a mapped character sequence MCS, obtain glyph position adjustments from the
  143. * font's kerning data.
  144. *
  145. * @param mcs mapped character sequence
  146. * @param font applicable font
  147. * @return glyph position adjustments (or null if no kerning)
  148. */
  149. private static int[][] getKerningAdjustments(CharSequence mcs, final Font font) {
  150. int nc = mcs.length();
  151. // extract kerning array
  152. int[] ka = new int[nc]; // kerning array
  153. for (int i = 0, n = nc, cPrev = -1; i < n; i++) {
  154. int c = mcs.charAt(i);
  155. // TODO !BMP
  156. if (cPrev >= 0) {
  157. ka[i] = font.getKernValue(cPrev, c);
  158. }
  159. cPrev = c;
  160. }
  161. // was there a non-zero kerning?
  162. boolean hasKerning = false;
  163. for (int i = 0, n = nc; i < n; i++) {
  164. if (ka[i] != 0) {
  165. hasKerning = true;
  166. break;
  167. }
  168. }
  169. // if non-zero kerning, then create and return glyph position adjustment array
  170. if (hasKerning) {
  171. int[][] gpa = new int[nc][4];
  172. for (int i = 0, n = nc; i < n; i++) {
  173. if (i > 0) {
  174. gpa [i - 1][GlyphPositioningTable.Value.IDX_X_ADVANCE] = ka[i];
  175. }
  176. }
  177. return gpa;
  178. } else {
  179. return null;
  180. }
  181. }
  182. private static GlyphMapping processWordNoMapping(TextFragment text, int startIndex, int endIndex,
  183. final Font font, MinOptMax letterSpaceIPD, MinOptMax[] letterSpaceAdjustArray,
  184. char precedingChar, final char breakOpportunityChar, final boolean endsWithHyphen, int level) {
  185. boolean kerning = font.hasKerning();
  186. MinOptMax wordIPD = MinOptMax.ZERO;
  187. if (LOG.isDebugEnabled()) {
  188. LOG.debug("PW: [" + startIndex + "," + endIndex + "]: {"
  189. + " -M"
  190. + ", level = " + level
  191. + " }");
  192. }
  193. for (int i = startIndex; i < endIndex; i++) {
  194. char currentChar = text.charAt(i);
  195. // character width
  196. int charWidth = font.getCharWidth(currentChar);
  197. wordIPD = wordIPD.plus(charWidth);
  198. // kerning
  199. if (kerning) {
  200. int kern = 0;
  201. if (i > startIndex) {
  202. char previousChar = text.charAt(i - 1);
  203. kern = font.getKernValue(previousChar, currentChar);
  204. } else if (precedingChar != 0) {
  205. kern = font.getKernValue(precedingChar, currentChar);
  206. }
  207. if (kern != 0) {
  208. addToLetterAdjust(letterSpaceAdjustArray, i, kern);
  209. wordIPD = wordIPD.plus(kern);
  210. }
  211. }
  212. }
  213. if (kerning
  214. && (breakOpportunityChar != 0)
  215. && !isSpace(breakOpportunityChar)
  216. && endIndex > 0
  217. && endsWithHyphen) {
  218. int kern = font.getKernValue(text.charAt(endIndex - 1), breakOpportunityChar);
  219. if (kern != 0) {
  220. addToLetterAdjust(letterSpaceAdjustArray, endIndex, kern);
  221. // TODO: add kern to wordIPD?
  222. }
  223. }
  224. // shy+chars at start of word: wordLength == 0 && breakOpportunity
  225. // shy only characters in word: wordLength == 0 && !breakOpportunity
  226. int wordLength = endIndex - startIndex;
  227. int letterSpaces = 0;
  228. if (wordLength != 0) {
  229. letterSpaces = wordLength - 1;
  230. // if there is a break opportunity and the next one (break character)
  231. // is not a space, it could be used as a line end;
  232. // add one more letter space, in case other text follows
  233. if ((breakOpportunityChar != 0) && !isSpace(breakOpportunityChar)) {
  234. letterSpaces++;
  235. }
  236. }
  237. assert letterSpaces >= 0;
  238. wordIPD = wordIPD.plus(letterSpaceIPD.mult(letterSpaces));
  239. // create and return the AreaInfo object
  240. return new GlyphMapping(startIndex, endIndex, 0,
  241. letterSpaces, wordIPD,
  242. endsWithHyphen,
  243. false, breakOpportunityChar != 0, font, level, null);
  244. }
  245. private static void addToLetterAdjust(MinOptMax[] letterSpaceAdjustArray, int index, int width) {
  246. if (letterSpaceAdjustArray[index] == null) {
  247. letterSpaceAdjustArray[index] = MinOptMax.getInstance(width);
  248. } else {
  249. letterSpaceAdjustArray[index] = letterSpaceAdjustArray[index].plus(width);
  250. }
  251. }
  252. /**
  253. * Indicates whether a character is a space in terms of this layout manager.
  254. *
  255. * @param ch the character
  256. * @return true if it's a space
  257. */
  258. public static boolean isSpace(final char ch) {
  259. return ch == CharUtilities.SPACE
  260. || CharUtilities.isNonBreakableSpace(ch)
  261. || CharUtilities.isFixedWidthSpace(ch);
  262. }
  263. /**
  264. * Obtain number of 'characters' contained in word. If word is mapped, then this
  265. * number may be less than or greater than the original length (breakIndex -
  266. * startIndex). We compute and memoize thius length upon first invocation of this
  267. * method.
  268. */
  269. public int getWordLength() {
  270. if (wordCharLength == -1) {
  271. if (mapping != null) {
  272. wordCharLength = mapping.length();
  273. } else {
  274. assert endIndex >= startIndex;
  275. wordCharLength = endIndex - startIndex;
  276. }
  277. }
  278. return wordCharLength;
  279. }
  280. public void addToAreaIPD(MinOptMax idp) {
  281. areaIPD = areaIPD.plus(idp);
  282. }
  283. public String toString() {
  284. return super.toString() + "{"
  285. + "interval = [" + startIndex + "," + endIndex + "]"
  286. + ", isSpace = " + isSpace
  287. + ", level = " + level
  288. + ", areaIPD = " + areaIPD
  289. + ", letterSpaceCount = " + letterSpaceCount
  290. + ", wordSpaceCount = " + wordSpaceCount
  291. + ", isHyphenated = " + isHyphenated
  292. + ", font = " + font
  293. + "}";
  294. }
  295. }