Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tdewolff/canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Nov 3, 2024
2 parents fe6454e + 7441cbf commit 40b2fbe
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 22 deletions.
4 changes: 2 additions & 2 deletions canvas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func TestCanvas(t *testing.T) {
//buf.Reset()
//pdfCompress = false
//c.WritePDF(buf)
//ioutil.WriteFile("test/canvas.pdf", buf.Bytes(), 0644)
//os.WriteFile("test/canvas.pdf", buf.Bytes(), 0644)
//s = regexp.MustCompile(`stream\nx(.|\n)+\nendstream\n`).ReplaceAllString(buf.String(), "stream\n\nendstream\n") // remove embedded font
//test.String(t, s, "%PDF-1.7\n1 0 obj\n<< /Subtype /TrueType /Filter /FlateDecode /Length 215980 >> stream\n\nendstream\nendobj\n5 0 obj\n<< /Type /Page /Contents 4 0 R /Group << /Type /Group /CS /DeviceRGB /I true /S /Transparency >> /MediaBox [0 0 60 93] /Parent 5 0 R /Resources << /Font << /F0 2 0 R >> /XObject << /Im0 3 0 R >> >> >>\nendobj\n6 0 obj\n<< /Type /Pages /Count 1 /Kids [5 0 R] >>\nendobj\n7 0 obj\n<< /Type /Catalog /Pages 6 0 R >>\nendobj\nxref\n0 8\n0000000000 65535 f\n0000000009 00000 n\n0000216083 00000 n\n0000227285 00000 n\n0000227491 00000 n\n0000227888 00000 n\n0000228104 00000 n\n0000228161 00000 n\ntrailer\n<< /Root 7 0 R /Size 7 >>\nstarxref\n228210\n%%EOF")

//buf.Reset()
//c.WriteEPS(buf)
//ioutil.WriteFile("test/canvas.eps", buf.Bytes(), 0644)
//os.WriteFile("test/canvas.eps", buf.Bytes(), 0644)
// TODO: test EPS when fully supported
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/pdftext/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"

"github.com/tdewolff/parse/v2/strconv"
)
Expand All @@ -31,7 +30,7 @@ type pdfReader struct {
}

func NewPDFReader(reader io.Reader, password string) (*pdfReader, error) {
data, err := ioutil.ReadAll(reader)
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
} else if len(data) < 8 || !bytes.Equal(data[:7], []byte("%PDF-1.")) || data[7] < '0' || '7' < data[7] {
Expand Down
3 changes: 1 addition & 2 deletions examples/go-chart/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"os"
"time"

Expand All @@ -15,7 +14,7 @@ import (
func main() {
xv, yv := xvalues(), yvalues()

dejavu, err := ioutil.ReadFile("../../resources/DejaVuSerif.ttf")
dejavu, err := os.ReadFile("../../resources/DejaVuSerif.ttf")
if err != nil {
panic(err)
}
Expand Down
15 changes: 8 additions & 7 deletions examples/html-canvas/files.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package canvas
import (
"fmt"
"image/color"
"io/ioutil"
"log"
"math"
"os"
Expand Down Expand Up @@ -253,7 +252,7 @@ func LoadSystemFont(name string, style FontStyle) (*Font, error) {

// LoadFontFile loads a font from a file.
func LoadFontFile(filename string, style FontStyle) (*Font, error) {
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to load font file '%s': %w", filename, err)
}
Expand All @@ -262,7 +261,7 @@ func LoadFontFile(filename string, style FontStyle) (*Font, error) {

// LoadFontCollection loads a font from a collection file and uses the font at the specified index.
func LoadFontCollection(filename string, index int, style FontStyle) (*Font, error) {
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to load font file '%s': %w", filename, err)
}
Expand Down Expand Up @@ -328,7 +327,6 @@ func (f *Font) SetVariations(variations string) {

// SetFeatures sets the font features (not yet supported).
func (f *Font) SetFeatures(features string) {
// TODO: support font features
f.features = features
}

Expand Down
5 changes: 2 additions & 3 deletions preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"image/color"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -17,7 +16,7 @@ func loadFont(name string, style FontStyle) ([]byte, error) {
if !ok {
return nil, fmt.Errorf("failed to find font '%s'", name)
}
return ioutil.ReadFile(filename)
return os.ReadFile(filename)
}

// DrawPreview draws the canvas's preview to a Context.
Expand All @@ -40,7 +39,7 @@ func DrawPreview(ctx *Context) error {
if err != nil {
return err
}
lenna, err := ioutil.ReadFile(filepath.Join(root, "resources/lenna.png"))
lenna, err := os.ReadFile(filepath.Join(root, "resources/lenna.png"))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion renderers/gonumplot.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *GonumPlot) SetLineWidth(length vg.Length) {
//
// The initial dash pattern is a solid line.
func (r *GonumPlot) SetLineDash(pattern []vg.Length, offset vg.Length) {
array := make([]float64, len(pattern))
array := make([]float64, 0, len(pattern))
for _, dash := range pattern {
array = append(array, float64(dash*mmPerPt))
}
Expand Down
28 changes: 27 additions & 1 deletion text/harfbuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package text

import (
"bytes"
"fmt"
"os"
"strings"

"github.com/go-text/typesetting/harfbuzz"
"github.com/go-text/typesetting/language"
Expand Down Expand Up @@ -52,7 +55,7 @@ func (s Shaper) Shape(text string, ppem uint16, direction Direction, script Scri
buf.Props.Script = language.Script(script)
buf.Props.Language = language.NewLanguage(lang)
buf.GuessSegmentProperties() // only sets direction, script, and language if unset
buf.Shape(s.font, nil)
buf.Shape(s.font, parseFeatures(features))

runeMap := make([]int, len(rtext)+1)
j := 0
Expand All @@ -77,6 +80,29 @@ func (s Shaper) Shape(text string, ppem uint16, direction Direction, script Scri
return glyphs
}

func parseFeatures(input string) []harfbuzz.Feature {
if input == "" {
return nil
}

features := []harfbuzz.Feature{}
for _, part := range strings.Split(input, ",") {
if part == "" {
continue
}

feature, err := harfbuzz.ParseFeature(part)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing feature '%s': %v\n", part, err)
continue
}

features = append(features, feature)
}

return features
}

func LookupScript(r rune) Script {
return Script(language.LookupScript(r))
}
Expand Down

0 comments on commit 40b2fbe

Please sign in to comment.