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.

TextLayoutManager.java 68KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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.layoutmgr.inline;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.LinkedList;
  22. import java.util.ListIterator;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.area.Trait;
  26. import org.apache.fop.area.inline.TextArea;
  27. import org.apache.fop.fo.Constants;
  28. import org.apache.fop.fo.FOText;
  29. import org.apache.fop.fonts.Font;
  30. import org.apache.fop.layoutmgr.InlineKnuthSequence;
  31. import org.apache.fop.layoutmgr.KnuthBox;
  32. import org.apache.fop.layoutmgr.KnuthElement;
  33. import org.apache.fop.layoutmgr.KnuthGlue;
  34. import org.apache.fop.layoutmgr.KnuthPenalty;
  35. import org.apache.fop.layoutmgr.KnuthSequence;
  36. import org.apache.fop.layoutmgr.LayoutContext;
  37. import org.apache.fop.layoutmgr.LeafPosition;
  38. import org.apache.fop.layoutmgr.Position;
  39. import org.apache.fop.layoutmgr.PositionIterator;
  40. import org.apache.fop.layoutmgr.TraitSetter;
  41. import org.apache.fop.text.linebreak.LineBreakStatus;
  42. import org.apache.fop.traits.MinOptMax;
  43. import org.apache.fop.traits.SpaceVal;
  44. import org.apache.fop.util.CharUtilities;
  45. /**
  46. * LayoutManager for text (a sequence of characters) which generates one
  47. * or more inline areas.
  48. */
  49. public class TextLayoutManager extends LeafNodeLayoutManager {
  50. /**
  51. * Store information about each potential text area.
  52. * Index of character which ends the area, IPD of area, including
  53. * any word-space and letter-space.
  54. * Number of word-spaces?
  55. */
  56. private class AreaInfo {
  57. private short iStartIndex;
  58. private short iBreakIndex;
  59. private short iWScount;
  60. private short iLScount;
  61. private MinOptMax ipdArea;
  62. private boolean bHyphenated;
  63. private boolean isSpace;
  64. private boolean breakOppAfter;
  65. public AreaInfo(short iSIndex, short iBIndex, short iWS, short iLS,
  66. MinOptMax ipd, boolean bHyph, boolean isSpace, boolean breakOppAfter) {
  67. iStartIndex = iSIndex;
  68. iBreakIndex = iBIndex;
  69. iWScount = iWS;
  70. iLScount = iLS;
  71. ipdArea = ipd;
  72. bHyphenated = bHyph;
  73. this.isSpace = isSpace;
  74. this.breakOppAfter = breakOppAfter;
  75. }
  76. public String toString() {
  77. return "[ lscnt=" + iLScount
  78. + ", wscnt=" + iWScount
  79. + ", ipd=" + ipdArea.toString()
  80. + ", sidx=" + iStartIndex
  81. + ", bidx=" + iBreakIndex
  82. + ", hyph=" + bHyphenated
  83. + ", space=" + isSpace
  84. + "]";
  85. }
  86. }
  87. // this class stores information about changes in vecAreaInfo
  88. // which are not yet applied
  89. private class PendingChange {
  90. public AreaInfo ai;
  91. public int index;
  92. public PendingChange(AreaInfo ai, int index) {
  93. this.ai = ai;
  94. this.index = index;
  95. }
  96. }
  97. /**
  98. * logging instance
  99. */
  100. private static Log log = LogFactory.getLog(TextLayoutManager.class);
  101. // Hold all possible breaks for the text in this LM's FO.
  102. private ArrayList vecAreaInfo;
  103. /** Non-space characters on which we can end a line. */
  104. private static final String BREAK_CHARS = "-/";
  105. /** Used to reduce instantiation of MinOptMax with zero length. Do not modify! */
  106. private static final MinOptMax ZERO_MINOPTMAX = new MinOptMax(0);
  107. private FOText foText;
  108. private char[] textArray;
  109. /**
  110. * Contains an array of widths to adjust for kerning. The first entry can
  111. * be used to influence the start position of the first letter. The entry i+1 defines the
  112. * cursor advancement after the character i. A null entry means no special advancement.
  113. */
  114. private MinOptMax[] letterAdjustArray; //size = textArray.length + 1
  115. private static final char NEWLINE = '\n';
  116. private Font font = null;
  117. /** Start index of first character in this parent Area */
  118. private short iAreaStart = 0;
  119. /** Start index of next TextArea */
  120. private short iNextStart = 0;
  121. /** Size since last makeArea call, except for last break */
  122. private MinOptMax ipdTotal;
  123. /** Size including last break possibility returned */
  124. // private MinOptMax nextIPD = new MinOptMax(0);
  125. /** size of a space character (U+0020) glyph in current font */
  126. private int spaceCharIPD;
  127. private MinOptMax wordSpaceIPD;
  128. private MinOptMax letterSpaceIPD;
  129. /** size of the hyphen character glyph in current font */
  130. private int hyphIPD;
  131. /** 1/1 of word-spacing value */
  132. private SpaceVal ws;
  133. /** 1/2 of word-spacing value */
  134. private SpaceVal halfWS;
  135. /** 1/2 of letter-spacing value */
  136. private SpaceVal halfLS;
  137. /** Number of space characters after previous possible break position. */
  138. private int iNbSpacesPending;
  139. private boolean bChanged = false;
  140. private int iReturnedIndex = 0;
  141. private short iThisStart = 0;
  142. private short iTempStart = 0;
  143. private LinkedList changeList = null;
  144. private AlignmentContext alignmentContext = null;
  145. private int lineStartBAP = 0;
  146. private int lineEndBAP = 0;
  147. /**
  148. * Create a Text layout manager.
  149. *
  150. * @param node The FOText object to be rendered
  151. */
  152. public TextLayoutManager(FOText node) {
  153. super();
  154. foText = node;
  155. textArray = new char[node.endIndex - node.startIndex];
  156. System.arraycopy(node.ca, node.startIndex, textArray, 0,
  157. node.endIndex - node.startIndex);
  158. letterAdjustArray = new MinOptMax[textArray.length + 1];
  159. vecAreaInfo = new java.util.ArrayList();
  160. }
  161. /** @see org.apache.fop.layoutmgr.LayoutManager#initialize */
  162. public void initialize() {
  163. font = foText.getCommonFont().getFontState(foText.getFOEventHandler().getFontInfo(), this);
  164. // With CID fonts, space isn't neccesary currentFontState.width(32)
  165. spaceCharIPD = font.getCharWidth(' ');
  166. // Use hyphenationChar property
  167. hyphIPD = font.getCharWidth(foText.getCommonHyphenation().hyphenationCharacter);
  168. SpaceVal ls = SpaceVal.makeLetterSpacing(foText.getLetterSpacing());
  169. halfLS = new SpaceVal(MinOptMax.multiply(ls.getSpace(), 0.5),
  170. ls.isConditional(), ls.isForcing(), ls.getPrecedence());
  171. ws = SpaceVal.makeWordSpacing(foText.getWordSpacing(), ls, font);
  172. // Make half-space: <space> on either side of a word-space)
  173. halfWS = new SpaceVal(MinOptMax.multiply(ws.getSpace(), 0.5),
  174. ws.isConditional(), ws.isForcing(), ws.getPrecedence());
  175. // letter space applies only to consecutive non-space characters,
  176. // while word space applies to space characters;
  177. // i.e. the spaces in the string "A SIMPLE TEST" are:
  178. // A<<ws>>S<ls>I<ls>M<ls>P<ls>L<ls>E<<ws>>T<ls>E<ls>S<ls>T
  179. // there is no letter space after the last character of a word,
  180. // nor after a space character
  181. // NOTE: The above is not quite correct. Read on in XSL 1.0, 7.16.2, letter-spacing
  182. // set letter space and word space dimension;
  183. // the default value "normal" was converted into a MinOptMax value
  184. // in the SpaceVal.makeWordSpacing() method
  185. letterSpaceIPD = ls.getSpace();
  186. wordSpaceIPD = MinOptMax.add(new MinOptMax(spaceCharIPD), ws.getSpace());
  187. }
  188. /**
  189. * Reset position for returning next BreakPossibility.
  190. *
  191. * @param prevPos the position to reset to
  192. */
  193. public void resetPosition(Position prevPos) {
  194. if (prevPos != null) {
  195. // ASSERT (prevPos.getLM() == this)
  196. if (prevPos.getLM() != this) {
  197. log.error("TextLayoutManager.resetPosition: "
  198. + "LM mismatch!!!");
  199. }
  200. LeafPosition tbp = (LeafPosition) prevPos;
  201. AreaInfo ai = (AreaInfo) vecAreaInfo.get(tbp.getLeafPos());
  202. if (ai.iBreakIndex != iNextStart) {
  203. iNextStart = ai.iBreakIndex;
  204. vecAreaInfo.ensureCapacity(tbp.getLeafPos() + 1);
  205. // TODO: reset or recalculate total IPD = sum of all word IPD
  206. // up to the break position
  207. ipdTotal = ai.ipdArea;
  208. setFinished(false);
  209. }
  210. } else {
  211. // Reset to beginning!
  212. vecAreaInfo.clear();
  213. iNextStart = 0;
  214. setFinished(false);
  215. }
  216. }
  217. // TODO: see if we can use normal getNextBreakPoss for this with
  218. // extra hyphenation information in LayoutContext
  219. private boolean getHyphenIPD(HyphContext hc, MinOptMax hyphIPD) {
  220. // Skip leading word-space before calculating count?
  221. boolean bCanHyphenate = true;
  222. int iStopIndex = iNextStart + hc.getNextHyphPoint();
  223. if (textArray.length < iStopIndex) {
  224. iStopIndex = textArray.length;
  225. bCanHyphenate = false;
  226. }
  227. hc.updateOffset(iStopIndex - iNextStart);
  228. for (; iNextStart < iStopIndex; iNextStart++) {
  229. char c = textArray[iNextStart];
  230. hyphIPD.opt += font.getCharWidth(c);
  231. // letter-space?
  232. }
  233. // Need to include hyphen size too, but don't count it in the
  234. // stored running total, since it would be double counted
  235. // with later hyphenation points
  236. return bCanHyphenate;
  237. }
  238. /**
  239. * Generate and add areas to parent area.
  240. * This can either generate an area for each TextArea and each space, or
  241. * an area containing all text with a parameter controlling the size of
  242. * the word space. The latter is most efficient for PDF generation.
  243. * Set size of each area.
  244. * @param posIter Iterator over Position information returned
  245. * by this LayoutManager.
  246. * @param context LayoutContext for adjustments
  247. */
  248. public void addAreas(PositionIterator posIter, LayoutContext context) {
  249. // Add word areas
  250. AreaInfo ai = null;
  251. int iWScount = 0;
  252. int iLScount = 0;
  253. int firstAreaInfoIndex = -1;
  254. int lastAreaInfoIndex = 0;
  255. MinOptMax realWidth = new MinOptMax(0);
  256. /* On first area created, add any leading space.
  257. * Calculate word-space stretch value.
  258. */
  259. while (posIter.hasNext()) {
  260. LeafPosition tbpNext = (LeafPosition) posIter.next();
  261. if (tbpNext == null) {
  262. continue; //Ignore elements without Positions
  263. }
  264. if (tbpNext.getLeafPos() != -1) {
  265. ai = (AreaInfo) vecAreaInfo.get(tbpNext.getLeafPos());
  266. if (firstAreaInfoIndex == -1) {
  267. firstAreaInfoIndex = tbpNext.getLeafPos();
  268. }
  269. iWScount += ai.iWScount;
  270. iLScount += ai.iLScount;
  271. realWidth.add(ai.ipdArea);
  272. lastAreaInfoIndex = tbpNext.getLeafPos();
  273. }
  274. }
  275. if (ai == null) {
  276. return;
  277. }
  278. int textLength = ai.iBreakIndex - ai.iStartIndex;
  279. if (ai.iLScount == textLength && !ai.bHyphenated
  280. && context.isLastArea()) {
  281. // the line ends at a character like "/" or "-";
  282. // remove the letter space after the last character
  283. realWidth.add(MinOptMax.multiply(letterSpaceIPD, -1));
  284. iLScount--;
  285. }
  286. for (int i = ai.iStartIndex; i < ai.iBreakIndex; i++) {
  287. MinOptMax ladj = letterAdjustArray[i + 1];
  288. if (ladj != null && ladj.isElastic()) {
  289. iLScount++;
  290. }
  291. }
  292. // add hyphenation character if the last word is hyphenated
  293. if (context.isLastArea() && ai.bHyphenated) {
  294. realWidth.add(new MinOptMax(hyphIPD));
  295. }
  296. // Calculate adjustments
  297. int iDifference = 0;
  298. int iTotalAdjust = 0;
  299. int iWordSpaceDim = wordSpaceIPD.opt;
  300. int iLetterSpaceDim = letterSpaceIPD.opt;
  301. double dIPDAdjust = context.getIPDAdjust();
  302. double dSpaceAdjust = context.getSpaceAdjust(); // not used
  303. // calculate total difference between real and available width
  304. if (dIPDAdjust > 0.0) {
  305. iDifference = (int) ((double) (realWidth.max - realWidth.opt)
  306. * dIPDAdjust);
  307. } else {
  308. iDifference = (int) ((double) (realWidth.opt - realWidth.min)
  309. * dIPDAdjust);
  310. }
  311. // set letter space adjustment
  312. if (dIPDAdjust > 0.0) {
  313. iLetterSpaceDim
  314. += (int) ((double) (letterSpaceIPD.max - letterSpaceIPD.opt)
  315. * dIPDAdjust);
  316. } else {
  317. iLetterSpaceDim
  318. += (int) ((double) (letterSpaceIPD.opt - letterSpaceIPD.min)
  319. * dIPDAdjust);
  320. }
  321. iTotalAdjust += (iLetterSpaceDim - letterSpaceIPD.opt) * iLScount;
  322. // set word space adjustment
  323. //
  324. if (iWScount > 0) {
  325. iWordSpaceDim += (int) ((iDifference - iTotalAdjust) / iWScount);
  326. } else {
  327. // there are no word spaces in this area
  328. }
  329. iTotalAdjust += (iWordSpaceDim - wordSpaceIPD.opt) * iWScount;
  330. if (iTotalAdjust != iDifference) {
  331. // the applied adjustment is greater or smaller than the needed one
  332. log.trace("TextLM.addAreas: error in word / letter space adjustment = "
  333. + (iTotalAdjust - iDifference));
  334. // set iTotalAdjust = iDifference, so that the width of the TextArea
  335. // will counterbalance the error and the other inline areas will be
  336. // placed correctly
  337. iTotalAdjust = iDifference;
  338. }
  339. TextArea t = createTextArea(realWidth, iTotalAdjust, context,
  340. wordSpaceIPD.opt - spaceCharIPD,
  341. firstAreaInfoIndex, lastAreaInfoIndex,
  342. context.isLastArea());
  343. // iWordSpaceDim is computed in relation to wordSpaceIPD.opt
  344. // but the renderer needs to know the adjustment in relation
  345. // to the size of the space character in the current font;
  346. // moreover, the pdf renderer adds the character spacing even to
  347. // the last character of a word and to space characters: in order
  348. // to avoid this, we must subtract the letter space width twice;
  349. // the renderer will compute the space width as:
  350. // space width =
  351. // = "normal" space width + letterSpaceAdjust + wordSpaceAdjust
  352. // = spaceCharIPD + letterSpaceAdjust +
  353. // + (iWordSpaceDim - spaceCharIPD - 2 * letterSpaceAdjust)
  354. // = iWordSpaceDim - letterSpaceAdjust
  355. t.setTextLetterSpaceAdjust(iLetterSpaceDim);
  356. t.setTextWordSpaceAdjust(iWordSpaceDim - spaceCharIPD
  357. - 2 * t.getTextLetterSpaceAdjust());
  358. if (context.getIPDAdjust() != 0) {
  359. // add information about space width
  360. t.setSpaceDifference(wordSpaceIPD.opt - spaceCharIPD
  361. - 2 * t.getTextLetterSpaceAdjust());
  362. }
  363. parentLM.addChildArea(t);
  364. }
  365. /**
  366. * Create an inline word area.
  367. * This creates a TextArea and sets up the various attributes.
  368. *
  369. * @param width the MinOptMax width of the content
  370. * @param adjust the total ipd adjustment with respect to the optimal width
  371. * @param context the layout context
  372. * @param spaceDiff unused
  373. * @param firstIndex the index of the first AreaInfo used for the TextArea
  374. * @param lastIndex the index of the last AreaInfo used for the TextArea
  375. * @param isLastArea is this TextArea the last in a line?
  376. * @return the new text area
  377. */
  378. protected TextArea createTextArea(MinOptMax width, int adjust,
  379. LayoutContext context, int spaceDiff,
  380. int firstIndex, int lastIndex, boolean isLastArea) {
  381. TextArea textArea;
  382. if (context.getIPDAdjust() == 0.0) {
  383. // create just a TextArea
  384. textArea = new TextArea();
  385. } else {
  386. // justified area: create a TextArea with extra info
  387. // about potential adjustments
  388. textArea = new TextArea(width.max - width.opt,
  389. width.opt - width.min,
  390. adjust);
  391. }
  392. textArea.setIPD(width.opt + adjust);
  393. textArea.setBPD(font.getAscender() - font.getDescender());
  394. textArea.setBaselineOffset(font.getAscender());
  395. if (textArea.getBPD() == alignmentContext.getHeight()) {
  396. textArea.setOffset(0);
  397. } else {
  398. textArea.setOffset(alignmentContext.getOffset());
  399. }
  400. // set the text of the TextArea, split into words and spaces
  401. int wordStartIndex = -1;
  402. AreaInfo areaInfo;
  403. int len = 0;
  404. for (int i = firstIndex; i <= lastIndex; i++) {
  405. areaInfo = (AreaInfo) vecAreaInfo.get(i);
  406. if (areaInfo.isSpace) {
  407. // areaInfo stores information about spaces
  408. // add the spaces - except zero-width spaces - to the TextArea
  409. for (int j = areaInfo.iStartIndex; j < areaInfo.iBreakIndex; j++) {
  410. char spaceChar = textArray[j];
  411. if (!CharUtilities.isZeroWidthSpace(spaceChar)) {
  412. textArea.addSpace(spaceChar, 0,
  413. CharUtilities.isAdjustableSpace(spaceChar));
  414. }
  415. }
  416. } else {
  417. // areaInfo stores information about a word fragment
  418. if (wordStartIndex == -1) {
  419. // here starts a new word
  420. wordStartIndex = i;
  421. len = 0;
  422. }
  423. len += areaInfo.iBreakIndex - areaInfo.iStartIndex;
  424. if (i == lastIndex || ((AreaInfo) vecAreaInfo.get(i + 1)).isSpace) {
  425. // here ends a new word
  426. // add a word to the TextArea
  427. if (isLastArea
  428. && i == lastIndex
  429. && areaInfo.bHyphenated) {
  430. len++;
  431. }
  432. StringBuffer wordChars = new StringBuffer(len);
  433. int[] letterAdjust = new int[len];
  434. int letter = 0;
  435. for (int j = wordStartIndex; j <= i; j++) {
  436. AreaInfo ai = (AreaInfo) vecAreaInfo.get(j);
  437. int lsCount = ai.iLScount;
  438. wordChars.append(textArray, ai.iStartIndex, ai.iBreakIndex - ai.iStartIndex);
  439. for (int k = 0; k < ai.iBreakIndex - ai.iStartIndex; k++) {
  440. MinOptMax adj = letterAdjustArray[ai.iStartIndex + k];
  441. if (letter > 0) {
  442. letterAdjust[letter] = (adj != null ? adj.opt : 0);
  443. }
  444. if (lsCount > 0) {
  445. letterAdjust[letter] += textArea.getTextLetterSpaceAdjust();
  446. lsCount--;
  447. }
  448. letter++;
  449. }
  450. }
  451. // String wordChars = new String(textArray, wordStartIndex, len);
  452. if (isLastArea
  453. && i == lastIndex
  454. && areaInfo.bHyphenated) {
  455. // add the hyphenation character
  456. wordChars.append(foText.getCommonHyphenation().hyphenationCharacter);
  457. }
  458. textArea.addWord(wordChars.toString(), 0, letterAdjust);
  459. wordStartIndex = -1;
  460. }
  461. }
  462. }
  463. TraitSetter.addFontTraits(textArea, font);
  464. textArea.addTrait(Trait.COLOR, foText.getColor());
  465. TraitSetter.addTextDecoration(textArea, foText.getTextDecoration());
  466. return textArea;
  467. }
  468. private void addToLetterAdjust(int index, int width) {
  469. if (letterAdjustArray[index] == null) {
  470. letterAdjustArray[index] = new MinOptMax(width);
  471. } else {
  472. letterAdjustArray[index].add(width);
  473. }
  474. }
  475. private void addToLetterAdjust(int index, MinOptMax width) {
  476. if (letterAdjustArray[index] == null) {
  477. letterAdjustArray[index] = new MinOptMax(width);
  478. } else {
  479. letterAdjustArray[index].add(width);
  480. }
  481. }
  482. /**
  483. * Indicates whether a character is a space in terms of this layout manager.
  484. * @param ch the character
  485. * @return true if it's a space
  486. */
  487. private static boolean isSpace(final char ch) {
  488. return ch == CharUtilities.SPACE
  489. || ch == CharUtilities.NBSPACE
  490. || CharUtilities.isFixedWidthSpace(ch);
  491. }
  492. /** @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(LayoutContext, int) */
  493. public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
  494. lineStartBAP = context.getLineStartBorderAndPaddingWidth();
  495. lineEndBAP = context.getLineEndBorderAndPaddingWidth();
  496. alignmentContext = context.getAlignmentContext();
  497. LinkedList returnList = new LinkedList();
  498. KnuthSequence sequence = new InlineKnuthSequence();
  499. AreaInfo ai = null;
  500. AreaInfo prevAi = null;
  501. returnList.add(sequence);
  502. LineBreakStatus lbs = new LineBreakStatus();
  503. iThisStart = iNextStart;
  504. boolean inWord = false;
  505. boolean inWhitespace = false;
  506. char ch = 0;
  507. while (iNextStart < textArray.length) {
  508. ch = textArray[iNextStart];
  509. boolean breakOpportunity = false;
  510. byte breakAction = lbs.nextChar(ch);
  511. switch (breakAction) {
  512. case LineBreakStatus.COMBINING_PROHIBITED_BREAK:
  513. case LineBreakStatus.PROHIBITED_BREAK:
  514. break;
  515. case LineBreakStatus.EXPLICIT_BREAK:
  516. break;
  517. case LineBreakStatus.COMBINING_INDIRECT_BREAK:
  518. case LineBreakStatus.DIRECT_BREAK:
  519. case LineBreakStatus.INDIRECT_BREAK:
  520. breakOpportunity = true;
  521. break;
  522. default:
  523. log.error("Unexpected breakAction: " + breakAction);
  524. }
  525. if (inWord) {
  526. if (breakOpportunity || isSpace(ch) || ch == NEWLINE) {
  527. //Word boundary found, process widths and kerning
  528. int lastIndex = iNextStart;
  529. while (lastIndex > 0 && textArray[lastIndex - 1] == CharUtilities.SOFT_HYPHEN) {
  530. lastIndex--;
  531. }
  532. int wordLength = lastIndex - iThisStart;
  533. boolean kerning = font.hasKerning();
  534. MinOptMax wordIPD = new MinOptMax(0);
  535. for (int i = iThisStart; i < lastIndex; i++) {
  536. char c = textArray[i];
  537. //character width
  538. int charWidth = font.getCharWidth(c);
  539. wordIPD.add(charWidth);
  540. //kerning
  541. if (kerning) {
  542. int kern = 0;
  543. if (i > iThisStart) {
  544. char previous = textArray[i - 1];
  545. kern = font.getKernValue(previous, c) * font.getFontSize() / 1000;
  546. } else if (prevAi != null && !prevAi.isSpace && prevAi.iBreakIndex > 0) {
  547. char previous = textArray[prevAi.iBreakIndex - 1];
  548. kern = font.getKernValue(previous, c) * font.getFontSize() / 1000;
  549. }
  550. if (kern != 0) {
  551. //log.info("Kerning between " + previous + " and " + c + ": " + kern);
  552. addToLetterAdjust(i, kern);
  553. wordIPD.add(kern);
  554. }
  555. }
  556. }
  557. if (kerning && breakOpportunity && !isSpace(ch) && lastIndex > 0 && textArray[lastIndex] == CharUtilities.SOFT_HYPHEN) {
  558. int kern = font.getKernValue(textArray[lastIndex - 1], ch) * font.getFontSize() / 1000;
  559. if (kern != 0) {
  560. addToLetterAdjust(lastIndex, kern);
  561. }
  562. }
  563. int iLetterSpaces = wordLength - 1;
  564. // if there is a break opportunity and the next one
  565. // is not a space, it could be used as a line end;
  566. // add one more letter space, in case other text follows
  567. if (breakOpportunity && !isSpace(ch)) {
  568. iLetterSpaces++;
  569. }
  570. wordIPD.add(MinOptMax.multiply(letterSpaceIPD, iLetterSpaces));
  571. // create the AreaInfo object
  572. ai = new AreaInfo(iThisStart, (short)lastIndex, (short) 0,
  573. (short) iLetterSpaces,
  574. wordIPD, textArray[lastIndex] == CharUtilities.SOFT_HYPHEN, false, breakOpportunity);
  575. vecAreaInfo.add(ai);
  576. prevAi = ai;
  577. iTempStart = iNextStart;
  578. // create the elements
  579. sequence.addAll(createElementsForAWordFragment(alignment, ai,
  580. vecAreaInfo.size() - 1, letterSpaceIPD));
  581. ai = null;
  582. iThisStart = iNextStart;
  583. }
  584. } else if (inWhitespace) {
  585. if (ch != CharUtilities.SPACE || breakOpportunity) {
  586. // End of whitespace
  587. // create the AreaInfo object
  588. ai = new AreaInfo(iThisStart, (short) (iNextStart),
  589. (short) (iNextStart - iThisStart), (short) 0,
  590. MinOptMax.multiply(wordSpaceIPD, iNextStart - iThisStart),
  591. false, true, breakOpportunity);
  592. vecAreaInfo.add(ai);
  593. prevAi = ai;
  594. // create the elements
  595. sequence.addAll
  596. (createElementsForASpace(alignment, ai, vecAreaInfo.size() - 1));
  597. ai = null;
  598. iThisStart = iNextStart;
  599. }
  600. } else {
  601. if (ai != null) {
  602. vecAreaInfo.add(ai);
  603. prevAi = ai;
  604. ai.breakOppAfter = ch == CharUtilities.SPACE || breakOpportunity;
  605. sequence.addAll
  606. (createElementsForASpace(alignment, ai, vecAreaInfo.size() - 1));
  607. ai = null;
  608. }
  609. if (breakAction == LineBreakStatus.EXPLICIT_BREAK) {
  610. if (lineEndBAP != 0) {
  611. sequence.add
  612. (new KnuthGlue(lineEndBAP, 0, 0,
  613. new LeafPosition(this, -1), true));
  614. }
  615. sequence.endSequence();
  616. sequence = new InlineKnuthSequence();
  617. returnList.add(sequence);
  618. }
  619. }
  620. if ((ch == CharUtilities.SPACE
  621. && foText.getWhitespaceTreatment() == Constants.EN_PRESERVE)
  622. || ch == CharUtilities.NBSPACE) {
  623. // preserved space or non-breaking space:
  624. // create the AreaInfo object
  625. ai = new AreaInfo(iNextStart, (short) (iNextStart + 1),
  626. (short) 1, (short) 0,
  627. wordSpaceIPD, false, true, breakOpportunity);
  628. iThisStart = (short) (iNextStart + 1);
  629. } else if (CharUtilities.isFixedWidthSpace(ch)) {
  630. // create the AreaInfo object
  631. MinOptMax ipd = new MinOptMax(font.getCharWidth(ch));
  632. ai = new AreaInfo(iNextStart, (short) (iNextStart + 1),
  633. (short) 0, (short) 0,
  634. ipd, false, true, breakOpportunity);
  635. iThisStart = (short) (iNextStart + 1);
  636. } else if (ch == NEWLINE) {
  637. // linefeed; this can happen when linefeed-treatment="preserve"
  638. iThisStart = (short) (iNextStart + 1);
  639. }
  640. inWord = !isSpace(ch) && ch != NEWLINE;
  641. inWhitespace = ch == CharUtilities.SPACE && foText.getWhitespaceTreatment() != Constants.EN_PRESERVE;
  642. iNextStart++;
  643. } // end of while
  644. // Process any last elements
  645. if (inWord) {
  646. int lastIndex = iNextStart;
  647. if (textArray[iNextStart - 1] == CharUtilities.SOFT_HYPHEN) {
  648. lastIndex--;
  649. }
  650. int wordLength = lastIndex - iThisStart;
  651. boolean kerning = font.hasKerning();
  652. MinOptMax wordIPD = new MinOptMax(0);
  653. for (int i = iThisStart; i < lastIndex; i++) {
  654. char c = textArray[i];
  655. //character width
  656. int charWidth = font.getCharWidth(c);
  657. wordIPD.add(charWidth);
  658. //kerning
  659. if (kerning) {
  660. int kern = 0;
  661. if (i > iThisStart) {
  662. char previous = textArray[i - 1];
  663. kern = font.getKernValue(previous, c) * font.getFontSize() / 1000;
  664. } else if (prevAi != null && !prevAi.isSpace) {
  665. char previous = textArray[prevAi.iBreakIndex - 1];
  666. kern = font.getKernValue(previous, c) * font.getFontSize() / 1000;
  667. }
  668. if (kern != 0) {
  669. //log.info("Kerning between " + previous + " and " + c + ": " + kern);
  670. addToLetterAdjust(i, kern);
  671. wordIPD.add(kern);
  672. }
  673. }
  674. }
  675. int iLetterSpaces = wordLength - 1;
  676. wordIPD.add(MinOptMax.multiply(letterSpaceIPD, iLetterSpaces));
  677. // create the AreaInfo object
  678. ai = new AreaInfo(iThisStart, (short)lastIndex, (short) 0,
  679. (short) iLetterSpaces,
  680. wordIPD, false, false, false);
  681. vecAreaInfo.add(ai);
  682. iTempStart = iNextStart;
  683. // create the elements
  684. sequence.addAll(createElementsForAWordFragment(alignment, ai,
  685. vecAreaInfo.size() - 1, letterSpaceIPD));
  686. ai = null;
  687. } else if (inWhitespace) {
  688. ai = new AreaInfo(iThisStart, (short) (iNextStart),
  689. (short) (iNextStart - iThisStart), (short) 0,
  690. MinOptMax.multiply(wordSpaceIPD, iNextStart - iThisStart),
  691. false, true, true);
  692. vecAreaInfo.add(ai);
  693. // create the elements
  694. sequence.addAll
  695. (createElementsForASpace(alignment, ai, vecAreaInfo.size() - 1));
  696. ai = null;
  697. } else if (ai != null) {
  698. vecAreaInfo.add(ai);
  699. ai.breakOppAfter = ch == CharUtilities.ZERO_WIDTH_SPACE;
  700. sequence.addAll
  701. (createElementsForASpace(alignment, ai, vecAreaInfo.size() - 1));
  702. ai = null;
  703. } else if (ch == NEWLINE) {
  704. if (lineEndBAP != 0) {
  705. sequence.add
  706. (new KnuthGlue(lineEndBAP, 0, 0,
  707. new LeafPosition(this, -1), true));
  708. }
  709. sequence.endSequence();
  710. sequence = new InlineKnuthSequence();
  711. returnList.add(sequence);
  712. }
  713. if (((List)returnList.getLast()).size() == 0) {
  714. //Remove an empty sequence because of a trailing newline
  715. returnList.removeLast();
  716. }
  717. setFinished(true);
  718. if (returnList.size() > 0) {
  719. return returnList;
  720. } else {
  721. return null;
  722. }
  723. }
  724. /** @see InlineLevelLayoutManager#addALetterSpaceTo(List) */
  725. public List addALetterSpaceTo(List oldList) {
  726. // old list contains only a box, or the sequence: box penalty glue box;
  727. // look at the Position stored in the first element in oldList
  728. // which is always a box
  729. ListIterator oldListIterator = oldList.listIterator();
  730. KnuthElement el = (KnuthElement)oldListIterator.next();
  731. LeafPosition pos = (LeafPosition) ((KnuthBox) el).getPosition();
  732. AreaInfo ai = (AreaInfo) vecAreaInfo.get(pos.getLeafPos());
  733. ai.iLScount++;
  734. ai.ipdArea.add(letterSpaceIPD);
  735. if (BREAK_CHARS.indexOf(textArray[iTempStart - 1]) >= 0) {
  736. // the last character could be used as a line break
  737. // append new elements to oldList
  738. oldListIterator = oldList.listIterator(oldList.size());
  739. oldListIterator.add(new KnuthPenalty(0, KnuthPenalty.FLAGGED_PENALTY, true,
  740. new LeafPosition(this, -1), false));
  741. oldListIterator.add(new KnuthGlue(letterSpaceIPD.opt,
  742. letterSpaceIPD.max - letterSpaceIPD.opt,
  743. letterSpaceIPD.opt - letterSpaceIPD.min,
  744. new LeafPosition(this, -1), false));
  745. } else if (letterSpaceIPD.min == letterSpaceIPD.max) {
  746. // constant letter space: replace the box
  747. oldListIterator.set(new KnuthInlineBox(ai.ipdArea.opt, alignmentContext, pos, false));
  748. } else {
  749. // adjustable letter space: replace the glue
  750. oldListIterator.next(); // this would return the penalty element
  751. oldListIterator.next(); // this would return the glue element
  752. oldListIterator.set(new KnuthGlue(ai.iLScount * letterSpaceIPD.opt,
  753. ai.iLScount * (letterSpaceIPD.max - letterSpaceIPD.opt),
  754. ai.iLScount * (letterSpaceIPD.opt - letterSpaceIPD.min),
  755. new LeafPosition(this, -1), true));
  756. }
  757. return oldList;
  758. }
  759. /**
  760. * remove the AreaInfo object represented by the given elements,
  761. * so that it won't generate any element when getChangedKnuthElements
  762. * will be called
  763. *
  764. * @param oldList the elements representing the word space
  765. */
  766. public void removeWordSpace(List oldList) {
  767. // find the element storing the Position whose value
  768. // points to the AreaInfo object
  769. ListIterator oldListIterator = oldList.listIterator();
  770. if (((KnuthElement) ((LinkedList) oldList).getFirst()).isPenalty()) {
  771. // non breaking space: oldList starts with a penalty
  772. oldListIterator.next();
  773. }
  774. if (oldList.size() > 2) {
  775. // alignment is either center, start or end:
  776. // the first two elements does not store the needed Position
  777. oldListIterator.next();
  778. oldListIterator.next();
  779. }
  780. int leafValue = ((LeafPosition) ((KnuthElement) oldListIterator.next()).getPosition()).getLeafPos();
  781. // only the last word space can be a trailing space!
  782. if (leafValue == vecAreaInfo.size() - 1) {
  783. vecAreaInfo.remove(leafValue);
  784. } else {
  785. log.error("trying to remove a non-trailing word space");
  786. }
  787. }
  788. /** @see InlineLevelLayoutManager#hyphenate(Position, HyphContext) */
  789. public void hyphenate(Position pos, HyphContext hc) {
  790. AreaInfo ai
  791. = (AreaInfo) vecAreaInfo.get(((LeafPosition) pos).getLeafPos());
  792. int iStartIndex = ai.iStartIndex;
  793. int iStopIndex;
  794. boolean bNothingChanged = true;
  795. while (iStartIndex < ai.iBreakIndex) {
  796. MinOptMax newIPD = new MinOptMax(0);
  797. boolean bHyphenFollows;
  798. if (hc.hasMoreHyphPoints()
  799. && (iStopIndex = iStartIndex + hc.getNextHyphPoint())
  800. <= ai.iBreakIndex) {
  801. // iStopIndex is the index of the first character
  802. // after a hyphenation point
  803. bHyphenFollows = true;
  804. } else {
  805. // there are no more hyphenation points,
  806. // or the next one is after ai.iBreakIndex
  807. bHyphenFollows = false;
  808. iStopIndex = ai.iBreakIndex;
  809. }
  810. hc.updateOffset(iStopIndex - iStartIndex);
  811. //log.info("Word: " + new String(textArray, iStartIndex, iStopIndex - iStartIndex));
  812. for (int i = iStartIndex; i < iStopIndex; i++) {
  813. char c = textArray[i];
  814. newIPD.add(new MinOptMax(font.getCharWidth(c)));
  815. //if (i > iStartIndex) {
  816. if (i < iStopIndex) {
  817. MinOptMax la = this.letterAdjustArray[i + 1];
  818. if ((i == iStopIndex - 1) && bHyphenFollows) {
  819. //the letter adjust here needs to be handled further down during
  820. //element generation because it depends on hyph/no-hyph condition
  821. la = null;
  822. }
  823. if (la != null) {
  824. newIPD.add(la);
  825. }
  826. }
  827. }
  828. // add letter spaces
  829. boolean bIsWordEnd
  830. = iStopIndex == ai.iBreakIndex
  831. && ai.iLScount < (ai.iBreakIndex - ai.iStartIndex);
  832. newIPD.add(MinOptMax.multiply(letterSpaceIPD,
  833. (bIsWordEnd
  834. ? (iStopIndex - iStartIndex - 1)
  835. : (iStopIndex - iStartIndex))));
  836. if (!(bNothingChanged
  837. && iStopIndex == ai.iBreakIndex
  838. && bHyphenFollows == false)) {
  839. // the new AreaInfo object is not equal to the old one
  840. if (changeList == null) {
  841. changeList = new LinkedList();
  842. }
  843. changeList.add
  844. (new PendingChange
  845. (new AreaInfo((short) iStartIndex, (short) iStopIndex,
  846. (short) 0,
  847. (short) (bIsWordEnd
  848. ? (iStopIndex - iStartIndex - 1)
  849. : (iStopIndex - iStartIndex)),
  850. newIPD, bHyphenFollows, false, false),
  851. ((LeafPosition) pos).getLeafPos()));
  852. bNothingChanged = false;
  853. }
  854. iStartIndex = iStopIndex;
  855. }
  856. if (!bChanged && !bNothingChanged) {
  857. bChanged = true;
  858. }
  859. }
  860. /** @see InlineLevelLayoutManager#applyChanges(List) */
  861. public boolean applyChanges(List oldList) {
  862. setFinished(false);
  863. if (changeList != null) {
  864. int iAddedAI = 0;
  865. int iRemovedAI = 0;
  866. int iOldIndex = -1;
  867. PendingChange currChange = null;
  868. ListIterator changeListIterator = changeList.listIterator();
  869. while (changeListIterator.hasNext()) {
  870. currChange = (PendingChange) changeListIterator.next();
  871. if (currChange.index != iOldIndex) {
  872. iRemovedAI++;
  873. iAddedAI++;
  874. iOldIndex = currChange.index;
  875. vecAreaInfo.remove(currChange.index + iAddedAI - iRemovedAI);
  876. vecAreaInfo.add(currChange.index + iAddedAI - iRemovedAI,
  877. currChange.ai);
  878. } else {
  879. iAddedAI++;
  880. vecAreaInfo.add(currChange.index + iAddedAI - iRemovedAI,
  881. currChange.ai);
  882. }
  883. }
  884. changeList.clear();
  885. }
  886. iReturnedIndex = 0;
  887. return bChanged;
  888. }
  889. /** @see org.apache.fop.layoutmgr.LayoutManager#getChangedKnuthElements(List, int) */
  890. public LinkedList getChangedKnuthElements(List oldList,
  891. int alignment) {
  892. if (isFinished()) {
  893. return null;
  894. }
  895. LinkedList returnList = new LinkedList();
  896. while (iReturnedIndex < vecAreaInfo.size()) {
  897. AreaInfo ai = (AreaInfo) vecAreaInfo.get(iReturnedIndex);
  898. if (ai.iWScount == 0) {
  899. // ai refers either to a word or a word fragment
  900. returnList.addAll
  901. (createElementsForAWordFragment(alignment, ai, iReturnedIndex, letterSpaceIPD));
  902. } else {
  903. // ai refers to a space
  904. returnList.addAll
  905. (createElementsForASpace(alignment, ai, iReturnedIndex));
  906. }
  907. iReturnedIndex++;
  908. } // end of while
  909. setFinished(true);
  910. //ElementListObserver.observe(returnList, "text-changed", null);
  911. return returnList;
  912. }
  913. /** @see InlineLevelLayoutManager#getWordChars(StringBuffer, Position) */
  914. public void getWordChars(StringBuffer sbChars, Position pos) {
  915. int iLeafValue = ((LeafPosition) pos).getLeafPos();
  916. if (iLeafValue != -1) {
  917. AreaInfo ai = (AreaInfo) vecAreaInfo.get(iLeafValue);
  918. sbChars.append(new String(textArray, ai.iStartIndex,
  919. ai.iBreakIndex - ai.iStartIndex));
  920. }
  921. }
  922. private LinkedList createElementsForASpace(int alignment,
  923. AreaInfo ai, int leafValue) {
  924. LinkedList spaceElements = new LinkedList();
  925. LeafPosition mainPosition = new LeafPosition(this, leafValue);
  926. if (!ai.breakOppAfter) {
  927. // a non-breaking space
  928. if (alignment == EN_JUSTIFY) {
  929. // the space can stretch and shrink, and must be preserved
  930. // when starting a line
  931. spaceElements.add(new KnuthInlineBox(0, null,
  932. notifyPos(new LeafPosition(this, -1)), true));
  933. spaceElements.add(new KnuthPenalty(0, KnuthElement.INFINITE,
  934. false, new LeafPosition(this, -1), false));
  935. spaceElements.add(new KnuthGlue(ai.ipdArea.opt, ai.ipdArea.max - ai.ipdArea.opt,
  936. ai.ipdArea.opt - ai.ipdArea.min, mainPosition, false));
  937. } else {
  938. // the space does not need to stretch or shrink, and must be
  939. // preserved when starting a line
  940. spaceElements.add(new KnuthInlineBox(ai.ipdArea.opt, null,
  941. mainPosition, true));
  942. }
  943. } else {
  944. if (textArray[ai.iStartIndex] != CharUtilities.SPACE
  945. || foText.getWhitespaceTreatment() == Constants.EN_PRESERVE) {
  946. // a breaking space that needs to be preserved
  947. switch (alignment) {
  948. case EN_CENTER:
  949. // centered text:
  950. // if the second element is chosen as a line break these elements
  951. // add a constant amount of stretch at the end of a line and at the
  952. // beginning of the next one, otherwise they don't add any stretch
  953. spaceElements.add(new KnuthGlue(lineEndBAP,
  954. 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  955. new LeafPosition(this, -1), false));
  956. spaceElements
  957. .add(new KnuthPenalty(
  958. 0,
  959. 0, false,
  960. new LeafPosition(this, -1), false));
  961. spaceElements.add(new KnuthGlue(
  962. - (lineStartBAP + lineEndBAP), -6
  963. * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  964. new LeafPosition(this, -1), false));
  965. spaceElements.add(new KnuthInlineBox(0, null,
  966. notifyPos(new LeafPosition(this, -1)), false));
  967. spaceElements.add(new KnuthPenalty(0, KnuthElement.INFINITE,
  968. false, new LeafPosition(this, -1), false));
  969. spaceElements.add(new KnuthGlue(ai.ipdArea.opt + lineStartBAP,
  970. 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  971. mainPosition, false));
  972. break;
  973. case EN_START: // fall through
  974. case EN_END:
  975. // left- or right-aligned text:
  976. // if the second element is chosen as a line break these elements
  977. // add a constant amount of stretch at the end of a line, otherwise
  978. // they don't add any stretch
  979. spaceElements.add(new KnuthGlue(lineEndBAP,
  980. 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  981. new LeafPosition(this, -1), false));
  982. spaceElements.add(new KnuthPenalty(0, 0, false,
  983. new LeafPosition(this, -1), false));
  984. spaceElements.add(new KnuthGlue(
  985. - (lineStartBAP + lineEndBAP), -3
  986. * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  987. new LeafPosition(this, -1), false));
  988. spaceElements.add(new KnuthInlineBox(0, null,
  989. notifyPos(new LeafPosition(this, -1)), false));
  990. spaceElements.add(new KnuthPenalty(0,
  991. KnuthElement.INFINITE, false, new LeafPosition(
  992. this, -1), false));
  993. spaceElements.add(new KnuthGlue(ai.ipdArea.opt + lineStartBAP, 0, 0,
  994. mainPosition, false));
  995. break;
  996. case EN_JUSTIFY:
  997. // justified text:
  998. // the stretch and shrink depends on the space width
  999. spaceElements.add(new KnuthGlue(lineEndBAP, 0, 0,
  1000. new LeafPosition(this, -1), false));
  1001. spaceElements.add(new KnuthPenalty(0, 0, false,
  1002. new LeafPosition(this, -1), false));
  1003. spaceElements.add(new KnuthGlue(
  1004. - (lineStartBAP + lineEndBAP), ai.ipdArea.max
  1005. - ai.ipdArea.opt, ai.ipdArea.opt - ai.ipdArea.min,
  1006. new LeafPosition(this, -1), false));
  1007. spaceElements.add(new KnuthInlineBox(0, null,
  1008. notifyPos(new LeafPosition(this, -1)), false));
  1009. spaceElements.add(new KnuthPenalty(0,
  1010. KnuthElement.INFINITE, false, new LeafPosition(
  1011. this, -1), false));
  1012. spaceElements.add(new KnuthGlue(lineStartBAP + ai.ipdArea.opt, 0, 0,
  1013. mainPosition, false));
  1014. break;
  1015. default:
  1016. // last line justified, the other lines unjustified:
  1017. // use only the space stretch
  1018. spaceElements.add(new KnuthGlue(lineEndBAP, 0, 0,
  1019. new LeafPosition(this, -1), false));
  1020. spaceElements.add(new KnuthPenalty(0, 0, false,
  1021. new LeafPosition(this, -1), false));
  1022. spaceElements.add(new KnuthGlue(
  1023. - (lineStartBAP + lineEndBAP), ai.ipdArea.max
  1024. - ai.ipdArea.opt, 0,
  1025. new LeafPosition(this, -1), false));
  1026. spaceElements.add(new KnuthInlineBox(0, null,
  1027. notifyPos(new LeafPosition(this, -1)), false));
  1028. spaceElements.add(new KnuthPenalty(0,
  1029. KnuthElement.INFINITE, false, new LeafPosition(
  1030. this, -1), false));
  1031. spaceElements.add(new KnuthGlue(lineStartBAP + ai.ipdArea.opt, 0, 0,
  1032. mainPosition, false));
  1033. }
  1034. } else {
  1035. // a (possible block) of breaking spaces
  1036. switch (alignment) {
  1037. case EN_CENTER:
  1038. // centered text:
  1039. // if the second element is chosen as a line break these elements
  1040. // add a constant amount of stretch at the end of a line and at the
  1041. // beginning of the next one, otherwise they don't add any stretch
  1042. spaceElements.add(new KnuthGlue(lineEndBAP,
  1043. 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1044. new LeafPosition(this, -1), false));
  1045. spaceElements
  1046. .add(new KnuthPenalty(
  1047. 0, 0, false,
  1048. new LeafPosition(this, -1), false));
  1049. spaceElements.add(new KnuthGlue(ai.ipdArea.opt
  1050. - (lineStartBAP + lineEndBAP), -6
  1051. * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1052. mainPosition, false));
  1053. spaceElements.add(new KnuthInlineBox(0, null,
  1054. notifyPos(new LeafPosition(this, -1)), false));
  1055. spaceElements.add(new KnuthPenalty(0, KnuthElement.INFINITE,
  1056. false, new LeafPosition(this, -1), false));
  1057. spaceElements.add(new KnuthGlue(lineStartBAP,
  1058. 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1059. new LeafPosition(this, -1), false));
  1060. break;
  1061. case EN_START: // fall through
  1062. case EN_END:
  1063. // left- or right-aligned text:
  1064. // if the second element is chosen as a line break these elements
  1065. // add a constant amount of stretch at the end of a line, otherwise
  1066. // they don't add any stretch
  1067. if (lineStartBAP != 0 || lineEndBAP != 0) {
  1068. spaceElements.add(new KnuthGlue(lineEndBAP,
  1069. 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1070. new LeafPosition(this, -1), false));
  1071. spaceElements.add(new KnuthPenalty(0, 0, false,
  1072. new LeafPosition(this, -1), false));
  1073. spaceElements.add(new KnuthGlue(ai.ipdArea.opt
  1074. - (lineStartBAP + lineEndBAP), -3
  1075. * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1076. mainPosition, false));
  1077. spaceElements.add(new KnuthInlineBox(0, null,
  1078. notifyPos(new LeafPosition(this, -1)), false));
  1079. spaceElements.add(new KnuthPenalty(0,
  1080. KnuthElement.INFINITE, false, new LeafPosition(
  1081. this, -1), false));
  1082. spaceElements.add(new KnuthGlue(lineStartBAP, 0, 0,
  1083. new LeafPosition(this, -1), false));
  1084. } else {
  1085. spaceElements.add(new KnuthGlue(0,
  1086. 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1087. new LeafPosition(this, -1), false));
  1088. spaceElements.add(new KnuthPenalty(0, 0, false,
  1089. new LeafPosition(this, -1), false));
  1090. spaceElements.add(new KnuthGlue(ai.ipdArea.opt, -3
  1091. * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1092. mainPosition, false));
  1093. }
  1094. break;
  1095. case EN_JUSTIFY:
  1096. // justified text:
  1097. // the stretch and shrink depends on the space width
  1098. if (lineStartBAP != 0 || lineEndBAP != 0) {
  1099. spaceElements.add(new KnuthGlue(lineEndBAP, 0, 0,
  1100. new LeafPosition(this, -1), false));
  1101. spaceElements.add(new KnuthPenalty(0, 0, false,
  1102. new LeafPosition(this, -1), false));
  1103. spaceElements.add(new KnuthGlue(
  1104. ai.ipdArea.opt - (lineStartBAP + lineEndBAP),
  1105. ai.ipdArea.max - ai.ipdArea.opt,
  1106. ai.ipdArea.opt - ai.ipdArea.min,
  1107. mainPosition, false));
  1108. spaceElements.add(new KnuthInlineBox(0, null,
  1109. notifyPos(new LeafPosition(this, -1)), false));
  1110. spaceElements.add(new KnuthPenalty(0,
  1111. KnuthElement.INFINITE, false, new LeafPosition(
  1112. this, -1), false));
  1113. spaceElements.add(new KnuthGlue(lineStartBAP, 0, 0,
  1114. new LeafPosition(this, -1), false));
  1115. } else {
  1116. spaceElements.add(new KnuthGlue(ai.ipdArea.opt,
  1117. ai.ipdArea.max - ai.ipdArea.opt,
  1118. ai.ipdArea.opt - ai.ipdArea.min,
  1119. mainPosition, false));
  1120. }
  1121. break;
  1122. default:
  1123. // last line justified, the other lines unjustified:
  1124. // use only the space stretch
  1125. if (lineStartBAP != 0 || lineEndBAP != 0) {
  1126. spaceElements.add(new KnuthGlue(lineEndBAP, 0, 0,
  1127. new LeafPosition(this, -1), false));
  1128. spaceElements.add(new KnuthPenalty(0, 0, false,
  1129. new LeafPosition(this, -1), false));
  1130. spaceElements.add(new KnuthGlue(
  1131. ai.ipdArea.opt - (lineStartBAP + lineEndBAP),
  1132. ai.ipdArea.max - ai.ipdArea.opt,
  1133. 0, mainPosition, false));
  1134. spaceElements.add(new KnuthInlineBox(0, null,
  1135. notifyPos(new LeafPosition(this, -1)), false));
  1136. spaceElements.add(new KnuthPenalty(0,
  1137. KnuthElement.INFINITE, false, new LeafPosition(
  1138. this, -1), false));
  1139. spaceElements.add(new KnuthGlue(lineStartBAP, 0, 0,
  1140. new LeafPosition(this, -1), false));
  1141. } else {
  1142. spaceElements.add(new KnuthGlue(ai.ipdArea.opt,
  1143. ai.ipdArea.max - ai.ipdArea.opt, 0,
  1144. mainPosition, false));
  1145. }
  1146. }
  1147. }
  1148. }
  1149. return spaceElements;
  1150. }
  1151. private LinkedList createElementsForAWordFragment(int alignment,
  1152. AreaInfo ai, int leafValue, MinOptMax letterSpaceWidth) {
  1153. LinkedList wordElements = new LinkedList();
  1154. LeafPosition mainPosition = new LeafPosition(this, leafValue);
  1155. // if the last character of the word fragment is '-' or '/',
  1156. // the fragment could end a line; in this case, it loses one
  1157. // of its letter spaces;
  1158. boolean bSuppressibleLetterSpace = ai.breakOppAfter && !ai.bHyphenated;
  1159. if (letterSpaceWidth.min == letterSpaceWidth.max) {
  1160. // constant letter spacing
  1161. wordElements.add
  1162. (new KnuthInlineBox(
  1163. bSuppressibleLetterSpace
  1164. ? ai.ipdArea.opt - letterSpaceWidth.opt
  1165. : ai.ipdArea.opt,
  1166. alignmentContext,
  1167. notifyPos(mainPosition), false));
  1168. } else {
  1169. // adjustable letter spacing
  1170. int unsuppressibleLetterSpaces
  1171. = bSuppressibleLetterSpace ? ai.iLScount - 1 : ai.iLScount;
  1172. wordElements.add
  1173. (new KnuthInlineBox(ai.ipdArea.opt
  1174. - ai.iLScount * letterSpaceWidth.opt,
  1175. alignmentContext,
  1176. notifyPos(mainPosition), false));
  1177. wordElements.add
  1178. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1179. new LeafPosition(this, -1), true));
  1180. wordElements.add
  1181. (new KnuthGlue(unsuppressibleLetterSpaces * letterSpaceWidth.opt,
  1182. unsuppressibleLetterSpaces * (letterSpaceWidth.max - letterSpaceWidth.opt),
  1183. unsuppressibleLetterSpaces * (letterSpaceWidth.opt - letterSpaceWidth.min),
  1184. new LeafPosition(this, -1), true));
  1185. wordElements.add
  1186. (new KnuthInlineBox(0, null,
  1187. notifyPos(new LeafPosition(this, -1)), true));
  1188. }
  1189. // extra-elements if the word fragment is the end of a syllable,
  1190. // or it ends with a character that can be used as a line break
  1191. if (ai.bHyphenated) {
  1192. MinOptMax widthIfNoBreakOccurs = null;
  1193. if (ai.iBreakIndex < textArray.length) {
  1194. //Add in kerning in no-break condition
  1195. widthIfNoBreakOccurs = letterAdjustArray[ai.iBreakIndex];
  1196. }
  1197. //if (ai.iBreakIndex)
  1198. // the word fragment ends at the end of a syllable:
  1199. // if a break occurs the content width increases,
  1200. // otherwise nothing happens
  1201. wordElements.addAll(createElementsForAHyphen(alignment, hyphIPD, widthIfNoBreakOccurs, ai.breakOppAfter && ai.bHyphenated));
  1202. } else if (bSuppressibleLetterSpace) {
  1203. // the word fragment ends with a character that acts as a hyphen
  1204. // if a break occurs the width does not increase,
  1205. // otherwise there is one more letter space
  1206. wordElements.addAll(createElementsForAHyphen(alignment, 0, letterSpaceWidth, true));
  1207. }
  1208. return wordElements;
  1209. }
  1210. // static final int SOFT_HYPHEN_PENALTY = KnuthPenalty.FLAGGED_PENALTY / 10;
  1211. static final int SOFT_HYPHEN_PENALTY = 1;
  1212. private LinkedList createElementsForAHyphen(int alignment,
  1213. int widthIfBreakOccurs, MinOptMax widthIfNoBreakOccurs, boolean unflagged) {
  1214. if (widthIfNoBreakOccurs == null) {
  1215. widthIfNoBreakOccurs = ZERO_MINOPTMAX;
  1216. }
  1217. LinkedList hyphenElements = new LinkedList();
  1218. switch (alignment) {
  1219. case EN_CENTER :
  1220. // centered text:
  1221. /*
  1222. hyphenElements.add
  1223. (new KnuthGlue(0, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1224. new LeafPosition(this, -1), false));
  1225. hyphenElements.add
  1226. (new KnuthPenalty(hyphIPD,
  1227. KnuthPenalty.FLAGGED_PENALTY, true,
  1228. new LeafPosition(this, -1), false));
  1229. hyphenElements.add
  1230. (new KnuthGlue(0,
  1231. - 6 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1232. new LeafPosition(this, -1), false));
  1233. hyphenElements.add
  1234. (new KnuthInlineBox(0, 0, 0, 0,
  1235. new LeafPosition(this, -1), false));
  1236. hyphenElements.add
  1237. (new KnuthPenalty(0, KnuthElement.INFINITE, true,
  1238. new LeafPosition(this, -1), false));
  1239. hyphenElements.add
  1240. (new KnuthGlue(0, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1241. new LeafPosition(this, -1), false));
  1242. */
  1243. hyphenElements.add
  1244. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1245. new LeafPosition(this, -1), true));
  1246. hyphenElements.add
  1247. (new KnuthGlue(lineEndBAP, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1248. new LeafPosition(this, -1), true));
  1249. hyphenElements.add
  1250. (new KnuthPenalty(hyphIPD,
  1251. unflagged ? SOFT_HYPHEN_PENALTY : KnuthPenalty.FLAGGED_PENALTY, !unflagged,
  1252. new LeafPosition(this, -1), false));
  1253. hyphenElements.add
  1254. (new KnuthGlue(-(lineEndBAP + lineStartBAP),
  1255. -6 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1256. new LeafPosition(this, -1), false));
  1257. hyphenElements.add
  1258. (new KnuthInlineBox(0, null,
  1259. notifyPos(new LeafPosition(this, -1)), true));
  1260. hyphenElements.add
  1261. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1262. new LeafPosition(this, -1), true));
  1263. hyphenElements.add
  1264. (new KnuthGlue(lineStartBAP, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1265. new LeafPosition(this, -1), true));
  1266. break;
  1267. case EN_START : // fall through
  1268. case EN_END :
  1269. // left- or right-aligned text:
  1270. /*
  1271. hyphenElements.add
  1272. (new KnuthGlue(0, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1273. new LeafPosition(this, -1), false));
  1274. hyphenElements.add
  1275. (new KnuthPenalty(widthIfBreakOccurs,
  1276. KnuthPenalty.FLAGGED_PENALTY, true,
  1277. new LeafPosition(this, -1), false));
  1278. hyphenElements.add
  1279. (new KnuthGlue(widthIfNoBreakOccurs.opt,
  1280. - 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1281. new LeafPosition(this, -1), false));
  1282. */
  1283. if (lineStartBAP != 0 || lineEndBAP != 0) {
  1284. hyphenElements.add
  1285. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1286. new LeafPosition(this, -1), true));
  1287. hyphenElements.add
  1288. (new KnuthGlue(lineEndBAP, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1289. new LeafPosition(this, -1), false));
  1290. hyphenElements.add
  1291. (new KnuthPenalty(widthIfBreakOccurs,
  1292. unflagged ? SOFT_HYPHEN_PENALTY : KnuthPenalty.FLAGGED_PENALTY, !unflagged,
  1293. new LeafPosition(this, -1), false));
  1294. hyphenElements.add
  1295. (new KnuthGlue(widthIfNoBreakOccurs.opt - (lineStartBAP + lineEndBAP),
  1296. -3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1297. new LeafPosition(this, -1), false));
  1298. hyphenElements.add
  1299. (new KnuthInlineBox(0, null,
  1300. notifyPos(new LeafPosition(this, -1)), false));
  1301. hyphenElements.add
  1302. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1303. new LeafPosition(this, -1), false));
  1304. hyphenElements.add
  1305. (new KnuthGlue(lineStartBAP, 0, 0,
  1306. new LeafPosition(this, -1), false));
  1307. } else {
  1308. hyphenElements.add
  1309. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1310. new LeafPosition(this, -1), true));
  1311. hyphenElements.add
  1312. (new KnuthGlue(0, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1313. new LeafPosition(this, -1), false));
  1314. hyphenElements.add
  1315. (new KnuthPenalty(widthIfBreakOccurs,
  1316. unflagged ? SOFT_HYPHEN_PENALTY : KnuthPenalty.FLAGGED_PENALTY, !unflagged,
  1317. new LeafPosition(this, -1), false));
  1318. hyphenElements.add
  1319. (new KnuthGlue(widthIfNoBreakOccurs.opt,
  1320. -3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
  1321. new LeafPosition(this, -1), false));
  1322. }
  1323. break;
  1324. default:
  1325. // justified text, or last line justified:
  1326. // just a flagged penalty
  1327. /*
  1328. hyphenElements.add
  1329. (new KnuthPenalty(widthIfBreakOccurs,
  1330. KnuthPenalty.FLAGGED_PENALTY, true,
  1331. new LeafPosition(this, -1), false));
  1332. */
  1333. if (lineStartBAP != 0 || lineEndBAP != 0) {
  1334. hyphenElements.add
  1335. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1336. new LeafPosition(this, -1), true));
  1337. hyphenElements.add
  1338. (new KnuthGlue(lineEndBAP, 0, 0,
  1339. new LeafPosition(this, -1), false));
  1340. hyphenElements.add
  1341. (new KnuthPenalty(widthIfBreakOccurs,
  1342. unflagged ? SOFT_HYPHEN_PENALTY : KnuthPenalty.FLAGGED_PENALTY, !unflagged,
  1343. new LeafPosition(this, -1), false));
  1344. // extra elements representing a letter space that is suppressed
  1345. // if a break occurs
  1346. if (widthIfNoBreakOccurs.min != 0
  1347. || widthIfNoBreakOccurs.max != 0) {
  1348. hyphenElements.add
  1349. (new KnuthGlue(widthIfNoBreakOccurs.opt - (lineStartBAP + lineEndBAP),
  1350. widthIfNoBreakOccurs.max - widthIfNoBreakOccurs.opt,
  1351. widthIfNoBreakOccurs.opt - widthIfNoBreakOccurs.min,
  1352. new LeafPosition(this, -1), false));
  1353. } else {
  1354. hyphenElements.add
  1355. (new KnuthGlue(-(lineStartBAP + lineEndBAP), 0, 0,
  1356. new LeafPosition(this, -1), false));
  1357. }
  1358. hyphenElements.add
  1359. (new KnuthInlineBox(0, null,
  1360. notifyPos(new LeafPosition(this, -1)), false));
  1361. hyphenElements.add
  1362. (new KnuthPenalty(0, KnuthElement.INFINITE, false,
  1363. new LeafPosition(this, -1), false));
  1364. hyphenElements.add
  1365. (new KnuthGlue(lineStartBAP, 0, 0,
  1366. new LeafPosition(this, -1), false));
  1367. } else {
  1368. hyphenElements.add
  1369. (new KnuthPenalty(widthIfBreakOccurs,
  1370. unflagged ? SOFT_HYPHEN_PENALTY : KnuthPenalty.FLAGGED_PENALTY, !unflagged,
  1371. new LeafPosition(this, -1), false));
  1372. // extra elements representing a letter space that is suppressed
  1373. // if a break occurs
  1374. if (widthIfNoBreakOccurs.min != 0
  1375. || widthIfNoBreakOccurs.max != 0) {
  1376. hyphenElements.add
  1377. (new KnuthGlue(widthIfNoBreakOccurs.opt,
  1378. widthIfNoBreakOccurs.max - widthIfNoBreakOccurs.opt,
  1379. widthIfNoBreakOccurs.opt - widthIfNoBreakOccurs.min,
  1380. new LeafPosition(this, -1), false));
  1381. }
  1382. }
  1383. }
  1384. return hyphenElements;
  1385. }
  1386. }