Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* GRAPHITE2 LICENSING |
michael@0 | 2 | |
michael@0 | 3 | Copyright 2010, SIL International |
michael@0 | 4 | All rights reserved. |
michael@0 | 5 | |
michael@0 | 6 | This library is free software; you can redistribute it and/or modify |
michael@0 | 7 | it under the terms of the GNU Lesser General Public License as published |
michael@0 | 8 | by the Free Software Foundation; either version 2.1 of License, or |
michael@0 | 9 | (at your option) any later version. |
michael@0 | 10 | |
michael@0 | 11 | This program is distributed in the hope that it will be useful, |
michael@0 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
michael@0 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
michael@0 | 14 | Lesser General Public License for more details. |
michael@0 | 15 | |
michael@0 | 16 | You should also have received a copy of the GNU Lesser General Public |
michael@0 | 17 | License along with this library in the file named "LICENSE". |
michael@0 | 18 | If not, write to the Free Software Foundation, 51 Franklin Street, |
michael@0 | 19 | Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
michael@0 | 20 | internet at http://www.fsf.org/licenses/lgpl.html. |
michael@0 | 21 | |
michael@0 | 22 | Alternatively, the contents of this file may be used under the terms of the |
michael@0 | 23 | Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
michael@0 | 24 | License, as published by the Free Software Foundation, either version 2 |
michael@0 | 25 | of the License or (at your option) any later version. |
michael@0 | 26 | */ |
michael@0 | 27 | #include <stdio.h> |
michael@0 | 28 | |
michael@0 | 29 | #include "graphite2/Log.h" |
michael@0 | 30 | #include "inc/debug.h" |
michael@0 | 31 | #include "inc/CharInfo.h" |
michael@0 | 32 | #include "inc/Slot.h" |
michael@0 | 33 | #include "inc/Segment.h" |
michael@0 | 34 | #include "inc/json.h" |
michael@0 | 35 | |
michael@0 | 36 | #if defined _WIN32 |
michael@0 | 37 | #include "windows.h" |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | using namespace graphite2; |
michael@0 | 41 | |
michael@0 | 42 | #if !defined GRAPHITE2_NTRACING |
michael@0 | 43 | json *global_log = NULL; |
michael@0 | 44 | #endif |
michael@0 | 45 | |
michael@0 | 46 | extern "C" { |
michael@0 | 47 | |
michael@0 | 48 | bool gr_start_logging(GR_MAYBE_UNUSED gr_face * face, const char *log_path) |
michael@0 | 49 | { |
michael@0 | 50 | if (!log_path) return false; |
michael@0 | 51 | |
michael@0 | 52 | #if !defined GRAPHITE2_NTRACING |
michael@0 | 53 | gr_stop_logging(face); |
michael@0 | 54 | #if defined _WIN32 |
michael@0 | 55 | int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, log_path, -1, 0, 0); |
michael@0 | 56 | if (n == 0 || n > MAX_PATH - 12) return false; |
michael@0 | 57 | |
michael@0 | 58 | LPWSTR wlog_path = gralloc<WCHAR>(n); |
michael@0 | 59 | if (!wlog_path) return false; |
michael@0 | 60 | FILE *log = 0; |
michael@0 | 61 | if (wlog_path && MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, log_path, -1, wlog_path, n)) |
michael@0 | 62 | log = _wfopen(wlog_path, L"wt"); |
michael@0 | 63 | |
michael@0 | 64 | free(wlog_path); |
michael@0 | 65 | #else // _WIN32 |
michael@0 | 66 | FILE *log = fopen(log_path, "wt"); |
michael@0 | 67 | #endif // _WIN32 |
michael@0 | 68 | if (!log) return false; |
michael@0 | 69 | |
michael@0 | 70 | if (face) |
michael@0 | 71 | { |
michael@0 | 72 | face->setLogger(log); |
michael@0 | 73 | if (!face->logger()) return false; |
michael@0 | 74 | |
michael@0 | 75 | *face->logger() << json::array; |
michael@0 | 76 | #ifdef GRAPHITE2_TELEMETRY |
michael@0 | 77 | *face->logger() << face->tele; |
michael@0 | 78 | #endif |
michael@0 | 79 | } |
michael@0 | 80 | else |
michael@0 | 81 | { |
michael@0 | 82 | global_log = new json(log); |
michael@0 | 83 | *global_log << json::array; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | return true; |
michael@0 | 87 | #else // GRAPHITE2_NTRACING |
michael@0 | 88 | return false; |
michael@0 | 89 | #endif // GRAPHITE2_NTRACING |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | bool graphite_start_logging(FILE * /* log */, GrLogMask /* mask */) |
michael@0 | 93 | { |
michael@0 | 94 | //#if !defined GRAPHITE2_NTRACING |
michael@0 | 95 | // graphite_stop_logging(); |
michael@0 | 96 | // |
michael@0 | 97 | // if (!log) return false; |
michael@0 | 98 | // |
michael@0 | 99 | // dbgout = new json(log); |
michael@0 | 100 | // if (!dbgout) return false; |
michael@0 | 101 | // |
michael@0 | 102 | // *dbgout << json::array; |
michael@0 | 103 | // return true; |
michael@0 | 104 | //#else |
michael@0 | 105 | return false; |
michael@0 | 106 | //#endif |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | void gr_stop_logging(GR_MAYBE_UNUSED gr_face * face) |
michael@0 | 110 | { |
michael@0 | 111 | #if !defined GRAPHITE2_NTRACING |
michael@0 | 112 | if (face && face->logger()) |
michael@0 | 113 | { |
michael@0 | 114 | FILE * log = face->logger()->stream(); |
michael@0 | 115 | face->setLogger(0); |
michael@0 | 116 | fclose(log); |
michael@0 | 117 | } |
michael@0 | 118 | else if (!face && global_log) |
michael@0 | 119 | { |
michael@0 | 120 | FILE * log = global_log->stream(); |
michael@0 | 121 | delete global_log; |
michael@0 | 122 | fclose(log); |
michael@0 | 123 | } |
michael@0 | 124 | #endif |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | void graphite_stop_logging() |
michael@0 | 128 | { |
michael@0 | 129 | // if (dbgout) delete dbgout; |
michael@0 | 130 | // dbgout = 0; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | } // extern "C" |
michael@0 | 134 | |
michael@0 | 135 | #ifdef GRAPHITE2_TELEMETRY |
michael@0 | 136 | size_t * graphite2::telemetry::_category = 0UL; |
michael@0 | 137 | #endif |
michael@0 | 138 | |
michael@0 | 139 | #if !defined GRAPHITE2_NTRACING |
michael@0 | 140 | |
michael@0 | 141 | #ifdef GRAPHITE2_TELEMETRY |
michael@0 | 142 | |
michael@0 | 143 | json & graphite2::operator << (json & j, const telemetry & t) throw() |
michael@0 | 144 | { |
michael@0 | 145 | j << json::object |
michael@0 | 146 | << "type" << "telemetry" |
michael@0 | 147 | << "silf" << t.silf |
michael@0 | 148 | << "states" << t.states |
michael@0 | 149 | << "starts" << t.starts |
michael@0 | 150 | << "transitions" << t.transitions |
michael@0 | 151 | << "glyphs" << t.glyph |
michael@0 | 152 | << "code" << t.code |
michael@0 | 153 | << "misc" << t.misc |
michael@0 | 154 | << "total" << (t.silf + t.states + t.starts + t.transitions + t.glyph + t.code + t.misc) |
michael@0 | 155 | << json::close; |
michael@0 | 156 | return j; |
michael@0 | 157 | } |
michael@0 | 158 | #else |
michael@0 | 159 | json & graphite2::operator << (json & j, const telemetry &) throw() |
michael@0 | 160 | { |
michael@0 | 161 | return j; |
michael@0 | 162 | } |
michael@0 | 163 | #endif |
michael@0 | 164 | |
michael@0 | 165 | |
michael@0 | 166 | json & graphite2::operator << (json & j, const CharInfo & ci) throw() |
michael@0 | 167 | { |
michael@0 | 168 | return j << json::object |
michael@0 | 169 | << "offset" << ci.base() |
michael@0 | 170 | << "unicode" << ci.unicodeChar() |
michael@0 | 171 | << "break" << ci.breakWeight() |
michael@0 | 172 | << "flags" << ci.flags() |
michael@0 | 173 | << "slot" << json::flat << json::object |
michael@0 | 174 | << "before" << ci.before() |
michael@0 | 175 | << "after" << ci.after() |
michael@0 | 176 | << json::close |
michael@0 | 177 | << json::close; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | |
michael@0 | 181 | json & graphite2::operator << (json & j, const dslot & ds) throw() |
michael@0 | 182 | { |
michael@0 | 183 | assert(ds.first); |
michael@0 | 184 | assert(ds.second); |
michael@0 | 185 | const Segment & seg = *ds.first; |
michael@0 | 186 | const Slot & s = *ds.second; |
michael@0 | 187 | |
michael@0 | 188 | j << json::object |
michael@0 | 189 | << "id" << objectid(ds) |
michael@0 | 190 | << "gid" << s.gid() |
michael@0 | 191 | << "charinfo" << json::flat << json::object |
michael@0 | 192 | << "original" << s.original() |
michael@0 | 193 | << "before" << s.before() |
michael@0 | 194 | << "after" << s.after() |
michael@0 | 195 | << json::close |
michael@0 | 196 | << "origin" << s.origin() |
michael@0 | 197 | << "shift" << Position(float(s.getAttr(0, gr_slatShiftX, 0)), |
michael@0 | 198 | float(s.getAttr(0, gr_slatShiftY, 0))) |
michael@0 | 199 | << "advance" << s.advancePos() |
michael@0 | 200 | << "insert" << s.isInsertBefore() |
michael@0 | 201 | << "break" << s.getAttr(&seg, gr_slatBreak, 0); |
michael@0 | 202 | if (s.just() > 0) |
michael@0 | 203 | j << "justification" << s.just(); |
michael@0 | 204 | if (s.getBidiLevel() > 0) |
michael@0 | 205 | j << "bidi" << s.getBidiLevel(); |
michael@0 | 206 | if (!s.isBase()) |
michael@0 | 207 | j << "parent" << json::flat << json::object |
michael@0 | 208 | << "id" << objectid(dslot(&seg, s.attachedTo())) |
michael@0 | 209 | << "level" << s.getAttr(0, gr_slatAttLevel, 0) |
michael@0 | 210 | << "offset" << s.attachOffset() |
michael@0 | 211 | << json::close; |
michael@0 | 212 | j << "user" << json::flat << json::array; |
michael@0 | 213 | for (int n = 0; n!= seg.numAttrs(); ++n) |
michael@0 | 214 | j << s.userAttrs()[n]; |
michael@0 | 215 | j << json::close; |
michael@0 | 216 | if (s.firstChild()) |
michael@0 | 217 | { |
michael@0 | 218 | j << "children" << json::flat << json::array; |
michael@0 | 219 | for (const Slot *c = s.firstChild(); c; c = c->nextSibling()) |
michael@0 | 220 | j << objectid(dslot(&seg, c)); |
michael@0 | 221 | j << json::close; |
michael@0 | 222 | } |
michael@0 | 223 | return j << json::close; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | |
michael@0 | 227 | graphite2::objectid::objectid(const dslot & ds) throw() |
michael@0 | 228 | { |
michael@0 | 229 | const Slot * const p = ds.second; |
michael@0 | 230 | uint32 s = reinterpret_cast<size_t>(p); |
michael@0 | 231 | sprintf(name, "%.4x-%.2x-%.4hx", uint16(s >> 16), uint16(p ? p->userAttrs()[ds.first->silf()->numUser()] : 0), uint16(s)); |
michael@0 | 232 | name[sizeof name-1] = 0; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | graphite2::objectid::objectid(const Segment * const p) throw() |
michael@0 | 236 | { |
michael@0 | 237 | uint32 s = reinterpret_cast<size_t>(p); |
michael@0 | 238 | sprintf(name, "%.4x-%.2x-%.4hx", uint16(s >> 16), 0, uint16(s)); |
michael@0 | 239 | name[sizeof name-1] = 0; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | #endif |