001: /**
002: * $Id: RtfImportHeader.java 2337 2006-08-28 11:21:15Z blowagie $
003: * $Name$
004: *
005: * Copyright 2006 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999-2006 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000-2006 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */package com.lowagie.text.rtf.direct;
050:
051: import java.awt.Color;
052: import java.util.HashMap;
053:
054: import com.lowagie.text.rtf.document.RtfDocument;
055: import com.lowagie.text.rtf.style.RtfColor;
056: import com.lowagie.text.rtf.style.RtfFont;
057:
058: /**
059: * The RtfImportHeader stores the docment header information from
060: * an RTF document that is being imported. Currently font and
061: * color settings are stored. The RtfImportHeader maintains a mapping
062: * from font and color numbers from the imported RTF document to
063: * the RTF document that is the target of the import. This guarantees
064: * that the merged document has the correct font and color settings.
065: *
066: * @version $Revision: 2337 $
067: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
068: */
069: public class RtfImportHeader {
070: /**
071: * The HashMap storing the font number mappings.
072: */
073: private HashMap importFontMapping = null;
074: /**
075: * The HashMap storing the color number mapings.
076: */
077: private HashMap importColorMapping = null;
078: /**
079: * The RtfDocument to get font and color numbers from.
080: */
081: private RtfDocument rtfDoc = null;
082:
083: /**
084: * Constructs a new RtfImportHeader.
085: *
086: * @param rtfDoc The RtfDocument to get font and color numbers from.
087: */
088: public RtfImportHeader(RtfDocument rtfDoc) {
089: this .rtfDoc = rtfDoc;
090: this .importFontMapping = new HashMap();
091: this .importColorMapping = new HashMap();
092: }
093:
094: /**
095: * Imports a font. The font name is looked up in the RtfDocumentHeader and
096: * then the mapping from original font number to actual font number is added.
097: *
098: * @param fontNr The original font number.
099: * @param fontName The font name to look up.
100: */
101: public void importFont(String fontNr, String fontName) {
102: RtfFont rtfFont = new RtfFont(fontName);
103: rtfFont.setRtfDocument(this .rtfDoc);
104: this .importFontMapping.put(fontNr, Integer.toString(this .rtfDoc
105: .getDocumentHeader().getFontNumber(rtfFont)));
106: }
107:
108: /**
109: * Performs the mapping from the original font number to the actual
110: * font number in the resulting RTF document. If the font number was not
111: * seen during import (thus no mapping) then 0 is returned, guaranteeing
112: * that the font number is always valid.
113: *
114: * @param fontNr The font number to map.
115: * @return The mapped font number.
116: */
117: public String mapFontNr(String fontNr) {
118: if (this .importFontMapping.containsKey(fontNr)) {
119: return (String) this .importFontMapping.get(fontNr);
120: } else {
121: return "0";
122: }
123: }
124:
125: /**
126: * Imports a color value. The color number for the color defined
127: * by its red, green and blue values is determined and then the
128: * resulting mapping is added.
129: *
130: * @param colorNr The original color number.
131: * @param color The color to import.
132: */
133: public void importColor(String colorNr, Color color) {
134: RtfColor rtfColor = new RtfColor(this .rtfDoc, color);
135: this .importColorMapping.put(colorNr, Integer.toString(rtfColor
136: .getColorNumber()));
137: }
138:
139: /**
140: * Performs the mapping from the original font number to the actual font
141: * number used in the RTF document. If the color number was not
142: * seen during import (thus no mapping) then 0 is returned, guaranteeing
143: * that the color number is always valid.
144: *
145: * @param colorNr The color number to map.
146: * @return The mapped color number
147: */
148: public String mapColorNr(String colorNr) {
149: if (this .importColorMapping.containsKey(colorNr)) {
150: return (String) this .importColorMapping.get(colorNr);
151: } else {
152: return "0";
153: }
154: }
155: }
|