/** Gets the kerning between two Unicode characters. The characters * are converted to names and this names are used to find the kerning * pairs in the <CODE>Hashtable</CODE> <CODE>KernPairs</CODE>. * @param char1 the first char * @param char2 the second char * @return the kerning to be applied */ public override int GetKerning(int char1, int char2) { string first = GlyphList.UnicodeToName((int)char1); if (first == null) { return(0); } string second = GlyphList.UnicodeToName((int)char2); if (second == null) { return(0); } Object[] obj = (Object[])KernPairs[first]; if (obj == null) { return(0); } for (int k = 0; k < obj.Length; k += 2) { if (second.Equals(obj[k])) { return((int)obj[k + 1]); } } return(0); }
/** * Sets the kerning between two Unicode chars. * @param char1 the first char * @param char2 the second char * @param kern the kerning to apply in normalized 1000 units * @return <code>true</code> if the kerning was applied, <code>false</code> otherwise */ public override bool SetKerning(int char1, int char2, int kern) { String first = GlyphList.UnicodeToName((int)char1); if (first == null) { return(false); } String second = GlyphList.UnicodeToName((int)char2); if (second == null) { return(false); } Object[] obj = (Object[])KernPairs[first]; if (obj == null) { obj = new Object[] { second, kern }; KernPairs[first] = obj; return(true); } for (int k = 0; k < obj.Length; k += 2) { if (second.Equals(obj[k])) { obj[k + 1] = kern; return(true); } } int size = obj.Length; Object[] obj2 = new Object[size + 2]; Array.Copy(obj, 0, obj2, 0, size); obj2[size] = second; obj2[size + 1] = kern; KernPairs[first] = obj2; return(true); }
internal override void WriteFont(PdfWriter writer, PdfIndirectReference piRef, Object[] oParams) { if (this.writer != writer) { throw new ArgumentException("Type3 font used with the wrong PdfWriter"); } // Get first & lastchar ... int firstChar = 0; while (firstChar < usedSlot.Length && !usedSlot[firstChar]) { firstChar++; } if (firstChar == usedSlot.Length) { throw new DocumentException("No glyphs defined for Type3 font"); } int lastChar = usedSlot.Length - 1; while (lastChar >= firstChar && !usedSlot[lastChar]) { lastChar--; } int[] widths = new int[lastChar - firstChar + 1]; int[] invOrd = new int[lastChar - firstChar + 1]; int invOrdIndx = 0, w = 0; for (int u = firstChar; u <= lastChar; u++, w++) { if (usedSlot[u]) { invOrd[invOrdIndx++] = u; widths[w] = widths3[u]; } } PdfArray diffs = new PdfArray(); PdfDictionary charprocs = new PdfDictionary(); int last = -1; for (int k = 0; k < invOrdIndx; ++k) { int c = invOrd[k]; if (c > last) { last = c; diffs.Add(new PdfNumber(last)); } ++last; int c2 = invOrd[k]; String s = GlyphList.UnicodeToName(c2); if (s == null) { s = "a" + c2; } PdfName n = new PdfName(s); diffs.Add(n); Type3Glyph glyph = (Type3Glyph)char2glyph[(char)c2]; PdfStream stream = new PdfStream(glyph.ToPdf(null)); stream.FlateCompress(compressionLevel); PdfIndirectReference refp = writer.AddToBody(stream).IndirectReference; charprocs.Put(n, refp); } PdfDictionary font = new PdfDictionary(PdfName.FONT); font.Put(PdfName.SUBTYPE, PdfName.TYPE3); if (colorized) { font.Put(PdfName.FONTBBOX, new PdfRectangle(0, 0, 0, 0)); } else { font.Put(PdfName.FONTBBOX, new PdfRectangle(llx, lly, urx, ury)); } font.Put(PdfName.FONTMATRIX, new PdfArray(new float[] { 0.001f, 0, 0, 0.001f, 0, 0 })); font.Put(PdfName.CHARPROCS, writer.AddToBody(charprocs).IndirectReference); PdfDictionary encoding = new PdfDictionary(); encoding.Put(PdfName.DIFFERENCES, diffs); font.Put(PdfName.ENCODING, writer.AddToBody(encoding).IndirectReference); font.Put(PdfName.FIRSTCHAR, new PdfNumber(firstChar)); font.Put(PdfName.LASTCHAR, new PdfNumber(lastChar)); font.Put(PdfName.WIDTHS, writer.AddToBody(new PdfArray(widths)).IndirectReference); if (pageResources.HasResources()) { font.Put(PdfName.RESOURCES, writer.AddToBody(pageResources.Resources).IndirectReference); } writer.AddToBody(font, piRef); }
private void DoType1TT() { PdfObject enc = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING)); if (enc == null) { FillEncoding(null); } else { if (enc.IsName()) { FillEncoding((PdfName)enc); } else { PdfDictionary encDic = (PdfDictionary)enc; enc = PdfReader.GetPdfObject(encDic.Get(PdfName.BASEENCODING)); if (enc == null) { FillEncoding(null); } else { FillEncoding((PdfName)enc); } PdfArray diffs = encDic.GetAsArray(PdfName.DIFFERENCES); if (diffs != null) { diffmap = new IntHashtable(); int currentNumber = 0; for (int k = 0; k < diffs.Size; ++k) { PdfObject obj = diffs[k]; if (obj.IsNumber()) { currentNumber = ((PdfNumber)obj).IntValue; } else { int[] c = GlyphList.NameToUnicode(PdfName.DecodeName(((PdfName)obj).ToString())); if (c != null && c.Length > 0) { uni2byte[c[0]] = currentNumber; diffmap[c[0]] = currentNumber; } ++currentNumber; } } } } } PdfArray newWidths = font.GetAsArray(PdfName.WIDTHS); PdfNumber first = font.GetAsNumber(PdfName.FIRSTCHAR); PdfNumber last = font.GetAsNumber(PdfName.LASTCHAR); if (BuiltinFonts14.ContainsKey(fontName)) { BaseFont bf; bf = BaseFont.CreateFont(fontName, WINANSI, false); int[] e = uni2byte.ToOrderedKeys(); for (int k = 0; k < e.Length; ++k) { int n = uni2byte[e[k]]; widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k])); } if (diffmap != null) //widths for differences must override existing ones { e = diffmap.ToOrderedKeys(); for (int k = 0; k < e.Length; ++k) { int n = diffmap[e[k]]; widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k])); } diffmap = null; } Ascender = bf.GetFontDescriptor(ASCENT, 1000); CapHeight = bf.GetFontDescriptor(CAPHEIGHT, 1000); Descender = bf.GetFontDescriptor(DESCENT, 1000); ItalicAngle = bf.GetFontDescriptor(ITALICANGLE, 1000); llx = bf.GetFontDescriptor(BBOXLLX, 1000); lly = bf.GetFontDescriptor(BBOXLLY, 1000); urx = bf.GetFontDescriptor(BBOXURX, 1000); ury = bf.GetFontDescriptor(BBOXURY, 1000); } if (first != null && last != null && newWidths != null) { int f = first.IntValue; for (int k = 0; k < newWidths.Size; ++k) { widths[f + k] = newWidths.GetAsNumber(k).IntValue; } } FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR)); }