Debian Codegraph @

fontforgeexe/histograms.cpp

raw symbols SCIP
1 /* Copyright (C) 2003-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <math.h>
29 #include <algorithm>
30
31 #include "fontforge.h"
32 #include "autohint.h"
33 #include "dumppfa.h"
34 #include "splineutil.h"
35
36 #include "gtk/show_histogram_shim.hpp"
37
38 /* This operations are designed to work on a single font. NOT a CID collection*/
39 /* A CID collection must be treated one sub-font at a time */
40
41 using HistogramMap = std::map<int, ff::dlg::HistogramBarRecord>;
42
43 static HistogramMap HistFindBlues(SplineFont* sf, int layer, uint8_t* selected,
44 EncMap* map) {
45 HistogramMap blues_map;
46
47 for (int i = 0; i < (selected == NULL ? sf->glyphcnt : map->enccount);
48 ++i) {
49 int top, bottom;
50 SplineChar* sc;
51 DBounds b;
52
53 int gid = selected == NULL ? i : map->map[i];
54 if (gid != -1 && (sc = sf->glyphs[gid]) != NULL &&
55 sc->layers[ly_fore].splines != NULL &&
56 sc->layers[ly_fore].refs == NULL &&
57 (selected == NULL || selected[i])) {
58 SplineCharLayerFindBounds(sc, layer, &b);
59 bottom = rint(b.miny);
60 top = rint(b.maxy);
61 if (top == bottom) continue;
62
63 blues_map[top].count++;
64 blues_map[top].glyph_names.push_back(sc->name);
65
66 blues_map[bottom].count++;
67 blues_map[bottom].glyph_names.push_back(sc->name);
68 }
69 }
70 return blues_map;
71 }
72
73 static HistogramMap HistFindStemWidths(SplineFont* sf, int layer,
74 uint8_t* selected, EncMap* map,
75 int hor) {
76 HistogramMap stems_map;
77
78 for (int i = 0; i < (selected == NULL ? sf->glyphcnt : map->enccount);
79 ++i) {
80 int gid = selected == NULL ? i : map->map[i];
81 SplineChar* sc;
82 if (gid != -1 && (sc = sf->glyphs[gid]) != NULL &&
83 sc->layers[ly_fore].splines != NULL &&
84 sc->layers[ly_fore].refs == NULL &&
85 (selected == NULL || selected[i])) {
86 if (autohint_before_generate && sc->changedsincelasthinted &&
87 !sc->manualhints)
88 SplineCharAutoHint(sc, layer, NULL);
89 for (StemInfo* stem = hor ? sc->hstem : sc->vstem; stem != NULL;
90 stem = stem->next) {
91 if (stem->ghost) continue;
92 int val = rint(stem->width);
93 if (val <= 0) val = -val;
94
95 stems_map[val].count++;
96 stems_map[val].glyph_names.push_back(sc->name);
97 }
98 }
99 }
100 return stems_map;
101 }
102
103 static HistogramMap HistFindHStemWidths(SplineFont* sf, int layer,
104 uint8_t* selected, EncMap* map) {
105 return HistFindStemWidths(sf, layer, selected, map, true);
106 }
107
108 static HistogramMap HistFindVStemWidths(SplineFont* sf, int layer,
109 uint8_t* selected, EncMap* map) {
110 return HistFindStemWidths(sf, layer, selected, map, false);
111 }
112
113 static void HistSet(SplineFont* sf, struct psdict* private_dict,
114 const ff::dlg::UiStrings& ui_strings,
115 const ff::dlg::PrivateDictValues& result) {
116 if ((result.primary.empty() || result.primary == "[]") &&
117 (result.secondary.empty() || result.secondary == "[]") &&
118 private_dict == NULL)
119 return;
120
121 if (private_dict == NULL) {
122 sf->private_dict = private_dict =
123 (psdict*)calloc(1, sizeof(struct psdict));
124 private_dict->cnt = 10;
125 private_dict->keys = (char**)calloc(10, sizeof(char*));
126 private_dict->values = (char**)calloc(10, sizeof(char*));
127 }
128 PSDictChangeEntry(private_dict, ui_strings.primary_label.c_str(),
129 result.primary.c_str());
130 PSDictChangeEntry(private_dict, ui_strings.secondary_label.c_str(),
131 result.secondary.c_str());
132 }
133
134 static bool CheckSmallSelection(uint8_t* selected, EncMap* map,
135 SplineFont* sf) {
136 int cnt, tot;
137
138 if (selected == NULL)
139 // All glyphs are considered selected, so no "small selection" warning.
140 return (false);
141
142 for (int i = cnt = tot = 0; i < map->enccount; ++i) {
143 int gid = map->map[i];
144 if (gid != -1 && sf->glyphs[gid] != NULL) {
145 ++tot;
146 if (selected[i]) ++cnt;
147 }
148 }
149 return ((cnt == 1 && tot > 1) || (cnt < 8 && tot > 30));
150 }
151
152 void SFHistogram(GWindow parent, SplineFont* sf, int layer,
153 struct psdict* private_dict, uint8_t* selected, EncMap* map,
154 enum hist_type which) {
155 int j, upper_bound;
156 HistogramMap values_map;
157 ff::dlg::HistogramData dlg_data;
158
159 if (private_dict == NULL) private_dict = sf->private_dict;
160 switch (which) {
161 case hist_hstem:
162 values_map = HistFindHStemWidths(sf, layer, selected, map);
163 break;
164 case hist_vstem:
165 values_map = HistFindVStemWidths(sf, layer, selected, map);
166 break;
167 case hist_blues:
168 values_map = HistFindBlues(sf, layer, selected, map);
169 break;
170 }
171
172 if (values_map.empty()) { /* Found nothing */
173 dlg_data.lower_bound = upper_bound = 0;
174 } else {
175 dlg_data.lower_bound = values_map.begin()->first;
176 upper_bound = values_map.rbegin()->first;
177 }
178
179 // Convert map to array for use in the histogram. Array of bars is
180 // initialized with zeros.
181 dlg_data.bars.resize(upper_bound - dlg_data.lower_bound + 1);
182 for (auto& [key, value] : values_map) {
183 // Purge duplicate glyph names from glyph list.
184 auto last =
185 std::unique(value.glyph_names.begin(), value.glyph_names.end());
186 value.glyph_names.erase(last, value.glyph_names.end());
187
188 dlg_data.bars[key - dlg_data.lower_bound] = value;
189 }
190
191 dlg_data.type = which;
192 dlg_data.small_selection_warning = CheckSmallSelection(selected, map, sf);
193
194 const ff::dlg::UiStrings& ui_strings =
195 ff::dlg::kHistogramUiStrings.at(dlg_data.type);
196
197 if ((j = PSDictFindEntry(private_dict, ui_strings.primary_label.c_str())) !=
198 -1)
199 dlg_data.initial_values.primary = private_dict->values[j];
200 if ((j = PSDictFindEntry(private_dict,
201 ui_strings.secondary_label.c_str())) != -1)
202 dlg_data.initial_values.secondary = private_dict->values[j];
203
204 std::optional<ff::dlg::PrivateDictValues> result =
205 ff::dlg::show_histogram_dialog(parent, dlg_data);
206
207 if (result.has_value())
208 HistSet(sf, private_dict, ui_strings, result.value());
209 }