TrueTypeHinting.vb
- ''
- '' This code is part of Document Solutions for Imaging demos.
- '' Copyright (c) MESCIUS inc. All rights reserved.
- ''
- Imports System.IO
- Imports System.Drawing
- Imports GrapeCity.Documents.Imaging
- Imports GrapeCity.Documents.Text
- Imports GrapeCity.Documents.Drawing
- Imports GCTEXT = GrapeCity.Documents.Text
- Imports GCDRAW = GrapeCity.Documents.Drawing
-
- '' This sample demonstrates how to use TrueType font hinting instructions.
- ''
- '' Many TrueType fonts include low-level hinting instructions.
- '' The original purpose of introducing hinting instructions was
- '' to improve the look of glyphs when the font size Is comparable
- '' to the device resolution. But such instructions are now also used
- '' (especially in CJK fonts) to reuse some glyph parts in different
- '' glyphs regardless of the font size.
- '' GcGraphics supports hinting instructions. To enable it, set
- '' TextFormat.EnableFontHinting property to true when rendering text.
- ''
- '' This sample renders a Latin text in a small size with hinting off And on,
- '' And then renders a CJK text also with hinting off And on.
- Public Class TrueTypeHinting
- Function GenerateImage(
- ByVal pixelSize As Size,
- ByVal dpi As Single,
- ByVal opaque As Boolean,
- Optional ByVal sampleParams As String() = Nothing) As GcBitmap
-
- Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
- Dim dy = dpi / 2
- Dim ip = New PointF(dpi, dpi)
- Using g = bmp.CreateGraphics(Color.White)
- '' Turning anti-aliasing off makes the hinting effect on small text more obvious:
- g.Renderer.Aliased = True
-
- Const sOff = "Hinting OFF: "
- Const sOn = "Hinting ON: "
-
- '' Draw a Latin string with hinting instructions off And on:
- Const sLat = "The quick brown fox jumps over the lazy dog."
-
- Dim tfLat = New TextFormat() With
- {
- .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arial.ttf")),
- .FontSize = 12,
- .FontName = "Arial",
- .EnableFontHinting = False
- }
- g.DrawString(sOff + sLat, tfLat, ip)
- ip.Y += dy
-
- tfLat.FontSize = 10
- g.DrawString(sOff + sLat, tfLat, ip)
- ip.Y += dy * 2
-
- tfLat.EnableFontHinting = True
- tfLat.FontSize = 12
- g.DrawString(sOn + sLat, tfLat, ip)
- ip.Y += dy
-
- tfLat.FontSize = 10
- g.DrawString(sOn + sLat, tfLat, ip)
- ip.Y += dy * 2
-
- '' Draw a CJK string with hinting instructions off And on
- '' "Best year in spring, best day in morning"
- Const sCJK = "一年之计在于春,一日之计在于晨"
-
- '' For CJK text, we can turn anti-aliasing on as the effect of
- '' hinting Is obvious in the characters' form itself:
- g.Renderer.Aliased = False
-
- Dim tfCJK = New TextFormat() With
- {
- .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "kaiu.ttf")),
- .FontSize = 30,
- .EnableFontHinting = False
- }
- g.DrawString(sOff + vbCrLf + sCJK, tfCJK, ip)
- ip.Y += dy * 3
-
- tfCJK.EnableFontHinting = True
- g.DrawString(sOn + vbCrLf + sCJK, tfCJK, ip)
- End Using
- '' Done
- Return bmp
- End Function
- End Class
-