diff --git a/font.c b/font.c index 0d0ad7e..99c1dd6 100644 --- a/font.c +++ b/font.c @@ -4,97 +4,118 @@ #include "fn.h" typedef struct Glyph Glyph; +typedef struct Font Font; struct Glyph { uchar *bmp; - int w, h, ox, oy; + int w, h, ox, oy, base; +}; + +struct Font +{ + stbtt_fontinfo info; + uchar *data; + float scale; + int base; }; enum { Maxfonts = 4 }; -static stbtt_fontinfo fonts[Maxfonts]; -static uchar *fontdata[Maxfonts]; -static float scale[Maxfonts]; +static Font fonts[Maxfonts]; static int nfonts; static Glyph cache[Nglyphs]; static u32int blendtab[2][256]; static u32int -blend(u32int bg, int a) +blend(u32int bg, u32int fg, int a) { int r, g, b, inv; inv = 255 - a; - r = (bg >> 16 & 0xff) * inv / 255; - g = (bg >> 8 & 0xff) * inv / 255; - b = (bg & 0xff) * inv / 255; + r = ((bg >> 16 & 0xff) * inv + (fg >> 16 & 0xff) * a) / 255; + g = ((bg >> 8 & 0xff) * inv + (fg >> 8 & 0xff) * a) / 255; + b = ((bg & 0xff) * inv + (fg & 0xff) * a) / 255; return (r << 16) | (g << 8) | b; } -static void +static int loadfont(char *path) { - int fd; + Font *f; + int ascent, descent, gap, fd; long sz, n; - if(nfonts >= Maxfonts) - die("too many fonts"); fd = open(path, OREAD); - if(fd < 0) - die("can't open font: %s", path); + if(fd < 0){ + fprint(2, "strans: popup: can't open font: %s\n", path); + return 0; + } sz = seek(fd, 0, 2); seek(fd, 0, 0); - fontdata[nfonts] = emalloc(sz); - n = readn(fd, fontdata[nfonts], sz); - close(fd); - if(n != sz) - die("can't read font: %s", path); - if(!stbtt_InitFont(&fonts[nfonts], fontdata[nfonts], stbtt_GetFontOffsetForIndex(fontdata[nfonts], 0))) - die("can't init font: %s", path); - scale[nfonts] = stbtt_ScaleForPixelHeight(&fonts[nfonts], Fontsz); - nfonts++; -} - -static int -isfont(char *name) -{ - char *p; - - p = strrchr(name, '.'); - if(p == nil) + if(sz <= 0){ + fprint(2, "strans: popup: empty font: %s\n", path); + close(fd); return 0; - return strcmp(p, ".ttf") == 0 || strcmp(p, ".otf") == 0; + } + f = &fonts[nfonts]; + f->data = emalloc(sz); + n = readn(fd, f->data, sz); + close(fd); + if(n != sz){ + fprint(2, "strans: popup: can't read font: %s\n", path); + free(f->data); + f->data = nil; + return 0; + } + if(!stbtt_InitFont(&f->info, f->data, + stbtt_GetFontOffsetForIndex(f->data, 0))){ + fprint(2, "strans: popup: can't init font: %s\n", path); + free(f->data); + f->data = nil; + return 0; + } + f->scale = stbtt_ScaleForPixelHeight(&f->info, Fontsz); + stbtt_GetFontVMetrics(&f->info, &ascent, &descent, &gap); + (void)descent; + (void)gap; + f->base = (int)(ascent * f->scale); + nfonts++; + return 1; } void fontinit(char *dir) { - int fd, n, i, a; - Dir *d; + static char *names[] = { + "NotoSans-Regular.ttf", + "NotoSansMonoCJKjp-Regular.otf", + "NotoEmoji-Regular.ttf", + }; + int i, a; char path[256]; - fd = open(dir, OREAD); - if(fd < 0) - die("can't open font dir: %s", dir); - n = dirreadall(fd, &d); - close(fd); - if(n < 0) - die("can't read font dir: %s", dir); - for(i = 0; i < n; i++){ - if(isfont(d[i].name)){ - snprint(path, sizeof path, "%s/%s", dir, d[i].name); - loadfont(path); + nfonts = 0; + memset(cache, 0, sizeof cache); + for(i = 0; i < nelem(names); i++){ + if(strlen(dir) + 1 + strlen(names[i]) + 1 > sizeof path){ + fprint(2, "strans: popup: font path is too long\n"); + continue; } + snprint(path, sizeof path, "%s/%s", dir, names[i]); + loadfont(path); } - free(d); - if(nfonts == 0) - die("no fonts in %s", dir); for(a = 0; a < 256; a++){ - blendtab[0][a] = blend(Colbg, a); - blendtab[1][a] = blend(Colsel, a); + blendtab[0][a] = blend(Colbg, Colfg, a); + blendtab[1][a] = blend(Colsel, Colfg, a); } } +int +fontready(void) +{ + return nfonts != 0; +} + void putfont(u32int *buf, int w, int h, int px, int py, Rune r) { @@ -108,17 +129,21 @@ putfont(u32int *buf, int w, int h, int px, int py, Rune r) g = &cache[r]; if(g->bmp == nil){ for(f = 0; f < nfonts; f++){ - if(stbtt_FindGlyphIndex(&fonts[f], r) == 0) + if(stbtt_FindGlyphIndex(&fonts[f].info, r) == 0) continue; - g->bmp = stbtt_GetCodepointBitmap(&fonts[f], scale[f], scale[f], r, &g->w, &g->h, &g->ox, &g->oy); - if(g->bmp != nil) + g->bmp = stbtt_GetCodepointBitmap(&fonts[f].info, + fonts[f].scale, fonts[f].scale, r, + &g->w, &g->h, &g->ox, &g->oy); + if(g->bmp != nil){ + g->base = fonts[f].base; break; + } } if(g->bmp == nil) return; } - y0 = py + g->oy + Fontsz - Fontbase; + y0 = py + g->base + g->oy; j0 = y0 < 0 ? -y0 : 0; j1 = y0 + g->h > h ? h - y0 : g->h; x0 = px + g->ox; diff --git a/font/Apache-2.0-NotoEmoji.txt b/font/Apache-2.0-NotoEmoji.txt new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/font/Apache-2.0-NotoEmoji.txt @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/font/NotoSansMonoCJKsc-Regular.otf b/font/NotoSansMonoCJKjp-Regular.otf similarity index 97% rename from font/NotoSansMonoCJKsc-Regular.otf rename to font/NotoSansMonoCJKjp-Regular.otf index 5ff7011..25927d8 100644 Binary files a/font/NotoSansMonoCJKsc-Regular.otf and b/font/NotoSansMonoCJKjp-Regular.otf differ diff --git a/font/OFL-NotoSans.txt b/font/OFL-NotoSans.txt new file mode 100644 index 0000000..c82d72e --- /dev/null +++ b/font/OFL-NotoSans.txt @@ -0,0 +1,94 @@ +Copyright 2018 The Noto Project Authors (github.com/googlei18n/noto-fonts) + +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font +creation efforts of academic and linguistic communities, and to +provide a free and open framework in which fonts may be shared and +improved in partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply to +any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software +components as distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, +deleting, or substituting -- in part or in whole -- any of the +components of the Original Version, by changing formats or by porting +the Font Software to a new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, +modify, redistribute, and sell modified and unmodified copies of the +Font Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, in +Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the +corresponding Copyright Holder. This restriction only applies to the +primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created using +the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/font/OFL-NotoSansCJK.txt b/font/OFL-NotoSansCJK.txt new file mode 100644 index 0000000..d952d62 --- /dev/null +++ b/font/OFL-NotoSansCJK.txt @@ -0,0 +1,92 @@ +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font +creation efforts of academic and linguistic communities, and to +provide a free and open framework in which fonts may be shared and +improved in partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply to +any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software +components as distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, +deleting, or substituting -- in part or in whole -- any of the +components of the Original Version, by changing formats or by porting +the Font Software to a new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, +modify, redistribute, and sell modified and unmodified copies of the +Font Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, in +Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the +corresponding Copyright Holder. This restriction only applies to the +primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created using +the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/font/PROVENANCE b/font/PROVENANCE new file mode 100644 index 0000000..d1ebda1 --- /dev/null +++ b/font/PROVENANCE @@ -0,0 +1,37 @@ +Bundled popup fonts +=================== + +The renderer tries these files in this fixed order: + +1. NotoSans-Regular.ttf +2. NotoSansMonoCJKjp-Regular.otf +3. NotoEmoji-Regular.ttf + +NotoSans-Regular.ttf +-------------------- + +SHA-256: 9cb49a54e520423033f9727be2e53e4805a60656deb09c219740d8e5f3e033ac +Internal family/version: Noto Sans Regular, version 2.005 +License: SIL Open Font License 1.1 (OFL-NotoSans.txt) + +This file entered the strans repository in commit 874b941. That commit did +not record its download URL, and the current upstream build is not +byte-identical. The checksum above identifies the bundled file; no claim is +made that the current upstream URL regenerates it. + +NotoSansMonoCJKjp-Regular.otf +-------------------------------- + +SHA-256: 4d01725be822d144cf9a56ade981e6fb920cd7a610b8fc24cc601a920beea5b9 +Release: Noto Sans CJK 2.004, tag Sans2.004 +Source: https://raw.githubusercontent.com/notofonts/noto-cjk/Sans2.004/Sans/Mono/NotoSansMonoCJKjp-Regular.otf +License: SIL Open Font License 1.1 (OFL-NotoSansCJK.txt) +License source: https://raw.githubusercontent.com/notofonts/noto-cjk/Sans2.004/LICENSE + +NotoEmoji-Regular.ttf +--------------------- + +SHA-256: 415dc6290378574135b64c808dc640c1df7531973290c4970c51fdeb849cb0c5 +Source: https://raw.githubusercontent.com/googlei18n/noto-emoji/2f1ffdd6fbbd05d6f382138a3d3adcd89c5ce800/fonts/NotoEmoji-Regular.ttf +License: Apache License 2.0 (Apache-2.0-NotoEmoji.txt) +License source: https://raw.githubusercontent.com/googlei18n/noto-emoji/2f1ffdd6fbbd05d6f382138a3d3adcd89c5ce800/LICENSE diff --git a/popup_layout.c b/popup_layout.c new file mode 100644 index 0000000..7c85bdf --- /dev/null +++ b/popup_layout.c @@ -0,0 +1,29 @@ +#include "dat.h" +#include "popup_layout.h" + +int +popupcells(Rune *r, int n) +{ + int i, w; + + w = 0; + for(i = 0; i < n; i++) + if(r[i] != 0xfe0e && r[i] != 0xfe0f) + w++; + return w; +} + +void +popupposition(Caret *caret, int pointerx, int pointery, int screenw, + int screenh, int w, int h, int *x, int *y) +{ + if(caret->valid){ + *x = caret->x; + *y = caret->y + max(caret->h, 0); + }else{ + *x = pointerx + 10; + *y = pointery + 10; + } + *x = max(0, min(*x, screenw - w)); + *y = max(0, min(*y, screenh - h)); +} diff --git a/popup_layout.h b/popup_layout.h new file mode 100644 index 0000000..c41e660 --- /dev/null +++ b/popup_layout.h @@ -0,0 +1,7 @@ +#ifndef STRANS_POPUP_LAYOUT_H +#define STRANS_POPUP_LAYOUT_H + +int popupcells(Rune*, int); +void popupposition(Caret*, int, int, int, int, int, int, int*, int*); + +#endif diff --git a/tests/Makefile b/tests/Makefile index ae42f9e..873cbb4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -5,9 +5,11 @@ LIBS = -lthread -lbio PROG = unit_test TESTSRC = unit_test.c test_util.c str_test.c hash_test.c trie_test.c \ - ko_test.c vi_test.c engine_test.c dict_test.c ipc_test.c wayland_test.c + ko_test.c vi_test.c engine_test.c dict_test.c ipc_test.c wayland_test.c \ + popup_test.c TESTOBJ = $(TESTSRC:.c=.o) -PARENTSRC = str.c hash.c trie.c dict.c ko.c vi.c ipc.c wayland_state.c +PARENTSRC = str.c hash.c trie.c dict.c ko.c vi.c ipc.c wayland_state.c \ + popup_layout.c PARENTOBJ = $(PARENTSRC:%.c=unit_%.o) OBJS = $(TESTOBJ) $(PARENTOBJ) diff --git a/tests/popup_test.c b/tests/popup_test.c new file mode 100644 index 0000000..442d87d --- /dev/null +++ b/tests/popup_test.c @@ -0,0 +1,28 @@ +#include "test.h" +#include "../popup_layout.h" + +void +popup_layout(struct ct *t) +{ + Rune heart[] = { 0x2764, 0xfe0f }; + Caret caret; + int x, y; + + CT_EQ_INT(t, 1, popupcells(heart, nelem(heart))); + memset(&caret, 0, sizeof caret); + popupposition(&caret, 40, 50, 800, 600, 100, 60, &x, &y); + CT_EQ_INT(t, 50, x); + CT_EQ_INT(t, 60, y); + caret.valid = 1; + caret.x = 70; + caret.y = 80; + caret.h = 20; + popupposition(&caret, 40, 50, 800, 600, 100, 60, &x, &y); + CT_EQ_INT(t, 70, x); + CT_EQ_INT(t, 100, y); + caret.x = 790; + caret.y = 590; + popupposition(&caret, 0, 0, 800, 600, 100, 60, &x, &y); + CT_EQ_INT(t, 700, x); + CT_EQ_INT(t, 540, y); +} diff --git a/tests/test.h b/tests/test.h index 6a103a2..756c6cc 100644 --- a/tests/test.h +++ b/tests/test.h @@ -26,6 +26,7 @@ void trie_optional_outputs_and_invalid_lengths(struct ct*); void wayland_press_release_state(struct ct*); void wayland_repeat_state(struct ct*); void wayland_repeat_cancellation(struct ct*); +void popup_layout(struct ct*); void production_maps_load(struct ct*); void transmap_states(struct ct*); void korean_sequences(struct ct*); diff --git a/tests/unit_test.c b/tests/unit_test.c index c4846e9..6a87032 100644 --- a/tests/unit_test.c +++ b/tests/unit_test.c @@ -75,6 +75,7 @@ static const struct ct_test tests[] = { { "wayland/press-release", wayland_press_release_state }, { "wayland/repeat", wayland_repeat_state }, { "wayland/repeat-cancellation", wayland_repeat_cancellation }, + { "popup/layout", popup_layout }, { "map/production-lifecycle", production_maps_load }, { "transmap/states", transmap_states }, { "hangul/sequences", korean_sequences }, diff --git a/win.c b/win.c index 802f8e3..79d6a1c 100644 --- a/win.c +++ b/win.c @@ -1,6 +1,7 @@ #include #include "dat.h" #include "fn.h" +#include "popup_layout.h" enum { Asciitofull = 0xFEE0, @@ -12,9 +13,12 @@ static xcb_connection_t *conn; static xcb_screen_t *scr; static xcb_window_t win; static xcb_gcontext_t gc; +static xcb_pixmap_t pix; static u32int *img; static int depth; +extern int fontready(void); + static xcb_screen_t* getscr(xcb_connection_t *c, int n) { @@ -26,7 +30,70 @@ getscr(xcb_connection_t *c, int n) return nil; } +static xcb_visualtype_t* +getvisual(xcb_screen_t *s) +{ + xcb_depth_iterator_t di; + xcb_visualtype_iterator_t vi; + + for(di = xcb_screen_allowed_depths_iterator(s); di.rem; + xcb_depth_next(&di)){ + if(di.data->depth != s->root_depth) + continue; + for(vi = xcb_depth_visuals_iterator(di.data); vi.rem; + xcb_visualtype_next(&vi)) + if(vi.data->visual_id == s->root_visual) + return vi.data; + } + return nil; +} + +static int +validformat(xcb_connection_t *c, xcb_screen_t *s) +{ + const xcb_setup_t *setup; + xcb_format_iterator_t fi; + xcb_visualtype_t *v; + u32int one; + int lsb; + + setup = xcb_get_setup(c); + v = getvisual(s); + if(setup == nil || v == nil || s->root_depth != 24 || + v->_class != XCB_VISUAL_CLASS_TRUE_COLOR || + v->bits_per_rgb_value != 8 || + v->red_mask != 0xff0000 || v->green_mask != 0x00ff00 || + v->blue_mask != 0x0000ff) + return 0; + one = 1; + lsb = *(uchar*)&one != 0; + if((lsb && setup->image_byte_order != XCB_IMAGE_ORDER_LSB_FIRST) || + (!lsb && setup->image_byte_order != XCB_IMAGE_ORDER_MSB_FIRST)) + return 0; + for(fi = xcb_setup_pixmap_formats_iterator(setup); fi.rem; + xcb_format_next(&fi)) + if(fi.data->depth == 24 && fi.data->bits_per_pixel == 32 && + fi.data->scanline_pad == 32) + return 1; + return 0; +} + static void +wincleanup(void) +{ + free(img); + img = nil; + if(conn != nil){ + xcb_disconnect(conn); + conn = nil; + } + scr = nil; + win = 0; + gc = 0; + pix = 0; +} + +static int wininit(void) { int n; @@ -35,11 +102,17 @@ wininit(void) xcb_intern_atom_reply_t *r1, *r2; conn = xcb_connect(nil, &n); - if(conn == nil || xcb_connection_has_error(conn)) - die("xcb_connect"); + if(conn == nil || xcb_connection_has_error(conn)){ + fprint(2, "strans: popup disabled: cannot connect to X display\n"); + wincleanup(); + return 0; + } scr = getscr(conn, n); - if(scr == nil) - die("no screen"); + if(scr == nil || !validformat(conn, scr)){ + fprint(2, "strans: popup disabled: unsupported X root format\n"); + wincleanup(); + return 0; + } depth = scr->root_depth; win = xcb_generate_id(conn); mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | @@ -65,16 +138,29 @@ wininit(void) free(r2); gc = xcb_generate_id(conn); xcb_create_gc(conn, gc, win, 0, nil); + pix = xcb_generate_id(conn); + xcb_create_pixmap(conn, depth, pix, win, Imgw, Imgh); + mask = XCB_CW_BACK_PIXMAP; + xcb_change_window_attributes(conn, win, mask, &pix); img = emalloc(Imgw * Imgh * sizeof(img[0])); fontinit(fontdir); + if(!fontready()){ + fprint(2, "strans: popup disabled: no usable fonts\n"); + wincleanup(); + return 0; + } + return 1; } static void drawstr(u32int *buf, int x, int y, Rune *r, int n, int maxw, int maxh) { while(n-- > 0){ - putfont(buf, maxw, maxh, x, y, *r++); - x += Fontsz; + if(*r != 0xfe0e && *r != 0xfe0f){ + putfont(buf, maxw, maxh, x, y, *r); + x += Fontsz; + } + r++; } } @@ -111,19 +197,20 @@ drawkouho(Drawcmd *dc, int first, int n, int w, int h) static void putimage(int w, int h) { - xcb_put_image(conn, XCB_IMAGE_FORMAT_Z_PIXMAP, win, gc, + xcb_put_image(conn, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc, w, h, 0, 0, 0, depth, w * h * 4, (u8int*)img); - xcb_flush(conn); + /* The retained background pixmap lets the server repaint exposures. */ + xcb_clear_area(conn, 0, win, 0, 0, w, h); } -static void +static int winhide(void) { xcb_unmap_window(conn, win); - xcb_flush(conn); + return xcb_flush(conn) > 0; } -static void +static int winshow(Drawcmd *dc) { int npre, px, py, w, h, i, n, maxw; @@ -134,23 +221,27 @@ winshow(Drawcmd *dc) n = min(max(dc->nkouho, 0), Maxdisp); npre = dc->pre.n != 0; if(n == 0 && npre == 0){ - winhide(); - return; + return winhide(); } - cookie = xcb_query_pointer(conn, scr->root); - maxw = dc->pre.n; + maxw = popupcells(dc->pre.r, dc->pre.n); for(i = 0; i < n; i++) - maxw = max(maxw, dc->kouho[i].n); - ptr = xcb_query_pointer_reply(conn, cookie, nil); - if(ptr == nil) - die("xcb_query_pointer"); - px = ptr->root_x + 10; - py = ptr->root_y + 10; - free(ptr); + maxw = max(maxw, popupcells(dc->kouho[i].r, dc->kouho[i].n)); vals[3] = h = (n + npre) * Fontsz; vals[2] = w = (maxw + 3) * Fontsz; - vals[1] = py = max(0, min(py, scr->height_in_pixels - h)); - vals[0] = px = max(0, min(px, scr->width_in_pixels - w)); + px = py = 0; + if(!dc->caret.valid){ + cookie = xcb_query_pointer(conn, scr->root); + ptr = xcb_query_pointer_reply(conn, cookie, nil); + if(ptr == nil) + return 0; + px = ptr->root_x; + py = ptr->root_y; + free(ptr); + } + popupposition(&dc->caret, px, py, scr->width_in_pixels, + scr->height_in_pixels, w, h, &px, &py); + vals[1] = py; + vals[0] = px; xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, @@ -158,6 +249,7 @@ winshow(Drawcmd *dc) xcb_map_window(conn, win); drawkouho(dc, 0, n, w, h); putimage(w, h); + return xcb_flush(conn) > 0; } void @@ -166,13 +258,16 @@ drawthread(void*) Drawcmd dc; threadsetname("draw"); - wininit(); + if(!wininit()) + return; while(chanrecv(drawc, &dc) > 0){ while(channbrecv(drawc, &dc) > 0) ; - if(dc.nkouho == 0 && dc.pre.n == 0) - winhide(); - else - winshow(&dc); + if(dc.nkouho == 0 && dc.pre.n == 0){ + if(!winhide()) + break; + }else if(!winshow(&dc)) + break; } + wincleanup(); }