github.com/phpdave11/gofpdf@v1.4.2/ttfparser_test.go (about)

     1  /*
     2   * Copyright (c) 2013-2015 Kurt Jung (Gmail: kurt.w.jung)
     3   *
     4   * Permission to use, copy, modify, and distribute this software for any
     5   * purpose with or without fee is hereby granted, provided that the above
     6   * copyright notice and this permission notice appear in all copies.
     7   *
     8   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     9   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    10   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    11   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    12   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    13   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    14   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    15   */
    16  
    17  package gofpdf_test
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  
    23  	"github.com/phpdave11/gofpdf"
    24  	"github.com/phpdave11/gofpdf/internal/example"
    25  )
    26  
    27  func ExampleTtfParse() {
    28  	ttf, err := gofpdf.TtfParse(example.FontDir() + "/calligra.ttf")
    29  	if err == nil {
    30  		fmt.Printf("Postscript name:  %s\n", ttf.PostScriptName)
    31  		fmt.Printf("unitsPerEm:       %8d\n", ttf.UnitsPerEm)
    32  		fmt.Printf("Xmin:             %8d\n", ttf.Xmin)
    33  		fmt.Printf("Ymin:             %8d\n", ttf.Ymin)
    34  		fmt.Printf("Xmax:             %8d\n", ttf.Xmax)
    35  		fmt.Printf("Ymax:             %8d\n", ttf.Ymax)
    36  	} else {
    37  		fmt.Printf("%s\n", err)
    38  	}
    39  	// Output:
    40  	// Postscript name:  CalligrapherRegular
    41  	// unitsPerEm:           1000
    42  	// Xmin:                 -173
    43  	// Ymin:                 -234
    44  	// Xmax:                 1328
    45  	// Ymax:                  899
    46  }
    47  
    48  func hexStr(s string) string {
    49  	var b bytes.Buffer
    50  	b.WriteString("\"")
    51  	for _, ch := range []byte(s) {
    52  		b.WriteString(fmt.Sprintf("\\x%02x", ch))
    53  	}
    54  	b.WriteString("\":")
    55  	return b.String()
    56  }
    57  
    58  func ExampleFpdf_GetStringWidth() {
    59  	pdf := gofpdf.New("", "", "", example.FontDir())
    60  	pdf.SetFont("Helvetica", "", 12)
    61  	pdf.AddPage()
    62  	for _, s := range []string{"Hello", "世界", "\xe7a va?"} {
    63  		fmt.Printf("%-32s width %5.2f, bytes %2d, runes %2d\n",
    64  			hexStr(s), pdf.GetStringWidth(s), len(s), len([]rune(s)))
    65  		if pdf.Err() {
    66  			fmt.Println(pdf.Error())
    67  		}
    68  	}
    69  	pdf.Close()
    70  	// Output:
    71  	// "\x48\x65\x6c\x6c\x6f":          width  9.64, bytes  5, runes  5
    72  	// "\xe4\xb8\x96\xe7\x95\x8c":      width 13.95, bytes  6, runes  2
    73  	// "\xe7\x61\x20\x76\x61\x3f":      width 12.47, bytes  6, runes  6
    74  }