2018-07-26 13:52:28 +00:00
/ *
* This file is subject to the terms and conditions defined in
* file ' LICENSE . md ' , which is part of this source code package .
* /
package creator
import (
"fmt"
"io/ioutil"
"testing"
2018-09-06 09:45:04 +00:00
"github.com/unidoc/unidoc/pdf/model"
)
var (
2018-09-21 16:25:57 +10:00
fontHelvetica = model . NewStandard14FontMustCompile ( model . Helvetica )
fontHelveticaBold = model . NewStandard14FontMustCompile ( model . HelveticaBold )
2018-07-26 13:52:28 +00:00
)
func TestTableMultiParagraphWrapped ( t * testing . T ) {
c := New ( )
2018-10-12 23:00:02 +03:00
pageHistoryTable := c . NewTable ( 4 )
2018-07-26 13:52:28 +00:00
pageHistoryTable . SetColumnWidths ( 0.1 , 0.6 , 0.15 , 0.15 )
content := [ ] [ ] string {
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
}
2018-09-06 09:45:04 +00:00
2018-07-26 13:52:28 +00:00
for _ , rows := range content {
for _ , txt := range rows {
2018-10-12 23:00:02 +03:00
p := c . NewParagraph ( txt )
2018-07-26 13:52:28 +00:00
p . SetFontSize ( 12 )
p . SetFont ( fontHelvetica )
p . SetColor ( ColorBlack )
p . SetEnableWrap ( true )
cell := pageHistoryTable . NewCell ( )
2018-09-06 09:45:04 +00:00
cell . SetBorder ( CellBorderSideAll , CellBorderStyleSingle , 1 )
2018-07-26 13:52:28 +00:00
cell . SetContent ( p )
}
}
err := c . Draw ( pageHistoryTable )
if err != nil {
t . Fatalf ( "Error drawing: %v" , err )
}
2018-11-06 11:34:32 +00:00
err = c . WriteToFile ( tempFile ( "table_pagehist.pdf" ) )
2018-07-26 13:52:28 +00:00
if err != nil {
t . Fatalf ( "Fail: %v\n" , err )
}
}
func TestTableWithImage ( t * testing . T ) {
c := New ( )
2018-10-12 23:00:02 +03:00
pageHistoryTable := c . NewTable ( 4 )
2018-07-26 13:52:28 +00:00
pageHistoryTable . SetColumnWidths ( 0.1 , 0.6 , 0.15 , 0.15 )
content := [ ] [ ] string {
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
}
for _ , rows := range content {
for _ , txt := range rows {
2018-10-12 23:00:02 +03:00
p := c . NewParagraph ( txt )
2018-07-26 13:52:28 +00:00
p . SetFontSize ( 12 )
p . SetFont ( fontHelvetica )
p . SetColor ( ColorBlack )
p . SetEnableWrap ( true )
cell := pageHistoryTable . NewCell ( )
2018-09-06 09:45:04 +00:00
cell . SetBorder ( CellBorderSideAll , CellBorderStyleSingle , 1 )
2018-07-26 13:52:28 +00:00
err := cell . SetContent ( p )
if err != nil {
t . Fatalf ( "Error: %v" , err )
}
}
}
pageHistoryTable . SkipCells ( 1 )
2018-08-14 17:57:26 +00:00
// Add image.
2018-07-26 13:52:28 +00:00
imgData , err := ioutil . ReadFile ( testImageFile1 )
if err != nil {
t . Errorf ( "Fail: %v\n" , err )
return
}
2018-10-12 23:00:02 +03:00
img , err := c . NewImageFromData ( imgData )
2018-07-26 13:52:28 +00:00
if err != nil {
t . Errorf ( "Fail: %v\n" , err )
return
}
img . margins . top = 2.0
img . margins . bottom = 2.0
img . margins . left = 2.0
img . margins . bottom = 2.0
img . ScaleToWidth ( 0.3 * c . Width ( ) )
fmt . Printf ( "Scaling image to width: %v\n" , 0.5 * c . Width ( ) )
cell := pageHistoryTable . NewCell ( )
cell . SetContent ( img )
2018-09-06 09:45:04 +00:00
cell . SetBorder ( CellBorderSideAll , CellBorderStyleSingle , 1 )
2018-07-26 13:52:28 +00:00
err = c . Draw ( pageHistoryTable )
if err != nil {
t . Fatalf ( "Error drawing: %v" , err )
}
2018-11-06 11:34:32 +00:00
err = c . WriteToFile ( tempFile ( "table_pagehist_with_img.pdf" ) )
2018-07-26 13:52:28 +00:00
if err != nil {
t . Fatalf ( "Fail: %v\n" , err )
}
}
func TestTableWithDiv ( t * testing . T ) {
c := New ( )
2018-10-12 23:00:02 +03:00
pageHistoryTable := c . NewTable ( 4 )
2018-07-26 13:52:28 +00:00
pageHistoryTable . SetColumnWidths ( 0.1 , 0.6 , 0.15 , 0.15 )
headings := [ ] string {
"" , "Description" , "Passing" , "Total" ,
}
content := [ ] [ ] string {
{ "1" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "2" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "3" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "3" , "FullText Search Highlight the Term in Results. Going hunting in the winter can be fruitful, especially if it has not been too cold and the deer are well fed. \n\nissues 60" , "120 90 30" , "130 1" } ,
{ "4" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "5" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "6" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130 a b c d e f g" } ,
{ "7" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "8" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130 gogogoggogoogogo" } ,
{ "9" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "10" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "11" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "12" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "13" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "14" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "15" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "16" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "17" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "18" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "19" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
{ "20" , "FullText Search Highlight the Term in Results \n\nissues 60" , "120" , "130" } ,
}
for _ , rows := range content {
for colIdx , txt := range rows {
2018-10-12 23:00:02 +03:00
p := c . NewParagraph ( txt )
2018-07-26 13:52:28 +00:00
p . SetFontSize ( 12 )
p . SetFont ( fontHelvetica )
p . SetColor ( ColorBlack )
p . SetMargins ( 0 , 5 , 10.0 , 10.0 )
if len ( txt ) > 10 {
p . SetTextAlignment ( TextAlignmentJustify )
} else {
p . SetTextAlignment ( TextAlignmentCenter )
}
// Place cell contents (header and text) inside a div.
2018-10-12 23:00:02 +03:00
div := c . NewDivision ( )
2018-07-26 13:52:28 +00:00
if len ( headings [ colIdx ] ) > 0 {
2018-10-12 23:00:02 +03:00
heading := c . NewParagraph ( headings [ colIdx ] )
2018-07-26 13:52:28 +00:00
heading . SetFontSize ( 14 )
heading . SetFont ( fontHelveticaBold )
heading . SetColor ( ColorRed )
heading . SetTextAlignment ( TextAlignmentCenter )
err := div . Add ( heading )
if err != nil {
t . Fatalf ( "Error: %v" , err )
}
}
err := div . Add ( p )
if err != nil {
t . Fatalf ( "Error: %v" , err )
}
cell := pageHistoryTable . NewCell ( )
2018-09-06 09:45:04 +00:00
cell . SetBorder ( CellBorderSideAll , CellBorderStyleSingle , 1 )
2018-07-26 13:52:28 +00:00
err = cell . SetContent ( div )
if err != nil {
t . Fatalf ( "Error: %v" , err )
}
}
}
pageHistoryTable . SkipCells ( 1 )
2018-08-14 17:57:26 +00:00
// Add image.
2018-07-26 13:52:28 +00:00
imgData , err := ioutil . ReadFile ( testImageFile1 )
if err != nil {
t . Errorf ( "Fail: %v\n" , err )
return
}
2018-10-12 23:00:02 +03:00
img , err := c . NewImageFromData ( imgData )
2018-07-26 13:52:28 +00:00
if err != nil {
t . Errorf ( "Fail: %v\n" , err )
return
}
img . margins . top = 2.0
img . margins . bottom = 2.0
img . margins . left = 2.0
img . margins . bottom = 2.0
img . ScaleToWidth ( 0.2 * c . Width ( ) )
fmt . Printf ( "Scaling image to width: %v\n" , 0.5 * c . Width ( ) )
cell := pageHistoryTable . NewCell ( )
cell . SetContent ( img )
2018-09-06 09:45:04 +00:00
cell . SetBorder ( CellBorderSideAll , CellBorderStyleSingle , 1 )
2018-07-26 13:52:28 +00:00
err = c . Draw ( pageHistoryTable )
if err != nil {
t . Fatalf ( "Error drawing: %v" , err )
}
2018-11-06 11:34:32 +00:00
err = c . WriteToFile ( tempFile ( "table_pagehist_with_div.pdf" ) )
2018-07-26 13:52:28 +00:00
if err != nil {
t . Fatalf ( "Fail: %v\n" , err )
}
}