internal bool SetField(String field, PdfObject value) { Dictionary<String, Object> map = fields; StringTokenizer tk = new StringTokenizer(field, "."); if (!tk.HasMoreTokens()) return false; while (true) { String s = tk.NextToken(); Object obj; map.TryGetValue(s, out obj); if (tk.HasMoreTokens()) { if (obj == null) { obj = new Dictionary<String, Object>(); map[s] = obj; map = (Dictionary<string,object>)obj; continue; } else if (obj is Dictionary<String, Object>) map = (Dictionary<String, Object>)obj; else return false; } else { if (!(obj is Dictionary<String, Object>)) { map[s] = value; return true; } else return false; } } }
internal bool SetField(String field, PdfObject value) { Hashtable map = fields; StringTokenizer tk = new StringTokenizer(field, "."); if (!tk.HasMoreTokens()) return false; while (true) { String s = tk.NextToken(); Object obj = map[s]; if (tk.HasMoreTokens()) { if (obj == null) { obj = new Hashtable(); map[s] = obj; map = (Hashtable)obj; continue; } else if (obj is Hashtable) map = (Hashtable)obj; else return false; } else { if (!(obj is Hashtable)) { map[s] = value; return true; } else return false; } } }
static GlyphList() { Stream istr = null; try { istr = BaseFont.GetResourceStream("glyphlist.txt"); if (istr == null) { string msg = string.Format("glyphlist.txt not found as resource. (path: {0})", BaseFont.RESOURCE_PATH); throw new Exception(msg); } byte[] buf = new byte[1024]; MemoryStream outp = new MemoryStream(); while (true) { int size = istr.Read(buf, 0, buf.Length); if (size == 0) break; outp.Write(buf, 0, size); } istr.Close(); istr = null; String s = PdfEncodings.ConvertToString(outp.ToArray(), null); StringTokenizer tk = new StringTokenizer(s, "\r\n"); while (tk.HasMoreTokens()) { String line = tk.NextToken(); if (line.StartsWith("#")) continue; StringTokenizer t2 = new StringTokenizer(line, " ;\r\n\t\f"); String name = null; String hex = null; if (!t2.HasMoreTokens()) continue; name = t2.NextToken(); if (!t2.HasMoreTokens()) continue; hex = t2.NextToken(); int num = int.Parse(hex, NumberStyles.HexNumber); unicode2names[num] = name; names2unicode[name] = new int[]{num}; } } catch (Exception e) { Console.Error.WriteLine("glyphlist.txt loading error: " + e.Message); } finally { if (istr != null) { try { istr.Close(); } catch { // empty on purpose } } } }
public override IList<IElement> Start(IWorkerContext ctx, Tag tag) { float height = 0; //TODO check viewbox float width = 0; //TODO check viewbox Rectangle r = null; String viewbox = ""; IDictionary<String, String> attributes = tag.Attributes; if(attributes != null){ try { if (attributes.ContainsKey("height")) { height = float.Parse(attributes["height"]); } }catch { //TODO } try{ if (attributes.ContainsKey("width")) { width = float.Parse(attributes["width"]); } }catch { //TODO } try{ if (attributes.TryGetValue("viewBox", out viewbox)) { r = new Rectangle(0, 0); StringTokenizer st = new StringTokenizer(viewbox); if (st.HasMoreTokens()) r.Right = float.Parse(st.NextToken()); if (st.HasMoreTokens()) r.Bottom = -float.Parse(st.NextToken()); if (st.HasMoreTokens()) r.Left = r.Right + float.Parse(st.NextToken()); if (st.HasMoreTokens()) r.Top = r.Bottom + float.Parse(st.NextToken()); r.Normalize(); } } catch { //TODO } } if (r == null) { r = new Rectangle(width, height); } else if (width == 0 && height == 0) { width = r.Width; height = r.Height; } IList<IElement> elems = new List<IElement>(); elems.Add(new Svg(height, width, r, tag.CSS)); return elems; }
public void DrawMultiLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury) { PdfAppearance tp = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly); PdfAppearance tp2 = (PdfAppearance)tp.Duplicate; tp2.SetFontAndSize(font, fontSize); tp2.ResetRGBColorFill(); field.DefaultAppearanceString = tp2; tp.DrawTextField(0f, 0f, urx - llx, ury - lly); tp.BeginVariableText(); tp.SaveState(); tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f); tp.Clip(); tp.NewPath(); tp.BeginText(); tp.SetFontAndSize(font, fontSize); tp.ResetRGBColorFill(); tp.SetTextMatrix(4, 5); System.util.StringTokenizer tokenizer = new System.util.StringTokenizer(text, "\n"); float yPos = ury - lly; while (tokenizer.HasMoreTokens()) { yPos -= fontSize * 1.2f; tp.ShowTextAligned(PdfContentByte.ALIGN_LEFT, tokenizer.NextToken(), 3, yPos, 0); } tp.EndText(); tp.RestoreState(); tp.EndVariableText(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
public override IList<IElement> End(IWorkerContext ctx, Tag tag, IList<IElement> currentContent) { IList<IElement> elems = new List<IElement>(); IDictionary<String, String> attributes = tag.Attributes; if (attributes != null && attributes.ContainsKey(POINTS)) { String str = attributes[POINTS]; StringTokenizer tokenizer = new StringTokenizer(CleanPath(str), ", \t\n\r\f"); IList<String> values = new List<String>(); while (tokenizer.HasMoreTokens()) { String value = tokenizer.NextToken().Trim(); //System.out.Println(value); values.Add(value); } PathBean.Builder pathBuilder = new PathBean.Builder(); if (values.Count % 2 == 1) { values.RemoveAt(values.Count - 1); } if (values.Count % 2 == 0) { for (int i = 0; i < (values.Count / 2); i++) { PathItem.Builder itemBuilder = new PathItem.Builder(); if (i == 0) { itemBuilder.SetType('M'); } else { itemBuilder.SetType('L'); } itemBuilder.AddCoordinate(values[i * 2]); itemBuilder.AddCoordinate(values[(i * 2) + 1]); pathBuilder.SetPathItem(itemBuilder.Build()); } } if (tag.Name.Equals(SvgTagNames.POLYGON)) { PathItem.Builder itemBuilder = new PathItem.Builder(); itemBuilder.SetType('z'); pathBuilder.SetPathItem(itemBuilder.Build()); } elems.Add(new Path(pathBuilder.Build(), tag.CSS)); return elems; } else { return new List<IElement>(0); } }
static IList<String> SplitString(String transform) { List<String> list = new List<String>(); StringTokenizer tokenizer = new StringTokenizer(transform, ")", false); while (tokenizer.HasMoreTokens()) { list.Add(tokenizer.NextToken().Trim() + ")"); } return list; }
/** * Creates an Table object based on a list of properties. * @param attributes * @return a Table */ public static Table GetTable(Properties attributes) { String value; Table table; value = attributes[ElementTags.WIDTHS]; if (value != null) { StringTokenizer widthTokens = new StringTokenizer(value, ";"); ArrayList values = new ArrayList(); while (widthTokens.HasMoreTokens()) { values.Add(widthTokens.NextToken()); } table = new Table(values.Count); float[] widths = new float[table.Columns]; for (int i = 0; i < values.Count; i++) { value = (String)values[i]; widths[i] = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } table.Widths = widths; } else { value = attributes[ElementTags.COLUMNS]; try { table = new Table(int.Parse(value)); } catch { table = new Table(1); } } table.Border = Table.BOX; table.BorderWidth = 1; table.DefaultCell.Border = Table.BOX; value = attributes[ElementTags.LASTHEADERROW]; if (value != null) { table.LastHeaderRow = int.Parse(value); } value = attributes[ElementTags.ALIGN]; if (value != null) { table.SetAlignment(value); } value = attributes[ElementTags.CELLSPACING]; if (value != null) { table.Spacing = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } value = attributes[ElementTags.CELLPADDING]; if (value != null) { table.Padding = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } value = attributes[ElementTags.OFFSET]; if (value != null) { table.Offset = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } value = attributes[ElementTags.WIDTH]; if (value != null) { if (value.EndsWith("%")) table.Width = float.Parse(value.Substring(0, value.Length - 1), System.Globalization.NumberFormatInfo.InvariantInfo); else { table.Width = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); table.Locked = true; } } table.TableFitsPage = Utilities.CheckTrueOrFalse(attributes, ElementTags.TABLEFITSPAGE); table.CellsFitPage = Utilities.CheckTrueOrFalse(attributes, ElementTags.CELLSFITPAGE); table.Convert2pdfptable = Utilities.CheckTrueOrFalse(attributes, ElementTags.CONVERT2PDFP); SetRectangleProperties(table, attributes); return table; }
/// <summary> /// This method parses a string with attributes and returns a Properties object. /// </summary> /// <param name="str">a string of this form: 'key1="value1"; key2="value2";... keyN="valueN" '</param> /// <returns>a Properties object</returns> public static Properties ParseAttributes(string str) { Properties result = new Properties(); if (str == null) return result; StringTokenizer keyValuePairs = new StringTokenizer(str, ";"); StringTokenizer keyValuePair; string key; string value; while (keyValuePairs.HasMoreTokens()) { keyValuePair = new StringTokenizer(keyValuePairs.NextToken(), ":"); if (keyValuePair.HasMoreTokens()) key = keyValuePair.NextToken().Trim().Trim(); else continue; if (keyValuePair.HasMoreTokens()) value = keyValuePair.NextToken().Trim(); else continue; if (value.StartsWith("\"")) value = value.Substring(1); if (value.EndsWith("\"")) value = value.Substring(0, value.Length - 1); result.Add(key.ToLower(CultureInfo.InvariantCulture), value); } return result; }
public Font GetFont(ChainedProperties props) { String face = props[ElementTags.FACE]; if (face != null) { StringTokenizer tok = new StringTokenizer(face, ","); while (tok.HasMoreTokens()) { face = tok.NextToken().Trim(); if (face.StartsWith("\"")) face = face.Substring(1); if (face.EndsWith("\"")) face = face.Substring(0, face.Length - 1); if (fontImp.IsRegistered(face)) break; } } int style = 0; if (props.HasProperty(HtmlTags.I)) style |= Font.ITALIC; if (props.HasProperty(HtmlTags.B)) style |= Font.BOLD; if (props.HasProperty(HtmlTags.U)) style |= Font.UNDERLINE; if (props.HasProperty(HtmlTags.S)) style |= Font.STRIKETHRU ; String value = props[ElementTags.SIZE]; float size = 12; if (value != null) size = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); BaseColor color = Markup.DecodeColor(props["color"]); String encoding = props["encoding"]; if (encoding == null) encoding = BaseFont.WINANSI; return fontImp.GetFont(face, encoding, true, size, style, color); }
private static void SetParagraphLeading(Paragraph p, String leading) { if (leading == null) { p.SetLeading(0, 1.5f); return; } try { StringTokenizer tk = new StringTokenizer(leading, " ,"); String v = tk.NextToken(); float v1 = float.Parse(v, System.Globalization.NumberFormatInfo.InvariantInfo); if (!tk.HasMoreTokens()) { p.SetLeading(v1, 0); return; } v = tk.NextToken(); float v2 = float.Parse(v, System.Globalization.NumberFormatInfo.InvariantInfo); p.SetLeading(v1, v2); } catch { p.SetLeading(0, 1.5f); } }
/** Gets the field value. * @param field the field name * @return the field value or <CODE>null</CODE> if not found */ public String GetField(String field) { Hashtable map = fields; StringTokenizer tk = new StringTokenizer(field, "."); if (!tk.HasMoreTokens()) return null; while (true) { String s = tk.NextToken(); Object obj = map[s]; if (obj == null) return null; if (tk.HasMoreTokens()) { if (obj is Hashtable) map = (Hashtable)obj; else return null; } else { if (obj is Hashtable) return null; else { if (((PdfObject)obj).IsString()) return ((PdfString)obj).ToUnicodeString(); else return PdfName.DecodeName(obj.ToString()); } } } }
internal void MergeField(String name, AcroFields.Item item) { Dictionary<string,object> map = fieldTree; StringTokenizer tk = new StringTokenizer(name, "."); if (!tk.HasMoreTokens()) return; while (true) { String s = tk.NextToken(); Object obj; map.TryGetValue(s, out obj); if (tk.HasMoreTokens()) { if (obj == null) { obj = new Dictionary<string,object>(); map[s] = obj; map = (Dictionary<string,object>)obj; continue; } else if (obj is Dictionary<string,object>) map = (Dictionary<string,object>)obj; else return; } else { if (obj is Dictionary<string,object>) return; PdfDictionary merged = item.GetMerged(0); if (obj == null) { PdfDictionary field = new PdfDictionary(); if (PdfName.SIG.Equals(merged.Get(PdfName.FT))) hasSignature = true; foreach (PdfName key in merged.Keys) { if (fieldKeys.ContainsKey(key)) field.Put(key, merged.Get(key)); } List<object> list = new List<object>(); list.Add(field); CreateWidgets(list, item); map[s] = list; } else { List<object> list = (List<object>)obj; PdfDictionary field = (PdfDictionary)list[0]; PdfName type1 = (PdfName)field.Get(PdfName.FT); PdfName type2 = (PdfName)merged.Get(PdfName.FT); if (type1 == null || !type1.Equals(type2)) return; int flag1 = 0; PdfObject f1 = field.Get(PdfName.FF); if (f1 != null && f1.IsNumber()) flag1 = ((PdfNumber)f1).IntValue; int flag2 = 0; PdfObject f2 = merged.Get(PdfName.FF); if (f2 != null && f2.IsNumber()) flag2 = ((PdfNumber)f2).IntValue; if (type1.Equals(PdfName.BTN)) { if (((flag1 ^ flag2) & PdfFormField.FF_PUSHBUTTON) != 0) return; if ((flag1 & PdfFormField.FF_PUSHBUTTON) == 0 && ((flag1 ^ flag2) & PdfFormField.FF_RADIO) != 0) return; } else if (type1.Equals(PdfName.CH)) { if (((flag1 ^ flag2) & PdfFormField.FF_COMBO) != 0) return; } CreateWidgets(list, item); } return; } } }
/** * Creates the <CODE>widths</CODE> and the <CODE>differences</CODE> arrays * @throws UnsupportedEncodingException the encoding is not supported */ protected void CreateEncoding() { if (encoding.StartsWith("#")) { specialMap = new IntHashtable(); StringTokenizer tok = new StringTokenizer(encoding.Substring(1), " ,\t\n\r\f"); if (tok.NextToken().Equals("full")) { while (tok.HasMoreTokens()) { String order = tok.NextToken(); String name = tok.NextToken(); char uni = (char)int.Parse(tok.NextToken(), NumberStyles.HexNumber); int orderK; if (order.StartsWith("'")) orderK = (int)order[1]; else orderK = int.Parse(order); orderK %= 256; specialMap[(int)uni] = orderK; differences[orderK] = name; unicodeDifferences[orderK] = uni; widths[orderK] = GetRawWidth((int)uni, name); charBBoxes[orderK] = GetRawCharBBox((int)uni, name); } } else { int k = 0; if (tok.HasMoreTokens()) k = int.Parse(tok.NextToken()); while (tok.HasMoreTokens() && k < 256) { String hex = tok.NextToken(); int uni = int.Parse(hex, NumberStyles.HexNumber) % 0x10000; String name = GlyphList.UnicodeToName(uni); if (name != null) { specialMap[uni] = k; differences[k] = name; unicodeDifferences[k] = (char)uni; widths[k] = GetRawWidth(uni, name); charBBoxes[k] = GetRawCharBBox(uni, name); ++k; } } } for (int k = 0; k < 256; ++k) { if (differences[k] == null) { differences[k] = notdef; } } } else if (fontSpecific) { for (int k = 0; k < 256; ++k) { widths[k] = GetRawWidth(k, null); charBBoxes[k] = GetRawCharBBox(k, null); } } else { string s; string name; char c; byte[] b = new byte[1]; for (int k = 0; k < 256; ++k) { b[0] = (byte)k; s = PdfEncodings.ConvertToString(b, encoding); if (s.Length > 0) { c = s[0]; } else { c = '?'; } name = GlyphList.UnicodeToName((int)c); if (name == null) name = notdef; differences[k] = name; this.UnicodeDifferences[k] = c; widths[k] = GetRawWidth((int)c, name); charBBoxes[k] = GetRawCharBBox((int)c, name); } } }
/** * Sets the leading of a Paragraph object. * @param paragraph the Paragraph for which we set the leading * @param leading the String value of the leading */ protected static void SetParagraphLeading(Paragraph paragraph, String leading) { // default leading if (leading == null) { paragraph.SetLeading(0, 1.5f); return; } try { StringTokenizer tk = new StringTokenizer(leading, " ,"); // absolute leading String v = tk.NextToken(); float v1 = float.Parse(v, CultureInfo.InvariantCulture); if (!tk.HasMoreTokens()) { paragraph.SetLeading(v1, 0); return; } // relative leading v = tk.NextToken(); float v2 = float.Parse(v, CultureInfo.InvariantCulture); paragraph.SetLeading(v1, v2); } catch { // default leading paragraph.SetLeading(0, 1.5f); } }
/** Removes the field value. * @param field the field name * @return <CODE>true</CODE> if the field was found and removed, * <CODE>false</CODE> otherwise */ public bool RemoveField(String field) { Hashtable map = fields; StringTokenizer tk = new StringTokenizer(field, "."); if (!tk.HasMoreTokens()) return false; ArrayList hist = new ArrayList(); while (true) { String s = tk.NextToken(); Object obj = map[s]; if (obj == null) return false; hist.Add(map); hist.Add(s); if (tk.HasMoreTokens()) { if (obj is Hashtable) map = (Hashtable)obj; else return false; } else { if (obj is Hashtable) return false; else break; } } for (int k = hist.Count - 2; k >= 0; k -= 2) { map = (Hashtable)hist[k]; String s = (String)hist[k + 1]; map.Remove(s); if (map.Count > 0) break; } return true; }
internal static IntHashtable CreateMetric(string s) { IntHashtable h = new IntHashtable(); StringTokenizer tk = new StringTokenizer(s); while (tk.HasMoreTokens()) { int n1 = int.Parse(tk.NextToken()); h[n1] = int.Parse(tk.NextToken()); } return h; }
static IList<float> GetValuesFromStr(String str) { try { int index = str.IndexOf('(') + 1; int count = str.IndexOf(')') - index; String numbers = str.Substring(index, count); IList<float> result = new List<float>(); StringTokenizer tokenizer = new StringTokenizer(numbers, ","); while (tokenizer.HasMoreTokens()) { result.Add(float.Parse(tokenizer.NextToken())); } return result; } catch { throw new SvgParseException("Could not parse the transform"); } }
internal void MergeField(String name, AcroFields.Item item) { Hashtable map = fieldTree; StringTokenizer tk = new StringTokenizer(name, "."); if (!tk.HasMoreTokens()) return; while (true) { String s = tk.NextToken(); Object obj = map[s]; if (tk.HasMoreTokens()) { if (obj == null) { obj = new Hashtable(); map[s] = obj; map = (Hashtable)obj; continue; } else if (obj is Hashtable) map = (Hashtable)obj; else return; } else { if (obj is Hashtable) return; PdfDictionary merged = (PdfDictionary)item.merged[0]; if (obj == null) { PdfDictionary field = new PdfDictionary(); foreach (PdfName key in merged.Keys) { if (fieldKeys.ContainsKey(key)) field.Put(key, merged.Get(key)); } ArrayList list = new ArrayList(); list.Add(field); CreateWidgets(list, item); map[s] = list; } else { ArrayList list = (ArrayList)obj; PdfDictionary field = (PdfDictionary)list[0]; PdfName type1 = (PdfName)field.Get(PdfName.FT); PdfName type2 = (PdfName)merged.Get(PdfName.FT); if (type1 == null || !type1.Equals(type2)) return; int flag1 = 0; PdfObject f1 = field.Get(PdfName.FF); if (f1 != null && f1.IsNumber()) flag1 = ((PdfNumber)f1).IntValue; int flag2 = 0; PdfObject f2 = merged.Get(PdfName.FF); if (f2 != null && f2.IsNumber()) flag2 = ((PdfNumber)f2).IntValue; if (type1.Equals(PdfName.BTN)) { if (((flag1 ^ flag2) & PdfFormField.FF_PUSHBUTTON) != 0) return; if ((flag1 & PdfFormField.FF_PUSHBUTTON) == 0 && ((flag1 ^ flag2) & PdfFormField.FF_RADIO) != 0) return; } else if (type1.Equals(PdfName.CH)) { if (((flag1 ^ flag2) & PdfFormField.FF_COMBO) != 0) return; } CreateWidgets(list, item); } return; } } }
static HTMLWorker() { StringTokenizer tok = new StringTokenizer(tagsSupportedString); while (tok.HasMoreTokens()) tagsSupported[tok.NextToken()] = null; }
/** Creates a CJK font. * @param fontName the name of the font * @param enc the encoding of the font * @param emb always <CODE>false</CODE>. CJK font and not embedded * @throws DocumentException on error * @throws IOException on error */ internal CJKFont(string fontName, string enc, bool emb) { LoadProperties(); this.FontType = FONT_TYPE_CJK; string nameBase = GetBaseName(fontName); if (!IsCJKFont(nameBase, enc)) throw new DocumentException("Font '" + fontName + "' with '" + enc + "' encoding is not a CJK font."); if (nameBase.Length < fontName.Length) { style = fontName.Substring(nameBase.Length); fontName = nameBase; } this.fontName = fontName; encoding = CJK_ENCODING; vertical = enc.EndsWith("V"); CMap = enc; if (enc.StartsWith("Identity-")) { cidDirect = true; string s = cjkFonts[fontName]; s = s.Substring(0, s.IndexOf('_')); char[] c = (char[])allCMaps[s]; if (c == null) { c = ReadCMap(s); if (c == null) throw new DocumentException("The cmap " + s + " does not exist as a resource."); c[CID_NEWLINE] = '\n'; allCMaps.Add(s, c); } translationMap = c; } else { char[] c = (char[])allCMaps[enc]; if (c == null) { string s = cjkEncodings[enc]; if (s == null) throw new DocumentException("The resource cjkencodings.properties does not contain the encoding " + enc); StringTokenizer tk = new StringTokenizer(s); string nt = tk.NextToken(); c = (char[])allCMaps[nt]; if (c == null) { c = ReadCMap(nt); allCMaps.Add(nt, c); } if (tk.HasMoreTokens()) { string nt2 = tk.NextToken(); char[] m2 = ReadCMap(nt2); for (int k = 0; k < 0x10000; ++k) { if (m2[k] == 0) m2[k] = c[k]; } allCMaps.Add(enc, m2); c = m2; } } translationMap = c; } fontDesc = (Hashtable)allFonts[fontName]; if (fontDesc == null) { fontDesc = ReadFontProperties(fontName); allFonts.Add(fontName, fontDesc); } hMetrics = (IntHashtable)fontDesc["W"]; vMetrics = (IntHashtable)fontDesc["W2"]; }
virtual public void Text(String str) { StringTokenizer tk = new StringTokenizer(str); while (tk.HasMoreTokens()) { String word = tk.NextToken(); // System.out.Println("\"" + word + "\""); switch (currElement) { case ELEM_CLASSES: consumer.AddClass(word); break; case ELEM_EXCEPTIONS: exception.Add(word); exception = NormalizeException(exception); consumer.AddException(GetExceptionWord(exception), new List<object>(exception)); exception.Clear(); break; case ELEM_PATTERNS: consumer.AddPattern(GetPattern(word), GetInterletterValues(word)); break; } } }
internal static PdfArray CreateDestinationArray(String value, PdfWriter writer) { PdfArray ar = new PdfArray(); StringTokenizer tk = new StringTokenizer(value); int n = int.Parse(tk.NextToken()); ar.Add(writer.GetPageReference(n)); if (!tk.HasMoreTokens()) { ar.Add(PdfName.XYZ); ar.Add(new float[]{0, 10000, 0}); } else { String fn = tk.NextToken(); if (fn.StartsWith("/")) fn = fn.Substring(1); ar.Add(new PdfName(fn)); for (int k = 0; k < 4 && tk.HasMoreTokens(); ++k) { fn = tk.NextToken(); if (fn.Equals("null")) ar.Add(PdfNull.PDFNULL); else ar.Add(new PdfNumber(fn)); } } return ar; }
/** Creates a CJK font. * @param fontName the name of the font * @param enc the encoding of the font * @param emb always <CODE>false</CODE>. CJK font and not embedded * @throws DocumentException on error * @throws IOException on error */ internal CJKFont(string fontName, string enc, bool emb) { LoadProperties(); this.FontType = FONT_TYPE_CJK; string nameBase = GetBaseName(fontName); if (!IsCJKFont(nameBase, enc)) throw new DocumentException(MessageLocalization.GetComposedMessage("font.1.with.2.encoding.is.not.a.cjk.font", fontName, enc)); if (nameBase.Length < fontName.Length) { style = fontName.Substring(nameBase.Length); fontName = nameBase; } this.fontName = fontName; encoding = CJK_ENCODING; vertical = enc.EndsWith("V"); CMap = enc; if (enc.StartsWith("Identity-")) { cidDirect = true; string s = cjkFonts[fontName]; s = s.Substring(0, s.IndexOf('_')); char[] c; lock (allCMaps) { allCMaps.TryGetValue(s, out c); } if (c == null) { c = ReadCMap(s); if (c == null) throw new DocumentException(MessageLocalization.GetComposedMessage("the.cmap.1.does.not.exist.as.a.resource", s)); c[CID_NEWLINE] = '\n'; lock (allCMaps) { allCMaps[s] = c; } } translationMap = c; } else { char[] c; lock (allCMaps) { allCMaps.TryGetValue(enc, out c); } if (c == null) { string s = cjkEncodings[enc]; if (s == null) throw new DocumentException(MessageLocalization.GetComposedMessage("the.resource.cjkencodings.properties.does.not.contain.the.encoding.1", enc)); StringTokenizer tk = new StringTokenizer(s); string nt = tk.NextToken(); lock (allCMaps) { allCMaps.TryGetValue(nt, out c); } if (c == null) { c = ReadCMap(nt); lock (allCMaps) { allCMaps[nt] = c; } } if (tk.HasMoreTokens()) { string nt2 = tk.NextToken(); char[] m2 = ReadCMap(nt2); for (int k = 0; k < 0x10000; ++k) { if (m2[k] == 0) m2[k] = c[k]; } lock (allCMaps) { allCMaps[enc] = m2; } c = m2; } } translationMap = c; } lock (allFonts) { allFonts.TryGetValue(fontName, out fontDesc); } allFonts.TryGetValue(fontName, out fontDesc); if (fontDesc == null) { fontDesc = ReadFontProperties(fontName); lock (allFonts) { allFonts[fontName] = fontDesc; } } hMetrics = (IntHashtable)fontDesc["W"]; vMetrics = (IntHashtable)fontDesc["W2"]; }
/** Removes the field value. * @param field the field name * @return <CODE>true</CODE> if the field was found and removed, * <CODE>false</CODE> otherwise */ public bool RemoveField(String field) { Dictionary<String, Object> map = fields; StringTokenizer tk = new StringTokenizer(field, "."); if (!tk.HasMoreTokens()) return false; List<object> hist = new List<object>(); while (true) { String s = tk.NextToken(); Object obj; map.TryGetValue(s, out obj); if (obj == null) return false; hist.Add(map); hist.Add(s); if (tk.HasMoreTokens()) { if (obj is Dictionary<String, Object>) map = (Dictionary<String, Object>)obj; else return false; } else { if (obj is Dictionary<String, Object>) return false; else break; } } for (int k = hist.Count - 2; k >= 0; k -= 2) { map = (Dictionary<String, Object>)hist[k]; String s = (String)hist[k + 1]; map.Remove(s); if (map.Count > 0) break; } return true; }
internal static void CreateOutlineAction(PdfDictionary outline, Dictionary<String, Object> map, PdfWriter writer, bool namedAsNames) { try { String action = GetVal(map, "Action"); if ("GoTo".Equals(action)) { String p; if ((p = GetVal(map, "Named")) != null) { if (namedAsNames) outline.Put(PdfName.DEST, new PdfName(p)); else outline.Put(PdfName.DEST, new PdfString(p, null)); } else if ((p = GetVal(map, "Page")) != null) { PdfArray ar = new PdfArray(); StringTokenizer tk = new StringTokenizer(p); int n = int.Parse(tk.NextToken()); ar.Add(writer.GetPageReference(n)); if (!tk.HasMoreTokens()) { ar.Add(PdfName.XYZ); ar.Add(new float[]{0, 10000, 0}); } else { String fn = tk.NextToken(); if (fn.StartsWith("/")) fn = fn.Substring(1); ar.Add(new PdfName(fn)); for (int k = 0; k < 4 && tk.HasMoreTokens(); ++k) { fn = tk.NextToken(); if (fn.Equals("null")) ar.Add(PdfNull.PDFNULL); else ar.Add(new PdfNumber(fn)); } } outline.Put(PdfName.DEST, ar); } } else if ("GoToR".Equals(action)) { String p; PdfDictionary dic = new PdfDictionary(); if ((p = GetVal(map, "Named")) != null) dic.Put(PdfName.D, new PdfString(p, null)); else if ((p = GetVal(map, "NamedN")) != null) dic.Put(PdfName.D, new PdfName(p)); else if ((p = GetVal(map, "Page")) != null){ PdfArray ar = new PdfArray(); StringTokenizer tk = new StringTokenizer(p); ar.Add(new PdfNumber(tk.NextToken())); if (!tk.HasMoreTokens()) { ar.Add(PdfName.XYZ); ar.Add(new float[]{0, 10000, 0}); } else { String fn = tk.NextToken(); if (fn.StartsWith("/")) fn = fn.Substring(1); ar.Add(new PdfName(fn)); for (int k = 0; k < 4 && tk.HasMoreTokens(); ++k) { fn = tk.NextToken(); if (fn.Equals("null")) ar.Add(PdfNull.PDFNULL); else ar.Add(new PdfNumber(fn)); } } dic.Put(PdfName.D, ar); } String file = GetVal(map, "File"); if (dic.Size > 0 && file != null) { dic.Put(PdfName.S, PdfName.GOTOR); dic.Put(PdfName.F, new PdfString(file)); String nw = GetVal(map, "NewWindow"); if (nw != null) { if (nw.Equals("true")) dic.Put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE); else if (nw.Equals("false")) dic.Put(PdfName.NEWWINDOW, PdfBoolean.PDFFALSE); } outline.Put(PdfName.A, dic); } } else if ("URI".Equals(action)) { String uri = GetVal(map, "URI"); if (uri != null) { PdfDictionary dic = new PdfDictionary(); dic.Put(PdfName.S, PdfName.URI); dic.Put(PdfName.URI, new PdfString(uri)); outline.Put(PdfName.A, dic); } } else if ("JS".Equals(action)) { String code = GetVal(map, "Code"); if(code != null) { outline.Put(PdfName.A, PdfAction.JavaScript(code, writer)); } } else if ("Launch".Equals(action)) { String file = GetVal(map, "File"); if (file != null) { PdfDictionary dic = new PdfDictionary(); dic.Put(PdfName.S, PdfName.LAUNCH); dic.Put(PdfName.F, new PdfString(file)); outline.Put(PdfName.A, dic); } } } catch { // empty on purpose } }
/** Gets the field value. * @param field the field name * @return the field value or <CODE>null</CODE> if not found */ public String GetField(String field) { Dictionary<String, Object> map = fields; StringTokenizer tk = new StringTokenizer(field, "."); if (!tk.HasMoreTokens()) return null; while (true) { String s = tk.NextToken(); Object obj; map.TryGetValue(s, out obj); if (obj == null) return null; if (tk.HasMoreTokens()) { if (obj is Dictionary<String, Object>) map = (Dictionary<String, Object>)obj; else return null; } else { if (obj is Dictionary<String, Object>) return null; else { if (((PdfObject)obj).IsString()) return ((PdfString)obj).ToUnicodeString(); else return PdfName.DecodeName(obj.ToString()); } } } }
virtual public void DrawMultiLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury) { PdfAppearance tp = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly); PdfAppearance tp2 = (PdfAppearance)tp.Duplicate; tp2.SetFontAndSize(font, fontSize); tp2.ResetRGBColorFill(); field.DefaultAppearanceString = tp2; tp.DrawTextField(0f, 0f, urx - llx, ury - lly); tp.BeginVariableText(); tp.SaveState(); tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f); tp.Clip(); tp.NewPath(); tp.BeginText(); tp.SetFontAndSize(font, fontSize); tp.ResetRGBColorFill(); tp.SetTextMatrix(4, 5); System.util.StringTokenizer tokenizer = new System.util.StringTokenizer(text, "\n"); float yPos = ury - lly; while (tokenizer.HasMoreTokens()) { yPos -= fontSize * 1.2f; tp.ShowTextAligned(PdfContentByte.ALIGN_LEFT, tokenizer.NextToken(), 3, yPos, 0); } tp.EndText(); tp.RestoreState(); tp.EndVariableText(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
public Font GetFont(ChainedProperties props) { String face = props[ElementTags.FACE]; // try again, under the CSS key. //ISSUE: If both are present, we always go with face, even if font-family was // defined more recently in our ChainedProperties. One solution would go like this: // Map all our supported style attributes to the 'normal' tag name, so we could // look everything up under that one tag, retrieving the most current value. if (face == null || face.Trim().Length == 0) { face = props[Markup.CSS_KEY_FONTFAMILY]; } if (face != null) { StringTokenizer tok = new StringTokenizer(face, ","); while (tok.HasMoreTokens()) { face = tok.NextToken().Trim(); if (face.StartsWith("\"")) face = face.Substring(1); if (face.EndsWith("\"")) face = face.Substring(0, face.Length - 1); if (fontImp.IsRegistered(face)) break; } } int style = 0; String textDec = props[Markup.CSS_KEY_TEXTDECORATION]; if (textDec != null && textDec.Trim().Length != 0) { if (Markup.CSS_VALUE_UNDERLINE.Equals(textDec)) { style |= Font.UNDERLINE; } else if (Markup.CSS_VALUE_LINETHROUGH.Equals(textDec)) { style |= Font.STRIKETHRU; } } if (props.HasProperty(HtmlTags.I)) style |= Font.ITALIC; if (props.HasProperty(HtmlTags.B)) style |= Font.BOLD; if (props.HasProperty(HtmlTags.U)) style |= Font.UNDERLINE; if (props.HasProperty(HtmlTags.S)) style |= Font.STRIKETHRU ; String value = props[ElementTags.SIZE]; float size = 12; if (value != null) size = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); BaseColor color = Markup.DecodeColor(props["color"]); String encoding = props["encoding"]; if (encoding == null) encoding = BaseFont.WINANSI; return fontImp.GetFont(face, encoding, true, size, style, color); }
/** * Creates a Font object based on a chain of properties. * @param chain chain of properties * @return an iText Font object */ public Font GetFont(ChainedProperties chain) { // [1] font name String face = chain[HtmlTags.FACE]; // try again, under the CSS key. //ISSUE: If both are present, we always go with face, even if font-family was // defined more recently in our ChainedProperties. One solution would go like this: // Map all our supported style attributes to the 'normal' tag name, so we could // look everything up under that one tag, retrieving the most current value. if (face == null || face.Trim().Length == 0) { face = chain[HtmlTags.FONTFAMILY]; } // if the font consists of a comma separated list, // take the first font that is registered if (face != null) { StringTokenizer tok = new StringTokenizer(face, ","); while (tok.HasMoreTokens()) { face = tok.NextToken().Trim(); if (face.StartsWith("\"")) face = face.Substring(1); if (face.EndsWith("\"")) face = face.Substring(0, face.Length - 1); if (provider.IsRegistered(face)) break; } } // [2] encoding String encoding = chain[HtmlTags.ENCODING]; if (encoding == null) encoding = BaseFont.WINANSI; // [3] embedded // [4] font size String value = chain[HtmlTags.SIZE]; float size = 12; if (value != null) size = float.Parse(value, CultureInfo.InvariantCulture); // [5] font style int style = 0; // text-decoration String decoration = chain[HtmlTags.TEXTDECORATION]; if (decoration != null && decoration.Trim().Length != 0) { if (HtmlTags.UNDERLINE.Equals(decoration)) { style |= Font.UNDERLINE; } else if (HtmlTags.LINETHROUGH.Equals(decoration)) { style |= Font.STRIKETHRU; } } // italic if (chain.HasProperty(HtmlTags.I)) style |= Font.ITALIC; // bold if (chain.HasProperty(HtmlTags.B)) style |= Font.BOLD; // underline if (chain.HasProperty(HtmlTags.U)) style |= Font.UNDERLINE; // strikethru if (chain.HasProperty(HtmlTags.S)) style |= Font.STRIKETHRU; // [6] Color BaseColor color = HtmlUtilities.DecodeColor(chain[HtmlTags.COLOR]); // Get the font object from the provider return provider.GetFont(face, encoding, true, size, style, color); }
/** Reads the font metrics * @param rf the AFM file * @throws DocumentException the AFM file is invalid * @throws IOException the AFM file could not be read */ virtual public void Process(RandomAccessFileOrArray rf) { string line; bool isMetrics = false; while ((line = rf.ReadLine()) != null) { StringTokenizer tok = new StringTokenizer(line, " ,\n\r\t\f"); if (!tok.HasMoreTokens()) continue; string ident = tok.NextToken(); if (ident.Equals("FontName")) FontName = tok.NextToken("\u00ff").Substring(1); else if (ident.Equals("FullName")) FullName = tok.NextToken("\u00ff").Substring(1); else if (ident.Equals("FamilyName")) FamilyName = tok.NextToken("\u00ff").Substring(1); else if (ident.Equals("Weight")) Weight = tok.NextToken("\u00ff").Substring(1); else if (ident.Equals("ItalicAngle")) ItalicAngle = float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("IsFixedPitch")) IsFixedPitch = tok.NextToken().Equals("true"); else if (ident.Equals("CharacterSet")) CharacterSet = tok.NextToken("\u00ff").Substring(1); else if (ident.Equals("FontBBox")) { llx = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); lly = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); urx = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); ury = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); } else if (ident.Equals("UnderlinePosition")) UnderlinePosition = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("UnderlineThickness")) UnderlineThickness = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("EncodingScheme")) EncodingScheme = tok.NextToken("\u00ff").Substring(1); else if (ident.Equals("CapHeight")) CapHeight = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("XHeight")) XHeight = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("Ascender")) Ascender = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("Descender")) Descender = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("StdHW")) StdHW = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("StdVW")) StdVW = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("StartCharMetrics")) { isMetrics = true; break; } } if (!isMetrics) throw new DocumentException(MessageLocalization.GetComposedMessage("missing.startcharmetrics.in.1", fileName)); while ((line = rf.ReadLine()) != null) { StringTokenizer tok = new StringTokenizer(line); if (!tok.HasMoreTokens()) continue; string ident = tok.NextToken(); if (ident.Equals("EndCharMetrics")) { isMetrics = false; break; } int C = -1; int WX = 250; string N = ""; int[] B = null; tok = new StringTokenizer(line, ";"); while (tok.HasMoreTokens()) { StringTokenizer tokc = new StringTokenizer(tok.NextToken()); if (!tokc.HasMoreTokens()) continue; ident = tokc.NextToken(); if (ident.Equals("C")) C = int.Parse(tokc.NextToken()); else if (ident.Equals("WX")) WX = (int)float.Parse(tokc.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); else if (ident.Equals("N")) N = tokc.NextToken(); else if (ident.Equals("B")) { B = new int[]{int.Parse(tokc.NextToken()), int.Parse(tokc.NextToken()), int.Parse(tokc.NextToken()), int.Parse(tokc.NextToken())}; } } Object[] metrics = new Object[]{C, WX, N, B}; if (C >= 0) CharMetrics[C] = metrics; CharMetrics[N] = metrics; } if (isMetrics) throw new DocumentException(MessageLocalization.GetComposedMessage("missing.endcharmetrics.in.1", fileName)); if (!CharMetrics.ContainsKey("nonbreakingspace")) { Object[] space; CharMetrics.TryGetValue("space", out space); if (space != null) CharMetrics["nonbreakingspace"] = space; } while ((line = rf.ReadLine()) != null) { StringTokenizer tok = new StringTokenizer(line); if (!tok.HasMoreTokens()) continue; string ident = tok.NextToken(); if (ident.Equals("EndFontMetrics")) return; if (ident.Equals("StartKernPairs")) { isMetrics = true; break; } } if (!isMetrics) throw new DocumentException(MessageLocalization.GetComposedMessage("missing.endfontmetrics.in.1", fileName)); while ((line = rf.ReadLine()) != null) { StringTokenizer tok = new StringTokenizer(line); if (!tok.HasMoreTokens()) continue; string ident = tok.NextToken(); if (ident.Equals("KPX")) { string first = tok.NextToken(); string second = tok.NextToken(); int width = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo); Object[] relates; KernPairs.TryGetValue(first, out relates); if (relates == null) KernPairs[first] = new Object[]{second, width}; else { int n = relates.Length; Object[] relates2 = new Object[n + 2]; Array.Copy(relates, 0, relates2, 0, n); relates2[n] = second; relates2[n + 1] = width; KernPairs[first] = relates2; } } else if (ident.Equals("EndKernPairs")) { isMetrics = false; break; } } if (isMetrics) throw new DocumentException(MessageLocalization.GetComposedMessage("missing.endkernpairs.in.1", fileName)); rf.Close(); }