Esempio n. 1
0
 public static PdfName ToPdfName(this StreamFilter streamFilter)
 {
     return(streamFilter switch
     {
         StreamFilter.DCTDecode => PdfName.Get("DCTDecode"),
         _ => throw new NotImplementedException(),
     });
        /**
         * Creates a new TrueType font embedder for the given TTF as a PDCIDFontType2.
         *
         * @param document parent document
         * @param dict font dictionary
         * @param ttf True Type Font
         * @param parent parent Type 0 font
         * @ if the TTF could not be read
         */
        public PdfCIDFontType2Embedder(Document document, PdfDictionary dict, TrueTypeFont ttf, bool embedSubset, PdfType0Font parent, bool vertical)
            : base(document, dict, ttf, embedSubset)
        {
            this.document = document;
            this.dict     = dict;
            this.parent   = parent;
            this.vertical = vertical;

            // parent Type 0 font
            dict[PdfName.Subtype]  = PdfName.Type0;
            dict[PdfName.BaseFont] = PdfName.Get(FontDescriptor.FontName);
            dict[PdfName.Encoding] = vertical ? PdfName.IdentityV : PdfName.IdentityH; // CID = GID

            // descendant CIDFont
            cidFont = CreateCIDFont();
            PdfArray descendantFonts = new PdfArray();

            descendantFonts.Add(document.File.Register(cidFont));
            dict[PdfName.DescendantFonts] = descendantFonts;

            if (!embedSubset)
            {
                // build GID -> Unicode map
                BuildToUnicodeCMap(null);
            }
        }
Esempio n. 3
0
        internal bool TryWriteToStream(PdfStream stream, out uint position)
        {
            position = 0;

            if (_isWritten)
            {
                return(false);
            }
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(_imageStream), "Internal image is already disposed");
            }

            position = (uint)stream.Position;

            stream.IndirectStream(this, _imageStream, this, static (image, dictionary) =>
            {
                dictionary
                .Type(ObjectType.XObject)
                .SubType(XObjectSubType.Image)
                .Write(PdfName.Get("Width"), image._width)
                .Write(PdfName.Get("Height"), image._height)
                .Write(PdfName.Get("ColorSpace"), PdfName.Get("DeviceRGB"))
                .Write(PdfName.Get("BitsPerComponent"), 8)
                .Write(PdfName.Get("Decode"), 0f, 1f, 0f, 1f, 0f, 1f);
            },
                                  StreamFilter.DCTDecode);

            _isWritten = true;

            return(true);
        }
        private PdfDictionary CreateCIDFont()
        {
            PdfDictionary cidFont = new PdfDictionary();

            // Type, Subtype
            cidFont[PdfName.Type]    = PdfName.Font;
            cidFont[PdfName.Subtype] = PdfName.CIDFontType2;
            // BaseFont
            cidFont[PdfName.BaseFont] = PdfName.Get(fontDescriptor.FontName);
            // CIDSystemInfo
            PdfDictionary info = toCIDSystemInfo("Adobe", "Identity", 0);

            cidFont[PdfName.CIDSystemInfo] = info;
            // FontDescriptor
            cidFont[PdfName.FontDescriptor] = fontDescriptor.BaseObject;

            // W - widths
            BuildWidths(cidFont);

            // Vertical metrics
            if (vertical)
            {
                BuildVerticalMetrics(cidFont);
            }

            // CIDToGIDMap
            cidFont[PdfName.CIDToGIDMap] = PdfName.Identity;

            return(cidFont);
        }
Esempio n. 5
0
        /**
         * This will load a PFB to be embedded into a document.
         *
         * @param doc The PDF document that will hold the embedded font.
         * @param dict The Font dictionary to write to.
         * @param pfbStream The pfb input.
         * @throws IOException If there is an error loading the data.
         */
        public PdfType1FontEmbedder(Document doc, PdfDictionary dict, Bytes.IInputStream pfbStream, Encoding encoding)
        {
            dict[PdfName.Subtype] = PdfName.Type1;

            // read the pfb
            byte[]    pfbBytes  = pfbStream.ToByteArray();
            PfbParser pfbParser = new PfbParser(pfbBytes);

            type1 = Type1Font.CreateWithPFB(pfbBytes);

            if (encoding == null)
            {
                fontEncoding = Type1Encoding.FromFontBox(type1.Encoding);
            }
            else
            {
                fontEncoding = encoding;
            }

            // build font descriptor
            FontDescriptor fd = BuildFontDescriptor(type1);

            PdfStream fontStream = new PdfStream(pfbParser.GetInputStream());

            fontStream.Header[PdfName.Length] = PdfInteger.Get(pfbParser.Size);
            for (int i = 0; i < pfbParser.Lengths.Length; i++)
            {
                fontStream.Header[new PdfName("Length" + (i + 1))] = PdfInteger.Get(pfbParser.Lengths[i]);
            }
            fd.FontFile = new FontFile(doc, fontStream);

            // set the values
            dict[PdfName.FontDescriptor] = fd.BaseObject;
            dict[PdfName.BaseFont]       = PdfName.Get(type1.Name);

            // widths
            List <int> widths = new List <int>(256);

            for (int code = 0; code <= 255; code++)
            {
                string name  = fontEncoding.GetName(code);
                int    width = (int)Math.Round(type1.GetWidth(name));
                widths.Add(width);
            }

            dict[PdfName.FirstChar] = PdfInteger.Get(0);
            dict[PdfName.LastChar]  = PdfInteger.Get(255);
            dict[PdfName.Widths]    = new PdfArray(widths.Select(p => PdfInteger.Get(p)));
            dict[PdfName.Encoding]  = encoding.GetPdfObject();
        }
        internal uint WriteToStream(PdfStream stream)
        {
            if (_isWritten)
            {
                throw new InvalidOperationException("Object is already written to stream.");
            }

            var position = (uint)stream.Position;

            stream.IndirectDictionary(this, static (did, dictionary) =>
            {
                if (!string.IsNullOrWhiteSpace(did.Title))
                {
                    dictionary.Write(PdfName.Get("Title"), _toPdfHexadecimalString(did.Title !));
                }
Esempio n. 7
0
        public PdfType1Font(Document context, string baseFont) : base(context, baseFont)
        {
            Dictionary[PdfName.Subtype]  = PdfName.Type1;
            Dictionary[PdfName.BaseFont] = PdfName.Get(baseFont);
            switch (baseFont)
            {
            case "ZapfDingbats":
                encoding = ZapfDingbatsEncoding.Instance;
                break;

            case "Symbol":
                encoding = SymbolEncoding.Instance;
                break;

            default:
                encoding = WinAnsiEncoding.Instance;
                Dictionary[PdfName.Encoding] = PdfName.WinAnsiEncoding;
                break;
            }

            // standard 14 fonts may be accessed concurrently, as they are singletons
            codeToBytesMap = new Dictionary <int, byte[]>();

            // todo: could load the PFB font here if we wanted to support Standard 14 embedding
            type1font = null;
            FontMapping <BaseFont> mapping = FontMappers.Instance.GetBaseFont(BaseFont, FontDescriptor);

            genericFont = mapping.Font;

            if (mapping.IsFallback)
            {
                string fontName;
                try
                {
                    fontName = genericFont.Name;
                }
                catch (IOException e)
                {
                    Debug.WriteLine($"debug: Couldn't get font name - setting to '?' {e}");
                    fontName = "?";
                }
                Debug.WriteLine($"warn: Using fallback font {fontName} for base font {BaseFont}");
            }
            isEmbedded          = false;
            isDamaged           = false;
            fontMatrixTransform = SKMatrix.Identity;
        }
Esempio n. 8
0
 public void SetName(int index, string value)
 {
     this[index] = PdfName.Get(value);
 }
Esempio n. 9
0
 public override PdfDirectObject GetPdfObject()
 {
     return(PdfName.Get("ZapfDingbatsEncoding"));
 }
Esempio n. 10
0
 public override PdfDirectObject GetPdfObject()
 {
     return(PdfName.Get("SymbolEncoding"));
 }