Exemplo n.º 1
0
func Page(pd *pdfread.PdfReaderT, page int) []byte {
	pg := pd.Pages()
	if page >= len(pg) {
		complain("Page does not exist!\n")
	}
	mbox := util.StringArray(pd.Arr(pd.Att("/MediaBox", pg[page])))
	drw := svgdraw.NewTestSvg()
	svgtext.New(pd, drw).Page = page
	w := strm.Mul(strm.Sub(mbox[2], mbox[0]), "1.25")
	h := strm.Mul(strm.Sub(mbox[3], mbox[1]), "1.25")
	drw.Write.Out(
		"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"+
			"<svg\n"+
			"   xmlns:svg=\"http://www.w3.org/2000/svg\"\n"+
			"   xmlns=\"http://www.w3.org/2000/svg\"\n"+
			"   version=\"1.0\"\n"+
			"   width=\"%s\"\n"+
			"   height=\"%s\">\n"+
			"<g transform=\"matrix(1.25,0,0,-1.25,%s,%s)\">\n",
		w, h,
		strm.Mul(mbox[0], "-1.25"),
		strm.Mul(mbox[3], "1.25"))
	cont := pd.ForcedArray(pd.Dic(pg[page])["/Contents"])
	_, ps := pd.DecodedStream(cont[0])
	drw.Interpret(fancy.SliceReader(ps))
	drw.Draw.CloseDrawing()
	drw.Write.Out("</g>\n</svg>\n")
	return drw.Write.Content
}
Exemplo n.º 2
0
func main() {
	a, _ := ioutil.ReadFile(os.Args[1])
	if a[0] == 128 {
		a = pfb.Decode(a)
	}
	g := type1.Read(fancy.SliceReader(a))
	fmt.Printf("%v\n", util.StringArray(g.St.Dump()))
	dumpT1(g)
}
Exemplo n.º 3
0
// pd.DecodedStream() returns decoded contents of a stream.
func (pd *PdfReaderT) DecodedStream(reference []byte) (DictionaryT, []byte) {
	dic, data := pd.Stream(reference)
	if f, ok := dic["/Filter"]; ok {
		filter := pd.ForcedArray(f)
		var decos [][]byte
		if d, ok := dic["/DecodeParams"]; ok {
			decos = pd.ForcedArray(d)
		} else {
			decos = make([][]byte, len(filter))
		}
		for ff := range filter {
			deco := pd.Dic(decos[ff])
			switch string(filter[ff]) {
			case "/FlateDecode":
				data = fancy.ReadAndClose(zlib.NewInflater(fancy.SliceReader(data)))
			case "/LZWDecode":
				early := true
				if deco != nil {
					if s, ok := deco["/EarlyChange"]; ok {
						early = pd.Num(s) == 1
					}
				}
				data = lzw.Decode(data, early)
			case "/ASCII85Decode":
				ds := data
				for len(ds) > 1 && ds[len(ds)-1] < 33 {
					ds = ds[0 : len(ds)-1]
				}
				if len(ds) >= 2 && ds[len(ds)-1] == '>' && ds[len(ds)-2] == '~' {
					ds = ds[0 : len(ds)-2]
				}
				data = fancy.ReadAll(ascii85.NewDecoder(fancy.SliceReader(ds)))
			case "/ASCIIHexDecode":
				data = hex.Decode(string(data))
			default:
				data = []byte{}
			}
		}
	}
	return dic, data
}
Exemplo n.º 4
0
// Array() extracts an array from PDF data.
func Array(s []byte) [][]byte {
	if len(s) < 2 || s[0] != '[' || s[len(s)-1] != ']' {
		return nil
	}
	rdr := fancy.SliceReader(s[1 : len(s)-1])
	r := make([][]byte, MAX_PDF_ARRAYSIZE)
	b := 0
	for {
		r[b], _ = refToken(rdr)
		if len(r[b]) == 0 {
			break
		}
		b++
	}
	if b == 0 {
		return nil
	}
	return r[0:b]
}
Exemplo n.º 5
0
func (t *SvgTextT) cmap(font string) (r *cmapi.CharMapperT) {
	var ok bool
	if r, ok = t.cmaps[font]; ok {
		return
	}
	r = cm_identity // setup default
	if t.fonts == nil {
		t.fonts = t.Pdf.PageFonts(t.Pdf.Pages()[t.Page])
		if t.fonts == nil {
			return
		}
	}
	if dr, ok := t.fonts[font]; ok {
		d := t.Pdf.Dic(dr)
		if tu, ok := d["/ToUnicode"]; ok {
			_, cm := t.Pdf.DecodedStream(tu)
			r = cmapi.Read(fancy.SliceReader(cm))
			t.cmaps[font] = r
		}
	}
	return
}
Exemplo n.º 6
0
// Dictionary() makes a map/hash from PDF dictionary data.
func Dictionary(s []byte) DictionaryT {
	if len(s) < 4 {
		return nil
	}
	e := len(s) - 1
	if s[0] != s[1] || s[0] != '<' || s[e] != s[e-1] || s[e] != '>' {
		return nil
	}
	r := make(DictionaryT)
	rdr := fancy.SliceReader(s[2 : e-1])
	for {
		t, _ := ps.Token(rdr)
		if len(t) == 0 {
			break
		}
		if t[0] != '/' {
			return nil
		}
		k := string(t)
		t, _ = refToken(rdr)
		r[k] = t
	}
	return r
}