github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/creator/division_test.go (about)

     1  /*
     2   * This file is subject to the terms and conditions defined in
     3   * file 'LICENSE.md', which is part of this source code package.
     4   */
     5  
     6  package creator
     7  
     8  import (
     9  	"math/rand"
    10  	"strconv"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/unidoc/unidoc/pdf/model/fonts"
    15  )
    16  
    17  var seed = rand.New(rand.NewSource(time.Now().UnixNano()))
    18  
    19  const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    20  
    21  func RandString(length int) string {
    22  	b := make([]byte, length)
    23  	for i := range b {
    24  		b[i] = charset[seed.Intn(len(charset))]
    25  	}
    26  	return string(b)
    27  }
    28  
    29  func TestDivVertical(t *testing.T) {
    30  	fontRegular := fonts.NewFontCourier()
    31  	fontBold := fonts.NewFontCourierBold()
    32  
    33  	c := New()
    34  	c.NewPage()
    35  
    36  	// Draw section title.
    37  	p := NewParagraph("Regular division component")
    38  	p.SetMargins(0, 0, 20, 10)
    39  	p.SetFont(fontBold)
    40  
    41  	err := c.Draw(p)
    42  	if err != nil {
    43  		t.Fatalf("Error drawing: %v", err)
    44  	}
    45  
    46  	// Draw division.
    47  	div := NewDivision()
    48  
    49  	if div.Inline() {
    50  		t.Fatal("Fail: Incorrect inline mode value")
    51  	}
    52  
    53  	p = NewParagraph("Components are stacked vertically ")
    54  	p.SetFont(fontRegular)
    55  	div.Add(p)
    56  
    57  	p = NewParagraph("but not horizontally")
    58  	p.SetFont(fontBold)
    59  	div.Add(p)
    60  
    61  	// Add styled paragraph
    62  	style := NewTextStyle()
    63  	style.Color = ColorRGBFrom8bit(0, 0, 255)
    64  
    65  	s := NewStyledParagraph("Not even with a styled ", style)
    66  
    67  	style.Color = ColorRGBFrom8bit(255, 0, 0)
    68  	s.Append("paragraph", style)
    69  
    70  	div.Add(s)
    71  
    72  	err = c.Draw(div)
    73  	if err != nil {
    74  		t.Fatalf("Error drawing: %v", err)
    75  	}
    76  
    77  	// Write output file.
    78  	err = c.WriteToFile("/tmp/division_vertical.pdf")
    79  	if err != nil {
    80  		t.Fatalf("Fail: %v\n", err)
    81  	}
    82  }
    83  
    84  func TestDivInline(t *testing.T) {
    85  	fontRegular := fonts.NewFontCourier()
    86  	fontBold := fonts.NewFontCourierBold()
    87  
    88  	c := New()
    89  	c.NewPage()
    90  
    91  	// Draw section title.
    92  	p := NewParagraph("Inline division component")
    93  	p.SetMargins(0, 0, 20, 10)
    94  	p.SetFont(fontBold)
    95  
    96  	err := c.Draw(p)
    97  	if err != nil {
    98  		t.Fatalf("Error drawing: %v", err)
    99  	}
   100  
   101  	// Draw division.
   102  	div := NewDivision()
   103  	div.SetInline(true)
   104  
   105  	if !div.Inline() {
   106  		t.Fatal("Fail: Incorrect inline mode value")
   107  	}
   108  
   109  	p = NewParagraph("Components are stacked both vertically ")
   110  	p.SetEnableWrap(false)
   111  	p.SetFont(fontRegular)
   112  	div.Add(p)
   113  
   114  	p = NewParagraph("and horizontally. ")
   115  	p.SetEnableWrap(false)
   116  	p.SetFont(fontBold)
   117  	div.Add(p)
   118  
   119  	p = NewParagraph("Only if they fit right!")
   120  	p.SetEnableWrap(false)
   121  	p.SetFont(fontRegular)
   122  	div.Add(p)
   123  
   124  	p = NewParagraph("This one did not fit in the available line space. ")
   125  	p.SetEnableWrap(false)
   126  	p.SetFont(fontBold)
   127  	div.Add(p)
   128  
   129  	// Add styled paragraph
   130  	style := NewTextStyle()
   131  	style.Color = ColorRGBFrom8bit(0, 0, 255)
   132  
   133  	s := NewStyledParagraph("This styled paragraph should ", style)
   134  
   135  	style.Color = ColorRGBFrom8bit(255, 0, 0)
   136  	s.Append("fit", style)
   137  
   138  	style.Color = ColorRGBFrom8bit(0, 255, 0)
   139  	style.Font = fontBold
   140  	s.Append(" right in.", style)
   141  
   142  	div.Add(s)
   143  
   144  	err = c.Draw(div)
   145  	if err != nil {
   146  		t.Fatalf("Error drawing: %v", err)
   147  	}
   148  
   149  	// Write output file.
   150  	err = c.WriteToFile("/tmp/division_inline.pdf")
   151  	if err != nil {
   152  		t.Fatalf("Fail: %v\n", err)
   153  	}
   154  }
   155  
   156  func TestDivNumberMatrix(t *testing.T) {
   157  	fontRegular := fonts.NewFontCourier()
   158  	fontBold := fonts.NewFontCourierBold()
   159  
   160  	c := New()
   161  	c.NewPage()
   162  
   163  	// Draw section title.
   164  	p := NewParagraph("A list of numbers in an inline division")
   165  	p.SetMargins(0, 0, 20, 10)
   166  	p.SetFont(fontBold)
   167  
   168  	err := c.Draw(p)
   169  	if err != nil {
   170  		t.Fatalf("Error drawing: %v", err)
   171  	}
   172  
   173  	// Draw division.
   174  	div := NewDivision()
   175  	div.SetInline(true)
   176  
   177  	for i := 0; i < 100; i++ {
   178  		r := byte(seed.Intn(200))
   179  		g := byte(seed.Intn(200))
   180  		b := byte(seed.Intn(200))
   181  
   182  		p := NewParagraph(strconv.Itoa(i) + " ")
   183  		p.SetEnableWrap(false)
   184  		p.SetColor(ColorRGBFrom8bit(r, g, b))
   185  
   186  		if seed.Intn(2)%2 == 0 {
   187  			p.SetFont(fontRegular)
   188  		} else {
   189  			p.SetFont(fontBold)
   190  		}
   191  
   192  		div.Add(p)
   193  	}
   194  
   195  	err = c.Draw(div)
   196  	if err != nil {
   197  		t.Fatalf("Error drawing: %v", err)
   198  	}
   199  
   200  	// Write output file.
   201  	err = c.WriteToFile("/tmp/division_number_matrix.pdf")
   202  	if err != nil {
   203  		t.Fatalf("Fail: %v\n", err)
   204  	}
   205  }
   206  
   207  func TestDivRandomSequences(t *testing.T) {
   208  	fontRegular := fonts.NewFontHelvetica()
   209  	fontBold := fonts.NewFontHelveticaBold()
   210  
   211  	c := New()
   212  	c.NewPage()
   213  
   214  	// Draw section title.
   215  	p := NewParagraph("Inline division of random sequences on multiple pages")
   216  	p.SetMargins(0, 0, 20, 10)
   217  	p.SetFont(fontBold)
   218  
   219  	err := c.Draw(p)
   220  	if err != nil {
   221  		t.Fatalf("Error drawing: %v", err)
   222  	}
   223  
   224  	// Draw division.
   225  	div := NewDivision()
   226  	div.SetInline(true)
   227  
   228  	style := NewTextStyle()
   229  
   230  	for i := 0; i < 350; i++ {
   231  		r := byte(seed.Intn(200))
   232  		g := byte(seed.Intn(200))
   233  		b := byte(seed.Intn(200))
   234  
   235  		word := RandString(seed.Intn(10)+5) + " "
   236  		fontSize := float64(11 + seed.Intn(3))
   237  
   238  		if seed.Intn(2)%2 == 0 {
   239  			p := NewParagraph(word)
   240  			p.SetEnableWrap(false)
   241  			p.SetColor(ColorRGBFrom8bit(r, g, b))
   242  			p.SetFontSize(fontSize)
   243  
   244  			if seed.Intn(2)%2 == 0 {
   245  				p.SetFont(fontBold)
   246  			} else {
   247  				p.SetFont(fontRegular)
   248  			}
   249  
   250  			div.Add(p)
   251  		} else {
   252  			style.Color = ColorRGBFrom8bit(r, g, b)
   253  			style.FontSize = fontSize
   254  
   255  			if seed.Intn(2)%2 == 0 {
   256  				style.Font = fontBold
   257  			} else {
   258  				style.Font = fontRegular
   259  			}
   260  
   261  			s := NewStyledParagraph(word, style)
   262  			s.SetEnableWrap(false)
   263  			div.Add(s)
   264  		}
   265  	}
   266  
   267  	err = c.Draw(div)
   268  	if err != nil {
   269  		t.Fatalf("Error drawing: %v", err)
   270  	}
   271  
   272  	// Write output file.
   273  	err = c.WriteToFile("/tmp/division_random_sequences.pdf")
   274  	if err != nil {
   275  		t.Fatalf("Fail: %v\n", err)
   276  	}
   277  }
   278  
   279  func TestTableDivisions(t *testing.T) {
   280  	fontRegular := fonts.NewFontHelvetica()
   281  	fontBold := fonts.NewFontHelveticaBold()
   282  
   283  	c := New()
   284  	c.NewPage()
   285  
   286  	// Draw section title.
   287  	p := NewParagraph("Table containing division components")
   288  	p.SetMargins(0, 0, 20, 10)
   289  	p.SetFont(fontBold)
   290  
   291  	err := c.Draw(p)
   292  	if err != nil {
   293  		t.Fatalf("Error drawing: %v", err)
   294  	}
   295  
   296  	table := NewTable(2)
   297  	table.SetColumnWidths(0.35, 0.65)
   298  
   299  	// Add regular division to table.
   300  	divRegular := NewDivision()
   301  
   302  	p = NewParagraph("Components are stacked vertically ")
   303  	p.SetFont(fontRegular)
   304  	divRegular.Add(p)
   305  
   306  	p = NewParagraph("but not horizontally")
   307  	p.SetFont(fontBold)
   308  	divRegular.Add(p)
   309  
   310  	cell := table.NewCell()
   311  	cell.SetBorder(CellBorderStyleBox, 1)
   312  	cell.SetContent(divRegular)
   313  
   314  	// Add inline division to table.
   315  	divInline := NewDivision()
   316  	divInline.SetInline(true)
   317  
   318  	p = NewParagraph("Components are stacked vertically ")
   319  	p.SetEnableWrap(false)
   320  	p.SetFont(fontRegular)
   321  	divInline.Add(p)
   322  
   323  	p = NewParagraph("and horizontally. ")
   324  	p.SetEnableWrap(false)
   325  	p.SetFont(fontBold)
   326  	divInline.Add(p)
   327  
   328  	p = NewParagraph("Only if they fit!")
   329  	p.SetEnableWrap(false)
   330  	p.SetFont(fontRegular)
   331  	divInline.Add(p)
   332  
   333  	p = NewParagraph("This one did not fit in the available line space")
   334  	p.SetEnableWrap(false)
   335  	p.SetFont(fontBold)
   336  	divInline.Add(p)
   337  
   338  	cell = table.NewCell()
   339  	cell.SetBorder(CellBorderStyleBox, 1)
   340  	cell.SetContent(divInline)
   341  
   342  	// Draw table.
   343  	err = c.Draw(table)
   344  	if err != nil {
   345  		t.Fatalf("Error drawing: %v", err)
   346  	}
   347  
   348  	// Write output file.
   349  	err = c.WriteToFile("/tmp/division_table.pdf")
   350  	if err != nil {
   351  		t.Fatalf("Fail: %v\n", err)
   352  	}
   353  }