From 8022e9b91aea9cdbf7f7c561fc59a6e291cbfa59 Mon Sep 17 00:00:00 2001 From: DarkFreedman Date: Mon, 6 May 2019 13:23:42 +0300 Subject: Added copyrights. And "right to left" languages support. --- def.go | 1 + fpdf.go | 43 +++++++++++++++++++++++++++++++++++++++++-- utf8fontfile.go | 18 ++++++++++++++++++ util.go | 1 + 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/def.go b/def.go index 4c46d03..205389b 100644 --- a/def.go +++ b/def.go @@ -497,6 +497,7 @@ type PageBox struct { // Fpdf is the principal structure for creating a single PDF document type Fpdf struct { isCurrentUTF8 bool // is current font used in utf-8 mode + isRTL bool // is is right to left mode enabled page int // current page number n int // current object number offsets []int // array of object offsets diff --git a/fpdf.go b/fpdf.go index 1400a85..fa79033 100644 --- a/fpdf.go +++ b/fpdf.go @@ -618,6 +618,16 @@ func (f *Fpdf) AliasNbPages(aliasStr string) { f.aliasNbPagesStr = aliasStr } +// enable right to left mode +func (f *Fpdf) RTL() { + f.isRTL = true +} + +// disable right to left mode +func (f *Fpdf) LTR() { + f.isRTL = false +} + // open begins a document func (f *Fpdf) open() { f.state = 1 @@ -2091,6 +2101,10 @@ func (f *Fpdf) Bookmark(txtStr string, level int, y float64) { func (f *Fpdf) Text(x, y float64, txtStr string) { var txt2 string if f.isCurrentUTF8 { + if f.isRTL { + txtStr = revertText(txtStr) + x -= f.GetStringWidth(txtStr) + } txt2 = f.escape(utf8toutf16(txtStr, false)) for _, uni := range []rune(txtStr) { f.currentFont.usedRunes[int(uni)] = int(uni) @@ -2279,6 +2293,9 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr, borderStr string, ln int, } //If multibyte, Tw has no effect - do word spacing using an adjustment before each space if (f.ws != 0 || alignStr == "J") && f.isCurrentUTF8 { // && f.ws != 0 + if f.isRTL { + txtStr = revertText(txtStr) + } wmax := int(math.Ceil((w - 2*f.cMargin) * 1000 / f.fontSize)) for _, uni := range []rune(txtStr) { f.currentFont.usedRunes[int(uni)] = int(uni) @@ -2301,6 +2318,9 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr, borderStr string, ln int, } else { var txt2 string if f.isCurrentUTF8 { + if f.isRTL { + txtStr = revertText(txtStr) + } txt2 = f.escape(utf8toutf16(txtStr, false)) for _, uni := range []rune(txtStr) { f.currentFont.usedRunes[int(uni)] = int(uni) @@ -2344,6 +2364,17 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr, borderStr string, ln int, return } +// Revert string to use in RTL languages +func revertText(text string) string { + oldText := []rune(text) + newText := make([]rune, len(oldText)) + lenght := len(oldText) - 1 + for i, r := range oldText { + newText[lenght-i] = r + } + return string(newText) +} + // Cell is a simpler version of CellFormat with no fill, border, links or // special alignment. func (f *Fpdf) Cell(w, h float64, txtStr string) { @@ -2496,7 +2527,11 @@ func (f *Fpdf) MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill if f.isCurrentUTF8 { newAlignStr := alignStr if newAlignStr == "J" { - newAlignStr = "L" + if f.isRTL { + newAlignStr = "R" + } else { + newAlignStr = "L" + } } f.CellFormat(w, h, string([]rune(s)[j:i]), b, 2, newAlignStr, fill, 0, "") } else { @@ -2576,7 +2611,11 @@ func (f *Fpdf) MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill } if f.isCurrentUTF8 { if alignStr == "J" { - alignStr = "" + if f.isRTL { + alignStr = "R" + } else { + alignStr = "" + } } f.CellFormat(w, h, string([]rune(s)[j:i]), b, 2, alignStr, fill, 0, "") } else { diff --git a/utf8fontfile.go b/utf8fontfile.go index ad9f924..964e919 100644 --- a/utf8fontfile.go +++ b/utf8fontfile.go @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019 Arteom Korotkiy (Gmail: arteomkorotkiy) + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + package gofpdf import ( @@ -14,6 +30,8 @@ const symbolScale = 1 << 3 const symbolContinue = 1 << 5 const symbolAllScale = 1 << 6 const symbol2x2 = 1 << 7 + +// CID map Init const toUnicode = "/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo\n<> def\n/CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n1 begincodespacerange\n<0000> \nendcodespacerange\n1 beginbfrange\n<0000> <0000>\nendbfrange\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend" type utf8FontFile struct { diff --git a/util.go b/util.go index 1a776e8..e96cdd8 100644 --- a/util.go +++ b/util.go @@ -321,6 +321,7 @@ func (s *SizeType) ScaleToHeight(height float64) SizeType { return SizeType{width, height} } +//The untypedKeyMap structure and its methods are copyrighted 2019 by Arteom Korotkiy (Gmail: arteomkorotkiy). //Imitation of untyped Map Array type untypedKeyMap struct { keySet []interface{} -- cgit v1.2.1-24-ge1ad