public Label(Vector2 position, string text, TrueTypeFont font = null, int fontSize = 12) : base(position, Vector2.One) { Text = text; Color = Color.White; Font = font ?? new TrueTypeFont(UiContentLoader.Instance.DefaultFontPath, fontSize); Font.Size = fontSize; }
protected override void RenderContent() { base.RenderContent(); //System.Diagnostics.Debug.WriteLine("Cursor Col: {0}, cursor Row: {1}",_cursorColumn, _cursorRow); TrueTypeFont font = Renderer.GetFont(); int lineHeight = font.GetLineHeight(); Vector3 textPos = new Vector3(); for (int i = 0; i < _lines.Count; i++) { string text = _lines[i]; Color4 colour = Color4.Black; textPos.Y = lineHeight - font.GetTextHeight(text) + (i * lineHeight); Renderer.PrintText(text, ref textPos, ref colour, ref colour); } if (_active) { try { int cursorX = font.GetTextWidth(_lines[_cursorRow].Substring(0, _cursorColumn)) + 1; Vector3 position = new Vector3(cursorX, (_cursorRow * lineHeight) + 1, 0); Vector3 scale = new Vector3(2, lineHeight, 1); Color4 colour = Color4.Black; Renderer.FillShape(ShapeFactory.Rectangle, ref position, ref scale, ref colour); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("cursor X error in TextBox.cs render.\n" + e.Message); _cursorColumn--; // emergency fix } } }
public override void Process(TrueTypeFont font) { var reader = new TrueTypeReader(Data); var version = reader.ReadUShort(); var length = reader.ReadUShort(); var coverage = reader.ReadUShort(); NumberOfPairs = reader.ReadUShort(); SearchRange = reader.ReadUShort(); EntrySelector = reader.ReadUShort(); RangeShift = reader.ReadUShort(); Kernpairs = new Dictionary <uint, short>(NumberOfPairs); for (var i = 0; i < NumberOfPairs; i++) { var glyphPair = reader.ReadULong(); var kernUnits = reader.ReadFWord(); Kernpairs[glyphPair] = kernUnits; } reader.Dispose(); }
public VisualPatternGridController(IImageProvider imageProvider, TrueTypeFont symbolsFont, IndexedImage whiteCrosses, Size imageBoxSize = default(Size)) : base(imageProvider, imageBoxSize) { WhiteCrosses = whiteCrosses; SymbolsFont = symbolsFont; imageProvider.Image.PixelChanged += Image_PixelChanged; }
public int DrawGlyph(int x, int y, float size, Glyph g, TrueTypeFont f) { var scale = size / f.UnitsPerEm; var gr = panel1.CreateGraphics(); gr.SmoothingMode = SmoothingMode.AntiAlias; gr.TranslateTransform(x, y + ((f.YMax - f.YMin) * scale)); gr.ScaleTransform(scale, -scale); var triangles = TrianglateGlyph(g); var debugColors = new [] { Brushes.Red, Brushes.Blue }; var cgg = new CGLFGlyph(); cgg.YMin = g.YMin; cgg.YMax = g.YMax; cgg.XMin = g.XMin; cgg.XMax = g.XMax; for (var i = 0; i < triangles.Count; i++) { var triangle = triangles[i]; cgg.Triangles.Add(triangle); gr.FillPolygon(checkBox1.Checked ? debugColors[i % 2] : Brushes.Black, triangle.ToArray()); } Cglf.AddGlyph(cgg); return((int)(g.XMax * scale)); }
public override void Process(TrueTypeFont font) { this.font = font; // noop // This table is merely a data holder to access the raw data according to loca lookup }
public void DrawFontFile(string name) { var f = new TrueTypeFont(File.ReadAllBytes(name)); Cglf.UnitsPerEm = f.UnitsPerEm; Cglf.YMax = f.YMax; Cglf.YMin = f.YMin; Cglf.XMin = f.XMin; Cglf.XMax = f.XMax; var x = 0; var y = 0; var c = 0; var size = Convert.ToDouble(textBox1.Text); panel1.Height = 70 * f.Lenght; for (int i = 7; i < f.Lenght; i++) { x += DrawGlyph(x, y, (float)size, f.ReadGlyph(i), f) + 5; if (c > (panel1.Width / (int)(f.XMax * (size / f.UnitsPerEm)))) { c = 0; x = 0; y += (int)(f.YMax * (size / f.UnitsPerEm)) + 5; } c++; } }
protected override void LoadContent() { new UiContentLoader(Content).LoadUiContent(); testTexture = Content.Load <Texture>("small.jpg"); labelFont = Content.Load <TrueTypeFont>("ARIAL.TTF"); }
public static void Draw(Graphics gr) { var font = new TrueTypeFont(Karla.KarlaRegularTtf); float scale = 64f / font.UnitsPerEm; var y = 5; var x = 5; int c = 0; for (int i = 6; i < (font.GlyphCount() - 6) / 2; i++) { var g = font.ReadGlyph(i); font.DrawGlyph(gr, i, scale, -scale, x, (int)(y + ((font.YMax - font.YMin) * scale))); x += (int)(scale * g.XMax) + 5; if (c > 10) { y += (int)(scale * g.YMax) + 40; c = 0; x = 5; } c++; } }
public virtual void TestOtherLinesAddition() { byte[] ttf = StreamUtil.InputStreamToArray(new FileStream(iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext .CurrentContext.TestDirectory) + "/resources/itext/io/font/otf/FreeSans.ttf", FileMode.Open, FileAccess.Read )); TrueTypeFont font = new TrueTypeFont(ttf); GlyphLine containerLine = new GlyphLine(ConstructGlyphListFromString("Viva France!", font)); GlyphLine childLine1 = new GlyphLine(ConstructGlyphListFromString(" Liberte", font)); containerLine.Add(childLine1); NUnit.Framework.Assert.AreEqual(containerLine.end, 12); containerLine.end = 20; GlyphLine childLine2 = new GlyphLine(ConstructGlyphListFromString(" Egalite", font)); containerLine.Add(childLine2); NUnit.Framework.Assert.AreEqual(containerLine.end, 20); containerLine.start = 10; GlyphLine childLine3 = new GlyphLine(ConstructGlyphListFromString(" Fraternite", font)); containerLine.Add(childLine3); NUnit.Framework.Assert.AreEqual(containerLine.start, 10); containerLine.start = 0; containerLine.Add(ConstructGlyphListFromString("!", font)[0]); containerLine.end = 40; NUnit.Framework.Assert.AreEqual(containerLine.glyphs.Count, 40); }
/// <summary> /// Load information about all fonts found in the cache's directories. /// </summary> public void Load() { _fonts = new List <Font>(); _fontFiles = new List <FontInfo>(); // Open all *.ttf and *.afm files in each of our font directories // and test whether it's a font file. If it is a font file then read // it and get its info. This doesn't load the font, it just // reads some metadata. foreach (string dir in _fontDirectories) { // True Type fonts string[] filenames = Directory.GetFiles(dir, "*.ttf"); foreach (string filename in filenames) { FontInfo info = TrueTypeFont.GetFontInfo(filename); _fontFiles.Add(info); } // Type 1 fonts filenames = Directory.GetFiles(dir, "*.afm"); foreach (string filename in filenames) { FontInfo info = Type1Font.GetFontInfo(filename); _fontFiles.Add(info); } } }
protected override void RenderContent() { base.RenderContent(); TrueTypeFont font = Renderer.GetFont(); Color4 textColour = Color4.Black; int cY = (GetContentHeight() / 2) - (font.GetTextHeight(_text) / 2); Vector3 textPos = new Vector3(); textPos.X = _offsetX + 1; textPos.Y = cY; Renderer.PrintText(_text, ref textPos, ref textColour, ref textColour); int cursorX = font.GetTextWidth(_text.Substring(0, _cursorPos)); cursorX += _offsetX; if (_active) { Vector3 position = new Vector3(cursorX, 1, 0); Vector3 scale = new Vector3(2, GetContentHeight() - 2, 1); Renderer.FillShape(ShapeFactory.Rectangle, ref position, ref scale, ref textColour); } }
protected internal virtual void AddRangeUni(TrueTypeFont ttf, IDictionary <int, int[]> longTag, bool includeMetrics ) { if (!subset && (subsetRanges != null || ttf.GetDirectoryOffset() > 0)) { int[] rg = subsetRanges == null && ttf.GetDirectoryOffset() > 0 ? new int[] { 0, 0xffff } : CompactRanges( subsetRanges); IDictionary <int, int[]> usemap = ttf.GetActiveCmap(); System.Diagnostics.Debug.Assert(usemap != null); foreach (KeyValuePair <int, int[]> e in usemap) { int[] v = e.Value; int gi = v[0]; if (longTag.ContainsKey(v[0])) { continue; } int c = e.Key; bool skip = true; for (int k = 0; k < rg.Length; k += 2) { if (c >= rg[k] && c <= rg[k + 1]) { skip = false; break; } } if (!skip) { longTag[gi] = includeMetrics ? new int[] { v[0], v[1], c } : null; } } } }
public Editor(TrueTypeFont font) { Font = font; Buffer = new EditorBuffer(); Options = new EditorOptions { HighlightCurrentLine = true, TabSize = 2 }; Hotkeys = new List <Hotkey>(); TextRenderer = new TextRenderer(this); Window = new Window(this); ModeLine = new ModeLine(Font); Cursor = new Cursor { ForceVisible = true }; _ = new FileSystem(this); _ = new Modification(this); _ = new Navigation(this); _ = new StatusControl(this); }
private void ProcessSubtables(TrueTypeFont font) { foreach (var subtable in Subtables) { subtable.Process(font); } }
public CheckBox(Vector2 position) : base(position, Vector2.One) { Checked = false; Text = "CheckBox"; Font = new TrueTypeFont(UiContentLoader.Instance.DefaultFontPath, 13); CheckmarkTexture = new Texture(UiContentLoader.Instance.CheckmarkTexturePath); }
public virtual void TestOtherLinesWithActualTextAddition02() { byte[] ttf = StreamUtil.InputStreamToArray(new FileStream(iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext .CurrentContext.TestDirectory) + "/resources/itext/io/font/otf/FreeSans.ttf", FileMode.Open, FileAccess.Read )); TrueTypeFont font = new TrueTypeFont(ttf); GlyphLine containerLine = new GlyphLine(ConstructGlyphListFromString("France", font)); containerLine.SetActualText(1, 5, "id"); GlyphLine childLine = new GlyphLine(ConstructGlyphListFromString("---Liberte", font)); childLine.SetActualText(3, 10, "Viva"); containerLine.Add(childLine); containerLine.end = 16; NUnit.Framework.Assert.IsNull(containerLine.actualText[0]); for (int i = 1; i < 5; i++) { NUnit.Framework.Assert.AreEqual("id", containerLine.actualText[i].value); } for (int i = 5; i < 9; i++) { NUnit.Framework.Assert.IsNull(containerLine.actualText[i]); } for (int i = 9; i < 16; i++) { NUnit.Framework.Assert.AreEqual("Viva", containerLine.actualText[i].value); } NUnit.Framework.Assert.AreEqual("Fide---Viva", containerLine.ToString()); }
public override void Process(TrueTypeFont font) { var reader = new TrueTypeReader(Data); Version = reader.ReadUShort(); NumberOfTables = reader.ReadUShort(); Subtables = new List <KerningSubtableBase>(NumberOfTables); for (var t = 0; t < NumberOfTables; t++) { var subtableOffset = reader.Position; var version = reader.ReadUShort(); var length = reader.ReadUShort(); var coverage = reader.ReadUShort(); var subtableType = ImplementationTypeAttribute.GetTypeFromEnumValue(typeof(KerningSubtableType), GetSubtypeFromCoverage(coverage)); var subtable = Activator.CreateInstance(subtableType) as KerningSubtableBase; subtable.ReadData(reader, (uint)subtableOffset, length); Subtables.Add(subtable); } ProcessSubtables(font); reader.Dispose(); }
/// <summary> /// Returns the glyph index for the given character, or 0 if the character is not supported by this font /// </summary> public static uint GetGlyphIndex(char c, TrueTypeFont font) { uint glyphIndex = 0; // Prefer Windows platform UCS2 glyphs as they are the recommended default on the Windows platform var preferred = font.CmapTable.EncodingRecords.FirstOrDefault( e => e.PlatformId == Platform.Windows && e.WindowsEncodingId == WindowsEncoding.UnicodeUCS2); if (preferred != null) { glyphIndex = preferred.Subtable.GetGlyphIndex(c); } if (glyphIndex != 0) { return(glyphIndex); } // Fall back to using any table to find the match foreach (var record in font.CmapTable.EncodingRecords) { glyphIndex = record.Subtable.GetGlyphIndex(c); if (glyphIndex != 0) { return(glyphIndex); } } return(0); }
internal PdfType0Font(TrueTypeFont ttf, String cmap) : base() { if (!cmap.Equals(PdfEncodings.IDENTITY_H) && !cmap.Equals(PdfEncodings.IDENTITY_V)) { throw new PdfException("only.identity.cmaps.supports.with.truetype"); } if (!ttf.GetFontNames().AllowEmbedding()) { throw new PdfException("1.cannot.be.embedded.due.to.licensing.restrictions").SetMessageParams(ttf.GetFontNames ().GetFontName() + ttf.GetFontNames().GetStyle()); } this.fontProgram = ttf; this.embedded = true; vertical = cmap.EndsWith("V"); cmapEncoding = new CMapEncoding(cmap); longTag = new LinkedDictionary <int, int[]>(); cidFontType = CID_FONT_TYPE_2; if (ttf.IsFontSpecific()) { specificUnicodeDifferences = new char[256]; byte[] bytes = new byte[1]; for (int k = 0; k < 256; ++k) { bytes[0] = (byte)k; String s = PdfEncodings.ConvertToString(bytes, null); char ch = s.Length > 0 ? s[0] : '?'; specificUnicodeDifferences[k] = ch; } } }
public void OnTextTrigger() { if (!ContentSelectable()) { return; } if (!_active) { _active = true; } TrueTypeFont font = Renderer.GetFont(); Vector2 mouse = StateWindow.Instance.GetMousePosition(); int relativeY = (int)(mouse.Y - GetWorldContentPosition().Y) - _verticalBar.GetRelativeScroll(); relativeY /= font.GetLineHeight(); _cursorRow = Math.Min(relativeY, _lines.Count - 1); int relativeX = (int)(mouse.X - GetWorldContentPosition().X); string line = _lines[_cursorRow]; _cursorColumn = 0; for (int i = 0; i < line.Length; i++) { if (relativeX > font.GetTextWidth(line.Substring(0, i + 1))) { _cursorColumn++; } else { break; } } _cursorColumn = Math.Min(line.Length, _cursorColumn); }
internal PdfType0Font(TrueTypeFont ttf, String cmap) : base() { if (!PdfEncodings.IDENTITY_H.Equals(cmap) && !PdfEncodings.IDENTITY_V.Equals(cmap)) { throw new PdfException(PdfException.OnlyIdentityCMapsSupportsWithTrueType); } if (!ttf.GetFontNames().AllowEmbedding()) { throw new PdfException(PdfException.CannotBeEmbeddedDueToLicensingRestrictions).SetMessageParams(ttf.GetFontNames ().GetFontName() + ttf.GetFontNames().GetStyle()); } this.fontProgram = ttf; this.embedded = true; vertical = cmap.EndsWith("V"); cmapEncoding = new CMapEncoding(cmap); longTag = new SortedSet <int>(); cidFontType = CID_FONT_TYPE_2; if (ttf.IsFontSpecific()) { specificUnicodeDifferences = new char[256]; byte[] bytes = new byte[1]; for (int k = 0; k < 256; ++k) { bytes[0] = (byte)k; String s = PdfEncodings.ConvertToString(bytes, null); char ch = s.Length > 0 ? s[0] : '?'; specificUnicodeDifferences[k] = ch; } } }
public void SetFont(TrueTypeFont fontToShow) { _font = fontToShow; tableList.DataSource = _font.ListTablesKeys().Select(MapTableDescription).ToList(); //tableProperties.SelectedObject = fontToShow.ReadGlyph('$'); }
public void ShouldGetName() { var font = TrueTypeFont.FromFile(@"C:\Windows\Fonts\arial.ttf"); var name = NameHelper.GetName(NameId.FontSubfamilyName, new CultureInfo("nl-NL"), font); Assert.Equal("Standaard", name); }
public void ShouldGetGlyph() { var font = TrueTypeFont.FromFile(@"C:\Windows\Fonts\arial.ttf"); var glyphIndex = GlyphHelper.GetGlyphIndex('A', font); Assert.NotEqual((uint)0, glyphIndex); }
public override void Process(TrueTypeFont font) { var reader = new TrueTypeReader(Data); Version = reader.ReadFixed(); NumberOfGlyphs = reader.ReadUShort(); reader.Dispose(); }
public PortableFont(Font fontSpec, float dpi) { _fontSpec = fontSpec; _dpi = dpi; EnsureFontCache(); BaseTrueTypeFont = new TrueTypeFont(GetFontFilePath(fontSpec)); }
public TrueTypeStandard14FallbackSimpleFont(NameToken name, AdobeFontMetrics fontMetrics, Encoding encoding, TrueTypeFont font, MetricOverrides overrides) { this.fontMetrics = fontMetrics; this.encoding = encoding ?? throw new ArgumentNullException(nameof(encoding)); this.font = font; this.overrides = overrides; Name = name; }
private static GlyphRecord[] GetGlyphRecordsInFont(TrueTypeFont font, TrueTypeDataBytes data) { var indexToLocationTable = font.TableRegister.IndexToLocationTable; var numGlyphs = indexToLocationTable.GlyphOffsets.Count - 1; var glyphDirectory = font.TableRegister.GlyphTable.DirectoryTable; data.Seek(glyphDirectory.Offset); var glyphRecords = new GlyphRecord[numGlyphs]; for (var i = 0; i < numGlyphs; i++) { var glyphOffset = (int)(glyphDirectory.Offset + indexToLocationTable.GlyphOffsets[i]); if (indexToLocationTable.GlyphOffsets[i + 1] <= indexToLocationTable.GlyphOffsets[i]) { glyphRecords[i] = new GlyphRecord(glyphOffset); continue; } data.Seek(glyphOffset); if (glyphOffset >= glyphDirectory.Offset + glyphDirectory.Length) { throw new InvalidOperationException($"Failed to read expected number of glyphs {numGlyphs}, only got to index {i} before reaching end of input."); } var numberOfContours = data.ReadSignedShort(); var type = numberOfContours >= 0 ? GlyphType.Simple : GlyphType.Composite; // Read bounds. data.ReadSignedShort(); data.ReadSignedShort(); data.ReadSignedShort(); data.ReadSignedShort(); if (type == GlyphType.Simple) { ReadSimpleGlyph(data, numberOfContours); glyphRecords[i] = new GlyphRecord(glyphOffset, type, (int)(data.Position - glyphOffset)); } else { var glyphIndices = ReadCompositeGlyph(data); // Skip hinting instructions but include them in the output. var next = indexToLocationTable.GlyphOffsets[i + 1]; data.Seek(glyphDirectory.Offset + next - 1); glyphRecords[i] = new GlyphRecord(glyphOffset, type, (int)(data.Position - glyphOffset), glyphIndices); } } return(glyphRecords); }
public virtual void UnsupportedCmapTest() { NUnit.Framework.Assert.That(() => { TrueTypeFont ttf = new TrueTypeFont(sourceFolder + "NotoSerif-Regular_v1.7.ttf"); PdfType0Font type0Font = new PdfType0Font(ttf, PdfEncodings.WINANSI); } , NUnit.Framework.Throws.InstanceOf <PdfException>().With.Message.EqualTo(PdfException.OnlyIdentityCMapsSupportsWithTrueType)) ; }
static void Main(string[] args) { var font = new TrueTypeFont(@"Anonymous\Roboto-medium.ttf"); // Render some characters... for (uint ch = 32; ch <= 0x7f; ch++) { int width, height, xOffset, yOffset; float scale = font.GetScaleForPixelHeight(80); byte[] data = font.GetCodepointBitmap(ch, scale, scale, out width, out height, out xOffset, out yOffset); SaveBitmap(data, 0, 0, width, height, width, "Char-" + ch.ToString() + ".png"); } // Let's try baking. Tasty tasty. BakedCharCollection characters; float pixelHeight = 18; var bitmap = font.BakeFontBitmap(pixelHeight, out characters, true); SaveBitmap(bitmap.Buffer, 0, 0, bitmap.Width, bitmap.Height, bitmap.Width, "BakeResult1.png"); // Now, let's give serialization a go. using (var file = File.OpenWrite("BakeResult2.temp")) { var bitmapSaver = new BinaryFormatter(); bitmapSaver.Serialize(file, bitmap); bitmapSaver.Serialize(file, characters); int ascent, descent, lineGap; float scale = font.GetScaleForPixelHeight(pixelHeight); font.GetFontVMetrics(out ascent, out descent, out lineGap); bitmapSaver.Serialize(file, (float)ascent * scale); bitmapSaver.Serialize(file, (float)descent * scale); bitmapSaver.Serialize(file, (float)lineGap * scale); } using (var file = File.OpenRead("BakeResult2.temp")) { var bitmapLoader = new BinaryFormatter(); var bitmapAgain = (FontBitmap)bitmapLoader.Deserialize(file); var charactersAgain = (BakedCharCollection)bitmapLoader.Deserialize(file); SaveBitmap(bitmapAgain.Buffer, 0, 0, bitmapAgain.Width, bitmapAgain.Height, bitmap.Width, "BakeResult2.png"); for (char ch = 'A'; ch <= 'Z'; ch++) { BakedChar bakedChar = charactersAgain[ch]; if (bakedChar.IsEmpty) { continue; } SaveBitmap(bitmapAgain.Buffer, bakedChar.X0, bakedChar.Y0, bakedChar.X1, bakedChar.Y1, bitmapAgain.Stride, "SmallChar-" + ch.ToString() + ".png"); } } }