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