diff options
author | Pale Moon <git-repo@palemoon.org> | 2015-12-15 20:46:39 +0100 |
---|---|---|
committer | Pale Moon <git-repo@palemoon.org> | 2015-12-15 20:46:39 +0100 |
commit | f81490b7b65bfee8f0ed27aa3f4542a52db59d31 (patch) | |
tree | bc8ca89cf4fed09df6a9eaa8d2493239e919784e | |
parent | e812aaae23b067a290dca0c94d5f672955584953 (diff) | |
download | palemoon-gre-f81490b7b65bfee8f0ed27aa3f4542a52db59d31.tar.gz |
Update graphite library to 1.3.4
85 files changed, 8374 insertions, 4818 deletions
diff --git a/gfx/graphite2/ChangeLog b/gfx/graphite2/ChangeLog new file mode 100644 index 000000000..2d5a0c113 --- /dev/null +++ b/gfx/graphite2/ChangeLog @@ -0,0 +1,181 @@ +1.3.4 + . Transition from Mercurial to Git + . Bug fixes + . Fix Collision Kerning ignoring some diacritics + . Handle pass bits 16-31 to speed up fonts with > 16 passes + . Various minor fuzz bug fixes + . Make Coverity happy + . Add GR_FALLTHROUGH macro for clang c++11 + +1.3.3 + . Slight speed up in Collision Avoidance + . Remove dead bidi code + . Bug fixes + . Between pass bidi reorderings and at the end + . Decompressor fuzz bugs + . Other fuzz bugs + +1.3.2 + . Remove full bidi. All segments are assumed to be single directioned. + . Bug fixes: + . Decompressor corner cases + . Various fuzz bugs + +1.3.1 + . Deprecation warning: Full bidi support is about to be deprecated. Make contact + if this impacts you. + . Change compression block format slightly to conform to LZ4 + . Bug fixes: + . Handle mono direction text with diacritics consistently. Fonts + now see the direction they expect consistently and bidi now + gives expected results. + . Fixed lots of fuzz bugs + . Coverity cleanups + . Build now works for clang and/or asan and/or afl etc. + +1.3.0 + . Add collision avoidance + . Shift Collider + . Kern Collider + . Octabox outlines and subboxes + . Add compressed Silf and Glat table support + . Bug fixes: + . Stop loops forming in the child, sibling tree + . Handle bidi mirroring correctly if no bidi occurring + +1.2.4 + . Face failure now has error code reporting via debug logging + . can now call gr_start_logging(NULL, fname) + . gr2fonttest --alltrace added + . Format 14 table support + . Not done. To be handled entirely in the compiler + . Bidi support for Unicode 6.3 Isolating direction controls + . Fonts no longer require a glyf/loca table. In such cases the bounding box is always 0. + . Clang ASAN build support added for testing. + . Handle out of memory sanely. + . Documentation improvements + . Bug fixes: + . Enforce fonts having to store glyph attributes by monotonically increasing attribute number + . zeropadding was not getting called on feature tags + . automatic associations for unassociated characters + . use direct engine on Mac + . various extreme case reading 1 past the end errors fixed + . remove tabs from sources so that it becomes readable again + +1.2.3 + . Bug fixes only: + . fix byte swapping when testing cmap subtable lengths + . work around armel compilation problems with conditional operators + . fix pseudoglyph support for advance and bbox + +1.2.2 + . Add support for passKeySlot (makes Charis 2x faster) up to 32 passes + . Add telemetry output to json if enabled in build GRAPHITE2_TELEMETRY + . Shrink font memory footprint particularly in the fsm + . Add -S to comparerenderer + . Bug fixes: + . Fix shift.x being reversed for rtl text + . Fix faulty fallback justification + . Fix bad cmap handling + . Support compiling on old Solaris where bidi attributes clash with register names + . Follow the crowd in using Windows.h + +1.2.1 + . Bug fixes: + . Allow glyph reattachment + . Allow signed glyph attributes + . Various build problems with MacOS, old gcc versions, etc. + . Various overrun read errors fixed + +1.2.0 + . API Changes: + . Added Windows friendly gr_start_logging and gr_stop_logging, now per face + . Added gr_make_face_with_ops, gr_make_face_with_seg_cache_and_ops + . Added gr_make_font_with_ops + . Added gr_face_is_char_supported + . Added gr_face_info to give info to apps about face capabilities + . Deprecated gr_make_face, gr_make_face_with_seg_cache, gr_make_font_with_advance_fn + . Deprecated graphite_start_logging and graphite_stop_logging + . These functions are stubbed now and do nothing, but do compile and link. + . Bump API version to 3 + . Add C# wrapper to contrib + . Handle justification information in a font and do something useful with it + . Builds clang clean (has done for a while) + . Bug fixes + . Windows build and bug fixes + . Add extra information to json debug output + . Added windows build documentation + . Added freetype sample code and test + +1.1.3 + . Default build has GRAPHITE2_COMPARE_RENDERER to OFF to reduce dependencies + . Builds on Mac with clang + . Debug output improvements + . Tidy up perl wrappers + . Fuzz tester improvements + . Various bug fixes for bad font handling + +1.1.2 + . Support feature ids < 4 chars when space padded for inclusion in FF 14. + . More fuzztesting and removal of causes of valgrind bad reads and sigabrts + . Remove contrib/android into its own repo (http://hg.palaso.org/grandroid) + . Update comparerenderer to latest harfbuzzng api + +1.1.1 + . Missing Log.h included + . perl wrappers updated + +1.1.0 + . Refactored debug output to use json + . Renamed VM_MACHINE_TYPE to GRAPHITE2_VM_TYPE + . Renamed DISABLE_SEGCACHE to GRAPHITE2_NSEGCACE + . Renamed DISBALE_FILE_FACE to GRAPHITE2_NFILEFACE + . Renamed ENABLE_COMPARE_RENDERER to GRAPHTIE2_COMPARE_RENDERER + . Renamed DOXYGEN_CONFIG to GRAPHITE2_DOXYGEN_CONFIG + . Renamed GR2_CUSTOM_HEADER to GRAPHITE2_CUSTOM_HEADER + . Renamed GR2_EXPORTING to GRAPHITE2_EXPORTING + . Added GRAPHITE2_STATIC for static only builds + . Added GRAPHITE2_NTRACING to compile out tracing code + . Documented GRAPHITE2_{EXPORTING,STATIC,NTRACING} in hacking.txt + . Bump libtool version to 2.1.0 + . dumb font rendering works + . slot user attributes are now signed rather than unsigned + . add support for long class maps + . Rename perl library to avoid nameclash on Windows + . Various robustness fixes + . Moved internal .h files into src/inc + . Parallelise fuzztest + . General build improvements, particularly on Windows + +1.0.3 + . Fix UTF16 surrogate support + . script and lang tags may be space padded or null padded + . Remove need for WORDS_BIGENDIAN, do it all automatically + . Remove all #include <new>. Use CLASS_NEW_DELETE instead. + . Fix comparerenderer to work with current hbng + . Add valgrind to fuzztest to ensure good memory use at all times + . Fix new fuzztest exposed bugs. + . Fix bugs exposed by Mozilla security review + . Add continuous integration build on Windows support + +1.0.2 + . Fix Windows build + . Comparerenderer uses hbng enforcing ot rendering + . Add Bidi .hasChar support and refactor mirroring code + . Make cmake default Release rather than debug + . Don't compile in a boat load of TtfUtil that isn't used, saving 15% of binary + . Chase the FSF around its latest office moves + . WORDS_BIGENDIAN is set at the top so tests now pass on ppc, etc. + . More words in the manual + +1.0.1 + . Release is the default build in cmake now. + . Refactor cmake build to not rebuild things so much. + . Include a missing file + . Remove -nostdlibs, making gcc happy everywhere + . Update comparerenderer to latest hbng interface + . Add changelog + +1.0.0 + . First major release of perfect code! + diff --git a/gfx/graphite2/LICENSE b/gfx/graphite2/LICENSE new file mode 100644 index 000000000..2d2d780e6 --- /dev/null +++ b/gfx/graphite2/LICENSE @@ -0,0 +1,510 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations +below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it +becomes a de-facto standard. To achieve this, non-free programs must +be allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control +compilation and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at least + three years, to give the same user the materials specified in + Subsection 6a, above, for a charge no more than the cost of + performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply, and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License +may add an explicit geographical distribution limitation excluding those +countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms +of the ordinary General Public License). + + To apply these terms, attach the following notices to the library. +It is safest to attach them to the start of each source file to most +effectively convey the exclusion of warranty; and each file should +have at least the "copyright" line and a pointer to where the full +notice is found. + + + <one line to give the library's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or +your school, if any, to sign a "copyright disclaimer" for the library, +if necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James + Random Hacker. + + <signature of Ty Coon>, 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/gfx/graphite2/README.mcp b/gfx/graphite2/README.mcp new file mode 100644 index 000000000..3440e1f2d --- /dev/null +++ b/gfx/graphite2/README.mcp @@ -0,0 +1,12 @@ +This directory contains the Graphite2 library from https://github.com/silnrsi/graphite/ + +Current version derived from upstream release version 1.3.4 + + +Note about updating: +The .sh script in this directory is defunct, but kept in place as a guide to follow manually. + +Most notably: + * <cstdio> needs to be replaced with <stdio.h> in *.cpp/*.h #includes + * Windows.h needs to be replaced with windows.h (caps) -- currently not an issue. +
\ No newline at end of file diff --git a/gfx/graphite2/README.md b/gfx/graphite2/README.md new file mode 100644 index 000000000..2c4ecf58d --- /dev/null +++ b/gfx/graphite2/README.md @@ -0,0 +1,32 @@ +# Graphite engine + +## What is Graphite? + +Graphite is a system that can be used to create “smart fonts” capable of displaying writing systems with various complex behaviors. A smart font contains not only letter shapes but also additional instructions indicating how to combine and position the letters in complex ways. + +Graphite was primarily developed to provide the flexibility needed for minority languages which often need to be written according to slightly different rules than well-known languages that use the same script. + +Examples of complex script behaviors Graphite can handle include: + +* contextual shaping +* ligatures +* reordering +* split glyphs +* bidirectionality +* stacking diacritics +* complex positioning +* shape aware kerning +* automatic diacritic collision avoidance + +See [examples of scripts with complex rendering](http://scripts.sil.org/CmplxRndExamples). + +## Graphite system overview +The Graphite system consists of: + +* A rule-based programming language [Graphite Description Language](http://scripts.sil.org/cms/scripts/page.php?site_id=projects&item_id=graphite_devFont#gdl) (GDL) that can be used to describe the behavior of a writing system +* A compiler for that language +* A rendering engine that can serve as the layout component of a text-processing application + +Graphite renders TrueType fonts that have been extended by means of compiling a GDL program. + +Further technical information is available on the [Graphite technical overview](http://scripts.sil.org/cms/scripts/page.php?site_id=projects&item_id=graphite_techAbout) page. diff --git a/gfx/graphite2/README.mozilla b/gfx/graphite2/README.mozilla deleted file mode 100644 index 33b1ceee3..000000000 --- a/gfx/graphite2/README.mozilla +++ /dev/null @@ -1,6 +0,0 @@ -This directory contains the Graphite2 library from http://hg.palaso.org/graphitedev - -Current version derived from upstream changeset 6b8ddb7d599f - -See gfx/graphite2/moz-gr-update.sh for update procedure. - diff --git a/gfx/graphite2/include/graphite2/Font.h b/gfx/graphite2/include/graphite2/Font.h index 85ee73d91..e5b9aec2a 100644 --- a/gfx/graphite2/include/graphite2/Font.h +++ b/gfx/graphite2/include/graphite2/Font.h @@ -29,8 +29,8 @@ #include "graphite2/Types.h" #define GR2_VERSION_MAJOR 1 -#define GR2_VERSION_MINOR 2 -#define GR2_VERSION_BUGFIX 3 +#define GR2_VERSION_MINOR 3 +#define GR2_VERSION_BUGFIX 4 #ifdef __cplusplus extern "C" @@ -296,7 +296,7 @@ typedef struct gr_font_ops gr_font_ops; * @param appFontHandle font specific information that must stay alive as long * as the font does * @param font_ops pointer font specific callback structure for hinted metrics. - * Must stay alive for the duration of the call. + * Need only stay alive for the duration of the call. * @param face the face this font corresponds to. Must stay alive as long as * the font does. */ @@ -310,7 +310,6 @@ GR2_API gr_font* gr_make_font_with_ops(float ppm, const void* appFontHandle, con * @param appFontHandle font specific information that must stay alive as long * as the font does * @param getAdvance callback function reference that returns horizontal advance in pixels for a glyph. - * Must stay alive for the duration of the call. * @param face the face this font corresponds to. Must stay alive as long as * the font does. */ diff --git a/gfx/graphite2/include/graphite2/Segment.h b/gfx/graphite2/include/graphite2/Segment.h index 580f83763..0c3c49a3a 100644 --- a/gfx/graphite2/include/graphite2/Segment.h +++ b/gfx/graphite2/include/graphite2/Segment.h @@ -120,11 +120,45 @@ enum gr_attrCode { /// Justification weight for this glyph (not implemented) gr_slatJWeight, /// Amount this slot mush shrink or stretch in design units - gr_slatJWidth, + gr_slatJWidth = 29, /// SubSegment split point gr_slatSegSplit = gr_slatJStretch + 29, /// User defined attribute, see subattr for user attr number gr_slatUserDefn, + /// Bidi level + gr_slatBidiLevel = 56, + /// Collision flags + gr_slatColFlags, + /// Collision constraint rectangle left (bl.x) + gr_slatColLimitblx, + /// Collision constraint rectangle lower (bl.y) + gr_slatColLimitbly, + /// Collision constraint rectangle right (tr.x) + gr_slatColLimittrx, + /// Collision constraint rectangle upper (tr.y) + gr_slatColLimittry, + /// Collision shift x + gr_slatColShiftx, + /// Collision shift y + gr_slatColShifty, + /// Collision margin + gr_slatColMargin, + /// Margin cost weight + gr_slatColMarginWt, + // Additional glyph that excludes movement near this one: + gr_slatColExclGlyph, + gr_slatColExclOffx, + gr_slatColExclOffy, + // Collision sequence enforcing attributes: + gr_slatSeqClass, + gr_slatSeqProxClass, + gr_slatSeqOrder, + gr_slatSeqAboveXoff, + gr_slatSeqAboveWt, + gr_slatSeqBelowXlim, + gr_slatSeqBelowWt, + gr_slatSeqValignHt, + gr_slatSeqValignWt, /// not implemented gr_slatMax, @@ -136,7 +170,8 @@ enum gr_bidirtl { /// Underlying paragraph direction is RTL gr_rtl = 1, /// Set this to not run the bidi pass internally, even if the font asks for it. - /// This presumes that the segment is in a single direction. + /// This presumes that the segment is in a single direction. Most of the time + /// this bit should be set unless you know you are passing full paragraphs of text. gr_nobidi = 2, /// Disable auto mirroring for rtl text gr_nomirror = 4 @@ -327,8 +362,8 @@ GR2_API const gr_slot* gr_slot_first_attachment(const gr_slot* p); * * This returns the next slot in the singly linked list of slots attached to this * slot's parent. If there are no more such slots, NULL is returned. If there is - * no parent, i.e. the passed slot is a base, then the next base in graphical order - * (ltr even for rtl text) is returned. + * no parent, i.e. the passed slot is a cluster base, then the next cluster base + * in graphical order (ltr, even for rtl text) is returned. * * if gr_slot_next_sibling_attachment(p) != NULL then gr_slot_attached_to(gr_slot_next_sibling_attachment(p)) == gr_slot_attached_to(p). */ @@ -398,7 +433,13 @@ GR2_API unsigned int gr_slot_index(const gr_slot* p/*not NULL*/); */ GR2_API int gr_slot_attr(const gr_slot* p/*not NULL*/, const gr_segment* pSeg/*not NULL*/, enum gr_attrCode index, gr_uint8 subindex); //tbd - do we need to expose this? -/** Returns whether text may be inserted before this glyph [check this isn't inverted] **/ +/** Returns whether text may be inserted before this glyph. + * + * This indicates whether a cursor can be put before this slot. It applies to + * base glyphs that have no parent as well as attached glyphs that have the + * .insert attribute explicitly set to true. This is the primary mechanism + * for identifying contiguous sequences of base plus diacritics. + */ GR2_API int gr_slot_can_insert_before(const gr_slot* p); /** Returns the original gr_char_info index this slot refers to. diff --git a/gfx/graphite2/include/graphite2/Types.h b/gfx/graphite2/include/graphite2/Types.h index 268bee7a9..e8e45c1ff 100644 --- a/gfx/graphite2/include/graphite2/Types.h +++ b/gfx/graphite2/include/graphite2/Types.h @@ -58,12 +58,15 @@ enum gr_encform { #endif #endif #define GR2_LOCAL -#else - #if __GNUC__ >= 4 - #define GR2_API __attribute__ ((visibility("default"))) - #define GR2_LOCAL __attribute__ ((visibility("hidden"))) +#elif __GNUC__ >= 4 + #if defined GRAPHITE2_STATIC + #define GR2_API __attribute__ ((visibility("hidden"))) #else - #define GR2_API - #define GR2_LOCAL + #define GR2_API __attribute__ ((visibility("default"))) #endif + #define GR2_LOCAL __attribute__ ((visibility("hidden"))) +#else + #define GR2_API + #define GR2_LOCAL #endif + diff --git a/gfx/graphite2/include/graphite2/XmlLog.h b/gfx/graphite2/include/graphite2/XmlLog.h deleted file mode 100644 index 554132756..000000000 --- a/gfx/graphite2/include/graphite2/XmlLog.h +++ /dev/null @@ -1,55 +0,0 @@ -/* GRAPHITE2 LICENSING - - Copyright 2010, SIL International - All rights reserved. - - This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2.1 of License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should also have received a copy of the GNU Lesser General Public - License along with this library in the file named "LICENSE". - If not, write to the Free Software Foundation, 51 Franklin Street, - Suite 500, Boston, MA 02110-1335, USA or visit their web page on the - internet at http://www.fsf.org/licenses/lgpl.html. - - Alternatively, the contents of this file may be used under the terms - of the Mozilla Public License (http://mozilla.org/MPL) or the GNU - General Public License, as published by the Free Software Foundation, - either version 2 of the License or (at your option) any later version. -*/ -#pragma once - -#include <graphite2/Types.h> -#include <stdio.h> - -typedef enum { - GRLOG_NONE = 0x0, - GRLOG_FACE = 0x01, - GRLOG_SEGMENT = 0x02, - GRLOG_PASS = 0x04, - GRLOG_CACHE = 0x08, - - GRLOG_OPCODE = 0x80, - GRLOG_ALL = 0xFF -} GrLogMask; - -// If startGraphiteLogging returns true, logging is enabled and the FILE handle -// will be closed by graphite when stopGraphiteLogging is called. -#ifdef __cplusplus -extern "C" -{ -#endif - -GR2_API bool graphite_start_logging(FILE * logFile, GrLogMask mask); //may not do anthing if disabled in the implementation of the engine. -GR2_API void graphite_stop_logging(); - -#ifdef __cplusplus -} -#endif diff --git a/gfx/graphite2/src/Bidi.cpp b/gfx/graphite2/src/Bidi.cpp deleted file mode 100644 index c97eeb173..000000000 --- a/gfx/graphite2/src/Bidi.cpp +++ /dev/null @@ -1,578 +0,0 @@ -/* GRAPHITE2 LICENSING - - Copyright 2010, SIL International - All rights reserved. - - This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2.1 of License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should also have received a copy of the GNU Lesser General Public - License along with this library in the file named "LICENSE". - If not, write to the Free Software Foundation, 51 Franklin Street, - suite 500, Boston, MA 02110-1335, USA or visit their web page on the - internet at http://www.fsf.org/licenses/lgpl.html. - -Alternatively, the contents of this file may be used under the terms of the -Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public -License, as published by the Free Software Foundation, either version 2 -of the License or (at your option) any later version. -*/ -#include "inc/Main.h" -#include "inc/Slot.h" -#include "inc/Segment.h" - -using namespace graphite2; - -enum DirCode { // Hungarian: dirc - Unk = -1, - N = 0, // other neutrals (default) - ON - L = 1, // left-to-right, strong - L - R = 2, // right-to-left, strong - R - AL = 3, // Arabic letter, right-to-left, strong, AR - EN = 4, // European number, left-to-right, weak - EN - EUS = 5, // European separator, left-to-right, weak - ES - ET = 6, // European number terminator, left-to-right, weak - ET - AN = 7, // Arabic number, left-to-right, weak - AN - CUS = 8, // Common number separator, left-to-right, weak - CS - WS = 9, // white space, neutral - WS - BN = 10, // boundary neutral - BN - - LRO = 11, // LTR override - RLO = 12, // RTL override - LRE = 13, // LTR embedding - RLE = 14, // RTL embedding - PDF = 15, // pop directional format - NSM = 16, // non-space mark - - ON = N -}; - -enum DirMask { - Nmask = 1, - Lmask = 2, - Rmask = 4, - ALmask = 8, - ENmask = 0x10, - ESmask = 0x20, - ETmask = 0x40, - ANmask = 0x80, - CSmask = 0x100, - WSmask = 0x200, - BNmask = 0x400, - LROmask = 0x800, - RLOmask = 0x1000, - LREmask = 0x2000, - RLEmask = 0x4000, - PDFmask = 0x8000, - NSMmask = 0x10000 -}; - -unsigned int bidi_class_map[] = { 0, 1, 2, 5, 4, 8, 9, 3, 7, 0, 0, 0, 0, 0, 0, 0, 6 }; -// Algorithms based on Unicode reference standard code. Thanks Asmus Freitag. -#define MAX_LEVEL 61 - -Slot *resolveExplicit(int level, int dir, Slot *s, int nNest = 0) -{ - int nLastValid = nNest; - Slot *res = NULL; - for ( ; s && !res; s = s->next()) - { - int cls = s->getBidiClass(); - switch(cls) - { - case LRO: - case LRE: - nNest++; - if (level & 1) - s->setBidiLevel(level + 1); - else - s->setBidiLevel(level + 2); - if (s->getBidiLevel() > MAX_LEVEL) - s->setBidiLevel(level); - else - { - s = resolveExplicit(s->getBidiLevel(), (cls == LRE ? N : L), s->next(), nNest); - nNest--; - if (s) continue; else break; - } - cls = BN; - s->setBidiClass(cls); - break; - - case RLO: - case RLE: - nNest++; - if (level & 1) - s->setBidiLevel(level + 2); - else - s->setBidiLevel(level + 1); - if (s->getBidiLevel() > MAX_LEVEL) - s->setBidiLevel(level); - else - { - s = resolveExplicit(s->getBidiLevel(), (cls == RLE ? N : R), s->next(), nNest); - nNest--; - if (s) continue; else break; - } - cls = BN; - s->setBidiClass(cls); - break; - - case PDF: - cls = BN; - s->setBidiClass(cls); - if (nNest) - { - if (nLastValid < nNest) - --nNest; - else - res = s; - } - break; - } - - if (dir != N) - cls = dir; - if (s) - { - s->setBidiLevel(level); - if (s->getBidiClass() != BN) - s->setBidiClass(cls); - } - else - break; - } - return res; -} - -// === RESOLVE WEAK TYPES ================================================ - -enum bidi_state // possible states -{ - xa, // arabic letter - xr, // right leter - xl, // left letter - - ao, // arabic lett. foll by ON - ro, // right lett. foll by ON - lo, // left lett. foll by ON - - rt, // ET following R - lt, // ET following L - - cn, // EN, AN following AL - ra, // arabic number foll R - re, // european number foll R - la, // arabic number foll L - le, // european number foll L - - ac, // CS following cn - rc, // CS following ra - rs, // CS,ES following re - lc, // CS following la - ls, // CS,ES following le - - ret, // ET following re - let, // ET following le -} ; - -enum bidi_state_mask -{ - xamask = 1, - xrmask = 2, - xlmask = 4, - aomask = 8, - romask = 0x10, - lomask = 0x20, - rtmask = 0x40, - ltmask = 0x80, - cnmask = 0x100, - ramask = 0x200, - remask = 0x400, - lamask = 0x800, - lemask = 0x1000, - acmask = 0x2000, - rcmask = 0x4000, - rsmask = 0x8000, - lcmask = 0x10000, - lsmask = 0x20000, - retmask = 0x40000, - letmask = 0x80000 -}; - -const bidi_state stateWeak[][10] = -{ - // N, L, R, AN, EN, AL,NSM, CS, ES, ET, -{ /*xa*/ ao, xl, xr, cn, cn, xa, xa, ao, ao, ao, /* arabic letter */ }, -{ /*xr*/ ro, xl, xr, ra, re, xa, xr, ro, ro, rt, /* right letter */ }, -{ /*xl*/ lo, xl, xr, la, le, xa, xl, lo, lo, lt, /* left letter */ }, - -{ /*ao*/ ao, xl, xr, cn, cn, xa, ao, ao, ao, ao, /* arabic lett. foll by ON*/ }, -{ /*ro*/ ro, xl, xr, ra, re, xa, ro, ro, ro, rt, /* right lett. foll by ON */ }, -{ /*lo*/ lo, xl, xr, la, le, xa, lo, lo, lo, lt, /* left lett. foll by ON */ }, - -{ /*rt*/ ro, xl, xr, ra, re, xa, rt, ro, ro, rt, /* ET following R */ }, -{ /*lt*/ lo, xl, xr, la, le, xa, lt, lo, lo, lt, /* ET following L */ }, - -{ /*cn*/ ao, xl, xr, cn, cn, xa, cn, ac, ao, ao, /* EN, AN following AL */ }, -{ /*ra*/ ro, xl, xr, ra, re, xa, ra, rc, ro, rt, /* arabic number foll R */ }, -{ /*re*/ ro, xl, xr, ra, re, xa, re, rs, rs,ret, /* european number foll R */ }, -{ /*la*/ lo, xl, xr, la, le, xa, la, lc, lo, lt, /* arabic number foll L */ }, -{ /*le*/ lo, xl, xr, la, le, xa, le, ls, ls,let, /* european number foll L */ }, - -{ /*ac*/ ao, xl, xr, cn, cn, xa, ao, ao, ao, ao, /* CS following cn */ }, -{ /*rc*/ ro, xl, xr, ra, re, xa, ro, ro, ro, rt, /* CS following ra */ }, -{ /*rs*/ ro, xl, xr, ra, re, xa, ro, ro, ro, rt, /* CS,ES following re */ }, -{ /*lc*/ lo, xl, xr, la, le, xa, lo, lo, lo, lt, /* CS following la */ }, -{ /*ls*/ lo, xl, xr, la, le, xa, lo, lo, lo, lt, /* CS,ES following le */ }, - -{ /*ret*/ ro, xl, xr, ra, re, xa,ret, ro, ro,ret, /* ET following re */ }, -{ /*let*/ lo, xl, xr, la, le, xa,let, lo, lo,let, /* ET following le */ }, - - -}; - -enum bidi_action // possible actions -{ - // primitives - IX = 0x100, // increment - XX = 0xF, // no-op - - // actions - xxx = (XX << 4) + XX, // no-op - xIx = IX + xxx, // increment run - xxN = (XX << 4) + ON, // set current to N - xxE = (XX << 4) + EN, // set current to EN - xxA = (XX << 4) + AN, // set current to AN - xxR = (XX << 4) + R, // set current to R - xxL = (XX << 4) + L, // set current to L - Nxx = (ON << 4) + 0xF, // set run to neutral - Axx = (AN << 4) + 0xF, // set run to AN - ExE = (EN << 4) + EN, // set run to EN, set current to EN - NIx = (ON << 4) + 0xF + IX, // set run to N, increment - NxN = (ON << 4) + ON, // set run to N, set current to N - NxR = (ON << 4) + R, // set run to N, set current to R - NxE = (ON << 4) + EN, // set run to N, set current to EN - - AxA = (AN << 4) + AN, // set run to AN, set current to AN - NxL = (ON << 4) + L, // set run to N, set current to L - LxL = (L << 4) + L, // set run to L, set current to L -}; - - -const bidi_action actionWeak[][10] = -{ - // N,.. L, R, AN, EN, AL, NSM, CS,..ES, ET, -{ /*xa*/ xxx, xxx, xxx, xxx, xxA, xxR, xxR, xxN, xxN, xxN, /* arabic letter */ }, -{ /*xr*/ xxx, xxx, xxx, xxx, xxE, xxR, xxR, xxN, xxN, xIx, /* right leter */ }, -{ /*xl*/ xxx, xxx, xxx, xxx, xxL, xxR, xxL, xxN, xxN, xIx, /* left letter */ }, - -{ /*ao*/ xxx, xxx, xxx, xxx, xxA, xxR, xxN, xxN, xxN, xxN, /* arabic lett. foll by ON */ }, -{ /*ro*/ xxx, xxx, xxx, xxx, xxE, xxR, xxN, xxN, xxN, xIx, /* right lett. foll by ON */ }, -{ /*lo*/ xxx, xxx, xxx, xxx, xxL, xxR, xxN, xxN, xxN, xIx, /* left lett. foll by ON */ }, - -{ /*rt*/ Nxx, Nxx, Nxx, Nxx, ExE, NxR, xIx, NxN, NxN, xIx, /* ET following R */ }, -{ /*lt*/ Nxx, Nxx, Nxx, Nxx, LxL, NxR, xIx, NxN, NxN, xIx, /* ET following L */ }, - -{ /*cn*/ xxx, xxx, xxx, xxx, xxA, xxR, xxA, xIx, xxN, xxN, /* EN, AN following AL */ }, -{ /*ra*/ xxx, xxx, xxx, xxx, xxE, xxR, xxA, xIx, xxN, xIx, /* arabic number foll R */ }, -{ /*re*/ xxx, xxx, xxx, xxx, xxE, xxR, xxE, xIx, xIx, xxE, /* european number foll R */ }, -{ /*la*/ xxx, xxx, xxx, xxx, xxL, xxR, xxA, xIx, xxN, xIx, /* arabic number foll L */ }, -{ /*le*/ xxx, xxx, xxx, xxx, xxL, xxR, xxL, xIx, xIx, xxL, /* european number foll L */ }, - -{ /*ac*/ Nxx, Nxx, Nxx, Axx, AxA, NxR, NxN, NxN, NxN, NxN, /* CS following cn */ }, -{ /*rc*/ Nxx, Nxx, Nxx, Axx, NxE, NxR, NxN, NxN, NxN, NIx, /* CS following ra */ }, -{ /*rs*/ Nxx, Nxx, Nxx, Nxx, ExE, NxR, NxN, NxN, NxN, NIx, /* CS,ES following re */ }, -{ /*lc*/ Nxx, Nxx, Nxx, Axx, NxL, NxR, NxN, NxN, NxN, NIx, /* CS following la */ }, -{ /*ls*/ Nxx, Nxx, Nxx, Nxx, LxL, NxR, NxN, NxN, NxN, NIx, /* CS,ES following le */ }, - -{ /*ret*/xxx, xxx, xxx, xxx, xxE, xxR, xxE, xxN, xxN, xxE, /* ET following re */ }, -{ /*let*/xxx, xxx, xxx, xxx, xxL, xxR, xxL, xxN, xxN, xxL, /* ET following le */ }, -}; - -inline uint8 GetDeferredType(bidi_action a) { return (a >> 4) & 0xF; } -inline uint8 GetResolvedType(bidi_action a) { return a & 0xF; } -inline DirCode EmbeddingDirection(int l) { return l & 1 ? R : L; } -inline bool IsDeferredState(bidi_state a) { return 0 != ((1 << a) & (rtmask | ltmask | acmask | rcmask | rsmask | lcmask | lsmask)); } -inline bool IsModifiedClass(DirCode a) { return 0 != ((1 << a) & (ALmask | NSMmask | ESmask | CSmask | ETmask | ENmask)); } - -void SetDeferredRunClass(Slot *s, Slot *sRun, int nval) -{ - if (!sRun || s == sRun) return; - for (Slot *p = s->prev(); p != sRun; p = p->prev()) - p->setBidiClass(nval); -} - -void resolveWeak(int baseLevel, Slot *s) -{ - int state = (baseLevel & 1) ? xr : xl; - int cls; - int level = baseLevel; - Slot *sRun = NULL; - Slot *sLast = s; - - for ( ; s; s = s->next()) - { - sLast = s; - cls = s->getBidiClass(); - if (cls == BN) - { - s->setBidiLevel(level); - if (!s->next() && level != baseLevel) - s->setBidiClass(EmbeddingDirection(level)); - else if (s->next() && level != s->next()->getBidiLevel() && s->next()->getBidiClass() != BN) - { - int newLevel = s->next()->getBidiLevel(); - if (level > newLevel) - newLevel = level; - s->setBidiLevel(newLevel); - s->setBidiClass(EmbeddingDirection(newLevel)); - level = s->next()->getBidiLevel(); - } - else - continue; - } - - bidi_action action = actionWeak[state][bidi_class_map[cls]]; - int clsRun = GetDeferredType(action); - if (clsRun != XX) - { - SetDeferredRunClass(s, sRun, clsRun); - sRun = NULL; - } - int clsNew = GetResolvedType(action); - if (clsNew != XX) - s->setBidiClass(clsNew); - if (!sRun && (IX & action)) - sRun = s->prev(); - state = stateWeak[state][bidi_class_map[cls]]; - } - - cls = EmbeddingDirection(level); - int clsRun = GetDeferredType(actionWeak[state][bidi_class_map[cls]]); - if (clsRun != XX) - SetDeferredRunClass(sLast, sRun, clsRun); -} - -// Neutrals -enum neutral_action -{ - // action to resolve previous input - nL = L, // resolve EN to L - En = 3 << 4, // resolve neutrals run to embedding level direction - Rn = R << 4, // resolve neutrals run to strong right - Ln = L << 4, // resolved neutrals run to strong left - In = (1<<8), // increment count of deferred neutrals - LnL = (1<<4)+L, // set run and EN to L -}; - -int GetDeferredNeutrals(int action, int level) -{ - action = (action >> 4) & 0xF; - if (action == (En >> 4)) - return EmbeddingDirection(level); - else - return action; -} - -int GetResolvedNeutrals(int action) -{ - action = action & 0xF; - if (action == In) - return 0; - else - return action; -} - -// state values -enum neutral_state -{ - // new temporary class - r, // R and characters resolved to R - l, // L and characters resolved to L - rn, // N preceded by right - ln, // N preceded by left - a, // AN preceded by left (the abbrev 'la' is used up above) - na, // N preceeded by a -} ; - -const uint8 neutral_class_map[] = { 0, 1, 2, 0, 4, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 }; - -const int actionNeutrals[][5] = -{ -// N, L, R, AN, EN, = cls - // state = -{ In, 0, 0, 0, 0, }, // r right -{ In, 0, 0, 0, L, }, // l left - -{ In, En, Rn, Rn, Rn, }, // rn N preceded by right -{ In, Ln, En, En, LnL, }, // ln N preceded by left - -{ In, 0, 0, 0, L, }, // a AN preceded by left -{ In, En, Rn, Rn, En, }, // na N preceded by a -} ; - -const int stateNeutrals[][5] = -{ -// N, L, R, AN, EN = cls - // state = -{ rn, l, r, r, r, }, // r right -{ ln, l, r, a, l, }, // l left - -{ rn, l, r, r, r, }, // rn N preceded by right -{ ln, l, r, a, l, }, // ln N preceded by left - -{ na, l, r, a, l, }, // a AN preceded by left -{ na, l, r, a, l, }, // na N preceded by la -} ; - -void resolveNeutrals(int baseLevel, Slot *s) -{ - int state = baseLevel ? r : l; - int cls; - Slot *sRun = NULL; - Slot *sLast = s; - int level = baseLevel; - - for ( ; s; s = s->next()) - { - sLast = s; - cls = s->getBidiClass(); - if (cls == BN) - { - if (sRun) - sRun = sRun->prev(); - continue; - } - - int action = actionNeutrals[state][neutral_class_map[cls]]; - int clsRun = GetDeferredNeutrals(action, level); - if (clsRun != N) - { - SetDeferredRunClass(s, sRun, clsRun); - sRun = NULL; - } - int clsNew = GetResolvedNeutrals(action); - if (clsNew != N) - s->setBidiClass(clsNew); - if (!sRun && (action & In)) - sRun = s->prev(); - state = stateNeutrals[state][neutral_class_map[cls]]; - level = s->getBidiLevel(); - } - cls = EmbeddingDirection(level); - int clsRun = GetDeferredNeutrals(actionNeutrals[state][neutral_class_map[cls]], level); - if (clsRun != N) - SetDeferredRunClass(sLast, sRun, clsRun); -} - -const int addLevel[][4] = -{ - // L, R, AN, EN = cls - // level = -/* even */ { 0, 1, 2, 2, }, // EVEN -/* odd */ { 1, 0, 1, 1, }, // ODD - -}; - -void resolveImplicit(Slot *s, Segment *seg, uint8 aMirror) -{ - bool rtl = seg->dir() & 1; - for ( ; s; s = s->next()) - { - int cls = s->getBidiClass(); - if (cls == BN) - continue; - else if (cls == AN) - cls = AL; - if (cls < 5 && cls > 0) - { - int level = s->getBidiLevel(); - level += addLevel[level & 1][cls - 1]; - s->setBidiLevel(level); - if (aMirror) - { - int hasChar = seg->glyphAttr(s->gid(), aMirror + 1); - if ( ((level & 1) && (!(seg->dir() & 4) || !hasChar)) - || ((rtl ^ (level & 1)) && (seg->dir() & 4) && hasChar) ) - { - unsigned short g = seg->glyphAttr(s->gid(), aMirror); - if (g) s->setGlyph(seg, g); - } - } - } - } -} - -void resolveWhitespace(int baseLevel, Segment *seg, uint8 aBidi, Slot *s) -{ - for ( ; s; s = s->prev()) - { - int cls = seg->glyphAttr(s->gid(), aBidi); - if (cls == WS) - s->setBidiLevel(baseLevel); - else - break; - } -} - - -inline -Slot * join(int level, Slot * a, Slot * b) -{ - if (!a) return b; - if (level & 1) { Slot * const t = a; a = b; b = t; } - Slot * const t = b->prev(); - a->prev()->next(b); b->prev(a->prev()); // splice middle - t->next(a); a->prev(t); // splice ends - return a; -} - - -Slot * span(Slot * & cs, const bool rtl) -{ - Slot * r = cs, * re = cs; cs = cs->next(); - if (rtl) - { - Slot * t = r->next(); r->next(r->prev()); r->prev(t); - for (int l = r->getBidiLevel(); cs && l == cs->getBidiLevel(); cs = cs->prev()) - { - re = cs; - t = cs->next(); cs->next(cs->prev()); cs->prev(t); - } - r->next(re); - re->prev(r); - r = re; - } - else - { - for (int l = r->getBidiLevel(); cs && l == cs->getBidiLevel(); cs = cs->next()) - re = cs; - r->prev(re); - re->next(r); - } - if (cs) cs->prev(0); - return r; -} - - -Slot *resolveOrder(Slot * & cs, const bool reordered, const int level) -{ - Slot * r = 0; - int ls; - while (cs && level <= (ls = cs->getBidiLevel() - reordered)) - { - r = join(level, r, level >= ls - ? span(cs, level & 1) - : resolveOrder(cs, reordered, level+1)); - } - return r; -} - diff --git a/gfx/graphite2/src/CMakeLists.txt b/gfx/graphite2/src/CMakeLists.txt index bb57772f9..4f1e7e5db 100644 --- a/gfx/graphite2/src/CMakeLists.txt +++ b/gfx/graphite2/src/CMakeLists.txt @@ -52,6 +52,10 @@ if (GRAPHITE2_NTRACING) set(TRACING) endif (GRAPHITE2_NTRACING) +if (GRAPHITE2_TELEMETRY) + add_definitions(-DGRAPHITE2_TELEMETRY) +endif (GRAPHITE2_TELEMETRY) + set(GRAPHITE_HEADERS ../include/graphite2/Font.h ../include/graphite2/Segment.h @@ -70,19 +74,21 @@ add_library(graphite2 SHARED gr_logging.cpp gr_segment.cpp gr_slot.cpp - Bidi.cpp CachedFace.cpp CmapCache.cpp Code.cpp + Collider.cpp + Decompressor.cpp Face.cpp FeatureMap.cpp Font.cpp GlyphFace.cpp GlyphCache.cpp + Intervals.cpp Justifier.cpp NameTable.cpp Pass.cpp - Rule.cpp + Position.cpp Segment.cpp Silf.cpp Slot.cpp @@ -103,12 +109,23 @@ set_target_properties(graphite2 PROPERTIES PUBLIC_HEADER "${GRAPHITE_HEADERS}" if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") set_target_properties(graphite2 PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Wno-unknown-pragmas -Wendif-labels -Wshadow -Wctor-dtor-privacy -Wnon-virtual-dtor -fno-rtti -fno-exceptions -fvisibility=hidden -fvisibility-inlines-hidden -fno-stack-protector" - LINK_FLAGS "-nodefaultlibs" + LINK_FLAGS "-nodefaultlibs ${GRAPHITE_LINK_FLAGS}" LINKER_LANGUAGE C) + if (CMAKE_COMPILER_IS_GNUCXX) + add_definitions(-Wdouble-promotion) + endif (CMAKE_COMPILER_IS_GNUCXX) + message(STATUS "Compiler ID is: ${CMAKE_CXX_COMPILER_ID}") + if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") + add_definitions(-Wimplicit-fallthrough) + endif (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") if (${CMAKE_CXX_COMPILER} MATCHES ".*mingw.*") target_link_libraries(graphite2 kernel32 msvcr90 mingw32 gcc user32) else (${CMAKE_CXX_COMPILER} MATCHES ".*mingw.*") - target_link_libraries(graphite2 c gcc) + if (GRAPHITE2_ASAN) + target_link_libraries(graphite2 c gcc_s) + else (GRAPHITE2_ASAN) + target_link_libraries(graphite2 c gcc) + endif (GRAPHITE2_ASAN) include(Graphite) nolib_test(stdc++ $<TARGET_SONAME_FILE:graphite2>) endif (${CMAKE_CXX_COMPILER} MATCHES ".*mingw.*") @@ -118,7 +135,7 @@ endif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") set_target_properties(graphite2 PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Wno-unknown-pragmas -Wendif-labels -Wshadow -Wno-ctor-dtor-privacy -Wno-non-virtual-dtor -fno-rtti -fno-exceptions -fvisibility=hidden -fvisibility-inlines-hidden -fno-stack-protector" + COMPILE_FLAGS "-Wall -Wextra -Wno-unknown-pragmas -Wimplicit-fallthrough -Wendif-labels -Wshadow -Wno-ctor-dtor-privacy -Wno-non-virtual-dtor -fno-rtti -fno-exceptions -fvisibility=hidden -fvisibility-inlines-hidden -fno-stack-protector" LINK_FLAGS "-nodefaultlibs" LINKER_LANGUAGE C) target_link_libraries(graphite2 c) diff --git a/gfx/graphite2/src/CachedFace.cpp b/gfx/graphite2/src/CachedFace.cpp index 364276885..fd599bde8 100644 --- a/gfx/graphite2/src/CachedFace.cpp +++ b/gfx/graphite2/src/CachedFace.cpp @@ -61,7 +61,7 @@ bool CachedFace::runGraphite(Segment *seg, const Silf *pSilf) const if (silfIndex == m_numSilf) return false; SegCache * const segCache = m_cacheStore->getOrCreate(silfIndex, seg->getFeatures(0)); if (!segCache) - return false; + return false; assert(m_cacheStore); // find where the segment can be broken @@ -71,23 +71,23 @@ bool CachedFace::runGraphite(Segment *seg, const Silf *pSilf) const int subSegStart = 0; for (unsigned int i = 0; i < seg->charInfoCount(); ++i) { - const unsigned int length = i - subSegStart + 1; + const unsigned int length = i - subSegStart + 1; if (length < eMaxSpliceSize) cmapGlyphs[length-1] = subSegEndSlot->gid(); else return false; const bool spaceOnly = m_cacheStore->isSpaceGlyph(subSegEndSlot->gid()); // at this stage the character to slot mapping is still 1 to 1 - const int breakWeight = seg->charinfo(i)->breakWeight(), - nextBreakWeight = (i + 1 < seg->charInfoCount())? - seg->charinfo(i+1)->breakWeight() : 0; + const int breakWeight = seg->charinfo(i)->breakWeight(), + nextBreakWeight = (i + 1 < seg->charInfoCount())? + seg->charinfo(i+1)->breakWeight() : 0; const uint8 f = seg->charinfo(i)->flags(); if (((spaceOnly - || (breakWeight > 0 && breakWeight <= gr_breakWord) - || i + 1 == seg->charInfoCount() - || ((nextBreakWeight < 0 && nextBreakWeight >= gr_breakBeforeWord) - || (subSegEndSlot->next() && m_cacheStore->isSpaceGlyph(subSegEndSlot->next()->gid())))) - && f != 1) - || f == 2) + || (breakWeight > 0 && breakWeight <= gr_breakWord) + || i + 1 == seg->charInfoCount() + || ((nextBreakWeight < 0 && nextBreakWeight >= gr_breakBeforeWord) + || (subSegEndSlot->next() && m_cacheStore->isSpaceGlyph(subSegEndSlot->next()->gid())))) + && f != 1) + || f == 2) { // record the next slot before any splicing Slot * nextSlot = subSegEndSlot->next(); @@ -103,8 +103,8 @@ bool CachedFace::runGraphite(Segment *seg, const Silf *pSilf) const pSilf->runGraphite(seg, pSilf->substitutionPass(), pSilf->numPasses()); if (length < eMaxSpliceSize) { - seg->associateChars(); - entry = segCache->cache(m_cacheStore, cmapGlyphs, length, seg, subSegStart); + seg->associateChars(subSegStart, length); + segCache->cache(m_cacheStore, cmapGlyphs, length, seg, subSegStart); } seg->removeScope(scopeState); } diff --git a/gfx/graphite2/src/CmapCache.cpp b/gfx/graphite2/src/CmapCache.cpp index f388df9d2..a945ef234 100644 --- a/gfx/graphite2/src/CmapCache.cpp +++ b/gfx/graphite2/src/CmapCache.cpp @@ -38,11 +38,11 @@ const void * bmp_subtable(const Face::Table & cmap) { const void * stbl; if (!cmap.size()) return 0; - if (TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 1, cmap.size())) - || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 3, cmap.size())) - || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 2, cmap.size())) - || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 1, cmap.size())) - || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 0, cmap.size()))) + if (TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 1, cmap.size()), cmap.size()) + || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 3, cmap.size()), cmap.size()) + || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 2, cmap.size()), cmap.size()) + || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 1, cmap.size()), cmap.size()) + || TtfUtil::CheckCmapSubtable4(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 0, cmap.size()), cmap.size())) return stbl; return 0; } @@ -51,8 +51,8 @@ const void * smp_subtable(const Face::Table & cmap) { const void * stbl; if (!cmap.size()) return 0; - if (TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 10, cmap.size())) - || TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 4, cmap.size()))) + if (TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 3, 10, cmap.size()), cmap.size()) + || TtfUtil::CheckCmapSubtable12(stbl = TtfUtil::FindCmapSubtable(cmap, 0, 4, cmap.size()), cmap.size())) return stbl; return 0; } @@ -89,7 +89,7 @@ CachedCmap::CachedCmap(const Face & face) m_blocks(0) { const Face::Table cmap(face, Tag::cmap); - if (!cmap) return; + if (!cmap) return; const void * bmp_cmap = bmp_subtable(cmap); const void * smp_cmap = smp_subtable(cmap); @@ -114,7 +114,7 @@ CachedCmap::~CachedCmap() throw() if (!m_blocks) return; unsigned int numBlocks = (m_isBmpOnly)? 0x100 : 0x1100; for (unsigned int i = 0; i < numBlocks; i++) - free(m_blocks[i]); + free(m_blocks[i]); free(m_blocks); } @@ -130,7 +130,7 @@ uint16 CachedCmap::operator [] (const uint32 usv) const throw() CachedCmap::operator bool() const throw() { - return m_blocks != 0; + return m_blocks != 0; } @@ -150,6 +150,6 @@ uint16 DirectCmap::operator [] (const uint32 usv) const throw() DirectCmap::operator bool () const throw() { - return _cmap && _bmp; + return _cmap && _bmp; } diff --git a/gfx/graphite2/src/Code.cpp b/gfx/graphite2/src/Code.cpp index 04ed6fe28..5355cbd42 100644 --- a/gfx/graphite2/src/Code.cpp +++ b/gfx/graphite2/src/Code.cpp @@ -89,12 +89,13 @@ public: byte max_ref; analysis() : slotref(0), max_ref(0) {}; - void set_ref(int index) throw(); + void set_ref(int index, bool incinsert=false) throw(); + void set_noref(int index) throw(); void set_changed(int index) throw(); }; - decoder(const limits & lims, Code &code) throw(); + decoder(limits & lims, Code &code, enum passtype pt) throw(); bool load(const byte * bc_begin, const byte * bc_end); void apply_analysis(instr * const code, instr * code_end); @@ -105,8 +106,9 @@ private: opcode fetch_opcode(const byte * bc); void analyse_opcode(const opcode, const int8 * const dp) throw(); bool emit_opcode(opcode opc, const byte * & bc); - bool validate_opcode(const opcode opc, const byte * const bc); + bool validate_opcode(const opcode opc, const byte * const bc); bool valid_upto(const uint16 limit, const uint16 x) const throw(); + bool test_context() const throw(); void failure(const status_t s) const throw() { _code.failure(s); } Code & _code; @@ -114,14 +116,16 @@ private: uint16 _rule_length; instr * _instr; byte * _data; - const limits & _max; + limits & _max; analysis _analysis; + enum passtype _passtype; + int _stack_depth; }; struct Machine::Code::decoder::limits { - const byte * const bytecode; + const byte * bytecode; const uint8 pre_context; const uint16 rule_length, classes, @@ -130,42 +134,46 @@ struct Machine::Code::decoder::limits const byte attrid[gr_slatMax]; }; -inline Machine::Code::decoder::decoder(const limits & lims, Code &code) throw() +inline Machine::Code::decoder::decoder(limits & lims, Code &code, enum passtype pt) throw() : _code(code), _pre_context(code._constraint ? 0 : lims.pre_context), _rule_length(code._constraint ? 1 : lims.rule_length), - _instr(code._code), _data(code._data), _max(lims) + _instr(code._code), _data(code._data), _max(lims), _passtype(pt), + _stack_depth(0) { } Machine::Code::Code(bool is_constraint, const byte * bytecode_begin, const byte * const bytecode_end, - uint8 pre_context, uint16 rule_length, const Silf & silf, const Face & face) + uint8 pre_context, uint16 rule_length, const Silf & silf, const Face & face, + enum passtype pt, byte * * const _out) : _code(0), _data(0), _data_size(0), _instr_count(0), _max_ref(0), _status(loaded), - _constraint(is_constraint), _modify(false), _delete(false), _own(true) + _constraint(is_constraint), _modify(false), _delete(false), _own(_out==0) { +#ifdef GRAPHITE2_TELEMETRY + telemetry::category _code_cat(face.tele.code); +#endif assert(bytecode_begin != 0); if (bytecode_begin == bytecode_end) { - ::new (this) Code(); + // ::new (this) Code(); return; } assert(bytecode_end > bytecode_begin); const opcode_t * op_to_fn = Machine::getOpcodeTable(); - // Allocate code and dat target buffers, these sizes are a worst case + // Allocate code and data target buffers, these sizes are a worst case // estimate. Once we know their real sizes the we'll shrink them. - _code = static_cast<instr *>(malloc((bytecode_end - bytecode_begin) - * sizeof(instr))); - _data = static_cast<byte *>(malloc((bytecode_end - bytecode_begin) - * sizeof(byte))); + if (_out) _code = reinterpret_cast<instr *>(*_out); + else _code = static_cast<instr *>(malloc(estimateCodeDataOut(bytecode_end-bytecode_begin))); + _data = reinterpret_cast<byte *>(_code + (bytecode_end - bytecode_begin)); if (!_code || !_data) { failure(alloc_failed); return; } - const decoder::limits lims = { + decoder::limits lims = { bytecode_end, pre_context, rule_length, @@ -181,7 +189,7 @@ Machine::Code::Code(bool is_constraint, const byte * bytecode_begin, const byte 0,0,0,0,0,0,0, silf.numUser()} }; - decoder dec(lims, *this); + decoder dec(lims, *this, pt); if(!dec.load(bytecode_begin, bytecode_end)) return; @@ -206,17 +214,28 @@ Machine::Code::Code(bool is_constraint, const byte * bytecode_begin, const byte // Now we know exactly how much code and data the program really needs // realloc the buffers to exactly the right size so we don't waste any // memory. - assert((bytecode_end - bytecode_begin) >= std::ptrdiff_t(_instr_count)); - assert((bytecode_end - bytecode_begin) >= std::ptrdiff_t(_data_size)); - _code = static_cast<instr *>(realloc(_code, (_instr_count+1)*sizeof(instr))); - _data = static_cast<byte *>(realloc(_data, _data_size*sizeof(byte))); + assert((bytecode_end - bytecode_begin) >= ptrdiff_t(_instr_count)); + assert((bytecode_end - bytecode_begin) >= ptrdiff_t(_data_size)); + memmove(_code + (_instr_count+1), _data, _data_size*sizeof(byte)); + size_t const total_sz = ((_instr_count+1) + (_data_size + sizeof(instr)-1)/sizeof(instr))*sizeof(instr); + if (_out) + *_out += total_sz; + else + _code = static_cast<instr *>(realloc(_code, total_sz)); + _data = reinterpret_cast<byte *>(_code + (_instr_count+1)); if (!_code) + { failure(alloc_failed); + return; + } // Make this RET_ZERO, we should never reach this but just in case ... _code[_instr_count] = op_to_fn[RET_ZERO].impl[_constraint]; +#ifdef GRAPHITE2_TELEMETRY + telemetry::count_bytes(_data_size + (_instr_count+1)*sizeof(instr)); +#endif } Machine::Code::~Code() throw () @@ -228,6 +247,7 @@ Machine::Code::~Code() throw () bool Machine::Code::decoder::load(const byte * bc, const byte * bc_end) { + _max.bytecode = bc_end; while (bc < bc_end) { const opcode opc = fetch_opcode(bc++); @@ -251,61 +271,88 @@ opcode Machine::Code::decoder::fetch_opcode(const byte * bc) const opcode opc = opcode(*bc++); // Do some basic sanity checks based on what we know about the opcode - if (!validate_opcode(opc, bc)) return MAX_OPCODE; + if (!validate_opcode(opc, bc)) return MAX_OPCODE; // And check it's arguments as far as possible switch (opc) { case NOP : + break; case PUSH_BYTE : case PUSH_BYTEU : case PUSH_SHORT : case PUSH_SHORTU : case PUSH_LONG : + ++_stack_depth; + break; case ADD : case SUB : case MUL : case DIV : case MIN_ : case MAX_ : - case NEG : - case TRUNC8 : - case TRUNC16 : - case COND : case AND : case OR : - case NOT : case EQUAL : case NOT_EQ : case LESS : case GTR : case LESS_EQ : case GTR_EQ : + case BITOR : + case BITAND : + if (--_stack_depth <= 0) + failure(underfull_stack); + break; + case NEG : + case TRUNC8 : + case TRUNC16 : + case NOT : + case BITNOT : + case BITSET : + if (_stack_depth <= 0) + failure(underfull_stack); + break; + case COND : + _stack_depth -= 2; + if (_stack_depth <= 0) + failure(underfull_stack); break; case NEXT : case NEXT_N : // runtime checked case COPY_NEXT : + test_context(); ++_pre_context; break; case PUT_GLYPH_8BIT_OBS : valid_upto(_max.classes, bc[0]); + test_context(); break; case PUT_SUBS_8BIT_OBS : valid_upto(_rule_length, _pre_context + int8(bc[0])); valid_upto(_max.classes, bc[1]); valid_upto(_max.classes, bc[2]); + test_context(); break; case PUT_COPY : valid_upto(_rule_length, _pre_context + int8(bc[0])); + test_context(); break; case INSERT : - --_pre_context; + if (_passtype >= PASS_TYPE_POSITIONING) + failure(invalid_opcode); + else + --_pre_context; break; case DELETE : + if (_passtype >= PASS_TYPE_POSITIONING) + failure(invalid_opcode); + test_context(); break; case ASSOC : for (uint8 num = bc[0]; num; --num) valid_upto(_rule_length, _pre_context + int8(bc[num])); + test_context(); break; case CNTXT_ITEM : valid_upto(_max.rule_length, _max.pre_context + int8(bc[0])); @@ -316,72 +363,98 @@ opcode Machine::Code::decoder::fetch_opcode(const byte * bc) case ATTR_ADD : case ATTR_SUB : case ATTR_SET_SLOT : - valid_upto(gr_slatMax, bc[0]); + if (--_stack_depth < 0) + failure(underfull_stack); + valid_upto(gr_slatMax, bc[0]); + test_context(); break; case IATTR_SET_SLOT : + if (--_stack_depth < 0) + failure(underfull_stack); if (valid_upto(gr_slatMax, bc[0])) - valid_upto(_max.attrid[bc[0]], bc[1]); + valid_upto(_max.attrid[bc[0]], bc[1]); + test_context(); break; case PUSH_SLOT_ATTR : + ++_stack_depth; valid_upto(gr_slatMax, bc[0]); valid_upto(_rule_length, _pre_context + int8(bc[1])); break; case PUSH_GLYPH_ATTR_OBS : + ++_stack_depth; valid_upto(_max.glyf_attrs, bc[0]); valid_upto(_rule_length, _pre_context + int8(bc[1])); break; case PUSH_GLYPH_METRIC : + ++_stack_depth; valid_upto(kgmetDescent, bc[0]); valid_upto(_rule_length, _pre_context + int8(bc[1])); // level: dp[2] no check necessary break; case PUSH_FEAT : + ++_stack_depth; valid_upto(_max.features, bc[0]); valid_upto(_rule_length, _pre_context + int8(bc[1])); break; case PUSH_ATT_TO_GATTR_OBS : + ++_stack_depth; valid_upto(_max.glyf_attrs, bc[0]); valid_upto(_rule_length, _pre_context + int8(bc[1])); break; case PUSH_ATT_TO_GLYPH_METRIC : + ++_stack_depth; valid_upto(kgmetDescent, bc[0]); valid_upto(_rule_length, _pre_context + int8(bc[1])); // level: dp[2] no check necessary break; case PUSH_ISLOT_ATTR : + ++_stack_depth; if (valid_upto(gr_slatMax, bc[0])) { - valid_upto(_rule_length, _pre_context + int8(bc[1])); - valid_upto(_max.attrid[bc[0]], bc[2]); + valid_upto(_rule_length, _pre_context + int8(bc[1])); + valid_upto(_max.attrid[bc[0]], bc[2]); } break; case PUSH_IGLYPH_ATTR :// not implemented + ++_stack_depth; + break; case POP_RET : + if (--_stack_depth < 0) + failure(underfull_stack); + GR_FALLTHROUGH; + // no break case RET_ZERO : case RET_TRUE : break; case IATTR_SET : case IATTR_ADD : case IATTR_SUB : + if (--_stack_depth < 0) + failure(underfull_stack); if (valid_upto(gr_slatMax, bc[0])) - valid_upto(_max.attrid[bc[0]], bc[1]); + valid_upto(_max.attrid[bc[0]], bc[1]); + test_context(); break; case PUSH_PROC_STATE : // dummy: dp[0] no check necessary case PUSH_VERSION : + ++_stack_depth; break; case PUT_SUBS : valid_upto(_rule_length, _pre_context + int8(bc[0])); valid_upto(_max.classes, uint16(bc[1]<< 8) | bc[2]); valid_upto(_max.classes, uint16(bc[3]<< 8) | bc[4]); + test_context(); break; case PUT_SUBS2 : // not implemented case PUT_SUBS3 : // not implemented break; case PUT_GLYPH : valid_upto(_max.classes, uint16(bc[0]<< 8) | bc[1]); + test_context(); break; case PUSH_GLYPH_ATTR : case PUSH_ATT_TO_GLYPH_ATTR : + ++_stack_depth; valid_upto(_max.glyf_attrs, uint16(bc[0]<< 8) | bc[1]); valid_upto(_rule_length, _pre_context + int8(bc[2])); break; @@ -406,14 +479,23 @@ void Machine::Code::decoder::analyse_opcode(const opcode opc, const int8 * arg) case PUT_GLYPH_8BIT_OBS : case PUT_GLYPH : _code._modify = true; - _analysis.set_changed(_analysis.slotref); + _analysis.set_changed(0); + break; + case ATTR_SET : + case ATTR_ADD : + case ATTR_SET_SLOT : + case IATTR_SET_SLOT : + case IATTR_SET : + case IATTR_ADD : + case IATTR_SUB : + _analysis.set_noref(0); break; case NEXT : case COPY_NEXT : if (!_analysis.contexts[_analysis.slotref].flags.inserted) ++_analysis.slotref; _analysis.contexts[_analysis.slotref] = context(_code._instr_count+1); - if (_analysis.slotref > _analysis.max_ref) _analysis.max_ref = _analysis.slotref; + // if (_analysis.slotref > _analysis.max_ref) _analysis.max_ref = _analysis.slotref; break; case INSERT : _analysis.contexts[_analysis.slotref].flags.inserted = true; @@ -422,18 +504,21 @@ void Machine::Code::decoder::analyse_opcode(const opcode opc, const int8 * arg) case PUT_SUBS_8BIT_OBS : // slotref on 1st parameter case PUT_SUBS : _code._modify = true; - _analysis.set_changed(_analysis.slotref); + _analysis.set_changed(0); + GR_FALLTHROUGH; // no break case PUT_COPY : { - if (arg[0] != 0) { _analysis.set_changed(_analysis.slotref); _code._modify = true; } + if (arg[0] != 0) { _analysis.set_changed(0); _code._modify = true; } if (arg[0] <= 0 && -arg[0] <= _analysis.slotref - _analysis.contexts[_analysis.slotref].flags.inserted) - _analysis.set_ref(_analysis.slotref + arg[0] - _analysis.contexts[_analysis.slotref].flags.inserted); - else if (_analysis.slotref + arg[0] > _analysis.max_ref) _analysis.max_ref = _analysis.slotref + arg[0]; + _analysis.set_ref(arg[0], true); + else if (arg[0] > 0) + _analysis.set_ref(arg[0], true); break; } case PUSH_ATT_TO_GATTR_OBS : // slotref on 2nd parameter if (_code._constraint) return; + GR_FALLTHROUGH; // no break case PUSH_GLYPH_ATTR_OBS : case PUSH_SLOT_ATTR : @@ -442,16 +527,19 @@ void Machine::Code::decoder::analyse_opcode(const opcode opc, const int8 * arg) case PUSH_ISLOT_ATTR : case PUSH_FEAT : if (arg[1] <= 0 && -arg[1] <= _analysis.slotref - _analysis.contexts[_analysis.slotref].flags.inserted) - _analysis.set_ref(_analysis.slotref + arg[1] - _analysis.contexts[_analysis.slotref].flags.inserted); - else if (_analysis.slotref + arg[1] > _analysis.max_ref) _analysis.max_ref = _analysis.slotref + arg[1]; + _analysis.set_ref(arg[1], true); + else if (arg[1] > 0) + _analysis.set_ref(arg[1], true); break; case PUSH_ATT_TO_GLYPH_ATTR : if (_code._constraint) return; + GR_FALLTHROUGH; // no break case PUSH_GLYPH_ATTR : if (arg[2] <= 0 && -arg[2] <= _analysis.slotref - _analysis.contexts[_analysis.slotref].flags.inserted) - _analysis.set_ref(_analysis.slotref + arg[2] - _analysis.contexts[_analysis.slotref].flags.inserted); - else if (_analysis.slotref + arg[2] > _analysis.max_ref) _analysis.max_ref = _analysis.slotref + arg[2]; + _analysis.set_ref(arg[2], true); + else if (arg[2] > 0) + _analysis.set_ref(arg[2], true); break; case ASSOC : // slotrefs in varargs break; @@ -497,16 +585,23 @@ bool Machine::Code::decoder::emit_opcode(opcode opc, const byte * & bc) byte & instr_skip = _data[-1]; byte & data_skip = *_data++; ++_code._data_size; + const byte *curr_end = _max.bytecode; if (load(bc, bc + instr_skip)) { bc += instr_skip; data_skip = instr_skip - (_code._instr_count - ctxt_start); instr_skip = _code._instr_count - ctxt_start; + _max.bytecode = curr_end; _rule_length = 1; _pre_context = 0; } + else + { + _pre_context = 0; + return false; + } } return bool(_code); @@ -529,6 +624,7 @@ void Machine::Code::decoder::apply_analysis(instr * const code, instr * code_end *tip = temp_copy; ++code_end; ++tempcount; + _code._delete = true; } _code._instr_count = code_end - code; @@ -538,29 +634,43 @@ void Machine::Code::decoder::apply_analysis(instr * const code, instr * code_end inline bool Machine::Code::decoder::validate_opcode(const opcode opc, const byte * const bc) { - if (opc >= MAX_OPCODE) - { - failure(invalid_opcode); - return false; - } - const opcode_t & op = Machine::getOpcodeTable()[opc]; - const size_t param_sz = op.param_sz == VARARGS ? bc[0] + 1 : op.param_sz; - if (bc + param_sz > _max.bytecode) - { - failure(arguments_exhausted); - return false; - } - return true; + if (opc >= MAX_OPCODE) + { + failure(invalid_opcode); + return false; + } + const opcode_t & op = Machine::getOpcodeTable()[opc]; + if (op.param_sz == VARARGS && bc >= _max.bytecode) + { + failure(arguments_exhausted); + return false; + } + const size_t param_sz = op.param_sz == VARARGS ? bc[0] + 1 : op.param_sz; + if (bc - 1 + param_sz >= _max.bytecode) + { + failure(arguments_exhausted); + return false; + } + return true; } bool Machine::Code::decoder::valid_upto(const uint16 limit, const uint16 x) const throw() { - const bool t = x < limit; - if (!t) failure(out_of_range_data); + const bool t = x < limit; + if (!t) failure(out_of_range_data); return t; } +bool Machine::Code::decoder::test_context() const throw() +{ + if (_pre_context >= _rule_length) + { + failure(out_of_range_data); + return false; + } + return true; +} inline void Machine::Code::failure(const status_t s) throw() { @@ -570,23 +680,35 @@ void Machine::Code::failure(const status_t s) throw() { inline -void Machine::Code::decoder::analysis::set_ref(const int index) throw() { - contexts[index].flags.referenced = true; - if (index > max_ref) max_ref = index; +void Machine::Code::decoder::analysis::set_ref(int index, bool incinsert) throw() { + if (incinsert && contexts[slotref].flags.inserted) --index; + if (index + slotref < 0) return; + contexts[index + slotref].flags.referenced = true; + if ((index > 0 || !contexts[index + slotref].flags.inserted) && index + slotref > max_ref) max_ref = index + slotref; } inline -void Machine::Code::decoder::analysis::set_changed(const int index) throw() { - contexts[index].flags.changed = true; - if (index > max_ref) max_ref = index; +void Machine::Code::decoder::analysis::set_noref(int index) throw() { + if (contexts[slotref].flags.inserted) --index; + if (index + slotref < 0) return; + if ((index > 0 || !contexts[index + slotref].flags.inserted) && index + slotref > max_ref) max_ref = index + slotref; +} + + +inline +void Machine::Code::decoder::analysis::set_changed(int index) throw() { + if (contexts[slotref].flags.inserted) --index; + if (index + slotref < 0) return; + contexts[index + slotref].flags.changed = true; + if ((index > 0 || !contexts[index + slotref].flags.inserted) && index + slotref > max_ref) max_ref = index + slotref; } void Machine::Code::release_buffers() throw() { - free(_code); - free(_data); + if (_own) + free(_code); _code = 0; _data = 0; _own = false; @@ -595,14 +717,15 @@ void Machine::Code::release_buffers() throw() int32 Machine::Code::run(Machine & m, slotref * & map) const { - assert(_own); +// assert(_own); assert(*this); // Check we are actually runnable - if (m.slotMap().size() <= size_t(_max_ref + m.slotMap().context())) + if (m.slotMap().size() <= size_t(_max_ref + m.slotMap().context()) + || m.slotMap()[_max_ref + m.slotMap().context()] == 0) { m._status = Machine::slot_offset_out_bounds; -// return (m.slotMap().end() - map); return 1; +// return m.run(_code, _data, map); } return m.run(_code, _data, map); diff --git a/gfx/graphite2/src/Collider.cpp b/gfx/graphite2/src/Collider.cpp new file mode 100644 index 000000000..87f067d3f --- /dev/null +++ b/gfx/graphite2/src/Collider.cpp @@ -0,0 +1,1088 @@ +/* GRAPHITE2 LICENSING + + Copyright 2010, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ +#include <algorithm> +#include <limits> +#include <math.h> +#include <string> +#include <functional> +#include "inc/Collider.h" +#include "inc/Segment.h" +#include "inc/Slot.h" +#include "inc/GlyphCache.h" +#include "inc/Sparse.h" + +#define ISQRT2 0.707106781f + +// Possible rounding error for subbox boundaries: 0.016 = 1/64 = 1/256 * 4 +// (values in font range from 0..256) +// #define SUBBOX_RND_ERR 0.016 + +using namespace graphite2; + +//// SHIFT-COLLIDER //// + +// Initialize the Collider to hold the basic movement limits for the +// target slot, the one we are focusing on fixing. +bool ShiftCollider::initSlot(Segment *seg, Slot *aSlot, const Rect &limit, float margin, float marginWeight, + const Position &currShift, const Position &currOffset, int dir, GR_MAYBE_UNUSED json * const dbgout) +{ + int i; + float mx, mn; + float a, shift; + const GlyphCache &gc = seg->getFace()->glyphs(); + unsigned short gid = aSlot->gid(); + if (!gc.check(gid)) + return false; + const BBox &bb = gc.getBoundingBBox(gid); + const SlantBox &sb = gc.getBoundingSlantBox(gid); + //float sx = aSlot->origin().x + currShift.x; + //float sy = aSlot->origin().y + currShift.y; + if (currOffset.x != 0.f || currOffset.y != 0.f) + _limit = Rect(limit.bl - currOffset, limit.tr - currOffset); + else + _limit = limit; + // For a ShiftCollider, these indices indicate which vector we are moving by: + // each _ranges represents absolute space with respect to the origin of the slot. Thus take into account true origins but subtract the vmin for the slot + for (i = 0; i < 4; ++i) + { + switch (i) { + case 0 : // x direction + mn = _limit.bl.x + currOffset.x; + mx = _limit.tr.x + currOffset.x; + _len[i] = bb.xa - bb.xi; + a = currOffset.y + currShift.y; + _ranges[i].initialise<XY>(mn, mx, margin, marginWeight, a); + break; + case 1 : // y direction + mn = _limit.bl.y + currOffset.y; + mx = _limit.tr.y + currOffset.y; + _len[i] = bb.ya - bb.yi; + a = currOffset.x + currShift.x; + _ranges[i].initialise<XY>(mn, mx, margin, marginWeight, a); + break; + case 2 : // sum (negatively sloped diagonal boundaries) + // pick closest x,y limit boundaries in s direction + shift = currOffset.x + currOffset.y + currShift.x + currShift.y; + mn = -2 * min(currShift.x - _limit.bl.x, currShift.y - _limit.bl.y) + shift; + mx = 2 * min(_limit.tr.x - currShift.x, _limit.tr.y - currShift.y) + shift; + _len[i] = sb.sa - sb.si; + a = currOffset.x - currOffset.y + currShift.x - currShift.y; + _ranges[i].initialise<SD>(mn, mx, margin / ISQRT2, marginWeight, a); + break; + case 3 : // diff (positively sloped diagonal boundaries) + // pick closest x,y limit boundaries in d direction + shift = currOffset.x - currOffset.y + currShift.x - currShift.y; + mn = -2 * min(currShift.x - _limit.bl.x, _limit.tr.y - currShift.y) + shift; + mx = 2 * min(_limit.tr.x - currShift.x, currShift.y - _limit.bl.y) + shift; + _len[i] = sb.da - sb.di; + a = currOffset.x + currOffset.y + currShift.x + currShift.y; + _ranges[i].initialise<SD>(mn, mx, margin / ISQRT2, marginWeight, a); + break; + } + } + + _target = aSlot; + if ((dir & 1) == 0) + { + // For LTR, switch and negate x limits. + _limit.bl.x = -1 * limit.tr.x; + //_limit.tr.x = -1 * limit.bl.x; + } + _currOffset = currOffset; + _currShift = currShift; + _origin = aSlot->origin() - currOffset; // the original anchor position of the glyph + + _margin = margin; + _marginWt = marginWeight; + + SlotCollision *c = seg->collisionInfo(aSlot); + _seqClass = c->seqClass(); + _seqProxClass = c->seqProxClass(); + _seqOrder = c->seqOrder(); + return true; +} + +template <class O> +float sdm(float vi, float va, float mx, float my, O op) +{ + float res = 2 * mx - vi; + if (op(res, vi + 2 * my)) + { + res = va + 2 * my; + if (op(res, 2 * mx - va)) + res = mx + my; + } + return res; +} + +// Mark an area with a cost that can vary along the x or y axis. The region is expressed in terms of the centre of the target glyph in each axis +void ShiftCollider::addBox_slope(bool isx, const Rect &box, const BBox &bb, const SlantBox &sb, const Position &org, float weight, float m, bool minright, int axis) +{ + float a, c; + switch (axis) { + case 0 : + if (box.bl.y < org.y + bb.ya && box.tr.y > org.y + bb.yi && box.width() > 0) + { + a = org.y + 0.5f * (bb.yi + bb.ya); + c = 0.5f * (bb.xi + bb.xa); + if (isx) + _ranges[axis].weighted<XY>(box.bl.x - c, box.tr.x - c, weight, a, m, + (minright ? box.tr.x : box.bl.x) - c, a, 0, false); + else + _ranges[axis].weighted<XY>(box.bl.x - c, box.tr.x - c, weight, a, 0, 0, org.y, + m * (a * a + sqr((minright ? box.tr.y : box.bl.y) - 0.5f * (bb.yi + bb.ya))), false); + } + break; + case 1 : + if (box.bl.x < org.x + bb.xa && box.tr.x > org.x + bb.xi && box.height() > 0) + { + a = org.x + 0.5f * (bb.xi + bb.xa); + c = 0.5f * (bb.yi + bb.ya); + if (isx) + _ranges[axis].weighted<XY>(box.bl.y - c, box.tr.y - c, weight, a, 0, 0, org.x, + m * (a * a + sqr((minright ? box.tr.x : box.bl.x) - 0.5f * (bb.xi + bb.xa))), false); + else + _ranges[axis].weighted<XY>(box.bl.y - c, box.tr.y - c, weight, a, m, + (minright ? box.tr.y : box.bl.y) - c, a, 0, false); + } + break; + case 2 : + if (box.bl.x - box.tr.y < org.x - org.y + sb.da && box.tr.x - box.bl.y > org.x - org.y + sb.di) + { + float d = org.x - org.y + 0.5f * (sb.di + sb.da); + c = 0.5f * (sb.si + sb.sa); + float smax = min(2 * box.tr.x - d, 2 * box.tr.y + d); + float smin = max(2 * box.bl.x - d, 2 * box.bl.y + d); + if (smin > smax) return; + float si; + a = d; + if (isx) + si = 2 * (minright ? box.tr.x : box.bl.x) - a; + else + si = 2 * (minright ? box.tr.y : box.bl.y) + a; + _ranges[axis].weighted<SD>(smin - c, smax - c, weight / 2, a, m / 2, si, 0, 0, isx); + } + break; + case 3 : + if (box.bl.x + box.bl.y < org.x + org.y + sb.sa && box.tr.x + box.tr.y > org.x + org.y + sb.si) + { + float s = org.x + org.y + 0.5f * (sb.si + sb.sa); + c = 0.5f * (sb.di + sb.da); + float dmax = min(2 * box.tr.x - s, s - 2 * box.bl.y); + float dmin = max(2 * box.bl.x - s, s - 2 * box.tr.y); + if (dmin > dmax) return; + float di; + a = s; + if (isx) + di = 2 * (minright ? box.tr.x : box.bl.x) - a; + else + di = 2 * (minright ? box.tr.y : box.bl.y) + a; + _ranges[axis].weighted<SD>(dmin - c, dmax - c, weight / 2, a, m / 2, di, 0, 0, !isx); + } + break; + default : + break; + } + return; +} + +// Mark an area with an absolute cost, making it completely inaccessible. +inline void ShiftCollider::removeBox(const Rect &box, const BBox &bb, const SlantBox &sb, const Position &org, int axis) +{ + float c; + switch (axis) { + case 0 : + if (box.bl.y < org.y + bb.ya && box.tr.y > org.y + bb.yi && box.width() > 0) + { + c = 0.5f * (bb.xi + bb.xa); + _ranges[axis].exclude(box.bl.x - c, box.tr.x - c); + } + break; + case 1 : + if (box.bl.x < org.x + bb.xa && box.tr.x > org.x + bb.xi && box.height() > 0) + { + c = 0.5f * (bb.yi + bb.ya); + _ranges[axis].exclude(box.bl.y - c, box.tr.y - c); + } + break; + case 2 : + if (box.bl.x - box.tr.y < org.x - org.y + sb.da && box.tr.x - box.bl.y > org.x - org.y + sb.di + && box.width() > 0 && box.height() > 0) + { + float di = org.x - org.y + sb.di; + float da = org.x - org.y + sb.da; + float smax = sdm(di, da, box.tr.x, box.tr.y, std::greater<float>()); + float smin = sdm(da, di, box.bl.x, box.bl.y, std::less<float>()); + c = 0.5f * (sb.si + sb.sa); + _ranges[axis].exclude(smin - c, smax - c); + } + break; + case 3 : + if (box.bl.x + box.bl.y < org.x + org.y + sb.sa && box.tr.x + box.tr.y > org.x + org.y + sb.si + && box.width() > 0 && box.height() > 0) + { + float si = org.x + org.y + sb.si; + float sa = org.x + org.y + sb.sa; + float dmax = sdm(si, sa, box.tr.x, -box.bl.y, std::greater<float>()); + float dmin = sdm(sa, si, box.bl.x, -box.tr.y, std::less<float>()); + c = 0.5f * (sb.di + sb.da); + _ranges[axis].exclude(dmin - c, dmax - c); + } + break; + default : + break; + } + return; +} + +// Adjust the movement limits for the target to avoid having it collide +// with the given neighbor slot. Also determine if there is in fact a collision +// between the target and the given slot. +bool ShiftCollider::mergeSlot(Segment *seg, Slot *slot, const Position &currShift, + bool isAfter, // slot is logically after _target + bool sameCluster, bool &hasCol, bool isExclusion, + GR_MAYBE_UNUSED json * const dbgout ) +{ + bool isCol = false; + const float sx = slot->origin().x - _origin.x + currShift.x; + const float sy = slot->origin().y - _origin.y + currShift.y; + const float sd = sx - sy; + const float ss = sx + sy; + float vmin, vmax; + float omin, omax, otmin, otmax; + float cmin, cmax; // target limits + float torg; + const GlyphCache &gc = seg->getFace()->glyphs(); + const unsigned short gid = slot->gid(); + if (!gc.check(gid)) + return false; + const BBox &bb = gc.getBoundingBBox(gid); + + SlotCollision * cslot = seg->collisionInfo(slot); + int orderFlags = 0; + bool sameClass = _seqProxClass == 0 && cslot->seqClass() == _seqClass; + if (sameCluster && _seqClass + && (sameClass || (_seqProxClass != 0 && cslot->seqClass() == _seqProxClass))) + // Force the target glyph to be in the specified direction from the slot we're testing. + orderFlags = _seqOrder; + + // short circuit if only interested in direct collision and we are out of range + if (orderFlags || (sx + bb.xa + _margin >= _limit.bl.x && sx + bb.xi - _margin <= _limit.tr.x) + || (sy + bb.ya + _margin >= _limit.bl.y && sy + bb.yi - _margin <= _limit.tr.y)) + + { + const float tx = _currOffset.x + _currShift.x; + const float ty = _currOffset.y + _currShift.y; + const float td = tx - ty; + const float ts = tx + ty; + const SlantBox &sb = gc.getBoundingSlantBox(gid); + const unsigned short tgid = _target->gid(); + const BBox &tbb = gc.getBoundingBBox(tgid); + const SlantBox &tsb = gc.getBoundingSlantBox(tgid); + float seq_above_wt = cslot->seqAboveWt(); + float seq_below_wt = cslot->seqBelowWt(); + float seq_valign_wt = cslot->seqValignWt(); + // if isAfter, invert orderFlags for diagonal orders. + if (isAfter) + { + // invert appropriate bits + orderFlags ^= (sameClass ? 0x3F : 0x3); + // consider 2 bits at a time, non overlapping. If both bits set, clear them + orderFlags = orderFlags ^ ((((orderFlags >> 1) & orderFlags) & 0x15) * 3); + } + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + dbgout->setenv(0, slot); +#endif + + // Process main bounding octabox. + for (int i = 0; i < 4; ++i) + { + switch (i) { + case 0 : // x direction + vmin = max(max(bb.xi - tbb.xa + sx, sb.di - tsb.da + ty + sd), sb.si - tsb.sa - ty + ss); + vmax = min(min(bb.xa - tbb.xi + sx, sb.da - tsb.di + ty + sd), sb.sa - tsb.si - ty + ss); + otmin = tbb.yi + ty; + otmax = tbb.ya + ty; + omin = bb.yi + sy; + omax = bb.ya + sy; + torg = _currOffset.x; + cmin = _limit.bl.x + torg; + cmax = _limit.tr.x - tbb.xi + tbb.xa + torg; + break; + case 1 : // y direction + vmin = max(max(bb.yi - tbb.ya + sy, tsb.di - sb.da + tx - sd), sb.si - tsb.sa - tx + ss); + vmax = min(min(bb.ya - tbb.yi + sy, tsb.da - sb.di + tx - sd), sb.sa - tsb.si - tx + ss); + otmin = tbb.xi + tx; + otmax = tbb.xa + tx; + omin = bb.xi + sx; + omax = bb.xa + sx; + torg = _currOffset.y; + cmin = _limit.bl.y + torg; + cmax = _limit.tr.y - tbb.yi + tbb.ya + torg; + break; + case 2 : // sum - moving along the positively-sloped vector, so the boundaries are the + // negatively-sloped boundaries. + vmin = max(max(sb.si - tsb.sa + ss, 2 * (bb.yi - tbb.ya + sy) + td), 2 * (bb.xi - tbb.xa + sx) - td); + vmax = min(min(sb.sa - tsb.si + ss, 2 * (bb.ya - tbb.yi + sy) + td), 2 * (bb.xa - tbb.xi + sx) - td); + otmin = tsb.di + td; + otmax = tsb.da + td; + omin = sb.di + sd; + omax = sb.da + sd; + torg = _currOffset.x + _currOffset.y; + cmin = _limit.bl.x + _limit.bl.y + torg; + cmax = _limit.tr.x + _limit.tr.y - tsb.si + tsb.sa + torg; + break; + case 3 : // diff - moving along the negatively-sloped vector, so the boundaries are the + // positively-sloped boundaries. + vmin = max(max(sb.di - tsb.da + sd, 2 * (bb.xi - tbb.xa + sx) - ts), -2 * (bb.ya - tbb.yi + sy) + ts); + vmax = min(min(sb.da - tsb.di + sd, 2 * (bb.xa - tbb.xi + sx) - ts), -2 * (bb.yi - tbb.ya + sy) + ts); + otmin = tsb.si + ts; + otmax = tsb.sa + ts; + omin = sb.si + ss; + omax = sb.sa + ss; + torg = _currOffset.x - _currOffset.y; + cmin = _limit.bl.x - _limit.tr.y + torg; + cmax = _limit.tr.x - _limit.bl.y - tsb.di + tsb.da + torg; + break; + default : + continue; + } + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + dbgout->setenv(1, reinterpret_cast<void *>(-1)); +#define DBGTAG(x) if (dbgout) dbgout->setenv(1, reinterpret_cast<void *>(-x)); +#else +#define DBGTAG(x) +#endif + + if (orderFlags) + { + Position org(tx, ty); + float xminf = _limit.bl.x + _currOffset.x + tbb.xi; + float xpinf = _limit.tr.x + _currOffset.x + tbb.xa; + float ypinf = _limit.tr.y + _currOffset.y + tbb.ya; + float yminf = _limit.bl.y + _currOffset.y + tbb.yi; + switch (orderFlags) { + case SlotCollision::SEQ_ORDER_RIGHTUP : + { + float r1Xedge = cslot->seqAboveXoff() + 0.5f * (bb.xi + bb.xa) + sx; + float r3Xedge = cslot->seqBelowXlim() + bb.xa + sx + 0.5f * (tbb.xa - tbb.xi); + float r2Yedge = 0.5f * (bb.yi + bb.ya) + sy; + + // DBGTAG(1x) means the regions are up and right + // region 1 + DBGTAG(11) + addBox_slope(true, Rect(Position(xminf, r2Yedge), Position(r1Xedge, ypinf)), + tbb, tsb, org, 0, seq_above_wt, true, i); + // region 2 + DBGTAG(12) + removeBox(Rect(Position(xminf, yminf), Position(r3Xedge, r2Yedge)), tbb, tsb, org, i); + // region 3, which end is zero is irrelevant since m weight is 0 + DBGTAG(13) + addBox_slope(true, Rect(Position(r3Xedge, yminf), Position(xpinf, r2Yedge - cslot->seqValignHt())), + tbb, tsb, org, seq_below_wt, 0, true, i); + // region 4 + DBGTAG(14) + addBox_slope(false, Rect(Position(sx + bb.xi, r2Yedge), Position(xpinf, r2Yedge + cslot->seqValignHt())), + tbb, tsb, org, 0, seq_valign_wt, true, i); + // region 5 + DBGTAG(15) + addBox_slope(false, Rect(Position(sx + bb.xi, r2Yedge - cslot->seqValignHt()), Position(xpinf, r2Yedge)), + tbb, tsb, org, seq_below_wt, seq_valign_wt, false, i); + break; + } + case SlotCollision::SEQ_ORDER_LEFTDOWN : + { + float r1Xedge = 0.5f * (bb.xi + bb.xa) + cslot->seqAboveXoff() + sx; + float r3Xedge = bb.xi - cslot->seqBelowXlim() + sx - 0.5f * (tbb.xa - tbb.xi); + float r2Yedge = 0.5f * (bb.yi + bb.ya) + sy; + // DBGTAG(2x) means the regions are up and right + // region 1 + DBGTAG(21) + addBox_slope(true, Rect(Position(r1Xedge, yminf), Position(xpinf, r2Yedge)), + tbb, tsb, org, 0, seq_above_wt, false, i); + // region 2 + DBGTAG(22) + removeBox(Rect(Position(r3Xedge, r2Yedge), Position(xpinf, ypinf)), tbb, tsb, org, i); + // region 3 + DBGTAG(23) + addBox_slope(true, Rect(Position(xminf, r2Yedge - cslot->seqValignHt()), Position(r3Xedge, ypinf)), + tbb, tsb, org, seq_below_wt, 0, false, i); + // region 4 + DBGTAG(24) + addBox_slope(false, Rect(Position(xminf, r2Yedge), Position(sx + bb.xa, r2Yedge + cslot->seqValignHt())), + tbb, tsb, org, 0, seq_valign_wt, true, i); + // region 5 + DBGTAG(25) + addBox_slope(false, Rect(Position(xminf, r2Yedge - cslot->seqValignHt()), + Position(sx + bb.xa, r2Yedge)), tbb, tsb, org, seq_below_wt, seq_valign_wt, false, i); + break; + } + case SlotCollision::SEQ_ORDER_NOABOVE : // enforce neighboring glyph being above + DBGTAG(31); + removeBox(Rect(Position(bb.xi - tbb.xa + sx, sy + bb.ya), + Position(bb.xa - tbb.xi + sx, ypinf)), tbb, tsb, org, i); + break; + case SlotCollision::SEQ_ORDER_NOBELOW : // enforce neighboring glyph being below + DBGTAG(32); + removeBox(Rect(Position(bb.xi - tbb.xa + sx, yminf), + Position(bb.xa - tbb.xi + sx, sy + bb.yi)), tbb, tsb, org, i); + break; + case SlotCollision::SEQ_ORDER_NOLEFT : // enforce neighboring glyph being to the left + DBGTAG(33) + removeBox(Rect(Position(xminf, bb.yi - tbb.ya + sy), + Position(bb.xi - tbb.xa + sx, bb.ya - tbb.yi + sy)), tbb, tsb, org, i); + break; + case SlotCollision::SEQ_ORDER_NORIGHT : // enforce neighboring glyph being to the right + DBGTAG(34) + removeBox(Rect(Position(bb.xa - tbb.xi + sx, bb.yi - tbb.ya + sy), + Position(xpinf, bb.ya - tbb.yi + sy)), tbb, tsb, org, i); + break; + default : + break; + } + } + + if (vmax < cmin - _margin || vmin > cmax + _margin || omax < otmin - _margin || omin > otmax + _margin) + continue; + + // Process sub-boxes that are defined for this glyph. + // We only need to do this if there was in fact a collision with the main octabox. + uint8 numsub = gc.numSubBounds(gid); + if (numsub > 0) + { + bool anyhits = false; + for (int j = 0; j < numsub; ++j) + { + const BBox &sbb = gc.getSubBoundingBBox(gid, j); + const SlantBox &ssb = gc.getSubBoundingSlantBox(gid, j); + switch (i) { + case 0 : // x + vmin = max(max(sbb.xi-tbb.xa+sx, ssb.di-tsb.da+sd+ty), ssb.si-tsb.sa+ss-ty); + vmax = min(min(sbb.xa-tbb.xi+sx, ssb.da-tsb.di+sd+ty), ssb.sa-tsb.si+ss-ty); + omin = sbb.yi + sy; + omax = sbb.ya + sy; + break; + case 1 : // y + vmin = max(max(sbb.yi-tbb.ya+sy, tsb.di-ssb.da-sd+tx), ssb.si-tsb.sa+ss-tx); + vmax = min(min(sbb.ya-tbb.yi+sy, tsb.da-ssb.di-sd+tx), ssb.sa-tsb.si+ss-tx); + omin = sbb.xi + sx; + omax = sbb.xa + sx; + break; + case 2 : // sum + vmin = max(max(ssb.si-tsb.sa+ss, 2*(sbb.yi-tbb.ya+sy)+td), 2*(sbb.xi-tbb.xa+sx)-td); + vmax = min(min(ssb.sa-tsb.si+ss, 2*(sbb.ya-tbb.yi+sy)+td), 2*(sbb.xa-tbb.xi+sx)-td); + omin = ssb.di + sd; + omax = ssb.da + sd; + break; + case 3 : // diff + vmin = max(max(ssb.di-tsb.da+sd, 2*(sbb.xi-tbb.xa+sx)-ts), -2*(sbb.ya-tbb.yi+sy)+ts); + vmax = min(min(ssb.da-tsb.di+sd, 2*(sbb.xa-tbb.xi+sx)-ts), -2*(sbb.yi-tbb.ya+sy)+ts); + omin = ssb.si + ss; + omax = ssb.sa + ss; + break; + } + if (vmax < cmin - _margin || vmin > cmax + _margin || omax < otmin - _margin || omin > otmax + _margin) + continue; + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + dbgout->setenv(1, reinterpret_cast<void *>(j)); +#endif + if (omin > otmax) + _ranges[i].weightedAxis(i, vmin - _margin, vmax + _margin, 0, 0, 0, 0, 0, + sqr(_margin - omin + otmax) * _marginWt, false); + else if (omax < otmin) + _ranges[i].weightedAxis(i, vmin - _margin, vmax + _margin, 0, 0, 0, 0, 0, + sqr(_margin - otmin + omax) * _marginWt, false); + else + _ranges[i].exclude_with_margins(vmin, vmax, i); + anyhits = true; + } + if (anyhits) + isCol = true; + } + else // no sub-boxes + { +#if !defined GRAPHITE2_NTRACING + if (dbgout) + dbgout->setenv(1, reinterpret_cast<void *>(-1)); +#endif + isCol = true; + if (omin > otmax) + _ranges[i].weightedAxis(i, vmin - _margin, vmax + _margin, 0, 0, 0, 0, 0, + sqr(_margin - omin + otmax) * _marginWt, false); + else if (omax < otmin) + _ranges[i].weightedAxis(i, vmin - _margin, vmax + _margin, 0, 0, 0, 0, 0, + sqr(_margin - otmin + omax) * _marginWt, false); + else + _ranges[i].exclude_with_margins(vmin, vmax, i); + + } + } + } + bool res = true; + if (cslot->exclGlyph() > 0 && gc.check(cslot->exclGlyph()) && !isExclusion) + { + // Set up the bogus slot representing the exclusion glyph. + Slot *exclSlot = seg->newSlot(); + exclSlot->setGlyph(seg, cslot->exclGlyph()); + Position exclOrigin(slot->origin() + cslot->exclOffset()); + exclSlot->origin(exclOrigin); + res &= mergeSlot(seg, exclSlot, currShift, isAfter, sameCluster, isCol, true, dbgout ); + seg->freeSlot(exclSlot); + } + hasCol |= isCol; + return res; + +} // end of ShiftCollider::mergeSlot + + +// Figure out where to move the target glyph to, and return the amount to shift by. +Position ShiftCollider::resolve(GR_MAYBE_UNUSED Segment *seg, bool &isCol, GR_MAYBE_UNUSED json * const dbgout) +{ + float tbase; + float totalCost = (float)(std::numeric_limits<float>::max() / 2); + Position resultPos = Position(0, 0); +#if !defined GRAPHITE2_NTRACING + int bestAxis = -1; + if (dbgout) + { + outputJsonDbgStartSlot(dbgout, seg); + *dbgout << "vectors" << json::array; + } +#endif + isCol = true; + for (int i = 0; i < 4; ++i) + { + float bestCost = -1; + float bestPos; + // Calculate the margin depending on whether we are moving diagonally or not: + switch (i) { + case 0 : // x direction + tbase = _currOffset.x; + break; + case 1 : // y direction + tbase = _currOffset.y; + break; + case 2 : // sum (negatively-sloped diagonals) + tbase = _currOffset.x + _currOffset.y; + break; + case 3 : // diff (positively-sloped diagonals) + tbase = _currOffset.x - _currOffset.y; + break; + } + Position testp; + bestPos = _ranges[i].closest(0, bestCost) - tbase; // Get the best relative position +#if !defined GRAPHITE2_NTRACING + if (dbgout) + outputJsonDbgOneVector(dbgout, seg, i, tbase, bestCost, bestPos) ; +#endif + if (bestCost >= 0.0f) + { + isCol = false; + switch (i) { + case 0 : testp = Position(bestPos, _currShift.y); break; + case 1 : testp = Position(_currShift.x, bestPos); break; + case 2 : testp = Position(0.5f * (_currShift.x - _currShift.y + bestPos), 0.5f * (_currShift.y - _currShift.x + bestPos)); break; + case 3 : testp = Position(0.5f * (_currShift.x + _currShift.y + bestPos), 0.5f * (_currShift.x + _currShift.y - bestPos)); break; + } + if (bestCost < totalCost - 0.01f) + { + totalCost = bestCost; + resultPos = testp; +#if !defined GRAPHITE2_NTRACING + bestAxis = i; +#endif + } + } + } // end of loop over 4 directions + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + outputJsonDbgEndSlot(dbgout, resultPos, bestAxis, isCol); +#endif + + return resultPos; + +} // end of ShiftCollider::resolve + + +#if !defined GRAPHITE2_NTRACING + +void ShiftCollider::outputJsonDbg(json * const dbgout, Segment *seg, int axis) +{ + int axisMax = axis; + if (axis < 0) // output all axes + { + *dbgout << "gid" << _target->gid() + << "limit" << _limit + << "target" << json::object + << "origin" << _target->origin() + << "margin" << _margin + << "bbox" << seg->theGlyphBBoxTemporary(_target->gid()) + << "slantbox" << seg->getFace()->glyphs().slant(_target->gid()) + << json::close; // target object + *dbgout << "ranges" << json::array; + axis = 0; + axisMax = 3; + } + for (int iAxis = axis; iAxis <= axisMax; ++iAxis) + { + *dbgout << json::flat << json::array << _ranges[iAxis].position(); + for (Zones::const_iterator s = _ranges[iAxis].begin(), e = _ranges[iAxis].end(); s != e; ++s) + *dbgout << json::flat << json::array + << Position(s->x, s->xm) << s->sm << s->smx << s->c + << json::close; + *dbgout << json::close; + } + if (axis < axisMax) // looped through the _ranges array for all axes + *dbgout << json::close; // ranges array +} + +void ShiftCollider::outputJsonDbgStartSlot(json * const dbgout, Segment *seg) +{ + *dbgout << json::object // slot - not closed till the end of the caller method + << "slot" << objectid(dslot(seg, _target)) + << "gid" << _target->gid() + << "limit" << _limit + << "target" << json::object + << "origin" << _origin + << "currShift" << _currShift + << "currOffset" << seg->collisionInfo(_target)->offset() + << "bbox" << seg->theGlyphBBoxTemporary(_target->gid()) + << "slantBox" << seg->getFace()->glyphs().slant(_target->gid()) + << "fix" << "shift"; + *dbgout << json::close; // target object +} + +void ShiftCollider::outputJsonDbgEndSlot(GR_MAYBE_UNUSED json * const dbgout, + Position resultPos, int bestAxis, bool isCol) +{ + *dbgout << json::close // vectors array + << "result" << resultPos + //<< "scraping" << _scraping[bestAxis] + << "bestAxis" << bestAxis + << "stillBad" << isCol + << json::close; // slot object +} + +void ShiftCollider::outputJsonDbgOneVector(json * const dbgout, Segment *seg, int axis, + float tleft, float bestCost, float bestVal) +{ + const char * label; + switch (axis) + { + case 0: label = "x"; break; + case 1: label = "y"; break; + case 2: label = "sum (NE-SW)"; break; + case 3: label = "diff (NW-SE)"; break; + default: label = "???"; break; + } + + *dbgout << json::object // vector + << "direction" << label + << "targetMin" << tleft; + + outputJsonDbgRemovals(dbgout, axis, seg); + + *dbgout << "ranges"; + outputJsonDbg(dbgout, seg, axis); + + *dbgout << "bestCost" << bestCost + << "bestVal" << bestVal + tleft + << json::close; // vectors object +} + +void ShiftCollider::outputJsonDbgRemovals(json * const dbgout, int axis, Segment *seg) +{ + *dbgout << "removals" << json::array; + _ranges[axis].jsonDbgOut(seg); + *dbgout << json::close; // removals array +} + +#endif // !defined GRAPHITE2_NTRACING + + +//// KERN-COLLIDER //// + +inline +static float localmax (float al, float au, float bl, float bu, float x) +{ + if (al < bl) + { if (au < bu) return au < x ? au : x; } + else if (au > bu) return bl < x ? bl : x; + return x; +} + +inline +static float localmin(float al, float au, float bl, float bu, float x) +{ + if (bl > al) + { if (bu > au) return bl > x ? bl : x; } + else if (au > bu) return al > x ? al : x; + return x; +} + +// Return the given edge of the glyph at height y, taking any slant box into account. +static float get_edge(Segment *seg, const Slot *s, const Position &shift, float y, float width, bool isRight) +{ + const GlyphCache &gc = seg->getFace()->glyphs(); + unsigned short gid = s->gid(); + float sx = s->origin().x + shift.x; + float sy = s->origin().y + shift.y; + uint8 numsub = gc.numSubBounds(gid); + float res = isRight ? (float)-1e38 : (float)1e38; + + if (numsub > 0) + { + for (int i = 0; i < numsub; ++i) + { + const BBox &sbb = gc.getSubBoundingBBox(gid, i); + const SlantBox &ssb = gc.getSubBoundingSlantBox(gid, i); + if (sy + sbb.yi > y + width / 2 || sy + sbb.ya < y - width / 2) + continue; + if (isRight) + { + float x = sx + sbb.xa; + if (x > res) + { + float td = sx - sy + ssb.da + y; + float ts = sx + sy + ssb.sa - y; + x = localmax(td - width / 2, td + width / 2, ts - width / 2, ts + width / 2, x); + if (x > res) + res = x; + } + } + else + { + float x = sx + sbb.xi; + if (x < res) + { + float td = sx - sy + ssb.di + y; + float ts = sx + sy + ssb.si - y; + x = localmin(td - width / 2, td + width / 2, ts - width / 2, ts + width / 2, x); + if (x < res) + res = x; + } + } + } + } + else + { + const BBox &bb = gc.getBoundingBBox(gid); + const SlantBox &sb = gc.getBoundingSlantBox(gid); + float td = sx - sy + y; + float ts = sx + sy - y; + if (isRight) + res = localmax(td + sb.da - width / 2, td + sb.da + width / 2, ts + sb.sa - width / 2, ts + sb.sa + width / 2, sx + bb.xa); + else + res = localmin(td + sb.di - width / 2, td + sb.di + width / 2, ts + sb.si - width / 2, ts + sb.si + width / 2, sx + bb.xi); + } + return res; +} + + +bool KernCollider::initSlot(Segment *seg, Slot *aSlot, const Rect &limit, float margin, + const Position &currShift, const Position &offsetPrev, int dir, + float ymin, float ymax, GR_MAYBE_UNUSED json * const dbgout) +{ + const GlyphCache &gc = seg->getFace()->glyphs(); + const Slot *base = aSlot; + // const Slot *last = aSlot; + const Slot *s; + int numSlices; + while (base->attachedTo()) + base = base->attachedTo(); + if (margin < 10) margin = 10; + + _limit = limit; + _offsetPrev = offsetPrev; // kern from a previous pass + + // Calculate the height of the glyph and how many horizontal slices to use. + if (_maxy >= 1e37f) + { + _maxy = ymax; + _miny = ymin; + _sliceWidth = margin / 1.5f; + numSlices = int((_maxy - _miny + 2) / (_sliceWidth / 1.5f) + 1.f); // +2 helps with rounding errors + _edges.clear(); + _edges.insert(_edges.begin(), numSlices, (dir & 1) ? 1e38f : -1e38f); + _xbound = (dir & 1) ? (float)1e38f : (float)-1e38f; + } + else if (_maxy != ymax || _miny != ymin) + { + if (_miny != ymin) + { + numSlices = int((ymin - _miny) / _sliceWidth - 1); + _miny += numSlices * _sliceWidth; + if (numSlices < 0) + _edges.insert(_edges.begin(), -numSlices, (dir & 1) ? 1e38f : -1e38f); + else if ((unsigned)numSlices < _edges.size()) // this shouldn't fire since we always grow the range + { + Vector<float>::iterator e = _edges.begin(); + while (numSlices--) + ++e; + _edges.erase(_edges.begin(), e); + } + } + if (_maxy != ymax) + { + numSlices = int((ymax - _miny) / _sliceWidth + 1); + _maxy = numSlices * _sliceWidth + _miny; + if (numSlices > (int)_edges.size()) + _edges.insert(_edges.end(), numSlices - _edges.size(), (dir & 1) ? 1e38f : -1e38f); + else if (numSlices < (int)_edges.size()) // this shouldn't fire since we always grow the range + { + while ((int)_edges.size() > numSlices) + _edges.pop_back(); + } + } + } + numSlices = _edges.size(); + +#if !defined GRAPHITE2_NTRACING + // Debugging + _seg = seg; + _slotNear.clear(); + _slotNear.insert(_slotNear.begin(), numSlices, NULL); + _nearEdges.clear(); + _nearEdges.insert(_nearEdges.begin(), numSlices, (dir & 1) ? -1e38f : +1e38f); +#endif + + // Determine the trailing edge of each slice (ie, left edge for a RTL glyph). + for (s = base; s; s = s->nextInCluster(s)) + { + SlotCollision *c = seg->collisionInfo(s); + if (!gc.check(s->gid())) + return false; + const BBox &bs = gc.getBoundingBBox(s->gid()); + float x = s->origin().x + c->shift().x + ((dir & 1) ? bs.xi : bs.xa); + // Loop over slices. + // Note smin might not be zero if glyph s is not at the bottom of the cluster; similarly for smax. + float toffset = c->shift().y - _miny + 1 + s->origin().y; + int smin = max(0, int((bs.yi + toffset) / _sliceWidth)); + int smax = min(numSlices - 1, int((bs.ya + toffset) / _sliceWidth + 1)); + for (int i = smin; i <= smax; ++i) + { + float t; + float y = _miny - 1 + (i + .5f) * _sliceWidth; // vertical center of slice + if ((dir & 1) && x < _edges[i]) + { + t = get_edge(seg, s, c->shift(), y, _sliceWidth, false); + if (t < _edges[i]) + { + _edges[i] = t; + if (t < _xbound) + _xbound = t; + } + } + else if (!(dir & 1) && x > _edges[i]) + { + t = get_edge(seg, s, c->shift(), y, _sliceWidth, true); + if (t > _edges[i]) + { + _edges[i] = t; + if (t > _xbound) + _xbound = t; + } + } + } + } + _mingap = (float)1e38; + _target = aSlot; + _margin = margin; + _currShift = currShift; + return true; +} // end of KernCollider::initSlot + + +// Determine how much the target slot needs to kern away from the given slot. +// In other words, merge information from given slot's position with what the target slot knows +// about how it can kern. +// Return false if we know there is no collision, true if we think there might be one. +bool KernCollider::mergeSlot(Segment *seg, Slot *slot, const Position &currShift, float currSpace, int dir, GR_MAYBE_UNUSED json * const dbgout) +{ + int rtl = (dir & 1) * 2 - 1; + if (!seg->getFace()->glyphs().check(slot->gid())) + return false; + const Rect &bb = seg->theGlyphBBoxTemporary(slot->gid()); + const float sx = slot->origin().x + currShift.x; + float x = sx + (rtl > 0 ? bb.tr.x : bb.bl.x); + // this isn't going to reduce _mingap so skip + if ((rtl > 0 && x < _xbound - _mingap - currSpace) || (rtl <= 0 && x > _xbound + _mingap + currSpace)) + return false; + + const float sy = slot->origin().y + currShift.y; + int smin = max(0, int((bb.bl.y + (1 - _miny + sy)) / _sliceWidth + 1)); + int smax = min((int)_edges.size() - 1, int((bb.tr.y + (1 - _miny + sy)) / _sliceWidth + 1)); + bool collides = false; + + for (int i = smin; i <= smax; ++i) + { + float t; + float y = (float)(_miny - 1 + (i + .5f) * _sliceWidth); // vertical center of slice + if (x * rtl > _edges[i] * rtl - _mingap - currSpace) + { + // 2 * currSpace to account for the space that is already separating them and the space we want to add + float m = get_edge(seg, slot, currShift, y, _sliceWidth, rtl > 0) + 2 * rtl * currSpace; + t = rtl * (_edges[i] - m); + // Check slices above and below (if any). + if (i < (int)_edges.size() - 1) t = min(t, rtl * (_edges[i+1] - m)); + if (i > 0) t = min(t, rtl * (_edges[i-1] - m)); + // _mingap is positive to shrink + if (t < _mingap) + { + _mingap = t; + collides = true; + } +#if !defined GRAPHITE2_NTRACING + // Debugging - remember the closest neighboring edge for this slice. + if (rtl * m > rtl * _nearEdges[i]) + { + _slotNear[i] = slot; + _nearEdges[i] = m; + } +#endif + } + } + return collides; // note that true is not a necessarily reliable value + +} // end of KernCollider::mergeSlot + + +// Return the amount to kern by. +Position KernCollider::resolve(GR_MAYBE_UNUSED Segment *seg, GR_MAYBE_UNUSED Slot *slot, + int dir, float margin, GR_MAYBE_UNUSED json * const dbgout) +{ + float resultNeeded = (1 - 2 * (dir & 1)) * (_mingap - margin); + float result = min(_limit.tr.x - _offsetPrev.x, max(resultNeeded, _limit.bl.x - _offsetPrev.x)); + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + { + *dbgout << json::object // slot + << "slot" << objectid(dslot(seg, _target)) + << "gid" << _target->gid() + << "margin" << _margin + << "limit" << _limit + << "miny" << _miny + << "maxy" << _maxy + << "slicewidth" << _sliceWidth + << "target" << json::object + << "origin" << _target->origin() + //<< "currShift" << _currShift + << "offsetPrev" << _offsetPrev + << "bbox" << seg->theGlyphBBoxTemporary(_target->gid()) + << "slantBox" << seg->getFace()->glyphs().slant(_target->gid()) + << "fix" << "kern" + << json::close; // target object + + *dbgout << "slices" << json::array; + for (int is = 0; is < (int)_edges.size(); is++) + { + *dbgout << json::flat << json::object + << "i" << is + << "targetEdge" << _edges[is] + << "neighbor" << objectid(dslot(seg, _slotNear[is])) + << "nearEdge" << _nearEdges[is] + << json::close; + } + *dbgout << json::close; // slices array + + *dbgout + << "xbound" << _xbound + << "minGap" << _mingap + << "needed" << resultNeeded + << "result" << result + << "stillBad" << (result != resultNeeded) + << json::close; // slot object + } +#endif + + return Position(result, 0.); + +} // end of KernCollider::resolve + +void KernCollider::shift(const Position &mv, int dir) +{ + for (Vector<float>::iterator e = _edges.begin(); e != _edges.end(); ++e) + *e += mv.x; + _xbound += (1 - 2 * (dir & 1)) * mv.x; +} + +//// SLOT-COLLISION //// + +// Initialize the collision attributes for the given slot. +SlotCollision::SlotCollision(Segment *seg, Slot *slot) +{ + initFromSlot(seg, slot); +} + +void SlotCollision::initFromSlot(Segment *seg, Slot *slot) +{ + // Initialize slot attributes from glyph attributes. + // The order here must match the order in the grcompiler code, + // GrcSymbolTable::AssignInternalGlyphAttrIDs. + uint16 gid = slot->gid(); + uint16 aCol = seg->silf()->aCollision(); // flags attr ID + const GlyphFace * glyphFace = seg->getFace()->glyphs().glyphSafe(gid); + if (!glyphFace) + return; + const sparse &p = glyphFace->attrs(); + _flags = p[aCol]; + _limit = Rect(Position(p[aCol+1], p[aCol+2]), + Position(p[aCol+3], p[aCol+4])); + _margin = p[aCol+5]; + _marginWt = p[aCol+6]; + + _seqClass = p[aCol+7]; + _seqProxClass = p[aCol+8]; + _seqOrder = p[aCol+9]; + _seqAboveXoff = p[aCol+10]; + _seqAboveWt = p[aCol+11]; + _seqBelowXlim = p[aCol+12]; + _seqBelowWt = p[aCol+13]; + _seqValignHt = p[aCol+14]; + _seqValignWt = p[aCol+15]; + + // These attributes do not have corresponding glyph attribute: + _exclGlyph = 0; + _exclOffset = Position(0, 0); +} + +float SlotCollision::getKern(int dir) const +{ + if ((_flags & SlotCollision::COLL_KERN) != 0) + return float(_shift.x * ((dir & 1) ? -1 : 1)); + else + return 0; +} + diff --git a/gfx/graphite2/src/Decompressor.cpp b/gfx/graphite2/src/Decompressor.cpp new file mode 100644 index 000000000..084570ff3 --- /dev/null +++ b/gfx/graphite2/src/Decompressor.cpp @@ -0,0 +1,113 @@ +/* GRAPHITE2 LICENSING + + Copyright 2015, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ +#include <cassert> + +#include "inc/Decompressor.h" +#include "inc/Compression.h" + +using namespace lz4; + +namespace { + +inline +u32 read_literal(u8 const * &s, u8 const * const e, u32 l) { + if (l == 15 && s != e) + { + u8 b = 0; + do { l += b = *s++; } while(b==0xff && s != e); + } + return l; +} + +bool read_sequence(u8 const * &src, u8 const * const end, u8 const * &literal, u32 & literal_len, u32 & match_len, u32 & match_dist) +{ + u8 const token = *src++; + + literal_len = read_literal(src, end, token >> 4); + literal = src; + src += literal_len; + + if (src > end - 2) + return false; + + match_dist = *src++; + match_dist |= *src++ << 8; + match_len = read_literal(src, end, token & 0xf); + + return src <= end-5; +} + +} + +int lz4::decompress(void const *in, size_t in_size, void *out, size_t out_size) +{ + if (out_size <= in_size || in_size < sizeof(unsigned long)+1) + return -1; + + u8 const * src = static_cast<u8 const *>(in), + * literal = 0, + * const src_end = src + in_size; + + u8 * dst = static_cast<u8*>(out), + * const dst_end = dst + out_size; + + u32 literal_len = 0, + match_len = 0, + match_dist = 0; + + while (read_sequence(src, src_end, literal, literal_len, match_len, match_dist)) + { + if (literal_len != 0) + { + // Copy in literal. At this point the last full sequence must be at + // least MINMATCH + 5 from the end of the output buffer. + if (dst + align(literal_len) > dst_end - (MINMATCH+5)) + return -1; + dst = overrun_copy(dst, literal, literal_len); + } + + // Copy, possibly repeating, match from earlier in the + // decoded output. + u8 const * const pcpy = dst - match_dist; + if (pcpy < static_cast<u8*>(out) + || dst + match_len + MINMATCH > dst_end - 5) + return -1; + if (dst > pcpy+sizeof(unsigned long) + && dst + align(match_len + MINMATCH) <= dst_end) + dst = overrun_copy(dst, pcpy, match_len + MINMATCH); + else + dst = safe_copy(dst, pcpy, match_len + MINMATCH); + } + + if (literal + literal_len > src_end + || dst + literal_len > dst_end) + return -1; + dst = fast_copy(dst, literal, literal_len); + + return dst - (u8*)out; +} + diff --git a/gfx/graphite2/src/Face.cpp b/gfx/graphite2/src/Face.cpp index 35fca80fd..ab77d7220 100644 --- a/gfx/graphite2/src/Face.cpp +++ b/gfx/graphite2/src/Face.cpp @@ -28,6 +28,7 @@ of the License or (at your option) any later version. #include "graphite2/Segment.h" #include "inc/CmapCache.h" #include "inc/debug.h" +#include "inc/Decompressor.h" #include "inc/Endian.h" #include "inc/Face.h" #include "inc/FileFace.h" @@ -36,10 +37,20 @@ of the License or (at your option) any later version. #include "inc/SegCacheStore.h" #include "inc/Segment.h" #include "inc/NameTable.h" - +#include "inc/Error.h" using namespace graphite2; +namespace +{ +enum compression +{ + NONE, + LZ4 +}; + +} + Face::Face(const void* appFaceHandle/*non-NULL*/, const gr_face_ops & ops) : m_appFaceHandle(appFaceHandle), m_pFileFace(NULL), @@ -47,6 +58,7 @@ Face::Face(const void* appFaceHandle/*non-NULL*/, const gr_face_ops & ops) m_cmap(NULL), m_pNames(NULL), m_logger(NULL), + m_error(0), m_errcntxt(0), m_silfs(NULL), m_numSilf(0), m_ascent(0), @@ -78,17 +90,26 @@ float Face::default_glyph_advance(const void* font_ptr, gr_uint16 glyphid) bool Face::readGlyphs(uint32 faceOptions) { + Error e; +#ifdef GRAPHITE2_TELEMETRY + telemetry::category _glyph_cat(tele.glyph); +#endif + error_context(EC_READGLYPHS); + m_pGlyphFaceCache = new GlyphCache(*this, faceOptions); + + if (e.test(!m_pGlyphFaceCache, E_OUTOFMEM) + || e.test(m_pGlyphFaceCache->numGlyphs() == 0, E_NOGLYPHS) + || e.test(m_pGlyphFaceCache->unitsPerEm() == 0, E_BADUPEM)) + { + return error(e); + } + if (faceOptions & gr_face_cacheCmap) - m_cmap = new CachedCmap(*this); + m_cmap = new CachedCmap(*this); else - m_cmap = new DirectCmap(*this); - - m_pGlyphFaceCache = new GlyphCache(*this, faceOptions); - if (!m_pGlyphFaceCache - || m_pGlyphFaceCache->numGlyphs() == 0 - || m_pGlyphFaceCache->unitsPerEm() == 0 - || !m_cmap || !*m_cmap) - return false; + m_cmap = new DirectCmap(*this); + if (e.test(!m_cmap, E_OUTOFMEM) || e.test(!*m_cmap, E_BADCMAP)) + return error(e); if (faceOptions & gr_face_preloadGlyphs) nameTable(); // preload the name table along with the glyphs. @@ -98,24 +119,32 @@ bool Face::readGlyphs(uint32 faceOptions) bool Face::readGraphite(const Table & silf) { +#ifdef GRAPHITE2_TELEMETRY + telemetry::category _silf_cat(tele.silf); +#endif + Error e; + error_context(EC_READSILF); const byte * p = silf; - if (!p) return false; + if (e.test(!p, E_NOSILF) || e.test(silf.size() < 20, E_BADSIZE)) return error(e); const uint32 version = be::read<uint32>(p); - if (version < 0x00020000) return false; + if (e.test(version < 0x00020000, E_TOOOLD)) return error(e); if (version >= 0x00030000) - be::skip<uint32>(p); // compilerVersion + be::skip<uint32>(p); // compilerVersion m_numSilf = be::read<uint16>(p); - be::skip<uint16>(p); // reserved + + be::skip<uint16>(p); // reserved bool havePasses = false; m_silfs = new Silf[m_numSilf]; + if (e.test(!m_silfs, E_OUTOFMEM)) return error(e); for (int i = 0; i < m_numSilf; i++) { + error_context(EC_ASILF + (i << 8)); const uint32 offset = be::read<uint32>(p), - next = i == m_numSilf - 1 ? silf.size() : be::peek<uint32>(p); - if (next > silf.size() || offset >= next) - return false; + next = i == m_numSilf - 1 ? silf.size() : be::peek<uint32>(p); + if (e.test(next > silf.size() || offset >= next, E_BADSIZE)) + return error(e); if (!m_silfs[i].readGraphite(silf + offset, next - offset, *this, version)) return false; @@ -138,33 +167,43 @@ bool Face::runGraphite(Segment *seg, const Silf *aSilf) const json * dbgout = logger(); if (dbgout) { - *dbgout << json::object - << "id" << objectid(seg) - << "passes" << json::array; + *dbgout << json::object + << "id" << objectid(seg) + << "passes" << json::array; } #endif - bool res = aSilf->runGraphite(seg, 0, aSilf->justificationPass()); +// if ((seg->dir() & 1) != aSilf->dir()) +// seg->reverseSlots(); + if ((seg->dir() & 3) == 3 && aSilf->bidiPass() == 0xFF) + seg->doMirror(aSilf->aMirror()); + bool res = aSilf->runGraphite(seg, 0, aSilf->positionPass(), true); if (res) - res = aSilf->runGraphite(seg, aSilf->positionPass(), aSilf->numPasses()); + { + seg->associateChars(0, seg->charInfoCount()); + if (aSilf->flags() & 0x20) + res &= seg->initCollisions(); + res &= aSilf->runGraphite(seg, aSilf->positionPass(), aSilf->numPasses(), false); + } #if !defined GRAPHITE2_NTRACING - if (dbgout) -{ - *dbgout << json::item - << json::close // Close up the passes array - << "output" << json::array; - for(Slot * s = seg->first(); s; s = s->next()) - *dbgout << dslot(seg, s); - seg->finalise(0); // Call this here to fix up charinfo back indexes. - *dbgout << json::close - << "advance" << seg->advance() - << "chars" << json::array; - for(size_t i = 0, n = seg->charInfoCount(); i != n; ++i) - *dbgout << json::flat << *seg->charinfo(i); - *dbgout << json::close // Close up the chars array - << json::close; // Close up the segment object - } + if (dbgout) +{ + seg->positionSlots(0, 0, 0, aSilf->dir()); + *dbgout << json::item + << json::close // Close up the passes array + << "output" << json::array; + for(Slot * s = seg->first(); s; s = s->next()) + *dbgout << dslot(seg, s); + seg->finalise(0); // Call this here to fix up charinfo back indexes. + *dbgout << json::close + << "advance" << seg->advance() + << "chars" << json::array; + for(size_t i = 0, n = seg->charInfoCount(); i != n; ++i) + *dbgout << json::flat << *seg->charinfo(i); + *dbgout << json::close // Close up the chars array + << json::close; // Close up the segment object + } #endif return res; @@ -199,7 +238,9 @@ uint16 Face::getGlyphMetric(uint16 gid, uint8 metric) const { case kgmetAscent : return m_ascent; case kgmetDescent : return m_descent; - default: return glyphs().glyph(gid)->getMetric(metric); + default: + if (gid > glyphs().numGlyphs()) return 0; + return glyphs().glyph(gid)->getMetric(metric); } } @@ -231,17 +272,32 @@ uint16 Face::languageForLocale(const char * locale) const return 0; } -Face::Table::Table(const Face & face, const Tag n) throw() -: _f(&face) + + +Face::Table::Table(const Face & face, const Tag n, uint32 version) throw() +: _f(&face), _compressed(false) { size_t sz = 0; - _p = reinterpret_cast<const byte *>((*_f->m_ops.get_table)(_f->m_appFaceHandle, n, &sz)); + _p = static_cast<const byte *>((*_f->m_ops.get_table)(_f->m_appFaceHandle, n, &sz)); _sz = uint32(sz); + if (!TtfUtil::CheckTable(n, _p, _sz)) { this->~Table(); // Make sure we release the table buffer even if the table filed it's checks - _p = 0; _sz = 0; + return; } + + if (be::peek<uint32>(_p) >= version) + decompress(); +} + +void Face::Table::releaseBuffers() +{ + if (_compressed) + free(const_cast<byte *>(_p)); + else if (_p && _f->m_ops.release_table) + (*_f->m_ops.release_table)(_f->m_appFaceHandle, _p); + _p = 0; _sz = 0; } Face::Table & Face::Table::operator = (const Table & rhs) throw() @@ -253,3 +309,58 @@ Face::Table & Face::Table::operator = (const Table & rhs) throw() return *this; } +Error Face::Table::decompress() +{ + Error e; + if (e.test(_sz < 5 * sizeof(uint32), E_BADSIZE)) + return e; + byte * uncompressed_table = 0; + size_t uncompressed_size = 0; + + const byte * p = _p; + const uint32 version = be::read<uint32>(p); // Table version number. + + // The scheme is in the top 5 bits of the 1st uint32. + const uint32 hdr = be::read<uint32>(p); + switch(compression(hdr >> 27)) + { + case NONE: return e; + + case LZ4: + { + uncompressed_size = hdr & 0x07ffffff; + uncompressed_table = gralloc<byte>(uncompressed_size); + if (!e.test(!uncompressed_table, E_OUTOFMEM)) + // coverity[forward_null : FALSE] - uncompressed_table has been checked so can't be null + // coverity[checked_return : FALSE] - we test e later + e.test(lz4::decompress(p, _sz - 2*sizeof(uint32), uncompressed_table, uncompressed_size) != signed(uncompressed_size), E_SHRINKERFAILED); + break; + } + + default: + e.error(E_BADSCHEME); + }; + + // Check the uncompressed version number against the original. + if (!e) + // coverity[forward_null : FALSE] - uncompressed_table has already been tested so can't be null + // coverity[checked_return : FALSE] - we test e later + e.test(be::peek<uint32>(uncompressed_table) != version, E_SHRINKERFAILED); + + // Tell the provider to release the compressed form since were replacing + // it anyway. + releaseBuffers(); + + if (e) + { + free(uncompressed_table); + uncompressed_table = 0; + uncompressed_size = 0; + } + + _p = uncompressed_table; + _sz = uncompressed_size; + _compressed = true; + + return e; +} diff --git a/gfx/graphite2/src/FeatureMap.cpp b/gfx/graphite2/src/FeatureMap.cpp index aa638d5cb..b8c840527 100644 --- a/gfx/graphite2/src/FeatureMap.cpp +++ b/gfx/graphite2/src/FeatureMap.cpp @@ -41,38 +41,38 @@ using namespace graphite2; namespace { - static int cmpNameAndFeatures(const void *ap, const void *bp) - { - const NameAndFeatureRef & a = *static_cast<const NameAndFeatureRef *>(ap), - & b = *static_cast<const NameAndFeatureRef *>(bp); - return (a < b ? -1 : (b < a ? 1 : 0)); - } - - const size_t FEAT_HEADER = sizeof(uint32) + 2*sizeof(uint16) + sizeof(uint32), - FEATURE_SIZE = sizeof(uint32) - + 2*sizeof(uint16) - + sizeof(uint32) - + 2*sizeof(uint16), - FEATURE_SETTING_SIZE = sizeof(int16) + sizeof(uint16); - - uint16 readFeatureSettings(const byte * p, FeatureSetting * s, size_t num_settings) - { - uint16 max_val = 0; + static int cmpNameAndFeatures(const void *ap, const void *bp) + { + const NameAndFeatureRef & a = *static_cast<const NameAndFeatureRef *>(ap), + & b = *static_cast<const NameAndFeatureRef *>(bp); + return (a < b ? -1 : (b < a ? 1 : 0)); + } + + const size_t FEAT_HEADER = sizeof(uint32) + 2*sizeof(uint16) + sizeof(uint32), + FEATURE_SIZE = sizeof(uint32) + + 2*sizeof(uint16) + + sizeof(uint32) + + 2*sizeof(uint16), + FEATURE_SETTING_SIZE = sizeof(int16) + sizeof(uint16); + + uint16 readFeatureSettings(const byte * p, FeatureSetting * s, size_t num_settings) + { + uint16 max_val = 0; for (FeatureSetting * const end = s + num_settings; s != end; ++s) { - const int16 value = be::read<int16>(p); + const int16 value = be::read<int16>(p); ::new (s) FeatureSetting(value, be::read<uint16>(p)); - if (uint16(value) > max_val) max_val = value; + if (uint16(value) > max_val) max_val = value; } return max_val; - } + } } FeatureRef::FeatureRef(const Face & face, - unsigned short & bits_offset, uint32 max_val, - uint32 name, uint16 uiName, uint16 flags, - FeatureSetting *settings, uint16 num_set) throw() + unsigned short & bits_offset, uint32 max_val, + uint32 name, uint16 uiName, uint16 flags, + FeatureSetting *settings, uint16 num_set) throw() : m_pFace(&face), m_nameValues(settings), m_mask(mask_over_val(max_val)), @@ -82,13 +82,13 @@ FeatureRef::FeatureRef(const Face & face, m_flags(flags), m_numSet(num_set) { - const uint8 need_bits = bit_set_count(m_mask); - m_index = (bits_offset + need_bits) / SIZEOF_CHUNK; - if (m_index > bits_offset / SIZEOF_CHUNK) - bits_offset = m_index*SIZEOF_CHUNK; - m_bits = bits_offset % SIZEOF_CHUNK; - bits_offset += need_bits; - m_mask <<= m_bits; + const uint8 need_bits = bit_set_count(m_mask); + m_index = (bits_offset + need_bits) / SIZEOF_CHUNK; + if (m_index > bits_offset / SIZEOF_CHUNK) + bits_offset = m_index*SIZEOF_CHUNK; + m_bits = bits_offset % SIZEOF_CHUNK; + bits_offset += need_bits; + m_mask <<= m_bits; } FeatureRef::~FeatureRef() throw() @@ -104,7 +104,7 @@ bool FeatureMap::readFeats(const Face & face) if (feat.size() < FEAT_HEADER) return false; const byte *const feat_start = p, - *const feat_end = p + feat.size(); + *const feat_end = p + feat.size(); const uint32 version = be::read<uint32>(p); m_numFeats = be::read<uint16>(p); @@ -112,29 +112,31 @@ bool FeatureMap::readFeats(const Face & face) be::skip<uint32>(p); // Sanity checks - if (m_numFeats == 0) return true; + if (m_numFeats == 0) return true; if (version < 0x00010000 || - p + m_numFeats*FEATURE_SIZE > feat_end) + p + m_numFeats*FEATURE_SIZE > feat_end) { //defensive - m_numFeats = 0; - return false; + m_numFeats = 0; + return false; } m_feats = new FeatureRef [m_numFeats]; - uint16 * const defVals = gralloc<uint16>(m_numFeats); + uint16 * const defVals = gralloc<uint16>(m_numFeats); + if (!defVals || !m_feats) return false; unsigned short bits = 0; //to cause overflow on first Feature for (int i = 0, ie = m_numFeats; i != ie; i++) { - const uint32 label = version < 0x00020000 ? be::read<uint16>(p) : be::read<uint32>(p); - const uint16 num_settings = be::read<uint16>(p); + const uint32 label = version < 0x00020000 ? be::read<uint16>(p) : be::read<uint32>(p); + const uint16 num_settings = be::read<uint16>(p); if (version >= 0x00020000) - be::skip<uint16>(p); - const byte * const feat_setts = feat_start + be::read<uint32>(p); - const uint16 flags = be::read<uint16>(p), - uiName = be::read<uint16>(p); + be::skip<uint16>(p); + const uint32 settings_offset = be::read<uint32>(p); + const uint16 flags = be::read<uint16>(p), + uiName = be::read<uint16>(p); - if (feat_setts + num_settings * FEATURE_SETTING_SIZE > feat_end) + if (settings_offset > size_t(feat_end - feat_start) + || settings_offset + num_settings * FEATURE_SETTING_SIZE > size_t(feat_end - feat_start)) { free(defVals); return false; @@ -144,26 +146,36 @@ bool FeatureMap::readFeats(const Face & face) uint32 maxVal; if (num_settings != 0) { - uiSet = gralloc<FeatureSetting>(num_settings); - maxVal = readFeatureSettings(feat_setts, uiSet, num_settings); - defVals[i] = uiSet[0].value(); + uiSet = gralloc<FeatureSetting>(num_settings); + if (!uiSet) + { + free(defVals); + return false; + } + maxVal = readFeatureSettings(feat_start + settings_offset, uiSet, num_settings); + defVals[i] = uiSet[0].value(); } else { - uiSet = 0; - maxVal = 0xffffffff; - defVals[i] = 0; + uiSet = 0; + maxVal = 0xffffffff; + defVals[i] = 0; } - ::new (m_feats + i) FeatureRef (face, bits, maxVal, - label, uiName, flags, - uiSet, num_settings); + ::new (m_feats + i) FeatureRef (face, bits, maxVal, + label, uiName, flags, + uiSet, num_settings); } - m_defaultFeatures = new Features(bits/(sizeof(uint32)*8) + 1, *this); + new (&m_defaultFeatures) Features(bits/(sizeof(uint32)*8) + 1, *this); m_pNamedFeats = new NameAndFeatureRef[m_numFeats]; + if (!m_pNamedFeats) + { + free(defVals); + return false; + } for (int i = 0; i < m_numFeats; ++i) { - m_feats[i].applyValToFeature(defVals[i], *m_defaultFeatures); + m_feats[i].applyValToFeature(defVals[i], m_defaultFeatures); m_pNamedFeats[i] = m_feats+i; } @@ -184,7 +196,7 @@ bool SillMap::readFace(const Face & face) bool SillMap::readSill(const Face & face) { - const Face::Table sill(face, TtfUtil::Tag::Sill); + const Face::Table sill(face, TtfUtil::Tag::Sill); const byte *p = sill; if (!p) return true; @@ -203,7 +215,8 @@ bool SillMap::readSill(const Face & face) uint16 numSettings = be::read<uint16>(p); uint16 offset = be::read<uint16>(p); if (offset + 8U * numSettings > sill.size() && numSettings > 0) return false; - Features* feats = new Features(*m_FeatureMap.m_defaultFeatures); + Features* feats = new Features(m_FeatureMap.m_defaultFeatures); + if (!feats) return false; const byte *pLSet = sill + offset; // Apply langauge specific settings @@ -213,11 +226,11 @@ bool SillMap::readSill(const Face & face) uint16 val = be::read<uint16>(pLSet); pLSet += 2; const FeatureRef* pRef = m_FeatureMap.findFeatureRef(name); - if (pRef) pRef->applyValToFeature(val, *feats); + if (pRef) pRef->applyValToFeature(val, *feats); } // Add the language id feature which is always feature id 1 const FeatureRef* pRef = m_FeatureMap.findFeatureRef(1); - if (pRef) pRef->applyValToFeature(langid, *feats); + if (pRef) pRef->applyValToFeature(langid, *feats); m_langFeats[i].m_lang = langid; m_langFeats[i].m_pFeatures = feats; @@ -238,7 +251,7 @@ Features* SillMap::cloneFeatures(uint32 langname/*0 means default*/) const return new Features(*m_langFeats[i].m_pFeatures); } } - return new Features (*m_FeatureMap.m_defaultFeatures); + return new Features (m_FeatureMap.m_defaultFeatures); } diff --git a/gfx/graphite2/src/FileFace.cpp b/gfx/graphite2/src/FileFace.cpp index 79a321353..43aff71a7 100644 --- a/gfx/graphite2/src/FileFace.cpp +++ b/gfx/graphite2/src/FileFace.cpp @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2012, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -83,7 +83,7 @@ const void *FileFace::get_table_fn(const void* appFaceHandle, unsigned int name, if (!TtfUtil::GetTableInfo(name, file_face._header_tbl, file_face._table_dir, tbl_offset, tbl_len)) return 0; - if (tbl_offset + tbl_len > file_face._file_len + if (tbl_offset > file_face._file_len || tbl_len > file_face._file_len - tbl_offset || fseek(file_face._file, tbl_offset, SEEK_SET) != 0) return 0; diff --git a/gfx/graphite2/src/Font.cpp b/gfx/graphite2/src/Font.cpp index 8860d3b58..5cec362bf 100644 --- a/gfx/graphite2/src/Font.cpp +++ b/gfx/graphite2/src/Font.cpp @@ -37,7 +37,7 @@ Font::Font(float ppm, const Face & f, const void * appFontHandle, const gr_font_ m_hinted(appFontHandle && ops && (ops->glyph_advance_x || ops->glyph_advance_y)) { memset(&m_ops, 0, sizeof m_ops); - if (m_hinted) + if (m_hinted && ops) memcpy(&m_ops, ops, min(sizeof m_ops, ops->size)); else m_ops.glyph_advance_x = &Face::default_glyph_advance; @@ -47,14 +47,14 @@ Font::Font(float ppm, const Face & f, const void * appFontHandle, const gr_font_ if (m_advances) { for (float *advp = m_advances; nGlyphs; --nGlyphs, ++advp) - *advp = INVALID_ADVANCE; + *advp = INVALID_ADVANCE; } } /*virtual*/ Font::~Font() { - free(m_advances); + free(m_advances); } diff --git a/gfx/graphite2/src/GlyphCache.cpp b/gfx/graphite2/src/GlyphCache.cpp index 457397b75..1cd5054f7 100644 --- a/gfx/graphite2/src/GlyphCache.cpp +++ b/gfx/graphite2/src/GlyphCache.cpp @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2012, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -31,52 +31,55 @@ of the License or (at your option) any later version. #include "inc/GlyphCache.h" #include "inc/GlyphFace.h" #include "inc/Endian.h" +#include "inc/bits.h" using namespace graphite2; namespace { - class glat_iterator : public std::iterator<std::input_iterator_tag, std::pair<sparse::key_type, sparse::mapped_type> > + // Iterator over version 1 or 2 glat entries which consist of a series of + // +-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+ + // v1 |k|n|v1 |v2 |...|vN | or v2 | k | n |v1 |v2 |...|vN | + // +-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+ + // variable length structures. + + template<typename W> + class _glat_iterator : public std::iterator<std::input_iterator_tag, std::pair<sparse::key_type, sparse::mapped_type> > { + unsigned short key() const { return be::peek<W>(_e) + _n; } + unsigned int run() const { return be::peek<W>(_e+sizeof(W)); } + void advance_entry() { _n = 0; _e = _v; be::skip<W>(_v,2); } public: - glat_iterator(const void * glat=0) : _p(reinterpret_cast<const byte *>(glat)), _n(0) {} + _glat_iterator(const void * glat=0) : _e(reinterpret_cast<const byte *>(glat)), _v(_e+2*sizeof(W)), _n(0) {} - glat_iterator & operator ++ () { ++_v.first; --_n; _p += sizeof(uint16); if (_n == -1) { _p -= 2; _v.first = *_p++; _n = *_p++; } return *this; } - glat_iterator operator ++ (int) { glat_iterator tmp(*this); operator++(); return tmp; } + _glat_iterator<W> & operator ++ () { + ++_n; be::skip<uint16>(_v); + if (_n == run()) advance_entry(); + return *this; + } + _glat_iterator<W> operator ++ (int) { _glat_iterator<W> tmp(*this); operator++(); return tmp; } - bool operator == (const glat_iterator & rhs) { return _p >= rhs._p || _p + _n*sizeof(uint16) > rhs._p; } - bool operator != (const glat_iterator & rhs) { return !operator==(rhs); } + // This is strictly a >= operator. A true == operator could be + // implemented that test for overlap but it would be more expensive a + // test. + bool operator == (const _glat_iterator<W> & rhs) { return _v >= rhs._e - 1; } + bool operator != (const _glat_iterator<W> & rhs) { return !operator==(rhs); } value_type operator * () const { - if (_n==0) { _v.first = *_p++; _n = *_p++; } - _v.second = be::peek<uint16>(_p); - return _v; + return value_type(key(), be::peek<uint16>(_v)); } - const value_type * operator ->() const { operator * (); return &_v; } protected: - mutable const byte * _p; - mutable value_type _v; - mutable int _n; + const byte * _e, * _v; + size_t _n; }; - class glat2_iterator : public glat_iterator - { - public: - glat2_iterator(const void * glat) : glat_iterator(glat) {} - - glat2_iterator & operator ++ () { ++_v.first; --_n; _p += sizeof(uint16); if (_n == -1) { _p -= sizeof(uint16)*2; _v.first = be::read<uint16>(_p); _n = be::read<uint16>(_p); } return *this; } - glat2_iterator operator ++ (int) { glat2_iterator tmp(*this); operator++(); return tmp; } - - value_type operator * () const { - if (_n==0) { _v.first = be::read<uint16>(_p); _n = be::read<uint16>(_p); } - _v.second = be::peek<uint16>(_p); - return _v; - } - const value_type * operator ->() const { operator * (); return &_v; } - }; + typedef _glat_iterator<uint8> glat_iterator; + typedef _glat_iterator<uint16> glat2_iterator; } +const SlantBox SlantBox::empty = {0,0,0,0}; + class GlyphCache::Loader { @@ -87,8 +90,10 @@ public: unsigned short int units_per_em() const throw(); unsigned short int num_glyphs() const throw(); unsigned short int num_attrs() const throw(); + bool has_boxes() const throw(); - const GlyphFace * read_glyph(unsigned short gid, GlyphFace &) const throw(); + const GlyphFace * read_glyph(unsigned short gid, GlyphFace &, int *numsubs) const throw(); + GlyphBox * read_box(uint16 gid, GlyphBox *curr, const GlyphFace & face) const throw(); CLASS_NEW_DELETE; private: @@ -101,6 +106,7 @@ private: m_pGloc; bool _long_fmt; + bool _has_boxes; unsigned short _num_glyphs_graphics, //i.e. boundary box and advance _num_glyphs_attributes, _num_attrs; // number of glyph attributes per glyph @@ -111,31 +117,49 @@ private: GlyphCache::GlyphCache(const Face & face, const uint32 face_options) : _glyph_loader(new Loader(face, bool(face_options & gr_face_dumbRendering))), _glyphs(_glyph_loader && *_glyph_loader ? grzeroalloc<const GlyphFace *>(_glyph_loader->num_glyphs()) : 0), + _boxes(_glyph_loader && _glyph_loader->has_boxes() ? grzeroalloc<GlyphBox *>(_glyph_loader->num_glyphs()) : 0), _num_glyphs(_glyphs ? _glyph_loader->num_glyphs() : 0), _num_attrs(_glyphs ? _glyph_loader->num_attrs() : 0), _upem(_glyphs ? _glyph_loader->units_per_em() : 0) { if ((face_options & gr_face_preloadGlyphs) && _glyph_loader && _glyphs) { + int numsubs = 0; GlyphFace * const glyphs = new GlyphFace [_num_glyphs]; if (!glyphs) return; // The 0 glyph is definately required. - _glyphs[0] = _glyph_loader->read_glyph(0, glyphs[0]); + _glyphs[0] = _glyph_loader->read_glyph(0, glyphs[0], &numsubs); // glyphs[0] has the same address as the glyphs array just allocated, // thus assigning the &glyphs[0] to _glyphs[0] means _glyphs[0] points // to the entire array. const GlyphFace * loaded = _glyphs[0]; for (uint16 gid = 1; loaded && gid != _num_glyphs; ++gid) - _glyphs[gid] = loaded = _glyph_loader->read_glyph(gid, glyphs[gid]); + _glyphs[gid] = loaded = _glyph_loader->read_glyph(gid, glyphs[gid], &numsubs); if (!loaded) { _glyphs[0] = 0; delete [] glyphs; } + else if (numsubs > 0) + { + GlyphBox * boxes = (GlyphBox *)gralloc<char>(_num_glyphs * sizeof(GlyphBox) + numsubs * 8 * sizeof(float)); + GlyphBox * currbox = boxes; + + for (uint16 gid = 0; currbox && gid != _num_glyphs; ++gid) + { + _boxes[gid] = currbox; + currbox = _glyph_loader->read_box(gid, currbox, *_glyphs[gid]); + } + if (!currbox) + { + free(boxes); + _boxes[0] = 0; + } + } delete _glyph_loader; _glyph_loader = 0; } @@ -144,6 +168,11 @@ GlyphCache::GlyphCache(const Face & face, const uint32 face_options) { free(_glyphs); _glyphs = 0; + if (_boxes) + { + free(_boxes); + _boxes = 0; + } _num_glyphs = _num_attrs = _upem = 0; } } @@ -163,6 +192,18 @@ GlyphCache::~GlyphCache() delete [] _glyphs[0]; free(_glyphs); } + if (_boxes) + { + if (_glyph_loader) + { + GlyphBox * * g = _boxes; + for (uint16 n = _num_glyphs; n; --n, ++g) + free(*g); + } + else + free(_boxes[0]); + free(_boxes); + } delete _glyph_loader; } @@ -171,13 +212,23 @@ const GlyphFace *GlyphCache::glyph(unsigned short glyphid) const //result m const GlyphFace * & p = _glyphs[glyphid]; if (p == 0 && _glyph_loader) { + int numsubs = 0; GlyphFace * g = new GlyphFace(); - if (g) p = _glyph_loader->read_glyph(glyphid, *g); + if (g) p = _glyph_loader->read_glyph(glyphid, *g, &numsubs); if (!p) { delete g; return *_glyphs; } + if (_boxes) + { + _boxes[glyphid] = (GlyphBox *)gralloc<char>(sizeof(GlyphBox) + 8 * numsubs * sizeof(float)); + if (!_glyph_loader->read_box(glyphid, _boxes[glyphid], *_glyphs[glyphid])) + { + free(_boxes[glyphid]); + _boxes[glyphid] = 0; + } + } } return p; } @@ -191,6 +242,7 @@ GlyphCache::Loader::Loader(const Face & face, const bool dumb_font) _glyf(face, Tag::glyf), _loca(face, Tag::loca), _long_fmt(false), + _has_boxes(false), _num_glyphs_graphics(0), _num_glyphs_attributes(0), _num_attrs(0) @@ -203,7 +255,7 @@ GlyphCache::Loader::Loader(const Face & face, const bool dumb_font) _num_glyphs_graphics = TtfUtil::GlyphCount(maxp); // This will fail if the number of glyphs is wildly out of range. - if (TtfUtil::LocaLookup(_num_glyphs_graphics-1, _loca, _loca.size(), _head) == size_t(-1)) + if (_glyf && TtfUtil::LocaLookup(_num_glyphs_graphics-1, _loca, _loca.size(), _head) == size_t(-2)) { _head = Face::Table(); return; @@ -211,7 +263,7 @@ GlyphCache::Loader::Loader(const Face & face, const bool dumb_font) if (!dumb_font) { - if ((m_pGlat = Face::Table(face, Tag::Glat)) == NULL + if ((m_pGlat = Face::Table(face, Tag::Glat, 0x00030000)) == NULL || (m_pGloc = Face::Table(face, Tag::Gloc)) == NULL || m_pGloc.size() < 6) { @@ -219,32 +271,48 @@ GlyphCache::Loader::Loader(const Face & face, const bool dumb_font) return; } const byte * p = m_pGloc; - const int version = be::read<uint32>(p); + int version = be::read<uint32>(p); const uint16 flags = be::read<uint16>(p); _num_attrs = be::read<uint16>(p); // We can accurately calculate the number of attributed glyphs by // subtracting the length of the attribids array (numAttribs long if present) // and dividing by either 2 or 4 depending on shor or lonf format _long_fmt = flags & 1; - _num_glyphs_attributes = (m_pGloc.size() + int tmpnumgattrs = (m_pGloc.size() - (p - m_pGloc) - sizeof(uint16)*(flags & 0x2 ? _num_attrs : 0)) / (_long_fmt ? sizeof(uint32) : sizeof(uint16)) - 1; - if (version != 0x00010000 + if (version >= 0x00020000 || tmpnumgattrs < 0 || tmpnumgattrs > 65535 || _num_attrs == 0 || _num_attrs > 0x3000 // is this hard limit appropriate? - || _num_glyphs_graphics > _num_glyphs_attributes) + || _num_glyphs_graphics > tmpnumgattrs) + { + _head = Face::Table(); + return; + } + + _num_glyphs_attributes = static_cast<unsigned short>(tmpnumgattrs); + p = m_pGlat; + version = be::read<uint32>(p); + if (version >= 0x00040000) // reject Glat tables that are too new { _head = Face::Table(); return; } + else if (version >= 0x00030000) + { + unsigned int glatflags = be::read<uint32>(p); + _has_boxes = glatflags & 1; + // delete this once the compiler is fixed + _has_boxes = true; + } } } inline GlyphCache::Loader::operator bool () const throw() { - return _head && _hhea && _hmtx && _glyf && _loca; + return _head && _hhea && _hmtx && !(bool(_glyf) != bool(_loca)); } inline @@ -265,23 +333,37 @@ unsigned short int GlyphCache::Loader::num_attrs() const throw() return _num_attrs; } -const GlyphFace * GlyphCache::Loader::read_glyph(unsigned short glyphid, GlyphFace & glyph) const throw() +inline +bool GlyphCache::Loader::has_boxes () const throw() +{ + return _has_boxes; +} + +const GlyphFace * GlyphCache::Loader::read_glyph(unsigned short glyphid, GlyphFace & glyph, int *numsubs) const throw() { Rect bbox; Position advance; if (glyphid < _num_glyphs_graphics) { - int nLsb, xMin, yMin, xMax, yMax; + int nLsb; unsigned int nAdvWid; - size_t locidx = TtfUtil::LocaLookup(glyphid, _loca, _loca.size(), _head); - void *pGlyph = TtfUtil::GlyfLookup(_glyf, locidx, _glyf.size()); + if (_glyf) + { + int xMin, yMin, xMax, yMax; + size_t locidx = TtfUtil::LocaLookup(glyphid, _loca, _loca.size(), _head); + void *pGlyph = TtfUtil::GlyfLookup(_glyf, locidx, _glyf.size()); + + if (pGlyph && TtfUtil::GlyfBox(pGlyph, xMin, yMin, xMax, yMax)) + { + if ((xMin > xMax) || (yMin > yMax)) + return 0; + bbox = Rect(Position(static_cast<float>(xMin), static_cast<float>(yMin)), + Position(static_cast<float>(xMax), static_cast<float>(yMax))); + } + } if (TtfUtil::HorMetrics(glyphid, _hmtx, _hmtx.size(), _hhea, nLsb, nAdvWid)) advance = Position(static_cast<float>(nAdvWid), 0); - - if (pGlyph && TtfUtil::GlyfBox(pGlyph, xMin, yMin, xMax, yMax)) - bbox = Rect(Position(static_cast<float>(xMin), static_cast<float>(yMin)), - Position(static_cast<float>(xMax), static_cast<float>(yMax))); } if (glyphid < _num_glyphs_attributes) @@ -308,30 +390,90 @@ const GlyphFace * GlyphCache::Loader::read_glyph(unsigned short glyphid, GlyphFa return 0; const uint32 glat_version = be::peek<uint32>(m_pGlat); + if (glat_version == 0x00030000) + { + const byte * p = m_pGlat + glocs; + uint16 bmap = be::read<uint16>(p); + int num = bit_set_count((uint32)bmap); + if (numsubs) *numsubs += num; + glocs += 6 + 8 * num; + if (glocs > gloce) + return 0; + } if (glat_version < 0x00020000) { if (gloce - glocs < 2*sizeof(byte)+sizeof(uint16) || gloce - glocs > _num_attrs*(2*sizeof(byte)+sizeof(uint16))) - { - return 0; - } - + return 0; new (&glyph) GlyphFace(bbox, advance, glat_iterator(m_pGlat + glocs), glat_iterator(m_pGlat + gloce)); } else { - if (gloce - glocs < 3*sizeof(uint16) - || gloce - glocs > _num_attrs*3*sizeof(uint16)) - { - return 0; - } - + if (gloce - glocs < 3*sizeof(uint16) // can a glyph have no attributes? why not? + || gloce - glocs > _num_attrs*3*sizeof(uint16) + || glocs > m_pGlat.size() - 2*sizeof(uint16)) + return 0; new (&glyph) GlyphFace(bbox, advance, glat2_iterator(m_pGlat + glocs), glat2_iterator(m_pGlat + gloce)); } - - if (glyph.attrs().capacity() > _num_attrs) + if (!glyph.attrs() || glyph.attrs().capacity() > _num_attrs) return 0; } - return &glyph; } + +inline float scale_to(uint8 t, float zmin, float zmax) +{ + return (zmin + t * (zmax - zmin) / 255); +} + +Rect readbox(Rect &b, uint8 zxmin, uint8 zymin, uint8 zxmax, uint8 zymax) +{ + return Rect(Position(scale_to(zxmin, b.bl.x, b.tr.x), scale_to(zymin, b.bl.y, b.tr.y)), + Position(scale_to(zxmax, b.bl.x, b.tr.x), scale_to(zymax, b.bl.y, b.tr.y))); +} + +GlyphBox * GlyphCache::Loader::read_box(uint16 gid, GlyphBox *curr, const GlyphFace & glyph) const throw() +{ + if (gid >= _num_glyphs_attributes) return 0; + + const byte * gloc = m_pGloc; + size_t glocs = 0, gloce = 0; + + be::skip<uint32>(gloc); + be::skip<uint16>(gloc,2); + if (_long_fmt) + { + be::skip<uint32>(gloc, gid); + glocs = be::read<uint32>(gloc); + gloce = be::peek<uint32>(gloc); + } + else + { + be::skip<uint16>(gloc, gid); + glocs = be::read<uint16>(gloc); + gloce = be::peek<uint16>(gloc); + } + + if (glocs >= m_pGlat.size() || gloce > m_pGlat.size()) + return 0; + + const byte * p = m_pGlat + glocs; + uint16 bmap = be::read<uint16>(p); + int num = bit_set_count((uint32)bmap); + + Rect bbox = glyph.theBBox(); + Rect diamax(Position(bbox.bl.x + bbox.bl.y, bbox.bl.x - bbox.tr.y), + Position(bbox.tr.x + bbox.tr.y, bbox.tr.x - bbox.bl.y)); + Rect diabound = readbox(diamax, p[0], p[2], p[1], p[3]); + ::new (curr) GlyphBox(num, bmap, &diabound); + be::skip<uint8>(p, 4); + + for (int i = 0; i < num * 2; ++i) + { + Rect box = readbox((i & 1) ? diamax : bbox, p[0], p[2], p[1], p[3]); + curr->addSubBox(i >> 1, i & 1, &box); + be::skip<uint8>(p, 4); + } + return (GlyphBox *)((char *)(curr) + sizeof(GlyphBox) + 2 * num * sizeof(Rect)); +} + diff --git a/gfx/graphite2/src/GlyphFaceCache.cpp b/gfx/graphite2/src/GlyphFaceCache.cpp deleted file mode 100644 index 9abb1aa9d..000000000 --- a/gfx/graphite2/src/GlyphFaceCache.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/* GRAPHITE2 LICENSING - - Copyright 2010, SIL International - All rights reserved. - - This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2.1 of License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should also have received a copy of the GNU Lesser General Public - License along with this library in the file named "LICENSE". - If not, write to the Free Software Foundation, 51 Franklin Street, - Suite 500, Boston, MA 02110-1335, USA or visit their web page on the - internet at http://www.fsf.org/licenses/lgpl.html. - -Alternatively, the contents of this file may be used under the terms of the -Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public -License, as published by the Free Software Foundation, either version 2 -of the License or (at your option) any later version. -*/ -#include "inc/GlyphFaceCache.h" -#include "graphite2/Font.h" -#include "inc/Face.h" //for the tags -#include "inc/Endian.h" - -using namespace graphite2; - -/*virtual*/ bool GlyphFaceCacheHeader::initialize(const Face & face, const bool dumb_font) //return result indicates success. Do not use if failed. -{ - if ((m_pLoca = face.getTable(Tag::loca, &m_lLoca)) == NULL) return false; - if ((m_pHead = face.getTable(Tag::head)) == NULL) return false; - if ((m_pGlyf = face.getTable(Tag::glyf, &m_lGlyf)) == NULL) return false; - if ((m_pHmtx = face.getTable(Tag::hmtx, &m_lHmtx)) == NULL) return false; - if ((m_pHHea = face.getTable(Tag::hhea)) == NULL) return false; - - const void* pMaxp = face.getTable(Tag::maxp); - if (pMaxp == NULL) return false; - m_nGlyphs = m_nGlyphsWithGraphics = (unsigned short)TtfUtil::GlyphCount(pMaxp); - if (TtfUtil::LocaLookup(m_nGlyphs-1, m_pLoca, m_lLoca, m_pHead) == size_t(-1)) - return false; // This will fail if m_nGlyphs is wildly out of range. - - if (!dumb_font) - { - if ((m_pGlat = face.getTable(Tag::Glat, &m_lGlat)) == NULL) return false; - m_fGlat = be::peek<uint32>(m_pGlat); - size_t lGloc; - if ((m_pGloc = face.getTable(Tag::Gloc, &lGloc)) == NULL) return false; - if (lGloc < 6) return false; - int version = be::read<uint32>(m_pGloc); - if (version != 0x00010000) return false; - - const uint16 locFlags = be::read<uint16>(m_pGloc); - m_numAttrs = be::read<uint16>(m_pGloc); - if (m_numAttrs > 0x1000) return false; // is this hard limit appropriate? - - if (locFlags & 1) - { - m_locFlagsUse32Bit = true; - m_nGlyphsWithAttributes = (unsigned short)((lGloc - 12) / 4); - } - else - { - m_locFlagsUse32Bit = false; - m_nGlyphsWithAttributes = (unsigned short)((lGloc - 10) / 2); - } - - if (m_nGlyphsWithAttributes > m_nGlyphs) - m_nGlyphs = m_nGlyphsWithAttributes; - } - - return true; -} - -GlyphFaceCache* GlyphFaceCache::makeCache(const GlyphFaceCacheHeader& hdr) -{ - return new (hdr) GlyphFaceCache(hdr); -} - -GlyphFaceCache::GlyphFaceCache(const GlyphFaceCacheHeader& hdr) -: GlyphFaceCacheHeader(hdr) -{ - unsigned int nGlyphs = numGlyphs(); - - for (unsigned int i = 0; i < nGlyphs; i++) - { - *glyphPtrDirect(i) = NULL; - } -} - -GlyphFaceCache::~GlyphFaceCache() -{ - unsigned int nGlyphs = numGlyphs(); - int deltaPointers = (*glyphPtrDirect(nGlyphs-1u) - *glyphPtrDirect(0u)); - if ((nGlyphs > 0u) && (deltaPointers == static_cast<int>(nGlyphs - 1))) - { - for (unsigned int i=0 ; i<nGlyphs; ++i) - { - GlyphFace *p = *glyphPtrDirect(i); - assert (p); - p->~GlyphFace(); - } - free (*glyphPtrDirect(0)); - } - else - { - for (unsigned int i=0 ; i<nGlyphs; ++i) - { - GlyphFace *p = *glyphPtrDirect(i); - if (p) - { - p->~GlyphFace(); - free(p); - } - } - } -} - -void GlyphFaceCache::loadAllGlyphs() -{ - unsigned int nGlyphs = numGlyphs(); -// size_t sparse_size = 0; - GlyphFace * glyphs = gralloc<GlyphFace>(nGlyphs); - for (unsigned short glyphid = 0; glyphid < nGlyphs; glyphid++) - { - GlyphFace **p = glyphPtrDirect(glyphid); - *p = &(glyphs[glyphid]); - new(*p) GlyphFace(*this, glyphid); -// sparse_size += (*p)->m_attrs._sizeof(); - } -// const size_t flat_size = nGlyphs*(sizeof(uint16*) + sizeof(uint16)*numAttrs()); -// assert(sparse_size <= flat_size); -} - -/*virtual*/ const GlyphFace *GlyphFaceCache::glyph(unsigned short glyphid) const //result may be changed by subsequent call with a different glyphid -{ - GlyphFace **p = glyphPtrDirect(glyphid); - if (*p) - return *p; - - *p = (GlyphFace*)malloc(sizeof(GlyphFace)); - new(*p) GlyphFace(*this, glyphid); - return *p; -} diff --git a/gfx/graphite2/src/Intervals.cpp b/gfx/graphite2/src/Intervals.cpp new file mode 100644 index 000000000..1e34e5987 --- /dev/null +++ b/gfx/graphite2/src/Intervals.cpp @@ -0,0 +1,294 @@ +/* GRAPHITE2 LICENSING + + Copyright 2010, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ +#include <algorithm> +#include <cmath> +#include <limits> + +#include "inc/Intervals.h" +#include "inc/Segment.h" +#include "inc/Slot.h" +#include "inc/debug.h" +#include "inc/bits.h" + +using namespace graphite2; + +#include <cmath> + +inline +Zones::Exclusion Zones::Exclusion::split_at(float p) { + Exclusion r(*this); + r.xm = x = p; + return r; +} + +inline +void Zones::Exclusion::left_trim(float p) { + x = p; +} + +inline +Zones::Exclusion & Zones::Exclusion::operator += (Exclusion const & rhs) { + c += rhs.c; sm += rhs.sm; smx += rhs.smx; open = false; + return *this; +} + +inline +uint8 Zones::Exclusion::outcode(float val) const { + float p = val; + return ((p >= xm) << 1) | (p < x); +} + +void Zones::exclude_with_margins(float xmin, float xmax, int axis) { + remove(xmin, xmax); + weightedAxis(axis, xmin-_margin_len, xmin, 0, 0, _margin_weight, xmin-_margin_len, 0, 0, false); + weightedAxis(axis, xmax, xmax+_margin_len, 0, 0, _margin_weight, xmax+_margin_len, 0, 0, false); +} + +namespace +{ + +inline +bool separated(float a, float b) { + return a != b; + //return std::fabs(a-b) > std::numeric_limits<float>::epsilon(); // std::epsilon may not work. but 0.5 fails exising 64 bit tests + //return std::fabs(a-b) > 0.5f; +} + +} + +void Zones::insert(Exclusion e) +{ +#if !defined GRAPHITE2_NTRACING + addDebug(&e); +#endif + e.x = max(e.x, _pos); + e.xm = min(e.xm, _posm); + if (e.x >= e.xm) return; + + for (iterator i = _exclusions.begin(), ie = _exclusions.end(); i != ie && e.x < e.xm; ++i) + { + const uint8 oca = e.outcode(i->x), + ocb = e.outcode(i->xm); + if ((oca & ocb) != 0) continue; + + switch (oca ^ ocb) // What kind of overlap? + { + case 0: // e completely covers i + // split e at i.x into e1,e2 + // split e2 at i.mx into e2,e3 + // drop e1 ,i+e2, e=e3 + *i += e; + e.left_trim(i->xm); + break; + case 1: // e overlaps on the rhs of i + // split i at e->x into i1,i2 + // split e at i.mx into e1,e2 + // trim i1, insert i2+e1, e=e2 + if (!separated(i->xm, e.x)) break; + if (separated(i->x,e.x)) { i = _exclusions.insert(i,i->split_at(e.x)); ++i; } + *i += e; + e.left_trim(i->xm); + break; + case 2: // e overlaps on the lhs of i + // split e at i->x into e1,e2 + // split i at e.mx into i1,i2 + // drop e1, insert e2+i1, trim i2 + if (!separated(e.xm, i->x)) return; + if (separated(e.xm, i->xm)) i = _exclusions.insert(i,i->split_at(e.xm)); + *i += e; + return; + case 3: // i completely covers e + // split i at e.x into i1,i2 + // split i2 at e.mx into i2,i3 + // insert i1, insert e+i2 + if (separated(e.xm, i->xm)) i = _exclusions.insert(i,i->split_at(e.xm)); + i = _exclusions.insert(i, i->split_at(e.x)); + *++i += e; + return; + } + + ie = _exclusions.end(); + } +} + + +void Zones::remove(float x, float xm) +{ +#if !defined GRAPHITE2_NTRACING + removeDebug(x, xm); +#endif + x = max(x, _pos); + xm = min(xm, _posm); + if (x >= xm) return; + + for (iterator i = _exclusions.begin(), ie = _exclusions.end(); i != ie; ++i) + { + const uint8 oca = i->outcode(x), + ocb = i->outcode(xm); + if ((oca & ocb) != 0) continue; + + switch (oca ^ ocb) // What kind of overlap? + { + case 0: // i completely covers e + if (separated(i->x, x)) { i = _exclusions.insert(i,i->split_at(x)); ++i; } + GR_FALLTHROUGH; + // no break + case 1: // i overlaps on the rhs of e + i->left_trim(xm); + return; + case 2: // i overlaps on the lhs of e + i->xm = x; + if (separated(i->x, i->xm)) break; + GR_FALLTHROUGH; + // no break + case 3: // e completely covers i + i = _exclusions.erase(i); + --i; + break; + } + + ie = _exclusions.end(); + } +} + + +Zones::const_iterator Zones::find_exclusion_under(float x) const +{ + int l = 0, h = _exclusions.size(); + + while (l < h) + { + int const p = (l+h) >> 1; + switch (_exclusions[p].outcode(x)) + { + case 0 : return _exclusions.begin()+p; + case 1 : h = p; break; + case 2 : + case 3 : l = p+1; break; + } + } + + return _exclusions.begin()+l; +} + + +float Zones::closest(float origin, float & cost) const +{ + float best_c = std::numeric_limits<float>::max(), + best_x = 0; + + const const_iterator start = find_exclusion_under(origin); + + // Forward scan looking for lowest cost + for (const_iterator i = start, ie = _exclusions.end(); i != ie; ++i) + if (i->track_cost(best_c, best_x, origin)) break; + + // Backward scan looking for lowest cost + // We start from the exclusion to the immediate left of start since we've + // already tested start with the right most scan above. + for (const_iterator i = start-1, ie = _exclusions.begin()-1; i != ie; --i) + if (i->track_cost(best_c, best_x, origin)) break; + + cost = (best_c == std::numeric_limits<float>::max() ? -1 : best_c); + return best_x; +} + + +// Cost and test position functions + +bool Zones::Exclusion::track_cost(float & best_cost, float & best_pos, float origin) const { + const float p = test_position(origin), + localc = cost(p - origin); + if (open && localc > best_cost) return true; + + if (localc < best_cost) + { + best_cost = localc; + best_pos = p; + } + return false; +} + +inline +float Zones::Exclusion::cost(float p) const { + return (sm * p - 2 * smx) * p + c; +} + + +float Zones::Exclusion::test_position(float origin) const { + if (sm < 0) + { + // sigh, test both ends and perhaps the middle too! + float res = x; + float cl = cost(x); + if (x < origin && xm > origin) + { + float co = cost(origin); + if (co < cl) + { + cl = co; + res = origin; + } + } + float cr = cost(xm); + return cl > cr ? xm : res; + } + else + { + float zerox = smx / sm + origin; + if (zerox < x) return x; + else if (zerox > xm) return xm; + else return zerox; + } +} + + +#if !defined GRAPHITE2_NTRACING + +void Zones::jsonDbgOut(Segment *seg) const { + + if (_dbg) + { + for (Zones::idebugs s = dbgs_begin(), e = dbgs_end(); s != e; ++s) + { + *_dbg << json::flat << json::array + << objectid(dslot(seg, (Slot *)(s->_env[0]))) + << reinterpret_cast<ptrdiff_t>(s->_env[1]); + if (s->_isdel) + *_dbg << "remove" << Position(s->_excl.x, s->_excl.xm); + else + *_dbg << "exclude" << json::flat << json::array + << s->_excl.x << s->_excl.xm + << s->_excl.sm << s->_excl.smx << s->_excl.c + << json::close; + *_dbg << json::close; + } + } +} + +#endif + diff --git a/gfx/graphite2/src/Justifier.cpp b/gfx/graphite2/src/Justifier.cpp index fc667dca1..e40e02902 100644 --- a/gfx/graphite2/src/Justifier.cpp +++ b/gfx/graphite2/src/Justifier.cpp @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2012, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -31,7 +31,7 @@ of the License or (at your option) any later version. #include "inc/CharInfo.h" #include "inc/Slot.h" #include "inc/Main.h" -#include <math.h> +#include <cmath> using namespace graphite2; @@ -60,7 +60,7 @@ void JustifyTotal::accumulate(Slot *s, Segment *seg, int level) m_tWeight += s->getJustify(seg, level, 3); } -float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUSED justFlags flags, Slot *pFirst, Slot *pLast) +float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUSED justFlags jflags, Slot *pFirst, Slot *pLast) { Slot *s, *end; float currWidth = 0.0; @@ -70,17 +70,24 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS if (width < 0 && !(silf()->flags())) return width; + if ((m_dir & 1) != m_silf->dir() && m_silf->bidiPass() != m_silf->numPasses()) + { + reverseSlots(); + s = pFirst; + pFirst = pLast; + pLast = s; + } if (!pFirst) pFirst = pSlot; while (!pFirst->isBase()) pFirst = pFirst->attachedTo(); if (!pLast) pLast = last(); while (!pLast->isBase()) pLast = pLast->attachedTo(); const float base = pFirst->origin().x / scale; width = width / scale; - if ((flags & gr_justEndInline) == 0) + if ((jflags & gr_justEndInline) == 0) { do { Rect bbox = theGlyphBBoxTemporary(pLast->glyph()); - if (bbox.bl.x != 0. || bbox.bl.y != 0. || bbox.tr.x != 0. || bbox.tr.y == 0.) + if (bbox.bl.x != 0.f || bbox.bl.y != 0.f || bbox.tr.x != 0.f || bbox.tr.y == 0.f) break; pLast = pLast->prev(); } while (pLast != pFirst); @@ -116,7 +123,7 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS ++numLevels; } - JustifyTotal *stats = new JustifyTotal[numLevels]; + Vector<JustifyTotal> stats(numLevels); for (s = pFirst; s != end; s = s->nextSibling()) { float w = s->origin().x / scale + s->advance() - base; @@ -126,7 +133,7 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS s->just(0); } - for (int i = (width < 0.0) ? -1 : numLevels - 1; i >= 0; --i) + for (int i = (width < 0.0f) ? -1 : numLevels - 1; i >= 0; --i) { float diff; float error = 0.; @@ -158,7 +165,7 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS if (-pref > max) pref = -max; else tWeight += w; } - int actual = step ? int(pref / step) * step : int(pref); + int actual = int(pref / step) * step; if (actual) { @@ -170,7 +177,7 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS } } currWidth += diff - error; - } while (i == 0 && int(abs(error)) > 0 && tWeight); + } while (i == 0 && int(std::abs(error)) > 0 && tWeight); } Slot *oldFirst = m_first; @@ -179,6 +186,7 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS { m_first = pSlot = addLineEnd(pSlot); m_last = pLast = addLineEnd(end); + if (!m_first || !m_last) return -1.0; } else { @@ -191,27 +199,27 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS json * const dbgout = m_face->logger(); if (dbgout) *dbgout << json::object - << "justifies" << objectid(this) + << "justifies" << objectid(this) << "passes" << json::array; #endif - if (m_silf->justificationPass() != m_silf->positionPass() && (width >= 0. || (silf()->flags() & 1))) + if (m_silf->justificationPass() != m_silf->positionPass() && (width >= 0.f || (silf()->flags() & 1))) m_silf->runGraphite(this, m_silf->justificationPass(), m_silf->positionPass()); #if !defined GRAPHITE2_NTRACING if (dbgout) { *dbgout << json::item << json::close; // Close up the passes array - positionSlots(NULL, pSlot, pLast); + positionSlots(NULL, pSlot, pLast, m_dir); Slot *lEnd = pLast->nextSibling(); *dbgout << "output" << json::array; for(Slot * t = pSlot; t != lEnd; t = t->next()) - *dbgout << dslot(this, t); - *dbgout << json::close << json::close; + *dbgout << dslot(this, t); + *dbgout << json::close << json::close; } #endif - res = positionSlots(font, pSlot, pLast); + res = positionSlots(font, pSlot, pLast, m_dir); if (silf()->flags() & 1) { @@ -220,12 +228,16 @@ float Segment::justify(Slot *pSlot, const Font *font, float width, GR_MAYBE_UNUS } m_first = oldFirst; m_last = oldLast; + + if ((m_dir & 1) != m_silf->dir() && m_silf->bidiPass() != m_silf->numPasses()) + reverseSlots(); return res.x; } Slot *Segment::addLineEnd(Slot *nSlot) { Slot *eSlot = newSlot(); + if (!eSlot) return NULL; const uint16 gid = silf()->endLineGlyphid(); const GlyphFace * theGlyph = m_face->glyphs().glyphSafe(gid); eSlot->setGlyph(this, gid, theGlyph); diff --git a/gfx/graphite2/src/NameTable.cpp b/gfx/graphite2/src/NameTable.cpp index c9eb7651f..f83adb09c 100644 --- a/gfx/graphite2/src/NameTable.cpp +++ b/gfx/graphite2/src/NameTable.cpp @@ -145,6 +145,12 @@ void* NameTable::getName(uint16& languageId, uint16 nameId, gr_encform enc, uint } utf16Length >>= 1; // in utf16 units utf16::codeunit_t * utf16Name = gralloc<utf16::codeunit_t>(utf16Length); + if (!utf16Name) + { + languageId = 0; + length = 0; + return NULL; + } const uint8* pName = m_nameData + offset; for (size_t i = 0; i < utf16Length; i++) { @@ -154,28 +160,46 @@ void* NameTable::getName(uint16& languageId, uint16 nameId, gr_encform enc, uint { case gr_utf8: { - utf8::codeunit_t* uniBuffer = gralloc<utf8::codeunit_t>(3 * utf16Length + 1); + utf8::codeunit_t* uniBuffer = gralloc<utf8::codeunit_t>(3 * utf16Length + 1); + if (!uniBuffer) + { + free(utf16Name); + languageId = 0; + length = 0; + return NULL; + } utf8::iterator d = uniBuffer; for (utf16::const_iterator s = utf16Name, e = utf16Name + utf16Length; s != e; ++s, ++d) - *d = *s; + *d = *s; length = d - uniBuffer; uniBuffer[length] = 0; + free(utf16Name); return uniBuffer; } case gr_utf16: - length = utf16Length; - return utf16Name; + length = utf16Length; + return utf16Name; case gr_utf32: { - utf32::codeunit_t * uniBuffer = gralloc<utf32::codeunit_t>(utf16Length + 1); - utf32::iterator d = uniBuffer; - for (utf16::const_iterator s = utf16Name, e = utf16Name + utf16Length; s != e; ++s, ++d) - *d = *s; - length = d - uniBuffer; - uniBuffer[length] = 0; - return uniBuffer; + utf32::codeunit_t * uniBuffer = gralloc<utf32::codeunit_t>(utf16Length + 1); + if (!uniBuffer) + { + free(utf16Name); + languageId = 0; + length = 0; + return NULL; + } + utf32::iterator d = uniBuffer; + for (utf16::const_iterator s = utf16Name, e = utf16Name + utf16Length; s != e; ++s, ++d) + *d = *s; + length = d - uniBuffer; + uniBuffer[length] = 0; + free(utf16Name); + return uniBuffer; } } + free(utf16Name); + languageId = 0; length = 0; return NULL; } diff --git a/gfx/graphite2/src/Pass.cpp b/gfx/graphite2/src/Pass.cpp index d90035490..f6d269931 100644 --- a/gfx/graphite2/src/Pass.cpp +++ b/gfx/graphite2/src/Pass.cpp @@ -31,14 +31,24 @@ of the License or (at your option) any later version. #include <cstring> #include <cstdlib> #include <cassert> +#include <cmath> #include "inc/Segment.h" #include "inc/Code.h" #include "inc/Rule.h" +#include "inc/Error.h" +#include "inc/Collider.h" using namespace graphite2; using vm::Machine; typedef Machine::Code Code; +enum KernCollison +{ + None = 0, + CrossSpace = 1, + InWord = 2, + reserved = 3 +}; Pass::Pass() : m_silf(0), @@ -48,16 +58,22 @@ Pass::Pass() m_startStates(0), m_transitions(0), m_states(0), - m_flags(0), + m_codes(0), + m_progs(0), + m_numCollRuns(0), + m_kernColls(0), m_iMaxLoop(0), m_numGlyphs(0), m_numRules(0), m_numStates(0), m_numTransition(0), m_numSuccess(0), + m_successStart(0), m_numColumns(0), m_minPreCtxt(0), - m_maxPreCtxt(0) + m_maxPreCtxt(0), + m_colThreshold(0), + m_isReverseDir(false) { } @@ -69,21 +85,31 @@ Pass::~Pass() free(m_states); free(m_ruleMap); - delete [] m_rules; + if (m_rules) delete [] m_rules; + if (m_codes) delete [] m_codes; + free(m_progs); } -bool Pass::readPass(const byte * const pass_start, size_t pass_length, size_t subtable_base, GR_MAYBE_UNUSED const Face & face) +bool Pass::readPass(const byte * const pass_start, size_t pass_length, size_t subtable_base, + GR_MAYBE_UNUSED Face & face, passtype pt, GR_MAYBE_UNUSED uint32 version, Error &e) { - const byte * p = pass_start, - * const pass_end = p + pass_length; + const byte * p = pass_start, + * const pass_end = p + pass_length; size_t numRanges; - if (pass_length < 40) return false; + if (e.test(pass_length < 40, E_BADPASSLENGTH)) return face.error(e); // Read in basic values - m_flags = be::read<byte>(p); + const byte flags = be::read<byte>(p); + if (e.test((flags & 0x1f) && pt < PASS_TYPE_POSITIONING, E_BADCOLLISIONPASS)) + return face.error(e); + m_numCollRuns = flags & 0x7; + m_kernColls = (flags >> 3) & 0x3; + m_isReverseDir = (flags >> 5) & 0x1; m_iMaxLoop = be::read<byte>(p); + if (m_iMaxLoop < 1) m_iMaxLoop = 1; be::skip<byte>(p,2); // skip maxContext & maxBackup m_numRules = be::read<uint16>(p); + if (e.test(!m_numRules && m_numCollRuns == 0, E_BADEMPTYPASS)) return face.error(e); be::skip<uint16>(p); // fsmOffset - not sure why we would want this const byte * const pcCode = pass_start + be::read<uint32>(p) - subtable_base, * const rcCode = pass_start + be::read<uint32>(p) - subtable_base, @@ -97,14 +123,16 @@ bool Pass::readPass(const byte * const pass_start, size_t pass_length, size_t su be::skip<uint16>(p, 3); // skip searchRange, entrySelector & rangeShift. assert(p - pass_start == 40); // Perform some sanity checks. - if ( m_numTransition > m_numStates - || m_numSuccess > m_numStates - || m_numSuccess + m_numTransition < m_numStates - || numRanges == 0) - return false; + if ( e.test(m_numTransition > m_numStates, E_BADNUMTRANS) + || e.test(m_numSuccess > m_numStates, E_BADNUMSUCCESS) + || e.test(m_numSuccess + m_numTransition < m_numStates, E_BADNUMSTATES) + || e.test(m_numRules && numRanges == 0, E_NORANGES) + || e.test(m_numColumns > 0x7FFF, E_BADNUMCOLUMNS)) + return face.error(e); m_successStart = m_numStates - m_numSuccess; - if (p + numRanges * 6 - 4 > pass_end) return false; + // test for beyond end - 1 to account for reading uint16 + if (e.test(p + numRanges * 6 - 2 > pass_end, E_BADPASSLENGTH)) return face.error(e); m_numGlyphs = be::peek<uint16>(p + numRanges * 6 - 4) + 1; // Calculate the start of various arrays. const byte * const ranges = p; @@ -113,56 +141,69 @@ bool Pass::readPass(const byte * const pass_start, size_t pass_length, size_t su be::skip<uint16>(p, m_numSuccess + 1); // More sanity checks - if (reinterpret_cast<const byte *>(o_rule_map + m_numSuccess*sizeof(uint16)) > pass_end - || p > pass_end) - return false; + if (e.test(reinterpret_cast<const byte *>(o_rule_map + m_numSuccess*sizeof(uint16)) > pass_end + || p > pass_end, E_BADRULEMAPLEN)) + return face.error(e); const size_t numEntries = be::peek<uint16>(o_rule_map + m_numSuccess*sizeof(uint16)); const byte * const rule_map = p; be::skip<uint16>(p, numEntries); - if (p + 2*sizeof(uint8) > pass_end) return false; + if (e.test(p + 2*sizeof(uint8) > pass_end, E_BADPASSLENGTH)) return face.error(e); m_minPreCtxt = be::read<uint8>(p); m_maxPreCtxt = be::read<uint8>(p); - if (m_minPreCtxt > m_maxPreCtxt) return false; + if (e.test(m_minPreCtxt > m_maxPreCtxt, E_BADCTXTLENBOUNDS)) return face.error(e); const byte * const start_states = p; be::skip<int16>(p, m_maxPreCtxt - m_minPreCtxt + 1); const uint16 * const sort_keys = reinterpret_cast<const uint16 *>(p); be::skip<uint16>(p, m_numRules); const byte * const precontext = p; be::skip<byte>(p, m_numRules); - be::skip<byte>(p); // skip reserved byte - if (p + sizeof(uint16) > pass_end) return false; + if (e.test(p + sizeof(uint16) + sizeof(uint8) > pass_end, E_BADCTXTLENS)) return face.error(e); + m_colThreshold = be::read<uint8>(p); + if (m_colThreshold == 0) m_colThreshold = 10; // A default const size_t pass_constraint_len = be::read<uint16>(p); + const uint16 * const o_constraint = reinterpret_cast<const uint16 *>(p); be::skip<uint16>(p, m_numRules + 1); const uint16 * const o_actions = reinterpret_cast<const uint16 *>(p); be::skip<uint16>(p, m_numRules + 1); const byte * const states = p; + if (e.test(p + 2u*m_numTransition*m_numColumns >= pass_end, E_BADPASSLENGTH)) return face.error(e); be::skip<int16>(p, m_numTransition*m_numColumns); - be::skip<byte>(p); // skip reserved byte - if (p != pcCode || p >= pass_end) return false; + be::skip<uint8>(p); + if (e.test(p != pcCode, E_BADPASSCCODEPTR)) return face.error(e); be::skip<byte>(p, pass_constraint_len); - if (p != rcCode || p >= pass_end - || size_t(rcCode - pcCode) != pass_constraint_len) return false; + if (e.test(p != rcCode, E_BADRULECCODEPTR) + || e.test(size_t(rcCode - pcCode) != pass_constraint_len, E_BADCCODELEN)) return face.error(e); be::skip<byte>(p, be::peek<uint16>(o_constraint + m_numRules)); - if (p != aCode || p >= pass_end) return false; + if (e.test(p != aCode, E_BADACTIONCODEPTR)) return face.error(e); be::skip<byte>(p, be::peek<uint16>(o_actions + m_numRules)); // We should be at the end or within the pass - if (p > pass_end) return false; + if (e.test(p > pass_end, E_BADPASSLENGTH)) return face.error(e); // Load the pass constraint if there is one. if (pass_constraint_len) { + face.error_context(face.error_context() + 1); m_cPConstraint = vm::Machine::Code(true, pcCode, pcCode + pass_constraint_len, - precontext[0], be::peek<uint16>(sort_keys), *m_silf, face); - if (!m_cPConstraint) return false; + precontext[0], be::peek<uint16>(sort_keys), *m_silf, face, PASS_TYPE_UNKNOWN); + if (e.test(!m_cPConstraint, E_OUTOFMEM) + || e.test(!m_cPConstraint, m_cPConstraint.status() + E_CODEFAILURE)) + return face.error(e); + face.error_context(face.error_context() - 1); + } + if (m_numRules) + { + if (!readRanges(ranges, numRanges, e)) return face.error(e); + if (!readRules(rule_map, numEntries, precontext, sort_keys, + o_constraint, rcCode, o_actions, aCode, face, pt, e)) return false; } - if (!readRanges(ranges, numRanges)) return false; - if (!readRules(rule_map, numEntries, precontext, sort_keys, - o_constraint, rcCode, o_actions, aCode, face)) return false; - return readStates(start_states, states, o_rule_map, face); +#ifdef GRAPHITE2_TELEMETRY + telemetry::category _states_cat(face.tele.states); +#endif + return m_numRules ? readStates(start_states, states, o_rule_map, face, e) : true; } @@ -170,12 +211,11 @@ bool Pass::readRules(const byte * rule_map, const size_t num_entries, const byte *precontext, const uint16 * sort_key, const uint16 * o_constraint, const byte *rc_data, const uint16 * o_action, const byte * ac_data, - const Face & face) + Face & face, passtype pt, Error &e) { const byte * const ac_data_end = ac_data + be::peek<uint16>(o_action + m_numRules); const byte * const rc_data_end = rc_data + be::peek<uint16>(o_constraint + m_numRules); - if (!(m_rules = new Rule [m_numRules])) return false; precontext += m_numRules; sort_key += m_numRules; o_constraint += m_numRules; @@ -185,9 +225,20 @@ bool Pass::readRules(const byte * rule_map, const size_t num_entries, const byte * ac_begin = 0, * rc_begin = 0, * ac_end = ac_data + be::peek<uint16>(o_action), * rc_end = rc_data + be::peek<uint16>(o_constraint); + + // Allocate pools + m_rules = new Rule [m_numRules]; + m_codes = new Code [m_numRules*2]; + const size_t prog_pool_sz = vm::Machine::Code::estimateCodeDataOut(ac_end - ac_data + rc_end - rc_data); + m_progs = gralloc<byte>(prog_pool_sz); + byte * prog_pool_free = m_progs, + * prog_pool_end = m_progs + prog_pool_sz; + if (e.test(!(m_rules && m_codes && m_progs), E_OUTOFMEM)) return face.error(e); + Rule * r = m_rules + m_numRules - 1; for (size_t n = m_numRules; n; --n, --r, ac_end = ac_begin, rc_end = rc_begin) { + face.error_context((face.error_context() & 0xFFFF00) + EC_ARULE + ((n - 1) << 24)); r->preContext = *--precontext; r->sort = be::peek<uint16>(--sort_key); #ifndef NDEBUG @@ -200,24 +251,44 @@ bool Pass::readRules(const byte * rule_map, const size_t num_entries, rc_begin = be::peek<uint16>(o_constraint) ? rc_data + be::peek<uint16>(o_constraint) : rc_end; if (ac_begin > ac_end || ac_begin > ac_data_end || ac_end > ac_data_end - || rc_begin > rc_end || rc_begin > rc_data_end || rc_end > rc_data_end) + || rc_begin > rc_end || rc_begin > rc_data_end || rc_end > rc_data_end + || vm::Machine::Code::estimateCodeDataOut(ac_end - ac_begin + rc_end - rc_begin) > size_t(prog_pool_end - prog_pool_free)) return false; - r->action = new vm::Machine::Code(false, ac_begin, ac_end, r->preContext, r->sort, *m_silf, face); - r->constraint = new vm::Machine::Code(true, rc_begin, rc_end, r->preContext, r->sort, *m_silf, face); + r->action = new (m_codes+n*2-2) vm::Machine::Code(false, ac_begin, ac_end, r->preContext, r->sort, *m_silf, face, pt, &prog_pool_free); + r->constraint = new (m_codes+n*2-1) vm::Machine::Code(true, rc_begin, rc_end, r->preContext, r->sort, *m_silf, face, pt, &prog_pool_free); + + if (e.test(!r->action || !r->constraint, E_OUTOFMEM) + || e.test(r->action->status() != Code::loaded, r->action->status() + E_CODEFAILURE) + || e.test(r->constraint->status() != Code::loaded, r->constraint->status() + E_CODEFAILURE) + || e.test(!r->constraint->immutable(), E_MUTABLECCODE)) + return face.error(e); + } - if (!r->action || !r->constraint - || r->action->status() != Code::loaded - || r->constraint->status() != Code::loaded - || !r->constraint->immutable()) - return false; + byte * moved_progs = static_cast<byte *>(realloc(m_progs, prog_pool_free - m_progs)); + if (e.test(!moved_progs, E_OUTOFMEM)) + { + if (prog_pool_free - m_progs == 0) m_progs = 0; + return face.error(e); + } + + if (moved_progs != m_progs) + { + for (Code * c = m_codes, * const ce = c + m_numRules*2; c != ce; ++c) + { + c->externalProgramMoved(moved_progs - m_progs); + } + m_progs = moved_progs; } // Load the rule entries map + face.error_context((face.error_context() & 0xFFFF00) + EC_APASS); + //TODO: Coverty: 1315804: FORWARD_NULL RuleEntry * re = m_ruleMap = gralloc<RuleEntry>(num_entries); + if (e.test(!re, E_OUTOFMEM)) return face.error(e); for (size_t n = num_entries; n; --n, ++re) { const ptrdiff_t rn = be::read<uint16>(rule_map); - if (rn >= m_numRules) return false; + if (e.test(rn >= m_numRules, E_BADRULENUM)) return face.error(e); re->rule = m_rules + rn; } @@ -227,19 +298,32 @@ bool Pass::readRules(const byte * rule_map, const size_t num_entries, static int cmpRuleEntry(const void *a, const void *b) { return (*(RuleEntry *)a < *(RuleEntry *)b ? -1 : (*(RuleEntry *)b < *(RuleEntry *)a ? 1 : 0)); } -bool Pass::readStates(const byte * starts, const byte *states, const byte * o_rule_map, GR_MAYBE_UNUSED const Face & face) +bool Pass::readStates(const byte * starts, const byte *states, const byte * o_rule_map, GR_MAYBE_UNUSED Face & face, Error &e) { +#ifdef GRAPHITE2_TELEMETRY + telemetry::category _states_cat(face.tele.starts); +#endif m_startStates = gralloc<uint16>(m_maxPreCtxt - m_minPreCtxt + 1); +#ifdef GRAPHITE2_TELEMETRY + telemetry::set_category(face.tele.states); +#endif m_states = gralloc<State>(m_numStates); +#ifdef GRAPHITE2_TELEMETRY + telemetry::set_category(face.tele.transitions); +#endif m_transitions = gralloc<uint16>(m_numTransition * m_numColumns); - if (!m_startStates || !m_states || !m_transitions) return false; + if (e.test(!m_startStates || !m_states || !m_transitions, E_OUTOFMEM)) return face.error(e); // load start states for (uint16 * s = m_startStates, * const s_end = s + m_maxPreCtxt - m_minPreCtxt + 1; s != s_end; ++s) { *s = be::read<uint16>(starts); - if (*s >= m_numStates) return false; // true; + if (e.test(*s >= m_numStates, E_BADSTATE)) + { + face.error_context((face.error_context() & 0xFFFF00) + EC_ASTARTS + ((s - m_startStates) << 24)); + return face.error(e); // true; + } } // load state transition table. @@ -247,7 +331,11 @@ bool Pass::readStates(const byte * starts, const byte *states, const byte * o_ru * const t_end = t + m_numTransition*m_numColumns; t != t_end; ++t) { *t = be::read<uint16>(states); - if (*t >= m_numStates) return false; + if (e.test(*t >= m_numStates, E_BADSTATE)) + { + face.error_context((face.error_context() & 0xFFFF00) + EC_ATRANS + (((t - m_transitions) / m_numColumns) << 24)); + return face.error(e); + } } State * s = m_states, @@ -258,8 +346,11 @@ bool Pass::readStates(const byte * starts, const byte *states, const byte * o_ru RuleEntry * const begin = s < success_begin ? 0 : m_ruleMap + be::read<uint16>(o_rule_map), * const end = s < success_begin ? 0 : m_ruleMap + be::peek<uint16>(o_rule_map); - if (begin >= rule_map_end || end > rule_map_end || begin > end) - return false; + if (e.test(begin >= rule_map_end || end > rule_map_end || begin > end, E_BADRULEMAPPING)) + { + face.error_context((face.error_context() & 0xFFFF00) + EC_ARULEMAP + (n << 24)); + return face.error(e); + } s->rules = begin; s->rules_end = (end - begin <= FiniteStateMachine::MAX_RULES)? end : begin + FiniteStateMachine::MAX_RULES; @@ -269,9 +360,10 @@ bool Pass::readStates(const byte * starts, const byte *states, const byte * o_ru return true; } -bool Pass::readRanges(const byte * ranges, size_t num_ranges) +bool Pass::readRanges(const byte * ranges, size_t num_ranges, Error &e) { m_cols = gralloc<uint16>(m_numGlyphs); + if (e.test(!m_cols, E_OUTOFMEM)) return false; memset(m_cols, 0xFF, m_numGlyphs * sizeof(uint16)); for (size_t n = num_ranges; n; --n) { @@ -279,52 +371,78 @@ bool Pass::readRanges(const byte * ranges, size_t num_ranges) * ci_end = m_cols + be::read<uint16>(ranges) + 1, col = be::read<uint16>(ranges); - if (ci >= ci_end || ci_end > m_cols+m_numGlyphs || col >= m_numColumns) + if (e.test(ci >= ci_end || ci_end > m_cols+m_numGlyphs || col >= m_numColumns, E_BADRANGE)) return false; // A glyph must only belong to one column at a time while (ci != ci_end && *ci == 0xffff) *ci++ = col; - if (ci != ci_end) + if (e.test(ci != ci_end, E_BADRANGE)) return false; } return true; } -void Pass::runGraphite(Machine & m, FiniteStateMachine & fsm) const +bool Pass::runGraphite(vm::Machine & m, FiniteStateMachine & fsm, bool reverse) const { - Slot *s = m.slotMap().segment.first(); - if (!s || !testPassConstraint(m)) return; - Slot *currHigh = s->next(); + Slot *s = m.slotMap().segment.first(); + if (!s || !testPassConstraint(m)) return true; + if (reverse) + { + m.slotMap().segment.reverseSlots(); + s = m.slotMap().segment.first(); + } + if (m_numRules) + { + Slot *currHigh = s->next(); #if !defined GRAPHITE2_NTRACING - if (fsm.dbgout) *fsm.dbgout << "rules" << json::array; - json::closer rules_array_closer(fsm.dbgout); + if (fsm.dbgout) *fsm.dbgout << "rules" << json::array; + json::closer rules_array_closer(fsm.dbgout); #endif - m.slotMap().highwater(currHigh); - int lc = m_iMaxLoop; - do + m.slotMap().highwater(currHigh); + int lc = m_iMaxLoop; + do + { + findNDoRule(s, m, fsm); + if (s && (s == m.slotMap().highwater() || m.slotMap().highpassed() || --lc == 0)) { + if (!lc) + s = m.slotMap().highwater(); + lc = m_iMaxLoop; + if (s) + m.slotMap().highwater(s->next()); + } + } while (s); + } + //TODO: Use enums for flags + const bool collisions = m_numCollRuns || m_kernColls; + + if (!collisions || !m.slotMap().segment.hasCollisionInfo()) + return true; + + if (m_numCollRuns) { - findNDoRule(s, m, fsm); - if (s && (m.slotMap().highpassed() || s == m.slotMap().highwater() || --lc == 0)) { - if (!lc) - { -// if (dbgout) *dbgout << json::item << json::flat << rule_event(-1, s, 1); - s = m.slotMap().highwater(); - } - lc = m_iMaxLoop; - if (s) - m.slotMap().highwater(s->next()); + if (!(m.slotMap().segment.flags() & Segment::SEG_INITCOLLISIONS)) + { + m.slotMap().segment.positionSlots(0, 0, 0, m.slotMap().dir(), true); +// m.slotMap().segment.flags(m.slotMap().segment.flags() | Segment::SEG_INITCOLLISIONS); } - } while (s); + if (!collisionShift(&m.slotMap().segment, m.slotMap().dir(), fsm.dbgout)) + return false; + } + if ((m_kernColls) && !collisionKern(&m.slotMap().segment, m.slotMap().dir(), fsm.dbgout)) + return false; + if (collisions && !collisionFinish(&m.slotMap().segment, fsm.dbgout)) + return false; + return true; } bool Pass::runFSM(FiniteStateMachine& fsm, Slot * slot) const { - fsm.reset(slot, m_maxPreCtxt); + fsm.reset(slot, m_maxPreCtxt); if (fsm.slots.context() < m_minPreCtxt) return false; @@ -356,17 +474,17 @@ bool Pass::runFSM(FiniteStateMachine& fsm, Slot * slot) const inline Slot * input_slot(const SlotMap & slots, const int n) { - Slot * s = slots[slots.context() + n]; - if (!s->isCopied()) return s; + Slot * s = slots[slots.context() + n]; + if (!s->isCopied()) return s; - return s->prev() ? s->prev()->next() : (s->next() ? s->next()->prev() : slots.segment.last()); + return s->prev() ? s->prev()->next() : (s->next() ? s->next()->prev() : slots.segment.last()); } inline Slot * output_slot(const SlotMap & slots, const int n) { - Slot * s = slots[slots.context() + n - 1]; - return s ? s->next() : slots.segment.first(); + Slot * s = slots[slots.context() + n - 1]; + return s ? s->next() : slots.segment.first(); } #endif //!defined GRAPHITE2_NTRACING @@ -385,91 +503,93 @@ void Pass::findNDoRule(Slot * & slot, Machine &m, FiniteStateMachine & fsm) cons #if !defined GRAPHITE2_NTRACING if (fsm.dbgout) { - if (fsm.rules.size() != 0) - { - *fsm.dbgout << json::item << json::object; - dumpRuleEventConsidered(fsm, *r); - if (r != re) - { - const int adv = doAction(r->rule->action, slot, m); - dumpRuleEventOutput(fsm, *r->rule, slot); - if (r->rule->action->deletes()) fsm.slots.collectGarbage(); - adjustSlot(adv, slot, fsm.slots); - *fsm.dbgout << "cursor" << objectid(dslot(&fsm.slots.segment, slot)) - << json::close; // Close RuelEvent object - - return; - } - else - { - *fsm.dbgout << json::close // close "considered" array - << "output" << json::null - << "cursor" << objectid(dslot(&fsm.slots.segment, slot->next())) - << json::close; - } - } + if (fsm.rules.size() != 0) + { + *fsm.dbgout << json::item << json::object; + dumpRuleEventConsidered(fsm, *r); + if (r != re) + { + const int adv = doAction(r->rule->action, slot, m); + dumpRuleEventOutput(fsm, m, *r->rule, slot); + if (r->rule->action->deletes()) fsm.slots.collectGarbage(slot); + adjustSlot(adv, slot, fsm.slots); + *fsm.dbgout << "cursor" << objectid(dslot(&fsm.slots.segment, slot)) + << json::close; // Close RuelEvent object + + return; + } + else + { + *fsm.dbgout << json::close // close "considered" array + << "output" << json::null + << "cursor" << objectid(dslot(&fsm.slots.segment, slot->next())) + << json::close; + } + } } else #endif { - if (r != re) - { - const int adv = doAction(r->rule->action, slot, m); - if (r->rule->action->deletes()) fsm.slots.collectGarbage(); - adjustSlot(adv, slot, fsm.slots); - return; - } + if (r != re) + { + const int adv = doAction(r->rule->action, slot, m); + if (r->rule->action->deletes()) fsm.slots.collectGarbage(slot); + adjustSlot(adv, slot, fsm.slots); + return; + } } } slot = slot->next(); + return; } #if !defined GRAPHITE2_NTRACING void Pass::dumpRuleEventConsidered(const FiniteStateMachine & fsm, const RuleEntry & re) const { - *fsm.dbgout << "considered" << json::array; - for (const RuleEntry *r = fsm.rules.begin(); r != &re; ++r) - { - if (r->rule->preContext > fsm.slots.context()) continue; - *fsm.dbgout << json::flat << json::object - << "id" << r->rule - m_rules - << "failed" << true - << "input" << json::flat << json::object - << "start" << objectid(dslot(&fsm.slots.segment, input_slot(fsm.slots, -r->rule->preContext))) - << "length" << r->rule->sort - << json::close // close "input" - << json::close; // close Rule object - } + *fsm.dbgout << "considered" << json::array; + for (const RuleEntry *r = fsm.rules.begin(); r != &re; ++r) + { + if (r->rule->preContext > fsm.slots.context()) + continue; + *fsm.dbgout << json::flat << json::object + << "id" << r->rule - m_rules + << "failed" << true + << "input" << json::flat << json::object + << "start" << objectid(dslot(&fsm.slots.segment, input_slot(fsm.slots, -r->rule->preContext))) + << "length" << r->rule->sort + << json::close // close "input" + << json::close; // close Rule object + } } -void Pass::dumpRuleEventOutput(const FiniteStateMachine & fsm, const Rule & r, Slot * const last_slot) const +void Pass::dumpRuleEventOutput(const FiniteStateMachine & fsm, Machine & m, const Rule & r, Slot * const last_slot) const { - *fsm.dbgout << json::item << json::flat << json::object - << "id" << &r - m_rules - << "failed" << false - << "input" << json::flat << json::object - << "start" << objectid(dslot(&fsm.slots.segment, input_slot(fsm.slots, 0))) - << "length" << r.sort - r.preContext - << json::close // close "input" - << json::close // close Rule object - << json::close // close considered array - << "output" << json::object - << "range" << json::flat << json::object - << "start" << objectid(dslot(&fsm.slots.segment, input_slot(fsm.slots, 0))) - << "end" << objectid(dslot(&fsm.slots.segment, last_slot)) - << json::close // close "input" - << "slots" << json::array; - const Position rsb_prepos = last_slot ? last_slot->origin() : fsm.slots.segment.advance(); - fsm.slots.segment.positionSlots(0); - - for(Slot * slot = output_slot(fsm.slots, 0); slot != last_slot; slot = slot->next()) - *fsm.dbgout << dslot(&fsm.slots.segment, slot); - *fsm.dbgout << json::close // close "slots" - << "postshift" << (last_slot ? last_slot->origin() : fsm.slots.segment.advance()) - rsb_prepos - << json::close; // close "output" object + *fsm.dbgout << json::item << json::flat << json::object + << "id" << &r - m_rules + << "failed" << false + << "input" << json::flat << json::object + << "start" << objectid(dslot(&fsm.slots.segment, input_slot(fsm.slots, 0))) + << "length" << r.sort - r.preContext + << json::close // close "input" + << json::close // close Rule object + << json::close // close considered array + << "output" << json::object + << "range" << json::flat << json::object + << "start" << objectid(dslot(&fsm.slots.segment, input_slot(fsm.slots, 0))) + << "end" << objectid(dslot(&fsm.slots.segment, last_slot)) + << json::close // close "input" + << "slots" << json::array; + const Position rsb_prepos = last_slot ? last_slot->origin() : fsm.slots.segment.advance(); + fsm.slots.segment.positionSlots(0, 0, 0, m.slotMap().dir()); + + for(Slot * slot = output_slot(fsm.slots, 0); slot != last_slot; slot = slot->next()) + *fsm.dbgout << dslot(&fsm.slots.segment, slot); + *fsm.dbgout << json::close // close "slots" + << "postshift" << (last_slot ? last_slot->origin() : fsm.slots.segment.advance()) - rsb_prepos + << json::close; // close "output" object } @@ -491,7 +611,7 @@ bool Pass::testPassConstraint(Machine & m) const #if !defined GRAPHITE2_NTRACING json * const dbgout = m.slotMap().segment.getFace()->logger(); if (dbgout) - *dbgout << "constraint" << (ret && m.status() == Machine::finished); + *dbgout << "constraint" << (ret && m.status() == Machine::finished); #endif return ret && m.status() == Machine::finished; @@ -500,16 +620,16 @@ bool Pass::testPassConstraint(Machine & m) const bool Pass::testConstraint(const Rule & r, Machine & m) const { - const uint16 curr_context = m.slotMap().context(); + const uint16 curr_context = m.slotMap().context(); if (unsigned(r.sort - r.preContext) > m.slotMap().size() - curr_context - || curr_context - r.preContext < 0) return false; + || curr_context - r.preContext < 0) return false; if (!*r.constraint) return true; assert(r.constraint->constraint()); vm::slotref * map = m.slotMap().begin() + curr_context - r.preContext; for (int n = r.sort; n && map; --n, ++map) { - if (!*map) continue; + if (!*map) continue; const int32 ret = r.constraint->run(m, map); if (!ret || m.status() != Machine::finished) return false; @@ -519,12 +639,16 @@ bool Pass::testConstraint(const Rule & r, Machine & m) const } -void SlotMap::collectGarbage() +void SlotMap::collectGarbage(Slot * &aSlot) { for(Slot **s = begin(), *const *const se = end() - 1; s != se; ++s) { Slot *& slot = *s; if(slot->isDeleted() || slot->isCopied()) + { + if (slot == aSlot) + aSlot = slot->prev() ? slot->prev() : slot->next(); segment.freeSlot(slot); + } } } @@ -542,9 +666,9 @@ int Pass::doAction(const Code *codeptr, Slot * & slot_out, vm::Machine & m) cons if (m.status() != Machine::finished) { - slot_out = NULL; - smap.highwater(0); - return 0; + slot_out = NULL; + smap.highwater(0); + return 0; } slot_out = *map; @@ -554,29 +678,32 @@ int Pass::doAction(const Code *codeptr, Slot * & slot_out, vm::Machine & m) cons void Pass::adjustSlot(int delta, Slot * & slot_out, SlotMap & smap) const { - if (delta < 0) + if (!slot_out) { - if (!slot_out) + if (smap.highpassed() || slot_out == smap.highwater()) { slot_out = smap.segment.last(); ++delta; - if (smap.highpassed() && !smap.highwater()) - smap.highpassed(false); + if (!smap.highwater()) + smap.highpassed(false); + } + else + { + slot_out = smap.segment.first(); + --delta; } + } + if (delta < 0) + { while (++delta <= 0 && slot_out) { if (smap.highpassed() && smap.highwater() == slot_out) - smap.highpassed(false); + smap.highpassed(false); slot_out = slot_out->prev(); } } else if (delta > 0) { - if (!slot_out) - { - slot_out = smap.segment.first(); - --delta; - } while (--delta >= 0 && slot_out) { slot_out = slot_out->next(); @@ -586,3 +713,372 @@ void Pass::adjustSlot(int delta, Slot * & slot_out, SlotMap & smap) const } } +bool Pass::collisionShift(Segment *seg, int dir, json * const dbgout) const +{ + ShiftCollider shiftcoll(dbgout); + // bool isfirst = true; + bool hasCollisions = false; + Slot *start = seg->first(); // turn on collision fixing for the first slot + Slot *end = NULL; + bool moved = false; + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << "collisions" << json::array + << json::flat << json::object << "num-loops" << m_numCollRuns << json::close; +#endif + + while (start) + { +#if !defined GRAPHITE2_NTRACING + if (dbgout) *dbgout << json::object << "phase" << "1" << "moves" << json::array; +#endif + hasCollisions = false; + end = NULL; + // phase 1 : position shiftable glyphs, ignoring kernable glyphs + for (Slot *s = start; s; s = s->next()) + { + const SlotCollision * c = seg->collisionInfo(s); + if (start && (c->flags() & (SlotCollision::COLL_FIX | SlotCollision::COLL_KERN)) == SlotCollision::COLL_FIX + && !resolveCollisions(seg, s, start, shiftcoll, false, dir, moved, hasCollisions, dbgout)) + return false; + if (s != start && (c->flags() & SlotCollision::COLL_END)) + { + end = s->next(); + break; + } + } + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << json::close << json::close; // phase-1 +#endif + + // phase 2 : loop until happy. + for (int i = 0; i < m_numCollRuns - 1; ++i) + { + if (hasCollisions || moved) + { + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << json::object << "phase" << "2a" << "loop" << i << "moves" << json::array; +#endif + // phase 2a : if any shiftable glyphs are in collision, iterate backwards, + // fixing them and ignoring other non-collided glyphs. Note that this handles ONLY + // glyphs that are actually in collision from phases 1 or 2b, and working backwards + // has the intended effect of breaking logjams. + if (hasCollisions) + { + hasCollisions = false; + #if 0 + moved = true; + for (Slot *s = start; s != end; s = s->next()) + { + SlotCollision * c = seg->collisionInfo(s); + c->setShift(Position(0, 0)); + } + #endif + Slot *lend = end ? end->prev() : seg->last(); + Slot *lstart = start->prev(); + for (Slot *s = lend; s != lstart; s = s->prev()) + { + SlotCollision * c = seg->collisionInfo(s); + if (start && (c->flags() & (SlotCollision::COLL_FIX | SlotCollision::COLL_KERN | SlotCollision::COLL_ISCOL)) + == (SlotCollision::COLL_FIX | SlotCollision::COLL_ISCOL)) // ONLY if this glyph is still colliding + { + if (!resolveCollisions(seg, s, lend, shiftcoll, true, dir, moved, hasCollisions, dbgout)) + return false; + c->setFlags(c->flags() | SlotCollision::COLL_TEMPLOCK); + } + } + } + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << json::close << json::close // phase 2a + << json::object << "phase" << "2b" << "loop" << i << "moves" << json::array; +#endif + + // phase 2b : redo basic diacritic positioning pass for ALL glyphs. Each successive loop adjusts + // glyphs from their current adjusted position, which has the effect of gradually minimizing the + // resulting adjustment; ie, the final result will be gradually closer to the original location. + // Also it allows more flexibility in the final adjustment, since it is moving along the + // possible 8 vectors from successively different starting locations. + if (moved) + { + moved = false; + for (Slot *s = start; s != end; s = s->next()) + { + SlotCollision * c = seg->collisionInfo(s); + if (start && (c->flags() & (SlotCollision::COLL_FIX | SlotCollision::COLL_TEMPLOCK + | SlotCollision::COLL_KERN)) == SlotCollision::COLL_FIX + && !resolveCollisions(seg, s, start, shiftcoll, false, dir, moved, hasCollisions, dbgout)) + return false; + else if (c->flags() & SlotCollision::COLL_TEMPLOCK) + c->setFlags(c->flags() & ~SlotCollision::COLL_TEMPLOCK); + } + } + // if (!hasCollisions) // no, don't leave yet because phase 2b will continue to improve things + // break; +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << json::close << json::close; // phase 2 +#endif + } + } + if (!end) + break; + start = NULL; + for (Slot *s = end->prev(); s; s = s->next()) + { + if (seg->collisionInfo(s)->flags() & SlotCollision::COLL_START) + { + start = s; + break; + } + } + } + return true; +} + +bool Pass::collisionKern(Segment *seg, int dir, json * const dbgout) const +{ + KernCollider kerncoll(dbgout); + Slot *start = seg->first(); + float ymin = 1e38f; + float ymax = -1e38f; + const GlyphCache &gc = seg->getFace()->glyphs(); + + // phase 3 : handle kerning of clusters +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << json::object << "phase" << "3" << "moves" << json::array; +#endif + + for (Slot *s = seg->first(); s; s = s->next()) + { + if (!gc.check(s->gid())) + return false; + const SlotCollision * c = seg->collisionInfo(s); + const Rect &bbox = seg->theGlyphBBoxTemporary(s->gid()); + float y = s->origin().y + c->shift().y; + ymax = max(y + bbox.tr.y, ymax); + ymin = min(y + bbox.bl.y, ymin); + if (start && (c->flags() & (SlotCollision::COLL_KERN | SlotCollision::COLL_FIX)) + == (SlotCollision::COLL_KERN | SlotCollision::COLL_FIX)) + resolveKern(seg, s, start, kerncoll, dir, ymin, ymax, dbgout); + if (c->flags() & SlotCollision::COLL_END) + start = NULL; + if (c->flags() & SlotCollision::COLL_START) + start = s; + } + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << json::close << json::close; // phase 3 +#endif + return true; +} + +bool Pass::collisionFinish(Segment *seg, GR_MAYBE_UNUSED json * const dbgout) const +{ + for (Slot *s = seg->first(); s; s = s->next()) + { + SlotCollision *c = seg->collisionInfo(s); + if (c->shift().x != 0 || c->shift().y != 0) + { + const Position newOffset = c->shift(); + const Position nullPosition(0, 0); + c->setOffset(newOffset + c->offset()); + c->setShift(nullPosition); + } + } +// seg->positionSlots(); + +#if !defined GRAPHITE2_NTRACING + if (dbgout) + *dbgout << json::close; +#endif + return true; +} + +// Can slot s be kerned, or is it attached to something that can be kerned? +static bool inKernCluster(Segment *seg, Slot *s) +{ + SlotCollision *c = seg->collisionInfo(s); + if (c->flags() & SlotCollision::COLL_KERN /** && c->flags() & SlotCollision::COLL_FIX **/ ) + return true; + while (s->attachedTo()) + { + s = s->attachedTo(); + c = seg->collisionInfo(s); + if (c->flags() & SlotCollision::COLL_KERN /** && c->flags() & SlotCollision::COLL_FIX **/ ) + return true; + } + return false; +} + +// Fix collisions for the given slot. +// Return true if everything was fixed, false if there are still collisions remaining. +// isRev means be we are processing backwards. +bool Pass::resolveCollisions(Segment *seg, Slot *slotFix, Slot *start, + ShiftCollider &coll, GR_MAYBE_UNUSED bool isRev, int dir, bool &moved, bool &hasCol, + json * const dbgout) const +{ + Slot * nbor; // neighboring slot + SlotCollision *cFix = seg->collisionInfo(slotFix); + if (!coll.initSlot(seg, slotFix, cFix->limit(), cFix->margin(), cFix->marginWt(), + cFix->shift(), cFix->offset(), dir, dbgout)) + return false; + bool collides = false; + // When we're processing forward, ignore kernable glyphs that preceed the target glyph. + // When processing backward, don't ignore these until we pass slotFix. + bool ignoreForKern = !isRev; + bool rtl = dir & 1; + Slot *base = slotFix; + while (base->attachedTo()) + base = base->attachedTo(); + Position zero(0., 0.); + + // Look for collisions with the neighboring glyphs. + for (nbor = start; nbor; nbor = isRev ? nbor->prev() : nbor->next()) + { + SlotCollision *cNbor = seg->collisionInfo(nbor); + bool sameCluster = nbor->isChildOf(base); + if (nbor != slotFix // don't process if this is the slot of interest + && !(cNbor->flags() & SlotCollision::COLL_IGNORE) // don't process if ignoring + && (nbor == base || sameCluster // process if in the same cluster as slotFix + || !inKernCluster(seg, nbor) // or this cluster is not to be kerned + || (rtl ^ ignoreForKern)) // or it comes before(ltr) or after(rtl) + && (!isRev // if processing forwards then good to merge otherwise only: + || !(cNbor->flags() & SlotCollision::COLL_FIX) // merge in immovable stuff + || ((cNbor->flags() & SlotCollision::COLL_KERN) && !sameCluster) // ignore other kernable clusters + || (cNbor->flags() & SlotCollision::COLL_ISCOL)) // test against other collided glyphs + && !coll.mergeSlot(seg, nbor, cNbor->shift(), !ignoreForKern, sameCluster, collides, false, dbgout)) + return false; + else if (nbor == slotFix) + // Switching sides of this glyph - if we were ignoring kernable stuff before, don't anymore. + ignoreForKern = !ignoreForKern; + + if (nbor != start && (cNbor->flags() & (isRev ? SlotCollision::COLL_START : SlotCollision::COLL_END))) + break; + } + bool isCol = false; + if (collides || cFix->shift().x != 0.f || cFix->shift().y != 0.f) + { + Position shift = coll.resolve(seg, isCol, dbgout); + // isCol has been set to true if a collision remains. + if (std::fabs(shift.x) < 1e38f && std::fabs(shift.y) < 1e38f) + { + if (sqr(shift.x-cFix->shift().x) + sqr(shift.y-cFix->shift().y) >= m_colThreshold * m_colThreshold) + moved = true; + cFix->setShift(shift); + if (slotFix->firstChild()) + { + Rect bbox; + Position here = slotFix->origin() + shift; + float clusterMin = here.x; + slotFix->firstChild()->finalise(seg, NULL, here, bbox, 0, clusterMin, rtl, false); + } + } + } + else + { + // This glyph is not colliding with anything. +#if !defined GRAPHITE2_NTRACING + if (dbgout) + { + *dbgout << json::object + << "missed" << objectid(dslot(seg, slotFix)); + coll.outputJsonDbg(dbgout, seg, -1); + *dbgout << json::close; + } +#endif + } + + // Set the is-collision flag bit. + if (isCol) + { cFix->setFlags(cFix->flags() | SlotCollision::COLL_ISCOL | SlotCollision::COLL_KNOWN); } + else + { cFix->setFlags((cFix->flags() & ~SlotCollision::COLL_ISCOL) | SlotCollision::COLL_KNOWN); } + hasCol |= isCol; + return true; +} + +float Pass::resolveKern(Segment *seg, Slot *slotFix, GR_MAYBE_UNUSED Slot *start, KernCollider &coll, int dir, + float &ymin, float &ymax, json *const dbgout) const +{ + Slot *nbor; // neighboring slot + float currSpace = 0.; + bool collides = false; + unsigned int space_count = 0; + Slot *base = slotFix; + while (base->attachedTo()) + base = base->attachedTo(); + SlotCollision *cFix = seg->collisionInfo(base); + const GlyphCache &gc = seg->getFace()->glyphs(); + + if (base != slotFix) + { + cFix->setFlags(cFix->flags() | SlotCollision::COLL_KERN | SlotCollision::COLL_FIX); + return 0; + } + bool seenEnd = (cFix->flags() & SlotCollision::COLL_END) != 0; + bool isInit = false; + + for (nbor = slotFix->next(); nbor; nbor = nbor->next()) + { + if (nbor->isChildOf(base)) + continue; + if (!gc.check(nbor->gid())) + return 0.; + const Rect &bb = seg->theGlyphBBoxTemporary(nbor->gid()); + SlotCollision *cNbor = seg->collisionInfo(nbor); + if (bb.bl.y == 0.f && bb.tr.y == 0.f) + { + if (m_kernColls == InWord) + break; + // Add space for a space glyph. + currSpace += nbor->advance(); + ++space_count; + } + else + { + space_count = 0; + float y = nbor->origin().y + cNbor->shift().y; + ymax = max(y + bb.tr.y, ymax); + ymin = min(y + bb.bl.y, ymin); + if (nbor != slotFix && !(cNbor->flags() & SlotCollision::COLL_IGNORE)) + { + seenEnd = true; + if (!isInit) + { + if (!coll.initSlot(seg, slotFix, cFix->limit(), cFix->margin(), + cFix->shift(), cFix->offset(), dir, ymin, ymax, dbgout)) + return 0.; + isInit = true; + } + collides |= coll.mergeSlot(seg, nbor, cNbor->shift(), currSpace, dir, dbgout); + } + } + if (cNbor->flags() & SlotCollision::COLL_END) + { + if (seenEnd && space_count < 2) + break; + else + seenEnd = true; + } + } + if (collides) + { + Position mv = coll.resolve(seg, slotFix, dir, cFix->margin(), dbgout); + coll.shift(mv, dir); + Position delta = slotFix->advancePos() + mv - cFix->shift(); + slotFix->advance(delta); + cFix->setShift(mv); + return mv.x; + } + return 0.; +} + diff --git a/gfx/graphite2/src/Position.cpp b/gfx/graphite2/src/Position.cpp new file mode 100644 index 000000000..172479875 --- /dev/null +++ b/gfx/graphite2/src/Position.cpp @@ -0,0 +1,98 @@ +/* GRAPHITE2 LICENSING + + Copyright 2010, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ +#include "inc/Position.h" +#include <cmath> + +using namespace graphite2; + +bool Rect::hitTest(Rect &other) +{ + if (bl.x > other.tr.x) return false; + if (tr.x < other.bl.x) return false; + if (bl.y > other.tr.y) return false; + if (tr.y < other.bl.y) return false; + return true; +} + +Position Rect::overlap(Position &offset, Rect &other, Position &othero) +{ + float ax = (bl.x + offset.x) - (other.tr.x + othero.x); + float ay = (bl.y + offset.y) - (other.tr.y + othero.y); + float bx = (other.bl.x + othero.x) - (tr.x + offset.x); + float by = (other.bl.y + othero.y) - (tr.y + offset.y); + return Position((ax > bx ? ax : bx), (ay > by ? ay : by)); +} + +float boundmin(float move, float lim1, float lim2, float &error) +{ + // error is always positive for easy comparison + if (move < lim1 && move < lim2) + { error = 0.; return move; } + else if (lim1 < lim2) + { error = std::fabs(move - lim1); return lim1; } + else + { error = std::fabs(move - lim2); return lim2; } +} + +#if 0 +Position Rect::constrainedAvoid(Position &offset, Rect &box, Rect &sdbox, Position &other, Rect &obox, Rect &osdbox) +{ + // a = max, i = min, s = sum, d = diff + float eax, eay, eix, eiy, eas, eis, ead, eid; + float beste = INF; + Position res; + // calculate the movements in each direction and the error (amount of remaining overlap) + // first param is movement, second and third are movement over the constraining box + float ax = boundmin(obox.tr.x + other.x - box.bl.x - offset.x + 1, tr.x - offset.x, INF, &eax); + float ay = boundmin(obox.tr.y + other.y - box.bl.y - offset.y + 1, tr.y - offset.y, INF, &eay); + float ix = boundmin(obox.bl.x + other.x - box.tr.x - offset.x + 1, bl.x - offset.x, INF, &eix); + float iy = boundmin(obox.bl.y + other.y - box.tr.y - offset.y + 1, bl.y - offset.y, INF, &eiy); + float as = boundmin(ISQRT2 * (osdbox.tr.x + other.x + other.y - sdbox.bl.x - offset.x - offset.y) + 1, tr.x - offset.x, tr.y - offset.y, &eas); + float is = boundmin(ISQRT2 * (osdbox.bl.x + other.x + other.y - sdbox.tr.x - offset.x - offset.y) + 1, bl.x - offset.x, bl.y - offset.y, &eis); + float ad = boundmin(ISQRT2 * (osdbox.tr.y + other.x - other.y - sdbox.bl.y - offset.x + offset.y) + 1, tr.y - offset.y, tr.x - offset.x, &ead); + float id = boundmin(ISQRT2 * (osdbox.bl.y + other.x - other.y - sdbox.tr.y - offset.x + offset.y) + 1, bl.y - offset.y, bl.x - offset.x, &eid); + + if (eax < beste) + { res = Position(ax, 0); beste = eax; } + if (eay < beste) + { res = Position(0, ay); beste = eay; } + if (eix < beste) + { res = Position(ix, 0); beste = eix; } + if (eiy < beste) + { res = Position(0, iy); beste = eiy; } + if (SQRT2 * (eas) < beste) + { res = Position(as, ad); beste = SQRT2 * (eas); } + if (SQRT2 * (eis) < beste) + { res = Position(is, is); beste = SQRT2 * (eis); } + if (SQRT2 * (ead) < beste) + { res = Position(ad, ad); beste = SQRT2 * (ead); } + if (SQRT2 * (eid) < beste) + { res = Position(id, id); beste = SQRT2 * (eid); } + return res; +} +#endif + diff --git a/gfx/graphite2/src/SegCache.cpp b/gfx/graphite2/src/SegCache.cpp index 446dfbec4..b577efa27 100644 --- a/gfx/graphite2/src/SegCache.cpp +++ b/gfx/graphite2/src/SegCache.cpp @@ -40,7 +40,7 @@ using namespace graphite2; SegCache::SegCache(const SegCacheStore * store, const Features & feats) : m_prefixLength(ePrefixLength), - m_maxCachedSegLength(eMaxSpliceSize), +// m_maxCachedSegLength(eMaxSpliceSize), m_segmentCount(0), m_features(feats), m_totalAccessCount(0l), m_totalMisses(0l), @@ -84,7 +84,7 @@ SegCacheEntry* SegCache::cache(SegCacheStore * store, const uint16* cmapGlyphs, { uint16 pos = 0; if (!length) return NULL; - assert(length < m_maxCachedSegLength); +// assert(length < m_maxCachedSegLength); SegCachePrefixArray pArray = m_prefixes; while (pos + 1 < m_prefixLength) { diff --git a/gfx/graphite2/src/SegCacheEntry.cpp b/gfx/graphite2/src/SegCacheEntry.cpp index 5e8764618..9dc4c386b 100644 --- a/gfx/graphite2/src/SegCacheEntry.cpp +++ b/gfx/graphite2/src/SegCacheEntry.cpp @@ -41,12 +41,13 @@ SegCacheEntry::SegCacheEntry(const uint16* cmapGlyphs, size_t length, Segment * m_attr(NULL), m_justs(NULL), m_accessCount(0), m_lastAccess(cacheTime) { - for (uint16 i = 0; i < length; i++) - { - m_unicode[i] = cmapGlyphs[i]; - } + if (m_unicode) + for (uint16 i = 0; i < length; i++) + m_unicode[i] = cmapGlyphs[i]; + const size_t glyphCount = seg->slotCount(), sizeof_sjust = SlotJustify::size_of(seg->silf()->numJustLevels()); + if (!glyphCount) return; size_t num_justs = 0, justs_pos = 0; if (seg->hasJustification()) @@ -60,7 +61,9 @@ SegCacheEntry::SegCacheEntry(const uint16* cmapGlyphs, size_t length, Segment * } const Slot * slot = seg->first(); m_glyph = new Slot[glyphCount]; - m_attr = gralloc<int16>(glyphCount * seg->numAttrs()); + int attrSize = seg->numAttrs() + (seg->hasCollisionInfo() ? (sizeof(SlotCollision) + 1) / 2 : 0); + m_attr = gralloc<int16>(glyphCount * attrSize); + if (!m_glyph || (!m_attr && seg->numAttrs())) return; m_glyphLength = glyphCount; Slot * slotCopy = m_glyph; m_glyph->prev(NULL); @@ -68,16 +71,16 @@ SegCacheEntry::SegCacheEntry(const uint16* cmapGlyphs, size_t length, Segment * uint16 pos = 0; while (slot) { - slotCopy->userAttrs(m_attr + pos * seg->numAttrs()); + slotCopy->userAttrs(m_attr + pos * attrSize); slotCopy->m_justs = m_justs ? reinterpret_cast<SlotJustify *>(m_justs + justs_pos++ * sizeof_sjust) : 0; - slotCopy->set(*slot, -static_cast<int32>(charOffset), seg->numAttrs(), seg->silf()->numJustLevels()); + slotCopy->set(*slot, -static_cast<int32>(charOffset), attrSize, seg->silf()->numJustLevels(), length); slotCopy->index(pos); if (slot->firstChild()) - slotCopy->m_child = m_glyph + slot->firstChild()->index(); + slotCopy->m_child = m_glyph + slot->firstChild()->index(); if (slot->attachedTo()) - slotCopy->attachTo(m_glyph + slot->attachedTo()->index()); + slotCopy->attachTo(m_glyph + slot->attachedTo()->index()); if (slot->nextSibling()) - slotCopy->m_sibling = m_glyph + slot->nextSibling()->index(); + slotCopy->m_sibling = m_glyph + slot->nextSibling()->index(); slot = slot->next(); ++slotCopy; ++pos; diff --git a/gfx/graphite2/src/Segment.cpp b/gfx/graphite2/src/Segment.cpp index ccce282bf..dd565acb6 100644 --- a/gfx/graphite2/src/Segment.cpp +++ b/gfx/graphite2/src/Segment.cpp @@ -36,6 +36,7 @@ of the License or (at your option) any later version. #include "inc/Slot.h" #include "inc/Main.h" #include "inc/CmapCache.h" +#include "inc/Collider.h" #include "graphite2/Segment.h" @@ -54,7 +55,8 @@ Segment::Segment(unsigned int numchars, const Face* face, uint32 script, int tex m_numCharinfo(numchars), m_passBits(m_silf->aPassBits() ? -1 : 0), m_defaultOriginal(0), - m_dir(textDir) + m_dir(textDir), + m_flags(((m_silf->flags() & 0x20) != 0) << 1) { freeSlot(newSlot()); m_bufSize = log_binary(numchars)+1; @@ -64,8 +66,10 @@ Segment::~Segment() { for (SlotRope::iterator i = m_slots.begin(); i != m_slots.end(); ++i) free(*i); - for (AttributeRope::iterator j = m_userAttrs.begin(); j != m_userAttrs.end(); ++j) - free(*j); + for (AttributeRope::iterator i = m_userAttrs.begin(); i != m_userAttrs.end(); ++i) + free(*i); + for (JustifyRope::iterator i = m_justifies.begin(); i != m_justifies.end(); ++i) + free(*i); delete[] m_charinfo; } @@ -106,15 +110,15 @@ void Segment::removeScope(SegmentScopeState & state) m_defaultOriginal = 0; } - +#if 0 void Segment::append(const Segment &other) { Rect bbox = other.m_bbox + m_advance; m_slots.insert(m_slots.end(), other.m_slots.begin(), other.m_slots.end()); - CharInfo* pNewCharInfo = new CharInfo[m_numCharinfo+other.m_numCharinfo]; //since CharInfo has no constructor, this doesn't do much + CharInfo* pNewCharInfo = new CharInfo[m_numCharinfo+other.m_numCharinfo]; //since CharInfo has no constructor, this doesn't do much for (unsigned int i=0 ; i<m_numCharinfo ; ++i) - pNewCharInfo[i] = m_charinfo[i]; + pNewCharInfo[i] = m_charinfo[i]; m_last->next(other.m_first); other.m_last->prev(m_last); m_userAttrs.insert(m_userAttrs.end(), other.m_userAttrs.begin(), other.m_userAttrs.end()); @@ -131,12 +135,14 @@ void Segment::append(const Segment &other) m_bbox = m_bbox.widen(bbox); m_passBits &= other.passBits(); } +#endif #endif // GRAPHITE2_NSEGCACHE void Segment::appendSlot(int id, int cid, int gid, int iFeats, size_t coffset) { Slot *aSlot = newSlot(); + if (!aSlot) return; m_charinfo[id].init(cid); m_charinfo[id].feats(iFeats); m_charinfo[id].base(coffset); @@ -161,20 +167,29 @@ Slot *Segment::newSlot() { if (!m_freeSlots) { + // check that the segment doesn't grow indefinintely + if (m_numGlyphs > m_numCharinfo * MAX_SEG_GROWTH_FACTOR) + return NULL; int numUser = m_silf->numUser(); #if !defined GRAPHITE2_NTRACING if (m_face->logger()) ++numUser; #endif Slot *newSlots = grzeroalloc<Slot>(m_bufSize); - int16 *newAttrs = grzeroalloc<int16>(numUser * m_bufSize); - newSlots[0].userAttrs(newAttrs); - for (size_t i = 1; i < m_bufSize - 1; i++) + int attrSize = numUser + (hasCollisionInfo() ? ((sizeof(SlotCollision) + 1) / 2) : 0); + int16 *newAttrs = grzeroalloc<int16>(m_bufSize * attrSize); + if (!newSlots || !newAttrs) { + free(newSlots); + free(newAttrs); + return NULL; + } + for (size_t i = 0; i < m_bufSize; i++) + { + ::new (newSlots + i) Slot(newAttrs + i * attrSize); newSlots[i].next(newSlots + i + 1); - newSlots[i].userAttrs(newAttrs + i * numUser); } - newSlots[m_bufSize - 1].userAttrs(newAttrs + (m_bufSize - 1) * numUser); newSlots[m_bufSize - 1].next(NULL); + newSlots[0].next(NULL); m_slots.push_back(newSlots); m_userAttrs.push_back(newAttrs); m_freeSlots = (m_bufSize > 1)? newSlots + 1 : NULL; @@ -198,8 +213,9 @@ void Segment::freeSlot(Slot *aSlot) aSlot->removeChild(aSlot->firstChild()); } // reset the slot incase it is reused - ::new (aSlot) Slot; - memset(aSlot->userAttrs(), 0, m_silf->numUser() * sizeof(int16)); + ::new (aSlot) Slot(aSlot->userAttrs()); + int attrSize = m_silf->numUser() + (hasCollisionInfo() ? ((sizeof(SlotCollision) + 1) / 2) : 0); + memset(aSlot->userAttrs(), 0, attrSize * sizeof(int16)); // Update generation counter for debug #if !defined GRAPHITE2_NTRACING if (m_face->logger()) @@ -219,6 +235,7 @@ SlotJustify *Segment::newJustify() { const size_t justSize = SlotJustify::size_of(m_silf->numJustLevels()); byte *justs = grzeroalloc<byte>(justSize * m_bufSize); + if (!justs) return NULL; for (int i = m_bufSize - 2; i >= 0; --i) { SlotJustify *p = reinterpret_cast<SlotJustify *>(justs + justSize * i); @@ -248,6 +265,7 @@ void Segment::splice(size_t offset, size_t length, Slot * const startSlot, Slot * endSlot, const Slot * srcSlot, const size_t numGlyphs) { + size_t numChars = length; extendLength(numGlyphs - length); // remove any extra if (numGlyphs < length) @@ -268,6 +286,7 @@ void Segment::splice(size_t offset, size_t length, Slot * const startSlot, while (numGlyphs > length) { Slot * extra = newSlot(); + if (!extra) return; extra->prev(endSlot); extra->next(endSlot->next()); endSlot->next(extra); @@ -282,67 +301,131 @@ void Segment::splice(size_t offset, size_t length, Slot * const startSlot, endSlot = endSlot->next(); assert(numGlyphs == length); + assert(offset + numChars <= m_numCharinfo); Slot * indexmap[eMaxSpliceSize*3]; assert(numGlyphs < sizeof indexmap/sizeof *indexmap); + int attrSize = m_silf->numUser() + (hasCollisionInfo() ? ((sizeof(SlotCollision) + 1) / 2) : 0); Slot * slot = startSlot; for (uint16 i=0; i < numGlyphs; slot = slot->next(), ++i) - indexmap[i] = slot; + indexmap[i] = slot; - slot = startSlot; - for (slot=startSlot; slot != endSlot; slot = slot->next(), srcSlot = srcSlot->next()) + for (slot = startSlot; slot != endSlot; slot = slot->next(), srcSlot = srcSlot->next()) { - slot->set(*srcSlot, offset, m_silf->numUser(), m_silf->numJustLevels()); - if (srcSlot->attachedTo()) slot->attachTo(indexmap[srcSlot->attachedTo()->index()]); - if (srcSlot->nextSibling()) slot->m_sibling = indexmap[srcSlot->nextSibling()->index()]; - if (srcSlot->firstChild()) slot->m_child = indexmap[srcSlot->firstChild()->index()]; + slot->set(*srcSlot, offset, attrSize, m_silf->numJustLevels(), numChars); + if (srcSlot->attachedTo()) slot->attachTo(indexmap[srcSlot->attachedTo()->index()]); + if (srcSlot->nextSibling()) slot->m_sibling = indexmap[srcSlot->nextSibling()->index()]; + if (srcSlot->firstChild()) slot->m_child = indexmap[srcSlot->firstChild()->index()]; } } #endif // GRAPHITE2_NSEGCACHE +// reverse the slots but keep diacritics in their same position after their bases +void Segment::reverseSlots() +{ + m_dir = m_dir ^ 64; // invert the reverse flag + if (m_first == m_last) return; // skip 0 or 1 glyph runs + + Slot *t = 0; + Slot *curr = m_first; + Slot *tlast; + Slot *tfirst; + Slot *out = 0; + + while (curr && getSlotBidiClass(curr) == 16) + curr = curr->next(); + if (!curr) return; + tfirst = curr->prev(); + tlast = curr; + + while (curr) + { + if (getSlotBidiClass(curr) == 16) + { + Slot *d = curr->next(); + while (d && getSlotBidiClass(d) == 16) + d = d->next(); + + d = d ? d->prev() : m_last; + Slot *p = out->next(); // one after the diacritics. out can't be null + if (p) + p->prev(d); + else + tlast = d; + t = d->next(); + d->next(p); + curr->prev(out); + out->next(curr); + } + else // will always fire first time round the loop + { + if (out) + out->prev(curr); + t = curr->next(); + curr->next(out); + out = curr; + } + curr = t; + } + out->prev(tfirst); + if (tfirst) + tfirst->next(out); + else + m_first = out; + m_last = tlast; +} + void Segment::linkClusters(Slot *s, Slot * end) { - end = end->next(); - - for (; s != end && !s->isBase(); s = s->next()); - Slot * ls = s; - - if (m_dir & 1) - { - for (; s != end; s = s->next()) - { - if (!s->isBase()) continue; - - s->sibling(ls); - ls = s; - } - } - else - { - for (; s != end; s = s->next()) - { - if (!s->isBase()) continue; - - ls->sibling(s); - ls = s; - } - } + end = end->next(); + + for (; s != end && !s->isBase(); s = s->next()); + Slot * ls = s; + + if (m_dir & 1) + { + for (; s != end; s = s->next()) + { + if (!s->isBase()) continue; + + s->sibling(ls); + ls = s; + } + } + else + { + for (; s != end; s = s->next()) + { + if (!s->isBase()) continue; + + ls->sibling(s); + ls = s; + } + } } -Position Segment::positionSlots(const Font *font, Slot * iStart, Slot * iEnd) +Position Segment::positionSlots(const Font *font, Slot * iStart, Slot * iEnd, bool isRtl, bool isFinal) { Position currpos(0., 0.); - Rect bbox; float clusterMin = 0.; + Rect bbox; - if (!iStart) iStart = m_first; - if (!iEnd) iEnd = m_last; + if (currdir() != isRtl) + { + Slot *temp; + reverseSlots(); + temp = iStart; + iStart = iEnd; + iEnd = temp; + } + if (!iStart) iStart = m_first; + if (!iEnd) iEnd = m_last; - if (m_dir & 1) + if (isRtl) { for (Slot * s = iEnd, * const end = iStart->prev(); s && s != end; s = s->prev()) { if (s->isBase()) - currpos = s->finalise(this, font, currpos, bbox, 0, clusterMin = currpos.x); + currpos = s->finalise(this, font, currpos, bbox, 0, clusterMin = currpos.x, isRtl, isFinal); } } else @@ -350,27 +433,46 @@ Position Segment::positionSlots(const Font *font, Slot * iStart, Slot * iEnd) for (Slot * s = iStart, * const end = iEnd->next(); s && s != end; s = s->next()) { if (s->isBase()) - currpos = s->finalise(this, font, currpos, bbox, 0, clusterMin = currpos.x); + currpos = s->finalise(this, font, currpos, bbox, 0, clusterMin = currpos.x, isRtl, isFinal); } } return currpos; } -void Segment::associateChars() +void Segment::associateChars(int offset, int numChars) { - int i = 0; + int i = 0, j = 0; + CharInfo *c, *cend; + for (c = m_charinfo + offset, cend = m_charinfo + offset + numChars; c != cend; ++c) + { + c->before(-1); + c->after(-1); + } for (Slot * s = m_first; s; s->index(i++), s = s->next()) { - int j = s->before(); - if (j < 0) continue; + j = s->before(); + if (j < 0) continue; for (const int after = s->after(); j <= after; ++j) - { - CharInfo & c = *charinfo(j); - if (c.before() == -1 || i < c.before()) c.before(i); - if (c.after() < i) c.after(i); - } + { + c = charinfo(j); + if (c->before() == -1 || i < c->before()) c->before(i); + if (c->after() < i) c->after(i); + } + } + for (Slot *s = m_first; s; s = s->next()) + { + int a; + for (a = s->after() + 1; a < offset + numChars && charinfo(a)->after() < 0; ++a) + { charinfo(a)->after(s->index()); } + --a; + s->after(a); + + for (a = s->before() - 1; a >= offset && charinfo(a)->before() < 0; --a) + { charinfo(a)->before(s->index()); } + ++a; + s->before(a); } } @@ -378,81 +480,50 @@ void Segment::associateChars() template <typename utf_iter> inline void process_utf_data(Segment & seg, const Face & face, const int fid, utf_iter c, size_t n_chars) { - const Cmap & cmap = face.cmap(); - int slotid = 0; - - const typename utf_iter::codeunit_type * const base = c; - for (; n_chars; --n_chars, ++c, ++slotid) - { - const uint32 usv = *c; - uint16 gid = cmap[usv]; - if (!gid) gid = face.findPseudo(usv); - seg.appendSlot(slotid, usv, gid, fid, c - base); - } -} - + const Cmap & cmap = face.cmap(); + int slotid = 0; -void Segment::read_text(const Face *face, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void* pStart, size_t nChars) -{ - assert(face); - assert(pFeats); - - switch (enc) - { - case gr_utf8: process_utf_data(*this, *face, addFeatures(*pFeats), utf8::const_iterator(pStart), nChars); break; - case gr_utf16: process_utf_data(*this, *face, addFeatures(*pFeats), utf16::const_iterator(pStart), nChars); break; - case gr_utf32: process_utf_data(*this, *face, addFeatures(*pFeats), utf32::const_iterator(pStart), nChars); break; - } -} - -void Segment::prepare_pos(const Font * /*font*/) -{ - // copy key changeable metrics into slot (if any); + const typename utf_iter::codeunit_type * const base = c; + for (; n_chars; --n_chars, ++c, ++slotid) + { + const uint32 usv = *c; + uint16 gid = cmap[usv]; + if (!gid) gid = face.findPseudo(usv); + seg.appendSlot(slotid, usv, gid, fid, c - base); + } } -Slot *resolveExplicit(int level, int dir, Slot *s, int nNest = 0); -void resolveWeak(int baseLevel, Slot *s); -void resolveNeutrals(int baseLevel, Slot *s); -void resolveImplicit(Slot *s, Segment *seg, uint8 aMirror); -void resolveWhitespace(int baseLevel, Segment *seg, uint8 aBidi, Slot *s); -Slot *resolveOrder(Slot * & s, const bool reordered, const int level = 0); -void Segment::bidiPass(uint8 aBidi, int paradir, uint8 aMirror) +bool Segment::read_text(const Face *face, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void* pStart, size_t nChars) { - if (slotCount() == 0) - return; + assert(face); + assert(pFeats); + if (!m_charinfo) return false; - Slot *s; - int baseLevel = paradir ? 1 : 0; - unsigned int bmask = 0; - for (s = first(); s; s = s->next()) - { - unsigned int bAttr = glyphAttr(s->gid(), aBidi); - s->setBidiClass((bAttr <= 16) * bAttr); - bmask |= (1 << s->getBidiClass()); - s->setBidiLevel(baseLevel); - } - if (bmask & (paradir ? 0x92 : 0x9C)) + // utf iterator is self recovering so we don't care about the error state of the iterator. + switch (enc) { - if (bmask & 0xF800) - resolveExplicit(baseLevel, 0, first(), 0); - if (bmask & 0x10178) - resolveWeak(baseLevel, first()); - if (bmask & 0x361) - resolveNeutrals(baseLevel, first()); - resolveImplicit(first(), this, aMirror); - resolveWhitespace(baseLevel, this, aBidi, last()); - s = resolveOrder(s = first(), baseLevel != 0); - first(s); last(s->prev()); - s->prev()->next(0); s->prev(0); + case gr_utf8: process_utf_data(*this, *face, addFeatures(*pFeats), utf8::const_iterator(pStart), nChars); break; + case gr_utf16: process_utf_data(*this, *face, addFeatures(*pFeats), utf16::const_iterator(pStart), nChars); break; + case gr_utf32: process_utf_data(*this, *face, addFeatures(*pFeats), utf32::const_iterator(pStart), nChars); break; } - else if (!(dir() & 4) && baseLevel && aMirror) + return true; +} + +void Segment::doMirror(uint16 aMirror) +{ + Slot * s; + for (s = m_first; s; s = s->next()) { - for (s = first(); s; s = s->next()) - { - unsigned short g = glyphAttr(s->gid(), aMirror); - if (g) s->setGlyph(this, g); - } + unsigned short g = glyphAttr(s->gid(), aMirror); + if (g && (!(dir() & 4) || !glyphAttr(s->gid(), aMirror + 1))) + s->setGlyph(this, g); } } +bool Segment::initCollisions() +{ + for (Slot *p = m_first; p; p = p->next()) + ::new (collisionInfo(p)) SlotCollision(this, p); + return true; +} diff --git a/gfx/graphite2/src/Silf.cpp b/gfx/graphite2/src/Silf.cpp index 74b800361..adfad4029 100644 --- a/gfx/graphite2/src/Silf.cpp +++ b/gfx/graphite2/src/Silf.cpp @@ -31,6 +31,7 @@ of the License or (at your option) any later version. #include "inc/Silf.h" #include "inc/Segment.h" #include "inc/Rule.h" +#include "inc/Error.h" using namespace graphite2; @@ -50,6 +51,7 @@ Silf::Silf() throw() m_jPass(0), m_bPass(0), m_flags(0), + m_dir(0), m_aPseudo(0), m_aBreak(0), m_aUser(0), @@ -57,6 +59,7 @@ Silf::Silf() throw() m_aMirror(0), m_aPassBits(0), m_iMaxComp(0), + m_aCollision(0), m_aLig(0), m_numPseudo(0), m_nClass(0), @@ -86,18 +89,23 @@ void Silf::releaseBuffers() throw() } -bool Silf::readGraphite(const byte * const silf_start, size_t lSilf, const Face& face, uint32 version) +bool Silf::readGraphite(const byte * const silf_start, size_t lSilf, Face& face, uint32 version) { const byte * p = silf_start, - * const silf_end = p + lSilf; + * const silf_end = p + lSilf; + Error e; + if (e.test(version >= 0x00060000, E_BADSILFVERSION)) + { + releaseBuffers(); return face.error(e); + } if (version >= 0x00030000) { - if (lSilf < 28) { releaseBuffers(); return false; } - be::skip<int32>(p); // ruleVersion + if (e.test(lSilf < 28, E_BADSIZE)) { releaseBuffers(); return face.error(e); } + be::skip<int32>(p); // ruleVersion be::skip<uint16>(p,2); // passOffset & pseudosOffset } - else if (lSilf < 20) { releaseBuffers(); return false; } + else if (e.test(lSilf < 20, E_BADSIZE)) { releaseBuffers(); return face.error(e); } const uint16 maxGlyph = be::read<uint16>(p); m_silfinfo.extra_ascent = be::read<uint16>(p); m_silfinfo.extra_descent = be::read<uint16>(p); @@ -116,70 +124,100 @@ bool Silf::readGraphite(const byte * const silf_start, size_t lSilf, const Face& // Read Justification levels. m_numJusts = be::read<uint8>(p); - if (maxGlyph >= face.glyphs().numGlyphs() - || p + m_numJusts * 8 >= silf_end) { releaseBuffers(); return false; } - m_justs = gralloc<Justinfo>(m_numJusts); - for (uint8 i = 0; i < m_numJusts; i++) + if (e.test(maxGlyph >= face.glyphs().numGlyphs(), E_BADMAXGLYPH) + || e.test(p + m_numJusts * 8 >= silf_end, E_BADNUMJUSTS)) + { + releaseBuffers(); return face.error(e); + } + + if (m_numJusts) { - ::new(m_justs + i) Justinfo(p[0], p[1], p[2], p[3]); - be::skip<byte>(p,8); + m_justs = gralloc<Justinfo>(m_numJusts); + if (e.test(!m_justs, E_OUTOFMEM)) return face.error(e); + + for (uint8 i = 0; i < m_numJusts; i++) + { + ::new(m_justs + i) Justinfo(p[0], p[1], p[2], p[3]); + be::skip<byte>(p,8); + } } - if (p + sizeof(uint16) + sizeof(uint8)*8 >= silf_end) { releaseBuffers(); return false; } - m_aLig = be::read<uint16>(p); - m_aUser = be::read<uint8>(p); - m_iMaxComp = be::read<uint8>(p); - be::skip<byte>(p,5); // direction and 4 reserved bytes - be::skip<uint16>(p, be::read<uint8>(p)); // don't need critical features yet - be::skip<byte>(p); // reserved - if (p >= silf_end) { releaseBuffers(); return false; } - be::skip<uint32>(p, be::read<uint8>(p)); // don't use scriptTag array. - if (p + sizeof(uint16) + sizeof(uint32) >= silf_end) { releaseBuffers(); return false; } + if (e.test(p + sizeof(uint16) + sizeof(uint8)*8 >= silf_end, E_BADENDJUSTS)) { releaseBuffers(); return face.error(e); } + m_aLig = be::read<uint16>(p); + m_aUser = be::read<uint8>(p); + m_iMaxComp = be::read<uint8>(p); + m_dir = be::read<uint8>(p) - 1; + m_aCollision = be::read<uint8>(p); + be::skip<byte>(p,3); + be::skip<uint16>(p, be::read<uint8>(p)); // don't need critical features yet + be::skip<byte>(p); // reserved + if (e.test(p >= silf_end, E_BADCRITFEATURES)) { releaseBuffers(); return face.error(e); } + be::skip<uint32>(p, be::read<uint8>(p)); // don't use scriptTag array. + if (e.test(p + sizeof(uint16) + sizeof(uint32) >= silf_end, E_BADSCRIPTTAGS)) { releaseBuffers(); return face.error(e); } m_gEndLine = be::read<uint16>(p); // lbGID const byte * o_passes = p, * const passes_start = silf_start + be::read<uint32>(p); const size_t num_attrs = face.glyphs().numAttrs(); - if (m_aPseudo >= num_attrs - || m_aBreak >= num_attrs - || m_aBidi >= num_attrs - || m_aMirror>= num_attrs - || m_numPasses > 128 || passes_start >= silf_end - || m_pPass < m_sPass || m_pPass > m_numPasses || m_sPass > m_numPasses - || m_jPass < m_pPass || m_jPass > m_numPasses - || (m_bPass != 0xFF && (m_bPass < m_jPass || m_bPass > m_numPasses)) - || m_aLig > 127) { releaseBuffers(); return false; } + if (e.test(m_aPseudo >= num_attrs, E_BADAPSEUDO) + || e.test(m_aBreak >= num_attrs, E_BADABREAK) + || e.test(m_aBidi >= num_attrs, E_BADABIDI) + || e.test(m_aMirror>= num_attrs, E_BADAMIRROR) + || e.test(m_aCollision && m_aCollision >= num_attrs - 5, E_BADACOLLISION) + || e.test(m_numPasses > 128, E_BADNUMPASSES) || e.test(passes_start >= silf_end, E_BADPASSESSTART) + || e.test(m_pPass < m_sPass, E_BADPASSBOUND) || e.test(m_pPass > m_numPasses, E_BADPPASS) || e.test(m_sPass > m_numPasses, E_BADSPASS) + || e.test(m_jPass < m_pPass, E_BADJPASSBOUND) || e.test(m_jPass > m_numPasses, E_BADJPASS) + || e.test((m_bPass != 0xFF && (m_bPass < m_jPass || m_bPass > m_numPasses)), E_BADBPASS) + || e.test(m_aLig > 127, E_BADALIG)) + { + releaseBuffers(); + return face.error(e); + } be::skip<uint32>(p, m_numPasses); - if (p + sizeof(uint16) >= passes_start) { releaseBuffers(); return false; } + if (e.test(p + sizeof(uint16) >= passes_start, E_BADPASSESSTART)) { releaseBuffers(); return face.error(e); } m_numPseudo = be::read<uint16>(p); - be::skip<uint16>(p, 3); // searchPseudo, pseudoSelector, pseudoShift - if (p + m_numPseudo*(sizeof(uint32) + sizeof(uint16)) >= passes_start) { - releaseBuffers(); return false; - } + be::skip<uint16>(p, 3); // searchPseudo, pseudoSelector, pseudoShift m_pseudos = new Pseudo[m_numPseudo]; + if (e.test(p + m_numPseudo*(sizeof(uint32) + sizeof(uint16)) >= passes_start, E_BADNUMPSEUDO) + || e.test(!m_pseudos, E_OUTOFMEM)) + { + releaseBuffers(); return face.error(e); + } for (int i = 0; i < m_numPseudo; i++) { m_pseudos[i].uid = be::read<uint32>(p); m_pseudos[i].gid = be::read<uint16>(p); } - const size_t clen = readClassMap(p, passes_start - p, version); - if (clen == ERROROFFSET || p + clen > passes_start) { releaseBuffers(); return false; } - + const size_t clen = readClassMap(p, passes_start - p, version, e); m_passes = new Pass[m_numPasses]; + if (e || e.test(p + clen > passes_start, E_BADPASSESSTART) + || e.test(!m_passes, E_OUTOFMEM)) + { releaseBuffers(); return face.error(e); } + for (size_t i = 0; i < m_numPasses; ++i) { const byte * const pass_start = silf_start + be::read<uint32>(o_passes), - * const pass_end = silf_start + be::peek<uint32>(o_passes); - if (pass_start > pass_end || pass_end > silf_end) { - releaseBuffers(); return false; + * const pass_end = silf_start + be::peek<uint32>(o_passes); + face.error_context((face.error_context() & 0xFF00) + EC_ASILF + (i << 16)); + if (e.test(pass_start > pass_end, E_BADPASSSTART) + || e.test(pass_start < passes_start, E_BADPASSSTART) + || e.test(pass_end > silf_end, E_BADPASSEND)) { + releaseBuffers(); return face.error(e); } + enum passtype pt = PASS_TYPE_UNKNOWN; + if (i >= m_jPass) pt = PASS_TYPE_JUSTIFICATION; + else if (i >= m_pPass) pt = PASS_TYPE_POSITIONING; + else if (i >= m_sPass) pt = PASS_TYPE_SUBSTITUTE; + else pt = PASS_TYPE_LINEBREAK; + m_passes[i].init(this); - if (!m_passes[i].readPass(pass_start, pass_end - pass_start, pass_start - silf_start, face)) + if (!m_passes[i].readPass(pass_start, pass_end - pass_start, pass_start - silf_start, face, pt, + version, e)) { - releaseBuffers(); - return false; + releaseBuffers(); + return false; } } @@ -192,69 +230,74 @@ bool Silf::readGraphite(const byte * const silf_start, size_t lSilf, const Face& return true; } -template<typename T> inline uint32 Silf::readClassOffsets(const byte *&p, size_t data_len) +template<typename T> inline uint32 Silf::readClassOffsets(const byte *&p, size_t data_len, Error &e) { - const T cls_off = 2*sizeof(uint16) + sizeof(T)*(m_nClass+1); - const size_t max_off = (be::peek<T>(p + sizeof(T)*m_nClass) - cls_off)/sizeof(uint16); - // Check that the last+1 offset is less than or equal to the class map length. - if (be::peek<T>(p) != cls_off || max_off > (data_len - cls_off)/sizeof(uint16)) - return ERROROFFSET; - - // Read in all the offsets. - m_classOffsets = gralloc<uint32>(m_nClass+1); - for (uint32 * o = m_classOffsets, * const o_end = o + m_nClass + 1; o != o_end; ++o) - { - *o = (be::read<T>(p) - cls_off)/sizeof(uint16); - if (*o > max_off) - return ERROROFFSET; - } + const T cls_off = 2*sizeof(uint16) + sizeof(T)*(m_nClass+1); + const size_t max_off = (be::peek<T>(p + sizeof(T)*m_nClass) - cls_off)/sizeof(uint16); + // Check that the last+1 offset is less than or equal to the class map length. + if (e.test(be::peek<T>(p) != cls_off, E_MISALIGNEDCLASSES) + || e.test(max_off > (data_len - cls_off)/sizeof(uint16), E_HIGHCLASSOFFSET)) + return ERROROFFSET; + + // Read in all the offsets. + m_classOffsets = gralloc<uint32>(m_nClass+1); + if (e.test(!m_classOffsets, E_OUTOFMEM)) return ERROROFFSET; + for (uint32 * o = m_classOffsets, * const o_end = o + m_nClass + 1; o != o_end; ++o) + { + *o = (be::read<T>(p) - cls_off)/sizeof(uint16); + if (e.test(*o > max_off, E_HIGHCLASSOFFSET)) + return ERROROFFSET; + } return max_off; } -size_t Silf::readClassMap(const byte *p, size_t data_len, uint32 version) +size_t Silf::readClassMap(const byte *p, size_t data_len, uint32 version, Error &e) { - if (data_len < sizeof(uint16)*2) return ERROROFFSET; + if (e.test(data_len < sizeof(uint16)*2, E_BADCLASSSIZE)) return ERROROFFSET; - m_nClass = be::read<uint16>(p); - m_nLinear = be::read<uint16>(p); + m_nClass = be::read<uint16>(p); + m_nLinear = be::read<uint16>(p); - // Check that numLinear < numClass, - // that there is at least enough data for numClasses offsets. - if (m_nLinear > m_nClass - || (m_nClass + 1) * (version >= 0x00040000 ? sizeof(uint32) : sizeof(uint16))> (data_len - 4)) - return ERROROFFSET; + // Check that numLinear < numClass, + // that there is at least enough data for numClasses offsets. + if (e.test(m_nLinear > m_nClass, E_TOOMANYLINEAR) + || e.test((m_nClass + 1) * (version >= 0x00040000 ? sizeof(uint32) : sizeof(uint16)) > (data_len - 4), E_CLASSESTOOBIG)) + return ERROROFFSET; - uint32 max_off; if (version >= 0x00040000) - max_off = readClassOffsets<uint32>(p, data_len); + max_off = readClassOffsets<uint32>(p, data_len, e); else - max_off = readClassOffsets<uint16>(p, data_len); + max_off = readClassOffsets<uint16>(p, data_len, e); if (max_off == ERROROFFSET) return ERROROFFSET; - // Check the linear offsets are sane, these must be monotonically increasing. - for (const uint32 *o = m_classOffsets, * const o_end = o + m_nLinear; o != o_end; ++o) - if (o[0] > o[1]) - return ERROROFFSET; + if (e.test((int)max_off < m_nLinear + (m_nClass - m_nLinear) * 6, E_CLASSESTOOBIG)) + return ERROROFFSET; + + // Check the linear offsets are sane, these must be monotonically increasing. + for (const uint32 *o = m_classOffsets, * const o_end = o + m_nLinear; o != o_end; ++o) + if (e.test(o[0] > o[1], E_BADCLASSOFFSET)) + return ERROROFFSET; - // Fortunately the class data is all uint16s so we can decode these now + // Fortunately the class data is all uint16s so we can decode these now m_classData = gralloc<uint16>(max_off); + if (e.test(!m_classData, E_OUTOFMEM)) return ERROROFFSET; for (uint16 *d = m_classData, * const d_end = d + max_off; d != d_end; ++d) *d = be::read<uint16>(p); - // Check the lookup class invariants for each non-linear class - for (const uint32 *o = m_classOffsets + m_nLinear, * const o_end = m_classOffsets + m_nClass; o != o_end; ++o) - { - const uint16 * lookup = m_classData + *o; - if (*o > max_off - 4 // LookupClass doesn't stretch over max_off - || lookup[0] == 0 // A LookupClass with no looks is a suspicious thing ... - || lookup[0] > (max_off - *o - 4)/2 // numIDs lookup pairs fits within (start of LookupClass' lookups array, max_off] - || lookup[3] != lookup[0] - lookup[1]) // rangeShift: numIDs - searchRange - return ERROROFFSET; - } - - return max_off; + // Check the lookup class invariants for each non-linear class + for (const uint32 *o = m_classOffsets + m_nLinear, * const o_end = m_classOffsets + m_nClass; o != o_end; ++o) + { + const uint16 * lookup = m_classData + *o; + if (e.test(*o + 4 > max_off, E_HIGHCLASSOFFSET) // LookupClass doesn't stretch over max_off + || e.test(lookup[0] == 0 // A LookupClass with no looks is a suspicious thing ... + || lookup[0] * 2 + *o + 4 > max_off // numIDs lookup pairs fits within (start of LookupClass' lookups array, max_off] + || lookup[3] + lookup[1] != lookup[0], E_BADCLASSLOOKUPINFO)) // rangeShift: numIDs - searchRange + return ERROROFFSET; + } + + return max_off; } uint16 Silf::findPseudo(uint32 uid) const @@ -271,19 +314,19 @@ uint16 Silf::findClassIndex(uint16 cid, uint16 gid) const const uint16 * cls = m_classData + m_classOffsets[cid]; if (cid < m_nLinear) // output class being used for input, shouldn't happen { - for (unsigned int i = 0, n = m_classOffsets[cid + 1]; i < n; ++i, ++cls) + for (unsigned int i = 0, n = m_classOffsets[cid + 1] - m_classOffsets[cid]; i < n; ++i, ++cls) if (*cls == gid) return i; return -1; } else { - const uint16 * min = cls + 4, // lookups array - * max = min + cls[0]*2; // lookups aray is numIDs (cls[0]) uint16 pairs long - do + const uint16 * min = cls + 4, // lookups array + * max = min + cls[0]*2; // lookups aray is numIDs (cls[0]) uint16 pairs long + do { - const uint16 * p = min + (-2 & ((max-min)/2)); - if (p[0] > gid) max = p; - else min = p; + const uint16 * p = min + (-2 & ((max-min)/2)); + if (p[0] > gid) max = p; + else min = p; } while (max - min > 2); return min[0] == gid ? min[1] : -1; @@ -309,78 +352,79 @@ uint16 Silf::getClassGlyph(uint16 cid, unsigned int index) const } -bool Silf::runGraphite(Segment *seg, uint8 firstPass, uint8 lastPass) const +bool Silf::runGraphite(Segment *seg, uint8 firstPass, uint8 lastPass, int dobidi) const { assert(seg != 0); - SlotMap map(*seg); + SlotMap map(*seg, m_dir); FiniteStateMachine fsm(map, seg->getFace()->logger()); vm::Machine m(map); unsigned int initSize = seg->slotCount(); + uint8 lbidi = m_bPass; #if !defined GRAPHITE2_NTRACING json * const dbgout = seg->getFace()->logger(); #endif if (lastPass == 0) { - if (firstPass == lastPass) + if (firstPass == lastPass && lbidi == 0xFF) return true; lastPass = m_numPasses; } + if ((firstPass < lbidi || (dobidi && firstPass == lbidi)) && (lastPass >= lbidi || (dobidi && lastPass + 1 == lbidi))) + lastPass++; + else + lbidi = 0xFF; for (size_t i = firstPass; i < lastPass; ++i) { - // bidi and mirroring - if (i == m_bPass) + // bidi and mirroring + if (i == lbidi) { #if !defined GRAPHITE2_NTRACING - if (dbgout) - { - *dbgout << json::item << json::object - << "id" << -1 - << "slots" << json::array; - seg->positionSlots(0); - for(Slot * s = seg->first(); s; s = s->next()) - *dbgout << dslot(seg, s); - *dbgout << json::close - << "rules" << json::array << json::close - << json::close; - } -#endif - - if (!(seg->dir() & 2)) - seg->bidiPass(m_aBidi, seg->dir() & 1, m_aMirror); - else if (m_aMirror) + if (dbgout) { - Slot * s; - for (s = seg->first(); s; s = s->next()) - { - unsigned short g = seg->glyphAttr(s->gid(), m_aMirror); - if (g && (!(seg->dir() & 4) || !seg->glyphAttr(s->gid(), m_aMirror + 1))) - s->setGlyph(seg, g); - } + *dbgout << json::item << json::object + << "id" << -1 + << "slots" << json::array; + seg->positionSlots(0, 0, 0, m_dir); + for(Slot * s = seg->first(); s; s = s->next()) + *dbgout << dslot(seg, s); + *dbgout << json::close + << "rules" << json::array << json::close + << json::close; } +#endif + if (seg->currdir() != (m_dir & 1)) + seg->reverseSlots(); + if (m_aMirror && (seg->dir() & 3) == 3) + seg->doMirror(m_aMirror); + --i; + lbidi = lastPass; + --lastPass; + continue; } #if !defined GRAPHITE2_NTRACING - if (dbgout) - { - *dbgout << json::item << json::object - << "id" << i+1 - << "slots" << json::array; - seg->positionSlots(0); - for(Slot * s = seg->first(); s; s = s->next()) - *dbgout << dslot(seg, s); - *dbgout << json::close; - } + if (dbgout) + { + *dbgout << json::item << json::object + << "id" << i+1 + << "slots" << json::array; + seg->positionSlots(0, 0, 0, m_dir); + for(Slot * s = seg->first(); s; s = s->next()) + *dbgout << dslot(seg, s); + *dbgout << json::close; + } #endif // test whether to reorder, prepare for positioning - if (i >= 32 || (seg->passBits() & (1 << i)) == 0) - m_passes[i].runGraphite(m, fsm); + bool reverse = (lbidi == 0xFF) && (seg->currdir() != ((m_dir & 1) ^ m_passes[i].reverseDir())); + if ((i >= 32 || (seg->passBits() & (1 << i)) == 0 || m_passes[i].collisionLoops()) + && !m_passes[i].runGraphite(m, fsm, reverse)) + return false; // only subsitution passes can change segment length, cached subsegments are short for their text if (m.status() != vm::Machine::finished - || (i < m_pPass && (seg->slotCount() > initSize * MAX_SEG_GROWTH_FACTOR - || (seg->slotCount() && seg->slotCount() * MAX_SEG_GROWTH_FACTOR < initSize)))) + || (seg->slotCount() && seg->slotCount() * MAX_SEG_GROWTH_FACTOR < initSize)) return false; } return true; diff --git a/gfx/graphite2/src/Slot.cpp b/gfx/graphite2/src/Slot.cpp index c6fc2b567..551bec977 100644 --- a/gfx/graphite2/src/Slot.cpp +++ b/gfx/graphite2/src/Slot.cpp @@ -29,31 +29,37 @@ of the License or (at your option) any later version. #include "inc/Silf.h" #include "inc/CharInfo.h" #include "inc/Rule.h" +#include "inc/Collider.h" using namespace graphite2; -Slot::Slot() : +Slot::Slot(int16 *user_attrs) : m_next(NULL), m_prev(NULL), m_glyphid(0), m_realglyphid(0), m_original(0), m_before(0), m_after(0), m_index(0), m_parent(NULL), m_child(NULL), m_sibling(NULL), - m_position(0, 0), m_shift(0, 0), m_advance(-1, -1), + m_position(0, 0), m_shift(0, 0), m_advance(0, 0), m_attach(0, 0), m_with(0, 0), m_just(0.), - m_flags(0), m_attLevel(0), m_bidiCls(0), m_bidiLevel(0), m_justs(NULL) - // Do not set m_userAttr since it is set *before* new is called since this - // is used as a positional new to reset the GrSlot + m_flags(0), m_attLevel(0), m_bidiCls(-1), m_bidiLevel(0), + m_userAttr(user_attrs), m_justs(NULL) { } // take care, this does not copy any of the GrSlot pointer fields -void Slot::set(const Slot & orig, int charOffset, size_t numUserAttr, size_t justLevels) +void Slot::set(const Slot & orig, int charOffset, size_t sizeAttr, size_t justLevels, size_t numChars) { // leave m_next and m_prev unchanged m_glyphid = orig.m_glyphid; m_realglyphid = orig.m_realglyphid; m_original = orig.m_original + charOffset; - m_before = orig.m_before + charOffset; - m_after = orig.m_after + charOffset; + if (charOffset + int(orig.m_before) < 0) + m_before = 0; + else + m_before = orig.m_before + charOffset; + if (charOffset <= 0 && orig.m_after + charOffset >= numChars) + m_after = numChars - 1; + else + m_after = orig.m_after + charOffset; m_parent = NULL; m_child = NULL; m_sibling = NULL; @@ -67,13 +73,9 @@ void Slot::set(const Slot & orig, int charOffset, size_t numUserAttr, size_t jus m_bidiCls = orig.m_bidiCls; m_bidiLevel = orig.m_bidiLevel; if (m_userAttr && orig.m_userAttr) - { - memcpy(m_userAttr, orig.m_userAttr, numUserAttr * sizeof(*m_userAttr)); - } + memcpy(m_userAttr, orig.m_userAttr, sizeAttr * sizeof(*m_userAttr)); if (m_justs && orig.m_justs) - { memcpy(m_justs, orig.m_justs, SlotJustify::size_of(justLevels)); - } } void Slot::update(int /*numGrSlots*/, int numCharInfo, Position &relpos) @@ -83,19 +85,26 @@ void Slot::update(int /*numGrSlots*/, int numCharInfo, Position &relpos) m_position = m_position + relpos; } -Position Slot::finalise(const Segment *seg, const Font *font, Position & base, Rect & bbox, uint8 attrLevel, float & clusterMin) +Position Slot::finalise(const Segment *seg, const Font *font, Position & base, Rect & bbox, uint8 attrLevel, float & clusterMin, bool rtl, bool isFinal) { + SlotCollision *coll = NULL; if (attrLevel && m_attLevel > attrLevel) return Position(0, 0); - float scale = 1.0; - Position shift(m_shift.x * ((seg->dir() & 1) * -2 + 1) + m_just, m_shift.y); + float scale = font ? font->scale() : 1.0f; + Position shift(m_shift.x * (rtl * -2 + 1) + m_just, m_shift.y); float tAdvance = m_advance.x + m_just; + if (isFinal && (coll = seg->collisionInfo(this))) + { + const Position &collshift = coll->offset(); + if (!(coll->flags() & SlotCollision::COLL_KERN) || rtl) + shift = shift + collshift; + } const GlyphFace * glyphFace = seg->getFace()->glyphs().glyphSafe(glyph()); if (font) { scale = font->scale(); shift *= scale; if (font->isHinted() && glyphFace) - tAdvance = (m_advance.x - glyphFace->theAdvance().x + m_just) * scale + font->advance(m_glyphid); + tAdvance = (m_advance.x - glyphFace->theAdvance().x + m_just) * scale + font->advance(glyph()); else tAdvance *= scale; } @@ -105,15 +114,15 @@ Position Slot::finalise(const Segment *seg, const Font *font, Position & base, R if (!m_parent) { res = base + Position(tAdvance, m_advance.y * scale); - clusterMin = base.x; + clusterMin = m_position.x; } else { float tAdv; m_position += (m_attach - m_with) * scale; - tAdv = m_advance.x >= 0.5 ? m_position.x + tAdvance - shift.x : 0.f; + tAdv = m_advance.x >= 0.5f ? m_position.x + tAdvance - shift.x : 0.f; res = Position(tAdv, 0); - if ((m_advance.x >= 0.5 || m_position.x < 0) && m_position.x < clusterMin) clusterMin = m_position.x; + if ((m_advance.x >= 0.5f || m_position.x < 0) && m_position.x < clusterMin) clusterMin = m_position.x; } if (glyphFace) @@ -124,19 +133,19 @@ Position Slot::finalise(const Segment *seg, const Font *font, Position & base, R if (m_child && m_child != this && m_child->attachedTo() == this) { - Position tRes = m_child->finalise(seg, font, m_position, bbox, attrLevel, clusterMin); - if ((!m_parent || m_advance.x >= 0.5) && tRes.x > res.x) res = tRes; + Position tRes = m_child->finalise(seg, font, m_position, bbox, attrLevel, clusterMin, rtl, isFinal); + if ((!m_parent || m_advance.x >= 0.5f) && tRes.x > res.x) res = tRes; } if (m_parent && m_sibling && m_sibling != this && m_sibling->attachedTo() == m_parent) { - Position tRes = m_sibling->finalise(seg, font, base, bbox, attrLevel, clusterMin); + Position tRes = m_sibling->finalise(seg, font, base, bbox, attrLevel, clusterMin, rtl, isFinal); if (tRes.x > res.x) res = tRes; } if (!m_parent && clusterMin < base.x) { - Position adj = Position(base.x - clusterMin, 0.); + Position adj = Position(m_position.x - clusterMin, 0.); res += adj; m_position += adj; if (m_child) m_child->floodShift(adj); @@ -144,12 +153,14 @@ Position Slot::finalise(const Segment *seg, const Font *font, Position & base, R return res; } -int32 Slot::clusterMetric(const Segment *seg, uint8 metric, uint8 attrLevel) +int32 Slot::clusterMetric(const Segment *seg, uint8 metric, uint8 attrLevel, bool rtl) { Position base; + if (glyph() >= seg->getFace()->glyphs().numGlyphs()) + return 0; Rect bbox = seg->theGlyphBBoxTemporary(glyph()); float clusterMin = 0.; - Position res = finalise(seg, NULL, base, bbox, attrLevel, clusterMin); + Position res = finalise(seg, NULL, base, bbox, attrLevel, clusterMin, rtl, false); switch (metrics(metric)) { @@ -178,9 +189,10 @@ int32 Slot::clusterMetric(const Segment *seg, uint8 metric, uint8 attrLevel) } } +#define SLOTGETCOLATTR(x) { SlotCollision *c = seg->collisionInfo(this); return c ? int(c-> x) : 0; } + int Slot::getAttr(const Segment *seg, attrCode ind, uint8 subindex) const { - if (!this) return 0; if (ind == gr_slatUserDefnV1) { ind = gr_slatUserDefn; @@ -194,38 +206,70 @@ int Slot::getAttr(const Segment *seg, attrCode ind, uint8 subindex) const switch (ind) { - case gr_slatAdvX : return int(m_advance.x); - case gr_slatAdvY : return int(m_advance.y); - case gr_slatAttTo : return m_parent ? 1 : 0; - case gr_slatAttX : return int(m_attach.x); - case gr_slatAttY : return int(m_attach.y); + case gr_slatAdvX : return int(m_advance.x); + case gr_slatAdvY : return int(m_advance.y); + case gr_slatAttTo : return m_parent ? 1 : 0; + case gr_slatAttX : return int(m_attach.x); + case gr_slatAttY : return int(m_attach.y); case gr_slatAttXOff : - case gr_slatAttYOff : return 0; + case gr_slatAttYOff : return 0; case gr_slatAttWithX : return int(m_with.x); case gr_slatAttWithY : return int(m_with.y); case gr_slatAttWithXOff: case gr_slatAttWithYOff:return 0; - case gr_slatAttLevel : return m_attLevel; - case gr_slatBreak : return seg->charinfo(m_original)->breakWeight(); - case gr_slatCompRef : return 0; - case gr_slatDir : return seg->dir(); - case gr_slatInsert : return isInsertBefore(); - case gr_slatPosX : return int(m_position.x); // but need to calculate it - case gr_slatPosY : return int(m_position.y); - case gr_slatShiftX : return int(m_shift.x); - case gr_slatShiftY : return int(m_shift.y); - case gr_slatMeasureSol: return -1; // err what's this? + case gr_slatAttLevel : return m_attLevel; + case gr_slatBreak : return seg->charinfo(m_original)->breakWeight(); + case gr_slatCompRef : return 0; + case gr_slatDir : return seg->dir() & 1; + case gr_slatInsert : return isInsertBefore(); + case gr_slatPosX : return int(m_position.x); // but need to calculate it + case gr_slatPosY : return int(m_position.y); + case gr_slatShiftX : return int(m_shift.x); + case gr_slatShiftY : return int(m_shift.y); + case gr_slatMeasureSol: return -1; // err what's this? case gr_slatMeasureEol: return -1; - case gr_slatJWidth: return m_just; - case gr_slatUserDefn : return m_userAttr[subindex]; + case gr_slatJWidth: return int(m_just); + case gr_slatUserDefn : return m_userAttr[subindex]; case gr_slatSegSplit : return seg->charinfo(m_original)->flags() & 3; - default : return 0; + case gr_slatBidiLevel: return m_bidiLevel; + case gr_slatColFlags : { SlotCollision *c = seg->collisionInfo(this); return c ? c->flags() : 0; } + case gr_slatColLimitblx : SLOTGETCOLATTR(limit().bl.x) + case gr_slatColLimitbly : SLOTGETCOLATTR(limit().bl.y) + case gr_slatColLimittrx : SLOTGETCOLATTR(limit().tr.x) + case gr_slatColLimittry : SLOTGETCOLATTR(limit().tr.y) + case gr_slatColShiftx : SLOTGETCOLATTR(offset().x) + case gr_slatColShifty : SLOTGETCOLATTR(offset().y) + case gr_slatColMargin : SLOTGETCOLATTR(margin()) + case gr_slatColMarginWt : SLOTGETCOLATTR(marginWt()) + case gr_slatColExclGlyph : SLOTGETCOLATTR(exclGlyph()) + case gr_slatColExclOffx : SLOTGETCOLATTR(exclOffset().x) + case gr_slatColExclOffy : SLOTGETCOLATTR(exclOffset().y) + case gr_slatSeqClass : SLOTGETCOLATTR(seqClass()) + case gr_slatSeqProxClass : SLOTGETCOLATTR(seqProxClass()) + case gr_slatSeqOrder : SLOTGETCOLATTR(seqOrder()) + case gr_slatSeqAboveXoff : SLOTGETCOLATTR(seqAboveXoff()) + case gr_slatSeqAboveWt : SLOTGETCOLATTR(seqAboveWt()) + case gr_slatSeqBelowXlim : SLOTGETCOLATTR(seqBelowXlim()) + case gr_slatSeqBelowWt : SLOTGETCOLATTR(seqBelowWt()) + case gr_slatSeqValignHt : SLOTGETCOLATTR(seqValignHt()) + case gr_slatSeqValignWt : SLOTGETCOLATTR(seqValignWt()) + default : return 0; } } +#define SLOTCOLSETATTR(x) { \ + SlotCollision *c = seg->collisionInfo(this); \ + if (c) { c-> x ; c->setFlags(c->flags() & ~SlotCollision::COLL_KNOWN); } \ + break; } +#define SLOTCOLSETCOMPLEXATTR(t, y, x) { \ + SlotCollision *c = seg->collisionInfo(this); \ + if (c) { \ + const t &s = c-> y; \ + c-> x ; c->setFlags(c->flags() & ~SlotCollision::COLL_KNOWN); } \ + break; } + void Slot::setAttr(Segment *seg, attrCode ind, uint8 subindex, int16 value, const SlotMap & map) { - if (!this) return; if (ind == gr_slatUserDefnV1) { ind = gr_slatUserDefn; @@ -239,20 +283,20 @@ void Slot::setAttr(Segment *seg, attrCode ind, uint8 subindex, int16 value, cons switch (ind) { - case gr_slatAdvX : m_advance.x = value; break; - case gr_slatAdvY : m_advance.y = value; break; + case gr_slatAdvX : m_advance.x = value; break; + case gr_slatAdvY : m_advance.y = value; break; case gr_slatAttTo : { const uint16 idx = uint16(value); if (idx < map.size() && map[idx]) { Slot *other = map[idx]; - if (other == this) break; + if (other == this || other == m_parent) break; if (m_parent) m_parent->removeChild(this); - if (other->child(this)) + if (!other->isChildOf(this) && other->child(this)) { attachTo(other); - if (((seg->dir() & 1) != 0) ^ (idx > subindex)) + if ((map.dir() != 0) ^ (idx > subindex)) m_with = Position(advance(), 0); else // normal match to previous root m_attach = Position(other->advance(), 0); @@ -260,36 +304,59 @@ void Slot::setAttr(Segment *seg, attrCode ind, uint8 subindex, int16 value, cons } break; } - case gr_slatAttX : m_attach.x = value; break; - case gr_slatAttY : m_attach.y = value; break; + case gr_slatAttX : m_attach.x = value; break; + case gr_slatAttY : m_attach.y = value; break; case gr_slatAttXOff : - case gr_slatAttYOff : break; - case gr_slatAttWithX : m_with.x = value; break; - case gr_slatAttWithY : m_with.y = value; break; + case gr_slatAttYOff : break; + case gr_slatAttWithX : m_with.x = value; break; + case gr_slatAttWithY : m_with.y = value; break; case gr_slatAttWithXOff : - case gr_slatAttWithYOff : break; + case gr_slatAttWithYOff : break; case gr_slatAttLevel : m_attLevel = byte(value); break; case gr_slatBreak : seg->charinfo(m_original)->breakWeight(value); break; - case gr_slatCompRef : break; // not sure what to do here - case gr_slatDir : break; // read only + case gr_slatCompRef : break; // not sure what to do here + case gr_slatDir : break; case gr_slatInsert : markInsertBefore(value? true : false); break; - case gr_slatPosX : break; // can't set these here - case gr_slatPosY : break; - case gr_slatShiftX : m_shift.x = value; break; + case gr_slatPosX : break; // can't set these here + case gr_slatPosY : break; + case gr_slatShiftX : m_shift.x = value; break; case gr_slatShiftY : m_shift.y = value; break; - case gr_slatMeasureSol : break; - case gr_slatMeasureEol : break; - case gr_slatJWidth : just(value); break; + case gr_slatMeasureSol : break; + case gr_slatMeasureEol : break; + case gr_slatJWidth : just(value); break; case gr_slatSegSplit : seg->charinfo(m_original)->addflags(value & 3); break; case gr_slatUserDefn : m_userAttr[subindex] = value; break; + case gr_slatColFlags : { + SlotCollision *c = seg->collisionInfo(this); + if (c) + c->setFlags(value); + break; } + case gr_slatColLimitblx : SLOTCOLSETCOMPLEXATTR(Rect, limit(), setLimit(Rect(Position(value, s.bl.y), s.tr))) + case gr_slatColLimitbly : SLOTCOLSETCOMPLEXATTR(Rect, limit(), setLimit(Rect(Position(s.bl.x, value), s.tr))) + case gr_slatColLimittrx : SLOTCOLSETCOMPLEXATTR(Rect, limit(), setLimit(Rect(s.bl, Position(value, s.tr.y)))) + case gr_slatColLimittry : SLOTCOLSETCOMPLEXATTR(Rect, limit(), setLimit(Rect(s.bl, Position(s.tr.x, value)))) + case gr_slatColMargin : SLOTCOLSETATTR(setMargin(value)) + case gr_slatColMarginWt : SLOTCOLSETATTR(setMarginWt(value)) + case gr_slatColExclGlyph : SLOTCOLSETATTR(setExclGlyph(value)) + case gr_slatColExclOffx : SLOTCOLSETCOMPLEXATTR(Position, exclOffset(), setExclOffset(Position(value, s.y))) + case gr_slatColExclOffy : SLOTCOLSETCOMPLEXATTR(Position, exclOffset(), setExclOffset(Position(s.x, value))) + case gr_slatSeqClass : SLOTCOLSETATTR(setSeqClass(value)) + case gr_slatSeqProxClass : SLOTCOLSETATTR(setSeqProxClass(value)) + case gr_slatSeqOrder : SLOTCOLSETATTR(setSeqOrder(value)) + case gr_slatSeqAboveXoff : SLOTCOLSETATTR(setSeqAboveXoff(value)) + case gr_slatSeqAboveWt : SLOTCOLSETATTR(setSeqAboveWt(value)) + case gr_slatSeqBelowXlim : SLOTCOLSETATTR(setSeqBelowXlim(value)) + case gr_slatSeqBelowWt : SLOTCOLSETATTR(setSeqBelowWt(value)) + case gr_slatSeqValignHt : SLOTCOLSETATTR(setSeqValignHt(value)) + case gr_slatSeqValignWt : SLOTCOLSETATTR(setSeqValignWt(value)) default : - break; + break; } } @@ -319,6 +386,7 @@ void Slot::setJustify(Segment *seg, uint8 level, uint8 subindex, int16 value) if (!m_justs) { SlotJustify *j = seg->newJustify(); + if (!j) return; j->LoadSlot(this, seg); m_justs = j; } @@ -368,6 +436,7 @@ bool Slot::removeSibling(Slot *ap) else if (ap == m_sibling) { m_sibling = m_sibling->nextSibling(); + ap->sibling(NULL); return true; } else @@ -378,6 +447,7 @@ bool Slot::removeSibling(Slot *ap) void Slot::setGlyph(Segment *seg, uint16 glyphid, const GlyphFace * theGlyph) { m_glyphid = glyphid; + m_bidiCls = -1; if (!theGlyph) { theGlyph = seg->getFace()->glyphs().glyphSafe(glyphid); @@ -389,6 +459,8 @@ void Slot::setGlyph(Segment *seg, uint16 glyphid, const GlyphFace * theGlyph) } } m_realglyphid = theGlyph->attrs()[seg->silf()->aPseudo()]; + if (m_realglyphid > seg->getFace()->glyphs().numGlyphs()) + m_realglyphid = 0; const GlyphFace *aGlyph = theGlyph; if (m_realglyphid) { @@ -397,7 +469,11 @@ void Slot::setGlyph(Segment *seg, uint16 glyphid, const GlyphFace * theGlyph) } m_advance = Position(aGlyph->theAdvance().x, 0.); if (seg->silf()->aPassBits()) + { seg->mergePassBits(theGlyph->attrs()[seg->silf()->aPassBits()]); + if (seg->silf()->numPasses() > 16) + seg->mergePassBits(theGlyph->attrs()[seg->silf()->aPassBits()+1] << 16); + } } void Slot::floodShift(Position adj) @@ -419,3 +495,30 @@ void SlotJustify::LoadSlot(const Slot *s, const Segment *seg) v[3] = seg->glyphAttr(s->gid(), justs->attrWeight()); } } + +Slot * Slot::nextInCluster(const Slot *s) const +{ + Slot *base; + if (s->firstChild()) + return s->firstChild(); + else if (s->nextSibling()) + return s->nextSibling(); + while ((base = s->attachedTo())) + { + // if (base->firstChild() == s && base->nextSibling()) + if (base->nextSibling()) + return base->nextSibling(); + s = base; + } + return NULL; +} + +bool Slot::isChildOf(const Slot *base) const +{ + if (m_parent == base) + return true; + else if (!m_parent) + return false; + else + return m_parent->isChildOf(base); +} diff --git a/gfx/graphite2/src/Sparse.cpp b/gfx/graphite2/src/Sparse.cpp index a3eb52a19..aa4311366 100644 --- a/gfx/graphite2/src/Sparse.cpp +++ b/gfx/graphite2/src/Sparse.cpp @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2011, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -30,31 +30,33 @@ of the License or (at your option) any later version. using namespace graphite2; +const sparse::chunk sparse::empty_chunk = {0,0}; sparse::~sparse() throw() { - free(m_array.values); + if (m_array.map == &empty_chunk) return; + free(m_array.values); } sparse::mapped_type sparse::operator [] (const key_type k) const throw() { mapped_type g = key_type(k/SIZEOF_CHUNK - m_nchunks) >> (sizeof k*8 - 1); - const chunk & c = m_array.map[g*k/SIZEOF_CHUNK]; - const mask_t m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK)); - g *= m & 1; + const chunk & c = m_array.map[g*k/SIZEOF_CHUNK]; + const mask_t m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK)); + g *= m & 1; - return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))]; + return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))]; } size_t sparse::capacity() const throw() { - size_t n = m_nchunks, - s = 0; + size_t n = m_nchunks, + s = 0; - for (const chunk *ci=m_array.map; n; --n, ++ci) - s += bit_set_count(ci->mask); + for (const chunk *ci=m_array.map; n; --n, ++ci) + s += bit_set_count(ci->mask); - return s; + return s; } diff --git a/gfx/graphite2/src/TtfUtil.cpp b/gfx/graphite2/src/TtfUtil.cpp index ba26946dc..0b6b98035 100644 --- a/gfx/graphite2/src/TtfUtil.cpp +++ b/gfx/graphite2/src/TtfUtil.cpp @@ -32,12 +32,12 @@ Last reviewed: Not yet. Description Implements the methods for TtfUtil class. This file should remain portable to any C++ - environment by only using standard C++ and the TTF structurs defined in Tt.h. + environment by only using standard C++ and the TTF structurs defined in Tt.h. -------------------------------------------------------------------------------*//*:End Ignore*/ /*********************************************************************************************** - Include files + Include files ***********************************************************************************************/ // Language headers //#include <algorithm> @@ -54,75 +54,77 @@ Description #include "inc/Endian.h" /*********************************************************************************************** - Forward declarations + Forward declarations ***********************************************************************************************/ /*********************************************************************************************** - Local Constants and static variables + Local Constants and static variables ***********************************************************************************************/ namespace { - // max number of components allowed in composite glyphs - const int kMaxGlyphComponents = 8; +#ifdef ALL_TTFUTILS + // max number of components allowed in composite glyphs + const int kMaxGlyphComponents = 8; +#endif - template <int R, typename T> - inline float fixed_to_float(const T f) { - return float(f)/float(2^R); - } + template <int R, typename T> + inline float fixed_to_float(const T f) { + return float(f)/float(2^R); + } /*---------------------------------------------------------------------------------------------- - Table of standard Postscript glyph names. From Martin Hosken. Disagress with ttfdump.exe + Table of standard Postscript glyph names. From Martin Hosken. Disagress with ttfdump.exe ---------------------------------------------------------------------------------------------*/ #ifdef ALL_TTFUTILS - const int kcPostNames = 258; - - const char * rgPostName[kcPostNames] = { - ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", - "dollar", "percent", "ampersand", "quotesingle", "parenleft", - "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash", - "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", - "nine", "colon", "semicolon", "less", "equal", "greater", "question", - "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", - "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", - "bracketleft", "backslash", "bracketright", "asciicircum", - "underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", - "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", - "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", - "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", - "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", - "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", - "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", - "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", - "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", - "section", "bullet", "paragraph", "germandbls", "registered", - "copyright", "trademark", "acute", "dieresis", "notequal", "AE", - "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", - "mu", "partialdiff", "summation", "product", "pi", "integral", - "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", - "exclamdown", "logicalnot", "radical", "florin", "approxequal", - "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", - "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", - "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", - "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", - "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", - "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", - "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", - "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", - "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", - "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", - "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", - "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", - "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", - "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", - "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", - "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", - "dcroat" }; + const int kcPostNames = 258; + + const char * rgPostName[kcPostNames] = { + ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", + "dollar", "percent", "ampersand", "quotesingle", "parenleft", + "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash", + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", + "nine", "colon", "semicolon", "less", "equal", "greater", "question", + "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", + "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", + "bracketleft", "backslash", "bracketright", "asciicircum", + "underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", + "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", + "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", + "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", + "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", + "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", + "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", + "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", + "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", + "section", "bullet", "paragraph", "germandbls", "registered", + "copyright", "trademark", "acute", "dieresis", "notequal", "AE", + "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", + "mu", "partialdiff", "summation", "product", "pi", "integral", + "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", + "exclamdown", "logicalnot", "radical", "florin", "approxequal", + "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", + "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", + "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", + "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", + "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", + "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", + "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", + "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", + "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", + "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", + "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", + "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", + "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", + "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", + "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", + "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", + "dcroat" }; #endif } // end of namespace /*********************************************************************************************** - Methods + Methods ***********************************************************************************************/ /* Note on error processing: The code guards against bad glyph ids being used to look up data @@ -147,681 +149,693 @@ namespace TtfUtil /*---------------------------------------------------------------------------------------------- - Get offset and size of the offset table needed to find table directory. - Return true if success, false otherwise. - lSize excludes any table directory entries. + Get offset and size of the offset table needed to find table directory. + Return true if success, false otherwise. + lSize excludes any table directory entries. ----------------------------------------------------------------------------------------------*/ bool GetHeaderInfo(size_t & lOffset, size_t & lSize) { - lOffset = 0; - lSize = offsetof(Sfnt::OffsetSubTable, table_directory); - assert(sizeof(uint32) + 4*sizeof (uint16) == lSize); - return true; + lOffset = 0; + lSize = offsetof(Sfnt::OffsetSubTable, table_directory); + assert(sizeof(uint32) + 4*sizeof (uint16) == lSize); + return true; } /*---------------------------------------------------------------------------------------------- - Check the offset table for expected data. - Return true if success, false otherwise. + Check the offset table for expected data. + Return true if success, false otherwise. ----------------------------------------------------------------------------------------------*/ bool CheckHeader(const void * pHdr) { - const Sfnt::OffsetSubTable * pOffsetTable - = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr); + const Sfnt::OffsetSubTable * pOffsetTable + = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr); - return pHdr && be::swap(pOffsetTable->scaler_type) == Sfnt::OffsetSubTable::TrueTypeWin; + return pHdr && be::swap(pOffsetTable->scaler_type) == Sfnt::OffsetSubTable::TrueTypeWin; } /*---------------------------------------------------------------------------------------------- - Get offset and size of the table directory. - Return true if successful, false otherwise. + Get offset and size of the table directory. + Return true if successful, false otherwise. ----------------------------------------------------------------------------------------------*/ bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize) { - const Sfnt::OffsetSubTable * pOffsetTable - = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr); - - lOffset = offsetof(Sfnt::OffsetSubTable, table_directory); - lSize = be::swap(pOffsetTable->num_tables) - * sizeof(Sfnt::OffsetSubTable::Entry); - - return true; + const Sfnt::OffsetSubTable * pOffsetTable + = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr); + + lOffset = offsetof(Sfnt::OffsetSubTable, table_directory); + lSize = be::swap(pOffsetTable->num_tables) + * sizeof(Sfnt::OffsetSubTable::Entry); + + return true; } /*---------------------------------------------------------------------------------------------- - Get offset and size of the specified table. - Return true if successful, false otherwise. On false, offset and size will be 0. + Get offset and size of the specified table. + Return true if successful, false otherwise. On false, offset and size will be 0. ----------------------------------------------------------------------------------------------*/ bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir, - size_t & lOffset, size_t & lSize) + size_t & lOffset, size_t & lSize) { - const Sfnt::OffsetSubTable * pOffsetTable - = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr); - const size_t num_tables = be::swap(pOffsetTable->num_tables); - const Sfnt::OffsetSubTable::Entry - * entry_itr = reinterpret_cast<const Sfnt::OffsetSubTable::Entry *>( - pTableDir), - * const dir_end = entry_itr + num_tables; - - if (num_tables > 40) - return false; - - for (;entry_itr != dir_end; ++entry_itr) // 40 - safe guard - { - if (be::swap(entry_itr->tag) == TableTag) - { - lOffset = be::swap(entry_itr->offset); - lSize = be::swap(entry_itr->length); - return true; - } - } - - return false; + const Sfnt::OffsetSubTable * pOffsetTable + = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr); + const size_t num_tables = be::swap(pOffsetTable->num_tables); + const Sfnt::OffsetSubTable::Entry + * entry_itr = reinterpret_cast<const Sfnt::OffsetSubTable::Entry *>( + pTableDir), + * const dir_end = entry_itr + num_tables; + + if (num_tables > 40) + return false; + + for (;entry_itr != dir_end; ++entry_itr) // 40 - safe guard + { + if (be::swap(entry_itr->tag) == TableTag) + { + lOffset = be::swap(entry_itr->offset); + lSize = be::swap(entry_itr->length); + return true; + } + } + + return false; } /*---------------------------------------------------------------------------------------------- - Check the specified table. Tests depend on the table type. - Return true if successful, false otherwise. + Check the specified table. Tests depend on the table type. + Return true if successful, false otherwise. ----------------------------------------------------------------------------------------------*/ bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize) { - using namespace Sfnt; - - if (pTable == 0) return false; - - switch(TableId) - { - case Tag::cmap: // cmap - { - const Sfnt::CharacterCodeMap * const pCmap - = reinterpret_cast<const Sfnt::CharacterCodeMap *>(pTable); - return be::swap(pCmap->version) == 0; - } - - case Tag::head: // head - { - const Sfnt::FontHeader * const pHead - = reinterpret_cast<const Sfnt::FontHeader *>(pTable); - bool r = be::swap(pHead->version) == OneFix - && be::swap(pHead->magic_number) == FontHeader::MagicNumber - && be::swap(pHead->glyph_data_format) - == FontHeader::GlypDataFormat - && (be::swap(pHead->index_to_loc_format) - == FontHeader::ShortIndexLocFormat - || be::swap(pHead->index_to_loc_format) - == FontHeader::LongIndexLocFormat) - && sizeof(FontHeader) <= lTableSize; - return r; - } - - case Tag::post: // post - { - const Sfnt::PostScriptGlyphName * const pPost - = reinterpret_cast<const Sfnt::PostScriptGlyphName *>(pTable); - const fixed format = be::swap(pPost->format); - bool r = format == PostScriptGlyphName::Format1 - || format == PostScriptGlyphName::Format2 - || format == PostScriptGlyphName::Format3 - || format == PostScriptGlyphName::Format25; - return r; - } - - case Tag::hhea: // hhea - { - const Sfnt::HorizontalHeader * pHhea = - reinterpret_cast<const Sfnt::HorizontalHeader *>(pTable); - bool r = be::swap(pHhea->version) == OneFix - && be::swap(pHhea->metric_data_format) == 0 - && sizeof (Sfnt::HorizontalHeader) <= lTableSize; - return r; - } - - case Tag::maxp: // maxp - { - const Sfnt::MaximumProfile * pMaxp = - reinterpret_cast<const Sfnt::MaximumProfile *>(pTable); - bool r = be::swap(pMaxp->version) == OneFix - && sizeof(Sfnt::MaximumProfile) <= lTableSize; - return r; - } - - case Tag::OS_2: // OS/2 - { - const Sfnt::Compatibility * pOs2 - = reinterpret_cast<const Sfnt::Compatibility *>(pTable); - if (be::swap(pOs2->version) == 0) - { // OS/2 table version 1 size -// if (sizeof(Sfnt::Compatibility) -// - sizeof(uint32)*2 - sizeof(int16)*2 -// - sizeof(uint16)*3 <= lTableSize) - if (sizeof(Sfnt::Compatibility0) <= lTableSize) - return true; - } - else if (be::swap(pOs2->version) == 1) - { // OS/2 table version 2 size -// if (sizeof(Sfnt::Compatibility) -// - sizeof(int16) *2 -// - sizeof(uint16)*3 <= lTableSize) - if (sizeof(Sfnt::Compatibility1) <= lTableSize) - return true; - } - else if (be::swap(pOs2->version) == 2) - { // OS/2 table version 3 size - if (sizeof(Sfnt::Compatibility2) <= lTableSize) - return true; - } - else if (be::swap(pOs2->version) == 3 || be::swap(pOs2->version) == 4) - { // OS/2 table version 4 size - version 4 changed the meaning of some fields which we don't use - if (sizeof(Sfnt::Compatibility3) <= lTableSize) - return true; - } - else - return false; - break; - } - - case Tag::name: - { - const Sfnt::FontNames * pName - = reinterpret_cast<const Sfnt::FontNames *>(pTable); - return be::swap(pName->format) == 0; - } - - default: - break; - } - - return true; + using namespace Sfnt; + + if (pTable == 0 || lTableSize < 4) return false; + + switch(TableId) + { + case Tag::cmap: // cmap + { + const Sfnt::CharacterCodeMap * const pCmap + = reinterpret_cast<const Sfnt::CharacterCodeMap *>(pTable); + if (lTableSize < sizeof(Sfnt::CharacterCodeMap)) + return false; + return be::swap(pCmap->version) == 0; + } + + case Tag::head: // head + { + const Sfnt::FontHeader * const pHead + = reinterpret_cast<const Sfnt::FontHeader *>(pTable); + if (lTableSize < sizeof(Sfnt::FontHeader)) + return false; + bool r = be::swap(pHead->version) == OneFix + && be::swap(pHead->magic_number) == FontHeader::MagicNumber + && be::swap(pHead->glyph_data_format) + == FontHeader::GlypDataFormat + && (be::swap(pHead->index_to_loc_format) + == FontHeader::ShortIndexLocFormat + || be::swap(pHead->index_to_loc_format) + == FontHeader::LongIndexLocFormat) + && sizeof(FontHeader) <= lTableSize; + return r; + } + + case Tag::post: // post + { + const Sfnt::PostScriptGlyphName * const pPost + = reinterpret_cast<const Sfnt::PostScriptGlyphName *>(pTable); + if (lTableSize < sizeof(Sfnt::PostScriptGlyphName)) + return false; + const fixed format = be::swap(pPost->format); + bool r = format == PostScriptGlyphName::Format1 + || format == PostScriptGlyphName::Format2 + || format == PostScriptGlyphName::Format3 + || format == PostScriptGlyphName::Format25; + return r; + } + + case Tag::hhea: // hhea + { + const Sfnt::HorizontalHeader * pHhea = + reinterpret_cast<const Sfnt::HorizontalHeader *>(pTable); + if (lTableSize < sizeof(Sfnt::HorizontalHeader)) + return false; + bool r = be::swap(pHhea->version) == OneFix + && be::swap(pHhea->metric_data_format) == 0 + && sizeof (Sfnt::HorizontalHeader) <= lTableSize; + return r; + } + + case Tag::maxp: // maxp + { + const Sfnt::MaximumProfile * pMaxp = + reinterpret_cast<const Sfnt::MaximumProfile *>(pTable); + if (lTableSize < sizeof(Sfnt::MaximumProfile)) + return false; + bool r = be::swap(pMaxp->version) == OneFix + && sizeof(Sfnt::MaximumProfile) <= lTableSize; + return r; + } + + case Tag::OS_2: // OS/2 + { + const Sfnt::Compatibility * pOs2 + = reinterpret_cast<const Sfnt::Compatibility *>(pTable); + if (be::swap(pOs2->version) == 0) + { // OS/2 table version 1 size +// if (sizeof(Sfnt::Compatibility) +// - sizeof(uint32)*2 - sizeof(int16)*2 +// - sizeof(uint16)*3 <= lTableSize) + if (sizeof(Sfnt::Compatibility0) <= lTableSize) + return true; + } + else if (be::swap(pOs2->version) == 1) + { // OS/2 table version 2 size +// if (sizeof(Sfnt::Compatibility) +// - sizeof(int16) *2 +// - sizeof(uint16)*3 <= lTableSize) + if (sizeof(Sfnt::Compatibility1) <= lTableSize) + return true; + } + else if (be::swap(pOs2->version) == 2) + { // OS/2 table version 3 size + if (sizeof(Sfnt::Compatibility2) <= lTableSize) + return true; + } + else if (be::swap(pOs2->version) == 3 || be::swap(pOs2->version) == 4) + { // OS/2 table version 4 size - version 4 changed the meaning of some fields which we don't use + if (sizeof(Sfnt::Compatibility3) <= lTableSize) + return true; + } + else + return false; + break; + } + + case Tag::name: + { + const Sfnt::FontNames * pName + = reinterpret_cast<const Sfnt::FontNames *>(pTable); + if (lTableSize < sizeof(Sfnt::FontNames)) + return false; + return be::swap(pName->format) == 0; + } + + default: + break; + } + + return true; } /*---------------------------------------------------------------------------------------------- - Return the number of glyphs in the font. Should never be less than zero. + Return the number of glyphs in the font. Should never be less than zero. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ size_t GlyphCount(const void * pMaxp) { - const Sfnt::MaximumProfile * pTable = - reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp); - return be::swap(pTable->num_glyphs); + const Sfnt::MaximumProfile * pTable = + reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp); + return be::swap(pTable->num_glyphs); } #ifdef ALL_TTFUTILS /*---------------------------------------------------------------------------------------------- - Return the maximum number of components for any composite glyph in the font. + Return the maximum number of components for any composite glyph in the font. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ size_t MaxCompositeComponentCount(const void * pMaxp) { - const Sfnt::MaximumProfile * pTable = - reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp); - return be::swap(pTable->max_component_elements); + const Sfnt::MaximumProfile * pTable = + reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp); + return be::swap(pTable->max_component_elements); } /*---------------------------------------------------------------------------------------------- - Composite glyphs can be composed of glyphs that are themselves composites. - This method returns the maximum number of levels like this for any glyph in the font. - A non-composite glyph has a level of 1. + Composite glyphs can be composed of glyphs that are themselves composites. + This method returns the maximum number of levels like this for any glyph in the font. + A non-composite glyph has a level of 1. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ size_t MaxCompositeLevelCount(const void * pMaxp) { - const Sfnt::MaximumProfile * pTable = - reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp); - return be::swap(pTable->max_component_depth); + const Sfnt::MaximumProfile * pTable = + reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp); + return be::swap(pTable->max_component_depth); } /*---------------------------------------------------------------------------------------------- - Return the number of glyphs in the font according to a differt source. - Should never be less than zero. Return -1 on failure. + Return the number of glyphs in the font according to a differt source. + Should never be less than zero. Return -1 on failure. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ size_t LocaGlyphCount(size_t lLocaSize, const void * pHead) //throw(std::domain_error) { - const Sfnt::FontHeader * pTable - = reinterpret_cast<const Sfnt::FontHeader *>(pHead); - - if (be::swap(pTable->index_to_loc_format) - == Sfnt::FontHeader::ShortIndexLocFormat) - // loca entries are two bytes and have been divided by two - return (lLocaSize >> 1) - 1; - - if (be::swap(pTable->index_to_loc_format) - == Sfnt::FontHeader::LongIndexLocFormat) - // loca entries are four bytes - return (lLocaSize >> 2) - 1; - - return -1; - //throw std::domain_error("head table in inconsistent state. The font may be corrupted"); + const Sfnt::FontHeader * pTable + = reinterpret_cast<const Sfnt::FontHeader *>(pHead); + + if (be::swap(pTable->index_to_loc_format) + == Sfnt::FontHeader::ShortIndexLocFormat) + // loca entries are two bytes and have been divided by two + return (lLocaSize >> 1) - 1; + + if (be::swap(pTable->index_to_loc_format) + == Sfnt::FontHeader::LongIndexLocFormat) + // loca entries are four bytes + return (lLocaSize >> 2) - 1; + + return -1; + //throw std::domain_error("head table in inconsistent state. The font may be corrupted"); } #endif /*---------------------------------------------------------------------------------------------- - Return the design units the font is designed with + Return the design units the font is designed with ----------------------------------------------------------------------------------------------*/ int DesignUnits(const void * pHead) { - const Sfnt::FontHeader * pTable = - reinterpret_cast<const Sfnt::FontHeader *>(pHead); - - return be::swap(pTable->units_per_em); + const Sfnt::FontHeader * pTable = + reinterpret_cast<const Sfnt::FontHeader *>(pHead); + + return be::swap(pTable->units_per_em); } #ifdef ALL_TTFUTILS /*---------------------------------------------------------------------------------------------- - Return the checksum from the head table, which serves as a unique identifer for the font. + Return the checksum from the head table, which serves as a unique identifer for the font. ----------------------------------------------------------------------------------------------*/ int HeadTableCheckSum(const void * pHead) { - const Sfnt::FontHeader * pTable = - reinterpret_cast<const Sfnt::FontHeader *>(pHead); - - return be::swap(pTable->check_sum_adjustment); + const Sfnt::FontHeader * pTable = + reinterpret_cast<const Sfnt::FontHeader *>(pHead); + + return be::swap(pTable->check_sum_adjustment); } /*---------------------------------------------------------------------------------------------- - Return the create time from the head table. This consists of a 64-bit integer, which - we return here as two 32-bit integers. + Return the create time from the head table. This consists of a 64-bit integer, which + we return here as two 32-bit integers. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ void HeadTableCreateTime(const void * pHead, - unsigned int * pnDateBC, unsigned int * pnDateAD) + unsigned int * pnDateBC, unsigned int * pnDateAD) { - const Sfnt::FontHeader * pTable = - reinterpret_cast<const Sfnt::FontHeader *>(pHead); - - *pnDateBC = be::swap(pTable->created[0]); - *pnDateAD = be::swap(pTable->created[1]); + const Sfnt::FontHeader * pTable = + reinterpret_cast<const Sfnt::FontHeader *>(pHead); + + *pnDateBC = be::swap(pTable->created[0]); + *pnDateAD = be::swap(pTable->created[1]); } /*---------------------------------------------------------------------------------------------- - Return the modify time from the head table.This consists of a 64-bit integer, which - we return here as two 32-bit integers. + Return the modify time from the head table.This consists of a 64-bit integer, which + we return here as two 32-bit integers. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ void HeadTableModifyTime(const void * pHead, - unsigned int * pnDateBC, unsigned int *pnDateAD) + unsigned int * pnDateBC, unsigned int *pnDateAD) { - const Sfnt::FontHeader * pTable = - reinterpret_cast<const Sfnt::FontHeader *>(pHead); - - *pnDateBC = be::swap(pTable->modified[0]); - *pnDateAD = be::swap(pTable->modified[1]); + const Sfnt::FontHeader * pTable = + reinterpret_cast<const Sfnt::FontHeader *>(pHead); + + *pnDateBC = be::swap(pTable->modified[0]); + *pnDateAD = be::swap(pTable->modified[1]); } /*---------------------------------------------------------------------------------------------- - Return true if the font is italic. + Return true if the font is italic. ----------------------------------------------------------------------------------------------*/ bool IsItalic(const void * pHead) { - const Sfnt::FontHeader * pTable = - reinterpret_cast<const Sfnt::FontHeader *>(pHead); + const Sfnt::FontHeader * pTable = + reinterpret_cast<const Sfnt::FontHeader *>(pHead); - return ((be::swap(pTable->mac_style) & 0x00000002) != 0); + return ((be::swap(pTable->mac_style) & 0x00000002) != 0); } /*---------------------------------------------------------------------------------------------- - Return the ascent for the font + Return the ascent for the font ----------------------------------------------------------------------------------------------*/ int FontAscent(const void * pOs2) { - const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2); + const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2); - return be::swap(pTable->win_ascent); + return be::swap(pTable->win_ascent); } /*---------------------------------------------------------------------------------------------- - Return the descent for the font + Return the descent for the font ----------------------------------------------------------------------------------------------*/ int FontDescent(const void * pOs2) { - const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2); + const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2); - return be::swap(pTable->win_descent); + return be::swap(pTable->win_descent); } /*---------------------------------------------------------------------------------------------- - Get the bold and italic style bits. - Return true if successful. false otherwise. - In addition to checking the OS/2 table, one could also check - the head table's macStyle field (overridden by the OS/2 table on Win) - the sub-family name in the name table (though this can contain oblique, dark, etc too) + Get the bold and italic style bits. + Return true if successful. false otherwise. + In addition to checking the OS/2 table, one could also check + the head table's macStyle field (overridden by the OS/2 table on Win) + the sub-family name in the name table (though this can contain oblique, dark, etc too) ----------------------------------------------------------------------------------------------*/ bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic) { - const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2); + const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2); - fBold = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Bold) != 0; - fItalic = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Italic) != 0; - - return true; + fBold = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Bold) != 0; + fItalic = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Italic) != 0; + + return true; } #endif /*---------------------------------------------------------------------------------------------- - Method for searching name table. + Method for searching name table. ----------------------------------------------------------------------------------------------*/ bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId, - int nLangId, int nNameId, size_t & lOffset, size_t & lSize) + int nLangId, int nNameId, size_t & lOffset, size_t & lSize) { - lOffset = 0; - lSize = 0; - - const Sfnt::FontNames * pTable = reinterpret_cast<const Sfnt::FontNames *>(pName); - uint16 cRecord = be::swap(pTable->count); - uint16 nRecordOffset = be::swap(pTable->string_offset); - const Sfnt::NameRecord * pRecord = reinterpret_cast<const Sfnt::NameRecord *>(pTable + 1); - - for (int i = 0; i < cRecord; ++i) - { - if (be::swap(pRecord->platform_id) == nPlatformId && - be::swap(pRecord->platform_specific_id) == nEncodingId && - be::swap(pRecord->language_id) == nLangId && - be::swap(pRecord->name_id) == nNameId) - { - lOffset = be::swap(pRecord->offset) + nRecordOffset; - lSize = be::swap(pRecord->length); - return true; - } - pRecord++; - } - - return false; + lOffset = 0; + lSize = 0; + + const Sfnt::FontNames * pTable = reinterpret_cast<const Sfnt::FontNames *>(pName); + uint16 cRecord = be::swap(pTable->count); + uint16 nRecordOffset = be::swap(pTable->string_offset); + const Sfnt::NameRecord * pRecord = reinterpret_cast<const Sfnt::NameRecord *>(pTable + 1); + + for (int i = 0; i < cRecord; ++i) + { + if (be::swap(pRecord->platform_id) == nPlatformId && + be::swap(pRecord->platform_specific_id) == nEncodingId && + be::swap(pRecord->language_id) == nLangId && + be::swap(pRecord->name_id) == nNameId) + { + lOffset = be::swap(pRecord->offset) + nRecordOffset; + lSize = be::swap(pRecord->length); + return true; + } + pRecord++; + } + + return false; } #ifdef ALL_TTFUTILS /*---------------------------------------------------------------------------------------------- - Return all the lang-IDs that have data for the given name-IDs. Assume that there is room - in the return array (langIdList) for 128 items. The purpose of this method is to return - a list of all possible lang-IDs. + Return all the lang-IDs that have data for the given name-IDs. Assume that there is room + in the return array (langIdList) for 128 items. The purpose of this method is to return + a list of all possible lang-IDs. ----------------------------------------------------------------------------------------------*/ int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId, - int * nameIdList, int cNameIds, short * langIdList) + int * nameIdList, int cNameIds, short * langIdList) { - const Sfnt::FontNames * pTable = reinterpret_cast<const Sfnt::FontNames *>(pName); + const Sfnt::FontNames * pTable = reinterpret_cast<const Sfnt::FontNames *>(pName); int cLangIds = 0; - uint16 cRecord = be::swap(pTable->count); + uint16 cRecord = be::swap(pTable->count); if (cRecord > 127) return cLangIds; - //uint16 nRecordOffset = swapw(pTable->stringOffset); - const Sfnt::NameRecord * pRecord = reinterpret_cast<const Sfnt::NameRecord *>(pTable + 1); - - for (int i = 0; i < cRecord; ++i) - { - if (be::swap(pRecord->platform_id) == nPlatformId && - be::swap(pRecord->platform_specific_id) == nEncodingId) - { - bool fNameFound = false; - int nLangId = be::swap(pRecord->language_id); - int nNameId = be::swap(pRecord->name_id); - for (int j = 0; j < cNameIds; j++) - { - if (nNameId == nameIdList[j]) - { - fNameFound = true; - break; - } - } - if (fNameFound) - { - // Add it if it's not there. - int ilang; - for (ilang = 0; ilang < cLangIds; ilang++) - if (langIdList[ilang] == nLangId) - break; - if (ilang >= cLangIds) - { - langIdList[cLangIds] = short(nLangId); - cLangIds++; - } - if (cLangIds == 128) - return cLangIds; - } - } - pRecord++; - } - - return cLangIds; + //uint16 nRecordOffset = swapw(pTable->stringOffset); + const Sfnt::NameRecord * pRecord = reinterpret_cast<const Sfnt::NameRecord *>(pTable + 1); + + for (int i = 0; i < cRecord; ++i) + { + if (be::swap(pRecord->platform_id) == nPlatformId && + be::swap(pRecord->platform_specific_id) == nEncodingId) + { + bool fNameFound = false; + int nLangId = be::swap(pRecord->language_id); + int nNameId = be::swap(pRecord->name_id); + for (int j = 0; j < cNameIds; j++) + { + if (nNameId == nameIdList[j]) + { + fNameFound = true; + break; + } + } + if (fNameFound) + { + // Add it if it's not there. + int ilang; + for (ilang = 0; ilang < cLangIds; ilang++) + if (langIdList[ilang] == nLangId) + break; + if (ilang >= cLangIds) + { + langIdList[cLangIds] = short(nLangId); + cLangIds++; + } + if (cLangIds == 128) + return cLangIds; + } + } + pRecord++; + } + + return cLangIds; } /*---------------------------------------------------------------------------------------------- - Get the offset and size of the font family name in English for the MS Platform with Unicode - writing system. The offset is within the pName data. The string is double byte with MSB - first. + Get the offset and size of the font family name in English for the MS Platform with Unicode + writing system. The offset is within the pName data. The string is double byte with MSB + first. ----------------------------------------------------------------------------------------------*/ bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize) { - return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, - Sfnt::NameRecord::Family, lOffset, lSize); + return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, + Sfnt::NameRecord::Family, lOffset, lSize); } /*---------------------------------------------------------------------------------------------- - Get the offset and size of the full font name in English for the MS Platform with Unicode - writing system. The offset is within the pName data. The string is double byte with MSB - first. + Get the offset and size of the full font name in English for the MS Platform with Unicode + writing system. The offset is within the pName data. The string is double byte with MSB + first. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize) { - return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, - Sfnt::NameRecord::Fullname, lOffset, lSize); + return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, + Sfnt::NameRecord::Fullname, lOffset, lSize); } /*---------------------------------------------------------------------------------------------- - Get the offset and size of the font family name in English for the MS Platform with Symbol - writing system. The offset is within the pName data. The string is double byte with MSB - first. + Get the offset and size of the font family name in English for the MS Platform with Symbol + writing system. The offset is within the pName data. The string is double byte with MSB + first. ----------------------------------------------------------------------------------------------*/ bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize) { - return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, - Sfnt::NameRecord::Family, lOffset, lSize); + return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, + Sfnt::NameRecord::Family, lOffset, lSize); } /*---------------------------------------------------------------------------------------------- - Get the offset and size of the full font name in English for the MS Platform with Symbol - writing system. The offset is within the pName data. The string is double byte with MSB - first. + Get the offset and size of the full font name in English for the MS Platform with Symbol + writing system. The offset is within the pName data. The string is double byte with MSB + first. - Note: this method is not currently used by the Graphite engine. + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize) { - return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, - Sfnt::NameRecord::Fullname, lOffset, lSize); + return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, + Sfnt::NameRecord::Fullname, lOffset, lSize); } /*---------------------------------------------------------------------------------------------- - Return the Glyph ID for a given Postscript name. This method finds the first glyph which - matches the requested Postscript name. Ideally every glyph should have a unique Postscript - name (except for special names such as .notdef), but this is not always true. - On failure return value less than zero. - -1 - table search failed - -2 - format 3 table (no Postscript glyph info) - -3 - other failures - - Note: this method is not currently used by the Graphite engine. + Return the Glyph ID for a given Postscript name. This method finds the first glyph which + matches the requested Postscript name. Ideally every glyph should have a unique Postscript + name (except for special names such as .notdef), but this is not always true. + On failure return value less than zero. + -1 - table search failed + -2 - format 3 table (no Postscript glyph info) + -3 - other failures + + Note: this method is not currently used by the Graphite engine. ----------------------------------------------------------------------------------------------*/ int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp, - const char * pPostName) + const char * pPostName) { - using namespace Sfnt; - - const Sfnt::PostScriptGlyphName * pTable - = reinterpret_cast<const Sfnt::PostScriptGlyphName *>(pPost); - fixed format = be::swap(pTable->format); - - if (format == PostScriptGlyphName::Format3) - { // format 3 - no Postscript glyph info in font - return -2; - } - - // search for given Postscript name among the standard names - int iPostName = -1; // index in standard names - for (int i = 0; i < kcPostNames; i++) - { - if (!strcmp(pPostName, rgPostName[i])) - { - iPostName = i; - break; - } - } - - if (format == PostScriptGlyphName::Format1) - { // format 1 - use standard Postscript names - return iPostName; - } - - if (format == PostScriptGlyphName::Format25) - { - if (iPostName == -1) - return -1; - - const PostScriptGlyphName25 * pTable25 - = static_cast<const PostScriptGlyphName25 *>(pTable); - int cnGlyphs = GlyphCount(pMaxp); - for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs && nGlyphId < kcPostNames; - nGlyphId++) - { // glyph_name_index25 contains bytes so no byte swapping needed - // search for first glyph id that uses the standard name - if (nGlyphId + pTable25->offset[nGlyphId] == iPostName) - return nGlyphId; - } - } - - if (format == PostScriptGlyphName::Format2) - { // format 2 - const PostScriptGlyphName2 * pTable2 - = static_cast<const PostScriptGlyphName2 *>(pTable); - - int cnGlyphs = be::swap(pTable2->number_of_glyphs); - - if (iPostName != -1) - { // did match a standard name, look for first glyph id mapped to that name - for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++) - { - if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iPostName) - return nGlyphId; - } - } - - { // did not match a standard name, search font specific names - size_t nStrSizeGoal = strlen(pPostName); - const char * pFirstGlyphName = reinterpret_cast<const char *>( - &pTable2->glyph_name_index[0] + cnGlyphs); - const char * pGlyphName = pFirstGlyphName; - int iInNames = 0; // index in font specific names - bool fFound = false; - const char * const endOfTable - = reinterpret_cast<const char *>(pTable2) + lPostSize; - while (pGlyphName < endOfTable && !fFound) - { // search Pascal strings for first matching name - size_t nStringSize = size_t(*pGlyphName); - if (nStrSizeGoal != nStringSize || - strncmp(pGlyphName + 1, pPostName, nStringSize)) - { // did not match - ++iInNames; - pGlyphName += nStringSize + 1; - } - else - { // did match - fFound = true; - } - } - if (!fFound) - return -1; // no font specific name matches request - - iInNames += kcPostNames; - for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++) - { // search for first glyph id that maps to the found string index - if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iInNames) - return nGlyphId; - } - return -1; // no glyph mapped to this index (very strange) - } - } - - return -3; + using namespace Sfnt; + + const Sfnt::PostScriptGlyphName * pTable + = reinterpret_cast<const Sfnt::PostScriptGlyphName *>(pPost); + fixed format = be::swap(pTable->format); + + if (format == PostScriptGlyphName::Format3) + { // format 3 - no Postscript glyph info in font + return -2; + } + + // search for given Postscript name among the standard names + int iPostName = -1; // index in standard names + for (int i = 0; i < kcPostNames; i++) + { + if (!strcmp(pPostName, rgPostName[i])) + { + iPostName = i; + break; + } + } + + if (format == PostScriptGlyphName::Format1) + { // format 1 - use standard Postscript names + return iPostName; + } + + if (format == PostScriptGlyphName::Format25) + { + if (iPostName == -1) + return -1; + + const PostScriptGlyphName25 * pTable25 + = static_cast<const PostScriptGlyphName25 *>(pTable); + int cnGlyphs = GlyphCount(pMaxp); + for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs && nGlyphId < kcPostNames; + nGlyphId++) + { // glyph_name_index25 contains bytes so no byte swapping needed + // search for first glyph id that uses the standard name + if (nGlyphId + pTable25->offset[nGlyphId] == iPostName) + return nGlyphId; + } + } + + if (format == PostScriptGlyphName::Format2) + { // format 2 + const PostScriptGlyphName2 * pTable2 + = static_cast<const PostScriptGlyphName2 *>(pTable); + + int cnGlyphs = be::swap(pTable2->number_of_glyphs); + + if (iPostName != -1) + { // did match a standard name, look for first glyph id mapped to that name + for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++) + { + if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iPostName) + return nGlyphId; + } + } + + { // did not match a standard name, search font specific names + size_t nStrSizeGoal = strlen(pPostName); + const char * pFirstGlyphName = reinterpret_cast<const char *>( + &pTable2->glyph_name_index[0] + cnGlyphs); + const char * pGlyphName = pFirstGlyphName; + int iInNames = 0; // index in font specific names + bool fFound = false; + const char * const endOfTable + = reinterpret_cast<const char *>(pTable2) + lPostSize; + while (pGlyphName < endOfTable && !fFound) + { // search Pascal strings for first matching name + size_t nStringSize = size_t(*pGlyphName); + if (nStrSizeGoal != nStringSize || + strncmp(pGlyphName + 1, pPostName, nStringSize)) + { // did not match + ++iInNames; + pGlyphName += nStringSize + 1; + } + else + { // did match + fFound = true; + } + } + if (!fFound) + return -1; // no font specific name matches request + + iInNames += kcPostNames; + for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++) + { // search for first glyph id that maps to the found string index + if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iInNames) + return nGlyphId; + } + return -1; // no glyph mapped to this index (very strange) + } + } + + return -3; } /*---------------------------------------------------------------------------------------------- - Convert a Unicode character string from big endian (MSB first, Motorola) format to little - endian (LSB first, Intel) format. - nSize is the number of Unicode characters in the string. It should not include any - terminating null. If nSize is 0, it is assumed the string is null terminated. nSize - defaults to 0. - Return true if successful, false otherwise. + Convert a Unicode character string from big endian (MSB first, Motorola) format to little + endian (LSB first, Intel) format. + nSize is the number of Unicode characters in the string. It should not include any + terminating null. If nSize is 0, it is assumed the string is null terminated. nSize + defaults to 0. + Return true if successful, false otherwise. ----------------------------------------------------------------------------------------------*/ void SwapWString(void * pWStr, size_t nSize /* = 0 */) //throw (std::invalid_argument) { - if (pWStr == 0) - { -// throw std::invalid_argument("null pointer given"); + if (pWStr == 0) + { +// throw std::invalid_argument("null pointer given"); return; - } + } - uint16 * pStr = reinterpret_cast<uint16 *>(pWStr); - uint16 * const pStrEnd = pStr + (nSize == 0 ? wcslen((const wchar_t*)pStr) : nSize); + uint16 * pStr = reinterpret_cast<uint16 *>(pWStr); + uint16 * const pStrEnd = pStr + (nSize == 0 ? wcslen((const wchar_t*)pStr) : nSize); for (; pStr != pStrEnd; ++pStr) *pStr = be::swap(*pStr); -// std::transform(pStr, pStrEnd, pStr, read<uint16>); +// std::transform(pStr, pStrEnd, pStr, read<uint16>); -// for (int i = 0; i < nSize; i++) -// { // swap the wide characters in the string -// pStr[i] = utf16(be::swap(uint16(pStr[i]))); -// } +// for (int i = 0; i < nSize; i++) +// { // swap the wide characters in the string +// pStr[i] = utf16(be::swap(uint16(pStr[i]))); +// } } #endif /*---------------------------------------------------------------------------------------------- - Get the left-side bearing and and advance width based on the given tables and Glyph ID - Return true if successful, false otherwise. On false, one or both value could be INT_MIN + Get the left-side bearing and and advance width based on the given tables and Glyph ID + Return true if successful, false otherwise. On false, one or both value could be INT_MIN ----------------------------------------------------------------------------------------------*/ bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize, const void * pHhea, - int & nLsb, unsigned int & nAdvWid) + int & nLsb, unsigned int & nAdvWid) { - const Sfnt::HorizontalMetric * phmtx = - reinterpret_cast<const Sfnt::HorizontalMetric *>(pHmtx); - - const Sfnt::HorizontalHeader * phhea = - reinterpret_cast<const Sfnt::HorizontalHeader *>(pHhea); - - size_t cLongHorMetrics = be::swap(phhea->num_long_hor_metrics); - if (nGlyphId < cLongHorMetrics) - { // glyph id is acceptable - if (nGlyphId * sizeof(Sfnt::HorizontalMetric) >= lHmtxSize) return false; - nAdvWid = be::swap(phmtx[nGlyphId].advance_width); - nLsb = be::swap(phmtx[nGlyphId].left_side_bearing); - } - else - { - // guard against bad glyph id - size_t lLsbOffset = sizeof(Sfnt::HorizontalMetric) * cLongHorMetrics + - sizeof(int16) * (nGlyphId - cLongHorMetrics); // offset in bytes - // We test like this as LsbOffset is an offset not a length. - if (lLsbOffset > lHmtxSize - sizeof(int16)) - { - nLsb = 0; - return false; - } + const Sfnt::HorizontalMetric * phmtx = + reinterpret_cast<const Sfnt::HorizontalMetric *>(pHmtx); + + const Sfnt::HorizontalHeader * phhea = + reinterpret_cast<const Sfnt::HorizontalHeader *>(pHhea); + + size_t cLongHorMetrics = be::swap(phhea->num_long_hor_metrics); + if (nGlyphId < cLongHorMetrics) + { // glyph id is acceptable + if ((nGlyphId + 1) * sizeof(Sfnt::HorizontalMetric) > lHmtxSize) return false; + nAdvWid = be::swap(phmtx[nGlyphId].advance_width); + nLsb = be::swap(phmtx[nGlyphId].left_side_bearing); + } + else + { + // guard against bad glyph id + size_t lLsbOffset = sizeof(Sfnt::HorizontalMetric) * cLongHorMetrics + + sizeof(int16) * (nGlyphId - cLongHorMetrics); // offset in bytes + // We test like this as LsbOffset is an offset not a length. + if (lLsbOffset >= lHmtxSize - sizeof(int16) || cLongHorMetrics == 0) + { + nLsb = 0; + return false; + } nAdvWid = be::swap(phmtx[cLongHorMetrics - 1].advance_width); - nLsb = be::peek<int16>(reinterpret_cast<const byte *>(phmtx) + lLsbOffset); - } + nLsb = be::peek<int16>(reinterpret_cast<const byte *>(phmtx) + lLsbOffset); + } - return true; + return true; } /*---------------------------------------------------------------------------------------------- - Return a pointer to the requested cmap subtable. By default find the Microsoft Unicode - subtable. Pass nEncoding as -1 to find first table that matches only nPlatformId. - Return NULL if the subtable cannot be found. + Return a pointer to the requested cmap subtable. By default find the Microsoft Unicode + subtable. Pass nEncoding as -1 to find first table that matches only nPlatformId. + Return NULL if the subtable cannot be found. ----------------------------------------------------------------------------------------------*/ const void * FindCmapSubtable(const void * pCmap, int nPlatformId, /* =3 */ int nEncodingId, /* = 1 */ size_t length) { @@ -838,10 +852,11 @@ const void * FindCmapSubtable(const void * pCmap, int nPlatformId, /* =3 */ int const uint8 * pRtn = reinterpret_cast<const uint8 *>(pCmap) + offset; if (length) { - if (offset > length) return NULL; + if (offset > length - 2) return NULL; uint16 format = be::read<uint16>(pRtn); if (format == 4) { + if (offset > length - 4) return NULL; uint16 subTableLength = be::peek<uint16>(pRtn); if (i + 1 == csuPlatforms) { @@ -853,6 +868,7 @@ const void * FindCmapSubtable(const void * pCmap, int nPlatformId, /* =3 */ int } if (format == 12) { + if (offset > length - 6) return NULL; uint32 subTableLength = be::peek<uint32>(pRtn); if (i + 1 == csuPlatforms) { @@ -871,17 +887,19 @@ const void * FindCmapSubtable(const void * pCmap, int nPlatformId, /* =3 */ int } /*---------------------------------------------------------------------------------------------- - Check the Microsoft Unicode subtable for expected values + Check the Microsoft Unicode subtable for expected values ----------------------------------------------------------------------------------------------*/ -bool CheckCmapSubtable4(const void * pCmapSubtable4) +bool CheckCmapSubtable4(const void * pCmapSubtable4, size_t table_len /*, unsigned int maxgid*/) { if (!pCmapSubtable4) return false; - const Sfnt::CmapSubTable * pTable = reinterpret_cast<const Sfnt::CmapSubTable *>(pCmapSubtable4); - // Bob H says ome freeware TT fonts have version 1 (eg, CALIGULA.TTF) - // so don't check subtable version. 21 Mar 2002 spec changes version to language. + const Sfnt::CmapSubTable * pTable = reinterpret_cast<const Sfnt::CmapSubTable *>(pCmapSubtable4); + // Bob H say some freeware TT fonts have version 1 (eg, CALIGULA.TTF) + // so don't check subtable version. 21 Mar 2002 spec changes version to language. if (be::swap(pTable->format) != 4) return false; const Sfnt::CmapSubTableFormat4 * pTable4 = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmapSubtable4); uint16 length = be::swap(pTable4->length); + if (length > table_len) + return false; if (length < sizeof(Sfnt::CmapSubTableFormat4)) return false; uint16 nRanges = be::swap(pTable4->seg_count_x2) >> 1; @@ -889,29 +907,58 @@ bool CheckCmapSubtable4(const void * pCmapSubtable4) return false; // check last range is properly terminated uint16 chEnd = be::peek<uint16>(pTable4->end_code + nRanges - 1); - return (chEnd == 0xFFFF); + if (chEnd != 0xFFFF) + return false; +#if 0 + int lastend = -1; + for (int i = 0; i < nRanges; ++i) + { + uint16 end = be::peek<uint16>(pTable4->end_code + i); + uint16 start = be::peek<uint16>(pTable4->end_code + nRanges + 1 + i); + int16 delta = be::peek<int16>(pTable4->end_code + 2*nRanges + 1 + i); + uint16 offset = be::peek<uint16>(pTable4->end_code + 3*nRanges + 1 + i); + if (lastend >= end || lastend >= start) + return false; + if (offset) + { + const uint16 *gstart = pTable4->end_code + 3*nRanges + 1 + i + (offset >> 1); + const uint16 *gend = gstart + end - start; + if ((char *)gend >= (char *)pCmapSubtable4 + length) + return false; + while (gstart <= gend) + { + uint16 g = be::peek<uint16>(gstart++); + if (g && ((g + delta) & 0xFFFF) > maxgid) + return false; + } + } + else if (((delta + end) & 0xFFFF) > maxgid) + return false; + lastend = end; + } +#endif + return true; } /*---------------------------------------------------------------------------------------------- - Return the Glyph ID for the given Unicode ID in the Microsoft Unicode subtable. - (Actually this code only depends on subtable being format 4.) - Return 0 if the Unicode ID is not in the subtable. + Return the Glyph ID for the given Unicode ID in the Microsoft Unicode subtable. + (Actually this code only depends on subtable being format 4.) + Return 0 if the Unicode ID is not in the subtable. ----------------------------------------------------------------------------------------------*/ gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey) { - const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmapSubtabel4); + const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmapSubtabel4); - uint16 nSeg = be::swap(pTable->seg_count_x2) >> 1; + uint16 nSeg = be::swap(pTable->seg_count_x2) >> 1; - uint16 n; - const uint16 * pLeft, * pMid; - uint16 cMid, chStart, chEnd; + uint16 n; + const uint16 * pLeft, * pMid; + uint16 cMid, chStart, chEnd; if (rangeKey) { pMid = &(pTable->end_code[rangeKey]); chEnd = be::peek<uint16>(pMid); - n = rangeKey; } else { @@ -967,1012 +1014,1027 @@ gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, } /*---------------------------------------------------------------------------------------------- - Return the next Unicode value in the cmap. Pass 0 to obtain the first item. - Returns 0xFFFF as the last item. - pRangeKey is an optional key that is used to optimize the search; its value is the range - in which the character is found. + Return the next Unicode value in the cmap. Pass 0 to obtain the first item. + Returns 0xFFFF as the last item. + pRangeKey is an optional key that is used to optimize the search; its value is the range + in which the character is found. ----------------------------------------------------------------------------------------------*/ unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId, int * pRangeKey) { - const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmap31); - - uint16 nRange = be::swap(pTable->seg_count_x2) >> 1; - - uint32 nUnicodePrev = (uint32)nUnicodeId; - - const uint16 * pStartCode = &(pTable->end_code[0]) - + nRange // length of end code array - + 1; // reserved word - - if (nUnicodePrev == 0) - { - // return the first codepoint. - if (pRangeKey) - *pRangeKey = 0; - return be::peek<uint16>(pStartCode); - } - else if (nUnicodePrev >= 0xFFFF) - { - if (pRangeKey) - *pRangeKey = nRange - 1; - return 0xFFFF; - } - - int iRange = (pRangeKey) ? *pRangeKey : 0; - // Just in case we have a bad key: - while (iRange > 0 && be::peek<uint16>(pStartCode + iRange) > nUnicodePrev) - iRange--; - while (be::peek<uint16>(pTable->end_code + iRange) < nUnicodePrev) - iRange++; - - // Now iRange is the range containing nUnicodePrev. - unsigned int nStartCode = be::peek<uint16>(pStartCode + iRange); - unsigned int nEndCode = be::peek<uint16>(pTable->end_code + iRange); - - if (nStartCode > nUnicodePrev) - // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable - // answer this time around. - nUnicodePrev = nStartCode - 1; - - if (nEndCode > nUnicodePrev) - { - // Next is in the same range; it is the next successive codepoint. - if (pRangeKey) - *pRangeKey = iRange; - return nUnicodePrev + 1; - } - - // Otherwise the next codepoint is the first one in the next range. - // There is guaranteed to be a next range because there must be one that - // ends with 0xFFFF. - if (pRangeKey) - *pRangeKey = iRange + 1; - return be::peek<uint16>(pStartCode + iRange + 1); + const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmap31); + + uint16 nRange = be::swap(pTable->seg_count_x2) >> 1; + + uint32 nUnicodePrev = (uint32)nUnicodeId; + + const uint16 * pStartCode = &(pTable->end_code[0]) + + nRange // length of end code array + + 1; // reserved word + + if (nUnicodePrev == 0) + { + // return the first codepoint. + if (pRangeKey) + *pRangeKey = 0; + return be::peek<uint16>(pStartCode); + } + else if (nUnicodePrev >= 0xFFFF) + { + if (pRangeKey) + *pRangeKey = nRange - 1; + return 0xFFFF; + } + + int iRange = (pRangeKey) ? *pRangeKey : 0; + // Just in case we have a bad key: + while (iRange > 0 && be::peek<uint16>(pStartCode + iRange) > nUnicodePrev) + iRange--; + while (be::peek<uint16>(pTable->end_code + iRange) < nUnicodePrev) + iRange++; + + // Now iRange is the range containing nUnicodePrev. + unsigned int nStartCode = be::peek<uint16>(pStartCode + iRange); + unsigned int nEndCode = be::peek<uint16>(pTable->end_code + iRange); + + if (nStartCode > nUnicodePrev) + // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable + // answer this time around. + nUnicodePrev = nStartCode - 1; + + if (nEndCode > nUnicodePrev) + { + // Next is in the same range; it is the next successive codepoint. + if (pRangeKey) + *pRangeKey = iRange; + return nUnicodePrev + 1; + } + + // Otherwise the next codepoint is the first one in the next range. + // There is guaranteed to be a next range because there must be one that + // ends with 0xFFFF. + if (pRangeKey) + *pRangeKey = iRange + 1; + return be::peek<uint16>(pStartCode + iRange + 1); } /*---------------------------------------------------------------------------------------------- - Check the Microsoft UCS-4 subtable for expected values. + Check the Microsoft UCS-4 subtable for expected values. ----------------------------------------------------------------------------------------------*/ -bool CheckCmapSubtable12(const void *pCmapSubtable12) +bool CheckCmapSubtable12(const void *pCmapSubtable12, size_t table_len /*, unsigned int maxgid*/) { if (!pCmapSubtable12) return false; - const Sfnt::CmapSubTable * pTable = reinterpret_cast<const Sfnt::CmapSubTable *>(pCmapSubtable12); + const Sfnt::CmapSubTable * pTable = reinterpret_cast<const Sfnt::CmapSubTable *>(pCmapSubtable12); if (be::swap(pTable->format) != 12) return false; const Sfnt::CmapSubTableFormat12 * pTable12 = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmapSubtable12); uint32 length = be::swap(pTable12->length); + if (length > table_len) + return false; if (length < sizeof(Sfnt::CmapSubTableFormat12)) return false; - - return (length == (sizeof(Sfnt::CmapSubTableFormat12) + (be::swap(pTable12->num_groups) - 1) - * sizeof(uint32) * 3)); + uint32 num_groups = be::swap(pTable12->num_groups); + if (length != (sizeof(Sfnt::CmapSubTableFormat12) + (num_groups - 1) * sizeof(uint32) * 3)) + return false; +#if 0 + for (unsigned int i = 0; i < num_groups; ++i) + { + if (be::swap(pTable12->group[i].end_char_code) - be::swap(pTable12->group[i].start_char_code) + be::swap(pTable12->group[i].start_glyph_id) > maxgid) + return false; + if (i > 0 && be::swap(pTable12->group[i].start_char_code) <= be::swap(pTable12->group[i-1].end_char_code)) + return false; + } +#endif + return true; } /*---------------------------------------------------------------------------------------------- - Return the Glyph ID for the given Unicode ID in the Microsoft UCS-4 subtable. - (Actually this code only depends on subtable being format 12.) - Return 0 if the Unicode ID is not in the subtable. + Return the Glyph ID for the given Unicode ID in the Microsoft UCS-4 subtable. + (Actually this code only depends on subtable being format 12.) + Return 0 if the Unicode ID is not in the subtable. ----------------------------------------------------------------------------------------------*/ gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey) { - const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmap310); - - //uint32 uLength = be::swap(pTable->length); //could use to test for premature end of table - uint32 ucGroups = be::swap(pTable->num_groups); - - for (unsigned int i = rangeKey; i < ucGroups; i++) - { - uint32 uStartCode = be::swap(pTable->group[i].start_char_code); - uint32 uEndCode = be::swap(pTable->group[i].end_char_code); - if (uUnicodeId >= uStartCode && uUnicodeId <= uEndCode) - { - uint32 uDiff = uUnicodeId - uStartCode; - uint32 uStartGid = be::swap(pTable->group[i].start_glyph_id); - return static_cast<gid16>(uStartGid + uDiff); - } - } - - return 0; + const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmap310); + + //uint32 uLength = be::swap(pTable->length); //could use to test for premature end of table + uint32 ucGroups = be::swap(pTable->num_groups); + + for (unsigned int i = rangeKey; i < ucGroups; i++) + { + uint32 uStartCode = be::swap(pTable->group[i].start_char_code); + uint32 uEndCode = be::swap(pTable->group[i].end_char_code); + if (uUnicodeId >= uStartCode && uUnicodeId <= uEndCode) + { + uint32 uDiff = uUnicodeId - uStartCode; + uint32 uStartGid = be::swap(pTable->group[i].start_glyph_id); + return static_cast<gid16>(uStartGid + uDiff); + } + } + + return 0; } /*---------------------------------------------------------------------------------------------- - Return the next Unicode value in the cmap. Pass 0 to obtain the first item. - Returns 0x10FFFF as the last item. - pRangeKey is an optional key that is used to optimize the search; its value is the range - in which the character is found. + Return the next Unicode value in the cmap. Pass 0 to obtain the first item. + Returns 0x10FFFF as the last item. + pRangeKey is an optional key that is used to optimize the search; its value is the range + in which the character is found. ----------------------------------------------------------------------------------------------*/ unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId, int * pRangeKey) { - const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmap310); - - int nRange = be::swap(pTable->num_groups); - - uint32 nUnicodePrev = (uint32)nUnicodeId; - - if (nUnicodePrev == 0) - { - // return the first codepoint. - if (pRangeKey) - *pRangeKey = 0; - return be::swap(pTable->group[0].start_char_code); - } - else if (nUnicodePrev >= 0x10FFFF) - { - if (pRangeKey) - *pRangeKey = nRange; - return 0x10FFFF; - } - - int iRange = (pRangeKey) ? *pRangeKey : 0; - // Just in case we have a bad key: - while (iRange > 0 && be::swap(pTable->group[iRange].start_char_code) > nUnicodePrev) - iRange--; - while (be::swap(pTable->group[iRange].end_char_code) < nUnicodePrev) - iRange++; - - // Now iRange is the range containing nUnicodePrev. - - unsigned int nStartCode = be::swap(pTable->group[iRange].start_char_code); - unsigned int nEndCode = be::swap(pTable->group[iRange].end_char_code); - - if (nStartCode > nUnicodePrev) - // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable - // answer this time around. - nUnicodePrev = nStartCode - 1; - - if (nEndCode > nUnicodePrev) - { - // Next is in the same range; it is the next successive codepoint. - if (pRangeKey) - *pRangeKey = iRange; - return nUnicodePrev + 1; - } - - // Otherwise the next codepoint is the first one in the next range, or 10FFFF if we're done. - if (pRangeKey) - *pRangeKey = iRange + 1; - return (iRange + 1 >= nRange) ? 0x10FFFF : be::swap(pTable->group[iRange + 1].start_char_code); + const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmap310); + + int nRange = be::swap(pTable->num_groups); + + uint32 nUnicodePrev = (uint32)nUnicodeId; + + if (nUnicodePrev == 0) + { + // return the first codepoint. + if (pRangeKey) + *pRangeKey = 0; + return be::swap(pTable->group[0].start_char_code); + } + else if (nUnicodePrev >= 0x10FFFF) + { + if (pRangeKey) + *pRangeKey = nRange; + return 0x10FFFF; + } + + int iRange = (pRangeKey) ? *pRangeKey : 0; + // Just in case we have a bad key: + while (iRange > 0 && be::swap(pTable->group[iRange].start_char_code) > nUnicodePrev) + iRange--; + while (be::swap(pTable->group[iRange].end_char_code) < nUnicodePrev) + iRange++; + + // Now iRange is the range containing nUnicodePrev. + + unsigned int nStartCode = be::swap(pTable->group[iRange].start_char_code); + unsigned int nEndCode = be::swap(pTable->group[iRange].end_char_code); + + if (nStartCode > nUnicodePrev) + // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable + // answer this time around. + nUnicodePrev = nStartCode - 1; + + if (nEndCode > nUnicodePrev) + { + // Next is in the same range; it is the next successive codepoint. + if (pRangeKey) + *pRangeKey = iRange; + return nUnicodePrev + 1; + } + + // Otherwise the next codepoint is the first one in the next range, or 10FFFF if we're done. + if (pRangeKey) + *pRangeKey = iRange + 1; + return (iRange + 1 >= nRange) ? 0x10FFFF : be::swap(pTable->group[iRange + 1].start_char_code); } /*---------------------------------------------------------------------------------------------- - Return the offset stored in the loca table for the given Glyph ID. - (This offset is into the glyf table.) - Return -1 if the lookup failed. - Technically this method should return an unsigned long but it is unlikely the offset will - exceed 2^31. + Return the offset stored in the loca table for the given Glyph ID. + (This offset is into the glyf table.) + Return -1 if the lookup failed. + Technically this method should return an unsigned long but it is unlikely the offset will + exceed 2^31. ----------------------------------------------------------------------------------------------*/ size_t LocaLookup(gid16 nGlyphId, - const void * pLoca, size_t lLocaSize, - const void * pHead) // throw (std::out_of_range) + const void * pLoca, size_t lLocaSize, + const void * pHead) // throw (std::out_of_range) { - const Sfnt::FontHeader * pTable = reinterpret_cast<const Sfnt::FontHeader *>(pHead); - - // CheckTable verifies the index_to_loc_format is valid - if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat) - { // loca entries are two bytes and have been divided by two - if (nGlyphId < (lLocaSize >> 1) - 1) // allow sentinel value to be accessed - { - const uint16 * pShortTable = reinterpret_cast<const uint16 *>(pLoca); - return (be::peek<uint16>(pShortTable + nGlyphId) << 1); - } - } - - if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat) - { // loca entries are four bytes - if (nGlyphId < (lLocaSize >> 2) - 1) - { - const uint32 * pLongTable = reinterpret_cast<const uint32 *>(pLoca); - return be::peek<uint32>(pLongTable + nGlyphId); - } - } - - // only get here if glyph id was bad - return -1; - //throw std::out_of_range("glyph id out of range for font"); + const Sfnt::FontHeader * pTable = reinterpret_cast<const Sfnt::FontHeader *>(pHead); + size_t res = -2; + + // CheckTable verifies the index_to_loc_format is valid + if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat) + { // loca entries are two bytes and have been divided by two + if (lLocaSize > 1 && nGlyphId + 1u < lLocaSize >> 1) // allow sentinel value to be accessed + { + const uint16 * pShortTable = reinterpret_cast<const uint16 *>(pLoca); + res = be::peek<uint16>(pShortTable + nGlyphId) << 1; + if (res == static_cast<size_t>(be::peek<uint16>(pShortTable + nGlyphId + 1) << 1)) + return -1; + } + } + else if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat) + { // loca entries are four bytes + if (lLocaSize > 3 && nGlyphId + 1u < lLocaSize >> 2) + { + const uint32 * pLongTable = reinterpret_cast<const uint32 *>(pLoca); + res = be::peek<uint32>(pLongTable + nGlyphId); + if (res == static_cast<size_t>(be::peek<uint32>(pLongTable + nGlyphId + 1))) + return -1; + } + } + + // only get here if glyph id was bad + return res; + //throw std::out_of_range("glyph id out of range for font"); } /*---------------------------------------------------------------------------------------------- - Return a pointer into the glyf table based on the given offset (from LocaLookup). - Return NULL on error. + Return a pointer into the glyf table based on the given offset (from LocaLookup). + Return NULL on error. ----------------------------------------------------------------------------------------------*/ void * GlyfLookup(const void * pGlyf, size_t nGlyfOffset, size_t nTableLen) { - const uint8 * pByte = reinterpret_cast<const uint8 *>(pGlyf); - if (nGlyfOffset == size_t(-1) || nGlyfOffset >= nTableLen) + const uint8 * pByte = reinterpret_cast<const uint8 *>(pGlyf); + if (nGlyfOffset + pByte < pByte || nGlyfOffset + sizeof(Sfnt::Glyph) >= nTableLen) return NULL; - return const_cast<uint8 *>(pByte + nGlyfOffset); + return const_cast<uint8 *>(pByte + nGlyfOffset); } /*---------------------------------------------------------------------------------------------- - Get the bounding box coordinates for a simple glyf entry (non-composite). - Return true if successful, false otherwise. + Get the bounding box coordinates for a simple glyf entry (non-composite). + Return true if successful, false otherwise. ----------------------------------------------------------------------------------------------*/ bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin, - int & xMax, int & yMax) + int & xMax, int & yMax) { - const Sfnt::Glyph * pGlyph = reinterpret_cast<const Sfnt::Glyph *>(pSimpleGlyf); + const Sfnt::Glyph * pGlyph = reinterpret_cast<const Sfnt::Glyph *>(pSimpleGlyf); - xMin = be::swap(pGlyph->x_min); - yMin = be::swap(pGlyph->y_min); - xMax = be::swap(pGlyph->x_max); - yMax = be::swap(pGlyph->y_max); + xMin = be::swap(pGlyph->x_min); + yMin = be::swap(pGlyph->y_min); + xMax = be::swap(pGlyph->x_max); + yMax = be::swap(pGlyph->y_max); - return true; + return true; } #ifdef ALL_TTFUTILS /*---------------------------------------------------------------------------------------------- - Return the number of contours for a simple glyf entry (non-composite) - Returning -1 means this is a composite glyph + Return the number of contours for a simple glyf entry (non-composite) + Returning -1 means this is a composite glyph ----------------------------------------------------------------------------------------------*/ int GlyfContourCount(const void * pSimpleGlyf) { - const Sfnt::Glyph * pGlyph = reinterpret_cast<const Sfnt::Glyph *>(pSimpleGlyf); - return be::swap(pGlyph->number_of_contours); // -1 means composite glyph + const Sfnt::Glyph * pGlyph = reinterpret_cast<const Sfnt::Glyph *>(pSimpleGlyf); + return be::swap(pGlyph->number_of_contours); // -1 means composite glyph } /*---------------------------------------------------------------------------------------------- - Get the point numbers for the end points of the glyph contours for a simple - glyf entry (non-composite). - cnPointsTotal - count of contours from GlyfContourCount(); (same as number of end points) - prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers - cnPoints - count of points placed in above range - Return true if successful, false otherwise. - False could indicate a multi-level composite glyphs. + Get the point numbers for the end points of the glyph contours for a simple + glyf entry (non-composite). + cnPointsTotal - count of contours from GlyfContourCount(); (same as number of end points) + prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers + cnPoints - count of points placed in above range + Return true if successful, false otherwise. + False could indicate a multi-level composite glyphs. ----------------------------------------------------------------------------------------------*/ bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint, - int cnPointsTotal, int & cnPoints) + int cnPointsTotal, int & cnPoints) { - const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); + const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); - int cContours = be::swap(pGlyph->number_of_contours); - if (cContours < 0) - return false; // this method isn't supposed handle composite glyphs + int cContours = be::swap(pGlyph->number_of_contours); + if (cContours < 0) + return false; // this method isn't supposed handle composite glyphs - for (int i = 0; i < cContours && i < cnPointsTotal; i++) - { - prgnContourEndPoint[i] = be::swap(pGlyph->end_pts_of_contours[i]); - } + for (int i = 0; i < cContours && i < cnPointsTotal; i++) + { + prgnContourEndPoint[i] = be::swap(pGlyph->end_pts_of_contours[i]); + } - cnPoints = cContours; - return true; + cnPoints = cContours; + return true; } /*---------------------------------------------------------------------------------------------- - Get the points for a simple glyf entry (non-composite) - cnPointsTotal - count of points from largest end point obtained from GlyfContourEndPoints - prgnX & prgnY - should point to buffers large enough to hold cnPointsTotal integers - The ranges are parallel so that coordinates for point(n) are found at offset n in both - ranges. This is raw point data with relative coordinates. - prgbFlag - should point to a buffer a large enough to hold cnPointsTotal bytes - This range is parallel to the prgnX & prgnY - cnPoints - count of points placed in above ranges - Return true if successful, false otherwise. - False could indicate a composite glyph + Get the points for a simple glyf entry (non-composite) + cnPointsTotal - count of points from largest end point obtained from GlyfContourEndPoints + prgnX & prgnY - should point to buffers large enough to hold cnPointsTotal integers + The ranges are parallel so that coordinates for point(n) are found at offset n in both + ranges. This is raw point data with relative coordinates. + prgbFlag - should point to a buffer a large enough to hold cnPointsTotal bytes + This range is parallel to the prgnX & prgnY + cnPoints - count of points placed in above ranges + Return true if successful, false otherwise. + False could indicate a composite glyph ----------------------------------------------------------------------------------------------*/ bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY, - char * prgbFlag, int cnPointsTotal, int & cnPoints) + char * prgbFlag, int cnPointsTotal, int & cnPoints) { - using namespace Sfnt; - - const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); - int cContours = be::swap(pGlyph->number_of_contours); - // return false for composite glyph - if (cContours <= 0) - return false; - int cPts = be::swap(pGlyph->end_pts_of_contours[cContours - 1]) + 1; - if (cPts > cnPointsTotal) - return false; - - // skip over bounding box data & point to byte count of instructions (hints) - const uint8 * pbGlyph = reinterpret_cast<const uint8 *> - (&pGlyph->end_pts_of_contours[cContours]); - - // skip over hints & point to first flag - int cbHints = be::swap(*(uint16 *)pbGlyph); - pbGlyph += sizeof(uint16); - pbGlyph += cbHints; - - // load flags & point to first x coordinate - int iFlag = 0; - while (iFlag < cPts) - { - if (!(*pbGlyph & SimpleGlyph::Repeat)) - { // flag isn't repeated - prgbFlag[iFlag] = (char)*pbGlyph; - pbGlyph++; - iFlag++; - } - else - { // flag is repeated; count specified by next byte - char chFlag = (char)*pbGlyph; - pbGlyph++; - int cFlags = (int)*pbGlyph; - pbGlyph++; - prgbFlag[iFlag] = chFlag; - iFlag++; - for (int i = 0; i < cFlags; i++) - { - prgbFlag[iFlag + i] = chFlag; - } - iFlag += cFlags; - } - } - if (iFlag != cPts) - return false; - - // load x coordinates - iFlag = 0; - while (iFlag < cPts) - { - if (prgbFlag[iFlag] & SimpleGlyph::XShort) - { - prgnX[iFlag] = *pbGlyph; - if (!(prgbFlag[iFlag] & SimpleGlyph::XIsPos)) - { - prgnX[iFlag] = -prgnX[iFlag]; - } - pbGlyph++; - } - else - { - if (prgbFlag[iFlag] & SimpleGlyph::XIsSame) - { - prgnX[iFlag] = 0; - // do NOT increment pbGlyph - } - else - { - prgnX[iFlag] = be::swap(*(int16 *)pbGlyph); - pbGlyph += sizeof(int16); - } - } - iFlag++; - } - - // load y coordinates - iFlag = 0; - while (iFlag < cPts) - { - if (prgbFlag[iFlag] & SimpleGlyph::YShort) - { - prgnY[iFlag] = *pbGlyph; - if (!(prgbFlag[iFlag] & SimpleGlyph::YIsPos)) - { - prgnY[iFlag] = -prgnY[iFlag]; - } - pbGlyph++; - } - else - { - if (prgbFlag[iFlag] & SimpleGlyph::YIsSame) - { - prgnY[iFlag] = 0; - // do NOT increment pbGlyph - } - else - { - prgnY[iFlag] = be::swap(*(int16 *)pbGlyph); - pbGlyph += sizeof(int16); - } - } - iFlag++; - } - - cnPoints = cPts; - return true; + using namespace Sfnt; + + const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); + int cContours = be::swap(pGlyph->number_of_contours); + // return false for composite glyph + if (cContours <= 0) + return false; + int cPts = be::swap(pGlyph->end_pts_of_contours[cContours - 1]) + 1; + if (cPts > cnPointsTotal) + return false; + + // skip over bounding box data & point to byte count of instructions (hints) + const uint8 * pbGlyph = reinterpret_cast<const uint8 *> + (&pGlyph->end_pts_of_contours[cContours]); + + // skip over hints & point to first flag + int cbHints = be::swap(*(uint16 *)pbGlyph); + pbGlyph += sizeof(uint16); + pbGlyph += cbHints; + + // load flags & point to first x coordinate + int iFlag = 0; + while (iFlag < cPts) + { + if (!(*pbGlyph & SimpleGlyph::Repeat)) + { // flag isn't repeated + prgbFlag[iFlag] = (char)*pbGlyph; + pbGlyph++; + iFlag++; + } + else + { // flag is repeated; count specified by next byte + char chFlag = (char)*pbGlyph; + pbGlyph++; + int cFlags = (int)*pbGlyph; + pbGlyph++; + prgbFlag[iFlag] = chFlag; + iFlag++; + for (int i = 0; i < cFlags; i++) + { + prgbFlag[iFlag + i] = chFlag; + } + iFlag += cFlags; + } + } + if (iFlag != cPts) + return false; + + // load x coordinates + iFlag = 0; + while (iFlag < cPts) + { + if (prgbFlag[iFlag] & SimpleGlyph::XShort) + { + prgnX[iFlag] = *pbGlyph; + if (!(prgbFlag[iFlag] & SimpleGlyph::XIsPos)) + { + prgnX[iFlag] = -prgnX[iFlag]; + } + pbGlyph++; + } + else + { + if (prgbFlag[iFlag] & SimpleGlyph::XIsSame) + { + prgnX[iFlag] = 0; + // do NOT increment pbGlyph + } + else + { + prgnX[iFlag] = be::swap(*(int16 *)pbGlyph); + pbGlyph += sizeof(int16); + } + } + iFlag++; + } + + // load y coordinates + iFlag = 0; + while (iFlag < cPts) + { + if (prgbFlag[iFlag] & SimpleGlyph::YShort) + { + prgnY[iFlag] = *pbGlyph; + if (!(prgbFlag[iFlag] & SimpleGlyph::YIsPos)) + { + prgnY[iFlag] = -prgnY[iFlag]; + } + pbGlyph++; + } + else + { + if (prgbFlag[iFlag] & SimpleGlyph::YIsSame) + { + prgnY[iFlag] = 0; + // do NOT increment pbGlyph + } + else + { + prgnY[iFlag] = be::swap(*(int16 *)pbGlyph); + pbGlyph += sizeof(int16); + } + } + iFlag++; + } + + cnPoints = cPts; + return true; } /*---------------------------------------------------------------------------------------------- - Fill prgnCompId with the component Glyph IDs from pSimpleGlyf. - Client must allocate space before calling. - pSimpleGlyf - assumed to point to a composite glyph - cCompIdTotal - the number of elements in prgnCompId - cCompId - the total number of Glyph IDs stored in prgnCompId - Return true if successful, false otherwise - False could indicate a non-composite glyph or the input array was not big enough + Fill prgnCompId with the component Glyph IDs from pSimpleGlyf. + Client must allocate space before calling. + pSimpleGlyf - assumed to point to a composite glyph + cCompIdTotal - the number of elements in prgnCompId + cCompId - the total number of Glyph IDs stored in prgnCompId + Return true if successful, false otherwise + False could indicate a non-composite glyph or the input array was not big enough ----------------------------------------------------------------------------------------------*/ bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId, - size_t cnCompIdTotal, size_t & cnCompId) + size_t cnCompIdTotal, size_t & cnCompId) { - using namespace Sfnt; - - if (GlyfContourCount(pSimpleGlyf) >= 0) - return false; - - const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); - // for a composite glyph, the special data begins here - const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]); - - uint16 GlyphFlags; - size_t iCurrentComp = 0; - do - { - GlyphFlags = be::swap(*((uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - prgnCompId[iCurrentComp++] = be::swap(*((uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - if (iCurrentComp >= cnCompIdTotal) - return false; - int nOffset = 0; - nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; - nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; - nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; - nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; - pbGlyph += nOffset; - } while (GlyphFlags & CompoundGlyph::MoreComponents); - - cnCompId = iCurrentComp; - - return true; + using namespace Sfnt; + + if (GlyfContourCount(pSimpleGlyf) >= 0) + return false; + + const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); + // for a composite glyph, the special data begins here + const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]); + + uint16 GlyphFlags; + size_t iCurrentComp = 0; + do + { + GlyphFlags = be::swap(*((uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + prgnCompId[iCurrentComp++] = be::swap(*((uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + if (iCurrentComp >= cnCompIdTotal) + return false; + int nOffset = 0; + nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; + nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; + nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; + nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; + pbGlyph += nOffset; + } while (GlyphFlags & CompoundGlyph::MoreComponents); + + cnCompId = iCurrentComp; + + return true; } /*---------------------------------------------------------------------------------------------- - Return info on how a component glyph is to be placed - pSimpleGlyph - assumed to point to a composite glyph - nCompId - glyph id for component of interest - bOffset - if true, a & b are the x & y offsets for this component - if false, b is the point on this component that is attaching to point a on the - preceding glyph - Return true if successful, false otherwise - False could indicate a non-composite glyph or that component wasn't found + Return info on how a component glyph is to be placed + pSimpleGlyph - assumed to point to a composite glyph + nCompId - glyph id for component of interest + bOffset - if true, a & b are the x & y offsets for this component + if false, b is the point on this component that is attaching to point a on the + preceding glyph + Return true if successful, false otherwise + False could indicate a non-composite glyph or that component wasn't found ----------------------------------------------------------------------------------------------*/ bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId, - bool fOffset, int & a, int & b) + bool fOffset, int & a, int & b) { - using namespace Sfnt; - - if (GlyfContourCount(pSimpleGlyf) >= 0) - return false; - - const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); - // for a composite glyph, the special data begins here - const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]); - - uint16 GlyphFlags; - do - { - GlyphFlags = be::swap(*((uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - if (be::swap(*((uint16 *)pbGlyph)) == nCompId) - { - pbGlyph += sizeof(uint16); // skip over glyph id of component - fOffset = (GlyphFlags & CompoundGlyph::ArgsAreXYValues) == CompoundGlyph::ArgsAreXYValues; - - if (GlyphFlags & CompoundGlyph::Arg1Arg2Words ) - { - a = be::swap(*(int16 *)pbGlyph); - pbGlyph += sizeof(int16); - b = be::swap(*(int16 *)pbGlyph); - pbGlyph += sizeof(int16); - } - else - { // args are signed bytes - a = *pbGlyph++; - b = *pbGlyph++; - } - return true; - } - pbGlyph += sizeof(uint16); // skip over glyph id of component - int nOffset = 0; - nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; - nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; - nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; - nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; - pbGlyph += nOffset; - } while (GlyphFlags & CompoundGlyph::MoreComponents); - - // didn't find requested component - fOffset = true; - a = 0; - b = 0; - return false; + using namespace Sfnt; + + if (GlyfContourCount(pSimpleGlyf) >= 0) + return false; + + const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); + // for a composite glyph, the special data begins here + const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]); + + uint16 GlyphFlags; + do + { + GlyphFlags = be::swap(*((uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + if (be::swap(*((uint16 *)pbGlyph)) == nCompId) + { + pbGlyph += sizeof(uint16); // skip over glyph id of component + fOffset = (GlyphFlags & CompoundGlyph::ArgsAreXYValues) == CompoundGlyph::ArgsAreXYValues; + + if (GlyphFlags & CompoundGlyph::Arg1Arg2Words ) + { + a = be::swap(*(int16 *)pbGlyph); + pbGlyph += sizeof(int16); + b = be::swap(*(int16 *)pbGlyph); + pbGlyph += sizeof(int16); + } + else + { // args are signed bytes + a = *pbGlyph++; + b = *pbGlyph++; + } + return true; + } + pbGlyph += sizeof(uint16); // skip over glyph id of component + int nOffset = 0; + nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; + nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; + nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; + nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; + pbGlyph += nOffset; + } while (GlyphFlags & CompoundGlyph::MoreComponents); + + // didn't find requested component + fOffset = true; + a = 0; + b = 0; + return false; } /*---------------------------------------------------------------------------------------------- - Return info on how a component glyph is to be transformed - pSimpleGlyph - assumed to point to a composite glyph - nCompId - glyph id for component of interest - flt11, flt11, flt11, flt11 - a 2x2 matrix giving the transform - bTransOffset - whether to transform the offset from above method - The spec is unclear about the meaning of this flag - Currently - initialize to true for MS rasterizer and false for Mac rasterizer, then - on return it will indicate whether transform should apply to offset (MSDN CD 10/99) - Return true if successful, false otherwise - False could indicate a non-composite glyph or that component wasn't found + Return info on how a component glyph is to be transformed + pSimpleGlyph - assumed to point to a composite glyph + nCompId - glyph id for component of interest + flt11, flt11, flt11, flt11 - a 2x2 matrix giving the transform + bTransOffset - whether to transform the offset from above method + The spec is unclear about the meaning of this flag + Currently - initialize to true for MS rasterizer and false for Mac rasterizer, then + on return it will indicate whether transform should apply to offset (MSDN CD 10/99) + Return true if successful, false otherwise + False could indicate a non-composite glyph or that component wasn't found ----------------------------------------------------------------------------------------------*/ bool GetComponentTransform(const void * pSimpleGlyf, int nCompId, - float & flt11, float & flt12, float & flt21, float & flt22, - bool & fTransOffset) + float & flt11, float & flt12, float & flt21, float & flt22, + bool & fTransOffset) { - using namespace Sfnt; - - if (GlyfContourCount(pSimpleGlyf) >= 0) - return false; - - const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); - // for a composite glyph, the special data begins here - const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]); - - uint16 GlyphFlags; - do - { - GlyphFlags = be::swap(*((uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - if (be::swap(*((uint16 *)pbGlyph)) == nCompId) - { - pbGlyph += sizeof(uint16); // skip over glyph id of component - pbGlyph += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; // skip over placement data - - if (fTransOffset) // MS rasterizer - fTransOffset = !(GlyphFlags & CompoundGlyph::UnscaledOffset); - else // Apple rasterizer - fTransOffset = (GlyphFlags & CompoundGlyph::ScaledOffset) != 0; - - if (GlyphFlags & CompoundGlyph::HaveScale) - { - flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - flt12 = 0; - flt21 = 0; - flt22 = flt11; - } - else if (GlyphFlags & CompoundGlyph::HaveXAndYScale) - { - flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - flt12 = 0; - flt21 = 0; - flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - } - else if (GlyphFlags & CompoundGlyph::HaveTwoByTwo) - { - flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - flt12 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - flt21 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); - pbGlyph += sizeof(uint16); - } - else - { // identity transform - flt11 = 1.0; - flt12 = 0.0; - flt21 = 0.0; - flt22 = 1.0; - } - return true; - } - pbGlyph += sizeof(uint16); // skip over glyph id of component - int nOffset = 0; - nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; - nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; - nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; - nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; - pbGlyph += nOffset; - } while (GlyphFlags & CompoundGlyph::MoreComponents); - - // didn't find requested component - fTransOffset = false; - flt11 = 1; - flt12 = 0; - flt21 = 0; - flt22 = 1; - return false; + using namespace Sfnt; + + if (GlyfContourCount(pSimpleGlyf) >= 0) + return false; + + const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf); + // for a composite glyph, the special data begins here + const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]); + + uint16 GlyphFlags; + do + { + GlyphFlags = be::swap(*((uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + if (be::swap(*((uint16 *)pbGlyph)) == nCompId) + { + pbGlyph += sizeof(uint16); // skip over glyph id of component + pbGlyph += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; // skip over placement data + + if (fTransOffset) // MS rasterizer + fTransOffset = !(GlyphFlags & CompoundGlyph::UnscaledOffset); + else // Apple rasterizer + fTransOffset = (GlyphFlags & CompoundGlyph::ScaledOffset) != 0; + + if (GlyphFlags & CompoundGlyph::HaveScale) + { + flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + flt12 = 0; + flt21 = 0; + flt22 = flt11; + } + else if (GlyphFlags & CompoundGlyph::HaveXAndYScale) + { + flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + flt12 = 0; + flt21 = 0; + flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + } + else if (GlyphFlags & CompoundGlyph::HaveTwoByTwo) + { + flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + flt12 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + flt21 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); + pbGlyph += sizeof(uint16); + } + else + { // identity transform + flt11 = 1.0; + flt12 = 0.0; + flt21 = 0.0; + flt22 = 1.0; + } + return true; + } + pbGlyph += sizeof(uint16); // skip over glyph id of component + int nOffset = 0; + nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; + nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; + nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; + nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; + pbGlyph += nOffset; + } while (GlyphFlags & CompoundGlyph::MoreComponents); + + // didn't find requested component + fTransOffset = false; + flt11 = 1; + flt12 = 0; + flt21 = 0; + flt22 = 1; + return false; } #endif /*---------------------------------------------------------------------------------------------- - Return a pointer into the glyf table based on the given tables and Glyph ID - Since this method doesn't check for spaces, it is good to call IsSpace before using it. - Return NULL on error. + Return a pointer into the glyf table based on the given tables and Glyph ID + Since this method doesn't check for spaces, it is good to call IsSpace before using it. + Return NULL on error. ----------------------------------------------------------------------------------------------*/ void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead) + size_t lGlyfSize, size_t lLocaSize, const void * pHead) { - // test for valid glyph id - // CheckTable verifies the index_to_loc_format is valid - - const Sfnt::FontHeader * pTable - = reinterpret_cast<const Sfnt::FontHeader *>(pHead); - - if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat) - { // loca entries are two bytes (and have been divided by two) - if (nGlyphId >= (lLocaSize >> 1) - 1) // don't allow nGlyphId to access sentinel - { -// throw std::out_of_range("glyph id out of range for font"); + // test for valid glyph id + // CheckTable verifies the index_to_loc_format is valid + + const Sfnt::FontHeader * pTable + = reinterpret_cast<const Sfnt::FontHeader *>(pHead); + + if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat) + { // loca entries are two bytes (and have been divided by two) + if (nGlyphId >= (lLocaSize >> 1) - 1) // don't allow nGlyphId to access sentinel + { +// throw std::out_of_range("glyph id out of range for font"); return NULL; - } - } - if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat) - { // loca entries are four bytes - if (nGlyphId >= (lLocaSize >> 2) - 1) - { -// throw std::out_of_range("glyph id out of range for font"); + } + } + if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat) + { // loca entries are four bytes + if (nGlyphId >= (lLocaSize >> 2) - 1) + { +// throw std::out_of_range("glyph id out of range for font"); return NULL; - } - } + } + } - long lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead); - void * pSimpleGlyf = GlyfLookup(pGlyf, lGlyfOffset, lGlyfSize); // invalid loca offset returns null - return pSimpleGlyf; + long lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead); + void * pSimpleGlyf = GlyfLookup(pGlyf, lGlyfOffset, lGlyfSize); // invalid loca offset returns null + return pSimpleGlyf; } #ifdef ALL_TTFUTILS /*---------------------------------------------------------------------------------------------- - Determine if a particular Glyph ID has any data in the glyf table. If it is white space, - there will be no glyf data, though there will be metric data in hmtx, etc. + Determine if a particular Glyph ID has any data in the glyf table. If it is white space, + there will be no glyf data, though there will be metric data in hmtx, etc. ----------------------------------------------------------------------------------------------*/ bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead) { - size_t lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead); - - // the +1 should always work because there is a sentinel value at the end of the loca table - size_t lNextGlyfOffset = LocaLookup(nGlyphId + 1, pLoca, lLocaSize, pHead); + size_t lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead); + + // the +1 should always work because there is a sentinel value at the end of the loca table + size_t lNextGlyfOffset = LocaLookup(nGlyphId + 1, pLoca, lLocaSize, pHead); - return (lNextGlyfOffset - lGlyfOffset) == 0; + return (lNextGlyfOffset - lGlyfOffset) == 0; } /*---------------------------------------------------------------------------------------------- - Determine if a particular Glyph ID is a multi-level composite. + Determine if a particular Glyph ID is a multi-level composite. ----------------------------------------------------------------------------------------------*/ bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, long lLocaSize, const void * pHead) + size_t lGlyfSize, long lLocaSize, const void * pHead) { - if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} + if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} - void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == NULL) - return false; // no way to really indicate an error occured here + void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == NULL) + return false; // no way to really indicate an error occured here - if (GlyfContourCount(pSimpleGlyf) >= 0) - return false; + if (GlyfContourCount(pSimpleGlyf) >= 0) + return false; - int rgnCompId[kMaxGlyphComponents]; // assumes only a limited number of glyph components - size_t cCompIdTotal = kMaxGlyphComponents; - size_t cCompId = 0; + int rgnCompId[kMaxGlyphComponents]; // assumes only a limited number of glyph components + size_t cCompIdTotal = kMaxGlyphComponents; + size_t cCompId = 0; - if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) - return false; + if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) + return false; - for (size_t i = 0; i < cCompId; i++) - { - pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), - pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == NULL) {return false;} + for (size_t i = 0; i < cCompId; i++) + { + pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), + pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == NULL) {return false;} - if (GlyfContourCount(pSimpleGlyf) < 0) - return true; - } + if (GlyfContourCount(pSimpleGlyf) < 0) + return true; + } - return false; + return false; } /*---------------------------------------------------------------------------------------------- - Get the bounding box coordinates based on the given tables and Glyph ID - Handles both simple and composite glyphs. - Return true if successful, false otherwise. On false, all point values will be INT_MIN - False may indicate a white space glyph + Get the bounding box coordinates based on the given tables and Glyph ID + Handles both simple and composite glyphs. + Return true if successful, false otherwise. On false, all point values will be INT_MIN + False may indicate a white space glyph ----------------------------------------------------------------------------------------------*/ bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax) + size_t lGlyfSize, size_t lLocaSize, const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax) { - xMin = yMin = xMax = yMax = INT_MIN; + xMin = yMin = xMax = yMax = INT_MIN; - if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} + if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} - void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == NULL) {return false;} + void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == NULL) {return false;} - return GlyfBox(pSimpleGlyf, xMin, yMin, xMax, yMax); + return GlyfBox(pSimpleGlyf, xMin, yMin, xMax, yMax); } /*---------------------------------------------------------------------------------------------- - Get the number of contours based on the given tables and Glyph ID - Handles both simple and composite glyphs. - Return true if successful, false otherwise. On false, cnContours will be INT_MIN - False may indicate a white space glyph or a multi-level composite glyph. + Get the number of contours based on the given tables and Glyph ID + Handles both simple and composite glyphs. + Return true if successful, false otherwise. On false, cnContours will be INT_MIN + False may indicate a white space glyph or a multi-level composite glyph. ----------------------------------------------------------------------------------------------*/ bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead, size_t & cnContours) + size_t lGlyfSize, size_t lLocaSize, const void * pHead, size_t & cnContours) { - cnContours = static_cast<size_t>(INT_MIN); - - if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} - - void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == NULL) {return false;} - - int cRtnContours = GlyfContourCount(pSimpleGlyf); - if (cRtnContours >= 0) - { - cnContours = size_t(cRtnContours); - return true; - } - - //handle composite glyphs - - int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components - size_t cCompIdTotal = kMaxGlyphComponents; - size_t cCompId = 0; - - if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) - return false; - - cRtnContours = 0; - int cTmp = 0; - for (size_t i = 0; i < cCompId; i++) - { - if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} - pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), - pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == 0) {return false;} - // return false on multi-level composite - if ((cTmp = GlyfContourCount(pSimpleGlyf)) < 0) - return false; - cRtnContours += cTmp; - } - - cnContours = size_t(cRtnContours); - return true; + cnContours = static_cast<size_t>(INT_MIN); + + if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} + + void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == NULL) {return false;} + + int cRtnContours = GlyfContourCount(pSimpleGlyf); + if (cRtnContours >= 0) + { + cnContours = size_t(cRtnContours); + return true; + } + + //handle composite glyphs + + int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components + size_t cCompIdTotal = kMaxGlyphComponents; + size_t cCompId = 0; + + if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) + return false; + + cRtnContours = 0; + int cTmp = 0; + for (size_t i = 0; i < cCompId; i++) + { + if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} + pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), + pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == 0) {return false;} + // return false on multi-level composite + if ((cTmp = GlyfContourCount(pSimpleGlyf)) < 0) + return false; + cRtnContours += cTmp; + } + + cnContours = size_t(cRtnContours); + return true; } /*---------------------------------------------------------------------------------------------- - Get the point numbers for the end points of the glyph contours based on the given tables - and Glyph ID - Handles both simple and composite glyphs. - cnPoints - count of contours from GlyfContourCount (same as number of end points) - prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers - Return true if successful, false otherwise. On false, all end points are INT_MIN - False may indicate a white space glyph or a multi-level composite glyph. + Get the point numbers for the end points of the glyph contours based on the given tables + and Glyph ID + Handles both simple and composite glyphs. + cnPoints - count of contours from GlyfContourCount (same as number of end points) + prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers + Return true if successful, false otherwise. On false, all end points are INT_MIN + False may indicate a white space glyph or a multi-level composite glyph. ----------------------------------------------------------------------------------------------*/ bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead, - int * prgnContourEndPoint, size_t cnPoints) + size_t lGlyfSize, size_t lLocaSize, const void * pHead, + int * prgnContourEndPoint, size_t cnPoints) { memset(prgnContourEndPoint, 0xFF, cnPoints * sizeof(int)); - // std::fill_n(prgnContourEndPoint, cnPoints, INT_MIN); - - if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} - - void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == NULL) {return false;} - - int cContours = GlyfContourCount(pSimpleGlyf); - int cActualPts = 0; - if (cContours > 0) - return GlyfContourEndPoints(pSimpleGlyf, prgnContourEndPoint, cnPoints, cActualPts); - - // handle composite glyphs - - int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components - size_t cCompIdTotal = kMaxGlyphComponents; - size_t cCompId = 0; - - if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) - return false; - - int * prgnCurrentEndPoint = prgnContourEndPoint; - int cCurrentPoints = cnPoints; - int nPrevPt = 0; - for (size_t i = 0; i < cCompId; i++) - { - if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} - pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == NULL) {return false;} - // returns false on multi-level composite - if (!GlyfContourEndPoints(pSimpleGlyf, prgnCurrentEndPoint, cCurrentPoints, cActualPts)) - return false; - // points in composite are numbered sequentially as components are added - // must adjust end point numbers for new point numbers - for (int j = 0; j < cActualPts; j++) - prgnCurrentEndPoint[j] += nPrevPt; - nPrevPt = prgnCurrentEndPoint[cActualPts - 1] + 1; - - prgnCurrentEndPoint += cActualPts; - cCurrentPoints -= cActualPts; - } - - return true; + // std::fill_n(prgnContourEndPoint, cnPoints, INT_MIN); + + if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} + + void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == NULL) {return false;} + + int cContours = GlyfContourCount(pSimpleGlyf); + int cActualPts = 0; + if (cContours > 0) + return GlyfContourEndPoints(pSimpleGlyf, prgnContourEndPoint, cnPoints, cActualPts); + + // handle composite glyphs + + int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components + size_t cCompIdTotal = kMaxGlyphComponents; + size_t cCompId = 0; + + if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) + return false; + + int * prgnCurrentEndPoint = prgnContourEndPoint; + int cCurrentPoints = cnPoints; + int nPrevPt = 0; + for (size_t i = 0; i < cCompId; i++) + { + if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} + pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == NULL) {return false;} + // returns false on multi-level composite + if (!GlyfContourEndPoints(pSimpleGlyf, prgnCurrentEndPoint, cCurrentPoints, cActualPts)) + return false; + // points in composite are numbered sequentially as components are added + // must adjust end point numbers for new point numbers + for (int j = 0; j < cActualPts; j++) + prgnCurrentEndPoint[j] += nPrevPt; + nPrevPt = prgnCurrentEndPoint[cActualPts - 1] + 1; + + prgnCurrentEndPoint += cActualPts; + cCurrentPoints -= cActualPts; + } + + return true; } /*---------------------------------------------------------------------------------------------- - Get the points for a glyph based on the given tables and Glyph ID - Handles both simple and composite glyphs. - cnPoints - count of points from largest end point obtained from GlyfContourEndPoints - prgnX & prgnY - should point to buffers large enough to hold cnPoints integers - The ranges are parallel so that coordinates for point(n) are found at offset n in - both ranges. These points are in absolute coordinates. - prgfOnCurve - should point to a buffer a large enough to hold cnPoints bytes (bool) - This range is parallel to the prgnX & prgnY - Return true if successful, false otherwise. On false, all points may be INT_MIN - False may indicate a white space glyph, a multi-level composite, or a corrupt font - // TODO: doesn't support composite glyphs whose components are themselves components - It's not clear from the TTF spec when the transforms should be applied. Should the - transform be done before or after attachment point calcs? (current code - before) - Should the transform be applied to other offsets? (currently - no; however commented - out code is in place so that if CompoundGlyph::UnscaledOffset on the MS rasterizer is - clear (typical) then yes, and if CompoundGlyph::ScaledOffset on the Apple rasterizer is - clear (typical?) then no). See GetComponentTransform. - It's also unclear where point numbering with attachment poinst starts - (currently - first point number is relative to whole glyph, second point number is - relative to current glyph). + Get the points for a glyph based on the given tables and Glyph ID + Handles both simple and composite glyphs. + cnPoints - count of points from largest end point obtained from GlyfContourEndPoints + prgnX & prgnY - should point to buffers large enough to hold cnPoints integers + The ranges are parallel so that coordinates for point(n) are found at offset n in + both ranges. These points are in absolute coordinates. + prgfOnCurve - should point to a buffer a large enough to hold cnPoints bytes (bool) + This range is parallel to the prgnX & prgnY + Return true if successful, false otherwise. On false, all points may be INT_MIN + False may indicate a white space glyph, a multi-level composite, or a corrupt font + It's not clear from the TTF spec when the transforms should be applied. Should the + transform be done before or after attachment point calcs? (current code - before) + Should the transform be applied to other offsets? (currently - no; however commented + out code is in place so that if CompoundGlyph::UnscaledOffset on the MS rasterizer is + clear (typical) then yes, and if CompoundGlyph::ScaledOffset on the Apple rasterizer is + clear (typical?) then no). See GetComponentTransform. + It's also unclear where point numbering with attachment poinst starts + (currently - first point number is relative to whole glyph, second point number is + relative to current glyph). ----------------------------------------------------------------------------------------------*/ bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, - const void * pLoca, size_t lGlyfSize, size_t lLocaSize, const void * pHead, - const int * /*prgnContourEndPoint*/, size_t /*cnEndPoints*/, - int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints) + const void * pLoca, size_t lGlyfSize, size_t lLocaSize, const void * pHead, + const int * /*prgnContourEndPoint*/, size_t /*cnEndPoints*/, + int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints) { memset(prgnX, 0x7F, cnPoints * sizeof(int)); memset(prgnY, 0x7F, cnPoints * sizeof(int)); - if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) - return false; - - void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pSimpleGlyf == NULL) - return false; - - int cContours = GlyfContourCount(pSimpleGlyf); - int cActualPts; - if (cContours > 0) - { - if (!GlyfPoints(pSimpleGlyf, prgnX, prgnY, (char *)prgfOnCurve, cnPoints, cActualPts)) - return false; - CalcAbsolutePoints(prgnX, prgnY, cnPoints); - SimplifyFlags((char *)prgfOnCurve, cnPoints); - return true; - } - - // handle composite glyphs - int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components - size_t cCompIdTotal = kMaxGlyphComponents; - size_t cCompId = 0; - - // this will fail if there are more components than there is room for - if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) - return false; - - int * prgnCurrentX = prgnX; - int * prgnCurrentY = prgnY; - char * prgbCurrentFlag = (char *)prgfOnCurve; // converting bool to char should be safe - int cCurrentPoints = cnPoints; - bool fOffset = true, fTransOff = true; - int a, b; - float flt11, flt12, flt21, flt22; - // int * prgnPrevX = prgnX; // in case first att pt number relative to preceding glyph - // int * prgnPrevY = prgnY; - for (size_t i = 0; i < cCompId; i++) - { - if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} - void * pCompGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); - if (pCompGlyf == NULL) {return false;} - // returns false on multi-level composite - if (!GlyfPoints(pCompGlyf, prgnCurrentX, prgnCurrentY, prgbCurrentFlag, - cCurrentPoints, cActualPts)) - return false; - if (!GetComponentPlacement(pSimpleGlyf, rgnCompId[i], fOffset, a, b)) - return false; - if (!GetComponentTransform(pSimpleGlyf, rgnCompId[i], - flt11, flt12, flt21, flt22, fTransOff)) - return false; - bool fIdTrans = flt11 == 1.0 && flt12 == 0.0 && flt21 == 0.0 && flt22 == 1.0; - - // convert points to absolute coordinates - // do before transform and attachment point placement are applied - CalcAbsolutePoints(prgnCurrentX, prgnCurrentY, cActualPts); - - // apply transform - see main method note above - // do before attachment point calcs - if (!fIdTrans) - for (int j = 0; j < cActualPts; j++) - { - int x = prgnCurrentX[j]; // store before transform applied - int y = prgnCurrentY[j]; - prgnCurrentX[j] = (int)(x * flt11 + y * flt12); - prgnCurrentY[j] = (int)(x * flt21 + y * flt22); - } - - // apply placement - see main method note above - int nXOff, nYOff; - if (fOffset) // explicit x & y offsets - { - /* ignore fTransOff for now - if (fTransOff && !fIdTrans) - { // transform x & y offsets - nXOff = (int)(a * flt11 + b * flt12); - nYOff = (int)(a * flt21 + b * flt22); - } - else */ - { // don't transform offset - nXOff = a; - nYOff = b; - } - } - else // attachment points - { // in case first point is relative to preceding glyph and second relative to current - // nXOff = prgnPrevX[a] - prgnCurrentX[b]; - // nYOff = prgnPrevY[a] - prgnCurrentY[b]; - // first point number relative to whole composite, second relative to current glyph - nXOff = prgnX[a] - prgnCurrentX[b]; - nYOff = prgnY[a] - prgnCurrentY[b]; - } - for (int j = 0; j < cActualPts; j++) - { - prgnCurrentX[j] += nXOff; - prgnCurrentY[j] += nYOff; - } - - // prgnPrevX = prgnCurrentX; - // prgnPrevY = prgnCurrentY; - prgnCurrentX += cActualPts; - prgnCurrentY += cActualPts; - prgbCurrentFlag += cActualPts; - cCurrentPoints -= cActualPts; - } - - SimplifyFlags((char *)prgfOnCurve, cnPoints); - - return true; + if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) + return false; + + void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pSimpleGlyf == NULL) + return false; + + int cContours = GlyfContourCount(pSimpleGlyf); + int cActualPts; + if (cContours > 0) + { + if (!GlyfPoints(pSimpleGlyf, prgnX, prgnY, (char *)prgfOnCurve, cnPoints, cActualPts)) + return false; + CalcAbsolutePoints(prgnX, prgnY, cnPoints); + SimplifyFlags((char *)prgfOnCurve, cnPoints); + return true; + } + + // handle composite glyphs + int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components + size_t cCompIdTotal = kMaxGlyphComponents; + size_t cCompId = 0; + + // this will fail if there are more components than there is room for + if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) + return false; + + int * prgnCurrentX = prgnX; + int * prgnCurrentY = prgnY; + char * prgbCurrentFlag = (char *)prgfOnCurve; // converting bool to char should be safe + int cCurrentPoints = cnPoints; + bool fOffset = true, fTransOff = true; + int a, b; + float flt11, flt12, flt21, flt22; + // int * prgnPrevX = prgnX; // in case first att pt number relative to preceding glyph + // int * prgnPrevY = prgnY; + for (size_t i = 0; i < cCompId; i++) + { + if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} + void * pCompGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); + if (pCompGlyf == NULL) {return false;} + // returns false on multi-level composite + if (!GlyfPoints(pCompGlyf, prgnCurrentX, prgnCurrentY, prgbCurrentFlag, + cCurrentPoints, cActualPts)) + return false; + if (!GetComponentPlacement(pSimpleGlyf, rgnCompId[i], fOffset, a, b)) + return false; + if (!GetComponentTransform(pSimpleGlyf, rgnCompId[i], + flt11, flt12, flt21, flt22, fTransOff)) + return false; + bool fIdTrans = flt11 == 1.0 && flt12 == 0.0 && flt21 == 0.0 && flt22 == 1.0; + + // convert points to absolute coordinates + // do before transform and attachment point placement are applied + CalcAbsolutePoints(prgnCurrentX, prgnCurrentY, cActualPts); + + // apply transform - see main method note above + // do before attachment point calcs + if (!fIdTrans) + for (int j = 0; j < cActualPts; j++) + { + int x = prgnCurrentX[j]; // store before transform applied + int y = prgnCurrentY[j]; + prgnCurrentX[j] = (int)(x * flt11 + y * flt12); + prgnCurrentY[j] = (int)(x * flt21 + y * flt22); + } + + // apply placement - see main method note above + int nXOff, nYOff; + if (fOffset) // explicit x & y offsets + { + /* ignore fTransOff for now + if (fTransOff && !fIdTrans) + { // transform x & y offsets + nXOff = (int)(a * flt11 + b * flt12); + nYOff = (int)(a * flt21 + b * flt22); + } + else */ + { // don't transform offset + nXOff = a; + nYOff = b; + } + } + else // attachment points + { // in case first point is relative to preceding glyph and second relative to current + // nXOff = prgnPrevX[a] - prgnCurrentX[b]; + // nYOff = prgnPrevY[a] - prgnCurrentY[b]; + // first point number relative to whole composite, second relative to current glyph + nXOff = prgnX[a] - prgnCurrentX[b]; + nYOff = prgnY[a] - prgnCurrentY[b]; + } + for (int j = 0; j < cActualPts; j++) + { + prgnCurrentX[j] += nXOff; + prgnCurrentY[j] += nYOff; + } + + // prgnPrevX = prgnCurrentX; + // prgnPrevY = prgnCurrentY; + prgnCurrentX += cActualPts; + prgnCurrentY += cActualPts; + prgbCurrentFlag += cActualPts; + cCurrentPoints -= cActualPts; + } + + SimplifyFlags((char *)prgfOnCurve, cnPoints); + + return true; } /*---------------------------------------------------------------------------------------------- - Simplify the meaning of flags to just indicate whether point is on-curve or off-curve. + Simplify the meaning of flags to just indicate whether point is on-curve or off-curve. ---------------------------------------------------------------------------------------------*/ bool SimplifyFlags(char * prgbFlags, int cnPoints) { - for (int i = 0; i < cnPoints; i++) - prgbFlags[i] = static_cast<char>(prgbFlags[i] & Sfnt::SimpleGlyph::OnCurve); - return true; + for (int i = 0; i < cnPoints; i++) + prgbFlags[i] = static_cast<char>(prgbFlags[i] & Sfnt::SimpleGlyph::OnCurve); + return true; } /*---------------------------------------------------------------------------------------------- - Convert relative point coordinates to absolute coordinates - Points are stored in the font such that they are offsets from one another except for the - first point of a glyph. + Convert relative point coordinates to absolute coordinates + Points are stored in the font such that they are offsets from one another except for the + first point of a glyph. ---------------------------------------------------------------------------------------------*/ bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints) { - int nX = prgnX[0]; - int nY = prgnY[0]; - for (int i = 1; i < cnPoints; i++) - { - prgnX[i] += nX; - nX = prgnX[i]; - prgnY[i] += nY; - nY = prgnY[i]; - } - - return true; + int nX = prgnX[0]; + int nY = prgnY[0]; + for (int i = 1; i < cnPoints; i++) + { + prgnX[i] += nX; + nX = prgnX[i]; + prgnY[i] += nY; + nY = prgnY[i]; + } + + return true; } #endif /*---------------------------------------------------------------------------------------------- - Return the length of the 'name' table in bytes. - Currently used. + Return the length of the 'name' table in bytes. + Currently used. ---------------------------------------------------------------------------------------------*/ #if 0 size_t NameTableLength(const byte * pTable) { - byte * pb = (const_cast<byte *>(pTable)) + 2; // skip format - size_t cRecords = *pb++ << 8; cRecords += *pb++; - int dbStringOffset0 = (*pb++) << 8; dbStringOffset0 += *pb++; - int dbMaxStringOffset = 0; - for (size_t irec = 0; irec < cRecords; irec++) - { - int nPlatform = (*pb++) << 8; nPlatform += *pb++; - int nEncoding = (*pb++) << 8; nEncoding += *pb++; - int nLanguage = (*pb++) << 8; nLanguage += *pb++; - int nName = (*pb++) << 8; nName += *pb++; - int cbStringLen = (*pb++) << 8; cbStringLen += *pb++; - int dbStringOffset = (*pb++) << 8; dbStringOffset += *pb++; - if (dbMaxStringOffset < dbStringOffset + cbStringLen) - dbMaxStringOffset = dbStringOffset + cbStringLen; - } - return dbStringOffset0 + dbMaxStringOffset; + byte * pb = (const_cast<byte *>(pTable)) + 2; // skip format + size_t cRecords = *pb++ << 8; cRecords += *pb++; + int dbStringOffset0 = (*pb++) << 8; dbStringOffset0 += *pb++; + int dbMaxStringOffset = 0; + for (size_t irec = 0; irec < cRecords; irec++) + { + int nPlatform = (*pb++) << 8; nPlatform += *pb++; + int nEncoding = (*pb++) << 8; nEncoding += *pb++; + int nLanguage = (*pb++) << 8; nLanguage += *pb++; + int nName = (*pb++) << 8; nName += *pb++; + int cbStringLen = (*pb++) << 8; cbStringLen += *pb++; + int dbStringOffset = (*pb++) << 8; dbStringOffset += *pb++; + if (dbMaxStringOffset < dbStringOffset + cbStringLen) + dbMaxStringOffset = dbStringOffset + cbStringLen; + } + return dbStringOffset0 + dbMaxStringOffset; } #endif diff --git a/gfx/graphite2/src/UtfCodec.cpp b/gfx/graphite2/src/UtfCodec.cpp index ca3c0c0a7..d1db5fa11 100644 --- a/gfx/graphite2/src/UtfCodec.cpp +++ b/gfx/graphite2/src/UtfCodec.cpp @@ -35,11 +35,11 @@ using namespace graphite2; const int8 _utf_codec<8>::sz_lut[16] = { - 1,1,1,1,1,1,1,1, // 1 byte - 0,0,0,0, // trailing byte - 2,2, // 2 bytes - 3, // 3 bytes - 4 // 4 bytes + 1,1,1,1,1,1,1,1, // 1 byte + 0,0,0,0, // trailing byte + 2,2, // 2 bytes + 3, // 3 bytes + 4 // 4 bytes }; const byte _utf_codec<8>::mask_lut[5] = {0x7f, 0xff, 0x3f, 0x1f, 0x0f}; diff --git a/gfx/graphite2/src/XmlTraceLog.cpp b/gfx/graphite2/src/XmlTraceLog.cpp deleted file mode 100644 index f34d15018..000000000 --- a/gfx/graphite2/src/XmlTraceLog.cpp +++ /dev/null @@ -1,219 +0,0 @@ -/* GRAPHITE2 LICENSING - - Copyright 2010, SIL International - All rights reserved. - - This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2.1 of License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should also have received a copy of the GNU Lesser General Public - License along with this library in the file named "LICENSE". - If not, write to the Free Software Foundation, 51 Franklin Street, - Suite 500, Boston, MA 02110-1335, USA or visit their web page on the - internet at http://www.fsf.org/licenses/lgpl.html. - -Alternatively, the contents of this file may be used under the terms of the -Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public -License, as published by the Free Software Foundation, either version 2 -of the License or (at your option) any later version. -*/ - -#include <cstring> -#include <cstdarg> -#include "Main.h" -#include "XmlTraceLog.h" - - -using namespace graphite2; - -#ifndef DISABLE_TRACING - -/*static*/ XmlTraceLog XmlTraceLog::sm_NullLog(NULL, NULL, GRLOG_NONE); -XmlTraceLog * XmlTraceLog::sLog = &sm_NullLog; - -XmlTraceLog::XmlTraceLog(FILE * file, const char * ns, GrLogMask logMask) - : m_file(file), m_depth(0), m_mask(logMask) -{ - if (!m_file) return; - int deep = 0; -#ifdef ENABLE_DEEP_TRACING - deep = 1; -#endif - fprintf(m_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%s xmlns=\"%s\" mask=\"%x\" deep=\"%d\">", - xmlTraceLogElements[ElementTopLevel].mName, ns, logMask, deep); - m_elementStack[m_depth++] = ElementTopLevel; - m_elementEmpty = true; - m_inElement = false; - m_lastNodeText = false; -} - -XmlTraceLog::~XmlTraceLog() -{ - if (m_file && m_file != stdout && m_file != stderr) - { - assert(m_depth == 1); - while (m_depth > 0) - { - closeElement(m_elementStack[m_depth-1]); - } - fclose(m_file); - m_file = NULL; - } -} - -void XmlTraceLog::addSingleElement(XmlTraceLogElement eId, const int value) -{ - if (!m_file) return; - if (m_inElement) - { - if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) - fprintf(m_file, ">"); - } - if (xmlTraceLogElements[eId].mFlags & m_mask) - { - if (!m_lastNodeText) - { - fprintf(m_file, "\n"); - for (size_t i = 0; i < m_depth; i++) - { - fprintf(m_file, " "); - } - } - fprintf(m_file, "<%s val=\"%d\"/>", xmlTraceLogElements[eId].mName, value); - } - m_inElement = false; - m_lastNodeText = false; -} - -void XmlTraceLog::writeElementArray(XmlTraceLogElement eId, XmlTraceLogAttribute aId, int16 values [], size_t length) -{ - if (!m_file) return; - if (xmlTraceLogElements[eId].mFlags & m_mask) - { - if(m_inElement && xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) - { - fprintf(m_file, ">"); - m_inElement = false; - } - // line break after 5 columns - for (size_t i = 0; i < length; i++) - { - if (i % 5 == 0) - { - fprintf(m_file, "\n"); - for (size_t j = 0; j < m_depth; j++) - { - fprintf(m_file, " "); - } - } - fprintf(m_file, "<%s index=\"%d\" %s=\"%d\"/>", xmlTraceLogElements[eId].mName, int(i), - xmlTraceLogAttributes[aId], (int)values[i]); - } - } -} - -void XmlTraceLog::writeText(const char * utf8) -{ - if (!m_file) return; - if (m_inElement) - { - if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) - { - fprintf(m_file, ">"); - } - m_inElement = false; - } - if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) - { - escapeIfNeeded(utf8); - } - m_lastNodeText = true; -} - -void XmlTraceLog::writeUnicode(const uint32 code) -{ - if (!m_file) return; - if (m_inElement) - { - if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) - { - fprintf(m_file, ">"); - } - m_inElement = false; - } - if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) - { - fprintf(m_file, "&#x%02x;", code); - } - m_lastNodeText = true; -} - -void XmlTraceLog::escapeIfNeeded(const char * data) -{ - size_t length = strlen(data); - for (size_t i = 0; i < length; i++) - { - switch (data[i]) - { - case '<': - fprintf(m_file, "<"); - break; - case '>': - fprintf(m_file, ">"); - break; - case '&': - fprintf(m_file, "&"); - break; - case '"': - fprintf(m_file, """); - break; - default: - fprintf(m_file, "%c", data[i]); - } - } -} - -static const int MAX_MSG_LEN = 1024; - -void XmlTraceLog::error(const char * msg, ...) -{ - if (!m_file) return; - openElement(ElementError); - va_list args; - va_start(args, msg); - char buffer[MAX_MSG_LEN]; -#ifndef NDEBUG - int len = -#endif - vsnprintf(buffer, MAX_MSG_LEN, msg, args); - assert(len + 1 < MAX_MSG_LEN); - writeText(buffer); - va_end(args); - closeElement(ElementError); -} - -void XmlTraceLog::warning(const char * msg, ...) -{ - if (!m_file) return; - openElement(ElementWarning); - va_list args; - va_start(args, msg); - char buffer[MAX_MSG_LEN]; -#ifndef NDEBUG - int len = -#endif - vsnprintf(buffer, MAX_MSG_LEN, msg, args); - assert(len + 1 < MAX_MSG_LEN); - writeText(buffer); - va_end(args); - closeElement(ElementWarning); -} - -#endif //!DISABLE_TRACING diff --git a/gfx/graphite2/src/XmlTraceLogTags.cpp b/gfx/graphite2/src/XmlTraceLogTags.cpp deleted file mode 100644 index b6ec970ac..000000000 --- a/gfx/graphite2/src/XmlTraceLogTags.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/* GRAPHITE2 LICENSING - - Copyright 2010, SIL International - All rights reserved. - - This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2.1 of License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should also have received a copy of the GNU Lesser General Public - License along with this library in the file named "LICENSE". - If not, write to the Free Software Foundation, 51 Franklin Street, - Suite 500, Boston, MA 02110-1335, USA or visit their web page on the - internet at http://www.fsf.org/licenses/lgpl.html. - -Alternatively, the contents of this file may be used under the terms of the -Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public -License, as published by the Free Software Foundation, either version 2 -of the License or (at your option) any later version. -*/ -#include "Main.h" -#include "XmlTraceLogTags.h" - - -using namespace graphite2; - -#ifndef DISABLE_TRACING - - -// start this at same line number as in XmlTraceLogTags.h -const XmlTraceLogTag graphite2::xmlTraceLogElements[NumElements] = { - XmlTraceLogTag("Graphite2Log", GRLOG_ALL), - XmlTraceLogTag("Face", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("Glyphs", GRLOG_FACE), - XmlTraceLogTag("GlyphFace", GRLOG_FACE), - XmlTraceLogTag("Attr", GRLOG_FACE), - XmlTraceLogTag("Silf", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("SilfSub", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("Pass", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("Pseudo", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("ClassMap", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("LookupClass", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("Lookup", GRLOG_FACE | GRLOG_PASS), - XmlTraceLogTag("Range", GRLOG_PASS), - XmlTraceLogTag("RuleMap", GRLOG_PASS), - XmlTraceLogTag("Rule", GRLOG_PASS), - XmlTraceLogTag("StartState", GRLOG_PASS), - XmlTraceLogTag("StateTransitions", GRLOG_PASS), - XmlTraceLogTag("TR", GRLOG_PASS), - XmlTraceLogTag("TD", GRLOG_PASS), - XmlTraceLogTag("Constraint", GRLOG_PASS), - XmlTraceLogTag("Constraints", GRLOG_PASS), - XmlTraceLogTag("Actions", GRLOG_PASS), - XmlTraceLogTag("Action", GRLOG_PASS), - XmlTraceLogTag("Features", GRLOG_PASS), - XmlTraceLogTag("Feature", GRLOG_PASS), - XmlTraceLogTag("FeatureSetting", GRLOG_PASS), - XmlTraceLogTag("Segment", GRLOG_SEGMENT), - XmlTraceLogTag("Slot", GRLOG_SEGMENT), - XmlTraceLogTag("Text", GRLOG_SEGMENT), - XmlTraceLogTag("OpCode", GRLOG_OPCODE), - XmlTraceLogTag("TestRule", GRLOG_OPCODE), - XmlTraceLogTag("DoRule", GRLOG_OPCODE), - XmlTraceLogTag("RunPass", GRLOG_OPCODE), - XmlTraceLogTag("Params", GRLOG_OPCODE), - XmlTraceLogTag("Push", GRLOG_OPCODE), - XmlTraceLogTag("SubSeg", GRLOG_SEGMENT), - XmlTraceLogTag("SegCache", GRLOG_CACHE), - XmlTraceLogTag("SegCacheEntry", GRLOG_CACHE), - XmlTraceLogTag("Glyph", GRLOG_CACHE), - XmlTraceLogTag("PassResult", GRLOG_OPCODE), - - XmlTraceLogTag("Error", GRLOG_ALL), - XmlTraceLogTag("Warning", GRLOG_ALL) - // Nothing corresponds to NumElements -}; - - - -// start this at same line number as in XmlTraceLogTags.h -const char * graphite2::xmlTraceLogAttributes[NumAttributes] = { - "index", - "version", - "major", - "minor", - "num", - "glyphId", - "advance", - "advanceX", - "advanceY", - "attrId", - "attrVal", - "compilerMajor", - "compilerMinor", - "numPasses",//AttrNumPasses, - "subPass",//AttrSubPass, - "posPass",//AttrPosPass, - "justPass",//AttrJustPass, - "bidiPass",//AttrBidiPass, - "preContext",//AttrPreContext, - "postContext",//AttrPostContext, - "pseudoGlyph",//AttrPseudoGlyph, - "breakWeight",//AttrBreakWeight, - "directionality",//AttrDirectionality, - "numJustLevels",//AttrNumJustLevels, - "numLigCompAttr",//AttrLigComp, - "numUserDefinedAttr",//AttrUserDefn, - "maxNumLigComp",//AttrNumLigComp, - "numCriticalFeatures",//AttrNumCritFeatures, - "numScripts",//AttrNumScripts - "lineBreakglyph",//,AttrLBGlyph, - "numPseudo", - "numClasses", - "numLinear", - "passId",//AttrPassId, - "flags",//AttrFlags, - "maxRuleLoop",//AttrMaxRuleLoop, - "maxRuleContext",//AttrMaxRuleContext, - "maxBackup",//AttrMaxBackup, - "numRules",//AttrNumRules, - "numRows",//AttrNumRows, - "numTransitionStates",//AttrNumTransition, - "numSuccessStates",//AttrNumSuccess, - "numColumns",//AttrNumColumns, - "numRanges",//AttrNumRanges, - "minPrecontext",//AttrMinPrecontext, - "maxPrecontext",//AttrMaxPrecontext, - "firstId", - "lastId", - "colId", - "successId", - "ruleId", - "contextLength", - "state", - "value", - "sortKey", - "precontext", - "action", - "actionCode", - "arg1", - "arg2", - "arg3", - "arg4", - "arg5", - "arg6", - "arg7", - "arg8", - "label", - "length", - "x", - "y", - "before", - "after", - "encoding", - "name", - "result", - "default", - "accessCount", - "lastAccess", - "misses" -}; - -#endif diff --git a/gfx/graphite2/src/call_machine.cpp b/gfx/graphite2/src/call_machine.cpp index f06f43cbd..e1d0ce5d2 100644 --- a/gfx/graphite2/src/call_machine.cpp +++ b/gfx/graphite2/src/call_machine.cpp @@ -50,7 +50,7 @@ of the License or (at your option) any later version. vm::Machine::stack_t * const sb, regbank & reg // These are required by opcodes.h and should not be changed -#define STARTOP(name) bool name(registers) REGPARM(4);\ +#define STARTOP(name) bool name(registers) REGPARM(4);\ bool name(registers) { #define ENDOP return (sp - sb)/Machine::STACK_MAX==0; \ } @@ -70,6 +70,7 @@ struct regbank { SlotMap & smap; slotref * const map_base; const instr * & ip; + uint8 direction; int8 flags; }; @@ -86,6 +87,7 @@ namespace { #define map reg.map #define mapb reg.map_base #define flags reg.flags +#define dir reg.direction #include "inc/opcodes.h" @@ -96,6 +98,7 @@ namespace { #undef map #undef mapb #undef flags +#undef dir } Machine::stack_t Machine::run(const instr * program, @@ -110,7 +113,7 @@ Machine::stack_t Machine::run(const instr * program, const byte * dp = data; stack_t * sp = _stack + Machine::STACK_GUARD, * const sb = sp; - regbank reg = {*map, map, _map, _map.begin()+_map.context(), ip, 0}; + regbank reg = {*map, map, _map, _map.begin()+_map.context(), ip, _map.dir(), 0}; // Run the program while ((reinterpret_cast<ip_t>(*++ip))(dp, sp, sb, reg)) {} diff --git a/gfx/graphite2/src/direct_machine.cpp b/gfx/graphite2/src/direct_machine.cpp index 09709c303..b6b9ee042 100644 --- a/gfx/graphite2/src/direct_machine.cpp +++ b/gfx/graphite2/src/direct_machine.cpp @@ -61,6 +61,7 @@ const void * direct_run(const bool get_table_mode, const byte * data, Machine::stack_t * stack, slotref * & __map, + uint8 _dir, SlotMap * __smap=0) { // We need to define and return to opcode table from within this function @@ -79,6 +80,7 @@ const void * direct_run(const bool get_table_mode, slotref is = *__map, * map = __map, * const mapb = smap.begin()+smap.context(); + uint8 dir = _dir; int8 flags = 0; // start the program @@ -109,7 +111,7 @@ Machine::stack_t Machine::run(const instr * program, assert(program != 0); const stack_t *sp = static_cast<const stack_t *>( - direct_run(false, program, data, _stack, is, &_map)); + direct_run(false, program, data, _stack, is, _map.dir(), &_map)); const stack_t ret = sp == _stack+STACK_GUARD+1 ? *sp-- : 0; check_final_stack(sp); return ret; diff --git a/gfx/graphite2/src/files.mk b/gfx/graphite2/src/files.mk index 358acef62..fde83761c 100644 --- a/gfx/graphite2/src/files.mk +++ b/gfx/graphite2/src/files.mk @@ -1,6 +1,6 @@ # GRAPHITE2 LICENSING # -# Copyright 2010, SIL International +# Copyright 2011, SIL International # All rights reserved. # # This library is free software; you can redistribute it and/or modify @@ -43,23 +43,26 @@ $(_NS)_SOURCES = \ $($(_NS)_BASE)/src/gr_face.cpp \ $($(_NS)_BASE)/src/gr_features.cpp \ $($(_NS)_BASE)/src/gr_font.cpp \ - $($(_NS)_BASE)/src/gr_logging.cpp \ + $($(_NS)_BASE)/src/gr_logging.cpp \ $($(_NS)_BASE)/src/gr_segment.cpp \ $($(_NS)_BASE)/src/gr_slot.cpp \ $($(_NS)_BASE)/src/json.cpp \ - $($(_NS)_BASE)/src/Bidi.cpp \ $($(_NS)_BASE)/src/CachedFace.cpp \ $($(_NS)_BASE)/src/CmapCache.cpp \ $($(_NS)_BASE)/src/Code.cpp \ + $($(_NS)_BASE)/src/Collider.cpp \ + $($(_NS)_BASE)/src/Decompressor.cpp \ $($(_NS)_BASE)/src/Face.cpp \ $($(_NS)_BASE)/src/FeatureMap.cpp \ $($(_NS)_BASE)/src/FileFace.cpp \ $($(_NS)_BASE)/src/Font.cpp \ $($(_NS)_BASE)/src/GlyphCache.cpp \ $($(_NS)_BASE)/src/GlyphFace.cpp \ + $($(_NS)_BASE)/src/Intervals.cpp \ $($(_NS)_BASE)/src/Justifier.cpp \ $($(_NS)_BASE)/src/NameTable.cpp \ $($(_NS)_BASE)/src/Pass.cpp \ + $($(_NS)_BASE)/src/Position.cpp \ $($(_NS)_BASE)/src/SegCache.cpp \ $($(_NS)_BASE)/src/SegCacheEntry.cpp \ $($(_NS)_BASE)/src/SegCacheStore.cpp \ @@ -78,7 +81,11 @@ $(_NS)_PRIVATE_HEADERS = \ $($(_NS)_BASE)/src/inc/CharInfo.h \ $($(_NS)_BASE)/src/inc/CmapCache.h \ $($(_NS)_BASE)/src/inc/Code.h \ + $($(_NS)_BASE)/src/inc/Collider.h \ + $($(_NS)_BASE)/src/inc/Compression.h \ + $($(_NS)_BASE)/src/inc/Decompressor.h \ $($(_NS)_BASE)/src/inc/Endian.h \ + $($(_NS)_BASE)/src/inc/Error.h \ $($(_NS)_BASE)/src/inc/Face.h \ $($(_NS)_BASE)/src/inc/FeatureMap.h \ $($(_NS)_BASE)/src/inc/FeatureVal.h \ @@ -86,6 +93,7 @@ $(_NS)_PRIVATE_HEADERS = \ $($(_NS)_BASE)/src/inc/Font.h \ $($(_NS)_BASE)/src/inc/GlyphCache.h \ $($(_NS)_BASE)/src/inc/GlyphFace.h \ + $($(_NS)_BASE)/src/inc/Intervals.h \ $($(_NS)_BASE)/src/inc/List.h \ $($(_NS)_BASE)/src/inc/locale2lcid.h \ $($(_NS)_BASE)/src/inc/Machine.h \ diff --git a/gfx/graphite2/src/gr_face.cpp b/gfx/graphite2/src/gr_face.cpp index e0f243355..fe1eb8382 100644 --- a/gfx/graphite2/src/gr_face.cpp +++ b/gfx/graphite2/src/gr_face.cpp @@ -31,14 +31,22 @@ of the License or (at your option) any later version. #include "inc/CachedFace.h" #include "inc/CmapCache.h" #include "inc/Silf.h" +#include "inc/json.h" using namespace graphite2; +#if !defined GRAPHITE2_NTRACING +extern json *global_log; +#endif + namespace { bool load_face(Face & face, unsigned int options) { - Face::Table silf(face, Tag::Silf); +#ifdef GRAPHITE2_TELEMETRY + telemetry::category _misc_cat(face.tele.misc); +#endif + Face::Table silf(face, Tag::Silf, 0x00050000); if (silf) options &= ~gr_face_dumbRendering; else if (!(options & gr_face_dumbRendering)) return false; @@ -46,8 +54,27 @@ namespace if (!face.readGlyphs(options)) return false; - return silf ? face.readFeatures() && face.readGraphite(silf) - : options & gr_face_dumbRendering; + if (silf) + { + if (!face.readFeatures() || !face.readGraphite(silf)) + { +#if !defined GRAPHITE2_NTRACING + if (global_log) + { + *global_log << json::object + << "type" << "fontload" + << "failure" << face.error() + << "context" << face.error_context() + << json::close; + } +#endif + return false; + } + else + return true; + } + else + return options & gr_face_dumbRendering; } } @@ -56,7 +83,7 @@ extern "C" { gr_face* gr_make_face_with_ops(const void* appFaceHandle/*non-NULL*/, const gr_face_ops *ops, unsigned int faceOptions) //the appFaceHandle must stay alive all the time when the gr_face is alive. When finished with the gr_face, call destroy_face { - if (ops == 0) return 0; + if (ops == 0) return 0; Face *res = new Face(appFaceHandle, *ops); if (res && load_face(*res, faceOptions)) @@ -76,7 +103,7 @@ gr_face* gr_make_face(const void* appFaceHandle/*non-NULL*/, gr_get_table_fn tab gr_face* gr_make_face_with_seg_cache_and_ops(const void* appFaceHandle/*non-NULL*/, const gr_face_ops *ops, unsigned int cacheSize, unsigned int faceOptions) //the appFaceHandle must stay alive all the time when the GrFace is alive. When finished with the GrFace, call destroy_face { - if (ops == 0) return 0; + if (ops == 0) return 0; CachedFace *res = new CachedFace(appFaceHandle, *ops); if (res && load_face(*res, faceOptions) @@ -117,17 +144,17 @@ void gr_tag_to_str(gr_uint32 tag, char *str) inline uint32 zeropad(const uint32 x) { - if (x == 0x20202020) return 0; - if ((x & 0x00FFFFFF) == 0x00202020) return x & 0xFF000000; - if ((x & 0x0000FFFF) == 0x00002020) return x & 0xFFFF0000; - if ((x & 0x000000FF) == 0x00000020) return x & 0xFFFFFF00; - return x; + if (x == 0x20202020) return 0; + if ((x & 0x00FFFFFF) == 0x00202020) return x & 0xFF000000; + if ((x & 0x0000FFFF) == 0x00002020) return x & 0xFFFF0000; + if ((x & 0x000000FF) == 0x00000020) return x & 0xFFFFFF00; + return x; } gr_feature_val* gr_face_featureval_for_lang(const gr_face* pFace, gr_uint32 langname/*0 means clone default*/) //clones the features. if none for language, clones the default { assert(pFace); - zeropad(langname); + langname = zeropad(langname); return static_cast<gr_feature_val *>(pFace->theSill().cloneFeatures(langname)); } @@ -135,7 +162,7 @@ gr_feature_val* gr_face_featureval_for_lang(const gr_face* pFace, gr_uint32 lang const gr_feature_ref* gr_face_find_fref(const gr_face* pFace, gr_uint32 featId) //When finished with the FeatureRef, call destroy_FeatureRef { assert(pFace); - zeropad(featId); + featId = zeropad(featId); const FeatureRef* pRef = pFace->featureById(featId); return static_cast<const gr_feature_ref*>(pRef); } diff --git a/gfx/graphite2/src/gr_features.cpp b/gfx/graphite2/src/gr_features.cpp index 63c5564ff..38cebb7b5 100644 --- a/gfx/graphite2/src/gr_features.cpp +++ b/gfx/graphite2/src/gr_features.cpp @@ -37,7 +37,7 @@ extern "C" { gr_uint16 gr_fref_feature_value(const gr_feature_ref* pfeatureref, const gr_feature_val* feats) //returns 0 if either pointer is NULL { - if (!pfeatureref || !feats) return 0; + if (!pfeatureref || !feats) return 0; return pfeatureref->getFeatureVal(*feats); } @@ -45,7 +45,7 @@ gr_uint16 gr_fref_feature_value(const gr_feature_ref* pfeatureref, const gr_feat int gr_fref_set_feature_value(const gr_feature_ref* pfeatureref, gr_uint16 val, gr_feature_val* pDest) { - if (!pfeatureref || !pDest) return 0; + if (!pfeatureref || !pDest) return 0; return pfeatureref->applyValToFeature(val, *pDest); } @@ -121,7 +121,7 @@ void* gr_fref_value_label(const gr_feature_ref*pfeatureref, gr_uint16 setting, void gr_label_destroy(void * label) { - free(label); + free(label); } gr_feature_val* gr_featureval_clone(const gr_feature_val* pfeatures/*may be NULL*/) diff --git a/gfx/graphite2/src/gr_font.cpp b/gfx/graphite2/src/gr_font.cpp index 22e4ec9c1..ed70ab738 100644 --- a/gfx/graphite2/src/gr_font.cpp +++ b/gfx/graphite2/src/gr_font.cpp @@ -47,9 +47,9 @@ gr_font* gr_make_font(float ppm/*pixels per em*/, const gr_face *face) gr_font* gr_make_font_with_ops(float ppm/*pixels per em*/, const void* appFontHandle/*non-NULL*/, const gr_font_ops * font_ops, const gr_face * face/*needed for scaling*/) { //the appFontHandle must stay alive all the time when the gr_font is alive. When finished with the gr_font, call destroy_gr_font - if (face == 0) return 0; + if (face == 0) return 0; - Font * const res = new Font(ppm, *face, appFontHandle, font_ops); + Font * const res = new Font(ppm, *face, appFontHandle, font_ops); return static_cast<gr_font*>(res); } diff --git a/gfx/graphite2/src/gr_logging.cpp b/gfx/graphite2/src/gr_logging.cpp index d09dc2f19..a4afcc2a5 100644 --- a/gfx/graphite2/src/gr_logging.cpp +++ b/gfx/graphite2/src/gr_logging.cpp @@ -31,6 +31,8 @@ of the License or (at your option) any later version. #include "inc/CharInfo.h" #include "inc/Slot.h" #include "inc/Segment.h" +#include "inc/json.h" +#include "inc/Collider.h" #if defined _WIN32 #include "windows.h" @@ -38,45 +40,60 @@ of the License or (at your option) any later version. using namespace graphite2; -extern "C" { +#if !defined GRAPHITE2_NTRACING +json *global_log = NULL; +#endif +extern "C" { -bool gr_start_logging(gr_face * face, const char *log_path) +bool gr_start_logging(GR_MAYBE_UNUSED gr_face * face, const char *log_path) { - if (!face || !log_path) return false; + if (!log_path) return false; #if !defined GRAPHITE2_NTRACING - gr_stop_logging(face); + gr_stop_logging(face); #if defined _WIN32 - int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, log_path, -1, 0, 0); - if (n == 0 || n > MAX_PATH - 12) return false; + int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, log_path, -1, 0, 0); + if (n == 0 || n > MAX_PATH - 12) return false; - LPWSTR wlog_path = gralloc<WCHAR>(n); - FILE *log = 0; - if (wlog_path && MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, log_path, -1, wlog_path, n)) - log = _wfopen(wlog_path, L"wt"); + LPWSTR wlog_path = gralloc<WCHAR>(n); + if (!wlog_path) return false; + FILE *log = 0; + if (wlog_path && MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, log_path, -1, wlog_path, n)) + log = _wfopen(wlog_path, L"wt"); - free(wlog_path); + free(wlog_path); #else // _WIN32 - FILE *log = fopen(log_path, "wt"); + FILE *log = fopen(log_path, "wt"); #endif // _WIN32 - if (!log) return false; + if (!log) return false; - face->setLogger(log); - if (!face->logger()) return false; + if (face) + { + face->setLogger(log); + if (!face->logger()) return false; - *face->logger() << json::array; - - return true; + *face->logger() << json::array; +#ifdef GRAPHITE2_TELEMETRY + *face->logger() << face->tele; +#endif + } + else + { + global_log = new json(log); + *global_log << json::array; + } + + return true; #else // GRAPHITE2_NTRACING - return false; + return false; #endif // GRAPHITE2_NTRACING } bool graphite_start_logging(FILE * /* log */, GrLogMask /* mask */) { //#if !defined GRAPHITE2_NTRACING -// graphite_stop_logging(); +// graphite_stop_logging(); // // if (!log) return false; // @@ -90,17 +107,21 @@ bool graphite_start_logging(FILE * /* log */, GrLogMask /* mask */) //#endif } -void gr_stop_logging(gr_face * face) +void gr_stop_logging(GR_MAYBE_UNUSED gr_face * face) { - if (!face) return; - #if !defined GRAPHITE2_NTRACING - if (face->logger()) - { - FILE * log = face->logger()->stream(); - face->setLogger(0); - fclose(log); - } + if (face && face->logger()) + { + FILE * log = face->logger()->stream(); + face->setLogger(0); + fclose(log); + } + else if (!face && global_log) + { + FILE * log = global_log->stream(); + delete global_log; + fclose(log); + } #endif } @@ -112,88 +133,134 @@ void graphite_stop_logging() } // extern "C" +#ifdef GRAPHITE2_TELEMETRY +size_t * graphite2::telemetry::_category = 0UL; +#endif + #if !defined GRAPHITE2_NTRACING +#ifdef GRAPHITE2_TELEMETRY + +json & graphite2::operator << (json & j, const telemetry & t) throw() +{ + j << json::object + << "type" << "telemetry" + << "silf" << t.silf + << "states" << t.states + << "starts" << t.starts + << "transitions" << t.transitions + << "glyphs" << t.glyph + << "code" << t.code + << "misc" << t.misc + << "total" << (t.silf + t.states + t.starts + t.transitions + t.glyph + t.code + t.misc) + << json::close; + return j; +} +#else json & graphite2::operator << (json & j, const telemetry &) throw() { return j; } +#endif json & graphite2::operator << (json & j, const CharInfo & ci) throw() { - return j << json::object - << "offset" << ci.base() - << "unicode" << ci.unicodeChar() - << "break" << ci.breakWeight() - << "flags" << ci.flags() - << "slot" << json::flat << json::object - << "before" << ci.before() - << "after" << ci.after() - << json::close - << json::close; + return j << json::object + << "offset" << ci.base() + << "unicode" << ci.unicodeChar() + << "break" << ci.breakWeight() + << "flags" << ci.flags() + << "slot" << json::flat << json::object + << "before" << ci.before() + << "after" << ci.after() + << json::close + << json::close; } json & graphite2::operator << (json & j, const dslot & ds) throw() { - assert(ds.first); - assert(ds.second); - const Segment & seg = *ds.first; - const Slot & s = *ds.second; - - j << json::object - << "id" << objectid(ds) - << "gid" << s.gid() - << "charinfo" << json::flat << json::object - << "original" << s.original() - << "before" << s.before() - << "after" << s.after() - << json::close - << "origin" << s.origin() - << "shift" << Position(float(s.getAttr(0, gr_slatShiftX, 0)), - float(s.getAttr(0, gr_slatShiftY, 0))) - << "advance" << s.advancePos() - << "insert" << s.isInsertBefore() - << "break" << s.getAttr(&seg, gr_slatBreak, 0); - if (s.just() > 0) - j << "justification" << s.just(); - if (s.getBidiLevel() > 0) - j << "bidi" << s.getBidiLevel(); - if (!s.isBase()) - j << "parent" << json::flat << json::object - << "id" << objectid(dslot(&seg, s.attachedTo())) - << "level" << s.getAttr(0, gr_slatAttLevel, 0) - << "offset" << s.attachOffset() - << json::close; - j << "user" << json::flat << json::array; - for (int n = 0; n!= seg.numAttrs(); ++n) - j << s.userAttrs()[n]; - j << json::close; - if (s.firstChild()) - { - j << "children" << json::flat << json::array; - for (const Slot *c = s.firstChild(); c; c = c->nextSibling()) - j << objectid(dslot(&seg, c)); - j << json::close; - } - return j << json::close; + assert(ds.first); + assert(ds.second); + const Segment & seg = *ds.first; + const Slot & s = *ds.second; + const SlotCollision *cslot = seg.collisionInfo(ds.second); + + j << json::object + << "id" << objectid(ds) + << "gid" << s.gid() + << "charinfo" << json::flat << json::object + << "original" << s.original() + << "before" << s.before() + << "after" << s.after() + << json::close + << "origin" << s.origin() + << "shift" << Position(float(s.getAttr(0, gr_slatShiftX, 0)), + float(s.getAttr(0, gr_slatShiftY, 0))) + << "advance" << s.advancePos() + << "insert" << s.isInsertBefore() + << "break" << s.getAttr(&seg, gr_slatBreak, 0); + if (s.just() > 0) + j << "justification" << s.just(); + if (s.getBidiLevel() > 0) + j << "bidi" << s.getBidiLevel(); + if (!s.isBase()) + j << "parent" << json::flat << json::object + << "id" << objectid(dslot(&seg, s.attachedTo())) + << "level" << s.getAttr(0, gr_slatAttLevel, 0) + << "offset" << s.attachOffset() + << json::close; + j << "user" << json::flat << json::array; + for (int n = 0; n!= seg.numAttrs(); ++n) + j << s.userAttrs()[n]; + j << json::close; + if (s.firstChild()) + { + j << "children" << json::flat << json::array; + for (const Slot *c = s.firstChild(); c; c = c->nextSibling()) + j << objectid(dslot(&seg, c)); + j << json::close; + } + if (cslot) + { + // Note: the reason for using Positions to lump together related attributes is to make the + // JSON output slightly more compact. + j << "collision" << json::flat << json::object +// << "shift" << cslot->shift() -- not used pass level, only within the collision routine itself + << "offset" << cslot->offset() + << "limit" << cslot->limit() + << "flags" << cslot->flags() + << "margin" << Position(cslot->margin(), cslot->marginWt()) + << "exclude" << cslot->exclGlyph() + << "excludeoffset" << cslot->exclOffset(); + if (cslot->seqOrder() != 0) + { + j << "seqclass" << Position(cslot->seqClass(), cslot->seqProxClass()) + << "seqorder" << cslot->seqOrder() + << "seqabove" << Position(cslot->seqAboveXoff(), cslot->seqAboveWt()) + << "seqbelow" << Position(cslot->seqBelowXlim(), cslot->seqBelowWt()) + << "seqvalign" << Position(cslot->seqValignHt(), cslot->seqValignWt()); + } + j << json::close; + } + return j << json::close; } graphite2::objectid::objectid(const dslot & ds) throw() { const Slot * const p = ds.second; - uint32 s = reinterpret_cast<size_t>(p); - sprintf(name, "%.4x-%.2x-%.4hx", uint16(s >> 16), uint16(p ? p->userAttrs()[ds.first->silf()->numUser()] : 0), uint16(s)); - name[sizeof name-1] = 0; + uint32 s = reinterpret_cast<size_t>(p); + sprintf(name, "%.4x-%.2x-%.4hx", uint16(s >> 16), uint16(p ? p->userAttrs()[ds.first->silf()->numUser()] : 0), uint16(s)); + name[sizeof name-1] = 0; } graphite2::objectid::objectid(const Segment * const p) throw() { - uint32 s = reinterpret_cast<size_t>(p); - sprintf(name, "%.4x-%.2x-%.4hx", uint16(s >> 16), 0, uint16(s)); - name[sizeof name-1] = 0; + uint32 s = reinterpret_cast<size_t>(p); + sprintf(name, "%.4x-%.2x-%.4hx", uint16(s >> 16), 0, uint16(s)); + name[sizeof name-1] = 0; } #endif diff --git a/gfx/graphite2/src/gr_segment.cpp b/gfx/graphite2/src/gr_segment.cpp index 6eca66b4a..0ab7a0854 100644 --- a/gfx/graphite2/src/gr_segment.cpp +++ b/gfx/graphite2/src/gr_segment.cpp @@ -42,17 +42,13 @@ namespace // if (!font) return NULL; Segment* pRes=new Segment(nChars, face, script, dir); - pRes->read_text(face, pFeats, enc, pStart, nChars); - if (!pRes->runGraphite()) + + if (!pRes->read_text(face, pFeats, enc, pStart, nChars) || !pRes->runGraphite()) { delete pRes; return NULL; } - // run the line break passes - // run the substitution passes - pRes->prepare_pos(font); - // run the positioning passes - pRes->finalise(font); + pRes->finalise(font, true); return static_cast<gr_segment*>(pRes); } @@ -64,46 +60,46 @@ namespace template <typename utf_iter> inline size_t count_unicode_chars(utf_iter first, const utf_iter last, const void **error) { - size_t n_chars = 0; - uint32 usv = 0; - - if (last) - { - for (;first != last; ++first, ++n_chars) - if ((usv = *first) == 0 || first.error()) break; - } - else - { - while ((usv = *first) != 0 && !first.error()) - { - ++first; - ++n_chars; - } - } - - if (error) *error = first.error() ? first : 0; - return n_chars; + size_t n_chars = 0; + uint32 usv = 0; + + if (last) + { + for (;first != last; ++first, ++n_chars) + if ((usv = *first) == 0 || first.error()) break; + } + else + { + while ((usv = *first) != 0 && !first.error()) + { + ++first; + ++n_chars; + } + } + + if (error) *error = first.error() ? first : 0; + return n_chars; } extern "C" { size_t gr_count_unicode_characters(gr_encform enc, const void* buffer_begin, const void* buffer_end/*don't go on or past end, If NULL then ignored*/, const void** pError) //Also stops on nul. Any nul is not in the count { - assert(buffer_begin); - - switch (enc) - { - case gr_utf8: return count_unicode_chars<utf8::const_iterator>(buffer_begin, buffer_end, pError); break; - case gr_utf16: return count_unicode_chars<utf16::const_iterator>(buffer_begin, buffer_end, pError); break; - case gr_utf32: return count_unicode_chars<utf32::const_iterator>(buffer_begin, buffer_end, pError); break; - default: return 0; - } + assert(buffer_begin); + + switch (enc) + { + case gr_utf8: return count_unicode_chars<utf8::const_iterator>(buffer_begin, buffer_end, pError); break; + case gr_utf16: return count_unicode_chars<utf16::const_iterator>(buffer_begin, buffer_end, pError); break; + case gr_utf32: return count_unicode_chars<utf32::const_iterator>(buffer_begin, buffer_end, pError); break; + default: return 0; + } } gr_segment* gr_make_seg(const gr_font *font, const gr_face *face, gr_uint32 script, const gr_feature_val* pFeats, gr_encform enc, const void* pStart, size_t nChars, int dir) { - const gr_feature_val * tmp_feats = 0; + const gr_feature_val * tmp_feats = 0; if (pFeats == 0) pFeats = tmp_feats = static_cast<const gr_feature_val*>(face->theSill().cloneFeatures(0)); gr_segment * seg = makeAndInitialize(font, face, script, pFeats, enc, pStart, nChars, dir); diff --git a/gfx/graphite2/src/gr_slot.cpp b/gfx/graphite2/src/gr_slot.cpp index c615e40ff..0e41f6cb5 100644 --- a/gfx/graphite2/src/gr_slot.cpp +++ b/gfx/graphite2/src/gr_slot.cpp @@ -103,11 +103,11 @@ float gr_slot_advance_X(const gr_slot* p/*not NULL*/, const gr_face *face, const return res; } -float gr_slot_advance_Y(const gr_slot *p/*not NULL*/, const gr_face *face, const gr_font *font) +float gr_slot_advance_Y(const gr_slot *p/*not NULL*/, GR_MAYBE_UNUSED const gr_face *face, const gr_font *font) { assert(p); float res = p->advancePos().y; - if (font && (face || !face)) + if (font) return res * font->scale(); else return res; diff --git a/gfx/graphite2/src/inc/CharInfo.h b/gfx/graphite2/src/inc/CharInfo.h index 2debaf734..d9e56eeed 100644 --- a/gfx/graphite2/src/inc/CharInfo.h +++ b/gfx/graphite2/src/inc/CharInfo.h @@ -34,7 +34,7 @@ class CharInfo { public: - CharInfo() : m_char(0), m_before(-1), m_after(0), m_base(0), m_featureid(0), m_break(0), m_flags(0) {} + CharInfo() : m_char(0), m_before(-1), m_after(-1), m_base(0), m_featureid(0), m_break(0), m_flags(0) {} void init(int cid) { m_char = cid; } unsigned int unicodeChar() const { return m_char; } void feats(int offset) { m_featureid = offset; } @@ -56,8 +56,8 @@ private: int m_before; // slot index before us, comes before int m_after; // slot index after us, comes after size_t m_base; // offset into input string corresponding to this charinfo - uint8 m_featureid; // index into features list in the segment - int8 m_break; // breakweight coming from lb table + uint8 m_featureid; // index into features list in the segment + int8 m_break; // breakweight coming from lb table uint8 m_flags; // 0,1 segment split. }; diff --git a/gfx/graphite2/src/inc/CmapCache.h b/gfx/graphite2/src/inc/CmapCache.h index a7df78ea5..7820c958b 100644 --- a/gfx/graphite2/src/inc/CmapCache.h +++ b/gfx/graphite2/src/inc/CmapCache.h @@ -37,13 +37,13 @@ class Cmap { public: - virtual ~Cmap() throw() {} + virtual ~Cmap() throw() {} - virtual uint16 operator [] (const uint32) const throw() { return 0; } + virtual uint16 operator [] (const uint32) const throw() { return 0; } - virtual operator bool () const throw() { return false; } + virtual operator bool () const throw() { return false; } - CLASS_NEW_DELETE; + CLASS_NEW_DELETE; }; class DirectCmap : public Cmap @@ -52,9 +52,9 @@ class DirectCmap : public Cmap DirectCmap & operator = (const DirectCmap &); public: - DirectCmap(const Face &); - virtual uint16 operator [] (const uint32 usv) const throw(); - virtual operator bool () const throw(); + DirectCmap(const Face &); + virtual uint16 operator [] (const uint32 usv) const throw(); + virtual operator bool () const throw(); CLASS_NEW_DELETE; private: @@ -69,10 +69,10 @@ class CachedCmap : public Cmap CachedCmap & operator = (const CachedCmap &); public: - CachedCmap(const Face &); - virtual ~CachedCmap() throw(); - virtual uint16 operator [] (const uint32 usv) const throw(); - virtual operator bool () const throw(); + CachedCmap(const Face &); + virtual ~CachedCmap() throw(); + virtual uint16 operator [] (const uint32 usv) const throw(); + virtual operator bool () const throw(); CLASS_NEW_DELETE; private: bool m_isBmpOnly; diff --git a/gfx/graphite2/src/inc/Code.h b/gfx/graphite2/src/inc/Code.h index d9539b0da..02d09a109 100644 --- a/gfx/graphite2/src/inc/Code.h +++ b/gfx/graphite2/src/inc/Code.h @@ -41,6 +41,14 @@ namespace graphite2 { class Silf; class Face; +enum passtype { + PASS_TYPE_UNKNOWN = 0, + PASS_TYPE_LINEBREAK, + PASS_TYPE_SUBSTITUTE, + PASS_TYPE_POSITIONING, + PASS_TYPE_JUSTIFICATION +}; + namespace vm { class Machine::Code @@ -56,7 +64,8 @@ public: jump_past_end, arguments_exhausted, missing_return, - nested_context_item + nested_context_item, + underfull_stack }; private: @@ -77,30 +86,41 @@ private: void failure(const status_t) throw(); public: + static size_t estimateCodeDataOut(size_t num_bytecodes); + Code() throw(); Code(bool is_constraint, const byte * bytecode_begin, const byte * const bytecode_end, - uint8 pre_context, uint16 rule_length, const Silf &, const Face &); + uint8 pre_context, uint16 rule_length, const Silf &, const Face &, + enum passtype pt, byte * * const _out = 0); Code(const Machine::Code &) throw(); ~Code() throw(); Code & operator=(const Code &rhs) throw(); - operator bool () const throw(); - status_t status() const throw(); - bool constraint() const throw(); - size_t dataSize() const throw(); - size_t instructionCount() const throw(); - bool immutable() const throw(); - bool deletes() const throw(); - size_t maxRef() const throw(); + operator bool () const throw() { return _code && status() == loaded; } + status_t status() const throw() { return _status; } + bool constraint() const throw() { return _constraint; } + size_t dataSize() const throw() { return _data_size; } + size_t instructionCount() const throw() { return _instr_count; } + bool immutable() const throw() { return !(_delete || _modify); } + bool deletes() const throw() { return _delete; } + size_t maxRef() const throw() { return _max_ref; } + void externalProgramMoved(ptrdiff_t) throw(); int32 run(Machine &m, slotref * & map) const; CLASS_NEW_DELETE; }; +inline +size_t Machine::Code::estimateCodeDataOut(size_t n_bc) +{ + return n_bc * (sizeof(instr)+sizeof(byte)); +} + + inline Machine::Code::Code() throw() : _code(0), _data(0), _data_size(0), _instr_count(0), _max_ref(0), - _status(loaded), _constraint(false), _modify(false),_delete(false), + _status(loaded), _constraint(false), _modify(false), _delete(false), _own(false) { } @@ -136,39 +156,13 @@ inline Machine::Code & Machine::Code::operator=(const Machine::Code &rhs) throw( return *this; } -inline Machine::Code::operator bool () const throw () { - return _code && status() == loaded; -} - -inline Machine::Code::status_t Machine::Code::status() const throw() { - return _status; -} - -inline bool Machine::Code::constraint() const throw() { - return _constraint; -} - -inline size_t Machine::Code::dataSize() const throw() { - return _data_size; -} - -inline size_t Machine::Code::instructionCount() const throw() { - return _instr_count; -} - -inline bool Machine::Code::immutable() const throw() -{ - return !(_delete || _modify); -} - -inline bool Machine::Code::deletes() const throw() -{ - return _delete; -} - -inline size_t Machine::Code::maxRef() const throw() +inline void Machine::Code::externalProgramMoved(ptrdiff_t dist) throw() { - return _max_ref; + if (_code && !_own) + { + _code += dist / sizeof(instr); + _data += dist; + } } } // namespace vm diff --git a/gfx/graphite2/src/inc/Collider.h b/gfx/graphite2/src/inc/Collider.h new file mode 100644 index 000000000..bcbf3a224 --- /dev/null +++ b/gfx/graphite2/src/inc/Collider.h @@ -0,0 +1,242 @@ +/* GRAPHITE2 LICENSING + + Copyright 2010, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ +#pragma once + +#include "inc/List.h" +#include "inc/Position.h" +#include "inc/Intervals.h" +#include "inc/debug.h" + +namespace graphite2 { + +class json; +class Slot; +class Segment; + +#define SLOTCOLSETUINTPROP(x, y) uint16 x() const { return _ ##x; } void y (uint16 v) { _ ##x = v; } +#define SLOTCOLSETINTPROP(x, y) int16 x() const { return _ ##x; } void y (int16 v) { _ ##x = v; } +#define SLOTCOLSETPOSITIONPROP(x, y) const Position &x() const { return _ ##x; } void y (const Position &v) { _ ##x = v; } + +// Slot attributes related to collision-fixing +class SlotCollision +{ +public: + enum { + // COLL_TESTONLY = 0, // default - test other glyphs for collision with this one, but don't move this one + COLL_FIX = 1, // fix collisions involving this glyph + COLL_IGNORE = 2, // ignore this glyph altogether + COLL_START = 4, // start of range of possible collisions + COLL_END = 8, // end of range of possible collisions + COLL_KERN = 16, // collisions with this glyph are fixed by adding kerning space after it + COLL_ISCOL = 32, // this glyph has a collision + COLL_KNOWN = 64, // we've figured out what's happening with this glyph + COLL_TEMPLOCK = 128, // Lock glyphs that have been given priority positioning + ////COLL_JUMPABLE = 128, // moving glyphs may jump this stationary glyph in any direction - DELETE + ////COLL_OVERLAP = 256, // use maxoverlap to restrict - DELETE + }; + + // Behavior for the collision.order attribute. To GDL this is an enum, to us it's a bitfield, with only 1 bit set + // Allows for easier inversion. + enum { + SEQ_ORDER_LEFTDOWN = 1, + SEQ_ORDER_RIGHTUP = 2, + SEQ_ORDER_NOABOVE = 4, + SEQ_ORDER_NOBELOW = 8, + SEQ_ORDER_NOLEFT = 16, + SEQ_ORDER_NORIGHT = 32 + }; + + SlotCollision(Segment *seg, Slot *slot); + void initFromSlot(Segment *seg, Slot *slot); + + const Rect &limit() const { return _limit; } + void setLimit(const Rect &r) { _limit = r; } + SLOTCOLSETPOSITIONPROP(shift, setShift) + SLOTCOLSETPOSITIONPROP(offset, setOffset) + SLOTCOLSETPOSITIONPROP(exclOffset, setExclOffset) + SLOTCOLSETUINTPROP(margin, setMargin) + SLOTCOLSETUINTPROP(marginWt, setMarginWt) + SLOTCOLSETUINTPROP(flags, setFlags) + SLOTCOLSETUINTPROP(exclGlyph, setExclGlyph) + SLOTCOLSETUINTPROP(seqClass, setSeqClass) + SLOTCOLSETUINTPROP(seqProxClass, setSeqProxClass) + SLOTCOLSETUINTPROP(seqOrder, setSeqOrder) + SLOTCOLSETINTPROP(seqAboveXoff, setSeqAboveXoff) + SLOTCOLSETUINTPROP(seqAboveWt, setSeqAboveWt) + SLOTCOLSETINTPROP(seqBelowXlim, setSeqBelowXlim) + SLOTCOLSETUINTPROP(seqBelowWt, setSeqBelowWt) + SLOTCOLSETUINTPROP(seqValignHt, setSeqValignHt) + SLOTCOLSETUINTPROP(seqValignWt, setSeqValignWt) + + float getKern(int dir) const; + +private: + Rect _limit; + Position _shift; // adjustment within the given pass + Position _offset; // total adjustment for collisions + Position _exclOffset; + uint16 _margin; + uint16 _marginWt; + uint16 _flags; + uint16 _exclGlyph; + uint16 _seqClass; + uint16 _seqProxClass; + uint16 _seqOrder; + int16 _seqAboveXoff; + uint16 _seqAboveWt; + int16 _seqBelowXlim; + uint16 _seqBelowWt; + uint16 _seqValignHt; + uint16 _seqValignWt; + +}; // end of class SlotColllision + +struct BBox; +struct SlantBox; + +class ShiftCollider +{ +public: + typedef std::pair<float, float> fpair; + typedef Vector<fpair> vfpairs; + typedef vfpairs::iterator ivfpairs; + + ShiftCollider(json *dbgout); + ~ShiftCollider() throw() { }; + + bool initSlot(Segment *seg, Slot *aSlot, const Rect &constraint, + float margin, float marginMin, const Position &currShift, + const Position &currOffset, int dir, GR_MAYBE_UNUSED json * const dbgout); + bool mergeSlot(Segment *seg, Slot *slot, const Position &currShift, bool isAfter, + bool sameCluster, bool &hasCol, bool isExclusion, GR_MAYBE_UNUSED json * const dbgout); + Position resolve(Segment *seg, bool &isCol, GR_MAYBE_UNUSED json * const dbgout); + void addBox_slope(bool isx, const Rect &box, const BBox &bb, const SlantBox &sb, const Position &org, float weight, float m, bool minright, int mode); + void removeBox(const Rect &box, const BBox &bb, const SlantBox &sb, const Position &org, int mode); + const Position &origin() const { return _origin; } + +#if !defined GRAPHITE2_NTRACING + void outputJsonDbg(json * const dbgout, Segment *seg, int axis); + void outputJsonDbgStartSlot(json * const dbgout, Segment *seg); + void outputJsonDbgEndSlot(json * const dbgout, Position resultPos, int bestAxis, bool isCol); + void outputJsonDbgOneVector(json * const dbgout, Segment *seg, int axis, float tleft, float bestCost, float bestVal); + void outputJsonDbgRawRanges(json * const dbgout, int axis); + void outputJsonDbgRemovals(json * const dbgout, int axis, Segment *seg); +#endif + + CLASS_NEW_DELETE; + +protected: + Zones _ranges[4]; // possible movements in 4 directions (horizontally, vertically, diagonally); + Slot * _target; // the glyph to fix + Rect _limit; + Position _currShift; + Position _currOffset; + Position _origin; // Base for all relative calculations + float _margin; + float _marginWt; + float _len[4]; + uint16 _seqClass; + uint16 _seqProxClass; + uint16 _seqOrder; + + //bool _scraping[4]; + +}; // end of class ShiftCollider + +inline +ShiftCollider::ShiftCollider(GR_MAYBE_UNUSED json *dbgout) +: _target(0), + _margin(0.0), + _marginWt(0.0), + _seqClass(0), + _seqProxClass(0), + _seqOrder(0) +{ +#if !defined GRAPHITE2_NTRACING + for (int i = 0; i < 4; ++i) + _ranges[i].setdebug(dbgout); +#endif +} + +class KernCollider +{ +public: + KernCollider(json *dbg); + ~KernCollider() throw() { }; + bool initSlot(Segment *seg, Slot *aSlot, const Rect &constraint, float margin, + const Position &currShift, const Position &offsetPrev, int dir, + float ymin, float ymax, json * const dbgout); + bool mergeSlot(Segment *seg, Slot *slot, const Position &currShift, float currSpace, int dir, json * const dbgout); + Position resolve(Segment *seg, Slot *slot, int dir, float margin, json * const dbgout); + void shift(const Position &mv, int dir); + + CLASS_NEW_DELETE; + +private: + Slot * _target; // the glyph to fix + Rect _limit; + float _margin; + Position _offsetPrev; // kern from a previous pass + Position _currShift; // NOT USED?? + float _miny; // y-coordinates offset by global slot position + float _maxy; + Vector<float> _edges; // edges of horizontal slices + float _sliceWidth; // width of each slice + float _mingap; + float _xbound; // max or min edge + +#if !defined GRAPHITE2_NTRACING + // Debugging + Segment * _seg; + Vector<float> _nearEdges; // closest potential collision in each slice + Vector<Slot*> _slotNear; +#endif +}; // end of class KernCollider + + +inline +float sqr(float x) { + return x * x; +} + +inline +KernCollider::KernCollider(GR_MAYBE_UNUSED json *dbg) +: _target(0), + _margin(0.0f), + _miny(-1e38f), + _maxy(1e38f), + _sliceWidth(0.0f), + _mingap(0.0f), + _xbound(0.0) +{ +#if !defined GRAPHITE2_NTRACING + _seg = 0; +#endif +}; + +}; // end of namespace graphite2 + diff --git a/gfx/graphite2/src/inc/Compression.h b/gfx/graphite2/src/inc/Compression.h new file mode 100644 index 000000000..61351c416 --- /dev/null +++ b/gfx/graphite2/src/inc/Compression.h @@ -0,0 +1,103 @@ +/* GRAPHITE2 LICENSING + + Copyright 2015, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ + +#pragma once + +#include <cassert> +#include <cstddef> +#include <cstring> + +namespace +{ + +#if defined(_MSC_VER) +typedef unsigned __int8 u8; +typedef unsigned __int16 u16; +typedef unsigned __int32 u32; +typedef unsigned __int64 u64; +#else +#include <stdint.h> +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +#endif + +ptrdiff_t const MINMATCH = 4; + +template<int S> +inline +void unaligned_copy(void * d, void const * s) { + ::memcpy(d, s, S); +} + +inline +size_t align(size_t p) { + return (p + sizeof(unsigned long)-1) & ~(sizeof(unsigned long)-1); +} + +inline +u8 * safe_copy(u8 * d, u8 const * s, size_t n) { + while (n--) *d++ = *s++; + return d; +} + +inline +u8 * overrun_copy(u8 * d, u8 const * s, size_t n) { + size_t const WS = sizeof(unsigned long); + u8 const * e = s + n; + do + { + unaligned_copy<WS>(d, s); + d += WS; + s += WS; + } + while (s < e); + d-=(s-e); + + return d; +} + + +inline +u8 * fast_copy(u8 * d, u8 const * s, size_t n) { + size_t const WS = sizeof(unsigned long); + size_t wn = n/WS; + while (wn--) + { + unaligned_copy<WS>(d, s); + d += WS; + s += WS; + } + n &= WS-1; + return safe_copy(d, s, n); +} + + +} // end of anonymous namespace + + diff --git a/gfx/graphite2/src/Rule.cpp b/gfx/graphite2/src/inc/Decompressor.h index afdab495f..3d81864d1 100644 --- a/gfx/graphite2/src/Rule.cpp +++ b/gfx/graphite2/src/inc/Decompressor.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2015, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -25,7 +25,32 @@ License, as published by the Free Software Foundation, either version 2 of the License or (at your option) any later version. */ -#include "inc/Rule.h" -#include "inc/Segment.h" +#pragma once + +#include <cstddef> + +namespace lz4 +{ + +// decompress an LZ4 block +// Parameters: +// @in - Input buffer containing an LZ4 block. +// @in_size - Size of the input LZ4 block in bytes. +// @out - Output buffer to hold decompressed results. +// @out_size - The size of the buffer pointed to by @out. +// Invariants: +// @in - This buffer must be at least 1 machine word in length, +// regardless of the actual LZ4 block size. +// @in_size - This must be at least 4 and must also be <= to the +// allocated buffer @in. +// @out - This must be bigger than the input buffer and at least +// 13 bytes. +// @out_size - Must always be big enough to hold the expected size. +// Return: +// -1 - Decompression failed. +// size - Actual number of bytes decompressed. +int decompress(void const *in, size_t in_size, void *out, size_t out_size); + +} // end of namespace shrinker + -using namespace graphite2; diff --git a/gfx/graphite2/src/inc/Endian.h b/gfx/graphite2/src/inc/Endian.h index 16ff79f55..fc84ec278 100644 --- a/gfx/graphite2/src/inc/Endian.h +++ b/gfx/graphite2/src/inc/Endian.h @@ -1,6 +1,7 @@ -/*----------------------------------------------------------------------------- -Copyright (C) 2011 SIL International -Responsibility: Tim Eves +/* GRAPHITE2 LICENSING + + Copyright 2011, SIL International + All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published @@ -22,17 +23,21 @@ Alternatively, the contents of this file may be used under the terms of the Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public License, as published by the Free Software Foundation, either version 2 of the License or (at your option) any later version. +*/ +/* Description: -A set of fast template based decoders for decoding values of any C integer -type up to long int size laid out with most significant byte first or least -significant byte first (aka big endian or little endian). These are CPU -byte order agnostic and will function the same regardless of the CPUs native -byte order. -Being template based means if the either le or be class is not used then -template code of unused functions will not be instantiated by the compiler -and thus shouldn't cause any overhead. ------------------------------------------------------------------------------*/ + A set of fast template based decoders for decoding values of any C integer + type up to long int size laid out with most significant byte first or least + significant byte first (aka big endian or little endian). These are CPU + byte order agnostic and will function the same regardless of the CPUs native + byte order. + + Being template based means if the either le or be class is not used then + template code of unused functions will not be instantiated by the compiler + and thus shouldn't cause any overhead. +*/ + #include <cstddef> #pragma once @@ -40,32 +45,32 @@ and thus shouldn't cause any overhead. class be { - template<int S> - inline static unsigned long int _peek(const unsigned char * p) { - return _peek<S/2>(p) << (S/2)*8 | _peek<S/2>(p+S/2); - } + template<int S> + inline static unsigned long int _peek(const unsigned char * p) { + return _peek<S/2>(p) << (S/2)*8 | _peek<S/2>(p+S/2); + } public: - template<typename T> - inline static T peek(const void * p) { - return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p))); - } - - template<typename T> - inline static T read(const unsigned char * &p) { - const T r = T(_peek<sizeof(T)>(p)); - p += sizeof r; - return r; - } - - template<typename T> - inline static T swap(const T x) { - return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x))); - } - - template<typename T> - inline static void skip(const unsigned char * &p, size_t n=1) { - p += sizeof(T)*n; - } + template<typename T> + inline static T peek(const void * p) { + return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p))); + } + + template<typename T> + inline static T read(const unsigned char * &p) { + const T r = T(_peek<sizeof(T)>(p)); + p += sizeof r; + return r; + } + + template<typename T> + inline static T swap(const T x) { + return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x))); + } + + template<typename T> + inline static void skip(const unsigned char * &p, size_t n=1) { + p += sizeof(T)*n; + } }; template<> @@ -74,32 +79,32 @@ inline unsigned long int be::_peek<1>(const unsigned char * p) { return *p; } class le { - template<int S> - inline static unsigned long int _peek(const unsigned char * p) { - return _peek<S/2>(p) | _peek<S/2>(p+S/2) << (S/2)*8; - } + template<int S> + inline static unsigned long int _peek(const unsigned char * p) { + return _peek<S/2>(p) | _peek<S/2>(p+S/2) << (S/2)*8; + } public: - template<typename T> - inline static T peek(const void * p) { - return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p))); - } - - template<typename T> - inline static T read(const unsigned char * &p) { - const T r = T(_peek<sizeof(T)>(p)); - p += sizeof r; - return r; - } - - template<typename T> - inline static T swap(const T x) { - return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x))); - } - - template<typename T> - inline static void skip(const unsigned char * &p, size_t n=1) { - p += sizeof(T)*n; - } + template<typename T> + inline static T peek(const void * p) { + return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p))); + } + + template<typename T> + inline static T read(const unsigned char * &p) { + const T r = T(_peek<sizeof(T)>(p)); + p += sizeof r; + return r; + } + + template<typename T> + inline static T swap(const T x) { + return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x))); + } + + template<typename T> + inline static void skip(const unsigned char * &p, size_t n=1) { + p += sizeof(T)*n; + } }; template<> diff --git a/gfx/graphite2/src/inc/Error.h b/gfx/graphite2/src/inc/Error.h new file mode 100644 index 000000000..07d70b78e --- /dev/null +++ b/gfx/graphite2/src/inc/Error.h @@ -0,0 +1,135 @@ +/* GRAPHITE2 LICENSING + + Copyright 2013, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ +#pragma once + +// numbers are explicitly assigned for future proofing + +namespace graphite2 +{ + +class Error +{ +public: + Error() : _e(0) {}; + operator bool() { return (_e != 0); } + int error() { return _e; } + void error(int e) { _e = e; } + bool test(bool pr, int err) { return (_e = int(pr) * err); } + +private: + int _e; +}; + +enum errcontext { + EC_READGLYPHS = 1, // while reading glyphs + EC_READSILF = 2, // in Silf table + EC_ASILF = 3, // in Silf %d + EC_APASS = 4, // in Silf %d, pass %d + EC_PASSCCODE = 5, // in pass constraint code for Silf %d, pass %d + EC_ARULE = 6, // in Silf %d, pass %d, rule %d + EC_ASTARTS = 7, // in Silf %d, pass %d, start state %d + EC_ATRANS = 8, // in Silf %d, pass %d, fsm state %d + EC_ARULEMAP = 9 // in Silf %d, pass %d, state %d +}; + +enum errors { + E_OUTOFMEM = 1, // Out of memory + E_NOGLYPHS = 2, // There are no glyphs in the font + E_BADUPEM = 3, // The units per em for the font is bad (0) + E_BADCMAP = 4, // The font does not contain any useful cmaps + E_NOSILF = 5, // Missing Silf table + E_TOOOLD = 6, // Silf table version is too old + E_BADSIZE = 7, // context object has the wrong structural size +// Silf Subtable Errors take a Silf subtable number * 256 in the context + E_BADMAXGLYPH = 8, // Silf max glyph id is too high + E_BADNUMJUSTS = 9, // Number of Silf justification blocks is too high + E_BADENDJUSTS = 10, // Silf justification blocks take too much of the Silf table space + E_BADCRITFEATURES = 11, // Critical features section in a Silf table is too big + E_BADSCRIPTTAGS = 12, // Silf script tags area is too big + E_BADAPSEUDO = 13, // The pseudo glyph attribute number is too high + E_BADABREAK = 14, // The linebreak glyph attribute number is too high + E_BADABIDI = 15, // The bidi glyph attribute number is too high + E_BADAMIRROR = 16, // The mirrored glyph attribute number is too high + E_BADNUMPASSES = 17, // The number of passes is > 128 + E_BADPASSESSTART = 18, // The Silf table is too small to hold any passes + E_BADPASSBOUND = 19, // The positioning pass number is too low or the substitution pass number is too high + E_BADPPASS = 20, // The positioning pass number is too high + E_BADSPASS = 21, // the substitution pass number is too high + E_BADJPASSBOUND = 22, // the justification pass must be higher than the positioning pass + E_BADJPASS = 23, // the justification pass is too high + E_BADALIG = 24, // the number of initial ligature component glyph attributes is too high + E_BADBPASS = 25, // the bidi pass number is specified and is either too high or too low + E_BADNUMPSEUDO = 26, // The number of pseudo glyphs is too high + E_BADCLASSSIZE = 27, // The size of the classes block is bad + E_TOOMANYLINEAR = 28, // The number of linear classes in the silf table is too high + E_CLASSESTOOBIG = 29, // There are too many classes for the space allocated in the Silf subtable + E_MISALIGNEDCLASSES = 30, // The class offsets in the class table don't line up with the number of classes + E_HIGHCLASSOFFSET = 31, // The class offsets point out of the class table + E_BADCLASSOFFSET = 32, // A class offset is less than one following it + E_BADCLASSLOOKUPINFO = 33, // The search header info for a non-linear class has wrong values in it +// Pass subtable errors. Context has pass number * 65536 + E_BADPASSSTART = 34, // The start offset for a particular pass is bad + E_BADPASSEND = 35, // The end offset for a particular pass is bad + E_BADPASSLENGTH = 36, // The length of the pass is too small + E_BADNUMTRANS = 37, // The number of transition states in the fsm is bad + E_BADNUMSUCCESS = 38, // The number of success states in the fsm is bad + E_BADNUMSTATES = 39, // The number of states in the fsm is bad + E_NORANGES = 40, // There are no columns in the fsm + E_BADRULEMAPLEN = 41, // The size of the success state to rule mapping is bad + E_BADCTXTLENBOUNDS = 42, // The precontext maximum is greater than its minimum + E_BADCTXTLENS = 43, // The lists of rule lengths or pre context lengths is bad + E_BADPASSCCODEPTR = 44, // The pass constraint code position does not align with where the forward reference says it should be + E_BADRULECCODEPTR = 45, // The rule constraint code position does not align with where the forward reference says it should be + E_BADCCODELEN = 46, // Bad rule/pass constraint code length + E_BADACTIONCODEPTR = 47, // The action code position does not align with where the forward reference says it should be + E_MUTABLECCODE = 48, // Constraint code edits slots. It shouldn't. + E_BADSTATE = 49, // Bad state transition referencing an illegal state + E_BADRULEMAPPING = 50, // The structure of the rule mapping is bad + E_BADRANGE = 51, // Bad column range structure including a glyph in more than one column + E_BADRULENUM = 52, // A reference to a rule is out of range (too high) + E_BADACOLLISION = 53, // Bad Silf table collision attribute number (too high) + E_BADEMPTYPASS = 54, // Can't have empty passes (no rules) except for collision passes + E_BADSILFVERSION = 55, // The Silf table has a bad version (probably too high) + E_BADCOLLISIONPASS = 56, // Collision flags set on a non positioning pass + E_BADNUMCOLUMNS = 57, // Arbitrarily limit number of columns in fsm +// Code errors + E_CODEFAILURE = 60, // Base code error. The following subcodes must align with Machine::Code::status_t in Code.h + E_CODEALLOC = 61, // Out of memory + E_INVALIDOPCODE = 62, // Invalid op code + E_UNIMPOPCODE = 63, // Unimplemented op code encountered + E_OUTOFRANGECODE = 64, // Code argument out of range + E_BADJUMPCODE = 65, // Code jumps past end of op codes + E_CODEBADARGS = 66, // Code arguments exhausted + E_CODENORETURN = 67, // Missing return type op code at end of code + E_CODENESTEDCTXT = 68, // Nested context encountered in code +// Compression errors + E_BADSCHEME = 69, + E_SHRINKERFAILED = 70, +}; + +} + diff --git a/gfx/graphite2/src/inc/Face.h b/gfx/graphite2/src/inc/Face.h index 20c5b4322..ba1f24aee 100644 --- a/gfx/graphite2/src/inc/Face.h +++ b/gfx/graphite2/src/inc/Face.h @@ -34,6 +34,7 @@ of the License or (at your option) any later version. #include "inc/FeatureMap.h" #include "inc/TtfUtil.h" #include "inc/Silf.h" +#include "inc/Error.h" namespace graphite2 { @@ -42,6 +43,7 @@ class FileFace; class GlyphCache; class NameTable; class json; +class Font; using TtfUtil::Tag; @@ -55,7 +57,7 @@ class Face Face& operator=(const Face&); public: - class Table; + class Table; static float default_glyph_advance(const void* face_ptr, gr_uint16 glyphid); Face(const void* appFaceHandle/*non-NULL*/, const gr_face_ops & ops); @@ -88,6 +90,12 @@ public: uint16 getGlyphMetric(uint16 gid, uint8 metric) const; uint16 findPseudo(uint32 uid) const; + // Errors + unsigned int error() const { return m_error; } + bool error(Error e) { m_error = e.error(); return false; } + unsigned int error_context() const { return m_error; } + void error_context(unsigned int errcntxt) { m_errcntxt = errcntxt; } + CLASS_NEW_DELETE; private: SillMap m_Sill; @@ -98,12 +106,18 @@ private: mutable Cmap * m_cmap; // cmap cache if available mutable NameTable * m_pNames; mutable json * m_logger; + unsigned int m_error; + unsigned int m_errcntxt; protected: Silf * m_silfs; // silf subtables. uint16 m_numSilf; // num silf subtables in the silf table private: uint16 m_ascent, m_descent; +#ifdef GRAPHITE2_TELEMETRY +public: + mutable telemetry tele; +#endif }; @@ -135,7 +149,7 @@ const FeatureRef *Face::feature(uint16 index) const inline const GlyphCache & Face::glyphs() const { - return *m_pGlyphFaceCache; + return *m_pGlyphFaceCache; } inline @@ -157,10 +171,15 @@ class Face::Table const Face * _f; mutable const byte * _p; uint32 _sz; + bool _compressed; + + Error decompress(); + + void releaseBuffers(); public: Table() throw(); - Table(const Face & face, const Tag n) throw(); + Table(const Face & face, const Tag n, uint32 version=0xffffffff) throw(); Table(const Table & rhs) throw(); ~Table() throw(); @@ -172,34 +191,33 @@ public: inline Face::Table::Table() throw() -: _f(0), _p(0), _sz(0) +: _f(0), _p(0), _sz(0), _compressed(false) { } inline Face::Table::Table(const Table & rhs) throw() -: _f(rhs._f), _p(rhs._p), _sz(rhs._sz) +: _f(rhs._f), _p(rhs._p), _sz(rhs._sz), _compressed(rhs._compressed) { - rhs._p = 0; + rhs._p = 0; } inline Face::Table::~Table() throw() { - if (_p && _f->m_ops.release_table) - (*_f->m_ops.release_table)(_f->m_appFaceHandle, _p); + releaseBuffers(); } inline Face::Table::operator const byte * () const throw() { - return _p; + return _p; } inline -size_t Face::Table::size() const throw() +size_t Face::Table::size() const throw() { - return _sz; + return _sz; } } // namespace graphite2 diff --git a/gfx/graphite2/src/inc/FeatureMap.h b/gfx/graphite2/src/inc/FeatureMap.h index dc5175c30..fc845f6e1 100644 --- a/gfx/graphite2/src/inc/FeatureMap.h +++ b/gfx/graphite2/src/inc/FeatureMap.h @@ -52,11 +52,11 @@ private: class FeatureRef { - typedef uint32 chunk_t; - static const uint8 SIZEOF_CHUNK = sizeof(chunk_t)*8; + typedef uint32 chunk_t; + static const uint8 SIZEOF_CHUNK = sizeof(chunk_t)*8; public: - FeatureRef() : m_nameValues(0) {} + FeatureRef(); FeatureRef(const Face & face, unsigned short & bits_offset, uint32 max_val, uint32 name, uint16 uiName, uint16 flags, FeatureSetting *settings, uint16 num_set) throw(); @@ -64,8 +64,8 @@ public: bool applyValToFeature(uint32 val, Features& pDest) const; //defined in GrFaceImp.h void maskFeature(Features & pDest) const { - if (m_index < pDest.size()) //defensive - pDest[m_index] |= m_mask; + if (m_index < pDest.size()) //defensive + pDest[m_index] |= m_mask; } uint32 getFeatureVal(const Features& feats) const; //defined in GrFaceImp.h @@ -83,22 +83,32 @@ public: private: FeatureRef(const FeatureRef & rhs); - const Face * m_pFace; //not NULL + const Face * m_pFace; //not NULL FeatureSetting * m_nameValues; // array of name table ids for feature values chunk_t m_mask, // bit mask to get the value from the vector - m_max; // max value the value can take - uint32 m_id; // feature identifier/name - uint16 m_nameid, // Name table id for feature name - m_flags, // feature flags (unused at the moment but read from the font) - m_numSet; // number of values (number of entries in m_nameValues) - byte m_bits, // how many bits to shift the value into place - m_index; // index into the array to find the ulong to mask + m_max; // max value the value can take + uint32 m_id; // feature identifier/name + uint16 m_nameid, // Name table id for feature name + m_flags, // feature flags (unused at the moment but read from the font) + m_numSet; // number of values (number of entries in m_nameValues) + byte m_bits, // how many bits to shift the value into place + m_index; // index into the array to find the ulong to mask private: //unimplemented FeatureRef& operator=(const FeatureRef&); }; +inline +FeatureRef::FeatureRef() +: m_pFace(0), m_nameValues(0), + m_mask(0), m_max(0), m_id(0), + m_nameid(0), m_flags(0), m_numSet(0), + m_bits(0), m_index(0) +{ +} + + class NameAndFeatureRef { public: @@ -117,9 +127,8 @@ class NameAndFeatureRef class FeatureMap { public: - FeatureMap() : m_numFeats(0), m_feats(NULL), m_pNamedFeats(NULL), - m_defaultFeatures(NULL) {} - ~FeatureMap() { delete [] m_feats; delete[] m_pNamedFeats; delete m_defaultFeatures; } + FeatureMap() : m_numFeats(0), m_feats(NULL), m_pNamedFeats(NULL) {} + ~FeatureMap() { delete [] m_feats; delete[] m_pNamedFeats; } bool readFeats(const Face & face); const FeatureRef *findFeatureRef(uint32 name) const; @@ -135,9 +144,9 @@ friend class SillMap; FeatureRef *m_feats; NameAndFeatureRef* m_pNamedFeats; //owned - FeatureVal* m_defaultFeatures; //owned + FeatureVal m_defaultFeatures; //owned -private: //defensive on m_feats, m_pNamedFeats, and m_defaultFeatures +private: //defensive on m_feats, m_pNamedFeats, and m_defaultFeatures FeatureMap(const FeatureMap&); FeatureMap& operator=(const FeatureMap&); }; diff --git a/gfx/graphite2/src/inc/FeatureVal.h b/gfx/graphite2/src/inc/FeatureVal.h index 8c9298c76..5fbcf0820 100644 --- a/gfx/graphite2/src/inc/FeatureVal.h +++ b/gfx/graphite2/src/inc/FeatureVal.h @@ -56,7 +56,7 @@ public: CLASS_NEW_DELETE private: - friend class FeatureRef; //so that FeatureRefs can manipulate m_vec directly + friend class FeatureRef; //so that FeatureRefs can manipulate m_vec directly const FeatureMap* m_pMap; }; diff --git a/gfx/graphite2/src/inc/FileFace.h b/gfx/graphite2/src/inc/FileFace.h index b3a51ee4d..dfcf3e8d3 100644 --- a/gfx/graphite2/src/inc/FileFace.h +++ b/gfx/graphite2/src/inc/FileFace.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2012, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify diff --git a/gfx/graphite2/src/inc/Font.h b/gfx/graphite2/src/inc/Font.h index 650bc7cd8..bc7084050 100644 --- a/gfx/graphite2/src/inc/Font.h +++ b/gfx/graphite2/src/inc/Font.h @@ -32,7 +32,7 @@ of the License or (at your option) any later version. namespace graphite2 { -#define INVALID_ADVANCE -1e38f // can't be a static const because non-integral +#define INVALID_ADVANCE -1e38f // can't be a static const because non-integral class Font { @@ -69,13 +69,13 @@ float Font::advance(unsigned short glyphid) const inline float Font::scale() const { - return m_scale; + return m_scale; } inline bool Font::isHinted() const { - return m_hinted; + return m_hinted; } inline diff --git a/gfx/graphite2/src/inc/GlyphCache.h b/gfx/graphite2/src/inc/GlyphCache.h index 744571529..cf7c2469c 100644 --- a/gfx/graphite2/src/inc/GlyphCache.h +++ b/gfx/graphite2/src/inc/GlyphCache.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2012, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -29,19 +29,68 @@ of the License or (at your option) any later version. #include "graphite2/Font.h" #include "inc/Main.h" +#include "inc/Position.h" +#include "inc/GlyphFace.h" namespace graphite2 { class Face; class FeatureVal; -class GlyphFace; class Segment; + +struct SlantBox +{ + static const SlantBox empty; + +// SlantBox(float psi = 0., float pdi = 0., float psa = 0., float pda = 0.) : si(psi), di(pdi), sa(psa), da(pda) {}; + float width() const { return sa - si; } + float height() const { return da - di; } + float si; // min + float di; // min + float sa; // max + float da; // max +}; + + +struct BBox +{ + BBox(float pxi = 0, float pyi = 0., float pxa = 0., float pya = 0.) : xi(pxi), yi(pyi), xa(pxa), ya(pya) {}; + float width() const { return xa - xi; } + float height() const { return ya - yi; } + float xi; // min + float yi; // min + float xa; // max + float ya; // max +}; + + +class GlyphBox +{ + GlyphBox(const GlyphBox &); + GlyphBox & operator = (const GlyphBox &); + +public: + GlyphBox(uint8 numsubs, unsigned short bitmap, Rect *slanted) : _num(numsubs), _bitmap(bitmap), _slant(*slanted) {}; + + void addSubBox(int subindex, int boundary, Rect *val) { _subs[subindex * 2 + boundary] = *val; } + Rect &subVal(int subindex, int boundary) { return _subs[subindex * 2 + boundary]; } + const Rect &slant() const { return _slant; } + uint8 num() const { return _num; } + const Rect *subs() const { return _subs; } + +private: + uint8 _num; + unsigned short _bitmap; + Rect _slant; + Rect _subs[1]; +}; + class GlyphCache { class Loader; - GlyphCache(const GlyphCache&); + GlyphCache(const GlyphCache&); GlyphCache& operator=(const GlyphCache&); public: @@ -54,15 +103,26 @@ public: const GlyphFace *glyph(unsigned short glyphid) const; //result may be changed by subsequent call with a different glyphid const GlyphFace *glyphSafe(unsigned short glyphid) const; + float getBoundingMetric(unsigned short glyphid, uint8 metric) const; + uint8 numSubBounds(unsigned short glyphid) const; + float getSubBoundingMetric(unsigned short glyphid, uint8 subindex, uint8 metric) const; + const Rect & slant(unsigned short glyphid) const { return _boxes[glyphid] ? _boxes[glyphid]->slant() : _empty_slant_box; } + const SlantBox & getBoundingSlantBox(unsigned short glyphid) const; + const BBox & getBoundingBBox(unsigned short glyphid) const; + const SlantBox & getSubBoundingSlantBox(unsigned short glyphid, uint8 subindex) const; + const BBox & getSubBoundingBBox(unsigned short glyphid, uint8 subindex) const; + bool check(unsigned short glyphid) const; CLASS_NEW_DELETE; private: - const Loader * _glyph_loader; - const GlyphFace * * _glyphs; - unsigned short _num_glyphs, - _num_attrs, - _upem; + const Rect _empty_slant_box; + const Loader * _glyph_loader; + const GlyphFace * * _glyphs; + GlyphBox * * _boxes; + unsigned short _num_glyphs, + _num_attrs, + _upem; }; inline @@ -84,9 +144,79 @@ unsigned short GlyphCache::unitsPerEm() const throw() } inline +bool GlyphCache::check(unsigned short glyphid) const +{ + return _boxes && glyphid < _num_glyphs; +} + +inline const GlyphFace *GlyphCache::glyphSafe(unsigned short glyphid) const { return glyphid < _num_glyphs ? glyph(glyphid) : NULL; } +inline +float GlyphCache::getBoundingMetric(unsigned short glyphid, uint8 metric) const +{ + if (glyphid >= _num_glyphs) return 0.; + switch (metric) { + case 0: return (float)(glyph(glyphid)->theBBox().bl.x); // x_min + case 1: return (float)(glyph(glyphid)->theBBox().bl.y); // y_min + case 2: return (float)(glyph(glyphid)->theBBox().tr.x); // x_max + case 3: return (float)(glyph(glyphid)->theBBox().tr.y); // y_max + case 4: return (float)(_boxes[glyphid] ? _boxes[glyphid]->slant().bl.x : 0.f); // sum_min + case 5: return (float)(_boxes[glyphid] ? _boxes[glyphid]->slant().bl.y : 0.f); // diff_min + case 6: return (float)(_boxes[glyphid] ? _boxes[glyphid]->slant().tr.x : 0.f); // sum_max + case 7: return (float)(_boxes[glyphid] ? _boxes[glyphid]->slant().tr.y : 0.f); // diff_max + default: return 0.; + } +} + +inline const SlantBox &GlyphCache::getBoundingSlantBox(unsigned short glyphid) const +{ + return _boxes[glyphid] ? *(SlantBox *)(&(_boxes[glyphid]->slant())) : SlantBox::empty; +} + +inline const BBox &GlyphCache::getBoundingBBox(unsigned short glyphid) const +{ + return *(BBox *)(&(glyph(glyphid)->theBBox())); +} + +inline +float GlyphCache::getSubBoundingMetric(unsigned short glyphid, uint8 subindex, uint8 metric) const +{ + GlyphBox *b = _boxes[glyphid]; + if (b == NULL || subindex >= b->num()) return 0; + + switch (metric) { + case 0: return b->subVal(subindex, 0).bl.x; + case 1: return b->subVal(subindex, 0).bl.y; + case 2: return b->subVal(subindex, 0).tr.x; + case 3: return b->subVal(subindex, 0).tr.y; + case 4: return b->subVal(subindex, 1).bl.x; + case 5: return b->subVal(subindex, 1).bl.y; + case 6: return b->subVal(subindex, 1).tr.x; + case 7: return b->subVal(subindex, 1).tr.y; + default: return 0.; + } +} + +inline const SlantBox &GlyphCache::getSubBoundingSlantBox(unsigned short glyphid, uint8 subindex) const +{ + GlyphBox *b = _boxes[glyphid]; + return *(SlantBox *)(b->subs() + 2 * subindex + 1); +} + +inline const BBox &GlyphCache::getSubBoundingBBox(unsigned short glyphid, uint8 subindex) const +{ + GlyphBox *b = _boxes[glyphid]; + return *(BBox *)(b->subs() + 2 * subindex); +} + +inline +uint8 GlyphCache::numSubBounds(unsigned short glyphid) const +{ + return _boxes[glyphid] ? _boxes[glyphid]->num() : 0; +} + } // namespace graphite2 diff --git a/gfx/graphite2/src/inc/GlyphFaceCache.h b/gfx/graphite2/src/inc/GlyphFaceCache.h deleted file mode 100644 index 4afdfbe01..000000000 --- a/gfx/graphite2/src/inc/GlyphFaceCache.h +++ /dev/null @@ -1,103 +0,0 @@ -/* GRAPHITE2 LICENSING - - Copyright 2010, SIL International - All rights reserved. - - This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2.1 of License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should also have received a copy of the GNU Lesser General Public - License along with this library in the file named "LICENSE". - If not, write to the Free Software Foundation, 51 Franklin Street, - Suite 500, Boston, MA 02110-1335, USA or visit their web page on the - internet at http://www.fsf.org/licenses/lgpl.html. - -Alternatively, the contents of this file may be used under the terms of the -Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public -License, as published by the Free Software Foundation, either version 2 -of the License or (at your option) any later version. -*/ -#pragma once - -#include "inc/GlyphFace.h" -#include "graphite2/Font.h" - -namespace graphite2 { - -class Segment; -class Face; -class FeatureVal; - - -class GlyphFaceCacheHeader -{ -public: - bool initialize(const Face & face, const bool dumb_font); //return result indicates success. Do not use if failed. - unsigned short numGlyphs() const { return m_nGlyphs; } - unsigned short numAttrs() const { return m_numAttrs; } - -private: -friend class Face; -friend class GlyphFace; - const byte* m_pHead, - * m_pHHea, - * m_pHmtx, - * m_pGlat, - * m_pGloc, - * m_pGlyf, - * m_pLoca; - size_t m_lHmtx, - m_lGlat, - m_lGlyf, - m_lLoca; - - uint32 m_fGlat; - unsigned short m_numAttrs, // number of glyph attributes per glyph - m_nGlyphsWithGraphics, //i.e. boundary box and advance - m_nGlyphsWithAttributes, - m_nGlyphs; // number of glyphs in the font. Max of the above 2. - bool m_locFlagsUse32Bit; -}; - -class GlyphFaceCache : public GlyphFaceCacheHeader -{ -public: - static GlyphFaceCache* makeCache(const GlyphFaceCacheHeader& hdr /*, EGlyphCacheStrategy requested */); - - GlyphFaceCache(const GlyphFaceCacheHeader& hdr); - ~GlyphFaceCache(); - - const GlyphFace *glyphSafe(unsigned short glyphid) const { return glyphid<numGlyphs()?glyph(glyphid):NULL; } - uint16 glyphAttr(uint16 gid, uint8 gattr) const { if (gattr>=numAttrs()) return 0; const GlyphFace*p=glyphSafe(gid); return p?p->getAttr(gattr):0; } - - void * operator new (size_t s, const GlyphFaceCacheHeader& hdr) - { - return malloc(s + sizeof(GlyphFace*)*hdr.numGlyphs()); - } - // delete in case an exception is thrown in constructor - void operator delete(void* p, const GlyphFaceCacheHeader& ) throw() - { - free(p); - } - - const GlyphFace *glyph(unsigned short glyphid) const; //result may be changed by subsequent call with a different glyphid - void loadAllGlyphs(); - - CLASS_NEW_DELETE - -private: - GlyphFace **glyphPtrDirect(unsigned short glyphid) const { return (GlyphFace **)((const char*)(this)+sizeof(GlyphFaceCache)+sizeof(GlyphFace*)*glyphid);} - -private: //defensive - GlyphFaceCache(const GlyphFaceCache&); - GlyphFaceCache& operator=(const GlyphFaceCache&); -}; - -} // namespace graphite2 diff --git a/gfx/graphite2/src/inc/Intervals.h b/gfx/graphite2/src/inc/Intervals.h new file mode 100644 index 000000000..34fecce17 --- /dev/null +++ b/gfx/graphite2/src/inc/Intervals.h @@ -0,0 +1,234 @@ +/* GRAPHITE2 LICENSING + + Copyright 2010, SIL International + All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should also have received a copy of the GNU Lesser General Public + License along with this library in the file named "LICENSE". + If not, write to the Free Software Foundation, 51 Franklin Street, + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the + internet at http://www.fsf.org/licenses/lgpl.html. + +Alternatively, the contents of this file may be used under the terms of the +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public +License, as published by the Free Software Foundation, either version 2 +of the License or (at your option) any later version. +*/ +#pragma once + +#include <utility> + +#include "inc/Main.h" +#include "inc/List.h" +#include "inc/json.h" +#include "inc/Position.h" + +// An IntervalSet represents the possible movement of a given glyph in a given direction +// (horizontally, vertically, or diagonally). +// A vector is needed to represent disjoint ranges, eg, -300..-150, 20..200, 500..750. +// Each pair represents the min/max of a sub-range. + +namespace graphite2 { + +class Segment; + +enum zones_t {SD, XY}; + +class Zones +{ + struct Exclusion + { + template<zones_t O> + static Exclusion weighted(float xmin, float xmax, float f, float a0, + float m, float xi, float ai, float c, bool nega); + + float x, // x position + xm, // xmax position + c, // constant + sum(MiXi^2) + sm, // sum(Mi) + smx; // sum(MiXi) + bool open; + + Exclusion(float x, float w, float smi, float smxi, float c); + Exclusion & operator += (Exclusion const & rhs); + uint8 outcode(float p) const; + + Exclusion split_at(float p); + void left_trim(float p); + + bool track_cost(float & cost, float & x, float origin) const; + + private: + float test_position(float origin) const; + float cost(float x) const; + }; + + typedef Vector<Exclusion> exclusions; + + typedef exclusions::iterator iterator; + typedef Exclusion * pointer; + typedef Exclusion & reference; + typedef std::reverse_iterator<iterator> reverse_iterator; + +public: + typedef exclusions::const_iterator const_iterator; + typedef Exclusion const * const_pointer; + typedef Exclusion const & const_reference; + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; + +#if !defined GRAPHITE2_NTRACING + struct Debug + { + Exclusion _excl; + bool _isdel; + Vector<void *> _env; + + Debug(Exclusion *e, bool isdel, json *dbg) : _excl(*e), _isdel(isdel), _env(dbg->getenvs()) { }; + }; + + typedef Vector<Debug> debugs; + typedef debugs::const_iterator idebugs; + void addDebug(Exclusion *e); + void removeDebug(float pos, float posm); + void setdebug(json *dbgout) { _dbg = dbgout; } + idebugs dbgs_begin() const { return _dbgs.begin(); } + idebugs dbgs_end() const { return _dbgs.end(); } + void jsonDbgOut(Segment *seg) const; + Position position() const { return Position(_pos, _posm); } +#endif + + Zones(); + template<zones_t O> + void initialise(float xmin, float xmax, float margin_len, float margin_weight, float ao); + + void exclude(float xmin, float xmax); + void exclude_with_margins(float xmin, float xmax, int axis); + + template<zones_t O> + void weighted(float xmin, float xmax, float f, float a0, float mi, float xi, float ai, float c, bool nega); + void weightedAxis(int axis, float xmin, float xmax, float f, float a0, float mi, float xi, float ai, float c, bool nega); + + float closest( float origin, float &cost) const; + + const_iterator begin() const { return _exclusions.begin(); } + const_iterator end() const { return _exclusions.end(); } + +private: + exclusions _exclusions; +#if !defined GRAPHITE2_NTRACING + json * _dbg; + debugs _dbgs; +#endif + float _margin_len, + _margin_weight, + _pos, + _posm; + + void insert(Exclusion e); + void remove(float x, float xm); + const_iterator find_exclusion_under(float x) const; +}; + + +inline +Zones::Zones() +: _margin_len(0), _margin_weight(0), _pos(0), _posm(0) +{ +#if !defined GRAPHITE2_NTRACING + _dbg = 0; +#endif + _exclusions.reserve(8); +} + +inline +Zones::Exclusion::Exclusion(float x_, float xm_, float smi, float smxi, float c_) +: x(x_), xm(xm_), c(c_), sm(smi), smx(smxi), open(false) +{ } + +template<zones_t O> +inline +void Zones::initialise(float xmin, float xmax, float margin_len, + float margin_weight, float a0) +{ + _margin_len = margin_len; + _margin_weight = margin_weight; + _pos = xmin; + _posm = xmax; + _exclusions.clear(); + _exclusions.push_back(Exclusion::weighted<O>(xmin, xmax, 1, a0, 0, 0, 0, 0, false)); + _exclusions.front().open = true; +#if !defined GRAPHITE2_NTRACING + _dbgs.clear(); +#endif +} + +inline +void Zones::exclude(float xmin, float xmax) { + remove(xmin, xmax); +} + +template<zones_t O> +inline +void Zones::weighted(float xmin, float xmax, float f, float a0, + float m, float xi, float ai, float c, bool nega) { + insert(Exclusion::weighted<O>(xmin, xmax, f, a0, m, xi, ai, c, nega)); +} + +inline +void Zones::weightedAxis(int axis, float xmin, float xmax, float f, float a0, + float m, float xi, float ai, float c, bool nega) { + if (axis < 2) + weighted<XY>(xmin, xmax, f, a0, m, xi, ai, c, nega); + else + weighted<SD>(xmin, xmax, f, a0, m, xi, ai, c, nega); +} + +#if !defined GRAPHITE2_NTRACING +inline +void Zones::addDebug(Exclusion *e) { + if (_dbg) + _dbgs.push_back(Debug(e, false, _dbg)); +} + +inline +void Zones::removeDebug(float pos, float posm) { + if (_dbg) + { + Exclusion e(pos, posm, 0, 0, 0); + _dbgs.push_back(Debug(&e, true, _dbg)); + } +} +#endif + +template<> +inline +Zones::Exclusion Zones::Exclusion::weighted<XY>(float xmin, float xmax, float f, float a0, + float m, float xi, GR_MAYBE_UNUSED float ai, float c, GR_MAYBE_UNUSED bool nega) { + return Exclusion(xmin, xmax, + m + f, + m * xi, + m * xi * xi + f * a0 * a0 + c); +} + +template<> +inline +Zones::Exclusion Zones::Exclusion::weighted<SD>(float xmin, float xmax, float f, float a0, + float m, float xi, float ai,float c, bool nega) { + float xia = nega ? xi - ai : xi + ai; + return Exclusion(xmin, xmax, + 0.25f * (m + 2.f * f), + 0.25f * m * xia, + 0.25f * (m * xia * xia + 2.f * f * a0 * a0) + c); +} + +} // end of namespace graphite2 diff --git a/gfx/graphite2/src/inc/List.h b/gfx/graphite2/src/inc/List.h index f99036912..020560235 100644 --- a/gfx/graphite2/src/inc/List.h +++ b/gfx/graphite2/src/inc/List.h @@ -70,6 +70,7 @@ public: size_t capacity() const{ return m_end - m_first; } void reserve(size_t n); + void resize(size_t n, const T & v = T()); reference front() { assert(size() > 0); return *begin(); } const_reference front() const { assert(size() > 0); return *begin(); } @@ -82,7 +83,7 @@ public: void assign(size_t n, const T& u) { clear(); insert(begin(), n, u); } void assign(const_iterator first, const_iterator last) { clear(); insert(begin(), first, last); } - iterator insert(iterator p, const T & x) { p = _insert_default(p, 1); *p = x; return p; } + iterator insert(iterator p, const T & x) { p = _insert_default(p, 1); new (p) T(x); return p; } void insert(iterator p, size_t n, const T & x); void insert(iterator p, const_iterator first, const_iterator last); void pop_back() { assert(size() > 0); --m_last; } @@ -104,18 +105,27 @@ void Vector<T>::reserve(size_t n) { const ptrdiff_t sz = size(); m_first = static_cast<T*>(realloc(m_first, n*sizeof(T))); + if (!m_first) std::abort(); m_last = m_first + sz; m_end = m_first + n; } } +template <typename T> +inline +void Vector<T>::resize(size_t n, const T & v) { + const ptrdiff_t d = n-size(); + if (d < 0) erase(end()+d, end()); + else if (d > 0) insert(end(), d, v); +} + template<typename T> inline typename Vector<T>::iterator Vector<T>::_insert_default(iterator p, size_t n) { assert(begin() <= p && p <= end()); const ptrdiff_t i = p - begin(); - reserve((size() + n + 7) >> 3 << 3); + reserve(((size() + n + 7) >> 3) << 3); p = begin() + i; // Move tail if there is one if (p != end()) memmove(p + n, p, distance(p,end())*sizeof(T)); diff --git a/gfx/graphite2/src/inc/Machine.h b/gfx/graphite2/src/inc/Machine.h index f6af44329..4efb70cd9 100644 --- a/gfx/graphite2/src/inc/Machine.h +++ b/gfx/graphite2/src/inc/Machine.h @@ -110,7 +110,9 @@ enum opcode { PUSH_PROC_STATE, PUSH_VERSION, PUT_SUBS, PUT_SUBS2, PUT_SUBS3, PUT_GLYPH, PUSH_GLYPH_ATTR, PUSH_ATT_TO_GLYPH_ATTR, - MAX_OPCODE, + BITOR, BITAND, BITNOT, + BITSET, SET_FEAT, + MAX_OPCODE, // private opcodes for internal use only, comes after all other on disk opcodes TEMP_COPY = MAX_OPCODE }; @@ -147,8 +149,8 @@ public: CLASS_NEW_DELETE; SlotMap & slotMap() const throw(); - status_t status() const throw(); - operator bool () const throw(); + status_t status() const throw(); +// operator bool () const throw(); private: void check_final_stack(const stack_t * const sp); @@ -157,18 +159,18 @@ private: SlotMap & _map; stack_t _stack[STACK_MAX + 2*STACK_GUARD]; - status_t _status; + status_t _status; }; inline Machine::Machine(SlotMap & map) throw() : _map(map), _status(finished) { - // Initialise stack guard +1 entries as the stack pointer points to the - // current top of stack, hence the first push will never write entry 0. - // Initialising the guard space like this is unnecessary and is only - // done to keep valgrind happy during fuzz testing. Hopefully loop - // unrolling will flatten this. - for (size_t n = STACK_GUARD + 1; n; --n) _stack[n-1] = 0; + // Initialise stack guard +1 entries as the stack pointer points to the + // current top of stack, hence the first push will never write entry 0. + // Initialising the guard space like this is unnecessary and is only + // done to keep valgrind happy during fuzz testing. Hopefully loop + // unrolling will flatten this. + for (size_t n = STACK_GUARD + 1; n; --n) _stack[n-1] = 0; } inline SlotMap& Machine::slotMap() const throw() diff --git a/gfx/graphite2/src/inc/Main.h b/gfx/graphite2/src/inc/Main.h index 0f12e12fd..deeffcccd 100644 --- a/gfx/graphite2/src/inc/Main.h +++ b/gfx/graphite2/src/inc/Main.h @@ -44,19 +44,56 @@ typedef gr_int16 int16; typedef gr_int32 int32; typedef size_t uintptr; +#ifdef GRAPHITE2_TELEMETRY +struct telemetry +{ + class category; + + static size_t * _category; + static void set_category(size_t & t) throw() { _category = &t; } + static void stop() throw() { _category = 0; } + static void count_bytes(size_t n) throw() { if (_category) *_category += n; } + + size_t misc, + silf, + glyph, + code, + states, + starts, + transitions; + + telemetry() : misc(0), silf(0), glyph(0), code(0), states(0), starts(0), transitions(0) {} +}; + +class telemetry::category +{ + size_t * _prev; +public: + category(size_t & t) : _prev(_category) { _category = &t; } + ~category() { _category = _prev; } +}; + +#else struct telemetry {}; +#endif // typesafe wrapper around malloc for simple types // use free(pointer) to deallocate template <typename T> T * gralloc(size_t n) { - return reinterpret_cast<T*>(malloc(sizeof(T) * n)); +#ifdef GRAPHITE2_TELEMETRY + telemetry::count_bytes(sizeof(T) * n); +#endif + return static_cast<T*>(malloc(sizeof(T) * n)); } template <typename T> T * grzeroalloc(size_t n) { - return reinterpret_cast<T*>(calloc(n, sizeof(T))); +#ifdef GRAPHITE2_TELEMETRY + telemetry::count_bytes(sizeof(T) * n); +#endif + return static_cast<T*>(calloc(n, sizeof(T))); } template <typename T> @@ -83,8 +120,27 @@ inline T max(const T a, const T b) void operator delete[] (void * p)throw() { free(p); } \ void operator delete[] (void *, void *) throw() {} -#ifdef __GNUC__ +#if defined(__GNUC__) || defined(__clang__) #define GR_MAYBE_UNUSED __attribute__((unused)) #else #define GR_MAYBE_UNUSED #endif + +#if defined(__clang__) && __cplusplus >= 201103L + /* clang's fallthrough annotations are only available starting in C++11. */ + #define GR_FALLTHROUGH [[clang::fallthrough]] +#elif defined(_MSC_VER) + /* + * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis): + * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx + */ + #include <sal.h> + #define GR_FALLTHROUGH __fallthrough +#else + #define GR_FALLTHROUGH /* fallthrough */ +#endif + +#ifdef _MSC_VER +#pragma warning(disable: 4800) +#pragma warning(disable: 4355) +#endif diff --git a/gfx/graphite2/src/inc/Pass.h b/gfx/graphite2/src/inc/Pass.h index 5fea65a28..c8f1aa20b 100644 --- a/gfx/graphite2/src/inc/Pass.h +++ b/gfx/graphite2/src/inc/Pass.h @@ -38,6 +38,12 @@ struct Rule; struct RuleEntry; struct State; class FiniteStateMachine; +class Error; +class ShiftCollider; +class KernCollider; +class json; + +enum passtype; class Pass { @@ -45,38 +51,51 @@ public: Pass(); ~Pass(); - bool readPass(const byte * pPass, size_t pass_length, size_t subtable_base, const Face & face); - void runGraphite(vm::Machine & m, FiniteStateMachine & fsm) const; + bool readPass(const byte * pPass, size_t pass_length, size_t subtable_base, Face & face, + enum passtype pt, uint32 version, Error &e); + bool runGraphite(vm::Machine & m, FiniteStateMachine & fsm, bool reverse) const; void init(Silf *silf) { m_silf = silf; } - byte spaceContextuals() const { return (m_flags & 0x0E) >> 1; } + byte collisionLoops() const { return m_numCollRuns; } + bool reverseDir() const { return m_isReverseDir; } CLASS_NEW_DELETE private: - void findNDoRule(Slot* & iSlot, vm::Machine &, FiniteStateMachine& fsm) const; - int doAction(const vm::Machine::Code* codeptr, Slot * & slot_out, vm::Machine &) const; - bool testPassConstraint(vm::Machine & m) const; - bool testConstraint(const Rule & r, vm::Machine &) const; - bool readRules(const byte * rule_map, const size_t num_entries, + void findNDoRule(Slot* & iSlot, vm::Machine &, FiniteStateMachine& fsm) const; + int doAction(const vm::Machine::Code* codeptr, Slot * & slot_out, vm::Machine &) const; + bool testPassConstraint(vm::Machine & m) const; + bool testConstraint(const Rule & r, vm::Machine &) const; + bool readRules(const byte * rule_map, const size_t num_entries, const byte *precontext, const uint16 * sort_key, const uint16 * o_constraint, const byte *constraint_data, const uint16 * o_action, const byte * action_data, - const Face &); - bool readStates(const byte * starts, const byte * states, const byte * o_rule_map, const Face &); - bool readRanges(const byte * ranges, size_t num_ranges); - uint16 glyphToCol(const uint16 gid) const; - bool runFSM(FiniteStateMachine & fsm, Slot * slot) const; - void dumpRuleEventConsidered(const FiniteStateMachine & fsm, const RuleEntry & re) const; - void dumpRuleEventOutput(const FiniteStateMachine & fsm, const Rule & r, Slot * os) const; - void adjustSlot(int delta, Slot * & slot_out, SlotMap &) const; - const Silf* m_silf; - uint16 * m_cols; - Rule * m_rules; // rules - RuleEntry * m_ruleMap; - uint16 * m_startStates; // prectxt length - uint16 * m_transitions; - State * m_states; - - byte m_flags; + Face &, enum passtype pt, Error &e); + bool readStates(const byte * starts, const byte * states, const byte * o_rule_map, Face &, Error &e); + bool readRanges(const byte * ranges, size_t num_ranges, Error &e); + uint16 glyphToCol(const uint16 gid) const; + bool runFSM(FiniteStateMachine & fsm, Slot * slot) const; + void dumpRuleEventConsidered(const FiniteStateMachine & fsm, const RuleEntry & re) const; + void dumpRuleEventOutput(const FiniteStateMachine & fsm, vm::Machine & m, const Rule & r, Slot * os) const; + void adjustSlot(int delta, Slot * & slot_out, SlotMap &) const; + bool collisionShift(Segment *seg, int dir, json * const dbgout) const; + bool collisionKern(Segment *seg, int dir, json * const dbgout) const; + bool collisionFinish(Segment *seg, GR_MAYBE_UNUSED json * const dbgout) const; + bool resolveCollisions(Segment *seg, Slot *slot, Slot *start, ShiftCollider &coll, bool isRev, + int dir, bool &moved, bool &hasCol, json * const dbgout) const; + float resolveKern(Segment *seg, Slot *slot, Slot *start, KernCollider &coll, int dir, + float &ymin, float &ymax, json *const dbgout) const; + + const Silf * m_silf; + uint16 * m_cols; + Rule * m_rules; // rules + RuleEntry * m_ruleMap; + uint16 * m_startStates; // prectxt length + uint16 * m_transitions; + State * m_states; + vm::Machine::Code * m_codes; + byte * m_progs; + + byte m_numCollRuns; + byte m_kernColls; byte m_iMaxLoop; uint16 m_numGlyphs; uint16 m_numRules; @@ -87,9 +106,11 @@ private: uint16 m_numColumns; byte m_minPreCtxt; byte m_maxPreCtxt; + byte m_colThreshold; + bool m_isReverseDir; vm::Machine::Code m_cPConstraint; -private: //defensive +private: //defensive Pass(const Pass&); Pass& operator=(const Pass&); }; diff --git a/gfx/graphite2/src/inc/Position.h b/gfx/graphite2/src/inc/Position.h index c6f1b7556..97bc1cdf7 100644 --- a/gfx/graphite2/src/inc/Position.h +++ b/gfx/graphite2/src/inc/Position.h @@ -50,7 +50,16 @@ public : Rect(const Position& botLeft, const Position& topRight): bl(botLeft), tr(topRight) {} Rect widen(const Rect& other) { return Rect(Position(bl.x > other.bl.x ? other.bl.x : bl.x, bl.y > other.bl.y ? other.bl.y : bl.y), Position(tr.x > other.tr.x ? tr.x : other.tr.x, tr.y > other.tr.y ? tr.y : other.tr.y)); } Rect operator + (const Position &a) const { return Rect(Position(bl.x + a.x, bl.y + a.y), Position(tr.x + a.x, tr.y + a.y)); } + Rect operator - (const Position &a) const { return Rect(Position(bl.x - a.x, bl.y - a.y), Position(tr.x - a.x, tr.y - a.y)); } Rect operator * (float m) const { return Rect(Position(bl.x, bl.y) * m, Position(tr.x, tr.y) * m); } + float width() const { return tr.x - bl.x; } + float height() const { return tr.y - bl.y; } + + bool hitTest(Rect &other); + + // returns Position(overlapx, overlapy) where overlap<0 if overlapping else positive) + Position overlap(Position &offset, Rect &other, Position &otherOffset); + //Position constrainedAvoid(Position &offset, Rect &box, Rect &sdbox, Position &other, Rect &obox, Rect &osdbox); Position bl; Position tr; diff --git a/gfx/graphite2/src/inc/Rule.h b/gfx/graphite2/src/inc/Rule.h index 491d985e3..36c8d89a6 100644 --- a/gfx/graphite2/src/inc/Rule.h +++ b/gfx/graphite2/src/inc/Rule.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2011, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -41,8 +41,8 @@ struct Rule { uint16 rule_idx; #endif - Rule() : constraint(0), action(0), sort(0), preContext(0) {} - ~Rule(); + Rule(); + ~Rule() {} CLASS_NEW_DELETE; @@ -51,10 +51,16 @@ private: Rule & operator = (const Rule &); }; -inline Rule::~Rule() +inline +Rule::Rule() +: constraint(0), + action(0), + sort(0), + preContext(0) { - delete constraint; - delete action; +#ifndef NDEBUG + rule_idx = 0; +#endif } @@ -96,7 +102,7 @@ class SlotMap { public: enum {MAX_SLOTS=64}; - SlotMap(Segment & seg); + SlotMap(Segment & seg, uint8 direction); Slot * * begin(); Slot * * end(); @@ -107,12 +113,14 @@ public: Slot * const & operator[](int n) const; Slot * & operator [] (int); void pushSlot(Slot * const slot); - void collectGarbage(); + void collectGarbage(Slot *& aSlot); Slot * highwater() { return m_highwater; } void highwater(Slot *s) { m_highwater = s; m_highpassed = false; } - bool highpassed() const { return m_highpassed; } - void highpassed(bool v) { m_highpassed = v; } + bool highpassed() const { return m_highpassed; } + void highpassed(bool v) { m_highpassed = v; } + + uint8 dir() const { return m_dir; } Segment & segment; private: @@ -120,7 +128,8 @@ private: unsigned short m_size; unsigned short m_precontext; Slot * m_highwater; - bool m_highpassed; + uint8 m_dir; + bool m_highpassed; }; @@ -233,8 +242,8 @@ void FiniteStateMachine::Rules::accumulate_rules(const State &state) } inline -SlotMap::SlotMap(Segment & seg) -: segment(seg), m_size(0), m_precontext(0), m_highwater(0), m_highpassed(false) +SlotMap::SlotMap(Segment & seg, uint8 direction) +: segment(seg), m_size(0), m_precontext(0), m_highwater(0), m_dir(direction), m_highpassed(false) { m_slot_map[0] = 0; } diff --git a/gfx/graphite2/src/inc/SegCache.h b/gfx/graphite2/src/inc/SegCache.h index ac0996658..b360f7c93 100644 --- a/gfx/graphite2/src/inc/SegCache.h +++ b/gfx/graphite2/src/inc/SegCache.h @@ -93,9 +93,7 @@ public: if (m_entryCounts[length-1] + 1u > listSize) { if (m_entryCounts[length-1] == 0) - { listSize = 1; - } else { // the problem comes when you get incremental numeric ids in a large doc @@ -105,9 +103,7 @@ public: } newEntries = gralloc<SegCacheEntry>(listSize); if (!newEntries) - { return NULL; - } } uint16 insertPos = 0; @@ -267,7 +263,7 @@ private: unsigned long long minAccessCount, unsigned long long oldAccessTime); uint16 m_prefixLength; - uint16 m_maxCachedSegLength; +// uint16 m_maxCachedSegLength; size_t m_segmentCount; SegCachePrefixArray m_prefixes; Features m_features; diff --git a/gfx/graphite2/src/inc/Segment.h b/gfx/graphite2/src/inc/Segment.h index 8ddeeb810..bbeecef8b 100644 --- a/gfx/graphite2/src/inc/Segment.h +++ b/gfx/graphite2/src/inc/Segment.h @@ -35,10 +35,10 @@ of the License or (at your option) any later version. #include "inc/FeatureVal.h" #include "inc/GlyphCache.h" #include "inc/GlyphFace.h" -//#include "inc/Silf.h" #include "inc/Slot.h" #include "inc/Position.h" #include "inc/List.h" +#include "inc/Collider.h" #define MAX_SEG_GROWTH_FACTOR 256 @@ -46,7 +46,7 @@ namespace graphite2 { typedef Vector<Features> FeatureList; typedef Vector<Slot *> SlotRope; -typedef Vector<int16 *> AttributeRope; +typedef Vector<int16 *> AttributeRope; typedef Vector<SlotJustify *> JustifyRope; #ifndef GRAPHITE2_NSEGCACHE @@ -85,6 +85,12 @@ class Segment Segment& operator=(const Segment&); public: + + enum { + SEG_INITCOLLISIONS = 1, + SEG_HASCOLLISIONS = 2 + }; + unsigned int slotCount() const { return m_numGlyphs; } //one slot per glyph void extendLength(int num) { m_numGlyphs += num; } Position advance() const { return m_advance; } @@ -94,7 +100,6 @@ public: unsigned int charInfoCount() const { return m_numCharinfo; } const CharInfo *charinfo(unsigned int index) const { return index < m_numCharinfo ? m_charinfo + index : NULL; } CharInfo *charinfo(unsigned int index) { return index < m_numCharinfo ? m_charinfo + index : NULL; } - int8 dir() const { return m_dir; } Segment(unsigned int numchars, const Face* face, uint32 script, int dir); ~Segment(); @@ -106,6 +111,8 @@ public: Slot * endSlot, const Slot * srcSlot, const size_t numGlyphs); #endif + uint8 flags() const { return m_flags; } + void flags(uint8 f) { m_flags = f; } Slot *first() { return m_first; } void first(Slot *p) { m_first = p; } Slot *last() { return m_last; } @@ -115,18 +122,27 @@ public: void freeSlot(Slot *); SlotJustify *newJustify(); void freeJustify(SlotJustify *aJustify); - Position positionSlots(const Font *font, Slot *first=0, Slot *last=0); - void associateChars(); + Position positionSlots(const Font *font=0, Slot *first=0, Slot *last=0, bool isRtl = false, bool isFinal = true); + void associateChars(int offset, int num); void linkClusters(Slot *first, Slot *last); uint16 getClassGlyph(uint16 cid, uint16 offset) const { return m_silf->getClassGlyph(cid, offset); } uint16 findClassIndex(uint16 cid, uint16 gid) const { return m_silf->findClassIndex(cid, gid); } int addFeatures(const Features& feats) { m_feats.push_back(feats); return m_feats.size() - 1; } uint32 getFeature(int index, uint8 findex) const { const FeatureRef* pFR=m_face->theSill().theFeatureMap().featureRef(findex); if (!pFR) return 0; else return pFR->getFeatureVal(m_feats[index]); } + void setFeature(int index, uint8 findex, uint32 val) { + const FeatureRef* pFR=m_face->theSill().theFeatureMap().featureRef(findex); + if (pFR) + { + if (val > pFR->maxVal()) val = pFR->maxVal(); + pFR->applyValToFeature(val, m_feats[index]); + } } + int8 dir() const { return m_dir; } void dir(int8 val) { m_dir = val; } + bool currdir() const { return ((m_dir >> 6) ^ m_dir) & 1; } unsigned int passBits() const { return m_passBits; } void mergePassBits(const unsigned int val) { m_passBits &= val; } int16 glyphAttr(uint16 gid, uint16 gattr) const { const GlyphFace * p = m_face->glyphs().glyphSafe(gid); return p ? p->attrs()[gattr] : 0; } - int32 getGlyphMetric(Slot *iSlot, uint8 metric, uint8 attrLevel) const; + int32 getGlyphMetric(Slot *iSlot, uint8 metric, uint8 attrLevel, bool rtl) const; float glyphAdvance(uint16 gid) const { return m_face->glyphs().glyph(gid)->theAdvance().x; } const Rect &theGlyphBBoxTemporary(uint16 gid) const { return m_face->glyphs().glyph(gid)->theBBox(); } //warning value may become invalid when another glyph is accessed Slot *findRoot(Slot *is) const { return is->attachedTo() ? findRoot(is->attachedTo()) : is; } @@ -134,23 +150,27 @@ public: int defaultOriginal() const { return m_defaultOriginal; } const Face * getFace() const { return m_face; } const Features & getFeatures(unsigned int /*charIndex*/) { assert(m_feats.size() == 1); return m_feats[0]; } - void bidiPass(uint8 aBidi, int paradir, uint8 aMirror); + void bidiPass(int paradir, uint8 aMirror); + int8 getSlotBidiClass(Slot *s) const; + void doMirror(uint16 aMirror); Slot *addLineEnd(Slot *nSlot); void delLineEnd(Slot *s); bool hasJustification() const { return m_justifies.size() != 0; } + void reverseSlots(); bool isWhitespace(const int cid) const; + bool hasCollisionInfo() const { return (m_flags & SEG_HASCOLLISIONS); } + SlotCollision *collisionInfo(const Slot *s) const { return hasCollisionInfo() ? reinterpret_cast<SlotCollision *>(s->userAttrs() + m_silf->numUser()) : 0; } CLASS_NEW_DELETE public: //only used by: GrSegment* makeAndInitialize(const GrFont *font, const GrFace *face, uint32 script, const FeaturesHandle& pFeats/*must not be IsNull*/, encform enc, const void* pStart, size_t nChars, int dir); - void read_text(const Face *face, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void*pStart, size_t nChars); - void prepare_pos(const Font *font); - void finalise(const Font *font); + bool read_text(const Face *face, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void*pStart, size_t nChars); + void finalise(const Font *font, bool reverse=false); float justify(Slot *pSlot, const Font *font, float width, enum justFlags flags, Slot *pFirst, Slot *pLast); + bool initCollisions(); private: - Rect m_bbox; // ink box of the segment Position m_advance; // whole segment advance SlotRope m_slots; // Vector of slot buffers AttributeRope m_userAttrs; // Vector of userAttrs buffers @@ -169,26 +189,37 @@ private: m_passBits; // if bit set then skip pass int m_defaultOriginal; // number of whitespace chars in the string int8 m_dir; + uint8 m_flags; // General purpose flags }; - +inline +int8 Segment::getSlotBidiClass(Slot *s) const +{ + int8 res = s->getBidiClass(); + if (res != -1) return res; + res = int8(glyphAttr(s->gid(), m_silf->aBidi())); + s->setBidiClass(res); + return res; +} inline -void Segment::finalise(const Font *font) +void Segment::finalise(const Font *font, bool reverse) { - if (!m_first) return; + if (!m_first) return; - m_advance = positionSlots(font); - associateChars(); + m_advance = positionSlots(font, m_first, m_last, m_silf->dir(), true); + //associateChars(0, m_numCharinfo); + if (reverse && currdir() != (m_dir & 1)) + reverseSlots(); linkClusters(m_first, m_last); } inline -int32 Segment::getGlyphMetric(Slot *iSlot, uint8 metric, uint8 attrLevel) const { +int32 Segment::getGlyphMetric(Slot *iSlot, uint8 metric, uint8 attrLevel, bool rtl) const { if (attrLevel > 0) { Slot *is = findRoot(iSlot); - return is->clusterMetric(this, metric, attrLevel); + return is->clusterMetric(this, metric, attrLevel, rtl); } else return m_face->getGlyphMetric(iSlot->gid(), metric); @@ -211,62 +242,6 @@ bool Segment::isWhitespace(const int cid) const + (cid == 0x3000)) != 0; } -//inline -//bool Segment::isWhitespace(const int cid) const -//{ -// switch (cid >> 8) -// { -// case 0x00: -// switch (cid) -// { -// case 0x09: -// case 0x0A: -// case 0x0B: -// case 0x0C: -// case 0x0D: -// case 0x20: -// return true; -// default: -// break; -// } -// break; -// case 0x16: -// return cid == 0x1680; -// break; -// case 0x18: -// return cid == 0x180E; -// break; -// case 0x20: -// switch (cid) -// { -// case 0x00: -// case 0x01: -// case 0x02: -// case 0x03: -// case 0x04: -// case 0x05: -// case 0x06: -// case 0x07: -// case 0x08: -// case 0x09: -// case 0x0A: -// case 0x28: -// case 0x29: -// case 0x2F: -// case 0x5F: -// return true -// default: -// break; -// } -// break; -// case 0x30: -// return cid == 0x3000; -// break; -// } -// -// return false; -//} - } // namespace graphite2 struct gr_segment : public graphite2::Segment {}; diff --git a/gfx/graphite2/src/inc/Silf.h b/gfx/graphite2/src/inc/Silf.h index 526d9ba30..8e49f70c9 100644 --- a/gfx/graphite2/src/inc/Silf.h +++ b/gfx/graphite2/src/inc/Silf.h @@ -36,6 +36,7 @@ class Face; class Segment; class FeatureVal; class VMScratch; +class Error; class Pseudo { @@ -73,8 +74,8 @@ public: Silf() throw(); ~Silf() throw(); - bool readGraphite(const byte * const pSilf, size_t lSilf, const Face &face, uint32 version); - bool runGraphite(Segment *seg, uint8 firstPass=0, uint8 lastPass=0) const; + bool readGraphite(const byte * const pSilf, size_t lSilf, Face &face, uint32 version); + bool runGraphite(Segment *seg, uint8 firstPass=0, uint8 lastPass=0, int dobidi = 0) const; uint16 findClassIndex(uint16 cid, uint16 gid) const; uint16 getClassGlyph(uint16 cid, unsigned int index) const; uint16 findPseudo(uint32 uid) const; @@ -83,6 +84,8 @@ public: uint8 aBreak() const { return m_aBreak; } uint8 aMirror() const {return m_aMirror; } uint8 aPassBits() const { return m_aPassBits; } + uint8 aBidi() const { return m_aBidi; } + uint8 aCollision() const { return m_aCollision; } uint8 substitutionPass() const { return m_sPass; } uint8 positionPass() const { return m_pPass; } uint8 justificationPass() const { return m_jPass; } @@ -91,6 +94,7 @@ public: uint8 maxCompPerLig() const { return m_iMaxComp; } uint16 numClasses() const { return m_nClass; } byte flags() const { return m_flags; } + byte dir() const { return m_dir; } uint8 numJustLevels() const { return m_numJusts; } Justinfo *justAttrs() const { return m_justs; } uint16 endLineGlyphid() const { return m_gEndLine; } @@ -99,8 +103,8 @@ public: CLASS_NEW_DELETE; private: - size_t readClassMap(const byte *p, size_t data_len, uint32 version); - template<typename T> inline uint32 readClassOffsets(const byte *&p, size_t data_len); + size_t readClassMap(const byte *p, size_t data_len, uint32 version, Error &e); + template<typename T> inline uint32 readClassOffsets(const byte *&p, size_t data_len, Error &e); Pass * m_passes; Pseudo * m_pseudos; @@ -110,15 +114,12 @@ private: uint8 m_numPasses; uint8 m_numJusts; uint8 m_sPass, m_pPass, m_jPass, m_bPass, - m_flags; - - uint8 m_aPseudo, m_aBreak, m_aUser, m_aBidi, m_aMirror, m_aPassBits, - m_iMaxComp; - uint16 m_aLig, - m_numPseudo, - m_nClass, - m_nLinear, - m_gEndLine; + m_flags, m_dir; + + uint8 m_aPseudo, m_aBreak, m_aUser, m_aBidi, m_aMirror, m_aPassBits, + m_iMaxComp, m_aCollision; + uint16 m_aLig, m_numPseudo, m_nClass, m_nLinear, + m_gEndLine; gr_faceinfo m_silfinfo; void releaseBuffers() throw(); diff --git a/gfx/graphite2/src/inc/Slot.h b/gfx/graphite2/src/inc/Slot.h index 3f0c47388..a6e05491c 100644 --- a/gfx/graphite2/src/inc/Slot.h +++ b/gfx/graphite2/src/inc/Slot.h @@ -32,15 +32,13 @@ of the License or (at your option) any later version. #include "inc/Font.h" #include "inc/Position.h" - - namespace graphite2 { typedef gr_attrCode attrCode; class GlyphFace; -class Segment; class SegCacheEntry; +class Segment; struct SlotJustify { @@ -60,29 +58,30 @@ public: class Slot { - enum Flag - { - DELETED = 1, - INSERTED = 2, - COPIED = 4, - POSITIONED = 8, - ATTACHED = 16 - }; + enum Flag + { + DELETED = 1, + INSERTED = 2, + COPIED = 4, + POSITIONED = 8, + ATTACHED = 16 + }; public: - struct iterator; + struct iterator; unsigned short gid() const { return m_glyphid; } Position origin() const { return m_position; } float advance() const { return m_advance.x; } + void advance(Position &val) { m_advance = val; } Position advancePos() const { return m_advance; } int before() const { return m_before; } int after() const { return m_after; } uint32 index() const { return m_index; } void index(uint32 val) { m_index = val; } - Slot(); - void set(const Slot & slot, int charOffset, size_t numUserAttr, size_t justLevels); + Slot(int16 *m_userAttr = NULL); + void set(const Slot & slot, int charOffset, size_t numUserAttr, size_t justLevels, size_t numChars); Slot *next() const { return m_next; } void next(Slot *s) { m_next = s; } Slot *prev() const { return m_prev; } @@ -98,7 +97,7 @@ public: void after(int ind) { m_after = ind; } bool isBase() const { return (!m_parent); } void update(int numSlots, int numCharInfo, Position &relpos); - Position finalise(const Segment* seg, const Font* font, Position & base, Rect & bbox, uint8 attrLevel, float & clusterMin); + Position finalise(const Segment* seg, const Font* font, Position & base, Rect & bbox, uint8 attrLevel, float & clusterMin, bool rtl, bool isFinal); bool isDeleted() const { return (m_flags & DELETED) ? true : false; } void markDeleted(bool state) { if (state) m_flags |= DELETED; else m_flags &= ~DELETED; } bool isCopied() const { return (m_flags & COPIED) ? true : false; } @@ -108,8 +107,9 @@ public: bool isInsertBefore() const { return !(m_flags & INSERTED); } uint8 getBidiLevel() const { return m_bidiLevel; } void setBidiLevel(uint8 level) { m_bidiLevel = level; } - uint8 getBidiClass() const { return m_bidiCls; } - void setBidiClass(uint8 cls) { m_bidiCls = cls; } + int8 getBidiClass(const Segment *seg); + int8 getBidiClass() const { return m_bidiCls; } + void setBidiClass(int8 cls) { m_bidiCls = cls; } int16 *userAttrs() const { return m_userAttr; } void userAttrs(int16 *p) { m_userAttr = p; } void markInsertBefore(bool state) { if (!state) m_flags |= INSERTED; else m_flags &= ~INSERTED; } @@ -122,16 +122,20 @@ public: Slot *attachedTo() const { return m_parent; } Position attachOffset() const { return m_attach - m_with; } Slot* firstChild() const { return m_child; } + void firstChild(Slot *ap) { m_child = ap; } bool child(Slot *ap); Slot* nextSibling() const { return m_sibling; } + void nextSibling(Slot *ap) { m_sibling = ap; } bool sibling(Slot *ap); bool removeChild(Slot *ap); bool removeSibling(Slot *ap); - int32 clusterMetric(const Segment* seg, uint8 metric, uint8 attrLevel); + int32 clusterMetric(const Segment* seg, uint8 metric, uint8 attrLevel, bool rtl); void positionShift(Position a) { m_position += a; } void floodShift(Position adj); float just() const { return m_just; } void just(float j) { m_just = j; } + Slot *nextInCluster(const Slot *s) const; + bool isChildOf(const Slot *base) const; CLASS_NEW_DELETE @@ -140,7 +144,7 @@ private: Slot *m_prev; unsigned short m_glyphid; // glyph id uint16 m_realglyphid; - uint32 m_original; // charinfo that originated this slot (e.g. for feature values) + uint32 m_original; // charinfo that originated this slot (e.g. for feature values) uint32 m_before; // charinfo index of before association uint32 m_after; // charinfo index of after association uint32 m_index; // slot index given to this slot during finalising @@ -151,11 +155,11 @@ private: Position m_shift; // .shift slot attribute Position m_advance; // .advance slot attribute Position m_attach; // attachment point on us - Position m_with; // attachment point position on parent + Position m_with; // attachment point position on parent float m_just; // Justification inserted space uint8 m_flags; // holds bit flags byte m_attLevel; // attachment level - byte m_bidiCls; // bidirectional class + int8 m_bidiCls; // bidirectional class byte m_bidiLevel; // bidirectional level int16 *m_userAttr; // pointer to user attributes SlotJustify *m_justs; // pointer to justification parameters diff --git a/gfx/graphite2/src/inc/Sparse.h b/gfx/graphite2/src/inc/Sparse.h index cd685bdcb..4a8e3fa7d 100644 --- a/gfx/graphite2/src/inc/Sparse.h +++ b/gfx/graphite2/src/inc/Sparse.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2011, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -43,51 +43,52 @@ class sparse public: typedef uint16 key_type; typedef uint16 mapped_type; - typedef std::pair<const key_type, mapped_type> value_type; + typedef std::pair<const key_type, mapped_type> value_type; private: - typedef unsigned long mask_t; + typedef unsigned long mask_t; - static const unsigned char SIZEOF_CHUNK = (sizeof(mask_t) - sizeof(key_type))*8; + static const unsigned char SIZEOF_CHUNK = (sizeof(mask_t) - sizeof(key_type))*8; - struct chunk - { - mask_t mask:SIZEOF_CHUNK; - key_type offset; - }; + struct chunk + { + mask_t mask:SIZEOF_CHUNK; + key_type offset; + }; - sparse(const sparse &); - sparse & operator = (const sparse &); + static const chunk empty_chunk; + sparse(const sparse &); + sparse & operator = (const sparse &); public: - template<typename I> - sparse(I first, const I last); - sparse() throw(); - ~sparse() throw(); + template<typename I> + sparse(I first, const I last); + sparse() throw(); + ~sparse() throw(); - operator bool () const throw(); - mapped_type operator [] (const key_type k) const throw(); + operator bool () const throw(); + mapped_type operator [] (const key_type k) const throw(); - size_t capacity() const throw(); - size_t size() const throw(); + size_t capacity() const throw(); + size_t size() const throw(); - size_t _sizeof() const throw(); - - CLASS_NEW_DELETE; + size_t _sizeof() const throw(); + + CLASS_NEW_DELETE; private: - union { - chunk * map; - mapped_type * values; - } m_array; - key_type m_nchunks; + union { + chunk * map; + mapped_type * values; + } m_array; + key_type m_nchunks; }; inline sparse::sparse() throw() : m_nchunks(0) { - m_array.map = 0; + m_array.map = const_cast<graphite2::sparse::chunk *>(&empty_chunk); } @@ -97,49 +98,62 @@ sparse::sparse(I attr, const I last) { m_array.map = 0; - // Find the maximum extent of the key space. - size_t n_values=0; - for (I i = attr; i != last; ++i, ++n_values) - { - const key_type k = (*i).first / SIZEOF_CHUNK; - if (k >= m_nchunks) m_nchunks = k+1; - } - if (m_nchunks == 0) return; - - m_array.values = grzeroalloc<mapped_type>((m_nchunks*sizeof(chunk) + sizeof(mapped_type)-1) - / sizeof(mapped_type) - + n_values); - - if (m_array.values == 0) - { - free(m_array.values); m_array.map=0; - return; - } - - chunk * ci = m_array.map; - ci->offset = (m_nchunks*sizeof(chunk) + sizeof(mapped_type)-1)/sizeof(mapped_type); - mapped_type * vi = m_array.values + ci->offset; - for (; attr != last; ++attr, ++vi) - { - const typename std::iterator_traits<I>::value_type v = *attr; - chunk * const ci_ = m_array.map + v.first/SIZEOF_CHUNK; - - if (ci != ci_) - { - ci = ci_; - ci->offset = vi - m_array.values; - } - - ci->mask |= 1UL << (SIZEOF_CHUNK - 1 - (v.first % SIZEOF_CHUNK)); - *vi = v.second; - } + // Find the maximum extent of the key space. + size_t n_values=0; + long lastkey = -1; + for (I i = attr; i != last; ++i, ++n_values) + { + const typename std::iterator_traits<I>::value_type v = *i; + if (v.second == 0) { --n_values; continue; } + if (v.first <= lastkey) { m_nchunks = 0; return; } + + lastkey = v.first; + const key_type k = v.first / SIZEOF_CHUNK; + if (k >= m_nchunks) m_nchunks = k+1; + } + if (m_nchunks == 0) + { + m_array.map=const_cast<graphite2::sparse::chunk *>(&empty_chunk); + return; + } + + m_array.values = grzeroalloc<mapped_type>((m_nchunks*sizeof(chunk) + sizeof(mapped_type)-1) + / sizeof(mapped_type) + + n_values); + + if (m_array.values == 0) + { + free(m_array.values); m_array.map=0; + return; + } + + // coverity[forward_null : FALSE] Since m_array is union and m_array.values is not NULL + chunk * ci = m_array.map; + ci->offset = (m_nchunks*sizeof(chunk) + sizeof(mapped_type)-1)/sizeof(mapped_type); + mapped_type * vi = m_array.values + ci->offset; + for (; attr != last; ++attr, ++vi) + { + const typename std::iterator_traits<I>::value_type v = *attr; + if (v.second == 0) { --vi; continue; } + + chunk * const ci_ = m_array.map + v.first/SIZEOF_CHUNK; + + if (ci != ci_) + { + ci = ci_; + ci->offset = vi - m_array.values; + } + + ci->mask |= 1UL << (SIZEOF_CHUNK - 1 - (v.first % SIZEOF_CHUNK)); + *vi = v.second; + } } inline sparse::operator bool () const throw() { - return m_array.map != 0; + return m_array.map != 0; } inline @@ -151,7 +165,7 @@ size_t sparse::size() const throw() inline size_t sparse::_sizeof() const throw() { - return sizeof(sparse) + capacity()*sizeof(mapped_type) + m_nchunks*sizeof(chunk); + return sizeof(sparse) + capacity()*sizeof(mapped_type) + m_nchunks*sizeof(chunk); } } // namespace graphite2 diff --git a/gfx/graphite2/src/inc/TtfTypes.h b/gfx/graphite2/src/inc/TtfTypes.h index 49ffa3922..ea20e5775 100644 --- a/gfx/graphite2/src/inc/TtfTypes.h +++ b/gfx/graphite2/src/inc/TtfTypes.h @@ -37,380 +37,380 @@ Provides types required to represent the TTF basic types. //********************************************************************************************** -// Include files +// Include files //********************************************************************************************** namespace graphite2 { namespace TtfUtil { //********************************************************************************************** -// Forward declarations +// Forward declarations //********************************************************************************************** //********************************************************************************************** -// Type declarations +// Type declarations //********************************************************************************************** -typedef unsigned char uint8; -typedef uint8 byte; -typedef signed char int8; -typedef unsigned short uint16; -typedef short int16; -typedef unsigned int uint32; -typedef int int32; - -typedef int16 short_frac; -typedef int32 fixed; -typedef int16 fword; -typedef uint16 ufword; -typedef int16 f2dot14; -typedef uint32 long_date_time[2]; +typedef unsigned char uint8; +typedef uint8 byte; +typedef signed char int8; +typedef unsigned short uint16; +typedef short int16; +typedef unsigned int uint32; +typedef int int32; + +typedef int16 short_frac; +typedef int32 fixed; +typedef int16 fword; +typedef uint16 ufword; +typedef int16 f2dot14; +typedef uint32 long_date_time[2]; //********************************************************************************************** -// Constants and enum types +// Constants and enum types //**********************************************************************************************/ enum { - OneFix = 1<<16 + OneFix = 1<<16 }; //********************************************************************************************** -// Table declarations +// Table declarations //********************************************************************************************** namespace Sfnt { -#pragma pack(1) // We need this or the structure members aren't alligned - // correctly. Fortunately this form of pragma is supposed - // to be recongnised by VS C++ too (at least according to - // MSDN). - - struct OffsetSubTable - { - uint32 scaler_type; - uint16 num_tables, - search_range, - entry_selector, - range_shift; - struct Entry - { - uint32 tag, - checksum, - offset, - length; - } table_directory[1]; - - enum ScalerType - { - TrueTypeMac = 0x74727565U, - TrueTypeWin = 0x00010000U, - Type1 = 0x74797031U - }; - }; - - - - - struct CharacterCodeMap - { - uint16 version, - num_subtables; - struct - { - uint16 platform_id, - platform_specific_id; - uint32 offset; - } encoding[1]; - }; - - struct CmapSubTable - { - uint16 format, - length, - language; - }; - - struct CmapSubTableFormat4 : CmapSubTable - { - uint16 seg_count_x2, - search_range, - entry_selector, - range_shift, - end_code[1]; - // There are arrarys after this which need their - // start positions calculated since end_code is - // seg_count uint16s long. - }; - - struct CmapSubTableFormat12 - { - fixed format; - uint32 length, - language, - num_groups; - struct - { - uint32 start_char_code, - end_char_code, - start_glyph_id; - } group[1]; - }; - - - - struct FontHeader - { - fixed version, - font_revision; - uint32 check_sum_adjustment, - magic_number; - uint16 flags, - units_per_em; - long_date_time created, - modified; - fword x_min, - y_min, - x_max, - y_max; - uint16 mac_style, - lowest_rec_ppem; - int16 font_direction_hint, - index_to_loc_format, - glyph_data_format; - enum - { - MagicNumber = 0x5F0F3CF5, - GlypDataFormat = 0 - }; - enum {ShortIndexLocFormat, LongIndexLocFormat}; - }; - - - - - struct PostScriptGlyphName - { - fixed format, - italic_angle; - fword underline_position, - underline_thickness; - uint32 is_fixed_pitch, - min_mem_type42, - max_mem_type42, - min_mem_type1, - max_mem_type1; - enum - { - Format1 = 0x10000, - Format2 = 0x20000, - Format25 = 0x28000, - Format3 = 0x30000, - Format4 = 0x40000 - }; - }; - - struct PostScriptGlyphName2 : PostScriptGlyphName - { - uint16 number_of_glyphs, - glyph_name_index[1]; - }; - - struct PostScriptGlyphName25 : PostScriptGlyphName - { - uint16 number_of_glyphs; - int8 offset[1]; - }; - - struct PostScriptGlyphName3 : PostScriptGlyphName {}; - - struct PostScriptGlyphName4 : PostScriptGlyphName - { - uint16 glyph_to_char_map[1]; - }; - - - struct HorizontalHeader - { - fixed version; - fword ascent, - descent, - line_gap; - ufword advance_width_max; - fword min_left_side_bearing, - max_left_side_bearing, - x_max_element; - int16 caret_slope_rise, - caret_slope_run; - fword caret_offset; - int16 reserved[4], - metric_data_format; - uint16 num_long_hor_metrics; - }; - - struct MaximumProfile - { - fixed version; - uint16 num_glyphs, - max_points, - max_contours, - max_component_points, - max_component_contours, - max_zones, - max_twilight_points, - max_storage, - max_function_defs, - max_instruction_defs, - max_stack_elements, - max_size_of_instructions, - max_component_elements, - max_component_depth; - }; - - - typedef byte Panose[10]; - - struct Compatibility0 - { - uint16 version; - int16 x_avg_char_width; - uint16 weight_class, - width_class; - int16 fs_type, - y_subscript_x_size, - y_subscript_y_size, - y_subscript_x_offset, - y_subscript_y_offset, - y_superscript_x_size, - y_superscript_y_size, - y_superscript_x_offset, - y_superscript_y_offset, - y_strikeout_size, - y_strikeout_position, - family_class; - Panose panose; - uint32 unicode_range[4]; - int8 ach_vend_id[4]; - uint16 fs_selection, - fs_first_char_index, - fs_last_char_index, // Acording to Apple's spec this is where v0 should end - typo_ascender, - typo_descender, - type_linegap, - win_ascent, - win_descent; - - enum - { - Italic =0x01, - Underscore=0x02, - Negative =0x04, - Outlined =0x08, - StrikeOut =0x10, - Bold =0x20 - }; - }; - - struct Compatibility1 : Compatibility0 - { - uint32 codepage_range[2]; - }; - - struct Compatibility2 : Compatibility1 - { - int16 x_height, - cap_height; - uint16 default_char, - break_char, - max_context; - }; - - struct Compatibility3 : Compatibility2 {}; - - typedef Compatibility3 Compatibility; - - - struct NameRecord - { - uint16 platform_id, - platform_specific_id, - language_id, - name_id, - length, - offset; - enum {Unicode, Mactintosh, Reserved, Microsoft}; - enum - { - Copyright, Family, Subfamily, UniqueSubfamily, - Fullname, Version, PostScript - }; - }; - - struct LangTagRecord - { - uint16 length, - offset; - }; - - struct FontNames - { - uint16 format, - count, - string_offset; - NameRecord name_record[1]; - }; - - - struct HorizontalMetric - { - uint16 advance_width; - int16 left_side_bearing; - }; - - - struct Glyph - { - int16 number_of_contours; - fword x_min, - y_min, - x_max, - y_max; - }; - - struct SimpleGlyph : Glyph - { - uint16 end_pts_of_contours[1]; - enum - { - OnCurve = 0x01, - XShort = 0x02, - YShort = 0x04, - Repeat = 0x08, - XIsSame = 0x10, - XIsPos = 0x10, - YIsSame = 0x20, - YIsPos = 0x20 - }; - }; - - struct CompoundGlyph : Glyph - { - uint16 flags, - glyph_index; - enum - { - Arg1Arg2Words = 0x01, - ArgsAreXYValues = 0x02, - RoundXYToGrid = 0x04, - HaveScale = 0x08, - MoreComponents = 0x20, - HaveXAndYScale = 0x40, - HaveTwoByTwo = 0x80, - HaveInstructions = 0x100, - UseMyMetrics = 0x200, - OverlapCompund = 0x400, - ScaledOffset = 0x800, - UnscaledOffset = 0x1000 - }; - }; +#pragma pack(1) // We need this or the structure members aren't alligned + // correctly. Fortunately this form of pragma is supposed + // to be recongnised by VS C++ too (at least according to + // MSDN). + + struct OffsetSubTable + { + uint32 scaler_type; + uint16 num_tables, + search_range, + entry_selector, + range_shift; + struct Entry + { + uint32 tag, + checksum, + offset, + length; + } table_directory[1]; + + enum ScalerType + { + TrueTypeMac = 0x74727565U, + TrueTypeWin = 0x00010000U, + Type1 = 0x74797031U + }; + }; + + + + + struct CharacterCodeMap + { + uint16 version, + num_subtables; + struct + { + uint16 platform_id, + platform_specific_id; + uint32 offset; + } encoding[1]; + }; + + struct CmapSubTable + { + uint16 format, + length, + language; + }; + + struct CmapSubTableFormat4 : CmapSubTable + { + uint16 seg_count_x2, + search_range, + entry_selector, + range_shift, + end_code[1]; + // There are arrarys after this which need their + // start positions calculated since end_code is + // seg_count uint16s long. + }; + + struct CmapSubTableFormat12 + { + fixed format; + uint32 length, + language, + num_groups; + struct + { + uint32 start_char_code, + end_char_code, + start_glyph_id; + } group[1]; + }; + + + + struct FontHeader + { + fixed version, + font_revision; + uint32 check_sum_adjustment, + magic_number; + uint16 flags, + units_per_em; + long_date_time created, + modified; + fword x_min, + y_min, + x_max, + y_max; + uint16 mac_style, + lowest_rec_ppem; + int16 font_direction_hint, + index_to_loc_format, + glyph_data_format; + enum + { + MagicNumber = 0x5F0F3CF5, + GlypDataFormat = 0 + }; + enum {ShortIndexLocFormat, LongIndexLocFormat}; + }; + + + + + struct PostScriptGlyphName + { + fixed format, + italic_angle; + fword underline_position, + underline_thickness; + uint32 is_fixed_pitch, + min_mem_type42, + max_mem_type42, + min_mem_type1, + max_mem_type1; + enum + { + Format1 = 0x10000, + Format2 = 0x20000, + Format25 = 0x28000, + Format3 = 0x30000, + Format4 = 0x40000 + }; + }; + + struct PostScriptGlyphName2 : PostScriptGlyphName + { + uint16 number_of_glyphs, + glyph_name_index[1]; + }; + + struct PostScriptGlyphName25 : PostScriptGlyphName + { + uint16 number_of_glyphs; + int8 offset[1]; + }; + + struct PostScriptGlyphName3 : PostScriptGlyphName {}; + + struct PostScriptGlyphName4 : PostScriptGlyphName + { + uint16 glyph_to_char_map[1]; + }; + + + struct HorizontalHeader + { + fixed version; + fword ascent, + descent, + line_gap; + ufword advance_width_max; + fword min_left_side_bearing, + max_left_side_bearing, + x_max_element; + int16 caret_slope_rise, + caret_slope_run; + fword caret_offset; + int16 reserved[4], + metric_data_format; + uint16 num_long_hor_metrics; + }; + + struct MaximumProfile + { + fixed version; + uint16 num_glyphs, + max_points, + max_contours, + max_component_points, + max_component_contours, + max_zones, + max_twilight_points, + max_storage, + max_function_defs, + max_instruction_defs, + max_stack_elements, + max_size_of_instructions, + max_component_elements, + max_component_depth; + }; + + + typedef byte Panose[10]; + + struct Compatibility0 + { + uint16 version; + int16 x_avg_char_width; + uint16 weight_class, + width_class; + int16 fs_type, + y_subscript_x_size, + y_subscript_y_size, + y_subscript_x_offset, + y_subscript_y_offset, + y_superscript_x_size, + y_superscript_y_size, + y_superscript_x_offset, + y_superscript_y_offset, + y_strikeout_size, + y_strikeout_position, + family_class; + Panose panose; + uint32 unicode_range[4]; + int8 ach_vend_id[4]; + uint16 fs_selection, + fs_first_char_index, + fs_last_char_index, // Acording to Apple's spec this is where v0 should end + typo_ascender, + typo_descender, + type_linegap, + win_ascent, + win_descent; + + enum + { + Italic =0x01, + Underscore=0x02, + Negative =0x04, + Outlined =0x08, + StrikeOut =0x10, + Bold =0x20 + }; + }; + + struct Compatibility1 : Compatibility0 + { + uint32 codepage_range[2]; + }; + + struct Compatibility2 : Compatibility1 + { + int16 x_height, + cap_height; + uint16 default_char, + break_char, + max_context; + }; + + struct Compatibility3 : Compatibility2 {}; + + typedef Compatibility3 Compatibility; + + + struct NameRecord + { + uint16 platform_id, + platform_specific_id, + language_id, + name_id, + length, + offset; + enum {Unicode, Mactintosh, Reserved, Microsoft}; + enum + { + Copyright, Family, Subfamily, UniqueSubfamily, + Fullname, Version, PostScript + }; + }; + + struct LangTagRecord + { + uint16 length, + offset; + }; + + struct FontNames + { + uint16 format, + count, + string_offset; + NameRecord name_record[1]; + }; + + + struct HorizontalMetric + { + uint16 advance_width; + int16 left_side_bearing; + }; + + + struct Glyph + { + int16 number_of_contours; + fword x_min, + y_min, + x_max, + y_max; + }; + + struct SimpleGlyph : Glyph + { + uint16 end_pts_of_contours[1]; + enum + { + OnCurve = 0x01, + XShort = 0x02, + YShort = 0x04, + Repeat = 0x08, + XIsSame = 0x10, + XIsPos = 0x10, + YIsSame = 0x20, + YIsPos = 0x20 + }; + }; + + struct CompoundGlyph : Glyph + { + uint16 flags, + glyph_index; + enum + { + Arg1Arg2Words = 0x01, + ArgsAreXYValues = 0x02, + RoundXYToGrid = 0x04, + HaveScale = 0x08, + MoreComponents = 0x20, + HaveXAndYScale = 0x40, + HaveTwoByTwo = 0x80, + HaveInstructions = 0x100, + UseMyMetrics = 0x200, + OverlapCompund = 0x400, + ScaledOffset = 0x800, + UnscaledOffset = 0x1000 + }; + }; #pragma pack() } // end of namespace Sfnt diff --git a/gfx/graphite2/src/inc/TtfUtil.h b/gfx/graphite2/src/inc/TtfUtil.h index 7fafb69cf..43ef2daa9 100644 --- a/gfx/graphite2/src/inc/TtfUtil.h +++ b/gfx/graphite2/src/inc/TtfUtil.h @@ -51,155 +51,155 @@ typedef unsigned short gid16; // Enumeration used to specify a table in a TTF file class Tag { - unsigned long _v; + unsigned long _v; public: - Tag(const char n[5]) throw() : _v(TTF_TAG(n[0],n[1],n[2],n[3])) {} - Tag(const unsigned long tag) throw() : _v(tag) {} - - operator unsigned long () const throw () { return _v; } - - enum - { - Feat = TTF_TAG('F','e','a','t'), - Glat = TTF_TAG('G','l','a','t'), - Gloc = TTF_TAG('G','l','o','c'), - Sile = TTF_TAG('S','i','l','e'), - Silf = TTF_TAG('S','i','l','f'), - Sill = TTF_TAG('S','i','l','l'), - cmap = TTF_TAG('c','m','a','p'), - cvt = TTF_TAG('c','v','t',' '), - cryp = TTF_TAG('c','r','y','p'), - head = TTF_TAG('h','e','a','d'), - fpgm = TTF_TAG('f','p','g','m'), - gdir = TTF_TAG('g','d','i','r'), - glyf = TTF_TAG('g','l','y','f'), - hdmx = TTF_TAG('h','d','m','x'), - hhea = TTF_TAG('h','h','e','a'), - hmtx = TTF_TAG('h','m','t','x'), - loca = TTF_TAG('l','o','c','a'), - kern = TTF_TAG('k','e','r','n'), - LTSH = TTF_TAG('L','T','S','H'), - maxp = TTF_TAG('m','a','x','p'), - name = TTF_TAG('n','a','m','e'), - OS_2 = TTF_TAG('O','S','/','2'), - post = TTF_TAG('p','o','s','t'), - prep = TTF_TAG('p','r','e','p') - }; + Tag(const char n[5]) throw() : _v(TTF_TAG(n[0],n[1],n[2],n[3])) {} + Tag(const unsigned long tag) throw() : _v(tag) {} + + operator unsigned long () const throw () { return _v; } + + enum + { + Feat = TTF_TAG('F','e','a','t'), + Glat = TTF_TAG('G','l','a','t'), + Gloc = TTF_TAG('G','l','o','c'), + Sile = TTF_TAG('S','i','l','e'), + Silf = TTF_TAG('S','i','l','f'), + Sill = TTF_TAG('S','i','l','l'), + cmap = TTF_TAG('c','m','a','p'), + cvt = TTF_TAG('c','v','t',' '), + cryp = TTF_TAG('c','r','y','p'), + head = TTF_TAG('h','e','a','d'), + fpgm = TTF_TAG('f','p','g','m'), + gdir = TTF_TAG('g','d','i','r'), + glyf = TTF_TAG('g','l','y','f'), + hdmx = TTF_TAG('h','d','m','x'), + hhea = TTF_TAG('h','h','e','a'), + hmtx = TTF_TAG('h','m','t','x'), + loca = TTF_TAG('l','o','c','a'), + kern = TTF_TAG('k','e','r','n'), + LTSH = TTF_TAG('L','T','S','H'), + maxp = TTF_TAG('m','a','x','p'), + name = TTF_TAG('n','a','m','e'), + OS_2 = TTF_TAG('O','S','/','2'), + post = TTF_TAG('p','o','s','t'), + prep = TTF_TAG('p','r','e','p') + }; }; /*---------------------------------------------------------------------------------------------- - Class providing utility methods to parse a TrueType font file (TTF). - Callling application handles all file input and memory allocation. - Assumes minimal knowledge of TTF file format. + Class providing utility methods to parse a TrueType font file (TTF). + Callling application handles all file input and memory allocation. + Assumes minimal knowledge of TTF file format. ----------------------------------------------------------------------------------------------*/ - ////////////////////////////////// tools to find & check TTF tables - bool GetHeaderInfo(size_t & lOffset, size_t & lSize); - bool CheckHeader(const void * pHdr); - bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize); - bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir, - size_t & lOffset, size_t & lSize); - bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize); - - ////////////////////////////////// simple font wide info - size_t GlyphCount(const void * pMaxp); + ////////////////////////////////// tools to find & check TTF tables + bool GetHeaderInfo(size_t & lOffset, size_t & lSize); + bool CheckHeader(const void * pHdr); + bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize); + bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir, + size_t & lOffset, size_t & lSize); + bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize); + + ////////////////////////////////// simple font wide info + size_t GlyphCount(const void * pMaxp); #ifdef ALL_TTFUTILS - size_t MaxCompositeComponentCount(const void * pMaxp); - size_t MaxCompositeLevelCount(const void * pMaxp); - size_t LocaGlyphCount(size_t lLocaSize, const void * pHead); // throw (std::domain_error); + size_t MaxCompositeComponentCount(const void * pMaxp); + size_t MaxCompositeLevelCount(const void * pMaxp); + size_t LocaGlyphCount(size_t lLocaSize, const void * pHead); // throw (std::domain_error); #endif - int DesignUnits(const void * pHead); + int DesignUnits(const void * pHead); #ifdef ALL_TTFUTILS - int HeadTableCheckSum(const void * pHead); - void HeadTableCreateTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD); - void HeadTableModifyTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD); - bool IsItalic(const void * pHead); - int FontAscent(const void * pOs2); - int FontDescent(const void * pOs2); - bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic); - bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize); - bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize); - bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize); - bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize); - int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp, - const char * pPostName); + int HeadTableCheckSum(const void * pHead); + void HeadTableCreateTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD); + void HeadTableModifyTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD); + bool IsItalic(const void * pHead); + int FontAscent(const void * pOs2); + int FontDescent(const void * pOs2); + bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic); + bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize); + bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize); + bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize); + bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize); + int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp, + const char * pPostName); #endif - ////////////////////////////////// utility methods helpful for name table - bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId, - int nLangId, int nNameId, size_t & lOffset, size_t & lSize); - //size_t NameTableLength(const byte * pTable); + ////////////////////////////////// utility methods helpful for name table + bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId, + int nLangId, int nNameId, size_t & lOffset, size_t & lSize); + //size_t NameTableLength(const byte * pTable); #ifdef ALL_TTFUTILS - int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId, - int *nameIdList, int cNameIds, short *langIdList); - void SwapWString(void * pWStr, size_t nSize = 0); // throw (std::invalid_argument); + int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId, + int *nameIdList, int cNameIds, short *langIdList); + void SwapWString(void * pWStr, size_t nSize = 0); // throw (std::invalid_argument); #endif - ////////////////////////////////// cmap lookup tools - const void * FindCmapSubtable(const void * pCmap, int nPlatformId = 3, - int nEncodingId = 1, size_t length = 0); - bool CheckCmapSubtable4(const void * pCmap31); - gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey = 0); - unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId, - int * pRangeKey = 0); - bool CheckCmapSubtable12(const void *pCmap310); - gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey = 0); - unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId, - int * pRangeKey = 0); - - ///////////////////////////////// horizontal metric data for a glyph - bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize, - const void * pHhea, int & nLsb, unsigned int & nAdvWid); - - ////////////////////////////////// primitives for loca and glyf lookup - size_t LocaLookup(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, - const void * pHead); // throw (std::out_of_range); - void * GlyfLookup(const void * pGlyf, size_t lGlyfOffset, size_t lTableLen); - - ////////////////////////////////// primitves for simple glyph data - bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin, - int & xMax, int & yMax); + ////////////////////////////////// cmap lookup tools + const void * FindCmapSubtable(const void * pCmap, int nPlatformId = 3, + int nEncodingId = 1, size_t length = 0); + bool CheckCmapSubtable4(const void * pCmap31, size_t table_len /*, unsigned int maxgid*/); + gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey = 0); + unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId, + int * pRangeKey = 0); + bool CheckCmapSubtable12(const void *pCmap310, size_t table_len /*, unsigned int maxgid*/); + gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey = 0); + unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId, + int * pRangeKey = 0); + + ///////////////////////////////// horizontal metric data for a glyph + bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize, + const void * pHhea, int & nLsb, unsigned int & nAdvWid); + + ////////////////////////////////// primitives for loca and glyf lookup + size_t LocaLookup(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, + const void * pHead); // throw (std::out_of_range); + void * GlyfLookup(const void * pGlyf, size_t lGlyfOffset, size_t lTableLen); + + ////////////////////////////////// primitves for simple glyph data + bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin, + int & xMax, int & yMax); #ifdef ALL_TTFUTILS - int GlyfContourCount(const void * pSimpleGlyf); - bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint, - int cnPointsTotal, size_t & cnPoints); - bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY, - char * prgbFlag, int cnPointsTotal, int & cnPoints); - - // primitive to find the glyph ids in a composite glyph - bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId, - size_t cnCompIdTotal, size_t & cnCompId); - // primitive to find the placement data for a component in a composite glyph - bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId, - bool fOffset, int & a, int & b); - // primitive to find the transform data for a component in a composite glyph - bool GetComponentTransform(const void * pSimpleGlyf, int nCompId, - float & flt11, float & flt12, float & flt21, float & flt22, bool & fTransOffset); + int GlyfContourCount(const void * pSimpleGlyf); + bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint, + int cnPointsTotal, size_t & cnPoints); + bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY, + char * prgbFlag, int cnPointsTotal, int & cnPoints); + + // primitive to find the glyph ids in a composite glyph + bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId, + size_t cnCompIdTotal, size_t & cnCompId); + // primitive to find the placement data for a component in a composite glyph + bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId, + bool fOffset, int & a, int & b); + // primitive to find the transform data for a component in a composite glyph + bool GetComponentTransform(const void * pSimpleGlyf, int nCompId, + float & flt11, float & flt12, float & flt21, float & flt22, bool & fTransOffset); #endif - ////////////////////////////////// operate on composite or simple glyph (auto glyf lookup) - void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead); // primitive used by below methods + ////////////////////////////////// operate on composite or simple glyph (auto glyf lookup) + void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca, + size_t lGlyfSize, size_t lLocaSize, const void * pHead); // primitive used by below methods #ifdef ALL_TTFUTILS - // below are primary user methods for handling glyf data - bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead); - bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead); - - bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, size_t lGlyfSize, size_t lLocaSize, - const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax); - bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void *pHead, size_t & cnContours); - bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead, int * prgnContourEndPoint, size_t cnPoints); - bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, - size_t lGlyfSize, size_t lLocaSize, const void * pHead, const int * prgnContourEndPoint, size_t cnEndPoints, - int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints); - - // utitily method used by high-level GlyfPoints - bool SimplifyFlags(char * prgbFlags, int cnPoints); - bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints); + // below are primary user methods for handling glyf data + bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead); + bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca, + size_t lGlyfSize, size_t lLocaSize, const void * pHead); + + bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, size_t lGlyfSize, size_t lLocaSize, + const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax); + bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca, + size_t lGlyfSize, size_t lLocaSize, const void *pHead, size_t & cnContours); + bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, + size_t lGlyfSize, size_t lLocaSize, const void * pHead, int * prgnContourEndPoint, size_t cnPoints); + bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, + size_t lGlyfSize, size_t lLocaSize, const void * pHead, const int * prgnContourEndPoint, size_t cnEndPoints, + int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints); + + // utitily method used by high-level GlyfPoints + bool SimplifyFlags(char * prgbFlags, int cnPoints); + bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints); #endif } // end of namespace TtfUtil diff --git a/gfx/graphite2/src/inc/UtfCodec.h b/gfx/graphite2/src/inc/UtfCodec.h index 1a49179aa..66384ae18 100644 --- a/gfx/graphite2/src/inc/UtfCodec.h +++ b/gfx/graphite2/src/inc/UtfCodec.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2011, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -31,15 +31,15 @@ of the License or (at your option) any later version. namespace graphite2 { -typedef uint32 uchar_t; +typedef uint32 uchar_t; template <int N> struct _utf_codec { - typedef uchar_t codeunit_t; + typedef uchar_t codeunit_t; - static void put(codeunit_t * cp, const uchar_t , int8 & len) throw(); - static uchar_t get(const codeunit_t * cp, int8 & len) throw(); + static void put(codeunit_t * cp, const uchar_t , int8 & len) throw(); + static uchar_t get(const codeunit_t * cp, int8 & len) throw(); }; @@ -47,22 +47,22 @@ template <> struct _utf_codec<32> { private: - static const uchar_t limit = 0x110000; + static const uchar_t limit = 0x110000; public: - typedef uint32 codeunit_t; - - inline - static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw() - { - *cp = usv; l = 1; - } - - inline - static uchar_t get(const codeunit_t * cp, int8 & l) throw() - { - if (cp[0] < limit) { l = 1; return cp[0]; } - else { l = -1; return 0xFFFD; } - } + typedef uint32 codeunit_t; + + inline + static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw() + { + *cp = usv; l = 1; + } + + inline + static uchar_t get(const codeunit_t * cp, int8 & l) throw() + { + if (cp[0] < limit) { l = 1; return cp[0]; } + else { l = -1; return 0xFFFD; } + } }; @@ -70,35 +70,35 @@ template <> struct _utf_codec<16> { private: - static const int32 lead_offset = 0xD800 - (0x10000 >> 10); - static const int32 surrogate_offset = 0x10000 - (0xD800 << 10) - 0xDC00; + static const int32 lead_offset = 0xD800 - (0x10000 >> 10); + static const int32 surrogate_offset = 0x10000 - (0xD800 << 10) - 0xDC00; public: - typedef uint16 codeunit_t; - - inline - static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw() - { - if (usv < 0x10000) { l = 1; cp[0] = codeunit_t(usv); } - else - { - cp[0] = codeunit_t(lead_offset + (usv >> 10)); - cp[1] = codeunit_t(0xDC00 + (usv & 0x3FF)); - l = 2; - } - } - - inline - static uchar_t get(const codeunit_t * cp, int8 & l) throw() - { - const uint32 uh = cp[0]; - l = 1; - - if (0xD800 > uh || uh > 0xDFFF) { return uh; } - const uint32 ul = cp[1]; - if (uh > 0xDBFF || 0xDC00 > ul || ul > 0xDFFF) { l = -1; return 0xFFFD; } - ++l; - return (uh<<10) + ul + surrogate_offset; - } + typedef uint16 codeunit_t; + + inline + static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw() + { + if (usv < 0x10000) { l = 1; cp[0] = codeunit_t(usv); } + else + { + cp[0] = codeunit_t(lead_offset + (usv >> 10)); + cp[1] = codeunit_t(0xDC00 + (usv & 0x3FF)); + l = 2; + } + } + + inline + static uchar_t get(const codeunit_t * cp, int8 & l) throw() + { + const uint32 uh = cp[0]; + l = 1; + + if (0xD800 > uh || uh > 0xDFFF) { return uh; } + const uint32 ul = cp[1]; + if (uh > 0xDBFF || 0xDC00 > ul || ul > 0xDFFF) { l = -1; return 0xFFFD; } + ++l; + return (uh<<10) + ul + surrogate_offset; + } }; @@ -106,102 +106,105 @@ template <> struct _utf_codec<8> { private: - static const int8 sz_lut[16]; - static const byte mask_lut[5]; + static const int8 sz_lut[16]; + static const byte mask_lut[5]; public: - typedef uint8 codeunit_t; - - inline - static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw() - { - if (usv < 0x80) {l = 1; cp[0] = usv; return; } - if (usv < 0x0800) {l = 2; cp[0] = 0xC0 + (usv >> 6); cp[1] = 0x80 + (usv & 0x3F); return; } - if (usv < 0x10000) {l = 3; cp[0] = 0xE0 + (usv >> 12); cp[1] = 0x80 + ((usv >> 6) & 0x3F); cp[2] = 0x80 + (usv & 0x3F); return; } - else {l = 4; cp[0] = 0xF0 + (usv >> 18); cp[1] = 0x80 + ((usv >> 12) & 0x3F); cp[2] = 0x80 + ((usv >> 6) & 0x3F); cp[3] = 0x80 + (usv & 0x3F); return; } - } - - inline - static uchar_t get(const codeunit_t * cp, int8 & l) throw() - { - const int8 seq_sz = sz_lut[*cp >> 4]; - uchar_t u = *cp & mask_lut[seq_sz]; - l = 1; - bool toolong = false; - - switch(seq_sz) { - case 4: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong = (u < 0x10); // no break - case 3: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x20); // no break - case 2: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x80); // no break - case 1: break; - case 0: l = -1; return 0xFFFD; - } - - if (l != seq_sz || toolong) - { - l = -l; - return 0xFFFD; - } - return u; - } + typedef uint8 codeunit_t; + + inline + static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw() + { + if (usv < 0x80) {l = 1; cp[0] = usv; return; } + if (usv < 0x0800) {l = 2; cp[0] = 0xC0 + (usv >> 6); cp[1] = 0x80 + (usv & 0x3F); return; } + if (usv < 0x10000) {l = 3; cp[0] = 0xE0 + (usv >> 12); cp[1] = 0x80 + ((usv >> 6) & 0x3F); cp[2] = 0x80 + (usv & 0x3F); return; } + else {l = 4; cp[0] = 0xF0 + (usv >> 18); cp[1] = 0x80 + ((usv >> 12) & 0x3F); cp[2] = 0x80 + ((usv >> 6) & 0x3F); cp[3] = 0x80 + (usv & 0x3F); return; } + } + + inline + static uchar_t get(const codeunit_t * cp, int8 & l) throw() + { + const int8 seq_sz = sz_lut[*cp >> 4]; + uchar_t u = *cp & mask_lut[seq_sz]; + l = 1; + bool toolong = false; + + switch(seq_sz) { + case 4: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong = (u < 0x10); GR_FALLTHROUGH; + // no break + case 3: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x20); GR_FALLTHROUGH; + // no break + case 2: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x80); GR_FALLTHROUGH; + // no break + case 1: break; + case 0: l = -1; return 0xFFFD; + } + + if (l != seq_sz || toolong) + { + l = -l; + return 0xFFFD; + } + return u; + } }; template <typename C> class _utf_iterator { - typedef _utf_codec<sizeof(C)*8> codec; + typedef _utf_codec<sizeof(C)*8> codec; - C * cp; - mutable int8 sl; + C * cp; + mutable int8 sl; public: - typedef C codeunit_type; - typedef uchar_t value_type; - typedef uchar_t * pointer; + typedef C codeunit_type; + typedef uchar_t value_type; + typedef uchar_t * pointer; - class reference - { - const _utf_iterator & _i; + class reference + { + const _utf_iterator & _i; - reference(const _utf_iterator & i): _i(i) {} - public: - operator value_type () const throw () { return codec::get(_i.cp, _i.sl); } - reference & operator = (const value_type usv) throw() { codec::put(_i.cp, usv, _i.sl); return *this; } + reference(const _utf_iterator & i): _i(i) {} + public: + operator value_type () const throw () { return codec::get(_i.cp, _i.sl); } + reference & operator = (const value_type usv) throw() { codec::put(_i.cp, usv, _i.sl); return *this; } - friend class _utf_iterator; - }; + friend class _utf_iterator; + }; - _utf_iterator(const void * us=0) : cp(reinterpret_cast<C *>(const_cast<void *>(us))), sl(1) { } + _utf_iterator(const void * us=0) : cp(reinterpret_cast<C *>(const_cast<void *>(us))), sl(1) { } - _utf_iterator & operator ++ () { cp += abs(sl); return *this; } - _utf_iterator operator ++ (int) { _utf_iterator tmp(*this); operator++(); return tmp; } + _utf_iterator & operator ++ () { cp += abs(sl); return *this; } + _utf_iterator operator ++ (int) { _utf_iterator tmp(*this); operator++(); return tmp; } - bool operator == (const _utf_iterator & rhs) const throw() { return cp >= rhs.cp; } - bool operator != (const _utf_iterator & rhs) const throw() { return !operator==(rhs); } + bool operator == (const _utf_iterator & rhs) const throw() { return cp >= rhs.cp; } + bool operator != (const _utf_iterator & rhs) const throw() { return !operator==(rhs); } - reference operator * () const throw() { return *this; } - pointer operator ->() const throw() { return &operator *(); } + reference operator * () const throw() { return *this; } + pointer operator ->() const throw() { return &operator *(); } - operator codeunit_type * () const throw() { return cp; } + operator codeunit_type * () const throw() { return cp; } - bool error() const throw() { return sl < 1; } + bool error() const throw() { return sl < 1; } }; template <typename C> struct utf { - typedef typename _utf_codec<sizeof(C)*8>::codeunit_t codeunit_t; + typedef typename _utf_codec<sizeof(C)*8>::codeunit_t codeunit_t; - typedef _utf_iterator<C> iterator; - typedef _utf_iterator<const C> const_iterator; + typedef _utf_iterator<C> iterator; + typedef _utf_iterator<const C> const_iterator; }; -typedef utf<uint32> utf32; -typedef utf<uint16> utf16; -typedef utf<uint8> utf8; +typedef utf<uint32> utf32; +typedef utf<uint16> utf16; +typedef utf<uint8> utf8; } // namespace graphite2 diff --git a/gfx/graphite2/src/inc/bits.h b/gfx/graphite2/src/inc/bits.h index 56b2d1022..615c6cba6 100644 --- a/gfx/graphite2/src/inc/bits.h +++ b/gfx/graphite2/src/inc/bits.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2012, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -29,43 +29,91 @@ of the License or (at your option) any later version. namespace graphite2 { + +#if defined GRAPHITE2_BUILTINS && (defined __GNUC__ || defined __clang__) + template<typename T> inline unsigned int bit_set_count(T v) { - v = v - ((v >> 1) & T(~T(0)/3)); // temp - v = (v & T(~T(0)/15*3)) + ((v >> 2) & T(~T(0)/15*3)); // temp - v = (v + (v >> 4)) & T(~T(0)/255*15); // temp - return (T)(v * T(~T(0)/255)) >> (sizeof(T)-1)*8; // count + return __builtin_popcount(v); +} + +template<> +inline unsigned int bit_set_count(int16 v) +{ + return __builtin_popcount(static_cast<uint16>(v)); +} + +template<> +inline unsigned int bit_set_count(int8 v) +{ + return __builtin_popcount(static_cast<uint8>(v)); } +template<> +inline unsigned int bit_set_count(unsigned long v) +{ + return __builtin_popcountl(v); +} + +template<> +inline unsigned int bit_set_count(signed long v) +{ + return __builtin_popcountl(v); +} + +template<> +inline unsigned int bit_set_count(unsigned long long v) +{ + return __builtin_popcountll(v); +} + +template<> +inline unsigned int bit_set_count(signed long long v) +{ + return __builtin_popcountll(v); +} +#else + +template<typename T> +inline unsigned int bit_set_count(T v) +{ + v = v - ((v >> 1) & T(~(0UL)/3)); // temp + v = (v & T(~(0UL)/15*3)) + ((v >> 2) & T(~(0UL)/15*3)); // temp + v = (v + (v >> 4)) & T(~(0UL)/255*15); // temp + return (T)(v * T(~(0UL)/255)) >> (sizeof(T)-1)*8; // count +} + +#endif + template<int S> inline unsigned long _mask_over_val(unsigned long v) { - v = _mask_over_val<S/2>(v); - v |= v >> S*4; - return v; + v = _mask_over_val<S/2>(v); + v |= v >> S*4; + return v; } template<> inline unsigned long _mask_over_val<1>(unsigned long v) { - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - return v; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + return v; } template<typename T> inline T mask_over_val(T v) { - return _mask_over_val<sizeof(T)>(v); + return _mask_over_val<sizeof(T)>(v); } template<typename T> inline unsigned long next_highest_power2(T v) { - return _mask_over_val<sizeof(T)>(v-1)+1; + return _mask_over_val<sizeof(T)>(v-1)+1; } template<typename T> @@ -77,14 +125,22 @@ inline unsigned int log_binary(T v) template<typename T> inline T has_zero(const T x) { - return (x - T(~T(0)/255)) & ~x & T(~T(0)/255*128); + return (x - T(~T(0)/255)) & ~x & T(~T(0)/255*128); } template<typename T> inline T zero_bytes(const T x, unsigned char n) { - const T t = T(~T(0)/255*n); - return T((has_zero(x^t) >> 7)*n); + const T t = T(~T(0)/255*n); + return T((has_zero(x^t) >> 7)*n); +} + +#if 0 +inline float float_round(float x, uint32 m) +{ + *reinterpret_cast<unsigned int *>(&x) &= m; + return *reinterpret_cast<float *>(&x); } +#endif } diff --git a/gfx/graphite2/src/inc/debug.h b/gfx/graphite2/src/inc/debug.h index ba77cb191..97175eb2c 100644 --- a/gfx/graphite2/src/inc/debug.h +++ b/gfx/graphite2/src/inc/debug.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2011, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -24,7 +24,7 @@ Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public License, as published by the Free Software Foundation, either version 2 of the License or (at your option) any later version. */ -// debug.h +// debug.h // // Created on: 22 Dec 2011 // Author: tim @@ -44,16 +44,17 @@ class CharInfo; class Segment; class Slot; -typedef std::pair<const Segment * const, const Slot * const> dslot; +typedef std::pair<const Segment * const, const Slot * const> dslot; struct objectid { - char name[16]; - objectid(const dslot &) throw(); - objectid(const Segment * const p) throw(); + char name[16]; + objectid(const dslot &) throw(); + objectid(const Segment * const p) throw(); }; json & operator << (json & j, const Position &) throw(); +json & operator << (json & j, const Rect &) throw(); json & operator << (json & j, const CharInfo &) throw(); json & operator << (json & j, const dslot &) throw(); json & operator << (json & j, const objectid &) throw(); @@ -64,14 +65,21 @@ json & operator << (json & j, const telemetry &) throw(); inline json & operator << (json & j, const Position & p) throw() { - return j << json::flat << json::array << p.x << p.y << json::close; + return j << json::flat << json::array << p.x << p.y << json::close; +} + + +inline +json & operator << (json & j, const Rect & p) throw() +{ + return j << json::flat << json::array << p.bl.x << p.bl.y << p.tr.x << p.tr.y << json::close; } inline json & operator << (json & j, const objectid & sid) throw() { - return j << sid.name; + return j << sid.name; } diff --git a/gfx/graphite2/src/inc/json.h b/gfx/graphite2/src/inc/json.h index f08b41304..e9826832e 100644 --- a/gfx/graphite2/src/inc/json.h +++ b/gfx/graphite2/src/inc/json.h @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2011, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -29,9 +29,11 @@ of the License or (at your option) any later version. // Author: Tim Eves #pragma once + #include "inc/Main.h" #include <cassert> #include <stdio.h> +#include "inc/List.h" namespace graphite2 { @@ -41,53 +43,58 @@ class json json(const json &); json & operator = (const json &); - typedef void (*_context_t)(json &); - class _null_t {}; + typedef void (*_context_t)(json &); + class _null_t {}; - FILE * const _stream; - char _contexts[128], // context stack - * _context, // current context (top of stack) - * _flatten; // if !0 points to context above which - // pretty printed output should occur. + FILE * const _stream; + char _contexts[128], // context stack + * _context, // current context (top of stack) + * _flatten; // if !0 points to context above which + // pretty printed output should occur. + Vector<void *> _env; - void context(const char current) throw(); - void indent(const int d=0) throw(); - void push_context(const char, const char) throw(); - void pop_context() throw(); + void context(const char current) throw(); + void indent(const int d=0) throw(); + void push_context(const char, const char) throw(); + void pop_context() throw(); public: - class closer; - - typedef const char * string; - typedef double number; - typedef long signed int integer; - typedef bool boolean; - static const _null_t null; - - static void flat(json &) throw(); - static void close(json &) throw(); - static void object(json &) throw(); - static void array(json &) throw(); - static void item(json &) throw(); - - json(FILE * stream) throw(); - ~json() throw (); - - FILE * stream() const throw(); - - json & operator << (string) throw(); - json & operator << (number) throw(); - json & operator << (integer) throw(); - json & operator << (long unsigned int d) throw(); - json & operator << (boolean) throw(); - json & operator << (_null_t) throw(); - json & operator << (_context_t) throw(); - - operator bool() const throw(); - bool good() const throw(); - bool eof() const throw(); - - CLASS_NEW_DELETE; + class closer; + + typedef const char * string; + typedef double number; + typedef long signed int integer; + typedef bool boolean; + static const _null_t null; + + void setenv(unsigned int index, void *val) { _env.reserve(index + 1); if (index >= _env.size()) _env.insert(_env.end(), _env.size() - index + 1, 0); _env[index] = val; } + void *getenv(unsigned int index) const { return _env[index]; } + const Vector<void *> &getenvs() const { return _env; } + + static void flat(json &) throw(); + static void close(json &) throw(); + static void object(json &) throw(); + static void array(json &) throw(); + static void item(json &) throw(); + + json(FILE * stream) throw(); + ~json() throw (); + + FILE * stream() const throw(); + + json & operator << (string) throw(); + json & operator << (number) throw(); + json & operator << (integer) throw(); + json & operator << (long unsigned int d) throw(); + json & operator << (boolean) throw(); + json & operator << (_null_t) throw(); + json & operator << (_context_t) throw(); + + operator bool() const throw(); + bool good() const throw(); + bool eof() const throw(); + + CLASS_NEW_DELETE; }; class json::closer @@ -96,70 +103,70 @@ class json::closer closer(const closer &); closer & operator = (const closer &); - json * const _j; + json * const _j; public: - closer(json * const j) : _j(j) {} - ~closer() throw() { if (_j) *_j << close; } + closer(json * const j) : _j(j) {} + ~closer() throw() { if (_j) *_j << close; } }; inline json::json(FILE * s) throw() : _stream(s), _context(_contexts), _flatten(0) { - if (good()) - fflush(s); + if (good()) + fflush(s); } inline json::~json() throw () { - while (_context > _contexts) pop_context(); + while (_context > _contexts) pop_context(); } inline -FILE * json::stream() const throw() { return _stream; } +FILE * json::stream() const throw() { return _stream; } inline json & json::operator << (json::_context_t ctxt) throw() { - ctxt(*this); - return *this; + ctxt(*this); + return *this; } inline -json & operator << (json & j, signed char d) throw() { return j << json::integer(d); } +json & operator << (json & j, signed char d) throw() { return j << json::integer(d); } inline -json & operator << (json & j, short signed int d) throw() { return j << json::integer(d); } +json & operator << (json & j, short signed int d) throw() { return j << json::integer(d); } inline -json & operator << (json & j, signed int d) throw() { return j << json::integer(d); } +json & operator << (json & j, signed int d) throw() { return j << json::integer(d); } inline -json & operator << (json & j, unsigned char d) throw() { return j << json::integer(d); } +json & operator << (json & j, unsigned char d) throw() { return j << json::integer(d); } inline json & operator << (json & j, short unsigned int d) throw() { return j << json::integer(d); } inline -json & operator << (json & j, unsigned int d) throw() { return j << json::integer(d); } +json & operator << (json & j, unsigned int d) throw() { return j << json::integer(d); } inline json & operator << (json & j, char c) throw () { - const char str[2] = {c,0}; - return j << str; + const char str[2] = {c,0}; + return j << str; } inline -json::operator bool() const throw() { return good(); } +json::operator bool() const throw() { return good(); } inline -bool json::good() const throw() { return _stream && ferror(_stream) == 0; } +bool json::good() const throw() { return _stream && ferror(_stream) == 0; } inline -bool json::eof() const throw() { return feof(_stream) != 0; } +bool json::eof() const throw() { return feof(_stream) != 0; } } // namespace graphite2 diff --git a/gfx/graphite2/src/inc/locale2lcid.h b/gfx/graphite2/src/inc/locale2lcid.h index fc2d384b4..bc3e3d830 100644 --- a/gfx/graphite2/src/inc/locale2lcid.h +++ b/gfx/graphite2/src/inc/locale2lcid.h @@ -273,6 +273,11 @@ public: while (old[len]) len++; len += 2; mLangLookup[a][b] = gralloc<const IsoLangEntry *>(len); + if (!mLangLookup[a][b]) + { + mLangLookup[a][b] = old; + continue; + } mLangLookup[a][b][--len] = NULL; mLangLookup[a][b][--len] = &LANG_ENTRIES[i]; while (--len >= 0) @@ -285,6 +290,7 @@ public: else { mLangLookup[a][b] = gralloc<const IsoLangEntry *>(2); + if (!mLangLookup[a][b]) continue; mLangLookup[a][b][1] = NULL; mLangLookup[a][b][0] = &LANG_ENTRIES[i]; } @@ -295,8 +301,8 @@ public: ~Locale2Lang() { for (int i = 0; i != 26; ++i) - for (int j = 0; j != 26; ++j) - free(mLangLookup[i][j]); + for (int j = 0; j != 26; ++j) + free(mLangLookup[i][j]); } unsigned short getMsId(const char * locale) const { @@ -393,7 +399,7 @@ public: ++i; continue; } - if (strcmp(mLangLookup[a][b][i]->maCountry, region) == 0) + if (region && (strncmp(mLangLookup[a][b][i]->maCountry, region, regionLength) == 0)) { langId = mLangLookup[a][b][i]->mnLang; break; diff --git a/gfx/graphite2/src/inc/opcode_table.h b/gfx/graphite2/src/inc/opcode_table.h index 06916f6c8..73a99c820 100644 --- a/gfx/graphite2/src/inc/opcode_table.h +++ b/gfx/graphite2/src/inc/opcode_table.h @@ -43,7 +43,7 @@ of the License or (at your option) any later version. // level - any byte static const opcode_t opcode_table[] = { - {{do2(nop)}, 0, "NOP"}, + {{do2(nop)}, 0, "NOP"}, {{do2(push_byte)}, 1, "PUSH_BYTE"}, // number {{do2(push_byte_u)}, 1, "PUSH_BYTE_U"}, // number @@ -114,6 +114,11 @@ static const opcode_t opcode_table[] = {{do_(put_glyph), NILOP}, 2, "PUT_GLYPH"}, // output_class output_class {{do2(push_glyph_attr)}, 3, "PUSH_GLYPH_ATTR"}, // gattrnum gattrnum slot {{do2(push_att_to_glyph_attr)}, 3, "PUSH_ATT_TO_GLYPH_ATTR"}, // gattrnum gattrnum slot + {{do2(bor)}, 0, "BITOR"}, + {{do2(band)}, 0, "BITAND"}, + {{do2(bnot)}, 0, "BITNOT"}, // 0x40 + {{do2(setbits)}, 4, "BITSET"}, + {{do2(set_feat)}, 2, "SET_FEAT"}, // private opcodes for internal use only, comes after all other on disk opcodes. {{do_(temp_copy), NILOP}, 0, "TEMP_COPY"} }; diff --git a/gfx/graphite2/src/inc/opcodes.h b/gfx/graphite2/src/inc/opcodes.h index 835e89351..ce87092b6 100644 --- a/gfx/graphite2/src/inc/opcodes.h +++ b/gfx/graphite2/src/inc/opcodes.h @@ -61,6 +61,7 @@ of the License or (at your option) any later version. // isl = The last positioned slot // ip = The current instruction pointer // endPos = Position of advance of last cluster +// dir = writing system directionality of the font // #define NOT_IMPLEMENTED assert(false) @@ -199,9 +200,9 @@ STARTOP(next) if (map - &smap[0] >= int(smap.size())) DIE if (is) { - if (is == smap.highwater()) - smap.highpassed(true); - is = is->next(); + if (is == smap.highwater()) + smap.highpassed(true); + is = is->next(); } ++map; ENDOP @@ -241,20 +242,24 @@ ENDOP STARTOP(put_copy) declare_params(1); const int slot_ref = int8(*param); - if (is && (slot_ref ||is != *map)) + if (is) { - int16 *tempUserAttrs = is->userAttrs(); slotref ref = slotat(slot_ref); - if (ref) + if (ref && ref != is) { - memcpy(tempUserAttrs, ref->userAttrs(), seg.numAttrs() * sizeof(uint16)); + int16 *tempUserAttrs = is->userAttrs(); + if (is->attachedTo() || is->firstChild()) DIE Slot *prev = is->prev(); Slot *next = is->next(); - memcpy(is, slotat(slot_ref), sizeof(Slot)); + memcpy(tempUserAttrs, ref->userAttrs(), seg.numAttrs() * sizeof(uint16)); + memcpy(is, ref, sizeof(Slot)); + is->firstChild(NULL); + is->nextSibling(NULL); is->userAttrs(tempUserAttrs); is->next(next); is->prev(prev); - is->sibling(NULL); + if (is->attachedTo()) + is->attachedTo()->child(is); } is->markCopied(false); is->markDeleted(false); @@ -263,6 +268,7 @@ ENDOP STARTOP(insert) Slot *newSlot = seg.newSlot(); + if (!newSlot) DIE; Slot *iss = is; while (iss && iss->isDeleted()) iss = iss->next(); if (!iss) @@ -284,7 +290,7 @@ STARTOP(insert) { iss->prev()->next(newSlot); newSlot->prev(iss->prev()); - newSlot->before(iss->prev()->after()); + newSlot->before(iss->prev()->after()); } else { @@ -297,17 +303,19 @@ STARTOP(insert) { iss->prev(newSlot); newSlot->originate(iss->original()); - newSlot->after(iss->before()); + newSlot->after(iss->before()); } else if (newSlot->prev()) { newSlot->originate(newSlot->prev()->original()); - newSlot->after(newSlot->prev()->after()); + newSlot->after(newSlot->prev()->after()); } else { newSlot->originate(seg.defaultOriginal()); } + if (is == smap.highwater()) + smap.highpassed(false); is = newSlot; seg.extendLength(1); if (map != &smap[-1]) @@ -315,7 +323,7 @@ STARTOP(insert) ENDOP STARTOP(delete_) - if (!is) DIE + if (!is || is->isDeleted()) DIE is->markDeleted(true); if (is->prev()) is->prev()->next(is->next()); @@ -328,7 +336,7 @@ STARTOP(delete_) seg.last(is->prev()); if (is == smap.highwater()) - smap.highwater(is->next()); + smap.highwater(is->next()); if (is->prev()) is = is->prev(); seg.extendLength(-1); @@ -373,18 +381,18 @@ ENDOP STARTOP(attr_set) declare_params(1); - const attrCode slat = attrCode(uint8(*param)); + const attrCode slat = attrCode(uint8(*param)); const int val = int(pop()); is->setAttr(&seg, slat, 0, val, smap); ENDOP STARTOP(attr_add) declare_params(1); - const attrCode slat = attrCode(uint8(*param)); + const attrCode slat = attrCode(uint8(*param)); const int val = int(pop()); if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0) { - seg.positionSlots(0, *smap.begin(), *(smap.end()-1)); + seg.positionSlots(0, *smap.begin(), *(smap.end()-1), dir); flags |= POSITIONED; } int res = is->getAttr(&seg, slat, 0); @@ -393,11 +401,11 @@ ENDOP STARTOP(attr_sub) declare_params(1); - const attrCode slat = attrCode(uint8(*param)); + const attrCode slat = attrCode(uint8(*param)); const int val = int(pop()); if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0) { - seg.positionSlots(0, *smap.begin(), *(smap.end()-1)); + seg.positionSlots(0, *smap.begin(), *(smap.end()-1), dir); flags |= POSITIONED; } int res = is->getAttr(&seg, slat, 0); @@ -406,7 +414,7 @@ ENDOP STARTOP(attr_set_slot) declare_params(1); - const attrCode slat = attrCode(uint8(*param)); + const attrCode slat = attrCode(uint8(*param)); const int offset = (map - smap.begin())*int(slat == gr_slatAttTo); const int val = int(pop()) + offset; is->setAttr(&seg, slat, offset, val, smap); @@ -414,7 +422,7 @@ ENDOP STARTOP(iattr_set_slot) declare_params(2); - const attrCode slat = attrCode(uint8(param[0])); + const attrCode slat = attrCode(uint8(param[0])); const size_t idx = uint8(param[1]); const int val = int(pop()) + (map - smap.begin())*int(slat == gr_slatAttTo); is->setAttr(&seg, slat, idx, val, smap); @@ -422,11 +430,11 @@ ENDOP STARTOP(push_slot_attr) declare_params(2); - const attrCode slat = attrCode(uint8(param[0])); + const attrCode slat = attrCode(uint8(param[0])); const int slot_ref = int8(param[1]); if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0) { - seg.positionSlots(0, *smap.begin(), *(smap.end()-1)); + seg.positionSlots(0, *smap.begin(), *(smap.end()-1), dir); flags |= POSITIONED; } slotref slot = slotat(slot_ref); @@ -453,7 +461,7 @@ STARTOP(push_glyph_metric) const signed int attr_level = uint8(param[2]); slotref slot = slotat(slot_ref); if (slot) - push(seg.getGlyphMetric(slot, glyph_attr, attr_level)); + push(seg.getGlyphMetric(slot, glyph_attr, attr_level, dir)); ENDOP STARTOP(push_feat) @@ -491,18 +499,18 @@ STARTOP(push_att_to_glyph_metric) { slotref att = slot->attachedTo(); if (att) slot = att; - push(int32(seg.getGlyphMetric(slot, glyph_attr, attr_level))); + push(int32(seg.getGlyphMetric(slot, glyph_attr, attr_level, dir))); } ENDOP STARTOP(push_islot_attr) declare_params(3); - const attrCode slat = attrCode(uint8(param[0])); + const attrCode slat = attrCode(uint8(param[0])); const int slot_ref = int8(param[1]), idx = uint8(param[2]); if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0) { - seg.positionSlots(0, *smap.begin(), *(smap.end()-1)); + seg.positionSlots(0, *smap.begin(), *(smap.end()-1), dir); flags |= POSITIONED; } slotref slot = slotat(slot_ref); @@ -534,7 +542,7 @@ ENDOP STARTOP(iattr_set) declare_params(2); - const attrCode slat = attrCode(uint8(param[0])); + const attrCode slat = attrCode(uint8(param[0])); const size_t idx = uint8(param[1]); const int val = int(pop()); is->setAttr(&seg, slat, idx, val, smap); @@ -542,12 +550,12 @@ ENDOP STARTOP(iattr_add) declare_params(2); - const attrCode slat = attrCode(uint8(param[0])); + const attrCode slat = attrCode(uint8(param[0])); const size_t idx = uint8(param[1]); const int val = int(pop()); if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0) { - seg.positionSlots(0, *smap.begin(), *(smap.end()-1)); + seg.positionSlots(0, *smap.begin(), *(smap.end()-1), dir); flags |= POSITIONED; } int res = is->getAttr(&seg, slat, idx); @@ -556,12 +564,12 @@ ENDOP STARTOP(iattr_sub) declare_params(2); - const attrCode slat = attrCode(uint8(param[0])); + const attrCode slat = attrCode(uint8(param[0])); const size_t idx = uint8(param[1]); const int val = int(pop()); if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0) { - seg.positionSlots(0, *smap.begin(), *(smap.end()-1)); + seg.positionSlots(0, *smap.begin(), *(smap.end()-1), dir); flags |= POSITIONED; } int res = is->getAttr(&seg, slat, idx); @@ -635,6 +643,7 @@ ENDOP STARTOP(temp_copy) slotref newSlot = seg.newSlot(); + if (!newSlot || !is) DIE; int16 *tempUserAttrs = newSlot->userAttrs(); memcpy(newSlot, is, sizeof(Slot)); memcpy(tempUserAttrs, is->userAttrs(), seg.numAttrs() * sizeof(uint16)); @@ -642,3 +651,37 @@ STARTOP(temp_copy) newSlot->markCopied(true); *map = newSlot; ENDOP + +STARTOP(band) + binop(&); +ENDOP + +STARTOP(bor) + binop(|); +ENDOP + +STARTOP(bnot) + *sp = ~*sp; +ENDOP + +STARTOP(setbits) + declare_params(4); + const uint16 m = uint16(param[0]) << 8 + | uint8(param[1]); + const uint16 v = uint16(param[2]) << 8 + | uint8(param[3]); + *sp = ((*sp) & ~m) | v; +ENDOP + +STARTOP(set_feat) + declare_params(2); + const unsigned int feat = uint8(param[0]); + const int slot_ref = int8(param[1]); + slotref slot = slotat(slot_ref); + if (slot) + { + uint8 fid = seg.charinfo(slot->original())->fid(); + seg.setFeature(fid, feat, pop()); + } +ENDOP + diff --git a/gfx/graphite2/src/json.cpp b/gfx/graphite2/src/json.cpp index 5923e664d..48a563ce0 100644 --- a/gfx/graphite2/src/json.cpp +++ b/gfx/graphite2/src/json.cpp @@ -1,6 +1,6 @@ /* GRAPHITE2 LICENSING - Copyright 2010, SIL International + Copyright 2011, SIL International All rights reserved. This library is free software; you can redistribute it and/or modify @@ -30,97 +30,111 @@ of the License or (at your option) any later version. #if !defined GRAPHITE2_NTRACING #include <stdio.h> +#include <limits> #include "inc/json.h" using namespace graphite2; namespace { - enum - { - seq = ',', - obj='}', member=':', empty_obj='{', - arr=']', empty_arr='[' - }; + enum + { + seq = ',', + obj='}', member=':', empty_obj='{', + arr=']', empty_arr='[' + }; } -const json::_null_t json::null = {}; +const json::_null_t json::null = {}; inline void json::context(const char current) throw() { - fprintf(_stream, "%c", *_context); - indent(); - *_context = current; + fprintf(_stream, "%c", *_context); + indent(); + *_context = current; } void json::indent(const int d) throw() { - if (*_context == member || (_flatten && _flatten < _context)) - fputc(' ', _stream); - else - fprintf(_stream, "\n%*s", 4*int(_context - _contexts + d), ""); + if (*_context == member || (_flatten && _flatten < _context)) + fputc(' ', _stream); + else + fprintf(_stream, "\n%*s", 4*int(_context - _contexts + d), ""); } inline void json::push_context(const char prefix, const char suffix) throw() { - assert(_context - _contexts < ptrdiff_t(sizeof _contexts)); + assert(_context - _contexts < ptrdiff_t(sizeof _contexts)); - if (_context == _contexts) - *_context = suffix; - else - context(suffix); - *++_context = prefix; + if (_context == _contexts) + *_context = suffix; + else + context(suffix); + *++_context = prefix; } void json::pop_context() throw() { - assert(_context > _contexts); + assert(_context > _contexts); - if (*_context == seq) indent(-1); - else fputc(*_context, _stream); + if (*_context == seq) indent(-1); + else fputc(*_context, _stream); - fputc(*--_context, _stream); - if (_context == _contexts) fputc('\n', _stream); - fflush(_stream); + fputc(*--_context, _stream); + if (_context == _contexts) fputc('\n', _stream); + fflush(_stream); - if (_flatten >= _context) _flatten = 0; - *_context = seq; + if (_flatten >= _context) _flatten = 0; + *_context = seq; } // These four functions cannot be inlined as pointers to these // functions are needed for operator << (_context_t) to work. -void json::flat(json & j) throw() { if (!j._flatten) j._flatten = j._context; } -void json::close(json & j) throw() { j.pop_context(); } -void json::object(json & j) throw() { j.push_context('{', '}'); } -void json::array(json & j) throw() { j.push_context('[', ']'); } +void json::flat(json & j) throw() { if (!j._flatten) j._flatten = j._context; } +void json::close(json & j) throw() { j.pop_context(); } +void json::object(json & j) throw() { j.push_context('{', '}'); } +void json::array(json & j) throw() { j.push_context('[', ']'); } void json::item(json & j) throw() { - while (j._context > j._contexts+1 && j._context[-1] != arr) - j.pop_context(); + while (j._context > j._contexts+1 && j._context[-1] != arr) + j.pop_context(); } json & json::operator << (json::string s) throw() { - const char ctxt = _context[-1] == obj ? *_context == member ? seq : member : seq; - context(ctxt); - fprintf(_stream, "\"%s\"", s); - if (ctxt == member) fputc(' ', _stream); + const char ctxt = _context[-1] == obj ? *_context == member ? seq : member : seq; + context(ctxt); + fprintf(_stream, "\"%s\"", s); + if (ctxt == member) fputc(' ', _stream); - return *this; + return *this; } -json & json::operator << (json::number f) throw() { context(seq); fprintf(_stream, "%g", f); return *this; } -json & json::operator << (json::integer d) throw() { context(seq); fprintf(_stream, "%ld", d); return *this; } -json & json::operator << (long unsigned d) throw() { context(seq); fprintf(_stream, "%ld", d); return *this; } -json & json::operator << (json::boolean b) throw() { context(seq); fputs(b ? "true" : "false", _stream); return *this; } -json & json::operator << (json::_null_t) throw() { context(seq); fputs("null",_stream); return *this; } +json & json::operator << (json::number f) throw() +{ + context(seq); + if (std::numeric_limits<json::number>::infinity() == f) + fputs("Infinity", _stream); + else if (-std::numeric_limits<json::number>::infinity() == f) + fputs("-Infinity", _stream); + else if (std::numeric_limits<json::number>::quiet_NaN() == f || + std::numeric_limits<json::number>::signaling_NaN() == f) + fputs("NaN", _stream); + else + fprintf(_stream, "%g", f); + return *this; +} +json & json::operator << (json::integer d) throw() { context(seq); fprintf(_stream, "%ld", d); return *this; } +json & json::operator << (long unsigned d) throw() { context(seq); fprintf(_stream, "%ld", d); return *this; } +json & json::operator << (json::boolean b) throw() { context(seq); fputs(b ? "true" : "false", _stream); return *this; } +json & json::operator << (json::_null_t) throw() { context(seq); fputs("null",_stream); return *this; } #endif diff --git a/gfx/graphite2/src/moz.build b/gfx/graphite2/src/moz.build index 77bd7cbe8..97c45111e 100644 --- a/gfx/graphite2/src/moz.build +++ b/gfx/graphite2/src/moz.build @@ -33,19 +33,22 @@ CPP_SOURCES += [ 'gr_segment.cpp', 'gr_slot.cpp', 'json.cpp', - 'Bidi.cpp', 'CachedFace.cpp', 'CmapCache.cpp', 'Code.cpp', + 'Collider.cpp', + 'Decompressor.cpp', 'Face.cpp', 'FeatureMap.cpp', 'FileFace.cpp', 'Font.cpp', 'GlyphCache.cpp', 'GlyphFace.cpp', + 'Intervals.cpp', 'Justifier.cpp', 'NameTable.cpp', 'Pass.cpp', + 'Position.cpp', 'SegCache.cpp', 'SegCacheEntry.cpp', 'SegCacheStore.cpp', diff --git a/gfx/thebes/gfxGraphiteShaper.cpp b/gfx/thebes/gfxGraphiteShaper.cpp index b24cee0b9..3cfeb265b 100644 --- a/gfx/thebes/gfxGraphiteShaper.cpp +++ b/gfx/thebes/gfxGraphiteShaper.cpp @@ -161,9 +161,10 @@ gfxGraphiteShaper::ShapeText(gfxContext *aContext, size_t numChars = gr_count_unicode_characters(gr_utf16, aText, aText + aLength, nullptr); + gr_bidirtl grBidi = gr_bidirtl(aShapedText->IsRightToLeft() + ? (gr_rtl | gr_nobidi) : gr_nobidi); gr_segment *seg = gr_make_seg(mGrFont, mGrFace, 0, grFeatures, - gr_utf16, aText, numChars, - aShapedText->IsRightToLeft()); + gr_utf16, aText, numChars, grBidi); gr_featureval_destroy(grFeatures); |