コード例 #1
0
        /**
         * Gets an attribute. The search is made in <CODE>attributes</CODE>
         * and <CODE>noStroke</CODE>.
         * @param name the attribute key
         * @return the attribute value or null if not found
         */

        internal Object getAttribute(string name)
        {
            if (attributes.ContainsKey(name))
            {
                return(attributes[name]);
            }
            return(noStroke[name]);
        }
コード例 #2
0
        /**
         * Checks if the attribute exists.
         * @param name the attribute key
         * @return <CODE>true</CODE> if the attribute exists
         */

        internal bool isAttribute(string name)
        {
            if (attributes.ContainsKey(name))
            {
                return(true);
            }
            return(noStroke.ContainsKey(name));
        }
コード例 #3
0
 /// <summary>
 /// This method gets called when a start tag is encountered.
 /// </summary>
 /// <param name="uri"></param>
 /// <param name="lname"></param>
 /// <param name="name">the name of the tag that is encountered</param>
 /// <param name="attrs">the list of attributes</param>
 public override void startElement(String uri, String lname, String name, Hashtable attrs)
 {
     if (myTags.ContainsKey(name))
     {
         XmlPeer peer = (XmlPeer)myTags[name];
         handleStartingTags(peer.Tag, peer.getAttributes(attrs));
     }
     else
     {
         Properties attributes = new Properties();
         if (attrs != null)
         {
             foreach (string key in attrs.Keys)
             {
                 attributes.Add(key, (string)attrs[key]);
             }
         }
         handleStartingTags(name, attributes);
     }
 }
コード例 #4
0
        protected void flatGlyphs()
        {
            int[] tableLocation;
            tableLocation = (int[])tableDirectory["glyf"];
            if (tableLocation == null)
            {
                throw new DocumentException("Table 'glyf' does not exist in " + fileName);
            }
            int glyph0 = 0;

            if (!glyphsUsed.ContainsKey(glyph0))
            {
                glyphsUsed.Add(glyph0, null);
                glyphsInList.Add(glyph0);
            }
            tableGlyphOffset = tableLocation[TABLE_OFFSET];
            for (int k = 0; k < glyphsInList.Count; ++k)
            {
                int glyph = (int)glyphsInList[k];
                checkGlyphComposite(glyph);
            }
        }
コード例 #5
0
 internal void writeAllVisited()
 {
     while (nextRound.Count > 0)
     {
         ArrayList vec = nextRound;
         nextRound = new ArrayList();
         for (int k = 0; k < vec.Count; ++k)
         {
             int i = (int)vec[k];
             if (!visited.ContainsKey(i))
             {
                 visited.Add(i, null);
                 int n = i;
                 writer.addToBody(xrefObj[n], myXref[n]);
             }
         }
     }
 }
コード例 #6
0
        /**
         * Constructs a <CODE>PdfChunk</CODE>-object.
         *
         * @param chunk the original <CODE>Chunk</CODE>-object
         * @param action the <CODE>PdfAction</CODE> if the <CODE>Chunk</CODE> comes from an <CODE>Anchor</CODE>
         */

        internal PdfChunk(Chunk chunk, PdfAction action)
        {
            value = chunk.Content;

            iTextSharp.text.Font f = chunk.Font;
            float size             = f.Size;

            if (size == iTextSharp.text.Font.UNDEFINED)
            {
                size = 12;
            }
            BaseFont bf = f.BaseFont;
            int      family;
            int      style = f.Style;

            if (bf == null)
            {
                // translation of the font-family to a PDF font-family
                if (style == iTextSharp.text.Font.UNDEFINED)
                {
                    style = iTextSharp.text.Font.NORMAL;
                }
                switch (f.Family)
                {
                case iTextSharp.text.Font.COURIER:
                    switch (style & iTextSharp.text.Font.BOLDITALIC)
                    {
                    case iTextSharp.text.Font.BOLD:
                        family = PdfFont.COURIER_BOLD;
                        break;

                    case iTextSharp.text.Font.ITALIC:
                        family = PdfFont.COURIER_OBLIQUE;
                        break;

                    case iTextSharp.text.Font.BOLDITALIC:
                        family = PdfFont.COURIER_BOLDOBLIQUE;
                        break;

                    default:
                    case iTextSharp.text.Font.NORMAL:
                        family = PdfFont.COURIER;
                        break;
                    }
                    break;

                case iTextSharp.text.Font.TIMES_NEW_ROMAN:
                    switch (style & iTextSharp.text.Font.BOLDITALIC)
                    {
                    case iTextSharp.text.Font.BOLD:
                        family = PdfFont.TIMES_BOLD;
                        break;

                    case iTextSharp.text.Font.ITALIC:
                        family = PdfFont.TIMES_ITALIC;
                        break;

                    case iTextSharp.text.Font.BOLDITALIC:
                        family = PdfFont.TIMES_BOLDITALIC;
                        break;

                    default:
                    case iTextSharp.text.Font.NORMAL:
                        family = PdfFont.TIMES_ROMAN;
                        break;
                    }
                    break;

                case iTextSharp.text.Font.SYMBOL:
                    family = PdfFont.SYMBOL;
                    break;

                case iTextSharp.text.Font.ZAPFDINGBATS:
                    family = PdfFont.ZAPFDINGBATS;
                    break;

                default:
                case iTextSharp.text.Font.HELVETICA:
                    switch (style & iTextSharp.text.Font.BOLDITALIC)
                    {
                    case iTextSharp.text.Font.BOLD:
                        family = PdfFont.HELVETICA_BOLD;
                        break;

                    case iTextSharp.text.Font.ITALIC:
                        family = PdfFont.HELVETICA_OBLIQUE;
                        break;

                    case iTextSharp.text.Font.BOLDITALIC:
                        family = PdfFont.HELVETICA_BOLDOBLIQUE;
                        break;

                    default:
                    case iTextSharp.text.Font.NORMAL:
                        family = PdfFont.HELVETICA;
                        break;
                    }
                    break;
                }
                // creation of the PdfFont with the right size
                font = new PdfFont(family, size);
            }
            else
            {
                // bold simulation
                if ((style & iTextSharp.text.Font.BOLD) != 0)
                {
                    attributes.Add(Chunk.TEXTRENDERMODE, new Object[] { PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, size / 30f, null });
                }
                // italic simulation
                if ((style & iTextSharp.text.Font.ITALIC) != 0)
                {
                    attributes.Add(Chunk.SKEW, new float[] { 0, ITALIC_ANGLE });
                }

                font = new PdfFont(bf, size);
            }



            // other style possibilities
            Hashmap attr = chunk.Attributes;

            if (attr != null)
            {
                foreach (string name in attr.Keys)
                {
                    if (keysAttributes.ContainsKey(name))
                    {
                        attributes.Add(name, attr[name]);
                    }
                    else if (keysNoStroke.ContainsKey(name))
                    {
                        noStroke.Add(name, attr[name]);
                    }
                }
                if ("".Equals(attr[Chunk.GENERICTAG]))
                {
                    attributes.Add(Chunk.GENERICTAG, chunk.Content);
                }
            }
            if (f.isUnderlined())
            {
                attributes.Add(Chunk.UNDERLINE, null);
            }
            if (f.isStrikethru())
            {
                attributes.Add(Chunk.STRIKETHRU, null);
            }
            if (action != null)
            {
                attributes.Add(Chunk.ACTION, action);
            }
            // the color can't be stored in a PdfFont
            noStroke.Add(Chunk.COLOR, f.Color);
            noStroke.Add(Chunk.ENCODING, font.Font.Encoding);
            Object[] obj = (Object[])attributes[Chunk.IMAGE];
            if (obj == null)
            {
                image = null;
            }
            else
            {
                image         = (Image)obj[0];
                offsetX       = ((float)obj[1]);
                offsetY       = ((float)obj[2]);
                changeLeading = bool.Parse(obj[3].ToString());
            }
            font.Image     = image;
            encoding       = font.Font.Encoding;
            splitCharacter = (ISplitCharacter)noStroke[Chunk.SPLITCHARACTER];
            if (splitCharacter == null)
            {
                splitCharacter = this;
            }
        }
コード例 #7
0
ファイル: BaseFont.cs プロジェクト: aakkssqq/CreatePDFCSharp
        /** Creates a new font. This font can be one of the 14 built in types,
         * a Type1 font referred by an AFM file, a TrueType font (simple or collection) or a CJK font from the
         * Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier
         * appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
         * example would be "STSong-Light,Bold". Note that this modifiers do not work if
         * the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1".
         * This would get the second font (indexes start at 0), in this case "MS PGothic".
         * <P>
         * The fonts may or may not be cached depending on the flag <CODE>cached</CODE>.
         * If the <CODE>byte</CODE> arrays are present the font will be
         * read from them instead of the name. The name is still required to identify
         * the font type.
         * @param name the name of the font or it's location on file
         * @param encoding the encoding to be applied to this font
         * @param embedded true if the font is to be embedded in the PDF
         * @param cached true if the font comes from the cache or is added to
         * the cache if new. false if the font is always created new
         * @param ttfAfm the true type font or the afm in a byte array
         * @param pfb the pfb in a byte array
         * @return returns a new font. This font may come from the cache but only if cached
         * is true, otherwise it will always be created new
         * @throws DocumentException the font is invalid
         * @throws IOException the font file could not be read
         */
        public static BaseFont createFont(string name, string encoding, bool embedded, bool cached, byte[] ttfAfm, byte[] pfb)
        {
            string nameBase = getBaseName(name);

            encoding = normalizeEncoding(encoding);
            bool isBuiltinFonts14 = BuiltinFonts14.ContainsKey(name);
            bool isCJKFont        = isBuiltinFonts14 ? false : CJKFont.isCJKFont(nameBase, encoding);

            if (isBuiltinFonts14 || isCJKFont)
            {
                embedded = false;
            }
            else if (encoding.Equals(IDENTITY_H) || encoding.Equals(IDENTITY_V))
            {
                embedded = true;
            }
            BaseFont fontFound = null;
            BaseFont fontBuilt = null;
            string   key       = name + "\n" + encoding + "\n" + embedded;

            if (cached)
            {
                lock (fontCache) {
                    fontFound = (BaseFont)fontCache[key];
                }
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            if (isBuiltinFonts14 || name.ToLower().EndsWith(".afm"))
            {
                fontBuilt             = new Type1Font(name, encoding, embedded, ttfAfm, pfb);
                fontBuilt.fastWinansi = encoding.Equals(CP1252);
            }
            else if (nameBase.ToLower().EndsWith(".ttf") || nameBase.ToLower().IndexOf(".ttc,") > 0)
            {
                if (encoding.Equals(IDENTITY_H) || encoding.Equals(IDENTITY_V))
                {
                    fontBuilt = new TrueTypeFontUnicode(name, encoding, embedded, ttfAfm);
                }
                else
                {
                    fontBuilt             = new TrueTypeFont(name, encoding, embedded, ttfAfm);
                    fontBuilt.fastWinansi = encoding.Equals(CP1252);
                }
            }
            else if (nameBase.ToLower().EndsWith(".otf"))
            {
                fontBuilt             = new TrueTypeFont(name, encoding, embedded, ttfAfm);
                fontBuilt.fastWinansi = encoding.Equals(CP1252);
            }
            else if (isCJKFont)
            {
                fontBuilt = new CJKFont(name, encoding, embedded);
            }
            else
            {
                throw new DocumentException("Font '" + name + "' with '" + encoding + "' is not recognized.");
            }
            if (cached)
            {
                lock (fontCache) {
                    fontFound = (BaseFont)fontCache[key];
                    if (fontFound != null)
                    {
                        return(fontFound);
                    }
                    fontCache.Add(key, fontBuilt);
                }
            }
            return(fontBuilt);
        }
コード例 #8
0
        /** Converts the text into bytes to be placed in the document.
         * The conversion is done according to the font and the encoding and the characters
         * used are stored.
         * @param text the text to convert
         * @return the conversion
         */
        internal byte[] convertToBytes(string text)
        {
            byte[] b = null;
            switch (fontType)
            {
            case BaseFont.FONT_TYPE_T1:
            case BaseFont.FONT_TYPE_TT: {
                b = baseFont.convertToBytes(text);
                int len = b.Length;
                for (int k = 0; k < len; ++k)
                {
                    shortTag[((int)b[k]) & 0xff] = 1;
                }
                break;
            }

            case BaseFont.FONT_TYPE_CJK: {
                int len = text.Length;
                for (int k = 0; k < len; ++k)
                {
                    cjkTag[cjkFont.getCidCode(text[k])] = 0;
                }
                b = baseFont.convertToBytes(text);
                break;
            }

            case BaseFont.FONT_TYPE_TTUNI: {
                try {
                    int    len     = text.Length;
                    int[]  metrics = null;
                    char[] glyph   = new char[len];
                    int    i       = 0;
                    if (symbolic)
                    {
                        b   = PdfEncodings.convertToBytes(text, BaseFont.WINANSI);
                        len = b.Length;
                        for (int k = 0; k < len; ++k)
                        {
                            metrics = ttu.getMetricsTT(b[k] & 0xff);
                            if (metrics == null)
                            {
                                continue;
                            }
                            longTag.Add(metrics[0], new int[] { metrics[0], metrics[1], ttu.getUnicodeDifferences(b[k] & 0xff) });
                            glyph[i++] = (char)metrics[0];
                        }
                    }
                    else
                    {
                        for (int k = 0; k < len; ++k)
                        {
                            char c = text[k];
                            metrics = ttu.getMetricsTT(c);
                            if (metrics == null)
                            {
                                continue;
                            }
                            int m0 = metrics[0];
                            int gl = m0;
                            if (!longTag.ContainsKey(gl))
                            {
                                longTag.Add(gl, new int[] { m0, metrics[1], c });
                            }
                            glyph[i++] = (char)m0;
                        }
                    }
                    string s = new string(glyph, 0, i);
                    b = System.Text.Encoding.GetEncoding(CJKFont.CJK_ENCODING).GetBytes(s);
                }
                catch (Exception e) {
                    throw e;
                }
                break;
            }
            }
            return(b);
        }