2f043493c0db5be7fdd8f08f3d291bf63304b213
[yaffs-website] / vendor / lsolesen / pel / src / PelEntryWindowsString.php
1 <?php
2
3 /**
4  * PEL: PHP Exif Library.
5  * A library with support for reading and
6  * writing all Exif headers in JPEG and TIFF images using PHP.
7  *
8  * Copyright (C) 2004, 2005, 2006, 2007 Martin Geisler.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program in the file COPYING; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301 USA
24  */
25 namespace lsolesen\pel;
26
27 /**
28  * Classes used to hold bytes, both signed and unsigned.
29  * The {@link
30  * PelEntryWindowsString} class is used to manipulate strings in the
31  * format Windows XP needs.
32  *
33  * @author Martin Geisler <mgeisler@users.sourceforge.net>
34  * @license http://www.gnu.org/licenses/gpl.html GNU General Public
35  *          License (GPL)
36  * @package PEL
37  */
38
39 /**
40  * Class used to manipulate strings in the format Windows XP uses.
41  *
42  * When examining the file properties of an image in Windows XP one
43  * can fill in title, comment, author, keyword, and subject fields.
44  * Filling those fields and pressing OK will result in the data being
45  * written into the Exif data in the image.
46  *
47  * The data is written in a non-standard format and can thus not be
48  * loaded directly --- this class is needed to translate it into
49  * normal strings.
50  *
51  * It is important that entries from this class are only created with
52  * the {@link PelTag::XP_TITLE}, {@link PelTag::XP_COMMENT}, {@link
53  * PelTag::XP_AUTHOR}, {@link PelTag::XP_KEYWORD}, and {@link
54  * PelTag::XP_SUBJECT} tags. If another tag is used the data will no
55  * longer be correctly decoded when reloaded with PEL. (The data will
56  * be loaded as an {@link PelEntryByte} entry, which isn't as useful.)
57  *
58  * This class is to be used as in
59  * <code>
60  * $title = $ifd->getEntry(PelTag::XP_TITLE);
61  * print($title->getValue());
62  * $title->setValue('My favorite cat');
63  * </code>
64  * or if no entry is present one can add a new one with
65  * <code>
66  * $title = new PelEntryWindowsString(PelTag::XP_TITLE, 'A cute dog.');
67  * $ifd->addEntry($title);
68  * </code>
69  *
70  * @author Martin Geisler <mgeisler@users.sourceforge.net>
71  * @package PEL
72  */
73 class PelEntryWindowsString extends PelEntry
74 {
75
76     /**
77      * The string hold by this entry.
78      *
79      * This is the string that was given to the {@link __construct
80      * constructor} or later to {@link setValue}, without any extra NULL
81      * characters or any such nonsense.
82      *
83      * @var string
84      */
85     private $str;
86
87     /**
88      * Make a new PelEntry that can hold a Windows XP specific string.
89      *
90      * @param int $tag
91      *            the tag which this entry represents. This should be
92      *            one of {@link PelTag::XP_TITLE}, {@link PelTag::XP_COMMENT},
93      *            {@link PelTag::XP_AUTHOR}, {@link PelTag::XP_KEYWORD}, and {@link
94      *            PelTag::XP_SUBJECT} tags. If another tag is used, then this
95      *            entry will be incorrectly reloaded as a {@link PelEntryByte}.
96      *
97      * @param string $str
98      *            the string that this entry will represent. It will
99      *            be passed to {@link setValue} and thus has to obey its
100      *            requirements.
101      */
102     public function __construct($tag, $str = '')
103     {
104         $this->tag = $tag;
105         $this->format = PelFormat::BYTE;
106         $this->setValue($str);
107     }
108
109     /**
110      * Give the entry a new value.
111      *
112      * This will overwrite the previous value. The value can be
113      * retrieved later with the {@link getValue} method.
114      *
115      * @param string $str
116      *            the new value of the entry. This should be use the
117      *            Latin-1 encoding and be given without any extra NULL characters.
118      */
119     public function setValue($str)
120     {
121         $l = strlen($str);
122
123         $this->components = 2 * ($l + 1);
124         $this->str = $str;
125         $this->bytes = '';
126         for ($i = 0; $i < $l; $i ++) {
127             $this->bytes .= $str{$i} . chr(0x00);
128         }
129
130         $this->bytes .= chr(0x00) . chr(0x00);
131     }
132
133     /**
134      * Return the string of the entry.
135      *
136      * @return string the string held, without any extra NULL
137      *         characters. The string will be the same as the one given to
138      *         {@link setValue} or to the {@link __construct constructor}.
139      */
140     public function getValue()
141     {
142         return $this->str;
143     }
144
145     /**
146      * Return the string of the entry.
147      *
148      * This methods returns the same as {@link getValue}.
149      *
150      * @param boolean $brief
151      *            not used.
152      *
153      * @return string the string held, without any extra NULL
154      *         characters. The string will be the same as the one given to
155      *         {@link setValue} or to the {@link __construct constructor}.
156      */
157     public function getText($brief = false)
158     {
159         return $this->str;
160     }
161 }