summaryrefslogtreecommitdiff
path: root/fpdf.go
diff options
context:
space:
mode:
authorStani <spe.stani.be@gmail.com>2015-07-12 17:03:33 +0200
committerStani <spe.stani.be@gmail.com>2015-07-12 17:05:13 +0200
commitf00c0a597be2dca589bdd7bd6ba5937b19ebdcfe (patch)
treea363671fb0fd63a61fe4ed084b576799a4c843d8 /fpdf.go
parent4aa4a931b57c01e39ef920dce28cf8210e2c4cb0 (diff)
implement vertical baseline alignment for text
Diffstat (limited to 'fpdf.go')
-rw-r--r--fpdf.go34
1 files changed, 24 insertions, 10 deletions
diff --git a/fpdf.go b/fpdf.go
index 6eae426..fd0c91f 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -1376,6 +1376,16 @@ func (f *Fpdf) AddFont(familyStr, styleStr, fileStr string) {
f.AddFontFromReader(familyStr, styleStr, file)
}
+// getFontKey is used by AddFontFromReader and GetFontDesc
+func getFontKey(familyStr, styleStr string) string {
+ familyStr = strings.ToLower(familyStr)
+ styleStr = strings.ToUpper(styleStr)
+ if styleStr == "IB" {
+ styleStr = "BI"
+ }
+ return familyStr + styleStr
+}
+
// AddFontFromReader imports a TrueType, OpenType or Type1 font and makes it
// available using a reader that satisifies the io.Reader interface. See
// AddFont for details about familyStr and styleStr.
@@ -1385,12 +1395,7 @@ func (f *Fpdf) AddFontFromReader(familyStr, styleStr string, r io.Reader) {
}
// dbg("Adding family [%s], style [%s]", familyStr, styleStr)
var ok bool
- familyStr = strings.ToLower(familyStr)
- styleStr = strings.ToUpper(styleStr)
- if styleStr == "IB" {
- styleStr = "BI"
- }
- fontkey := familyStr + styleStr
+ fontkey := getFontKey(familyStr, styleStr)
_, ok = f.fonts[fontkey]
if ok {
return
@@ -1434,8 +1439,7 @@ func (f *Fpdf) AddFontFromReader(familyStr, styleStr string, r io.Reader) {
// documentation about the font descriptor.
// See AddFont for details about familyStr and styleStr.
func (f *Fpdf) GetFontDesc(familyStr, styleStr string) FontDescType {
- fontkey := familyStr + styleStr // see AddFontFromReader
- return f.fonts[fontkey].Desc
+ return f.fonts[getFontKey(familyStr, styleStr)].Desc
}
// SetFont sets the font used to print character strings. It is mandatory to
@@ -1686,8 +1690,8 @@ func (f *Fpdf) SetAcceptPageBreakFunc(fnc func() bool) {
// alignStr specifies how the text is to be positionined within the cell.
// Horizontal alignment is controlled by including "L", "C" or "R" (left,
// center, right) in alignStr. Vertical alignment is controlled by including
-// "T", "M" or "B" (top, middle, bottom) in alignStr. The default alignment is
-// left middle.
+// "T", "M", "B" or "A" (top, middle, bottom, baseline) in alignStr. The default
+// alignment is left middle.
//
// fill is true to paint the cell background or false to leave it transparent.
//
@@ -1778,6 +1782,16 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr string, borderStr string, ln int,
dy = (f.fontSize - h) / 2.0
} else if strings.Index(alignStr, "B") != -1 {
dy = (h - f.fontSize) / 2.0
+ } else if strings.Index(alignStr, "A") != -1 {
+ var descent float64
+ d := f.currentFont.Desc
+ if d.Descent == 0 {
+ // not defined (standard font?), use average of 19%
+ descent = -0.19 * f.fontSize
+ } else {
+ descent = float64(d.Descent) * f.fontSize / float64(d.Ascent-d.Descent)
+ }
+ dy = (h-f.fontSize)/2.0 - descent
} else {
dy = 0
}