Skip to content

Commit

Permalink
Font bold (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinLundSadun authored Jan 7, 2025
1 parent b06ba5b commit 19e3f20
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
8 changes: 5 additions & 3 deletions src/main/kotlin/no/nav/familie/pdf/pdf/PdfElementUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.itextpdf.kernel.pdf.tagging.StandardRoles
import com.itextpdf.layout.element.Image
import com.itextpdf.layout.element.Paragraph
import com.itextpdf.layout.element.Text
import no.nav.familie.pdf.pdf.PdfUtils.FontStil
import no.nav.familie.pdf.pdf.PdfUtils.settFont
import no.nav.familie.pdf.pdf.domain.VerdilisteElement

object PdfElementUtils {
Expand All @@ -20,12 +22,12 @@ object PdfElementUtils {
Paragraph().apply {
(element.label)
.takeIf { it?.isNotEmpty() == true }
?.let { add(Text(it).apply { simulateBold() }) }
?.let { add(Text(it).apply { settFont(FontStil.SEMIBOLD) }) }
element.alternativer?.takeIf { it.isNotEmpty() }?.let {
add(Text("\n"))
add(
Text(it).apply {
simulateItalic()
settFont(FontStil.ITALIC)
setFontSize(10f)
},
)
Expand Down Expand Up @@ -74,7 +76,7 @@ object PdfElementUtils {
Paragraph(tekst).apply {
setFontColor(DeviceRgb(0, 52, 125))
setFontSize(tekstStørrelse)
simulateBold()
settFont(FontStil.SEMIBOLD)
accessibilityProperties.role = rolle
}
}
50 changes: 37 additions & 13 deletions src/main/kotlin/no/nav/familie/pdf/pdf/PdfUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.itextpdf.layout.element.Link
import com.itextpdf.layout.element.Paragraph
import com.itextpdf.layout.element.Tab
import com.itextpdf.layout.element.TabStop
import com.itextpdf.layout.element.Text
import com.itextpdf.layout.properties.AreaBreakType
import com.itextpdf.layout.properties.TabAlignment
import com.itextpdf.layout.properties.TextAlignment
Expand Down Expand Up @@ -79,7 +80,7 @@ object PdfUtils {
UtilsMetaData.leggtilMetaData(pdfADokument, feltMap)

Document(pdfADokument).apply {
setFont(pdfSkrift())
settFont(FontStil.REGULAR)
leggTilSeksjonerOgOppdaterInnholdsfortegnelse(
feltMap,
innholdsfortegnelse,
Expand All @@ -100,7 +101,7 @@ object PdfUtils {
): Int {
val midlertidigPdfADokument = lagPdfADocument(ByteArrayOutputStream())
Document(midlertidigPdfADokument).apply {
setFont(pdfSkrift())
settFont(FontStil.REGULAR)
leggTilSeksjonerOgOppdaterInnholdsfortegnelse(
feltMap,
innholdsfortegnelse,
Expand All @@ -116,16 +117,6 @@ object PdfUtils {
}
}

private fun pdfSkrift(): PdfFont {
val skriftSti = "/fonts/SourceSans3-Regular.ttf"
val skriftProgram = FontProgramFactory.createFont(skriftSti)
return PdfFontFactory.createFont(
skriftProgram,
PdfEncodings.MACROMAN,
PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED,
)
}

private fun Document.leggTilSeksjonerOgOppdaterInnholdsfortegnelse(
feltMap: FeltMap,
innholdsfortegnelse: MutableList<InnholdsfortegnelseOppføringer>,
Expand Down Expand Up @@ -238,7 +229,7 @@ object PdfUtils {
add(
Paragraph(søknadstype).apply {
setMarginTop(-10f)
}
},
)
}
add(lagOverskriftH2("Innholdsfortegnelse"))
Expand Down Expand Up @@ -305,4 +296,37 @@ object PdfUtils {

return innholdsfortegnelseWrapper
}

private fun bestemFont(stil: FontStil): PdfFont {
val skriftSti =
when (stil) {
FontStil.REGULAR -> "/fonts/SourceSans3-Regular.ttf"
FontStil.SEMIBOLD -> "/fonts/SourceSans3-SemiBold.ttf"
FontStil.ITALIC -> "/fonts/SourceSans3-Italic.ttf"
}
val skriftProgram = FontProgramFactory.createFont(skriftSti)
return PdfFontFactory.createFont(
skriftProgram,
PdfEncodings.MACROMAN,
PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED,
)
}

fun Paragraph.settFont(stil: FontStil) {
this.setFont(bestemFont(stil))
}

fun Document.settFont(stil: FontStil) {
this.setFont(bestemFont(stil))
}

fun Text.settFont(stil: FontStil) {
this.setFont(bestemFont(stil))
}

enum class FontStil {
REGULAR,
SEMIBOLD,
ITALIC,
}
}
8 changes: 5 additions & 3 deletions src/main/kotlin/no/nav/familie/pdf/pdf/TabellUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.itextpdf.layout.element.Div
import com.itextpdf.layout.element.Paragraph
import com.itextpdf.layout.element.Table
import com.itextpdf.layout.properties.UnitValue
import no.nav.familie.pdf.pdf.PdfUtils.FontStil
import no.nav.familie.pdf.pdf.PdfUtils.settFont
import no.nav.familie.pdf.pdf.domain.VerdilisteElement

object TabellUtils {
Expand All @@ -28,7 +30,7 @@ object TabellUtils {
Paragraph(caption).apply {
setFontColor(DeviceRgb(0, 52, 125))
setFontSize(14f)
simulateBold()
settFont(FontStil.SEMIBOLD)
},
)
}
Expand Down Expand Up @@ -99,10 +101,10 @@ object TabellUtils {
.add(
Paragraph(tekst).apply {
setFontSize(12f)
if (erUthevet) settFont(FontStil.SEMIBOLD)
},
).apply {
setBorder(Border.NO_BORDER)
if (erUthevet) simulateBold()
if (erVenstreKolonne) setPaddingRight(10f) else setPaddingLeft(10f)
accessibilityProperties.role = StandardRoles.TD
}
Expand All @@ -116,7 +118,7 @@ object TabellUtils {
Paragraph(tekst).apply {
setFontColor(DeviceRgb(0, 86, 180))
setFontSize(14f)
simulateBold()
settFont(FontStil.SEMIBOLD)
},
).apply {
setBorder(Border.NO_BORDER)
Expand Down
Binary file added src/main/resources/fonts/SourceSans3-Italic.ttf
Binary file not shown.
Binary file not shown.

0 comments on commit 19e3f20

Please sign in to comment.