unipdf/render/internal/context/text_font.go

140 lines
3.6 KiB
Go
Raw Normal View History

Add basic image rendering support (#266) * Add render package * Add text state * Add more text operators * Remove unnecessary files * Add text font * Add custom text render method * Improve text rendering method * Rename text state methods * Refactor and document context interface * Refact text begin/end operators * Fix graphics state transformations * Keep original font when doing font substitution * Take page cropbox into account * Revert to substitution font if original font measurement is 0 * Add font substitution package * Implement addition transform.Point methods * Use transform.Point in the image context package * Remove unneeded functionality from the render image package * Fix golint notices in the image rendering package * Fix go vet notices in the render package * Fix golint notices in the top-level render package * Improve render context package documentation * Document context text state struct. * Document context text font struct. * Minor logging improvements * Add license disclaimer to the render package files * Avoid using package aliases where possible * Change style of section comments * Adapt render package import style to follow the developer guide * Improve documentation for the internal matrix implementation * Update render package dependency versions * Apply crop box post render * Account for offseted media boxes * Improve metrics of rendered characters * Fix text matrix translation * Change priority of fonts used for measuring rendered characters * Skip invalid m and l operators on image rendering * Small fix for v operator * Fix rendered characters spacing issues * Refactor naming of internal render packages
2020-03-02 23:22:54 +02:00
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package context
import (
"errors"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"github.com/unidoc/unipdf/v3/core"
"github.com/unidoc/unipdf/v3/model"
"github.com/unidoc/unipdf/v3/internal/textencoding"
)
// TextFont represents a font used to draw text to a target, through a
// rendering context.
type TextFont struct {
Font *model.PdfFont
Face font.Face
Size float64
ttf *truetype.Font
origFont *model.PdfFont
}
// NewTextFont returns a new text font instance based on the specified PDF font
// and the specified font size.
func NewTextFont(font *model.PdfFont, size float64) (*TextFont, error) {
descriptor := font.FontDescriptor()
if descriptor == nil {
return nil, errors.New("could not get font descriptor")
}
fontStream, ok := core.GetStream(descriptor.FontFile2)
if !ok {
return nil, errors.New("missing font file stream")
}
fontData, err := core.DecodeStream(fontStream)
if err != nil {
return nil, err
}
ttfFont, err := truetype.Parse(fontData)
if err != nil {
return nil, err
}
if size <= 1 {
size = 10
}
return &TextFont{
Font: font,
Face: truetype.NewFace(ttfFont, &truetype.Options{Size: size}),
Size: size,
ttf: ttfFont,
}, nil
}
// NewTextFontFromPath returns a new text font instance based on the specified
// font file and the specified font size.
func NewTextFontFromPath(filePath string, size float64) (*TextFont, error) {
font, err := model.NewPdfFontFromTTFFile(filePath)
if err != nil {
return nil, err
}
return NewTextFont(font, size)
}
// WithSize returns a new text font instance based on the current text font,
// with the specified font size.
func (tf *TextFont) WithSize(size float64, originalFont *model.PdfFont) *TextFont {
if size <= 1 {
size = 10
}
return &TextFont{
Font: tf.Font,
Face: truetype.NewFace(tf.ttf, &truetype.Options{Size: size}),
Size: size,
ttf: tf.ttf,
origFont: originalFont,
}
}
// BytesToCharcodes converts the specified byte data to character codes, using
// the encapsulated PDF font instance.
func (tf *TextFont) BytesToCharcodes(data []byte) []textencoding.CharCode {
if tf.origFont != nil {
return tf.origFont.BytesToCharcodes(data)
}
return tf.Font.BytesToCharcodes(data)
}
// CharcodesToUnicode converts the specified character codes to a slice of
// runes, using the encapsulated PDF font instance.
func (tf *TextFont) CharcodesToUnicode(charcodes []textencoding.CharCode) []rune {
if tf.origFont != nil {
return tf.origFont.CharcodesToUnicode(charcodes)
}
return tf.Font.CharcodesToUnicode(charcodes)
}
// GetCharMetrics returns the metrics of the specified character code. The
// character metrics are calculated by the internal PDF font.
func (tf *TextFont) GetCharMetrics(code textencoding.CharCode) (float64, float64, bool) {
if metrics, ok := tf.Font.GetCharMetrics(code); ok && metrics.Wx != 0 {
return metrics.Wx, metrics.Wy, ok
}
if tf.origFont == nil {
return 0, 0, false
}
metrics, ok := tf.origFont.GetCharMetrics(code)
return metrics.Wx, metrics.Wy, ok && metrics.Wx != 0
}
// GetRuneMetrics returns the metrics of the specified rune. The character
// metrics are calculated by the internal PDF font.
func (tf *TextFont) GetRuneMetrics(r rune) (float64, float64, bool) {
if metrics, ok := tf.Font.GetRuneMetrics(r); ok && metrics.Wx != 0 {
return metrics.Wx, metrics.Wy, ok
}
if tf.origFont == nil {
return 0, 0, false
}
metrics, ok := tf.origFont.GetRuneMetrics(r)
return metrics.Wx, metrics.Wy, ok && metrics.Wx != 0
}