Graphics-Primitive-Driver-Cairo-0.47000755000765000024 013024374634 17033 5ustar00gphatstaff000000000000README100644000765000024 61713024374634 17760 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47 This archive contains the distribution Graphics-Primitive-Driver-Cairo, version 0.47: Cairo backend for Graphics::Primitive This software is copyright (c) 2016 by Cold Hard Code, LLC. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. This README file was generated by Dist::Zilla::Plugin::Readme v6.008. Changes100644000765000024 676213024374634 20422 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47Revision history for Graphics-Primitive-Driver-Cairo 0.47 Dec 14 2016 0.46 Feb 17 2014 0.45 Jan 24 2014 0.44 Sep 27 2011 - Fix margin bug in bottom border drawing. (Thanks Jay Shirley) 0.43 February 12, 2010 - Copyright updates. 0.42 November 26th, 2009 - Fix POD typo (Thanks Brian Cassidy) 0.41 November 26th, 2009 - Match text changes (show_text) in angled text rendering, which was preventing angled text from drawing. 0.40 October 2nd, 2009 - Use show_text rather than text_path + fill for text rendering - Set new font options from Graphics::Primitive::Font (antialias, hint, etc) 0.39 - Honor padding when drawing backgrounds... ;) 0.38 - Honor margins when drawing background of components. 0.37 - Properly handle sizing of TextBoxes with rotated text 0.36 - Disable taint mode in 00-load.t (rafl) 0.35 - Fix big in draw image (was drawing box of width/width, not width/height) (Danny Luna) 0.34 - Remove various unnecessary bits 0.33 - Add new_sub_path before arc call, removing impossible-to-fix line segment 0.32 - Remove accidental inclusion of some debugging code 0.31 - Add relatively naive, but surprisingly effective, method for handling rotated text. - POD fixes 0.30 - Add Radial Gradient support 0.29 - Dependency Bump - Move pod tests to t/author 0.28 - Dependency Bumps - Add antialias_mode attribute 0.27 - POD updates 0.26 - TextLayout: slice now returns a TextBox with it's minimum_(height|width) set, rather than the (height|width). 0.25 - Implement get_textbox_layout - Implement _draw_ellipse and _draw_circle 0.24 - Keep up with Graphics::Primitive API changes - Bump dependencies 0.23 - Add _draw_polygon 0.22 - Fix the bug that the fix from 0.21 created. 0.21 - Fix data method not "finishing" the scene. 0.20 - Test adjustments for missing Test::Image::GD (thanks to Vladimir Timofeev) 0.19 - Take out debugging leftovers 0.18 - Dashed borders - Dashed stroke 0.17 - Fix retrieval of PNG surface via data 0.16 - Fiddle with text widths in get_text_bounding_box - Add image drawing (experimental) - Fix textbox alignment paired with margins 0.15 - Remove useless move_to and hash assignment in get_text_bounding_box - Get rid of surface_data accessor for a simple concatenation, remove some useless API pieces (append_surface_data and surface_data) - Fix broken margins when drawing complex borders. 0.14 - Use cairo's text_extents instead of text_path and fill_extents, this unfortunately breaks rotation for now 0.13 - Non-rotated TextBox alignment 0.12 - Make format case-insensitive 0.11 - Skip 0-width borders 0.10 - Handle per-side borders in Graphics::Primitive 0.17 - Add some simple output testing of borders, more to come 0.09 - Add caching to get_text_bounding_box 0.08 - TextBox: line_height - TextBox: don't ignore empty lines 0.07 - Ditch stupid component attribute!! - Implement _resize and _finish_page 0.06 - Add _draw_bezier 0.05 - Add check for arc_negative and use it if necessary - Add angle support to get_text_bounding box and _draw_textbox - Add reset 0.04 - Minor optimizations - Add arc to primitive elsif 0.03 - Add support for preserve operations - Add _draw_rectangle 0.02 - Add _draw_arc, _draw_path and _do_fill - Change to new Line accessor names - TextBox: tighten up height & width of text - Honor contiguous hint 0.01 Date/time First version, released on an unsuspecting world. LICENSE100644000765000024 4370213024374634 20147 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47This software is copyright (c) 2016 by Cold Hard Code, LLC. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2016 by Cold Hard Code, LLC. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 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. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's 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 General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) 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. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the 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 Program specifies a version number of the 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 Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, 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 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "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 PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. 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 PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. 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. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2016 by Cold Hard Code, LLC. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End t000755000765000024 013024374634 17217 5ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47load.t100644000765000024 16413024374634 20444 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/tuse strict; use warnings; use Test::More; BEGIN { use_ok( 'Graphics::Primitive::Driver::Cairo' ); } done_testing;META.yml100644000765000024 216413024374634 20370 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47--- abstract: 'Cairo backend for Graphics::Primitive' author: - 'Cory G Watson ' build_requires: Graphics::Color::RGB: '0' Graphics::Primitive::Font: '0' Test::More: '0' strict: '0' warnings: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 6.008, CPAN::Meta::Converter version 2.150005' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Graphics-Primitive-Driver-Cairo requires: Cairo: '0' Carp: '0' Geometry::Primitive::Point: '0' Geometry::Primitive::Rectangle: '0' Graphics::Primitive::Driver: '0' Graphics::Primitive::Driver::TextLayout: '0' Graphics::Primitive::TextBox: '0' IO::File: '0' Math::Trig: '0' Moose: '0' Moose::Util::TypeConstraints: '0' Text::Flow: '0' resources: bugtracker: https://github.com/gphat/graphics-primitive-driver-cairo/issues homepage: https://github.com/gphat/graphics-primitive-driver-cairo repository: https://github.com/gphat/graphics-primitive-driver-cairo.git version: '0.47' x_serialization_backend: 'YAML::Tiny version 1.69' MANIFEST100644000765000024 50213024374634 20222 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.008. Changes LICENSE MANIFEST META.json META.yml Makefile.PL README README.mkdn lib/Graphics/Primitive/Driver/Cairo.pm lib/Graphics/Primitive/Driver/Cairo/TextLayout.pm t/author-pod-syntax.t t/author-synopsis.t t/author/pod.t t/layout.t t/load.t META.json100644000765000024 375713024374634 20551 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47{ "abstract" : "Cairo backend for Graphics::Primitive", "author" : [ "Cory G Watson " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 6.008, CPAN::Meta::Converter version 2.150005", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Graphics-Primitive-Driver-Cairo", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "develop" : { "requires" : { "Test::Pod" : "1.41", "Test::Synopsis" : "0" } }, "runtime" : { "requires" : { "Cairo" : "0", "Carp" : "0", "Geometry::Primitive::Point" : "0", "Geometry::Primitive::Rectangle" : "0", "Graphics::Primitive::Driver" : "0", "Graphics::Primitive::Driver::TextLayout" : "0", "Graphics::Primitive::TextBox" : "0", "IO::File" : "0", "Math::Trig" : "0", "Moose" : "0", "Moose::Util::TypeConstraints" : "0", "Text::Flow" : "0" } }, "test" : { "requires" : { "Graphics::Color::RGB" : "0", "Graphics::Primitive::Font" : "0", "Test::More" : "0", "strict" : "0", "warnings" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/gphat/graphics-primitive-driver-cairo/issues" }, "homepage" : "https://github.com/gphat/graphics-primitive-driver-cairo", "repository" : { "type" : "git", "url" : "https://github.com/gphat/graphics-primitive-driver-cairo.git", "web" : "https://github.com/gphat/graphics-primitive-driver-cairo" } }, "version" : "0.47", "x_serialization_backend" : "Cpanel::JSON::XS version 3.0225" } layout.t100644000765000024 333513024374634 21065 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/tuse strict; use Test::More; use Graphics::Color::RGB; use Graphics::Primitive::Font; use Graphics::Primitive::TextBox; use Graphics::Primitive::Driver::Cairo; my $driver = Graphics::Primitive::Driver::Cairo->new(format => 'PNG', width => 80, height => 300); my $text = "Lorem ipsum dolor sit amet,\nconsectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam,\nquis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; # my $tl = Document::Writer::TextLayout->new( # default_color => Graphics::Color::RGB->new( # red => 0, green => 0, blue => 0, alpha => 1 # ), # font => Graphics::Primitive::Font->new( # size => 12 # ), # text => $text, # width => 80 # ); my $tb = Graphics::Primitive::TextBox->new( color => Graphics::Color::RGB->new, width => 80, height => 300, font => Graphics::Primitive::Font->new, text => $text ); my $tl = $driver->get_textbox_layout($tb); my $ret = $tl->slice(0, 20); isa_ok($ret, 'Graphics::Primitive::TextBox'); cmp_ok($ret->minimum_height, '>', 0, '0 offset, 20 size, > 0'); cmp_ok($ret->minimum_height, '<=', 20, '0 offset, 20 size, <= 20'); my $ret2 = $tl->slice($ret->height, 2); cmp_ok($ret2->minimum_height, '==', 0, 'previous offset, 2 size'); my $ret3 = $tl->slice($ret->height, 20); cmp_ok($ret3->minimum_height, '>', 0, 'previous offset, 20 size, > 0'); cmp_ok($ret3->minimum_height, '<=', 20, 'previous offset, 20 size, <= 20'); done_testing;README.mkdn100644000765000024 606513024374634 20733 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47# NAME Graphics::Primitive::Driver::Cairo - Cairo backend for Graphics::Primitive # VERSION version 0.47 # SYNOPSIS use Graphics::Primitive::Component; use Graphics::Primitive::Driver::Cairo; my $driver = Graphics::Primitive::Driver::Cairo->new; my $container = Graphics::Primitive::Container->new( width => 800, height => 600 ); my $black = Graphics::Primitive::Color->new(red => 0, green => 0, blue => 0); $container->border->width(1); $container->border->color($black); $container->padding( Graphics::Primitive::Insets->new(top => 5, bottom => 5, left => 5, right => 5) ); my $comp = Graphics::Primitive::Component->new; $comp->background_color($black); $container->add_component($comp, 'c'); my $lm = Layout::Manager::Compass->new; $lm->do_layout($container); my $driver = Graphics::Primitive::Driver::Cairo->new( format => 'PDF' ); $driver->draw($container); $driver->write('/Users/gphat/foo.pdf'); # DESCRIPTION This module draws Graphics::Primitive objects using Cairo. # IMPLEMENTATION DETAILS - **Borders** Borders are drawn clockwise starting with the top one. Since cairo can't do line-joins on different colored lines, each border overlaps those before it. This is not the way I'd like it to work, but i'm opting to fix this later. Consider yourself warned. # ATTRIBUTES ## antialias\_mode Set/Get the antialias mode of this driver. Options are default, none, gray and subpixel. ## cairo This driver's Cairo::Context object ## format Get the format for this driver. ## surface Get/Set the surface on which this driver is operating. # METHODS ## data Get the data in a scalar for this driver. ## write ($file) Write this driver's data to the specified file. ## get\_text\_bounding\_box ($font, $text, $angle) Returns two [Rectangles](https://metacpan.org/pod/Graphics::Primitive::Rectangle) that encloses the supplied text. The origin's x and y maybe negative, meaning that the glyphs in the text extending left of x or above y. The first rectangle is the bounding box required for a container that wants to contain the text. The second box is only useful if an optional angle is provided. This second rectangle is the bounding box of the un-rotated text that allows for a controlled rotation. If no angle is supplied then the two rectangles are actually the same object. If the optional angle is supplied the text will be rotated by the supplied amount in radians. ## get\_textbox\_layout ($tb) Returns a [Graphics::Primitive::Driver::TextLayout](https://metacpan.org/pod/Graphics::Primitive::Driver::TextLayout) for the supplied textbox. ## reset Reset the driver. ## draw Draws the specified component. Container's components are drawn recursively. # ACKNOWLEDGEMENTS Danny Luna # AUTHOR Cory G Watson # COPYRIGHT AND LICENSE This software is copyright (c) 2016 by Cold Hard Code, LLC. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Makefile.PL100644000765000024 363013024374634 21070 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.008. use strict; use warnings; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "Cairo backend for Graphics::Primitive", "AUTHOR" => "Cory G Watson ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "Graphics-Primitive-Driver-Cairo", "LICENSE" => "perl", "NAME" => "Graphics::Primitive::Driver::Cairo", "PREREQ_PM" => { "Cairo" => 0, "Carp" => 0, "Geometry::Primitive::Point" => 0, "Geometry::Primitive::Rectangle" => 0, "Graphics::Primitive::Driver" => 0, "Graphics::Primitive::Driver::TextLayout" => 0, "Graphics::Primitive::TextBox" => 0, "IO::File" => 0, "Math::Trig" => 0, "Moose" => 0, "Moose::Util::TypeConstraints" => 0, "Text::Flow" => 0 }, "TEST_REQUIRES" => { "Graphics::Color::RGB" => 0, "Graphics::Primitive::Font" => 0, "Test::More" => 0, "strict" => 0, "warnings" => 0 }, "VERSION" => "0.47", "test" => { "TESTS" => "t/*.t t/author/*.t" } ); my %FallbackPrereqs = ( "Cairo" => 0, "Carp" => 0, "Geometry::Primitive::Point" => 0, "Geometry::Primitive::Rectangle" => 0, "Graphics::Color::RGB" => 0, "Graphics::Primitive::Driver" => 0, "Graphics::Primitive::Driver::TextLayout" => 0, "Graphics::Primitive::Font" => 0, "Graphics::Primitive::TextBox" => 0, "IO::File" => 0, "Math::Trig" => 0, "Moose" => 0, "Moose::Util::TypeConstraints" => 0, "Test::More" => 0, "Text::Flow" => 0, "strict" => 0, "warnings" => 0 ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); author000755000765000024 013024374634 20521 5ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/tpod.t100644000765000024 35013024374634 21606 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/t/author#!perl -T use strict; use warnings; use Test::More; # Ensure a recent version of Test::Pod my $min_tp = 1.22; eval "use Test::Pod $min_tp"; plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; all_pod_files_ok(); author-synopsis.t100644000765000024 26213024374634 22713 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/t#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { print qq{1..0 # SKIP these tests are for testing by the author\n}; exit } } use Test::Synopsis; all_synopsis_ok(); author-pod-syntax.t100644000765000024 45413024374634 23135 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/t#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { print qq{1..0 # SKIP these tests are for testing by the author\n}; exit } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok(); Driver000755000765000024 013024374634 24465 5ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/lib/Graphics/PrimitiveCairo.pm100644000765000024 6261313024374634 26250 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/lib/Graphics/Primitive/Driverpackage Graphics::Primitive::Driver::Cairo; $Graphics::Primitive::Driver::Cairo::VERSION = '0.47'; use Moose; use Moose::Util::TypeConstraints; # ABSTRACT: Cairo backend for Graphics::Primitive use Cairo; use Carp; use Geometry::Primitive::Point; use Geometry::Primitive::Rectangle; use Graphics::Primitive::Driver::Cairo::TextLayout; use IO::File; use Math::Trig ':pi'; with 'Graphics::Primitive::Driver'; enum 'Graphics::Primitive::Driver::Cairo::AntialiasModes' => [ qw(default none gray subpixel) ]; enum 'Graphics::Primitive::Driver::Cairo::Format' => [ qw(PDF PS PNG SVG pdf ps png svg) ]; # If we encounter an operation with 'preserve' set to true we'll set this attr # to the number of primitives in that path. On each iteration we'll check # this attribute. If it's true, we'll skip that many primitives in the # current path and then reset the value. This allows us to leverage cairo's # fill_preserve and stroke_perserve and avoid wasting time redrawing. has '_preserve_count' => ( isa => 'Str', is => 'rw', default => sub { 0 } ); has 'antialias_mode' => ( is => 'rw', isa => 'Graphics::Primitive::Driver::Cairo::AntialiasModes' ); has 'cairo' => ( is => 'rw', isa => 'Cairo::Context', clearer => 'clear_cairo', lazy => 1, default => sub { my $self = shift; my $ctx = Cairo::Context->create($self->surface); if(defined($self->antialias_mode)) { $ctx->set_antialias($self->antialias_mode); } return $ctx; } ); has 'format' => ( is => 'ro', isa => 'Graphics::Primitive::Driver::Cairo::Format', default => sub { 'PNG' } ); has 'surface' => ( is => 'rw', clearer => 'clear_surface', lazy => 1, default => sub { # Lazily create our surface based on the format they are required # to've chosen when creating this object my $self = shift; my $surface; my $width = $self->width; my $height = $self->height; if(uc($self->format) eq 'PNG') { $surface = Cairo::ImageSurface->create( 'argb32', $width, $height ); } elsif(uc($self->format) eq 'PDF') { croak('Your Cairo does not have PostScript support!') unless Cairo::HAS_PDF_SURFACE; $surface = Cairo::PdfSurface->create_for_stream( sub { $self->{DATA} .= $_[1] }, $self, $width, $height # $self->can('append_surface_data'), $self, $width, $height ); } elsif(uc($self->format) eq 'PS') { croak('Your Cairo does not have PostScript support!') unless Cairo::HAS_PS_SURFACE; $surface = Cairo::PsSurface->create_for_stream( sub { $self->{DATA} .= $_[1] }, $self, $width, $height # $self->can('append_surface_data'), $self, $width, $height ); } elsif(uc($self->format) eq 'SVG') { croak('Your Cairo does not have SVG support!') unless Cairo::HAS_SVG_SURFACE; $surface = Cairo::SvgSurface->create_for_stream( sub { $self->{DATA} .= $_[1] }, $self, $width, $height # $self->can('append_surface_data'), $self, $width, $height ); } else { croak("Unknown format '".$self->format."'"); } return $surface; } ); sub data { my ($self) = @_; my $cr = $self->cairo; if(uc($self->format) eq 'PNG') { my $buff; $self->surface->write_to_png_stream(sub { my ($closure, $data) = @_; $buff .= $data; }); return $buff; } $cr->show_page; $cr = undef; $self->clear_cairo; $self->clear_surface; return $self->{DATA}; } around('draw', sub { my ($cont, $class, $comp) = @_; my $cairo = $class->cairo; $cairo->save; $cairo->translate($comp->origin->x, $comp->origin->y); $cairo->rectangle(0, 0, $comp->width, $comp->height); $cairo->clip; $cont->($class, $comp); $cairo->restore; }); sub write { my ($self, $file) = @_; my $fh = IO::File->new($file, 'w') or die("Unable to open '$file' for writing: $!"); $fh->binmode; $fh->print($self->data); $fh->close; } sub _draw_component { my ($self, $comp) = @_; my $width = $comp->width; my $height = $comp->height; my $context = $self->cairo; if(defined($comp->background_color)) { my ($mt, $mr, $mb, $ml) = $comp->margins->as_array; $context->set_source_rgba($comp->background_color->as_array_with_alpha); $context->rectangle( $mr, $mt, $comp->width - $mr - $ml, $comp->height - $mt - $mb ); $context->fill; } if(defined($comp->border)) { my $border = $comp->border; if($border->homogeneous) { # Don't bother if there's no width if($border->top->width) { $self->_draw_simple_border($comp); } } else { $self->_draw_complex_border($comp); } } } sub _draw_complex_border { my ($self, $comp) = @_; my ($mt, $mr, $mb, $ml) = $comp->margins->as_array; my $context = $self->cairo; my $border = $comp->border; my $width = $comp->width; my $height = $comp->height; my $bt = $border->top; my $thalf = (defined($bt) && defined($bt->color)) ? $bt->width / 2: 0; my $br = $border->right; my $rhalf = (defined($br) && defined($br->color)) ? $br->width / 2: 0; my $bb = $border->bottom; my $bhalf = (defined($bb) && defined($bb->color)) ? $bb->width / 2 : 0; my $bl = $border->left; my $lhalf = (defined($bl) && defined($bl->color)) ? $bl->width / 2 : 0; if($thalf) { $context->move_to($ml, $mt + $thalf); $context->set_source_rgba($bt->color->as_array_with_alpha); $context->set_line_width($bt->width); $context->rel_line_to($width - $mr - $ml, 0); my $dash = $bt->dash_pattern; if(defined($dash) && scalar(@{ $dash })) { $context->set_dash(0, @{ $dash }); } $context->stroke; $context->set_dash(0, []); } if($rhalf) { $context->move_to($width - $mr - $rhalf, $mt); $context->set_source_rgba($br->color->as_array_with_alpha); $context->set_line_width($br->width); $context->rel_line_to(0, $height - $mb); my $dash = $br->dash_pattern; if(defined($dash) && scalar(@{ $dash })) { $context->set_dash(0, @{ $dash }); } $context->stroke; $context->set_dash(0, []); } if($bhalf) { $context->move_to($width - $mr, $height - $bhalf - $mb); $context->set_source_rgba($bb->color->as_array_with_alpha); $context->set_line_width($bb->width); $context->rel_line_to(-($width - $mr - $ml), 0); my $dash = $bb->dash_pattern; if(defined($dash) && scalar(@{ $dash })) { $context->set_dash(0, @{ $dash }); } $context->stroke; } if($lhalf) { $context->move_to($ml + $lhalf, $mt); $context->set_source_rgba($bl->color->as_array_with_alpha); $context->set_line_width($bl->width); $context->rel_line_to(0, $height - $mb); my $dash = $bl->dash_pattern; if(defined($dash) && scalar(@{ $dash })) { $context->set_dash(0, @{ $dash }); } $context->stroke; $context->set_dash(0, []); } } sub _draw_simple_border { my ($self, $comp) = @_; my $context = $self->cairo; my $border = $comp->border; my $top = $border->top; my $bswidth = $top->width; $context->set_source_rgba($top->color->as_array_with_alpha); my @margins = $comp->margins->as_array; $context->set_line_width($bswidth); $context->set_line_cap($top->line_cap); $context->set_line_join($top->line_join); $context->new_path; my $swhalf = $bswidth / 2; my $width = $comp->width; my $height = $comp->height; my $dash = $top->dash_pattern; if(defined($dash) && scalar(@{ $dash })) { $context->set_dash(0, @{ $dash }); } $context->rectangle( $margins[3] + $swhalf, $margins[0] + $swhalf, $width - $bswidth - $margins[3] - $margins[1], $height - $bswidth - $margins[2] - $margins[0] ); $context->stroke; # Reset dashing $context->set_dash(0, []); } sub _draw_textbox { my ($self, $comp) = @_; return unless defined($comp->text); $self->_draw_component($comp); my $bbox = $comp->inside_bounding_box; my $height = $bbox->height; my $height2 = $height / 2; my $width = $bbox->width; my $width2 = $width / 2; my $halign = $comp->horizontal_alignment; my $valign = $comp->vertical_alignment; my $context = $self->cairo; my $font = $comp->font; my $fsize = $font->size; $context->select_font_face( $font->face, $font->slant, $font->weight ); $context->set_font_size($fsize); my $options = Cairo::FontOptions->create; $options->set_antialias($font->antialias_mode); $options->set_subpixel_order($font->subpixel_order); $options->set_hint_style($font->hint_style); $options->set_hint_metrics($font->hint_metrics); $context->set_font_options($options); my $angle = $comp->angle; $context->set_source_rgba($comp->color->as_array_with_alpha); my $lh = $comp->line_height; $lh = $fsize unless(defined($lh)); my $yaccum = $bbox->origin->y; foreach my $line (@{ $comp->layout->lines }) { my $text = $line->{text}; my $tbox = $line->{box}; my $o = $tbox->origin; my $bbo = $bbox->origin; my $twidth = $tbox->width; my $theight = $tbox->height; my $x = $bbox->origin->x + $o->x; my $ydiff = $theight + $o->y; my $xdiff = $twidth + $o->x; my $realh = $theight + $ydiff; my $realw = $twidth + $xdiff; my $theight2 = $realh / 2; my $twidth2 = $twidth / 2; my $y = $yaccum + $theight; $context->save; if($angle) { my $twidth2 = $twidth / 2; my $cwidth2 = $width / 2; my $cheight2 = $height / 2; $context->translate($cwidth2, $cheight2); $context->rotate($angle); $context->translate(-$cwidth2, -$cheight2); $context->move_to($cwidth2 - $twidth2, $cheight2 + $theight / 3.5); $context->show_text($text); } else { if($halign eq 'right') { $x += $width - $twidth; } elsif($halign eq 'center') { $x += $width2 - $twidth2; } if($valign eq 'bottom') { $y = $height - $ydiff; } elsif($valign eq 'center') { $y += $height2 - $theight2; } else { $y -= $ydiff; } $context->move_to($x, $y); $context->show_text($text); } $context->restore; $yaccum += $lh; } } sub _draw_arc { my ($self, $arc) = @_; my $context = $self->cairo; my $o = $arc->origin; if($arc->angle_start > $arc->angle_end) { $context->arc_negative( $o->x, $o->y, $arc->radius, $arc->angle_start, $arc->angle_end ); } else { $context->arc( $o->x, $o->y, $arc->radius, $arc->angle_start, $arc->angle_end ); } } sub _draw_bezier { my ($self, $bezier) = @_; my $context = $self->cairo; my $start = $bezier->start; my $end = $bezier->end; my $c1 = $bezier->control1; my $c2 = $bezier->control2; $context->curve_to($c1->x, $c1->y, $c2->x, $c2->y, $end->x, $end->y); } sub _draw_canvas { my ($self, $comp) = @_; $self->_draw_component($comp); foreach (@{ $comp->paths }) { $self->_draw_path($_->{path}, $_->{op}); } } sub _draw_circle { my ($self, $circle) = @_; my $context = $self->cairo; my $o = $circle->origin; $context->new_sub_path; $context->arc( $o->x, $o->y, $circle->radius, 0, pi2 ); } sub _draw_ellipse { my ($self, $ell) = @_; my $cairo = $self->cairo; my $o = $ell->origin; $cairo->new_sub_path; $cairo->save; $cairo->translate($o->x, $o->y); $cairo->scale($ell->width / 2, $ell->height / 2); $cairo->arc( $o->x, $o->y, 1, 0, pi2 ); $cairo->restore; } sub _draw_image { my ($self, $comp) = @_; $self->_draw_component($comp); my $cairo = $self->cairo; $cairo->save; my $imgs = Cairo::ImageSurface->create_from_png($comp->image); my $bb = $comp->inside_bounding_box; my $bumpx = 0; my $bumpy = 0; if($comp->horizontal_alignment eq 'center') { $bumpx = $bb->width / 2; if(defined($comp->scale)) { $bumpx -= $comp->scale->[0] * ($imgs->get_width / 2); } else { $bumpx -= $imgs->get_width / 2; } } elsif($comp->horizontal_alignment eq 'right') { $bumpx = $bb->width; if(defined($comp->scale)) { $bumpx -= $comp->scale->[0] * $imgs->get_width; } else { $bumpx -= $imgs->get_width; } } if($comp->vertical_alignment eq 'center') { $bumpy = $bb->height / 2; if(defined($comp->scale)) { $bumpy -= $comp->scale->[1] * ($imgs->get_height / 2); } else { $bumpy -= $imgs->get_height / 2; } } elsif($comp->vertical_alignment eq 'bottom') { $bumpy = $bb->height; if(defined($comp->scale)) { $bumpy -= $comp->scale->[1] * $imgs->get_height; } else { $bumpy -= $imgs->get_height; } } $cairo->translate($bb->origin->x + $bumpx, $bb->origin->y + $bumpy); $cairo->rectangle(0, 0, $imgs->get_width, $imgs->get_height); $cairo->clip; if(defined($comp->scale)) { $cairo->scale($comp->scale->[0], $comp->scale->[1]); } $cairo->rectangle( 0, 0, $imgs->get_width, $imgs->get_height ); $cairo->set_source_surface($imgs, 0, 0); $cairo->fill; $cairo->restore; } sub _draw_path { my ($self, $path, $op) = @_; my $context = $self->cairo; # If preserve count is set we've "preserved" a path that's made up # of X primitives. Set the sentinel to the the count so we skip that # many primitives my $pc = $self->_preserve_count; if($pc) { $self->_preserve_count(0); } else { $context->new_path; } my $pcount = $path->primitive_count; for(my $i = $pc; $i < $pcount; $i++) { my $prim = $path->get_primitive($i); my $hints = $path->get_hint($i); if(defined($hints)) { unless($hints->{contiguous}) { my $ps = $prim->point_start; $context->move_to( $ps->x, $ps->y ); } } # FIXME Check::ISA if($prim->isa('Geometry::Primitive::Line')) { $self->_draw_line($prim); } elsif($prim->isa('Geometry::Primitive::Rectangle')) { $self->_draw_rectangle($prim); } elsif($prim->isa('Geometry::Primitive::Arc')) { $self->_draw_arc($prim); } elsif($prim->isa('Geometry::Primitive::Bezier')) { $self->_draw_bezier($prim); } elsif($prim->isa('Geometry::Primitive::Circle')) { $self->_draw_circle($prim); } elsif($prim->isa('Geometry::Primitive::Ellipse')) { $self->_draw_ellipse($prim); } elsif($prim->isa('Geometry::Primitive::Polygon')) { $self->_draw_polygon($prim); } } if($op->isa('Graphics::Primitive::Operation::Stroke')) { $self->_do_stroke($op); } elsif($op->isa('Graphics::Primitive::Operation::Fill')) { $self->_do_fill($op); } if($op->preserve) { $self->_preserve_count($path->primitive_count); } } sub _draw_line { my ($self, $line) = @_; my $context = $self->cairo; my $end = $line->end; $context->line_to($end->x, $end->y); } sub _draw_polygon { my ($self, $poly) = @_; my $context = $self->cairo; for(my $i = 1; $i < $poly->point_count; $i++) { my $p = $poly->get_point($i); $context->line_to($p->x, $p->y); } $context->close_path; } sub _draw_rectangle { my ($self, $rect) = @_; my $context = $self->cairo; $context->rectangle( $rect->origin->x, $rect->origin->y, $rect->width, $rect->height ); } sub _do_fill { my ($self, $fill) = @_; my $context = $self->cairo; my $paint = $fill->paint; # FIXME Check::ISA? if($paint->isa('Graphics::Primitive::Paint::Gradient')) { my $patt; if($paint->isa('Graphics::Primitive::Paint::Gradient::Linear')) { $patt = Cairo::LinearGradient->create( $paint->line->start->x, $paint->line->start->y, $paint->line->end->x, $paint->line->end->y, ); } elsif($paint->isa('Graphics::Primitive::Paint::Gradient::Radial')) { $patt = Cairo::RadialGradient->create( $paint->start->origin->x, $paint->start->origin->y, $paint->start->radius, $paint->end->origin->x, $paint->end->origin->y, $paint->end->radius ); } else { croak('Unknown gradient type: '.ref($paint)); } foreach my $stop ($paint->stops) { my $color = $paint->get_stop($stop); $patt->add_color_stop_rgba( $stop, $color->red, $color->green, $color->blue, $color->alpha ); } $context->set_source($patt); } elsif($paint->isa('Graphics::Primitive::Paint::Solid')) { $context->set_source_rgba($paint->color->as_array_with_alpha); } if($fill->preserve) { $context->fill_preserve; } else { $context->fill; } } sub _do_stroke { my ($self, $stroke) = @_; my $br = $stroke->brush; my $context = $self->cairo; $context->set_source_rgba($br->color->as_array_with_alpha); $context->set_line_cap($br->line_cap); $context->set_line_join($br->line_join); $context->set_line_width($br->width); my $dash = $br->dash_pattern; if(defined($dash) && scalar(@{ $dash })) { $context->set_dash(0, @{ $dash }); } if($stroke->preserve) { $context->stroke_preserve; } else { $context->stroke; } # Reset dashing $context->set_dash(0, []); } sub _finish_page { my ($self) = @_; my $context = $self->cairo; $context->show_page; } sub _resize { my ($self, $width, $height) = @_; # Don't resize unless we have to if(($self->width != $width) || ($self->height != $height)) { $self->surface->set_size($width, $height); } } sub get_text_bounding_box { my ($self, $tb, $text) = @_; my $context = $self->cairo; my $font = $tb->font; unless(defined($text)) { $text = $tb->text; } $context->new_path; my $fsize = $font->size; my $options = Cairo::FontOptions->create; $options->set_antialias($font->antialias_mode); $options->set_subpixel_order($font->subpixel_order); $options->set_hint_style($font->hint_style); $options->set_hint_metrics($font->hint_metrics); $context->set_font_options($options); # my $key = "$text||".$font->face.'||'.$font->slant.'||'.$font->weight.'||'.$fsize; # If our text + font key is found, return the box we already made. # if(exists($self->{TBCACHE}->{$key})) { # return ($self->{TBCACHE}->{$key}->[0], $self->{TBCACHE}->{$key}->[1]); # } # my @exts; my $exts; if($text eq '') { # Catch empty lines. There's no sense trying to get it's height. We # just set it to the height of the font and move on. # @exts = (0, -$font->size, 0, 0); $exts->{y_bearing} = 0; $exts->{x_bearing} = 0; $exts->{x_advance} = 0; $exts->{width} = 0; $exts->{height} = $fsize; } else { $context->select_font_face( $font->face, $font->slant, $font->weight ); $context->set_font_size($fsize); $exts = $context->text_extents($text); } my $tbr = Geometry::Primitive::Rectangle->new( origin => Geometry::Primitive::Point->new( x => $exts->{x_bearing},#$exts[0], y => $exts->{y_bearing},#$exts[1], ), width => $exts->{width} + $exts->{x_bearing} + 1,#abs($exts[2]) + abs($exts[0]), height => $exts->{height},#$tbsize ); my $cb = $tbr; if($tb->angle) { $context->save; my $tw2 = $tb->width / 2; my $th2 = $tb->height / 2; $context->translate($tw2, $th2); $context->rotate($tb->angle); $context->translate(-$tw2, -$th2); my ($rw, $rh) = $self->_get_bounding_box($context, $exts); $cb = Geometry::Primitive::Rectangle->new( origin => $tbr->origin, width => $rw, height => $rh ); $context->restore; } # $self->{TBCACHE}->{$key} = [ $cb, $tbr ]; return ($cb, $tbr); } sub get_textbox_layout { my ($self, $comp) = @_; my $tl = Graphics::Primitive::Driver::Cairo::TextLayout->new( component => $comp ); $tl->layout($self); return $tl; } sub reset { my ($self) = @_; $self->clear_cairo; } sub _get_bounding_box { my ($self, $context, $exts) = @_; my $lw = $exts->{width} + abs($exts->{x_bearing}); my $lh = $exts->{height} + abs($exts->{y_bearing}); my $matrix = $context->get_matrix; my @corners = ([0,0], [$lw,0], [$lw,$lh], [0,$lh]); # Transform each of the four corners, the find the maximum X and Y # coordinates to create a bounding box my @points; foreach my $pt (@corners) { my ($x, $y) = $matrix->transform_point($pt->[0], $pt->[1]); push(@points, [ $x, $y ]); } my $maxX = $points[0]->[0]; my $maxY = $points[0]->[1]; my $minX = $points[0]->[0]; my $minY = $points[0]->[1]; foreach my $pt (@points) { if($pt->[0] > $maxX) { $maxX = $pt->[0]; } elsif($pt->[0] < $minX) { $minX = $pt->[0]; } if($pt->[1] > $maxY) { $maxY = $pt->[1]; } elsif($pt->[1] < $minY) { $minY = $pt->[1]; } } my $bw = $maxX - $minX; my $bh = $maxY - $minY; return ($bw, $bh); } no Moose; 1; __END__ =pod =head1 NAME Graphics::Primitive::Driver::Cairo - Cairo backend for Graphics::Primitive =head1 VERSION version 0.47 =head1 SYNOPSIS use Graphics::Primitive::Component; use Graphics::Primitive::Driver::Cairo; my $driver = Graphics::Primitive::Driver::Cairo->new; my $container = Graphics::Primitive::Container->new( width => 800, height => 600 ); my $black = Graphics::Primitive::Color->new(red => 0, green => 0, blue => 0); $container->border->width(1); $container->border->color($black); $container->padding( Graphics::Primitive::Insets->new(top => 5, bottom => 5, left => 5, right => 5) ); my $comp = Graphics::Primitive::Component->new; $comp->background_color($black); $container->add_component($comp, 'c'); my $lm = Layout::Manager::Compass->new; $lm->do_layout($container); my $driver = Graphics::Primitive::Driver::Cairo->new( format => 'PDF' ); $driver->draw($container); $driver->write('/Users/gphat/foo.pdf'); =head1 DESCRIPTION This module draws Graphics::Primitive objects using Cairo. =head1 IMPLEMENTATION DETAILS =over 4 =item B Borders are drawn clockwise starting with the top one. Since cairo can't do line-joins on different colored lines, each border overlaps those before it. This is not the way I'd like it to work, but i'm opting to fix this later. Consider yourself warned. =back =head1 ATTRIBUTES =head2 antialias_mode Set/Get the antialias mode of this driver. Options are default, none, gray and subpixel. =head2 cairo This driver's Cairo::Context object =head2 format Get the format for this driver. =head2 surface Get/Set the surface on which this driver is operating. =head1 METHODS =head2 data Get the data in a scalar for this driver. =head2 write ($file) Write this driver's data to the specified file. =head2 get_text_bounding_box ($font, $text, $angle) Returns two L that encloses the supplied text. The origin's x and y maybe negative, meaning that the glyphs in the text extending left of x or above y. The first rectangle is the bounding box required for a container that wants to contain the text. The second box is only useful if an optional angle is provided. This second rectangle is the bounding box of the un-rotated text that allows for a controlled rotation. If no angle is supplied then the two rectangles are actually the same object. If the optional angle is supplied the text will be rotated by the supplied amount in radians. =head2 get_textbox_layout ($tb) Returns a L for the supplied textbox. =head2 reset Reset the driver. =head2 draw Draws the specified component. Container's components are drawn recursively. =head1 ACKNOWLEDGEMENTS Danny Luna =head1 AUTHOR Cory G Watson =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2016 by Cold Hard Code, LLC. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut Cairo000755000765000024 013024374634 25522 5ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/lib/Graphics/Primitive/DriverTextLayout.pm100644000765000024 720113024374634 30342 0ustar00gphatstaff000000000000Graphics-Primitive-Driver-Cairo-0.47/lib/Graphics/Primitive/Driver/Cairopackage Graphics::Primitive::Driver::Cairo::TextLayout; $Graphics::Primitive::Driver::Cairo::TextLayout::VERSION = '0.47'; use Moose; # ABSTRACT: Text layout engine use Graphics::Primitive::TextBox; with 'Graphics::Primitive::Driver::TextLayout'; use Text::Flow; has 'lines' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); sub layout { my ($self, $driver) = @_; my $comp = $self->component; my $font = $comp->font; my $width = $self->height == -1 ? 0 : $comp->width ? $comp->width : $comp->minimum_width; my $text = $comp->text; unless($self->width) { $self->width($width); } unless(defined($text)) { $self->height(0); return; } my $size; my $flow = Text::Flow->new( check_height => sub { return 1; }, wrapper => Text::Flow::Wrap->new( check_width => sub { my $str = shift; my $r = $driver->get_text_bounding_box($comp, $str); unless($width) { # Catch the "no width" case. return 1; } if($r->width > $width) { return 0; } return 1; } ) ); my @text = $flow->flow($text); my $p = $text[0]; my @lines = split(/\n/, $p); my $height = 0; $width = 0; foreach my $l (@lines) { my ($cb, $tb) = $driver->get_text_bounding_box($comp); push(@{ $self->lines }, { text => $l, box => $tb, cb => $cb }); $height += $cb->height; $width += $cb->width; } $self->height($height); if(!defined($self->width) || ($self->width == 0)) { $self->width($width); } } sub slice { my ($self, $offset, $size) = @_; unless(defined($size)) { $size = $self->height; } my $font = $self->component->font; my $lh = $font->size; # my $lh = defined($self->line_height) # ? $self->line_height : $self->font->size; my @new; my $accum = 0; my $found = 0; for(my $i = 0; $i < scalar(@{ $self->lines }); $i++) { my $l = $self->lines->[$i]; my $llh = $l->{cb}->height; # If the 'local' line height is < the overall line height, use the # overall one. if($llh < $lh) { $llh = $lh; } if($accum < $offset) { $accum += $llh; next; } if(($accum + $llh) <= ($offset + $size)) { push(@new, $l); $accum += $llh; $found += $llh; } } return Graphics::Primitive::TextBox->new( lines => \@new, minimum_width => $self->width, minimum_height => $found ); } __PACKAGE__->meta->make_immutable; no Moose; 1; __END__ =pod =head1 NAME Graphics::Primitive::Driver::Cairo::TextLayout - Text layout engine =head1 VERSION version 0.47 =head1 SYNOPSIS my $driver = Graphics::Primitive::Driver::Cairo->new(format => 'PDF'); my $comp = Graphics::Primitive::TextBox->new; my $tl = $driver->get_textbox_layout($comp); =head1 DESCRIPTION Implements L. Please refer to it's documentation for usage. =head1 IMPLEMENTATION This text layout engine uses L and L's "toy text" API to layout text. =head1 AUTHOR Cory G Watson =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2016 by Cold Hard Code, LLC. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut