github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/font_test.go (about)

     1  /*
     2  Copyright 2020 The pdf Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/pdfcpu/pdfcpu/pkg/api"
    26  	"github.com/pdfcpu/pdfcpu/pkg/font"
    27  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
    28  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/color"
    29  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/draw"
    30  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
    31  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
    32  )
    33  
    34  func writeCoreFontDemoContent(xRefTable *model.XRefTable, p model.Page, fontName string) {
    35  	baseFontName := "Helvetica"
    36  	baseFontSize := 24
    37  	baseFontKey := p.Fm.EnsureKey(baseFontName)
    38  
    39  	fontKey := p.Fm.EnsureKey(fontName)
    40  	fontSize := 24
    41  
    42  	fillCol := color.NewSimpleColor(0xf7e6c7)
    43  	draw.DrawGrid(p.Buf, 16, 14, types.RectForWidthAndHeight(55, 2, 480, 422), color.Black, &fillCol)
    44  
    45  	td := model.TextDescriptor{
    46  		FontName:       baseFontName,
    47  		FontKey:        baseFontKey,
    48  		FontSize:       baseFontSize,
    49  		HAlign:         types.AlignCenter,
    50  		VAlign:         types.AlignBaseline,
    51  		Scale:          1.0,
    52  		ScaleAbs:       true,
    53  		RMode:          draw.RMFill,
    54  		StrokeCol:      color.Black,
    55  		FillCol:        color.NewSimpleColor(0xab6f30),
    56  		ShowBackground: true,
    57  		BackgroundCol:  color.SimpleColor{R: 1., G: .98, B: .77},
    58  	}
    59  
    60  	s := fmt.Sprintf("%s %d points", fontName, fontSize)
    61  	if fontName != "ZapfDingbats" && fontName != "Symbol" {
    62  		s = s + " (CP1252)"
    63  	}
    64  	td.X, td.Y, td.Text = p.MediaBox.Width()/2, 500, s
    65  	td.StrokeCol, td.FillCol = color.NewSimpleColor(0x77bdbd), color.NewSimpleColor(0xab6f30)
    66  	model.WriteMultiLine(xRefTable, p.Buf, p.MediaBox, nil, td)
    67  
    68  	for i := 0; i < 16; i++ {
    69  		s = fmt.Sprintf("#%02X", i)
    70  		td.X, td.Y, td.Text, td.FontSize = float64(70+i*30), 427, s, 14
    71  		td.StrokeCol, td.FillCol = color.Black, color.SimpleColor{B: .8}
    72  		model.WriteMultiLine(xRefTable, p.Buf, p.MediaBox, nil, td)
    73  	}
    74  
    75  	for j := 0; j < 14; j++ {
    76  		s = fmt.Sprintf("#%02X", j*16+32)
    77  		td.X, td.Y, td.Text = 41, float64(403-j*30), s
    78  		td.StrokeCol, td.FillCol = color.Black, color.SimpleColor{B: .8}
    79  		td.FontName, td.FontKey, td.FontSize = baseFontName, baseFontKey, 14
    80  		model.WriteMultiLine(xRefTable, p.Buf, p.MediaBox, nil, td)
    81  		for i := 0; i < 16; i++ {
    82  			b := byte(32 + j*16 + i)
    83  			s = string([]byte{b})
    84  			td.X, td.Y, td.Text = float64(70+i*30), float64(400-j*30), s
    85  			td.StrokeCol, td.FillCol = color.Black, color.Black
    86  			td.FontName, td.FontKey, td.FontSize = fontName, fontKey, fontSize
    87  			model.WriteMultiLine(xRefTable, p.Buf, p.MediaBox, nil, td)
    88  		}
    89  	}
    90  }
    91  
    92  func createCoreFontDemoPage(xRefTable *model.XRefTable, w, h int, fontName string) model.Page {
    93  	mediaBox := types.RectForDim(float64(w), float64(h))
    94  	p := model.NewPageWithBg(mediaBox, color.NewSimpleColor(0xbeded9))
    95  	writeCoreFontDemoContent(xRefTable, p, fontName)
    96  	return p
    97  }
    98  func TestCoreFontDemoPDF(t *testing.T) {
    99  	msg := "TestCoreFontDemoPDF"
   100  	w, h := 600, 600
   101  	for _, fn := range font.CoreFontNames() {
   102  		xRefTable, err := pdfcpu.CreateDemoXRef()
   103  		if err != nil {
   104  			t.Fatalf("%s: %v\n", msg, err)
   105  		}
   106  		rootDict, err := xRefTable.Catalog()
   107  		if err != nil {
   108  			t.Fatalf("%s: %v\n", msg, err)
   109  		}
   110  		p := createCoreFontDemoPage(xRefTable, w, h, fn)
   111  		if err = pdfcpu.AddPageTreeWithSamplePage(xRefTable, rootDict, p); err != nil {
   112  			t.Fatalf("%s: %v\n", msg, err)
   113  		}
   114  		outFile := filepath.Join("..", "..", "samples", "fonts", "core", fn+".pdf")
   115  		createAndValidate(t, xRefTable, outFile, msg)
   116  	}
   117  }
   118  
   119  func TestUserFontDemoPDF(t *testing.T) {
   120  	msg := "TestUserFontDemoPDF"
   121  
   122  	// For each installed user font create a single page pdf cheat sheet for every unicode plane covered
   123  	// in pkg/samples/fonts/user.
   124  	for _, fn := range font.UserFontNames() {
   125  		fmt.Println(fn)
   126  		if err := api.CreateUserFontDemoFiles(filepath.Join("..", "..", "samples", "fonts", "user"), fn); err != nil {
   127  			t.Fatalf("%s: %v\n", msg, err)
   128  		}
   129  	}
   130  }