1 /* 2 * Copyright 2010, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Michael Pfeiffer 7 */ 8 #include "GPParameterVisitor.h" 9 10 #include <String.h> 11 12 13 const char* kJobMode = "JobMode"; 14 const char* kJob = "Job"; 15 16 const char* kPrintingMode = "PrintingMode"; 17 const char* kColor = "Color"; 18 const char* kBlackAndWhite = "BW"; 19 20 const char* kResolution = "Resolution"; 21 const char* kFakeResolutionKey = ""; 22 23 const char* kPageSize = "PageSize"; 24 25 const char* kChannelBitDepth = "ChannelBitDepth"; 26 27 28 GPParameterVisitor::GPParameterVisitor() 29 : 30 fVariables(NULL), 31 fHasResolutionParameter(false) 32 { 33 } 34 35 36 GPParameterVisitor::~GPParameterVisitor() 37 { 38 } 39 40 41 void 42 GPParameterVisitor::Visit(const stp_printer_t* printer) 43 { 44 // this code is based on Gutenprint printer_options.c 45 const stp_vars_t* defaultVariables = stp_printer_get_defaults(printer); 46 stp_vars_t* variables = stp_vars_create_copy(defaultVariables); 47 fVariables = variables; 48 49 stp_set_string_parameter(variables, kJobMode, kJob); 50 51 stp_parameter_t printingMode; 52 stp_describe_parameter(variables, kPrintingMode, &printingMode); 53 bool isColorPrinter = stp_string_list_is_present(printingMode.bounds.str, 54 kColor) != 0; 55 stp_parameter_description_destroy(&printingMode); 56 57 if (isColorPrinter) 58 stp_set_string_parameter(variables, kPrintingMode, kColor); 59 else 60 stp_set_string_parameter(variables, kPrintingMode, kBlackAndWhite); 61 62 stp_set_string_parameter(variables, kChannelBitDepth, "8"); 63 64 stp_parameter_list_t list = stp_get_parameter_list(variables); 65 int size = stp_parameter_list_count(list); 66 67 for (int i = 0; i < size; i ++) { 68 const stp_parameter_t* parameter = stp_parameter_list_param(list, i); 69 stp_parameter_t description; 70 stp_describe_parameter(fVariables, parameter->name, &description); 71 VisitParameter(list, parameter, &description); 72 stp_parameter_description_destroy(&description); 73 } 74 75 // TODO check if this can really happen 76 if (!fHasResolutionParameter) { 77 AddMissingResolution(); 78 } 79 80 EndVisit(); 81 82 stp_parameter_list_destroy(list); 83 stp_vars_destroy(variables); 84 fVariables = NULL; 85 } 86 87 88 void 89 GPParameterVisitor::AddMissingResolution() 90 { 91 // some printer definitions don't have resolution parameter 92 // however "libprint" needs to know it for rasterization 93 94 // TODO find out if other parameters influence the resolution 95 // e.g. color vs black and white 96 int x, y; 97 stp_describe_resolution(fVariables, &x, &y); 98 99 BeginParameter(kResolution, "Resolution", STP_PARAMETER_CLASS_FEATURE); 100 DefaultStringParameter(kResolution, kFakeResolutionKey); 101 StringParameterSize(kResolution, 1); 102 103 if (x <= 0 || y <= 0) { 104 // TODO decide if more resolutions (150, 600) should be possible 105 x = 300; 106 y = 300; 107 } 108 109 BString displayName; 110 if (x != y) 111 displayName << x << " x " << y << " DPI"; 112 else 113 displayName << x << " DPI"; 114 115 ResolutionParameter(kResolution, kFakeResolutionKey, displayName.String(), 116 x, y); 117 118 EndParameter(kResolution); 119 } 120 121 122 void 123 GPParameterVisitor::VisitParameter(stp_parameter_list_t list, 124 const stp_parameter_t* parameter, stp_parameter_t* description) 125 { 126 // TODO decide which parameters should be revealed to user 127 // e.g. up to STP_PARAMETER_LEVEL_ADVANCED4; 128 const stp_parameter_level_t kMaxLevel = STP_PARAMETER_LEVEL_BASIC; 129 stp_parameter_class_t parameterClass = parameter->p_class; 130 if (parameter->read_only || 131 (parameter->p_level > kMaxLevel 132 && strcmp(parameter->name, kResolution) != 0) 133 || parameterClass != STP_PARAMETER_CLASS_OUTPUT 134 && parameterClass != STP_PARAMETER_CLASS_CORE 135 && parameterClass != STP_PARAMETER_CLASS_FEATURE) 136 return; 137 138 if (!description->is_active) 139 return; 140 141 switch (description->p_type) { 142 case STP_PARAMETER_TYPE_STRING_LIST: 143 if (!BeginParameter(description->name, description->text, 144 parameterClass)) 145 return; 146 VisitStringList(description); 147 EndParameter(description->name); 148 break; 149 150 case STP_PARAMETER_TYPE_BOOLEAN: 151 break; 152 153 case STP_PARAMETER_TYPE_DOUBLE: 154 break; 155 156 case STP_PARAMETER_TYPE_INT: 157 break; 158 159 case STP_PARAMETER_TYPE_DIMENSION: 160 break; 161 162 default: 163 break; 164 } 165 166 } 167 168 169 void 170 GPParameterVisitor::VisitStringList(stp_parameter_t* parameter) 171 { 172 stp_string_list_t* list = parameter->bounds.str; 173 int count = stp_string_list_count(list); 174 if (count <= 0) 175 return; 176 177 const char* name = parameter->name; 178 if (parameter->is_mandatory) 179 DefaultStringParameter(name, parameter->deflt.str); 180 else 181 DefaultStringParameter(name, NULL); 182 183 StringParameterSize(name, count); 184 185 for (int i = 0; i < count; i ++) { 186 const stp_param_string_t* entry = stp_string_list_param(list, i); 187 const char* key = entry->name; 188 const char* displayName = entry->text; 189 if (strcmp(name, kResolution) == 0) { 190 stp_set_string_parameter(fVariables, kResolution, key); 191 192 int x, y; 193 stp_describe_resolution(fVariables, &x, &y); 194 195 ResolutionParameter(name, key, displayName, x, y); 196 197 fHasResolutionParameter = true; 198 } else if (strcmp(name, kPageSize) == 0) { 199 stp_set_string_parameter(fVariables, kPageSize, key); 200 201 int width; 202 int height; 203 stp_get_media_size(fVariables, &width, &height); 204 BSize pageSize(width, height); 205 206 int left, right, top, bottom; 207 stp_get_imageable_area(fVariables, &left, &right, &bottom, &top); 208 BRect imageableArea(left, top, right, bottom); 209 210 PageSizeParameter(name, key, displayName, pageSize, imageableArea); 211 } else { 212 StringParameter(name, key, displayName); 213 } 214 } 215 } 216