├── .gitignore ├── .mm ├── bin └── magic ├── AUTHORS ├── LICENSE ├── Makefile ├── README ├── README.md ├── bounds.c ├── cadence.c ├── gap.c ├── gimpspinscale.c ├── gimpspinscale.h ├── glyph.c ├── glyph.h ├── gray.c ├── ipsumat.c ├── kernagic-debug-osx.sh ├── kernagic-debug-osx.txt ├── kernagic.c ├── kernagic.h ├── kernagic.nsis ├── lib-plist.c ├── original.c ├── render.c ├── rythm.c ├── tests ├── Makefile └── reference │ ├── bounds.png │ ├── cadence.png │ ├── gap.png │ └── original.png ├── ufo-glyph-parser.c └── ui-gtk.c /.gitignore: -------------------------------------------------------------------------------- 1 | kernagic 2 | kernagic-installer.exe 3 | kernagic.exe 4 | dlls/*.dll 5 | -------------------------------------------------------------------------------- /.mm/bin: -------------------------------------------------------------------------------- 1 | # bin 2 | 3 | BINARY ?= $(PROJECT_NAME) 4 | BIN_CFILES ?= $(wildcard *.c) 5 | 6 | BIN_TARGETS = $(BINARY) 7 | 8 | BIN_CFLAGS += $(CFLAGS) $(LIB_CFLAGS) 9 | BIN_LD_FLAGS += $(LD_FLAGS) $(LIB_LD_FLAGS) 10 | 11 | BIN_CFLAGS += $(shell test "$(BIN_PKGMODULES)" 2>/dev/null && pkg-config $(BIN_PKGMODULES) --cflags) 12 | BIN_LD_FLAGS += $(shell test "$(BIN_PKGMODULES)" 2>/dev/null && pkg-config $(BIN_PKGMODULES) --libs) 13 | 14 | $(BINARY): $(BIN_CFILES) $(LIBNAME_A) 15 | @echo "CCLD" $@; $(CC) $(BIN_CFILES) $(SYSROOT) -I .. $(BIN_CFLAGS) $(BIN_LD_FLAGS) \ 16 | $(LIBNAME_A) -o $@ 17 | 18 | $(BINARY).efence: $(BIN_CFILES) $(LIBNAME_A) 19 | @echo "CCLD" $@; $(CC) $(BIN_CFILES) $(SYSROOT) -I .. $(BIN_CFLAGS) $(BIN_LD_FLAGS) \ 20 | $(LIBNAME_A) -lefence -o $@ 21 | 22 | all: $(BIN_TARGETS) 23 | CLEAN_FILES += $(BIN_TARGETS) $(BINARY).efence 24 | buildapi-install-runtime: _install_bin 25 | install: _install_bin 26 | uninstall: _uninstall_bin 27 | _install_bin: $(BIN_TARGETS) 28 | install -d $(DESTDIR)$(PREFIX)/bin 29 | install $(BIN_TARGETS) $(DESTDIR)$(PREFIX)/bin 30 | _uninstall_bin: 31 | sudo rm -f $(DESTDIR)$(PREFIX)/bin/$(BINARY) 32 | 33 | help: _binhelp 34 | _binhelp: 35 | @echo "$(BINARY) build binary" 36 | @echo "$(BINARY).efence build binary with efence support" 37 | -------------------------------------------------------------------------------- /.mm/magic: -------------------------------------------------------------------------------- 1 | # PROJECT_NAME = my project 2 | all: 3 | 4 | CFLAGS += -Wall -Wextra 5 | #CFLAGS += -Ofast -g -rdynamic 6 | #CFLAGS += -g -O2 -rdynamic 7 | CFLAGS += -fPIC 8 | #CFLAGS += -fomit-frame-pointer 9 | CFLAGS += -funroll-loops -fforce-addr -ftracer -fpeel-loops -fmerge-all-constants 10 | CFLAGS += -Wno-unused-parameter -Wno-sign-compare 11 | CFLAGS += -I. -D_XOPEN_SOURCE=500 12 | CFLAGS += -std=c99 13 | 14 | # icky, should perhaps be moved to a commen start include 15 | 16 | P64 = `export | grep m64 > /dev/null && echo 64` 17 | 18 | clean: 19 | rm -f $(CLEAN_FILES) 20 | check: all 21 | make -C tests check 22 | short-check: all 23 | make -C tests update 24 | 25 | # defaults in case they are not set 26 | PROJECT_NAME ?= $(shell basename `pwd`) 27 | PREFIX ?= /usr/local 28 | 29 | all: subdirs 30 | subdirs: 31 | @for a in $(SUBDIRS);do $(MAKE) -C $$a;done 32 | 33 | firsthelp: 34 | @echo "help:" 35 | 36 | help: firsthelp 37 | @echo "----" 38 | 39 | foo: Makefile 40 | cp Makefile tmpmk 41 | for a in `cat tmpmk | grep "\.mm/" | sed 's/include//'`;do \ 42 | csplit tmpmk "/include $$a/"; cat xx00 $$a xx01 > tmpmk2; cp tmpmk2 tmpmk;done 43 | 44 | -include config 45 | 46 | _name: 47 | @echo $(PROJECT_NAME) 48 | _pkgtest: 49 | @echo "pkg-config deps: $(LIB_PKGMODULES)$(BIN_PKGMODULES)" 50 | @if (test "$(LIB_PKGMODULES)$(BIN_PKGMODULES)"); then pkg-config $(LIB_PKGMODULES) $(BIN_PKGMODULES) --libs --cflags; fi 51 | 52 | 53 | LIB_MAJOR_VERSION ?= 0 54 | LIB_MINOR_VERSION ?= 0 55 | LIB_MICRO_VERSION ?= 0 56 | 57 | LIB_VERSION=$(LIB_MAJOR_VERSION).$(LIB_MINOR_VERSION).$(LIB_MICRO_VERSION) 58 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Øyvind Kolås 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BIN_PKGMODULES=cairo gtk+-2.0 2 | PROJECT_NAME = kernagic 3 | 4 | CFLAGS += -O2 -g -Wall -Wextra 5 | LD_FLAGS += -lm 6 | 7 | include .mm/magic 8 | include .mm/bin 9 | 10 | # Install mingw-w64 for your distro this should contain the 11 | # i686-w64-mingw32-gcc compiler which is used for mingw cross-compiling of 12 | # 32bit windows applications. 13 | # 14 | # dependencies are downloaded automatically from files described at 15 | # http://www.gtk.org/download/win32.php 16 | 17 | gtk+-mingw-bundle/gtk+-bundle_2.24.10-20120208_win32.zip: 18 | (cd gtk+-mingw-bundle/; wget http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.24/gtk+-bundle_2.24.10-20120208_win32.zip; ) 19 | 20 | gtk+-mingw-bundle/unpacked.stamp: gtk+-mingw-bundle/gtk+-bundle_2.24.10-20120208_win32.zip 21 | (cd gtk+-mingw-bundle;\ 22 | unzip gtk+-bundle_2.24.10-20120208_win32.zip;\ 23 | touch unpacked.stamp) 24 | 25 | $(PROJECT_NAME).exe: *.c *.h Makefile gtk+-mingw-bundle/unpacked.stamp 26 | i686-w64-mingw32-gcc *.c -Wall -Wextra -Wno-unused-parameter \ 27 | -Igtk+-mingw-bundle/lib/glib-2.0/include \ 28 | -Igtk+-mingw-bundle/lib/gtk-2.0/include \ 29 | -Igtk+-mingw-bundle/include/glib-2.0/ \ 30 | -Igtk+-mingw-bundle/include/gtk-2.0/ \ 31 | -Igtk+-mingw-bundle/include/cairo/ \ 32 | -Igtk+-mingw-bundle/include/pango-1.0/ \ 33 | -Igtk+-mingw-bundle/include/gdk-pixbuf-2.0/ \ 34 | -Igtk+-mingw-bundle/include/atk-1.0/ \ 35 | \ 36 | -lm -O2 -std=c99 -mms-bitfields \ 37 | \ 38 | -mwindows -Wl,--subsystem,windows \ 39 | \ 40 | -Lgtk+-mingw-bundle/lib -lgio-2.0.dll \ 41 | -lglib-2.0.dll -lgobject-2.0.dll -lpango-1.0.dll -lpangocairo-1.0.dll \ 42 | -lgdk-win32-2.0.dll -lgtk-win32-2.0.dll -lcairo.dll \ 43 | \ 44 | -o $@ && echo 45 | i686-w64-mingw32-strip $@ 46 | 47 | # the kernagic.exe binary should work if you drop it into a folder with all 48 | # the dlls downloaded above. To make the installer; only the neccesary dlls 49 | # should be selected and placed in a dll directory. Then typing make 50 | # kernagic-installer.exe would bundle these dls's with a freshly built exe 51 | # producing an installer. 52 | 53 | kernagic-installer.exe: kernagic.exe kernagic.nsis 54 | @makensis kernagic.nsis 55 | @chmod a+x $@ 56 | @echo 57 | @ls -sh *.exe 58 | @echo 59 | @sha1sum *.exe 60 | 61 | clean: clean-extra 62 | clean-extra: 63 | rm -f *.exe kernagic 64 | rm -f tests/output/* 65 | 66 | sync: 67 | cp tests/output/* tests/reference 68 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Kernagic 2 | ======== 3 | 4 | Kernagic is a semi-automatic tool for spacing fonts. It provides ways to 5 | interactively preview global and local changes to the glyphs in a UFO font - 6 | these changes can act as a starting point for further manual fine tuning. 7 | 8 | The development began in Madrid at the Libre Graphics Meeting 2013, when Øyvind 9 | 'Pippin' Kolås (github.com/hodefoting pippin@gimp.org pippin.gimp.org) decided 10 | to hack around the automatic font spacing challenge. There, Dave Crossland 11 | introduced him to Frank Blokland's PhD research, published on the [Letter 12 | Model](http://lettermodel.org) website. The initial 'averaging' approach that 13 | Pippin explored (described in the [Interactivos 13 14 | Book](https://gitorious.org/libregraphicsmag/interactivosbook)) was later 15 | discarded, in favour of an approach of stem rhythm placement that deviates from 16 | Frank Blokland's theories. 17 | 18 | To fit into a diverse set of workflows, Kernagic uses UFO fonts for input. 19 | UFO can act as a superset of other formats, and tools like 20 | [Fontforge](http://github.com/fontforge/fontforge/) can be used for 21 | converting to and from it. 22 | 23 | The snap gap method relies on automatically detected rhythm points, if the 24 | rhythm point detection works poorly with your font, or you want to override 25 | spacing decisions, you can insert your own rhythm points by clicking within 26 | the x-height of the glyph to change in the preview; rhythm point overrides are 27 | saved in the individual glyph files. Clicking below the x-height of a glyph 28 | removes custom overrides; clicking above it inserts a single rhythm point to 29 | be used for both left and right sides of the glyphs. 30 | 31 | If metrics are determined, they can be saved back into the UFO (shifting the x 32 | co-ordinates of all points in all glyphs to change the left bearings, and 33 | setting a new advance width to set the right bearings.) 34 | 35 | For help about the command line interface, `kernagic --help` provides further 36 | documentation. 37 | 38 | Spacing Methods 39 | =============== 40 | 41 | For visualizing the effect of applied spacing, kernagic allow switch the 42 | rendering from showing original/none/applied metrics. 43 | 44 | Original 45 | -------- 46 | 47 | Show the original font, (included to be able to quickly switch between the 48 | original font and the resulting spacing, pressing F1, F2, F3, .. switches 49 | between the different methods.) 50 | 51 | An additional option is available here to proportionally scale the existing 52 | bearings. 53 | 54 | Snap gap 55 | -------- 56 | 57 | This methods permits specifying a desired gap between left and right rhythm 58 | points of glyphs. The bearings indicated by the gap is also snapped to grid in 59 | such a manner that the advance of the glyphs is a multiple of the snap value. 60 | 61 | Installation 62 | ===================== 63 | 64 | From Source 65 | --------------- 66 | 67 | ### Windows 68 | 69 | It is possible to compile Kernagic for Windows. TODO: Complete this 70 | 71 | ### Mac OS X 72 | 73 | 1. Install XCode. Either with the App Store, or by registering as an Apple 74 | Developer and installing the appropriate XCode version from the 75 | [Developer website](https://developer.apple.com/downloads/index.action) 76 | (Select 'Developer Tools' and type 'Xcode' in the search box.) 77 | 78 | 2. Install [Homebrew](http://brew.sh) 79 | 80 | 3. Run Terminal and then type `brew install kernagic --HEAD;` and hit enter 81 | 82 | 4. Run Terminal and then type `kernagic` and hit enter 83 | 84 | Usage 85 | ===================== 86 | 87 | A video on how to use the program is at: TODO 88 | 89 | Running `kernagic --ipsumat` is a way to run the ipsum generator stand-alone as a command line utility. Run `kernagic --ipsumat --help` for more information. 90 | 91 | 92 | Suggested workflow for snap gap method 93 | ---------------------------------------- 94 | 95 | 1) set the snap to 0 96 | 2) adjust the gap until the type is as loose/tight as you want it 97 | 3) adjust snap, so that it is at a high value; where the type hasn't 98 | fallen totally apart 99 | 4) manually adjust remaining glyphs still in need of tuning. 100 | 101 | Found a crash? 102 | --------------- 103 | 104 | A shell script, `kernagic-debug-osx.sh`, is included that allows you to produce 105 | backtraces conveniently on Mac OS X. 106 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | README -------------------------------------------------------------------------------- /bounds.c: -------------------------------------------------------------------------------- 1 | #include "kernagic.h" 2 | 3 | static void bounds_each (Glyph *lg, GtkProgressBar *progress) 4 | { 5 | /* 6 | we don't need to do anything, since this is the default 7 | 8 | lg->right_bearing = 0; 9 | lg->left_bearing = 0; 10 | */ 11 | } 12 | 13 | static KernagicMethod method = {"bounds", 14 | NULL, bounds_each, NULL}; 15 | 16 | KernagicMethod *kernagic_bounds = &method; 17 | -------------------------------------------------------------------------------- /cadence.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "kernagic.h" 4 | 5 | typedef struct Cadence 6 | { 7 | int left; 8 | char *utf8; 9 | int right; 10 | int flags; 11 | } Cadence; 12 | 13 | #define LEFT_EXTREME (1<<0) 14 | #define RIGHT_EXTREME (1<<1) 15 | #define RIGHT_STEM (1<<2) 16 | #define LEFT_STEM (1<<3) 17 | #define BOTH_EXTREME (LEFT_EXTREME | RIGHT_EXTREME) 18 | #define BOTH_STEM (LEFT_STEM | RIGHT_STEM) 19 | 20 | #define MAX_GLYPHS 4096 21 | 22 | float n_basis = 24; 23 | 24 | 25 | enum { 26 | S_E_CHAR = 0, 27 | S_IN_CHAR, 28 | S_IN_COMMENT, 29 | S_E_LEFT_MODE, 30 | S_IN_LEFT_MODE, 31 | S_E_LEFT_VAL, 32 | S_IN_LEFT_VAL, 33 | S_E_RIGHT_MODE, 34 | S_IN_RIGHT_MODE, 35 | S_E_RIGHT_VAL, 36 | S_IN_RIGHT_VAL, 37 | S_IN_BASIS, 38 | S_DONE 39 | }; 40 | 41 | extern float scale_factor; 42 | 43 | float left_most_center (Glyph *g) 44 | { 45 | /* we only look at a single scan-line in the middle of the x-height */ 46 | return g->leftmost[(int)(kernagic_x_height() * 1.5 * scale_factor)] / scale_factor; 47 | } 48 | 49 | float right_most_center (Glyph *g) 50 | { 51 | /* we only look at a single scan-line in the middle of the x-height */ 52 | return g->rightmost[(int)(kernagic_x_height() * 1.5 * scale_factor)] / scale_factor; 53 | } 54 | 55 | #define LEFT_EXTREME (1<<0) 56 | #define RIGHT_EXTREME (1<<1) 57 | #define RIGHT_STEM (1<<2) 58 | #define LEFT_STEM (1<<3) 59 | #define BOTH_EXTREME (LEFT_EXTREME | RIGHT_EXTREME) 60 | #define BOTH_STEM (LEFT_STEM | RIGHT_STEM) 61 | 62 | -------------------------------------------------------------------------------- /gap.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include 20 | #include "kernagic.h" 21 | 22 | extern float scale_factor; 23 | 24 | float left_most_center (Glyph *g); 25 | float right_most_center (Glyph *g); 26 | static float n_width = 0; 27 | 28 | extern int kernagic_n_overrides; 29 | extern int kernagic_override_unicode[256]; 30 | extern float kernagic_override_left[256]; 31 | extern float kernagic_override_right[256]; 32 | 33 | static void kernagic_gap_init (void) 34 | { 35 | Glyph *g = kernagic_find_glyph_unicode ('n'); 36 | if (!g) 37 | return; 38 | n_width = (right_most_center (g) - left_most_center(g)) * scale_factor; 39 | 40 | if (kernagic_n_overrides > 0) 41 | { 42 | for (int i = 0; i < kernagic_n_overrides; i++) 43 | { 44 | Glyph *g = kernagic_find_glyph_unicode (kernagic_override_unicode[i]); 45 | if (g) 46 | { 47 | g->lstem = g->ink_width * kernagic_override_left[i]; 48 | g->rstem = g->ink_width * kernagic_override_right[i]; 49 | } 50 | } 51 | } 52 | 53 | } 54 | 55 | float n_distance (void) 56 | { 57 | float lstem; 58 | float rstem; 59 | Glyph *g = kernagic_find_glyph_unicode ('n'); 60 | if (!g) 61 | return 100.0; 62 | 63 | lstem = g->stems[0]; 64 | rstem = g->stems[g->stem_count-1]; 65 | 66 | /* if manual overrides are set, use them */ 67 | if (g->lstem > 0.0) 68 | lstem = g->lstem; 69 | if (g->rstem > 0.0) 70 | rstem = g->rstem; 71 | 72 | return rstem - lstem; 73 | } 74 | 75 | static void kernagic_gap_each (Glyph *g, GtkProgressBar *progress) 76 | { 77 | float period = kerner_settings.snap; 78 | float gap = kerner_settings.gap * kernagic_x_height (); 79 | float left, right; 80 | float lstem; 81 | float rstem; 82 | 83 | lstem = g->stems[0]; 84 | rstem = g->stems[g->stem_count-1]; 85 | 86 | if (!g_unichar_islower (g->unicode) ) 87 | { 88 | if (g->ink_height >= kernagic_x_height () * 1.1) 89 | gap *= kerner_settings.big_glyph_scaling; 90 | } 91 | 92 | /* if manual overrides are set, use them */ 93 | if (g->lstem > 0.0) 94 | lstem = g->lstem; 95 | if (g->rstem > 0.0) 96 | rstem = g->rstem; 97 | 98 | left = gap + period * (0.5) - lstem; 99 | 100 | /* can we come up with something better than ink_width here?.. */ 101 | 102 | right = left + rstem + (gap) + period; 103 | right = ((int)(right/period))*period; 104 | right = right - (left + g->ink_width); 105 | 106 | left = left * kerner_settings.tracking / 100.0; 107 | right = right * kerner_settings.tracking / 100.0; 108 | 109 | kernagic_set_left_bearing (g, left); 110 | kernagic_set_right_bearing (g, right); 111 | } 112 | 113 | static KernagicMethod method = {"gap", kernagic_gap_init, kernagic_gap_each, NULL}; 114 | KernagicMethod *kernagic_gap = &method; 115 | 116 | -------------------------------------------------------------------------------- /gimpspinscale.c: -------------------------------------------------------------------------------- 1 | /* GIMP - The GNU Image Manipulation Program 2 | * Copyright (C) 1995 Spencer Kimball and Peter Mattis 3 | * 4 | * gimpspinscale.c 5 | * Copyright (C) 2010 Michael Natterer 6 | * 2012 Øyvind Kolås 7 | * 8 | * This program is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation; either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program. If not, see . 20 | */ 21 | 22 | #if 0 23 | #include "config.h" 24 | #endif 25 | #include 26 | 27 | #include 28 | 29 | #if 0 30 | #include "libgimpwidgets/gimpwidgets.h" 31 | #include "libgimpmath/gimpmath.h" 32 | 33 | #include "widgets-types.h" 34 | #endif 35 | 36 | #include "gimpspinscale.h" 37 | 38 | #if 0 39 | #include "gimp-intl.h" 40 | #endif 41 | 42 | 43 | enum 44 | { 45 | PROP_0, 46 | PROP_LABEL 47 | }; 48 | 49 | typedef enum 50 | { 51 | TARGET_NUMBER, 52 | TARGET_UPPER, 53 | TARGET_LOWER 54 | } SpinScaleTarget; 55 | 56 | 57 | typedef struct _GimpSpinScalePrivate GimpSpinScalePrivate; 58 | 59 | struct _GimpSpinScalePrivate 60 | { 61 | gchar *label; 62 | 63 | gboolean scale_limits_set; 64 | gdouble scale_lower; 65 | gdouble scale_upper; 66 | gdouble gamma; 67 | 68 | PangoLayout *layout; 69 | gboolean changing_value; 70 | gboolean relative_change; 71 | gdouble start_x; 72 | gdouble start_value; 73 | }; 74 | 75 | #define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \ 76 | GIMP_TYPE_SPIN_SCALE, \ 77 | GimpSpinScalePrivate)) 78 | 79 | 80 | static void gimp_spin_scale_dispose (GObject *object); 81 | static void gimp_spin_scale_finalize (GObject *object); 82 | static void gimp_spin_scale_set_property (GObject *object, 83 | guint property_id, 84 | const GValue *value, 85 | GParamSpec *pspec); 86 | static void gimp_spin_scale_get_property (GObject *object, 87 | guint property_id, 88 | GValue *value, 89 | GParamSpec *pspec); 90 | 91 | static void gimp_spin_scale_size_request (GtkWidget *widget, 92 | GtkRequisition *requisition); 93 | static void gimp_spin_scale_style_set (GtkWidget *widget, 94 | GtkStyle *prev_style); 95 | static gboolean gimp_spin_scale_expose (GtkWidget *widget, 96 | GdkEventExpose *event); 97 | static gboolean gimp_spin_scale_button_press (GtkWidget *widget, 98 | GdkEventButton *event); 99 | static gboolean gimp_spin_scale_button_release (GtkWidget *widget, 100 | GdkEventButton *event); 101 | static gboolean gimp_spin_scale_motion_notify (GtkWidget *widget, 102 | GdkEventMotion *event); 103 | static gboolean gimp_spin_scale_leave_notify (GtkWidget *widget, 104 | GdkEventCrossing *event); 105 | 106 | static void gimp_spin_scale_value_changed (GtkSpinButton *spin_button); 107 | 108 | 109 | G_DEFINE_TYPE (GimpSpinScale, gimp_spin_scale, GTK_TYPE_SPIN_BUTTON); 110 | 111 | #define parent_class gimp_spin_scale_parent_class 112 | 113 | 114 | static void 115 | gimp_spin_scale_class_init (GimpSpinScaleClass *klass) 116 | { 117 | GObjectClass *object_class = G_OBJECT_CLASS (klass); 118 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); 119 | GtkSpinButtonClass *spin_button_class = GTK_SPIN_BUTTON_CLASS (klass); 120 | 121 | object_class->dispose = gimp_spin_scale_dispose; 122 | object_class->finalize = gimp_spin_scale_finalize; 123 | object_class->set_property = gimp_spin_scale_set_property; 124 | object_class->get_property = gimp_spin_scale_get_property; 125 | 126 | widget_class->size_request = gimp_spin_scale_size_request; 127 | widget_class->style_set = gimp_spin_scale_style_set; 128 | widget_class->expose_event = gimp_spin_scale_expose; 129 | widget_class->button_press_event = gimp_spin_scale_button_press; 130 | widget_class->button_release_event = gimp_spin_scale_button_release; 131 | widget_class->motion_notify_event = gimp_spin_scale_motion_notify; 132 | widget_class->leave_notify_event = gimp_spin_scale_leave_notify; 133 | 134 | spin_button_class->value_changed = gimp_spin_scale_value_changed; 135 | 136 | g_object_class_install_property (object_class, PROP_LABEL, 137 | g_param_spec_string ("label", NULL, NULL, 138 | NULL, 139 | G_PARAM_READWRITE)); 140 | 141 | g_type_class_add_private (klass, sizeof (GimpSpinScalePrivate)); 142 | } 143 | 144 | static void 145 | gimp_spin_scale_init (GimpSpinScale *scale) 146 | { 147 | GimpSpinScalePrivate *private = GET_PRIVATE (scale); 148 | 149 | gtk_widget_add_events (GTK_WIDGET (scale), 150 | GDK_BUTTON_PRESS_MASK | 151 | GDK_BUTTON_RELEASE_MASK | 152 | GDK_POINTER_MOTION_MASK | 153 | GDK_BUTTON1_MOTION_MASK | 154 | GDK_LEAVE_NOTIFY_MASK); 155 | 156 | gtk_entry_set_alignment (GTK_ENTRY (scale), 1.0); 157 | gtk_entry_set_has_frame (GTK_ENTRY (scale), FALSE); 158 | gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (scale), TRUE); 159 | 160 | private->gamma = 1.0; 161 | } 162 | 163 | static void 164 | gimp_spin_scale_dispose (GObject *object) 165 | { 166 | GimpSpinScalePrivate *private = GET_PRIVATE (object); 167 | 168 | if (private->layout) 169 | { 170 | g_object_unref (private->layout); 171 | private->layout = NULL; 172 | } 173 | 174 | G_OBJECT_CLASS (parent_class)->dispose (object); 175 | } 176 | 177 | static void 178 | gimp_spin_scale_finalize (GObject *object) 179 | { 180 | GimpSpinScalePrivate *private = GET_PRIVATE (object); 181 | 182 | if (private->label) 183 | { 184 | g_free (private->label); 185 | private->label = NULL; 186 | } 187 | 188 | G_OBJECT_CLASS (parent_class)->finalize (object); 189 | } 190 | 191 | static void 192 | gimp_spin_scale_set_property (GObject *object, 193 | guint property_id, 194 | const GValue *value, 195 | GParamSpec *pspec) 196 | { 197 | GimpSpinScalePrivate *private = GET_PRIVATE (object); 198 | 199 | switch (property_id) 200 | { 201 | case PROP_LABEL: 202 | g_free (private->label); 203 | private->label = g_value_dup_string (value); 204 | if (private->layout) 205 | { 206 | g_object_unref (private->layout); 207 | private->layout = NULL; 208 | } 209 | gtk_widget_queue_resize (GTK_WIDGET (object)); 210 | break; 211 | 212 | default: 213 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 214 | break; 215 | } 216 | } 217 | 218 | static void 219 | gimp_spin_scale_get_property (GObject *object, 220 | guint property_id, 221 | GValue *value, 222 | GParamSpec *pspec) 223 | { 224 | GimpSpinScalePrivate *private = GET_PRIVATE (object); 225 | 226 | switch (property_id) 227 | { 228 | case PROP_LABEL: 229 | g_value_set_string (value, private->label); 230 | break; 231 | 232 | default: 233 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 234 | break; 235 | } 236 | } 237 | 238 | static void 239 | gimp_spin_scale_size_request (GtkWidget *widget, 240 | GtkRequisition *requisition) 241 | { 242 | GimpSpinScalePrivate *private = GET_PRIVATE (widget); 243 | GtkStyle *style = gtk_widget_get_style (widget); 244 | PangoContext *context = gtk_widget_get_pango_context (widget); 245 | PangoFontMetrics *metrics; 246 | gint height; 247 | 248 | GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition); 249 | 250 | metrics = pango_context_get_metrics (context, style->font_desc, 251 | pango_context_get_language (context)); 252 | 253 | height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + 254 | pango_font_metrics_get_descent (metrics)); 255 | 256 | requisition->height += height; 257 | 258 | if (private->label) 259 | { 260 | gint char_width; 261 | gint digit_width; 262 | gint char_pixels; 263 | 264 | char_width = pango_font_metrics_get_approximate_char_width (metrics); 265 | digit_width = pango_font_metrics_get_approximate_digit_width (metrics); 266 | char_pixels = PANGO_PIXELS (MAX (char_width, digit_width)); 267 | 268 | /* ~3 chars for the ellipses */ 269 | requisition->width += char_pixels * 3; 270 | } 271 | 272 | pango_font_metrics_unref (metrics); 273 | } 274 | 275 | static void 276 | gimp_spin_scale_style_set (GtkWidget *widget, 277 | GtkStyle *prev_style) 278 | { 279 | GimpSpinScalePrivate *private = GET_PRIVATE (widget); 280 | 281 | GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style); 282 | 283 | if (private->layout) 284 | { 285 | g_object_unref (private->layout); 286 | private->layout = NULL; 287 | } 288 | } 289 | 290 | static gboolean 291 | gimp_spin_scale_expose (GtkWidget *widget, 292 | GdkEventExpose *event) 293 | { 294 | GimpSpinScalePrivate *private = GET_PRIVATE (widget); 295 | GtkStyle *style = gtk_widget_get_style (widget); 296 | cairo_t *cr; 297 | gboolean rtl; 298 | gint w, h; 299 | 300 | GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event); 301 | 302 | cr = gdk_cairo_create (event->window); 303 | gdk_cairo_region (cr, event->region); 304 | cairo_clip (cr); 305 | 306 | rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL); 307 | 308 | w = gdk_window_get_width (event->window); 309 | h = gdk_window_get_height (event->window); 310 | 311 | cairo_set_line_width (cr, 1.0); 312 | 313 | if (event->window == gtk_entry_get_text_window (GTK_ENTRY (widget))) 314 | { 315 | /* let spinbutton-side line of rectangle disappear */ 316 | if (rtl) 317 | cairo_rectangle (cr, -0.5, 0.5, w, h - 1.0); 318 | else 319 | cairo_rectangle (cr, 0.5, 0.5, w, h - 1.0); 320 | 321 | gdk_cairo_set_source_color (cr, 322 | &style->text[gtk_widget_get_state (widget)]); 323 | cairo_stroke (cr); 324 | } 325 | else 326 | { 327 | /* let text-box-side line of rectangle disappear */ 328 | if (rtl) 329 | cairo_rectangle (cr, 0.5, 0.5, w, h - 1.0); 330 | else 331 | cairo_rectangle (cr, -0.5, 0.5, w, h - 1.0); 332 | 333 | gdk_cairo_set_source_color (cr, 334 | &style->text[gtk_widget_get_state (widget)]); 335 | cairo_stroke (cr); 336 | 337 | if (rtl) 338 | cairo_rectangle (cr, 1.5, 1.5, w - 2.0, h - 3.0); 339 | else 340 | cairo_rectangle (cr, 0.5, 1.5, w - 2.0, h - 3.0); 341 | 342 | gdk_cairo_set_source_color (cr, 343 | &style->base[gtk_widget_get_state (widget)]); 344 | cairo_stroke (cr); 345 | } 346 | 347 | if (private->label && 348 | gtk_widget_is_drawable (widget) && 349 | event->window == gtk_entry_get_text_window (GTK_ENTRY (widget))) 350 | { 351 | GtkRequisition requisition; 352 | GtkAllocation allocation; 353 | PangoRectangle logical; 354 | gint layout_offset_x; 355 | gint layout_offset_y; 356 | GtkStateType state; 357 | GdkColor text_color; 358 | GdkColor bar_text_color; 359 | gint window_width; 360 | gint window_height; 361 | gdouble progress_fraction; 362 | gint progress_x; 363 | gint progress_y; 364 | gint progress_width; 365 | gint progress_height; 366 | 367 | GTK_WIDGET_CLASS (parent_class)->size_request (widget, &requisition); 368 | gtk_widget_get_allocation (widget, &allocation); 369 | 370 | if (! private->layout) 371 | { 372 | private->layout = gtk_widget_create_pango_layout (widget, 373 | private->label); 374 | pango_layout_set_ellipsize (private->layout, PANGO_ELLIPSIZE_END); 375 | } 376 | 377 | pango_layout_set_width (private->layout, 378 | PANGO_SCALE * 379 | (allocation.width - requisition.width)); 380 | pango_layout_get_pixel_extents (private->layout, NULL, &logical); 381 | 382 | gtk_entry_get_layout_offsets (GTK_ENTRY (widget), NULL, &layout_offset_y); 383 | 384 | if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 385 | layout_offset_x = w - logical.width - 2; 386 | else 387 | layout_offset_x = 2; 388 | 389 | layout_offset_x -= logical.x; 390 | 391 | state = GTK_STATE_SELECTED; 392 | if (! gtk_widget_get_sensitive (widget)) 393 | state = GTK_STATE_INSENSITIVE; 394 | text_color = style->text[gtk_widget_get_state (widget)]; 395 | bar_text_color = style->fg[state]; 396 | 397 | window_width = gdk_window_get_width (event->window); 398 | window_height = gdk_window_get_height (event->window); 399 | 400 | progress_fraction = gtk_entry_get_progress_fraction (GTK_ENTRY (widget)); 401 | 402 | if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 403 | { 404 | progress_fraction = 1.0 - progress_fraction; 405 | 406 | progress_x = window_width * progress_fraction; 407 | progress_y = 0; 408 | progress_width = window_width - progress_x; 409 | progress_height = window_height; 410 | } 411 | else 412 | { 413 | progress_x = 0; 414 | progress_y = 0; 415 | progress_width = window_width * progress_fraction; 416 | progress_height = window_height; 417 | } 418 | 419 | cairo_save (cr); 420 | 421 | cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD); 422 | cairo_rectangle (cr, 0, 0, window_width, window_height); 423 | cairo_rectangle (cr, progress_x, progress_y, 424 | progress_width, progress_height); 425 | cairo_clip (cr); 426 | cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING); 427 | 428 | cairo_move_to (cr, layout_offset_x, layout_offset_y); 429 | gdk_cairo_set_source_color (cr, &text_color); 430 | pango_cairo_show_layout (cr, private->layout); 431 | 432 | cairo_restore (cr); 433 | 434 | cairo_rectangle (cr, progress_x, progress_y, 435 | progress_width, progress_height); 436 | cairo_clip (cr); 437 | 438 | cairo_move_to (cr, layout_offset_x, layout_offset_y); 439 | gdk_cairo_set_source_color (cr, &bar_text_color); 440 | pango_cairo_show_layout (cr, private->layout); 441 | } 442 | 443 | cairo_destroy (cr); 444 | 445 | return FALSE; 446 | } 447 | 448 | static SpinScaleTarget 449 | gimp_spin_scale_get_target (GtkWidget *widget, 450 | gdouble x, 451 | gdouble y) 452 | { 453 | GtkAllocation allocation; 454 | PangoRectangle logical; 455 | gint layout_x; 456 | gint layout_y; 457 | 458 | gtk_widget_get_allocation (widget, &allocation); 459 | gtk_entry_get_layout_offsets (GTK_ENTRY (widget), &layout_x, &layout_y); 460 | pango_layout_get_pixel_extents (gtk_entry_get_layout (GTK_ENTRY (widget)), 461 | NULL, &logical); 462 | 463 | if (x > layout_x && x < layout_x + logical.width && 464 | y > layout_y && y < layout_y + logical.height) 465 | { 466 | return TARGET_NUMBER; 467 | } 468 | else if (y > allocation.height / 2) 469 | { 470 | return TARGET_LOWER; 471 | } 472 | 473 | return TARGET_UPPER; 474 | } 475 | 476 | static void 477 | gimp_spin_scale_get_limits (GimpSpinScale *scale, 478 | gdouble *lower, 479 | gdouble *upper) 480 | { 481 | GimpSpinScalePrivate *private = GET_PRIVATE (scale); 482 | 483 | if (private->scale_limits_set) 484 | { 485 | *lower = private->scale_lower; 486 | *upper = private->scale_upper; 487 | } 488 | else 489 | { 490 | GtkSpinButton *spin_button = GTK_SPIN_BUTTON (scale); 491 | GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); 492 | 493 | *lower = gtk_adjustment_get_lower (adjustment); 494 | *upper = gtk_adjustment_get_upper (adjustment); 495 | } 496 | } 497 | 498 | static void 499 | gimp_spin_scale_change_value (GtkWidget *widget, 500 | gdouble x) 501 | { 502 | GimpSpinScalePrivate *private = GET_PRIVATE (widget); 503 | GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget); 504 | GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); 505 | GdkWindow *text_window = gtk_entry_get_text_window (GTK_ENTRY (widget)); 506 | gdouble lower; 507 | gdouble upper; 508 | gint width; 509 | gdouble value; 510 | 511 | gimp_spin_scale_get_limits (GIMP_SPIN_SCALE (widget), &lower, &upper); 512 | 513 | width = gdk_window_get_width (text_window); 514 | 515 | if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 516 | x = width - x; 517 | 518 | if (private->relative_change) 519 | { 520 | gdouble diff; 521 | gdouble step; 522 | 523 | step = (upper - lower) / width / 10.0; 524 | 525 | if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 526 | diff = x - (width - private->start_x); 527 | else 528 | diff = x - private->start_x; 529 | 530 | value = (private->start_value + diff * step); 531 | } 532 | else 533 | { 534 | gdouble fraction; 535 | 536 | fraction = x / (gdouble) width; 537 | if (fraction > 0.0) 538 | fraction = pow (fraction, private->gamma); 539 | 540 | value = fraction * (upper - lower) + lower; 541 | } 542 | 543 | gtk_adjustment_set_value (adjustment, value); 544 | } 545 | 546 | static gboolean 547 | gimp_spin_scale_button_press (GtkWidget *widget, 548 | GdkEventButton *event) 549 | { 550 | GimpSpinScalePrivate *private = GET_PRIVATE (widget); 551 | 552 | private->changing_value = FALSE; 553 | private->relative_change = FALSE; 554 | 555 | if (event->window == gtk_entry_get_text_window (GTK_ENTRY (widget))) 556 | { 557 | switch (gimp_spin_scale_get_target (widget, event->x, event->y)) 558 | { 559 | case TARGET_UPPER: 560 | private->changing_value = TRUE; 561 | 562 | gtk_widget_grab_focus (widget); 563 | 564 | gimp_spin_scale_change_value (widget, event->x); 565 | 566 | return TRUE; 567 | 568 | case TARGET_LOWER: 569 | private->changing_value = TRUE; 570 | 571 | gtk_widget_grab_focus (widget); 572 | 573 | private->relative_change = TRUE; 574 | private->start_x = event->x; 575 | private->start_value = gtk_adjustment_get_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget))); 576 | 577 | return TRUE; 578 | 579 | default: 580 | break; 581 | } 582 | } 583 | 584 | return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event); 585 | } 586 | 587 | static gboolean 588 | gimp_spin_scale_button_release (GtkWidget *widget, 589 | GdkEventButton *event) 590 | { 591 | GimpSpinScalePrivate *private = GET_PRIVATE (widget); 592 | 593 | if (private->changing_value) 594 | { 595 | private->changing_value = FALSE; 596 | 597 | gimp_spin_scale_change_value (widget, event->x); 598 | 599 | return TRUE; 600 | } 601 | 602 | return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event); 603 | } 604 | 605 | static gboolean 606 | gimp_spin_scale_motion_notify (GtkWidget *widget, 607 | GdkEventMotion *event) 608 | { 609 | GimpSpinScalePrivate *private = GET_PRIVATE (widget); 610 | 611 | gdk_event_request_motions (event); 612 | 613 | if (private->changing_value) 614 | { 615 | gimp_spin_scale_change_value (widget, event->x); 616 | 617 | return TRUE; 618 | } 619 | 620 | GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event); 621 | 622 | if (! (event->state & 623 | (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) && 624 | event->window == gtk_entry_get_text_window (GTK_ENTRY (widget))) 625 | { 626 | GdkDisplay *display = gtk_widget_get_display (widget); 627 | GdkCursor *cursor = NULL; 628 | 629 | switch (gimp_spin_scale_get_target (widget, event->x, event->y)) 630 | { 631 | case TARGET_NUMBER: 632 | cursor = gdk_cursor_new_for_display (display, GDK_XTERM); 633 | break; 634 | 635 | case TARGET_UPPER: 636 | cursor = gdk_cursor_new_for_display (display, GDK_SB_UP_ARROW); 637 | break; 638 | 639 | case TARGET_LOWER: 640 | cursor = gdk_cursor_new_for_display (display, GDK_SB_H_DOUBLE_ARROW); 641 | break; 642 | } 643 | 644 | gdk_window_set_cursor (event->window, cursor); 645 | gdk_cursor_unref (cursor); 646 | } 647 | 648 | return FALSE; 649 | } 650 | 651 | static gboolean 652 | gimp_spin_scale_leave_notify (GtkWidget *widget, 653 | GdkEventCrossing *event) 654 | { 655 | gdk_window_set_cursor (event->window, NULL); 656 | 657 | return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); 658 | } 659 | 660 | static void 661 | gimp_spin_scale_value_changed (GtkSpinButton *spin_button) 662 | { 663 | GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (spin_button); 664 | GimpSpinScalePrivate *private = GET_PRIVATE (spin_button); 665 | gdouble lower; 666 | gdouble upper; 667 | gdouble value; 668 | 669 | gimp_spin_scale_get_limits (GIMP_SPIN_SCALE (spin_button), &lower, &upper); 670 | 671 | value = CLAMP (gtk_adjustment_get_value (adjustment), lower, upper); 672 | 673 | gtk_entry_set_progress_fraction (GTK_ENTRY (spin_button), 674 | pow ((value - lower) / (upper - lower), 675 | 1.0 / private->gamma)); 676 | } 677 | 678 | 679 | /* public functions */ 680 | 681 | GtkWidget * 682 | gimp_spin_scale_new (GtkAdjustment *adjustment, 683 | const gchar *label, 684 | gint digits) 685 | { 686 | g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL); 687 | 688 | return g_object_new (GIMP_TYPE_SPIN_SCALE, 689 | "adjustment", adjustment, 690 | "label", label, 691 | "digits", digits, 692 | NULL); 693 | } 694 | 695 | void 696 | gimp_spin_scale_set_scale_limits (GimpSpinScale *scale, 697 | gdouble lower, 698 | gdouble upper) 699 | { 700 | GimpSpinScalePrivate *private; 701 | GtkSpinButton *spin_button; 702 | GtkAdjustment *adjustment; 703 | 704 | g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); 705 | 706 | private = GET_PRIVATE (scale); 707 | spin_button = GTK_SPIN_BUTTON (scale); 708 | adjustment = gtk_spin_button_get_adjustment (spin_button); 709 | 710 | g_return_if_fail (lower >= gtk_adjustment_get_lower (adjustment)); 711 | g_return_if_fail (upper <= gtk_adjustment_get_upper (adjustment)); 712 | 713 | private->scale_limits_set = TRUE; 714 | private->scale_lower = lower; 715 | private->scale_upper = upper; 716 | private->gamma = 1.0; 717 | 718 | gimp_spin_scale_value_changed (spin_button); 719 | } 720 | 721 | void 722 | gimp_spin_scale_set_gamma (GimpSpinScale *scale, 723 | gdouble gamma) 724 | { 725 | GimpSpinScalePrivate *private; 726 | 727 | g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); 728 | 729 | private = GET_PRIVATE (scale); 730 | 731 | private->gamma = gamma; 732 | 733 | gimp_spin_scale_value_changed (GTK_SPIN_BUTTON (scale)); 734 | } 735 | 736 | gdouble 737 | gimp_spin_scale_get_gamma (GimpSpinScale *scale) 738 | { 739 | GimpSpinScalePrivate *private; 740 | 741 | g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), 1.0); 742 | 743 | private = GET_PRIVATE (scale); 744 | 745 | return private->gamma; 746 | } 747 | 748 | void 749 | gimp_spin_scale_unset_scale_limits (GimpSpinScale *scale) 750 | { 751 | GimpSpinScalePrivate *private; 752 | 753 | g_return_if_fail (GIMP_IS_SPIN_SCALE (scale)); 754 | 755 | private = GET_PRIVATE (scale); 756 | 757 | private->scale_limits_set = FALSE; 758 | private->scale_lower = 0.0; 759 | private->scale_upper = 0.0; 760 | 761 | gimp_spin_scale_value_changed (GTK_SPIN_BUTTON (scale)); 762 | } 763 | 764 | gboolean 765 | gimp_spin_scale_get_scale_limits (GimpSpinScale *scale, 766 | gdouble *lower, 767 | gdouble *upper) 768 | { 769 | GimpSpinScalePrivate *private; 770 | 771 | g_return_val_if_fail (GIMP_IS_SPIN_SCALE (scale), FALSE); 772 | 773 | private = GET_PRIVATE (scale); 774 | 775 | if (lower) 776 | *lower = private->scale_lower; 777 | 778 | if (upper) 779 | *upper = private->scale_upper; 780 | 781 | return private->scale_limits_set; 782 | } 783 | -------------------------------------------------------------------------------- /gimpspinscale.h: -------------------------------------------------------------------------------- 1 | /* GIMP - The GNU Image Manipulation Program 2 | * Copyright (C) 1995 Spencer Kimball and Peter Mattis 3 | * 4 | * gimpspinscale.h 5 | * Copyright (C) 2010 Michael Natterer 6 | * 7 | * This program is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __GIMP_SPIN_SCALE_H__ 22 | #define __GIMP_SPIN_SCALE_H__ 23 | 24 | 25 | #define GIMP_TYPE_SPIN_SCALE (gimp_spin_scale_get_type ()) 26 | #define GIMP_SPIN_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_SPIN_SCALE, GimpSpinScale)) 27 | #define GIMP_SPIN_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_SPIN_SCALE, GimpSpinScaleClass)) 28 | #define GIMP_IS_SPIN_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_SPIN_SCALE)) 29 | #define GIMP_IS_SPIN_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_SPIN_SCALE)) 30 | #define GIMP_SPIN_SCALE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_SPIN_SCALE, GimpSpinScaleClass)) 31 | 32 | 33 | typedef struct _GimpSpinScale GimpSpinScale; 34 | typedef struct _GimpSpinScaleClass GimpSpinScaleClass; 35 | 36 | struct _GimpSpinScale 37 | { 38 | GtkSpinButton parent_instance; 39 | }; 40 | 41 | struct _GimpSpinScaleClass 42 | { 43 | GtkSpinButtonClass parent_class; 44 | }; 45 | 46 | 47 | GType gimp_spin_scale_get_type (void) G_GNUC_CONST; 48 | 49 | GtkWidget * gimp_spin_scale_new (GtkAdjustment *adjustment, 50 | const gchar *label, 51 | gint digits); 52 | 53 | void gimp_spin_scale_set_scale_limits (GimpSpinScale *scale, 54 | gdouble lower, 55 | gdouble upper); 56 | void gimp_spin_scale_unset_scale_limits (GimpSpinScale *scale); 57 | gboolean gimp_spin_scale_get_scale_limits (GimpSpinScale *scale, 58 | gdouble *lower, 59 | gdouble *upper); 60 | 61 | void gimp_spin_scale_set_gamma (GimpSpinScale *scale, 62 | gdouble gamma); 63 | gdouble gimp_spin_scale_get_gamma (GimpSpinScale *scale); 64 | 65 | 66 | #endif /* __GIMP_SPIN_SCALE_H__ */ 67 | -------------------------------------------------------------------------------- /glyph.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include "glyph.h" 26 | 27 | void kernagic_set_advance (Glyph *a, float advance) 28 | { 29 | //a->advance = advance; 30 | fprintf (stderr, "ooops\n"); 31 | } 32 | 33 | float kernagic_get_advance (Glyph *a) 34 | { 35 | return a->ink_width + a->left_bearing + a->right_bearing; 36 | } 37 | 38 | void kernagic_set_kerning (Glyph *a, Glyph *b, float kerning) 39 | { 40 | int intkern = kerning * 10; 41 | if (!a || !b) 42 | return; 43 | 44 | g_hash_table_insert (a->kerning, b, GINT_TO_POINTER (intkern)); 45 | } 46 | 47 | float kernagic_kern_get (Glyph *a, Glyph *b) 48 | { 49 | int intkern; 50 | float kern; 51 | if (!a || !b) 52 | return 0; 53 | 54 | gpointer pvalue = g_hash_table_lookup (a->kerning, b); 55 | 56 | intkern = GPOINTER_TO_INT (pvalue); 57 | kern = intkern / 10.0; 58 | return kern; 59 | } 60 | 61 | 62 | 63 | void glyph_free (Glyph *glyph) 64 | { 65 | if (glyph->path) 66 | g_free (glyph->path); 67 | if (glyph->name) 68 | g_free (glyph->name); 69 | if (glyph->xml) 70 | g_free (glyph->xml); 71 | if (glyph->raster) 72 | g_free (glyph->raster); 73 | if (glyph->kerning) 74 | g_hash_table_destroy (glyph->kerning); 75 | g_free (glyph); 76 | } 77 | 78 | 79 | Glyph *kernagic_glyph_new (const char *path) 80 | { 81 | Glyph *glyph = g_malloc0 (sizeof (Glyph)); 82 | g_file_get_contents (path, &glyph->xml, NULL, NULL); 83 | 84 | if (!glyph->xml) 85 | { 86 | g_free (glyph); 87 | glyph = NULL; 88 | } 89 | 90 | if (glyph) 91 | { 92 | glyph->kerning = g_hash_table_new (g_direct_hash, g_direct_equal); 93 | glyph->path = g_strdup (path); 94 | glyph->lstem = 0.0; 95 | glyph->rstem = 0.0; 96 | } 97 | return glyph; 98 | } 99 | 100 | void kernagic_glyph_reset (Glyph *glyph) 101 | { 102 | if (glyph->kerning) 103 | g_hash_table_destroy (glyph->kerning); 104 | glyph->kerning = g_hash_table_new (g_direct_hash, g_direct_equal); 105 | glyph->left_bearing = 0; 106 | glyph->right_bearing = 0; 107 | } 108 | 109 | void kernagic_glyph_free (Glyph *glyph) 110 | { 111 | if (glyph->xml) 112 | g_free (glyph->xml); 113 | if (glyph->raster) 114 | free (glyph->raster); 115 | if (glyph->name) 116 | g_free (glyph->name); 117 | g_free (glyph); 118 | } 119 | 120 | void 121 | kernagic_set_left_bearing (Glyph *g, float left_bearing) 122 | { 123 | g->left_bearing = left_bearing; 124 | } 125 | 126 | void 127 | kernagic_set_right_bearing (Glyph *g, float right_bearing) 128 | { 129 | g->right_bearing = right_bearing; 130 | } 131 | -------------------------------------------------------------------------------- /glyph.h: -------------------------------------------------------------------------------- 1 | #ifndef KERNAGIC_GLYPH_H 2 | #define KERNAGIC_GLYPH_H 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef struct _Glyph Glyph; 11 | 12 | struct _Glyph { 13 | char *path; 14 | char *name; 15 | char *xml; 16 | uint32_t unicode; 17 | 18 | /* each glyph loaded into kernagic can have an associated raster image 19 | */ 20 | uint8_t *raster; 21 | int r_width; /* raster width */ 22 | int r_height; 23 | 24 | int scan_width[1024]; /* 1024 is a really arbitrary number.. */ 25 | 26 | 27 | int offset_x; /* how many units have been subtracted out of glyphs outline 28 | * coordinates by the bearing stripping */ 29 | 30 | float ink_min_x; /* always 0 due to stripped bearings */ 31 | float ink_max_x; /* the ink extents is bounding box of the glyph */ 32 | float ink_min_y; /* outline. */ 33 | float ink_max_y; 34 | 35 | float ink_width; /* computed from ink_max_x - ink_min_x */ 36 | float ink_height; /* computed from ink_max_y - ink_min_y */ 37 | 38 | float left_original; 39 | float right_original; 40 | 41 | float left_bearing; 42 | float right_bearing; /* this should deprecate advance.. */ 43 | 44 | GHashTable *kerning; 45 | 46 | /* leftmost and rightmost pixel per row -1 for no pixel */ 47 | int leftmost[2048]; 48 | int rightmost[2048]; 49 | 50 | cairo_t *cr; /* used transiently during glyph rendering */ 51 | 52 | #define MAX_STEMS 32 53 | float stems[MAX_STEMS]; 54 | float stem_weight[MAX_STEMS]; 55 | int stem_count; 56 | 57 | float lstem; 58 | float rstem; 59 | 60 | int loaded; 61 | }; 62 | 63 | float kernagic_kern_get (Glyph *a, Glyph *b); 64 | void kernagic_set_kerning (Glyph *a, Glyph *b, float kerning); 65 | void kernagic_set_left_bearing (Glyph *g, float left_bearing); 66 | void kernagic_set_right_bearing (Glyph *g, float right_bearing); 67 | float kernagic_get_advance (Glyph *g); 68 | Glyph *kernagic_glyph_new (const char *path); 69 | void kernagic_glyph_free (Glyph *glyph); 70 | 71 | /* resets the bearing information of the glyph 72 | */ 73 | void kernagic_glyph_reset (Glyph *glyph); 74 | 75 | #endif 76 | 77 | -------------------------------------------------------------------------------- /gray.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include "kernagic.h" 22 | 23 | extern float scale_factor; 24 | 25 | static float alpha = 0.23; 26 | static float beta = 0.23; 27 | static gboolean visual_debug_enabled = FALSE; 28 | 29 | #define DIST_MAX 120 30 | 31 | static int s_width = 0; 32 | static int s_height = 0; 33 | static uint8_t *scratch = NULL; 34 | static uint8_t *scratch2 = NULL; 35 | static uint8_t *scratch3 = NULL; 36 | static long int scratch3_unicode = -1; 37 | 38 | static void place_a (Glyph *left, Glyph *right, float opacity); 39 | 40 | static void save_scratch (void) 41 | { 42 | memcpy (scratch2, scratch, s_width * s_height); 43 | } 44 | 45 | static int compute_dist (Glyph *left, Glyph *right, int space) 46 | { 47 | int x, y; 48 | int min_dist = 2000; 49 | 50 | if (scratch3_unicode == left->unicode) 51 | { 52 | memcpy (scratch2, scratch3, s_width * s_height); 53 | memcpy (scratch, scratch3, s_width * s_height); 54 | } 55 | else 56 | { 57 | memset (scratch, 0, s_height * s_width); 58 | place_a (left, right, 1.0); 59 | 60 | for (y = 1; y < s_height-1; y++) 61 | for (x = 1; x < s_width-1; x++) 62 | if (scratch [y * s_width + x] < 254) 63 | scratch [y * s_width + x] = 0; 64 | 65 | for (int j = 0; j < DIST_MAX; j++) 66 | { 67 | save_scratch (); 68 | for (y = 1; y < s_height-1; y++) 69 | for (x = 1; x < s_width-1; x++) 70 | { 71 | if (scratch2 [y * s_width + x] < 254) 72 | { 73 | if (scratch2 [y * s_width + x -1] || 74 | scratch2 [y * s_width + x +1] || 75 | scratch2 [y * s_width + x - s_width] || 76 | scratch2 [y * s_width + x + s_width]) 77 | scratch [y * s_width + x] ++; 78 | } 79 | } 80 | } 81 | save_scratch (); 82 | 83 | scratch3_unicode = left->unicode; 84 | memcpy (scratch3, scratch2, s_width * s_height); 85 | } 86 | 87 | for (y = 0; y < right->r_height; y++) 88 | for (x = 0; x < right->r_width; x++) 89 | if ( 90 | x + space < s_width && 91 | x + space > 0 && 92 | y < s_height && 93 | 94 | right->raster[y * right->r_width + x] > 170) 95 | { 96 | if (scratch2 [y * s_width + x + space] > 0 && 97 | DIST_MAX-scratch2 [y * s_width + x + space] < min_dist) 98 | min_dist = DIST_MAX-scratch [y * s_width + x + space]; 99 | scratch [y * s_width + x + space] = 200; 100 | } 101 | 102 | if (min_dist < 0) 103 | min_dist = -1; 104 | if (min_dist >= 1000) 105 | min_dist = -1; 106 | return min_dist; 107 | } 108 | 109 | 110 | static float compute_xheight_graylevel (Glyph *left, Glyph *right, int s) 111 | { 112 | int x0, y0, x1, y1; 113 | int x, y; 114 | int count = 0; 115 | 116 | float graylevel = 0; 117 | count = 0; 118 | y0 = kernagic_x_height () * 1.0 * scale_factor; 119 | y1 = kernagic_x_height () * 2.0 * scale_factor; 120 | x0 = left->ink_width * scale_factor * 0.4; 121 | x1 = s + right->ink_width * scale_factor * 0.6; 122 | for (y = y0; y < y1; y++) 123 | for (x = x0; x < x1; x++) 124 | { 125 | graylevel += scratch [y * s_width + x]; 126 | count ++; 127 | } 128 | graylevel = graylevel / count / 255.0; 129 | return graylevel; 130 | } 131 | 132 | /* build an array of valid starting points per scanline.. being the left most 133 | * non-filled pixels of the glyph.. 134 | * 135 | */ 136 | 137 | static void floodfill (int x, int y, int x0, int y0, int x1, int y1, int *count) 138 | { 139 | if (x < x0 || x > x1 || y < y0 || y > y1 || 140 | scratch [y * s_width + x] != 0) 141 | return; 142 | scratch [y * s_width + x] = 127; 143 | (*count) ++; 144 | floodfill (x + 1, y, x0, y0, x1, y1, count); 145 | floodfill (x - 1, y, x0, y0, x1, y1, count); 146 | floodfill (x, y + 1, x0, y0, x1, y1, count); 147 | floodfill (x, y - 1, x0, y0, x1, y1, count); 148 | } 149 | 150 | static float compute_negative_area_ratio (Glyph *left, Glyph *right, int s) 151 | { 152 | int x0, y0, x1, y1; 153 | int x = 0, y; 154 | int count = 0; 155 | 156 | count = 0; 157 | y0 = kernagic_x_height () * 1.04 * scale_factor; 158 | y1 = kernagic_x_height () * 1.96 * scale_factor; 159 | x0 = left->ink_width * scale_factor * 0.2; 160 | x1 = s + right->ink_width * scale_factor *0.8; 161 | 162 | for (y = y0 + 1; y < y1; y++) 163 | { 164 | x = left->scan_width[y]; 165 | if (scratch [y * s_width + x] == 0) 166 | break; 167 | } 168 | if (x < x0) 169 | x = x0; 170 | 171 | floodfill (x + 1, y, x0, y0, x1, y1, &count); 172 | return count / (kernagic_x_height () * scale_factor * kernagic_x_height() * scale_factor * 1.0); 173 | } 174 | 175 | static void place_a (Glyph *left, Glyph *right, float opacity) 176 | { 177 | int x, y; 178 | 179 | for (y = 0; y < left->r_height; y++) 180 | for (x = 0; x < left->r_width; x++) 181 | if (x < s_width && y < s_height) 182 | scratch [y * s_width + x] = left->raster[y * left->r_width + x] * opacity; 183 | } 184 | 185 | static void place_glyphs (Glyph *left, 186 | Glyph *right, 187 | float spacing) 188 | { 189 | int x, y; 190 | int space = spacing; 191 | assert (left); 192 | assert (right); 193 | 194 | memset (scratch, 0, s_height * s_width); 195 | place_a (left, right, 1.0); 196 | 197 | for (y = 0; y < right->r_height; y++) 198 | for (x = 0; x < right->r_width; x++) 199 | if (x + space < s_width && 200 | x + space >= 0 && 201 | y < s_height) 202 | scratch [y * s_width + x + space] += right->raster[y * right->r_width + x]; 203 | } 204 | 205 | static GtkWidget *drawing_area; 206 | 207 | 208 | 209 | static gboolean draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data) 210 | { 211 | cairo_set_source_rgb (cr, 1,1,1); 212 | cairo_paint (cr); 213 | 214 | cairo_surface_t *g_surface; 215 | 216 | g_surface = cairo_image_surface_create_for_data (scratch, CAIRO_FORMAT_A8, 217 | s_width, s_height, s_width); 218 | 219 | cairo_set_source_rgb (cr, 1,0,0); 220 | cairo_translate (cr, 0, 0); 221 | cairo_mask_surface (cr, g_surface, 0, 0); 222 | cairo_fill (cr); 223 | 224 | cairo_surface_destroy (g_surface); 225 | cairo_set_source_rgb (cr, 0,0,0); 226 | 227 | cairo_select_font_face(cr, "Sans", 228 | CAIRO_FONT_SLANT_NORMAL, 229 | CAIRO_FONT_WEIGHT_NORMAL); 230 | cairo_set_font_size(cr, 20); 231 | 232 | char buf[4096]; 233 | float y = 0.0; 234 | float x = 300; 235 | 236 | sprintf (buf, "alpha: %2.2f%%", 100 * alpha); 237 | cairo_move_to (cr, x, y+=30); 238 | cairo_show_text (cr, buf); 239 | 240 | 241 | sprintf (buf, "beta: %2.2f%%", 100 * beta); 242 | cairo_move_to (cr, x, y+=30); 243 | cairo_show_text (cr, buf); 244 | 245 | return FALSE; 246 | } 247 | 248 | void kerner_debug_ui (void) 249 | { 250 | GtkWidget *window; 251 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 252 | drawing_area = gtk_drawing_area_new (); 253 | gtk_widget_set_size_request (drawing_area, 512, 256); 254 | g_signal_connect (drawing_area, "draw", G_CALLBACK (draw_cb), NULL); 255 | gtk_container_add (GTK_CONTAINER (window), drawing_area); 256 | gtk_widget_show (drawing_area); 257 | gtk_widget_show (window); 258 | visual_debug_enabled = TRUE; 259 | } 260 | 261 | void init_kerner (void) 262 | { 263 | if (scratch != NULL) 264 | return; 265 | 266 | s_width = kernagic_x_height () * scale_factor * 4; 267 | s_height = kernagic_x_height () * scale_factor * 3; 268 | 269 | s_width /= 8; 270 | s_width *= 8; 271 | 272 | scratch = g_malloc0 (s_width * s_height); 273 | scratch2 = g_malloc0 (s_width * s_height); 274 | scratch3 = g_malloc0 (s_width * s_height); 275 | } 276 | 277 | float kerner_kern (KernerSettings *settings, 278 | Glyph *left, 279 | Glyph *right) 280 | { 281 | int s; 282 | int min_dist; 283 | 284 | gint best_advance = 0; 285 | gfloat best_diff = 10000.0; 286 | 287 | int maxs = left->ink_width * scale_factor * 1.5; 288 | 289 | if (maxs < kernagic_x_height () * scale_factor) 290 | maxs = kernagic_x_height () * scale_factor; 291 | 292 | /* XXX: it would be better to go left until going under min_dist, then from 293 | * guess mid_point go right until going over max dist,. this would cut out 294 | * even distance testing of the extremes that are not neccesary to test on. 295 | */ 296 | for (s = left->ink_width * scale_factor * 0.5; s < maxs; s++) 297 | { 298 | min_dist = compute_dist (left, right, s); 299 | 300 | if (min_dist < settings->maximum_distance * kernagic_x_height () * scale_factor / 100.0 && 301 | 302 | min_dist > settings->minimum_distance * kernagic_x_height () * scale_factor / 100.0) 303 | { 304 | place_glyphs (left, right, s); 305 | alpha = compute_xheight_graylevel (left, right, s); 306 | //place_glyphs (left, right, s); 307 | beta = compute_negative_area_ratio (left, right, s); 308 | 309 | float alphadiff = fabs (alpha - settings->snap / 100.0); 310 | //float betadiff = fabs (beta - settings->beta_target / 100.0); 311 | float sumdiff; 312 | 313 | //alphadiff *= alphadiff; 314 | //betadiff *= betadiff; 315 | //alphadiff *= settings->alpha_strength / 100.0; 316 | //betadiff *= settings->beta_strength / 100.0; 317 | 318 | sumdiff = alphadiff /* + betadiff */; 319 | 320 | if (sumdiff < best_diff) 321 | { 322 | best_diff = sumdiff; 323 | best_advance = s; 324 | } 325 | 326 | if (visual_debug_enabled) 327 | { 328 | gtk_widget_queue_draw (drawing_area); 329 | //for (int i = 0; i < 5500; i++) 330 | { 331 | gtk_main_iteration_do (FALSE); 332 | } 333 | } 334 | } 335 | } 336 | return best_advance / scale_factor; 337 | } 338 | 339 | void gray_each (Glyph *lg, GtkProgressBar *progress) 340 | { 341 | GList *right; 342 | for (right = kernagic_glyphs (); right; right = right->next) 343 | { 344 | Glyph *rg = right->data; 345 | if (progress || kernagic_deal_with_glyphs (lg->unicode, rg->unicode)) 346 | { 347 | /*XXX: kerner kern is wrong, this is method specific */ 348 | float kerned_advance = kerner_kern (&kerner_settings, lg, rg); 349 | 350 | lg->right_bearing = 0; 351 | lg->left_bearing = 0; 352 | kernagic_set_kerning (lg, rg, kerned_advance - kernagic_get_advance (lg)); 353 | } 354 | } 355 | } 356 | 357 | void recompute_right_bearings (void); 358 | 359 | static void done (void) 360 | { 361 | recompute_right_bearings (); 362 | } 363 | 364 | static KernagicMethod method = {"gray", 365 | NULL, gray_each, (void*)done}; 366 | 367 | KernagicMethod *kernagic_gray = &method; 368 | -------------------------------------------------------------------------------- /ipsumat.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #define MAX_WORDS 500 28 | 29 | int attempts = 500; 30 | 31 | static int print_score = 0; 32 | 33 | /** score_string: 34 | * @str input string 35 | * @desired_glyphs string containing highly desired glyphs 36 | * 37 | * Generates an ipsum with an attempt at maximizing the amount of differing 38 | * neighbour pairs. 39 | * 40 | * Returns the score for a string, a higher score is a better string. 41 | */ 42 | static int score_string (const char *str, 43 | const char *desired_glyphs) 44 | { 45 | /* we pick slightly larger than a power of two, to aovid aliasing of things 46 | * starting on multiples of 512 in the unicode set. 47 | */ 48 | #define ADJ_DIM 1023 49 | gunichar *ustr; 50 | char adjacency_matrix[ADJ_DIM*ADJ_DIM]={0,}; 51 | gunichar *p; 52 | 53 | if (!str || str[0] == 0) 54 | return 0; 55 | 56 | ustr = g_utf8_to_ucs4 (str, -1, NULL, NULL, NULL); 57 | 58 | if (!ustr) 59 | return 0; 60 | 61 | /* walk throguh the string ..*/ 62 | for (p = ustr; p[1]; p++) 63 | { 64 | gunichar x = p[0]; /* .. using the current .. */ 65 | gunichar y = p[1]; /* .. and the next characters unicode position ..*/ 66 | 67 | if (x==' ' || y == ' ') 68 | continue; /* (bailing if one of them is a space) */ 69 | 70 | x %= ADJ_DIM; /* with unicode positions wrapped down to our */ 71 | y %= ADJ_DIM; /* matrix dimensions */ 72 | 73 | /* mark cell in matrix as visited */ 74 | adjacency_matrix[y * ADJ_DIM + x] = 1; 75 | } 76 | 77 | /* count number of distinct pairs encountered (permitting some collisions, 78 | * in a bloom-filter like manner) */ 79 | { 80 | int i; 81 | int sum = 0; 82 | 83 | if (desired_glyphs) 84 | for (i = 0; ustr[i]; i++) 85 | { 86 | int j; 87 | for (j = 0; desired_glyphs[j]; j++) 88 | if (desired_glyphs[j] == ustr[i]) 89 | sum ++; 90 | } 91 | 92 | for (i = 0; i < ADJ_DIM * ADJ_DIM ; i ++) 93 | sum += adjacency_matrix[i] * 2; 94 | 95 | g_free (ustr); 96 | return sum; 97 | } 98 | } 99 | 100 | char *ipsumat_generate (const char *dict_path, 101 | const char *charset, 102 | const char *desired_glyphs, 103 | int max_wordlen, 104 | int max_words) 105 | { 106 | gunichar *p; 107 | gunichar *ucharset; 108 | gunichar *udesired_glyphs = NULL; 109 | 110 | int count = 0; 111 | int best_sentence[MAX_WORDS]={0,}; 112 | int sentence[MAX_WORDS]={0,}; 113 | char *words_str = NULL; 114 | gunichar *uwords_str = NULL; 115 | GList *words = NULL; 116 | GString *word = g_string_new (""); 117 | int best_score = 0; 118 | int i; 119 | if (!dict_path) 120 | dict_path = "/usr/share/dict/words"; 121 | 122 | g_file_get_contents (dict_path, &words_str, NULL, NULL); 123 | 124 | if (!words_str) 125 | return g_strdup ("problem opening dictionary"); 126 | 127 | uwords_str = g_utf8_to_ucs4 (words_str, -1, NULL, NULL, NULL); 128 | 129 | if (charset == NULL) 130 | charset = "abcdefghijklmnopqrstuvwxyz"; 131 | 132 | ucharset = g_utf8_to_ucs4 (charset, -1, NULL, NULL, NULL); 133 | if (desired_glyphs) 134 | udesired_glyphs = g_utf8_to_ucs4 (desired_glyphs, -1, NULL, NULL, NULL); 135 | 136 | 137 | if (max_words > MAX_WORDS) 138 | max_words = MAX_WORDS; 139 | 140 | for (p = uwords_str; *p; p++) 141 | { 142 | switch (*p) 143 | { 144 | case '\n': 145 | case '\r': 146 | case ' ': 147 | case '\t': 148 | if (word->len) 149 | { 150 | int skip = 0; 151 | int i; 152 | gunichar *uword = g_utf8_to_ucs4 (word->str, -1, NULL, NULL, NULL); 153 | for (i = 0; uword[i]; i++) 154 | { 155 | int k; 156 | skip++; 157 | for (k = 0; ucharset[k]; k++) 158 | if (ucharset[k]==uword[i]) 159 | { 160 | skip--;break; 161 | } 162 | } 163 | if (word->len > max_wordlen) 164 | skip++; 165 | 166 | if (!skip) 167 | { 168 | words = g_list_prepend (words, g_strdup (word->str)); 169 | count ++; 170 | } 171 | g_free (uword); 172 | } 173 | g_string_assign (word, ""); 174 | break; 175 | default: 176 | g_string_append_unichar (word, *p); 177 | break; 178 | } 179 | } 180 | g_free (ucharset); 181 | g_free (words_str); 182 | g_free (uwords_str); 183 | 184 | for (i = 0; i < attempts; i ++) 185 | { 186 | GString *example = g_string_new (""); 187 | int j; 188 | for (j = 0; j < max_words; j ++) 189 | { 190 | int n; 191 | const char *str; 192 | n = rand()%count; 193 | 194 | { 195 | int k; 196 | for (k = 0; k < j; k++) 197 | if (sentence[k]==n) 198 | { 199 | /* we try once more if it collides with already picked 200 | * random number,. - but this value will stick */ 201 | n = rand()%count; 202 | break; 203 | } 204 | } 205 | sentence[j] = n; 206 | 207 | str = g_list_nth_data (words, n); 208 | if (str) 209 | { 210 | if (j) 211 | g_string_append (example, " "); 212 | g_string_append (example, str); 213 | } 214 | } 215 | float score = score_string ((void*)example->str, desired_glyphs); 216 | if (score >= best_score) 217 | { 218 | for (j = 0; j < max_words; j ++) 219 | best_sentence[j] = sentence[j]; 220 | best_score = score; 221 | } 222 | g_string_free (example, TRUE); 223 | } 224 | 225 | if (print_score) 226 | printf ("Score: %i\n", best_score); 227 | 228 | { 229 | char *ret = NULL; 230 | int j; 231 | GString *s = g_string_new (""); 232 | 233 | if (desired_glyphs && desired_glyphs[0]) 234 | { 235 | g_string_append (s, desired_glyphs); 236 | g_string_append (s, " "); 237 | } 238 | 239 | for (j = 0; j < max_words; j ++) 240 | { 241 | const char *str; 242 | str = g_list_nth_data (words, best_sentence[j]); 243 | if (str) 244 | { 245 | if (j) 246 | g_string_append (s, " "); 247 | g_string_append (s, str); 248 | } 249 | } 250 | ret = strdup (s->str); 251 | g_string_free (s, TRUE); 252 | g_free (udesired_glyphs); 253 | return ret; 254 | } 255 | } 256 | 257 | /* the main used - when executable is named/symlinked ipsum */ 258 | int ipsumat (int argc, char **argv) 259 | { 260 | int argno; 261 | int iterations = 1; 262 | int max_wordlen = 7; 263 | int wordcount = 23; 264 | const char *glyphs = "abcdefghijklmnopqrstuvwxyz"; 265 | const char *desired_glyphs = ""; 266 | const char *dict_path = NULL; 267 | 268 | struct timeval time; 269 | gettimeofday (&time, NULL); 270 | srand (time.tv_sec); 271 | 272 | for (argno = 1; argno < argc; argno++) 273 | { 274 | if (!strcmp (argv[argno], "--help") || 275 | !strcmp (argv[argno], "-h")) 276 | { 277 | fprintf (stdout, "\n" 278 | "ipsumat [options]\n" 279 | " -m \n" 280 | " -w \n" 281 | " -i \n" 282 | " -s \n" 283 | " -g \n" 284 | " -d \n" 285 | " -D \n" 286 | " -p print score\n" 287 | "\n"); 288 | return 0; 289 | } 290 | else if (!strcmp (argv[argno], "-w")) 291 | { 292 | #define EXPECT_ARG if (!argv[argno+1]) {fprintf (stderr, "expected argument after %s\n", argv[argno]);exit(-1);} 293 | EXPECT_ARG; 294 | wordcount = atoi (argv[++argno]); 295 | } 296 | else if (!strcmp (argv[argno], "-m")) 297 | { 298 | EXPECT_ARG; 299 | max_wordlen = atoi (argv[++argno]); 300 | } 301 | else if (!strcmp (argv[argno], "-g")) 302 | { 303 | EXPECT_ARG; 304 | glyphs = argv[++argno]; 305 | } 306 | else if (!strcmp (argv[argno], "-s")) 307 | { 308 | EXPECT_ARG; 309 | srand (atoi(argv[++argno])); 310 | } 311 | else if (!strcmp (argv[argno], "-D")) 312 | { 313 | EXPECT_ARG; 314 | dict_path = argv[++argno]; 315 | } 316 | else if (!strcmp (argv[argno], "-d")) 317 | { 318 | EXPECT_ARG; 319 | desired_glyphs = argv[++argno]; 320 | } 321 | else if (!strcmp (argv[argno], "-p")) 322 | { 323 | print_score = 1; 324 | } 325 | else if (!strcmp (argv[argno], "-i")) 326 | { 327 | EXPECT_ARG; 328 | iterations = atoi (argv[++argno]); 329 | } 330 | else if (!strcmp (argv[argno], "-a")) 331 | { 332 | EXPECT_ARG; 333 | attempts = atoi (argv[++argno]); 334 | } 335 | } 336 | 337 | int i; 338 | for (i = 0; i < iterations; i ++) 339 | printf ("%s\n", 340 | ipsumat_generate (dict_path, 341 | glyphs, 342 | desired_glyphs, 343 | max_wordlen, 344 | wordcount)); 345 | return 0; 346 | } 347 | -------------------------------------------------------------------------------- /kernagic-debug-osx.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | script $HOME/Kernagic-Debug-Output.txt lldb --debug --source kernagic-debug.txt -- 3 | -------------------------------------------------------------------------------- /kernagic-debug-osx.txt: -------------------------------------------------------------------------------- 1 | version 2 | settings set frame-format "frame #${frame.index}: ${frame.pc}{ ${module.file.basename}`${function.name-with-args}{${function.pc-offset}}}{ at ${line.file.basename}:${line.number}}\n" 3 | target create /usr/local/bin/kernagic 4 | target select 0 5 | run 6 | -------------------------------------------------------------------------------- /kernagic.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "kernagic.h" 30 | 31 | #define SPECIMEN_SIZE 170 32 | 33 | #define PREVIEW_WIDTH 900 34 | #define PREVIEW_HEIGHT 670 35 | 36 | int canvas_width () 37 | { 38 | return PREVIEW_WIDTH; 39 | } 40 | 41 | int canvas_height () 42 | { 43 | return PREVIEW_HEIGHT; 44 | } 45 | 46 | int kernagic_n_overrides = 0; 47 | int kernagic_override_unicode[256]; 48 | float kernagic_override_left[256]; 49 | float kernagic_override_right[256]; 50 | 51 | void add_override (int unicode, float left, float right) 52 | { 53 | kernagic_override_unicode[kernagic_n_overrides] = unicode; 54 | kernagic_override_left[kernagic_n_overrides] = left; 55 | kernagic_override_right[kernagic_n_overrides++] = right; 56 | } 57 | 58 | KernerSettings kerner_settings = { 59 | 0, 60 | KERNER_DEFAULT_MIN, 61 | KERNER_DEFAULT_MAX, 62 | KERNER_DEFAULT_DIVISOR, 63 | KERNER_DEFAULT_SNAP, 64 | KERNER_DEFAULT_GAP, 65 | KERNER_DEFAULT_BIG_GLYPH_SCALING, 66 | KERNER_DEFAULT_TRACKING 67 | }; 68 | 69 | 70 | char *kernagic_sample_text = NULL; 71 | 72 | char *loaded_ufo_path = NULL; 73 | static GList *glyphs = NULL; 74 | float scale_factor = 0.18; 75 | static gunichar *glyph_string = NULL; 76 | 77 | extern KernagicMethod *kernagic_cadence, 78 | *kernagic_gap, 79 | *kernagic_gray, 80 | *kernagic_original, 81 | *kernagic_bounds; 82 | 83 | KernagicMethod *methods[32] = {NULL}; 84 | 85 | static void init_methods (void) 86 | { 87 | int i = 0; 88 | methods[i++] = kernagic_original; 89 | methods[i++] = kernagic_gap; 90 | methods[i++] = kernagic_bounds; 91 | methods[i] = NULL; 92 | }; 93 | 94 | 95 | gboolean kernagic_strip_bearing = FALSE; 96 | 97 | GList *kernagic_glyphs (void) 98 | { 99 | return glyphs; 100 | } 101 | 102 | void init_kernagic (void) 103 | { 104 | init_kerner (); 105 | } 106 | 107 | static gint unicode_sort (gconstpointer a, gconstpointer b) 108 | { 109 | const Glyph *ga = a; 110 | const Glyph *gb = b; 111 | 112 | return ga->unicode - gb->unicode; 113 | } 114 | 115 | static int add_glyph(const char *fpath) 116 | { 117 | if (strstr (fpath, "contents.plist")) 118 | return 0; 119 | Glyph *glyph = kernagic_glyph_new (fpath); 120 | 121 | if (glyph) 122 | glyphs = g_list_insert_sorted (glyphs, glyph, unicode_sort); 123 | return 0; 124 | } 125 | 126 | /* simplistic recomputation of right bearings; it uses the average advance 127 | * taking all kerning pairs into account, and modifies all the involved 128 | * kerning pairs accordingly. 129 | */ 130 | void 131 | recompute_right_bearings () 132 | { 133 | GList *l; 134 | for (l = glyphs; l; l= l->next) 135 | { 136 | Glyph *lg = l->data; 137 | GList *r; 138 | float advance_sum = 0; 139 | long glyph_count = 0; 140 | int new_advance; 141 | int advance_diff; 142 | for (r = glyphs; r; r= r->next) 143 | { 144 | Glyph *rg = r->data; 145 | advance_sum += kernagic_kern_get (lg, rg) + kernagic_get_advance (lg); 146 | glyph_count ++; 147 | } 148 | new_advance = advance_sum / glyph_count; 149 | advance_diff = new_advance - kernagic_get_advance (lg); 150 | 151 | lg->left_bearing = 0; 152 | lg->right_bearing = new_advance; 153 | 154 | for (r = glyphs; r; r= r->next) 155 | { 156 | Glyph *rg = r->data; 157 | float oldkern = kernagic_kern_get (lg, rg); 158 | kernagic_set_kerning (lg, rg, oldkern - advance_diff); 159 | } 160 | } 161 | } 162 | 163 | void remove_monitors (void); 164 | void add_monitors (const char *path); 165 | 166 | static FILE *kernagic_spacing_file = NULL; 167 | 168 | void kernagic_save_kerning_info (void) 169 | { 170 | GString *str = g_string_new ( 171 | "\n" 172 | "\n" 174 | "\n" 175 | " \n"); 176 | 177 | remove_monitors (); 178 | 179 | GList *left, *right; 180 | 181 | for (left = glyphs; left; left = left->next) 182 | { 183 | Glyph *lg = left->data; 184 | int found = 0; 185 | 186 | for (right = glyphs; right; right = right->next) 187 | { 188 | Glyph *rg = right->data; 189 | float kerning; 190 | 191 | if ((kerning=kernagic_kern_get (lg, rg)) != 0.0) 192 | { 193 | if (!found) 194 | { 195 | g_string_append_printf (str, 196 | " %s\n \n", lg->name); 197 | found = 1; 198 | } 199 | 200 | g_string_append_printf (str, 201 | " %s%d\n", 202 | rg->name, (int)kerning); 203 | 204 | } 205 | } 206 | if (found) 207 | g_string_append (str, " \n"); 208 | } 209 | 210 | g_string_append (str, " \n"); 211 | 212 | char path[4095]; 213 | GError *error = NULL; 214 | 215 | sprintf (path, "%s/kerning.plist", loaded_ufo_path); 216 | 217 | g_file_set_contents (path, str->str, -1, &error); 218 | if (error) 219 | fprintf (stderr, "EEeek %s\n", error->message); 220 | g_string_free (str, TRUE); 221 | 222 | GList *l; 223 | for (l = glyphs; l; l = l->next) 224 | { 225 | Glyph *glyph = l->data; 226 | rewrite_ufo_glyph (glyph); 227 | 228 | if (kernagic_spacing_file) 229 | if (glyph->unicode) 230 | fprintf (kernagic_spacing_file, "%i %f %d\n", 231 | glyph->unicode, 232 | glyph->offset_x + glyph->left_bearing + kernagic_x_shift, 233 | (int)(kernagic_get_advance (glyph))); 234 | } 235 | if (kernagic_spacing_file) 236 | fclose (kernagic_spacing_file); 237 | kernagic_spacing_file = NULL; 238 | 239 | sprintf (path, "%s/lib.plist", loaded_ufo_path); 240 | kernagic_libplist_rewrite (path); 241 | 242 | remove_monitors (); 243 | add_monitors (loaded_ufo_path); 244 | } 245 | 246 | void render_ufo_glyph (Glyph *glyph); 247 | 248 | GList *monitors = NULL; 249 | 250 | void remove_monitors (void) 251 | { 252 | for (GList *l = monitors; l ;l = monitors) 253 | { 254 | g_object_unref (G_OBJECT (l->data)); 255 | monitors = g_list_remove (monitors, l->data); 256 | } 257 | } 258 | 259 | void trigger_reload (void); 260 | 261 | void add_monitors (const char *path) 262 | { 263 | { 264 | GFileMonitor *monitor; 265 | monitor = g_file_monitor ( 266 | g_file_new_for_commandline_arg (path), 267 | G_FILE_MONITOR_NONE, 268 | NULL, NULL); 269 | if (monitor) 270 | { 271 | g_signal_connect (monitor, "changed", 272 | G_CALLBACK (trigger_reload), NULL); 273 | monitors = g_list_append (monitors, monitor); 274 | } 275 | 276 | /* we try to make monitors for the glyphs dir as well */ 277 | { 278 | GString *str = g_string_new (""); 279 | g_string_append_printf (str, "%s/glyphs", path); 280 | monitor = g_file_monitor ( 281 | g_file_new_for_commandline_arg (str->str), 282 | G_FILE_MONITOR_NONE, 283 | NULL, NULL); 284 | g_string_free (str, TRUE); 285 | } 286 | if (monitor) 287 | { 288 | g_signal_connect (monitor, "changed", 289 | G_CALLBACK (trigger_reload), NULL); 290 | monitors = g_list_append (monitors, monitor); 291 | } 292 | } 293 | } 294 | 295 | 296 | void kernagic_load_ufo (const char *font_path, gboolean strip_left_bearing) 297 | { 298 | char path[4095]; 299 | 300 | 301 | kernagic_strip_bearing = strip_left_bearing; 302 | 303 | if (loaded_ufo_path) 304 | g_free (loaded_ufo_path); 305 | loaded_ufo_path = g_strdup (font_path); 306 | if (loaded_ufo_path [strlen(loaded_ufo_path)] == '/') 307 | loaded_ufo_path [strlen(loaded_ufo_path)] = '\0'; 308 | 309 | remove_monitors (); 310 | add_monitors (loaded_ufo_path); 311 | 312 | GList *l; 313 | for (l = glyphs; l; l = l->next) 314 | { 315 | Glyph *glyph = l->data; 316 | kernagic_glyph_free (glyph); 317 | } 318 | g_list_free (glyphs); 319 | glyphs = NULL; 320 | 321 | sprintf (path, "%s/glyphs", loaded_ufo_path); 322 | 323 | { 324 | DIR *dp; 325 | struct dirent *dirp; 326 | dp = opendir(path); 327 | 328 | if (dp) 329 | { 330 | while ((dirp = readdir(dp)) != NULL) { 331 | if (dirp->d_name[0] != '.') 332 | { 333 | char buf[1024]; 334 | sprintf (buf, "%s/%s", path, dirp->d_name); 335 | add_glyph (buf); 336 | } 337 | } 338 | closedir(dp); 339 | } 340 | else 341 | { 342 | return ; 343 | } 344 | } 345 | 346 | { 347 | GList *l; 348 | /* first load all glyphs that are not using components.. */ 349 | for (l = glyphs; l; l=l->next) 350 | { 351 | Glyph *g = l->data; 352 | if (!strstr (g->xml, "next) 357 | { 358 | Glyph *g = l->data; 359 | if (strstr (g->xml, "next) 367 | { 368 | Glyph *glyph = l->data; 369 | int y; 370 | render_ufo_glyph (glyph); 371 | 372 | for (y = 0; y < glyph->r_height; y ++) 373 | { 374 | int x; 375 | int min = 8192; 376 | int max = -1; 377 | for (x = 0; x < glyph->r_width; x ++) 378 | { 379 | if (glyph->raster[y * glyph->r_width + x] > 0) 380 | { 381 | if (x < min) 382 | min = x; 383 | if (x > max) 384 | max = x; 385 | } 386 | } 387 | glyph->leftmost[y] = min; 388 | glyph->rightmost[y] = max; 389 | } 390 | } 391 | init_kernagic (); 392 | sprintf (path, "%s/lib.plist", loaded_ufo_path); 393 | kernagic_libplist_read (path); 394 | } 395 | 396 | void kernagic_kern_clear_all (void) 397 | { 398 | GList *l; 399 | for (l = glyphs; l; l = l->next) 400 | { 401 | Glyph *glyph = l->data; 402 | g_hash_table_remove_all (glyph->kerning); 403 | } 404 | } 405 | 406 | 407 | Glyph *kernagic_find_glyph (const char *name) 408 | { 409 | GList *l; 410 | for (l = glyphs; l; l = l->next) 411 | { 412 | Glyph *glyph = l->data; 413 | if (glyph->name && !strcmp (glyph->name, name)) 414 | return glyph; 415 | } 416 | return NULL; 417 | } 418 | 419 | Glyph *kernagic_find_glyph_unicode (unsigned int unicode) 420 | { 421 | GList *l; 422 | for (l = glyphs; l; l = l->next) 423 | { 424 | Glyph *glyph = l->data; 425 | if (glyph->unicode == unicode) 426 | return glyph; 427 | } 428 | return NULL; 429 | } 430 | 431 | /* programmatic way of finding x-height, is guaranteed to work better than 432 | * font metadata... 433 | */ 434 | float kernagic_x_height (void) 435 | { 436 | Glyph *g = kernagic_find_glyph_unicode ('x'); 437 | if (!g) 438 | return 1.0; /* avoiding 0, for divisions by it.. */ 439 | return (g->ink_max_y - g->ink_min_y); 440 | } 441 | 442 | gboolean kernagic_deal_with_glyph (gunichar unicode) 443 | { 444 | int i; 445 | if (!glyph_string) 446 | return TRUE; 447 | for (i = 0; glyph_string[i]; i++) 448 | if (glyph_string[i] == unicode) 449 | return TRUE; 450 | return FALSE; 451 | } 452 | 453 | gboolean kernagic_deal_with_glyphs (gunichar unicode, gunichar unicode2) 454 | { 455 | int i; 456 | if (!glyph_string) 457 | return TRUE; 458 | if (!unicode || !unicode2) 459 | return FALSE; 460 | for (i = 0; glyph_string[i]; i++) 461 | if (glyph_string[i] == unicode && 462 | glyph_string[i+1] == unicode2) 463 | return TRUE; 464 | return FALSE; 465 | } 466 | 467 | void kernagic_set_glyph_string (const char *utf8) 468 | { 469 | if (glyph_string) 470 | g_free (glyph_string); 471 | glyph_string = NULL; 472 | if (utf8) 473 | glyph_string = g_utf8_to_ucs4 (utf8, -1, NULL, NULL, NULL); 474 | } 475 | 476 | static int interactive = 1; 477 | 478 | gboolean kernagic_strip_left_bearing = KERNAGIC_DEFAULT_STRIP_LEFT_BEARING; 479 | char *kernagic_output = NULL; 480 | char *kernagic_output_png = NULL; 481 | 482 | void help (void) 483 | { 484 | printf ( 485 | "kernagic [options] \n" 486 | "\n" 487 | "Options:\n" 488 | " -m specify method, specify an invalid one for list of valid ones.\n" 489 | " -g gap\n" 490 | " -s snap\n" 491 | " -bs big scale\n" 492 | "\n" 493 | " --override glyph left right overrides left and right stems,\n" 494 | " the stems are set with horizontal ink bounds\n" 495 | " as the unit, e.g. 0.5 is the middle of inkbounds\n" 496 | " --center-glyphs utf8stringofglyphs overrides stems with single ink centered stem for specified glyphs.\n" 497 | " --x_shift fontdim_val\n" 498 | "\n" 499 | " -S sample string for PNG and UI\n" 500 | " -o instead of running UI create a copy of the input font, this make kernagic run non-interactive with the given parameters.\n" 501 | " -p write the test string to a png, using the given parameters.\n" 502 | "\n"); 503 | exit (0); 504 | } 505 | 506 | const char *ufo_path = NULL; 507 | int ipsumat (int argc, char **argv); 508 | 509 | float kernagic_x_shift = 0.0; 510 | 511 | void parse_args (int argc, char **argv) 512 | { 513 | int no; 514 | kerner_settings.method = methods[1]; 515 | 516 | for (no = 1; no < argc; no++) 517 | { 518 | if (!strcmp (argv[no], "--help") || 519 | !strcmp (argv[no], "-h")) 520 | help (); 521 | else if (!strcmp (argv[no], "--ipsumat")) 522 | { 523 | exit (ipsumat (argc, argv)); 524 | } 525 | else if (!strcmp (argv[no], "-d")) 526 | { 527 | #define EXPECT_ARG if (!argv[no+1]) {fprintf (stderr, "expected argument after %s\n", argv[no]);exit(-1);} 528 | EXPECT_ARG; 529 | kerner_settings.minimum_distance = atof (argv[++no]); 530 | } 531 | else if (!strcmp (argv[no], "-D")) 532 | { 533 | EXPECT_ARG; 534 | kerner_settings.maximum_distance = atof (argv[++no]); 535 | } 536 | else if (!strcmp (argv[no], "-g")) 537 | { 538 | EXPECT_ARG; 539 | kerner_settings.gap = atof (argv[++no]); 540 | } 541 | else if (!strcmp (argv[no], "-bs")) 542 | { 543 | EXPECT_ARG; 544 | kerner_settings.big_glyph_scaling = atof (argv[++no]); 545 | } 546 | else if (!strcmp (argv[no], "-t")) 547 | { 548 | EXPECT_ARG; 549 | kerner_settings.tracking = atof (argv[++no]); 550 | } 551 | else if (!strcmp (argv[no], "-s")) 552 | { 553 | EXPECT_ARG; 554 | kerner_settings.snap = atof (argv[++no]); 555 | if (kerner_settings.snap < 0.5) 556 | kerner_settings.snap = 1; 557 | } 558 | else if (!strcmp (argv[no], "--x_shift")) 559 | { 560 | EXPECT_ARG; 561 | kernagic_x_shift = atof (argv[++no]); 562 | } 563 | else if (!strcmp (argv[no], "--override")) 564 | { 565 | EXPECT_ARG; /* XXX: x3 */ 566 | { 567 | gunichar *ucs = g_utf8_to_ucs4_fast (argv[no+1], -1, NULL); 568 | if (ucs) 569 | { 570 | add_override (ucs[0], atof(argv[no+2]), atof(argv[no+3])); 571 | g_free (ucs); 572 | } 573 | } 574 | no+=3; 575 | } 576 | else if (!strcmp (argv[no], "-m")) 577 | { 578 | int i; 579 | char *method; 580 | int found = 0; 581 | EXPECT_ARG; 582 | method = argv[++no]; 583 | for (i = 0; methods[i]; i++) 584 | { 585 | if (!strcmp (method, methods[i]->name)) 586 | { 587 | kerner_settings.method = methods[i]; 588 | found = 1; 589 | break; 590 | } 591 | } 592 | if (!found) 593 | { 594 | fprintf (stderr, "unknown method %s\n", method); 595 | fprintf (stderr, "Available methods:"); 596 | for (i = 0; methods[i]; i++) 597 | { 598 | fprintf (stderr, " %s", methods[i]->name); 599 | } 600 | fprintf (stderr, "\n"); 601 | exit (-1); 602 | } 603 | } 604 | else if (!strcmp (argv[no], "-S")) 605 | { 606 | EXPECT_ARG; 607 | kernagic_sample_text = argv[++no]; 608 | } 609 | else if (!strcmp (argv[no], "--center-glyphs")) 610 | { 611 | EXPECT_ARG; 612 | { 613 | gunichar *ucs = g_utf8_to_ucs4_fast (argv[no+1], -1, NULL); 614 | int i; 615 | if (ucs) 616 | { 617 | for (i = 0; ucs[i]; i++) 618 | { 619 | add_override (ucs[i], 0.5, 0.5); 620 | } 621 | g_free (ucs); 622 | } 623 | } 624 | ++no; 625 | } 626 | else if (!strcmp (argv[no], "-x")) 627 | { 628 | EXPECT_ARG; 629 | kernagic_spacing_file = fopen (argv[++no], "w"); 630 | } 631 | else if (!strcmp (argv[no], "-o")) 632 | { 633 | EXPECT_ARG; 634 | kernagic_output = argv[++no]; 635 | interactive = 0; 636 | 637 | if (!ufo_path) 638 | { 639 | fprintf (stderr, "must specify input font before -o\n"); 640 | exit (-1); 641 | } 642 | 643 | char cmd[512]; 644 | /* XXX: does not work on windows */ 645 | sprintf (cmd, "rm -rf %s;cp -Rva %s %s", kernagic_output, ufo_path, kernagic_output); 646 | fprintf (stderr, "%s\n", cmd); 647 | 648 | system (cmd); 649 | ufo_path = kernagic_output; 650 | } 651 | else if (!strcmp (argv[no], "-p")) 652 | { 653 | EXPECT_ARG; 654 | kernagic_output_png = argv[++no]; 655 | interactive = 0; 656 | } 657 | else if (argv[no][0] == '-') 658 | { 659 | fprintf (stderr, "unknown argument %s\n", argv[no]); 660 | exit (-1); 661 | } 662 | else 663 | { 664 | ufo_path = argv[no]; 665 | } 666 | } 667 | } 668 | 669 | int ui_gtk (int argc, char **argv); 670 | KernagicMethod *kernagic_method_no (int no) 671 | { 672 | if (no < 0) no = 0; 673 | return methods[no]; 674 | } 675 | 676 | int kernagic_find_method_no (KernagicMethod *method) 677 | { 678 | int i; 679 | for (i = 0; methods[i]; i++) 680 | if (methods[i] == method) 681 | return i; 682 | return 0; 683 | } 684 | 685 | int kernagic_active_method_no (void) 686 | { 687 | return kernagic_find_method_no (kerner_settings.method); 688 | } 689 | 690 | extern uint8_t *kernagic_preview; 691 | 692 | int kernagic_libplist (int argc, char **argv); 693 | 694 | int main (int argc, char **argv) 695 | { 696 | if (!strcmp (basename(argv[0]), "ipsumat")) 697 | return ipsumat (argc, argv); 698 | 699 | if (!kernagic_preview) 700 | kernagic_preview = g_malloc0 (PREVIEW_WIDTH * PREVIEW_HEIGHT); 701 | 702 | init_methods (); 703 | parse_args (argc, argv); 704 | 705 | if (interactive) 706 | return ui_gtk (argc, argv); 707 | remove_monitors (); 708 | 709 | if (!ufo_path) 710 | { 711 | fprintf (stderr, "no font file to work on specified\n"); 712 | exit (-1); 713 | } 714 | 715 | kernagic_load_ufo (ufo_path, kernagic_strip_left_bearing); 716 | 717 | kernagic_set_glyph_string (NULL); 718 | kernagic_compute (NULL); 719 | 720 | if (kernagic_output_png) 721 | { 722 | int i; 723 | int len = canvas_width () * canvas_height (); 724 | cairo_surface_t *surface = 725 | cairo_image_surface_create_for_data (kernagic_preview, 726 | CAIRO_FORMAT_A8, canvas_width (), canvas_height (), canvas_width()); 727 | waterfall_offset = 20000; 728 | redraw_test_text (kernagic_sample_text, 0); 729 | for (i = 0; i < len; i++) 730 | kernagic_preview[i] = 255 - kernagic_preview[i]; 731 | cairo_surface_write_to_png (surface, kernagic_output_png); 732 | cairo_surface_destroy (surface); 733 | return 0; 734 | } 735 | kernagic_save_kerning_info (); 736 | return 0; 737 | } 738 | 739 | void kernagic_compute (GtkProgressBar *progress) 740 | { 741 | GList *glyphs = kernagic_glyphs (); 742 | long int total = g_list_length (glyphs); 743 | long int count = 0; 744 | GList *left; 745 | 746 | if (kerner_settings.method->init) 747 | kerner_settings.method->init (); 748 | 749 | for (left = glyphs; left; left = left->next) 750 | { 751 | Glyph *lg = left->data; 752 | if (progress) 753 | { 754 | float fraction = count / (float)total; 755 | gtk_progress_bar_set_fraction (progress, fraction); 756 | } 757 | 758 | kernagic_glyph_reset (lg); 759 | 760 | if (progress || kernagic_deal_with_glyph (lg->unicode)) 761 | { 762 | if (kerner_settings.method->each) 763 | kerner_settings.method->each (lg, progress); 764 | } 765 | count ++; 766 | } 767 | 768 | if (kerner_settings.method->done) 769 | kerner_settings.method->done (); 770 | 771 | /* space space to be width of i, if both glyphs exist */ 772 | { 773 | Glyph *space = kernagic_find_glyph_unicode (' '); 774 | Glyph *nbspace = kernagic_find_glyph_unicode (0x00A0); 775 | Glyph *i = kernagic_find_glyph_unicode ('i'); 776 | 777 | /* XXX: if i is not found.. just do 0.23% of x height? */ 778 | if (i && space) 779 | { 780 | float width = i->left_bearing + i->ink_width + i->right_bearing; 781 | 782 | space->right_bearing = 0; 783 | space->left_bearing = width; 784 | space->ink_width = 0; 785 | 786 | if (nbspace) 787 | { 788 | nbspace->right_bearing = 0; 789 | nbspace->left_bearing = width; 790 | nbspace->ink_width = 0; 791 | } 792 | } 793 | } 794 | } 795 | -------------------------------------------------------------------------------- /kernagic.h: -------------------------------------------------------------------------------- 1 | #ifndef KERNAGIC_H 2 | #define KERNAGIC_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "glyph.h" 10 | 11 | void kernagic_load_ufo (const char *ufo_path, gboolean strip_left_bearing); 12 | void load_ufo_glyph (Glyph *glyph); 13 | float kernagic_x_height (void); 14 | 15 | Glyph *kernagic_find_glyph_unicode (unsigned int unicode); 16 | Glyph *kernagic_find_glyph (const char *name); 17 | 18 | void kernagic_kern_clear_all (void); 19 | 20 | void kernagic_save_kerning_info (void); 21 | 22 | void kernagic_compute (GtkProgressBar *progress); 23 | void kernagic_compute_bearings (void); 24 | 25 | /* setting a glyph string makes kernagic only kern the involved pairs - 26 | * permitting */ 27 | void kernagic_set_glyph_string (const char *utf8); 28 | 29 | void rewrite_ufo_glyph (Glyph *glyph); 30 | 31 | #define KERNAGIC_DEFAULT_STRIP_LEFT_BEARING 1 32 | extern gboolean kernagic_strip_left_bearing; 33 | 34 | gboolean kernagic_deal_with_glyphs (gunichar left, gunichar right); 35 | gboolean kernagic_deal_with_glyph (gunichar unicode); 36 | 37 | /* ipsum generator is a single c function, relies on state from srandom () */ 38 | char *ipsumat_generate (const char *dict_path, 39 | const char *charset, 40 | const char *desired_glyphs, 41 | int max_wordlen, 42 | int max_words); 43 | 44 | GList *kernagic_glyphs (void); 45 | 46 | typedef struct _KernagicMethod KernagicMethod; 47 | KernagicMethod *kernagic_method_no (int no); 48 | int kernagic_active_method_no (void); 49 | 50 | struct _KernagicMethod { 51 | char *name; 52 | void (*init) (void); 53 | void (*each) (Glyph *glyph, GtkProgressBar *progress); 54 | void (*done) (void); 55 | }; 56 | 57 | typedef struct _Word Word; 58 | struct _Word 59 | { 60 | gchar *utf8; 61 | int len; 62 | int x; 63 | int y; 64 | int width; 65 | int height; 66 | }; 67 | const char *detect_word (int x, int y); 68 | float n_distance (void); 69 | 70 | extern int debug_start_y; 71 | 72 | 73 | typedef struct _KernerSettings KernerSettings; 74 | 75 | #define KERNER_DEFAULT_MODE 3 76 | #define KERNER_DEFAULT_MIN 15 77 | #define KERNER_DEFAULT_MAX 50 78 | #define KERNER_DEFAULT_DIVISOR 24 79 | #define KERNER_DEFAULT_SNAP 0 80 | #define KERNER_DEFAULT_GAP 0.3 81 | #define KERNER_DEFAULT_BIG_GLYPH_SCALING 1 82 | #define KERNER_DEFAULT_TRACKING 100 83 | 84 | struct _KernerSettings 85 | { 86 | KernagicMethod *method; 87 | float minimum_distance; 88 | float maximum_distance; 89 | float divisor; 90 | float snap; 91 | float gap; 92 | float big_glyph_scaling; 93 | float tracking; 94 | }; 95 | extern KernerSettings kerner_settings; 96 | 97 | float kerner_kern (KernerSettings *settings, Glyph *left, Glyph *right); 98 | void init_kerner (void); 99 | void kerner_debug_ui (void); 100 | 101 | void kernagic_set_cadence (const char *cadence_path); 102 | 103 | void redraw_test_text (const char *intext, int debuglevel); 104 | int canvas_width (); 105 | int canvas_height (); 106 | extern float debug_scale; 107 | extern float waterfall_offset; 108 | extern char *loaded_ufo_path; 109 | 110 | void kernagic_libplist_rewrite (const char *path); 111 | int kernagic_libplist_read (const char *path); 112 | extern float kernagic_x_shift; 113 | 114 | #define WATERFALL_START 0.02 115 | #define WATERFALL_SCALING 1.33 116 | #define WATERFALL_SPACING 0.7 117 | #define WATERFALL_LEVELS 10 118 | #define PREVIEW_PADDING 5 119 | 120 | #define IPSUM0 "ooonnnono frtjckxiv Page Down - " 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /kernagic.nsis: -------------------------------------------------------------------------------- 1 | !define APPNAME "kernagic" 2 | !define COMPANYNAME "hodefoting" 3 | !define DESCRIPTION "Semi automatic font spacing tool" 4 | # These three must be integers 5 | !define VERSIONMAJOR 0 6 | !define VERSIONMINOR 1 7 | !define VERSIONBUILD 1 8 | # These will be displayed by the "Click here for support information" link in "Add/Remove Programs" 9 | # It is possible to use "mailto:" links in here to open the email client 10 | !define HELPURL "http://..." # "Support Information" link 11 | !define UPDATEURL "http://..." # "Product Updates" link 12 | !define ABOUTURL "http://pippin.gimp.org/# "Publisher" link 13 | # This is the size (in kB) of all the files copied into "Program Files" 14 | !define INSTALLSIZE 20000 15 | 16 | RequestExecutionLevel admin 17 | 18 | InstallDir "$PROGRAMFILES\Kernagic" 19 | 20 | !include "MUI2.nsh" 21 | !include LogicLib.nsh 22 | 23 | LicenseData LICENSE 24 | 25 | !macro VerifyUserIsAdmin 26 | UserInfo::GetAccountType 27 | pop $0 28 | ${If} $0 != "admin" ;Require admin rights on NT4+ 29 | messageBox mb_iconstop "Administrator rights required!" 30 | setErrorLevel 740 ;ERROR_ELEVATION_REQUIRED 31 | quit 32 | ${EndIf} 33 | !macroend 34 | 35 | page license 36 | page directory 37 | Page instfiles 38 | 39 | Name "Kernagic" 40 | Outfile "kernagic-installer.exe" 41 | DirText "This will install kernagic a font spacing tool (for font designers), choose location for kernagic's files" 42 | 43 | function .onInit 44 | setShellVarContext all 45 | !insertmacro VerifyUserIsAdmin 46 | functionEnd 47 | 48 | Section ""; 49 | SetOutPath $INSTDIR 50 | File kernagic.exe 51 | File README 52 | File LICENSE 53 | File AUTHORS 54 | File gtk+-mingw-bundle\bin\*.dll 55 | WriteUninstaller "$INSTDIR\uninstall.exe" 56 | 57 | createDirectory "$SMPROGRAMS\${COMPANYNAME}" 58 | createShortCut "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk" "$INSTDIR\kernagic.exe" "" "$INSTDIR\logo.ico" 59 | 60 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayName" "${COMPANYNAME} - ${APPNAME} - ${DESCRIPTION}" 61 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\"" 62 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "QuietUninstallString" "$\"$INSTDIR\uninstall.exe$\" /S" 63 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "InstallLocation" "$\"$INSTDIR$\"" 64 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayIcon" "$\"$INSTDIR\logo.ico$\"" 65 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "Publisher" "$\"${COMPANYNAME}$\"" 66 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "HelpLink" "$\"${HELPURL}$\"" 67 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "URLUpdateInfo" "$\"${UPDATEURL}$\"" 68 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "URLInfoAbout" "$\"${ABOUTURL}$\"" 69 | WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayVersion" "$\"${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}$\"" 70 | WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "VersionMajor" ${VERSIONMAJOR} 71 | WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "VersionMinor" ${VERSIONMINOR} 72 | # There is no option for modifying or repairing the install 73 | WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "NoModify" 1 74 | WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "NoRepair" 1 75 | # Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size 76 | WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "EstimatedSize" ${INSTALLSIZE} 77 | 78 | SectionEnd 79 | 80 | function un.onInit 81 | SetShellVarContext all 82 | 83 | #Verify the uninstaller - last chance to back out 84 | MessageBox MB_OKCANCEL "Permanantly remove ${APPNAME}?" IDOK next 85 | Abort 86 | next: 87 | !insertmacro VerifyUserIsAdmin 88 | functionEnd 89 | 90 | section "uninstall" 91 | # Remove Start Menu launcher 92 | delete "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk" 93 | # Try to remove the Start Menu folder - this will only happen if it is empty 94 | rmDir "$SMPROGRAMS\${COMPANYNAME}" 95 | 96 | delete $INSTDIR\kernagic.exe 97 | delete $INSTDIR\README 98 | delete $INSTDIR\LICENSE 99 | delete $INSTDIR\AUTHORS 100 | delete $INSTDIR\*.dll 101 | delete $INSTDIR\uninstall.exe 102 | rmDir $INSTDIR 103 | 104 | DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" 105 | sectionEnd 106 | -------------------------------------------------------------------------------- /lib-plist.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include "kernagic.h" 26 | 27 | /* A little comment about the ufo data-directory format (it isn't really a 28 | * file format; but using the file system directly for the structure of a 29 | * composite media object is a lot better than what most applications do). 30 | * 31 | * The painful part is the use of XML plists. The desire to do something clean 32 | * for adding user data has wasted too many hours; the amount of code it would 33 | * take to do something this trivial has prevented me from properly starting 34 | * adding the feature. Even with a SAX like parser at my disposal the format 35 | * is ugly and unapproachable; not willing to change this until UFO changes 36 | * the .plists for something with less technical cost/debt. 37 | * 38 | * I suspect other .plist dealing code in kernagic has a bug^Wmalfeature - 39 | * that likely is masked by painlist parsers; as long as it works those .plist 40 | * files are no less broken than the format already is. 41 | */ 42 | 43 | static GString *ts = NULL; 44 | 45 | #define KEY "com.hodefoting.kernagic.1.0" 46 | 47 | 48 | int kernagic_libplist_read (const char *path) 49 | { 50 | gchar *input = NULL; 51 | int ret = 0; 52 | 53 | g_file_get_contents (path, &input, NULL, NULL); 54 | if (!input) 55 | { 56 | return 0; 57 | } 58 | 59 | if (strstr (input, KEY)) 60 | { 61 | gchar *p = strstr (input, KEY); 62 | while (*p != '>') p++; 63 | p++; 64 | while (*p != '>') p++; 65 | p++; 66 | float a, b, c; 67 | sscanf (p, "gap=%f snap=%f bigscale=%f", &a, &b, &c); 68 | 69 | kerner_settings.gap = a; 70 | kerner_settings.snap = b; 71 | if (c != 0) 72 | kerner_settings.big_glyph_scaling = c; 73 | 74 | ret = 1; 75 | } 76 | g_free (input); 77 | return ret; 78 | } 79 | 80 | void kernagic_libplist_rewrite (const char *path) 81 | { 82 | gchar *input = NULL; 83 | g_file_get_contents (path, &input, NULL, NULL); 84 | if (!input) 85 | { 86 | input = g_strdup (" "); 87 | } 88 | 89 | if (strstr (input, KEY)) 90 | { 91 | /* erase prior data */ 92 | gchar *p = strstr (input, KEY); 93 | while (*p != '<') p--; 94 | if (!strchr (p, '^')) 95 | return; 96 | while (*p != '^') 97 | { 98 | *p = ' '; 99 | p++; 100 | } 101 | while (*p != '>') 102 | { 103 | *p = ' '; 104 | p++; 105 | } 106 | *p = ' '; 107 | } 108 | 109 | { 110 | /* remove */ 111 | gchar *p = &input[strlen(input)]; 112 | while (*p != '<') 113 | { 114 | *p = 0; 115 | p--; 116 | } 117 | *p = 0; 118 | while (*p != '<') 119 | { 120 | *p = 0; 121 | p--; 122 | } 123 | *p = 0; 124 | p--; 125 | while (*p == '\n' || *p == ' ') 126 | { 127 | *p = 0; 128 | p--; 129 | } 130 | } 131 | 132 | ts = g_string_new (""); 133 | 134 | g_string_append (ts, input); 135 | 136 | g_string_append_printf (ts, "\n%s\n", KEY); 137 | g_string_append_printf (ts, "gap=%f snap=%f bigscale=%f^\n", 138 | kerner_settings.gap, 139 | kerner_settings.snap, 140 | kerner_settings.big_glyph_scaling); 141 | g_string_append (ts, "\n\n"); 142 | 143 | g_file_set_contents (path, ts->str, ts->len, NULL); 144 | g_string_free (ts, TRUE); 145 | ts = NULL; 146 | 147 | g_free (input); 148 | return; 149 | } 150 | -------------------------------------------------------------------------------- /original.c: -------------------------------------------------------------------------------- 1 | #include "kernagic.h" 2 | 3 | static void original_each (Glyph *g, GtkProgressBar *progress) 4 | { 5 | g->right_bearing = (g->right_original * kerner_settings.tracking)/100.0; 6 | g->left_bearing = (g->left_original * kerner_settings.tracking)/100.0; 7 | } 8 | 9 | static KernagicMethod method = {"original", 10 | NULL, original_each, NULL}; 11 | 12 | KernagicMethod *kernagic_original = &method; 13 | -------------------------------------------------------------------------------- /render.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include "kernagic.h" 20 | 21 | #include 22 | 23 | /* This is the main rendering code of kernagic.. 24 | */ 25 | int debug_start_y = 0; 26 | float debug_scale = 1.0; 27 | 28 | uint8_t *kernagic_preview = NULL; 29 | #define MAX_BIG 1024 30 | 31 | Glyph *g_entries[MAX_BIG]; 32 | int x_entries[MAX_BIG]; 33 | int big = 0; 34 | 35 | typedef struct { 36 | unsigned long unicode; 37 | double x; 38 | double y; 39 | } TextGlyph; 40 | 41 | extern float scale_factor; 42 | 43 | gboolean toggle_measurement_lines = FALSE; 44 | 45 | 46 | float advance_glyph (Glyph *g, float xo, int yo, float scale) 47 | { 48 | return xo + kernagic_get_advance (g) * scale_factor * scale; 49 | } 50 | 51 | void render_glyph (Glyph *glyph); 52 | 53 | float place_glyph (Glyph *g, float xo, int yo, float opacity, float scale) 54 | { 55 | int canvas_w = canvas_width (); 56 | int canvas_h = canvas_height (); 57 | cairo_t *cr; 58 | cairo_surface_t *surface = 59 | cairo_image_surface_create_for_data (kernagic_preview, 60 | CAIRO_FORMAT_A8, canvas_w, canvas_h, canvas_w); 61 | cr = cairo_create (surface); 62 | 63 | g->cr = cr; 64 | 65 | xo += (g->left_bearing) * scale * scale_factor; 66 | 67 | /* do transforms so that the original coordinates in the unmodified parsed 68 | * file is correct cairo-side 69 | */ 70 | cairo_translate (cr, xo, yo + 2*kernagic_x_height () * scale * scale_factor); 71 | cairo_scale (cr, scale * scale_factor, scale * scale_factor * -1.0); 72 | 73 | /* render glyph, renders glyphs with ink_bounds cut off */ 74 | render_glyph (g); 75 | 76 | cairo_destroy (cr); 77 | cairo_surface_destroy (surface); 78 | return advance_glyph (g, xo, yo, scale); 79 | } 80 | 81 | void place_glyphs (TextGlyph *glyphs, int num_glyphs, float opacity, float scale) 82 | { 83 | int i; 84 | for (i = 0; i < num_glyphs; i ++) 85 | { 86 | Glyph *glyph = kernagic_find_glyph_unicode (glyphs[i].unicode); 87 | if (glyph) 88 | place_glyph (glyph, glyphs[i].x, glyphs[i].y, opacity, scale); 89 | } 90 | } 91 | 92 | static void draw_glyph_debug 93 | (Glyph *g, float xo, int yo, float opacity, float scale, int debuglevel) 94 | { 95 | int x, y; 96 | int canvas_w = canvas_width (); 97 | int canvas_h = canvas_height (); 98 | 99 | float y0 = yo; 100 | float y1 = y0 + 512 * scale * 0.8; /* XXX: should be based on xheight */ 101 | 102 | scale *= scale_factor; 103 | 104 | if (toggle_measurement_lines) 105 | { 106 | /* glyph-block edges */ 107 | for (y = y0; y < y1; y++) 108 | { 109 | int xs[] = {0, kernagic_get_advance (g) * scale}; 110 | int i; 111 | for (i = 0; i < sizeof(xs)/sizeof(xs[0]); i++) 112 | { 113 | x = xs[i]; 114 | if (x + xo >= 0 && 115 | x + xo < canvas_w && 116 | y >= 0 && 117 | y < canvas_h && 118 | kernagic_preview [y * canvas_w + (int)(x + xo)] == 0 119 | ) 120 | kernagic_preview [y * canvas_w + (int)(x + xo)] = 255; 121 | } 122 | } 123 | 124 | for (y = y0 * 0.9 + y1 * 0.1; y < y1 * 0.9 + y0 * 0.1; y++) 125 | { 126 | /* automatic stems */ 127 | if (g->stem_count >=1) 128 | { 129 | x = g->stems[0] * scale + g->left_bearing * scale; 130 | if (x + xo >= 0 && 131 | x + xo < canvas_w && 132 | y >= 0 && 133 | y < canvas_h) 134 | kernagic_preview [y * canvas_w + (int)(x + xo)] = 64; 135 | } 136 | if (g->stem_count > 1) 137 | { 138 | x = g->stems[g->stem_count-1] * scale + g->left_bearing * scale; 139 | if (x + xo >= 0 && 140 | x + xo < canvas_w && 141 | y >= 0 && 142 | y < canvas_h) 143 | kernagic_preview [y * canvas_w + (int)(x + xo)] = 64; 144 | } 145 | 146 | /* manual stems */ 147 | if (g->lstem > 0) 148 | { 149 | x = g->lstem * scale + g->left_bearing * scale; 150 | if (x + xo >= 0 && 151 | x + xo < canvas_w && 152 | y >= 0 && 153 | y < canvas_h) 154 | kernagic_preview [y * canvas_w + (int)(x + xo)] = 255; 155 | } 156 | if (g->rstem > 0) 157 | { 158 | x = g->rstem * scale + g->left_bearing * scale; 159 | if (x + xo >= 0 && 160 | x + xo < canvas_w && 161 | y >= 0 && 162 | y < canvas_h) 163 | kernagic_preview [y * canvas_w + (int)(x + xo)] = 255; 164 | } 165 | } 166 | } 167 | } 168 | 169 | void draw_text (const char *string, float x, float y, float scale) 170 | { 171 | } 172 | 173 | float measure_word_width (const gunichar *uword,int ulen, float scale) 174 | { 175 | int j; 176 | Glyph *g; 177 | Glyph *prev_g = NULL; 178 | float x = 0; 179 | for (j = 0; j < ulen; j++) 180 | { 181 | g = kernagic_find_glyph_unicode (uword[j]); 182 | if (!g) 183 | continue; 184 | 185 | if (prev_g) 186 | x += kernagic_kern_get (prev_g, g) * scale_factor * scale; 187 | 188 | x = advance_glyph (g, x, 0, scale); 189 | prev_g = g; 190 | } 191 | return x; 192 | } 193 | 194 | float waterfall_offset = 0.0; 195 | extern int desired_pos; 196 | 197 | void redraw_test_text (const char *intext, int debuglevel) 198 | { 199 | float period = kerner_settings.snap; 200 | const char *utf8; 201 | gunichar *str2; 202 | int i; 203 | float x0; 204 | float y0; 205 | float linestep = 512; 206 | float x, y; 207 | 208 | int canvas_w = canvas_width (); 209 | int canvas_h = canvas_height (); 210 | again: 211 | x0 = 0; 212 | y0 = PREVIEW_PADDING; 213 | x = x0; 214 | y = y0; 215 | 216 | memset (kernagic_preview, 0, canvas_w * canvas_h); 217 | debug_start_y = canvas_h/2; 218 | debug_scale = 1.0; 219 | 220 | big = 0; 221 | utf8 = intext; 222 | if (utf8) 223 | { 224 | TextGlyph text[2048]; 225 | int text_count = 0; 226 | 227 | str2 = g_utf8_to_ucs4 (utf8, -1, NULL, NULL, NULL); 228 | if (str2) 229 | { 230 | 231 | float scale = WATERFALL_START; 232 | int waterfall = WATERFALL_LEVELS; 233 | 234 | int w; 235 | 236 | for (w = 0; w < waterfall; w++) 237 | { 238 | linestep = 512 * scale; 239 | 240 | text_count = 0; 241 | 242 | y = y0; 243 | x = x0; 244 | 245 | x = x - waterfall_offset * scale * scale_factor + canvas_w/2; 246 | 247 | i = 0; 248 | GString *word = g_string_new (""); 249 | gunichar uword[1024]; 250 | int ulen = 0; 251 | int wrap = 0; 252 | 253 | int j; 254 | 255 | if (w == waterfall - 1) 256 | { 257 | debug_start_y = y; 258 | debug_scale = scale; 259 | } 260 | 261 | for (; str2[i]; i++) 262 | { 263 | Glyph *g = kernagic_find_glyph_unicode (str2[i]); 264 | 265 | if (str2[i] == '\n') 266 | { 267 | if (y > 150) 268 | break; 269 | 270 | if (wrap && x + measure_word_width (uword, ulen, scale) > canvas_w -PREVIEW_PADDING) 271 | { 272 | y += linestep; 273 | x = x0; 274 | } 275 | Glyph *prev_g = NULL; 276 | 277 | for (j = 0; j < ulen; j++) 278 | { 279 | Glyph *g; 280 | 281 | g = kernagic_find_glyph_unicode (uword[j]); 282 | if (g) 283 | { 284 | if (prev_g) 285 | x += kernagic_kern_get (prev_g, g) * scale_factor * scale; 286 | text[text_count].unicode = g->unicode; 287 | text[text_count].x = x; 288 | text[text_count++].y = y; 289 | 290 | x = advance_glyph (g, x, y, scale); 291 | prev_g = g; 292 | } 293 | } 294 | 295 | if (wrap) 296 | { 297 | y += linestep; 298 | x = x0; 299 | } 300 | g_string_assign (word, ""); 301 | ulen = 0; 302 | } 303 | else if (g) 304 | { 305 | g_string_append_unichar (word, g->unicode); 306 | uword[ulen++] = g->unicode; 307 | } 308 | else if (str2[i] == ' ') /* we're only faking it if we have to */ 309 | { 310 | Glyph *t = kernagic_find_glyph_unicode ('i'); 311 | int j; 312 | Glyph *prev_g = NULL; 313 | 314 | if (wrap && x + measure_word_width (uword, ulen, scale) > canvas_w-PREVIEW_PADDING) 315 | { 316 | y += linestep; 317 | x = x0; 318 | 319 | } 320 | 321 | for (j = 0; j < ulen; j++) 322 | { 323 | Glyph *g; 324 | 325 | g = kernagic_find_glyph_unicode (uword[j]); 326 | if (g) 327 | { 328 | if (prev_g) 329 | x += kernagic_kern_get (prev_g, g) * scale_factor * scale; 330 | 331 | text[text_count].unicode = g->unicode; 332 | text[text_count].x = x; 333 | text[text_count++].y = y; 334 | 335 | if (w == waterfall-1) 336 | { 337 | 338 | if (desired_pos != -1 && desired_pos <= i) 339 | { 340 | waterfall_offset = (x + waterfall_offset * scale * scale_factor - canvas_w/2)/scale/scale_factor; 341 | desired_pos = -1; 342 | goto again; 343 | } 344 | g_entries[big] = g; 345 | x_entries[big++] = x; 346 | draw_glyph_debug (g, x, y, 1.0, scale, debuglevel); 347 | } 348 | 349 | x = advance_glyph (g, x, y, scale); 350 | prev_g = g; 351 | } 352 | else 353 | { 354 | g_entries[big] = kernagic_find_glyph_unicode ('i'); 355 | x_entries[big++] = x + waterfall_offset; 356 | } 357 | } 358 | 359 | if (t) 360 | x += kernagic_get_advance (t) * scale_factor * scale; 361 | 362 | g_string_assign (word, ""); 363 | ulen = 0; 364 | } 365 | } 366 | 367 | if (word->len) 368 | { 369 | if (wrap && x + measure_word_width (uword, ulen, scale) > canvas_w-PREVIEW_PADDING) 370 | { 371 | y += linestep; 372 | x = x0; 373 | } 374 | Glyph *prev_g = NULL; 375 | 376 | for (j = 0; j < ulen; j++) 377 | { 378 | Glyph *g; 379 | 380 | g = kernagic_find_glyph_unicode (uword[j]); 381 | if (g) 382 | { 383 | if (prev_g) 384 | x += kernagic_kern_get (prev_g, g) * scale_factor * scale; 385 | 386 | if (w == waterfall-1) 387 | { 388 | g_entries[big] = g; 389 | x_entries[big++] = x; 390 | 391 | if (desired_pos != -1 && desired_pos <= i) 392 | { 393 | waterfall_offset = (x + waterfall_offset * scale * scale_factor - canvas_w/2)/scale/scale_factor; 394 | desired_pos = -1; 395 | goto again; 396 | } 397 | 398 | draw_glyph_debug (g, x, y, 1.0, scale, debuglevel); 399 | } 400 | 401 | text[text_count].unicode = g->unicode; 402 | text[text_count].x = x; 403 | text[text_count++].y = y; 404 | 405 | x = advance_glyph (g, x, y, scale); 406 | prev_g = g; 407 | } 408 | else 409 | { 410 | if (w == waterfall - 1) 411 | { 412 | g_entries[big] = kernagic_find_glyph_unicode ('i'); 413 | x_entries[big++] = x; 414 | } 415 | } 416 | } 417 | } 418 | 419 | /* we wait with the blast until here */ 420 | place_glyphs (text, text_count, 1.0, scale); 421 | 422 | if (waterfall > 1) 423 | { 424 | y0 += 512 * scale * WATERFALL_SPACING; 425 | scale = scale * WATERFALL_SCALING; 426 | 427 | if (w == waterfall -2) /* snap last level to full size */ 428 | scale = 1.0; 429 | } 430 | g_string_free (word, TRUE); 431 | if (w == waterfall-1 && desired_pos != -1) 432 | { 433 | waterfall_offset = (x + waterfall_offset * scale * scale_factor - canvas_w/2)/scale/scale_factor; 434 | desired_pos = -1; 435 | goto again; 436 | } 437 | } 438 | g_free (str2); 439 | } 440 | } 441 | 442 | if (toggle_measurement_lines && period * scale_factor * debug_scale > 2) 443 | { 444 | int i; 445 | for (i = 0; i * period * scale_factor * debug_scale < canvas_w - period * scale_factor * debug_scale; i++) 446 | { 447 | int y; 448 | int x = (i + 0.5) * period * scale_factor * debug_scale; 449 | float y0, y1; 450 | 451 | y0 = debug_start_y + kernagic_x_height () * scale_factor * debug_scale * 0.8; 452 | y1 = debug_start_y + kernagic_x_height () * scale_factor * debug_scale * 2.2; 453 | 454 | for (y = y0; y < y1; y++) 455 | { 456 | if (x >= 0 && 457 | x < canvas_w && 458 | y >= 0 && 459 | y < canvas_h) 460 | kernagic_preview[y* canvas_w + x] = 461 | kernagic_preview[y* canvas_w + x] * 0.9 + 462 | 255 * 0.1; 463 | } 464 | } 465 | } 466 | } 467 | -------------------------------------------------------------------------------- /rythm.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include "kernagic.h" 20 | 21 | extern float scale_factor; 22 | 23 | float left_most_center (Glyph *g); 24 | float right_most_center (Glyph *g); 25 | static float n_width = 0; 26 | 27 | static void kernagic_rythm_init (void) 28 | { 29 | Glyph *g = kernagic_find_glyph_unicode ('n'); 30 | if (!g) 31 | return; 32 | n_width = (right_most_center (g) - left_most_center(g)) * scale_factor; 33 | } 34 | 35 | static void kernagic_rythm_each (Glyph *g, GtkProgressBar *progress) 36 | { 37 | float period = kerner_settings.snap; 38 | float gap = kerner_settings.gap; 39 | float rythm = 1; 40 | float left, right; 41 | float lstem; 42 | float rstem; 43 | 44 | if (rythm == 0) 45 | rythm = 1; 46 | 47 | lstem = g->stems[0]; 48 | rstem = g->stems[g->stem_count-1]; 49 | 50 | /* if manual overrides are set, use them */ 51 | if (g->lstem > 0.0) 52 | lstem = g->lstem; 53 | if (g->rstem > 0.0) 54 | rstem = g->rstem; 55 | 56 | left = period * (gap + 0.5) - lstem; 57 | 58 | /* can we come up with something better than ink_width here?.. */ 59 | 60 | right = left + rstem + (period * gap) + period; 61 | right = ((int)(right/period))*period; 62 | right = right - (left + g->ink_width); 63 | 64 | right = (period *rythm) - fmod (left + g->ink_width , period * rythm); 65 | 66 | left = left * kerner_settings.tracking / 100.0; 67 | right = right * kerner_settings.tracking / 100.0; 68 | 69 | kernagic_set_left_bearing (g, left); 70 | kernagic_set_right_bearing (g, right); 71 | } 72 | 73 | static KernagicMethod method = {"rythm", kernagic_rythm_init, kernagic_rythm_each, NULL}; 74 | KernagicMethod *kernagic_rythm = &method; 75 | 76 | -------------------------------------------------------------------------------- /tests/Makefile: -------------------------------------------------------------------------------- 1 | check: tests 2 | diff reference output 3 | ipsum=exalted packets ampuls flipped zaniest rump whose fiscal tank billowy blamer litchis immure woolies crafted staidly funnily inveigh marshes teaks 4 | 5 | tests: 6 | rm -rf output/* 7 | ../kernagic input/SansGuiltDB.ufo -m original -s '$(ipsum)' -p output/original.png 8 | ../kernagic input/SansGuiltDB.ufo -m cadence -s '$(ipsum)' -p output/cadence.png 9 | ../kernagic input/SansGuiltDB.ufo -m gap -c 10.93 -s '$(ipsum)' -p output/gap.png 10 | ../kernagic input/SansGuiltDB.ufo -m bounds -s '$(ipsum)' -p output/bounds.png 11 | -------------------------------------------------------------------------------- /tests/reference/bounds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hodefoting/kernagic/30be68c06a7bf6010d26eefd45ee9504d1239f44/tests/reference/bounds.png -------------------------------------------------------------------------------- /tests/reference/cadence.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hodefoting/kernagic/30be68c06a7bf6010d26eefd45ee9504d1239f44/tests/reference/cadence.png -------------------------------------------------------------------------------- /tests/reference/gap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hodefoting/kernagic/30be68c06a7bf6010d26eefd45ee9504d1239f44/tests/reference/gap.png -------------------------------------------------------------------------------- /tests/reference/original.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hodefoting/kernagic/30be68c06a7bf6010d26eefd45ee9504d1239f44/tests/reference/original.png -------------------------------------------------------------------------------- /ufo-glyph-parser.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "kernagic.h" 25 | 26 | static int cc = 0; 27 | static float cx[2]; 28 | static float cy[2]; 29 | static float scx = 0; 30 | static float scy = 0; 31 | static int sc = 0; 32 | static int first = 0; 33 | 34 | extern gboolean kernagic_strip_bearing; /* XXX: global and passed out of bounds.. */ 35 | 36 | static int pinlib = 0; 37 | static int pinself = 0; 38 | 39 | static void 40 | parse_component (Glyph *glyph, const char *base, float xoffset, float yoffset) 41 | { 42 | Glyph *component_glyph = kernagic_find_glyph (base); 43 | if (component_glyph) 44 | { 45 | int x, y; 46 | y = component_glyph->ink_min_y; 47 | x = component_glyph->ink_min_x + component_glyph->left_original; 48 | 49 | x -= xoffset; 50 | y -= yoffset; 51 | if (x > glyph->ink_max_x) 52 | glyph->ink_max_x = x; 53 | if (y > glyph->ink_max_y) 54 | glyph->ink_max_y = y; 55 | if (x < glyph->ink_min_x) 56 | glyph->ink_min_x = x; 57 | if (y < glyph->ink_min_y) 58 | glyph->ink_min_y = y; 59 | 60 | y = component_glyph->ink_max_y; 61 | x = component_glyph->ink_max_x + component_glyph->left_original; 62 | x -= xoffset; 63 | y -= yoffset; 64 | if (x > glyph->ink_max_x) 65 | glyph->ink_max_x = x; 66 | if (y > glyph->ink_max_y) 67 | glyph->ink_max_y = y; 68 | if (x < glyph->ink_min_x) 69 | glyph->ink_min_x = x; 70 | if (y < glyph->ink_min_y) 71 | glyph->ink_min_y = y; 72 | } 73 | else 74 | fprintf (stderr, "Problems importing '%s' maybe the component %s is using components\n", glyph->name, base); 75 | } 76 | 77 | static void 78 | parse_start_element (GMarkupParseContext *context, 79 | const gchar *element_name, 80 | const gchar **attribute_names, 81 | const gchar **attribute_values, 82 | gpointer user_data, 83 | GError **error) 84 | { 85 | Glyph *glyph = user_data; 86 | if (!strcmp (element_name, "glyph")) 87 | { 88 | const char **a_n, **a_v; 89 | for (a_n = attribute_names, 90 | a_v = attribute_values; *a_n; a_n++, a_v++) 91 | { 92 | if (!strcmp (*a_n, "name")) 93 | glyph->name = g_strdup (*a_v); 94 | } 95 | glyph->ink_min_x = 8192; 96 | glyph->ink_min_y = 8192; 97 | glyph->ink_max_x = -8192; 98 | glyph->ink_max_y = -8192; 99 | } 100 | else if (!strcmp (element_name, "advance")) 101 | { 102 | const char **a_n, **a_v; 103 | for (a_n = attribute_names, 104 | a_v = attribute_values; *a_n; a_n++, a_v++) 105 | { 106 | if (!strcmp (*a_n, "width")) 107 | glyph->right_original = atoi (*a_v); 108 | } 109 | } 110 | else if (!strcmp (element_name, "unicode")) 111 | { 112 | const char **a_n, **a_v; 113 | for (a_n = attribute_names, 114 | a_v = attribute_values; *a_n; a_n++, a_v++) 115 | { 116 | if (!strcmp (*a_n, "hex")) 117 | { 118 | unsigned int value; 119 | if(sscanf(*a_v, "%X;", &value) != 1) 120 | printf("Parse error\n"); 121 | glyph->unicode = value; 122 | } 123 | } 124 | } 125 | else if (!strcmp (element_name, "point")) 126 | { 127 | float x = 0; 128 | float y = 0; 129 | const char **a_n, **a_v; 130 | for (a_n = attribute_names, 131 | a_v = attribute_values; *a_n; a_n++, a_v++) 132 | { 133 | if (!strcmp (*a_n, "x")) 134 | x = atof (*a_v); 135 | if (!strcmp (*a_n, "y")) 136 | y = atof (*a_v); 137 | } 138 | if (x < glyph->ink_min_x) glyph->ink_min_x = x; 139 | if (y < glyph->ink_min_y) glyph->ink_min_y = y; 140 | if (x > glyph->ink_max_x) glyph->ink_max_x = x; 141 | if (y > glyph->ink_max_y) glyph->ink_max_y = y; 142 | } 143 | else if (!strcmp (element_name, "component")) 144 | { 145 | const char *base = ""; 146 | float xoffset = 0; 147 | float yoffset = 0; 148 | const char **a_n, **a_v; 149 | for (a_n = attribute_names, 150 | a_v = attribute_values; *a_n; a_n++, a_v++) 151 | { 152 | if (!strcmp (*a_n, "base")) base = *a_v; 153 | else if (!strcmp (*a_n, "xOffset")) xoffset = atof (*a_v); 154 | else if (!strcmp (*a_n, "yOffset")) yoffset = atof (*a_v); 155 | } 156 | parse_component (glyph, base, xoffset, yoffset); 157 | } 158 | else if (!strcmp (element_name, "lib")) 159 | { 160 | pinlib++; 161 | } 162 | } 163 | 164 | static void 165 | parse_text (GMarkupParseContext *context, 166 | const gchar *text, 167 | gsize text_len, 168 | gpointer user_data, 169 | GError **error) 170 | { 171 | Glyph *glyph = user_data; 172 | if (pinself) 173 | { 174 | if (!strcmp (text, "rstem")) 175 | pinself = 2; 176 | 177 | int number = atoi (text); 178 | if (number) 179 | { 180 | if (pinself == 1) 181 | glyph->lstem = number; 182 | else 183 | glyph->rstem = number; 184 | } 185 | } 186 | if (!strcmp (text, "org.pippin.gimp.org.kernagic")) 187 | { 188 | pinself = 1; 189 | } 190 | } 191 | 192 | static void 193 | parse_end_element (GMarkupParseContext *context, 194 | const gchar *element_name, 195 | gpointer user_data, 196 | GError **error) 197 | { 198 | //Glyph *glyph = user_data; 199 | if (!strcmp (element_name, "lib")) 200 | pinlib --; 201 | if (!strcmp (element_name, "dict")) 202 | pinself = 0; 203 | } 204 | 205 | void render_glyph (Glyph *glyph); 206 | 207 | static void 208 | render_component (Glyph *glyph, const char *base, float xoffset, float yoffset) 209 | { 210 | Glyph *cglyph = NULL; 211 | cairo_t *cr = glyph->cr; 212 | cglyph = kernagic_find_glyph (base); 213 | //fprintf (stderr, "Component %s %f,%f %p\n", base, xoffset, yoffset, cglyph); 214 | cairo_save (cr); 215 | 216 | xoffset -= cglyph->offset_x; 217 | 218 | cairo_translate (cr, xoffset, yoffset); 219 | 220 | cglyph->cr = cr; 221 | render_glyph (cglyph); 222 | cairo_restore (cr); 223 | } 224 | 225 | static void 226 | glif_start_element (GMarkupParseContext *context, 227 | const gchar *element_name, 228 | const gchar **attribute_names, 229 | const gchar **attribute_values, 230 | gpointer user_data, 231 | GError **error) 232 | { 233 | Glyph *glyph = user_data; 234 | cairo_t *cr = glyph->cr; 235 | 236 | if (!strcmp (element_name, "point")) 237 | { 238 | int offcurve = 1; 239 | int curveto = 0; 240 | float x = 0, y = 0; 241 | const char **a_n, **a_v; 242 | for (a_n = attribute_names, 243 | a_v = attribute_values; *a_n; a_n++, a_v++) 244 | { 245 | if (!strcmp (*a_n, "x")) 246 | x = atof (*a_v) + glyph->offset_x; 247 | else if (!strcmp (*a_n, "y")) 248 | y = atof (*a_v); 249 | else if (!strcmp (*a_n, "type")) 250 | { 251 | if (!strcmp (*a_v, "line") || 252 | !strcmp (*a_v, "curve")) 253 | offcurve = 0; 254 | if (!strcmp (*a_v, "curve")) 255 | curveto = 1; 256 | } 257 | } 258 | if (offcurve) 259 | { 260 | cx[cc] = x; 261 | cy[cc] = y; 262 | cc++; 263 | /* too many points; wrong type of curve */ 264 | assert (cc <= 2); 265 | } 266 | else 267 | { 268 | if (curveto && cc == 2) 269 | cairo_curve_to (cr, cx[0], cy[0], 270 | cx[1], cy[1], 271 | x, y); 272 | else if (curveto && cc == 1) 273 | cairo_curve_to (cr, cx[0], cy[0], 274 | cx[0], cy[0], 275 | x, y); 276 | else if (curveto && cc == 0) 277 | { 278 | if (first) 279 | { 280 | scx = x; 281 | scy = y; 282 | sc = 1; 283 | cairo_move_to (cr, x, y); 284 | } 285 | else 286 | { 287 | cairo_curve_to (cr, cx[0], cy[0], 288 | cx[0], cy[0], 289 | x, y); 290 | } 291 | } 292 | else 293 | cairo_line_to (cr, x, y); 294 | cc = 0; 295 | } 296 | first = 0; 297 | } 298 | else if (!strcmp (element_name, "contour")) 299 | { 300 | cairo_new_sub_path (cr); 301 | first = 1; 302 | sc = 0; 303 | cc = 0; 304 | } 305 | else if (!strcmp (element_name, "component")) 306 | { 307 | const char *base = ""; 308 | float xoffset = 0; 309 | float yoffset = 0; 310 | const char **a_n, **a_v; 311 | for (a_n = attribute_names, 312 | a_v = attribute_values; *a_n; a_n++, a_v++) 313 | { 314 | if (!strcmp (*a_n, "base")) base = *a_v; 315 | else if (!strcmp (*a_n, "xOffset")) xoffset = atof (*a_v); 316 | else if (!strcmp (*a_n, "yOffset")) yoffset = atof (*a_v); 317 | } 318 | render_component (glyph, base, xoffset, yoffset); 319 | } 320 | } 321 | 322 | static void 323 | glif_end_element (GMarkupParseContext *context, 324 | const gchar *element_name, 325 | gpointer user_data, 326 | GError **error) 327 | { 328 | Glyph *glyph = user_data; 329 | cairo_t *cr = glyph->cr; 330 | 331 | if (!strcmp (element_name, "contour")) 332 | { 333 | if (sc) 334 | { 335 | if (cc != 2) 336 | { 337 | fprintf (stderr, "it seems like font contained non cubic outlines, kernagic only deals with cubic bezier\n"); 338 | assert (cc == 2); 339 | } 340 | cairo_curve_to (cr, cx[0], cy[0], 341 | cx[1], cy[1], 342 | scx, scy); 343 | } 344 | } 345 | else if (!strcmp (element_name, "outline")) 346 | { 347 | cairo_set_source_rgb (cr, 0.0, 0.0,0.0); 348 | cairo_fill_preserve (cr); 349 | } 350 | } 351 | 352 | static GMarkupParser glif_parse = 353 | { parse_start_element, parse_end_element, parse_text, NULL, NULL }; 354 | 355 | static GMarkupParser glif_render = 356 | { glif_start_element, glif_end_element, NULL, NULL, NULL }; 357 | 358 | extern float scale_factor; 359 | 360 | void render_glyph (Glyph *glyph) 361 | { 362 | cairo_t *cr = glyph->cr; 363 | GMarkupParseContext *ctx; 364 | cairo_save (cr); 365 | ctx = g_markup_parse_context_new (&glif_render, 0, glyph, NULL); 366 | g_markup_parse_context_parse (ctx, glyph->xml, strlen (glyph->xml), NULL); 367 | g_markup_parse_context_free (ctx); 368 | cairo_restore (cr); 369 | 370 | { 371 | int y; 372 | for (y = 0; y < glyph->r_height; y ++) 373 | { 374 | int x; 375 | for (x = glyph->r_width -1; x>=0; x--) 376 | { 377 | if (glyph->raster[y * glyph->r_width + x] != 0) 378 | break; 379 | } 380 | glyph->scan_width[y] = x + 1; 381 | } 382 | } 383 | } 384 | 385 | void 386 | load_ufo_glyph (Glyph *glyph) 387 | { 388 | GMarkupParseContext *ctx; 389 | 390 | if (glyph->loaded) 391 | return; 392 | glyph->loaded = 1; 393 | 394 | ctx = g_markup_parse_context_new (&glif_parse, 0, glyph, NULL); 395 | g_markup_parse_context_parse (ctx, glyph->xml, strlen (glyph->xml), NULL); 396 | g_markup_parse_context_free (ctx); 397 | 398 | glyph->ink_width = glyph->ink_max_x - glyph->ink_min_x; 399 | glyph->ink_height = glyph->ink_max_y - glyph->ink_min_y; 400 | 401 | if (kernagic_strip_bearing) 402 | { 403 | glyph->offset_x = -glyph->ink_min_x; 404 | glyph->left_original = glyph->ink_min_x; 405 | glyph->right_original -= glyph->ink_max_x; 406 | 407 | glyph->ink_min_x += glyph->offset_x; 408 | glyph->ink_max_x += glyph->offset_x; 409 | 410 | glyph->left_bearing = 0; 411 | glyph->right_bearing = 0; 412 | } 413 | else 414 | { 415 | fprintf (stderr, "ow\n"); 416 | } 417 | } 418 | 419 | void gen_debug (Glyph *glyph) 420 | { 421 | uint8_t *raster = glyph->raster; 422 | int x; 423 | int t; 424 | float x_height = kernagic_x_height (); 425 | 426 | for (t = 1; t < 3; t ++) 427 | for (x = 0; x < glyph->r_width; x++) 428 | { 429 | long sum = 0; 430 | int y; 431 | long c = 0; 432 | int y0 = x_height * 1.0 * scale_factor; 433 | int y1 = x_height * 2.0 * scale_factor; 434 | 435 | for (y = y0; y < y1; y++) 436 | { 437 | sum += raster[glyph->r_width * y + x]; 438 | c++; 439 | } 440 | { 441 | int foo = sum / c; 442 | // foo /= 32; 443 | // foo *= 32; 444 | raster [glyph->r_width * (glyph->r_height-t) + x] = foo; 445 | } 446 | } 447 | 448 | /* detect candidate stems/rythm points*/ 449 | long sum = 0; 450 | long sum2 = 0; 451 | 452 | for (x = glyph->ink_width/2 * scale_factor * 0.0; 453 | x < glyph->ink_width * scale_factor * 0.3; x++) 454 | { 455 | int val; 456 | t = 1; 457 | val = raster [glyph->r_width * (glyph->r_height-t) + x]; 458 | sum += val; 459 | } 460 | sum2 = 0; 461 | for (x = 0; sum2 < sum/2; x++) 462 | { 463 | int val; 464 | t = 1; 465 | val = raster [glyph->r_width * (glyph->r_height-t) + x]; 466 | sum2 += val; 467 | } 468 | 469 | glyph->stems[glyph->stem_count] = x / scale_factor; 470 | glyph->stem_weight[glyph->stem_count++] = x; 471 | 472 | sum = 0; 473 | sum2 = 0; 474 | for (x = glyph->ink_width * scale_factor * 0.7; 475 | x < glyph->ink_width * scale_factor * 1.0; x++) 476 | { 477 | int val; 478 | t = 2; 479 | val = raster [glyph->r_width * (glyph->r_height-t) + x]; 480 | sum += val; 481 | } 482 | sum2 = 0; 483 | for (x = glyph->ink_width * scale_factor * 0.7; 484 | sum2 < sum /2 && x < glyph->r_width; x++) 485 | { 486 | int val; 487 | t = 1; 488 | val = raster [glyph->r_width * (glyph->r_height-t) + x]; 489 | sum2 += val; 490 | } 491 | 492 | glyph->stems[glyph->stem_count] = x / scale_factor; 493 | glyph->stem_weight[glyph->stem_count++] = 1; 494 | 495 | for (t = 1; t < 3; t ++) 496 | for (x = 0; x < glyph->r_width; x++) 497 | { 498 | raster [glyph->r_width * (glyph->r_height-t) + x] = 0; 499 | } 500 | } 501 | 502 | void 503 | render_ufo_glyph (Glyph *glyph) 504 | { 505 | cairo_surface_t *surface; 506 | cairo_t *cr; 507 | assert (glyph->xml); 508 | 509 | load_ufo_glyph (glyph); 510 | { 511 | glyph->r_width = kernagic_x_height () * scale_factor * 2.5; 512 | glyph->r_height = kernagic_x_height () * scale_factor * 2.8; 513 | glyph->r_width /= 16; 514 | glyph->r_width *= 16; 515 | 516 | glyph->raster = g_malloc0 (glyph->r_width * glyph->r_height); 517 | } 518 | int width = glyph->r_width; 519 | int height = glyph->r_height; 520 | uint8_t *raster = glyph->raster; 521 | assert (raster); 522 | 523 | surface = cairo_image_surface_create_for_data (raster, CAIRO_FORMAT_A8, 524 | width, height, width); 525 | cr = cairo_create (surface); 526 | glyph->cr = cr; 527 | 528 | cairo_set_source_rgba (cr, 1, 1, 1, 0); 529 | cairo_paint(cr); 530 | 531 | cairo_translate (cr, 0, kernagic_x_height () * 2.0 * scale_factor); 532 | cairo_scale (cr, scale_factor, -scale_factor); 533 | render_glyph (glyph); 534 | 535 | cairo_destroy (cr); 536 | glyph->cr = NULL; 537 | cairo_surface_destroy (surface); 538 | 539 | gen_debug (glyph); 540 | } 541 | 542 | /**********************************************************************************/ 543 | 544 | static GString *ts = NULL; 545 | 546 | static int inlib = 0; 547 | static int inself = 0; 548 | 549 | 550 | static void 551 | rewrite_start_element (GMarkupParseContext *context, 552 | const gchar *element_name, 553 | const gchar **attribute_names, 554 | const gchar **attribute_values, 555 | gpointer user_data, 556 | GError **error) 557 | { 558 | Glyph *glyph = user_data; 559 | 560 | if (inself) 561 | return; 562 | 563 | g_string_append_printf (ts, "<%s", element_name); 564 | const char **a_n, **a_v; 565 | 566 | if (!strcmp (element_name, "lib")) 567 | { 568 | inlib ++; 569 | } 570 | 571 | if (!strcmp (element_name, "component")) 572 | { 573 | const char *base = ""; 574 | float xoffset = 0; 575 | float yoffset = 0; 576 | const char **a_n, **a_v; 577 | for (a_n = attribute_names, 578 | a_v = attribute_values; *a_n; a_n++, a_v++) 579 | { 580 | if (!strcmp (*a_n, "base")) base = *a_v; 581 | else if (!strcmp (*a_n, "xOffset")) xoffset = atof (*a_v); 582 | else if (!strcmp (*a_n, "yOffset")) yoffset = atof (*a_v); 583 | } 584 | 585 | xoffset = xoffset + glyph->offset_x + glyph->left_bearing; 586 | 587 | Glyph *component_glyph = kernagic_find_glyph (base); 588 | if (component_glyph) 589 | { 590 | xoffset -= (component_glyph->offset_x + component_glyph->left_bearing); 591 | } 592 | 593 | xoffset += kernagic_x_shift; 594 | 595 | g_string_append_printf (ts, " base=\"%s\" xOffset=\"%f\" yOffset=\"%f\" ", base, xoffset, yoffset); 596 | } 597 | else 598 | { 599 | for (a_n = attribute_names, 600 | a_v = attribute_values; *a_n; a_n++, a_v++) 601 | { 602 | if (!strcmp (element_name, "point") && !strcmp (*a_n, "x")) 603 | { 604 | char str[512]; 605 | int value = atoi (*a_v); 606 | value = value + glyph->offset_x + glyph->left_bearing; 607 | 608 | value += kernagic_x_shift; 609 | sprintf (str, "%d", value); 610 | g_string_append_printf (ts, " %s=\"%s\"", *a_n, str); 611 | } 612 | else if (!strcmp (element_name, "advance") && !strcmp (*a_n, "width")) 613 | { 614 | char str[512]; 615 | sprintf (str, "%d", (int)(kernagic_get_advance (glyph))); 616 | g_string_append_printf (ts, " %s=\"%s\"", *a_n, str); 617 | } 618 | else 619 | { 620 | g_string_append_printf (ts, " %s=\"%s\"", *a_n, *a_v); 621 | } 622 | } 623 | } 624 | g_string_append_printf (ts, ">"); 625 | } 626 | 627 | static void 628 | rewrite_end_element (GMarkupParseContext *context, 629 | const gchar *element_name, 630 | gpointer user_data, 631 | GError **error) 632 | { 633 | Glyph *glyph = user_data; 634 | 635 | if (!strcmp (element_name, "lib")) 636 | { 637 | inlib --; 638 | } 639 | 640 | if (!inself) 641 | g_string_append_printf (ts, "", element_name); 642 | 643 | if (inself) 644 | { 645 | if (!strcmp (element_name, "dict")) 646 | { 647 | inself = 0; 648 | g_string_append_printf (ts, "lstem%0.0frstem%0.0f", 649 | glyph->lstem, glyph->rstem); 650 | } 651 | } 652 | } 653 | 654 | static void 655 | rewrite_text (GMarkupParseContext *context, 656 | const gchar *text, 657 | gsize text_len, 658 | gpointer user_data, 659 | GError **error) 660 | { 661 | if (inself) 662 | return; 663 | g_string_append_len (ts, text, text_len); 664 | 665 | if (inlib) 666 | { 667 | if (!strcmp (text, "org.pippin.gimp.org.kernagic")) 668 | inself = 1; 669 | } 670 | } 671 | 672 | static void 673 | rewrite_passthrough (GMarkupParseContext *context, 674 | const gchar *passthrough_text, 675 | gsize text_len, 676 | gpointer user_data, 677 | GError **error) 678 | { 679 | g_string_append_len (ts, passthrough_text, text_len); 680 | } 681 | 682 | static GMarkupParser glif_rewrite = 683 | { rewrite_start_element, rewrite_end_element, rewrite_text, rewrite_passthrough, NULL }; 684 | 685 | void 686 | rewrite_ufo_glyph (Glyph *glyph) 687 | { 688 | GString *tmp = g_string_new (""); 689 | ts = g_string_new (""); 690 | GMarkupParseContext *ctx = g_markup_parse_context_new (&glif_rewrite, 0, glyph, NULL); 691 | 692 | 693 | /* ensure that a lib section exist and that it contains a skeleton kernagic 694 | * section. 695 | */ 696 | 697 | if (strstr (glyph->xml, "xml, "org.pippin.gimp.kernagic")) 700 | { 701 | g_string_append (tmp, glyph->xml); 702 | } 703 | else 704 | { 705 | char *cut = strdup (glyph->xml); 706 | char *p = strstr (cut, ""); 710 | rest = p+1; 711 | if (p) 712 | *p = 0; 713 | g_string_append (tmp, cut); 714 | g_string_append (tmp, ">org.pippin.gimp.org.kernagiclstem0rstem0\n"); 715 | g_string_append (tmp, ""); 716 | g_string_append (tmp, rest); 717 | free (cut); 718 | } 719 | } 720 | else 721 | { 722 | char *cut = strdup (glyph->xml); 723 | char *p = strstr (cut, ""); 724 | if (p) 725 | *p = 0; 726 | g_string_append (tmp, cut); 727 | g_string_append (tmp, "org.pippin.gimp.org.kernagiclstem0rstem0\n\n"); 728 | g_string_append (tmp, "\n"); 729 | free (cut); 730 | } 731 | 732 | /* detect whether lstem and rstem are already existing,. if they do rewrite 733 | * them. 734 | * 735 | * otherwise insert stems.. after initial rewrite by simple search and 736 | * replace. 737 | */ 738 | 739 | g_markup_parse_context_parse (ctx, tmp->str, strlen (tmp->str), NULL); 740 | g_markup_parse_context_free (ctx); 741 | g_file_set_contents (glyph->path, ts->str, ts->len, NULL); 742 | g_string_free (ts, TRUE); 743 | g_string_free (tmp, TRUE); 744 | ts = NULL; 745 | } 746 | -------------------------------------------------------------------------------- /ui-gtk.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kernagic a libre spacing tool for Unified Font Objects. 3 | Copyright (C) 2013 Øyvind Kolås 4 | 5 | Kernagicis free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Kernagic is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Kernagic. If not, see . */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "kernagic.h" 24 | #include "gimpspinscale.h" 25 | 26 | static char *ipsum = NULL; 27 | 28 | #define INDEX_WIDTH 256 29 | #define INDEX_HEIGHT 256 30 | 31 | #define GTK2 1 32 | #define HILBERTCODE 1 33 | 34 | #ifdef GTK2 35 | #else 36 | #define gtk_hbox_new(a,n) gtk_box_new (GTK_ORIENTATION_HORIZONTAL, n) 37 | #define gtk_vbox_new(a,n) gtk_box_new (GTK_ORIENTATION_VERTICAL, n) 38 | #endif 39 | 40 | #define RESET_WATERFALL waterfall_offset = 0 41 | //waterfall_offset = 48784; 42 | 43 | extern Glyph *g_entries[]; 44 | extern int x_entries[]; 45 | extern int big; 46 | extern gboolean toggle_measurement_lines; 47 | extern float scale_factor; 48 | extern char *kernagic_sample_text; 49 | extern uint8_t *kernagic_preview; 50 | static GtkWidget *preview; 51 | static GtkWidget *GWindex = NULL; 52 | static GtkWidget *test_text; 53 | static GtkWidget *ipsum_glyphs; 54 | static GtkWidget *spin_method; 55 | static GtkWidget *spin_min_dist; 56 | static GtkWidget *spin_max_dist; 57 | static GtkWidget *spin_snap; 58 | static GtkWidget *spin_divisor; 59 | static GtkWidget *spin_tracking; 60 | static GtkWidget *spin_gap; 61 | static GtkWidget *spin_big_glyph_scaling; 62 | GtkWidget *spin_ipsum_no; 63 | 64 | 65 | static GtkWidget *vbox_options_original; 66 | static GtkWidget *vbox_options_gray; 67 | static GtkWidget *vbox_options_rythm; 68 | 69 | static GtkWidget *progress; 70 | static GtkWidget *toggle_measurement_lines_check; 71 | static GtkWidget *font_path; 72 | static GtkWidget *ipsum_path; 73 | static uint8_t *index_canvas = NULL; 74 | 75 | static void configure_kernagic (void) 76 | { 77 | kerner_settings.method = 78 | kernagic_method_no (gtk_combo_box_get_active (GTK_COMBO_BOX (spin_method))); 79 | 80 | kerner_settings.maximum_distance = 81 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_max_dist)); 82 | kerner_settings.minimum_distance = 83 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_min_dist)); 84 | kerner_settings.snap = 85 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_snap)); 86 | kerner_settings.divisor = 87 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_divisor)); 88 | kerner_settings.tracking = 89 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_tracking)); 90 | kerner_settings.gap = 91 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_gap)); 92 | kerner_settings.big_glyph_scaling = 93 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_big_glyph_scaling)); 94 | 95 | toggle_measurement_lines = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle_measurement_lines_check)); 96 | } 97 | 98 | static guint delayed_updater = 0; 99 | static gboolean delayed_trigger (gpointer foo) 100 | { 101 | GString *str = g_string_new (""); 102 | configure_kernagic (); 103 | 104 | g_string_append (str, gtk_entry_get_text (GTK_ENTRY (test_text))); 105 | if (ipsum) 106 | g_string_append (str, ipsum); 107 | 108 | kernagic_set_glyph_string (str->str); 109 | g_string_free (str, TRUE); 110 | kernagic_compute (NULL); 111 | 112 | redraw_test_text ( gtk_entry_get_text (GTK_ENTRY (test_text)),toggle_measurement_lines); 113 | 114 | gtk_widget_queue_draw (preview); 115 | 116 | if (GWindex) 117 | gtk_widget_queue_draw (GWindex); 118 | 119 | delayed_updater = 0; 120 | return FALSE; 121 | } 122 | 123 | static void trigger (void) 124 | { 125 | if (delayed_updater) 126 | { 127 | g_source_remove (delayed_updater); 128 | delayed_updater = 0; 129 | } 130 | delayed_updater = g_idle_add (delayed_trigger, NULL); 131 | } 132 | 133 | static int frozen = 0; 134 | 135 | static void trigger_divisor (void) 136 | { 137 | float divisor = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_divisor)); 138 | if (frozen) 139 | return; 140 | 141 | frozen++; 142 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_snap), 143 | n_distance () / divisor); 144 | frozen--; 145 | } 146 | 147 | void add_monitors (const char *font_path); 148 | 149 | static void trigger_cadence (void) 150 | { 151 | float cadence = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_snap)); 152 | //if (frozen) 153 | // return; 154 | 155 | frozen++; 156 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_divisor), 157 | n_distance () / cadence); 158 | frozen--; 159 | 160 | trigger (); 161 | } 162 | 163 | static void trigger_ipsum (void) 164 | { 165 | GString *str = g_string_new (""); 166 | GList *l, *list = kernagic_glyphs (); 167 | for (l = list; l; l = l->next) 168 | { 169 | Glyph *glyph = l->data; 170 | if (glyph->unicode) 171 | g_string_append_unichar (str, glyph->unicode); 172 | } 173 | if (!list) 174 | g_string_append (str, "abcdefghijklmnopqrstuvxyz"); 175 | if (ipsum) 176 | g_free (ipsum); 177 | ipsum = g_strdup (ipsumat_generate (NULL, str->str, 178 | gtk_entry_get_text (GTK_ENTRY (ipsum_glyphs)), 7, 11)); 179 | gtk_entry_set_text (GTK_ENTRY (test_text), ipsum); 180 | RESET_WATERFALL; 181 | trigger (); 182 | g_string_free (str, TRUE); 183 | } 184 | 185 | 186 | static guint delayed_reload_updater = 0; 187 | static gboolean delayed_reload_trigger (gpointer foo) 188 | { 189 | char path[4096]; 190 | char *ufo_path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (font_path)); 191 | kernagic_strip_left_bearing = TRUE; 192 | 193 | if (!ufo_path) 194 | return FALSE; 195 | 196 | kernagic_load_ufo (ufo_path, kernagic_strip_left_bearing); 197 | g_free (ufo_path); 198 | if (delayed_updater) 199 | { 200 | g_source_remove (delayed_updater); 201 | delayed_updater = 0; 202 | } 203 | delayed_trigger (foo); 204 | delayed_reload_updater = 0; 205 | 206 | 207 | /* override defaults with settings for file for snap/gap method */ 208 | sprintf (path, "%s/lib.plist", loaded_ufo_path); 209 | if (kernagic_libplist_read (path)) 210 | { 211 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_snap), 212 | kerner_settings.snap); 213 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_gap), 214 | kerner_settings.gap); 215 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_big_glyph_scaling), 216 | kerner_settings.big_glyph_scaling); 217 | } 218 | else 219 | { 220 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_divisor), KERNER_DEFAULT_DIVISOR+1); 221 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_divisor), KERNER_DEFAULT_DIVISOR); 222 | } 223 | 224 | return FALSE; 225 | } 226 | 227 | static void trigger_prop_show (void) 228 | { 229 | KernagicMethod *method = 230 | kernagic_method_no (gtk_combo_box_get_active (GTK_COMBO_BOX (spin_method))); 231 | 232 | gtk_widget_hide (vbox_options_gray); 233 | gtk_widget_hide (vbox_options_original); 234 | gtk_widget_hide (vbox_options_rythm); 235 | 236 | if (!strcmp (method->name, "original")) 237 | gtk_widget_show (vbox_options_original); 238 | else if (!strcmp (method->name, "gray")) 239 | gtk_widget_show (vbox_options_gray); 240 | else if (!strcmp (method->name, "rythm")|| 241 | !strcmp (method->name, "gap")) 242 | gtk_widget_show (vbox_options_rythm); 243 | } 244 | 245 | static void ipsum_reload (void) 246 | { 247 | int ipsum_no = 0; 248 | const char *path; 249 | if (ipsum) 250 | g_free (ipsum); 251 | ipsum = NULL; 252 | 253 | ipsum_no = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_ipsum_no)); 254 | path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (ipsum_path)); 255 | if (!path) 256 | { 257 | const char *ipsum = NULL; 258 | switch (ipsum_no) 259 | { 260 | case 1: 261 | ipsum = IPSUM0; 262 | break; 263 | case 2: 264 | ipsum = "yelping gluier soggily logbook baton adagios uncorks icecaps hydrant news prevue whelked towered quest except enrolls tried suppers goading journal cupolas drummer quests"; 265 | break; 266 | 267 | case 3: ipsum = "Click a small ipsum word to replace the position in the sample text being focused on. In the work area, clicking below the baseline removes custom rythm points, within the x-height left and right rythm point are set, above the x-height single rythm instance is specified."; 268 | 269 | break; 270 | 271 | default: 272 | ipsum = "Kernagic - 2013 © Øyvind Kolås - Released under AGPL"; 273 | break; 274 | } 275 | if (ipsum) 276 | { 277 | gtk_entry_set_text (GTK_ENTRY (test_text), ipsum); 278 | RESET_WATERFALL; 279 | trigger (); 280 | } 281 | return; 282 | } 283 | 284 | g_file_get_contents (path, &ipsum, NULL, NULL); 285 | if (ipsum) 286 | { 287 | GString *str; 288 | char *p; 289 | int lineno = 1; 290 | str = g_string_new (""); 291 | for (p = ipsum; *p; p++) 292 | { 293 | if (*p == '\n') 294 | { 295 | lineno++; 296 | } 297 | else 298 | { 299 | if (lineno == ipsum_no) 300 | { 301 | g_string_append_c (str, *p); 302 | } 303 | } 304 | } 305 | gtk_entry_set_text (GTK_ENTRY (test_text), str->str); 306 | RESET_WATERFALL; 307 | g_string_free (str, TRUE); 308 | trigger (); 309 | } 310 | } 311 | 312 | void trigger_reload (void) 313 | { 314 | if (delayed_reload_updater) 315 | { 316 | g_source_remove (delayed_reload_updater); 317 | delayed_reload_updater = 0; 318 | } 319 | delayed_reload_updater = g_timeout_add (250, delayed_reload_trigger, NULL); 320 | } 321 | 322 | #if 0 323 | static void set_defaults (void) 324 | { 325 | gtk_entry_set_text (GTK_ENTRY (test_text), "Kern Me Tight"); 326 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), KERNER_DEFAULT_MODE); 327 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_ipsum_no), 1); 328 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_min_dist), KERNER_DEFAULT_MIN); 329 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_max_dist), KERNER_DEFAULT_MAX); 330 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_snap), KERNER_DEFAULT_TARGET_GRAY); 331 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_divisor), KERNER_DEFAULT_DIVISOR); 332 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_gap), KERNER_DEFAULT_GAP); 333 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_tracking), KERNER_DEFAULT_TRACKING); 334 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle_measurement_lines_check), TRUE); 335 | } 336 | #endif 337 | 338 | static void set_defaults_from_args (void) 339 | { 340 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_ipsum_no), 1); 341 | 342 | if (kernagic_sample_text) 343 | gtk_entry_set_text (GTK_ENTRY (test_text), kernagic_sample_text); 344 | else 345 | { 346 | gtk_entry_set_text (GTK_ENTRY (test_text), IPSUM0); 347 | } 348 | RESET_WATERFALL; 349 | 350 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), kernagic_active_method_no()); 351 | 352 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_min_dist), kerner_settings.minimum_distance); 353 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_max_dist), kerner_settings.maximum_distance); 354 | 355 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_snap), kerner_settings.snap); 356 | 357 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_divisor), kerner_settings.divisor); 358 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_gap), kerner_settings.gap); 359 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_big_glyph_scaling), kerner_settings.big_glyph_scaling); 360 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_tracking), kerner_settings.tracking); 361 | 362 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle_measurement_lines_check), TRUE); 363 | } 364 | 365 | static void do_process (void) 366 | { 367 | kernagic_set_glyph_string (NULL); 368 | gtk_widget_show (progress); 369 | kernagic_compute (GTK_PROGRESS_BAR (progress)); 370 | gtk_widget_hide (progress); 371 | kernagic_save_kerning_info (); 372 | } 373 | 374 | static void do_save (void) 375 | { 376 | do_process (); 377 | kernagic_save_kerning_info (); 378 | } 379 | 380 | 381 | int desired_pos = -1; 382 | 383 | static gboolean 384 | cursor_position_changed_cb (GtkWidget *widget) 385 | { 386 | gint foo; 387 | g_object_get (widget, "cursor-position", &foo, NULL); 388 | desired_pos = foo; 389 | return FALSE; 390 | } 391 | 392 | static int pressed = 0; 393 | static float prevx = 0; 394 | 395 | static gboolean 396 | preview_motion_cb (GtkWidget *widget, GdkEvent *event, gpointer data) 397 | { 398 | float x = event->motion.x; 399 | if (pressed == 2) 400 | { 401 | waterfall_offset += (prevx-x) / scale_factor; 402 | trigger (); 403 | } 404 | 405 | prevx = x; 406 | return TRUE; 407 | } 408 | 409 | 410 | 411 | static gboolean 412 | preview_release_cb (GtkWidget *widget, GdkEvent *event, gpointer data) 413 | { 414 | pressed = 0; 415 | trigger (); 416 | return TRUE; 417 | } 418 | 419 | static void adjusted (int glyph_no) 420 | { 421 | int i; 422 | float x = 0; 423 | float prev_pos = (float)(x_entries[glyph_no]); 424 | float new_pos; 425 | x = - waterfall_offset * scale_factor + canvas_width ()/2; 426 | 427 | for (i = 0; i < glyph_no; i ++) 428 | { 429 | x += kernagic_get_advance (g_entries[i]) * scale_factor; 430 | /* XXX: currently broken if doing kerning as well as metrics 431 | if (i > 0) 432 | x += kernagic_kern_get ( 433 | g_entries[i-1], 434 | g_entries[i]) * scale_factor; 435 | */ 436 | } 437 | new_pos = x; 438 | 439 | waterfall_offset -= (new_pos - prev_pos); 440 | } 441 | 442 | static gboolean 443 | preview_press_cb (GtkWidget *widget, GdkEvent *event, gpointer data) 444 | { 445 | int i = 0; 446 | float x, y; 447 | Glyph *g; 448 | float advance; 449 | x = event->button.x; 450 | y = event->button.y; 451 | 452 | pressed = event->button.button; 453 | if (pressed > 1) 454 | return TRUE; 455 | 456 | for (i = 0; i+1 < big && x_entries[i+1] < event->button.x; i++); 457 | 458 | if (i + 1 >= big) 459 | i = big-1; 460 | 461 | x -= x_entries[i]; 462 | y = (y-debug_start_y) / (kernagic_x_height() * scale_factor * debug_scale); 463 | 464 | g = g_entries[i]; 465 | 466 | x /= (scale_factor * debug_scale); 467 | 468 | if (g) 469 | advance = kernagic_get_advance (g); 470 | 471 | /* should be adjusted according to an y-offset */ 472 | 473 | /* for lowest y coords- do word picking from background ipsum, 474 | * for detailed adjustments 475 | */ 476 | 477 | if (y < 0) 478 | { 479 | int w; 480 | float y0= PREVIEW_PADDING; 481 | float scale = WATERFALL_START; 482 | float pscale = scale; 483 | y = event->button.y; 484 | for (w = 0; w < WATERFALL_LEVELS; w++) 485 | { 486 | if (y < y0) 487 | { 488 | desired_pos = -1; 489 | waterfall_offset = 490 | waterfall_offset + ((event->button.x - canvas_width()/2) / pscale / scale_factor); 491 | 492 | trigger (); 493 | return TRUE; 494 | } 495 | y0 += 512 * scale * WATERFALL_SPACING; 496 | pscale = scale; 497 | scale = scale * WATERFALL_SCALING; 498 | } 499 | } 500 | else if (g && y < 0.5) 501 | { 502 | g->rstem = x - g->left_bearing; 503 | g->lstem = x - g->left_bearing; 504 | 505 | adjusted (i); 506 | } 507 | else if (g && y > 2.0) 508 | { 509 | g->rstem = 0; 510 | g->lstem = 0; 511 | adjusted (i); 512 | } 513 | else if (g) 514 | { 515 | if (x / advance < 0.5) 516 | { 517 | g->lstem = x - g->left_bearing; 518 | } 519 | else 520 | { 521 | g->rstem = x - g->left_bearing; 522 | } 523 | adjusted (i); 524 | } 525 | else 526 | { 527 | fprintf (stderr, "unhandled\n"); 528 | } 529 | 530 | trigger (); 531 | return TRUE; 532 | } 533 | 534 | 535 | #ifdef GTK2 536 | static gboolean 537 | preview_draw_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data) 538 | { 539 | cairo_t *cr = gdk_cairo_create (widget->window); 540 | #else 541 | static gboolean 542 | preview_draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data) 543 | { 544 | #endif 545 | cairo_save (cr); 546 | cairo_set_source_rgb (cr, 0.93,0.93,0.93); 547 | cairo_paint (cr); 548 | 549 | if (!kernagic_preview) 550 | { 551 | } 552 | else 553 | { 554 | cairo_surface_t *surface = 555 | cairo_image_surface_create_for_data (kernagic_preview, 556 | CAIRO_FORMAT_A8, canvas_width (), canvas_height (), canvas_width ()); 557 | cairo_set_source_rgb (cr, 0,0,0); 558 | cairo_mask_surface (cr, surface, 0, 0); 559 | cairo_surface_destroy (surface); 560 | } 561 | cairo_restore (cr); 562 | #ifdef GTK2 563 | cairo_destroy (cr); 564 | #endif 565 | return FALSE; 566 | } 567 | 568 | 569 | /* hilbert curve functions from wikipedia */ 570 | static void rot(int n, int *x, int *y, int rx, int ry) { 571 | if (ry == 0) { 572 | if (rx == 1) { 573 | *x = n-1 - *x; 574 | *y = n-1 - *y; 575 | } 576 | 577 | //Swap x and y 578 | int t = *x; 579 | *x = *y; 580 | *y = t; 581 | } 582 | } 583 | 584 | 585 | static void 586 | d2xy (int n, int d, int *x, int *y) 587 | { 588 | int rx, ry, s, t=d; 589 | *x = *y = 0; 590 | for (s=1; s0; s/=2) 607 | { 608 | rx = (x & s) > 0; 609 | ry = (y & s) > 0; 610 | d += s * s * ((3 * rx) ^ ry); 611 | rot(s, &x, &y, rx, ry); 612 | } 613 | return d; 614 | } 615 | 616 | gboolean 617 | index_press_cb (GtkWidget *widget, GdkEvent *event, gpointer data) 618 | { 619 | GString *str = g_string_new (""); 620 | float x, y; 621 | int i; 622 | int unicode; 623 | x = event->button.x; 624 | y = event->button.y; 625 | unicode = xy2d (256, x, y); 626 | //sprintf (buf, "%f %f %i", x, y, unicode); 627 | 628 | GList *list, *l; 629 | list = kernagic_glyphs (); 630 | 631 | for (l = list; l->next; l = l->next) 632 | { 633 | Glyph *glyph = l->data; 634 | if (glyph->unicode >= unicode) 635 | { 636 | break; 637 | } 638 | } 639 | list = l; 640 | 641 | for (i = 0, l = list;l->prev && i < 1; i++, l = l->prev); 642 | for (i = 0; l && i < 3; i++, l = l->next) 643 | { 644 | Glyph *glyph = l->data; 645 | g_string_append_unichar (str, glyph->unicode); 646 | } 647 | 648 | gtk_entry_set_text (GTK_ENTRY (test_text), str->str); 649 | g_string_free (str, TRUE); 650 | return TRUE; 651 | } 652 | 653 | #ifdef GTK2 654 | gboolean 655 | index_draw_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data) 656 | { 657 | cairo_t *cr = gdk_cairo_create (widget->window); 658 | #else 659 | static gboolean 660 | index_draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data) 661 | { 662 | #endif 663 | memset (index_canvas, 0, INDEX_WIDTH * INDEX_HEIGHT); 664 | GList *list, *l; 665 | list = kernagic_glyphs (); 666 | 667 | for (l = list; l; l = l->next) 668 | { 669 | Glyph *glyph = l->data; 670 | int unicode = glyph->unicode; 671 | int x, y; 672 | d2xy (256, unicode, &x, &y); 673 | 674 | if (x <0) x = 0; 675 | if (y <0) y = 0; 676 | if (x >= INDEX_WIDTH) x= INDEX_WIDTH-1; 677 | if (y >= INDEX_HEIGHT) y= INDEX_WIDTH-1; 678 | index_canvas [y * INDEX_WIDTH + x] = 255; 679 | } 680 | 681 | cairo_save (cr); 682 | /* 0.93 is gtk default gray :d */ 683 | cairo_set_source_rgb (cr, 0.93,0.93,0.93); 684 | cairo_paint (cr); 685 | { 686 | cairo_surface_t *surface = 687 | cairo_image_surface_create_for_data (index_canvas, 688 | CAIRO_FORMAT_A8, INDEX_WIDTH, INDEX_HEIGHT, INDEX_WIDTH); 689 | cairo_set_source_rgb (cr, 0,0,0); 690 | cairo_mask_surface (cr, surface, 0, 0); 691 | cairo_surface_destroy (surface); 692 | } 693 | cairo_restore (cr); 694 | #ifdef GTK2 695 | cairo_destroy (cr); 696 | #endif 697 | return FALSE; 698 | } 699 | 700 | extern const char *ufo_path; 701 | 702 | static gboolean 703 | kernagic_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data) 704 | { 705 | switch (event->keyval) 706 | { 707 | case GDK_Page_Up: 708 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_ipsum_no), 709 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_ipsum_no)) - 1); 710 | return TRUE; 711 | case GDK_Page_Down: 712 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_ipsum_no), 713 | gtk_spin_button_get_value (GTK_SPIN_BUTTON (spin_ipsum_no)) + 1); 714 | return TRUE; 715 | case GDK_F1: 716 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), 0); break; 717 | case GDK_F2: 718 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), 1); break; 719 | case GDK_F3: 720 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), 2); break; 721 | case GDK_F4: 722 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), 3); break; 723 | case GDK_F5: 724 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), 4); break; 725 | case GDK_F6: 726 | gtk_combo_box_set_active (GTK_COMBO_BOX (spin_method), 5); break; 727 | case GDK_s: 728 | case GDK_S: 729 | if (event->state & GDK_CONTROL_MASK) 730 | { 731 | do_save (); 732 | return TRUE; 733 | } 734 | break; 735 | 736 | case GDK_i: 737 | case GDK_I: 738 | if (event->state & GDK_CONTROL_MASK) 739 | { 740 | trigger_ipsum (); 741 | return TRUE; 742 | } 743 | break; 744 | case GDK_q: 745 | case GDK_Q: 746 | if (event->state & GDK_CONTROL_MASK) 747 | { 748 | gtk_main_quit (); 749 | return TRUE; 750 | } 751 | break; 752 | default: 753 | break; 754 | } 755 | return FALSE; 756 | } 757 | 758 | int ui_gtk (int argc, char **argv) 759 | { 760 | GtkWidget *window; 761 | GtkWidget *hbox; 762 | GtkWidget *vbox2; 763 | GtkWidget *vbox1; 764 | 765 | GtkSizeGroup *labels; 766 | GtkSizeGroup *sliders; 767 | 768 | if (!index_canvas) 769 | index_canvas = g_malloc0 (INDEX_WIDTH * INDEX_HEIGHT); 770 | 771 | gtk_init (&argc, &argv); 772 | 773 | labels = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); 774 | sliders = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); 775 | 776 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 777 | g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); 778 | g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (kernagic_key_press), NULL); 779 | 780 | 781 | hbox = gtk_hbox_new (FALSE, 5); 782 | gtk_container_add (GTK_CONTAINER (window), hbox); 783 | vbox1 = gtk_vbox_new (FALSE, 5); 784 | gtk_box_pack_start (GTK_BOX (hbox), vbox1, FALSE, FALSE, 2); 785 | 786 | gtk_container_set_border_width (GTK_CONTAINER (vbox1), 6); 787 | 788 | preview = gtk_drawing_area_new (); 789 | gtk_widget_set_size_request (preview, canvas_width(), canvas_height()); 790 | 791 | vbox2 = gtk_vbox_new (FALSE, 5); 792 | gtk_box_pack_end (GTK_BOX (vbox2), preview, TRUE, TRUE, 2); 793 | gtk_container_add (GTK_CONTAINER (hbox), vbox2); 794 | 795 | #ifdef GTK2 796 | g_signal_connect (preview, "expose-event", G_CALLBACK (preview_draw_cb), NULL); 797 | #else 798 | g_signal_connect (preview, "draw", G_CALLBACK (preview_draw_cb), NULL); 799 | #endif 800 | 801 | g_signal_connect (preview, "button-press-event", G_CALLBACK (preview_press_cb), NULL); 802 | 803 | g_signal_connect (preview, "button-release-event", G_CALLBACK (preview_release_cb), NULL); 804 | g_signal_connect (preview, "motion-notify-event", G_CALLBACK (preview_motion_cb), NULL); 805 | gtk_widget_add_events (preview, GDK_BUTTON_PRESS_MASK); 806 | gtk_widget_add_events (preview, GDK_BUTTON_RELEASE_MASK); 807 | gtk_widget_add_events (preview, GDK_POINTER_MOTION_MASK); 808 | 809 | { 810 | GtkWidget *hbox = gtk_hbox_new (FALSE, 4); 811 | GtkWidget *hbox2 = gtk_hbox_new (FALSE, 4); 812 | GtkWidget *label = gtk_label_new ("Font"); 813 | gtk_box_pack_start (GTK_BOX (vbox1), hbox, FALSE, FALSE, 2); 814 | 815 | font_path = gtk_file_chooser_button_new ("font", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER); 816 | 817 | if (ufo_path) 818 | { 819 | gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (font_path), ufo_path); 820 | } 821 | gtk_size_group_add_widget (labels, label); 822 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0); 823 | 824 | gtk_size_group_add_widget (sliders, hbox2); 825 | gtk_container_add (GTK_CONTAINER (hbox), label); 826 | gtk_container_add (GTK_CONTAINER (hbox), hbox2); 827 | gtk_container_add (GTK_CONTAINER (hbox2), font_path); 828 | 829 | { 830 | GtkWidget *save_button = gtk_button_new_with_label ("Save"); 831 | gtk_widget_set_tooltip_text (save_button, "Ctrl + S"); 832 | gtk_box_pack_end (GTK_BOX (hbox2), save_button, TRUE, TRUE, 2); 833 | g_signal_connect (save_button, "clicked", G_CALLBACK (do_save), NULL); 834 | } 835 | 836 | } 837 | 838 | { 839 | GtkWidget *hbox = gtk_hbox_new (FALSE, 4); 840 | GtkWidget *label = gtk_label_new ("Ipsum file"); 841 | gtk_box_pack_start (GTK_BOX (vbox1), hbox, FALSE, FALSE, 2); 842 | GtkWidget *hbox2 = gtk_hbox_new (FALSE, 4); 843 | 844 | ipsum_path = gtk_file_chooser_button_new ("ipsum", GTK_FILE_CHOOSER_ACTION_OPEN); 845 | gtk_size_group_add_widget (labels, label); 846 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0); 847 | 848 | spin_ipsum_no = gtk_spin_button_new_with_range (1, 100, 1); 849 | gtk_widget_set_tooltip_text (spin_ipsum_no, "PgUp / PgDn"); 850 | gtk_box_pack_start (GTK_BOX (hbox2), ipsum_path, TRUE, TRUE, 2); 851 | gtk_box_pack_start (GTK_BOX (hbox2), spin_ipsum_no, FALSE, FALSE, 2); 852 | gtk_size_group_add_widget (sliders, hbox2); 853 | 854 | gtk_container_add (GTK_CONTAINER (hbox), label); 855 | gtk_container_add (GTK_CONTAINER (hbox), hbox2); 856 | } 857 | { 858 | GtkWidget *hbox = gtk_hbox_new (FALSE, 4); 859 | GtkWidget *hbox2 = gtk_hbox_new (FALSE, 4); 860 | GtkWidget *label = gtk_label_new ("Ipsum glyphs"); 861 | gtk_box_pack_start (GTK_BOX (vbox1), hbox, FALSE, FALSE, 2); 862 | ipsum_glyphs = gtk_entry_new (); 863 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0); 864 | gtk_size_group_add_widget (labels, label); 865 | gtk_size_group_add_widget (sliders, hbox2); 866 | gtk_container_add (GTK_CONTAINER (hbox), label); 867 | gtk_container_add (GTK_CONTAINER (hbox), hbox2); 868 | gtk_container_add (GTK_CONTAINER (hbox2), ipsum_glyphs); 869 | 870 | { 871 | GtkWidget *button = gtk_button_new_with_label ("Generate"); 872 | gtk_widget_set_tooltip_text (button, "Ctrl + I"); 873 | gtk_box_pack_end (GTK_BOX (hbox2), button, TRUE, TRUE, 2); 874 | g_signal_connect (button, "clicked", G_CALLBACK (trigger_ipsum), NULL); 875 | } 876 | 877 | } 878 | { 879 | toggle_measurement_lines_check = gtk_check_button_new_with_label ("Measurement lines"); 880 | gtk_box_pack_start (GTK_BOX (vbox1), toggle_measurement_lines_check, FALSE, FALSE, 2); 881 | } 882 | 883 | { 884 | //GtkWidget *label = gtk_label_new ("Text sample"); 885 | test_text = gtk_entry_new (); 886 | #if 0 887 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0); 888 | gtk_size_group_add_widget (labels, label); 889 | gtk_size_group_add_widget (sliders, test_text); 890 | gtk_box_pack_start (GTK_BOX (vbox1), label, FALSE, FALSE, 2); 891 | gtk_box_pack_start (GTK_BOX (vbox1), test_text, FALSE, FALSE, 2); 892 | gtk_container_add (GTK_CONTAINER (hbox), test_text); 893 | #endif 894 | gtk_box_pack_start (GTK_BOX (vbox2), test_text, FALSE, FALSE, 2); 895 | } 896 | { 897 | spin_method = gtk_combo_box_text_new (); 898 | gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (spin_method), 899 | 0, "Original (F1)"); 900 | gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (spin_method), 901 | 1, "Snap gap (F2)"); 902 | gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (spin_method), 903 | 3, "Left and right bearings 0 (F3)"); 904 | gtk_widget_set_tooltip_text (spin_method, "F1, F2, F3…"); 905 | gtk_box_pack_start (GTK_BOX (vbox1), spin_method, FALSE, FALSE, 2); 906 | } 907 | 908 | vbox_options_original = gtk_vbox_new (FALSE, 4); 909 | gtk_box_pack_start (GTK_BOX (vbox1), vbox_options_original, FALSE, FALSE, 2); 910 | 911 | vbox_options_gray = gtk_vbox_new (FALSE, 4); 912 | gtk_box_pack_start (GTK_BOX (vbox1), vbox_options_gray, FALSE, FALSE, 2); 913 | { 914 | GtkWidget *hbox = gtk_hbox_new (FALSE, 4); 915 | GtkWidget *label = gtk_label_new ("Min distance"); 916 | gtk_size_group_add_widget (labels, label); 917 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0); 918 | spin_min_dist = gtk_spin_button_new_with_range (0.00, 100.0, 1); 919 | gtk_size_group_add_widget (sliders, spin_min_dist); 920 | gtk_container_add (GTK_CONTAINER (vbox_options_gray), hbox); 921 | gtk_container_add (GTK_CONTAINER (hbox), label); 922 | gtk_container_add (GTK_CONTAINER (hbox), spin_min_dist); 923 | } 924 | { 925 | GtkWidget *hbox = gtk_hbox_new (FALSE, 4); 926 | GtkWidget *label = gtk_label_new ("Max distance"); 927 | gtk_size_group_add_widget (labels, label); 928 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0); 929 | spin_max_dist = gtk_spin_button_new_with_range (0.00, 100.0, 1); 930 | gtk_size_group_add_widget (sliders, spin_max_dist); 931 | gtk_container_add (GTK_CONTAINER (vbox_options_gray), hbox); 932 | gtk_container_add (GTK_CONTAINER (hbox), label); 933 | gtk_container_add (GTK_CONTAINER (hbox), spin_max_dist); 934 | } 935 | vbox_options_rythm = gtk_vbox_new (FALSE, 4); 936 | gtk_box_pack_start (GTK_BOX (vbox1), vbox_options_rythm, FALSE, FALSE, 2); 937 | 938 | { 939 | GtkObject *adj = gtk_adjustment_new (1.0, 1.0, 500.0, 0.01, 1.0, 0); 940 | spin_snap = gimp_spin_scale_new (GTK_ADJUSTMENT (adj), "Snap", 2); 941 | gtk_container_add (GTK_CONTAINER (vbox_options_rythm), spin_snap); 942 | } 943 | 944 | { 945 | GtkObject *adj = gtk_adjustment_new (0.1, 0.0, 1.0, 0.001, 1.0, 0); 946 | spin_gap = gimp_spin_scale_new (GTK_ADJUSTMENT (adj), "Gap", 2); 947 | gtk_container_add (GTK_CONTAINER (vbox_options_rythm), spin_gap); 948 | } 949 | 950 | { 951 | GtkObject *adj = gtk_adjustment_new (12.0, 0, 100.0, 1, 1, 0); 952 | spin_divisor = gimp_spin_scale_new (GTK_ADJUSTMENT (adj), "Divisor", 0); 953 | // gtk_container_add (GTK_CONTAINER (vbox_options_rythm), spin_divisor); 954 | } 955 | 956 | 957 | { 958 | GtkObject *adj = gtk_adjustment_new (1.0, 1.0, 4.0, 0.01, 0.1, 0); 959 | spin_big_glyph_scaling = gimp_spin_scale_new (GTK_ADJUSTMENT (adj), "Tall glyph gap scaling", 3); 960 | gtk_container_add (GTK_CONTAINER (vbox_options_rythm), spin_big_glyph_scaling); 961 | } 962 | 963 | 964 | { 965 | GtkObject *adj = gtk_adjustment_new (1.0, 0.0, 300.0, 0.1, 1.0, 0); 966 | spin_tracking = gimp_spin_scale_new (GTK_ADJUSTMENT (adj), "Scale bearings", 1); 967 | gtk_container_add (GTK_CONTAINER (vbox_options_original), spin_tracking); 968 | } 969 | 970 | { 971 | progress = gtk_progress_bar_new (); 972 | #if 0 973 | gtk_progress_bar_set_show_text (GTK_PROGRESS_BAR (progress), TRUE); 974 | #endif 975 | gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress), 0.112); 976 | gtk_box_pack_start (GTK_BOX (vbox1), progress, FALSE, FALSE, 2); 977 | } 978 | 979 | #if 1 980 | GWindex = gtk_drawing_area_new (); 981 | gtk_widget_set_size_request (GWindex, INDEX_WIDTH, INDEX_HEIGHT/2); 982 | gtk_box_pack_end (GTK_BOX (vbox1), GWindex, FALSE, FALSE, 2); 983 | 984 | //g_signal_connect (GWindex, "draw", G_CALLBACK (index_draw_cb), NULL); 985 | g_signal_connect (GWindex, "expose-event", G_CALLBACK (index_draw_cb), NULL); 986 | 987 | #ifdef GTK2 988 | g_signal_connect (GWindex, "expose-event", G_CALLBACK (index_draw_cb), NULL); 989 | #else 990 | g_signal_connect (GWindex, "draw", G_CALLBACK (index_draw_cb), NULL); 991 | #endif 992 | 993 | g_signal_connect (GWindex, "button-press-event", G_CALLBACK (index_press_cb), NULL); 994 | gtk_widget_add_events (GWindex, GDK_BUTTON_PRESS_MASK); 995 | #endif 996 | 997 | #if 0 998 | { 999 | GtkWidget *help = gtk_label_new ("Click a small ipsum word to replace the word being worked on,\nclicking below the baseline removes custom rythm points, within the x-height left and right rythm points are set, above the x-height single rythm point overrides are set.\n\nThe short lines are rythm point-lines, weak ones auto-detected and dark ones manual overrides."); 1000 | gtk_label_set_line_wrap (GTK_LABEL (help), TRUE); 1001 | gtk_misc_set_alignment (GTK_MISC (help), 0.0, 0.0); 1002 | gtk_box_pack_start (GTK_BOX (vbox1), help, FALSE, FALSE, 2); 1003 | } 1004 | #endif 1005 | 1006 | /************/ 1007 | 1008 | /* when these change, we need to reinitialize from scratch */ 1009 | g_signal_connect (ipsum_path, "file-set", G_CALLBACK (ipsum_reload), NULL); 1010 | /* and when these change, we should be able to do an incremental update */ 1011 | g_signal_connect (toggle_measurement_lines_check, "toggled", G_CALLBACK (trigger), NULL); 1012 | g_signal_connect (spin_method, "notify::active", G_CALLBACK (trigger), NULL); 1013 | 1014 | g_signal_connect (spin_method, "notify::active", G_CALLBACK (trigger_prop_show), NULL); 1015 | g_signal_connect (spin_ipsum_no, "notify::value", G_CALLBACK (ipsum_reload), NULL); 1016 | g_signal_connect (spin_tracking, "notify::value", G_CALLBACK (trigger), NULL); 1017 | g_signal_connect (spin_min_dist, "notify::value", G_CALLBACK (trigger), NULL); 1018 | g_signal_connect (spin_max_dist, "notify::value", G_CALLBACK (trigger), NULL); 1019 | g_signal_connect (spin_snap, "value-changed", G_CALLBACK (trigger_cadence), NULL); 1020 | g_signal_connect (spin_divisor, "value-changed", G_CALLBACK (trigger_divisor), NULL); 1021 | g_signal_connect (spin_gap, "value-changed", G_CALLBACK (trigger), NULL); 1022 | g_signal_connect (spin_big_glyph_scaling, "value-changed", G_CALLBACK (trigger), NULL); 1023 | g_signal_connect (test_text, "notify::text", G_CALLBACK (trigger), NULL); 1024 | g_signal_connect (test_text, "notify::cursor-position", G_CALLBACK (cursor_position_changed_cb), NULL); 1025 | 1026 | g_signal_connect (ipsum_glyphs, "notify::text", G_CALLBACK (trigger_ipsum), NULL); 1027 | 1028 | set_defaults_from_args (); 1029 | 1030 | gtk_widget_show_all (hbox); 1031 | gtk_widget_hide (progress); 1032 | gtk_widget_show (window); 1033 | trigger_prop_show (); 1034 | 1035 | if (getenv ("KERNAGIC_DEBUG")) 1036 | kerner_debug_ui (); 1037 | 1038 | trigger_reload (); 1039 | 1040 | ipsum = g_strdup ("foo"); 1041 | 1042 | gtk_main (); 1043 | return 0; 1044 | } 1045 | --------------------------------------------------------------------------------