mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-23 15:05:52 +00:00
NEW: add native table support via dynamic protocol detection
This commit is contained in:
1 parent
d88f2ef7c0
commit
861c504c41
1 file changed
+147
-19
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
_CPRTFProducer.j
|
||||
|
||||
Serialize CPAttributedString to a RTF String
|
||||
Serialize CPAttributedString to an RTF String
|
||||
|
||||
Copyright (C) 2014 Daniel Boehringer
|
||||
This file is based on the RTFProducer from GNUStep
|
||||
(which i co-authored with Fred Kiefer in 1999)
|
||||
(which I co-authored with Fred Kiefer in 1999)
|
||||
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
@@ -77,28 +77,30 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark init methods
|
||||
#pragma mark Init methods
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init])
|
||||
{
|
||||
// maintain a dictionary for the used colours
|
||||
// (for rtf-header generation)
|
||||
// Maintain a dictionary for the used colors
|
||||
// (for RTF-header generation)
|
||||
colorDict = [CPMutableDictionary new];
|
||||
|
||||
//maintain a dictionary for the used fonts
|
||||
//(for rtf-header generation)
|
||||
// Maintain a dictionary for the used fonts
|
||||
// (for RTF-header generation)
|
||||
fontDict = [CPMutableDictionary new];
|
||||
|
||||
fgColor = [CPColor blackColor];
|
||||
bgColor= [CPColor whiteColor];
|
||||
bgColor = [CPColor whiteColor];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
// private stuff follows
|
||||
#pragma mark -
|
||||
#pragma mark Header & Formatting Support
|
||||
|
||||
- (CPString)fontTable
|
||||
{
|
||||
if (![fontDict count])
|
||||
@@ -313,7 +315,7 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
break;
|
||||
}
|
||||
|
||||
// write first line indent and left indent
|
||||
// Write first line indent and left indent
|
||||
var twips = _points2twips([paraStyle firstLineHeadIndent]);
|
||||
|
||||
if (twips != 0.0)
|
||||
@@ -354,7 +356,7 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
switch ([tab tabStopType])
|
||||
{
|
||||
case CPLeftTabStopType:
|
||||
// no tabkind emission needed
|
||||
// No tabkind emission needed
|
||||
break;
|
||||
/* case NSRightTabStopType:
|
||||
headerString += @"\\tqr";
|
||||
@@ -376,6 +378,104 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
return headerString;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Duck-Typed Table Engine
|
||||
|
||||
/**
|
||||
* Generates a native RTF table row structure from a conforming table data object.
|
||||
* This helper isolates the native RTF grid structure to ensure decoupling.
|
||||
*
|
||||
* @param tableObject An object implementing the dynamic getters `headers` and `rows`.
|
||||
* @return A raw RTF CPString containing cell bounds, row boundaries, and escaped strings.
|
||||
*/
|
||||
- (CPString)rtfStringForTable:(id)tableObject
|
||||
{
|
||||
var headers = [tableObject headers],
|
||||
rows = [tableObject rows];
|
||||
|
||||
var numCols = [headers count];
|
||||
if (numCols == 0 && [rows count] > 0)
|
||||
{
|
||||
numCols = [[rows objectAtIndex:0] count];
|
||||
}
|
||||
|
||||
if (numCols == 0)
|
||||
{
|
||||
return @"";
|
||||
}
|
||||
|
||||
var rtf = @"";
|
||||
|
||||
// Divide printable horizontal space evenly (approx. 5.5 inches or 7920 Twips total)
|
||||
var totalWidthTwips = 7920,
|
||||
colWidth = Math.floor(totalWidthTwips / numCols);
|
||||
|
||||
var makeRowRTF = function(cells, isHeader) {
|
||||
// \trowd clears existing cell definitions.
|
||||
// \trleft360 applies an indent matching a standard page margin.
|
||||
var rowStr = @"\\trowd\\trgaph108\\trleft360";
|
||||
|
||||
// 1. Declare row boundaries and configure cell border widths
|
||||
var currentX = 360;
|
||||
for (var c = 0; c < numCols; c++) {
|
||||
currentX += colWidth;
|
||||
rowStr += @"\\clbrdrt\\brdrs\\brdrw10\\clbrdrb\\brdrs\\brdrw10";
|
||||
rowStr += @"\\clbrdrl\\brdrs\\brdrw10\\clbrdrr\\brdrs\\brdrw10";
|
||||
rowStr += [CPString stringWithFormat:@"\\cellx%d", currentX];
|
||||
}
|
||||
|
||||
// 2. Escape, format, and serialize cell text content
|
||||
for (var c = 0; c < numCols; c++) {
|
||||
var textVal = @"";
|
||||
if (c < [cells count]) {
|
||||
textVal = [cells objectAtIndex:c];
|
||||
}
|
||||
|
||||
// Core RTF escaping (Backslashes escaped first to preserve subsequent tokens)
|
||||
textVal = textVal.replace(/\\/g, '\\\\');
|
||||
textVal = textVal.replace(/{/g, '\\{');
|
||||
textVal = textVal.replace(/}/g, '\\}');
|
||||
textVal = textVal.replace(/\n/g, '\\par ');
|
||||
|
||||
// Standard visual inline Markdown conversions for cellular parity
|
||||
textVal = textVal.replace(/\*\*([^*]+)\*\*/g, '\\b $1\\b0 ');
|
||||
textVal = textVal.replace(/\*([^*]+)\*/g, '\\i $1\\i0 ');
|
||||
|
||||
rowStr += @"\\intbl ";
|
||||
if (isHeader) {
|
||||
rowStr += @"\\b ";
|
||||
}
|
||||
rowStr += textVal;
|
||||
if (isHeader) {
|
||||
rowStr += @"\\b0 ";
|
||||
}
|
||||
rowStr += @"\\cell ";
|
||||
}
|
||||
|
||||
rowStr += @"\\row\n";
|
||||
return rowStr;
|
||||
};
|
||||
|
||||
// Construct headers
|
||||
if ([headers count] > 0) {
|
||||
rtf += makeRowRTF(headers, YES);
|
||||
}
|
||||
|
||||
// Construct content rows
|
||||
for (var r = 0; r < [rows count]; r++) {
|
||||
var rowData = [rows objectAtIndex:r];
|
||||
rtf += makeRowRTF(rowData, NO);
|
||||
}
|
||||
|
||||
// Explicitly restore standard paragraph attributes to escape table scope
|
||||
rtf += @"\\pard\n";
|
||||
|
||||
return rtf;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Attribute Processing
|
||||
|
||||
- (CPString)runStringForString:(CPString) substring
|
||||
attributes:(CPDictionary) attributes
|
||||
paragraphStart:(BOOL) first
|
||||
@@ -386,6 +486,33 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
attribEnum,
|
||||
currAttrib;
|
||||
|
||||
// --- DUCK-TYPING PROTOCOL DETECTION ---
|
||||
// Safely scan all incoming attributes for an object supporting headers & rows.
|
||||
// If found, completely divert processing to native RTF table construction.
|
||||
var keys = [attributes allKeys],
|
||||
count = [keys count],
|
||||
tableObject = nil;
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var key = [keys objectAtIndex:i],
|
||||
val = [attributes objectForKey:key];
|
||||
|
||||
if (val && typeof val.respondsToSelector === "function" &&
|
||||
[val respondsToSelector:@selector(headers)] &&
|
||||
[val respondsToSelector:@selector(rows)])
|
||||
{
|
||||
tableObject = val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (tableObject)
|
||||
{
|
||||
return [self rtfStringForTable:tableObject];
|
||||
}
|
||||
// --------------------------------------
|
||||
|
||||
if (first)
|
||||
{
|
||||
var paraStyle = [attributes objectForKey:CPParagraphStyleAttributeName];
|
||||
@@ -393,10 +520,10 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
}
|
||||
|
||||
/*
|
||||
* analyze attributes of current run
|
||||
* Analyze attributes of current run
|
||||
*
|
||||
* FIXME: All the character attributes should be output relative to the font
|
||||
* attributes of the paragraph. So if the paragraph has underline on it should
|
||||
* attributes of the paragraph. So if the paragraph has underline on, it should
|
||||
* still be possible to switch it off for some characters, which currently is
|
||||
* not possible.
|
||||
*/
|
||||
@@ -407,7 +534,7 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
if ([currAttrib isEqualToString:CPFontAttributeName])
|
||||
{
|
||||
/*
|
||||
* handle fonts
|
||||
* Handle fonts
|
||||
*/
|
||||
var font,
|
||||
fontName,
|
||||
@@ -418,13 +545,13 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
traits = [[CPFontManager sharedFontManager] traitsOfFont:font];
|
||||
|
||||
/*
|
||||
* font name
|
||||
* Font name
|
||||
*/
|
||||
if (currentFont == nil || ![fontName isEqualToString:[currentFont familyName]])
|
||||
headerString += [self fontToken:fontName];
|
||||
|
||||
/*
|
||||
* font size
|
||||
* Font size
|
||||
*/
|
||||
if (currentFont == nil || [font size] != [currentFont size])
|
||||
{
|
||||
@@ -435,7 +562,7 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
headerString += pString;
|
||||
}
|
||||
/*
|
||||
* font attributes
|
||||
* Font attributes
|
||||
*/
|
||||
if (traits & CPItalicFontMask)
|
||||
{
|
||||
@@ -565,7 +692,7 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
first = YES;
|
||||
|
||||
// FIXME <!> split along newline characters and run as outer loop
|
||||
while (CPMaxRange(currRange) < CPMaxRange(completeRange)) // save all "runs"
|
||||
while (CPMaxRange(currRange) < CPMaxRange(completeRange)) // Save all "runs"
|
||||
{
|
||||
var attributes,
|
||||
substring,
|
||||
@@ -599,7 +726,7 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
docDict = dict;
|
||||
|
||||
/*
|
||||
* do not change order! (esp. body has to be generated first; builds context)
|
||||
* Do not change order! (esp. body has to be generated first; builds context)
|
||||
*/
|
||||
bodyString = [self bodyString];
|
||||
trailerString = [self trailerString];
|
||||
@@ -610,4 +737,5 @@ function _points2twips(a) { return (a) * 20.0; }
|
||||
output += trailerString;
|
||||
return output;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user