Пример #1
0
func glyphDiffs(cp1, cp2 codepage.CodepageIndex, first, last int) (diffs array) {
	map1, map2 := cp1.Map(), cp2.Map()
	same := true
	for i := first; i <= last; i++ {
		if same {
			if map1[i] != map2[i] {
				same = false
				r := afm.RuneGlyphs[map2[i]]
				if r == "" {
					r = "question"
				}
				diffs = append(diffs, integer(i), name(r))
			}
		} else {
			if map1[i] == map2[i] {
				same = true
			} else {
				r := afm.RuneGlyphs[map2[i]]
				if r == "" {
					r = "question"
				}
				diffs = append(diffs, name(r))
			}
		}
	}
	return
}
Пример #2
0
func (piece *RichText) EachCodepage(fn func(cpi codepage.CodepageIndex, text string, p *RichText)) {
	piece.VisitAll(func(p *RichText) {
		if p == nil {
			panic("Whoa there!")
		}
		var lastIdx codepage.CodepageIndex = -1
		start := 0
		for i, r := range p.Text {
			if lastIdx >= 0 {
				if _, found := lastIdx.Codepage().CharForCodepoint(r); found {
					continue
				} else {
					if i > start {
						fn(lastIdx, p.Text[start:i], p)
						start = i
					}
					lastIdx, _ = codepage.IndexForCodepoint(r)
				}
			} else {
				if idx, found := codepage.IndexForCodepoint(r); found {
					if i > start {
						fn(lastIdx, p.Text[start:i], p)
						start = i
					}
					lastIdx = idx
				}
			}
		}
		if start < len(p.Text) {
			fn(lastIdx, p.Text[start:], p)
		}
	})
}
Пример #3
0
func (dw *DocWriter) fontKey(f *Font, cpi codepage.CodepageIndex) string {
	if f == nil {
		panic("fontKey: No font specified.")
	}
	if f.metrics == nil {
		panic("fontKey: font missing metrics.")
	}
	name := fmt.Sprintf("%s/%s-%s", f.metrics.PostScriptName(), cpi, f.subType)
	if key, ok := dw.fontKeys[name]; ok {
		return key
	}
	descriptor := newFontDescriptor(
		dw.nextSeq(), 0,
		f.metrics.PostScriptName(), f.metrics.Family(),
		f.metrics.Flags(),
		f.metrics.BoundingBox(),
		0, // missingWidth
		f.metrics.StemV(),
		0, // stemH
		f.metrics.ItalicAngle(),
		f.metrics.CapHeight(),
		f.metrics.XHeight(),
		f.metrics.Ascent(),
		f.metrics.Descent(),
		f.metrics.Leading(),
		0, 0) // maxWidth, avgWidth
	dw.file.body.add(descriptor)
	key := fmt.Sprintf("F%d", len(dw.fontKeys))
	dw.fontKeys[name] = key
	widths := dw.widthsForFontCodepage(f, cpi)
	dw.file.body.add(widths)
	var font *simpleFont
	switch f.subType {
	case "Type1":
		encoding, ok := dw.fontEncodings[cpi.String()]
		if !ok {
			differences := glyphDiffs(codepage.Idx_CP1252, cpi, 32, 255)
			encoding = newFontEncoding(dw.nextSeq(), 0, "WinAnsiEncoding", differences)
			dw.file.body.add(encoding)
			dw.fontEncodings[cpi.String()] = encoding
		}
		font = newType1Font(
			dw.nextSeq(), 0,
			f.metrics.PostScriptName(),
			32, 255, widths,
			descriptor, &indirectObjectRef{encoding})
	case "TrueType":
		font = newTrueTypeFont(
			dw.nextSeq(), 0,
			f.metrics.PostScriptName(),
			32, 255, widths,
			descriptor)
	}
	dw.file.body.add(font)
	dw.resources.fonts[key] = &indirectObjectRef{font}
	return key
}
Пример #4
0
func (dw *DocWriter) widthsForFontCodepage(f *Font, cpi codepage.CodepageIndex) *indirectObject {
	var widths [256]int
	upm := f.metrics.UnitsPerEm()
	// Avoid divide by zero error for unusual fonts.
	if upm > 0 {
		for i, r := range cpi.Map() {
			designWidth, _ := f.metrics.AdvanceWidth(r)
			widths[i] = designWidth * 1000 / upm
		}
	}
	pdfWidths := arrayFromInts(widths[32:])
	ioWidths := &indirectObject{dw.nextSeq(), 0, &pdfWidths}
	return ioWidths
}