1 17 18 19 20 package org.apache.fop.render.rtf; 21 22 import java.awt.Color ; 23 24 import org.apache.fop.apps.FOPException; 26 import org.apache.fop.datatypes.Length; 27 import org.apache.fop.fo.Constants; 28 import org.apache.fop.fo.FONode; 29 import org.apache.fop.fo.FOText; 30 import org.apache.fop.fo.flow.Block; 31 import org.apache.fop.fo.flow.BlockContainer; 32 import org.apache.fop.fo.flow.Inline; 33 import org.apache.fop.fo.flow.PageNumber; 34 import org.apache.fop.fo.properties.CommonBorderPaddingBackground; 35 import org.apache.fop.fo.properties.CommonFont; 36 import org.apache.fop.fo.properties.CommonMarginBlock; 37 import org.apache.fop.fo.properties.CommonTextDecoration; 38 import org.apache.fop.render.rtf.BorderAttributesConverter; 39 import org.apache.fop.render.rtf.rtflib.rtfdoc.IBorderAttributes; 40 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes; 41 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfColorTable; 42 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfFontManager; 43 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfText; 44 45 54 final class TextAttributesConverter { 55 56 59 private TextAttributesConverter() { 60 } 61 62 66 public static RtfAttributes convertAttributes(Block fobj) 67 throws FOPException { 68 FOPRtfAttributes attrib = new FOPRtfAttributes(); 69 attrFont(fobj.getCommonFont(), attrib); 70 attrFontColor(fobj.getColor(), attrib); 71 attrBlockBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib); 73 attrBlockMargin(fobj.getCommonMarginBlock(), attrib); 74 attrBlockTextAlign(fobj.getTextAlign(), attrib); 75 attrBorder(fobj.getCommonBorderPaddingBackground(), attrib, fobj); 76 77 return attrib; 78 } 79 80 84 public static RtfAttributes convertBlockContainerAttributes(BlockContainer fobj) 85 throws FOPException { 86 FOPRtfAttributes attrib = new FOPRtfAttributes(); 87 attrBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib); 88 attrBlockMargin(fobj.getCommonMarginBlock(), attrib); 89 attrBorder(fobj.getCommonBorderPaddingBackground(), attrib, fobj); 91 92 return attrib; 93 } 94 95 99 public static RtfAttributes convertCharacterAttributes( 100 FOText fobj) throws FOPException { 101 102 FOPRtfAttributes attrib = new FOPRtfAttributes(); 103 attrFont(fobj.getCommonFont(), attrib); 104 attrFontColor(fobj.getColor(), attrib); 105 attrTextDecoration(fobj.getTextDecoration(), attrib); 106 attrBaseLineShift(fobj.getBaseLineShift(), attrib); 107 return attrib; 108 } 109 110 114 public static RtfAttributes convertCharacterAttributes( 115 PageNumber fobj) throws FOPException { 116 117 FOPRtfAttributes attrib = new FOPRtfAttributes(); 118 attrFont(fobj.getCommonFont(), attrib); 119 attrTextDecoration(fobj.getTextDecoration(), attrib); 120 attrBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib); 121 return attrib; 122 } 123 124 128 public static RtfAttributes convertCharacterAttributes( 129 Inline fobj) throws FOPException { 130 131 FOPRtfAttributes attrib = new FOPRtfAttributes(); 132 attrFont(fobj.getCommonFont(), attrib); 133 attrFontColor(fobj.getColor(), attrib); 134 135 attrBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib); 136 attrInlineBorder(fobj.getCommonBorderPaddingBackground(), attrib); 137 return attrib; 138 } 139 140 private static void attrFont(CommonFont font, FOPRtfAttributes rtfAttr) { 141 rtfAttr.set(RtfText.ATTR_FONT_FAMILY, 142 RtfFontManager.getInstance().getFontNumber(font.getFirstFontFamily())); 143 rtfAttr.setHalfPoints(RtfText.ATTR_FONT_SIZE, font.fontSize); 144 145 if (font.fontWeight == Constants.EN_700 146 || font.fontWeight == Constants.EN_800 147 || font.fontWeight == Constants.EN_900) { 148 rtfAttr.set("b", 1); 150 } else { 151 rtfAttr.set("b", 0); 152 } 153 154 if (font.fontStyle == Constants.EN_ITALIC) { 155 rtfAttr.set(RtfText.ATTR_ITALIC, 1); 156 } else { 157 rtfAttr.set(RtfText.ATTR_ITALIC, 0); 158 } 159 160 161 } 162 163 164 private static void attrFontColor(Color colorType, RtfAttributes rtfAttr) { 165 if (colorType != null) { 167 if (colorType.getAlpha() != 0 168 || colorType.getRed() != 0 169 || colorType.getGreen() != 0 170 || colorType.getBlue() != 0) { 171 rtfAttr.set(RtfText.ATTR_FONT_COLOR, 172 convertFOPColorToRTF(colorType)); 173 } 174 } 175 } 176 177 178 179 private static void attrTextDecoration(CommonTextDecoration textDecoration, 180 RtfAttributes rtfAttr) { 181 if (textDecoration == null) { 182 rtfAttr.set(RtfText.ATTR_UNDERLINE, 0); 183 rtfAttr.set(RtfText.ATTR_STRIKETHROUGH, 0); 184 return; 185 } 186 187 if (textDecoration.hasUnderline()) { 188 rtfAttr.set(RtfText.ATTR_UNDERLINE, 1); 189 } else { 190 rtfAttr.set(RtfText.ATTR_UNDERLINE, 0); 191 } 192 193 if (textDecoration.hasLineThrough()) { 194 rtfAttr.set(RtfText.ATTR_STRIKETHROUGH, 1); 195 } else { 196 rtfAttr.set(RtfText.ATTR_STRIKETHROUGH, 0); 197 } 198 } 199 200 private static void attrBlockMargin(CommonMarginBlock cmb, FOPRtfAttributes rtfAttr) { 201 rtfAttr.setTwips(RtfText.SPACE_BEFORE, 202 cmb.spaceBefore.getOptimum(null).getLength()); 203 rtfAttr.setTwips(RtfText.SPACE_AFTER, 204 cmb.spaceAfter.getOptimum(null).getLength()); 205 rtfAttr.setTwips(RtfText.LEFT_INDENT_BODY, cmb.startIndent); 206 rtfAttr.setTwips(RtfText.RIGHT_INDENT_BODY, cmb.endIndent); 207 } 208 209 210 224 225 private static void attrBlockTextAlign(int alignment, RtfAttributes rtfAttr) { 226 String rtfValue = null; 227 switch (alignment) { 228 case Constants.EN_CENTER: 229 rtfValue = RtfText.ALIGN_CENTER; 230 break; 231 case Constants.EN_END: 232 rtfValue = RtfText.ALIGN_RIGHT; 233 break; 234 case Constants.EN_JUSTIFY: 235 rtfValue = RtfText.ALIGN_JUSTIFIED; 236 break; 237 default: 238 rtfValue = RtfText.ALIGN_LEFT; 239 break; 240 } 241 242 rtfAttr.set(rtfValue); 243 } 244 245 249 private static void attrBlockBackgroundColor( 250 CommonBorderPaddingBackground bpb, RtfAttributes rtfAttr) { 251 if (bpb.hasBackground()) { 252 rtfAttr.set(RtfText.SHADING, RtfText.FULL_SHADING); 253 rtfAttr.set(RtfText.SHADING_FRONT_COLOR, 254 convertFOPColorToRTF(bpb.backgroundColor)); 255 } 256 } 257 258 259 private static void attrBorder(CommonBorderPaddingBackground bpb, 260 RtfAttributes rtfAttr, FONode fobj) { 261 if (hasBorder(fobj.getParent())) { 262 attrInlineBorder(bpb, rtfAttr); 263 return; 264 } 265 266 BorderAttributesConverter.makeBorder(bpb, 267 CommonBorderPaddingBackground.BEFORE, rtfAttr, 268 IBorderAttributes.BORDER_TOP); 269 BorderAttributesConverter.makeBorder(bpb, 270 CommonBorderPaddingBackground.AFTER, rtfAttr, 271 IBorderAttributes.BORDER_BOTTOM); 272 BorderAttributesConverter.makeBorder(bpb, 273 CommonBorderPaddingBackground.START, rtfAttr, 274 IBorderAttributes.BORDER_LEFT); 275 BorderAttributesConverter.makeBorder(bpb, 276 CommonBorderPaddingBackground.END, rtfAttr, 277 IBorderAttributes.BORDER_RIGHT); 278 } 279 280 281 private static boolean hasBorder(FONode node) { 282 while (node != null) { 283 CommonBorderPaddingBackground commonBorderPaddingBackground = null; 284 if (node instanceof Block) { 285 Block block = (Block) node; 286 commonBorderPaddingBackground = block.getCommonBorderPaddingBackground(); 287 } else if (node instanceof BlockContainer) { 288 BlockContainer container = (BlockContainer) node; 289 commonBorderPaddingBackground = container.getCommonBorderPaddingBackground(); 290 } 291 292 if (commonBorderPaddingBackground != null 293 && commonBorderPaddingBackground.hasBorder()) { 294 return true; 295 } 296 297 node = node.getParent(); 298 } 299 return false; 300 } 301 302 303 private static void attrInlineBorder(CommonBorderPaddingBackground bpb, 304 RtfAttributes rtfAttr) { 305 BorderAttributesConverter.makeBorder(bpb, 306 CommonBorderPaddingBackground.BEFORE, rtfAttr, 307 IBorderAttributes.BORDER_CHARACTER); 308 } 309 310 316 private static void attrBackgroundColor(CommonBorderPaddingBackground bpb, 317 RtfAttributes rtfAttr) { 318 Color fopValue = bpb.backgroundColor; 319 int rtfColor = 0; 320 325 if ((fopValue == null) 326 || ((fopValue.getRed() == 0) 327 && (fopValue.getGreen() == 0) 328 && (fopValue.getBlue() == 0) 329 && (fopValue.getAlpha() == 0))) { 330 return; 331 } else { 332 rtfColor = convertFOPColorToRTF(fopValue); 333 } 334 335 rtfAttr.set(RtfText.ATTR_BACKGROUND_COLOR, rtfColor); 336 } 337 338 private static void attrBaseLineShift(Length baselineShift, RtfAttributes rtfAttr) { 339 340 int s = baselineShift.getEnum(); 341 342 if (s == Constants.EN_SUPER) { 343 rtfAttr.set(RtfText.ATTR_SUPERSCRIPT); 344 } else if (s == Constants.EN_SUB) { 345 rtfAttr.set(RtfText.ATTR_SUBSCRIPT); 346 } 347 } 348 349 354 public static int convertFOPColorToRTF(Color fopColor) { 355 int redComponent = fopColor.getRed(); 357 int greenComponent = fopColor.getGreen(); 358 int blueComponent = fopColor.getBlue(); 359 return RtfColorTable.getInstance().getColorNumber(redComponent, 360 greenComponent, blueComponent).intValue(); 361 } 362 } 363 | Popular Tags |