public static PDFColumnWidths Parse(string value) { if (string.IsNullOrEmpty(value)) { return(PDFColumnWidths.Empty); } string[] all = value.Split(_splitChars, StringSplitOptions.RemoveEmptyEntries); if (all.Length == 1) { //check to see if it is an explicit width for all columns (e.g. 200pt) var expl = all[0]; if (char.IsLetter(expl, expl.Length - 1)) { PDFUnit val; if (PDFUnit.TryParse(expl, out val)) { return(new PDFColumnWidths(val)); } else { throw new ArgumentException("The value '" + value + "' could not be converted to column widths. Either use an explicit width (e.g. 200pt) or a set of percentage widths (e.g. 30% 40% 30%, or 0.3 0.4 0.3) ", "value"); } } } double[] parsed = new double[all.Length]; double sum = 0; for (var i = 0; i < all.Length; i++) { double one; if (all[i] == "*") { one = UndefinedWidth; } else if (all[i].EndsWith("%")) { one = double.Parse(all[i].Substring(0, all[i].Length - 1)); one = one / 100.0; } else { one = double.Parse(all[i]); } sum += one; if (sum > 1.0) { throw new ArgumentOutOfRangeException("value", "The widths of the columns is a percentage number that must not exceed 1."); } parsed[i] = one; } return(new PDFColumnWidths(parsed)); }
/// <summary> /// Parses a string value in the format [T L B R] or [All] into a PDFThickness instance /// </summary> /// <param name="value">The string to parse</param> /// <returns>A new PDFSize instance</returns> /// <exception cref="ArgumentNullException" /> /// <exception cref="ArgumentException" /> public static PDFThickness Parse(string value) { if (String.IsNullOrEmpty(value)) { throw new ArgumentNullException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFThickness", "[T L B R], [TB RL] OR [All]")); } if (value.StartsWith(ThicknessStartChar.ToString()) || value.EndsWith(ThicknessEndChar.ToString())) { value = value.Substring(1, value.Length - 2); } else if (ThicknessStartAndEndRequired) { throw new ArgumentNullException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFThickness", "[T R B L], [TB RL] OR [All]")); } PDFUnit t, l, b, r; string[] thick = value.Split(ThicknessSeparatorChar); if (thick.Length == 1) { if (PDFUnit.TryParse(thick[0], out t) == false) { throw new ArgumentException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFThickness", "[T R B L], [TB RL] OR [All]")); } else { l = b = r = t; } } else if (thick.Length == 2) { if (PDFUnit.TryParse(thick[0], out t) == false || PDFUnit.TryParse(thick[1], out r) == false) { throw new ArgumentException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFThickness", "[T R B L], [TB RL] OR [All]")); } b = t; l = r; } else if (thick.Length != 4) { throw new ArgumentException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFThickness", "[T R B L], [TB RL] OR [All]")); } else { if (PDFUnit.TryParse(thick[0], out t) == false || PDFUnit.TryParse(thick[1], out r) == false || PDFUnit.TryParse(thick[2], out b) == false || PDFUnit.TryParse(thick[3], out l) == false) { throw new ArgumentException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFThickness", "[T R B L], [TB RL] OR [All]")); } } return(new PDFThickness(t, r, b, l)); }
/// <summary> /// Parses a string value in the format [T,L,W,H] or [All] into a PDFRect instance /// </summary> /// <param name="value">The string to parse</param> /// <returns>A new PDFRect instance</returns> /// <exception cref="ArgumentNullException" /> /// <exception cref="ArgumentException" /> public static PDFRect Parse(string value) { if (String.IsNullOrEmpty(value)) { throw new ArgumentNullException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFRect", "[T L W H] OR [All]")); } if (value.StartsWith(RectangleStartChar.ToString()) || value.EndsWith(RectangleEndChar.ToString())) { value = value.Substring(1, value.Length - 2); } else if (RectangleStartAndEndCharRequired) { throw new ArgumentNullException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFRect", "[T L W H] OR [All]")); } PDFUnit t, l, w, h; string[] rect = value.Split(RectangleSeparatorChar); if (rect.Length == 1) { if (PDFUnit.TryParse(rect[0], out t) == false) { throw new ArgumentException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFRect", "[T L W H] OR [All]")); } else { l = w = h = t; } } else if (rect.Length != 4) { throw new ArgumentException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFRect", "[T L W H] OR [All]")); } else { if (PDFUnit.TryParse(rect[0], out t) == false || PDFUnit.TryParse(rect[1], out l) == false || PDFUnit.TryParse(rect[2], out w) == false || PDFUnit.TryParse(rect[3], out h) == false) { throw new ArgumentException("value", String.Format(Errors.CouldNotParseValue_3, value, "PDFRect", "[T L W H] OR [All]")); } } return(new PDFRect(t, l, w, h)); }
public static bool TryParseRadial(string value, out PDFGradientRadialDescriptor radial) { radial = null; string[] all = _splitter.Split(value); if (all.Length == 0) { return(false); } RadialShape shape = RadialShape.Circle; RadialSize size = RadialSize.FarthestCorner; PDFUnit? xpos = null; PDFUnit? ypos = null; int colorStopIndex = 0; if (all[0].StartsWith("circle")) { shape = RadialShape.Circle; all[0] = all[0].Substring("circle".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("ellipse")) { radial = null; return(false); //shape = RadialShape.Ellipse; //all[0] = all[0].Substring("ellipse".Length).TrimStart(); //colorStopIndex = 1; } if (all[0].StartsWith("closest-side")) { //TODO:Parse at percents size = RadialSize.ClosestSide; all[0] = all[0].Substring("closest-side".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("closest-corner")) { size = RadialSize.ClosestCorner; all[0] = all[0].Substring("closest-corner".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("farthest-side")) { size = RadialSize.FarthestSide; all[0] = all[0].Substring("farthest-side".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("farthest-corner")) { size = RadialSize.FarthestCorner; all[0] = all[0].Substring("farthest-corner".Length).TrimStart(); colorStopIndex = 1; } //TODO: Support relative radii positions e.g. 10% 40% at .... if (all[0].StartsWith("at")) { all[0] = all[0].Substring("at".Length).TrimStart().ToLower(); var parts = all[0].Split(' '); foreach (var part in parts) { if (string.IsNullOrWhiteSpace(part)) { continue; } var item = part.Trim(); switch (item) { case ("top"): ypos = 0; break; case ("left"): xpos = 0; break; case ("bottom"): ypos = Double.MaxValue; break; case ("right"): xpos = Double.MaxValue; break; default: PDFUnit found; if (PDFUnit.TryParse(item, out found)) { if (xpos.HasValue) { ypos = found; } else { xpos = found; } } break; } } colorStopIndex = 1; } PDFGradientColor[] colors = new PDFGradientColor[all.Length - colorStopIndex]; for (int i = 0; i < colors.Length; i++) { PDFGradientColor parsed; if (PDFGradientColor.TryParse(all[i + colorStopIndex], out parsed)) { colors[i] = parsed; } else { return(false); } } radial = new PDFGradientRadialDescriptor() { Repeating = false, Shape = shape, Size = size, XCentre = xpos, YCentre = ypos, Colors = new List <PDFGradientColor>(colors) }; return(true); }