|
| 1 | +/* Copyright (C) 2018 LiveCode Ltd. |
| 2 | +
|
| 3 | +This file is part of LiveCode. |
| 4 | +
|
| 5 | +LiveCode is free software; you can redistribute it and/or modify it under |
| 6 | +the terms of the GNU General Public License v3 as published by the Free |
| 7 | +Software Foundation. |
| 8 | +
|
| 9 | +LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | +for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 16 | + |
| 17 | +#include "revpdfprinter.h" |
| 18 | + |
| 19 | +#include <ft2build.h> |
| 20 | +#include FT_FREETYPE_H |
| 21 | +#include FT_SIZES_H |
| 22 | + |
| 23 | +#include <cairo-ft.h> |
| 24 | +#include <time.h> |
| 25 | + |
| 26 | +static cairo_user_data_key_t s_ft_face_key; |
| 27 | + |
| 28 | +struct ft_face_user_data_t |
| 29 | +{ |
| 30 | + FT_Face face; |
| 31 | + FT_Size size; |
| 32 | + FT_Size old_size; |
| 33 | +}; |
| 34 | + |
| 35 | +static void free_ft_face_func(void* p_face) |
| 36 | +{ |
| 37 | + ft_face_user_data_t *t_data; |
| 38 | + t_data = (ft_face_user_data_t*)p_face; |
| 39 | + |
| 40 | + if (t_data->old_size != nil) |
| 41 | + FT_Activate_Size(t_data->old_size); |
| 42 | + |
| 43 | + if (t_data->size != nil) |
| 44 | + FT_Done_Size(t_data->size); |
| 45 | + |
| 46 | + if (t_data->face != nil) |
| 47 | + FT_Done_Face(t_data->face); |
| 48 | + |
| 49 | + MCMemoryDelete(t_data); |
| 50 | +} |
| 51 | + |
| 52 | +bool MCPDFPrintingDevice::create_cairo_font_from_custom_printer_font(const MCCustomPrinterFont &p_cp_font, cairo_font_face_t* &r_cairo_font) |
| 53 | +{ |
| 54 | + bool t_success; |
| 55 | + t_success = true; |
| 56 | + |
| 57 | + ft_face_user_data_t *t_user_data; |
| 58 | + t_user_data = nil; |
| 59 | + if (t_success) |
| 60 | + t_success = MCMemoryNew(t_user_data); |
| 61 | + |
| 62 | + // Get the FreeType face |
| 63 | + if (t_success) |
| 64 | + { |
| 65 | + t_user_data->face = (FT_Face)p_cp_font.handle; |
| 66 | + t_success = t_user_data->face != nil; |
| 67 | + } |
| 68 | + |
| 69 | + // Retain a reference to the FreeType face |
| 70 | + if (t_success) |
| 71 | + FT_Reference_Face(t_user_data->face); |
| 72 | + |
| 73 | + // Store the old face size so it can be restored once cairo is finished |
| 74 | + if (t_success) |
| 75 | + { |
| 76 | + t_user_data->old_size = t_user_data->face->size; |
| 77 | + t_success = FT_Err_Ok == FT_New_Size(t_user_data->face, &t_user_data->size); |
| 78 | + } |
| 79 | + if (t_success) |
| 80 | + t_success = FT_Err_Ok == FT_Activate_Size(t_user_data->size); |
| 81 | + if (t_success) |
| 82 | + t_success = FT_Err_Ok == FT_Set_Char_Size(t_user_data->face, p_cp_font.size * 64, 0, 0, 0); |
| 83 | + |
| 84 | + bool t_free_user_data; |
| 85 | + t_free_user_data = true; |
| 86 | + |
| 87 | + // We can now create the cairo_font |
| 88 | + cairo_font_face_t *t_font; |
| 89 | + t_font = nil; |
| 90 | + if (t_success) |
| 91 | + { |
| 92 | + t_font = cairo_ft_font_face_create_for_ft_face(t_user_data->face, 0); |
| 93 | + t_success = t_font != nil && (m_status = cairo_font_face_status(t_font)) == CAIRO_STATUS_SUCCESS; |
| 94 | + } |
| 95 | + if (t_success) |
| 96 | + { |
| 97 | + // Finally, add a piece of user data to the font face. This will ensure |
| 98 | + // the FreeType font is restored to its original state and released when |
| 99 | + // the cairo font is destroyed. |
| 100 | + |
| 101 | + if (cairo_font_face_get_user_data(t_font, &s_ft_face_key) == nil) |
| 102 | + { |
| 103 | + m_status = cairo_font_face_set_user_data(t_font, &s_ft_face_key, t_user_data, free_ft_face_func); |
| 104 | + t_success = m_status == CAIRO_STATUS_SUCCESS; |
| 105 | + if (t_success) |
| 106 | + t_free_user_data = false; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (t_free_user_data && t_user_data != nil) |
| 111 | + free_ft_face_func(t_user_data); |
| 112 | + |
| 113 | + if (t_success) |
| 114 | + r_cairo_font = t_font; |
| 115 | + else |
| 116 | + { |
| 117 | + if (t_font != nil) |
| 118 | + cairo_font_face_destroy(t_font); |
| 119 | + } |
| 120 | + |
| 121 | + return t_success; |
| 122 | +} |
| 123 | + |
| 124 | +bool MCPDFPrintingDevice::set_cairo_pdf_datetime_to_now(cairo_pdf_datetime_t &r_datetime) |
| 125 | +{ |
| 126 | + time_t t_time; |
| 127 | + tm * t_datetime; |
| 128 | + |
| 129 | + time ( &t_time ); |
| 130 | + |
| 131 | + t_datetime = gmtime ( &t_time ); |
| 132 | + |
| 133 | + r_datetime.year = t_datetime->tm_year + 1900; |
| 134 | + r_datetime.month = t_datetime->tm_mon + 1; |
| 135 | + r_datetime.day = t_datetime->tm_mday; |
| 136 | + r_datetime.hour = t_datetime->tm_hour; |
| 137 | + r_datetime.minute = t_datetime->tm_min; |
| 138 | + r_datetime.second = t_datetime->tm_sec; |
| 139 | + |
| 140 | + r_datetime.utc_minute_offset = 0; |
| 141 | + |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +// SN-2014-12-23: [[ Bug 14278 ]] Added system-specific to get the path. |
| 146 | +bool MCPDFPrintingDevice::get_filename(const char* p_utf8_path, char *& r_system_path) |
| 147 | +{ |
| 148 | + return MCCStringClone(p_utf8_path, r_system_path); |
| 149 | +} |
0 commit comments