1 17 18 19 20 package org.apache.fop.layoutmgr.inline; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.fop.datatypes.Length; 25 import org.apache.fop.datatypes.LengthBase; 26 import org.apache.fop.datatypes.SimplePercentBaseContext; 27 import org.apache.fop.fo.Constants; 28 29 30 34 public class BasicScaledBaselineTable implements ScaledBaselineTable, Constants { 35 36 37 protected Log log = LogFactory.getLog(BasicScaledBaselineTable.class); 38 39 private int altitude; 40 private int depth; 41 private int xHeight; 42 private int dominantBaselineIdentifier; 43 private int writingMode; 44 private int dominantBaselineOffset; 45 private int beforeEdgeOffset; 46 private int afterEdgeOffset; 47 48 private static final float HANGING_BASELINE_FACTOR = 0.8f; 49 private static final float MATHEMATICAL_BASELINE_FACTOR = 0.5f; 50 51 61 public BasicScaledBaselineTable(int altitude 62 , int depth 63 , int xHeight 64 , int dominantBaselineIdentifier 65 , int writingMode) { 66 this.altitude = altitude; 67 this.depth = depth; 68 this.xHeight = xHeight; 69 this.dominantBaselineIdentifier = dominantBaselineIdentifier; 70 this.writingMode = writingMode; 71 this.dominantBaselineOffset = getBaselineDefaultOffset(this.dominantBaselineIdentifier); 72 this.beforeEdgeOffset = altitude - dominantBaselineOffset; 73 this.afterEdgeOffset = depth - dominantBaselineOffset; 74 } 75 76 80 public int getDominantBaselineIdentifier() { 81 return this.dominantBaselineIdentifier; 82 } 83 84 88 public int getWritingMode() { 89 return this.writingMode; 90 } 91 92 98 public int getBaseline(int baselineIdentifier) { 99 int offset = 0; 100 if (!isHorizontalWritingMode()) { 101 switch (baselineIdentifier) { 102 case EN_TOP: 103 case EN_TEXT_TOP: 104 case EN_TEXT_BOTTOM: 105 case EN_BOTTOM: 106 log.warn("The given baseline is only supported for horizontal" 107 + " writing modes"); 108 return 0; 109 } 110 } 111 switch (baselineIdentifier) { 112 case EN_TOP: case EN_BEFORE_EDGE: 114 offset = beforeEdgeOffset; 115 break; 116 case EN_TEXT_TOP: 117 case EN_TEXT_BEFORE_EDGE: 118 case EN_HANGING: 119 case EN_CENTRAL: 120 case EN_MIDDLE: 121 case EN_MATHEMATICAL: 122 case EN_ALPHABETIC: 123 case EN_IDEOGRAPHIC: 124 case EN_TEXT_BOTTOM: 125 case EN_TEXT_AFTER_EDGE: 126 offset = getBaselineDefaultOffset(baselineIdentifier) - dominantBaselineOffset; 127 break; 128 case EN_BOTTOM: case EN_AFTER_EDGE: 130 offset = afterEdgeOffset; 131 break; 132 } 133 return offset; 134 } 135 136 private boolean isHorizontalWritingMode() { 137 return writingMode == EN_LR_TB || writingMode == EN_RL_TB; 138 } 139 140 146 private int getBaselineDefaultOffset(int baselineIdentifier) { 147 int offset = 0; 148 switch (baselineIdentifier) { 149 case EN_TEXT_BEFORE_EDGE: 150 offset = altitude; 151 break; 152 case EN_HANGING: 153 offset = (int)Math.round(altitude * HANGING_BASELINE_FACTOR); 154 break; 155 case EN_CENTRAL: 156 offset = (altitude - depth) / 2 + depth; 157 break; 158 case EN_MIDDLE: 159 offset = xHeight / 2; 160 break; 161 case EN_MATHEMATICAL: 162 offset = (int)Math.round(altitude * MATHEMATICAL_BASELINE_FACTOR); 163 break; 164 case EN_ALPHABETIC: 165 offset = 0; 166 break; 167 case EN_IDEOGRAPHIC: case EN_TEXT_AFTER_EDGE: 169 offset = depth; 170 break; 171 } 172 return offset; 173 } 174 175 178 public void setBeforeAndAfterBaselines(int beforeBaseline, int afterBaseline) { 179 beforeEdgeOffset = beforeBaseline; 180 afterEdgeOffset = afterBaseline; 181 } 182 183 186 public ScaledBaselineTable deriveScaledBaselineTable(int baselineIdentifier) { 187 BasicScaledBaselineTable bac 188 = new BasicScaledBaselineTable(altitude, depth, xHeight 189 , baselineIdentifier, this.writingMode); 190 return bac; 191 } 192 193 } 194 | Popular Tags |