pngtext

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 8 Imported by: 1

README

go-pngtext

Go library GoDoc

Parse textual data from a PNG file.

Usage

This code opens a file image.png and prints all textual data stored in the file:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/jkawamoto/go-pngtext"
)

func main() {
	r, err := os.Open("image.png")
	if err != nil {
		log.Fatalf("failed to open a file: %v", err)
	}
	defer func() {
		if err := r.Close(); err != nil {
			log.Fatalf("failed to close a file: %v", err)
		}
	}()

	res, err := pngtext.ParseTextualData(r)
	if err != nil {
		log.Fatalf("failed to parse textual data: %v", err)
	}

	for _, v:= range res{
        fmt.Printf("%v: %v\n", v.Keyword, v.Text)	
    }
}

License

This software is released under the MIT License, see LICENSE.

Documentation

Overview

Package pngtext provides function ParseTextualData that parses a PNG file and returns a list of textual data stored in the file.

To parse textual data, simply call ParseTextualData:

r, _ := os.Open("test.png")
defer r.Close()

res, _ := pngtext.ParseTextualData(r)

TextualDataList, which ParseTextualData returns as a result, is a list of TextualData but also implements Find function that helps you to get text data associated with a keyword. This returns TextualData of which keyword is Description:

res.Find("Description")

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotPngData                 = fmt.Errorf("not PNG data")
	ErrUnsupportedCompressionType = fmt.Errorf("unsupported compression type")
	ErrCRC                        = fmt.Errorf("CRC doesn't match")
)

Functions

This section is empty.

Types

type TextualData

type TextualData struct {
	// Keyword of the textual data.
	Keyword string
	// Text string associated with the keyword
	Text string
	// LanguageTag indicates the human language used by the translated keyword and the text.
	// Only iTXt chunk has this attribute.
	LanguageTag string
	// TranslatedKeyword is a translation of the keyword into the language indicated by the language tag.
	// Only iTXt chunk has this attribute.
	TranslatedKeyword string
}

TextualData defines attributes one chunk could have. See also: https://www.w3.org/TR/2003/REC-PNG-20031110/#11textinfo

type TextualDataList

type TextualDataList []*TextualData

TextualDataList is a list of *TextualData that provides Find and implements sort.Interface.

func ParseTextualData

func ParseTextualData(r io.Reader) (TextualDataList, error)

ParseTextualData reads PNG data from the given reader and parses textual data.

Example
r, _ := os.Open("test.png")
defer r.Close()

res, _ := pngtext.ParseTextualData(r)
fmt.Println(res.Find("Text").Text) 
Output:

text data

func (TextualDataList) Find

func (list TextualDataList) Find(keyword string) *TextualData

Find sequentially searches an item of which keyword matches the given one. Returns nil if not matches any item.

func (TextualDataList) Len

func (list TextualDataList) Len() int

Len is the number of elements in the collection.

func (TextualDataList) Less

func (list TextualDataList) Less(i, j int) bool

Less reports whether the element with index i must sort before the element with index j.

func (TextualDataList) Swap

func (list TextualDataList) Swap(i, j int)

Swap swaps the elements with indexes i and j.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL