public void SetTextSpacing(PDFUnit?wordSpacingOffset, PDFUnit?charSpacingOffset, PDFUnit fontSize) { if (charSpacingOffset.HasValue) { this.Writer.WriteOpCodeS(PDFOpCode.TxtCharSpacing, (PDFReal)charSpacingOffset.Value.PointsValue); } if (wordSpacingOffset.HasValue) { PDFFontDefinition defn = this.CurrentFontResource.Definition; //calculate the required width of a space in PDF Text units double spaceOffsetPDFUnits = ((wordSpacingOffset.Value.PointsValue) * (double)PDFFontDefinition.PDFGlyphUnits) / fontSize.PointsValue; //caclulate the normal width of a space in PDF Text units double spaceWidthPDFUnits = (defn.SpaceWidthFontUnits / defn.FontUnitsPerEm) * (double)PDFFontDefinition.PDFGlyphUnits; //calculate the actual space size (normal width + offset) in PDFTextUnits double spaceActualPDFUnits = spaceWidthPDFUnits + spaceOffsetPDFUnits; //calculate the factor (1 = normal size, 2 = double size, etc) double spaceFactor = spaceActualPDFUnits / spaceWidthPDFUnits; this.CustomWordSpace = spaceActualPDFUnits; this.Writer.WriteOpCodeS(PDFOpCode.TxtWordSpacing, (PDFReal)spaceFactor); } }
public virtual void SetResourceFont(string name, PDFFontDefinition definition) { this._familyName = name; this._cachedmetrics = definition.GetFontMetrics(this.Size); //var style = this.GetDrawingStyle(); //var sys = PDFFontFactory.GetSystemFont(this._familyName, this.GetDrawingStyle(), (float)this.Size.PointsValue); //int line = sys.FontFamily.GetLineSpacing(style); }
// // .ctor // #region private PDFFontResource(PDFFontDefinition defn, string resourceName) private PDFFontResource(PDFFontDefinition defn, PDFFontWidths widths, string resourceName) : base(PDFObjectTypes.FontResource) { if (null == defn) { throw new ArgumentNullException("defn"); } _defn = defn; _widths = widths; _fontName = defn.FullName; this.Name = (Native.PDFName)resourceName; }
// // static methods // #region public static PDFFontResource Load(PDFFontDefinition defn, string resourceName) /// <summary> /// Loads a PDFFontResource based on the definition and name /// </summary> /// <param name="defn"></param> /// <param name="resourceName"></param> /// <returns></returns> public static PDFFontResource Load(PDFFontDefinition defn, string resourceName) { if (null == defn) { throw new ArgumentNullException("defn"); } if (string.IsNullOrEmpty(resourceName)) { throw new ArgumentNullException("resourceName"); } PDFFontWidths widths = defn.GetWidths(); PDFFontResource rsrc = new PDFFontResource(defn, widths, resourceName); return(rsrc); }
private bool TryGetFont(IPDFDocument doc, PDFContextBase context, out PDFFontDefinition definition) { System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular; if (this.FontBold) { style |= System.Drawing.FontStyle.Bold; } if (this.FontItalic) { style |= System.Drawing.FontStyle.Italic; } string name = this.FontFamily.FamilyName; PDFFontFactory.TryEnsureFont(doc, context, this.Source, name, style, out definition); return(null != definition); }
public void SetTextSpacing(PDFUnit?wordSpacingOffset, PDFUnit?charSpacingOffset, PDFUnit fontSize) { if (charSpacingOffset.HasValue) { this.Writer.WriteOpCodeS(PDFOpCode.TxtCharSpacing, (PDFReal)charSpacingOffset.Value.PointsValue); } if (wordSpacingOffset.HasValue) { if (null == this.CurrentFontResource || null == this.CurrentFontResource.Definition) { this.Context.TraceLog.Add(TraceLevel.Warning, "Graphics", "Could not load the current font definition to set word spacing"); return; } else { PDFFontDefinition defn = this.CurrentFontResource.Definition; //calculate the required width of a space in PDF Text units double spaceOffsetPDFUnits = ((wordSpacingOffset.Value.PointsValue) * (double)PDFFontDefinition.PDFGlyphUnits) / fontSize.PointsValue; //caclulate the normal width of a space in PDF Text units double spaceWidthPDFUnits = (defn.SpaceWidthFontUnits / defn.FontUnitsPerEm) * (double)PDFFontDefinition.PDFGlyphUnits; //calculate the actual space size (normal width + offset) in PDFTextUnits double spaceActualPDFUnits = spaceWidthPDFUnits + spaceOffsetPDFUnits; //calculate the factor (1 = normal size, 2 = double size, etc) double spaceFactor = spaceActualPDFUnits / spaceWidthPDFUnits; this.CustomWordSpace = spaceActualPDFUnits; this.Writer.WriteOpCodeS(PDFOpCode.TxtWordSpacing, (PDFReal)spaceFactor); } } }
/// <summary> /// Measures a single line of text (no wrapping) and returns the size required, /// and the number of characters fitted into that line. Uses the current font and size. /// </summary> /// <param name="chars"></param> /// <param name="startIndex"></param> /// <param name="available"></param> /// <param name="options"></param> /// <param name="charsfitted"></param> /// <returns></returns> /// <remarks>Uses the TTFFontFile if available, otherwise will default to using the GDI+ measure, /// which is slower but supports postscript fonts</remarks> public PDFSize MeasureString(string chars, int startIndex, PDFSize available, PDFTextRenderOptions options, out int charsfitted) { PDFFontResource frsc = this.CurrentFontResource; PDFUnit fsize = this.CurrentFont.Size; if (null == frsc) { throw new InvalidOperationException("Current font has not be set on the graphics class, so strings cannot be measured"); } PDFFontDefinition defn = frsc.Definition; PDFSize measured; if (defn.CanMeasureStrings) { bool trimtoword = true; if (options.WrapText.HasValue) { if (options.WrapText == WordWrap.NoWrap) { available.Width = new PDFUnit(int.MaxValue, PageUnits.Points); } else if (options.WrapText == WordWrap.Character) { trimtoword = false; } } bool vertical = false; // Not currently supported. //inspect the spacing values - if any are set then we must use them in the calculations bool complexSpacing = false; double?wordSpace = null; double charSpace = Scryber.OpenType.TTFFile.NoCharacterSpace; double hScale = Scryber.OpenType.TTFFile.NoHorizontalScale; if (options.WordSpacing.HasValue) { complexSpacing = true; wordSpace = options.WordSpacing.Value.PointsValue; } if (options.CharacterSpacing.HasValue) { complexSpacing = true; charSpace = options.CharacterSpacing.Value.PointsValue; } if (options.CharacterHScale.HasValue) { complexSpacing = true; hScale = options.CharacterHScale.Value; } if (complexSpacing) { //TODO: Check if we can do this on the widths rather than the ttfile. measured = defn.MeasureStringWidth(chars, startIndex, fsize.PointsValue, available.Width.PointsValue, wordSpace, charSpace, hScale, vertical, trimtoword, out charsfitted); } else { //TODO: Check if we can do this on the widths rather than the ttfile. measured = defn.MeasureStringWidth(chars, startIndex, fsize.PointsValue, available.Width.PointsValue, trimtoword, out charsfitted); } if (charsfitted > 0) { this.RegisterStringUse(chars, startIndex, charsfitted); } } else { throw new NotSupportedException("The font definition cannot measure strings"); //if (startIndex > 0) // chars = chars.Substring(startIndex); //measured = this.MeasureGraphicsString(chars, available, options.WrapText.HasValue ? options.WrapText.Value : WordWrap.Auto, out charsfitted); } return(measured); }